Fix bug #15175 with cursor on boxed characters from display tables.
[emacs.git] / src / xdisp.c
blobfecf22af1e903e2f91195d4003a0ca6b1290e962
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 faces.
3916 For strings from wrap-prefix and line-prefix properties,
3917 use the default face, possibly remapped via
3918 Vface_remapping_alist. */
3919 base_face_id = it->string_from_prefix_prop_p
3920 ? (!NILP (Vface_remapping_alist)
3921 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3922 : DEFAULT_FACE_ID)
3923 : underlying_face_id (it);
3926 new_face_id = face_at_string_position (it->w,
3927 it->string,
3928 IT_STRING_CHARPOS (*it),
3929 bufpos,
3930 it->region_beg_charpos,
3931 it->region_end_charpos,
3932 &next_stop,
3933 base_face_id, 0);
3935 /* Is this a start of a run of characters with box? Caveat:
3936 this can be called for a freshly allocated iterator; face_id
3937 is -1 is this case. We know that the new face will not
3938 change until the next check pos, i.e. if the new face has a
3939 box, all characters up to that position will have a
3940 box. But, as usual, we don't know whether that position
3941 is really the end. */
3942 if (new_face_id != it->face_id)
3944 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3945 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3947 /* If new face has a box but old face hasn't, this is the
3948 start of a run of characters with box, i.e. it has a
3949 shadow on the left side. */
3950 it->start_of_box_run_p
3951 = new_face->box && (old_face == NULL || !old_face->box);
3952 it->face_box_p = new_face->box != FACE_NO_BOX;
3956 it->face_id = new_face_id;
3957 return HANDLED_NORMALLY;
3961 /* Return the ID of the face ``underlying'' IT's current position,
3962 which is in a string. If the iterator is associated with a
3963 buffer, return the face at IT's current buffer position.
3964 Otherwise, use the iterator's base_face_id. */
3966 static int
3967 underlying_face_id (struct it *it)
3969 int face_id = it->base_face_id, i;
3971 eassert (STRINGP (it->string));
3973 for (i = it->sp - 1; i >= 0; --i)
3974 if (NILP (it->stack[i].string))
3975 face_id = it->stack[i].face_id;
3977 return face_id;
3981 /* Compute the face one character before or after the current position
3982 of IT, in the visual order. BEFORE_P non-zero means get the face
3983 in front (to the left in L2R paragraphs, to the right in R2L
3984 paragraphs) of IT's screen position. Value is the ID of the face. */
3986 static int
3987 face_before_or_after_it_pos (struct it *it, int before_p)
3989 int face_id, limit;
3990 ptrdiff_t next_check_charpos;
3991 struct it it_copy;
3992 void *it_copy_data = NULL;
3994 eassert (it->s == NULL);
3996 if (STRINGP (it->string))
3998 ptrdiff_t bufpos, charpos;
3999 int base_face_id;
4001 /* No face change past the end of the string (for the case
4002 we are padding with spaces). No face change before the
4003 string start. */
4004 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4005 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4006 return it->face_id;
4008 if (!it->bidi_p)
4010 /* Set charpos to the position before or after IT's current
4011 position, in the logical order, which in the non-bidi
4012 case is the same as the visual order. */
4013 if (before_p)
4014 charpos = IT_STRING_CHARPOS (*it) - 1;
4015 else if (it->what == IT_COMPOSITION)
4016 /* For composition, we must check the character after the
4017 composition. */
4018 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4019 else
4020 charpos = IT_STRING_CHARPOS (*it) + 1;
4022 else
4024 if (before_p)
4026 /* With bidi iteration, the character before the current
4027 in the visual order cannot be found by simple
4028 iteration, because "reverse" reordering is not
4029 supported. Instead, we need to use the move_it_*
4030 family of functions. */
4031 /* Ignore face changes before the first visible
4032 character on this display line. */
4033 if (it->current_x <= it->first_visible_x)
4034 return it->face_id;
4035 SAVE_IT (it_copy, *it, it_copy_data);
4036 /* Implementation note: Since move_it_in_display_line
4037 works in the iterator geometry, and thinks the first
4038 character is always the leftmost, even in R2L lines,
4039 we don't need to distinguish between the R2L and L2R
4040 cases here. */
4041 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4042 it_copy.current_x - 1, MOVE_TO_X);
4043 charpos = IT_STRING_CHARPOS (it_copy);
4044 RESTORE_IT (it, it, it_copy_data);
4046 else
4048 /* Set charpos to the string position of the character
4049 that comes after IT's current position in the visual
4050 order. */
4051 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4053 it_copy = *it;
4054 while (n--)
4055 bidi_move_to_visually_next (&it_copy.bidi_it);
4057 charpos = it_copy.bidi_it.charpos;
4060 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4062 if (it->current.overlay_string_index >= 0)
4063 bufpos = IT_CHARPOS (*it);
4064 else
4065 bufpos = 0;
4067 base_face_id = underlying_face_id (it);
4069 /* Get the face for ASCII, or unibyte. */
4070 face_id = face_at_string_position (it->w,
4071 it->string,
4072 charpos,
4073 bufpos,
4074 it->region_beg_charpos,
4075 it->region_end_charpos,
4076 &next_check_charpos,
4077 base_face_id, 0);
4079 /* Correct the face for charsets different from ASCII. Do it
4080 for the multibyte case only. The face returned above is
4081 suitable for unibyte text if IT->string is unibyte. */
4082 if (STRING_MULTIBYTE (it->string))
4084 struct text_pos pos1 = string_pos (charpos, it->string);
4085 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4086 int c, len;
4087 struct face *face = FACE_FROM_ID (it->f, face_id);
4089 c = string_char_and_length (p, &len);
4090 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4093 else
4095 struct text_pos pos;
4097 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4098 || (IT_CHARPOS (*it) <= BEGV && before_p))
4099 return it->face_id;
4101 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4102 pos = it->current.pos;
4104 if (!it->bidi_p)
4106 if (before_p)
4107 DEC_TEXT_POS (pos, it->multibyte_p);
4108 else
4110 if (it->what == IT_COMPOSITION)
4112 /* For composition, we must check the position after
4113 the composition. */
4114 pos.charpos += it->cmp_it.nchars;
4115 pos.bytepos += it->len;
4117 else
4118 INC_TEXT_POS (pos, it->multibyte_p);
4121 else
4123 if (before_p)
4125 /* With bidi iteration, the character before the current
4126 in the visual order cannot be found by simple
4127 iteration, because "reverse" reordering is not
4128 supported. Instead, we need to use the move_it_*
4129 family of functions. */
4130 /* Ignore face changes before the first visible
4131 character on this display line. */
4132 if (it->current_x <= it->first_visible_x)
4133 return it->face_id;
4134 SAVE_IT (it_copy, *it, it_copy_data);
4135 /* Implementation note: Since move_it_in_display_line
4136 works in the iterator geometry, and thinks the first
4137 character is always the leftmost, even in R2L lines,
4138 we don't need to distinguish between the R2L and L2R
4139 cases here. */
4140 move_it_in_display_line (&it_copy, ZV,
4141 it_copy.current_x - 1, MOVE_TO_X);
4142 pos = it_copy.current.pos;
4143 RESTORE_IT (it, it, it_copy_data);
4145 else
4147 /* Set charpos to the buffer position of the character
4148 that comes after IT's current position in the visual
4149 order. */
4150 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4152 it_copy = *it;
4153 while (n--)
4154 bidi_move_to_visually_next (&it_copy.bidi_it);
4156 SET_TEXT_POS (pos,
4157 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4160 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4162 /* Determine face for CHARSET_ASCII, or unibyte. */
4163 face_id = face_at_buffer_position (it->w,
4164 CHARPOS (pos),
4165 it->region_beg_charpos,
4166 it->region_end_charpos,
4167 &next_check_charpos,
4168 limit, 0, -1);
4170 /* Correct the face for charsets different from ASCII. Do it
4171 for the multibyte case only. The face returned above is
4172 suitable for unibyte text if current_buffer is unibyte. */
4173 if (it->multibyte_p)
4175 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4176 struct face *face = FACE_FROM_ID (it->f, face_id);
4177 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4181 return face_id;
4186 /***********************************************************************
4187 Invisible text
4188 ***********************************************************************/
4190 /* Set up iterator IT from invisible properties at its current
4191 position. Called from handle_stop. */
4193 static enum prop_handled
4194 handle_invisible_prop (struct it *it)
4196 enum prop_handled handled = HANDLED_NORMALLY;
4197 int invis_p;
4198 Lisp_Object prop;
4200 if (STRINGP (it->string))
4202 Lisp_Object end_charpos, limit, charpos;
4204 /* Get the value of the invisible text property at the
4205 current position. Value will be nil if there is no such
4206 property. */
4207 charpos = make_number (IT_STRING_CHARPOS (*it));
4208 prop = Fget_text_property (charpos, Qinvisible, it->string);
4209 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4211 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4213 /* Record whether we have to display an ellipsis for the
4214 invisible text. */
4215 int display_ellipsis_p = (invis_p == 2);
4216 ptrdiff_t len, endpos;
4218 handled = HANDLED_RECOMPUTE_PROPS;
4220 /* Get the position at which the next visible text can be
4221 found in IT->string, if any. */
4222 endpos = len = SCHARS (it->string);
4223 XSETINT (limit, len);
4226 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4227 it->string, limit);
4228 if (INTEGERP (end_charpos))
4230 endpos = XFASTINT (end_charpos);
4231 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4232 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4233 if (invis_p == 2)
4234 display_ellipsis_p = 1;
4237 while (invis_p && endpos < len);
4239 if (display_ellipsis_p)
4240 it->ellipsis_p = 1;
4242 if (endpos < len)
4244 /* Text at END_CHARPOS is visible. Move IT there. */
4245 struct text_pos old;
4246 ptrdiff_t oldpos;
4248 old = it->current.string_pos;
4249 oldpos = CHARPOS (old);
4250 if (it->bidi_p)
4252 if (it->bidi_it.first_elt
4253 && it->bidi_it.charpos < SCHARS (it->string))
4254 bidi_paragraph_init (it->paragraph_embedding,
4255 &it->bidi_it, 1);
4256 /* Bidi-iterate out of the invisible text. */
4259 bidi_move_to_visually_next (&it->bidi_it);
4261 while (oldpos <= it->bidi_it.charpos
4262 && it->bidi_it.charpos < endpos);
4264 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4265 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4266 if (IT_CHARPOS (*it) >= endpos)
4267 it->prev_stop = endpos;
4269 else
4271 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4272 compute_string_pos (&it->current.string_pos, old, it->string);
4275 else
4277 /* The rest of the string is invisible. If this is an
4278 overlay string, proceed with the next overlay string
4279 or whatever comes and return a character from there. */
4280 if (it->current.overlay_string_index >= 0
4281 && !display_ellipsis_p)
4283 next_overlay_string (it);
4284 /* Don't check for overlay strings when we just
4285 finished processing them. */
4286 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4288 else
4290 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4291 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4296 else
4298 ptrdiff_t newpos, next_stop, start_charpos, tem;
4299 Lisp_Object pos, overlay;
4301 /* First of all, is there invisible text at this position? */
4302 tem = start_charpos = IT_CHARPOS (*it);
4303 pos = make_number (tem);
4304 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4305 &overlay);
4306 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4308 /* If we are on invisible text, skip over it. */
4309 if (invis_p && start_charpos < it->end_charpos)
4311 /* Record whether we have to display an ellipsis for the
4312 invisible text. */
4313 int display_ellipsis_p = invis_p == 2;
4315 handled = HANDLED_RECOMPUTE_PROPS;
4317 /* Loop skipping over invisible text. The loop is left at
4318 ZV or with IT on the first char being visible again. */
4321 /* Try to skip some invisible text. Return value is the
4322 position reached which can be equal to where we start
4323 if there is nothing invisible there. This skips both
4324 over invisible text properties and overlays with
4325 invisible property. */
4326 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4328 /* If we skipped nothing at all we weren't at invisible
4329 text in the first place. If everything to the end of
4330 the buffer was skipped, end the loop. */
4331 if (newpos == tem || newpos >= ZV)
4332 invis_p = 0;
4333 else
4335 /* We skipped some characters but not necessarily
4336 all there are. Check if we ended up on visible
4337 text. Fget_char_property returns the property of
4338 the char before the given position, i.e. if we
4339 get invis_p = 0, this means that the char at
4340 newpos is visible. */
4341 pos = make_number (newpos);
4342 prop = Fget_char_property (pos, Qinvisible, it->window);
4343 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4346 /* If we ended up on invisible text, proceed to
4347 skip starting with next_stop. */
4348 if (invis_p)
4349 tem = next_stop;
4351 /* If there are adjacent invisible texts, don't lose the
4352 second one's ellipsis. */
4353 if (invis_p == 2)
4354 display_ellipsis_p = 1;
4356 while (invis_p);
4358 /* The position newpos is now either ZV or on visible text. */
4359 if (it->bidi_p)
4361 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4362 int on_newline =
4363 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4364 int after_newline =
4365 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4367 /* If the invisible text ends on a newline or on a
4368 character after a newline, we can avoid the costly,
4369 character by character, bidi iteration to NEWPOS, and
4370 instead simply reseat the iterator there. That's
4371 because all bidi reordering information is tossed at
4372 the newline. This is a big win for modes that hide
4373 complete lines, like Outline, Org, etc. */
4374 if (on_newline || after_newline)
4376 struct text_pos tpos;
4377 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4379 SET_TEXT_POS (tpos, newpos, bpos);
4380 reseat_1 (it, tpos, 0);
4381 /* If we reseat on a newline/ZV, we need to prep the
4382 bidi iterator for advancing to the next character
4383 after the newline/EOB, keeping the current paragraph
4384 direction (so that PRODUCE_GLYPHS does TRT wrt
4385 prepending/appending glyphs to a glyph row). */
4386 if (on_newline)
4388 it->bidi_it.first_elt = 0;
4389 it->bidi_it.paragraph_dir = pdir;
4390 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4391 it->bidi_it.nchars = 1;
4392 it->bidi_it.ch_len = 1;
4395 else /* Must use the slow method. */
4397 /* With bidi iteration, the region of invisible text
4398 could start and/or end in the middle of a
4399 non-base embedding level. Therefore, we need to
4400 skip invisible text using the bidi iterator,
4401 starting at IT's current position, until we find
4402 ourselves outside of the invisible text.
4403 Skipping invisible text _after_ bidi iteration
4404 avoids affecting the visual order of the
4405 displayed text when invisible properties are
4406 added or removed. */
4407 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4409 /* If we were `reseat'ed to a new paragraph,
4410 determine the paragraph base direction. We
4411 need to do it now because
4412 next_element_from_buffer may not have a
4413 chance to do it, if we are going to skip any
4414 text at the beginning, which resets the
4415 FIRST_ELT flag. */
4416 bidi_paragraph_init (it->paragraph_embedding,
4417 &it->bidi_it, 1);
4421 bidi_move_to_visually_next (&it->bidi_it);
4423 while (it->stop_charpos <= it->bidi_it.charpos
4424 && it->bidi_it.charpos < newpos);
4425 IT_CHARPOS (*it) = it->bidi_it.charpos;
4426 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4427 /* If we overstepped NEWPOS, record its position in
4428 the iterator, so that we skip invisible text if
4429 later the bidi iteration lands us in the
4430 invisible region again. */
4431 if (IT_CHARPOS (*it) >= newpos)
4432 it->prev_stop = newpos;
4435 else
4437 IT_CHARPOS (*it) = newpos;
4438 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4441 /* If there are before-strings at the start of invisible
4442 text, and the text is invisible because of a text
4443 property, arrange to show before-strings because 20.x did
4444 it that way. (If the text is invisible because of an
4445 overlay property instead of a text property, this is
4446 already handled in the overlay code.) */
4447 if (NILP (overlay)
4448 && get_overlay_strings (it, it->stop_charpos))
4450 handled = HANDLED_RECOMPUTE_PROPS;
4451 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4453 else if (display_ellipsis_p)
4455 /* Make sure that the glyphs of the ellipsis will get
4456 correct `charpos' values. If we would not update
4457 it->position here, the glyphs would belong to the
4458 last visible character _before_ the invisible
4459 text, which confuses `set_cursor_from_row'.
4461 We use the last invisible position instead of the
4462 first because this way the cursor is always drawn on
4463 the first "." of the ellipsis, whenever PT is inside
4464 the invisible text. Otherwise the cursor would be
4465 placed _after_ the ellipsis when the point is after the
4466 first invisible character. */
4467 if (!STRINGP (it->object))
4469 it->position.charpos = newpos - 1;
4470 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4472 it->ellipsis_p = 1;
4473 /* Let the ellipsis display before
4474 considering any properties of the following char.
4475 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4476 handled = HANDLED_RETURN;
4481 return handled;
4485 /* Make iterator IT return `...' next.
4486 Replaces LEN characters from buffer. */
4488 static void
4489 setup_for_ellipsis (struct it *it, int len)
4491 /* Use the display table definition for `...'. Invalid glyphs
4492 will be handled by the method returning elements from dpvec. */
4493 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4495 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4496 it->dpvec = v->contents;
4497 it->dpend = v->contents + v->header.size;
4499 else
4501 /* Default `...'. */
4502 it->dpvec = default_invis_vector;
4503 it->dpend = default_invis_vector + 3;
4506 it->dpvec_char_len = len;
4507 it->current.dpvec_index = 0;
4508 it->dpvec_face_id = -1;
4510 /* Remember the current face id in case glyphs specify faces.
4511 IT's face is restored in set_iterator_to_next.
4512 saved_face_id was set to preceding char's face in handle_stop. */
4513 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4514 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4516 it->method = GET_FROM_DISPLAY_VECTOR;
4517 it->ellipsis_p = 1;
4522 /***********************************************************************
4523 'display' property
4524 ***********************************************************************/
4526 /* Set up iterator IT from `display' property at its current position.
4527 Called from handle_stop.
4528 We return HANDLED_RETURN if some part of the display property
4529 overrides the display of the buffer text itself.
4530 Otherwise we return HANDLED_NORMALLY. */
4532 static enum prop_handled
4533 handle_display_prop (struct it *it)
4535 Lisp_Object propval, object, overlay;
4536 struct text_pos *position;
4537 ptrdiff_t bufpos;
4538 /* Nonzero if some property replaces the display of the text itself. */
4539 int display_replaced_p = 0;
4541 if (STRINGP (it->string))
4543 object = it->string;
4544 position = &it->current.string_pos;
4545 bufpos = CHARPOS (it->current.pos);
4547 else
4549 XSETWINDOW (object, it->w);
4550 position = &it->current.pos;
4551 bufpos = CHARPOS (*position);
4554 /* Reset those iterator values set from display property values. */
4555 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4556 it->space_width = Qnil;
4557 it->font_height = Qnil;
4558 it->voffset = 0;
4560 /* We don't support recursive `display' properties, i.e. string
4561 values that have a string `display' property, that have a string
4562 `display' property etc. */
4563 if (!it->string_from_display_prop_p)
4564 it->area = TEXT_AREA;
4566 propval = get_char_property_and_overlay (make_number (position->charpos),
4567 Qdisplay, object, &overlay);
4568 if (NILP (propval))
4569 return HANDLED_NORMALLY;
4570 /* Now OVERLAY is the overlay that gave us this property, or nil
4571 if it was a text property. */
4573 if (!STRINGP (it->string))
4574 object = it->w->contents;
4576 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4577 position, bufpos,
4578 FRAME_WINDOW_P (it->f));
4580 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4583 /* Subroutine of handle_display_prop. Returns non-zero if the display
4584 specification in SPEC is a replacing specification, i.e. it would
4585 replace the text covered by `display' property with something else,
4586 such as an image or a display string. If SPEC includes any kind or
4587 `(space ...) specification, the value is 2; this is used by
4588 compute_display_string_pos, which see.
4590 See handle_single_display_spec for documentation of arguments.
4591 frame_window_p is non-zero if the window being redisplayed is on a
4592 GUI frame; this argument is used only if IT is NULL, see below.
4594 IT can be NULL, if this is called by the bidi reordering code
4595 through compute_display_string_pos, which see. In that case, this
4596 function only examines SPEC, but does not otherwise "handle" it, in
4597 the sense that it doesn't set up members of IT from the display
4598 spec. */
4599 static int
4600 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4601 Lisp_Object overlay, struct text_pos *position,
4602 ptrdiff_t bufpos, int frame_window_p)
4604 int replacing_p = 0;
4605 int rv;
4607 if (CONSP (spec)
4608 /* Simple specifications. */
4609 && !EQ (XCAR (spec), Qimage)
4610 && !EQ (XCAR (spec), Qspace)
4611 && !EQ (XCAR (spec), Qwhen)
4612 && !EQ (XCAR (spec), Qslice)
4613 && !EQ (XCAR (spec), Qspace_width)
4614 && !EQ (XCAR (spec), Qheight)
4615 && !EQ (XCAR (spec), Qraise)
4616 /* Marginal area specifications. */
4617 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4618 && !EQ (XCAR (spec), Qleft_fringe)
4619 && !EQ (XCAR (spec), Qright_fringe)
4620 && !NILP (XCAR (spec)))
4622 for (; CONSP (spec); spec = XCDR (spec))
4624 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4625 overlay, position, bufpos,
4626 replacing_p, frame_window_p)))
4628 replacing_p = rv;
4629 /* If some text in a string is replaced, `position' no
4630 longer points to the position of `object'. */
4631 if (!it || STRINGP (object))
4632 break;
4636 else if (VECTORP (spec))
4638 ptrdiff_t i;
4639 for (i = 0; i < ASIZE (spec); ++i)
4640 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4641 overlay, position, bufpos,
4642 replacing_p, frame_window_p)))
4644 replacing_p = rv;
4645 /* If some text in a string is replaced, `position' no
4646 longer points to the position of `object'. */
4647 if (!it || STRINGP (object))
4648 break;
4651 else
4653 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4654 position, bufpos, 0,
4655 frame_window_p)))
4656 replacing_p = rv;
4659 return replacing_p;
4662 /* Value is the position of the end of the `display' property starting
4663 at START_POS in OBJECT. */
4665 static struct text_pos
4666 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4668 Lisp_Object end;
4669 struct text_pos end_pos;
4671 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4672 Qdisplay, object, Qnil);
4673 CHARPOS (end_pos) = XFASTINT (end);
4674 if (STRINGP (object))
4675 compute_string_pos (&end_pos, start_pos, it->string);
4676 else
4677 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4679 return end_pos;
4683 /* Set up IT from a single `display' property specification SPEC. OBJECT
4684 is the object in which the `display' property was found. *POSITION
4685 is the position in OBJECT at which the `display' property was found.
4686 BUFPOS is the buffer position of OBJECT (different from POSITION if
4687 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4688 previously saw a display specification which already replaced text
4689 display with something else, for example an image; we ignore such
4690 properties after the first one has been processed.
4692 OVERLAY is the overlay this `display' property came from,
4693 or nil if it was a text property.
4695 If SPEC is a `space' or `image' specification, and in some other
4696 cases too, set *POSITION to the position where the `display'
4697 property ends.
4699 If IT is NULL, only examine the property specification in SPEC, but
4700 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4701 is intended to be displayed in a window on a GUI frame.
4703 Value is non-zero if something was found which replaces the display
4704 of buffer or string text. */
4706 static int
4707 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4708 Lisp_Object overlay, struct text_pos *position,
4709 ptrdiff_t bufpos, int display_replaced_p,
4710 int frame_window_p)
4712 Lisp_Object form;
4713 Lisp_Object location, value;
4714 struct text_pos start_pos = *position;
4715 int valid_p;
4717 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4718 If the result is non-nil, use VALUE instead of SPEC. */
4719 form = Qt;
4720 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4722 spec = XCDR (spec);
4723 if (!CONSP (spec))
4724 return 0;
4725 form = XCAR (spec);
4726 spec = XCDR (spec);
4729 if (!NILP (form) && !EQ (form, Qt))
4731 ptrdiff_t count = SPECPDL_INDEX ();
4732 struct gcpro gcpro1;
4734 /* Bind `object' to the object having the `display' property, a
4735 buffer or string. Bind `position' to the position in the
4736 object where the property was found, and `buffer-position'
4737 to the current position in the buffer. */
4739 if (NILP (object))
4740 XSETBUFFER (object, current_buffer);
4741 specbind (Qobject, object);
4742 specbind (Qposition, make_number (CHARPOS (*position)));
4743 specbind (Qbuffer_position, make_number (bufpos));
4744 GCPRO1 (form);
4745 form = safe_eval (form);
4746 UNGCPRO;
4747 unbind_to (count, Qnil);
4750 if (NILP (form))
4751 return 0;
4753 /* Handle `(height HEIGHT)' specifications. */
4754 if (CONSP (spec)
4755 && EQ (XCAR (spec), Qheight)
4756 && CONSP (XCDR (spec)))
4758 if (it)
4760 if (!FRAME_WINDOW_P (it->f))
4761 return 0;
4763 it->font_height = XCAR (XCDR (spec));
4764 if (!NILP (it->font_height))
4766 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4767 int new_height = -1;
4769 if (CONSP (it->font_height)
4770 && (EQ (XCAR (it->font_height), Qplus)
4771 || EQ (XCAR (it->font_height), Qminus))
4772 && CONSP (XCDR (it->font_height))
4773 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4775 /* `(+ N)' or `(- N)' where N is an integer. */
4776 int steps = XINT (XCAR (XCDR (it->font_height)));
4777 if (EQ (XCAR (it->font_height), Qplus))
4778 steps = - steps;
4779 it->face_id = smaller_face (it->f, it->face_id, steps);
4781 else if (FUNCTIONP (it->font_height))
4783 /* Call function with current height as argument.
4784 Value is the new height. */
4785 Lisp_Object height;
4786 height = safe_call1 (it->font_height,
4787 face->lface[LFACE_HEIGHT_INDEX]);
4788 if (NUMBERP (height))
4789 new_height = XFLOATINT (height);
4791 else if (NUMBERP (it->font_height))
4793 /* Value is a multiple of the canonical char height. */
4794 struct face *f;
4796 f = FACE_FROM_ID (it->f,
4797 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4798 new_height = (XFLOATINT (it->font_height)
4799 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4801 else
4803 /* Evaluate IT->font_height with `height' bound to the
4804 current specified height to get the new height. */
4805 ptrdiff_t count = SPECPDL_INDEX ();
4807 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4808 value = safe_eval (it->font_height);
4809 unbind_to (count, Qnil);
4811 if (NUMBERP (value))
4812 new_height = XFLOATINT (value);
4815 if (new_height > 0)
4816 it->face_id = face_with_height (it->f, it->face_id, new_height);
4820 return 0;
4823 /* Handle `(space-width WIDTH)'. */
4824 if (CONSP (spec)
4825 && EQ (XCAR (spec), Qspace_width)
4826 && CONSP (XCDR (spec)))
4828 if (it)
4830 if (!FRAME_WINDOW_P (it->f))
4831 return 0;
4833 value = XCAR (XCDR (spec));
4834 if (NUMBERP (value) && XFLOATINT (value) > 0)
4835 it->space_width = value;
4838 return 0;
4841 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4842 if (CONSP (spec)
4843 && EQ (XCAR (spec), Qslice))
4845 Lisp_Object tem;
4847 if (it)
4849 if (!FRAME_WINDOW_P (it->f))
4850 return 0;
4852 if (tem = XCDR (spec), CONSP (tem))
4854 it->slice.x = XCAR (tem);
4855 if (tem = XCDR (tem), CONSP (tem))
4857 it->slice.y = XCAR (tem);
4858 if (tem = XCDR (tem), CONSP (tem))
4860 it->slice.width = XCAR (tem);
4861 if (tem = XCDR (tem), CONSP (tem))
4862 it->slice.height = XCAR (tem);
4868 return 0;
4871 /* Handle `(raise FACTOR)'. */
4872 if (CONSP (spec)
4873 && EQ (XCAR (spec), Qraise)
4874 && CONSP (XCDR (spec)))
4876 if (it)
4878 if (!FRAME_WINDOW_P (it->f))
4879 return 0;
4881 #ifdef HAVE_WINDOW_SYSTEM
4882 value = XCAR (XCDR (spec));
4883 if (NUMBERP (value))
4885 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4886 it->voffset = - (XFLOATINT (value)
4887 * (FONT_HEIGHT (face->font)));
4889 #endif /* HAVE_WINDOW_SYSTEM */
4892 return 0;
4895 /* Don't handle the other kinds of display specifications
4896 inside a string that we got from a `display' property. */
4897 if (it && it->string_from_display_prop_p)
4898 return 0;
4900 /* Characters having this form of property are not displayed, so
4901 we have to find the end of the property. */
4902 if (it)
4904 start_pos = *position;
4905 *position = display_prop_end (it, object, start_pos);
4907 value = Qnil;
4909 /* Stop the scan at that end position--we assume that all
4910 text properties change there. */
4911 if (it)
4912 it->stop_charpos = position->charpos;
4914 /* Handle `(left-fringe BITMAP [FACE])'
4915 and `(right-fringe BITMAP [FACE])'. */
4916 if (CONSP (spec)
4917 && (EQ (XCAR (spec), Qleft_fringe)
4918 || EQ (XCAR (spec), Qright_fringe))
4919 && CONSP (XCDR (spec)))
4921 int fringe_bitmap;
4923 if (it)
4925 if (!FRAME_WINDOW_P (it->f))
4926 /* If we return here, POSITION has been advanced
4927 across the text with this property. */
4929 /* Synchronize the bidi iterator with POSITION. This is
4930 needed because we are not going to push the iterator
4931 on behalf of this display property, so there will be
4932 no pop_it call to do this synchronization for us. */
4933 if (it->bidi_p)
4935 it->position = *position;
4936 iterate_out_of_display_property (it);
4937 *position = it->position;
4939 return 1;
4942 else if (!frame_window_p)
4943 return 1;
4945 #ifdef HAVE_WINDOW_SYSTEM
4946 value = XCAR (XCDR (spec));
4947 if (!SYMBOLP (value)
4948 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4949 /* If we return here, POSITION has been advanced
4950 across the text with this property. */
4952 if (it && it->bidi_p)
4954 it->position = *position;
4955 iterate_out_of_display_property (it);
4956 *position = it->position;
4958 return 1;
4961 if (it)
4963 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4965 if (CONSP (XCDR (XCDR (spec))))
4967 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4968 int face_id2 = lookup_derived_face (it->f, face_name,
4969 FRINGE_FACE_ID, 0);
4970 if (face_id2 >= 0)
4971 face_id = face_id2;
4974 /* Save current settings of IT so that we can restore them
4975 when we are finished with the glyph property value. */
4976 push_it (it, position);
4978 it->area = TEXT_AREA;
4979 it->what = IT_IMAGE;
4980 it->image_id = -1; /* no image */
4981 it->position = start_pos;
4982 it->object = NILP (object) ? it->w->contents : object;
4983 it->method = GET_FROM_IMAGE;
4984 it->from_overlay = Qnil;
4985 it->face_id = face_id;
4986 it->from_disp_prop_p = 1;
4988 /* Say that we haven't consumed the characters with
4989 `display' property yet. The call to pop_it in
4990 set_iterator_to_next will clean this up. */
4991 *position = start_pos;
4993 if (EQ (XCAR (spec), Qleft_fringe))
4995 it->left_user_fringe_bitmap = fringe_bitmap;
4996 it->left_user_fringe_face_id = face_id;
4998 else
5000 it->right_user_fringe_bitmap = fringe_bitmap;
5001 it->right_user_fringe_face_id = face_id;
5004 #endif /* HAVE_WINDOW_SYSTEM */
5005 return 1;
5008 /* Prepare to handle `((margin left-margin) ...)',
5009 `((margin right-margin) ...)' and `((margin nil) ...)'
5010 prefixes for display specifications. */
5011 location = Qunbound;
5012 if (CONSP (spec) && CONSP (XCAR (spec)))
5014 Lisp_Object tem;
5016 value = XCDR (spec);
5017 if (CONSP (value))
5018 value = XCAR (value);
5020 tem = XCAR (spec);
5021 if (EQ (XCAR (tem), Qmargin)
5022 && (tem = XCDR (tem),
5023 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5024 (NILP (tem)
5025 || EQ (tem, Qleft_margin)
5026 || EQ (tem, Qright_margin))))
5027 location = tem;
5030 if (EQ (location, Qunbound))
5032 location = Qnil;
5033 value = spec;
5036 /* After this point, VALUE is the property after any
5037 margin prefix has been stripped. It must be a string,
5038 an image specification, or `(space ...)'.
5040 LOCATION specifies where to display: `left-margin',
5041 `right-margin' or nil. */
5043 valid_p = (STRINGP (value)
5044 #ifdef HAVE_WINDOW_SYSTEM
5045 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5046 && valid_image_p (value))
5047 #endif /* not HAVE_WINDOW_SYSTEM */
5048 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5050 if (valid_p && !display_replaced_p)
5052 int retval = 1;
5054 if (!it)
5056 /* Callers need to know whether the display spec is any kind
5057 of `(space ...)' spec that is about to affect text-area
5058 display. */
5059 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5060 retval = 2;
5061 return retval;
5064 /* Save current settings of IT so that we can restore them
5065 when we are finished with the glyph property value. */
5066 push_it (it, position);
5067 it->from_overlay = overlay;
5068 it->from_disp_prop_p = 1;
5070 if (NILP (location))
5071 it->area = TEXT_AREA;
5072 else if (EQ (location, Qleft_margin))
5073 it->area = LEFT_MARGIN_AREA;
5074 else
5075 it->area = RIGHT_MARGIN_AREA;
5077 if (STRINGP (value))
5079 it->string = value;
5080 it->multibyte_p = STRING_MULTIBYTE (it->string);
5081 it->current.overlay_string_index = -1;
5082 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5083 it->end_charpos = it->string_nchars = SCHARS (it->string);
5084 it->method = GET_FROM_STRING;
5085 it->stop_charpos = 0;
5086 it->prev_stop = 0;
5087 it->base_level_stop = 0;
5088 it->string_from_display_prop_p = 1;
5089 /* Say that we haven't consumed the characters with
5090 `display' property yet. The call to pop_it in
5091 set_iterator_to_next will clean this up. */
5092 if (BUFFERP (object))
5093 *position = start_pos;
5095 /* Force paragraph direction to be that of the parent
5096 object. If the parent object's paragraph direction is
5097 not yet determined, default to L2R. */
5098 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5099 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5100 else
5101 it->paragraph_embedding = L2R;
5103 /* Set up the bidi iterator for this display string. */
5104 if (it->bidi_p)
5106 it->bidi_it.string.lstring = it->string;
5107 it->bidi_it.string.s = NULL;
5108 it->bidi_it.string.schars = it->end_charpos;
5109 it->bidi_it.string.bufpos = bufpos;
5110 it->bidi_it.string.from_disp_str = 1;
5111 it->bidi_it.string.unibyte = !it->multibyte_p;
5112 it->bidi_it.w = it->w;
5113 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5116 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5118 it->method = GET_FROM_STRETCH;
5119 it->object = value;
5120 *position = it->position = start_pos;
5121 retval = 1 + (it->area == TEXT_AREA);
5123 #ifdef HAVE_WINDOW_SYSTEM
5124 else
5126 it->what = IT_IMAGE;
5127 it->image_id = lookup_image (it->f, value);
5128 it->position = start_pos;
5129 it->object = NILP (object) ? it->w->contents : object;
5130 it->method = GET_FROM_IMAGE;
5132 /* Say that we haven't consumed the characters with
5133 `display' property yet. The call to pop_it in
5134 set_iterator_to_next will clean this up. */
5135 *position = start_pos;
5137 #endif /* HAVE_WINDOW_SYSTEM */
5139 return retval;
5142 /* Invalid property or property not supported. Restore
5143 POSITION to what it was before. */
5144 *position = start_pos;
5145 return 0;
5148 /* Check if PROP is a display property value whose text should be
5149 treated as intangible. OVERLAY is the overlay from which PROP
5150 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5151 specify the buffer position covered by PROP. */
5154 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5155 ptrdiff_t charpos, ptrdiff_t bytepos)
5157 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5158 struct text_pos position;
5160 SET_TEXT_POS (position, charpos, bytepos);
5161 return handle_display_spec (NULL, prop, Qnil, overlay,
5162 &position, charpos, frame_window_p);
5166 /* Return 1 if PROP is a display sub-property value containing STRING.
5168 Implementation note: this and the following function are really
5169 special cases of handle_display_spec and
5170 handle_single_display_spec, and should ideally use the same code.
5171 Until they do, these two pairs must be consistent and must be
5172 modified in sync. */
5174 static int
5175 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5177 if (EQ (string, prop))
5178 return 1;
5180 /* Skip over `when FORM'. */
5181 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5183 prop = XCDR (prop);
5184 if (!CONSP (prop))
5185 return 0;
5186 /* Actually, the condition following `when' should be eval'ed,
5187 like handle_single_display_spec does, and we should return
5188 zero if it evaluates to nil. However, this function is
5189 called only when the buffer was already displayed and some
5190 glyph in the glyph matrix was found to come from a display
5191 string. Therefore, the condition was already evaluated, and
5192 the result was non-nil, otherwise the display string wouldn't
5193 have been displayed and we would have never been called for
5194 this property. Thus, we can skip the evaluation and assume
5195 its result is non-nil. */
5196 prop = XCDR (prop);
5199 if (CONSP (prop))
5200 /* Skip over `margin LOCATION'. */
5201 if (EQ (XCAR (prop), Qmargin))
5203 prop = XCDR (prop);
5204 if (!CONSP (prop))
5205 return 0;
5207 prop = XCDR (prop);
5208 if (!CONSP (prop))
5209 return 0;
5212 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5216 /* Return 1 if STRING appears in the `display' property PROP. */
5218 static int
5219 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5221 if (CONSP (prop)
5222 && !EQ (XCAR (prop), Qwhen)
5223 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5225 /* A list of sub-properties. */
5226 while (CONSP (prop))
5228 if (single_display_spec_string_p (XCAR (prop), string))
5229 return 1;
5230 prop = XCDR (prop);
5233 else if (VECTORP (prop))
5235 /* A vector of sub-properties. */
5236 ptrdiff_t i;
5237 for (i = 0; i < ASIZE (prop); ++i)
5238 if (single_display_spec_string_p (AREF (prop, i), string))
5239 return 1;
5241 else
5242 return single_display_spec_string_p (prop, string);
5244 return 0;
5247 /* Look for STRING in overlays and text properties in the current
5248 buffer, between character positions FROM and TO (excluding TO).
5249 BACK_P non-zero means look back (in this case, TO is supposed to be
5250 less than FROM).
5251 Value is the first character position where STRING was found, or
5252 zero if it wasn't found before hitting TO.
5254 This function may only use code that doesn't eval because it is
5255 called asynchronously from note_mouse_highlight. */
5257 static ptrdiff_t
5258 string_buffer_position_lim (Lisp_Object string,
5259 ptrdiff_t from, ptrdiff_t to, int back_p)
5261 Lisp_Object limit, prop, pos;
5262 int found = 0;
5264 pos = make_number (max (from, BEGV));
5266 if (!back_p) /* looking forward */
5268 limit = make_number (min (to, ZV));
5269 while (!found && !EQ (pos, limit))
5271 prop = Fget_char_property (pos, Qdisplay, Qnil);
5272 if (!NILP (prop) && display_prop_string_p (prop, string))
5273 found = 1;
5274 else
5275 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5276 limit);
5279 else /* looking back */
5281 limit = make_number (max (to, BEGV));
5282 while (!found && !EQ (pos, limit))
5284 prop = Fget_char_property (pos, Qdisplay, Qnil);
5285 if (!NILP (prop) && display_prop_string_p (prop, string))
5286 found = 1;
5287 else
5288 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5289 limit);
5293 return found ? XINT (pos) : 0;
5296 /* Determine which buffer position in current buffer STRING comes from.
5297 AROUND_CHARPOS is an approximate position where it could come from.
5298 Value is the buffer position or 0 if it couldn't be determined.
5300 This function is necessary because we don't record buffer positions
5301 in glyphs generated from strings (to keep struct glyph small).
5302 This function may only use code that doesn't eval because it is
5303 called asynchronously from note_mouse_highlight. */
5305 static ptrdiff_t
5306 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5308 const int MAX_DISTANCE = 1000;
5309 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5310 around_charpos + MAX_DISTANCE,
5313 if (!found)
5314 found = string_buffer_position_lim (string, around_charpos,
5315 around_charpos - MAX_DISTANCE, 1);
5316 return found;
5321 /***********************************************************************
5322 `composition' property
5323 ***********************************************************************/
5325 /* Set up iterator IT from `composition' property at its current
5326 position. Called from handle_stop. */
5328 static enum prop_handled
5329 handle_composition_prop (struct it *it)
5331 Lisp_Object prop, string;
5332 ptrdiff_t pos, pos_byte, start, end;
5334 if (STRINGP (it->string))
5336 unsigned char *s;
5338 pos = IT_STRING_CHARPOS (*it);
5339 pos_byte = IT_STRING_BYTEPOS (*it);
5340 string = it->string;
5341 s = SDATA (string) + pos_byte;
5342 it->c = STRING_CHAR (s);
5344 else
5346 pos = IT_CHARPOS (*it);
5347 pos_byte = IT_BYTEPOS (*it);
5348 string = Qnil;
5349 it->c = FETCH_CHAR (pos_byte);
5352 /* If there's a valid composition and point is not inside of the
5353 composition (in the case that the composition is from the current
5354 buffer), draw a glyph composed from the composition components. */
5355 if (find_composition (pos, -1, &start, &end, &prop, string)
5356 && composition_valid_p (start, end, prop)
5357 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5359 if (start < pos)
5360 /* As we can't handle this situation (perhaps font-lock added
5361 a new composition), we just return here hoping that next
5362 redisplay will detect this composition much earlier. */
5363 return HANDLED_NORMALLY;
5364 if (start != pos)
5366 if (STRINGP (it->string))
5367 pos_byte = string_char_to_byte (it->string, start);
5368 else
5369 pos_byte = CHAR_TO_BYTE (start);
5371 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5372 prop, string);
5374 if (it->cmp_it.id >= 0)
5376 it->cmp_it.ch = -1;
5377 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5378 it->cmp_it.nglyphs = -1;
5382 return HANDLED_NORMALLY;
5387 /***********************************************************************
5388 Overlay strings
5389 ***********************************************************************/
5391 /* The following structure is used to record overlay strings for
5392 later sorting in load_overlay_strings. */
5394 struct overlay_entry
5396 Lisp_Object overlay;
5397 Lisp_Object string;
5398 EMACS_INT priority;
5399 int after_string_p;
5403 /* Set up iterator IT from overlay strings at its current position.
5404 Called from handle_stop. */
5406 static enum prop_handled
5407 handle_overlay_change (struct it *it)
5409 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5410 return HANDLED_RECOMPUTE_PROPS;
5411 else
5412 return HANDLED_NORMALLY;
5416 /* Set up the next overlay string for delivery by IT, if there is an
5417 overlay string to deliver. Called by set_iterator_to_next when the
5418 end of the current overlay string is reached. If there are more
5419 overlay strings to display, IT->string and
5420 IT->current.overlay_string_index are set appropriately here.
5421 Otherwise IT->string is set to nil. */
5423 static void
5424 next_overlay_string (struct it *it)
5426 ++it->current.overlay_string_index;
5427 if (it->current.overlay_string_index == it->n_overlay_strings)
5429 /* No more overlay strings. Restore IT's settings to what
5430 they were before overlay strings were processed, and
5431 continue to deliver from current_buffer. */
5433 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5434 pop_it (it);
5435 eassert (it->sp > 0
5436 || (NILP (it->string)
5437 && it->method == GET_FROM_BUFFER
5438 && it->stop_charpos >= BEGV
5439 && it->stop_charpos <= it->end_charpos));
5440 it->current.overlay_string_index = -1;
5441 it->n_overlay_strings = 0;
5442 it->overlay_strings_charpos = -1;
5443 /* If there's an empty display string on the stack, pop the
5444 stack, to resync the bidi iterator with IT's position. Such
5445 empty strings are pushed onto the stack in
5446 get_overlay_strings_1. */
5447 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5448 pop_it (it);
5450 /* If we're at the end of the buffer, record that we have
5451 processed the overlay strings there already, so that
5452 next_element_from_buffer doesn't try it again. */
5453 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5454 it->overlay_strings_at_end_processed_p = 1;
5456 else
5458 /* There are more overlay strings to process. If
5459 IT->current.overlay_string_index has advanced to a position
5460 where we must load IT->overlay_strings with more strings, do
5461 it. We must load at the IT->overlay_strings_charpos where
5462 IT->n_overlay_strings was originally computed; when invisible
5463 text is present, this might not be IT_CHARPOS (Bug#7016). */
5464 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5466 if (it->current.overlay_string_index && i == 0)
5467 load_overlay_strings (it, it->overlay_strings_charpos);
5469 /* Initialize IT to deliver display elements from the overlay
5470 string. */
5471 it->string = it->overlay_strings[i];
5472 it->multibyte_p = STRING_MULTIBYTE (it->string);
5473 SET_TEXT_POS (it->current.string_pos, 0, 0);
5474 it->method = GET_FROM_STRING;
5475 it->stop_charpos = 0;
5476 it->end_charpos = SCHARS (it->string);
5477 if (it->cmp_it.stop_pos >= 0)
5478 it->cmp_it.stop_pos = 0;
5479 it->prev_stop = 0;
5480 it->base_level_stop = 0;
5482 /* Set up the bidi iterator for this overlay string. */
5483 if (it->bidi_p)
5485 it->bidi_it.string.lstring = it->string;
5486 it->bidi_it.string.s = NULL;
5487 it->bidi_it.string.schars = SCHARS (it->string);
5488 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5489 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5490 it->bidi_it.string.unibyte = !it->multibyte_p;
5491 it->bidi_it.w = it->w;
5492 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5496 CHECK_IT (it);
5500 /* Compare two overlay_entry structures E1 and E2. Used as a
5501 comparison function for qsort in load_overlay_strings. Overlay
5502 strings for the same position are sorted so that
5504 1. All after-strings come in front of before-strings, except
5505 when they come from the same overlay.
5507 2. Within after-strings, strings are sorted so that overlay strings
5508 from overlays with higher priorities come first.
5510 2. Within before-strings, strings are sorted so that overlay
5511 strings from overlays with higher priorities come last.
5513 Value is analogous to strcmp. */
5516 static int
5517 compare_overlay_entries (const void *e1, const void *e2)
5519 struct overlay_entry const *entry1 = e1;
5520 struct overlay_entry const *entry2 = e2;
5521 int result;
5523 if (entry1->after_string_p != entry2->after_string_p)
5525 /* Let after-strings appear in front of before-strings if
5526 they come from different overlays. */
5527 if (EQ (entry1->overlay, entry2->overlay))
5528 result = entry1->after_string_p ? 1 : -1;
5529 else
5530 result = entry1->after_string_p ? -1 : 1;
5532 else if (entry1->priority != entry2->priority)
5534 if (entry1->after_string_p)
5535 /* After-strings sorted in order of decreasing priority. */
5536 result = entry2->priority < entry1->priority ? -1 : 1;
5537 else
5538 /* Before-strings sorted in order of increasing priority. */
5539 result = entry1->priority < entry2->priority ? -1 : 1;
5541 else
5542 result = 0;
5544 return result;
5548 /* Load the vector IT->overlay_strings with overlay strings from IT's
5549 current buffer position, or from CHARPOS if that is > 0. Set
5550 IT->n_overlays to the total number of overlay strings found.
5552 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5553 a time. On entry into load_overlay_strings,
5554 IT->current.overlay_string_index gives the number of overlay
5555 strings that have already been loaded by previous calls to this
5556 function.
5558 IT->add_overlay_start contains an additional overlay start
5559 position to consider for taking overlay strings from, if non-zero.
5560 This position comes into play when the overlay has an `invisible'
5561 property, and both before and after-strings. When we've skipped to
5562 the end of the overlay, because of its `invisible' property, we
5563 nevertheless want its before-string to appear.
5564 IT->add_overlay_start will contain the overlay start position
5565 in this case.
5567 Overlay strings are sorted so that after-string strings come in
5568 front of before-string strings. Within before and after-strings,
5569 strings are sorted by overlay priority. See also function
5570 compare_overlay_entries. */
5572 static void
5573 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5575 Lisp_Object overlay, window, str, invisible;
5576 struct Lisp_Overlay *ov;
5577 ptrdiff_t start, end;
5578 ptrdiff_t size = 20;
5579 ptrdiff_t n = 0, i, j;
5580 int invis_p;
5581 struct overlay_entry *entries = alloca (size * sizeof *entries);
5582 USE_SAFE_ALLOCA;
5584 if (charpos <= 0)
5585 charpos = IT_CHARPOS (*it);
5587 /* Append the overlay string STRING of overlay OVERLAY to vector
5588 `entries' which has size `size' and currently contains `n'
5589 elements. AFTER_P non-zero means STRING is an after-string of
5590 OVERLAY. */
5591 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5592 do \
5594 Lisp_Object priority; \
5596 if (n == size) \
5598 struct overlay_entry *old = entries; \
5599 SAFE_NALLOCA (entries, 2, size); \
5600 memcpy (entries, old, size * sizeof *entries); \
5601 size *= 2; \
5604 entries[n].string = (STRING); \
5605 entries[n].overlay = (OVERLAY); \
5606 priority = Foverlay_get ((OVERLAY), Qpriority); \
5607 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5608 entries[n].after_string_p = (AFTER_P); \
5609 ++n; \
5611 while (0)
5613 /* Process overlay before the overlay center. */
5614 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5616 XSETMISC (overlay, ov);
5617 eassert (OVERLAYP (overlay));
5618 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5619 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5621 if (end < charpos)
5622 break;
5624 /* Skip this overlay if it doesn't start or end at IT's current
5625 position. */
5626 if (end != charpos && start != charpos)
5627 continue;
5629 /* Skip this overlay if it doesn't apply to IT->w. */
5630 window = Foverlay_get (overlay, Qwindow);
5631 if (WINDOWP (window) && XWINDOW (window) != it->w)
5632 continue;
5634 /* If the text ``under'' the overlay is invisible, both before-
5635 and after-strings from this overlay are visible; start and
5636 end position are indistinguishable. */
5637 invisible = Foverlay_get (overlay, Qinvisible);
5638 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5640 /* If overlay has a non-empty before-string, record it. */
5641 if ((start == charpos || (end == charpos && invis_p))
5642 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5643 && SCHARS (str))
5644 RECORD_OVERLAY_STRING (overlay, str, 0);
5646 /* If overlay has a non-empty after-string, record it. */
5647 if ((end == charpos || (start == charpos && invis_p))
5648 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5649 && SCHARS (str))
5650 RECORD_OVERLAY_STRING (overlay, str, 1);
5653 /* Process overlays after the overlay center. */
5654 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5656 XSETMISC (overlay, ov);
5657 eassert (OVERLAYP (overlay));
5658 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5659 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5661 if (start > charpos)
5662 break;
5664 /* Skip this overlay if it doesn't start or end at IT's current
5665 position. */
5666 if (end != charpos && start != charpos)
5667 continue;
5669 /* Skip this overlay if it doesn't apply to IT->w. */
5670 window = Foverlay_get (overlay, Qwindow);
5671 if (WINDOWP (window) && XWINDOW (window) != it->w)
5672 continue;
5674 /* If the text ``under'' the overlay is invisible, it has a zero
5675 dimension, and both before- and after-strings apply. */
5676 invisible = Foverlay_get (overlay, Qinvisible);
5677 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5679 /* If overlay has a non-empty before-string, record it. */
5680 if ((start == charpos || (end == charpos && invis_p))
5681 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5682 && SCHARS (str))
5683 RECORD_OVERLAY_STRING (overlay, str, 0);
5685 /* If overlay has a non-empty after-string, record it. */
5686 if ((end == charpos || (start == charpos && invis_p))
5687 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5688 && SCHARS (str))
5689 RECORD_OVERLAY_STRING (overlay, str, 1);
5692 #undef RECORD_OVERLAY_STRING
5694 /* Sort entries. */
5695 if (n > 1)
5696 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5698 /* Record number of overlay strings, and where we computed it. */
5699 it->n_overlay_strings = n;
5700 it->overlay_strings_charpos = charpos;
5702 /* IT->current.overlay_string_index is the number of overlay strings
5703 that have already been consumed by IT. Copy some of the
5704 remaining overlay strings to IT->overlay_strings. */
5705 i = 0;
5706 j = it->current.overlay_string_index;
5707 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5709 it->overlay_strings[i] = entries[j].string;
5710 it->string_overlays[i++] = entries[j++].overlay;
5713 CHECK_IT (it);
5714 SAFE_FREE ();
5718 /* Get the first chunk of overlay strings at IT's current buffer
5719 position, or at CHARPOS if that is > 0. Value is non-zero if at
5720 least one overlay string was found. */
5722 static int
5723 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5725 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5726 process. This fills IT->overlay_strings with strings, and sets
5727 IT->n_overlay_strings to the total number of strings to process.
5728 IT->pos.overlay_string_index has to be set temporarily to zero
5729 because load_overlay_strings needs this; it must be set to -1
5730 when no overlay strings are found because a zero value would
5731 indicate a position in the first overlay string. */
5732 it->current.overlay_string_index = 0;
5733 load_overlay_strings (it, charpos);
5735 /* If we found overlay strings, set up IT to deliver display
5736 elements from the first one. Otherwise set up IT to deliver
5737 from current_buffer. */
5738 if (it->n_overlay_strings)
5740 /* Make sure we know settings in current_buffer, so that we can
5741 restore meaningful values when we're done with the overlay
5742 strings. */
5743 if (compute_stop_p)
5744 compute_stop_pos (it);
5745 eassert (it->face_id >= 0);
5747 /* Save IT's settings. They are restored after all overlay
5748 strings have been processed. */
5749 eassert (!compute_stop_p || it->sp == 0);
5751 /* When called from handle_stop, there might be an empty display
5752 string loaded. In that case, don't bother saving it. But
5753 don't use this optimization with the bidi iterator, since we
5754 need the corresponding pop_it call to resync the bidi
5755 iterator's position with IT's position, after we are done
5756 with the overlay strings. (The corresponding call to pop_it
5757 in case of an empty display string is in
5758 next_overlay_string.) */
5759 if (!(!it->bidi_p
5760 && STRINGP (it->string) && !SCHARS (it->string)))
5761 push_it (it, NULL);
5763 /* Set up IT to deliver display elements from the first overlay
5764 string. */
5765 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5766 it->string = it->overlay_strings[0];
5767 it->from_overlay = Qnil;
5768 it->stop_charpos = 0;
5769 eassert (STRINGP (it->string));
5770 it->end_charpos = SCHARS (it->string);
5771 it->prev_stop = 0;
5772 it->base_level_stop = 0;
5773 it->multibyte_p = STRING_MULTIBYTE (it->string);
5774 it->method = GET_FROM_STRING;
5775 it->from_disp_prop_p = 0;
5777 /* Force paragraph direction to be that of the parent
5778 buffer. */
5779 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5780 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5781 else
5782 it->paragraph_embedding = L2R;
5784 /* Set up the bidi iterator for this overlay string. */
5785 if (it->bidi_p)
5787 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5789 it->bidi_it.string.lstring = it->string;
5790 it->bidi_it.string.s = NULL;
5791 it->bidi_it.string.schars = SCHARS (it->string);
5792 it->bidi_it.string.bufpos = pos;
5793 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5794 it->bidi_it.string.unibyte = !it->multibyte_p;
5795 it->bidi_it.w = it->w;
5796 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5798 return 1;
5801 it->current.overlay_string_index = -1;
5802 return 0;
5805 static int
5806 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5808 it->string = Qnil;
5809 it->method = GET_FROM_BUFFER;
5811 (void) get_overlay_strings_1 (it, charpos, 1);
5813 CHECK_IT (it);
5815 /* Value is non-zero if we found at least one overlay string. */
5816 return STRINGP (it->string);
5821 /***********************************************************************
5822 Saving and restoring state
5823 ***********************************************************************/
5825 /* Save current settings of IT on IT->stack. Called, for example,
5826 before setting up IT for an overlay string, to be able to restore
5827 IT's settings to what they were after the overlay string has been
5828 processed. If POSITION is non-NULL, it is the position to save on
5829 the stack instead of IT->position. */
5831 static void
5832 push_it (struct it *it, struct text_pos *position)
5834 struct iterator_stack_entry *p;
5836 eassert (it->sp < IT_STACK_SIZE);
5837 p = it->stack + it->sp;
5839 p->stop_charpos = it->stop_charpos;
5840 p->prev_stop = it->prev_stop;
5841 p->base_level_stop = it->base_level_stop;
5842 p->cmp_it = it->cmp_it;
5843 eassert (it->face_id >= 0);
5844 p->face_id = it->face_id;
5845 p->string = it->string;
5846 p->method = it->method;
5847 p->from_overlay = it->from_overlay;
5848 switch (p->method)
5850 case GET_FROM_IMAGE:
5851 p->u.image.object = it->object;
5852 p->u.image.image_id = it->image_id;
5853 p->u.image.slice = it->slice;
5854 break;
5855 case GET_FROM_STRETCH:
5856 p->u.stretch.object = it->object;
5857 break;
5859 p->position = position ? *position : it->position;
5860 p->current = it->current;
5861 p->end_charpos = it->end_charpos;
5862 p->string_nchars = it->string_nchars;
5863 p->area = it->area;
5864 p->multibyte_p = it->multibyte_p;
5865 p->avoid_cursor_p = it->avoid_cursor_p;
5866 p->space_width = it->space_width;
5867 p->font_height = it->font_height;
5868 p->voffset = it->voffset;
5869 p->string_from_display_prop_p = it->string_from_display_prop_p;
5870 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5871 p->display_ellipsis_p = 0;
5872 p->line_wrap = it->line_wrap;
5873 p->bidi_p = it->bidi_p;
5874 p->paragraph_embedding = it->paragraph_embedding;
5875 p->from_disp_prop_p = it->from_disp_prop_p;
5876 ++it->sp;
5878 /* Save the state of the bidi iterator as well. */
5879 if (it->bidi_p)
5880 bidi_push_it (&it->bidi_it);
5883 static void
5884 iterate_out_of_display_property (struct it *it)
5886 int buffer_p = !STRINGP (it->string);
5887 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5888 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5890 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5892 /* Maybe initialize paragraph direction. If we are at the beginning
5893 of a new paragraph, next_element_from_buffer may not have a
5894 chance to do that. */
5895 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5896 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5897 /* prev_stop can be zero, so check against BEGV as well. */
5898 while (it->bidi_it.charpos >= bob
5899 && it->prev_stop <= it->bidi_it.charpos
5900 && it->bidi_it.charpos < CHARPOS (it->position)
5901 && it->bidi_it.charpos < eob)
5902 bidi_move_to_visually_next (&it->bidi_it);
5903 /* Record the stop_pos we just crossed, for when we cross it
5904 back, maybe. */
5905 if (it->bidi_it.charpos > CHARPOS (it->position))
5906 it->prev_stop = CHARPOS (it->position);
5907 /* If we ended up not where pop_it put us, resync IT's
5908 positional members with the bidi iterator. */
5909 if (it->bidi_it.charpos != CHARPOS (it->position))
5910 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5911 if (buffer_p)
5912 it->current.pos = it->position;
5913 else
5914 it->current.string_pos = it->position;
5917 /* Restore IT's settings from IT->stack. Called, for example, when no
5918 more overlay strings must be processed, and we return to delivering
5919 display elements from a buffer, or when the end of a string from a
5920 `display' property is reached and we return to delivering display
5921 elements from an overlay string, or from a buffer. */
5923 static void
5924 pop_it (struct it *it)
5926 struct iterator_stack_entry *p;
5927 int from_display_prop = it->from_disp_prop_p;
5929 eassert (it->sp > 0);
5930 --it->sp;
5931 p = it->stack + it->sp;
5932 it->stop_charpos = p->stop_charpos;
5933 it->prev_stop = p->prev_stop;
5934 it->base_level_stop = p->base_level_stop;
5935 it->cmp_it = p->cmp_it;
5936 it->face_id = p->face_id;
5937 it->current = p->current;
5938 it->position = p->position;
5939 it->string = p->string;
5940 it->from_overlay = p->from_overlay;
5941 if (NILP (it->string))
5942 SET_TEXT_POS (it->current.string_pos, -1, -1);
5943 it->method = p->method;
5944 switch (it->method)
5946 case GET_FROM_IMAGE:
5947 it->image_id = p->u.image.image_id;
5948 it->object = p->u.image.object;
5949 it->slice = p->u.image.slice;
5950 break;
5951 case GET_FROM_STRETCH:
5952 it->object = p->u.stretch.object;
5953 break;
5954 case GET_FROM_BUFFER:
5955 it->object = it->w->contents;
5956 break;
5957 case GET_FROM_STRING:
5958 it->object = it->string;
5959 break;
5960 case GET_FROM_DISPLAY_VECTOR:
5961 if (it->s)
5962 it->method = GET_FROM_C_STRING;
5963 else if (STRINGP (it->string))
5964 it->method = GET_FROM_STRING;
5965 else
5967 it->method = GET_FROM_BUFFER;
5968 it->object = it->w->contents;
5971 it->end_charpos = p->end_charpos;
5972 it->string_nchars = p->string_nchars;
5973 it->area = p->area;
5974 it->multibyte_p = p->multibyte_p;
5975 it->avoid_cursor_p = p->avoid_cursor_p;
5976 it->space_width = p->space_width;
5977 it->font_height = p->font_height;
5978 it->voffset = p->voffset;
5979 it->string_from_display_prop_p = p->string_from_display_prop_p;
5980 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5981 it->line_wrap = p->line_wrap;
5982 it->bidi_p = p->bidi_p;
5983 it->paragraph_embedding = p->paragraph_embedding;
5984 it->from_disp_prop_p = p->from_disp_prop_p;
5985 if (it->bidi_p)
5987 bidi_pop_it (&it->bidi_it);
5988 /* Bidi-iterate until we get out of the portion of text, if any,
5989 covered by a `display' text property or by an overlay with
5990 `display' property. (We cannot just jump there, because the
5991 internal coherency of the bidi iterator state can not be
5992 preserved across such jumps.) We also must determine the
5993 paragraph base direction if the overlay we just processed is
5994 at the beginning of a new paragraph. */
5995 if (from_display_prop
5996 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5997 iterate_out_of_display_property (it);
5999 eassert ((BUFFERP (it->object)
6000 && IT_CHARPOS (*it) == it->bidi_it.charpos
6001 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6002 || (STRINGP (it->object)
6003 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6004 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6005 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6011 /***********************************************************************
6012 Moving over lines
6013 ***********************************************************************/
6015 /* Set IT's current position to the previous line start. */
6017 static void
6018 back_to_previous_line_start (struct it *it)
6020 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6022 DEC_BOTH (cp, bp);
6023 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6027 /* Move IT to the next line start.
6029 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6030 we skipped over part of the text (as opposed to moving the iterator
6031 continuously over the text). Otherwise, don't change the value
6032 of *SKIPPED_P.
6034 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6035 iterator on the newline, if it was found.
6037 Newlines may come from buffer text, overlay strings, or strings
6038 displayed via the `display' property. That's the reason we can't
6039 simply use find_newline_no_quit.
6041 Note that this function may not skip over invisible text that is so
6042 because of text properties and immediately follows a newline. If
6043 it would, function reseat_at_next_visible_line_start, when called
6044 from set_iterator_to_next, would effectively make invisible
6045 characters following a newline part of the wrong glyph row, which
6046 leads to wrong cursor motion. */
6048 static int
6049 forward_to_next_line_start (struct it *it, int *skipped_p,
6050 struct bidi_it *bidi_it_prev)
6052 ptrdiff_t old_selective;
6053 int newline_found_p, n;
6054 const int MAX_NEWLINE_DISTANCE = 500;
6056 /* If already on a newline, just consume it to avoid unintended
6057 skipping over invisible text below. */
6058 if (it->what == IT_CHARACTER
6059 && it->c == '\n'
6060 && CHARPOS (it->position) == IT_CHARPOS (*it))
6062 if (it->bidi_p && bidi_it_prev)
6063 *bidi_it_prev = it->bidi_it;
6064 set_iterator_to_next (it, 0);
6065 it->c = 0;
6066 return 1;
6069 /* Don't handle selective display in the following. It's (a)
6070 unnecessary because it's done by the caller, and (b) leads to an
6071 infinite recursion because next_element_from_ellipsis indirectly
6072 calls this function. */
6073 old_selective = it->selective;
6074 it->selective = 0;
6076 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6077 from buffer text. */
6078 for (n = newline_found_p = 0;
6079 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6080 n += STRINGP (it->string) ? 0 : 1)
6082 if (!get_next_display_element (it))
6083 return 0;
6084 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6085 if (newline_found_p && it->bidi_p && bidi_it_prev)
6086 *bidi_it_prev = it->bidi_it;
6087 set_iterator_to_next (it, 0);
6090 /* If we didn't find a newline near enough, see if we can use a
6091 short-cut. */
6092 if (!newline_found_p)
6094 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6095 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6096 1, &bytepos);
6097 Lisp_Object pos;
6099 eassert (!STRINGP (it->string));
6101 /* If there isn't any `display' property in sight, and no
6102 overlays, we can just use the position of the newline in
6103 buffer text. */
6104 if (it->stop_charpos >= limit
6105 || ((pos = Fnext_single_property_change (make_number (start),
6106 Qdisplay, Qnil,
6107 make_number (limit)),
6108 NILP (pos))
6109 && next_overlay_change (start) == ZV))
6111 if (!it->bidi_p)
6113 IT_CHARPOS (*it) = limit;
6114 IT_BYTEPOS (*it) = bytepos;
6116 else
6118 struct bidi_it bprev;
6120 /* Help bidi.c avoid expensive searches for display
6121 properties and overlays, by telling it that there are
6122 none up to `limit'. */
6123 if (it->bidi_it.disp_pos < limit)
6125 it->bidi_it.disp_pos = limit;
6126 it->bidi_it.disp_prop = 0;
6128 do {
6129 bprev = it->bidi_it;
6130 bidi_move_to_visually_next (&it->bidi_it);
6131 } while (it->bidi_it.charpos != limit);
6132 IT_CHARPOS (*it) = limit;
6133 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6134 if (bidi_it_prev)
6135 *bidi_it_prev = bprev;
6137 *skipped_p = newline_found_p = 1;
6139 else
6141 while (get_next_display_element (it)
6142 && !newline_found_p)
6144 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6145 if (newline_found_p && it->bidi_p && bidi_it_prev)
6146 *bidi_it_prev = it->bidi_it;
6147 set_iterator_to_next (it, 0);
6152 it->selective = old_selective;
6153 return newline_found_p;
6157 /* Set IT's current position to the previous visible line start. Skip
6158 invisible text that is so either due to text properties or due to
6159 selective display. Caution: this does not change IT->current_x and
6160 IT->hpos. */
6162 static void
6163 back_to_previous_visible_line_start (struct it *it)
6165 while (IT_CHARPOS (*it) > BEGV)
6167 back_to_previous_line_start (it);
6169 if (IT_CHARPOS (*it) <= BEGV)
6170 break;
6172 /* If selective > 0, then lines indented more than its value are
6173 invisible. */
6174 if (it->selective > 0
6175 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6176 it->selective))
6177 continue;
6179 /* Check the newline before point for invisibility. */
6181 Lisp_Object prop;
6182 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6183 Qinvisible, it->window);
6184 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6185 continue;
6188 if (IT_CHARPOS (*it) <= BEGV)
6189 break;
6192 struct it it2;
6193 void *it2data = NULL;
6194 ptrdiff_t pos;
6195 ptrdiff_t beg, end;
6196 Lisp_Object val, overlay;
6198 SAVE_IT (it2, *it, it2data);
6200 /* If newline is part of a composition, continue from start of composition */
6201 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6202 && beg < IT_CHARPOS (*it))
6203 goto replaced;
6205 /* If newline is replaced by a display property, find start of overlay
6206 or interval and continue search from that point. */
6207 pos = --IT_CHARPOS (it2);
6208 --IT_BYTEPOS (it2);
6209 it2.sp = 0;
6210 bidi_unshelve_cache (NULL, 0);
6211 it2.string_from_display_prop_p = 0;
6212 it2.from_disp_prop_p = 0;
6213 if (handle_display_prop (&it2) == HANDLED_RETURN
6214 && !NILP (val = get_char_property_and_overlay
6215 (make_number (pos), Qdisplay, Qnil, &overlay))
6216 && (OVERLAYP (overlay)
6217 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6218 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6220 RESTORE_IT (it, it, it2data);
6221 goto replaced;
6224 /* Newline is not replaced by anything -- so we are done. */
6225 RESTORE_IT (it, it, it2data);
6226 break;
6228 replaced:
6229 if (beg < BEGV)
6230 beg = BEGV;
6231 IT_CHARPOS (*it) = beg;
6232 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6236 it->continuation_lines_width = 0;
6238 eassert (IT_CHARPOS (*it) >= BEGV);
6239 eassert (IT_CHARPOS (*it) == BEGV
6240 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6241 CHECK_IT (it);
6245 /* Reseat iterator IT at the previous visible line start. Skip
6246 invisible text that is so either due to text properties or due to
6247 selective display. At the end, update IT's overlay information,
6248 face information etc. */
6250 void
6251 reseat_at_previous_visible_line_start (struct it *it)
6253 back_to_previous_visible_line_start (it);
6254 reseat (it, it->current.pos, 1);
6255 CHECK_IT (it);
6259 /* Reseat iterator IT on the next visible line start in the current
6260 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6261 preceding the line start. Skip over invisible text that is so
6262 because of selective display. Compute faces, overlays etc at the
6263 new position. Note that this function does not skip over text that
6264 is invisible because of text properties. */
6266 static void
6267 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6269 int newline_found_p, skipped_p = 0;
6270 struct bidi_it bidi_it_prev;
6272 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6274 /* Skip over lines that are invisible because they are indented
6275 more than the value of IT->selective. */
6276 if (it->selective > 0)
6277 while (IT_CHARPOS (*it) < ZV
6278 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6279 it->selective))
6281 eassert (IT_BYTEPOS (*it) == BEGV
6282 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6283 newline_found_p =
6284 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6287 /* Position on the newline if that's what's requested. */
6288 if (on_newline_p && newline_found_p)
6290 if (STRINGP (it->string))
6292 if (IT_STRING_CHARPOS (*it) > 0)
6294 if (!it->bidi_p)
6296 --IT_STRING_CHARPOS (*it);
6297 --IT_STRING_BYTEPOS (*it);
6299 else
6301 /* We need to restore the bidi iterator to the state
6302 it had on the newline, and resync the IT's
6303 position with that. */
6304 it->bidi_it = bidi_it_prev;
6305 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6306 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6310 else if (IT_CHARPOS (*it) > BEGV)
6312 if (!it->bidi_p)
6314 --IT_CHARPOS (*it);
6315 --IT_BYTEPOS (*it);
6317 else
6319 /* We need to restore the bidi iterator to the state it
6320 had on the newline and resync IT with that. */
6321 it->bidi_it = bidi_it_prev;
6322 IT_CHARPOS (*it) = it->bidi_it.charpos;
6323 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6325 reseat (it, it->current.pos, 0);
6328 else if (skipped_p)
6329 reseat (it, it->current.pos, 0);
6331 CHECK_IT (it);
6336 /***********************************************************************
6337 Changing an iterator's position
6338 ***********************************************************************/
6340 /* Change IT's current position to POS in current_buffer. If FORCE_P
6341 is non-zero, always check for text properties at the new position.
6342 Otherwise, text properties are only looked up if POS >=
6343 IT->check_charpos of a property. */
6345 static void
6346 reseat (struct it *it, struct text_pos pos, int force_p)
6348 ptrdiff_t original_pos = IT_CHARPOS (*it);
6350 reseat_1 (it, pos, 0);
6352 /* Determine where to check text properties. Avoid doing it
6353 where possible because text property lookup is very expensive. */
6354 if (force_p
6355 || CHARPOS (pos) > it->stop_charpos
6356 || CHARPOS (pos) < original_pos)
6358 if (it->bidi_p)
6360 /* For bidi iteration, we need to prime prev_stop and
6361 base_level_stop with our best estimations. */
6362 /* Implementation note: Of course, POS is not necessarily a
6363 stop position, so assigning prev_pos to it is a lie; we
6364 should have called compute_stop_backwards. However, if
6365 the current buffer does not include any R2L characters,
6366 that call would be a waste of cycles, because the
6367 iterator will never move back, and thus never cross this
6368 "fake" stop position. So we delay that backward search
6369 until the time we really need it, in next_element_from_buffer. */
6370 if (CHARPOS (pos) != it->prev_stop)
6371 it->prev_stop = CHARPOS (pos);
6372 if (CHARPOS (pos) < it->base_level_stop)
6373 it->base_level_stop = 0; /* meaning it's unknown */
6374 handle_stop (it);
6376 else
6378 handle_stop (it);
6379 it->prev_stop = it->base_level_stop = 0;
6384 CHECK_IT (it);
6388 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6389 IT->stop_pos to POS, also. */
6391 static void
6392 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6394 /* Don't call this function when scanning a C string. */
6395 eassert (it->s == NULL);
6397 /* POS must be a reasonable value. */
6398 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6400 it->current.pos = it->position = pos;
6401 it->end_charpos = ZV;
6402 it->dpvec = NULL;
6403 it->current.dpvec_index = -1;
6404 it->current.overlay_string_index = -1;
6405 IT_STRING_CHARPOS (*it) = -1;
6406 IT_STRING_BYTEPOS (*it) = -1;
6407 it->string = Qnil;
6408 it->method = GET_FROM_BUFFER;
6409 it->object = it->w->contents;
6410 it->area = TEXT_AREA;
6411 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6412 it->sp = 0;
6413 it->string_from_display_prop_p = 0;
6414 it->string_from_prefix_prop_p = 0;
6416 it->from_disp_prop_p = 0;
6417 it->face_before_selective_p = 0;
6418 if (it->bidi_p)
6420 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6421 &it->bidi_it);
6422 bidi_unshelve_cache (NULL, 0);
6423 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6424 it->bidi_it.string.s = NULL;
6425 it->bidi_it.string.lstring = Qnil;
6426 it->bidi_it.string.bufpos = 0;
6427 it->bidi_it.string.unibyte = 0;
6428 it->bidi_it.w = it->w;
6431 if (set_stop_p)
6433 it->stop_charpos = CHARPOS (pos);
6434 it->base_level_stop = CHARPOS (pos);
6436 /* This make the information stored in it->cmp_it invalidate. */
6437 it->cmp_it.id = -1;
6441 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6442 If S is non-null, it is a C string to iterate over. Otherwise,
6443 STRING gives a Lisp string to iterate over.
6445 If PRECISION > 0, don't return more then PRECISION number of
6446 characters from the string.
6448 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6449 characters have been returned. FIELD_WIDTH < 0 means an infinite
6450 field width.
6452 MULTIBYTE = 0 means disable processing of multibyte characters,
6453 MULTIBYTE > 0 means enable it,
6454 MULTIBYTE < 0 means use IT->multibyte_p.
6456 IT must be initialized via a prior call to init_iterator before
6457 calling this function. */
6459 static void
6460 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6461 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6462 int multibyte)
6464 /* No region in strings. */
6465 it->region_beg_charpos = it->region_end_charpos = -1;
6467 /* No text property checks performed by default, but see below. */
6468 it->stop_charpos = -1;
6470 /* Set iterator position and end position. */
6471 memset (&it->current, 0, sizeof it->current);
6472 it->current.overlay_string_index = -1;
6473 it->current.dpvec_index = -1;
6474 eassert (charpos >= 0);
6476 /* If STRING is specified, use its multibyteness, otherwise use the
6477 setting of MULTIBYTE, if specified. */
6478 if (multibyte >= 0)
6479 it->multibyte_p = multibyte > 0;
6481 /* Bidirectional reordering of strings is controlled by the default
6482 value of bidi-display-reordering. Don't try to reorder while
6483 loading loadup.el, as the necessary character property tables are
6484 not yet available. */
6485 it->bidi_p =
6486 NILP (Vpurify_flag)
6487 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6489 if (s == NULL)
6491 eassert (STRINGP (string));
6492 it->string = string;
6493 it->s = NULL;
6494 it->end_charpos = it->string_nchars = SCHARS (string);
6495 it->method = GET_FROM_STRING;
6496 it->current.string_pos = string_pos (charpos, string);
6498 if (it->bidi_p)
6500 it->bidi_it.string.lstring = string;
6501 it->bidi_it.string.s = NULL;
6502 it->bidi_it.string.schars = it->end_charpos;
6503 it->bidi_it.string.bufpos = 0;
6504 it->bidi_it.string.from_disp_str = 0;
6505 it->bidi_it.string.unibyte = !it->multibyte_p;
6506 it->bidi_it.w = it->w;
6507 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6508 FRAME_WINDOW_P (it->f), &it->bidi_it);
6511 else
6513 it->s = (const unsigned char *) s;
6514 it->string = Qnil;
6516 /* Note that we use IT->current.pos, not it->current.string_pos,
6517 for displaying C strings. */
6518 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6519 if (it->multibyte_p)
6521 it->current.pos = c_string_pos (charpos, s, 1);
6522 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6524 else
6526 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6527 it->end_charpos = it->string_nchars = strlen (s);
6530 if (it->bidi_p)
6532 it->bidi_it.string.lstring = Qnil;
6533 it->bidi_it.string.s = (const unsigned char *) s;
6534 it->bidi_it.string.schars = it->end_charpos;
6535 it->bidi_it.string.bufpos = 0;
6536 it->bidi_it.string.from_disp_str = 0;
6537 it->bidi_it.string.unibyte = !it->multibyte_p;
6538 it->bidi_it.w = it->w;
6539 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6540 &it->bidi_it);
6542 it->method = GET_FROM_C_STRING;
6545 /* PRECISION > 0 means don't return more than PRECISION characters
6546 from the string. */
6547 if (precision > 0 && it->end_charpos - charpos > precision)
6549 it->end_charpos = it->string_nchars = charpos + precision;
6550 if (it->bidi_p)
6551 it->bidi_it.string.schars = it->end_charpos;
6554 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6555 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6556 FIELD_WIDTH < 0 means infinite field width. This is useful for
6557 padding with `-' at the end of a mode line. */
6558 if (field_width < 0)
6559 field_width = INFINITY;
6560 /* Implementation note: We deliberately don't enlarge
6561 it->bidi_it.string.schars here to fit it->end_charpos, because
6562 the bidi iterator cannot produce characters out of thin air. */
6563 if (field_width > it->end_charpos - charpos)
6564 it->end_charpos = charpos + field_width;
6566 /* Use the standard display table for displaying strings. */
6567 if (DISP_TABLE_P (Vstandard_display_table))
6568 it->dp = XCHAR_TABLE (Vstandard_display_table);
6570 it->stop_charpos = charpos;
6571 it->prev_stop = charpos;
6572 it->base_level_stop = 0;
6573 if (it->bidi_p)
6575 it->bidi_it.first_elt = 1;
6576 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6577 it->bidi_it.disp_pos = -1;
6579 if (s == NULL && it->multibyte_p)
6581 ptrdiff_t endpos = SCHARS (it->string);
6582 if (endpos > it->end_charpos)
6583 endpos = it->end_charpos;
6584 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6585 it->string);
6587 CHECK_IT (it);
6592 /***********************************************************************
6593 Iteration
6594 ***********************************************************************/
6596 /* Map enum it_method value to corresponding next_element_from_* function. */
6598 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6600 next_element_from_buffer,
6601 next_element_from_display_vector,
6602 next_element_from_string,
6603 next_element_from_c_string,
6604 next_element_from_image,
6605 next_element_from_stretch
6608 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6611 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6612 (possibly with the following characters). */
6614 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6615 ((IT)->cmp_it.id >= 0 \
6616 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6617 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6618 END_CHARPOS, (IT)->w, \
6619 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6620 (IT)->string)))
6623 /* Lookup the char-table Vglyphless_char_display for character C (-1
6624 if we want information for no-font case), and return the display
6625 method symbol. By side-effect, update it->what and
6626 it->glyphless_method. This function is called from
6627 get_next_display_element for each character element, and from
6628 x_produce_glyphs when no suitable font was found. */
6630 Lisp_Object
6631 lookup_glyphless_char_display (int c, struct it *it)
6633 Lisp_Object glyphless_method = Qnil;
6635 if (CHAR_TABLE_P (Vglyphless_char_display)
6636 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6638 if (c >= 0)
6640 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6641 if (CONSP (glyphless_method))
6642 glyphless_method = FRAME_WINDOW_P (it->f)
6643 ? XCAR (glyphless_method)
6644 : XCDR (glyphless_method);
6646 else
6647 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6650 retry:
6651 if (NILP (glyphless_method))
6653 if (c >= 0)
6654 /* The default is to display the character by a proper font. */
6655 return Qnil;
6656 /* The default for the no-font case is to display an empty box. */
6657 glyphless_method = Qempty_box;
6659 if (EQ (glyphless_method, Qzero_width))
6661 if (c >= 0)
6662 return glyphless_method;
6663 /* This method can't be used for the no-font case. */
6664 glyphless_method = Qempty_box;
6666 if (EQ (glyphless_method, Qthin_space))
6667 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6668 else if (EQ (glyphless_method, Qempty_box))
6669 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6670 else if (EQ (glyphless_method, Qhex_code))
6671 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6672 else if (STRINGP (glyphless_method))
6673 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6674 else
6676 /* Invalid value. We use the default method. */
6677 glyphless_method = Qnil;
6678 goto retry;
6680 it->what = IT_GLYPHLESS;
6681 return glyphless_method;
6684 /* Load IT's display element fields with information about the next
6685 display element from the current position of IT. Value is zero if
6686 end of buffer (or C string) is reached. */
6688 static struct frame *last_escape_glyph_frame = NULL;
6689 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6690 static int last_escape_glyph_merged_face_id = 0;
6692 struct frame *last_glyphless_glyph_frame = NULL;
6693 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6694 int last_glyphless_glyph_merged_face_id = 0;
6696 static int
6697 get_next_display_element (struct it *it)
6699 /* Non-zero means that we found a display element. Zero means that
6700 we hit the end of what we iterate over. Performance note: the
6701 function pointer `method' used here turns out to be faster than
6702 using a sequence of if-statements. */
6703 int success_p;
6705 get_next:
6706 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6708 if (it->what == IT_CHARACTER)
6710 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6711 and only if (a) the resolved directionality of that character
6712 is R..." */
6713 /* FIXME: Do we need an exception for characters from display
6714 tables? */
6715 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6716 it->c = bidi_mirror_char (it->c);
6717 /* Map via display table or translate control characters.
6718 IT->c, IT->len etc. have been set to the next character by
6719 the function call above. If we have a display table, and it
6720 contains an entry for IT->c, translate it. Don't do this if
6721 IT->c itself comes from a display table, otherwise we could
6722 end up in an infinite recursion. (An alternative could be to
6723 count the recursion depth of this function and signal an
6724 error when a certain maximum depth is reached.) Is it worth
6725 it? */
6726 if (success_p && it->dpvec == NULL)
6728 Lisp_Object dv;
6729 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6730 int nonascii_space_p = 0;
6731 int nonascii_hyphen_p = 0;
6732 int c = it->c; /* This is the character to display. */
6734 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6736 eassert (SINGLE_BYTE_CHAR_P (c));
6737 if (unibyte_display_via_language_environment)
6739 c = DECODE_CHAR (unibyte, c);
6740 if (c < 0)
6741 c = BYTE8_TO_CHAR (it->c);
6743 else
6744 c = BYTE8_TO_CHAR (it->c);
6747 if (it->dp
6748 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6749 VECTORP (dv)))
6751 struct Lisp_Vector *v = XVECTOR (dv);
6753 /* Return the first character from the display table
6754 entry, if not empty. If empty, don't display the
6755 current character. */
6756 if (v->header.size)
6758 it->dpvec_char_len = it->len;
6759 it->dpvec = v->contents;
6760 it->dpend = v->contents + v->header.size;
6761 it->current.dpvec_index = 0;
6762 it->dpvec_face_id = -1;
6763 it->saved_face_id = it->face_id;
6764 it->method = GET_FROM_DISPLAY_VECTOR;
6765 it->ellipsis_p = 0;
6767 else
6769 set_iterator_to_next (it, 0);
6771 goto get_next;
6774 if (! NILP (lookup_glyphless_char_display (c, it)))
6776 if (it->what == IT_GLYPHLESS)
6777 goto done;
6778 /* Don't display this character. */
6779 set_iterator_to_next (it, 0);
6780 goto get_next;
6783 /* If `nobreak-char-display' is non-nil, we display
6784 non-ASCII spaces and hyphens specially. */
6785 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6787 if (c == 0xA0)
6788 nonascii_space_p = 1;
6789 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6790 nonascii_hyphen_p = 1;
6793 /* Translate control characters into `\003' or `^C' form.
6794 Control characters coming from a display table entry are
6795 currently not translated because we use IT->dpvec to hold
6796 the translation. This could easily be changed but I
6797 don't believe that it is worth doing.
6799 The characters handled by `nobreak-char-display' must be
6800 translated too.
6802 Non-printable characters and raw-byte characters are also
6803 translated to octal form. */
6804 if (((c < ' ' || c == 127) /* ASCII control chars */
6805 ? (it->area != TEXT_AREA
6806 /* In mode line, treat \n, \t like other crl chars. */
6807 || (c != '\t'
6808 && it->glyph_row
6809 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6810 || (c != '\n' && c != '\t'))
6811 : (nonascii_space_p
6812 || nonascii_hyphen_p
6813 || CHAR_BYTE8_P (c)
6814 || ! CHAR_PRINTABLE_P (c))))
6816 /* C is a control character, non-ASCII space/hyphen,
6817 raw-byte, or a non-printable character which must be
6818 displayed either as '\003' or as `^C' where the '\\'
6819 and '^' can be defined in the display table. Fill
6820 IT->ctl_chars with glyphs for what we have to
6821 display. Then, set IT->dpvec to these glyphs. */
6822 Lisp_Object gc;
6823 int ctl_len;
6824 int face_id;
6825 int lface_id = 0;
6826 int escape_glyph;
6828 /* Handle control characters with ^. */
6830 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6832 int g;
6834 g = '^'; /* default glyph for Control */
6835 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6836 if (it->dp
6837 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6839 g = GLYPH_CODE_CHAR (gc);
6840 lface_id = GLYPH_CODE_FACE (gc);
6842 if (lface_id)
6844 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6846 else if (it->f == last_escape_glyph_frame
6847 && it->face_id == last_escape_glyph_face_id)
6849 face_id = last_escape_glyph_merged_face_id;
6851 else
6853 /* Merge the escape-glyph face into the current face. */
6854 face_id = merge_faces (it->f, Qescape_glyph, 0,
6855 it->face_id);
6856 last_escape_glyph_frame = it->f;
6857 last_escape_glyph_face_id = it->face_id;
6858 last_escape_glyph_merged_face_id = face_id;
6861 XSETINT (it->ctl_chars[0], g);
6862 XSETINT (it->ctl_chars[1], c ^ 0100);
6863 ctl_len = 2;
6864 goto display_control;
6867 /* Handle non-ascii space in the mode where it only gets
6868 highlighting. */
6870 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6872 /* Merge `nobreak-space' into the current face. */
6873 face_id = merge_faces (it->f, Qnobreak_space, 0,
6874 it->face_id);
6875 XSETINT (it->ctl_chars[0], ' ');
6876 ctl_len = 1;
6877 goto display_control;
6880 /* Handle sequences that start with the "escape glyph". */
6882 /* the default escape glyph is \. */
6883 escape_glyph = '\\';
6885 if (it->dp
6886 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6888 escape_glyph = GLYPH_CODE_CHAR (gc);
6889 lface_id = GLYPH_CODE_FACE (gc);
6891 if (lface_id)
6893 /* The display table specified a face.
6894 Merge it into face_id and also into escape_glyph. */
6895 face_id = merge_faces (it->f, Qt, lface_id,
6896 it->face_id);
6898 else if (it->f == last_escape_glyph_frame
6899 && it->face_id == last_escape_glyph_face_id)
6901 face_id = last_escape_glyph_merged_face_id;
6903 else
6905 /* Merge the escape-glyph face into the current face. */
6906 face_id = merge_faces (it->f, Qescape_glyph, 0,
6907 it->face_id);
6908 last_escape_glyph_frame = it->f;
6909 last_escape_glyph_face_id = it->face_id;
6910 last_escape_glyph_merged_face_id = face_id;
6913 /* Draw non-ASCII hyphen with just highlighting: */
6915 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6917 XSETINT (it->ctl_chars[0], '-');
6918 ctl_len = 1;
6919 goto display_control;
6922 /* Draw non-ASCII space/hyphen with escape glyph: */
6924 if (nonascii_space_p || nonascii_hyphen_p)
6926 XSETINT (it->ctl_chars[0], escape_glyph);
6927 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6928 ctl_len = 2;
6929 goto display_control;
6933 char str[10];
6934 int len, i;
6936 if (CHAR_BYTE8_P (c))
6937 /* Display \200 instead of \17777600. */
6938 c = CHAR_TO_BYTE8 (c);
6939 len = sprintf (str, "%03o", c);
6941 XSETINT (it->ctl_chars[0], escape_glyph);
6942 for (i = 0; i < len; i++)
6943 XSETINT (it->ctl_chars[i + 1], str[i]);
6944 ctl_len = len + 1;
6947 display_control:
6948 /* Set up IT->dpvec and return first character from it. */
6949 it->dpvec_char_len = it->len;
6950 it->dpvec = it->ctl_chars;
6951 it->dpend = it->dpvec + ctl_len;
6952 it->current.dpvec_index = 0;
6953 it->dpvec_face_id = face_id;
6954 it->saved_face_id = it->face_id;
6955 it->method = GET_FROM_DISPLAY_VECTOR;
6956 it->ellipsis_p = 0;
6957 goto get_next;
6959 it->char_to_display = c;
6961 else if (success_p)
6963 it->char_to_display = it->c;
6967 /* Adjust face id for a multibyte character. There are no multibyte
6968 character in unibyte text. */
6969 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6970 && it->multibyte_p
6971 && success_p
6972 && FRAME_WINDOW_P (it->f))
6974 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6976 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6978 /* Automatic composition with glyph-string. */
6979 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6981 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6983 else
6985 ptrdiff_t pos = (it->s ? -1
6986 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6987 : IT_CHARPOS (*it));
6988 int c;
6990 if (it->what == IT_CHARACTER)
6991 c = it->char_to_display;
6992 else
6994 struct composition *cmp = composition_table[it->cmp_it.id];
6995 int i;
6997 c = ' ';
6998 for (i = 0; i < cmp->glyph_len; i++)
6999 /* TAB in a composition means display glyphs with
7000 padding space on the left or right. */
7001 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7002 break;
7004 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7008 done:
7009 /* Is this character the last one of a run of characters with
7010 box? If yes, set IT->end_of_box_run_p to 1. */
7011 if (it->face_box_p
7012 && it->s == NULL)
7014 if (it->method == GET_FROM_STRING && it->sp)
7016 int face_id = underlying_face_id (it);
7017 struct face *face = FACE_FROM_ID (it->f, face_id);
7019 if (face)
7021 if (face->box == FACE_NO_BOX)
7023 /* If the box comes from face properties in a
7024 display string, check faces in that string. */
7025 int string_face_id = face_after_it_pos (it);
7026 it->end_of_box_run_p
7027 = (FACE_FROM_ID (it->f, string_face_id)->box
7028 == FACE_NO_BOX);
7030 /* Otherwise, the box comes from the underlying face.
7031 If this is the last string character displayed, check
7032 the next buffer location. */
7033 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7034 && (it->current.overlay_string_index
7035 == it->n_overlay_strings - 1))
7037 ptrdiff_t ignore;
7038 int next_face_id;
7039 struct text_pos pos = it->current.pos;
7040 INC_TEXT_POS (pos, it->multibyte_p);
7042 next_face_id = face_at_buffer_position
7043 (it->w, CHARPOS (pos), it->region_beg_charpos,
7044 it->region_end_charpos, &ignore,
7045 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7046 -1);
7047 it->end_of_box_run_p
7048 = (FACE_FROM_ID (it->f, next_face_id)->box
7049 == FACE_NO_BOX);
7053 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7055 int face_id = face_after_it_pos (it);
7056 it->end_of_box_run_p
7057 = (face_id != it->face_id
7058 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7061 /* If we reached the end of the object we've been iterating (e.g., a
7062 display string or an overlay string), and there's something on
7063 IT->stack, proceed with what's on the stack. It doesn't make
7064 sense to return zero if there's unprocessed stuff on the stack,
7065 because otherwise that stuff will never be displayed. */
7066 if (!success_p && it->sp > 0)
7068 set_iterator_to_next (it, 0);
7069 success_p = get_next_display_element (it);
7072 /* Value is 0 if end of buffer or string reached. */
7073 return success_p;
7077 /* Move IT to the next display element.
7079 RESEAT_P non-zero means if called on a newline in buffer text,
7080 skip to the next visible line start.
7082 Functions get_next_display_element and set_iterator_to_next are
7083 separate because I find this arrangement easier to handle than a
7084 get_next_display_element function that also increments IT's
7085 position. The way it is we can first look at an iterator's current
7086 display element, decide whether it fits on a line, and if it does,
7087 increment the iterator position. The other way around we probably
7088 would either need a flag indicating whether the iterator has to be
7089 incremented the next time, or we would have to implement a
7090 decrement position function which would not be easy to write. */
7092 void
7093 set_iterator_to_next (struct it *it, int reseat_p)
7095 /* Reset flags indicating start and end of a sequence of characters
7096 with box. Reset them at the start of this function because
7097 moving the iterator to a new position might set them. */
7098 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7100 switch (it->method)
7102 case GET_FROM_BUFFER:
7103 /* The current display element of IT is a character from
7104 current_buffer. Advance in the buffer, and maybe skip over
7105 invisible lines that are so because of selective display. */
7106 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7107 reseat_at_next_visible_line_start (it, 0);
7108 else if (it->cmp_it.id >= 0)
7110 /* We are currently getting glyphs from a composition. */
7111 int i;
7113 if (! it->bidi_p)
7115 IT_CHARPOS (*it) += it->cmp_it.nchars;
7116 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7117 if (it->cmp_it.to < it->cmp_it.nglyphs)
7119 it->cmp_it.from = it->cmp_it.to;
7121 else
7123 it->cmp_it.id = -1;
7124 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7125 IT_BYTEPOS (*it),
7126 it->end_charpos, Qnil);
7129 else if (! it->cmp_it.reversed_p)
7131 /* Composition created while scanning forward. */
7132 /* Update IT's char/byte positions to point to the first
7133 character of the next grapheme cluster, or to the
7134 character visually after the current composition. */
7135 for (i = 0; i < it->cmp_it.nchars; i++)
7136 bidi_move_to_visually_next (&it->bidi_it);
7137 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7138 IT_CHARPOS (*it) = it->bidi_it.charpos;
7140 if (it->cmp_it.to < it->cmp_it.nglyphs)
7142 /* Proceed to the next grapheme cluster. */
7143 it->cmp_it.from = it->cmp_it.to;
7145 else
7147 /* No more grapheme clusters in this composition.
7148 Find the next stop position. */
7149 ptrdiff_t stop = it->end_charpos;
7150 if (it->bidi_it.scan_dir < 0)
7151 /* Now we are scanning backward and don't know
7152 where to stop. */
7153 stop = -1;
7154 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7155 IT_BYTEPOS (*it), stop, Qnil);
7158 else
7160 /* Composition created while scanning backward. */
7161 /* Update IT's char/byte positions to point to the last
7162 character of the previous grapheme cluster, or the
7163 character visually after the current composition. */
7164 for (i = 0; i < it->cmp_it.nchars; i++)
7165 bidi_move_to_visually_next (&it->bidi_it);
7166 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7167 IT_CHARPOS (*it) = it->bidi_it.charpos;
7168 if (it->cmp_it.from > 0)
7170 /* Proceed to the previous grapheme cluster. */
7171 it->cmp_it.to = it->cmp_it.from;
7173 else
7175 /* No more grapheme clusters in this composition.
7176 Find the next stop position. */
7177 ptrdiff_t stop = it->end_charpos;
7178 if (it->bidi_it.scan_dir < 0)
7179 /* Now we are scanning backward and don't know
7180 where to stop. */
7181 stop = -1;
7182 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7183 IT_BYTEPOS (*it), stop, Qnil);
7187 else
7189 eassert (it->len != 0);
7191 if (!it->bidi_p)
7193 IT_BYTEPOS (*it) += it->len;
7194 IT_CHARPOS (*it) += 1;
7196 else
7198 int prev_scan_dir = it->bidi_it.scan_dir;
7199 /* If this is a new paragraph, determine its base
7200 direction (a.k.a. its base embedding level). */
7201 if (it->bidi_it.new_paragraph)
7202 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7203 bidi_move_to_visually_next (&it->bidi_it);
7204 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7205 IT_CHARPOS (*it) = it->bidi_it.charpos;
7206 if (prev_scan_dir != it->bidi_it.scan_dir)
7208 /* As the scan direction was changed, we must
7209 re-compute the stop position for composition. */
7210 ptrdiff_t stop = it->end_charpos;
7211 if (it->bidi_it.scan_dir < 0)
7212 stop = -1;
7213 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7214 IT_BYTEPOS (*it), stop, Qnil);
7217 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7219 break;
7221 case GET_FROM_C_STRING:
7222 /* Current display element of IT is from a C string. */
7223 if (!it->bidi_p
7224 /* If the string position is beyond string's end, it means
7225 next_element_from_c_string is padding the string with
7226 blanks, in which case we bypass the bidi iterator,
7227 because it cannot deal with such virtual characters. */
7228 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7230 IT_BYTEPOS (*it) += it->len;
7231 IT_CHARPOS (*it) += 1;
7233 else
7235 bidi_move_to_visually_next (&it->bidi_it);
7236 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7237 IT_CHARPOS (*it) = it->bidi_it.charpos;
7239 break;
7241 case GET_FROM_DISPLAY_VECTOR:
7242 /* Current display element of IT is from a display table entry.
7243 Advance in the display table definition. Reset it to null if
7244 end reached, and continue with characters from buffers/
7245 strings. */
7246 ++it->current.dpvec_index;
7248 /* Restore face of the iterator to what they were before the
7249 display vector entry (these entries may contain faces). */
7250 it->face_id = it->saved_face_id;
7252 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7254 int recheck_faces = it->ellipsis_p;
7256 if (it->s)
7257 it->method = GET_FROM_C_STRING;
7258 else if (STRINGP (it->string))
7259 it->method = GET_FROM_STRING;
7260 else
7262 it->method = GET_FROM_BUFFER;
7263 it->object = it->w->contents;
7266 it->dpvec = NULL;
7267 it->current.dpvec_index = -1;
7269 /* Skip over characters which were displayed via IT->dpvec. */
7270 if (it->dpvec_char_len < 0)
7271 reseat_at_next_visible_line_start (it, 1);
7272 else if (it->dpvec_char_len > 0)
7274 if (it->method == GET_FROM_STRING
7275 && it->current.overlay_string_index >= 0
7276 && it->n_overlay_strings > 0)
7277 it->ignore_overlay_strings_at_pos_p = 1;
7278 it->len = it->dpvec_char_len;
7279 set_iterator_to_next (it, reseat_p);
7282 /* Maybe recheck faces after display vector */
7283 if (recheck_faces)
7284 it->stop_charpos = IT_CHARPOS (*it);
7286 break;
7288 case GET_FROM_STRING:
7289 /* Current display element is a character from a Lisp string. */
7290 eassert (it->s == NULL && STRINGP (it->string));
7291 /* Don't advance past string end. These conditions are true
7292 when set_iterator_to_next is called at the end of
7293 get_next_display_element, in which case the Lisp string is
7294 already exhausted, and all we want is pop the iterator
7295 stack. */
7296 if (it->current.overlay_string_index >= 0)
7298 /* This is an overlay string, so there's no padding with
7299 spaces, and the number of characters in the string is
7300 where the string ends. */
7301 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7302 goto consider_string_end;
7304 else
7306 /* Not an overlay string. There could be padding, so test
7307 against it->end_charpos . */
7308 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7309 goto consider_string_end;
7311 if (it->cmp_it.id >= 0)
7313 int i;
7315 if (! it->bidi_p)
7317 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7318 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7319 if (it->cmp_it.to < it->cmp_it.nglyphs)
7320 it->cmp_it.from = it->cmp_it.to;
7321 else
7323 it->cmp_it.id = -1;
7324 composition_compute_stop_pos (&it->cmp_it,
7325 IT_STRING_CHARPOS (*it),
7326 IT_STRING_BYTEPOS (*it),
7327 it->end_charpos, it->string);
7330 else if (! it->cmp_it.reversed_p)
7332 for (i = 0; i < it->cmp_it.nchars; i++)
7333 bidi_move_to_visually_next (&it->bidi_it);
7334 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7335 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7337 if (it->cmp_it.to < it->cmp_it.nglyphs)
7338 it->cmp_it.from = it->cmp_it.to;
7339 else
7341 ptrdiff_t stop = it->end_charpos;
7342 if (it->bidi_it.scan_dir < 0)
7343 stop = -1;
7344 composition_compute_stop_pos (&it->cmp_it,
7345 IT_STRING_CHARPOS (*it),
7346 IT_STRING_BYTEPOS (*it), stop,
7347 it->string);
7350 else
7352 for (i = 0; i < it->cmp_it.nchars; i++)
7353 bidi_move_to_visually_next (&it->bidi_it);
7354 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7355 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7356 if (it->cmp_it.from > 0)
7357 it->cmp_it.to = it->cmp_it.from;
7358 else
7360 ptrdiff_t stop = it->end_charpos;
7361 if (it->bidi_it.scan_dir < 0)
7362 stop = -1;
7363 composition_compute_stop_pos (&it->cmp_it,
7364 IT_STRING_CHARPOS (*it),
7365 IT_STRING_BYTEPOS (*it), stop,
7366 it->string);
7370 else
7372 if (!it->bidi_p
7373 /* If the string position is beyond string's end, it
7374 means next_element_from_string is padding the string
7375 with blanks, in which case we bypass the bidi
7376 iterator, because it cannot deal with such virtual
7377 characters. */
7378 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7380 IT_STRING_BYTEPOS (*it) += it->len;
7381 IT_STRING_CHARPOS (*it) += 1;
7383 else
7385 int prev_scan_dir = it->bidi_it.scan_dir;
7387 bidi_move_to_visually_next (&it->bidi_it);
7388 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7389 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7390 if (prev_scan_dir != it->bidi_it.scan_dir)
7392 ptrdiff_t stop = it->end_charpos;
7394 if (it->bidi_it.scan_dir < 0)
7395 stop = -1;
7396 composition_compute_stop_pos (&it->cmp_it,
7397 IT_STRING_CHARPOS (*it),
7398 IT_STRING_BYTEPOS (*it), stop,
7399 it->string);
7404 consider_string_end:
7406 if (it->current.overlay_string_index >= 0)
7408 /* IT->string is an overlay string. Advance to the
7409 next, if there is one. */
7410 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7412 it->ellipsis_p = 0;
7413 next_overlay_string (it);
7414 if (it->ellipsis_p)
7415 setup_for_ellipsis (it, 0);
7418 else
7420 /* IT->string is not an overlay string. If we reached
7421 its end, and there is something on IT->stack, proceed
7422 with what is on the stack. This can be either another
7423 string, this time an overlay string, or a buffer. */
7424 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7425 && it->sp > 0)
7427 pop_it (it);
7428 if (it->method == GET_FROM_STRING)
7429 goto consider_string_end;
7432 break;
7434 case GET_FROM_IMAGE:
7435 case GET_FROM_STRETCH:
7436 /* The position etc with which we have to proceed are on
7437 the stack. The position may be at the end of a string,
7438 if the `display' property takes up the whole string. */
7439 eassert (it->sp > 0);
7440 pop_it (it);
7441 if (it->method == GET_FROM_STRING)
7442 goto consider_string_end;
7443 break;
7445 default:
7446 /* There are no other methods defined, so this should be a bug. */
7447 emacs_abort ();
7450 eassert (it->method != GET_FROM_STRING
7451 || (STRINGP (it->string)
7452 && IT_STRING_CHARPOS (*it) >= 0));
7455 /* Load IT's display element fields with information about the next
7456 display element which comes from a display table entry or from the
7457 result of translating a control character to one of the forms `^C'
7458 or `\003'.
7460 IT->dpvec holds the glyphs to return as characters.
7461 IT->saved_face_id holds the face id before the display vector--it
7462 is restored into IT->face_id in set_iterator_to_next. */
7464 static int
7465 next_element_from_display_vector (struct it *it)
7467 Lisp_Object gc;
7468 int prev_face_id = it->face_id;
7469 int next_face_id;
7471 /* Precondition. */
7472 eassert (it->dpvec && it->current.dpvec_index >= 0);
7474 it->face_id = it->saved_face_id;
7476 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7477 That seemed totally bogus - so I changed it... */
7478 gc = it->dpvec[it->current.dpvec_index];
7480 if (GLYPH_CODE_P (gc))
7482 struct face *this_face, *prev_face, *next_face;
7484 it->c = GLYPH_CODE_CHAR (gc);
7485 it->len = CHAR_BYTES (it->c);
7487 /* The entry may contain a face id to use. Such a face id is
7488 the id of a Lisp face, not a realized face. A face id of
7489 zero means no face is specified. */
7490 if (it->dpvec_face_id >= 0)
7491 it->face_id = it->dpvec_face_id;
7492 else
7494 int lface_id = GLYPH_CODE_FACE (gc);
7495 if (lface_id > 0)
7496 it->face_id = merge_faces (it->f, Qt, lface_id,
7497 it->saved_face_id);
7500 /* Glyphs in the display vector could have the box face, so we
7501 need to set the related flags in the iterator, as
7502 appropriate. */
7503 this_face = FACE_FROM_ID (it->f, it->face_id);
7504 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7506 /* Is this character the first character of a box-face run? */
7507 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7508 && (!prev_face
7509 || prev_face->box == FACE_NO_BOX));
7511 /* For the last character of the box-face run, we need to look
7512 either at the next glyph from the display vector, or at the
7513 face we saw before the display vector. */
7514 next_face_id = it->saved_face_id;
7515 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7517 if (it->dpvec_face_id >= 0)
7518 next_face_id = it->dpvec_face_id;
7519 else
7521 int lface_id =
7522 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7524 if (lface_id > 0)
7525 next_face_id = merge_faces (it->f, Qt, lface_id,
7526 it->saved_face_id);
7529 next_face = FACE_FROM_ID (it->f, next_face_id);
7530 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7531 && (!next_face
7532 || next_face->box == FACE_NO_BOX));
7533 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7535 else
7536 /* Display table entry is invalid. Return a space. */
7537 it->c = ' ', it->len = 1;
7539 /* Don't change position and object of the iterator here. They are
7540 still the values of the character that had this display table
7541 entry or was translated, and that's what we want. */
7542 it->what = IT_CHARACTER;
7543 return 1;
7546 /* Get the first element of string/buffer in the visual order, after
7547 being reseated to a new position in a string or a buffer. */
7548 static void
7549 get_visually_first_element (struct it *it)
7551 int string_p = STRINGP (it->string) || it->s;
7552 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7553 ptrdiff_t bob = (string_p ? 0 : BEGV);
7555 if (STRINGP (it->string))
7557 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7558 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7560 else
7562 it->bidi_it.charpos = IT_CHARPOS (*it);
7563 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7566 if (it->bidi_it.charpos == eob)
7568 /* Nothing to do, but reset the FIRST_ELT flag, like
7569 bidi_paragraph_init does, because we are not going to
7570 call it. */
7571 it->bidi_it.first_elt = 0;
7573 else if (it->bidi_it.charpos == bob
7574 || (!string_p
7575 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7576 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7578 /* If we are at the beginning of a line/string, we can produce
7579 the next element right away. */
7580 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7581 bidi_move_to_visually_next (&it->bidi_it);
7583 else
7585 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7587 /* We need to prime the bidi iterator starting at the line's or
7588 string's beginning, before we will be able to produce the
7589 next element. */
7590 if (string_p)
7591 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7592 else
7593 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7594 IT_BYTEPOS (*it), -1,
7595 &it->bidi_it.bytepos);
7596 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7599 /* Now return to buffer/string position where we were asked
7600 to get the next display element, and produce that. */
7601 bidi_move_to_visually_next (&it->bidi_it);
7603 while (it->bidi_it.bytepos != orig_bytepos
7604 && it->bidi_it.charpos < eob);
7607 /* Adjust IT's position information to where we ended up. */
7608 if (STRINGP (it->string))
7610 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7611 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7613 else
7615 IT_CHARPOS (*it) = it->bidi_it.charpos;
7616 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7619 if (STRINGP (it->string) || !it->s)
7621 ptrdiff_t stop, charpos, bytepos;
7623 if (STRINGP (it->string))
7625 eassert (!it->s);
7626 stop = SCHARS (it->string);
7627 if (stop > it->end_charpos)
7628 stop = it->end_charpos;
7629 charpos = IT_STRING_CHARPOS (*it);
7630 bytepos = IT_STRING_BYTEPOS (*it);
7632 else
7634 stop = it->end_charpos;
7635 charpos = IT_CHARPOS (*it);
7636 bytepos = IT_BYTEPOS (*it);
7638 if (it->bidi_it.scan_dir < 0)
7639 stop = -1;
7640 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7641 it->string);
7645 /* Load IT with the next display element from Lisp string IT->string.
7646 IT->current.string_pos is the current position within the string.
7647 If IT->current.overlay_string_index >= 0, the Lisp string is an
7648 overlay string. */
7650 static int
7651 next_element_from_string (struct it *it)
7653 struct text_pos position;
7655 eassert (STRINGP (it->string));
7656 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7657 eassert (IT_STRING_CHARPOS (*it) >= 0);
7658 position = it->current.string_pos;
7660 /* With bidi reordering, the character to display might not be the
7661 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7662 that we were reseat()ed to a new string, whose paragraph
7663 direction is not known. */
7664 if (it->bidi_p && it->bidi_it.first_elt)
7666 get_visually_first_element (it);
7667 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7670 /* Time to check for invisible text? */
7671 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7673 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7675 if (!(!it->bidi_p
7676 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7677 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7679 /* With bidi non-linear iteration, we could find
7680 ourselves far beyond the last computed stop_charpos,
7681 with several other stop positions in between that we
7682 missed. Scan them all now, in buffer's logical
7683 order, until we find and handle the last stop_charpos
7684 that precedes our current position. */
7685 handle_stop_backwards (it, it->stop_charpos);
7686 return GET_NEXT_DISPLAY_ELEMENT (it);
7688 else
7690 if (it->bidi_p)
7692 /* Take note of the stop position we just moved
7693 across, for when we will move back across it. */
7694 it->prev_stop = it->stop_charpos;
7695 /* If we are at base paragraph embedding level, take
7696 note of the last stop position seen at this
7697 level. */
7698 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7699 it->base_level_stop = it->stop_charpos;
7701 handle_stop (it);
7703 /* Since a handler may have changed IT->method, we must
7704 recurse here. */
7705 return GET_NEXT_DISPLAY_ELEMENT (it);
7708 else if (it->bidi_p
7709 /* If we are before prev_stop, we may have overstepped
7710 on our way backwards a stop_pos, and if so, we need
7711 to handle that stop_pos. */
7712 && IT_STRING_CHARPOS (*it) < it->prev_stop
7713 /* We can sometimes back up for reasons that have nothing
7714 to do with bidi reordering. E.g., compositions. The
7715 code below is only needed when we are above the base
7716 embedding level, so test for that explicitly. */
7717 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7719 /* If we lost track of base_level_stop, we have no better
7720 place for handle_stop_backwards to start from than string
7721 beginning. This happens, e.g., when we were reseated to
7722 the previous screenful of text by vertical-motion. */
7723 if (it->base_level_stop <= 0
7724 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7725 it->base_level_stop = 0;
7726 handle_stop_backwards (it, it->base_level_stop);
7727 return GET_NEXT_DISPLAY_ELEMENT (it);
7731 if (it->current.overlay_string_index >= 0)
7733 /* Get the next character from an overlay string. In overlay
7734 strings, there is no field width or padding with spaces to
7735 do. */
7736 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7738 it->what = IT_EOB;
7739 return 0;
7741 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7742 IT_STRING_BYTEPOS (*it),
7743 it->bidi_it.scan_dir < 0
7744 ? -1
7745 : SCHARS (it->string))
7746 && next_element_from_composition (it))
7748 return 1;
7750 else if (STRING_MULTIBYTE (it->string))
7752 const unsigned char *s = (SDATA (it->string)
7753 + IT_STRING_BYTEPOS (*it));
7754 it->c = string_char_and_length (s, &it->len);
7756 else
7758 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7759 it->len = 1;
7762 else
7764 /* Get the next character from a Lisp string that is not an
7765 overlay string. Such strings come from the mode line, for
7766 example. We may have to pad with spaces, or truncate the
7767 string. See also next_element_from_c_string. */
7768 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7770 it->what = IT_EOB;
7771 return 0;
7773 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7775 /* Pad with spaces. */
7776 it->c = ' ', it->len = 1;
7777 CHARPOS (position) = BYTEPOS (position) = -1;
7779 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7780 IT_STRING_BYTEPOS (*it),
7781 it->bidi_it.scan_dir < 0
7782 ? -1
7783 : it->string_nchars)
7784 && next_element_from_composition (it))
7786 return 1;
7788 else if (STRING_MULTIBYTE (it->string))
7790 const unsigned char *s = (SDATA (it->string)
7791 + IT_STRING_BYTEPOS (*it));
7792 it->c = string_char_and_length (s, &it->len);
7794 else
7796 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7797 it->len = 1;
7801 /* Record what we have and where it came from. */
7802 it->what = IT_CHARACTER;
7803 it->object = it->string;
7804 it->position = position;
7805 return 1;
7809 /* Load IT with next display element from C string IT->s.
7810 IT->string_nchars is the maximum number of characters to return
7811 from the string. IT->end_charpos may be greater than
7812 IT->string_nchars when this function is called, in which case we
7813 may have to return padding spaces. Value is zero if end of string
7814 reached, including padding spaces. */
7816 static int
7817 next_element_from_c_string (struct it *it)
7819 int success_p = 1;
7821 eassert (it->s);
7822 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7823 it->what = IT_CHARACTER;
7824 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7825 it->object = Qnil;
7827 /* With bidi reordering, the character to display might not be the
7828 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7829 we were reseated to a new string, whose paragraph direction is
7830 not known. */
7831 if (it->bidi_p && it->bidi_it.first_elt)
7832 get_visually_first_element (it);
7834 /* IT's position can be greater than IT->string_nchars in case a
7835 field width or precision has been specified when the iterator was
7836 initialized. */
7837 if (IT_CHARPOS (*it) >= it->end_charpos)
7839 /* End of the game. */
7840 it->what = IT_EOB;
7841 success_p = 0;
7843 else if (IT_CHARPOS (*it) >= it->string_nchars)
7845 /* Pad with spaces. */
7846 it->c = ' ', it->len = 1;
7847 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7849 else if (it->multibyte_p)
7850 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7851 else
7852 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7854 return success_p;
7858 /* Set up IT to return characters from an ellipsis, if appropriate.
7859 The definition of the ellipsis glyphs may come from a display table
7860 entry. This function fills IT with the first glyph from the
7861 ellipsis if an ellipsis is to be displayed. */
7863 static int
7864 next_element_from_ellipsis (struct it *it)
7866 if (it->selective_display_ellipsis_p)
7867 setup_for_ellipsis (it, it->len);
7868 else
7870 /* The face at the current position may be different from the
7871 face we find after the invisible text. Remember what it
7872 was in IT->saved_face_id, and signal that it's there by
7873 setting face_before_selective_p. */
7874 it->saved_face_id = it->face_id;
7875 it->method = GET_FROM_BUFFER;
7876 it->object = it->w->contents;
7877 reseat_at_next_visible_line_start (it, 1);
7878 it->face_before_selective_p = 1;
7881 return GET_NEXT_DISPLAY_ELEMENT (it);
7885 /* Deliver an image display element. The iterator IT is already
7886 filled with image information (done in handle_display_prop). Value
7887 is always 1. */
7890 static int
7891 next_element_from_image (struct it *it)
7893 it->what = IT_IMAGE;
7894 it->ignore_overlay_strings_at_pos_p = 0;
7895 return 1;
7899 /* Fill iterator IT with next display element from a stretch glyph
7900 property. IT->object is the value of the text property. Value is
7901 always 1. */
7903 static int
7904 next_element_from_stretch (struct it *it)
7906 it->what = IT_STRETCH;
7907 return 1;
7910 /* Scan backwards from IT's current position until we find a stop
7911 position, or until BEGV. This is called when we find ourself
7912 before both the last known prev_stop and base_level_stop while
7913 reordering bidirectional text. */
7915 static void
7916 compute_stop_pos_backwards (struct it *it)
7918 const int SCAN_BACK_LIMIT = 1000;
7919 struct text_pos pos;
7920 struct display_pos save_current = it->current;
7921 struct text_pos save_position = it->position;
7922 ptrdiff_t charpos = IT_CHARPOS (*it);
7923 ptrdiff_t where_we_are = charpos;
7924 ptrdiff_t save_stop_pos = it->stop_charpos;
7925 ptrdiff_t save_end_pos = it->end_charpos;
7927 eassert (NILP (it->string) && !it->s);
7928 eassert (it->bidi_p);
7929 it->bidi_p = 0;
7932 it->end_charpos = min (charpos + 1, ZV);
7933 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7934 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7935 reseat_1 (it, pos, 0);
7936 compute_stop_pos (it);
7937 /* We must advance forward, right? */
7938 if (it->stop_charpos <= charpos)
7939 emacs_abort ();
7941 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7943 if (it->stop_charpos <= where_we_are)
7944 it->prev_stop = it->stop_charpos;
7945 else
7946 it->prev_stop = BEGV;
7947 it->bidi_p = 1;
7948 it->current = save_current;
7949 it->position = save_position;
7950 it->stop_charpos = save_stop_pos;
7951 it->end_charpos = save_end_pos;
7954 /* Scan forward from CHARPOS in the current buffer/string, until we
7955 find a stop position > current IT's position. Then handle the stop
7956 position before that. This is called when we bump into a stop
7957 position while reordering bidirectional text. CHARPOS should be
7958 the last previously processed stop_pos (or BEGV/0, if none were
7959 processed yet) whose position is less that IT's current
7960 position. */
7962 static void
7963 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7965 int bufp = !STRINGP (it->string);
7966 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7967 struct display_pos save_current = it->current;
7968 struct text_pos save_position = it->position;
7969 struct text_pos pos1;
7970 ptrdiff_t next_stop;
7972 /* Scan in strict logical order. */
7973 eassert (it->bidi_p);
7974 it->bidi_p = 0;
7977 it->prev_stop = charpos;
7978 if (bufp)
7980 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7981 reseat_1 (it, pos1, 0);
7983 else
7984 it->current.string_pos = string_pos (charpos, it->string);
7985 compute_stop_pos (it);
7986 /* We must advance forward, right? */
7987 if (it->stop_charpos <= it->prev_stop)
7988 emacs_abort ();
7989 charpos = it->stop_charpos;
7991 while (charpos <= where_we_are);
7993 it->bidi_p = 1;
7994 it->current = save_current;
7995 it->position = save_position;
7996 next_stop = it->stop_charpos;
7997 it->stop_charpos = it->prev_stop;
7998 handle_stop (it);
7999 it->stop_charpos = next_stop;
8002 /* Load IT with the next display element from current_buffer. Value
8003 is zero if end of buffer reached. IT->stop_charpos is the next
8004 position at which to stop and check for text properties or buffer
8005 end. */
8007 static int
8008 next_element_from_buffer (struct it *it)
8010 int success_p = 1;
8012 eassert (IT_CHARPOS (*it) >= BEGV);
8013 eassert (NILP (it->string) && !it->s);
8014 eassert (!it->bidi_p
8015 || (EQ (it->bidi_it.string.lstring, Qnil)
8016 && it->bidi_it.string.s == NULL));
8018 /* With bidi reordering, the character to display might not be the
8019 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8020 we were reseat()ed to a new buffer position, which is potentially
8021 a different paragraph. */
8022 if (it->bidi_p && it->bidi_it.first_elt)
8024 get_visually_first_element (it);
8025 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8028 if (IT_CHARPOS (*it) >= it->stop_charpos)
8030 if (IT_CHARPOS (*it) >= it->end_charpos)
8032 int overlay_strings_follow_p;
8034 /* End of the game, except when overlay strings follow that
8035 haven't been returned yet. */
8036 if (it->overlay_strings_at_end_processed_p)
8037 overlay_strings_follow_p = 0;
8038 else
8040 it->overlay_strings_at_end_processed_p = 1;
8041 overlay_strings_follow_p = get_overlay_strings (it, 0);
8044 if (overlay_strings_follow_p)
8045 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8046 else
8048 it->what = IT_EOB;
8049 it->position = it->current.pos;
8050 success_p = 0;
8053 else if (!(!it->bidi_p
8054 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8055 || IT_CHARPOS (*it) == it->stop_charpos))
8057 /* With bidi non-linear iteration, we could find ourselves
8058 far beyond the last computed stop_charpos, with several
8059 other stop positions in between that we missed. Scan
8060 them all now, in buffer's logical order, until we find
8061 and handle the last stop_charpos that precedes our
8062 current position. */
8063 handle_stop_backwards (it, it->stop_charpos);
8064 return GET_NEXT_DISPLAY_ELEMENT (it);
8066 else
8068 if (it->bidi_p)
8070 /* Take note of the stop position we just moved across,
8071 for when we will move back across it. */
8072 it->prev_stop = it->stop_charpos;
8073 /* If we are at base paragraph embedding level, take
8074 note of the last stop position seen at this
8075 level. */
8076 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8077 it->base_level_stop = it->stop_charpos;
8079 handle_stop (it);
8080 return GET_NEXT_DISPLAY_ELEMENT (it);
8083 else if (it->bidi_p
8084 /* If we are before prev_stop, we may have overstepped on
8085 our way backwards a stop_pos, and if so, we need to
8086 handle that stop_pos. */
8087 && IT_CHARPOS (*it) < it->prev_stop
8088 /* We can sometimes back up for reasons that have nothing
8089 to do with bidi reordering. E.g., compositions. The
8090 code below is only needed when we are above the base
8091 embedding level, so test for that explicitly. */
8092 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8094 if (it->base_level_stop <= 0
8095 || IT_CHARPOS (*it) < it->base_level_stop)
8097 /* If we lost track of base_level_stop, we need to find
8098 prev_stop by looking backwards. This happens, e.g., when
8099 we were reseated to the previous screenful of text by
8100 vertical-motion. */
8101 it->base_level_stop = BEGV;
8102 compute_stop_pos_backwards (it);
8103 handle_stop_backwards (it, it->prev_stop);
8105 else
8106 handle_stop_backwards (it, it->base_level_stop);
8107 return GET_NEXT_DISPLAY_ELEMENT (it);
8109 else
8111 /* No face changes, overlays etc. in sight, so just return a
8112 character from current_buffer. */
8113 unsigned char *p;
8114 ptrdiff_t stop;
8116 /* Maybe run the redisplay end trigger hook. Performance note:
8117 This doesn't seem to cost measurable time. */
8118 if (it->redisplay_end_trigger_charpos
8119 && it->glyph_row
8120 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8121 run_redisplay_end_trigger_hook (it);
8123 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8124 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8125 stop)
8126 && next_element_from_composition (it))
8128 return 1;
8131 /* Get the next character, maybe multibyte. */
8132 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8133 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8134 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8135 else
8136 it->c = *p, it->len = 1;
8138 /* Record what we have and where it came from. */
8139 it->what = IT_CHARACTER;
8140 it->object = it->w->contents;
8141 it->position = it->current.pos;
8143 /* Normally we return the character found above, except when we
8144 really want to return an ellipsis for selective display. */
8145 if (it->selective)
8147 if (it->c == '\n')
8149 /* A value of selective > 0 means hide lines indented more
8150 than that number of columns. */
8151 if (it->selective > 0
8152 && IT_CHARPOS (*it) + 1 < ZV
8153 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8154 IT_BYTEPOS (*it) + 1,
8155 it->selective))
8157 success_p = next_element_from_ellipsis (it);
8158 it->dpvec_char_len = -1;
8161 else if (it->c == '\r' && it->selective == -1)
8163 /* A value of selective == -1 means that everything from the
8164 CR to the end of the line is invisible, with maybe an
8165 ellipsis displayed for it. */
8166 success_p = next_element_from_ellipsis (it);
8167 it->dpvec_char_len = -1;
8172 /* Value is zero if end of buffer reached. */
8173 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8174 return success_p;
8178 /* Run the redisplay end trigger hook for IT. */
8180 static void
8181 run_redisplay_end_trigger_hook (struct it *it)
8183 Lisp_Object args[3];
8185 /* IT->glyph_row should be non-null, i.e. we should be actually
8186 displaying something, or otherwise we should not run the hook. */
8187 eassert (it->glyph_row);
8189 /* Set up hook arguments. */
8190 args[0] = Qredisplay_end_trigger_functions;
8191 args[1] = it->window;
8192 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8193 it->redisplay_end_trigger_charpos = 0;
8195 /* Since we are *trying* to run these functions, don't try to run
8196 them again, even if they get an error. */
8197 wset_redisplay_end_trigger (it->w, Qnil);
8198 Frun_hook_with_args (3, args);
8200 /* Notice if it changed the face of the character we are on. */
8201 handle_face_prop (it);
8205 /* Deliver a composition display element. Unlike the other
8206 next_element_from_XXX, this function is not registered in the array
8207 get_next_element[]. It is called from next_element_from_buffer and
8208 next_element_from_string when necessary. */
8210 static int
8211 next_element_from_composition (struct it *it)
8213 it->what = IT_COMPOSITION;
8214 it->len = it->cmp_it.nbytes;
8215 if (STRINGP (it->string))
8217 if (it->c < 0)
8219 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8220 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8221 return 0;
8223 it->position = it->current.string_pos;
8224 it->object = it->string;
8225 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8226 IT_STRING_BYTEPOS (*it), it->string);
8228 else
8230 if (it->c < 0)
8232 IT_CHARPOS (*it) += it->cmp_it.nchars;
8233 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8234 if (it->bidi_p)
8236 if (it->bidi_it.new_paragraph)
8237 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8238 /* Resync the bidi iterator with IT's new position.
8239 FIXME: this doesn't support bidirectional text. */
8240 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8241 bidi_move_to_visually_next (&it->bidi_it);
8243 return 0;
8245 it->position = it->current.pos;
8246 it->object = it->w->contents;
8247 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8248 IT_BYTEPOS (*it), Qnil);
8250 return 1;
8255 /***********************************************************************
8256 Moving an iterator without producing glyphs
8257 ***********************************************************************/
8259 /* Check if iterator is at a position corresponding to a valid buffer
8260 position after some move_it_ call. */
8262 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8263 ((it)->method == GET_FROM_STRING \
8264 ? IT_STRING_CHARPOS (*it) == 0 \
8265 : 1)
8268 /* Move iterator IT to a specified buffer or X position within one
8269 line on the display without producing glyphs.
8271 OP should be a bit mask including some or all of these bits:
8272 MOVE_TO_X: Stop upon reaching x-position TO_X.
8273 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8274 Regardless of OP's value, stop upon reaching the end of the display line.
8276 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8277 This means, in particular, that TO_X includes window's horizontal
8278 scroll amount.
8280 The return value has several possible values that
8281 say what condition caused the scan to stop:
8283 MOVE_POS_MATCH_OR_ZV
8284 - when TO_POS or ZV was reached.
8286 MOVE_X_REACHED
8287 -when TO_X was reached before TO_POS or ZV were reached.
8289 MOVE_LINE_CONTINUED
8290 - when we reached the end of the display area and the line must
8291 be continued.
8293 MOVE_LINE_TRUNCATED
8294 - when we reached the end of the display area and the line is
8295 truncated.
8297 MOVE_NEWLINE_OR_CR
8298 - when we stopped at a line end, i.e. a newline or a CR and selective
8299 display is on. */
8301 static enum move_it_result
8302 move_it_in_display_line_to (struct it *it,
8303 ptrdiff_t to_charpos, int to_x,
8304 enum move_operation_enum op)
8306 enum move_it_result result = MOVE_UNDEFINED;
8307 struct glyph_row *saved_glyph_row;
8308 struct it wrap_it, atpos_it, atx_it, ppos_it;
8309 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8310 void *ppos_data = NULL;
8311 int may_wrap = 0;
8312 enum it_method prev_method = it->method;
8313 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8314 int saw_smaller_pos = prev_pos < to_charpos;
8316 /* Don't produce glyphs in produce_glyphs. */
8317 saved_glyph_row = it->glyph_row;
8318 it->glyph_row = NULL;
8320 /* Use wrap_it to save a copy of IT wherever a word wrap could
8321 occur. Use atpos_it to save a copy of IT at the desired buffer
8322 position, if found, so that we can scan ahead and check if the
8323 word later overshoots the window edge. Use atx_it similarly, for
8324 pixel positions. */
8325 wrap_it.sp = -1;
8326 atpos_it.sp = -1;
8327 atx_it.sp = -1;
8329 /* Use ppos_it under bidi reordering to save a copy of IT for the
8330 position > CHARPOS that is the closest to CHARPOS. We restore
8331 that position in IT when we have scanned the entire display line
8332 without finding a match for CHARPOS and all the character
8333 positions are greater than CHARPOS. */
8334 if (it->bidi_p)
8336 SAVE_IT (ppos_it, *it, ppos_data);
8337 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8338 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8339 SAVE_IT (ppos_it, *it, ppos_data);
8342 #define BUFFER_POS_REACHED_P() \
8343 ((op & MOVE_TO_POS) != 0 \
8344 && BUFFERP (it->object) \
8345 && (IT_CHARPOS (*it) == to_charpos \
8346 || ((!it->bidi_p \
8347 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8348 && IT_CHARPOS (*it) > to_charpos) \
8349 || (it->what == IT_COMPOSITION \
8350 && ((IT_CHARPOS (*it) > to_charpos \
8351 && to_charpos >= it->cmp_it.charpos) \
8352 || (IT_CHARPOS (*it) < to_charpos \
8353 && to_charpos <= it->cmp_it.charpos)))) \
8354 && (it->method == GET_FROM_BUFFER \
8355 || (it->method == GET_FROM_DISPLAY_VECTOR \
8356 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8358 /* If there's a line-/wrap-prefix, handle it. */
8359 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8360 && it->current_y < it->last_visible_y)
8361 handle_line_prefix (it);
8363 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8364 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8366 while (1)
8368 int x, i, ascent = 0, descent = 0;
8370 /* Utility macro to reset an iterator with x, ascent, and descent. */
8371 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8372 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8373 (IT)->max_descent = descent)
8375 /* Stop if we move beyond TO_CHARPOS (after an image or a
8376 display string or stretch glyph). */
8377 if ((op & MOVE_TO_POS) != 0
8378 && BUFFERP (it->object)
8379 && it->method == GET_FROM_BUFFER
8380 && (((!it->bidi_p
8381 /* When the iterator is at base embedding level, we
8382 are guaranteed that characters are delivered for
8383 display in strictly increasing order of their
8384 buffer positions. */
8385 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8386 && IT_CHARPOS (*it) > to_charpos)
8387 || (it->bidi_p
8388 && (prev_method == GET_FROM_IMAGE
8389 || prev_method == GET_FROM_STRETCH
8390 || prev_method == GET_FROM_STRING)
8391 /* Passed TO_CHARPOS from left to right. */
8392 && ((prev_pos < to_charpos
8393 && IT_CHARPOS (*it) > to_charpos)
8394 /* Passed TO_CHARPOS from right to left. */
8395 || (prev_pos > to_charpos
8396 && IT_CHARPOS (*it) < to_charpos)))))
8398 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8400 result = MOVE_POS_MATCH_OR_ZV;
8401 break;
8403 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8404 /* If wrap_it is valid, the current position might be in a
8405 word that is wrapped. So, save the iterator in
8406 atpos_it and continue to see if wrapping happens. */
8407 SAVE_IT (atpos_it, *it, atpos_data);
8410 /* Stop when ZV reached.
8411 We used to stop here when TO_CHARPOS reached as well, but that is
8412 too soon if this glyph does not fit on this line. So we handle it
8413 explicitly below. */
8414 if (!get_next_display_element (it))
8416 result = MOVE_POS_MATCH_OR_ZV;
8417 break;
8420 if (it->line_wrap == TRUNCATE)
8422 if (BUFFER_POS_REACHED_P ())
8424 result = MOVE_POS_MATCH_OR_ZV;
8425 break;
8428 else
8430 if (it->line_wrap == WORD_WRAP)
8432 if (IT_DISPLAYING_WHITESPACE (it))
8433 may_wrap = 1;
8434 else if (may_wrap)
8436 /* We have reached a glyph that follows one or more
8437 whitespace characters. If the position is
8438 already found, we are done. */
8439 if (atpos_it.sp >= 0)
8441 RESTORE_IT (it, &atpos_it, atpos_data);
8442 result = MOVE_POS_MATCH_OR_ZV;
8443 goto done;
8445 if (atx_it.sp >= 0)
8447 RESTORE_IT (it, &atx_it, atx_data);
8448 result = MOVE_X_REACHED;
8449 goto done;
8451 /* Otherwise, we can wrap here. */
8452 SAVE_IT (wrap_it, *it, wrap_data);
8453 may_wrap = 0;
8458 /* Remember the line height for the current line, in case
8459 the next element doesn't fit on the line. */
8460 ascent = it->max_ascent;
8461 descent = it->max_descent;
8463 /* The call to produce_glyphs will get the metrics of the
8464 display element IT is loaded with. Record the x-position
8465 before this display element, in case it doesn't fit on the
8466 line. */
8467 x = it->current_x;
8469 PRODUCE_GLYPHS (it);
8471 if (it->area != TEXT_AREA)
8473 prev_method = it->method;
8474 if (it->method == GET_FROM_BUFFER)
8475 prev_pos = IT_CHARPOS (*it);
8476 set_iterator_to_next (it, 1);
8477 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8478 SET_TEXT_POS (this_line_min_pos,
8479 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8480 if (it->bidi_p
8481 && (op & MOVE_TO_POS)
8482 && IT_CHARPOS (*it) > to_charpos
8483 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8484 SAVE_IT (ppos_it, *it, ppos_data);
8485 continue;
8488 /* The number of glyphs we get back in IT->nglyphs will normally
8489 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8490 character on a terminal frame, or (iii) a line end. For the
8491 second case, IT->nglyphs - 1 padding glyphs will be present.
8492 (On X frames, there is only one glyph produced for a
8493 composite character.)
8495 The behavior implemented below means, for continuation lines,
8496 that as many spaces of a TAB as fit on the current line are
8497 displayed there. For terminal frames, as many glyphs of a
8498 multi-glyph character are displayed in the current line, too.
8499 This is what the old redisplay code did, and we keep it that
8500 way. Under X, the whole shape of a complex character must
8501 fit on the line or it will be completely displayed in the
8502 next line.
8504 Note that both for tabs and padding glyphs, all glyphs have
8505 the same width. */
8506 if (it->nglyphs)
8508 /* More than one glyph or glyph doesn't fit on line. All
8509 glyphs have the same width. */
8510 int single_glyph_width = it->pixel_width / it->nglyphs;
8511 int new_x;
8512 int x_before_this_char = x;
8513 int hpos_before_this_char = it->hpos;
8515 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8517 new_x = x + single_glyph_width;
8519 /* We want to leave anything reaching TO_X to the caller. */
8520 if ((op & MOVE_TO_X) && new_x > to_x)
8522 if (BUFFER_POS_REACHED_P ())
8524 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8525 goto buffer_pos_reached;
8526 if (atpos_it.sp < 0)
8528 SAVE_IT (atpos_it, *it, atpos_data);
8529 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8532 else
8534 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8536 it->current_x = x;
8537 result = MOVE_X_REACHED;
8538 break;
8540 if (atx_it.sp < 0)
8542 SAVE_IT (atx_it, *it, atx_data);
8543 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8548 if (/* Lines are continued. */
8549 it->line_wrap != TRUNCATE
8550 && (/* And glyph doesn't fit on the line. */
8551 new_x > it->last_visible_x
8552 /* Or it fits exactly and we're on a window
8553 system frame. */
8554 || (new_x == it->last_visible_x
8555 && FRAME_WINDOW_P (it->f)
8556 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8557 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8558 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8560 if (/* IT->hpos == 0 means the very first glyph
8561 doesn't fit on the line, e.g. a wide image. */
8562 it->hpos == 0
8563 || (new_x == it->last_visible_x
8564 && FRAME_WINDOW_P (it->f)))
8566 ++it->hpos;
8567 it->current_x = new_x;
8569 /* The character's last glyph just barely fits
8570 in this row. */
8571 if (i == it->nglyphs - 1)
8573 /* If this is the destination position,
8574 return a position *before* it in this row,
8575 now that we know it fits in this row. */
8576 if (BUFFER_POS_REACHED_P ())
8578 if (it->line_wrap != WORD_WRAP
8579 || wrap_it.sp < 0)
8581 it->hpos = hpos_before_this_char;
8582 it->current_x = x_before_this_char;
8583 result = MOVE_POS_MATCH_OR_ZV;
8584 break;
8586 if (it->line_wrap == WORD_WRAP
8587 && atpos_it.sp < 0)
8589 SAVE_IT (atpos_it, *it, atpos_data);
8590 atpos_it.current_x = x_before_this_char;
8591 atpos_it.hpos = hpos_before_this_char;
8595 prev_method = it->method;
8596 if (it->method == GET_FROM_BUFFER)
8597 prev_pos = IT_CHARPOS (*it);
8598 set_iterator_to_next (it, 1);
8599 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8600 SET_TEXT_POS (this_line_min_pos,
8601 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8602 /* On graphical terminals, newlines may
8603 "overflow" into the fringe if
8604 overflow-newline-into-fringe is non-nil.
8605 On text terminals, and on graphical
8606 terminals with no right margin, newlines
8607 may overflow into the last glyph on the
8608 display line.*/
8609 if (!FRAME_WINDOW_P (it->f)
8610 || ((it->bidi_p
8611 && it->bidi_it.paragraph_dir == R2L)
8612 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8613 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8614 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8616 if (!get_next_display_element (it))
8618 result = MOVE_POS_MATCH_OR_ZV;
8619 break;
8621 if (BUFFER_POS_REACHED_P ())
8623 if (ITERATOR_AT_END_OF_LINE_P (it))
8624 result = MOVE_POS_MATCH_OR_ZV;
8625 else
8626 result = MOVE_LINE_CONTINUED;
8627 break;
8629 if (ITERATOR_AT_END_OF_LINE_P (it)
8630 && (it->line_wrap != WORD_WRAP
8631 || wrap_it.sp < 0))
8633 result = MOVE_NEWLINE_OR_CR;
8634 break;
8639 else
8640 IT_RESET_X_ASCENT_DESCENT (it);
8642 if (wrap_it.sp >= 0)
8644 RESTORE_IT (it, &wrap_it, wrap_data);
8645 atpos_it.sp = -1;
8646 atx_it.sp = -1;
8649 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8650 IT_CHARPOS (*it)));
8651 result = MOVE_LINE_CONTINUED;
8652 break;
8655 if (BUFFER_POS_REACHED_P ())
8657 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8658 goto buffer_pos_reached;
8659 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8661 SAVE_IT (atpos_it, *it, atpos_data);
8662 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8666 if (new_x > it->first_visible_x)
8668 /* Glyph is visible. Increment number of glyphs that
8669 would be displayed. */
8670 ++it->hpos;
8674 if (result != MOVE_UNDEFINED)
8675 break;
8677 else if (BUFFER_POS_REACHED_P ())
8679 buffer_pos_reached:
8680 IT_RESET_X_ASCENT_DESCENT (it);
8681 result = MOVE_POS_MATCH_OR_ZV;
8682 break;
8684 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8686 /* Stop when TO_X specified and reached. This check is
8687 necessary here because of lines consisting of a line end,
8688 only. The line end will not produce any glyphs and we
8689 would never get MOVE_X_REACHED. */
8690 eassert (it->nglyphs == 0);
8691 result = MOVE_X_REACHED;
8692 break;
8695 /* Is this a line end? If yes, we're done. */
8696 if (ITERATOR_AT_END_OF_LINE_P (it))
8698 /* If we are past TO_CHARPOS, but never saw any character
8699 positions smaller than TO_CHARPOS, return
8700 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8701 did. */
8702 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8704 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8706 if (IT_CHARPOS (ppos_it) < ZV)
8708 RESTORE_IT (it, &ppos_it, ppos_data);
8709 result = MOVE_POS_MATCH_OR_ZV;
8711 else
8712 goto buffer_pos_reached;
8714 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8715 && IT_CHARPOS (*it) > to_charpos)
8716 goto buffer_pos_reached;
8717 else
8718 result = MOVE_NEWLINE_OR_CR;
8720 else
8721 result = MOVE_NEWLINE_OR_CR;
8722 break;
8725 prev_method = it->method;
8726 if (it->method == GET_FROM_BUFFER)
8727 prev_pos = IT_CHARPOS (*it);
8728 /* The current display element has been consumed. Advance
8729 to the next. */
8730 set_iterator_to_next (it, 1);
8731 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8732 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8733 if (IT_CHARPOS (*it) < to_charpos)
8734 saw_smaller_pos = 1;
8735 if (it->bidi_p
8736 && (op & MOVE_TO_POS)
8737 && IT_CHARPOS (*it) >= to_charpos
8738 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8739 SAVE_IT (ppos_it, *it, ppos_data);
8741 /* Stop if lines are truncated and IT's current x-position is
8742 past the right edge of the window now. */
8743 if (it->line_wrap == TRUNCATE
8744 && it->current_x >= it->last_visible_x)
8746 if (!FRAME_WINDOW_P (it->f)
8747 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8748 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8749 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8750 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8752 int at_eob_p = 0;
8754 if ((at_eob_p = !get_next_display_element (it))
8755 || BUFFER_POS_REACHED_P ()
8756 /* If we are past TO_CHARPOS, but never saw any
8757 character positions smaller than TO_CHARPOS,
8758 return MOVE_POS_MATCH_OR_ZV, like the
8759 unidirectional display did. */
8760 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8761 && !saw_smaller_pos
8762 && IT_CHARPOS (*it) > to_charpos))
8764 if (it->bidi_p
8765 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8766 RESTORE_IT (it, &ppos_it, ppos_data);
8767 result = MOVE_POS_MATCH_OR_ZV;
8768 break;
8770 if (ITERATOR_AT_END_OF_LINE_P (it))
8772 result = MOVE_NEWLINE_OR_CR;
8773 break;
8776 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8777 && !saw_smaller_pos
8778 && IT_CHARPOS (*it) > to_charpos)
8780 if (IT_CHARPOS (ppos_it) < ZV)
8781 RESTORE_IT (it, &ppos_it, ppos_data);
8782 result = MOVE_POS_MATCH_OR_ZV;
8783 break;
8785 result = MOVE_LINE_TRUNCATED;
8786 break;
8788 #undef IT_RESET_X_ASCENT_DESCENT
8791 #undef BUFFER_POS_REACHED_P
8793 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8794 restore the saved iterator. */
8795 if (atpos_it.sp >= 0)
8796 RESTORE_IT (it, &atpos_it, atpos_data);
8797 else if (atx_it.sp >= 0)
8798 RESTORE_IT (it, &atx_it, atx_data);
8800 done:
8802 if (atpos_data)
8803 bidi_unshelve_cache (atpos_data, 1);
8804 if (atx_data)
8805 bidi_unshelve_cache (atx_data, 1);
8806 if (wrap_data)
8807 bidi_unshelve_cache (wrap_data, 1);
8808 if (ppos_data)
8809 bidi_unshelve_cache (ppos_data, 1);
8811 /* Restore the iterator settings altered at the beginning of this
8812 function. */
8813 it->glyph_row = saved_glyph_row;
8814 return result;
8817 /* For external use. */
8818 void
8819 move_it_in_display_line (struct it *it,
8820 ptrdiff_t to_charpos, int to_x,
8821 enum move_operation_enum op)
8823 if (it->line_wrap == WORD_WRAP
8824 && (op & MOVE_TO_X))
8826 struct it save_it;
8827 void *save_data = NULL;
8828 int skip;
8830 SAVE_IT (save_it, *it, save_data);
8831 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8832 /* When word-wrap is on, TO_X may lie past the end
8833 of a wrapped line. Then it->current is the
8834 character on the next line, so backtrack to the
8835 space before the wrap point. */
8836 if (skip == MOVE_LINE_CONTINUED)
8838 int prev_x = max (it->current_x - 1, 0);
8839 RESTORE_IT (it, &save_it, save_data);
8840 move_it_in_display_line_to
8841 (it, -1, prev_x, MOVE_TO_X);
8843 else
8844 bidi_unshelve_cache (save_data, 1);
8846 else
8847 move_it_in_display_line_to (it, to_charpos, to_x, op);
8851 /* Move IT forward until it satisfies one or more of the criteria in
8852 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8854 OP is a bit-mask that specifies where to stop, and in particular,
8855 which of those four position arguments makes a difference. See the
8856 description of enum move_operation_enum.
8858 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8859 screen line, this function will set IT to the next position that is
8860 displayed to the right of TO_CHARPOS on the screen. */
8862 void
8863 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8865 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8866 int line_height, line_start_x = 0, reached = 0;
8867 void *backup_data = NULL;
8869 for (;;)
8871 if (op & MOVE_TO_VPOS)
8873 /* If no TO_CHARPOS and no TO_X specified, stop at the
8874 start of the line TO_VPOS. */
8875 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8877 if (it->vpos == to_vpos)
8879 reached = 1;
8880 break;
8882 else
8883 skip = move_it_in_display_line_to (it, -1, -1, 0);
8885 else
8887 /* TO_VPOS >= 0 means stop at TO_X in the line at
8888 TO_VPOS, or at TO_POS, whichever comes first. */
8889 if (it->vpos == to_vpos)
8891 reached = 2;
8892 break;
8895 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8897 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8899 reached = 3;
8900 break;
8902 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8904 /* We have reached TO_X but not in the line we want. */
8905 skip = move_it_in_display_line_to (it, to_charpos,
8906 -1, MOVE_TO_POS);
8907 if (skip == MOVE_POS_MATCH_OR_ZV)
8909 reached = 4;
8910 break;
8915 else if (op & MOVE_TO_Y)
8917 struct it it_backup;
8919 if (it->line_wrap == WORD_WRAP)
8920 SAVE_IT (it_backup, *it, backup_data);
8922 /* TO_Y specified means stop at TO_X in the line containing
8923 TO_Y---or at TO_CHARPOS if this is reached first. The
8924 problem is that we can't really tell whether the line
8925 contains TO_Y before we have completely scanned it, and
8926 this may skip past TO_X. What we do is to first scan to
8927 TO_X.
8929 If TO_X is not specified, use a TO_X of zero. The reason
8930 is to make the outcome of this function more predictable.
8931 If we didn't use TO_X == 0, we would stop at the end of
8932 the line which is probably not what a caller would expect
8933 to happen. */
8934 skip = move_it_in_display_line_to
8935 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8936 (MOVE_TO_X | (op & MOVE_TO_POS)));
8938 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8939 if (skip == MOVE_POS_MATCH_OR_ZV)
8940 reached = 5;
8941 else if (skip == MOVE_X_REACHED)
8943 /* If TO_X was reached, we want to know whether TO_Y is
8944 in the line. We know this is the case if the already
8945 scanned glyphs make the line tall enough. Otherwise,
8946 we must check by scanning the rest of the line. */
8947 line_height = it->max_ascent + it->max_descent;
8948 if (to_y >= it->current_y
8949 && to_y < it->current_y + line_height)
8951 reached = 6;
8952 break;
8954 SAVE_IT (it_backup, *it, backup_data);
8955 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8956 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8957 op & MOVE_TO_POS);
8958 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8959 line_height = it->max_ascent + it->max_descent;
8960 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8962 if (to_y >= it->current_y
8963 && to_y < it->current_y + line_height)
8965 /* If TO_Y is in this line and TO_X was reached
8966 above, we scanned too far. We have to restore
8967 IT's settings to the ones before skipping. But
8968 keep the more accurate values of max_ascent and
8969 max_descent we've found while skipping the rest
8970 of the line, for the sake of callers, such as
8971 pos_visible_p, that need to know the line
8972 height. */
8973 int max_ascent = it->max_ascent;
8974 int max_descent = it->max_descent;
8976 RESTORE_IT (it, &it_backup, backup_data);
8977 it->max_ascent = max_ascent;
8978 it->max_descent = max_descent;
8979 reached = 6;
8981 else
8983 skip = skip2;
8984 if (skip == MOVE_POS_MATCH_OR_ZV)
8985 reached = 7;
8988 else
8990 /* Check whether TO_Y is in this line. */
8991 line_height = it->max_ascent + it->max_descent;
8992 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8994 if (to_y >= it->current_y
8995 && to_y < it->current_y + line_height)
8997 /* When word-wrap is on, TO_X may lie past the end
8998 of a wrapped line. Then it->current is the
8999 character on the next line, so backtrack to the
9000 space before the wrap point. */
9001 if (skip == MOVE_LINE_CONTINUED
9002 && it->line_wrap == WORD_WRAP)
9004 int prev_x = max (it->current_x - 1, 0);
9005 RESTORE_IT (it, &it_backup, backup_data);
9006 skip = move_it_in_display_line_to
9007 (it, -1, prev_x, MOVE_TO_X);
9009 reached = 6;
9013 if (reached)
9014 break;
9016 else if (BUFFERP (it->object)
9017 && (it->method == GET_FROM_BUFFER
9018 || it->method == GET_FROM_STRETCH)
9019 && IT_CHARPOS (*it) >= to_charpos
9020 /* Under bidi iteration, a call to set_iterator_to_next
9021 can scan far beyond to_charpos if the initial
9022 portion of the next line needs to be reordered. In
9023 that case, give move_it_in_display_line_to another
9024 chance below. */
9025 && !(it->bidi_p
9026 && it->bidi_it.scan_dir == -1))
9027 skip = MOVE_POS_MATCH_OR_ZV;
9028 else
9029 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9031 switch (skip)
9033 case MOVE_POS_MATCH_OR_ZV:
9034 reached = 8;
9035 goto out;
9037 case MOVE_NEWLINE_OR_CR:
9038 set_iterator_to_next (it, 1);
9039 it->continuation_lines_width = 0;
9040 break;
9042 case MOVE_LINE_TRUNCATED:
9043 it->continuation_lines_width = 0;
9044 reseat_at_next_visible_line_start (it, 0);
9045 if ((op & MOVE_TO_POS) != 0
9046 && IT_CHARPOS (*it) > to_charpos)
9048 reached = 9;
9049 goto out;
9051 break;
9053 case MOVE_LINE_CONTINUED:
9054 /* For continued lines ending in a tab, some of the glyphs
9055 associated with the tab are displayed on the current
9056 line. Since it->current_x does not include these glyphs,
9057 we use it->last_visible_x instead. */
9058 if (it->c == '\t')
9060 it->continuation_lines_width += it->last_visible_x;
9061 /* When moving by vpos, ensure that the iterator really
9062 advances to the next line (bug#847, bug#969). Fixme:
9063 do we need to do this in other circumstances? */
9064 if (it->current_x != it->last_visible_x
9065 && (op & MOVE_TO_VPOS)
9066 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9068 line_start_x = it->current_x + it->pixel_width
9069 - it->last_visible_x;
9070 set_iterator_to_next (it, 0);
9073 else
9074 it->continuation_lines_width += it->current_x;
9075 break;
9077 default:
9078 emacs_abort ();
9081 /* Reset/increment for the next run. */
9082 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9083 it->current_x = line_start_x;
9084 line_start_x = 0;
9085 it->hpos = 0;
9086 it->current_y += it->max_ascent + it->max_descent;
9087 ++it->vpos;
9088 last_height = it->max_ascent + it->max_descent;
9089 it->max_ascent = it->max_descent = 0;
9092 out:
9094 /* On text terminals, we may stop at the end of a line in the middle
9095 of a multi-character glyph. If the glyph itself is continued,
9096 i.e. it is actually displayed on the next line, don't treat this
9097 stopping point as valid; move to the next line instead (unless
9098 that brings us offscreen). */
9099 if (!FRAME_WINDOW_P (it->f)
9100 && op & MOVE_TO_POS
9101 && IT_CHARPOS (*it) == to_charpos
9102 && it->what == IT_CHARACTER
9103 && it->nglyphs > 1
9104 && it->line_wrap == WINDOW_WRAP
9105 && it->current_x == it->last_visible_x - 1
9106 && it->c != '\n'
9107 && it->c != '\t'
9108 && it->vpos < it->w->window_end_vpos)
9110 it->continuation_lines_width += it->current_x;
9111 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9112 it->current_y += it->max_ascent + it->max_descent;
9113 ++it->vpos;
9114 last_height = it->max_ascent + it->max_descent;
9117 if (backup_data)
9118 bidi_unshelve_cache (backup_data, 1);
9120 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9124 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9126 If DY > 0, move IT backward at least that many pixels. DY = 0
9127 means move IT backward to the preceding line start or BEGV. This
9128 function may move over more than DY pixels if IT->current_y - DY
9129 ends up in the middle of a line; in this case IT->current_y will be
9130 set to the top of the line moved to. */
9132 void
9133 move_it_vertically_backward (struct it *it, int dy)
9135 int nlines, h;
9136 struct it it2, it3;
9137 void *it2data = NULL, *it3data = NULL;
9138 ptrdiff_t start_pos;
9139 int nchars_per_row
9140 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9141 ptrdiff_t pos_limit;
9143 move_further_back:
9144 eassert (dy >= 0);
9146 start_pos = IT_CHARPOS (*it);
9148 /* Estimate how many newlines we must move back. */
9149 nlines = max (1, dy / default_line_pixel_height (it->w));
9150 if (it->line_wrap == TRUNCATE)
9151 pos_limit = BEGV;
9152 else
9153 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9155 /* Set the iterator's position that many lines back. But don't go
9156 back more than NLINES full screen lines -- this wins a day with
9157 buffers which have very long lines. */
9158 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9159 back_to_previous_visible_line_start (it);
9161 /* Reseat the iterator here. When moving backward, we don't want
9162 reseat to skip forward over invisible text, set up the iterator
9163 to deliver from overlay strings at the new position etc. So,
9164 use reseat_1 here. */
9165 reseat_1 (it, it->current.pos, 1);
9167 /* We are now surely at a line start. */
9168 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9169 reordering is in effect. */
9170 it->continuation_lines_width = 0;
9172 /* Move forward and see what y-distance we moved. First move to the
9173 start of the next line so that we get its height. We need this
9174 height to be able to tell whether we reached the specified
9175 y-distance. */
9176 SAVE_IT (it2, *it, it2data);
9177 it2.max_ascent = it2.max_descent = 0;
9180 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9181 MOVE_TO_POS | MOVE_TO_VPOS);
9183 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9184 /* If we are in a display string which starts at START_POS,
9185 and that display string includes a newline, and we are
9186 right after that newline (i.e. at the beginning of a
9187 display line), exit the loop, because otherwise we will
9188 infloop, since move_it_to will see that it is already at
9189 START_POS and will not move. */
9190 || (it2.method == GET_FROM_STRING
9191 && IT_CHARPOS (it2) == start_pos
9192 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9193 eassert (IT_CHARPOS (*it) >= BEGV);
9194 SAVE_IT (it3, it2, it3data);
9196 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9197 eassert (IT_CHARPOS (*it) >= BEGV);
9198 /* H is the actual vertical distance from the position in *IT
9199 and the starting position. */
9200 h = it2.current_y - it->current_y;
9201 /* NLINES is the distance in number of lines. */
9202 nlines = it2.vpos - it->vpos;
9204 /* Correct IT's y and vpos position
9205 so that they are relative to the starting point. */
9206 it->vpos -= nlines;
9207 it->current_y -= h;
9209 if (dy == 0)
9211 /* DY == 0 means move to the start of the screen line. The
9212 value of nlines is > 0 if continuation lines were involved,
9213 or if the original IT position was at start of a line. */
9214 RESTORE_IT (it, it, it2data);
9215 if (nlines > 0)
9216 move_it_by_lines (it, nlines);
9217 /* The above code moves us to some position NLINES down,
9218 usually to its first glyph (leftmost in an L2R line), but
9219 that's not necessarily the start of the line, under bidi
9220 reordering. We want to get to the character position
9221 that is immediately after the newline of the previous
9222 line. */
9223 if (it->bidi_p
9224 && !it->continuation_lines_width
9225 && !STRINGP (it->string)
9226 && IT_CHARPOS (*it) > BEGV
9227 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9229 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9231 DEC_BOTH (cp, bp);
9232 cp = find_newline_no_quit (cp, bp, -1, NULL);
9233 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9235 bidi_unshelve_cache (it3data, 1);
9237 else
9239 /* The y-position we try to reach, relative to *IT.
9240 Note that H has been subtracted in front of the if-statement. */
9241 int target_y = it->current_y + h - dy;
9242 int y0 = it3.current_y;
9243 int y1;
9244 int line_height;
9246 RESTORE_IT (&it3, &it3, it3data);
9247 y1 = line_bottom_y (&it3);
9248 line_height = y1 - y0;
9249 RESTORE_IT (it, it, it2data);
9250 /* If we did not reach target_y, try to move further backward if
9251 we can. If we moved too far backward, try to move forward. */
9252 if (target_y < it->current_y
9253 /* This is heuristic. In a window that's 3 lines high, with
9254 a line height of 13 pixels each, recentering with point
9255 on the bottom line will try to move -39/2 = 19 pixels
9256 backward. Try to avoid moving into the first line. */
9257 && (it->current_y - target_y
9258 > min (window_box_height (it->w), line_height * 2 / 3))
9259 && IT_CHARPOS (*it) > BEGV)
9261 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9262 target_y - it->current_y));
9263 dy = it->current_y - target_y;
9264 goto move_further_back;
9266 else if (target_y >= it->current_y + line_height
9267 && IT_CHARPOS (*it) < ZV)
9269 /* Should move forward by at least one line, maybe more.
9271 Note: Calling move_it_by_lines can be expensive on
9272 terminal frames, where compute_motion is used (via
9273 vmotion) to do the job, when there are very long lines
9274 and truncate-lines is nil. That's the reason for
9275 treating terminal frames specially here. */
9277 if (!FRAME_WINDOW_P (it->f))
9278 move_it_vertically (it, target_y - (it->current_y + line_height));
9279 else
9283 move_it_by_lines (it, 1);
9285 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9292 /* Move IT by a specified amount of pixel lines DY. DY negative means
9293 move backwards. DY = 0 means move to start of screen line. At the
9294 end, IT will be on the start of a screen line. */
9296 void
9297 move_it_vertically (struct it *it, int dy)
9299 if (dy <= 0)
9300 move_it_vertically_backward (it, -dy);
9301 else
9303 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9304 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9305 MOVE_TO_POS | MOVE_TO_Y);
9306 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9308 /* If buffer ends in ZV without a newline, move to the start of
9309 the line to satisfy the post-condition. */
9310 if (IT_CHARPOS (*it) == ZV
9311 && ZV > BEGV
9312 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9313 move_it_by_lines (it, 0);
9318 /* Move iterator IT past the end of the text line it is in. */
9320 void
9321 move_it_past_eol (struct it *it)
9323 enum move_it_result rc;
9325 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9326 if (rc == MOVE_NEWLINE_OR_CR)
9327 set_iterator_to_next (it, 0);
9331 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9332 negative means move up. DVPOS == 0 means move to the start of the
9333 screen line.
9335 Optimization idea: If we would know that IT->f doesn't use
9336 a face with proportional font, we could be faster for
9337 truncate-lines nil. */
9339 void
9340 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9343 /* The commented-out optimization uses vmotion on terminals. This
9344 gives bad results, because elements like it->what, on which
9345 callers such as pos_visible_p rely, aren't updated. */
9346 /* struct position pos;
9347 if (!FRAME_WINDOW_P (it->f))
9349 struct text_pos textpos;
9351 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9352 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9353 reseat (it, textpos, 1);
9354 it->vpos += pos.vpos;
9355 it->current_y += pos.vpos;
9357 else */
9359 if (dvpos == 0)
9361 /* DVPOS == 0 means move to the start of the screen line. */
9362 move_it_vertically_backward (it, 0);
9363 /* Let next call to line_bottom_y calculate real line height */
9364 last_height = 0;
9366 else if (dvpos > 0)
9368 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9369 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9371 /* Only move to the next buffer position if we ended up in a
9372 string from display property, not in an overlay string
9373 (before-string or after-string). That is because the
9374 latter don't conceal the underlying buffer position, so
9375 we can ask to move the iterator to the exact position we
9376 are interested in. Note that, even if we are already at
9377 IT_CHARPOS (*it), the call below is not a no-op, as it
9378 will detect that we are at the end of the string, pop the
9379 iterator, and compute it->current_x and it->hpos
9380 correctly. */
9381 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9382 -1, -1, -1, MOVE_TO_POS);
9385 else
9387 struct it it2;
9388 void *it2data = NULL;
9389 ptrdiff_t start_charpos, i;
9390 int nchars_per_row
9391 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9392 ptrdiff_t pos_limit;
9394 /* Start at the beginning of the screen line containing IT's
9395 position. This may actually move vertically backwards,
9396 in case of overlays, so adjust dvpos accordingly. */
9397 dvpos += it->vpos;
9398 move_it_vertically_backward (it, 0);
9399 dvpos -= it->vpos;
9401 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9402 screen lines, and reseat the iterator there. */
9403 start_charpos = IT_CHARPOS (*it);
9404 if (it->line_wrap == TRUNCATE)
9405 pos_limit = BEGV;
9406 else
9407 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9408 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9409 back_to_previous_visible_line_start (it);
9410 reseat (it, it->current.pos, 1);
9412 /* Move further back if we end up in a string or an image. */
9413 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9415 /* First try to move to start of display line. */
9416 dvpos += it->vpos;
9417 move_it_vertically_backward (it, 0);
9418 dvpos -= it->vpos;
9419 if (IT_POS_VALID_AFTER_MOVE_P (it))
9420 break;
9421 /* If start of line is still in string or image,
9422 move further back. */
9423 back_to_previous_visible_line_start (it);
9424 reseat (it, it->current.pos, 1);
9425 dvpos--;
9428 it->current_x = it->hpos = 0;
9430 /* Above call may have moved too far if continuation lines
9431 are involved. Scan forward and see if it did. */
9432 SAVE_IT (it2, *it, it2data);
9433 it2.vpos = it2.current_y = 0;
9434 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9435 it->vpos -= it2.vpos;
9436 it->current_y -= it2.current_y;
9437 it->current_x = it->hpos = 0;
9439 /* If we moved too far back, move IT some lines forward. */
9440 if (it2.vpos > -dvpos)
9442 int delta = it2.vpos + dvpos;
9444 RESTORE_IT (&it2, &it2, it2data);
9445 SAVE_IT (it2, *it, it2data);
9446 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9447 /* Move back again if we got too far ahead. */
9448 if (IT_CHARPOS (*it) >= start_charpos)
9449 RESTORE_IT (it, &it2, it2data);
9450 else
9451 bidi_unshelve_cache (it2data, 1);
9453 else
9454 RESTORE_IT (it, it, it2data);
9458 /* Return 1 if IT points into the middle of a display vector. */
9461 in_display_vector_p (struct it *it)
9463 return (it->method == GET_FROM_DISPLAY_VECTOR
9464 && it->current.dpvec_index > 0
9465 && it->dpvec + it->current.dpvec_index != it->dpend);
9469 /***********************************************************************
9470 Messages
9471 ***********************************************************************/
9474 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9475 to *Messages*. */
9477 void
9478 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9480 Lisp_Object args[3];
9481 Lisp_Object msg, fmt;
9482 char *buffer;
9483 ptrdiff_t len;
9484 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9485 USE_SAFE_ALLOCA;
9487 fmt = msg = Qnil;
9488 GCPRO4 (fmt, msg, arg1, arg2);
9490 args[0] = fmt = build_string (format);
9491 args[1] = arg1;
9492 args[2] = arg2;
9493 msg = Fformat (3, args);
9495 len = SBYTES (msg) + 1;
9496 buffer = SAFE_ALLOCA (len);
9497 memcpy (buffer, SDATA (msg), len);
9499 message_dolog (buffer, len - 1, 1, 0);
9500 SAFE_FREE ();
9502 UNGCPRO;
9506 /* Output a newline in the *Messages* buffer if "needs" one. */
9508 void
9509 message_log_maybe_newline (void)
9511 if (message_log_need_newline)
9512 message_dolog ("", 0, 1, 0);
9516 /* Add a string M of length NBYTES to the message log, optionally
9517 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9518 true, means interpret the contents of M as multibyte. This
9519 function calls low-level routines in order to bypass text property
9520 hooks, etc. which might not be safe to run.
9522 This may GC (insert may run before/after change hooks),
9523 so the buffer M must NOT point to a Lisp string. */
9525 void
9526 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9528 const unsigned char *msg = (const unsigned char *) m;
9530 if (!NILP (Vmemory_full))
9531 return;
9533 if (!NILP (Vmessage_log_max))
9535 struct buffer *oldbuf;
9536 Lisp_Object oldpoint, oldbegv, oldzv;
9537 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9538 ptrdiff_t point_at_end = 0;
9539 ptrdiff_t zv_at_end = 0;
9540 Lisp_Object old_deactivate_mark;
9541 bool shown;
9542 struct gcpro gcpro1;
9544 old_deactivate_mark = Vdeactivate_mark;
9545 oldbuf = current_buffer;
9546 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9547 bset_undo_list (current_buffer, Qt);
9549 oldpoint = message_dolog_marker1;
9550 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9551 oldbegv = message_dolog_marker2;
9552 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9553 oldzv = message_dolog_marker3;
9554 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9555 GCPRO1 (old_deactivate_mark);
9557 if (PT == Z)
9558 point_at_end = 1;
9559 if (ZV == Z)
9560 zv_at_end = 1;
9562 BEGV = BEG;
9563 BEGV_BYTE = BEG_BYTE;
9564 ZV = Z;
9565 ZV_BYTE = Z_BYTE;
9566 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9568 /* Insert the string--maybe converting multibyte to single byte
9569 or vice versa, so that all the text fits the buffer. */
9570 if (multibyte
9571 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9573 ptrdiff_t i;
9574 int c, char_bytes;
9575 char work[1];
9577 /* Convert a multibyte string to single-byte
9578 for the *Message* buffer. */
9579 for (i = 0; i < nbytes; i += char_bytes)
9581 c = string_char_and_length (msg + i, &char_bytes);
9582 work[0] = (ASCII_CHAR_P (c)
9584 : multibyte_char_to_unibyte (c));
9585 insert_1_both (work, 1, 1, 1, 0, 0);
9588 else if (! multibyte
9589 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9591 ptrdiff_t i;
9592 int c, char_bytes;
9593 unsigned char str[MAX_MULTIBYTE_LENGTH];
9594 /* Convert a single-byte string to multibyte
9595 for the *Message* buffer. */
9596 for (i = 0; i < nbytes; i++)
9598 c = msg[i];
9599 MAKE_CHAR_MULTIBYTE (c);
9600 char_bytes = CHAR_STRING (c, str);
9601 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9604 else if (nbytes)
9605 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9607 if (nlflag)
9609 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9610 printmax_t dups;
9612 insert_1_both ("\n", 1, 1, 1, 0, 0);
9614 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9615 this_bol = PT;
9616 this_bol_byte = PT_BYTE;
9618 /* See if this line duplicates the previous one.
9619 If so, combine duplicates. */
9620 if (this_bol > BEG)
9622 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9623 prev_bol = PT;
9624 prev_bol_byte = PT_BYTE;
9626 dups = message_log_check_duplicate (prev_bol_byte,
9627 this_bol_byte);
9628 if (dups)
9630 del_range_both (prev_bol, prev_bol_byte,
9631 this_bol, this_bol_byte, 0);
9632 if (dups > 1)
9634 char dupstr[sizeof " [ times]"
9635 + INT_STRLEN_BOUND (printmax_t)];
9637 /* If you change this format, don't forget to also
9638 change message_log_check_duplicate. */
9639 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9640 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9641 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9646 /* If we have more than the desired maximum number of lines
9647 in the *Messages* buffer now, delete the oldest ones.
9648 This is safe because we don't have undo in this buffer. */
9650 if (NATNUMP (Vmessage_log_max))
9652 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9653 -XFASTINT (Vmessage_log_max) - 1, 0);
9654 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9657 BEGV = marker_position (oldbegv);
9658 BEGV_BYTE = marker_byte_position (oldbegv);
9660 if (zv_at_end)
9662 ZV = Z;
9663 ZV_BYTE = Z_BYTE;
9665 else
9667 ZV = marker_position (oldzv);
9668 ZV_BYTE = marker_byte_position (oldzv);
9671 if (point_at_end)
9672 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9673 else
9674 /* We can't do Fgoto_char (oldpoint) because it will run some
9675 Lisp code. */
9676 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9677 marker_byte_position (oldpoint));
9679 UNGCPRO;
9680 unchain_marker (XMARKER (oldpoint));
9681 unchain_marker (XMARKER (oldbegv));
9682 unchain_marker (XMARKER (oldzv));
9684 shown = buffer_window_count (current_buffer) > 0;
9685 set_buffer_internal (oldbuf);
9686 /* We called insert_1_both above with its 5th argument (PREPARE)
9687 zero, which prevents insert_1_both from calling
9688 prepare_to_modify_buffer, which in turns prevents us from
9689 incrementing windows_or_buffers_changed even if *Messages* is
9690 shown in some window. So we must manually incrementing
9691 windows_or_buffers_changed here to make up for that. */
9692 if (shown)
9693 windows_or_buffers_changed++;
9694 else
9695 windows_or_buffers_changed = old_windows_or_buffers_changed;
9696 message_log_need_newline = !nlflag;
9697 Vdeactivate_mark = old_deactivate_mark;
9702 /* We are at the end of the buffer after just having inserted a newline.
9703 (Note: We depend on the fact we won't be crossing the gap.)
9704 Check to see if the most recent message looks a lot like the previous one.
9705 Return 0 if different, 1 if the new one should just replace it, or a
9706 value N > 1 if we should also append " [N times]". */
9708 static intmax_t
9709 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9711 ptrdiff_t i;
9712 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9713 int seen_dots = 0;
9714 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9715 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9717 for (i = 0; i < len; i++)
9719 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9720 seen_dots = 1;
9721 if (p1[i] != p2[i])
9722 return seen_dots;
9724 p1 += len;
9725 if (*p1 == '\n')
9726 return 2;
9727 if (*p1++ == ' ' && *p1++ == '[')
9729 char *pend;
9730 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9731 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9732 return n + 1;
9734 return 0;
9738 /* Display an echo area message M with a specified length of NBYTES
9739 bytes. The string may include null characters. If M is not a
9740 string, clear out any existing message, and let the mini-buffer
9741 text show through.
9743 This function cancels echoing. */
9745 void
9746 message3 (Lisp_Object m)
9748 struct gcpro gcpro1;
9750 GCPRO1 (m);
9751 clear_message (1,1);
9752 cancel_echoing ();
9754 /* First flush out any partial line written with print. */
9755 message_log_maybe_newline ();
9756 if (STRINGP (m))
9758 ptrdiff_t nbytes = SBYTES (m);
9759 bool multibyte = STRING_MULTIBYTE (m);
9760 USE_SAFE_ALLOCA;
9761 char *buffer = SAFE_ALLOCA (nbytes);
9762 memcpy (buffer, SDATA (m), nbytes);
9763 message_dolog (buffer, nbytes, 1, multibyte);
9764 SAFE_FREE ();
9766 message3_nolog (m);
9768 UNGCPRO;
9772 /* The non-logging version of message3.
9773 This does not cancel echoing, because it is used for echoing.
9774 Perhaps we need to make a separate function for echoing
9775 and make this cancel echoing. */
9777 void
9778 message3_nolog (Lisp_Object m)
9780 struct frame *sf = SELECTED_FRAME ();
9782 if (FRAME_INITIAL_P (sf))
9784 if (noninteractive_need_newline)
9785 putc ('\n', stderr);
9786 noninteractive_need_newline = 0;
9787 if (STRINGP (m))
9788 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9789 if (cursor_in_echo_area == 0)
9790 fprintf (stderr, "\n");
9791 fflush (stderr);
9793 /* Error messages get reported properly by cmd_error, so this must be just an
9794 informative message; if the frame hasn't really been initialized yet, just
9795 toss it. */
9796 else if (INTERACTIVE && sf->glyphs_initialized_p)
9798 /* Get the frame containing the mini-buffer
9799 that the selected frame is using. */
9800 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9801 Lisp_Object frame = XWINDOW (mini_window)->frame;
9802 struct frame *f = XFRAME (frame);
9804 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9805 Fmake_frame_visible (frame);
9807 if (STRINGP (m) && SCHARS (m) > 0)
9809 set_message (m);
9810 if (minibuffer_auto_raise)
9811 Fraise_frame (frame);
9812 /* Assume we are not echoing.
9813 (If we are, echo_now will override this.) */
9814 echo_message_buffer = Qnil;
9816 else
9817 clear_message (1, 1);
9819 do_pending_window_change (0);
9820 echo_area_display (1);
9821 do_pending_window_change (0);
9822 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9823 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9828 /* Display a null-terminated echo area message M. If M is 0, clear
9829 out any existing message, and let the mini-buffer text show through.
9831 The buffer M must continue to exist until after the echo area gets
9832 cleared or some other message gets displayed there. Do not pass
9833 text that is stored in a Lisp string. Do not pass text in a buffer
9834 that was alloca'd. */
9836 void
9837 message1 (const char *m)
9839 message3 (m ? build_unibyte_string (m) : Qnil);
9843 /* The non-logging counterpart of message1. */
9845 void
9846 message1_nolog (const char *m)
9848 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9851 /* Display a message M which contains a single %s
9852 which gets replaced with STRING. */
9854 void
9855 message_with_string (const char *m, Lisp_Object string, int log)
9857 CHECK_STRING (string);
9859 if (noninteractive)
9861 if (m)
9863 if (noninteractive_need_newline)
9864 putc ('\n', stderr);
9865 noninteractive_need_newline = 0;
9866 fprintf (stderr, m, SDATA (string));
9867 if (!cursor_in_echo_area)
9868 fprintf (stderr, "\n");
9869 fflush (stderr);
9872 else if (INTERACTIVE)
9874 /* The frame whose minibuffer we're going to display the message on.
9875 It may be larger than the selected frame, so we need
9876 to use its buffer, not the selected frame's buffer. */
9877 Lisp_Object mini_window;
9878 struct frame *f, *sf = SELECTED_FRAME ();
9880 /* Get the frame containing the minibuffer
9881 that the selected frame is using. */
9882 mini_window = FRAME_MINIBUF_WINDOW (sf);
9883 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9885 /* Error messages get reported properly by cmd_error, so this must be
9886 just an informative message; if the frame hasn't really been
9887 initialized yet, just toss it. */
9888 if (f->glyphs_initialized_p)
9890 Lisp_Object args[2], msg;
9891 struct gcpro gcpro1, gcpro2;
9893 args[0] = build_string (m);
9894 args[1] = msg = string;
9895 GCPRO2 (args[0], msg);
9896 gcpro1.nvars = 2;
9898 msg = Fformat (2, args);
9900 if (log)
9901 message3 (msg);
9902 else
9903 message3_nolog (msg);
9905 UNGCPRO;
9907 /* Print should start at the beginning of the message
9908 buffer next time. */
9909 message_buf_print = 0;
9915 /* Dump an informative message to the minibuf. If M is 0, clear out
9916 any existing message, and let the mini-buffer text show through. */
9918 static void
9919 vmessage (const char *m, va_list ap)
9921 if (noninteractive)
9923 if (m)
9925 if (noninteractive_need_newline)
9926 putc ('\n', stderr);
9927 noninteractive_need_newline = 0;
9928 vfprintf (stderr, m, ap);
9929 if (cursor_in_echo_area == 0)
9930 fprintf (stderr, "\n");
9931 fflush (stderr);
9934 else if (INTERACTIVE)
9936 /* The frame whose mini-buffer we're going to display the message
9937 on. It may be larger than the selected frame, so we need to
9938 use its buffer, not the selected frame's buffer. */
9939 Lisp_Object mini_window;
9940 struct frame *f, *sf = SELECTED_FRAME ();
9942 /* Get the frame containing the mini-buffer
9943 that the selected frame is using. */
9944 mini_window = FRAME_MINIBUF_WINDOW (sf);
9945 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9947 /* Error messages get reported properly by cmd_error, so this must be
9948 just an informative message; if the frame hasn't really been
9949 initialized yet, just toss it. */
9950 if (f->glyphs_initialized_p)
9952 if (m)
9954 ptrdiff_t len;
9955 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9956 char *message_buf = alloca (maxsize + 1);
9958 len = doprnt (message_buf, maxsize, m, 0, ap);
9960 message3 (make_string (message_buf, len));
9962 else
9963 message1 (0);
9965 /* Print should start at the beginning of the message
9966 buffer next time. */
9967 message_buf_print = 0;
9972 void
9973 message (const char *m, ...)
9975 va_list ap;
9976 va_start (ap, m);
9977 vmessage (m, ap);
9978 va_end (ap);
9982 #if 0
9983 /* The non-logging version of message. */
9985 void
9986 message_nolog (const char *m, ...)
9988 Lisp_Object old_log_max;
9989 va_list ap;
9990 va_start (ap, m);
9991 old_log_max = Vmessage_log_max;
9992 Vmessage_log_max = Qnil;
9993 vmessage (m, ap);
9994 Vmessage_log_max = old_log_max;
9995 va_end (ap);
9997 #endif
10000 /* Display the current message in the current mini-buffer. This is
10001 only called from error handlers in process.c, and is not time
10002 critical. */
10004 void
10005 update_echo_area (void)
10007 if (!NILP (echo_area_buffer[0]))
10009 Lisp_Object string;
10010 string = Fcurrent_message ();
10011 message3 (string);
10016 /* Make sure echo area buffers in `echo_buffers' are live.
10017 If they aren't, make new ones. */
10019 static void
10020 ensure_echo_area_buffers (void)
10022 int i;
10024 for (i = 0; i < 2; ++i)
10025 if (!BUFFERP (echo_buffer[i])
10026 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10028 char name[30];
10029 Lisp_Object old_buffer;
10030 int j;
10032 old_buffer = echo_buffer[i];
10033 echo_buffer[i] = Fget_buffer_create
10034 (make_formatted_string (name, " *Echo Area %d*", i));
10035 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10036 /* to force word wrap in echo area -
10037 it was decided to postpone this*/
10038 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10040 for (j = 0; j < 2; ++j)
10041 if (EQ (old_buffer, echo_area_buffer[j]))
10042 echo_area_buffer[j] = echo_buffer[i];
10047 /* Call FN with args A1..A2 with either the current or last displayed
10048 echo_area_buffer as current buffer.
10050 WHICH zero means use the current message buffer
10051 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10052 from echo_buffer[] and clear it.
10054 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10055 suitable buffer from echo_buffer[] and clear it.
10057 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10058 that the current message becomes the last displayed one, make
10059 choose a suitable buffer for echo_area_buffer[0], and clear it.
10061 Value is what FN returns. */
10063 static int
10064 with_echo_area_buffer (struct window *w, int which,
10065 int (*fn) (ptrdiff_t, Lisp_Object),
10066 ptrdiff_t a1, Lisp_Object a2)
10068 Lisp_Object buffer;
10069 int this_one, the_other, clear_buffer_p, rc;
10070 ptrdiff_t count = SPECPDL_INDEX ();
10072 /* If buffers aren't live, make new ones. */
10073 ensure_echo_area_buffers ();
10075 clear_buffer_p = 0;
10077 if (which == 0)
10078 this_one = 0, the_other = 1;
10079 else if (which > 0)
10080 this_one = 1, the_other = 0;
10081 else
10083 this_one = 0, the_other = 1;
10084 clear_buffer_p = 1;
10086 /* We need a fresh one in case the current echo buffer equals
10087 the one containing the last displayed echo area message. */
10088 if (!NILP (echo_area_buffer[this_one])
10089 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10090 echo_area_buffer[this_one] = Qnil;
10093 /* Choose a suitable buffer from echo_buffer[] is we don't
10094 have one. */
10095 if (NILP (echo_area_buffer[this_one]))
10097 echo_area_buffer[this_one]
10098 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10099 ? echo_buffer[the_other]
10100 : echo_buffer[this_one]);
10101 clear_buffer_p = 1;
10104 buffer = echo_area_buffer[this_one];
10106 /* Don't get confused by reusing the buffer used for echoing
10107 for a different purpose. */
10108 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10109 cancel_echoing ();
10111 record_unwind_protect (unwind_with_echo_area_buffer,
10112 with_echo_area_buffer_unwind_data (w));
10114 /* Make the echo area buffer current. Note that for display
10115 purposes, it is not necessary that the displayed window's buffer
10116 == current_buffer, except for text property lookup. So, let's
10117 only set that buffer temporarily here without doing a full
10118 Fset_window_buffer. We must also change w->pointm, though,
10119 because otherwise an assertions in unshow_buffer fails, and Emacs
10120 aborts. */
10121 set_buffer_internal_1 (XBUFFER (buffer));
10122 if (w)
10124 wset_buffer (w, buffer);
10125 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10128 bset_undo_list (current_buffer, Qt);
10129 bset_read_only (current_buffer, Qnil);
10130 specbind (Qinhibit_read_only, Qt);
10131 specbind (Qinhibit_modification_hooks, Qt);
10133 if (clear_buffer_p && Z > BEG)
10134 del_range (BEG, Z);
10136 eassert (BEGV >= BEG);
10137 eassert (ZV <= Z && ZV >= BEGV);
10139 rc = fn (a1, a2);
10141 eassert (BEGV >= BEG);
10142 eassert (ZV <= Z && ZV >= BEGV);
10144 unbind_to (count, Qnil);
10145 return rc;
10149 /* Save state that should be preserved around the call to the function
10150 FN called in with_echo_area_buffer. */
10152 static Lisp_Object
10153 with_echo_area_buffer_unwind_data (struct window *w)
10155 int i = 0;
10156 Lisp_Object vector, tmp;
10158 /* Reduce consing by keeping one vector in
10159 Vwith_echo_area_save_vector. */
10160 vector = Vwith_echo_area_save_vector;
10161 Vwith_echo_area_save_vector = Qnil;
10163 if (NILP (vector))
10164 vector = Fmake_vector (make_number (9), Qnil);
10166 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10167 ASET (vector, i, Vdeactivate_mark); ++i;
10168 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10170 if (w)
10172 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10173 ASET (vector, i, w->contents); ++i;
10174 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10175 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10176 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10177 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10179 else
10181 int end = i + 6;
10182 for (; i < end; ++i)
10183 ASET (vector, i, Qnil);
10186 eassert (i == ASIZE (vector));
10187 return vector;
10191 /* Restore global state from VECTOR which was created by
10192 with_echo_area_buffer_unwind_data. */
10194 static void
10195 unwind_with_echo_area_buffer (Lisp_Object vector)
10197 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10198 Vdeactivate_mark = AREF (vector, 1);
10199 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10201 if (WINDOWP (AREF (vector, 3)))
10203 struct window *w;
10204 Lisp_Object buffer;
10206 w = XWINDOW (AREF (vector, 3));
10207 buffer = AREF (vector, 4);
10209 wset_buffer (w, buffer);
10210 set_marker_both (w->pointm, buffer,
10211 XFASTINT (AREF (vector, 5)),
10212 XFASTINT (AREF (vector, 6)));
10213 set_marker_both (w->start, buffer,
10214 XFASTINT (AREF (vector, 7)),
10215 XFASTINT (AREF (vector, 8)));
10218 Vwith_echo_area_save_vector = vector;
10222 /* Set up the echo area for use by print functions. MULTIBYTE_P
10223 non-zero means we will print multibyte. */
10225 void
10226 setup_echo_area_for_printing (int multibyte_p)
10228 /* If we can't find an echo area any more, exit. */
10229 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10230 Fkill_emacs (Qnil);
10232 ensure_echo_area_buffers ();
10234 if (!message_buf_print)
10236 /* A message has been output since the last time we printed.
10237 Choose a fresh echo area buffer. */
10238 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10239 echo_area_buffer[0] = echo_buffer[1];
10240 else
10241 echo_area_buffer[0] = echo_buffer[0];
10243 /* Switch to that buffer and clear it. */
10244 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10245 bset_truncate_lines (current_buffer, Qnil);
10247 if (Z > BEG)
10249 ptrdiff_t count = SPECPDL_INDEX ();
10250 specbind (Qinhibit_read_only, Qt);
10251 /* Note that undo recording is always disabled. */
10252 del_range (BEG, Z);
10253 unbind_to (count, Qnil);
10255 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10257 /* Set up the buffer for the multibyteness we need. */
10258 if (multibyte_p
10259 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10260 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10262 /* Raise the frame containing the echo area. */
10263 if (minibuffer_auto_raise)
10265 struct frame *sf = SELECTED_FRAME ();
10266 Lisp_Object mini_window;
10267 mini_window = FRAME_MINIBUF_WINDOW (sf);
10268 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10271 message_log_maybe_newline ();
10272 message_buf_print = 1;
10274 else
10276 if (NILP (echo_area_buffer[0]))
10278 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10279 echo_area_buffer[0] = echo_buffer[1];
10280 else
10281 echo_area_buffer[0] = echo_buffer[0];
10284 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10286 /* Someone switched buffers between print requests. */
10287 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10288 bset_truncate_lines (current_buffer, Qnil);
10294 /* Display an echo area message in window W. Value is non-zero if W's
10295 height is changed. If display_last_displayed_message_p is
10296 non-zero, display the message that was last displayed, otherwise
10297 display the current message. */
10299 static int
10300 display_echo_area (struct window *w)
10302 int i, no_message_p, window_height_changed_p;
10304 /* Temporarily disable garbage collections while displaying the echo
10305 area. This is done because a GC can print a message itself.
10306 That message would modify the echo area buffer's contents while a
10307 redisplay of the buffer is going on, and seriously confuse
10308 redisplay. */
10309 ptrdiff_t count = inhibit_garbage_collection ();
10311 /* If there is no message, we must call display_echo_area_1
10312 nevertheless because it resizes the window. But we will have to
10313 reset the echo_area_buffer in question to nil at the end because
10314 with_echo_area_buffer will sets it to an empty buffer. */
10315 i = display_last_displayed_message_p ? 1 : 0;
10316 no_message_p = NILP (echo_area_buffer[i]);
10318 window_height_changed_p
10319 = with_echo_area_buffer (w, display_last_displayed_message_p,
10320 display_echo_area_1,
10321 (intptr_t) w, Qnil);
10323 if (no_message_p)
10324 echo_area_buffer[i] = Qnil;
10326 unbind_to (count, Qnil);
10327 return window_height_changed_p;
10331 /* Helper for display_echo_area. Display the current buffer which
10332 contains the current echo area message in window W, a mini-window,
10333 a pointer to which is passed in A1. A2..A4 are currently not used.
10334 Change the height of W so that all of the message is displayed.
10335 Value is non-zero if height of W was changed. */
10337 static int
10338 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10340 intptr_t i1 = a1;
10341 struct window *w = (struct window *) i1;
10342 Lisp_Object window;
10343 struct text_pos start;
10344 int window_height_changed_p = 0;
10346 /* Do this before displaying, so that we have a large enough glyph
10347 matrix for the display. If we can't get enough space for the
10348 whole text, display the last N lines. That works by setting w->start. */
10349 window_height_changed_p = resize_mini_window (w, 0);
10351 /* Use the starting position chosen by resize_mini_window. */
10352 SET_TEXT_POS_FROM_MARKER (start, w->start);
10354 /* Display. */
10355 clear_glyph_matrix (w->desired_matrix);
10356 XSETWINDOW (window, w);
10357 try_window (window, start, 0);
10359 return window_height_changed_p;
10363 /* Resize the echo area window to exactly the size needed for the
10364 currently displayed message, if there is one. If a mini-buffer
10365 is active, don't shrink it. */
10367 void
10368 resize_echo_area_exactly (void)
10370 if (BUFFERP (echo_area_buffer[0])
10371 && WINDOWP (echo_area_window))
10373 struct window *w = XWINDOW (echo_area_window);
10374 int resized_p;
10375 Lisp_Object resize_exactly;
10377 if (minibuf_level == 0)
10378 resize_exactly = Qt;
10379 else
10380 resize_exactly = Qnil;
10382 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10383 (intptr_t) w, resize_exactly);
10384 if (resized_p)
10386 ++windows_or_buffers_changed;
10387 ++update_mode_lines;
10388 redisplay_internal ();
10394 /* Callback function for with_echo_area_buffer, when used from
10395 resize_echo_area_exactly. A1 contains a pointer to the window to
10396 resize, EXACTLY non-nil means resize the mini-window exactly to the
10397 size of the text displayed. A3 and A4 are not used. Value is what
10398 resize_mini_window returns. */
10400 static int
10401 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10403 intptr_t i1 = a1;
10404 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10408 /* Resize mini-window W to fit the size of its contents. EXACT_P
10409 means size the window exactly to the size needed. Otherwise, it's
10410 only enlarged until W's buffer is empty.
10412 Set W->start to the right place to begin display. If the whole
10413 contents fit, start at the beginning. Otherwise, start so as
10414 to make the end of the contents appear. This is particularly
10415 important for y-or-n-p, but seems desirable generally.
10417 Value is non-zero if the window height has been changed. */
10420 resize_mini_window (struct window *w, int exact_p)
10422 struct frame *f = XFRAME (w->frame);
10423 int window_height_changed_p = 0;
10425 eassert (MINI_WINDOW_P (w));
10427 /* By default, start display at the beginning. */
10428 set_marker_both (w->start, w->contents,
10429 BUF_BEGV (XBUFFER (w->contents)),
10430 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10432 /* Don't resize windows while redisplaying a window; it would
10433 confuse redisplay functions when the size of the window they are
10434 displaying changes from under them. Such a resizing can happen,
10435 for instance, when which-func prints a long message while
10436 we are running fontification-functions. We're running these
10437 functions with safe_call which binds inhibit-redisplay to t. */
10438 if (!NILP (Vinhibit_redisplay))
10439 return 0;
10441 /* Nil means don't try to resize. */
10442 if (NILP (Vresize_mini_windows)
10443 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10444 return 0;
10446 if (!FRAME_MINIBUF_ONLY_P (f))
10448 struct it it;
10449 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10450 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10451 int height;
10452 EMACS_INT max_height;
10453 int unit = FRAME_LINE_HEIGHT (f);
10454 struct text_pos start;
10455 struct buffer *old_current_buffer = NULL;
10457 if (current_buffer != XBUFFER (w->contents))
10459 old_current_buffer = current_buffer;
10460 set_buffer_internal (XBUFFER (w->contents));
10463 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10465 /* Compute the max. number of lines specified by the user. */
10466 if (FLOATP (Vmax_mini_window_height))
10467 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10468 else if (INTEGERP (Vmax_mini_window_height))
10469 max_height = XINT (Vmax_mini_window_height);
10470 else
10471 max_height = total_height / 4;
10473 /* Correct that max. height if it's bogus. */
10474 max_height = clip_to_bounds (1, max_height, total_height);
10476 /* Find out the height of the text in the window. */
10477 if (it.line_wrap == TRUNCATE)
10478 height = 1;
10479 else
10481 last_height = 0;
10482 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10483 if (it.max_ascent == 0 && it.max_descent == 0)
10484 height = it.current_y + last_height;
10485 else
10486 height = it.current_y + it.max_ascent + it.max_descent;
10487 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10488 height = (height + unit - 1) / unit;
10491 /* Compute a suitable window start. */
10492 if (height > max_height)
10494 height = max_height;
10495 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10496 move_it_vertically_backward (&it, (height - 1) * unit);
10497 start = it.current.pos;
10499 else
10500 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10501 SET_MARKER_FROM_TEXT_POS (w->start, start);
10503 if (EQ (Vresize_mini_windows, Qgrow_only))
10505 /* Let it grow only, until we display an empty message, in which
10506 case the window shrinks again. */
10507 if (height > WINDOW_TOTAL_LINES (w))
10509 int old_height = WINDOW_TOTAL_LINES (w);
10511 FRAME_WINDOWS_FROZEN (f) = 1;
10512 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10513 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10515 else if (height < WINDOW_TOTAL_LINES (w)
10516 && (exact_p || BEGV == ZV))
10518 int old_height = WINDOW_TOTAL_LINES (w);
10520 FRAME_WINDOWS_FROZEN (f) = 0;
10521 shrink_mini_window (w);
10522 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10525 else
10527 /* Always resize to exact size needed. */
10528 if (height > WINDOW_TOTAL_LINES (w))
10530 int old_height = WINDOW_TOTAL_LINES (w);
10532 FRAME_WINDOWS_FROZEN (f) = 1;
10533 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10534 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10536 else if (height < WINDOW_TOTAL_LINES (w))
10538 int old_height = WINDOW_TOTAL_LINES (w);
10540 FRAME_WINDOWS_FROZEN (f) = 0;
10541 shrink_mini_window (w);
10543 if (height)
10545 FRAME_WINDOWS_FROZEN (f) = 1;
10546 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10549 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10553 if (old_current_buffer)
10554 set_buffer_internal (old_current_buffer);
10557 return window_height_changed_p;
10561 /* Value is the current message, a string, or nil if there is no
10562 current message. */
10564 Lisp_Object
10565 current_message (void)
10567 Lisp_Object msg;
10569 if (!BUFFERP (echo_area_buffer[0]))
10570 msg = Qnil;
10571 else
10573 with_echo_area_buffer (0, 0, current_message_1,
10574 (intptr_t) &msg, Qnil);
10575 if (NILP (msg))
10576 echo_area_buffer[0] = Qnil;
10579 return msg;
10583 static int
10584 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10586 intptr_t i1 = a1;
10587 Lisp_Object *msg = (Lisp_Object *) i1;
10589 if (Z > BEG)
10590 *msg = make_buffer_string (BEG, Z, 1);
10591 else
10592 *msg = Qnil;
10593 return 0;
10597 /* Push the current message on Vmessage_stack for later restoration
10598 by restore_message. Value is non-zero if the current message isn't
10599 empty. This is a relatively infrequent operation, so it's not
10600 worth optimizing. */
10602 bool
10603 push_message (void)
10605 Lisp_Object msg = current_message ();
10606 Vmessage_stack = Fcons (msg, Vmessage_stack);
10607 return STRINGP (msg);
10611 /* Restore message display from the top of Vmessage_stack. */
10613 void
10614 restore_message (void)
10616 eassert (CONSP (Vmessage_stack));
10617 message3_nolog (XCAR (Vmessage_stack));
10621 /* Handler for unwind-protect calling pop_message. */
10623 void
10624 pop_message_unwind (void)
10626 /* Pop the top-most entry off Vmessage_stack. */
10627 eassert (CONSP (Vmessage_stack));
10628 Vmessage_stack = XCDR (Vmessage_stack);
10632 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10633 exits. If the stack is not empty, we have a missing pop_message
10634 somewhere. */
10636 void
10637 check_message_stack (void)
10639 if (!NILP (Vmessage_stack))
10640 emacs_abort ();
10644 /* Truncate to NCHARS what will be displayed in the echo area the next
10645 time we display it---but don't redisplay it now. */
10647 void
10648 truncate_echo_area (ptrdiff_t nchars)
10650 if (nchars == 0)
10651 echo_area_buffer[0] = Qnil;
10652 else if (!noninteractive
10653 && INTERACTIVE
10654 && !NILP (echo_area_buffer[0]))
10656 struct frame *sf = SELECTED_FRAME ();
10657 /* Error messages get reported properly by cmd_error, so this must be
10658 just an informative message; if the frame hasn't really been
10659 initialized yet, just toss it. */
10660 if (sf->glyphs_initialized_p)
10661 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10666 /* Helper function for truncate_echo_area. Truncate the current
10667 message to at most NCHARS characters. */
10669 static int
10670 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10672 if (BEG + nchars < Z)
10673 del_range (BEG + nchars, Z);
10674 if (Z == BEG)
10675 echo_area_buffer[0] = Qnil;
10676 return 0;
10679 /* Set the current message to STRING. */
10681 static void
10682 set_message (Lisp_Object string)
10684 eassert (STRINGP (string));
10686 message_enable_multibyte = STRING_MULTIBYTE (string);
10688 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10689 message_buf_print = 0;
10690 help_echo_showing_p = 0;
10692 if (STRINGP (Vdebug_on_message)
10693 && STRINGP (string)
10694 && fast_string_match (Vdebug_on_message, string) >= 0)
10695 call_debugger (list2 (Qerror, string));
10699 /* Helper function for set_message. First argument is ignored and second
10700 argument has the same meaning as for set_message.
10701 This function is called with the echo area buffer being current. */
10703 static int
10704 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10706 eassert (STRINGP (string));
10708 /* Change multibyteness of the echo buffer appropriately. */
10709 if (message_enable_multibyte
10710 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10711 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10713 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10714 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10715 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10717 /* Insert new message at BEG. */
10718 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10720 /* This function takes care of single/multibyte conversion.
10721 We just have to ensure that the echo area buffer has the right
10722 setting of enable_multibyte_characters. */
10723 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10725 return 0;
10729 /* Clear messages. CURRENT_P non-zero means clear the current
10730 message. LAST_DISPLAYED_P non-zero means clear the message
10731 last displayed. */
10733 void
10734 clear_message (int current_p, int last_displayed_p)
10736 if (current_p)
10738 echo_area_buffer[0] = Qnil;
10739 message_cleared_p = 1;
10742 if (last_displayed_p)
10743 echo_area_buffer[1] = Qnil;
10745 message_buf_print = 0;
10748 /* Clear garbaged frames.
10750 This function is used where the old redisplay called
10751 redraw_garbaged_frames which in turn called redraw_frame which in
10752 turn called clear_frame. The call to clear_frame was a source of
10753 flickering. I believe a clear_frame is not necessary. It should
10754 suffice in the new redisplay to invalidate all current matrices,
10755 and ensure a complete redisplay of all windows. */
10757 static void
10758 clear_garbaged_frames (void)
10760 if (frame_garbaged)
10762 Lisp_Object tail, frame;
10763 int changed_count = 0;
10765 FOR_EACH_FRAME (tail, frame)
10767 struct frame *f = XFRAME (frame);
10769 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10771 if (f->resized_p)
10773 redraw_frame (f);
10774 f->force_flush_display_p = 1;
10776 clear_current_matrices (f);
10777 changed_count++;
10778 f->garbaged = 0;
10779 f->resized_p = 0;
10783 frame_garbaged = 0;
10784 if (changed_count)
10785 ++windows_or_buffers_changed;
10790 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10791 is non-zero update selected_frame. Value is non-zero if the
10792 mini-windows height has been changed. */
10794 static int
10795 echo_area_display (int update_frame_p)
10797 Lisp_Object mini_window;
10798 struct window *w;
10799 struct frame *f;
10800 int window_height_changed_p = 0;
10801 struct frame *sf = SELECTED_FRAME ();
10803 mini_window = FRAME_MINIBUF_WINDOW (sf);
10804 w = XWINDOW (mini_window);
10805 f = XFRAME (WINDOW_FRAME (w));
10807 /* Don't display if frame is invisible or not yet initialized. */
10808 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10809 return 0;
10811 #ifdef HAVE_WINDOW_SYSTEM
10812 /* When Emacs starts, selected_frame may be the initial terminal
10813 frame. If we let this through, a message would be displayed on
10814 the terminal. */
10815 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10816 return 0;
10817 #endif /* HAVE_WINDOW_SYSTEM */
10819 /* Redraw garbaged frames. */
10820 clear_garbaged_frames ();
10822 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10824 echo_area_window = mini_window;
10825 window_height_changed_p = display_echo_area (w);
10826 w->must_be_updated_p = 1;
10828 /* Update the display, unless called from redisplay_internal.
10829 Also don't update the screen during redisplay itself. The
10830 update will happen at the end of redisplay, and an update
10831 here could cause confusion. */
10832 if (update_frame_p && !redisplaying_p)
10834 int n = 0;
10836 /* If the display update has been interrupted by pending
10837 input, update mode lines in the frame. Due to the
10838 pending input, it might have been that redisplay hasn't
10839 been called, so that mode lines above the echo area are
10840 garbaged. This looks odd, so we prevent it here. */
10841 if (!display_completed)
10842 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10844 if (window_height_changed_p
10845 /* Don't do this if Emacs is shutting down. Redisplay
10846 needs to run hooks. */
10847 && !NILP (Vrun_hooks))
10849 /* Must update other windows. Likewise as in other
10850 cases, don't let this update be interrupted by
10851 pending input. */
10852 ptrdiff_t count = SPECPDL_INDEX ();
10853 specbind (Qredisplay_dont_pause, Qt);
10854 windows_or_buffers_changed = 1;
10855 redisplay_internal ();
10856 unbind_to (count, Qnil);
10858 else if (FRAME_WINDOW_P (f) && n == 0)
10860 /* Window configuration is the same as before.
10861 Can do with a display update of the echo area,
10862 unless we displayed some mode lines. */
10863 update_single_window (w, 1);
10864 FRAME_RIF (f)->flush_display (f);
10866 else
10867 update_frame (f, 1, 1);
10869 /* If cursor is in the echo area, make sure that the next
10870 redisplay displays the minibuffer, so that the cursor will
10871 be replaced with what the minibuffer wants. */
10872 if (cursor_in_echo_area)
10873 ++windows_or_buffers_changed;
10876 else if (!EQ (mini_window, selected_window))
10877 windows_or_buffers_changed++;
10879 /* Last displayed message is now the current message. */
10880 echo_area_buffer[1] = echo_area_buffer[0];
10881 /* Inform read_char that we're not echoing. */
10882 echo_message_buffer = Qnil;
10884 /* Prevent redisplay optimization in redisplay_internal by resetting
10885 this_line_start_pos. This is done because the mini-buffer now
10886 displays the message instead of its buffer text. */
10887 if (EQ (mini_window, selected_window))
10888 CHARPOS (this_line_start_pos) = 0;
10890 return window_height_changed_p;
10893 /* Nonzero if the current window's buffer is shown in more than one
10894 window and was modified since last redisplay. */
10896 static int
10897 buffer_shared_and_changed (void)
10899 return (buffer_window_count (current_buffer) > 1
10900 && UNCHANGED_MODIFIED < MODIFF);
10903 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10904 is enabled and mark of W's buffer was changed since last W's update. */
10906 static int
10907 window_buffer_changed (struct window *w)
10909 struct buffer *b = XBUFFER (w->contents);
10911 eassert (BUFFER_LIVE_P (b));
10913 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10914 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10915 != (w->region_showing != 0)));
10918 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10920 static int
10921 mode_line_update_needed (struct window *w)
10923 return (w->column_number_displayed != -1
10924 && !(PT == w->last_point && !window_outdated (w))
10925 && (w->column_number_displayed != current_column ()));
10928 /* Nonzero if window start of W is frozen and may not be changed during
10929 redisplay. */
10931 static bool
10932 window_frozen_p (struct window *w)
10934 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10936 Lisp_Object window;
10938 XSETWINDOW (window, w);
10939 if (MINI_WINDOW_P (w))
10940 return 0;
10941 else if (EQ (window, selected_window))
10942 return 0;
10943 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10944 && EQ (window, Vminibuf_scroll_window))
10945 /* This special window can't be frozen too. */
10946 return 0;
10947 else
10948 return 1;
10950 return 0;
10953 /***********************************************************************
10954 Mode Lines and Frame Titles
10955 ***********************************************************************/
10957 /* A buffer for constructing non-propertized mode-line strings and
10958 frame titles in it; allocated from the heap in init_xdisp and
10959 resized as needed in store_mode_line_noprop_char. */
10961 static char *mode_line_noprop_buf;
10963 /* The buffer's end, and a current output position in it. */
10965 static char *mode_line_noprop_buf_end;
10966 static char *mode_line_noprop_ptr;
10968 #define MODE_LINE_NOPROP_LEN(start) \
10969 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10971 static enum {
10972 MODE_LINE_DISPLAY = 0,
10973 MODE_LINE_TITLE,
10974 MODE_LINE_NOPROP,
10975 MODE_LINE_STRING
10976 } mode_line_target;
10978 /* Alist that caches the results of :propertize.
10979 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10980 static Lisp_Object mode_line_proptrans_alist;
10982 /* List of strings making up the mode-line. */
10983 static Lisp_Object mode_line_string_list;
10985 /* Base face property when building propertized mode line string. */
10986 static Lisp_Object mode_line_string_face;
10987 static Lisp_Object mode_line_string_face_prop;
10990 /* Unwind data for mode line strings */
10992 static Lisp_Object Vmode_line_unwind_vector;
10994 static Lisp_Object
10995 format_mode_line_unwind_data (struct frame *target_frame,
10996 struct buffer *obuf,
10997 Lisp_Object owin,
10998 int save_proptrans)
11000 Lisp_Object vector, tmp;
11002 /* Reduce consing by keeping one vector in
11003 Vwith_echo_area_save_vector. */
11004 vector = Vmode_line_unwind_vector;
11005 Vmode_line_unwind_vector = Qnil;
11007 if (NILP (vector))
11008 vector = Fmake_vector (make_number (10), Qnil);
11010 ASET (vector, 0, make_number (mode_line_target));
11011 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11012 ASET (vector, 2, mode_line_string_list);
11013 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11014 ASET (vector, 4, mode_line_string_face);
11015 ASET (vector, 5, mode_line_string_face_prop);
11017 if (obuf)
11018 XSETBUFFER (tmp, obuf);
11019 else
11020 tmp = Qnil;
11021 ASET (vector, 6, tmp);
11022 ASET (vector, 7, owin);
11023 if (target_frame)
11025 /* Similarly to `with-selected-window', if the operation selects
11026 a window on another frame, we must restore that frame's
11027 selected window, and (for a tty) the top-frame. */
11028 ASET (vector, 8, target_frame->selected_window);
11029 if (FRAME_TERMCAP_P (target_frame))
11030 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11033 return vector;
11036 static void
11037 unwind_format_mode_line (Lisp_Object vector)
11039 Lisp_Object old_window = AREF (vector, 7);
11040 Lisp_Object target_frame_window = AREF (vector, 8);
11041 Lisp_Object old_top_frame = AREF (vector, 9);
11043 mode_line_target = XINT (AREF (vector, 0));
11044 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11045 mode_line_string_list = AREF (vector, 2);
11046 if (! EQ (AREF (vector, 3), Qt))
11047 mode_line_proptrans_alist = AREF (vector, 3);
11048 mode_line_string_face = AREF (vector, 4);
11049 mode_line_string_face_prop = AREF (vector, 5);
11051 /* Select window before buffer, since it may change the buffer. */
11052 if (!NILP (old_window))
11054 /* If the operation that we are unwinding had selected a window
11055 on a different frame, reset its frame-selected-window. For a
11056 text terminal, reset its top-frame if necessary. */
11057 if (!NILP (target_frame_window))
11059 Lisp_Object frame
11060 = WINDOW_FRAME (XWINDOW (target_frame_window));
11062 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11063 Fselect_window (target_frame_window, Qt);
11065 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11066 Fselect_frame (old_top_frame, Qt);
11069 Fselect_window (old_window, Qt);
11072 if (!NILP (AREF (vector, 6)))
11074 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11075 ASET (vector, 6, Qnil);
11078 Vmode_line_unwind_vector = vector;
11082 /* Store a single character C for the frame title in mode_line_noprop_buf.
11083 Re-allocate mode_line_noprop_buf if necessary. */
11085 static void
11086 store_mode_line_noprop_char (char c)
11088 /* If output position has reached the end of the allocated buffer,
11089 increase the buffer's size. */
11090 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11092 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11093 ptrdiff_t size = len;
11094 mode_line_noprop_buf =
11095 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11096 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11097 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11100 *mode_line_noprop_ptr++ = c;
11104 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11105 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11106 characters that yield more columns than PRECISION; PRECISION <= 0
11107 means copy the whole string. Pad with spaces until FIELD_WIDTH
11108 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11109 pad. Called from display_mode_element when it is used to build a
11110 frame title. */
11112 static int
11113 store_mode_line_noprop (const char *string, int field_width, int precision)
11115 const unsigned char *str = (const unsigned char *) string;
11116 int n = 0;
11117 ptrdiff_t dummy, nbytes;
11119 /* Copy at most PRECISION chars from STR. */
11120 nbytes = strlen (string);
11121 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11122 while (nbytes--)
11123 store_mode_line_noprop_char (*str++);
11125 /* Fill up with spaces until FIELD_WIDTH reached. */
11126 while (field_width > 0
11127 && n < field_width)
11129 store_mode_line_noprop_char (' ');
11130 ++n;
11133 return n;
11136 /***********************************************************************
11137 Frame Titles
11138 ***********************************************************************/
11140 #ifdef HAVE_WINDOW_SYSTEM
11142 /* Set the title of FRAME, if it has changed. The title format is
11143 Vicon_title_format if FRAME is iconified, otherwise it is
11144 frame_title_format. */
11146 static void
11147 x_consider_frame_title (Lisp_Object frame)
11149 struct frame *f = XFRAME (frame);
11151 if (FRAME_WINDOW_P (f)
11152 || FRAME_MINIBUF_ONLY_P (f)
11153 || f->explicit_name)
11155 /* Do we have more than one visible frame on this X display? */
11156 Lisp_Object tail, other_frame, fmt;
11157 ptrdiff_t title_start;
11158 char *title;
11159 ptrdiff_t len;
11160 struct it it;
11161 ptrdiff_t count = SPECPDL_INDEX ();
11163 FOR_EACH_FRAME (tail, other_frame)
11165 struct frame *tf = XFRAME (other_frame);
11167 if (tf != f
11168 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11169 && !FRAME_MINIBUF_ONLY_P (tf)
11170 && !EQ (other_frame, tip_frame)
11171 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11172 break;
11175 /* Set global variable indicating that multiple frames exist. */
11176 multiple_frames = CONSP (tail);
11178 /* Switch to the buffer of selected window of the frame. Set up
11179 mode_line_target so that display_mode_element will output into
11180 mode_line_noprop_buf; then display the title. */
11181 record_unwind_protect (unwind_format_mode_line,
11182 format_mode_line_unwind_data
11183 (f, current_buffer, selected_window, 0));
11185 Fselect_window (f->selected_window, Qt);
11186 set_buffer_internal_1
11187 (XBUFFER (XWINDOW (f->selected_window)->contents));
11188 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11190 mode_line_target = MODE_LINE_TITLE;
11191 title_start = MODE_LINE_NOPROP_LEN (0);
11192 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11193 NULL, DEFAULT_FACE_ID);
11194 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11195 len = MODE_LINE_NOPROP_LEN (title_start);
11196 title = mode_line_noprop_buf + title_start;
11197 unbind_to (count, Qnil);
11199 /* Set the title only if it's changed. This avoids consing in
11200 the common case where it hasn't. (If it turns out that we've
11201 already wasted too much time by walking through the list with
11202 display_mode_element, then we might need to optimize at a
11203 higher level than this.) */
11204 if (! STRINGP (f->name)
11205 || SBYTES (f->name) != len
11206 || memcmp (title, SDATA (f->name), len) != 0)
11207 x_implicitly_set_name (f, make_string (title, len), Qnil);
11211 #endif /* not HAVE_WINDOW_SYSTEM */
11214 /***********************************************************************
11215 Menu Bars
11216 ***********************************************************************/
11219 /* Prepare for redisplay by updating menu-bar item lists when
11220 appropriate. This can call eval. */
11222 void
11223 prepare_menu_bars (void)
11225 int all_windows;
11226 struct gcpro gcpro1, gcpro2;
11227 struct frame *f;
11228 Lisp_Object tooltip_frame;
11230 #ifdef HAVE_WINDOW_SYSTEM
11231 tooltip_frame = tip_frame;
11232 #else
11233 tooltip_frame = Qnil;
11234 #endif
11236 /* Update all frame titles based on their buffer names, etc. We do
11237 this before the menu bars so that the buffer-menu will show the
11238 up-to-date frame titles. */
11239 #ifdef HAVE_WINDOW_SYSTEM
11240 if (windows_or_buffers_changed || update_mode_lines)
11242 Lisp_Object tail, frame;
11244 FOR_EACH_FRAME (tail, frame)
11246 f = XFRAME (frame);
11247 if (!EQ (frame, tooltip_frame)
11248 && (FRAME_ICONIFIED_P (f)
11249 || FRAME_VISIBLE_P (f) == 1
11250 /* Exclude TTY frames that are obscured because they
11251 are not the top frame on their console. This is
11252 because x_consider_frame_title actually switches
11253 to the frame, which for TTY frames means it is
11254 marked as garbaged, and will be completely
11255 redrawn on the next redisplay cycle. This causes
11256 TTY frames to be completely redrawn, when there
11257 are more than one of them, even though nothing
11258 should be changed on display. */
11259 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11260 x_consider_frame_title (frame);
11263 #endif /* HAVE_WINDOW_SYSTEM */
11265 /* Update the menu bar item lists, if appropriate. This has to be
11266 done before any actual redisplay or generation of display lines. */
11267 all_windows = (update_mode_lines
11268 || buffer_shared_and_changed ()
11269 || windows_or_buffers_changed);
11270 if (all_windows)
11272 Lisp_Object tail, frame;
11273 ptrdiff_t count = SPECPDL_INDEX ();
11274 /* 1 means that update_menu_bar has run its hooks
11275 so any further calls to update_menu_bar shouldn't do so again. */
11276 int menu_bar_hooks_run = 0;
11278 record_unwind_save_match_data ();
11280 FOR_EACH_FRAME (tail, frame)
11282 f = XFRAME (frame);
11284 /* Ignore tooltip frame. */
11285 if (EQ (frame, tooltip_frame))
11286 continue;
11288 /* If a window on this frame changed size, report that to
11289 the user and clear the size-change flag. */
11290 if (FRAME_WINDOW_SIZES_CHANGED (f))
11292 Lisp_Object functions;
11294 /* Clear flag first in case we get an error below. */
11295 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11296 functions = Vwindow_size_change_functions;
11297 GCPRO2 (tail, functions);
11299 while (CONSP (functions))
11301 if (!EQ (XCAR (functions), Qt))
11302 call1 (XCAR (functions), frame);
11303 functions = XCDR (functions);
11305 UNGCPRO;
11308 GCPRO1 (tail);
11309 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11310 #ifdef HAVE_WINDOW_SYSTEM
11311 update_tool_bar (f, 0);
11312 #endif
11313 #ifdef HAVE_NS
11314 if (windows_or_buffers_changed
11315 && FRAME_NS_P (f))
11316 ns_set_doc_edited
11317 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11318 #endif
11319 UNGCPRO;
11322 unbind_to (count, Qnil);
11324 else
11326 struct frame *sf = SELECTED_FRAME ();
11327 update_menu_bar (sf, 1, 0);
11328 #ifdef HAVE_WINDOW_SYSTEM
11329 update_tool_bar (sf, 1);
11330 #endif
11335 /* Update the menu bar item list for frame F. This has to be done
11336 before we start to fill in any display lines, because it can call
11337 eval.
11339 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11341 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11342 already ran the menu bar hooks for this redisplay, so there
11343 is no need to run them again. The return value is the
11344 updated value of this flag, to pass to the next call. */
11346 static int
11347 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11349 Lisp_Object window;
11350 register struct window *w;
11352 /* If called recursively during a menu update, do nothing. This can
11353 happen when, for instance, an activate-menubar-hook causes a
11354 redisplay. */
11355 if (inhibit_menubar_update)
11356 return hooks_run;
11358 window = FRAME_SELECTED_WINDOW (f);
11359 w = XWINDOW (window);
11361 if (FRAME_WINDOW_P (f)
11363 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11364 || defined (HAVE_NS) || defined (USE_GTK)
11365 FRAME_EXTERNAL_MENU_BAR (f)
11366 #else
11367 FRAME_MENU_BAR_LINES (f) > 0
11368 #endif
11369 : FRAME_MENU_BAR_LINES (f) > 0)
11371 /* If the user has switched buffers or windows, we need to
11372 recompute to reflect the new bindings. But we'll
11373 recompute when update_mode_lines is set too; that means
11374 that people can use force-mode-line-update to request
11375 that the menu bar be recomputed. The adverse effect on
11376 the rest of the redisplay algorithm is about the same as
11377 windows_or_buffers_changed anyway. */
11378 if (windows_or_buffers_changed
11379 /* This used to test w->update_mode_line, but we believe
11380 there is no need to recompute the menu in that case. */
11381 || update_mode_lines
11382 || window_buffer_changed (w))
11384 struct buffer *prev = current_buffer;
11385 ptrdiff_t count = SPECPDL_INDEX ();
11387 specbind (Qinhibit_menubar_update, Qt);
11389 set_buffer_internal_1 (XBUFFER (w->contents));
11390 if (save_match_data)
11391 record_unwind_save_match_data ();
11392 if (NILP (Voverriding_local_map_menu_flag))
11394 specbind (Qoverriding_terminal_local_map, Qnil);
11395 specbind (Qoverriding_local_map, Qnil);
11398 if (!hooks_run)
11400 /* Run the Lucid hook. */
11401 safe_run_hooks (Qactivate_menubar_hook);
11403 /* If it has changed current-menubar from previous value,
11404 really recompute the menu-bar from the value. */
11405 if (! NILP (Vlucid_menu_bar_dirty_flag))
11406 call0 (Qrecompute_lucid_menubar);
11408 safe_run_hooks (Qmenu_bar_update_hook);
11410 hooks_run = 1;
11413 XSETFRAME (Vmenu_updating_frame, f);
11414 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11416 /* Redisplay the menu bar in case we changed it. */
11417 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11418 || defined (HAVE_NS) || defined (USE_GTK)
11419 if (FRAME_WINDOW_P (f))
11421 #if defined (HAVE_NS)
11422 /* All frames on Mac OS share the same menubar. So only
11423 the selected frame should be allowed to set it. */
11424 if (f == SELECTED_FRAME ())
11425 #endif
11426 set_frame_menubar (f, 0, 0);
11428 else
11429 /* On a terminal screen, the menu bar is an ordinary screen
11430 line, and this makes it get updated. */
11431 w->update_mode_line = 1;
11432 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11433 /* In the non-toolkit version, the menu bar is an ordinary screen
11434 line, and this makes it get updated. */
11435 w->update_mode_line = 1;
11436 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11438 unbind_to (count, Qnil);
11439 set_buffer_internal_1 (prev);
11443 return hooks_run;
11448 /***********************************************************************
11449 Output Cursor
11450 ***********************************************************************/
11452 #ifdef HAVE_WINDOW_SYSTEM
11454 /* EXPORT:
11455 Nominal cursor position -- where to draw output.
11456 HPOS and VPOS are window relative glyph matrix coordinates.
11457 X and Y are window relative pixel coordinates. */
11459 struct cursor_pos output_cursor;
11462 /* EXPORT:
11463 Set the global variable output_cursor to CURSOR. All cursor
11464 positions are relative to currently updated window. */
11466 void
11467 set_output_cursor (struct cursor_pos *cursor)
11469 output_cursor.hpos = cursor->hpos;
11470 output_cursor.vpos = cursor->vpos;
11471 output_cursor.x = cursor->x;
11472 output_cursor.y = cursor->y;
11476 /* EXPORT for RIF:
11477 Set a nominal cursor position.
11479 HPOS and VPOS are column/row positions in a window glyph matrix.
11480 X and Y are window text area relative pixel positions.
11482 This is always done during window update, so the position is the
11483 future output cursor position for currently updated window W.
11484 NOTE: W is used only to check whether this function is called
11485 in a consistent manner via the redisplay interface. */
11487 void
11488 x_cursor_to (struct window *w, int vpos, int hpos, int y, int x)
11490 eassert (w);
11492 /* Set the output cursor. */
11493 output_cursor.hpos = hpos;
11494 output_cursor.vpos = vpos;
11495 output_cursor.x = x;
11496 output_cursor.y = y;
11499 #endif /* HAVE_WINDOW_SYSTEM */
11502 /***********************************************************************
11503 Tool-bars
11504 ***********************************************************************/
11506 #ifdef HAVE_WINDOW_SYSTEM
11508 /* Where the mouse was last time we reported a mouse event. */
11510 struct frame *last_mouse_frame;
11512 /* Tool-bar item index of the item on which a mouse button was pressed
11513 or -1. */
11515 int last_tool_bar_item;
11517 /* Select `frame' temporarily without running all the code in
11518 do_switch_frame.
11519 FIXME: Maybe do_switch_frame should be trimmed down similarly
11520 when `norecord' is set. */
11521 static void
11522 fast_set_selected_frame (Lisp_Object frame)
11524 if (!EQ (selected_frame, frame))
11526 selected_frame = frame;
11527 selected_window = XFRAME (frame)->selected_window;
11531 /* Update the tool-bar item list for frame F. This has to be done
11532 before we start to fill in any display lines. Called from
11533 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11534 and restore it here. */
11536 static void
11537 update_tool_bar (struct frame *f, int save_match_data)
11539 #if defined (USE_GTK) || defined (HAVE_NS)
11540 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11541 #else
11542 int do_update = WINDOWP (f->tool_bar_window)
11543 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11544 #endif
11546 if (do_update)
11548 Lisp_Object window;
11549 struct window *w;
11551 window = FRAME_SELECTED_WINDOW (f);
11552 w = XWINDOW (window);
11554 /* If the user has switched buffers or windows, we need to
11555 recompute to reflect the new bindings. But we'll
11556 recompute when update_mode_lines is set too; that means
11557 that people can use force-mode-line-update to request
11558 that the menu bar be recomputed. The adverse effect on
11559 the rest of the redisplay algorithm is about the same as
11560 windows_or_buffers_changed anyway. */
11561 if (windows_or_buffers_changed
11562 || w->update_mode_line
11563 || update_mode_lines
11564 || window_buffer_changed (w))
11566 struct buffer *prev = current_buffer;
11567 ptrdiff_t count = SPECPDL_INDEX ();
11568 Lisp_Object frame, new_tool_bar;
11569 int new_n_tool_bar;
11570 struct gcpro gcpro1;
11572 /* Set current_buffer to the buffer of the selected
11573 window of the frame, so that we get the right local
11574 keymaps. */
11575 set_buffer_internal_1 (XBUFFER (w->contents));
11577 /* Save match data, if we must. */
11578 if (save_match_data)
11579 record_unwind_save_match_data ();
11581 /* Make sure that we don't accidentally use bogus keymaps. */
11582 if (NILP (Voverriding_local_map_menu_flag))
11584 specbind (Qoverriding_terminal_local_map, Qnil);
11585 specbind (Qoverriding_local_map, Qnil);
11588 GCPRO1 (new_tool_bar);
11590 /* We must temporarily set the selected frame to this frame
11591 before calling tool_bar_items, because the calculation of
11592 the tool-bar keymap uses the selected frame (see
11593 `tool-bar-make-keymap' in tool-bar.el). */
11594 eassert (EQ (selected_window,
11595 /* Since we only explicitly preserve selected_frame,
11596 check that selected_window would be redundant. */
11597 XFRAME (selected_frame)->selected_window));
11598 record_unwind_protect (fast_set_selected_frame, selected_frame);
11599 XSETFRAME (frame, f);
11600 fast_set_selected_frame (frame);
11602 /* Build desired tool-bar items from keymaps. */
11603 new_tool_bar
11604 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11605 &new_n_tool_bar);
11607 /* Redisplay the tool-bar if we changed it. */
11608 if (new_n_tool_bar != f->n_tool_bar_items
11609 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11611 /* Redisplay that happens asynchronously due to an expose event
11612 may access f->tool_bar_items. Make sure we update both
11613 variables within BLOCK_INPUT so no such event interrupts. */
11614 block_input ();
11615 fset_tool_bar_items (f, new_tool_bar);
11616 f->n_tool_bar_items = new_n_tool_bar;
11617 w->update_mode_line = 1;
11618 unblock_input ();
11621 UNGCPRO;
11623 unbind_to (count, Qnil);
11624 set_buffer_internal_1 (prev);
11630 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11631 F's desired tool-bar contents. F->tool_bar_items must have
11632 been set up previously by calling prepare_menu_bars. */
11634 static void
11635 build_desired_tool_bar_string (struct frame *f)
11637 int i, size, size_needed;
11638 struct gcpro gcpro1, gcpro2, gcpro3;
11639 Lisp_Object image, plist, props;
11641 image = plist = props = Qnil;
11642 GCPRO3 (image, plist, props);
11644 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11645 Otherwise, make a new string. */
11647 /* The size of the string we might be able to reuse. */
11648 size = (STRINGP (f->desired_tool_bar_string)
11649 ? SCHARS (f->desired_tool_bar_string)
11650 : 0);
11652 /* We need one space in the string for each image. */
11653 size_needed = f->n_tool_bar_items;
11655 /* Reuse f->desired_tool_bar_string, if possible. */
11656 if (size < size_needed || NILP (f->desired_tool_bar_string))
11657 fset_desired_tool_bar_string
11658 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11659 else
11661 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11662 Fremove_text_properties (make_number (0), make_number (size),
11663 props, f->desired_tool_bar_string);
11666 /* Put a `display' property on the string for the images to display,
11667 put a `menu_item' property on tool-bar items with a value that
11668 is the index of the item in F's tool-bar item vector. */
11669 for (i = 0; i < f->n_tool_bar_items; ++i)
11671 #define PROP(IDX) \
11672 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11674 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11675 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11676 int hmargin, vmargin, relief, idx, end;
11678 /* If image is a vector, choose the image according to the
11679 button state. */
11680 image = PROP (TOOL_BAR_ITEM_IMAGES);
11681 if (VECTORP (image))
11683 if (enabled_p)
11684 idx = (selected_p
11685 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11686 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11687 else
11688 idx = (selected_p
11689 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11690 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11692 eassert (ASIZE (image) >= idx);
11693 image = AREF (image, idx);
11695 else
11696 idx = -1;
11698 /* Ignore invalid image specifications. */
11699 if (!valid_image_p (image))
11700 continue;
11702 /* Display the tool-bar button pressed, or depressed. */
11703 plist = Fcopy_sequence (XCDR (image));
11705 /* Compute margin and relief to draw. */
11706 relief = (tool_bar_button_relief >= 0
11707 ? tool_bar_button_relief
11708 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11709 hmargin = vmargin = relief;
11711 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11712 INT_MAX - max (hmargin, vmargin)))
11714 hmargin += XFASTINT (Vtool_bar_button_margin);
11715 vmargin += XFASTINT (Vtool_bar_button_margin);
11717 else if (CONSP (Vtool_bar_button_margin))
11719 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11720 INT_MAX - hmargin))
11721 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11723 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11724 INT_MAX - vmargin))
11725 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11728 if (auto_raise_tool_bar_buttons_p)
11730 /* Add a `:relief' property to the image spec if the item is
11731 selected. */
11732 if (selected_p)
11734 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11735 hmargin -= relief;
11736 vmargin -= relief;
11739 else
11741 /* If image is selected, display it pressed, i.e. with a
11742 negative relief. If it's not selected, display it with a
11743 raised relief. */
11744 plist = Fplist_put (plist, QCrelief,
11745 (selected_p
11746 ? make_number (-relief)
11747 : make_number (relief)));
11748 hmargin -= relief;
11749 vmargin -= relief;
11752 /* Put a margin around the image. */
11753 if (hmargin || vmargin)
11755 if (hmargin == vmargin)
11756 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11757 else
11758 plist = Fplist_put (plist, QCmargin,
11759 Fcons (make_number (hmargin),
11760 make_number (vmargin)));
11763 /* If button is not enabled, and we don't have special images
11764 for the disabled state, make the image appear disabled by
11765 applying an appropriate algorithm to it. */
11766 if (!enabled_p && idx < 0)
11767 plist = Fplist_put (plist, QCconversion, Qdisabled);
11769 /* Put a `display' text property on the string for the image to
11770 display. Put a `menu-item' property on the string that gives
11771 the start of this item's properties in the tool-bar items
11772 vector. */
11773 image = Fcons (Qimage, plist);
11774 props = list4 (Qdisplay, image,
11775 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11777 /* Let the last image hide all remaining spaces in the tool bar
11778 string. The string can be longer than needed when we reuse a
11779 previous string. */
11780 if (i + 1 == f->n_tool_bar_items)
11781 end = SCHARS (f->desired_tool_bar_string);
11782 else
11783 end = i + 1;
11784 Fadd_text_properties (make_number (i), make_number (end),
11785 props, f->desired_tool_bar_string);
11786 #undef PROP
11789 UNGCPRO;
11793 /* Display one line of the tool-bar of frame IT->f.
11795 HEIGHT specifies the desired height of the tool-bar line.
11796 If the actual height of the glyph row is less than HEIGHT, the
11797 row's height is increased to HEIGHT, and the icons are centered
11798 vertically in the new height.
11800 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11801 count a final empty row in case the tool-bar width exactly matches
11802 the window width.
11805 static void
11806 display_tool_bar_line (struct it *it, int height)
11808 struct glyph_row *row = it->glyph_row;
11809 int max_x = it->last_visible_x;
11810 struct glyph *last;
11812 prepare_desired_row (row);
11813 row->y = it->current_y;
11815 /* Note that this isn't made use of if the face hasn't a box,
11816 so there's no need to check the face here. */
11817 it->start_of_box_run_p = 1;
11819 while (it->current_x < max_x)
11821 int x, n_glyphs_before, i, nglyphs;
11822 struct it it_before;
11824 /* Get the next display element. */
11825 if (!get_next_display_element (it))
11827 /* Don't count empty row if we are counting needed tool-bar lines. */
11828 if (height < 0 && !it->hpos)
11829 return;
11830 break;
11833 /* Produce glyphs. */
11834 n_glyphs_before = row->used[TEXT_AREA];
11835 it_before = *it;
11837 PRODUCE_GLYPHS (it);
11839 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11840 i = 0;
11841 x = it_before.current_x;
11842 while (i < nglyphs)
11844 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11846 if (x + glyph->pixel_width > max_x)
11848 /* Glyph doesn't fit on line. Backtrack. */
11849 row->used[TEXT_AREA] = n_glyphs_before;
11850 *it = it_before;
11851 /* If this is the only glyph on this line, it will never fit on the
11852 tool-bar, so skip it. But ensure there is at least one glyph,
11853 so we don't accidentally disable the tool-bar. */
11854 if (n_glyphs_before == 0
11855 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11856 break;
11857 goto out;
11860 ++it->hpos;
11861 x += glyph->pixel_width;
11862 ++i;
11865 /* Stop at line end. */
11866 if (ITERATOR_AT_END_OF_LINE_P (it))
11867 break;
11869 set_iterator_to_next (it, 1);
11872 out:;
11874 row->displays_text_p = row->used[TEXT_AREA] != 0;
11876 /* Use default face for the border below the tool bar.
11878 FIXME: When auto-resize-tool-bars is grow-only, there is
11879 no additional border below the possibly empty tool-bar lines.
11880 So to make the extra empty lines look "normal", we have to
11881 use the tool-bar face for the border too. */
11882 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11883 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11884 it->face_id = DEFAULT_FACE_ID;
11886 extend_face_to_end_of_line (it);
11887 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11888 last->right_box_line_p = 1;
11889 if (last == row->glyphs[TEXT_AREA])
11890 last->left_box_line_p = 1;
11892 /* Make line the desired height and center it vertically. */
11893 if ((height -= it->max_ascent + it->max_descent) > 0)
11895 /* Don't add more than one line height. */
11896 height %= FRAME_LINE_HEIGHT (it->f);
11897 it->max_ascent += height / 2;
11898 it->max_descent += (height + 1) / 2;
11901 compute_line_metrics (it);
11903 /* If line is empty, make it occupy the rest of the tool-bar. */
11904 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11906 row->height = row->phys_height = it->last_visible_y - row->y;
11907 row->visible_height = row->height;
11908 row->ascent = row->phys_ascent = 0;
11909 row->extra_line_spacing = 0;
11912 row->full_width_p = 1;
11913 row->continued_p = 0;
11914 row->truncated_on_left_p = 0;
11915 row->truncated_on_right_p = 0;
11917 it->current_x = it->hpos = 0;
11918 it->current_y += row->height;
11919 ++it->vpos;
11920 ++it->glyph_row;
11924 /* Max tool-bar height. */
11926 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11927 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11929 /* Value is the number of screen lines needed to make all tool-bar
11930 items of frame F visible. The number of actual rows needed is
11931 returned in *N_ROWS if non-NULL. */
11933 static int
11934 tool_bar_lines_needed (struct frame *f, int *n_rows)
11936 struct window *w = XWINDOW (f->tool_bar_window);
11937 struct it it;
11938 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11939 the desired matrix, so use (unused) mode-line row as temporary row to
11940 avoid destroying the first tool-bar row. */
11941 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11943 /* Initialize an iterator for iteration over
11944 F->desired_tool_bar_string in the tool-bar window of frame F. */
11945 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11946 it.first_visible_x = 0;
11947 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11948 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11949 it.paragraph_embedding = L2R;
11951 while (!ITERATOR_AT_END_P (&it))
11953 clear_glyph_row (temp_row);
11954 it.glyph_row = temp_row;
11955 display_tool_bar_line (&it, -1);
11957 clear_glyph_row (temp_row);
11959 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11960 if (n_rows)
11961 *n_rows = it.vpos > 0 ? it.vpos : -1;
11963 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11967 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11968 0, 1, 0,
11969 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11970 If FRAME is nil or omitted, use the selected frame. */)
11971 (Lisp_Object frame)
11973 struct frame *f = decode_any_frame (frame);
11974 struct window *w;
11975 int nlines = 0;
11977 if (WINDOWP (f->tool_bar_window)
11978 && (w = XWINDOW (f->tool_bar_window),
11979 WINDOW_TOTAL_LINES (w) > 0))
11981 update_tool_bar (f, 1);
11982 if (f->n_tool_bar_items)
11984 build_desired_tool_bar_string (f);
11985 nlines = tool_bar_lines_needed (f, NULL);
11989 return make_number (nlines);
11993 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11994 height should be changed. */
11996 static int
11997 redisplay_tool_bar (struct frame *f)
11999 struct window *w;
12000 struct it it;
12001 struct glyph_row *row;
12003 #if defined (USE_GTK) || defined (HAVE_NS)
12004 if (FRAME_EXTERNAL_TOOL_BAR (f))
12005 update_frame_tool_bar (f);
12006 return 0;
12007 #endif
12009 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12010 do anything. This means you must start with tool-bar-lines
12011 non-zero to get the auto-sizing effect. Or in other words, you
12012 can turn off tool-bars by specifying tool-bar-lines zero. */
12013 if (!WINDOWP (f->tool_bar_window)
12014 || (w = XWINDOW (f->tool_bar_window),
12015 WINDOW_TOTAL_LINES (w) == 0))
12016 return 0;
12018 /* Set up an iterator for the tool-bar window. */
12019 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12020 it.first_visible_x = 0;
12021 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12022 row = it.glyph_row;
12024 /* Build a string that represents the contents of the tool-bar. */
12025 build_desired_tool_bar_string (f);
12026 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12027 /* FIXME: This should be controlled by a user option. But it
12028 doesn't make sense to have an R2L tool bar if the menu bar cannot
12029 be drawn also R2L, and making the menu bar R2L is tricky due
12030 toolkit-specific code that implements it. If an R2L tool bar is
12031 ever supported, display_tool_bar_line should also be augmented to
12032 call unproduce_glyphs like display_line and display_string
12033 do. */
12034 it.paragraph_embedding = L2R;
12036 if (f->n_tool_bar_rows == 0)
12038 int nlines;
12040 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
12041 nlines != WINDOW_TOTAL_LINES (w)))
12043 Lisp_Object frame;
12044 int old_height = WINDOW_TOTAL_LINES (w);
12046 XSETFRAME (frame, f);
12047 Fmodify_frame_parameters (frame,
12048 list1 (Fcons (Qtool_bar_lines,
12049 make_number (nlines))));
12050 if (WINDOW_TOTAL_LINES (w) != old_height)
12052 clear_glyph_matrix (w->desired_matrix);
12053 fonts_changed_p = 1;
12054 return 1;
12059 /* Display as many lines as needed to display all tool-bar items. */
12061 if (f->n_tool_bar_rows > 0)
12063 int border, rows, height, extra;
12065 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12066 border = XINT (Vtool_bar_border);
12067 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12068 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12069 else if (EQ (Vtool_bar_border, Qborder_width))
12070 border = f->border_width;
12071 else
12072 border = 0;
12073 if (border < 0)
12074 border = 0;
12076 rows = f->n_tool_bar_rows;
12077 height = max (1, (it.last_visible_y - border) / rows);
12078 extra = it.last_visible_y - border - height * rows;
12080 while (it.current_y < it.last_visible_y)
12082 int h = 0;
12083 if (extra > 0 && rows-- > 0)
12085 h = (extra + rows - 1) / rows;
12086 extra -= h;
12088 display_tool_bar_line (&it, height + h);
12091 else
12093 while (it.current_y < it.last_visible_y)
12094 display_tool_bar_line (&it, 0);
12097 /* It doesn't make much sense to try scrolling in the tool-bar
12098 window, so don't do it. */
12099 w->desired_matrix->no_scrolling_p = 1;
12100 w->must_be_updated_p = 1;
12102 if (!NILP (Vauto_resize_tool_bars))
12104 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12105 int change_height_p = 0;
12107 /* If we couldn't display everything, change the tool-bar's
12108 height if there is room for more. */
12109 if (IT_STRING_CHARPOS (it) < it.end_charpos
12110 && it.current_y < max_tool_bar_height)
12111 change_height_p = 1;
12113 row = it.glyph_row - 1;
12115 /* If there are blank lines at the end, except for a partially
12116 visible blank line at the end that is smaller than
12117 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12118 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12119 && row->height >= FRAME_LINE_HEIGHT (f))
12120 change_height_p = 1;
12122 /* If row displays tool-bar items, but is partially visible,
12123 change the tool-bar's height. */
12124 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12125 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12126 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12127 change_height_p = 1;
12129 /* Resize windows as needed by changing the `tool-bar-lines'
12130 frame parameter. */
12131 if (change_height_p)
12133 Lisp_Object frame;
12134 int old_height = WINDOW_TOTAL_LINES (w);
12135 int nrows;
12136 int nlines = tool_bar_lines_needed (f, &nrows);
12138 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12139 && !f->minimize_tool_bar_window_p)
12140 ? (nlines > old_height)
12141 : (nlines != old_height));
12142 f->minimize_tool_bar_window_p = 0;
12144 if (change_height_p)
12146 XSETFRAME (frame, f);
12147 Fmodify_frame_parameters (frame,
12148 list1 (Fcons (Qtool_bar_lines,
12149 make_number (nlines))));
12150 if (WINDOW_TOTAL_LINES (w) != old_height)
12152 clear_glyph_matrix (w->desired_matrix);
12153 f->n_tool_bar_rows = nrows;
12154 fonts_changed_p = 1;
12155 return 1;
12161 f->minimize_tool_bar_window_p = 0;
12162 return 0;
12166 /* Get information about the tool-bar item which is displayed in GLYPH
12167 on frame F. Return in *PROP_IDX the index where tool-bar item
12168 properties start in F->tool_bar_items. Value is zero if
12169 GLYPH doesn't display a tool-bar item. */
12171 static int
12172 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12174 Lisp_Object prop;
12175 int success_p;
12176 int charpos;
12178 /* This function can be called asynchronously, which means we must
12179 exclude any possibility that Fget_text_property signals an
12180 error. */
12181 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12182 charpos = max (0, charpos);
12184 /* Get the text property `menu-item' at pos. The value of that
12185 property is the start index of this item's properties in
12186 F->tool_bar_items. */
12187 prop = Fget_text_property (make_number (charpos),
12188 Qmenu_item, f->current_tool_bar_string);
12189 if (INTEGERP (prop))
12191 *prop_idx = XINT (prop);
12192 success_p = 1;
12194 else
12195 success_p = 0;
12197 return success_p;
12201 /* Get information about the tool-bar item at position X/Y on frame F.
12202 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12203 the current matrix of the tool-bar window of F, or NULL if not
12204 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12205 item in F->tool_bar_items. Value is
12207 -1 if X/Y is not on a tool-bar item
12208 0 if X/Y is on the same item that was highlighted before.
12209 1 otherwise. */
12211 static int
12212 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12213 int *hpos, int *vpos, int *prop_idx)
12215 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12216 struct window *w = XWINDOW (f->tool_bar_window);
12217 int area;
12219 /* Find the glyph under X/Y. */
12220 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12221 if (*glyph == NULL)
12222 return -1;
12224 /* Get the start of this tool-bar item's properties in
12225 f->tool_bar_items. */
12226 if (!tool_bar_item_info (f, *glyph, prop_idx))
12227 return -1;
12229 /* Is mouse on the highlighted item? */
12230 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12231 && *vpos >= hlinfo->mouse_face_beg_row
12232 && *vpos <= hlinfo->mouse_face_end_row
12233 && (*vpos > hlinfo->mouse_face_beg_row
12234 || *hpos >= hlinfo->mouse_face_beg_col)
12235 && (*vpos < hlinfo->mouse_face_end_row
12236 || *hpos < hlinfo->mouse_face_end_col
12237 || hlinfo->mouse_face_past_end))
12238 return 0;
12240 return 1;
12244 /* EXPORT:
12245 Handle mouse button event on the tool-bar of frame F, at
12246 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12247 0 for button release. MODIFIERS is event modifiers for button
12248 release. */
12250 void
12251 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12252 int modifiers)
12254 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12255 struct window *w = XWINDOW (f->tool_bar_window);
12256 int hpos, vpos, prop_idx;
12257 struct glyph *glyph;
12258 Lisp_Object enabled_p;
12259 int ts;
12261 /* If not on the highlighted tool-bar item, and mouse-highlight is
12262 non-nil, return. This is so we generate the tool-bar button
12263 click only when the mouse button is released on the same item as
12264 where it was pressed. However, when mouse-highlight is disabled,
12265 generate the click when the button is released regardless of the
12266 highlight, since tool-bar items are not highlighted in that
12267 case. */
12268 frame_to_window_pixel_xy (w, &x, &y);
12269 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12270 if (ts == -1
12271 || (ts != 0 && !NILP (Vmouse_highlight)))
12272 return;
12274 /* When mouse-highlight is off, generate the click for the item
12275 where the button was pressed, disregarding where it was
12276 released. */
12277 if (NILP (Vmouse_highlight) && !down_p)
12278 prop_idx = last_tool_bar_item;
12280 /* If item is disabled, do nothing. */
12281 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12282 if (NILP (enabled_p))
12283 return;
12285 if (down_p)
12287 /* Show item in pressed state. */
12288 if (!NILP (Vmouse_highlight))
12289 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12290 last_tool_bar_item = prop_idx;
12292 else
12294 Lisp_Object key, frame;
12295 struct input_event event;
12296 EVENT_INIT (event);
12298 /* Show item in released state. */
12299 if (!NILP (Vmouse_highlight))
12300 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12302 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12304 XSETFRAME (frame, f);
12305 event.kind = TOOL_BAR_EVENT;
12306 event.frame_or_window = frame;
12307 event.arg = frame;
12308 kbd_buffer_store_event (&event);
12310 event.kind = TOOL_BAR_EVENT;
12311 event.frame_or_window = frame;
12312 event.arg = key;
12313 event.modifiers = modifiers;
12314 kbd_buffer_store_event (&event);
12315 last_tool_bar_item = -1;
12320 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12321 tool-bar window-relative coordinates X/Y. Called from
12322 note_mouse_highlight. */
12324 static void
12325 note_tool_bar_highlight (struct frame *f, int x, int y)
12327 Lisp_Object window = f->tool_bar_window;
12328 struct window *w = XWINDOW (window);
12329 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12330 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12331 int hpos, vpos;
12332 struct glyph *glyph;
12333 struct glyph_row *row;
12334 int i;
12335 Lisp_Object enabled_p;
12336 int prop_idx;
12337 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12338 int mouse_down_p, rc;
12340 /* Function note_mouse_highlight is called with negative X/Y
12341 values when mouse moves outside of the frame. */
12342 if (x <= 0 || y <= 0)
12344 clear_mouse_face (hlinfo);
12345 return;
12348 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12349 if (rc < 0)
12351 /* Not on tool-bar item. */
12352 clear_mouse_face (hlinfo);
12353 return;
12355 else if (rc == 0)
12356 /* On same tool-bar item as before. */
12357 goto set_help_echo;
12359 clear_mouse_face (hlinfo);
12361 /* Mouse is down, but on different tool-bar item? */
12362 mouse_down_p = (dpyinfo->grabbed
12363 && f == last_mouse_frame
12364 && FRAME_LIVE_P (f));
12365 if (mouse_down_p
12366 && last_tool_bar_item != prop_idx)
12367 return;
12369 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12371 /* If tool-bar item is not enabled, don't highlight it. */
12372 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12373 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12375 /* Compute the x-position of the glyph. In front and past the
12376 image is a space. We include this in the highlighted area. */
12377 row = MATRIX_ROW (w->current_matrix, vpos);
12378 for (i = x = 0; i < hpos; ++i)
12379 x += row->glyphs[TEXT_AREA][i].pixel_width;
12381 /* Record this as the current active region. */
12382 hlinfo->mouse_face_beg_col = hpos;
12383 hlinfo->mouse_face_beg_row = vpos;
12384 hlinfo->mouse_face_beg_x = x;
12385 hlinfo->mouse_face_beg_y = row->y;
12386 hlinfo->mouse_face_past_end = 0;
12388 hlinfo->mouse_face_end_col = hpos + 1;
12389 hlinfo->mouse_face_end_row = vpos;
12390 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12391 hlinfo->mouse_face_end_y = row->y;
12392 hlinfo->mouse_face_window = window;
12393 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12395 /* Display it as active. */
12396 show_mouse_face (hlinfo, draw);
12399 set_help_echo:
12401 /* Set help_echo_string to a help string to display for this tool-bar item.
12402 XTread_socket does the rest. */
12403 help_echo_object = help_echo_window = Qnil;
12404 help_echo_pos = -1;
12405 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12406 if (NILP (help_echo_string))
12407 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12410 #endif /* HAVE_WINDOW_SYSTEM */
12414 /************************************************************************
12415 Horizontal scrolling
12416 ************************************************************************/
12418 static int hscroll_window_tree (Lisp_Object);
12419 static int hscroll_windows (Lisp_Object);
12421 /* For all leaf windows in the window tree rooted at WINDOW, set their
12422 hscroll value so that PT is (i) visible in the window, and (ii) so
12423 that it is not within a certain margin at the window's left and
12424 right border. Value is non-zero if any window's hscroll has been
12425 changed. */
12427 static int
12428 hscroll_window_tree (Lisp_Object window)
12430 int hscrolled_p = 0;
12431 int hscroll_relative_p = FLOATP (Vhscroll_step);
12432 int hscroll_step_abs = 0;
12433 double hscroll_step_rel = 0;
12435 if (hscroll_relative_p)
12437 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12438 if (hscroll_step_rel < 0)
12440 hscroll_relative_p = 0;
12441 hscroll_step_abs = 0;
12444 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12446 hscroll_step_abs = XINT (Vhscroll_step);
12447 if (hscroll_step_abs < 0)
12448 hscroll_step_abs = 0;
12450 else
12451 hscroll_step_abs = 0;
12453 while (WINDOWP (window))
12455 struct window *w = XWINDOW (window);
12457 if (WINDOWP (w->contents))
12458 hscrolled_p |= hscroll_window_tree (w->contents);
12459 else if (w->cursor.vpos >= 0)
12461 int h_margin;
12462 int text_area_width;
12463 struct glyph_row *current_cursor_row
12464 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12465 struct glyph_row *desired_cursor_row
12466 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12467 struct glyph_row *cursor_row
12468 = (desired_cursor_row->enabled_p
12469 ? desired_cursor_row
12470 : current_cursor_row);
12471 int row_r2l_p = cursor_row->reversed_p;
12473 text_area_width = window_box_width (w, TEXT_AREA);
12475 /* Scroll when cursor is inside this scroll margin. */
12476 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12478 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12479 /* For left-to-right rows, hscroll when cursor is either
12480 (i) inside the right hscroll margin, or (ii) if it is
12481 inside the left margin and the window is already
12482 hscrolled. */
12483 && ((!row_r2l_p
12484 && ((w->hscroll
12485 && w->cursor.x <= h_margin)
12486 || (cursor_row->enabled_p
12487 && cursor_row->truncated_on_right_p
12488 && (w->cursor.x >= text_area_width - h_margin))))
12489 /* For right-to-left rows, the logic is similar,
12490 except that rules for scrolling to left and right
12491 are reversed. E.g., if cursor.x <= h_margin, we
12492 need to hscroll "to the right" unconditionally,
12493 and that will scroll the screen to the left so as
12494 to reveal the next portion of the row. */
12495 || (row_r2l_p
12496 && ((cursor_row->enabled_p
12497 /* FIXME: It is confusing to set the
12498 truncated_on_right_p flag when R2L rows
12499 are actually truncated on the left. */
12500 && cursor_row->truncated_on_right_p
12501 && w->cursor.x <= h_margin)
12502 || (w->hscroll
12503 && (w->cursor.x >= text_area_width - h_margin))))))
12505 struct it it;
12506 ptrdiff_t hscroll;
12507 struct buffer *saved_current_buffer;
12508 ptrdiff_t pt;
12509 int wanted_x;
12511 /* Find point in a display of infinite width. */
12512 saved_current_buffer = current_buffer;
12513 current_buffer = XBUFFER (w->contents);
12515 if (w == XWINDOW (selected_window))
12516 pt = PT;
12517 else
12518 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12520 /* Move iterator to pt starting at cursor_row->start in
12521 a line with infinite width. */
12522 init_to_row_start (&it, w, cursor_row);
12523 it.last_visible_x = INFINITY;
12524 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12525 current_buffer = saved_current_buffer;
12527 /* Position cursor in window. */
12528 if (!hscroll_relative_p && hscroll_step_abs == 0)
12529 hscroll = max (0, (it.current_x
12530 - (ITERATOR_AT_END_OF_LINE_P (&it)
12531 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12532 : (text_area_width / 2))))
12533 / FRAME_COLUMN_WIDTH (it.f);
12534 else if ((!row_r2l_p
12535 && w->cursor.x >= text_area_width - h_margin)
12536 || (row_r2l_p && w->cursor.x <= h_margin))
12538 if (hscroll_relative_p)
12539 wanted_x = text_area_width * (1 - hscroll_step_rel)
12540 - h_margin;
12541 else
12542 wanted_x = text_area_width
12543 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12544 - h_margin;
12545 hscroll
12546 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12548 else
12550 if (hscroll_relative_p)
12551 wanted_x = text_area_width * hscroll_step_rel
12552 + h_margin;
12553 else
12554 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12555 + h_margin;
12556 hscroll
12557 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12559 hscroll = max (hscroll, w->min_hscroll);
12561 /* Don't prevent redisplay optimizations if hscroll
12562 hasn't changed, as it will unnecessarily slow down
12563 redisplay. */
12564 if (w->hscroll != hscroll)
12566 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12567 w->hscroll = hscroll;
12568 hscrolled_p = 1;
12573 window = w->next;
12576 /* Value is non-zero if hscroll of any leaf window has been changed. */
12577 return hscrolled_p;
12581 /* Set hscroll so that cursor is visible and not inside horizontal
12582 scroll margins for all windows in the tree rooted at WINDOW. See
12583 also hscroll_window_tree above. Value is non-zero if any window's
12584 hscroll has been changed. If it has, desired matrices on the frame
12585 of WINDOW are cleared. */
12587 static int
12588 hscroll_windows (Lisp_Object window)
12590 int hscrolled_p = hscroll_window_tree (window);
12591 if (hscrolled_p)
12592 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12593 return hscrolled_p;
12598 /************************************************************************
12599 Redisplay
12600 ************************************************************************/
12602 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12603 to a non-zero value. This is sometimes handy to have in a debugger
12604 session. */
12606 #ifdef GLYPH_DEBUG
12608 /* First and last unchanged row for try_window_id. */
12610 static int debug_first_unchanged_at_end_vpos;
12611 static int debug_last_unchanged_at_beg_vpos;
12613 /* Delta vpos and y. */
12615 static int debug_dvpos, debug_dy;
12617 /* Delta in characters and bytes for try_window_id. */
12619 static ptrdiff_t debug_delta, debug_delta_bytes;
12621 /* Values of window_end_pos and window_end_vpos at the end of
12622 try_window_id. */
12624 static ptrdiff_t debug_end_vpos;
12626 /* Append a string to W->desired_matrix->method. FMT is a printf
12627 format string. If trace_redisplay_p is non-zero also printf the
12628 resulting string to stderr. */
12630 static void debug_method_add (struct window *, char const *, ...)
12631 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12633 static void
12634 debug_method_add (struct window *w, char const *fmt, ...)
12636 void *ptr = w;
12637 char *method = w->desired_matrix->method;
12638 int len = strlen (method);
12639 int size = sizeof w->desired_matrix->method;
12640 int remaining = size - len - 1;
12641 va_list ap;
12643 if (len && remaining)
12645 method[len] = '|';
12646 --remaining, ++len;
12649 va_start (ap, fmt);
12650 vsnprintf (method + len, remaining + 1, fmt, ap);
12651 va_end (ap);
12653 if (trace_redisplay_p)
12654 fprintf (stderr, "%p (%s): %s\n",
12655 ptr,
12656 ((BUFFERP (w->contents)
12657 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12658 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12659 : "no buffer"),
12660 method + len);
12663 #endif /* GLYPH_DEBUG */
12666 /* Value is non-zero if all changes in window W, which displays
12667 current_buffer, are in the text between START and END. START is a
12668 buffer position, END is given as a distance from Z. Used in
12669 redisplay_internal for display optimization. */
12671 static int
12672 text_outside_line_unchanged_p (struct window *w,
12673 ptrdiff_t start, ptrdiff_t end)
12675 int unchanged_p = 1;
12677 /* If text or overlays have changed, see where. */
12678 if (window_outdated (w))
12680 /* Gap in the line? */
12681 if (GPT < start || Z - GPT < end)
12682 unchanged_p = 0;
12684 /* Changes start in front of the line, or end after it? */
12685 if (unchanged_p
12686 && (BEG_UNCHANGED < start - 1
12687 || END_UNCHANGED < end))
12688 unchanged_p = 0;
12690 /* If selective display, can't optimize if changes start at the
12691 beginning of the line. */
12692 if (unchanged_p
12693 && INTEGERP (BVAR (current_buffer, selective_display))
12694 && XINT (BVAR (current_buffer, selective_display)) > 0
12695 && (BEG_UNCHANGED < start || GPT <= start))
12696 unchanged_p = 0;
12698 /* If there are overlays at the start or end of the line, these
12699 may have overlay strings with newlines in them. A change at
12700 START, for instance, may actually concern the display of such
12701 overlay strings as well, and they are displayed on different
12702 lines. So, quickly rule out this case. (For the future, it
12703 might be desirable to implement something more telling than
12704 just BEG/END_UNCHANGED.) */
12705 if (unchanged_p)
12707 if (BEG + BEG_UNCHANGED == start
12708 && overlay_touches_p (start))
12709 unchanged_p = 0;
12710 if (END_UNCHANGED == end
12711 && overlay_touches_p (Z - end))
12712 unchanged_p = 0;
12715 /* Under bidi reordering, adding or deleting a character in the
12716 beginning of a paragraph, before the first strong directional
12717 character, can change the base direction of the paragraph (unless
12718 the buffer specifies a fixed paragraph direction), which will
12719 require to redisplay the whole paragraph. It might be worthwhile
12720 to find the paragraph limits and widen the range of redisplayed
12721 lines to that, but for now just give up this optimization. */
12722 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12723 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12724 unchanged_p = 0;
12727 return unchanged_p;
12731 /* Do a frame update, taking possible shortcuts into account. This is
12732 the main external entry point for redisplay.
12734 If the last redisplay displayed an echo area message and that message
12735 is no longer requested, we clear the echo area or bring back the
12736 mini-buffer if that is in use. */
12738 void
12739 redisplay (void)
12741 redisplay_internal ();
12745 static Lisp_Object
12746 overlay_arrow_string_or_property (Lisp_Object var)
12748 Lisp_Object val;
12750 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12751 return val;
12753 return Voverlay_arrow_string;
12756 /* Return 1 if there are any overlay-arrows in current_buffer. */
12757 static int
12758 overlay_arrow_in_current_buffer_p (void)
12760 Lisp_Object vlist;
12762 for (vlist = Voverlay_arrow_variable_list;
12763 CONSP (vlist);
12764 vlist = XCDR (vlist))
12766 Lisp_Object var = XCAR (vlist);
12767 Lisp_Object val;
12769 if (!SYMBOLP (var))
12770 continue;
12771 val = find_symbol_value (var);
12772 if (MARKERP (val)
12773 && current_buffer == XMARKER (val)->buffer)
12774 return 1;
12776 return 0;
12780 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12781 has changed. */
12783 static int
12784 overlay_arrows_changed_p (void)
12786 Lisp_Object vlist;
12788 for (vlist = Voverlay_arrow_variable_list;
12789 CONSP (vlist);
12790 vlist = XCDR (vlist))
12792 Lisp_Object var = XCAR (vlist);
12793 Lisp_Object val, pstr;
12795 if (!SYMBOLP (var))
12796 continue;
12797 val = find_symbol_value (var);
12798 if (!MARKERP (val))
12799 continue;
12800 if (! EQ (COERCE_MARKER (val),
12801 Fget (var, Qlast_arrow_position))
12802 || ! (pstr = overlay_arrow_string_or_property (var),
12803 EQ (pstr, Fget (var, Qlast_arrow_string))))
12804 return 1;
12806 return 0;
12809 /* Mark overlay arrows to be updated on next redisplay. */
12811 static void
12812 update_overlay_arrows (int up_to_date)
12814 Lisp_Object vlist;
12816 for (vlist = Voverlay_arrow_variable_list;
12817 CONSP (vlist);
12818 vlist = XCDR (vlist))
12820 Lisp_Object var = XCAR (vlist);
12822 if (!SYMBOLP (var))
12823 continue;
12825 if (up_to_date > 0)
12827 Lisp_Object val = find_symbol_value (var);
12828 Fput (var, Qlast_arrow_position,
12829 COERCE_MARKER (val));
12830 Fput (var, Qlast_arrow_string,
12831 overlay_arrow_string_or_property (var));
12833 else if (up_to_date < 0
12834 || !NILP (Fget (var, Qlast_arrow_position)))
12836 Fput (var, Qlast_arrow_position, Qt);
12837 Fput (var, Qlast_arrow_string, Qt);
12843 /* Return overlay arrow string to display at row.
12844 Return integer (bitmap number) for arrow bitmap in left fringe.
12845 Return nil if no overlay arrow. */
12847 static Lisp_Object
12848 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12850 Lisp_Object vlist;
12852 for (vlist = Voverlay_arrow_variable_list;
12853 CONSP (vlist);
12854 vlist = XCDR (vlist))
12856 Lisp_Object var = XCAR (vlist);
12857 Lisp_Object val;
12859 if (!SYMBOLP (var))
12860 continue;
12862 val = find_symbol_value (var);
12864 if (MARKERP (val)
12865 && current_buffer == XMARKER (val)->buffer
12866 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12868 if (FRAME_WINDOW_P (it->f)
12869 /* FIXME: if ROW->reversed_p is set, this should test
12870 the right fringe, not the left one. */
12871 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12873 #ifdef HAVE_WINDOW_SYSTEM
12874 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12876 int fringe_bitmap;
12877 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12878 return make_number (fringe_bitmap);
12880 #endif
12881 return make_number (-1); /* Use default arrow bitmap. */
12883 return overlay_arrow_string_or_property (var);
12887 return Qnil;
12890 /* Return 1 if point moved out of or into a composition. Otherwise
12891 return 0. PREV_BUF and PREV_PT are the last point buffer and
12892 position. BUF and PT are the current point buffer and position. */
12894 static int
12895 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12896 struct buffer *buf, ptrdiff_t pt)
12898 ptrdiff_t start, end;
12899 Lisp_Object prop;
12900 Lisp_Object buffer;
12902 XSETBUFFER (buffer, buf);
12903 /* Check a composition at the last point if point moved within the
12904 same buffer. */
12905 if (prev_buf == buf)
12907 if (prev_pt == pt)
12908 /* Point didn't move. */
12909 return 0;
12911 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12912 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12913 && composition_valid_p (start, end, prop)
12914 && start < prev_pt && end > prev_pt)
12915 /* The last point was within the composition. Return 1 iff
12916 point moved out of the composition. */
12917 return (pt <= start || pt >= end);
12920 /* Check a composition at the current point. */
12921 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12922 && find_composition (pt, -1, &start, &end, &prop, buffer)
12923 && composition_valid_p (start, end, prop)
12924 && start < pt && end > pt);
12927 /* Reconsider the clip changes of buffer which is displayed in W. */
12929 static void
12930 reconsider_clip_changes (struct window *w)
12932 struct buffer *b = XBUFFER (w->contents);
12934 if (b->clip_changed
12935 && w->window_end_valid
12936 && w->current_matrix->buffer == b
12937 && w->current_matrix->zv == BUF_ZV (b)
12938 && w->current_matrix->begv == BUF_BEGV (b))
12939 b->clip_changed = 0;
12941 /* If display wasn't paused, and W is not a tool bar window, see if
12942 point has been moved into or out of a composition. In that case,
12943 we set b->clip_changed to 1 to force updating the screen. If
12944 b->clip_changed has already been set to 1, we can skip this
12945 check. */
12946 if (!b->clip_changed && w->window_end_valid)
12948 ptrdiff_t pt = (w == XWINDOW (selected_window)
12949 ? PT : marker_position (w->pointm));
12951 if ((w->current_matrix->buffer != b || pt != w->last_point)
12952 && check_point_in_composition (w->current_matrix->buffer,
12953 w->last_point, b, pt))
12954 b->clip_changed = 1;
12958 #define STOP_POLLING \
12959 do { if (! polling_stopped_here) stop_polling (); \
12960 polling_stopped_here = 1; } while (0)
12962 #define RESUME_POLLING \
12963 do { if (polling_stopped_here) start_polling (); \
12964 polling_stopped_here = 0; } while (0)
12967 /* Perhaps in the future avoid recentering windows if it
12968 is not necessary; currently that causes some problems. */
12970 static void
12971 redisplay_internal (void)
12973 struct window *w = XWINDOW (selected_window);
12974 struct window *sw;
12975 struct frame *fr;
12976 int pending;
12977 bool must_finish = 0, match_p;
12978 struct text_pos tlbufpos, tlendpos;
12979 int number_of_visible_frames;
12980 ptrdiff_t count;
12981 struct frame *sf;
12982 int polling_stopped_here = 0;
12983 Lisp_Object tail, frame;
12985 /* Non-zero means redisplay has to consider all windows on all
12986 frames. Zero means, only selected_window is considered. */
12987 int consider_all_windows_p;
12989 /* Non-zero means redisplay has to redisplay the miniwindow. */
12990 int update_miniwindow_p = 0;
12992 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12994 /* No redisplay if running in batch mode or frame is not yet fully
12995 initialized, or redisplay is explicitly turned off by setting
12996 Vinhibit_redisplay. */
12997 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12998 || !NILP (Vinhibit_redisplay))
12999 return;
13001 /* Don't examine these until after testing Vinhibit_redisplay.
13002 When Emacs is shutting down, perhaps because its connection to
13003 X has dropped, we should not look at them at all. */
13004 fr = XFRAME (w->frame);
13005 sf = SELECTED_FRAME ();
13007 if (!fr->glyphs_initialized_p)
13008 return;
13010 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13011 if (popup_activated ())
13012 return;
13013 #endif
13015 /* I don't think this happens but let's be paranoid. */
13016 if (redisplaying_p)
13017 return;
13019 /* Record a function that clears redisplaying_p
13020 when we leave this function. */
13021 count = SPECPDL_INDEX ();
13022 record_unwind_protect_void (unwind_redisplay);
13023 redisplaying_p = 1;
13024 specbind (Qinhibit_free_realized_faces, Qnil);
13026 /* Record this function, so it appears on the profiler's backtraces. */
13027 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13029 FOR_EACH_FRAME (tail, frame)
13030 XFRAME (frame)->already_hscrolled_p = 0;
13032 retry:
13033 /* Remember the currently selected window. */
13034 sw = w;
13036 pending = 0;
13037 last_escape_glyph_frame = NULL;
13038 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13039 last_glyphless_glyph_frame = NULL;
13040 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13042 /* If new fonts have been loaded that make a glyph matrix adjustment
13043 necessary, do it. */
13044 if (fonts_changed_p)
13046 adjust_glyphs (NULL);
13047 ++windows_or_buffers_changed;
13048 fonts_changed_p = 0;
13051 /* If face_change_count is non-zero, init_iterator will free all
13052 realized faces, which includes the faces referenced from current
13053 matrices. So, we can't reuse current matrices in this case. */
13054 if (face_change_count)
13055 ++windows_or_buffers_changed;
13057 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13058 && FRAME_TTY (sf)->previous_frame != sf)
13060 /* Since frames on a single ASCII terminal share the same
13061 display area, displaying a different frame means redisplay
13062 the whole thing. */
13063 windows_or_buffers_changed++;
13064 SET_FRAME_GARBAGED (sf);
13065 #ifndef DOS_NT
13066 set_tty_color_mode (FRAME_TTY (sf), sf);
13067 #endif
13068 FRAME_TTY (sf)->previous_frame = sf;
13071 /* Set the visible flags for all frames. Do this before checking for
13072 resized or garbaged frames; they want to know if their frames are
13073 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13074 number_of_visible_frames = 0;
13076 FOR_EACH_FRAME (tail, frame)
13078 struct frame *f = XFRAME (frame);
13080 if (FRAME_VISIBLE_P (f))
13081 ++number_of_visible_frames;
13082 clear_desired_matrices (f);
13085 /* Notice any pending interrupt request to change frame size. */
13086 do_pending_window_change (1);
13088 /* do_pending_window_change could change the selected_window due to
13089 frame resizing which makes the selected window too small. */
13090 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13091 sw = w;
13093 /* Clear frames marked as garbaged. */
13094 clear_garbaged_frames ();
13096 /* Build menubar and tool-bar items. */
13097 if (NILP (Vmemory_full))
13098 prepare_menu_bars ();
13100 if (windows_or_buffers_changed)
13101 update_mode_lines++;
13103 reconsider_clip_changes (w);
13105 /* In most cases selected window displays current buffer. */
13106 match_p = XBUFFER (w->contents) == current_buffer;
13107 if (match_p)
13109 ptrdiff_t count1;
13111 /* Detect case that we need to write or remove a star in the mode line. */
13112 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13114 w->update_mode_line = 1;
13115 if (buffer_shared_and_changed ())
13116 update_mode_lines++;
13119 /* Avoid invocation of point motion hooks by `current_column' below. */
13120 count1 = SPECPDL_INDEX ();
13121 specbind (Qinhibit_point_motion_hooks, Qt);
13123 if (mode_line_update_needed (w))
13124 w->update_mode_line = 1;
13126 unbind_to (count1, Qnil);
13129 consider_all_windows_p = (update_mode_lines
13130 || buffer_shared_and_changed ()
13131 || cursor_type_changed);
13133 /* If specs for an arrow have changed, do thorough redisplay
13134 to ensure we remove any arrow that should no longer exist. */
13135 if (overlay_arrows_changed_p ())
13136 consider_all_windows_p = windows_or_buffers_changed = 1;
13138 /* Normally the message* functions will have already displayed and
13139 updated the echo area, but the frame may have been trashed, or
13140 the update may have been preempted, so display the echo area
13141 again here. Checking message_cleared_p captures the case that
13142 the echo area should be cleared. */
13143 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13144 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13145 || (message_cleared_p
13146 && minibuf_level == 0
13147 /* If the mini-window is currently selected, this means the
13148 echo-area doesn't show through. */
13149 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13151 int window_height_changed_p = echo_area_display (0);
13153 if (message_cleared_p)
13154 update_miniwindow_p = 1;
13156 must_finish = 1;
13158 /* If we don't display the current message, don't clear the
13159 message_cleared_p flag, because, if we did, we wouldn't clear
13160 the echo area in the next redisplay which doesn't preserve
13161 the echo area. */
13162 if (!display_last_displayed_message_p)
13163 message_cleared_p = 0;
13165 if (fonts_changed_p)
13166 goto retry;
13167 else if (window_height_changed_p)
13169 consider_all_windows_p = 1;
13170 ++update_mode_lines;
13171 ++windows_or_buffers_changed;
13173 /* If window configuration was changed, frames may have been
13174 marked garbaged. Clear them or we will experience
13175 surprises wrt scrolling. */
13176 clear_garbaged_frames ();
13179 else if (EQ (selected_window, minibuf_window)
13180 && (current_buffer->clip_changed || window_outdated (w))
13181 && resize_mini_window (w, 0))
13183 /* Resized active mini-window to fit the size of what it is
13184 showing if its contents might have changed. */
13185 must_finish = 1;
13186 /* FIXME: this causes all frames to be updated, which seems unnecessary
13187 since only the current frame needs to be considered. This function
13188 needs to be rewritten with two variables, consider_all_windows and
13189 consider_all_frames. */
13190 consider_all_windows_p = 1;
13191 ++windows_or_buffers_changed;
13192 ++update_mode_lines;
13194 /* If window configuration was changed, frames may have been
13195 marked garbaged. Clear them or we will experience
13196 surprises wrt scrolling. */
13197 clear_garbaged_frames ();
13200 /* If showing the region, and mark has changed, we must redisplay
13201 the whole window. The assignment to this_line_start_pos prevents
13202 the optimization directly below this if-statement. */
13203 if (((!NILP (Vtransient_mark_mode)
13204 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13205 != (w->region_showing > 0))
13206 || (w->region_showing
13207 && w->region_showing
13208 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13209 CHARPOS (this_line_start_pos) = 0;
13211 /* Optimize the case that only the line containing the cursor in the
13212 selected window has changed. Variables starting with this_ are
13213 set in display_line and record information about the line
13214 containing the cursor. */
13215 tlbufpos = this_line_start_pos;
13216 tlendpos = this_line_end_pos;
13217 if (!consider_all_windows_p
13218 && CHARPOS (tlbufpos) > 0
13219 && !w->update_mode_line
13220 && !current_buffer->clip_changed
13221 && !current_buffer->prevent_redisplay_optimizations_p
13222 && FRAME_VISIBLE_P (XFRAME (w->frame))
13223 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13224 /* Make sure recorded data applies to current buffer, etc. */
13225 && this_line_buffer == current_buffer
13226 && match_p
13227 && !w->force_start
13228 && !w->optional_new_start
13229 /* Point must be on the line that we have info recorded about. */
13230 && PT >= CHARPOS (tlbufpos)
13231 && PT <= Z - CHARPOS (tlendpos)
13232 /* All text outside that line, including its final newline,
13233 must be unchanged. */
13234 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13235 CHARPOS (tlendpos)))
13237 if (CHARPOS (tlbufpos) > BEGV
13238 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13239 && (CHARPOS (tlbufpos) == ZV
13240 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13241 /* Former continuation line has disappeared by becoming empty. */
13242 goto cancel;
13243 else if (window_outdated (w) || MINI_WINDOW_P (w))
13245 /* We have to handle the case of continuation around a
13246 wide-column character (see the comment in indent.c around
13247 line 1340).
13249 For instance, in the following case:
13251 -------- Insert --------
13252 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13253 J_I_ ==> J_I_ `^^' are cursors.
13254 ^^ ^^
13255 -------- --------
13257 As we have to redraw the line above, we cannot use this
13258 optimization. */
13260 struct it it;
13261 int line_height_before = this_line_pixel_height;
13263 /* Note that start_display will handle the case that the
13264 line starting at tlbufpos is a continuation line. */
13265 start_display (&it, w, tlbufpos);
13267 /* Implementation note: It this still necessary? */
13268 if (it.current_x != this_line_start_x)
13269 goto cancel;
13271 TRACE ((stderr, "trying display optimization 1\n"));
13272 w->cursor.vpos = -1;
13273 overlay_arrow_seen = 0;
13274 it.vpos = this_line_vpos;
13275 it.current_y = this_line_y;
13276 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13277 display_line (&it);
13279 /* If line contains point, is not continued,
13280 and ends at same distance from eob as before, we win. */
13281 if (w->cursor.vpos >= 0
13282 /* Line is not continued, otherwise this_line_start_pos
13283 would have been set to 0 in display_line. */
13284 && CHARPOS (this_line_start_pos)
13285 /* Line ends as before. */
13286 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13287 /* Line has same height as before. Otherwise other lines
13288 would have to be shifted up or down. */
13289 && this_line_pixel_height == line_height_before)
13291 /* If this is not the window's last line, we must adjust
13292 the charstarts of the lines below. */
13293 if (it.current_y < it.last_visible_y)
13295 struct glyph_row *row
13296 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13297 ptrdiff_t delta, delta_bytes;
13299 /* We used to distinguish between two cases here,
13300 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13301 when the line ends in a newline or the end of the
13302 buffer's accessible portion. But both cases did
13303 the same, so they were collapsed. */
13304 delta = (Z
13305 - CHARPOS (tlendpos)
13306 - MATRIX_ROW_START_CHARPOS (row));
13307 delta_bytes = (Z_BYTE
13308 - BYTEPOS (tlendpos)
13309 - MATRIX_ROW_START_BYTEPOS (row));
13311 increment_matrix_positions (w->current_matrix,
13312 this_line_vpos + 1,
13313 w->current_matrix->nrows,
13314 delta, delta_bytes);
13317 /* If this row displays text now but previously didn't,
13318 or vice versa, w->window_end_vpos may have to be
13319 adjusted. */
13320 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13322 if (w->window_end_vpos < this_line_vpos)
13323 w->window_end_vpos = this_line_vpos;
13325 else if (w->window_end_vpos == this_line_vpos
13326 && this_line_vpos > 0)
13327 w->window_end_vpos = this_line_vpos - 1;
13328 w->window_end_valid = 0;
13330 /* Update hint: No need to try to scroll in update_window. */
13331 w->desired_matrix->no_scrolling_p = 1;
13333 #ifdef GLYPH_DEBUG
13334 *w->desired_matrix->method = 0;
13335 debug_method_add (w, "optimization 1");
13336 #endif
13337 #ifdef HAVE_WINDOW_SYSTEM
13338 update_window_fringes (w, 0);
13339 #endif
13340 goto update;
13342 else
13343 goto cancel;
13345 else if (/* Cursor position hasn't changed. */
13346 PT == w->last_point
13347 /* Make sure the cursor was last displayed
13348 in this window. Otherwise we have to reposition it. */
13349 && 0 <= w->cursor.vpos
13350 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13352 if (!must_finish)
13354 do_pending_window_change (1);
13355 /* If selected_window changed, redisplay again. */
13356 if (WINDOWP (selected_window)
13357 && (w = XWINDOW (selected_window)) != sw)
13358 goto retry;
13360 /* We used to always goto end_of_redisplay here, but this
13361 isn't enough if we have a blinking cursor. */
13362 if (w->cursor_off_p == w->last_cursor_off_p)
13363 goto end_of_redisplay;
13365 goto update;
13367 /* If highlighting the region, or if the cursor is in the echo area,
13368 then we can't just move the cursor. */
13369 else if (! (!NILP (Vtransient_mark_mode)
13370 && !NILP (BVAR (current_buffer, mark_active)))
13371 && (EQ (selected_window,
13372 BVAR (current_buffer, last_selected_window))
13373 || highlight_nonselected_windows)
13374 && !w->region_showing
13375 && NILP (Vshow_trailing_whitespace)
13376 && !cursor_in_echo_area)
13378 struct it it;
13379 struct glyph_row *row;
13381 /* Skip from tlbufpos to PT and see where it is. Note that
13382 PT may be in invisible text. If so, we will end at the
13383 next visible position. */
13384 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13385 NULL, DEFAULT_FACE_ID);
13386 it.current_x = this_line_start_x;
13387 it.current_y = this_line_y;
13388 it.vpos = this_line_vpos;
13390 /* The call to move_it_to stops in front of PT, but
13391 moves over before-strings. */
13392 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13394 if (it.vpos == this_line_vpos
13395 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13396 row->enabled_p))
13398 eassert (this_line_vpos == it.vpos);
13399 eassert (this_line_y == it.current_y);
13400 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13401 #ifdef GLYPH_DEBUG
13402 *w->desired_matrix->method = 0;
13403 debug_method_add (w, "optimization 3");
13404 #endif
13405 goto update;
13407 else
13408 goto cancel;
13411 cancel:
13412 /* Text changed drastically or point moved off of line. */
13413 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13416 CHARPOS (this_line_start_pos) = 0;
13417 consider_all_windows_p |= buffer_shared_and_changed ();
13418 ++clear_face_cache_count;
13419 #ifdef HAVE_WINDOW_SYSTEM
13420 ++clear_image_cache_count;
13421 #endif
13423 /* Build desired matrices, and update the display. If
13424 consider_all_windows_p is non-zero, do it for all windows on all
13425 frames. Otherwise do it for selected_window, only. */
13427 if (consider_all_windows_p)
13429 FOR_EACH_FRAME (tail, frame)
13430 XFRAME (frame)->updated_p = 0;
13432 FOR_EACH_FRAME (tail, frame)
13434 struct frame *f = XFRAME (frame);
13436 /* We don't have to do anything for unselected terminal
13437 frames. */
13438 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13439 && !EQ (FRAME_TTY (f)->top_frame, frame))
13440 continue;
13442 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13444 /* Mark all the scroll bars to be removed; we'll redeem
13445 the ones we want when we redisplay their windows. */
13446 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13447 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13449 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13450 redisplay_windows (FRAME_ROOT_WINDOW (f));
13452 /* The X error handler may have deleted that frame. */
13453 if (!FRAME_LIVE_P (f))
13454 continue;
13456 /* Any scroll bars which redisplay_windows should have
13457 nuked should now go away. */
13458 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13459 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13461 /* If fonts changed, display again. */
13462 /* ??? rms: I suspect it is a mistake to jump all the way
13463 back to retry here. It should just retry this frame. */
13464 if (fonts_changed_p)
13465 goto retry;
13467 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13469 /* See if we have to hscroll. */
13470 if (!f->already_hscrolled_p)
13472 f->already_hscrolled_p = 1;
13473 if (hscroll_windows (f->root_window))
13474 goto retry;
13477 /* Prevent various kinds of signals during display
13478 update. stdio is not robust about handling
13479 signals, which can cause an apparent I/O
13480 error. */
13481 if (interrupt_input)
13482 unrequest_sigio ();
13483 STOP_POLLING;
13485 /* Update the display. */
13486 set_window_update_flags (XWINDOW (f->root_window), 1);
13487 pending |= update_frame (f, 0, 0);
13488 f->updated_p = 1;
13493 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13495 if (!pending)
13497 /* Do the mark_window_display_accurate after all windows have
13498 been redisplayed because this call resets flags in buffers
13499 which are needed for proper redisplay. */
13500 FOR_EACH_FRAME (tail, frame)
13502 struct frame *f = XFRAME (frame);
13503 if (f->updated_p)
13505 mark_window_display_accurate (f->root_window, 1);
13506 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13507 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13512 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13514 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13515 struct frame *mini_frame;
13517 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13518 /* Use list_of_error, not Qerror, so that
13519 we catch only errors and don't run the debugger. */
13520 internal_condition_case_1 (redisplay_window_1, selected_window,
13521 list_of_error,
13522 redisplay_window_error);
13523 if (update_miniwindow_p)
13524 internal_condition_case_1 (redisplay_window_1, mini_window,
13525 list_of_error,
13526 redisplay_window_error);
13528 /* Compare desired and current matrices, perform output. */
13530 update:
13531 /* If fonts changed, display again. */
13532 if (fonts_changed_p)
13533 goto retry;
13535 /* Prevent various kinds of signals during display update.
13536 stdio is not robust about handling signals,
13537 which can cause an apparent I/O error. */
13538 if (interrupt_input)
13539 unrequest_sigio ();
13540 STOP_POLLING;
13542 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13544 if (hscroll_windows (selected_window))
13545 goto retry;
13547 XWINDOW (selected_window)->must_be_updated_p = 1;
13548 pending = update_frame (sf, 0, 0);
13551 /* We may have called echo_area_display at the top of this
13552 function. If the echo area is on another frame, that may
13553 have put text on a frame other than the selected one, so the
13554 above call to update_frame would not have caught it. Catch
13555 it here. */
13556 mini_window = FRAME_MINIBUF_WINDOW (sf);
13557 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13559 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13561 XWINDOW (mini_window)->must_be_updated_p = 1;
13562 pending |= update_frame (mini_frame, 0, 0);
13563 if (!pending && hscroll_windows (mini_window))
13564 goto retry;
13568 /* If display was paused because of pending input, make sure we do a
13569 thorough update the next time. */
13570 if (pending)
13572 /* Prevent the optimization at the beginning of
13573 redisplay_internal that tries a single-line update of the
13574 line containing the cursor in the selected window. */
13575 CHARPOS (this_line_start_pos) = 0;
13577 /* Let the overlay arrow be updated the next time. */
13578 update_overlay_arrows (0);
13580 /* If we pause after scrolling, some rows in the current
13581 matrices of some windows are not valid. */
13582 if (!WINDOW_FULL_WIDTH_P (w)
13583 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13584 update_mode_lines = 1;
13586 else
13588 if (!consider_all_windows_p)
13590 /* This has already been done above if
13591 consider_all_windows_p is set. */
13592 mark_window_display_accurate_1 (w, 1);
13594 /* Say overlay arrows are up to date. */
13595 update_overlay_arrows (1);
13597 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13598 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13601 update_mode_lines = 0;
13602 windows_or_buffers_changed = 0;
13603 cursor_type_changed = 0;
13606 /* Start SIGIO interrupts coming again. Having them off during the
13607 code above makes it less likely one will discard output, but not
13608 impossible, since there might be stuff in the system buffer here.
13609 But it is much hairier to try to do anything about that. */
13610 if (interrupt_input)
13611 request_sigio ();
13612 RESUME_POLLING;
13614 /* If a frame has become visible which was not before, redisplay
13615 again, so that we display it. Expose events for such a frame
13616 (which it gets when becoming visible) don't call the parts of
13617 redisplay constructing glyphs, so simply exposing a frame won't
13618 display anything in this case. So, we have to display these
13619 frames here explicitly. */
13620 if (!pending)
13622 int new_count = 0;
13624 FOR_EACH_FRAME (tail, frame)
13626 int this_is_visible = 0;
13628 if (XFRAME (frame)->visible)
13629 this_is_visible = 1;
13631 if (this_is_visible)
13632 new_count++;
13635 if (new_count != number_of_visible_frames)
13636 windows_or_buffers_changed++;
13639 /* Change frame size now if a change is pending. */
13640 do_pending_window_change (1);
13642 /* If we just did a pending size change, or have additional
13643 visible frames, or selected_window changed, redisplay again. */
13644 if ((windows_or_buffers_changed && !pending)
13645 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13646 goto retry;
13648 /* Clear the face and image caches.
13650 We used to do this only if consider_all_windows_p. But the cache
13651 needs to be cleared if a timer creates images in the current
13652 buffer (e.g. the test case in Bug#6230). */
13654 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13656 clear_face_cache (0);
13657 clear_face_cache_count = 0;
13660 #ifdef HAVE_WINDOW_SYSTEM
13661 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13663 clear_image_caches (Qnil);
13664 clear_image_cache_count = 0;
13666 #endif /* HAVE_WINDOW_SYSTEM */
13668 end_of_redisplay:
13669 unbind_to (count, Qnil);
13670 RESUME_POLLING;
13674 /* Redisplay, but leave alone any recent echo area message unless
13675 another message has been requested in its place.
13677 This is useful in situations where you need to redisplay but no
13678 user action has occurred, making it inappropriate for the message
13679 area to be cleared. See tracking_off and
13680 wait_reading_process_output for examples of these situations.
13682 FROM_WHERE is an integer saying from where this function was
13683 called. This is useful for debugging. */
13685 void
13686 redisplay_preserve_echo_area (int from_where)
13688 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13690 if (!NILP (echo_area_buffer[1]))
13692 /* We have a previously displayed message, but no current
13693 message. Redisplay the previous message. */
13694 display_last_displayed_message_p = 1;
13695 redisplay_internal ();
13696 display_last_displayed_message_p = 0;
13698 else
13699 redisplay_internal ();
13701 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13702 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13703 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13707 /* Function registered with record_unwind_protect in redisplay_internal. */
13709 static void
13710 unwind_redisplay (void)
13712 redisplaying_p = 0;
13716 /* Mark the display of leaf window W as accurate or inaccurate.
13717 If ACCURATE_P is non-zero mark display of W as accurate. If
13718 ACCURATE_P is zero, arrange for W to be redisplayed the next
13719 time redisplay_internal is called. */
13721 static void
13722 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13724 struct buffer *b = XBUFFER (w->contents);
13726 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13727 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13728 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13730 if (accurate_p)
13732 b->clip_changed = 0;
13733 b->prevent_redisplay_optimizations_p = 0;
13735 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13736 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13737 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13738 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13740 w->current_matrix->buffer = b;
13741 w->current_matrix->begv = BUF_BEGV (b);
13742 w->current_matrix->zv = BUF_ZV (b);
13744 w->last_cursor = w->cursor;
13745 w->last_cursor_off_p = w->cursor_off_p;
13747 if (w == XWINDOW (selected_window))
13748 w->last_point = BUF_PT (b);
13749 else
13750 w->last_point = marker_position (w->pointm);
13752 w->window_end_valid = 1;
13753 w->update_mode_line = 0;
13758 /* Mark the display of windows in the window tree rooted at WINDOW as
13759 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13760 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13761 be redisplayed the next time redisplay_internal is called. */
13763 void
13764 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13766 struct window *w;
13768 for (; !NILP (window); window = w->next)
13770 w = XWINDOW (window);
13771 if (WINDOWP (w->contents))
13772 mark_window_display_accurate (w->contents, accurate_p);
13773 else
13774 mark_window_display_accurate_1 (w, accurate_p);
13777 if (accurate_p)
13778 update_overlay_arrows (1);
13779 else
13780 /* Force a thorough redisplay the next time by setting
13781 last_arrow_position and last_arrow_string to t, which is
13782 unequal to any useful value of Voverlay_arrow_... */
13783 update_overlay_arrows (-1);
13787 /* Return value in display table DP (Lisp_Char_Table *) for character
13788 C. Since a display table doesn't have any parent, we don't have to
13789 follow parent. Do not call this function directly but use the
13790 macro DISP_CHAR_VECTOR. */
13792 Lisp_Object
13793 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13795 Lisp_Object val;
13797 if (ASCII_CHAR_P (c))
13799 val = dp->ascii;
13800 if (SUB_CHAR_TABLE_P (val))
13801 val = XSUB_CHAR_TABLE (val)->contents[c];
13803 else
13805 Lisp_Object table;
13807 XSETCHAR_TABLE (table, dp);
13808 val = char_table_ref (table, c);
13810 if (NILP (val))
13811 val = dp->defalt;
13812 return val;
13817 /***********************************************************************
13818 Window Redisplay
13819 ***********************************************************************/
13821 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13823 static void
13824 redisplay_windows (Lisp_Object window)
13826 while (!NILP (window))
13828 struct window *w = XWINDOW (window);
13830 if (WINDOWP (w->contents))
13831 redisplay_windows (w->contents);
13832 else if (BUFFERP (w->contents))
13834 displayed_buffer = XBUFFER (w->contents);
13835 /* Use list_of_error, not Qerror, so that
13836 we catch only errors and don't run the debugger. */
13837 internal_condition_case_1 (redisplay_window_0, window,
13838 list_of_error,
13839 redisplay_window_error);
13842 window = w->next;
13846 static Lisp_Object
13847 redisplay_window_error (Lisp_Object ignore)
13849 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13850 return Qnil;
13853 static Lisp_Object
13854 redisplay_window_0 (Lisp_Object window)
13856 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13857 redisplay_window (window, 0);
13858 return Qnil;
13861 static Lisp_Object
13862 redisplay_window_1 (Lisp_Object window)
13864 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13865 redisplay_window (window, 1);
13866 return Qnil;
13870 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13871 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13872 which positions recorded in ROW differ from current buffer
13873 positions.
13875 Return 0 if cursor is not on this row, 1 otherwise. */
13877 static int
13878 set_cursor_from_row (struct window *w, struct glyph_row *row,
13879 struct glyph_matrix *matrix,
13880 ptrdiff_t delta, ptrdiff_t delta_bytes,
13881 int dy, int dvpos)
13883 struct glyph *glyph = row->glyphs[TEXT_AREA];
13884 struct glyph *end = glyph + row->used[TEXT_AREA];
13885 struct glyph *cursor = NULL;
13886 /* The last known character position in row. */
13887 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13888 int x = row->x;
13889 ptrdiff_t pt_old = PT - delta;
13890 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13891 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13892 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13893 /* A glyph beyond the edge of TEXT_AREA which we should never
13894 touch. */
13895 struct glyph *glyphs_end = end;
13896 /* Non-zero means we've found a match for cursor position, but that
13897 glyph has the avoid_cursor_p flag set. */
13898 int match_with_avoid_cursor = 0;
13899 /* Non-zero means we've seen at least one glyph that came from a
13900 display string. */
13901 int string_seen = 0;
13902 /* Largest and smallest buffer positions seen so far during scan of
13903 glyph row. */
13904 ptrdiff_t bpos_max = pos_before;
13905 ptrdiff_t bpos_min = pos_after;
13906 /* Last buffer position covered by an overlay string with an integer
13907 `cursor' property. */
13908 ptrdiff_t bpos_covered = 0;
13909 /* Non-zero means the display string on which to display the cursor
13910 comes from a text property, not from an overlay. */
13911 int string_from_text_prop = 0;
13913 /* Don't even try doing anything if called for a mode-line or
13914 header-line row, since the rest of the code isn't prepared to
13915 deal with such calamities. */
13916 eassert (!row->mode_line_p);
13917 if (row->mode_line_p)
13918 return 0;
13920 /* Skip over glyphs not having an object at the start and the end of
13921 the row. These are special glyphs like truncation marks on
13922 terminal frames. */
13923 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13925 if (!row->reversed_p)
13927 while (glyph < end
13928 && INTEGERP (glyph->object)
13929 && glyph->charpos < 0)
13931 x += glyph->pixel_width;
13932 ++glyph;
13934 while (end > glyph
13935 && INTEGERP ((end - 1)->object)
13936 /* CHARPOS is zero for blanks and stretch glyphs
13937 inserted by extend_face_to_end_of_line. */
13938 && (end - 1)->charpos <= 0)
13939 --end;
13940 glyph_before = glyph - 1;
13941 glyph_after = end;
13943 else
13945 struct glyph *g;
13947 /* If the glyph row is reversed, we need to process it from back
13948 to front, so swap the edge pointers. */
13949 glyphs_end = end = glyph - 1;
13950 glyph += row->used[TEXT_AREA] - 1;
13952 while (glyph > end + 1
13953 && INTEGERP (glyph->object)
13954 && glyph->charpos < 0)
13956 --glyph;
13957 x -= glyph->pixel_width;
13959 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13960 --glyph;
13961 /* By default, in reversed rows we put the cursor on the
13962 rightmost (first in the reading order) glyph. */
13963 for (g = end + 1; g < glyph; g++)
13964 x += g->pixel_width;
13965 while (end < glyph
13966 && INTEGERP ((end + 1)->object)
13967 && (end + 1)->charpos <= 0)
13968 ++end;
13969 glyph_before = glyph + 1;
13970 glyph_after = end;
13973 else if (row->reversed_p)
13975 /* In R2L rows that don't display text, put the cursor on the
13976 rightmost glyph. Case in point: an empty last line that is
13977 part of an R2L paragraph. */
13978 cursor = end - 1;
13979 /* Avoid placing the cursor on the last glyph of the row, where
13980 on terminal frames we hold the vertical border between
13981 adjacent windows. */
13982 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13983 && !WINDOW_RIGHTMOST_P (w)
13984 && cursor == row->glyphs[LAST_AREA] - 1)
13985 cursor--;
13986 x = -1; /* will be computed below, at label compute_x */
13989 /* Step 1: Try to find the glyph whose character position
13990 corresponds to point. If that's not possible, find 2 glyphs
13991 whose character positions are the closest to point, one before
13992 point, the other after it. */
13993 if (!row->reversed_p)
13994 while (/* not marched to end of glyph row */
13995 glyph < end
13996 /* glyph was not inserted by redisplay for internal purposes */
13997 && !INTEGERP (glyph->object))
13999 if (BUFFERP (glyph->object))
14001 ptrdiff_t dpos = glyph->charpos - pt_old;
14003 if (glyph->charpos > bpos_max)
14004 bpos_max = glyph->charpos;
14005 if (glyph->charpos < bpos_min)
14006 bpos_min = glyph->charpos;
14007 if (!glyph->avoid_cursor_p)
14009 /* If we hit point, we've found the glyph on which to
14010 display the cursor. */
14011 if (dpos == 0)
14013 match_with_avoid_cursor = 0;
14014 break;
14016 /* See if we've found a better approximation to
14017 POS_BEFORE or to POS_AFTER. */
14018 if (0 > dpos && dpos > pos_before - pt_old)
14020 pos_before = glyph->charpos;
14021 glyph_before = glyph;
14023 else if (0 < dpos && dpos < pos_after - pt_old)
14025 pos_after = glyph->charpos;
14026 glyph_after = glyph;
14029 else if (dpos == 0)
14030 match_with_avoid_cursor = 1;
14032 else if (STRINGP (glyph->object))
14034 Lisp_Object chprop;
14035 ptrdiff_t glyph_pos = glyph->charpos;
14037 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14038 glyph->object);
14039 if (!NILP (chprop))
14041 /* If the string came from a `display' text property,
14042 look up the buffer position of that property and
14043 use that position to update bpos_max, as if we
14044 actually saw such a position in one of the row's
14045 glyphs. This helps with supporting integer values
14046 of `cursor' property on the display string in
14047 situations where most or all of the row's buffer
14048 text is completely covered by display properties,
14049 so that no glyph with valid buffer positions is
14050 ever seen in the row. */
14051 ptrdiff_t prop_pos =
14052 string_buffer_position_lim (glyph->object, pos_before,
14053 pos_after, 0);
14055 if (prop_pos >= pos_before)
14056 bpos_max = prop_pos - 1;
14058 if (INTEGERP (chprop))
14060 bpos_covered = bpos_max + XINT (chprop);
14061 /* If the `cursor' property covers buffer positions up
14062 to and including point, we should display cursor on
14063 this glyph. Note that, if a `cursor' property on one
14064 of the string's characters has an integer value, we
14065 will break out of the loop below _before_ we get to
14066 the position match above. IOW, integer values of
14067 the `cursor' property override the "exact match for
14068 point" strategy of positioning the cursor. */
14069 /* Implementation note: bpos_max == pt_old when, e.g.,
14070 we are in an empty line, where bpos_max is set to
14071 MATRIX_ROW_START_CHARPOS, see above. */
14072 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14074 cursor = glyph;
14075 break;
14079 string_seen = 1;
14081 x += glyph->pixel_width;
14082 ++glyph;
14084 else if (glyph > end) /* row is reversed */
14085 while (!INTEGERP (glyph->object))
14087 if (BUFFERP (glyph->object))
14089 ptrdiff_t dpos = glyph->charpos - pt_old;
14091 if (glyph->charpos > bpos_max)
14092 bpos_max = glyph->charpos;
14093 if (glyph->charpos < bpos_min)
14094 bpos_min = glyph->charpos;
14095 if (!glyph->avoid_cursor_p)
14097 if (dpos == 0)
14099 match_with_avoid_cursor = 0;
14100 break;
14102 if (0 > dpos && dpos > pos_before - pt_old)
14104 pos_before = glyph->charpos;
14105 glyph_before = glyph;
14107 else if (0 < dpos && dpos < pos_after - pt_old)
14109 pos_after = glyph->charpos;
14110 glyph_after = glyph;
14113 else if (dpos == 0)
14114 match_with_avoid_cursor = 1;
14116 else if (STRINGP (glyph->object))
14118 Lisp_Object chprop;
14119 ptrdiff_t glyph_pos = glyph->charpos;
14121 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14122 glyph->object);
14123 if (!NILP (chprop))
14125 ptrdiff_t prop_pos =
14126 string_buffer_position_lim (glyph->object, pos_before,
14127 pos_after, 0);
14129 if (prop_pos >= pos_before)
14130 bpos_max = prop_pos - 1;
14132 if (INTEGERP (chprop))
14134 bpos_covered = bpos_max + XINT (chprop);
14135 /* If the `cursor' property covers buffer positions up
14136 to and including point, we should display cursor on
14137 this glyph. */
14138 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14140 cursor = glyph;
14141 break;
14144 string_seen = 1;
14146 --glyph;
14147 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14149 x--; /* can't use any pixel_width */
14150 break;
14152 x -= glyph->pixel_width;
14155 /* Step 2: If we didn't find an exact match for point, we need to
14156 look for a proper place to put the cursor among glyphs between
14157 GLYPH_BEFORE and GLYPH_AFTER. */
14158 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14159 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14160 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14162 /* An empty line has a single glyph whose OBJECT is zero and
14163 whose CHARPOS is the position of a newline on that line.
14164 Note that on a TTY, there are more glyphs after that, which
14165 were produced by extend_face_to_end_of_line, but their
14166 CHARPOS is zero or negative. */
14167 int empty_line_p =
14168 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14169 && INTEGERP (glyph->object) && glyph->charpos > 0
14170 /* On a TTY, continued and truncated rows also have a glyph at
14171 their end whose OBJECT is zero and whose CHARPOS is
14172 positive (the continuation and truncation glyphs), but such
14173 rows are obviously not "empty". */
14174 && !(row->continued_p || row->truncated_on_right_p);
14176 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14178 ptrdiff_t ellipsis_pos;
14180 /* Scan back over the ellipsis glyphs. */
14181 if (!row->reversed_p)
14183 ellipsis_pos = (glyph - 1)->charpos;
14184 while (glyph > row->glyphs[TEXT_AREA]
14185 && (glyph - 1)->charpos == ellipsis_pos)
14186 glyph--, x -= glyph->pixel_width;
14187 /* That loop always goes one position too far, including
14188 the glyph before the ellipsis. So scan forward over
14189 that one. */
14190 x += glyph->pixel_width;
14191 glyph++;
14193 else /* row is reversed */
14195 ellipsis_pos = (glyph + 1)->charpos;
14196 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14197 && (glyph + 1)->charpos == ellipsis_pos)
14198 glyph++, x += glyph->pixel_width;
14199 x -= glyph->pixel_width;
14200 glyph--;
14203 else if (match_with_avoid_cursor)
14205 cursor = glyph_after;
14206 x = -1;
14208 else if (string_seen)
14210 int incr = row->reversed_p ? -1 : +1;
14212 /* Need to find the glyph that came out of a string which is
14213 present at point. That glyph is somewhere between
14214 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14215 positioned between POS_BEFORE and POS_AFTER in the
14216 buffer. */
14217 struct glyph *start, *stop;
14218 ptrdiff_t pos = pos_before;
14220 x = -1;
14222 /* If the row ends in a newline from a display string,
14223 reordering could have moved the glyphs belonging to the
14224 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14225 in this case we extend the search to the last glyph in
14226 the row that was not inserted by redisplay. */
14227 if (row->ends_in_newline_from_string_p)
14229 glyph_after = end;
14230 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14233 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14234 correspond to POS_BEFORE and POS_AFTER, respectively. We
14235 need START and STOP in the order that corresponds to the
14236 row's direction as given by its reversed_p flag. If the
14237 directionality of characters between POS_BEFORE and
14238 POS_AFTER is the opposite of the row's base direction,
14239 these characters will have been reordered for display,
14240 and we need to reverse START and STOP. */
14241 if (!row->reversed_p)
14243 start = min (glyph_before, glyph_after);
14244 stop = max (glyph_before, glyph_after);
14246 else
14248 start = max (glyph_before, glyph_after);
14249 stop = min (glyph_before, glyph_after);
14251 for (glyph = start + incr;
14252 row->reversed_p ? glyph > stop : glyph < stop; )
14255 /* Any glyphs that come from the buffer are here because
14256 of bidi reordering. Skip them, and only pay
14257 attention to glyphs that came from some string. */
14258 if (STRINGP (glyph->object))
14260 Lisp_Object str;
14261 ptrdiff_t tem;
14262 /* If the display property covers the newline, we
14263 need to search for it one position farther. */
14264 ptrdiff_t lim = pos_after
14265 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14267 string_from_text_prop = 0;
14268 str = glyph->object;
14269 tem = string_buffer_position_lim (str, pos, lim, 0);
14270 if (tem == 0 /* from overlay */
14271 || pos <= tem)
14273 /* If the string from which this glyph came is
14274 found in the buffer at point, or at position
14275 that is closer to point than pos_after, then
14276 we've found the glyph we've been looking for.
14277 If it comes from an overlay (tem == 0), and
14278 it has the `cursor' property on one of its
14279 glyphs, record that glyph as a candidate for
14280 displaying the cursor. (As in the
14281 unidirectional version, we will display the
14282 cursor on the last candidate we find.) */
14283 if (tem == 0
14284 || tem == pt_old
14285 || (tem - pt_old > 0 && tem < pos_after))
14287 /* The glyphs from this string could have
14288 been reordered. Find the one with the
14289 smallest string position. Or there could
14290 be a character in the string with the
14291 `cursor' property, which means display
14292 cursor on that character's glyph. */
14293 ptrdiff_t strpos = glyph->charpos;
14295 if (tem)
14297 cursor = glyph;
14298 string_from_text_prop = 1;
14300 for ( ;
14301 (row->reversed_p ? glyph > stop : glyph < stop)
14302 && EQ (glyph->object, str);
14303 glyph += incr)
14305 Lisp_Object cprop;
14306 ptrdiff_t gpos = glyph->charpos;
14308 cprop = Fget_char_property (make_number (gpos),
14309 Qcursor,
14310 glyph->object);
14311 if (!NILP (cprop))
14313 cursor = glyph;
14314 break;
14316 if (tem && glyph->charpos < strpos)
14318 strpos = glyph->charpos;
14319 cursor = glyph;
14323 if (tem == pt_old
14324 || (tem - pt_old > 0 && tem < pos_after))
14325 goto compute_x;
14327 if (tem)
14328 pos = tem + 1; /* don't find previous instances */
14330 /* This string is not what we want; skip all of the
14331 glyphs that came from it. */
14332 while ((row->reversed_p ? glyph > stop : glyph < stop)
14333 && EQ (glyph->object, str))
14334 glyph += incr;
14336 else
14337 glyph += incr;
14340 /* If we reached the end of the line, and END was from a string,
14341 the cursor is not on this line. */
14342 if (cursor == NULL
14343 && (row->reversed_p ? glyph <= end : glyph >= end)
14344 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14345 && STRINGP (end->object)
14346 && row->continued_p)
14347 return 0;
14349 /* A truncated row may not include PT among its character positions.
14350 Setting the cursor inside the scroll margin will trigger
14351 recalculation of hscroll in hscroll_window_tree. But if a
14352 display string covers point, defer to the string-handling
14353 code below to figure this out. */
14354 else if (row->truncated_on_left_p && pt_old < bpos_min)
14356 cursor = glyph_before;
14357 x = -1;
14359 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14360 /* Zero-width characters produce no glyphs. */
14361 || (!empty_line_p
14362 && (row->reversed_p
14363 ? glyph_after > glyphs_end
14364 : glyph_after < glyphs_end)))
14366 cursor = glyph_after;
14367 x = -1;
14371 compute_x:
14372 if (cursor != NULL)
14373 glyph = cursor;
14374 else if (glyph == glyphs_end
14375 && pos_before == pos_after
14376 && STRINGP ((row->reversed_p
14377 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14378 : row->glyphs[TEXT_AREA])->object))
14380 /* If all the glyphs of this row came from strings, put the
14381 cursor on the first glyph of the row. This avoids having the
14382 cursor outside of the text area in this very rare and hard
14383 use case. */
14384 glyph =
14385 row->reversed_p
14386 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14387 : row->glyphs[TEXT_AREA];
14389 if (x < 0)
14391 struct glyph *g;
14393 /* Need to compute x that corresponds to GLYPH. */
14394 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14396 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14397 emacs_abort ();
14398 x += g->pixel_width;
14402 /* ROW could be part of a continued line, which, under bidi
14403 reordering, might have other rows whose start and end charpos
14404 occlude point. Only set w->cursor if we found a better
14405 approximation to the cursor position than we have from previously
14406 examined candidate rows belonging to the same continued line. */
14407 if (/* we already have a candidate row */
14408 w->cursor.vpos >= 0
14409 /* that candidate is not the row we are processing */
14410 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14411 /* Make sure cursor.vpos specifies a row whose start and end
14412 charpos occlude point, and it is valid candidate for being a
14413 cursor-row. This is because some callers of this function
14414 leave cursor.vpos at the row where the cursor was displayed
14415 during the last redisplay cycle. */
14416 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14417 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14418 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14420 struct glyph *g1 =
14421 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14423 /* Don't consider glyphs that are outside TEXT_AREA. */
14424 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14425 return 0;
14426 /* Keep the candidate whose buffer position is the closest to
14427 point or has the `cursor' property. */
14428 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14429 w->cursor.hpos >= 0
14430 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14431 && ((BUFFERP (g1->object)
14432 && (g1->charpos == pt_old /* an exact match always wins */
14433 || (BUFFERP (glyph->object)
14434 && eabs (g1->charpos - pt_old)
14435 < eabs (glyph->charpos - pt_old))))
14436 /* previous candidate is a glyph from a string that has
14437 a non-nil `cursor' property */
14438 || (STRINGP (g1->object)
14439 && (!NILP (Fget_char_property (make_number (g1->charpos),
14440 Qcursor, g1->object))
14441 /* previous candidate is from the same display
14442 string as this one, and the display string
14443 came from a text property */
14444 || (EQ (g1->object, glyph->object)
14445 && string_from_text_prop)
14446 /* this candidate is from newline and its
14447 position is not an exact match */
14448 || (INTEGERP (glyph->object)
14449 && glyph->charpos != pt_old)))))
14450 return 0;
14451 /* If this candidate gives an exact match, use that. */
14452 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14453 /* If this candidate is a glyph created for the
14454 terminating newline of a line, and point is on that
14455 newline, it wins because it's an exact match. */
14456 || (!row->continued_p
14457 && INTEGERP (glyph->object)
14458 && glyph->charpos == 0
14459 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14460 /* Otherwise, keep the candidate that comes from a row
14461 spanning less buffer positions. This may win when one or
14462 both candidate positions are on glyphs that came from
14463 display strings, for which we cannot compare buffer
14464 positions. */
14465 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14466 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14467 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14468 return 0;
14470 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14471 w->cursor.x = x;
14472 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14473 w->cursor.y = row->y + dy;
14475 if (w == XWINDOW (selected_window))
14477 if (!row->continued_p
14478 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14479 && row->x == 0)
14481 this_line_buffer = XBUFFER (w->contents);
14483 CHARPOS (this_line_start_pos)
14484 = MATRIX_ROW_START_CHARPOS (row) + delta;
14485 BYTEPOS (this_line_start_pos)
14486 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14488 CHARPOS (this_line_end_pos)
14489 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14490 BYTEPOS (this_line_end_pos)
14491 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14493 this_line_y = w->cursor.y;
14494 this_line_pixel_height = row->height;
14495 this_line_vpos = w->cursor.vpos;
14496 this_line_start_x = row->x;
14498 else
14499 CHARPOS (this_line_start_pos) = 0;
14502 return 1;
14506 /* Run window scroll functions, if any, for WINDOW with new window
14507 start STARTP. Sets the window start of WINDOW to that position.
14509 We assume that the window's buffer is really current. */
14511 static struct text_pos
14512 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14514 struct window *w = XWINDOW (window);
14515 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14517 eassert (current_buffer == XBUFFER (w->contents));
14519 if (!NILP (Vwindow_scroll_functions))
14521 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14522 make_number (CHARPOS (startp)));
14523 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14524 /* In case the hook functions switch buffers. */
14525 set_buffer_internal (XBUFFER (w->contents));
14528 return startp;
14532 /* Make sure the line containing the cursor is fully visible.
14533 A value of 1 means there is nothing to be done.
14534 (Either the line is fully visible, or it cannot be made so,
14535 or we cannot tell.)
14537 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14538 is higher than window.
14540 A value of 0 means the caller should do scrolling
14541 as if point had gone off the screen. */
14543 static int
14544 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14546 struct glyph_matrix *matrix;
14547 struct glyph_row *row;
14548 int window_height;
14550 if (!make_cursor_line_fully_visible_p)
14551 return 1;
14553 /* It's not always possible to find the cursor, e.g, when a window
14554 is full of overlay strings. Don't do anything in that case. */
14555 if (w->cursor.vpos < 0)
14556 return 1;
14558 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14559 row = MATRIX_ROW (matrix, w->cursor.vpos);
14561 /* If the cursor row is not partially visible, there's nothing to do. */
14562 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14563 return 1;
14565 /* If the row the cursor is in is taller than the window's height,
14566 it's not clear what to do, so do nothing. */
14567 window_height = window_box_height (w);
14568 if (row->height >= window_height)
14570 if (!force_p || MINI_WINDOW_P (w)
14571 || w->vscroll || w->cursor.vpos == 0)
14572 return 1;
14574 return 0;
14578 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14579 non-zero means only WINDOW is redisplayed in redisplay_internal.
14580 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14581 in redisplay_window to bring a partially visible line into view in
14582 the case that only the cursor has moved.
14584 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14585 last screen line's vertical height extends past the end of the screen.
14587 Value is
14589 1 if scrolling succeeded
14591 0 if scrolling didn't find point.
14593 -1 if new fonts have been loaded so that we must interrupt
14594 redisplay, adjust glyph matrices, and try again. */
14596 enum
14598 SCROLLING_SUCCESS,
14599 SCROLLING_FAILED,
14600 SCROLLING_NEED_LARGER_MATRICES
14603 /* If scroll-conservatively is more than this, never recenter.
14605 If you change this, don't forget to update the doc string of
14606 `scroll-conservatively' and the Emacs manual. */
14607 #define SCROLL_LIMIT 100
14609 static int
14610 try_scrolling (Lisp_Object window, int just_this_one_p,
14611 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14612 int temp_scroll_step, int last_line_misfit)
14614 struct window *w = XWINDOW (window);
14615 struct frame *f = XFRAME (w->frame);
14616 struct text_pos pos, startp;
14617 struct it it;
14618 int this_scroll_margin, scroll_max, rc, height;
14619 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14620 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14621 Lisp_Object aggressive;
14622 /* We will never try scrolling more than this number of lines. */
14623 int scroll_limit = SCROLL_LIMIT;
14624 int frame_line_height = default_line_pixel_height (w);
14625 int window_total_lines
14626 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14628 #ifdef GLYPH_DEBUG
14629 debug_method_add (w, "try_scrolling");
14630 #endif
14632 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14634 /* Compute scroll margin height in pixels. We scroll when point is
14635 within this distance from the top or bottom of the window. */
14636 if (scroll_margin > 0)
14637 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14638 * frame_line_height;
14639 else
14640 this_scroll_margin = 0;
14642 /* Force arg_scroll_conservatively to have a reasonable value, to
14643 avoid scrolling too far away with slow move_it_* functions. Note
14644 that the user can supply scroll-conservatively equal to
14645 `most-positive-fixnum', which can be larger than INT_MAX. */
14646 if (arg_scroll_conservatively > scroll_limit)
14648 arg_scroll_conservatively = scroll_limit + 1;
14649 scroll_max = scroll_limit * frame_line_height;
14651 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14652 /* Compute how much we should try to scroll maximally to bring
14653 point into view. */
14654 scroll_max = (max (scroll_step,
14655 max (arg_scroll_conservatively, temp_scroll_step))
14656 * frame_line_height);
14657 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14658 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14659 /* We're trying to scroll because of aggressive scrolling but no
14660 scroll_step is set. Choose an arbitrary one. */
14661 scroll_max = 10 * frame_line_height;
14662 else
14663 scroll_max = 0;
14665 too_near_end:
14667 /* Decide whether to scroll down. */
14668 if (PT > CHARPOS (startp))
14670 int scroll_margin_y;
14672 /* Compute the pixel ypos of the scroll margin, then move IT to
14673 either that ypos or PT, whichever comes first. */
14674 start_display (&it, w, startp);
14675 scroll_margin_y = it.last_visible_y - this_scroll_margin
14676 - frame_line_height * extra_scroll_margin_lines;
14677 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14678 (MOVE_TO_POS | MOVE_TO_Y));
14680 if (PT > CHARPOS (it.current.pos))
14682 int y0 = line_bottom_y (&it);
14683 /* Compute how many pixels below window bottom to stop searching
14684 for PT. This avoids costly search for PT that is far away if
14685 the user limited scrolling by a small number of lines, but
14686 always finds PT if scroll_conservatively is set to a large
14687 number, such as most-positive-fixnum. */
14688 int slack = max (scroll_max, 10 * frame_line_height);
14689 int y_to_move = it.last_visible_y + slack;
14691 /* Compute the distance from the scroll margin to PT or to
14692 the scroll limit, whichever comes first. This should
14693 include the height of the cursor line, to make that line
14694 fully visible. */
14695 move_it_to (&it, PT, -1, y_to_move,
14696 -1, MOVE_TO_POS | MOVE_TO_Y);
14697 dy = line_bottom_y (&it) - y0;
14699 if (dy > scroll_max)
14700 return SCROLLING_FAILED;
14702 if (dy > 0)
14703 scroll_down_p = 1;
14707 if (scroll_down_p)
14709 /* Point is in or below the bottom scroll margin, so move the
14710 window start down. If scrolling conservatively, move it just
14711 enough down to make point visible. If scroll_step is set,
14712 move it down by scroll_step. */
14713 if (arg_scroll_conservatively)
14714 amount_to_scroll
14715 = min (max (dy, frame_line_height),
14716 frame_line_height * arg_scroll_conservatively);
14717 else if (scroll_step || temp_scroll_step)
14718 amount_to_scroll = scroll_max;
14719 else
14721 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14722 height = WINDOW_BOX_TEXT_HEIGHT (w);
14723 if (NUMBERP (aggressive))
14725 double float_amount = XFLOATINT (aggressive) * height;
14726 int aggressive_scroll = float_amount;
14727 if (aggressive_scroll == 0 && float_amount > 0)
14728 aggressive_scroll = 1;
14729 /* Don't let point enter the scroll margin near top of
14730 the window. This could happen if the value of
14731 scroll_up_aggressively is too large and there are
14732 non-zero margins, because scroll_up_aggressively
14733 means put point that fraction of window height
14734 _from_the_bottom_margin_. */
14735 if (aggressive_scroll + 2*this_scroll_margin > height)
14736 aggressive_scroll = height - 2*this_scroll_margin;
14737 amount_to_scroll = dy + aggressive_scroll;
14741 if (amount_to_scroll <= 0)
14742 return SCROLLING_FAILED;
14744 start_display (&it, w, startp);
14745 if (arg_scroll_conservatively <= scroll_limit)
14746 move_it_vertically (&it, amount_to_scroll);
14747 else
14749 /* Extra precision for users who set scroll-conservatively
14750 to a large number: make sure the amount we scroll
14751 the window start is never less than amount_to_scroll,
14752 which was computed as distance from window bottom to
14753 point. This matters when lines at window top and lines
14754 below window bottom have different height. */
14755 struct it it1;
14756 void *it1data = NULL;
14757 /* We use a temporary it1 because line_bottom_y can modify
14758 its argument, if it moves one line down; see there. */
14759 int start_y;
14761 SAVE_IT (it1, it, it1data);
14762 start_y = line_bottom_y (&it1);
14763 do {
14764 RESTORE_IT (&it, &it, it1data);
14765 move_it_by_lines (&it, 1);
14766 SAVE_IT (it1, it, it1data);
14767 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14770 /* If STARTP is unchanged, move it down another screen line. */
14771 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14772 move_it_by_lines (&it, 1);
14773 startp = it.current.pos;
14775 else
14777 struct text_pos scroll_margin_pos = startp;
14778 int y_offset = 0;
14780 /* See if point is inside the scroll margin at the top of the
14781 window. */
14782 if (this_scroll_margin)
14784 int y_start;
14786 start_display (&it, w, startp);
14787 y_start = it.current_y;
14788 move_it_vertically (&it, this_scroll_margin);
14789 scroll_margin_pos = it.current.pos;
14790 /* If we didn't move enough before hitting ZV, request
14791 additional amount of scroll, to move point out of the
14792 scroll margin. */
14793 if (IT_CHARPOS (it) == ZV
14794 && it.current_y - y_start < this_scroll_margin)
14795 y_offset = this_scroll_margin - (it.current_y - y_start);
14798 if (PT < CHARPOS (scroll_margin_pos))
14800 /* Point is in the scroll margin at the top of the window or
14801 above what is displayed in the window. */
14802 int y0, y_to_move;
14804 /* Compute the vertical distance from PT to the scroll
14805 margin position. Move as far as scroll_max allows, or
14806 one screenful, or 10 screen lines, whichever is largest.
14807 Give up if distance is greater than scroll_max or if we
14808 didn't reach the scroll margin position. */
14809 SET_TEXT_POS (pos, PT, PT_BYTE);
14810 start_display (&it, w, pos);
14811 y0 = it.current_y;
14812 y_to_move = max (it.last_visible_y,
14813 max (scroll_max, 10 * frame_line_height));
14814 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14815 y_to_move, -1,
14816 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14817 dy = it.current_y - y0;
14818 if (dy > scroll_max
14819 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14820 return SCROLLING_FAILED;
14822 /* Additional scroll for when ZV was too close to point. */
14823 dy += y_offset;
14825 /* Compute new window start. */
14826 start_display (&it, w, startp);
14828 if (arg_scroll_conservatively)
14829 amount_to_scroll = max (dy, frame_line_height *
14830 max (scroll_step, temp_scroll_step));
14831 else if (scroll_step || temp_scroll_step)
14832 amount_to_scroll = scroll_max;
14833 else
14835 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14836 height = WINDOW_BOX_TEXT_HEIGHT (w);
14837 if (NUMBERP (aggressive))
14839 double float_amount = XFLOATINT (aggressive) * height;
14840 int aggressive_scroll = float_amount;
14841 if (aggressive_scroll == 0 && float_amount > 0)
14842 aggressive_scroll = 1;
14843 /* Don't let point enter the scroll margin near
14844 bottom of the window, if the value of
14845 scroll_down_aggressively happens to be too
14846 large. */
14847 if (aggressive_scroll + 2*this_scroll_margin > height)
14848 aggressive_scroll = height - 2*this_scroll_margin;
14849 amount_to_scroll = dy + aggressive_scroll;
14853 if (amount_to_scroll <= 0)
14854 return SCROLLING_FAILED;
14856 move_it_vertically_backward (&it, amount_to_scroll);
14857 startp = it.current.pos;
14861 /* Run window scroll functions. */
14862 startp = run_window_scroll_functions (window, startp);
14864 /* Display the window. Give up if new fonts are loaded, or if point
14865 doesn't appear. */
14866 if (!try_window (window, startp, 0))
14867 rc = SCROLLING_NEED_LARGER_MATRICES;
14868 else if (w->cursor.vpos < 0)
14870 clear_glyph_matrix (w->desired_matrix);
14871 rc = SCROLLING_FAILED;
14873 else
14875 /* Maybe forget recorded base line for line number display. */
14876 if (!just_this_one_p
14877 || current_buffer->clip_changed
14878 || BEG_UNCHANGED < CHARPOS (startp))
14879 w->base_line_number = 0;
14881 /* If cursor ends up on a partially visible line,
14882 treat that as being off the bottom of the screen. */
14883 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14884 /* It's possible that the cursor is on the first line of the
14885 buffer, which is partially obscured due to a vscroll
14886 (Bug#7537). In that case, avoid looping forever . */
14887 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14889 clear_glyph_matrix (w->desired_matrix);
14890 ++extra_scroll_margin_lines;
14891 goto too_near_end;
14893 rc = SCROLLING_SUCCESS;
14896 return rc;
14900 /* Compute a suitable window start for window W if display of W starts
14901 on a continuation line. Value is non-zero if a new window start
14902 was computed.
14904 The new window start will be computed, based on W's width, starting
14905 from the start of the continued line. It is the start of the
14906 screen line with the minimum distance from the old start W->start. */
14908 static int
14909 compute_window_start_on_continuation_line (struct window *w)
14911 struct text_pos pos, start_pos;
14912 int window_start_changed_p = 0;
14914 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14916 /* If window start is on a continuation line... Window start may be
14917 < BEGV in case there's invisible text at the start of the
14918 buffer (M-x rmail, for example). */
14919 if (CHARPOS (start_pos) > BEGV
14920 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14922 struct it it;
14923 struct glyph_row *row;
14925 /* Handle the case that the window start is out of range. */
14926 if (CHARPOS (start_pos) < BEGV)
14927 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14928 else if (CHARPOS (start_pos) > ZV)
14929 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14931 /* Find the start of the continued line. This should be fast
14932 because find_newline is fast (newline cache). */
14933 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14934 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14935 row, DEFAULT_FACE_ID);
14936 reseat_at_previous_visible_line_start (&it);
14938 /* If the line start is "too far" away from the window start,
14939 say it takes too much time to compute a new window start. */
14940 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14941 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14943 int min_distance, distance;
14945 /* Move forward by display lines to find the new window
14946 start. If window width was enlarged, the new start can
14947 be expected to be > the old start. If window width was
14948 decreased, the new window start will be < the old start.
14949 So, we're looking for the display line start with the
14950 minimum distance from the old window start. */
14951 pos = it.current.pos;
14952 min_distance = INFINITY;
14953 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14954 distance < min_distance)
14956 min_distance = distance;
14957 pos = it.current.pos;
14958 if (it.line_wrap == WORD_WRAP)
14960 /* Under WORD_WRAP, move_it_by_lines is likely to
14961 overshoot and stop not at the first, but the
14962 second character from the left margin. So in
14963 that case, we need a more tight control on the X
14964 coordinate of the iterator than move_it_by_lines
14965 promises in its contract. The method is to first
14966 go to the last (rightmost) visible character of a
14967 line, then move to the leftmost character on the
14968 next line in a separate call. */
14969 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14970 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14971 move_it_to (&it, ZV, 0,
14972 it.current_y + it.max_ascent + it.max_descent, -1,
14973 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14975 else
14976 move_it_by_lines (&it, 1);
14979 /* Set the window start there. */
14980 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14981 window_start_changed_p = 1;
14985 return window_start_changed_p;
14989 /* Try cursor movement in case text has not changed in window WINDOW,
14990 with window start STARTP. Value is
14992 CURSOR_MOVEMENT_SUCCESS if successful
14994 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14996 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14997 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14998 we want to scroll as if scroll-step were set to 1. See the code.
15000 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15001 which case we have to abort this redisplay, and adjust matrices
15002 first. */
15004 enum
15006 CURSOR_MOVEMENT_SUCCESS,
15007 CURSOR_MOVEMENT_CANNOT_BE_USED,
15008 CURSOR_MOVEMENT_MUST_SCROLL,
15009 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15012 static int
15013 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15015 struct window *w = XWINDOW (window);
15016 struct frame *f = XFRAME (w->frame);
15017 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15019 #ifdef GLYPH_DEBUG
15020 if (inhibit_try_cursor_movement)
15021 return rc;
15022 #endif
15024 /* Previously, there was a check for Lisp integer in the
15025 if-statement below. Now, this field is converted to
15026 ptrdiff_t, thus zero means invalid position in a buffer. */
15027 eassert (w->last_point > 0);
15028 /* Likewise there was a check whether window_end_vpos is nil or larger
15029 than the window. Now window_end_vpos is int and so never nil, but
15030 let's leave eassert to check whether it fits in the window. */
15031 eassert (w->window_end_vpos < w->current_matrix->nrows);
15033 /* Handle case where text has not changed, only point, and it has
15034 not moved off the frame. */
15035 if (/* Point may be in this window. */
15036 PT >= CHARPOS (startp)
15037 /* Selective display hasn't changed. */
15038 && !current_buffer->clip_changed
15039 /* Function force-mode-line-update is used to force a thorough
15040 redisplay. It sets either windows_or_buffers_changed or
15041 update_mode_lines. So don't take a shortcut here for these
15042 cases. */
15043 && !update_mode_lines
15044 && !windows_or_buffers_changed
15045 && !cursor_type_changed
15046 /* Can't use this case if highlighting a region. When a
15047 region exists, cursor movement has to do more than just
15048 set the cursor. */
15049 && markpos_of_region () < 0
15050 && !w->region_showing
15051 && NILP (Vshow_trailing_whitespace)
15052 /* This code is not used for mini-buffer for the sake of the case
15053 of redisplaying to replace an echo area message; since in
15054 that case the mini-buffer contents per se are usually
15055 unchanged. This code is of no real use in the mini-buffer
15056 since the handling of this_line_start_pos, etc., in redisplay
15057 handles the same cases. */
15058 && !EQ (window, minibuf_window)
15059 && (FRAME_WINDOW_P (f)
15060 || !overlay_arrow_in_current_buffer_p ()))
15062 int this_scroll_margin, top_scroll_margin;
15063 struct glyph_row *row = NULL;
15064 int frame_line_height = default_line_pixel_height (w);
15065 int window_total_lines
15066 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15068 #ifdef GLYPH_DEBUG
15069 debug_method_add (w, "cursor movement");
15070 #endif
15072 /* Scroll if point within this distance from the top or bottom
15073 of the window. This is a pixel value. */
15074 if (scroll_margin > 0)
15076 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15077 this_scroll_margin *= frame_line_height;
15079 else
15080 this_scroll_margin = 0;
15082 top_scroll_margin = this_scroll_margin;
15083 if (WINDOW_WANTS_HEADER_LINE_P (w))
15084 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15086 /* Start with the row the cursor was displayed during the last
15087 not paused redisplay. Give up if that row is not valid. */
15088 if (w->last_cursor.vpos < 0
15089 || w->last_cursor.vpos >= w->current_matrix->nrows)
15090 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15091 else
15093 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15094 if (row->mode_line_p)
15095 ++row;
15096 if (!row->enabled_p)
15097 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15100 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15102 int scroll_p = 0, must_scroll = 0;
15103 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15105 if (PT > w->last_point)
15107 /* Point has moved forward. */
15108 while (MATRIX_ROW_END_CHARPOS (row) < PT
15109 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15111 eassert (row->enabled_p);
15112 ++row;
15115 /* If the end position of a row equals the start
15116 position of the next row, and PT is at that position,
15117 we would rather display cursor in the next line. */
15118 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15119 && MATRIX_ROW_END_CHARPOS (row) == PT
15120 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15121 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15122 && !cursor_row_p (row))
15123 ++row;
15125 /* If within the scroll margin, scroll. Note that
15126 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15127 the next line would be drawn, and that
15128 this_scroll_margin can be zero. */
15129 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15130 || PT > MATRIX_ROW_END_CHARPOS (row)
15131 /* Line is completely visible last line in window
15132 and PT is to be set in the next line. */
15133 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15134 && PT == MATRIX_ROW_END_CHARPOS (row)
15135 && !row->ends_at_zv_p
15136 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15137 scroll_p = 1;
15139 else if (PT < w->last_point)
15141 /* Cursor has to be moved backward. Note that PT >=
15142 CHARPOS (startp) because of the outer if-statement. */
15143 while (!row->mode_line_p
15144 && (MATRIX_ROW_START_CHARPOS (row) > PT
15145 || (MATRIX_ROW_START_CHARPOS (row) == PT
15146 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15147 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15148 row > w->current_matrix->rows
15149 && (row-1)->ends_in_newline_from_string_p))))
15150 && (row->y > top_scroll_margin
15151 || CHARPOS (startp) == BEGV))
15153 eassert (row->enabled_p);
15154 --row;
15157 /* Consider the following case: Window starts at BEGV,
15158 there is invisible, intangible text at BEGV, so that
15159 display starts at some point START > BEGV. It can
15160 happen that we are called with PT somewhere between
15161 BEGV and START. Try to handle that case. */
15162 if (row < w->current_matrix->rows
15163 || row->mode_line_p)
15165 row = w->current_matrix->rows;
15166 if (row->mode_line_p)
15167 ++row;
15170 /* Due to newlines in overlay strings, we may have to
15171 skip forward over overlay strings. */
15172 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15173 && MATRIX_ROW_END_CHARPOS (row) == PT
15174 && !cursor_row_p (row))
15175 ++row;
15177 /* If within the scroll margin, scroll. */
15178 if (row->y < top_scroll_margin
15179 && CHARPOS (startp) != BEGV)
15180 scroll_p = 1;
15182 else
15184 /* Cursor did not move. So don't scroll even if cursor line
15185 is partially visible, as it was so before. */
15186 rc = CURSOR_MOVEMENT_SUCCESS;
15189 if (PT < MATRIX_ROW_START_CHARPOS (row)
15190 || PT > MATRIX_ROW_END_CHARPOS (row))
15192 /* if PT is not in the glyph row, give up. */
15193 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15194 must_scroll = 1;
15196 else if (rc != CURSOR_MOVEMENT_SUCCESS
15197 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15199 struct glyph_row *row1;
15201 /* If rows are bidi-reordered and point moved, back up
15202 until we find a row that does not belong to a
15203 continuation line. This is because we must consider
15204 all rows of a continued line as candidates for the
15205 new cursor positioning, since row start and end
15206 positions change non-linearly with vertical position
15207 in such rows. */
15208 /* FIXME: Revisit this when glyph ``spilling'' in
15209 continuation lines' rows is implemented for
15210 bidi-reordered rows. */
15211 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15212 MATRIX_ROW_CONTINUATION_LINE_P (row);
15213 --row)
15215 /* If we hit the beginning of the displayed portion
15216 without finding the first row of a continued
15217 line, give up. */
15218 if (row <= row1)
15220 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15221 break;
15223 eassert (row->enabled_p);
15226 if (must_scroll)
15228 else if (rc != CURSOR_MOVEMENT_SUCCESS
15229 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15230 /* Make sure this isn't a header line by any chance, since
15231 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15232 && !row->mode_line_p
15233 && make_cursor_line_fully_visible_p)
15235 if (PT == MATRIX_ROW_END_CHARPOS (row)
15236 && !row->ends_at_zv_p
15237 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15238 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15239 else if (row->height > window_box_height (w))
15241 /* If we end up in a partially visible line, let's
15242 make it fully visible, except when it's taller
15243 than the window, in which case we can't do much
15244 about it. */
15245 *scroll_step = 1;
15246 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15248 else
15250 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15251 if (!cursor_row_fully_visible_p (w, 0, 1))
15252 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15253 else
15254 rc = CURSOR_MOVEMENT_SUCCESS;
15257 else if (scroll_p)
15258 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15259 else if (rc != CURSOR_MOVEMENT_SUCCESS
15260 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15262 /* With bidi-reordered rows, there could be more than
15263 one candidate row whose start and end positions
15264 occlude point. We need to let set_cursor_from_row
15265 find the best candidate. */
15266 /* FIXME: Revisit this when glyph ``spilling'' in
15267 continuation lines' rows is implemented for
15268 bidi-reordered rows. */
15269 int rv = 0;
15273 int at_zv_p = 0, exact_match_p = 0;
15275 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15276 && PT <= MATRIX_ROW_END_CHARPOS (row)
15277 && cursor_row_p (row))
15278 rv |= set_cursor_from_row (w, row, w->current_matrix,
15279 0, 0, 0, 0);
15280 /* As soon as we've found the exact match for point,
15281 or the first suitable row whose ends_at_zv_p flag
15282 is set, we are done. */
15283 at_zv_p =
15284 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15285 if (rv && !at_zv_p
15286 && w->cursor.hpos >= 0
15287 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15288 w->cursor.vpos))
15290 struct glyph_row *candidate =
15291 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15292 struct glyph *g =
15293 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15294 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15296 exact_match_p =
15297 (BUFFERP (g->object) && g->charpos == PT)
15298 || (INTEGERP (g->object)
15299 && (g->charpos == PT
15300 || (g->charpos == 0 && endpos - 1 == PT)));
15302 if (rv && (at_zv_p || exact_match_p))
15304 rc = CURSOR_MOVEMENT_SUCCESS;
15305 break;
15307 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15308 break;
15309 ++row;
15311 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15312 || row->continued_p)
15313 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15314 || (MATRIX_ROW_START_CHARPOS (row) == PT
15315 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15316 /* If we didn't find any candidate rows, or exited the
15317 loop before all the candidates were examined, signal
15318 to the caller that this method failed. */
15319 if (rc != CURSOR_MOVEMENT_SUCCESS
15320 && !(rv
15321 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15322 && !row->continued_p))
15323 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15324 else if (rv)
15325 rc = CURSOR_MOVEMENT_SUCCESS;
15327 else
15331 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15333 rc = CURSOR_MOVEMENT_SUCCESS;
15334 break;
15336 ++row;
15338 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15339 && MATRIX_ROW_START_CHARPOS (row) == PT
15340 && cursor_row_p (row));
15345 return rc;
15348 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15349 static
15350 #endif
15351 void
15352 set_vertical_scroll_bar (struct window *w)
15354 ptrdiff_t start, end, whole;
15356 /* Calculate the start and end positions for the current window.
15357 At some point, it would be nice to choose between scrollbars
15358 which reflect the whole buffer size, with special markers
15359 indicating narrowing, and scrollbars which reflect only the
15360 visible region.
15362 Note that mini-buffers sometimes aren't displaying any text. */
15363 if (!MINI_WINDOW_P (w)
15364 || (w == XWINDOW (minibuf_window)
15365 && NILP (echo_area_buffer[0])))
15367 struct buffer *buf = XBUFFER (w->contents);
15368 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15369 start = marker_position (w->start) - BUF_BEGV (buf);
15370 /* I don't think this is guaranteed to be right. For the
15371 moment, we'll pretend it is. */
15372 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15374 if (end < start)
15375 end = start;
15376 if (whole < (end - start))
15377 whole = end - start;
15379 else
15380 start = end = whole = 0;
15382 /* Indicate what this scroll bar ought to be displaying now. */
15383 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15384 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15385 (w, end - start, whole, start);
15389 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15390 selected_window is redisplayed.
15392 We can return without actually redisplaying the window if
15393 fonts_changed_p. In that case, redisplay_internal will
15394 retry. */
15396 static void
15397 redisplay_window (Lisp_Object window, int just_this_one_p)
15399 struct window *w = XWINDOW (window);
15400 struct frame *f = XFRAME (w->frame);
15401 struct buffer *buffer = XBUFFER (w->contents);
15402 struct buffer *old = current_buffer;
15403 struct text_pos lpoint, opoint, startp;
15404 int update_mode_line;
15405 int tem;
15406 struct it it;
15407 /* Record it now because it's overwritten. */
15408 int current_matrix_up_to_date_p = 0;
15409 int used_current_matrix_p = 0;
15410 /* This is less strict than current_matrix_up_to_date_p.
15411 It indicates that the buffer contents and narrowing are unchanged. */
15412 int buffer_unchanged_p = 0;
15413 int temp_scroll_step = 0;
15414 ptrdiff_t count = SPECPDL_INDEX ();
15415 int rc;
15416 int centering_position = -1;
15417 int last_line_misfit = 0;
15418 ptrdiff_t beg_unchanged, end_unchanged;
15419 int frame_line_height;
15421 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15422 opoint = lpoint;
15424 #ifdef GLYPH_DEBUG
15425 *w->desired_matrix->method = 0;
15426 #endif
15428 /* Make sure that both W's markers are valid. */
15429 eassert (XMARKER (w->start)->buffer == buffer);
15430 eassert (XMARKER (w->pointm)->buffer == buffer);
15432 restart:
15433 reconsider_clip_changes (w);
15434 frame_line_height = default_line_pixel_height (w);
15436 /* Has the mode line to be updated? */
15437 update_mode_line = (w->update_mode_line
15438 || update_mode_lines
15439 || buffer->clip_changed
15440 || buffer->prevent_redisplay_optimizations_p);
15442 if (MINI_WINDOW_P (w))
15444 if (w == XWINDOW (echo_area_window)
15445 && !NILP (echo_area_buffer[0]))
15447 if (update_mode_line)
15448 /* We may have to update a tty frame's menu bar or a
15449 tool-bar. Example `M-x C-h C-h C-g'. */
15450 goto finish_menu_bars;
15451 else
15452 /* We've already displayed the echo area glyphs in this window. */
15453 goto finish_scroll_bars;
15455 else if ((w != XWINDOW (minibuf_window)
15456 || minibuf_level == 0)
15457 /* When buffer is nonempty, redisplay window normally. */
15458 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15459 /* Quail displays non-mini buffers in minibuffer window.
15460 In that case, redisplay the window normally. */
15461 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15463 /* W is a mini-buffer window, but it's not active, so clear
15464 it. */
15465 int yb = window_text_bottom_y (w);
15466 struct glyph_row *row;
15467 int y;
15469 for (y = 0, row = w->desired_matrix->rows;
15470 y < yb;
15471 y += row->height, ++row)
15472 blank_row (w, row, y);
15473 goto finish_scroll_bars;
15476 clear_glyph_matrix (w->desired_matrix);
15479 /* Otherwise set up data on this window; select its buffer and point
15480 value. */
15481 /* Really select the buffer, for the sake of buffer-local
15482 variables. */
15483 set_buffer_internal_1 (XBUFFER (w->contents));
15485 current_matrix_up_to_date_p
15486 = (w->window_end_valid
15487 && !current_buffer->clip_changed
15488 && !current_buffer->prevent_redisplay_optimizations_p
15489 && !window_outdated (w));
15491 /* Run the window-bottom-change-functions
15492 if it is possible that the text on the screen has changed
15493 (either due to modification of the text, or any other reason). */
15494 if (!current_matrix_up_to_date_p
15495 && !NILP (Vwindow_text_change_functions))
15497 safe_run_hooks (Qwindow_text_change_functions);
15498 goto restart;
15501 beg_unchanged = BEG_UNCHANGED;
15502 end_unchanged = END_UNCHANGED;
15504 SET_TEXT_POS (opoint, PT, PT_BYTE);
15506 specbind (Qinhibit_point_motion_hooks, Qt);
15508 buffer_unchanged_p
15509 = (w->window_end_valid
15510 && !current_buffer->clip_changed
15511 && !window_outdated (w));
15513 /* When windows_or_buffers_changed is non-zero, we can't rely
15514 on the window end being valid, so set it to zero there. */
15515 if (windows_or_buffers_changed)
15517 /* If window starts on a continuation line, maybe adjust the
15518 window start in case the window's width changed. */
15519 if (XMARKER (w->start)->buffer == current_buffer)
15520 compute_window_start_on_continuation_line (w);
15522 w->window_end_valid = 0;
15523 /* If so, we also can't rely on current matrix
15524 and should not fool try_cursor_movement below. */
15525 current_matrix_up_to_date_p = 0;
15528 /* Some sanity checks. */
15529 CHECK_WINDOW_END (w);
15530 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15531 emacs_abort ();
15532 if (BYTEPOS (opoint) < CHARPOS (opoint))
15533 emacs_abort ();
15535 if (mode_line_update_needed (w))
15536 update_mode_line = 1;
15538 /* Point refers normally to the selected window. For any other
15539 window, set up appropriate value. */
15540 if (!EQ (window, selected_window))
15542 ptrdiff_t new_pt = marker_position (w->pointm);
15543 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15544 if (new_pt < BEGV)
15546 new_pt = BEGV;
15547 new_pt_byte = BEGV_BYTE;
15548 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15550 else if (new_pt > (ZV - 1))
15552 new_pt = ZV;
15553 new_pt_byte = ZV_BYTE;
15554 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15557 /* We don't use SET_PT so that the point-motion hooks don't run. */
15558 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15561 /* If any of the character widths specified in the display table
15562 have changed, invalidate the width run cache. It's true that
15563 this may be a bit late to catch such changes, but the rest of
15564 redisplay goes (non-fatally) haywire when the display table is
15565 changed, so why should we worry about doing any better? */
15566 if (current_buffer->width_run_cache)
15568 struct Lisp_Char_Table *disptab = buffer_display_table ();
15570 if (! disptab_matches_widthtab
15571 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15573 invalidate_region_cache (current_buffer,
15574 current_buffer->width_run_cache,
15575 BEG, Z);
15576 recompute_width_table (current_buffer, disptab);
15580 /* If window-start is screwed up, choose a new one. */
15581 if (XMARKER (w->start)->buffer != current_buffer)
15582 goto recenter;
15584 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15586 /* If someone specified a new starting point but did not insist,
15587 check whether it can be used. */
15588 if (w->optional_new_start
15589 && CHARPOS (startp) >= BEGV
15590 && CHARPOS (startp) <= ZV)
15592 w->optional_new_start = 0;
15593 start_display (&it, w, startp);
15594 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15595 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15596 if (IT_CHARPOS (it) == PT)
15597 w->force_start = 1;
15598 /* IT may overshoot PT if text at PT is invisible. */
15599 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15600 w->force_start = 1;
15603 force_start:
15605 /* Handle case where place to start displaying has been specified,
15606 unless the specified location is outside the accessible range. */
15607 if (w->force_start || window_frozen_p (w))
15609 /* We set this later on if we have to adjust point. */
15610 int new_vpos = -1;
15612 w->force_start = 0;
15613 w->vscroll = 0;
15614 w->window_end_valid = 0;
15616 /* Forget any recorded base line for line number display. */
15617 if (!buffer_unchanged_p)
15618 w->base_line_number = 0;
15620 /* Redisplay the mode line. Select the buffer properly for that.
15621 Also, run the hook window-scroll-functions
15622 because we have scrolled. */
15623 /* Note, we do this after clearing force_start because
15624 if there's an error, it is better to forget about force_start
15625 than to get into an infinite loop calling the hook functions
15626 and having them get more errors. */
15627 if (!update_mode_line
15628 || ! NILP (Vwindow_scroll_functions))
15630 update_mode_line = 1;
15631 w->update_mode_line = 1;
15632 startp = run_window_scroll_functions (window, startp);
15635 if (CHARPOS (startp) < BEGV)
15636 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15637 else if (CHARPOS (startp) > ZV)
15638 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15640 /* Redisplay, then check if cursor has been set during the
15641 redisplay. Give up if new fonts were loaded. */
15642 /* We used to issue a CHECK_MARGINS argument to try_window here,
15643 but this causes scrolling to fail when point begins inside
15644 the scroll margin (bug#148) -- cyd */
15645 if (!try_window (window, startp, 0))
15647 w->force_start = 1;
15648 clear_glyph_matrix (w->desired_matrix);
15649 goto need_larger_matrices;
15652 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15654 /* If point does not appear, try to move point so it does
15655 appear. The desired matrix has been built above, so we
15656 can use it here. */
15657 new_vpos = window_box_height (w) / 2;
15660 if (!cursor_row_fully_visible_p (w, 0, 0))
15662 /* Point does appear, but on a line partly visible at end of window.
15663 Move it back to a fully-visible line. */
15664 new_vpos = window_box_height (w);
15666 else if (w->cursor.vpos >=0)
15668 /* Some people insist on not letting point enter the scroll
15669 margin, even though this part handles windows that didn't
15670 scroll at all. */
15671 int window_total_lines
15672 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15673 int margin = min (scroll_margin, window_total_lines / 4);
15674 int pixel_margin = margin * frame_line_height;
15675 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15677 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15678 below, which finds the row to move point to, advances by
15679 the Y coordinate of the _next_ row, see the definition of
15680 MATRIX_ROW_BOTTOM_Y. */
15681 if (w->cursor.vpos < margin + header_line)
15683 w->cursor.vpos = -1;
15684 clear_glyph_matrix (w->desired_matrix);
15685 goto try_to_scroll;
15687 else
15689 int window_height = window_box_height (w);
15691 if (header_line)
15692 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15693 if (w->cursor.y >= window_height - pixel_margin)
15695 w->cursor.vpos = -1;
15696 clear_glyph_matrix (w->desired_matrix);
15697 goto try_to_scroll;
15702 /* If we need to move point for either of the above reasons,
15703 now actually do it. */
15704 if (new_vpos >= 0)
15706 struct glyph_row *row;
15708 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15709 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15710 ++row;
15712 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15713 MATRIX_ROW_START_BYTEPOS (row));
15715 if (w != XWINDOW (selected_window))
15716 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15717 else if (current_buffer == old)
15718 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15720 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15722 /* If we are highlighting the region, then we just changed
15723 the region, so redisplay to show it. */
15724 if (markpos_of_region () >= 0)
15726 clear_glyph_matrix (w->desired_matrix);
15727 if (!try_window (window, startp, 0))
15728 goto need_larger_matrices;
15732 #ifdef GLYPH_DEBUG
15733 debug_method_add (w, "forced window start");
15734 #endif
15735 goto done;
15738 /* Handle case where text has not changed, only point, and it has
15739 not moved off the frame, and we are not retrying after hscroll.
15740 (current_matrix_up_to_date_p is nonzero when retrying.) */
15741 if (current_matrix_up_to_date_p
15742 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15743 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15745 switch (rc)
15747 case CURSOR_MOVEMENT_SUCCESS:
15748 used_current_matrix_p = 1;
15749 goto done;
15751 case CURSOR_MOVEMENT_MUST_SCROLL:
15752 goto try_to_scroll;
15754 default:
15755 emacs_abort ();
15758 /* If current starting point was originally the beginning of a line
15759 but no longer is, find a new starting point. */
15760 else if (w->start_at_line_beg
15761 && !(CHARPOS (startp) <= BEGV
15762 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15764 #ifdef GLYPH_DEBUG
15765 debug_method_add (w, "recenter 1");
15766 #endif
15767 goto recenter;
15770 /* Try scrolling with try_window_id. Value is > 0 if update has
15771 been done, it is -1 if we know that the same window start will
15772 not work. It is 0 if unsuccessful for some other reason. */
15773 else if ((tem = try_window_id (w)) != 0)
15775 #ifdef GLYPH_DEBUG
15776 debug_method_add (w, "try_window_id %d", tem);
15777 #endif
15779 if (fonts_changed_p)
15780 goto need_larger_matrices;
15781 if (tem > 0)
15782 goto done;
15784 /* Otherwise try_window_id has returned -1 which means that we
15785 don't want the alternative below this comment to execute. */
15787 else if (CHARPOS (startp) >= BEGV
15788 && CHARPOS (startp) <= ZV
15789 && PT >= CHARPOS (startp)
15790 && (CHARPOS (startp) < ZV
15791 /* Avoid starting at end of buffer. */
15792 || CHARPOS (startp) == BEGV
15793 || !window_outdated (w)))
15795 int d1, d2, d3, d4, d5, d6;
15797 /* If first window line is a continuation line, and window start
15798 is inside the modified region, but the first change is before
15799 current window start, we must select a new window start.
15801 However, if this is the result of a down-mouse event (e.g. by
15802 extending the mouse-drag-overlay), we don't want to select a
15803 new window start, since that would change the position under
15804 the mouse, resulting in an unwanted mouse-movement rather
15805 than a simple mouse-click. */
15806 if (!w->start_at_line_beg
15807 && NILP (do_mouse_tracking)
15808 && CHARPOS (startp) > BEGV
15809 && CHARPOS (startp) > BEG + beg_unchanged
15810 && CHARPOS (startp) <= Z - end_unchanged
15811 /* Even if w->start_at_line_beg is nil, a new window may
15812 start at a line_beg, since that's how set_buffer_window
15813 sets it. So, we need to check the return value of
15814 compute_window_start_on_continuation_line. (See also
15815 bug#197). */
15816 && XMARKER (w->start)->buffer == current_buffer
15817 && compute_window_start_on_continuation_line (w)
15818 /* It doesn't make sense to force the window start like we
15819 do at label force_start if it is already known that point
15820 will not be visible in the resulting window, because
15821 doing so will move point from its correct position
15822 instead of scrolling the window to bring point into view.
15823 See bug#9324. */
15824 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15826 w->force_start = 1;
15827 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15828 goto force_start;
15831 #ifdef GLYPH_DEBUG
15832 debug_method_add (w, "same window start");
15833 #endif
15835 /* Try to redisplay starting at same place as before.
15836 If point has not moved off frame, accept the results. */
15837 if (!current_matrix_up_to_date_p
15838 /* Don't use try_window_reusing_current_matrix in this case
15839 because a window scroll function can have changed the
15840 buffer. */
15841 || !NILP (Vwindow_scroll_functions)
15842 || MINI_WINDOW_P (w)
15843 || !(used_current_matrix_p
15844 = try_window_reusing_current_matrix (w)))
15846 IF_DEBUG (debug_method_add (w, "1"));
15847 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15848 /* -1 means we need to scroll.
15849 0 means we need new matrices, but fonts_changed_p
15850 is set in that case, so we will detect it below. */
15851 goto try_to_scroll;
15854 if (fonts_changed_p)
15855 goto need_larger_matrices;
15857 if (w->cursor.vpos >= 0)
15859 if (!just_this_one_p
15860 || current_buffer->clip_changed
15861 || BEG_UNCHANGED < CHARPOS (startp))
15862 /* Forget any recorded base line for line number display. */
15863 w->base_line_number = 0;
15865 if (!cursor_row_fully_visible_p (w, 1, 0))
15867 clear_glyph_matrix (w->desired_matrix);
15868 last_line_misfit = 1;
15870 /* Drop through and scroll. */
15871 else
15872 goto done;
15874 else
15875 clear_glyph_matrix (w->desired_matrix);
15878 try_to_scroll:
15880 /* Redisplay the mode line. Select the buffer properly for that. */
15881 if (!update_mode_line)
15883 update_mode_line = 1;
15884 w->update_mode_line = 1;
15887 /* Try to scroll by specified few lines. */
15888 if ((scroll_conservatively
15889 || emacs_scroll_step
15890 || temp_scroll_step
15891 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15892 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15893 && CHARPOS (startp) >= BEGV
15894 && CHARPOS (startp) <= ZV)
15896 /* The function returns -1 if new fonts were loaded, 1 if
15897 successful, 0 if not successful. */
15898 int ss = try_scrolling (window, just_this_one_p,
15899 scroll_conservatively,
15900 emacs_scroll_step,
15901 temp_scroll_step, last_line_misfit);
15902 switch (ss)
15904 case SCROLLING_SUCCESS:
15905 goto done;
15907 case SCROLLING_NEED_LARGER_MATRICES:
15908 goto need_larger_matrices;
15910 case SCROLLING_FAILED:
15911 break;
15913 default:
15914 emacs_abort ();
15918 /* Finally, just choose a place to start which positions point
15919 according to user preferences. */
15921 recenter:
15923 #ifdef GLYPH_DEBUG
15924 debug_method_add (w, "recenter");
15925 #endif
15927 /* Forget any previously recorded base line for line number display. */
15928 if (!buffer_unchanged_p)
15929 w->base_line_number = 0;
15931 /* Determine the window start relative to point. */
15932 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15933 it.current_y = it.last_visible_y;
15934 if (centering_position < 0)
15936 int window_total_lines
15937 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15938 int margin =
15939 scroll_margin > 0
15940 ? min (scroll_margin, window_total_lines / 4)
15941 : 0;
15942 ptrdiff_t margin_pos = CHARPOS (startp);
15943 Lisp_Object aggressive;
15944 int scrolling_up;
15946 /* If there is a scroll margin at the top of the window, find
15947 its character position. */
15948 if (margin
15949 /* Cannot call start_display if startp is not in the
15950 accessible region of the buffer. This can happen when we
15951 have just switched to a different buffer and/or changed
15952 its restriction. In that case, startp is initialized to
15953 the character position 1 (BEGV) because we did not yet
15954 have chance to display the buffer even once. */
15955 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15957 struct it it1;
15958 void *it1data = NULL;
15960 SAVE_IT (it1, it, it1data);
15961 start_display (&it1, w, startp);
15962 move_it_vertically (&it1, margin * frame_line_height);
15963 margin_pos = IT_CHARPOS (it1);
15964 RESTORE_IT (&it, &it, it1data);
15966 scrolling_up = PT > margin_pos;
15967 aggressive =
15968 scrolling_up
15969 ? BVAR (current_buffer, scroll_up_aggressively)
15970 : BVAR (current_buffer, scroll_down_aggressively);
15972 if (!MINI_WINDOW_P (w)
15973 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15975 int pt_offset = 0;
15977 /* Setting scroll-conservatively overrides
15978 scroll-*-aggressively. */
15979 if (!scroll_conservatively && NUMBERP (aggressive))
15981 double float_amount = XFLOATINT (aggressive);
15983 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15984 if (pt_offset == 0 && float_amount > 0)
15985 pt_offset = 1;
15986 if (pt_offset && margin > 0)
15987 margin -= 1;
15989 /* Compute how much to move the window start backward from
15990 point so that point will be displayed where the user
15991 wants it. */
15992 if (scrolling_up)
15994 centering_position = it.last_visible_y;
15995 if (pt_offset)
15996 centering_position -= pt_offset;
15997 centering_position -=
15998 frame_line_height * (1 + margin + (last_line_misfit != 0))
15999 + WINDOW_HEADER_LINE_HEIGHT (w);
16000 /* Don't let point enter the scroll margin near top of
16001 the window. */
16002 if (centering_position < margin * frame_line_height)
16003 centering_position = margin * frame_line_height;
16005 else
16006 centering_position = margin * frame_line_height + pt_offset;
16008 else
16009 /* Set the window start half the height of the window backward
16010 from point. */
16011 centering_position = window_box_height (w) / 2;
16013 move_it_vertically_backward (&it, centering_position);
16015 eassert (IT_CHARPOS (it) >= BEGV);
16017 /* The function move_it_vertically_backward may move over more
16018 than the specified y-distance. If it->w is small, e.g. a
16019 mini-buffer window, we may end up in front of the window's
16020 display area. Start displaying at the start of the line
16021 containing PT in this case. */
16022 if (it.current_y <= 0)
16024 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16025 move_it_vertically_backward (&it, 0);
16026 it.current_y = 0;
16029 it.current_x = it.hpos = 0;
16031 /* Set the window start position here explicitly, to avoid an
16032 infinite loop in case the functions in window-scroll-functions
16033 get errors. */
16034 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16036 /* Run scroll hooks. */
16037 startp = run_window_scroll_functions (window, it.current.pos);
16039 /* Redisplay the window. */
16040 if (!current_matrix_up_to_date_p
16041 || windows_or_buffers_changed
16042 || cursor_type_changed
16043 /* Don't use try_window_reusing_current_matrix in this case
16044 because it can have changed the buffer. */
16045 || !NILP (Vwindow_scroll_functions)
16046 || !just_this_one_p
16047 || MINI_WINDOW_P (w)
16048 || !(used_current_matrix_p
16049 = try_window_reusing_current_matrix (w)))
16050 try_window (window, startp, 0);
16052 /* If new fonts have been loaded (due to fontsets), give up. We
16053 have to start a new redisplay since we need to re-adjust glyph
16054 matrices. */
16055 if (fonts_changed_p)
16056 goto need_larger_matrices;
16058 /* If cursor did not appear assume that the middle of the window is
16059 in the first line of the window. Do it again with the next line.
16060 (Imagine a window of height 100, displaying two lines of height
16061 60. Moving back 50 from it->last_visible_y will end in the first
16062 line.) */
16063 if (w->cursor.vpos < 0)
16065 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16067 clear_glyph_matrix (w->desired_matrix);
16068 move_it_by_lines (&it, 1);
16069 try_window (window, it.current.pos, 0);
16071 else if (PT < IT_CHARPOS (it))
16073 clear_glyph_matrix (w->desired_matrix);
16074 move_it_by_lines (&it, -1);
16075 try_window (window, it.current.pos, 0);
16077 else
16079 /* Not much we can do about it. */
16083 /* Consider the following case: Window starts at BEGV, there is
16084 invisible, intangible text at BEGV, so that display starts at
16085 some point START > BEGV. It can happen that we are called with
16086 PT somewhere between BEGV and START. Try to handle that case. */
16087 if (w->cursor.vpos < 0)
16089 struct glyph_row *row = w->current_matrix->rows;
16090 if (row->mode_line_p)
16091 ++row;
16092 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16095 if (!cursor_row_fully_visible_p (w, 0, 0))
16097 /* If vscroll is enabled, disable it and try again. */
16098 if (w->vscroll)
16100 w->vscroll = 0;
16101 clear_glyph_matrix (w->desired_matrix);
16102 goto recenter;
16105 /* Users who set scroll-conservatively to a large number want
16106 point just above/below the scroll margin. If we ended up
16107 with point's row partially visible, move the window start to
16108 make that row fully visible and out of the margin. */
16109 if (scroll_conservatively > SCROLL_LIMIT)
16111 int window_total_lines
16112 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16113 int margin =
16114 scroll_margin > 0
16115 ? min (scroll_margin, window_total_lines / 4)
16116 : 0;
16117 int move_down = w->cursor.vpos >= window_total_lines / 2;
16119 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16120 clear_glyph_matrix (w->desired_matrix);
16121 if (1 == try_window (window, it.current.pos,
16122 TRY_WINDOW_CHECK_MARGINS))
16123 goto done;
16126 /* If centering point failed to make the whole line visible,
16127 put point at the top instead. That has to make the whole line
16128 visible, if it can be done. */
16129 if (centering_position == 0)
16130 goto done;
16132 clear_glyph_matrix (w->desired_matrix);
16133 centering_position = 0;
16134 goto recenter;
16137 done:
16139 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16140 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16141 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16143 /* Display the mode line, if we must. */
16144 if ((update_mode_line
16145 /* If window not full width, must redo its mode line
16146 if (a) the window to its side is being redone and
16147 (b) we do a frame-based redisplay. This is a consequence
16148 of how inverted lines are drawn in frame-based redisplay. */
16149 || (!just_this_one_p
16150 && !FRAME_WINDOW_P (f)
16151 && !WINDOW_FULL_WIDTH_P (w))
16152 /* Line number to display. */
16153 || w->base_line_pos > 0
16154 /* Column number is displayed and different from the one displayed. */
16155 || (w->column_number_displayed != -1
16156 && (w->column_number_displayed != current_column ())))
16157 /* This means that the window has a mode line. */
16158 && (WINDOW_WANTS_MODELINE_P (w)
16159 || WINDOW_WANTS_HEADER_LINE_P (w)))
16161 display_mode_lines (w);
16163 /* If mode line height has changed, arrange for a thorough
16164 immediate redisplay using the correct mode line height. */
16165 if (WINDOW_WANTS_MODELINE_P (w)
16166 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16168 fonts_changed_p = 1;
16169 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16170 = DESIRED_MODE_LINE_HEIGHT (w);
16173 /* If header line height has changed, arrange for a thorough
16174 immediate redisplay using the correct header line height. */
16175 if (WINDOW_WANTS_HEADER_LINE_P (w)
16176 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16178 fonts_changed_p = 1;
16179 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16180 = DESIRED_HEADER_LINE_HEIGHT (w);
16183 if (fonts_changed_p)
16184 goto need_larger_matrices;
16187 if (!line_number_displayed && w->base_line_pos != -1)
16189 w->base_line_pos = 0;
16190 w->base_line_number = 0;
16193 finish_menu_bars:
16195 /* When we reach a frame's selected window, redo the frame's menu bar. */
16196 if (update_mode_line
16197 && EQ (FRAME_SELECTED_WINDOW (f), window))
16199 int redisplay_menu_p = 0;
16201 if (FRAME_WINDOW_P (f))
16203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16204 || defined (HAVE_NS) || defined (USE_GTK)
16205 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16206 #else
16207 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16208 #endif
16210 else
16211 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16213 if (redisplay_menu_p)
16214 display_menu_bar (w);
16216 #ifdef HAVE_WINDOW_SYSTEM
16217 if (FRAME_WINDOW_P (f))
16219 #if defined (USE_GTK) || defined (HAVE_NS)
16220 if (FRAME_EXTERNAL_TOOL_BAR (f))
16221 redisplay_tool_bar (f);
16222 #else
16223 if (WINDOWP (f->tool_bar_window)
16224 && (FRAME_TOOL_BAR_LINES (f) > 0
16225 || !NILP (Vauto_resize_tool_bars))
16226 && redisplay_tool_bar (f))
16227 ignore_mouse_drag_p = 1;
16228 #endif
16230 #endif
16233 #ifdef HAVE_WINDOW_SYSTEM
16234 if (FRAME_WINDOW_P (f)
16235 && update_window_fringes (w, (just_this_one_p
16236 || (!used_current_matrix_p && !overlay_arrow_seen)
16237 || w->pseudo_window_p)))
16239 update_begin (f);
16240 block_input ();
16241 if (draw_window_fringes (w, 1))
16242 x_draw_vertical_border (w);
16243 unblock_input ();
16244 update_end (f);
16246 #endif /* HAVE_WINDOW_SYSTEM */
16248 /* We go to this label, with fonts_changed_p set,
16249 if it is necessary to try again using larger glyph matrices.
16250 We have to redeem the scroll bar even in this case,
16251 because the loop in redisplay_internal expects that. */
16252 need_larger_matrices:
16254 finish_scroll_bars:
16256 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16258 /* Set the thumb's position and size. */
16259 set_vertical_scroll_bar (w);
16261 /* Note that we actually used the scroll bar attached to this
16262 window, so it shouldn't be deleted at the end of redisplay. */
16263 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16264 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16267 /* Restore current_buffer and value of point in it. The window
16268 update may have changed the buffer, so first make sure `opoint'
16269 is still valid (Bug#6177). */
16270 if (CHARPOS (opoint) < BEGV)
16271 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16272 else if (CHARPOS (opoint) > ZV)
16273 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16274 else
16275 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16277 set_buffer_internal_1 (old);
16278 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16279 shorter. This can be caused by log truncation in *Messages*. */
16280 if (CHARPOS (lpoint) <= ZV)
16281 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16283 unbind_to (count, Qnil);
16287 /* Build the complete desired matrix of WINDOW with a window start
16288 buffer position POS.
16290 Value is 1 if successful. It is zero if fonts were loaded during
16291 redisplay which makes re-adjusting glyph matrices necessary, and -1
16292 if point would appear in the scroll margins.
16293 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16294 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16295 set in FLAGS.) */
16298 try_window (Lisp_Object window, struct text_pos pos, int flags)
16300 struct window *w = XWINDOW (window);
16301 struct it it;
16302 struct glyph_row *last_text_row = NULL;
16303 struct frame *f = XFRAME (w->frame);
16304 int frame_line_height = default_line_pixel_height (w);
16306 /* Make POS the new window start. */
16307 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16309 /* Mark cursor position as unknown. No overlay arrow seen. */
16310 w->cursor.vpos = -1;
16311 overlay_arrow_seen = 0;
16313 /* Initialize iterator and info to start at POS. */
16314 start_display (&it, w, pos);
16316 /* Display all lines of W. */
16317 while (it.current_y < it.last_visible_y)
16319 if (display_line (&it))
16320 last_text_row = it.glyph_row - 1;
16321 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16322 return 0;
16325 /* Don't let the cursor end in the scroll margins. */
16326 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16327 && !MINI_WINDOW_P (w))
16329 int this_scroll_margin;
16330 int window_total_lines
16331 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16333 if (scroll_margin > 0)
16335 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16336 this_scroll_margin *= frame_line_height;
16338 else
16339 this_scroll_margin = 0;
16341 if ((w->cursor.y >= 0 /* not vscrolled */
16342 && w->cursor.y < this_scroll_margin
16343 && CHARPOS (pos) > BEGV
16344 && IT_CHARPOS (it) < ZV)
16345 /* rms: considering make_cursor_line_fully_visible_p here
16346 seems to give wrong results. We don't want to recenter
16347 when the last line is partly visible, we want to allow
16348 that case to be handled in the usual way. */
16349 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16351 w->cursor.vpos = -1;
16352 clear_glyph_matrix (w->desired_matrix);
16353 return -1;
16357 /* If bottom moved off end of frame, change mode line percentage. */
16358 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16359 w->update_mode_line = 1;
16361 /* Set window_end_pos to the offset of the last character displayed
16362 on the window from the end of current_buffer. Set
16363 window_end_vpos to its row number. */
16364 if (last_text_row)
16366 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16367 adjust_window_ends (w, last_text_row, 0);
16368 eassert
16369 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16370 w->window_end_vpos)));
16372 else
16374 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16375 w->window_end_pos = Z - ZV;
16376 w->window_end_vpos = 0;
16379 /* But that is not valid info until redisplay finishes. */
16380 w->window_end_valid = 0;
16381 return 1;
16386 /************************************************************************
16387 Window redisplay reusing current matrix when buffer has not changed
16388 ************************************************************************/
16390 /* Try redisplay of window W showing an unchanged buffer with a
16391 different window start than the last time it was displayed by
16392 reusing its current matrix. Value is non-zero if successful.
16393 W->start is the new window start. */
16395 static int
16396 try_window_reusing_current_matrix (struct window *w)
16398 struct frame *f = XFRAME (w->frame);
16399 struct glyph_row *bottom_row;
16400 struct it it;
16401 struct run run;
16402 struct text_pos start, new_start;
16403 int nrows_scrolled, i;
16404 struct glyph_row *last_text_row;
16405 struct glyph_row *last_reused_text_row;
16406 struct glyph_row *start_row;
16407 int start_vpos, min_y, max_y;
16409 #ifdef GLYPH_DEBUG
16410 if (inhibit_try_window_reusing)
16411 return 0;
16412 #endif
16414 if (/* This function doesn't handle terminal frames. */
16415 !FRAME_WINDOW_P (f)
16416 /* Don't try to reuse the display if windows have been split
16417 or such. */
16418 || windows_or_buffers_changed
16419 || cursor_type_changed)
16420 return 0;
16422 /* Can't do this if region may have changed. */
16423 if (markpos_of_region () >= 0
16424 || w->region_showing
16425 || !NILP (Vshow_trailing_whitespace))
16426 return 0;
16428 /* If top-line visibility has changed, give up. */
16429 if (WINDOW_WANTS_HEADER_LINE_P (w)
16430 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16431 return 0;
16433 /* Give up if old or new display is scrolled vertically. We could
16434 make this function handle this, but right now it doesn't. */
16435 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16436 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16437 return 0;
16439 /* The variable new_start now holds the new window start. The old
16440 start `start' can be determined from the current matrix. */
16441 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16442 start = start_row->minpos;
16443 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16445 /* Clear the desired matrix for the display below. */
16446 clear_glyph_matrix (w->desired_matrix);
16448 if (CHARPOS (new_start) <= CHARPOS (start))
16450 /* Don't use this method if the display starts with an ellipsis
16451 displayed for invisible text. It's not easy to handle that case
16452 below, and it's certainly not worth the effort since this is
16453 not a frequent case. */
16454 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16455 return 0;
16457 IF_DEBUG (debug_method_add (w, "twu1"));
16459 /* Display up to a row that can be reused. The variable
16460 last_text_row is set to the last row displayed that displays
16461 text. Note that it.vpos == 0 if or if not there is a
16462 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16463 start_display (&it, w, new_start);
16464 w->cursor.vpos = -1;
16465 last_text_row = last_reused_text_row = NULL;
16467 while (it.current_y < it.last_visible_y
16468 && !fonts_changed_p)
16470 /* If we have reached into the characters in the START row,
16471 that means the line boundaries have changed. So we
16472 can't start copying with the row START. Maybe it will
16473 work to start copying with the following row. */
16474 while (IT_CHARPOS (it) > CHARPOS (start))
16476 /* Advance to the next row as the "start". */
16477 start_row++;
16478 start = start_row->minpos;
16479 /* If there are no more rows to try, or just one, give up. */
16480 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16481 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16482 || CHARPOS (start) == ZV)
16484 clear_glyph_matrix (w->desired_matrix);
16485 return 0;
16488 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16490 /* If we have reached alignment, we can copy the rest of the
16491 rows. */
16492 if (IT_CHARPOS (it) == CHARPOS (start)
16493 /* Don't accept "alignment" inside a display vector,
16494 since start_row could have started in the middle of
16495 that same display vector (thus their character
16496 positions match), and we have no way of telling if
16497 that is the case. */
16498 && it.current.dpvec_index < 0)
16499 break;
16501 if (display_line (&it))
16502 last_text_row = it.glyph_row - 1;
16506 /* A value of current_y < last_visible_y means that we stopped
16507 at the previous window start, which in turn means that we
16508 have at least one reusable row. */
16509 if (it.current_y < it.last_visible_y)
16511 struct glyph_row *row;
16513 /* IT.vpos always starts from 0; it counts text lines. */
16514 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16516 /* Find PT if not already found in the lines displayed. */
16517 if (w->cursor.vpos < 0)
16519 int dy = it.current_y - start_row->y;
16521 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16522 row = row_containing_pos (w, PT, row, NULL, dy);
16523 if (row)
16524 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16525 dy, nrows_scrolled);
16526 else
16528 clear_glyph_matrix (w->desired_matrix);
16529 return 0;
16533 /* Scroll the display. Do it before the current matrix is
16534 changed. The problem here is that update has not yet
16535 run, i.e. part of the current matrix is not up to date.
16536 scroll_run_hook will clear the cursor, and use the
16537 current matrix to get the height of the row the cursor is
16538 in. */
16539 run.current_y = start_row->y;
16540 run.desired_y = it.current_y;
16541 run.height = it.last_visible_y - it.current_y;
16543 if (run.height > 0 && run.current_y != run.desired_y)
16545 update_begin (f);
16546 FRAME_RIF (f)->update_window_begin_hook (w);
16547 FRAME_RIF (f)->clear_window_mouse_face (w);
16548 FRAME_RIF (f)->scroll_run_hook (w, &run);
16549 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16550 update_end (f);
16553 /* Shift current matrix down by nrows_scrolled lines. */
16554 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16555 rotate_matrix (w->current_matrix,
16556 start_vpos,
16557 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16558 nrows_scrolled);
16560 /* Disable lines that must be updated. */
16561 for (i = 0; i < nrows_scrolled; ++i)
16562 (start_row + i)->enabled_p = 0;
16564 /* Re-compute Y positions. */
16565 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16566 max_y = it.last_visible_y;
16567 for (row = start_row + nrows_scrolled;
16568 row < bottom_row;
16569 ++row)
16571 row->y = it.current_y;
16572 row->visible_height = row->height;
16574 if (row->y < min_y)
16575 row->visible_height -= min_y - row->y;
16576 if (row->y + row->height > max_y)
16577 row->visible_height -= row->y + row->height - max_y;
16578 if (row->fringe_bitmap_periodic_p)
16579 row->redraw_fringe_bitmaps_p = 1;
16581 it.current_y += row->height;
16583 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16584 last_reused_text_row = row;
16585 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16586 break;
16589 /* Disable lines in the current matrix which are now
16590 below the window. */
16591 for (++row; row < bottom_row; ++row)
16592 row->enabled_p = row->mode_line_p = 0;
16595 /* Update window_end_pos etc.; last_reused_text_row is the last
16596 reused row from the current matrix containing text, if any.
16597 The value of last_text_row is the last displayed line
16598 containing text. */
16599 if (last_reused_text_row)
16600 adjust_window_ends (w, last_reused_text_row, 1);
16601 else if (last_text_row)
16602 adjust_window_ends (w, last_text_row, 0);
16603 else
16605 /* This window must be completely empty. */
16606 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16607 w->window_end_pos = Z - ZV;
16608 w->window_end_vpos = 0;
16610 w->window_end_valid = 0;
16612 /* Update hint: don't try scrolling again in update_window. */
16613 w->desired_matrix->no_scrolling_p = 1;
16615 #ifdef GLYPH_DEBUG
16616 debug_method_add (w, "try_window_reusing_current_matrix 1");
16617 #endif
16618 return 1;
16620 else if (CHARPOS (new_start) > CHARPOS (start))
16622 struct glyph_row *pt_row, *row;
16623 struct glyph_row *first_reusable_row;
16624 struct glyph_row *first_row_to_display;
16625 int dy;
16626 int yb = window_text_bottom_y (w);
16628 /* Find the row starting at new_start, if there is one. Don't
16629 reuse a partially visible line at the end. */
16630 first_reusable_row = start_row;
16631 while (first_reusable_row->enabled_p
16632 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16633 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16634 < CHARPOS (new_start)))
16635 ++first_reusable_row;
16637 /* Give up if there is no row to reuse. */
16638 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16639 || !first_reusable_row->enabled_p
16640 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16641 != CHARPOS (new_start)))
16642 return 0;
16644 /* We can reuse fully visible rows beginning with
16645 first_reusable_row to the end of the window. Set
16646 first_row_to_display to the first row that cannot be reused.
16647 Set pt_row to the row containing point, if there is any. */
16648 pt_row = NULL;
16649 for (first_row_to_display = first_reusable_row;
16650 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16651 ++first_row_to_display)
16653 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16654 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16655 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16656 && first_row_to_display->ends_at_zv_p
16657 && pt_row == NULL)))
16658 pt_row = first_row_to_display;
16661 /* Start displaying at the start of first_row_to_display. */
16662 eassert (first_row_to_display->y < yb);
16663 init_to_row_start (&it, w, first_row_to_display);
16665 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16666 - start_vpos);
16667 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16668 - nrows_scrolled);
16669 it.current_y = (first_row_to_display->y - first_reusable_row->y
16670 + WINDOW_HEADER_LINE_HEIGHT (w));
16672 /* Display lines beginning with first_row_to_display in the
16673 desired matrix. Set last_text_row to the last row displayed
16674 that displays text. */
16675 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16676 if (pt_row == NULL)
16677 w->cursor.vpos = -1;
16678 last_text_row = NULL;
16679 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16680 if (display_line (&it))
16681 last_text_row = it.glyph_row - 1;
16683 /* If point is in a reused row, adjust y and vpos of the cursor
16684 position. */
16685 if (pt_row)
16687 w->cursor.vpos -= nrows_scrolled;
16688 w->cursor.y -= first_reusable_row->y - start_row->y;
16691 /* Give up if point isn't in a row displayed or reused. (This
16692 also handles the case where w->cursor.vpos < nrows_scrolled
16693 after the calls to display_line, which can happen with scroll
16694 margins. See bug#1295.) */
16695 if (w->cursor.vpos < 0)
16697 clear_glyph_matrix (w->desired_matrix);
16698 return 0;
16701 /* Scroll the display. */
16702 run.current_y = first_reusable_row->y;
16703 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16704 run.height = it.last_visible_y - run.current_y;
16705 dy = run.current_y - run.desired_y;
16707 if (run.height)
16709 update_begin (f);
16710 FRAME_RIF (f)->update_window_begin_hook (w);
16711 FRAME_RIF (f)->clear_window_mouse_face (w);
16712 FRAME_RIF (f)->scroll_run_hook (w, &run);
16713 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16714 update_end (f);
16717 /* Adjust Y positions of reused rows. */
16718 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16719 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16720 max_y = it.last_visible_y;
16721 for (row = first_reusable_row; row < first_row_to_display; ++row)
16723 row->y -= dy;
16724 row->visible_height = row->height;
16725 if (row->y < min_y)
16726 row->visible_height -= min_y - row->y;
16727 if (row->y + row->height > max_y)
16728 row->visible_height -= row->y + row->height - max_y;
16729 if (row->fringe_bitmap_periodic_p)
16730 row->redraw_fringe_bitmaps_p = 1;
16733 /* Scroll the current matrix. */
16734 eassert (nrows_scrolled > 0);
16735 rotate_matrix (w->current_matrix,
16736 start_vpos,
16737 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16738 -nrows_scrolled);
16740 /* Disable rows not reused. */
16741 for (row -= nrows_scrolled; row < bottom_row; ++row)
16742 row->enabled_p = 0;
16744 /* Point may have moved to a different line, so we cannot assume that
16745 the previous cursor position is valid; locate the correct row. */
16746 if (pt_row)
16748 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16749 row < bottom_row
16750 && PT >= MATRIX_ROW_END_CHARPOS (row)
16751 && !row->ends_at_zv_p;
16752 row++)
16754 w->cursor.vpos++;
16755 w->cursor.y = row->y;
16757 if (row < bottom_row)
16759 /* Can't simply scan the row for point with
16760 bidi-reordered glyph rows. Let set_cursor_from_row
16761 figure out where to put the cursor, and if it fails,
16762 give up. */
16763 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16765 if (!set_cursor_from_row (w, row, w->current_matrix,
16766 0, 0, 0, 0))
16768 clear_glyph_matrix (w->desired_matrix);
16769 return 0;
16772 else
16774 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16775 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16777 for (; glyph < end
16778 && (!BUFFERP (glyph->object)
16779 || glyph->charpos < PT);
16780 glyph++)
16782 w->cursor.hpos++;
16783 w->cursor.x += glyph->pixel_width;
16789 /* Adjust window end. A null value of last_text_row means that
16790 the window end is in reused rows which in turn means that
16791 only its vpos can have changed. */
16792 if (last_text_row)
16793 adjust_window_ends (w, last_text_row, 0);
16794 else
16795 w->window_end_vpos -= nrows_scrolled;
16797 w->window_end_valid = 0;
16798 w->desired_matrix->no_scrolling_p = 1;
16800 #ifdef GLYPH_DEBUG
16801 debug_method_add (w, "try_window_reusing_current_matrix 2");
16802 #endif
16803 return 1;
16806 return 0;
16811 /************************************************************************
16812 Window redisplay reusing current matrix when buffer has changed
16813 ************************************************************************/
16815 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16816 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16817 ptrdiff_t *, ptrdiff_t *);
16818 static struct glyph_row *
16819 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16820 struct glyph_row *);
16823 /* Return the last row in MATRIX displaying text. If row START is
16824 non-null, start searching with that row. IT gives the dimensions
16825 of the display. Value is null if matrix is empty; otherwise it is
16826 a pointer to the row found. */
16828 static struct glyph_row *
16829 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16830 struct glyph_row *start)
16832 struct glyph_row *row, *row_found;
16834 /* Set row_found to the last row in IT->w's current matrix
16835 displaying text. The loop looks funny but think of partially
16836 visible lines. */
16837 row_found = NULL;
16838 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16839 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16841 eassert (row->enabled_p);
16842 row_found = row;
16843 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16844 break;
16845 ++row;
16848 return row_found;
16852 /* Return the last row in the current matrix of W that is not affected
16853 by changes at the start of current_buffer that occurred since W's
16854 current matrix was built. Value is null if no such row exists.
16856 BEG_UNCHANGED us the number of characters unchanged at the start of
16857 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16858 first changed character in current_buffer. Characters at positions <
16859 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16860 when the current matrix was built. */
16862 static struct glyph_row *
16863 find_last_unchanged_at_beg_row (struct window *w)
16865 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16866 struct glyph_row *row;
16867 struct glyph_row *row_found = NULL;
16868 int yb = window_text_bottom_y (w);
16870 /* Find the last row displaying unchanged text. */
16871 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16872 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16873 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16874 ++row)
16876 if (/* If row ends before first_changed_pos, it is unchanged,
16877 except in some case. */
16878 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16879 /* When row ends in ZV and we write at ZV it is not
16880 unchanged. */
16881 && !row->ends_at_zv_p
16882 /* When first_changed_pos is the end of a continued line,
16883 row is not unchanged because it may be no longer
16884 continued. */
16885 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16886 && (row->continued_p
16887 || row->exact_window_width_line_p))
16888 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16889 needs to be recomputed, so don't consider this row as
16890 unchanged. This happens when the last line was
16891 bidi-reordered and was killed immediately before this
16892 redisplay cycle. In that case, ROW->end stores the
16893 buffer position of the first visual-order character of
16894 the killed text, which is now beyond ZV. */
16895 && CHARPOS (row->end.pos) <= ZV)
16896 row_found = row;
16898 /* Stop if last visible row. */
16899 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16900 break;
16903 return row_found;
16907 /* Find the first glyph row in the current matrix of W that is not
16908 affected by changes at the end of current_buffer since the
16909 time W's current matrix was built.
16911 Return in *DELTA the number of chars by which buffer positions in
16912 unchanged text at the end of current_buffer must be adjusted.
16914 Return in *DELTA_BYTES the corresponding number of bytes.
16916 Value is null if no such row exists, i.e. all rows are affected by
16917 changes. */
16919 static struct glyph_row *
16920 find_first_unchanged_at_end_row (struct window *w,
16921 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16923 struct glyph_row *row;
16924 struct glyph_row *row_found = NULL;
16926 *delta = *delta_bytes = 0;
16928 /* Display must not have been paused, otherwise the current matrix
16929 is not up to date. */
16930 eassert (w->window_end_valid);
16932 /* A value of window_end_pos >= END_UNCHANGED means that the window
16933 end is in the range of changed text. If so, there is no
16934 unchanged row at the end of W's current matrix. */
16935 if (w->window_end_pos >= END_UNCHANGED)
16936 return NULL;
16938 /* Set row to the last row in W's current matrix displaying text. */
16939 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16941 /* If matrix is entirely empty, no unchanged row exists. */
16942 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16944 /* The value of row is the last glyph row in the matrix having a
16945 meaningful buffer position in it. The end position of row
16946 corresponds to window_end_pos. This allows us to translate
16947 buffer positions in the current matrix to current buffer
16948 positions for characters not in changed text. */
16949 ptrdiff_t Z_old =
16950 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16951 ptrdiff_t Z_BYTE_old =
16952 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16953 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16954 struct glyph_row *first_text_row
16955 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16957 *delta = Z - Z_old;
16958 *delta_bytes = Z_BYTE - Z_BYTE_old;
16960 /* Set last_unchanged_pos to the buffer position of the last
16961 character in the buffer that has not been changed. Z is the
16962 index + 1 of the last character in current_buffer, i.e. by
16963 subtracting END_UNCHANGED we get the index of the last
16964 unchanged character, and we have to add BEG to get its buffer
16965 position. */
16966 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16967 last_unchanged_pos_old = last_unchanged_pos - *delta;
16969 /* Search backward from ROW for a row displaying a line that
16970 starts at a minimum position >= last_unchanged_pos_old. */
16971 for (; row > first_text_row; --row)
16973 /* This used to abort, but it can happen.
16974 It is ok to just stop the search instead here. KFS. */
16975 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16976 break;
16978 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16979 row_found = row;
16983 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16985 return row_found;
16989 /* Make sure that glyph rows in the current matrix of window W
16990 reference the same glyph memory as corresponding rows in the
16991 frame's frame matrix. This function is called after scrolling W's
16992 current matrix on a terminal frame in try_window_id and
16993 try_window_reusing_current_matrix. */
16995 static void
16996 sync_frame_with_window_matrix_rows (struct window *w)
16998 struct frame *f = XFRAME (w->frame);
16999 struct glyph_row *window_row, *window_row_end, *frame_row;
17001 /* Preconditions: W must be a leaf window and full-width. Its frame
17002 must have a frame matrix. */
17003 eassert (BUFFERP (w->contents));
17004 eassert (WINDOW_FULL_WIDTH_P (w));
17005 eassert (!FRAME_WINDOW_P (f));
17007 /* If W is a full-width window, glyph pointers in W's current matrix
17008 have, by definition, to be the same as glyph pointers in the
17009 corresponding frame matrix. Note that frame matrices have no
17010 marginal areas (see build_frame_matrix). */
17011 window_row = w->current_matrix->rows;
17012 window_row_end = window_row + w->current_matrix->nrows;
17013 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17014 while (window_row < window_row_end)
17016 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17017 struct glyph *end = window_row->glyphs[LAST_AREA];
17019 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17020 frame_row->glyphs[TEXT_AREA] = start;
17021 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17022 frame_row->glyphs[LAST_AREA] = end;
17024 /* Disable frame rows whose corresponding window rows have
17025 been disabled in try_window_id. */
17026 if (!window_row->enabled_p)
17027 frame_row->enabled_p = 0;
17029 ++window_row, ++frame_row;
17034 /* Find the glyph row in window W containing CHARPOS. Consider all
17035 rows between START and END (not inclusive). END null means search
17036 all rows to the end of the display area of W. Value is the row
17037 containing CHARPOS or null. */
17039 struct glyph_row *
17040 row_containing_pos (struct window *w, ptrdiff_t charpos,
17041 struct glyph_row *start, struct glyph_row *end, int dy)
17043 struct glyph_row *row = start;
17044 struct glyph_row *best_row = NULL;
17045 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17046 int last_y;
17048 /* If we happen to start on a header-line, skip that. */
17049 if (row->mode_line_p)
17050 ++row;
17052 if ((end && row >= end) || !row->enabled_p)
17053 return NULL;
17055 last_y = window_text_bottom_y (w) - dy;
17057 while (1)
17059 /* Give up if we have gone too far. */
17060 if (end && row >= end)
17061 return NULL;
17062 /* This formerly returned if they were equal.
17063 I think that both quantities are of a "last plus one" type;
17064 if so, when they are equal, the row is within the screen. -- rms. */
17065 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17066 return NULL;
17068 /* If it is in this row, return this row. */
17069 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17070 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17071 /* The end position of a row equals the start
17072 position of the next row. If CHARPOS is there, we
17073 would rather consider it displayed in the next
17074 line, except when this line ends in ZV. */
17075 && !row_for_charpos_p (row, charpos)))
17076 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17078 struct glyph *g;
17080 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17081 || (!best_row && !row->continued_p))
17082 return row;
17083 /* In bidi-reordered rows, there could be several rows whose
17084 edges surround CHARPOS, all of these rows belonging to
17085 the same continued line. We need to find the row which
17086 fits CHARPOS the best. */
17087 for (g = row->glyphs[TEXT_AREA];
17088 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17089 g++)
17091 if (!STRINGP (g->object))
17093 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17095 mindif = eabs (g->charpos - charpos);
17096 best_row = row;
17097 /* Exact match always wins. */
17098 if (mindif == 0)
17099 return best_row;
17104 else if (best_row && !row->continued_p)
17105 return best_row;
17106 ++row;
17111 /* Try to redisplay window W by reusing its existing display. W's
17112 current matrix must be up to date when this function is called,
17113 i.e. window_end_valid must be nonzero.
17115 Value is
17117 1 if display has been updated
17118 0 if otherwise unsuccessful
17119 -1 if redisplay with same window start is known not to succeed
17121 The following steps are performed:
17123 1. Find the last row in the current matrix of W that is not
17124 affected by changes at the start of current_buffer. If no such row
17125 is found, give up.
17127 2. Find the first row in W's current matrix that is not affected by
17128 changes at the end of current_buffer. Maybe there is no such row.
17130 3. Display lines beginning with the row + 1 found in step 1 to the
17131 row found in step 2 or, if step 2 didn't find a row, to the end of
17132 the window.
17134 4. If cursor is not known to appear on the window, give up.
17136 5. If display stopped at the row found in step 2, scroll the
17137 display and current matrix as needed.
17139 6. Maybe display some lines at the end of W, if we must. This can
17140 happen under various circumstances, like a partially visible line
17141 becoming fully visible, or because newly displayed lines are displayed
17142 in smaller font sizes.
17144 7. Update W's window end information. */
17146 static int
17147 try_window_id (struct window *w)
17149 struct frame *f = XFRAME (w->frame);
17150 struct glyph_matrix *current_matrix = w->current_matrix;
17151 struct glyph_matrix *desired_matrix = w->desired_matrix;
17152 struct glyph_row *last_unchanged_at_beg_row;
17153 struct glyph_row *first_unchanged_at_end_row;
17154 struct glyph_row *row;
17155 struct glyph_row *bottom_row;
17156 int bottom_vpos;
17157 struct it it;
17158 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17159 int dvpos, dy;
17160 struct text_pos start_pos;
17161 struct run run;
17162 int first_unchanged_at_end_vpos = 0;
17163 struct glyph_row *last_text_row, *last_text_row_at_end;
17164 struct text_pos start;
17165 ptrdiff_t first_changed_charpos, last_changed_charpos;
17167 #ifdef GLYPH_DEBUG
17168 if (inhibit_try_window_id)
17169 return 0;
17170 #endif
17172 /* This is handy for debugging. */
17173 #if 0
17174 #define GIVE_UP(X) \
17175 do { \
17176 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17177 return 0; \
17178 } while (0)
17179 #else
17180 #define GIVE_UP(X) return 0
17181 #endif
17183 SET_TEXT_POS_FROM_MARKER (start, w->start);
17185 /* Don't use this for mini-windows because these can show
17186 messages and mini-buffers, and we don't handle that here. */
17187 if (MINI_WINDOW_P (w))
17188 GIVE_UP (1);
17190 /* This flag is used to prevent redisplay optimizations. */
17191 if (windows_or_buffers_changed || cursor_type_changed)
17192 GIVE_UP (2);
17194 /* Verify that narrowing has not changed.
17195 Also verify that we were not told to prevent redisplay optimizations.
17196 It would be nice to further
17197 reduce the number of cases where this prevents try_window_id. */
17198 if (current_buffer->clip_changed
17199 || current_buffer->prevent_redisplay_optimizations_p)
17200 GIVE_UP (3);
17202 /* Window must either use window-based redisplay or be full width. */
17203 if (!FRAME_WINDOW_P (f)
17204 && (!FRAME_LINE_INS_DEL_OK (f)
17205 || !WINDOW_FULL_WIDTH_P (w)))
17206 GIVE_UP (4);
17208 /* Give up if point is known NOT to appear in W. */
17209 if (PT < CHARPOS (start))
17210 GIVE_UP (5);
17212 /* Another way to prevent redisplay optimizations. */
17213 if (w->last_modified == 0)
17214 GIVE_UP (6);
17216 /* Verify that window is not hscrolled. */
17217 if (w->hscroll != 0)
17218 GIVE_UP (7);
17220 /* Verify that display wasn't paused. */
17221 if (!w->window_end_valid)
17222 GIVE_UP (8);
17224 /* Can't use this if highlighting a region because a cursor movement
17225 will do more than just set the cursor. */
17226 if (markpos_of_region () >= 0)
17227 GIVE_UP (9);
17229 /* Likewise if highlighting trailing whitespace. */
17230 if (!NILP (Vshow_trailing_whitespace))
17231 GIVE_UP (11);
17233 /* Likewise if showing a region. */
17234 if (w->region_showing)
17235 GIVE_UP (10);
17237 /* Can't use this if overlay arrow position and/or string have
17238 changed. */
17239 if (overlay_arrows_changed_p ())
17240 GIVE_UP (12);
17242 /* When word-wrap is on, adding a space to the first word of a
17243 wrapped line can change the wrap position, altering the line
17244 above it. It might be worthwhile to handle this more
17245 intelligently, but for now just redisplay from scratch. */
17246 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17247 GIVE_UP (21);
17249 /* Under bidi reordering, adding or deleting a character in the
17250 beginning of a paragraph, before the first strong directional
17251 character, can change the base direction of the paragraph (unless
17252 the buffer specifies a fixed paragraph direction), which will
17253 require to redisplay the whole paragraph. It might be worthwhile
17254 to find the paragraph limits and widen the range of redisplayed
17255 lines to that, but for now just give up this optimization and
17256 redisplay from scratch. */
17257 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17258 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17259 GIVE_UP (22);
17261 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17262 only if buffer has really changed. The reason is that the gap is
17263 initially at Z for freshly visited files. The code below would
17264 set end_unchanged to 0 in that case. */
17265 if (MODIFF > SAVE_MODIFF
17266 /* This seems to happen sometimes after saving a buffer. */
17267 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17269 if (GPT - BEG < BEG_UNCHANGED)
17270 BEG_UNCHANGED = GPT - BEG;
17271 if (Z - GPT < END_UNCHANGED)
17272 END_UNCHANGED = Z - GPT;
17275 /* The position of the first and last character that has been changed. */
17276 first_changed_charpos = BEG + BEG_UNCHANGED;
17277 last_changed_charpos = Z - END_UNCHANGED;
17279 /* If window starts after a line end, and the last change is in
17280 front of that newline, then changes don't affect the display.
17281 This case happens with stealth-fontification. Note that although
17282 the display is unchanged, glyph positions in the matrix have to
17283 be adjusted, of course. */
17284 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17285 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17286 && ((last_changed_charpos < CHARPOS (start)
17287 && CHARPOS (start) == BEGV)
17288 || (last_changed_charpos < CHARPOS (start) - 1
17289 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17291 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17292 struct glyph_row *r0;
17294 /* Compute how many chars/bytes have been added to or removed
17295 from the buffer. */
17296 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17297 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17298 Z_delta = Z - Z_old;
17299 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17301 /* Give up if PT is not in the window. Note that it already has
17302 been checked at the start of try_window_id that PT is not in
17303 front of the window start. */
17304 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17305 GIVE_UP (13);
17307 /* If window start is unchanged, we can reuse the whole matrix
17308 as is, after adjusting glyph positions. No need to compute
17309 the window end again, since its offset from Z hasn't changed. */
17310 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17311 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17312 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17313 /* PT must not be in a partially visible line. */
17314 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17315 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17317 /* Adjust positions in the glyph matrix. */
17318 if (Z_delta || Z_delta_bytes)
17320 struct glyph_row *r1
17321 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17322 increment_matrix_positions (w->current_matrix,
17323 MATRIX_ROW_VPOS (r0, current_matrix),
17324 MATRIX_ROW_VPOS (r1, current_matrix),
17325 Z_delta, Z_delta_bytes);
17328 /* Set the cursor. */
17329 row = row_containing_pos (w, PT, r0, NULL, 0);
17330 if (row)
17331 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17332 else
17333 emacs_abort ();
17334 return 1;
17338 /* Handle the case that changes are all below what is displayed in
17339 the window, and that PT is in the window. This shortcut cannot
17340 be taken if ZV is visible in the window, and text has been added
17341 there that is visible in the window. */
17342 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17343 /* ZV is not visible in the window, or there are no
17344 changes at ZV, actually. */
17345 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17346 || first_changed_charpos == last_changed_charpos))
17348 struct glyph_row *r0;
17350 /* Give up if PT is not in the window. Note that it already has
17351 been checked at the start of try_window_id that PT is not in
17352 front of the window start. */
17353 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17354 GIVE_UP (14);
17356 /* If window start is unchanged, we can reuse the whole matrix
17357 as is, without changing glyph positions since no text has
17358 been added/removed in front of the window end. */
17359 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17360 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17361 /* PT must not be in a partially visible line. */
17362 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17363 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17365 /* We have to compute the window end anew since text
17366 could have been added/removed after it. */
17367 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17368 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17370 /* Set the cursor. */
17371 row = row_containing_pos (w, PT, r0, NULL, 0);
17372 if (row)
17373 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17374 else
17375 emacs_abort ();
17376 return 2;
17380 /* Give up if window start is in the changed area.
17382 The condition used to read
17384 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17386 but why that was tested escapes me at the moment. */
17387 if (CHARPOS (start) >= first_changed_charpos
17388 && CHARPOS (start) <= last_changed_charpos)
17389 GIVE_UP (15);
17391 /* Check that window start agrees with the start of the first glyph
17392 row in its current matrix. Check this after we know the window
17393 start is not in changed text, otherwise positions would not be
17394 comparable. */
17395 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17396 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17397 GIVE_UP (16);
17399 /* Give up if the window ends in strings. Overlay strings
17400 at the end are difficult to handle, so don't try. */
17401 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17402 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17403 GIVE_UP (20);
17405 /* Compute the position at which we have to start displaying new
17406 lines. Some of the lines at the top of the window might be
17407 reusable because they are not displaying changed text. Find the
17408 last row in W's current matrix not affected by changes at the
17409 start of current_buffer. Value is null if changes start in the
17410 first line of window. */
17411 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17412 if (last_unchanged_at_beg_row)
17414 /* Avoid starting to display in the middle of a character, a TAB
17415 for instance. This is easier than to set up the iterator
17416 exactly, and it's not a frequent case, so the additional
17417 effort wouldn't really pay off. */
17418 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17419 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17420 && last_unchanged_at_beg_row > w->current_matrix->rows)
17421 --last_unchanged_at_beg_row;
17423 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17424 GIVE_UP (17);
17426 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17427 GIVE_UP (18);
17428 start_pos = it.current.pos;
17430 /* Start displaying new lines in the desired matrix at the same
17431 vpos we would use in the current matrix, i.e. below
17432 last_unchanged_at_beg_row. */
17433 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17434 current_matrix);
17435 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17436 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17438 eassert (it.hpos == 0 && it.current_x == 0);
17440 else
17442 /* There are no reusable lines at the start of the window.
17443 Start displaying in the first text line. */
17444 start_display (&it, w, start);
17445 it.vpos = it.first_vpos;
17446 start_pos = it.current.pos;
17449 /* Find the first row that is not affected by changes at the end of
17450 the buffer. Value will be null if there is no unchanged row, in
17451 which case we must redisplay to the end of the window. delta
17452 will be set to the value by which buffer positions beginning with
17453 first_unchanged_at_end_row have to be adjusted due to text
17454 changes. */
17455 first_unchanged_at_end_row
17456 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17457 IF_DEBUG (debug_delta = delta);
17458 IF_DEBUG (debug_delta_bytes = delta_bytes);
17460 /* Set stop_pos to the buffer position up to which we will have to
17461 display new lines. If first_unchanged_at_end_row != NULL, this
17462 is the buffer position of the start of the line displayed in that
17463 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17464 that we don't stop at a buffer position. */
17465 stop_pos = 0;
17466 if (first_unchanged_at_end_row)
17468 eassert (last_unchanged_at_beg_row == NULL
17469 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17471 /* If this is a continuation line, move forward to the next one
17472 that isn't. Changes in lines above affect this line.
17473 Caution: this may move first_unchanged_at_end_row to a row
17474 not displaying text. */
17475 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17476 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17477 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17478 < it.last_visible_y))
17479 ++first_unchanged_at_end_row;
17481 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17482 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17483 >= it.last_visible_y))
17484 first_unchanged_at_end_row = NULL;
17485 else
17487 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17488 + delta);
17489 first_unchanged_at_end_vpos
17490 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17491 eassert (stop_pos >= Z - END_UNCHANGED);
17494 else if (last_unchanged_at_beg_row == NULL)
17495 GIVE_UP (19);
17498 #ifdef GLYPH_DEBUG
17500 /* Either there is no unchanged row at the end, or the one we have
17501 now displays text. This is a necessary condition for the window
17502 end pos calculation at the end of this function. */
17503 eassert (first_unchanged_at_end_row == NULL
17504 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17506 debug_last_unchanged_at_beg_vpos
17507 = (last_unchanged_at_beg_row
17508 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17509 : -1);
17510 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17512 #endif /* GLYPH_DEBUG */
17515 /* Display new lines. Set last_text_row to the last new line
17516 displayed which has text on it, i.e. might end up as being the
17517 line where the window_end_vpos is. */
17518 w->cursor.vpos = -1;
17519 last_text_row = NULL;
17520 overlay_arrow_seen = 0;
17521 while (it.current_y < it.last_visible_y
17522 && !fonts_changed_p
17523 && (first_unchanged_at_end_row == NULL
17524 || IT_CHARPOS (it) < stop_pos))
17526 if (display_line (&it))
17527 last_text_row = it.glyph_row - 1;
17530 if (fonts_changed_p)
17531 return -1;
17534 /* Compute differences in buffer positions, y-positions etc. for
17535 lines reused at the bottom of the window. Compute what we can
17536 scroll. */
17537 if (first_unchanged_at_end_row
17538 /* No lines reused because we displayed everything up to the
17539 bottom of the window. */
17540 && it.current_y < it.last_visible_y)
17542 dvpos = (it.vpos
17543 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17544 current_matrix));
17545 dy = it.current_y - first_unchanged_at_end_row->y;
17546 run.current_y = first_unchanged_at_end_row->y;
17547 run.desired_y = run.current_y + dy;
17548 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17550 else
17552 delta = delta_bytes = dvpos = dy
17553 = run.current_y = run.desired_y = run.height = 0;
17554 first_unchanged_at_end_row = NULL;
17556 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17559 /* Find the cursor if not already found. We have to decide whether
17560 PT will appear on this window (it sometimes doesn't, but this is
17561 not a very frequent case.) This decision has to be made before
17562 the current matrix is altered. A value of cursor.vpos < 0 means
17563 that PT is either in one of the lines beginning at
17564 first_unchanged_at_end_row or below the window. Don't care for
17565 lines that might be displayed later at the window end; as
17566 mentioned, this is not a frequent case. */
17567 if (w->cursor.vpos < 0)
17569 /* Cursor in unchanged rows at the top? */
17570 if (PT < CHARPOS (start_pos)
17571 && last_unchanged_at_beg_row)
17573 row = row_containing_pos (w, PT,
17574 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17575 last_unchanged_at_beg_row + 1, 0);
17576 if (row)
17577 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17580 /* Start from first_unchanged_at_end_row looking for PT. */
17581 else if (first_unchanged_at_end_row)
17583 row = row_containing_pos (w, PT - delta,
17584 first_unchanged_at_end_row, NULL, 0);
17585 if (row)
17586 set_cursor_from_row (w, row, w->current_matrix, delta,
17587 delta_bytes, dy, dvpos);
17590 /* Give up if cursor was not found. */
17591 if (w->cursor.vpos < 0)
17593 clear_glyph_matrix (w->desired_matrix);
17594 return -1;
17598 /* Don't let the cursor end in the scroll margins. */
17600 int this_scroll_margin, cursor_height;
17601 int frame_line_height = default_line_pixel_height (w);
17602 int window_total_lines
17603 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17605 this_scroll_margin =
17606 max (0, min (scroll_margin, window_total_lines / 4));
17607 this_scroll_margin *= frame_line_height;
17608 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17610 if ((w->cursor.y < this_scroll_margin
17611 && CHARPOS (start) > BEGV)
17612 /* Old redisplay didn't take scroll margin into account at the bottom,
17613 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17614 || (w->cursor.y + (make_cursor_line_fully_visible_p
17615 ? cursor_height + this_scroll_margin
17616 : 1)) > it.last_visible_y)
17618 w->cursor.vpos = -1;
17619 clear_glyph_matrix (w->desired_matrix);
17620 return -1;
17624 /* Scroll the display. Do it before changing the current matrix so
17625 that xterm.c doesn't get confused about where the cursor glyph is
17626 found. */
17627 if (dy && run.height)
17629 update_begin (f);
17631 if (FRAME_WINDOW_P (f))
17633 FRAME_RIF (f)->update_window_begin_hook (w);
17634 FRAME_RIF (f)->clear_window_mouse_face (w);
17635 FRAME_RIF (f)->scroll_run_hook (w, &run);
17636 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17638 else
17640 /* Terminal frame. In this case, dvpos gives the number of
17641 lines to scroll by; dvpos < 0 means scroll up. */
17642 int from_vpos
17643 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17644 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17645 int end = (WINDOW_TOP_EDGE_LINE (w)
17646 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17647 + window_internal_height (w));
17649 #if defined (HAVE_GPM) || defined (MSDOS)
17650 x_clear_window_mouse_face (w);
17651 #endif
17652 /* Perform the operation on the screen. */
17653 if (dvpos > 0)
17655 /* Scroll last_unchanged_at_beg_row to the end of the
17656 window down dvpos lines. */
17657 set_terminal_window (f, end);
17659 /* On dumb terminals delete dvpos lines at the end
17660 before inserting dvpos empty lines. */
17661 if (!FRAME_SCROLL_REGION_OK (f))
17662 ins_del_lines (f, end - dvpos, -dvpos);
17664 /* Insert dvpos empty lines in front of
17665 last_unchanged_at_beg_row. */
17666 ins_del_lines (f, from, dvpos);
17668 else if (dvpos < 0)
17670 /* Scroll up last_unchanged_at_beg_vpos to the end of
17671 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17672 set_terminal_window (f, end);
17674 /* Delete dvpos lines in front of
17675 last_unchanged_at_beg_vpos. ins_del_lines will set
17676 the cursor to the given vpos and emit |dvpos| delete
17677 line sequences. */
17678 ins_del_lines (f, from + dvpos, dvpos);
17680 /* On a dumb terminal insert dvpos empty lines at the
17681 end. */
17682 if (!FRAME_SCROLL_REGION_OK (f))
17683 ins_del_lines (f, end + dvpos, -dvpos);
17686 set_terminal_window (f, 0);
17689 update_end (f);
17692 /* Shift reused rows of the current matrix to the right position.
17693 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17694 text. */
17695 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17696 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17697 if (dvpos < 0)
17699 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17700 bottom_vpos, dvpos);
17701 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17702 bottom_vpos);
17704 else if (dvpos > 0)
17706 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17707 bottom_vpos, dvpos);
17708 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17709 first_unchanged_at_end_vpos + dvpos);
17712 /* For frame-based redisplay, make sure that current frame and window
17713 matrix are in sync with respect to glyph memory. */
17714 if (!FRAME_WINDOW_P (f))
17715 sync_frame_with_window_matrix_rows (w);
17717 /* Adjust buffer positions in reused rows. */
17718 if (delta || delta_bytes)
17719 increment_matrix_positions (current_matrix,
17720 first_unchanged_at_end_vpos + dvpos,
17721 bottom_vpos, delta, delta_bytes);
17723 /* Adjust Y positions. */
17724 if (dy)
17725 shift_glyph_matrix (w, current_matrix,
17726 first_unchanged_at_end_vpos + dvpos,
17727 bottom_vpos, dy);
17729 if (first_unchanged_at_end_row)
17731 first_unchanged_at_end_row += dvpos;
17732 if (first_unchanged_at_end_row->y >= it.last_visible_y
17733 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17734 first_unchanged_at_end_row = NULL;
17737 /* If scrolling up, there may be some lines to display at the end of
17738 the window. */
17739 last_text_row_at_end = NULL;
17740 if (dy < 0)
17742 /* Scrolling up can leave for example a partially visible line
17743 at the end of the window to be redisplayed. */
17744 /* Set last_row to the glyph row in the current matrix where the
17745 window end line is found. It has been moved up or down in
17746 the matrix by dvpos. */
17747 int last_vpos = w->window_end_vpos + dvpos;
17748 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17750 /* If last_row is the window end line, it should display text. */
17751 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17753 /* If window end line was partially visible before, begin
17754 displaying at that line. Otherwise begin displaying with the
17755 line following it. */
17756 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17758 init_to_row_start (&it, w, last_row);
17759 it.vpos = last_vpos;
17760 it.current_y = last_row->y;
17762 else
17764 init_to_row_end (&it, w, last_row);
17765 it.vpos = 1 + last_vpos;
17766 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17767 ++last_row;
17770 /* We may start in a continuation line. If so, we have to
17771 get the right continuation_lines_width and current_x. */
17772 it.continuation_lines_width = last_row->continuation_lines_width;
17773 it.hpos = it.current_x = 0;
17775 /* Display the rest of the lines at the window end. */
17776 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17777 while (it.current_y < it.last_visible_y
17778 && !fonts_changed_p)
17780 /* Is it always sure that the display agrees with lines in
17781 the current matrix? I don't think so, so we mark rows
17782 displayed invalid in the current matrix by setting their
17783 enabled_p flag to zero. */
17784 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17785 if (display_line (&it))
17786 last_text_row_at_end = it.glyph_row - 1;
17790 /* Update window_end_pos and window_end_vpos. */
17791 if (first_unchanged_at_end_row && !last_text_row_at_end)
17793 /* Window end line if one of the preserved rows from the current
17794 matrix. Set row to the last row displaying text in current
17795 matrix starting at first_unchanged_at_end_row, after
17796 scrolling. */
17797 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17798 row = find_last_row_displaying_text (w->current_matrix, &it,
17799 first_unchanged_at_end_row);
17800 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17801 adjust_window_ends (w, row, 1);
17802 eassert (w->window_end_bytepos >= 0);
17803 IF_DEBUG (debug_method_add (w, "A"));
17805 else if (last_text_row_at_end)
17807 adjust_window_ends (w, last_text_row_at_end, 0);
17808 eassert (w->window_end_bytepos >= 0);
17809 IF_DEBUG (debug_method_add (w, "B"));
17811 else if (last_text_row)
17813 /* We have displayed either to the end of the window or at the
17814 end of the window, i.e. the last row with text is to be found
17815 in the desired matrix. */
17816 adjust_window_ends (w, last_text_row, 0);
17817 eassert (w->window_end_bytepos >= 0);
17819 else if (first_unchanged_at_end_row == NULL
17820 && last_text_row == NULL
17821 && last_text_row_at_end == NULL)
17823 /* Displayed to end of window, but no line containing text was
17824 displayed. Lines were deleted at the end of the window. */
17825 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17826 int vpos = w->window_end_vpos;
17827 struct glyph_row *current_row = current_matrix->rows + vpos;
17828 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17830 for (row = NULL;
17831 row == NULL && vpos >= first_vpos;
17832 --vpos, --current_row, --desired_row)
17834 if (desired_row->enabled_p)
17836 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17837 row = desired_row;
17839 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17840 row = current_row;
17843 eassert (row != NULL);
17844 w->window_end_vpos = vpos + 1;
17845 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17846 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17847 eassert (w->window_end_bytepos >= 0);
17848 IF_DEBUG (debug_method_add (w, "C"));
17850 else
17851 emacs_abort ();
17853 IF_DEBUG (debug_end_pos = w->window_end_pos;
17854 debug_end_vpos = w->window_end_vpos);
17856 /* Record that display has not been completed. */
17857 w->window_end_valid = 0;
17858 w->desired_matrix->no_scrolling_p = 1;
17859 return 3;
17861 #undef GIVE_UP
17866 /***********************************************************************
17867 More debugging support
17868 ***********************************************************************/
17870 #ifdef GLYPH_DEBUG
17872 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17873 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17874 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17877 /* Dump the contents of glyph matrix MATRIX on stderr.
17879 GLYPHS 0 means don't show glyph contents.
17880 GLYPHS 1 means show glyphs in short form
17881 GLYPHS > 1 means show glyphs in long form. */
17883 void
17884 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17886 int i;
17887 for (i = 0; i < matrix->nrows; ++i)
17888 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17892 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17893 the glyph row and area where the glyph comes from. */
17895 void
17896 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17898 if (glyph->type == CHAR_GLYPH
17899 || glyph->type == GLYPHLESS_GLYPH)
17901 fprintf (stderr,
17902 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17903 glyph - row->glyphs[TEXT_AREA],
17904 (glyph->type == CHAR_GLYPH
17905 ? 'C'
17906 : 'G'),
17907 glyph->charpos,
17908 (BUFFERP (glyph->object)
17909 ? 'B'
17910 : (STRINGP (glyph->object)
17911 ? 'S'
17912 : (INTEGERP (glyph->object)
17913 ? '0'
17914 : '-'))),
17915 glyph->pixel_width,
17916 glyph->u.ch,
17917 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17918 ? glyph->u.ch
17919 : '.'),
17920 glyph->face_id,
17921 glyph->left_box_line_p,
17922 glyph->right_box_line_p);
17924 else if (glyph->type == STRETCH_GLYPH)
17926 fprintf (stderr,
17927 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17928 glyph - row->glyphs[TEXT_AREA],
17929 'S',
17930 glyph->charpos,
17931 (BUFFERP (glyph->object)
17932 ? 'B'
17933 : (STRINGP (glyph->object)
17934 ? 'S'
17935 : (INTEGERP (glyph->object)
17936 ? '0'
17937 : '-'))),
17938 glyph->pixel_width,
17940 ' ',
17941 glyph->face_id,
17942 glyph->left_box_line_p,
17943 glyph->right_box_line_p);
17945 else if (glyph->type == IMAGE_GLYPH)
17947 fprintf (stderr,
17948 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17949 glyph - row->glyphs[TEXT_AREA],
17950 'I',
17951 glyph->charpos,
17952 (BUFFERP (glyph->object)
17953 ? 'B'
17954 : (STRINGP (glyph->object)
17955 ? 'S'
17956 : (INTEGERP (glyph->object)
17957 ? '0'
17958 : '-'))),
17959 glyph->pixel_width,
17960 glyph->u.img_id,
17961 '.',
17962 glyph->face_id,
17963 glyph->left_box_line_p,
17964 glyph->right_box_line_p);
17966 else if (glyph->type == COMPOSITE_GLYPH)
17968 fprintf (stderr,
17969 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17970 glyph - row->glyphs[TEXT_AREA],
17971 '+',
17972 glyph->charpos,
17973 (BUFFERP (glyph->object)
17974 ? 'B'
17975 : (STRINGP (glyph->object)
17976 ? 'S'
17977 : (INTEGERP (glyph->object)
17978 ? '0'
17979 : '-'))),
17980 glyph->pixel_width,
17981 glyph->u.cmp.id);
17982 if (glyph->u.cmp.automatic)
17983 fprintf (stderr,
17984 "[%d-%d]",
17985 glyph->slice.cmp.from, glyph->slice.cmp.to);
17986 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17987 glyph->face_id,
17988 glyph->left_box_line_p,
17989 glyph->right_box_line_p);
17994 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17995 GLYPHS 0 means don't show glyph contents.
17996 GLYPHS 1 means show glyphs in short form
17997 GLYPHS > 1 means show glyphs in long form. */
17999 void
18000 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18002 if (glyphs != 1)
18004 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18005 fprintf (stderr, "==============================================================================\n");
18007 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18008 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18009 vpos,
18010 MATRIX_ROW_START_CHARPOS (row),
18011 MATRIX_ROW_END_CHARPOS (row),
18012 row->used[TEXT_AREA],
18013 row->contains_overlapping_glyphs_p,
18014 row->enabled_p,
18015 row->truncated_on_left_p,
18016 row->truncated_on_right_p,
18017 row->continued_p,
18018 MATRIX_ROW_CONTINUATION_LINE_P (row),
18019 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18020 row->ends_at_zv_p,
18021 row->fill_line_p,
18022 row->ends_in_middle_of_char_p,
18023 row->starts_in_middle_of_char_p,
18024 row->mouse_face_p,
18025 row->x,
18026 row->y,
18027 row->pixel_width,
18028 row->height,
18029 row->visible_height,
18030 row->ascent,
18031 row->phys_ascent);
18032 /* The next 3 lines should align to "Start" in the header. */
18033 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18034 row->end.overlay_string_index,
18035 row->continuation_lines_width);
18036 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18037 CHARPOS (row->start.string_pos),
18038 CHARPOS (row->end.string_pos));
18039 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18040 row->end.dpvec_index);
18043 if (glyphs > 1)
18045 int area;
18047 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18049 struct glyph *glyph = row->glyphs[area];
18050 struct glyph *glyph_end = glyph + row->used[area];
18052 /* Glyph for a line end in text. */
18053 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18054 ++glyph_end;
18056 if (glyph < glyph_end)
18057 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18059 for (; glyph < glyph_end; ++glyph)
18060 dump_glyph (row, glyph, area);
18063 else if (glyphs == 1)
18065 int area;
18067 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18069 char *s = alloca (row->used[area] + 4);
18070 int i;
18072 for (i = 0; i < row->used[area]; ++i)
18074 struct glyph *glyph = row->glyphs[area] + i;
18075 if (i == row->used[area] - 1
18076 && area == TEXT_AREA
18077 && INTEGERP (glyph->object)
18078 && glyph->type == CHAR_GLYPH
18079 && glyph->u.ch == ' ')
18081 strcpy (&s[i], "[\\n]");
18082 i += 4;
18084 else if (glyph->type == CHAR_GLYPH
18085 && glyph->u.ch < 0x80
18086 && glyph->u.ch >= ' ')
18087 s[i] = glyph->u.ch;
18088 else
18089 s[i] = '.';
18092 s[i] = '\0';
18093 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18099 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18100 Sdump_glyph_matrix, 0, 1, "p",
18101 doc: /* Dump the current matrix of the selected window to stderr.
18102 Shows contents of glyph row structures. With non-nil
18103 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18104 glyphs in short form, otherwise show glyphs in long form. */)
18105 (Lisp_Object glyphs)
18107 struct window *w = XWINDOW (selected_window);
18108 struct buffer *buffer = XBUFFER (w->contents);
18110 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18111 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18112 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18113 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18114 fprintf (stderr, "=============================================\n");
18115 dump_glyph_matrix (w->current_matrix,
18116 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18117 return Qnil;
18121 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18122 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18123 (void)
18125 struct frame *f = XFRAME (selected_frame);
18126 dump_glyph_matrix (f->current_matrix, 1);
18127 return Qnil;
18131 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18132 doc: /* Dump glyph row ROW to stderr.
18133 GLYPH 0 means don't dump glyphs.
18134 GLYPH 1 means dump glyphs in short form.
18135 GLYPH > 1 or omitted means dump glyphs in long form. */)
18136 (Lisp_Object row, Lisp_Object glyphs)
18138 struct glyph_matrix *matrix;
18139 EMACS_INT vpos;
18141 CHECK_NUMBER (row);
18142 matrix = XWINDOW (selected_window)->current_matrix;
18143 vpos = XINT (row);
18144 if (vpos >= 0 && vpos < matrix->nrows)
18145 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18146 vpos,
18147 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18148 return Qnil;
18152 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18153 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18154 GLYPH 0 means don't dump glyphs.
18155 GLYPH 1 means dump glyphs in short form.
18156 GLYPH > 1 or omitted means dump glyphs in long form. */)
18157 (Lisp_Object row, Lisp_Object glyphs)
18159 struct frame *sf = SELECTED_FRAME ();
18160 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18161 EMACS_INT vpos;
18163 CHECK_NUMBER (row);
18164 vpos = XINT (row);
18165 if (vpos >= 0 && vpos < m->nrows)
18166 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18167 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18168 return Qnil;
18172 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18173 doc: /* Toggle tracing of redisplay.
18174 With ARG, turn tracing on if and only if ARG is positive. */)
18175 (Lisp_Object arg)
18177 if (NILP (arg))
18178 trace_redisplay_p = !trace_redisplay_p;
18179 else
18181 arg = Fprefix_numeric_value (arg);
18182 trace_redisplay_p = XINT (arg) > 0;
18185 return Qnil;
18189 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18190 doc: /* Like `format', but print result to stderr.
18191 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18192 (ptrdiff_t nargs, Lisp_Object *args)
18194 Lisp_Object s = Fformat (nargs, args);
18195 fprintf (stderr, "%s", SDATA (s));
18196 return Qnil;
18199 #endif /* GLYPH_DEBUG */
18203 /***********************************************************************
18204 Building Desired Matrix Rows
18205 ***********************************************************************/
18207 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18208 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18210 static struct glyph_row *
18211 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18213 struct frame *f = XFRAME (WINDOW_FRAME (w));
18214 struct buffer *buffer = XBUFFER (w->contents);
18215 struct buffer *old = current_buffer;
18216 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18217 int arrow_len = SCHARS (overlay_arrow_string);
18218 const unsigned char *arrow_end = arrow_string + arrow_len;
18219 const unsigned char *p;
18220 struct it it;
18221 bool multibyte_p;
18222 int n_glyphs_before;
18224 set_buffer_temp (buffer);
18225 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18226 it.glyph_row->used[TEXT_AREA] = 0;
18227 SET_TEXT_POS (it.position, 0, 0);
18229 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18230 p = arrow_string;
18231 while (p < arrow_end)
18233 Lisp_Object face, ilisp;
18235 /* Get the next character. */
18236 if (multibyte_p)
18237 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18238 else
18240 it.c = it.char_to_display = *p, it.len = 1;
18241 if (! ASCII_CHAR_P (it.c))
18242 it.char_to_display = BYTE8_TO_CHAR (it.c);
18244 p += it.len;
18246 /* Get its face. */
18247 ilisp = make_number (p - arrow_string);
18248 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18249 it.face_id = compute_char_face (f, it.char_to_display, face);
18251 /* Compute its width, get its glyphs. */
18252 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18253 SET_TEXT_POS (it.position, -1, -1);
18254 PRODUCE_GLYPHS (&it);
18256 /* If this character doesn't fit any more in the line, we have
18257 to remove some glyphs. */
18258 if (it.current_x > it.last_visible_x)
18260 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18261 break;
18265 set_buffer_temp (old);
18266 return it.glyph_row;
18270 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18271 glyphs to insert is determined by produce_special_glyphs. */
18273 static void
18274 insert_left_trunc_glyphs (struct it *it)
18276 struct it truncate_it;
18277 struct glyph *from, *end, *to, *toend;
18279 eassert (!FRAME_WINDOW_P (it->f)
18280 || (!it->glyph_row->reversed_p
18281 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18282 || (it->glyph_row->reversed_p
18283 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18285 /* Get the truncation glyphs. */
18286 truncate_it = *it;
18287 truncate_it.current_x = 0;
18288 truncate_it.face_id = DEFAULT_FACE_ID;
18289 truncate_it.glyph_row = &scratch_glyph_row;
18290 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18291 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18292 truncate_it.object = make_number (0);
18293 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18295 /* Overwrite glyphs from IT with truncation glyphs. */
18296 if (!it->glyph_row->reversed_p)
18298 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18300 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18301 end = from + tused;
18302 to = it->glyph_row->glyphs[TEXT_AREA];
18303 toend = to + it->glyph_row->used[TEXT_AREA];
18304 if (FRAME_WINDOW_P (it->f))
18306 /* On GUI frames, when variable-size fonts are displayed,
18307 the truncation glyphs may need more pixels than the row's
18308 glyphs they overwrite. We overwrite more glyphs to free
18309 enough screen real estate, and enlarge the stretch glyph
18310 on the right (see display_line), if there is one, to
18311 preserve the screen position of the truncation glyphs on
18312 the right. */
18313 int w = 0;
18314 struct glyph *g = to;
18315 short used;
18317 /* The first glyph could be partially visible, in which case
18318 it->glyph_row->x will be negative. But we want the left
18319 truncation glyphs to be aligned at the left margin of the
18320 window, so we override the x coordinate at which the row
18321 will begin. */
18322 it->glyph_row->x = 0;
18323 while (g < toend && w < it->truncation_pixel_width)
18325 w += g->pixel_width;
18326 ++g;
18328 if (g - to - tused > 0)
18330 memmove (to + tused, g, (toend - g) * sizeof(*g));
18331 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18333 used = it->glyph_row->used[TEXT_AREA];
18334 if (it->glyph_row->truncated_on_right_p
18335 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18336 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18337 == STRETCH_GLYPH)
18339 int extra = w - it->truncation_pixel_width;
18341 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18345 while (from < end)
18346 *to++ = *from++;
18348 /* There may be padding glyphs left over. Overwrite them too. */
18349 if (!FRAME_WINDOW_P (it->f))
18351 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18353 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18354 while (from < end)
18355 *to++ = *from++;
18359 if (to > toend)
18360 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18362 else
18364 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18366 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18367 that back to front. */
18368 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18369 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18370 toend = it->glyph_row->glyphs[TEXT_AREA];
18371 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18372 if (FRAME_WINDOW_P (it->f))
18374 int w = 0;
18375 struct glyph *g = to;
18377 while (g >= toend && w < it->truncation_pixel_width)
18379 w += g->pixel_width;
18380 --g;
18382 if (to - g - tused > 0)
18383 to = g + tused;
18384 if (it->glyph_row->truncated_on_right_p
18385 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18386 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18388 int extra = w - it->truncation_pixel_width;
18390 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18394 while (from >= end && to >= toend)
18395 *to-- = *from--;
18396 if (!FRAME_WINDOW_P (it->f))
18398 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18400 from =
18401 truncate_it.glyph_row->glyphs[TEXT_AREA]
18402 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18403 while (from >= end && to >= toend)
18404 *to-- = *from--;
18407 if (from >= end)
18409 /* Need to free some room before prepending additional
18410 glyphs. */
18411 int move_by = from - end + 1;
18412 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18413 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18415 for ( ; g >= g0; g--)
18416 g[move_by] = *g;
18417 while (from >= end)
18418 *to-- = *from--;
18419 it->glyph_row->used[TEXT_AREA] += move_by;
18424 /* Compute the hash code for ROW. */
18425 unsigned
18426 row_hash (struct glyph_row *row)
18428 int area, k;
18429 unsigned hashval = 0;
18431 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18432 for (k = 0; k < row->used[area]; ++k)
18433 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18434 + row->glyphs[area][k].u.val
18435 + row->glyphs[area][k].face_id
18436 + row->glyphs[area][k].padding_p
18437 + (row->glyphs[area][k].type << 2));
18439 return hashval;
18442 /* Compute the pixel height and width of IT->glyph_row.
18444 Most of the time, ascent and height of a display line will be equal
18445 to the max_ascent and max_height values of the display iterator
18446 structure. This is not the case if
18448 1. We hit ZV without displaying anything. In this case, max_ascent
18449 and max_height will be zero.
18451 2. We have some glyphs that don't contribute to the line height.
18452 (The glyph row flag contributes_to_line_height_p is for future
18453 pixmap extensions).
18455 The first case is easily covered by using default values because in
18456 these cases, the line height does not really matter, except that it
18457 must not be zero. */
18459 static void
18460 compute_line_metrics (struct it *it)
18462 struct glyph_row *row = it->glyph_row;
18464 if (FRAME_WINDOW_P (it->f))
18466 int i, min_y, max_y;
18468 /* The line may consist of one space only, that was added to
18469 place the cursor on it. If so, the row's height hasn't been
18470 computed yet. */
18471 if (row->height == 0)
18473 if (it->max_ascent + it->max_descent == 0)
18474 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18475 row->ascent = it->max_ascent;
18476 row->height = it->max_ascent + it->max_descent;
18477 row->phys_ascent = it->max_phys_ascent;
18478 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18479 row->extra_line_spacing = it->max_extra_line_spacing;
18482 /* Compute the width of this line. */
18483 row->pixel_width = row->x;
18484 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18485 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18487 eassert (row->pixel_width >= 0);
18488 eassert (row->ascent >= 0 && row->height > 0);
18490 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18491 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18493 /* If first line's physical ascent is larger than its logical
18494 ascent, use the physical ascent, and make the row taller.
18495 This makes accented characters fully visible. */
18496 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18497 && row->phys_ascent > row->ascent)
18499 row->height += row->phys_ascent - row->ascent;
18500 row->ascent = row->phys_ascent;
18503 /* Compute how much of the line is visible. */
18504 row->visible_height = row->height;
18506 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18507 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18509 if (row->y < min_y)
18510 row->visible_height -= min_y - row->y;
18511 if (row->y + row->height > max_y)
18512 row->visible_height -= row->y + row->height - max_y;
18514 else
18516 row->pixel_width = row->used[TEXT_AREA];
18517 if (row->continued_p)
18518 row->pixel_width -= it->continuation_pixel_width;
18519 else if (row->truncated_on_right_p)
18520 row->pixel_width -= it->truncation_pixel_width;
18521 row->ascent = row->phys_ascent = 0;
18522 row->height = row->phys_height = row->visible_height = 1;
18523 row->extra_line_spacing = 0;
18526 /* Compute a hash code for this row. */
18527 row->hash = row_hash (row);
18529 it->max_ascent = it->max_descent = 0;
18530 it->max_phys_ascent = it->max_phys_descent = 0;
18534 /* Append one space to the glyph row of iterator IT if doing a
18535 window-based redisplay. The space has the same face as
18536 IT->face_id. Value is non-zero if a space was added.
18538 This function is called to make sure that there is always one glyph
18539 at the end of a glyph row that the cursor can be set on under
18540 window-systems. (If there weren't such a glyph we would not know
18541 how wide and tall a box cursor should be displayed).
18543 At the same time this space let's a nicely handle clearing to the
18544 end of the line if the row ends in italic text. */
18546 static int
18547 append_space_for_newline (struct it *it, int default_face_p)
18549 if (FRAME_WINDOW_P (it->f))
18551 int n = it->glyph_row->used[TEXT_AREA];
18553 if (it->glyph_row->glyphs[TEXT_AREA] + n
18554 < it->glyph_row->glyphs[1 + TEXT_AREA])
18556 /* Save some values that must not be changed.
18557 Must save IT->c and IT->len because otherwise
18558 ITERATOR_AT_END_P wouldn't work anymore after
18559 append_space_for_newline has been called. */
18560 enum display_element_type saved_what = it->what;
18561 int saved_c = it->c, saved_len = it->len;
18562 int saved_char_to_display = it->char_to_display;
18563 int saved_x = it->current_x;
18564 int saved_face_id = it->face_id;
18565 int saved_box_end = it->end_of_box_run_p;
18566 struct text_pos saved_pos;
18567 Lisp_Object saved_object;
18568 struct face *face;
18570 saved_object = it->object;
18571 saved_pos = it->position;
18573 it->what = IT_CHARACTER;
18574 memset (&it->position, 0, sizeof it->position);
18575 it->object = make_number (0);
18576 it->c = it->char_to_display = ' ';
18577 it->len = 1;
18579 /* If the default face was remapped, be sure to use the
18580 remapped face for the appended newline. */
18581 if (default_face_p)
18582 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18583 else if (it->face_before_selective_p)
18584 it->face_id = it->saved_face_id;
18585 face = FACE_FROM_ID (it->f, it->face_id);
18586 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18587 /* In R2L rows, we will prepend a stretch glyph that will
18588 have the end_of_box_run_p flag set for it, so there's no
18589 need for the appended newline glyph to have that flag
18590 set. */
18591 if (it->glyph_row->reversed_p
18592 /* But if the appended newline glyph goes all the way to
18593 the end of the row, there will be no stretch glyph,
18594 so leave the box flag set. */
18595 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18596 it->end_of_box_run_p = 0;
18598 PRODUCE_GLYPHS (it);
18600 it->override_ascent = -1;
18601 it->constrain_row_ascent_descent_p = 0;
18602 it->current_x = saved_x;
18603 it->object = saved_object;
18604 it->position = saved_pos;
18605 it->what = saved_what;
18606 it->face_id = saved_face_id;
18607 it->len = saved_len;
18608 it->c = saved_c;
18609 it->char_to_display = saved_char_to_display;
18610 it->end_of_box_run_p = saved_box_end;
18611 return 1;
18615 return 0;
18619 /* Extend the face of the last glyph in the text area of IT->glyph_row
18620 to the end of the display line. Called from display_line. If the
18621 glyph row is empty, add a space glyph to it so that we know the
18622 face to draw. Set the glyph row flag fill_line_p. If the glyph
18623 row is R2L, prepend a stretch glyph to cover the empty space to the
18624 left of the leftmost glyph. */
18626 static void
18627 extend_face_to_end_of_line (struct it *it)
18629 struct face *face, *default_face;
18630 struct frame *f = it->f;
18632 /* If line is already filled, do nothing. Non window-system frames
18633 get a grace of one more ``pixel'' because their characters are
18634 1-``pixel'' wide, so they hit the equality too early. This grace
18635 is needed only for R2L rows that are not continued, to produce
18636 one extra blank where we could display the cursor. */
18637 if (it->current_x >= it->last_visible_x
18638 + (!FRAME_WINDOW_P (f)
18639 && it->glyph_row->reversed_p
18640 && !it->glyph_row->continued_p))
18641 return;
18643 /* The default face, possibly remapped. */
18644 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18646 /* Face extension extends the background and box of IT->face_id
18647 to the end of the line. If the background equals the background
18648 of the frame, we don't have to do anything. */
18649 if (it->face_before_selective_p)
18650 face = FACE_FROM_ID (f, it->saved_face_id);
18651 else
18652 face = FACE_FROM_ID (f, it->face_id);
18654 if (FRAME_WINDOW_P (f)
18655 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18656 && face->box == FACE_NO_BOX
18657 && face->background == FRAME_BACKGROUND_PIXEL (f)
18658 && !face->stipple
18659 && !it->glyph_row->reversed_p)
18660 return;
18662 /* Set the glyph row flag indicating that the face of the last glyph
18663 in the text area has to be drawn to the end of the text area. */
18664 it->glyph_row->fill_line_p = 1;
18666 /* If current character of IT is not ASCII, make sure we have the
18667 ASCII face. This will be automatically undone the next time
18668 get_next_display_element returns a multibyte character. Note
18669 that the character will always be single byte in unibyte
18670 text. */
18671 if (!ASCII_CHAR_P (it->c))
18673 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18676 if (FRAME_WINDOW_P (f))
18678 /* If the row is empty, add a space with the current face of IT,
18679 so that we know which face to draw. */
18680 if (it->glyph_row->used[TEXT_AREA] == 0)
18682 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18683 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18684 it->glyph_row->used[TEXT_AREA] = 1;
18686 #ifdef HAVE_WINDOW_SYSTEM
18687 if (it->glyph_row->reversed_p)
18689 /* Prepend a stretch glyph to the row, such that the
18690 rightmost glyph will be drawn flushed all the way to the
18691 right margin of the window. The stretch glyph that will
18692 occupy the empty space, if any, to the left of the
18693 glyphs. */
18694 struct font *font = face->font ? face->font : FRAME_FONT (f);
18695 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18696 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18697 struct glyph *g;
18698 int row_width, stretch_ascent, stretch_width;
18699 struct text_pos saved_pos;
18700 int saved_face_id, saved_avoid_cursor, saved_box_start;
18702 for (row_width = 0, g = row_start; g < row_end; g++)
18703 row_width += g->pixel_width;
18704 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18705 if (stretch_width > 0)
18707 stretch_ascent =
18708 (((it->ascent + it->descent)
18709 * FONT_BASE (font)) / FONT_HEIGHT (font));
18710 saved_pos = it->position;
18711 memset (&it->position, 0, sizeof it->position);
18712 saved_avoid_cursor = it->avoid_cursor_p;
18713 it->avoid_cursor_p = 1;
18714 saved_face_id = it->face_id;
18715 saved_box_start = it->start_of_box_run_p;
18716 /* The last row's stretch glyph should get the default
18717 face, to avoid painting the rest of the window with
18718 the region face, if the region ends at ZV. */
18719 if (it->glyph_row->ends_at_zv_p)
18720 it->face_id = default_face->id;
18721 else
18722 it->face_id = face->id;
18723 it->start_of_box_run_p = 0;
18724 append_stretch_glyph (it, make_number (0), stretch_width,
18725 it->ascent + it->descent, stretch_ascent);
18726 it->position = saved_pos;
18727 it->avoid_cursor_p = saved_avoid_cursor;
18728 it->face_id = saved_face_id;
18729 it->start_of_box_run_p = saved_box_start;
18732 #endif /* HAVE_WINDOW_SYSTEM */
18734 else
18736 /* Save some values that must not be changed. */
18737 int saved_x = it->current_x;
18738 struct text_pos saved_pos;
18739 Lisp_Object saved_object;
18740 enum display_element_type saved_what = it->what;
18741 int saved_face_id = it->face_id;
18743 saved_object = it->object;
18744 saved_pos = it->position;
18746 it->what = IT_CHARACTER;
18747 memset (&it->position, 0, sizeof it->position);
18748 it->object = make_number (0);
18749 it->c = it->char_to_display = ' ';
18750 it->len = 1;
18751 /* The last row's blank glyphs should get the default face, to
18752 avoid painting the rest of the window with the region face,
18753 if the region ends at ZV. */
18754 if (it->glyph_row->ends_at_zv_p)
18755 it->face_id = default_face->id;
18756 else
18757 it->face_id = face->id;
18759 PRODUCE_GLYPHS (it);
18761 while (it->current_x <= it->last_visible_x)
18762 PRODUCE_GLYPHS (it);
18764 /* Don't count these blanks really. It would let us insert a left
18765 truncation glyph below and make us set the cursor on them, maybe. */
18766 it->current_x = saved_x;
18767 it->object = saved_object;
18768 it->position = saved_pos;
18769 it->what = saved_what;
18770 it->face_id = saved_face_id;
18775 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18776 trailing whitespace. */
18778 static int
18779 trailing_whitespace_p (ptrdiff_t charpos)
18781 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18782 int c = 0;
18784 while (bytepos < ZV_BYTE
18785 && (c = FETCH_CHAR (bytepos),
18786 c == ' ' || c == '\t'))
18787 ++bytepos;
18789 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18791 if (bytepos != PT_BYTE)
18792 return 1;
18794 return 0;
18798 /* Highlight trailing whitespace, if any, in ROW. */
18800 static void
18801 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18803 int used = row->used[TEXT_AREA];
18805 if (used)
18807 struct glyph *start = row->glyphs[TEXT_AREA];
18808 struct glyph *glyph = start + used - 1;
18810 if (row->reversed_p)
18812 /* Right-to-left rows need to be processed in the opposite
18813 direction, so swap the edge pointers. */
18814 glyph = start;
18815 start = row->glyphs[TEXT_AREA] + used - 1;
18818 /* Skip over glyphs inserted to display the cursor at the
18819 end of a line, for extending the face of the last glyph
18820 to the end of the line on terminals, and for truncation
18821 and continuation glyphs. */
18822 if (!row->reversed_p)
18824 while (glyph >= start
18825 && glyph->type == CHAR_GLYPH
18826 && INTEGERP (glyph->object))
18827 --glyph;
18829 else
18831 while (glyph <= start
18832 && glyph->type == CHAR_GLYPH
18833 && INTEGERP (glyph->object))
18834 ++glyph;
18837 /* If last glyph is a space or stretch, and it's trailing
18838 whitespace, set the face of all trailing whitespace glyphs in
18839 IT->glyph_row to `trailing-whitespace'. */
18840 if ((row->reversed_p ? glyph <= start : glyph >= start)
18841 && BUFFERP (glyph->object)
18842 && (glyph->type == STRETCH_GLYPH
18843 || (glyph->type == CHAR_GLYPH
18844 && glyph->u.ch == ' '))
18845 && trailing_whitespace_p (glyph->charpos))
18847 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18848 if (face_id < 0)
18849 return;
18851 if (!row->reversed_p)
18853 while (glyph >= start
18854 && BUFFERP (glyph->object)
18855 && (glyph->type == STRETCH_GLYPH
18856 || (glyph->type == CHAR_GLYPH
18857 && glyph->u.ch == ' ')))
18858 (glyph--)->face_id = face_id;
18860 else
18862 while (glyph <= start
18863 && BUFFERP (glyph->object)
18864 && (glyph->type == STRETCH_GLYPH
18865 || (glyph->type == CHAR_GLYPH
18866 && glyph->u.ch == ' ')))
18867 (glyph++)->face_id = face_id;
18874 /* Value is non-zero if glyph row ROW should be
18875 considered to hold the buffer position CHARPOS. */
18877 static int
18878 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18880 int result = 1;
18882 if (charpos == CHARPOS (row->end.pos)
18883 || charpos == MATRIX_ROW_END_CHARPOS (row))
18885 /* Suppose the row ends on a string.
18886 Unless the row is continued, that means it ends on a newline
18887 in the string. If it's anything other than a display string
18888 (e.g., a before-string from an overlay), we don't want the
18889 cursor there. (This heuristic seems to give the optimal
18890 behavior for the various types of multi-line strings.)
18891 One exception: if the string has `cursor' property on one of
18892 its characters, we _do_ want the cursor there. */
18893 if (CHARPOS (row->end.string_pos) >= 0)
18895 if (row->continued_p)
18896 result = 1;
18897 else
18899 /* Check for `display' property. */
18900 struct glyph *beg = row->glyphs[TEXT_AREA];
18901 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18902 struct glyph *glyph;
18904 result = 0;
18905 for (glyph = end; glyph >= beg; --glyph)
18906 if (STRINGP (glyph->object))
18908 Lisp_Object prop
18909 = Fget_char_property (make_number (charpos),
18910 Qdisplay, Qnil);
18911 result =
18912 (!NILP (prop)
18913 && display_prop_string_p (prop, glyph->object));
18914 /* If there's a `cursor' property on one of the
18915 string's characters, this row is a cursor row,
18916 even though this is not a display string. */
18917 if (!result)
18919 Lisp_Object s = glyph->object;
18921 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18923 ptrdiff_t gpos = glyph->charpos;
18925 if (!NILP (Fget_char_property (make_number (gpos),
18926 Qcursor, s)))
18928 result = 1;
18929 break;
18933 break;
18937 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18939 /* If the row ends in middle of a real character,
18940 and the line is continued, we want the cursor here.
18941 That's because CHARPOS (ROW->end.pos) would equal
18942 PT if PT is before the character. */
18943 if (!row->ends_in_ellipsis_p)
18944 result = row->continued_p;
18945 else
18946 /* If the row ends in an ellipsis, then
18947 CHARPOS (ROW->end.pos) will equal point after the
18948 invisible text. We want that position to be displayed
18949 after the ellipsis. */
18950 result = 0;
18952 /* If the row ends at ZV, display the cursor at the end of that
18953 row instead of at the start of the row below. */
18954 else if (row->ends_at_zv_p)
18955 result = 1;
18956 else
18957 result = 0;
18960 return result;
18963 /* Value is non-zero if glyph row ROW should be
18964 used to hold the cursor. */
18966 static int
18967 cursor_row_p (struct glyph_row *row)
18969 return row_for_charpos_p (row, PT);
18974 /* Push the property PROP so that it will be rendered at the current
18975 position in IT. Return 1 if PROP was successfully pushed, 0
18976 otherwise. Called from handle_line_prefix to handle the
18977 `line-prefix' and `wrap-prefix' properties. */
18979 static int
18980 push_prefix_prop (struct it *it, Lisp_Object prop)
18982 struct text_pos pos =
18983 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18985 eassert (it->method == GET_FROM_BUFFER
18986 || it->method == GET_FROM_DISPLAY_VECTOR
18987 || it->method == GET_FROM_STRING);
18989 /* We need to save the current buffer/string position, so it will be
18990 restored by pop_it, because iterate_out_of_display_property
18991 depends on that being set correctly, but some situations leave
18992 it->position not yet set when this function is called. */
18993 push_it (it, &pos);
18995 if (STRINGP (prop))
18997 if (SCHARS (prop) == 0)
18999 pop_it (it);
19000 return 0;
19003 it->string = prop;
19004 it->string_from_prefix_prop_p = 1;
19005 it->multibyte_p = STRING_MULTIBYTE (it->string);
19006 it->current.overlay_string_index = -1;
19007 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19008 it->end_charpos = it->string_nchars = SCHARS (it->string);
19009 it->method = GET_FROM_STRING;
19010 it->stop_charpos = 0;
19011 it->prev_stop = 0;
19012 it->base_level_stop = 0;
19014 /* Force paragraph direction to be that of the parent
19015 buffer/string. */
19016 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19017 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19018 else
19019 it->paragraph_embedding = L2R;
19021 /* Set up the bidi iterator for this display string. */
19022 if (it->bidi_p)
19024 it->bidi_it.string.lstring = it->string;
19025 it->bidi_it.string.s = NULL;
19026 it->bidi_it.string.schars = it->end_charpos;
19027 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19028 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19029 it->bidi_it.string.unibyte = !it->multibyte_p;
19030 it->bidi_it.w = it->w;
19031 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19034 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19036 it->method = GET_FROM_STRETCH;
19037 it->object = prop;
19039 #ifdef HAVE_WINDOW_SYSTEM
19040 else if (IMAGEP (prop))
19042 it->what = IT_IMAGE;
19043 it->image_id = lookup_image (it->f, prop);
19044 it->method = GET_FROM_IMAGE;
19046 #endif /* HAVE_WINDOW_SYSTEM */
19047 else
19049 pop_it (it); /* bogus display property, give up */
19050 return 0;
19053 return 1;
19056 /* Return the character-property PROP at the current position in IT. */
19058 static Lisp_Object
19059 get_it_property (struct it *it, Lisp_Object prop)
19061 Lisp_Object position, object = it->object;
19063 if (STRINGP (object))
19064 position = make_number (IT_STRING_CHARPOS (*it));
19065 else if (BUFFERP (object))
19067 position = make_number (IT_CHARPOS (*it));
19068 object = it->window;
19070 else
19071 return Qnil;
19073 return Fget_char_property (position, prop, object);
19076 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19078 static void
19079 handle_line_prefix (struct it *it)
19081 Lisp_Object prefix;
19083 if (it->continuation_lines_width > 0)
19085 prefix = get_it_property (it, Qwrap_prefix);
19086 if (NILP (prefix))
19087 prefix = Vwrap_prefix;
19089 else
19091 prefix = get_it_property (it, Qline_prefix);
19092 if (NILP (prefix))
19093 prefix = Vline_prefix;
19095 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19097 /* If the prefix is wider than the window, and we try to wrap
19098 it, it would acquire its own wrap prefix, and so on till the
19099 iterator stack overflows. So, don't wrap the prefix. */
19100 it->line_wrap = TRUNCATE;
19101 it->avoid_cursor_p = 1;
19107 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19108 only for R2L lines from display_line and display_string, when they
19109 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19110 the line/string needs to be continued on the next glyph row. */
19111 static void
19112 unproduce_glyphs (struct it *it, int n)
19114 struct glyph *glyph, *end;
19116 eassert (it->glyph_row);
19117 eassert (it->glyph_row->reversed_p);
19118 eassert (it->area == TEXT_AREA);
19119 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19121 if (n > it->glyph_row->used[TEXT_AREA])
19122 n = it->glyph_row->used[TEXT_AREA];
19123 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19124 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19125 for ( ; glyph < end; glyph++)
19126 glyph[-n] = *glyph;
19129 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19130 and ROW->maxpos. */
19131 static void
19132 find_row_edges (struct it *it, struct glyph_row *row,
19133 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19134 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19136 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19137 lines' rows is implemented for bidi-reordered rows. */
19139 /* ROW->minpos is the value of min_pos, the minimal buffer position
19140 we have in ROW, or ROW->start.pos if that is smaller. */
19141 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19142 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19143 else
19144 /* We didn't find buffer positions smaller than ROW->start, or
19145 didn't find _any_ valid buffer positions in any of the glyphs,
19146 so we must trust the iterator's computed positions. */
19147 row->minpos = row->start.pos;
19148 if (max_pos <= 0)
19150 max_pos = CHARPOS (it->current.pos);
19151 max_bpos = BYTEPOS (it->current.pos);
19154 /* Here are the various use-cases for ending the row, and the
19155 corresponding values for ROW->maxpos:
19157 Line ends in a newline from buffer eol_pos + 1
19158 Line is continued from buffer max_pos + 1
19159 Line is truncated on right it->current.pos
19160 Line ends in a newline from string max_pos + 1(*)
19161 (*) + 1 only when line ends in a forward scan
19162 Line is continued from string max_pos
19163 Line is continued from display vector max_pos
19164 Line is entirely from a string min_pos == max_pos
19165 Line is entirely from a display vector min_pos == max_pos
19166 Line that ends at ZV ZV
19168 If you discover other use-cases, please add them here as
19169 appropriate. */
19170 if (row->ends_at_zv_p)
19171 row->maxpos = it->current.pos;
19172 else if (row->used[TEXT_AREA])
19174 int seen_this_string = 0;
19175 struct glyph_row *r1 = row - 1;
19177 /* Did we see the same display string on the previous row? */
19178 if (STRINGP (it->object)
19179 /* this is not the first row */
19180 && row > it->w->desired_matrix->rows
19181 /* previous row is not the header line */
19182 && !r1->mode_line_p
19183 /* previous row also ends in a newline from a string */
19184 && r1->ends_in_newline_from_string_p)
19186 struct glyph *start, *end;
19188 /* Search for the last glyph of the previous row that came
19189 from buffer or string. Depending on whether the row is
19190 L2R or R2L, we need to process it front to back or the
19191 other way round. */
19192 if (!r1->reversed_p)
19194 start = r1->glyphs[TEXT_AREA];
19195 end = start + r1->used[TEXT_AREA];
19196 /* Glyphs inserted by redisplay have an integer (zero)
19197 as their object. */
19198 while (end > start
19199 && INTEGERP ((end - 1)->object)
19200 && (end - 1)->charpos <= 0)
19201 --end;
19202 if (end > start)
19204 if (EQ ((end - 1)->object, it->object))
19205 seen_this_string = 1;
19207 else
19208 /* If all the glyphs of the previous row were inserted
19209 by redisplay, it means the previous row was
19210 produced from a single newline, which is only
19211 possible if that newline came from the same string
19212 as the one which produced this ROW. */
19213 seen_this_string = 1;
19215 else
19217 end = r1->glyphs[TEXT_AREA] - 1;
19218 start = end + r1->used[TEXT_AREA];
19219 while (end < start
19220 && INTEGERP ((end + 1)->object)
19221 && (end + 1)->charpos <= 0)
19222 ++end;
19223 if (end < start)
19225 if (EQ ((end + 1)->object, it->object))
19226 seen_this_string = 1;
19228 else
19229 seen_this_string = 1;
19232 /* Take note of each display string that covers a newline only
19233 once, the first time we see it. This is for when a display
19234 string includes more than one newline in it. */
19235 if (row->ends_in_newline_from_string_p && !seen_this_string)
19237 /* If we were scanning the buffer forward when we displayed
19238 the string, we want to account for at least one buffer
19239 position that belongs to this row (position covered by
19240 the display string), so that cursor positioning will
19241 consider this row as a candidate when point is at the end
19242 of the visual line represented by this row. This is not
19243 required when scanning back, because max_pos will already
19244 have a much larger value. */
19245 if (CHARPOS (row->end.pos) > max_pos)
19246 INC_BOTH (max_pos, max_bpos);
19247 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19249 else if (CHARPOS (it->eol_pos) > 0)
19250 SET_TEXT_POS (row->maxpos,
19251 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19252 else if (row->continued_p)
19254 /* If max_pos is different from IT's current position, it
19255 means IT->method does not belong to the display element
19256 at max_pos. However, it also means that the display
19257 element at max_pos was displayed in its entirety on this
19258 line, which is equivalent to saying that the next line
19259 starts at the next buffer position. */
19260 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19261 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19262 else
19264 INC_BOTH (max_pos, max_bpos);
19265 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19268 else if (row->truncated_on_right_p)
19269 /* display_line already called reseat_at_next_visible_line_start,
19270 which puts the iterator at the beginning of the next line, in
19271 the logical order. */
19272 row->maxpos = it->current.pos;
19273 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19274 /* A line that is entirely from a string/image/stretch... */
19275 row->maxpos = row->minpos;
19276 else
19277 emacs_abort ();
19279 else
19280 row->maxpos = it->current.pos;
19283 /* Construct the glyph row IT->glyph_row in the desired matrix of
19284 IT->w from text at the current position of IT. See dispextern.h
19285 for an overview of struct it. Value is non-zero if
19286 IT->glyph_row displays text, as opposed to a line displaying ZV
19287 only. */
19289 static int
19290 display_line (struct it *it)
19292 struct glyph_row *row = it->glyph_row;
19293 Lisp_Object overlay_arrow_string;
19294 struct it wrap_it;
19295 void *wrap_data = NULL;
19296 int may_wrap = 0, wrap_x IF_LINT (= 0);
19297 int wrap_row_used = -1;
19298 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19299 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19300 int wrap_row_extra_line_spacing IF_LINT (= 0);
19301 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19302 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19303 int cvpos;
19304 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19305 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19307 /* We always start displaying at hpos zero even if hscrolled. */
19308 eassert (it->hpos == 0 && it->current_x == 0);
19310 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19311 >= it->w->desired_matrix->nrows)
19313 it->w->nrows_scale_factor++;
19314 fonts_changed_p = 1;
19315 return 0;
19318 /* Is IT->w showing the region? */
19319 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19321 /* Clear the result glyph row and enable it. */
19322 prepare_desired_row (row);
19324 row->y = it->current_y;
19325 row->start = it->start;
19326 row->continuation_lines_width = it->continuation_lines_width;
19327 row->displays_text_p = 1;
19328 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19329 it->starts_in_middle_of_char_p = 0;
19331 /* Arrange the overlays nicely for our purposes. Usually, we call
19332 display_line on only one line at a time, in which case this
19333 can't really hurt too much, or we call it on lines which appear
19334 one after another in the buffer, in which case all calls to
19335 recenter_overlay_lists but the first will be pretty cheap. */
19336 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19338 /* Move over display elements that are not visible because we are
19339 hscrolled. This may stop at an x-position < IT->first_visible_x
19340 if the first glyph is partially visible or if we hit a line end. */
19341 if (it->current_x < it->first_visible_x)
19343 enum move_it_result move_result;
19345 this_line_min_pos = row->start.pos;
19346 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19347 MOVE_TO_POS | MOVE_TO_X);
19348 /* If we are under a large hscroll, move_it_in_display_line_to
19349 could hit the end of the line without reaching
19350 it->first_visible_x. Pretend that we did reach it. This is
19351 especially important on a TTY, where we will call
19352 extend_face_to_end_of_line, which needs to know how many
19353 blank glyphs to produce. */
19354 if (it->current_x < it->first_visible_x
19355 && (move_result == MOVE_NEWLINE_OR_CR
19356 || move_result == MOVE_POS_MATCH_OR_ZV))
19357 it->current_x = it->first_visible_x;
19359 /* Record the smallest positions seen while we moved over
19360 display elements that are not visible. This is needed by
19361 redisplay_internal for optimizing the case where the cursor
19362 stays inside the same line. The rest of this function only
19363 considers positions that are actually displayed, so
19364 RECORD_MAX_MIN_POS will not otherwise record positions that
19365 are hscrolled to the left of the left edge of the window. */
19366 min_pos = CHARPOS (this_line_min_pos);
19367 min_bpos = BYTEPOS (this_line_min_pos);
19369 else
19371 /* We only do this when not calling `move_it_in_display_line_to'
19372 above, because move_it_in_display_line_to calls
19373 handle_line_prefix itself. */
19374 handle_line_prefix (it);
19377 /* Get the initial row height. This is either the height of the
19378 text hscrolled, if there is any, or zero. */
19379 row->ascent = it->max_ascent;
19380 row->height = it->max_ascent + it->max_descent;
19381 row->phys_ascent = it->max_phys_ascent;
19382 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19383 row->extra_line_spacing = it->max_extra_line_spacing;
19385 /* Utility macro to record max and min buffer positions seen until now. */
19386 #define RECORD_MAX_MIN_POS(IT) \
19387 do \
19389 int composition_p = !STRINGP ((IT)->string) \
19390 && ((IT)->what == IT_COMPOSITION); \
19391 ptrdiff_t current_pos = \
19392 composition_p ? (IT)->cmp_it.charpos \
19393 : IT_CHARPOS (*(IT)); \
19394 ptrdiff_t current_bpos = \
19395 composition_p ? CHAR_TO_BYTE (current_pos) \
19396 : IT_BYTEPOS (*(IT)); \
19397 if (current_pos < min_pos) \
19399 min_pos = current_pos; \
19400 min_bpos = current_bpos; \
19402 if (IT_CHARPOS (*it) > max_pos) \
19404 max_pos = IT_CHARPOS (*it); \
19405 max_bpos = IT_BYTEPOS (*it); \
19408 while (0)
19410 /* Loop generating characters. The loop is left with IT on the next
19411 character to display. */
19412 while (1)
19414 int n_glyphs_before, hpos_before, x_before;
19415 int x, nglyphs;
19416 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19418 /* Retrieve the next thing to display. Value is zero if end of
19419 buffer reached. */
19420 if (!get_next_display_element (it))
19422 /* Maybe add a space at the end of this line that is used to
19423 display the cursor there under X. Set the charpos of the
19424 first glyph of blank lines not corresponding to any text
19425 to -1. */
19426 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19427 row->exact_window_width_line_p = 1;
19428 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19429 || row->used[TEXT_AREA] == 0)
19431 row->glyphs[TEXT_AREA]->charpos = -1;
19432 row->displays_text_p = 0;
19434 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19435 && (!MINI_WINDOW_P (it->w)
19436 || (minibuf_level && EQ (it->window, minibuf_window))))
19437 row->indicate_empty_line_p = 1;
19440 it->continuation_lines_width = 0;
19441 row->ends_at_zv_p = 1;
19442 /* A row that displays right-to-left text must always have
19443 its last face extended all the way to the end of line,
19444 even if this row ends in ZV, because we still write to
19445 the screen left to right. We also need to extend the
19446 last face if the default face is remapped to some
19447 different face, otherwise the functions that clear
19448 portions of the screen will clear with the default face's
19449 background color. */
19450 if (row->reversed_p
19451 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19452 extend_face_to_end_of_line (it);
19453 break;
19456 /* Now, get the metrics of what we want to display. This also
19457 generates glyphs in `row' (which is IT->glyph_row). */
19458 n_glyphs_before = row->used[TEXT_AREA];
19459 x = it->current_x;
19461 /* Remember the line height so far in case the next element doesn't
19462 fit on the line. */
19463 if (it->line_wrap != TRUNCATE)
19465 ascent = it->max_ascent;
19466 descent = it->max_descent;
19467 phys_ascent = it->max_phys_ascent;
19468 phys_descent = it->max_phys_descent;
19470 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19472 if (IT_DISPLAYING_WHITESPACE (it))
19473 may_wrap = 1;
19474 else if (may_wrap)
19476 SAVE_IT (wrap_it, *it, wrap_data);
19477 wrap_x = x;
19478 wrap_row_used = row->used[TEXT_AREA];
19479 wrap_row_ascent = row->ascent;
19480 wrap_row_height = row->height;
19481 wrap_row_phys_ascent = row->phys_ascent;
19482 wrap_row_phys_height = row->phys_height;
19483 wrap_row_extra_line_spacing = row->extra_line_spacing;
19484 wrap_row_min_pos = min_pos;
19485 wrap_row_min_bpos = min_bpos;
19486 wrap_row_max_pos = max_pos;
19487 wrap_row_max_bpos = max_bpos;
19488 may_wrap = 0;
19493 PRODUCE_GLYPHS (it);
19495 /* If this display element was in marginal areas, continue with
19496 the next one. */
19497 if (it->area != TEXT_AREA)
19499 row->ascent = max (row->ascent, it->max_ascent);
19500 row->height = max (row->height, it->max_ascent + it->max_descent);
19501 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19502 row->phys_height = max (row->phys_height,
19503 it->max_phys_ascent + it->max_phys_descent);
19504 row->extra_line_spacing = max (row->extra_line_spacing,
19505 it->max_extra_line_spacing);
19506 set_iterator_to_next (it, 1);
19507 continue;
19510 /* Does the display element fit on the line? If we truncate
19511 lines, we should draw past the right edge of the window. If
19512 we don't truncate, we want to stop so that we can display the
19513 continuation glyph before the right margin. If lines are
19514 continued, there are two possible strategies for characters
19515 resulting in more than 1 glyph (e.g. tabs): Display as many
19516 glyphs as possible in this line and leave the rest for the
19517 continuation line, or display the whole element in the next
19518 line. Original redisplay did the former, so we do it also. */
19519 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19520 hpos_before = it->hpos;
19521 x_before = x;
19523 if (/* Not a newline. */
19524 nglyphs > 0
19525 /* Glyphs produced fit entirely in the line. */
19526 && it->current_x < it->last_visible_x)
19528 it->hpos += nglyphs;
19529 row->ascent = max (row->ascent, it->max_ascent);
19530 row->height = max (row->height, it->max_ascent + it->max_descent);
19531 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19532 row->phys_height = max (row->phys_height,
19533 it->max_phys_ascent + it->max_phys_descent);
19534 row->extra_line_spacing = max (row->extra_line_spacing,
19535 it->max_extra_line_spacing);
19536 if (it->current_x - it->pixel_width < it->first_visible_x)
19537 row->x = x - it->first_visible_x;
19538 /* Record the maximum and minimum buffer positions seen so
19539 far in glyphs that will be displayed by this row. */
19540 if (it->bidi_p)
19541 RECORD_MAX_MIN_POS (it);
19543 else
19545 int i, new_x;
19546 struct glyph *glyph;
19548 for (i = 0; i < nglyphs; ++i, x = new_x)
19550 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19551 new_x = x + glyph->pixel_width;
19553 if (/* Lines are continued. */
19554 it->line_wrap != TRUNCATE
19555 && (/* Glyph doesn't fit on the line. */
19556 new_x > it->last_visible_x
19557 /* Or it fits exactly on a window system frame. */
19558 || (new_x == it->last_visible_x
19559 && FRAME_WINDOW_P (it->f)
19560 && (row->reversed_p
19561 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19562 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19564 /* End of a continued line. */
19566 if (it->hpos == 0
19567 || (new_x == it->last_visible_x
19568 && FRAME_WINDOW_P (it->f)
19569 && (row->reversed_p
19570 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19571 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19573 /* Current glyph is the only one on the line or
19574 fits exactly on the line. We must continue
19575 the line because we can't draw the cursor
19576 after the glyph. */
19577 row->continued_p = 1;
19578 it->current_x = new_x;
19579 it->continuation_lines_width += new_x;
19580 ++it->hpos;
19581 if (i == nglyphs - 1)
19583 /* If line-wrap is on, check if a previous
19584 wrap point was found. */
19585 if (wrap_row_used > 0
19586 /* Even if there is a previous wrap
19587 point, continue the line here as
19588 usual, if (i) the previous character
19589 was a space or tab AND (ii) the
19590 current character is not. */
19591 && (!may_wrap
19592 || IT_DISPLAYING_WHITESPACE (it)))
19593 goto back_to_wrap;
19595 /* Record the maximum and minimum buffer
19596 positions seen so far in glyphs that will be
19597 displayed by this row. */
19598 if (it->bidi_p)
19599 RECORD_MAX_MIN_POS (it);
19600 set_iterator_to_next (it, 1);
19601 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19603 if (!get_next_display_element (it))
19605 row->exact_window_width_line_p = 1;
19606 it->continuation_lines_width = 0;
19607 row->continued_p = 0;
19608 row->ends_at_zv_p = 1;
19610 else if (ITERATOR_AT_END_OF_LINE_P (it))
19612 row->continued_p = 0;
19613 row->exact_window_width_line_p = 1;
19617 else if (it->bidi_p)
19618 RECORD_MAX_MIN_POS (it);
19620 else if (CHAR_GLYPH_PADDING_P (*glyph)
19621 && !FRAME_WINDOW_P (it->f))
19623 /* A padding glyph that doesn't fit on this line.
19624 This means the whole character doesn't fit
19625 on the line. */
19626 if (row->reversed_p)
19627 unproduce_glyphs (it, row->used[TEXT_AREA]
19628 - n_glyphs_before);
19629 row->used[TEXT_AREA] = n_glyphs_before;
19631 /* Fill the rest of the row with continuation
19632 glyphs like in 20.x. */
19633 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19634 < row->glyphs[1 + TEXT_AREA])
19635 produce_special_glyphs (it, IT_CONTINUATION);
19637 row->continued_p = 1;
19638 it->current_x = x_before;
19639 it->continuation_lines_width += x_before;
19641 /* Restore the height to what it was before the
19642 element not fitting on the line. */
19643 it->max_ascent = ascent;
19644 it->max_descent = descent;
19645 it->max_phys_ascent = phys_ascent;
19646 it->max_phys_descent = phys_descent;
19648 else if (wrap_row_used > 0)
19650 back_to_wrap:
19651 if (row->reversed_p)
19652 unproduce_glyphs (it,
19653 row->used[TEXT_AREA] - wrap_row_used);
19654 RESTORE_IT (it, &wrap_it, wrap_data);
19655 it->continuation_lines_width += wrap_x;
19656 row->used[TEXT_AREA] = wrap_row_used;
19657 row->ascent = wrap_row_ascent;
19658 row->height = wrap_row_height;
19659 row->phys_ascent = wrap_row_phys_ascent;
19660 row->phys_height = wrap_row_phys_height;
19661 row->extra_line_spacing = wrap_row_extra_line_spacing;
19662 min_pos = wrap_row_min_pos;
19663 min_bpos = wrap_row_min_bpos;
19664 max_pos = wrap_row_max_pos;
19665 max_bpos = wrap_row_max_bpos;
19666 row->continued_p = 1;
19667 row->ends_at_zv_p = 0;
19668 row->exact_window_width_line_p = 0;
19669 it->continuation_lines_width += x;
19671 /* Make sure that a non-default face is extended
19672 up to the right margin of the window. */
19673 extend_face_to_end_of_line (it);
19675 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19677 /* A TAB that extends past the right edge of the
19678 window. This produces a single glyph on
19679 window system frames. We leave the glyph in
19680 this row and let it fill the row, but don't
19681 consume the TAB. */
19682 if ((row->reversed_p
19683 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19684 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19685 produce_special_glyphs (it, IT_CONTINUATION);
19686 it->continuation_lines_width += it->last_visible_x;
19687 row->ends_in_middle_of_char_p = 1;
19688 row->continued_p = 1;
19689 glyph->pixel_width = it->last_visible_x - x;
19690 it->starts_in_middle_of_char_p = 1;
19692 else
19694 /* Something other than a TAB that draws past
19695 the right edge of the window. Restore
19696 positions to values before the element. */
19697 if (row->reversed_p)
19698 unproduce_glyphs (it, row->used[TEXT_AREA]
19699 - (n_glyphs_before + i));
19700 row->used[TEXT_AREA] = n_glyphs_before + i;
19702 /* Display continuation glyphs. */
19703 it->current_x = x_before;
19704 it->continuation_lines_width += x;
19705 if (!FRAME_WINDOW_P (it->f)
19706 || (row->reversed_p
19707 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19708 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19709 produce_special_glyphs (it, IT_CONTINUATION);
19710 row->continued_p = 1;
19712 extend_face_to_end_of_line (it);
19714 if (nglyphs > 1 && i > 0)
19716 row->ends_in_middle_of_char_p = 1;
19717 it->starts_in_middle_of_char_p = 1;
19720 /* Restore the height to what it was before the
19721 element not fitting on the line. */
19722 it->max_ascent = ascent;
19723 it->max_descent = descent;
19724 it->max_phys_ascent = phys_ascent;
19725 it->max_phys_descent = phys_descent;
19728 break;
19730 else if (new_x > it->first_visible_x)
19732 /* Increment number of glyphs actually displayed. */
19733 ++it->hpos;
19735 /* Record the maximum and minimum buffer positions
19736 seen so far in glyphs that will be displayed by
19737 this row. */
19738 if (it->bidi_p)
19739 RECORD_MAX_MIN_POS (it);
19741 if (x < it->first_visible_x)
19742 /* Glyph is partially visible, i.e. row starts at
19743 negative X position. */
19744 row->x = x - it->first_visible_x;
19746 else
19748 /* Glyph is completely off the left margin of the
19749 window. This should not happen because of the
19750 move_it_in_display_line at the start of this
19751 function, unless the text display area of the
19752 window is empty. */
19753 eassert (it->first_visible_x <= it->last_visible_x);
19756 /* Even if this display element produced no glyphs at all,
19757 we want to record its position. */
19758 if (it->bidi_p && nglyphs == 0)
19759 RECORD_MAX_MIN_POS (it);
19761 row->ascent = max (row->ascent, it->max_ascent);
19762 row->height = max (row->height, it->max_ascent + it->max_descent);
19763 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19764 row->phys_height = max (row->phys_height,
19765 it->max_phys_ascent + it->max_phys_descent);
19766 row->extra_line_spacing = max (row->extra_line_spacing,
19767 it->max_extra_line_spacing);
19769 /* End of this display line if row is continued. */
19770 if (row->continued_p || row->ends_at_zv_p)
19771 break;
19774 at_end_of_line:
19775 /* Is this a line end? If yes, we're also done, after making
19776 sure that a non-default face is extended up to the right
19777 margin of the window. */
19778 if (ITERATOR_AT_END_OF_LINE_P (it))
19780 int used_before = row->used[TEXT_AREA];
19782 row->ends_in_newline_from_string_p = STRINGP (it->object);
19784 /* Add a space at the end of the line that is used to
19785 display the cursor there. */
19786 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19787 append_space_for_newline (it, 0);
19789 /* Extend the face to the end of the line. */
19790 extend_face_to_end_of_line (it);
19792 /* Make sure we have the position. */
19793 if (used_before == 0)
19794 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19796 /* Record the position of the newline, for use in
19797 find_row_edges. */
19798 it->eol_pos = it->current.pos;
19800 /* Consume the line end. This skips over invisible lines. */
19801 set_iterator_to_next (it, 1);
19802 it->continuation_lines_width = 0;
19803 break;
19806 /* Proceed with next display element. Note that this skips
19807 over lines invisible because of selective display. */
19808 set_iterator_to_next (it, 1);
19810 /* If we truncate lines, we are done when the last displayed
19811 glyphs reach past the right margin of the window. */
19812 if (it->line_wrap == TRUNCATE
19813 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19814 ? (it->current_x >= it->last_visible_x)
19815 : (it->current_x > it->last_visible_x)))
19817 /* Maybe add truncation glyphs. */
19818 if (!FRAME_WINDOW_P (it->f)
19819 || (row->reversed_p
19820 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19821 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19823 int i, n;
19825 if (!row->reversed_p)
19827 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19828 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19829 break;
19831 else
19833 for (i = 0; i < row->used[TEXT_AREA]; i++)
19834 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19835 break;
19836 /* Remove any padding glyphs at the front of ROW, to
19837 make room for the truncation glyphs we will be
19838 adding below. The loop below always inserts at
19839 least one truncation glyph, so also remove the
19840 last glyph added to ROW. */
19841 unproduce_glyphs (it, i + 1);
19842 /* Adjust i for the loop below. */
19843 i = row->used[TEXT_AREA] - (i + 1);
19846 it->current_x = x_before;
19847 if (!FRAME_WINDOW_P (it->f))
19849 for (n = row->used[TEXT_AREA]; i < n; ++i)
19851 row->used[TEXT_AREA] = i;
19852 produce_special_glyphs (it, IT_TRUNCATION);
19855 else
19857 row->used[TEXT_AREA] = i;
19858 produce_special_glyphs (it, IT_TRUNCATION);
19861 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19863 /* Don't truncate if we can overflow newline into fringe. */
19864 if (!get_next_display_element (it))
19866 it->continuation_lines_width = 0;
19867 row->ends_at_zv_p = 1;
19868 row->exact_window_width_line_p = 1;
19869 break;
19871 if (ITERATOR_AT_END_OF_LINE_P (it))
19873 row->exact_window_width_line_p = 1;
19874 goto at_end_of_line;
19876 it->current_x = x_before;
19879 row->truncated_on_right_p = 1;
19880 it->continuation_lines_width = 0;
19881 reseat_at_next_visible_line_start (it, 0);
19882 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19883 it->hpos = hpos_before;
19884 break;
19888 if (wrap_data)
19889 bidi_unshelve_cache (wrap_data, 1);
19891 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19892 at the left window margin. */
19893 if (it->first_visible_x
19894 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19896 if (!FRAME_WINDOW_P (it->f)
19897 || (row->reversed_p
19898 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19899 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19900 insert_left_trunc_glyphs (it);
19901 row->truncated_on_left_p = 1;
19904 /* Remember the position at which this line ends.
19906 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19907 cannot be before the call to find_row_edges below, since that is
19908 where these positions are determined. */
19909 row->end = it->current;
19910 if (!it->bidi_p)
19912 row->minpos = row->start.pos;
19913 row->maxpos = row->end.pos;
19915 else
19917 /* ROW->minpos and ROW->maxpos must be the smallest and
19918 `1 + the largest' buffer positions in ROW. But if ROW was
19919 bidi-reordered, these two positions can be anywhere in the
19920 row, so we must determine them now. */
19921 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19924 /* If the start of this line is the overlay arrow-position, then
19925 mark this glyph row as the one containing the overlay arrow.
19926 This is clearly a mess with variable size fonts. It would be
19927 better to let it be displayed like cursors under X. */
19928 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19929 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19930 !NILP (overlay_arrow_string)))
19932 /* Overlay arrow in window redisplay is a fringe bitmap. */
19933 if (STRINGP (overlay_arrow_string))
19935 struct glyph_row *arrow_row
19936 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19937 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19938 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19939 struct glyph *p = row->glyphs[TEXT_AREA];
19940 struct glyph *p2, *end;
19942 /* Copy the arrow glyphs. */
19943 while (glyph < arrow_end)
19944 *p++ = *glyph++;
19946 /* Throw away padding glyphs. */
19947 p2 = p;
19948 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19949 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19950 ++p2;
19951 if (p2 > p)
19953 while (p2 < end)
19954 *p++ = *p2++;
19955 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19958 else
19960 eassert (INTEGERP (overlay_arrow_string));
19961 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19963 overlay_arrow_seen = 1;
19966 /* Highlight trailing whitespace. */
19967 if (!NILP (Vshow_trailing_whitespace))
19968 highlight_trailing_whitespace (it->f, it->glyph_row);
19970 /* Compute pixel dimensions of this line. */
19971 compute_line_metrics (it);
19973 /* Implementation note: No changes in the glyphs of ROW or in their
19974 faces can be done past this point, because compute_line_metrics
19975 computes ROW's hash value and stores it within the glyph_row
19976 structure. */
19978 /* Record whether this row ends inside an ellipsis. */
19979 row->ends_in_ellipsis_p
19980 = (it->method == GET_FROM_DISPLAY_VECTOR
19981 && it->ellipsis_p);
19983 /* Save fringe bitmaps in this row. */
19984 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19985 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19986 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19987 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19989 it->left_user_fringe_bitmap = 0;
19990 it->left_user_fringe_face_id = 0;
19991 it->right_user_fringe_bitmap = 0;
19992 it->right_user_fringe_face_id = 0;
19994 /* Maybe set the cursor. */
19995 cvpos = it->w->cursor.vpos;
19996 if ((cvpos < 0
19997 /* In bidi-reordered rows, keep checking for proper cursor
19998 position even if one has been found already, because buffer
19999 positions in such rows change non-linearly with ROW->VPOS,
20000 when a line is continued. One exception: when we are at ZV,
20001 display cursor on the first suitable glyph row, since all
20002 the empty rows after that also have their position set to ZV. */
20003 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20004 lines' rows is implemented for bidi-reordered rows. */
20005 || (it->bidi_p
20006 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20007 && PT >= MATRIX_ROW_START_CHARPOS (row)
20008 && PT <= MATRIX_ROW_END_CHARPOS (row)
20009 && cursor_row_p (row))
20010 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20012 /* Prepare for the next line. This line starts horizontally at (X
20013 HPOS) = (0 0). Vertical positions are incremented. As a
20014 convenience for the caller, IT->glyph_row is set to the next
20015 row to be used. */
20016 it->current_x = it->hpos = 0;
20017 it->current_y += row->height;
20018 SET_TEXT_POS (it->eol_pos, 0, 0);
20019 ++it->vpos;
20020 ++it->glyph_row;
20021 /* The next row should by default use the same value of the
20022 reversed_p flag as this one. set_iterator_to_next decides when
20023 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20024 the flag accordingly. */
20025 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20026 it->glyph_row->reversed_p = row->reversed_p;
20027 it->start = row->end;
20028 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20030 #undef RECORD_MAX_MIN_POS
20033 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20034 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20035 doc: /* Return paragraph direction at point in BUFFER.
20036 Value is either `left-to-right' or `right-to-left'.
20037 If BUFFER is omitted or nil, it defaults to the current buffer.
20039 Paragraph direction determines how the text in the paragraph is displayed.
20040 In left-to-right paragraphs, text begins at the left margin of the window
20041 and the reading direction is generally left to right. In right-to-left
20042 paragraphs, text begins at the right margin and is read from right to left.
20044 See also `bidi-paragraph-direction'. */)
20045 (Lisp_Object buffer)
20047 struct buffer *buf = current_buffer;
20048 struct buffer *old = buf;
20050 if (! NILP (buffer))
20052 CHECK_BUFFER (buffer);
20053 buf = XBUFFER (buffer);
20056 if (NILP (BVAR (buf, bidi_display_reordering))
20057 || NILP (BVAR (buf, enable_multibyte_characters))
20058 /* When we are loading loadup.el, the character property tables
20059 needed for bidi iteration are not yet available. */
20060 || !NILP (Vpurify_flag))
20061 return Qleft_to_right;
20062 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20063 return BVAR (buf, bidi_paragraph_direction);
20064 else
20066 /* Determine the direction from buffer text. We could try to
20067 use current_matrix if it is up to date, but this seems fast
20068 enough as it is. */
20069 struct bidi_it itb;
20070 ptrdiff_t pos = BUF_PT (buf);
20071 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20072 int c;
20073 void *itb_data = bidi_shelve_cache ();
20075 set_buffer_temp (buf);
20076 /* bidi_paragraph_init finds the base direction of the paragraph
20077 by searching forward from paragraph start. We need the base
20078 direction of the current or _previous_ paragraph, so we need
20079 to make sure we are within that paragraph. To that end, find
20080 the previous non-empty line. */
20081 if (pos >= ZV && pos > BEGV)
20082 DEC_BOTH (pos, bytepos);
20083 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20084 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20086 while ((c = FETCH_BYTE (bytepos)) == '\n'
20087 || c == ' ' || c == '\t' || c == '\f')
20089 if (bytepos <= BEGV_BYTE)
20090 break;
20091 bytepos--;
20092 pos--;
20094 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20095 bytepos--;
20097 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20098 itb.paragraph_dir = NEUTRAL_DIR;
20099 itb.string.s = NULL;
20100 itb.string.lstring = Qnil;
20101 itb.string.bufpos = 0;
20102 itb.string.unibyte = 0;
20103 /* We have no window to use here for ignoring window-specific
20104 overlays. Using NULL for window pointer will cause
20105 compute_display_string_pos to use the current buffer. */
20106 itb.w = NULL;
20107 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20108 bidi_unshelve_cache (itb_data, 0);
20109 set_buffer_temp (old);
20110 switch (itb.paragraph_dir)
20112 case L2R:
20113 return Qleft_to_right;
20114 break;
20115 case R2L:
20116 return Qright_to_left;
20117 break;
20118 default:
20119 emacs_abort ();
20124 DEFUN ("move-point-visually", Fmove_point_visually,
20125 Smove_point_visually, 1, 1, 0,
20126 doc: /* Move point in the visual order in the specified DIRECTION.
20127 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20128 left.
20130 Value is the new character position of point. */)
20131 (Lisp_Object direction)
20133 struct window *w = XWINDOW (selected_window);
20134 struct buffer *b = XBUFFER (w->contents);
20135 struct glyph_row *row;
20136 int dir;
20137 Lisp_Object paragraph_dir;
20139 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20140 (!(ROW)->continued_p \
20141 && INTEGERP ((GLYPH)->object) \
20142 && (GLYPH)->type == CHAR_GLYPH \
20143 && (GLYPH)->u.ch == ' ' \
20144 && (GLYPH)->charpos >= 0 \
20145 && !(GLYPH)->avoid_cursor_p)
20147 CHECK_NUMBER (direction);
20148 dir = XINT (direction);
20149 if (dir > 0)
20150 dir = 1;
20151 else
20152 dir = -1;
20154 /* If current matrix is up-to-date, we can use the information
20155 recorded in the glyphs, at least as long as the goal is on the
20156 screen. */
20157 if (w->window_end_valid
20158 && !windows_or_buffers_changed
20159 && b
20160 && !b->clip_changed
20161 && !b->prevent_redisplay_optimizations_p
20162 && !window_outdated (w)
20163 && w->cursor.vpos >= 0
20164 && w->cursor.vpos < w->current_matrix->nrows
20165 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20167 struct glyph *g = row->glyphs[TEXT_AREA];
20168 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20169 struct glyph *gpt = g + w->cursor.hpos;
20171 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20173 if (BUFFERP (g->object) && g->charpos != PT)
20175 SET_PT (g->charpos);
20176 w->cursor.vpos = -1;
20177 return make_number (PT);
20179 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20181 ptrdiff_t new_pos;
20183 if (BUFFERP (gpt->object))
20185 new_pos = PT;
20186 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20187 new_pos += (row->reversed_p ? -dir : dir);
20188 else
20189 new_pos -= (row->reversed_p ? -dir : dir);;
20191 else if (BUFFERP (g->object))
20192 new_pos = g->charpos;
20193 else
20194 break;
20195 SET_PT (new_pos);
20196 w->cursor.vpos = -1;
20197 return make_number (PT);
20199 else if (ROW_GLYPH_NEWLINE_P (row, g))
20201 /* Glyphs inserted at the end of a non-empty line for
20202 positioning the cursor have zero charpos, so we must
20203 deduce the value of point by other means. */
20204 if (g->charpos > 0)
20205 SET_PT (g->charpos);
20206 else if (row->ends_at_zv_p && PT != ZV)
20207 SET_PT (ZV);
20208 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20209 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20210 else
20211 break;
20212 w->cursor.vpos = -1;
20213 return make_number (PT);
20216 if (g == e || INTEGERP (g->object))
20218 if (row->truncated_on_left_p || row->truncated_on_right_p)
20219 goto simulate_display;
20220 if (!row->reversed_p)
20221 row += dir;
20222 else
20223 row -= dir;
20224 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20225 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20226 goto simulate_display;
20228 if (dir > 0)
20230 if (row->reversed_p && !row->continued_p)
20232 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20233 w->cursor.vpos = -1;
20234 return make_number (PT);
20236 g = row->glyphs[TEXT_AREA];
20237 e = g + row->used[TEXT_AREA];
20238 for ( ; g < e; g++)
20240 if (BUFFERP (g->object)
20241 /* Empty lines have only one glyph, which stands
20242 for the newline, and whose charpos is the
20243 buffer position of the newline. */
20244 || ROW_GLYPH_NEWLINE_P (row, g)
20245 /* When the buffer ends in a newline, the line at
20246 EOB also has one glyph, but its charpos is -1. */
20247 || (row->ends_at_zv_p
20248 && !row->reversed_p
20249 && INTEGERP (g->object)
20250 && g->type == CHAR_GLYPH
20251 && g->u.ch == ' '))
20253 if (g->charpos > 0)
20254 SET_PT (g->charpos);
20255 else if (!row->reversed_p
20256 && row->ends_at_zv_p
20257 && PT != ZV)
20258 SET_PT (ZV);
20259 else
20260 continue;
20261 w->cursor.vpos = -1;
20262 return make_number (PT);
20266 else
20268 if (!row->reversed_p && !row->continued_p)
20270 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20271 w->cursor.vpos = -1;
20272 return make_number (PT);
20274 e = row->glyphs[TEXT_AREA];
20275 g = e + row->used[TEXT_AREA] - 1;
20276 for ( ; g >= e; g--)
20278 if (BUFFERP (g->object)
20279 || (ROW_GLYPH_NEWLINE_P (row, g)
20280 && g->charpos > 0)
20281 /* Empty R2L lines on GUI frames have the buffer
20282 position of the newline stored in the stretch
20283 glyph. */
20284 || g->type == STRETCH_GLYPH
20285 || (row->ends_at_zv_p
20286 && row->reversed_p
20287 && INTEGERP (g->object)
20288 && g->type == CHAR_GLYPH
20289 && g->u.ch == ' '))
20291 if (g->charpos > 0)
20292 SET_PT (g->charpos);
20293 else if (row->reversed_p
20294 && row->ends_at_zv_p
20295 && PT != ZV)
20296 SET_PT (ZV);
20297 else
20298 continue;
20299 w->cursor.vpos = -1;
20300 return make_number (PT);
20307 simulate_display:
20309 /* If we wind up here, we failed to move by using the glyphs, so we
20310 need to simulate display instead. */
20312 if (b)
20313 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20314 else
20315 paragraph_dir = Qleft_to_right;
20316 if (EQ (paragraph_dir, Qright_to_left))
20317 dir = -dir;
20318 if (PT <= BEGV && dir < 0)
20319 xsignal0 (Qbeginning_of_buffer);
20320 else if (PT >= ZV && dir > 0)
20321 xsignal0 (Qend_of_buffer);
20322 else
20324 struct text_pos pt;
20325 struct it it;
20326 int pt_x, target_x, pixel_width, pt_vpos;
20327 bool at_eol_p;
20328 bool overshoot_expected = false;
20329 bool target_is_eol_p = false;
20331 /* Setup the arena. */
20332 SET_TEXT_POS (pt, PT, PT_BYTE);
20333 start_display (&it, w, pt);
20335 if (it.cmp_it.id < 0
20336 && it.method == GET_FROM_STRING
20337 && it.area == TEXT_AREA
20338 && it.string_from_display_prop_p
20339 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20340 overshoot_expected = true;
20342 /* Find the X coordinate of point. We start from the beginning
20343 of this or previous line to make sure we are before point in
20344 the logical order (since the move_it_* functions can only
20345 move forward). */
20346 reseat_at_previous_visible_line_start (&it);
20347 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20348 if (IT_CHARPOS (it) != PT)
20349 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20350 -1, -1, -1, MOVE_TO_POS);
20351 pt_x = it.current_x;
20352 pt_vpos = it.vpos;
20353 if (dir > 0 || overshoot_expected)
20355 struct glyph_row *row = it.glyph_row;
20357 /* When point is at beginning of line, we don't have
20358 information about the glyph there loaded into struct
20359 it. Calling get_next_display_element fixes that. */
20360 if (pt_x == 0)
20361 get_next_display_element (&it);
20362 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20363 it.glyph_row = NULL;
20364 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20365 it.glyph_row = row;
20366 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20367 it, lest it will become out of sync with it's buffer
20368 position. */
20369 it.current_x = pt_x;
20371 else
20372 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20373 pixel_width = it.pixel_width;
20374 if (overshoot_expected && at_eol_p)
20375 pixel_width = 0;
20376 else if (pixel_width <= 0)
20377 pixel_width = 1;
20379 /* If there's a display string at point, we are actually at the
20380 glyph to the left of point, so we need to correct the X
20381 coordinate. */
20382 if (overshoot_expected)
20383 pt_x += pixel_width;
20385 /* Compute target X coordinate, either to the left or to the
20386 right of point. On TTY frames, all characters have the same
20387 pixel width of 1, so we can use that. On GUI frames we don't
20388 have an easy way of getting at the pixel width of the
20389 character to the left of point, so we use a different method
20390 of getting to that place. */
20391 if (dir > 0)
20392 target_x = pt_x + pixel_width;
20393 else
20394 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20396 /* Target X coordinate could be one line above or below the line
20397 of point, in which case we need to adjust the target X
20398 coordinate. Also, if moving to the left, we need to begin at
20399 the left edge of the point's screen line. */
20400 if (dir < 0)
20402 if (pt_x > 0)
20404 start_display (&it, w, pt);
20405 reseat_at_previous_visible_line_start (&it);
20406 it.current_x = it.current_y = it.hpos = 0;
20407 if (pt_vpos != 0)
20408 move_it_by_lines (&it, pt_vpos);
20410 else
20412 move_it_by_lines (&it, -1);
20413 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20414 target_is_eol_p = true;
20417 else
20419 if (at_eol_p
20420 || (target_x >= it.last_visible_x
20421 && it.line_wrap != TRUNCATE))
20423 if (pt_x > 0)
20424 move_it_by_lines (&it, 0);
20425 move_it_by_lines (&it, 1);
20426 target_x = 0;
20430 /* Move to the target X coordinate. */
20431 #ifdef HAVE_WINDOW_SYSTEM
20432 /* On GUI frames, as we don't know the X coordinate of the
20433 character to the left of point, moving point to the left
20434 requires walking, one grapheme cluster at a time, until we
20435 find ourself at a place immediately to the left of the
20436 character at point. */
20437 if (FRAME_WINDOW_P (it.f) && dir < 0)
20439 struct text_pos new_pos = it.current.pos;
20440 enum move_it_result rc = MOVE_X_REACHED;
20442 while (it.current_x + it.pixel_width <= target_x
20443 && rc == MOVE_X_REACHED)
20445 int new_x = it.current_x + it.pixel_width;
20447 new_pos = it.current.pos;
20448 if (new_x == it.current_x)
20449 new_x++;
20450 rc = move_it_in_display_line_to (&it, ZV, new_x,
20451 MOVE_TO_POS | MOVE_TO_X);
20452 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20453 break;
20455 /* If we ended up on a composed character inside
20456 bidi-reordered text (e.g., Hebrew text with diacritics),
20457 the iterator gives us the buffer position of the last (in
20458 logical order) character of the composed grapheme cluster,
20459 which is not what we want. So we cheat: we compute the
20460 character position of the character that follows (in the
20461 logical order) the one where the above loop stopped. That
20462 character will appear on display to the left of point. */
20463 if (it.bidi_p
20464 && it.bidi_it.scan_dir == -1
20465 && new_pos.charpos - IT_CHARPOS (it) > 1)
20467 new_pos.charpos = IT_CHARPOS (it) + 1;
20468 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20470 it.current.pos = new_pos;
20472 else
20473 #endif
20474 if (it.current_x != target_x)
20475 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20477 /* When lines are truncated, the above loop will stop at the
20478 window edge. But we want to get to the end of line, even if
20479 it is beyond the window edge; automatic hscroll will then
20480 scroll the window to show point as appropriate. */
20481 if (target_is_eol_p && it.line_wrap == TRUNCATE
20482 && get_next_display_element (&it))
20484 struct text_pos new_pos = it.current.pos;
20486 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20488 set_iterator_to_next (&it, 0);
20489 if (it.method == GET_FROM_BUFFER)
20490 new_pos = it.current.pos;
20491 if (!get_next_display_element (&it))
20492 break;
20495 it.current.pos = new_pos;
20498 /* If we ended up in a display string that covers point, move to
20499 buffer position to the right in the visual order. */
20500 if (dir > 0)
20502 while (IT_CHARPOS (it) == PT)
20504 set_iterator_to_next (&it, 0);
20505 if (!get_next_display_element (&it))
20506 break;
20510 /* Move point to that position. */
20511 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20514 return make_number (PT);
20516 #undef ROW_GLYPH_NEWLINE_P
20520 /***********************************************************************
20521 Menu Bar
20522 ***********************************************************************/
20524 /* Redisplay the menu bar in the frame for window W.
20526 The menu bar of X frames that don't have X toolkit support is
20527 displayed in a special window W->frame->menu_bar_window.
20529 The menu bar of terminal frames is treated specially as far as
20530 glyph matrices are concerned. Menu bar lines are not part of
20531 windows, so the update is done directly on the frame matrix rows
20532 for the menu bar. */
20534 static void
20535 display_menu_bar (struct window *w)
20537 struct frame *f = XFRAME (WINDOW_FRAME (w));
20538 struct it it;
20539 Lisp_Object items;
20540 int i;
20542 /* Don't do all this for graphical frames. */
20543 #ifdef HAVE_NTGUI
20544 if (FRAME_W32_P (f))
20545 return;
20546 #endif
20547 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20548 if (FRAME_X_P (f))
20549 return;
20550 #endif
20552 #ifdef HAVE_NS
20553 if (FRAME_NS_P (f))
20554 return;
20555 #endif /* HAVE_NS */
20557 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20558 eassert (!FRAME_WINDOW_P (f));
20559 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20560 it.first_visible_x = 0;
20561 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20562 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20563 if (FRAME_WINDOW_P (f))
20565 /* Menu bar lines are displayed in the desired matrix of the
20566 dummy window menu_bar_window. */
20567 struct window *menu_w;
20568 menu_w = XWINDOW (f->menu_bar_window);
20569 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20570 MENU_FACE_ID);
20571 it.first_visible_x = 0;
20572 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20574 else
20575 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20577 /* This is a TTY frame, i.e. character hpos/vpos are used as
20578 pixel x/y. */
20579 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20580 MENU_FACE_ID);
20581 it.first_visible_x = 0;
20582 it.last_visible_x = FRAME_COLS (f);
20585 /* FIXME: This should be controlled by a user option. See the
20586 comments in redisplay_tool_bar and display_mode_line about
20587 this. */
20588 it.paragraph_embedding = L2R;
20590 /* Clear all rows of the menu bar. */
20591 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20593 struct glyph_row *row = it.glyph_row + i;
20594 clear_glyph_row (row);
20595 row->enabled_p = 1;
20596 row->full_width_p = 1;
20599 /* Display all items of the menu bar. */
20600 items = FRAME_MENU_BAR_ITEMS (it.f);
20601 for (i = 0; i < ASIZE (items); i += 4)
20603 Lisp_Object string;
20605 /* Stop at nil string. */
20606 string = AREF (items, i + 1);
20607 if (NILP (string))
20608 break;
20610 /* Remember where item was displayed. */
20611 ASET (items, i + 3, make_number (it.hpos));
20613 /* Display the item, pad with one space. */
20614 if (it.current_x < it.last_visible_x)
20615 display_string (NULL, string, Qnil, 0, 0, &it,
20616 SCHARS (string) + 1, 0, 0, -1);
20619 /* Fill out the line with spaces. */
20620 if (it.current_x < it.last_visible_x)
20621 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20623 /* Compute the total height of the lines. */
20624 compute_line_metrics (&it);
20629 /***********************************************************************
20630 Mode Line
20631 ***********************************************************************/
20633 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20634 FORCE is non-zero, redisplay mode lines unconditionally.
20635 Otherwise, redisplay only mode lines that are garbaged. Value is
20636 the number of windows whose mode lines were redisplayed. */
20638 static int
20639 redisplay_mode_lines (Lisp_Object window, int force)
20641 int nwindows = 0;
20643 while (!NILP (window))
20645 struct window *w = XWINDOW (window);
20647 if (WINDOWP (w->contents))
20648 nwindows += redisplay_mode_lines (w->contents, force);
20649 else if (force
20650 || FRAME_GARBAGED_P (XFRAME (w->frame))
20651 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20653 struct text_pos lpoint;
20654 struct buffer *old = current_buffer;
20656 /* Set the window's buffer for the mode line display. */
20657 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20658 set_buffer_internal_1 (XBUFFER (w->contents));
20660 /* Point refers normally to the selected window. For any
20661 other window, set up appropriate value. */
20662 if (!EQ (window, selected_window))
20664 struct text_pos pt;
20666 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20667 if (CHARPOS (pt) < BEGV)
20668 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20669 else if (CHARPOS (pt) > (ZV - 1))
20670 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20671 else
20672 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20675 /* Display mode lines. */
20676 clear_glyph_matrix (w->desired_matrix);
20677 if (display_mode_lines (w))
20679 ++nwindows;
20680 w->must_be_updated_p = 1;
20683 /* Restore old settings. */
20684 set_buffer_internal_1 (old);
20685 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20688 window = w->next;
20691 return nwindows;
20695 /* Display the mode and/or header line of window W. Value is the
20696 sum number of mode lines and header lines displayed. */
20698 static int
20699 display_mode_lines (struct window *w)
20701 Lisp_Object old_selected_window = selected_window;
20702 Lisp_Object old_selected_frame = selected_frame;
20703 Lisp_Object new_frame = w->frame;
20704 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20705 int n = 0;
20707 selected_frame = new_frame;
20708 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20709 or window's point, then we'd need select_window_1 here as well. */
20710 XSETWINDOW (selected_window, w);
20711 XFRAME (new_frame)->selected_window = selected_window;
20713 /* These will be set while the mode line specs are processed. */
20714 line_number_displayed = 0;
20715 w->column_number_displayed = -1;
20717 if (WINDOW_WANTS_MODELINE_P (w))
20719 struct window *sel_w = XWINDOW (old_selected_window);
20721 /* Select mode line face based on the real selected window. */
20722 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20723 BVAR (current_buffer, mode_line_format));
20724 ++n;
20727 if (WINDOW_WANTS_HEADER_LINE_P (w))
20729 display_mode_line (w, HEADER_LINE_FACE_ID,
20730 BVAR (current_buffer, header_line_format));
20731 ++n;
20734 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20735 selected_frame = old_selected_frame;
20736 selected_window = old_selected_window;
20737 return n;
20741 /* Display mode or header line of window W. FACE_ID specifies which
20742 line to display; it is either MODE_LINE_FACE_ID or
20743 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20744 display. Value is the pixel height of the mode/header line
20745 displayed. */
20747 static int
20748 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20750 struct it it;
20751 struct face *face;
20752 ptrdiff_t count = SPECPDL_INDEX ();
20754 init_iterator (&it, w, -1, -1, NULL, face_id);
20755 /* Don't extend on a previously drawn mode-line.
20756 This may happen if called from pos_visible_p. */
20757 it.glyph_row->enabled_p = 0;
20758 prepare_desired_row (it.glyph_row);
20760 it.glyph_row->mode_line_p = 1;
20762 /* FIXME: This should be controlled by a user option. But
20763 supporting such an option is not trivial, since the mode line is
20764 made up of many separate strings. */
20765 it.paragraph_embedding = L2R;
20767 record_unwind_protect (unwind_format_mode_line,
20768 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20770 mode_line_target = MODE_LINE_DISPLAY;
20772 /* Temporarily make frame's keyboard the current kboard so that
20773 kboard-local variables in the mode_line_format will get the right
20774 values. */
20775 push_kboard (FRAME_KBOARD (it.f));
20776 record_unwind_save_match_data ();
20777 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20778 pop_kboard ();
20780 unbind_to (count, Qnil);
20782 /* Fill up with spaces. */
20783 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20785 compute_line_metrics (&it);
20786 it.glyph_row->full_width_p = 1;
20787 it.glyph_row->continued_p = 0;
20788 it.glyph_row->truncated_on_left_p = 0;
20789 it.glyph_row->truncated_on_right_p = 0;
20791 /* Make a 3D mode-line have a shadow at its right end. */
20792 face = FACE_FROM_ID (it.f, face_id);
20793 extend_face_to_end_of_line (&it);
20794 if (face->box != FACE_NO_BOX)
20796 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20797 + it.glyph_row->used[TEXT_AREA] - 1);
20798 last->right_box_line_p = 1;
20801 return it.glyph_row->height;
20804 /* Move element ELT in LIST to the front of LIST.
20805 Return the updated list. */
20807 static Lisp_Object
20808 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20810 register Lisp_Object tail, prev;
20811 register Lisp_Object tem;
20813 tail = list;
20814 prev = Qnil;
20815 while (CONSP (tail))
20817 tem = XCAR (tail);
20819 if (EQ (elt, tem))
20821 /* Splice out the link TAIL. */
20822 if (NILP (prev))
20823 list = XCDR (tail);
20824 else
20825 Fsetcdr (prev, XCDR (tail));
20827 /* Now make it the first. */
20828 Fsetcdr (tail, list);
20829 return tail;
20831 else
20832 prev = tail;
20833 tail = XCDR (tail);
20834 QUIT;
20837 /* Not found--return unchanged LIST. */
20838 return list;
20841 /* Contribute ELT to the mode line for window IT->w. How it
20842 translates into text depends on its data type.
20844 IT describes the display environment in which we display, as usual.
20846 DEPTH is the depth in recursion. It is used to prevent
20847 infinite recursion here.
20849 FIELD_WIDTH is the number of characters the display of ELT should
20850 occupy in the mode line, and PRECISION is the maximum number of
20851 characters to display from ELT's representation. See
20852 display_string for details.
20854 Returns the hpos of the end of the text generated by ELT.
20856 PROPS is a property list to add to any string we encounter.
20858 If RISKY is nonzero, remove (disregard) any properties in any string
20859 we encounter, and ignore :eval and :propertize.
20861 The global variable `mode_line_target' determines whether the
20862 output is passed to `store_mode_line_noprop',
20863 `store_mode_line_string', or `display_string'. */
20865 static int
20866 display_mode_element (struct it *it, int depth, int field_width, int precision,
20867 Lisp_Object elt, Lisp_Object props, int risky)
20869 int n = 0, field, prec;
20870 int literal = 0;
20872 tail_recurse:
20873 if (depth > 100)
20874 elt = build_string ("*too-deep*");
20876 depth++;
20878 switch (XTYPE (elt))
20880 case Lisp_String:
20882 /* A string: output it and check for %-constructs within it. */
20883 unsigned char c;
20884 ptrdiff_t offset = 0;
20886 if (SCHARS (elt) > 0
20887 && (!NILP (props) || risky))
20889 Lisp_Object oprops, aelt;
20890 oprops = Ftext_properties_at (make_number (0), elt);
20892 /* If the starting string's properties are not what
20893 we want, translate the string. Also, if the string
20894 is risky, do that anyway. */
20896 if (NILP (Fequal (props, oprops)) || risky)
20898 /* If the starting string has properties,
20899 merge the specified ones onto the existing ones. */
20900 if (! NILP (oprops) && !risky)
20902 Lisp_Object tem;
20904 oprops = Fcopy_sequence (oprops);
20905 tem = props;
20906 while (CONSP (tem))
20908 oprops = Fplist_put (oprops, XCAR (tem),
20909 XCAR (XCDR (tem)));
20910 tem = XCDR (XCDR (tem));
20912 props = oprops;
20915 aelt = Fassoc (elt, mode_line_proptrans_alist);
20916 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20918 /* AELT is what we want. Move it to the front
20919 without consing. */
20920 elt = XCAR (aelt);
20921 mode_line_proptrans_alist
20922 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20924 else
20926 Lisp_Object tem;
20928 /* If AELT has the wrong props, it is useless.
20929 so get rid of it. */
20930 if (! NILP (aelt))
20931 mode_line_proptrans_alist
20932 = Fdelq (aelt, mode_line_proptrans_alist);
20934 elt = Fcopy_sequence (elt);
20935 Fset_text_properties (make_number (0), Flength (elt),
20936 props, elt);
20937 /* Add this item to mode_line_proptrans_alist. */
20938 mode_line_proptrans_alist
20939 = Fcons (Fcons (elt, props),
20940 mode_line_proptrans_alist);
20941 /* Truncate mode_line_proptrans_alist
20942 to at most 50 elements. */
20943 tem = Fnthcdr (make_number (50),
20944 mode_line_proptrans_alist);
20945 if (! NILP (tem))
20946 XSETCDR (tem, Qnil);
20951 offset = 0;
20953 if (literal)
20955 prec = precision - n;
20956 switch (mode_line_target)
20958 case MODE_LINE_NOPROP:
20959 case MODE_LINE_TITLE:
20960 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20961 break;
20962 case MODE_LINE_STRING:
20963 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20964 break;
20965 case MODE_LINE_DISPLAY:
20966 n += display_string (NULL, elt, Qnil, 0, 0, it,
20967 0, prec, 0, STRING_MULTIBYTE (elt));
20968 break;
20971 break;
20974 /* Handle the non-literal case. */
20976 while ((precision <= 0 || n < precision)
20977 && SREF (elt, offset) != 0
20978 && (mode_line_target != MODE_LINE_DISPLAY
20979 || it->current_x < it->last_visible_x))
20981 ptrdiff_t last_offset = offset;
20983 /* Advance to end of string or next format specifier. */
20984 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20987 if (offset - 1 != last_offset)
20989 ptrdiff_t nchars, nbytes;
20991 /* Output to end of string or up to '%'. Field width
20992 is length of string. Don't output more than
20993 PRECISION allows us. */
20994 offset--;
20996 prec = c_string_width (SDATA (elt) + last_offset,
20997 offset - last_offset, precision - n,
20998 &nchars, &nbytes);
21000 switch (mode_line_target)
21002 case MODE_LINE_NOPROP:
21003 case MODE_LINE_TITLE:
21004 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21005 break;
21006 case MODE_LINE_STRING:
21008 ptrdiff_t bytepos = last_offset;
21009 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21010 ptrdiff_t endpos = (precision <= 0
21011 ? string_byte_to_char (elt, offset)
21012 : charpos + nchars);
21014 n += store_mode_line_string (NULL,
21015 Fsubstring (elt, make_number (charpos),
21016 make_number (endpos)),
21017 0, 0, 0, Qnil);
21019 break;
21020 case MODE_LINE_DISPLAY:
21022 ptrdiff_t bytepos = last_offset;
21023 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21025 if (precision <= 0)
21026 nchars = string_byte_to_char (elt, offset) - charpos;
21027 n += display_string (NULL, elt, Qnil, 0, charpos,
21028 it, 0, nchars, 0,
21029 STRING_MULTIBYTE (elt));
21031 break;
21034 else /* c == '%' */
21036 ptrdiff_t percent_position = offset;
21038 /* Get the specified minimum width. Zero means
21039 don't pad. */
21040 field = 0;
21041 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21042 field = field * 10 + c - '0';
21044 /* Don't pad beyond the total padding allowed. */
21045 if (field_width - n > 0 && field > field_width - n)
21046 field = field_width - n;
21048 /* Note that either PRECISION <= 0 or N < PRECISION. */
21049 prec = precision - n;
21051 if (c == 'M')
21052 n += display_mode_element (it, depth, field, prec,
21053 Vglobal_mode_string, props,
21054 risky);
21055 else if (c != 0)
21057 bool multibyte;
21058 ptrdiff_t bytepos, charpos;
21059 const char *spec;
21060 Lisp_Object string;
21062 bytepos = percent_position;
21063 charpos = (STRING_MULTIBYTE (elt)
21064 ? string_byte_to_char (elt, bytepos)
21065 : bytepos);
21066 spec = decode_mode_spec (it->w, c, field, &string);
21067 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21069 switch (mode_line_target)
21071 case MODE_LINE_NOPROP:
21072 case MODE_LINE_TITLE:
21073 n += store_mode_line_noprop (spec, field, prec);
21074 break;
21075 case MODE_LINE_STRING:
21077 Lisp_Object tem = build_string (spec);
21078 props = Ftext_properties_at (make_number (charpos), elt);
21079 /* Should only keep face property in props */
21080 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21082 break;
21083 case MODE_LINE_DISPLAY:
21085 int nglyphs_before, nwritten;
21087 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21088 nwritten = display_string (spec, string, elt,
21089 charpos, 0, it,
21090 field, prec, 0,
21091 multibyte);
21093 /* Assign to the glyphs written above the
21094 string where the `%x' came from, position
21095 of the `%'. */
21096 if (nwritten > 0)
21098 struct glyph *glyph
21099 = (it->glyph_row->glyphs[TEXT_AREA]
21100 + nglyphs_before);
21101 int i;
21103 for (i = 0; i < nwritten; ++i)
21105 glyph[i].object = elt;
21106 glyph[i].charpos = charpos;
21109 n += nwritten;
21112 break;
21115 else /* c == 0 */
21116 break;
21120 break;
21122 case Lisp_Symbol:
21123 /* A symbol: process the value of the symbol recursively
21124 as if it appeared here directly. Avoid error if symbol void.
21125 Special case: if value of symbol is a string, output the string
21126 literally. */
21128 register Lisp_Object tem;
21130 /* If the variable is not marked as risky to set
21131 then its contents are risky to use. */
21132 if (NILP (Fget (elt, Qrisky_local_variable)))
21133 risky = 1;
21135 tem = Fboundp (elt);
21136 if (!NILP (tem))
21138 tem = Fsymbol_value (elt);
21139 /* If value is a string, output that string literally:
21140 don't check for % within it. */
21141 if (STRINGP (tem))
21142 literal = 1;
21144 if (!EQ (tem, elt))
21146 /* Give up right away for nil or t. */
21147 elt = tem;
21148 goto tail_recurse;
21152 break;
21154 case Lisp_Cons:
21156 register Lisp_Object car, tem;
21158 /* A cons cell: five distinct cases.
21159 If first element is :eval or :propertize, do something special.
21160 If first element is a string or a cons, process all the elements
21161 and effectively concatenate them.
21162 If first element is a negative number, truncate displaying cdr to
21163 at most that many characters. If positive, pad (with spaces)
21164 to at least that many characters.
21165 If first element is a symbol, process the cadr or caddr recursively
21166 according to whether the symbol's value is non-nil or nil. */
21167 car = XCAR (elt);
21168 if (EQ (car, QCeval))
21170 /* An element of the form (:eval FORM) means evaluate FORM
21171 and use the result as mode line elements. */
21173 if (risky)
21174 break;
21176 if (CONSP (XCDR (elt)))
21178 Lisp_Object spec;
21179 spec = safe_eval (XCAR (XCDR (elt)));
21180 n += display_mode_element (it, depth, field_width - n,
21181 precision - n, spec, props,
21182 risky);
21185 else if (EQ (car, QCpropertize))
21187 /* An element of the form (:propertize ELT PROPS...)
21188 means display ELT but applying properties PROPS. */
21190 if (risky)
21191 break;
21193 if (CONSP (XCDR (elt)))
21194 n += display_mode_element (it, depth, field_width - n,
21195 precision - n, XCAR (XCDR (elt)),
21196 XCDR (XCDR (elt)), risky);
21198 else if (SYMBOLP (car))
21200 tem = Fboundp (car);
21201 elt = XCDR (elt);
21202 if (!CONSP (elt))
21203 goto invalid;
21204 /* elt is now the cdr, and we know it is a cons cell.
21205 Use its car if CAR has a non-nil value. */
21206 if (!NILP (tem))
21208 tem = Fsymbol_value (car);
21209 if (!NILP (tem))
21211 elt = XCAR (elt);
21212 goto tail_recurse;
21215 /* Symbol's value is nil (or symbol is unbound)
21216 Get the cddr of the original list
21217 and if possible find the caddr and use that. */
21218 elt = XCDR (elt);
21219 if (NILP (elt))
21220 break;
21221 else if (!CONSP (elt))
21222 goto invalid;
21223 elt = XCAR (elt);
21224 goto tail_recurse;
21226 else if (INTEGERP (car))
21228 register int lim = XINT (car);
21229 elt = XCDR (elt);
21230 if (lim < 0)
21232 /* Negative int means reduce maximum width. */
21233 if (precision <= 0)
21234 precision = -lim;
21235 else
21236 precision = min (precision, -lim);
21238 else if (lim > 0)
21240 /* Padding specified. Don't let it be more than
21241 current maximum. */
21242 if (precision > 0)
21243 lim = min (precision, lim);
21245 /* If that's more padding than already wanted, queue it.
21246 But don't reduce padding already specified even if
21247 that is beyond the current truncation point. */
21248 field_width = max (lim, field_width);
21250 goto tail_recurse;
21252 else if (STRINGP (car) || CONSP (car))
21254 Lisp_Object halftail = elt;
21255 int len = 0;
21257 while (CONSP (elt)
21258 && (precision <= 0 || n < precision))
21260 n += display_mode_element (it, depth,
21261 /* Do padding only after the last
21262 element in the list. */
21263 (! CONSP (XCDR (elt))
21264 ? field_width - n
21265 : 0),
21266 precision - n, XCAR (elt),
21267 props, risky);
21268 elt = XCDR (elt);
21269 len++;
21270 if ((len & 1) == 0)
21271 halftail = XCDR (halftail);
21272 /* Check for cycle. */
21273 if (EQ (halftail, elt))
21274 break;
21278 break;
21280 default:
21281 invalid:
21282 elt = build_string ("*invalid*");
21283 goto tail_recurse;
21286 /* Pad to FIELD_WIDTH. */
21287 if (field_width > 0 && n < field_width)
21289 switch (mode_line_target)
21291 case MODE_LINE_NOPROP:
21292 case MODE_LINE_TITLE:
21293 n += store_mode_line_noprop ("", field_width - n, 0);
21294 break;
21295 case MODE_LINE_STRING:
21296 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21297 break;
21298 case MODE_LINE_DISPLAY:
21299 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21300 0, 0, 0);
21301 break;
21305 return n;
21308 /* Store a mode-line string element in mode_line_string_list.
21310 If STRING is non-null, display that C string. Otherwise, the Lisp
21311 string LISP_STRING is displayed.
21313 FIELD_WIDTH is the minimum number of output glyphs to produce.
21314 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21315 with spaces. FIELD_WIDTH <= 0 means don't pad.
21317 PRECISION is the maximum number of characters to output from
21318 STRING. PRECISION <= 0 means don't truncate the string.
21320 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21321 properties to the string.
21323 PROPS are the properties to add to the string.
21324 The mode_line_string_face face property is always added to the string.
21327 static int
21328 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21329 int field_width, int precision, Lisp_Object props)
21331 ptrdiff_t len;
21332 int n = 0;
21334 if (string != NULL)
21336 len = strlen (string);
21337 if (precision > 0 && len > precision)
21338 len = precision;
21339 lisp_string = make_string (string, len);
21340 if (NILP (props))
21341 props = mode_line_string_face_prop;
21342 else if (!NILP (mode_line_string_face))
21344 Lisp_Object face = Fplist_get (props, Qface);
21345 props = Fcopy_sequence (props);
21346 if (NILP (face))
21347 face = mode_line_string_face;
21348 else
21349 face = list2 (face, mode_line_string_face);
21350 props = Fplist_put (props, Qface, face);
21352 Fadd_text_properties (make_number (0), make_number (len),
21353 props, lisp_string);
21355 else
21357 len = XFASTINT (Flength (lisp_string));
21358 if (precision > 0 && len > precision)
21360 len = precision;
21361 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21362 precision = -1;
21364 if (!NILP (mode_line_string_face))
21366 Lisp_Object face;
21367 if (NILP (props))
21368 props = Ftext_properties_at (make_number (0), lisp_string);
21369 face = Fplist_get (props, Qface);
21370 if (NILP (face))
21371 face = mode_line_string_face;
21372 else
21373 face = list2 (face, mode_line_string_face);
21374 props = list2 (Qface, face);
21375 if (copy_string)
21376 lisp_string = Fcopy_sequence (lisp_string);
21378 if (!NILP (props))
21379 Fadd_text_properties (make_number (0), make_number (len),
21380 props, lisp_string);
21383 if (len > 0)
21385 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21386 n += len;
21389 if (field_width > len)
21391 field_width -= len;
21392 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21393 if (!NILP (props))
21394 Fadd_text_properties (make_number (0), make_number (field_width),
21395 props, lisp_string);
21396 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21397 n += field_width;
21400 return n;
21404 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21405 1, 4, 0,
21406 doc: /* Format a string out of a mode line format specification.
21407 First arg FORMAT specifies the mode line format (see `mode-line-format'
21408 for details) to use.
21410 By default, the format is evaluated for the currently selected window.
21412 Optional second arg FACE specifies the face property to put on all
21413 characters for which no face is specified. The value nil means the
21414 default face. The value t means whatever face the window's mode line
21415 currently uses (either `mode-line' or `mode-line-inactive',
21416 depending on whether the window is the selected window or not).
21417 An integer value means the value string has no text
21418 properties.
21420 Optional third and fourth args WINDOW and BUFFER specify the window
21421 and buffer to use as the context for the formatting (defaults
21422 are the selected window and the WINDOW's buffer). */)
21423 (Lisp_Object format, Lisp_Object face,
21424 Lisp_Object window, Lisp_Object buffer)
21426 struct it it;
21427 int len;
21428 struct window *w;
21429 struct buffer *old_buffer = NULL;
21430 int face_id;
21431 int no_props = INTEGERP (face);
21432 ptrdiff_t count = SPECPDL_INDEX ();
21433 Lisp_Object str;
21434 int string_start = 0;
21436 w = decode_any_window (window);
21437 XSETWINDOW (window, w);
21439 if (NILP (buffer))
21440 buffer = w->contents;
21441 CHECK_BUFFER (buffer);
21443 /* Make formatting the modeline a non-op when noninteractive, otherwise
21444 there will be problems later caused by a partially initialized frame. */
21445 if (NILP (format) || noninteractive)
21446 return empty_unibyte_string;
21448 if (no_props)
21449 face = Qnil;
21451 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21452 : EQ (face, Qt) ? (EQ (window, selected_window)
21453 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21454 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21455 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21456 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21457 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21458 : DEFAULT_FACE_ID;
21460 old_buffer = current_buffer;
21462 /* Save things including mode_line_proptrans_alist,
21463 and set that to nil so that we don't alter the outer value. */
21464 record_unwind_protect (unwind_format_mode_line,
21465 format_mode_line_unwind_data
21466 (XFRAME (WINDOW_FRAME (w)),
21467 old_buffer, selected_window, 1));
21468 mode_line_proptrans_alist = Qnil;
21470 Fselect_window (window, Qt);
21471 set_buffer_internal_1 (XBUFFER (buffer));
21473 init_iterator (&it, w, -1, -1, NULL, face_id);
21475 if (no_props)
21477 mode_line_target = MODE_LINE_NOPROP;
21478 mode_line_string_face_prop = Qnil;
21479 mode_line_string_list = Qnil;
21480 string_start = MODE_LINE_NOPROP_LEN (0);
21482 else
21484 mode_line_target = MODE_LINE_STRING;
21485 mode_line_string_list = Qnil;
21486 mode_line_string_face = face;
21487 mode_line_string_face_prop
21488 = NILP (face) ? Qnil : list2 (Qface, face);
21491 push_kboard (FRAME_KBOARD (it.f));
21492 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21493 pop_kboard ();
21495 if (no_props)
21497 len = MODE_LINE_NOPROP_LEN (string_start);
21498 str = make_string (mode_line_noprop_buf + string_start, len);
21500 else
21502 mode_line_string_list = Fnreverse (mode_line_string_list);
21503 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21504 empty_unibyte_string);
21507 unbind_to (count, Qnil);
21508 return str;
21511 /* Write a null-terminated, right justified decimal representation of
21512 the positive integer D to BUF using a minimal field width WIDTH. */
21514 static void
21515 pint2str (register char *buf, register int width, register ptrdiff_t d)
21517 register char *p = buf;
21519 if (d <= 0)
21520 *p++ = '0';
21521 else
21523 while (d > 0)
21525 *p++ = d % 10 + '0';
21526 d /= 10;
21530 for (width -= (int) (p - buf); width > 0; --width)
21531 *p++ = ' ';
21532 *p-- = '\0';
21533 while (p > buf)
21535 d = *buf;
21536 *buf++ = *p;
21537 *p-- = d;
21541 /* Write a null-terminated, right justified decimal and "human
21542 readable" representation of the nonnegative integer D to BUF using
21543 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21545 static const char power_letter[] =
21547 0, /* no letter */
21548 'k', /* kilo */
21549 'M', /* mega */
21550 'G', /* giga */
21551 'T', /* tera */
21552 'P', /* peta */
21553 'E', /* exa */
21554 'Z', /* zetta */
21555 'Y' /* yotta */
21558 static void
21559 pint2hrstr (char *buf, int width, ptrdiff_t d)
21561 /* We aim to represent the nonnegative integer D as
21562 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21563 ptrdiff_t quotient = d;
21564 int remainder = 0;
21565 /* -1 means: do not use TENTHS. */
21566 int tenths = -1;
21567 int exponent = 0;
21569 /* Length of QUOTIENT.TENTHS as a string. */
21570 int length;
21572 char * psuffix;
21573 char * p;
21575 if (quotient >= 1000)
21577 /* Scale to the appropriate EXPONENT. */
21580 remainder = quotient % 1000;
21581 quotient /= 1000;
21582 exponent++;
21584 while (quotient >= 1000);
21586 /* Round to nearest and decide whether to use TENTHS or not. */
21587 if (quotient <= 9)
21589 tenths = remainder / 100;
21590 if (remainder % 100 >= 50)
21592 if (tenths < 9)
21593 tenths++;
21594 else
21596 quotient++;
21597 if (quotient == 10)
21598 tenths = -1;
21599 else
21600 tenths = 0;
21604 else
21605 if (remainder >= 500)
21607 if (quotient < 999)
21608 quotient++;
21609 else
21611 quotient = 1;
21612 exponent++;
21613 tenths = 0;
21618 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21619 if (tenths == -1 && quotient <= 99)
21620 if (quotient <= 9)
21621 length = 1;
21622 else
21623 length = 2;
21624 else
21625 length = 3;
21626 p = psuffix = buf + max (width, length);
21628 /* Print EXPONENT. */
21629 *psuffix++ = power_letter[exponent];
21630 *psuffix = '\0';
21632 /* Print TENTHS. */
21633 if (tenths >= 0)
21635 *--p = '0' + tenths;
21636 *--p = '.';
21639 /* Print QUOTIENT. */
21642 int digit = quotient % 10;
21643 *--p = '0' + digit;
21645 while ((quotient /= 10) != 0);
21647 /* Print leading spaces. */
21648 while (buf < p)
21649 *--p = ' ';
21652 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21653 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21654 type of CODING_SYSTEM. Return updated pointer into BUF. */
21656 static unsigned char invalid_eol_type[] = "(*invalid*)";
21658 static char *
21659 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21661 Lisp_Object val;
21662 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21663 const unsigned char *eol_str;
21664 int eol_str_len;
21665 /* The EOL conversion we are using. */
21666 Lisp_Object eoltype;
21668 val = CODING_SYSTEM_SPEC (coding_system);
21669 eoltype = Qnil;
21671 if (!VECTORP (val)) /* Not yet decided. */
21673 *buf++ = multibyte ? '-' : ' ';
21674 if (eol_flag)
21675 eoltype = eol_mnemonic_undecided;
21676 /* Don't mention EOL conversion if it isn't decided. */
21678 else
21680 Lisp_Object attrs;
21681 Lisp_Object eolvalue;
21683 attrs = AREF (val, 0);
21684 eolvalue = AREF (val, 2);
21686 *buf++ = multibyte
21687 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21688 : ' ';
21690 if (eol_flag)
21692 /* The EOL conversion that is normal on this system. */
21694 if (NILP (eolvalue)) /* Not yet decided. */
21695 eoltype = eol_mnemonic_undecided;
21696 else if (VECTORP (eolvalue)) /* Not yet decided. */
21697 eoltype = eol_mnemonic_undecided;
21698 else /* eolvalue is Qunix, Qdos, or Qmac. */
21699 eoltype = (EQ (eolvalue, Qunix)
21700 ? eol_mnemonic_unix
21701 : (EQ (eolvalue, Qdos) == 1
21702 ? eol_mnemonic_dos : eol_mnemonic_mac));
21706 if (eol_flag)
21708 /* Mention the EOL conversion if it is not the usual one. */
21709 if (STRINGP (eoltype))
21711 eol_str = SDATA (eoltype);
21712 eol_str_len = SBYTES (eoltype);
21714 else if (CHARACTERP (eoltype))
21716 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21717 int c = XFASTINT (eoltype);
21718 eol_str_len = CHAR_STRING (c, tmp);
21719 eol_str = tmp;
21721 else
21723 eol_str = invalid_eol_type;
21724 eol_str_len = sizeof (invalid_eol_type) - 1;
21726 memcpy (buf, eol_str, eol_str_len);
21727 buf += eol_str_len;
21730 return buf;
21733 /* Return a string for the output of a mode line %-spec for window W,
21734 generated by character C. FIELD_WIDTH > 0 means pad the string
21735 returned with spaces to that value. Return a Lisp string in
21736 *STRING if the resulting string is taken from that Lisp string.
21738 Note we operate on the current buffer for most purposes. */
21740 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21742 static const char *
21743 decode_mode_spec (struct window *w, register int c, int field_width,
21744 Lisp_Object *string)
21746 Lisp_Object obj;
21747 struct frame *f = XFRAME (WINDOW_FRAME (w));
21748 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21749 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21750 produce strings from numerical values, so limit preposterously
21751 large values of FIELD_WIDTH to avoid overrunning the buffer's
21752 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21753 bytes plus the terminating null. */
21754 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21755 struct buffer *b = current_buffer;
21757 obj = Qnil;
21758 *string = Qnil;
21760 switch (c)
21762 case '*':
21763 if (!NILP (BVAR (b, read_only)))
21764 return "%";
21765 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21766 return "*";
21767 return "-";
21769 case '+':
21770 /* This differs from %* only for a modified read-only buffer. */
21771 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21772 return "*";
21773 if (!NILP (BVAR (b, read_only)))
21774 return "%";
21775 return "-";
21777 case '&':
21778 /* This differs from %* in ignoring read-only-ness. */
21779 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21780 return "*";
21781 return "-";
21783 case '%':
21784 return "%";
21786 case '[':
21788 int i;
21789 char *p;
21791 if (command_loop_level > 5)
21792 return "[[[... ";
21793 p = decode_mode_spec_buf;
21794 for (i = 0; i < command_loop_level; i++)
21795 *p++ = '[';
21796 *p = 0;
21797 return decode_mode_spec_buf;
21800 case ']':
21802 int i;
21803 char *p;
21805 if (command_loop_level > 5)
21806 return " ...]]]";
21807 p = decode_mode_spec_buf;
21808 for (i = 0; i < command_loop_level; i++)
21809 *p++ = ']';
21810 *p = 0;
21811 return decode_mode_spec_buf;
21814 case '-':
21816 register int i;
21818 /* Let lots_of_dashes be a string of infinite length. */
21819 if (mode_line_target == MODE_LINE_NOPROP
21820 || mode_line_target == MODE_LINE_STRING)
21821 return "--";
21822 if (field_width <= 0
21823 || field_width > sizeof (lots_of_dashes))
21825 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21826 decode_mode_spec_buf[i] = '-';
21827 decode_mode_spec_buf[i] = '\0';
21828 return decode_mode_spec_buf;
21830 else
21831 return lots_of_dashes;
21834 case 'b':
21835 obj = BVAR (b, name);
21836 break;
21838 case 'c':
21839 /* %c and %l are ignored in `frame-title-format'.
21840 (In redisplay_internal, the frame title is drawn _before_ the
21841 windows are updated, so the stuff which depends on actual
21842 window contents (such as %l) may fail to render properly, or
21843 even crash emacs.) */
21844 if (mode_line_target == MODE_LINE_TITLE)
21845 return "";
21846 else
21848 ptrdiff_t col = current_column ();
21849 w->column_number_displayed = col;
21850 pint2str (decode_mode_spec_buf, width, col);
21851 return decode_mode_spec_buf;
21854 case 'e':
21855 #ifndef SYSTEM_MALLOC
21857 if (NILP (Vmemory_full))
21858 return "";
21859 else
21860 return "!MEM FULL! ";
21862 #else
21863 return "";
21864 #endif
21866 case 'F':
21867 /* %F displays the frame name. */
21868 if (!NILP (f->title))
21869 return SSDATA (f->title);
21870 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21871 return SSDATA (f->name);
21872 return "Emacs";
21874 case 'f':
21875 obj = BVAR (b, filename);
21876 break;
21878 case 'i':
21880 ptrdiff_t size = ZV - BEGV;
21881 pint2str (decode_mode_spec_buf, width, size);
21882 return decode_mode_spec_buf;
21885 case 'I':
21887 ptrdiff_t size = ZV - BEGV;
21888 pint2hrstr (decode_mode_spec_buf, width, size);
21889 return decode_mode_spec_buf;
21892 case 'l':
21894 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21895 ptrdiff_t topline, nlines, height;
21896 ptrdiff_t junk;
21898 /* %c and %l are ignored in `frame-title-format'. */
21899 if (mode_line_target == MODE_LINE_TITLE)
21900 return "";
21902 startpos = marker_position (w->start);
21903 startpos_byte = marker_byte_position (w->start);
21904 height = WINDOW_TOTAL_LINES (w);
21906 /* If we decided that this buffer isn't suitable for line numbers,
21907 don't forget that too fast. */
21908 if (w->base_line_pos == -1)
21909 goto no_value;
21911 /* If the buffer is very big, don't waste time. */
21912 if (INTEGERP (Vline_number_display_limit)
21913 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21915 w->base_line_pos = 0;
21916 w->base_line_number = 0;
21917 goto no_value;
21920 if (w->base_line_number > 0
21921 && w->base_line_pos > 0
21922 && w->base_line_pos <= startpos)
21924 line = w->base_line_number;
21925 linepos = w->base_line_pos;
21926 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21928 else
21930 line = 1;
21931 linepos = BUF_BEGV (b);
21932 linepos_byte = BUF_BEGV_BYTE (b);
21935 /* Count lines from base line to window start position. */
21936 nlines = display_count_lines (linepos_byte,
21937 startpos_byte,
21938 startpos, &junk);
21940 topline = nlines + line;
21942 /* Determine a new base line, if the old one is too close
21943 or too far away, or if we did not have one.
21944 "Too close" means it's plausible a scroll-down would
21945 go back past it. */
21946 if (startpos == BUF_BEGV (b))
21948 w->base_line_number = topline;
21949 w->base_line_pos = BUF_BEGV (b);
21951 else if (nlines < height + 25 || nlines > height * 3 + 50
21952 || linepos == BUF_BEGV (b))
21954 ptrdiff_t limit = BUF_BEGV (b);
21955 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21956 ptrdiff_t position;
21957 ptrdiff_t distance =
21958 (height * 2 + 30) * line_number_display_limit_width;
21960 if (startpos - distance > limit)
21962 limit = startpos - distance;
21963 limit_byte = CHAR_TO_BYTE (limit);
21966 nlines = display_count_lines (startpos_byte,
21967 limit_byte,
21968 - (height * 2 + 30),
21969 &position);
21970 /* If we couldn't find the lines we wanted within
21971 line_number_display_limit_width chars per line,
21972 give up on line numbers for this window. */
21973 if (position == limit_byte && limit == startpos - distance)
21975 w->base_line_pos = -1;
21976 w->base_line_number = 0;
21977 goto no_value;
21980 w->base_line_number = topline - nlines;
21981 w->base_line_pos = BYTE_TO_CHAR (position);
21984 /* Now count lines from the start pos to point. */
21985 nlines = display_count_lines (startpos_byte,
21986 PT_BYTE, PT, &junk);
21988 /* Record that we did display the line number. */
21989 line_number_displayed = 1;
21991 /* Make the string to show. */
21992 pint2str (decode_mode_spec_buf, width, topline + nlines);
21993 return decode_mode_spec_buf;
21994 no_value:
21996 char* p = decode_mode_spec_buf;
21997 int pad = width - 2;
21998 while (pad-- > 0)
21999 *p++ = ' ';
22000 *p++ = '?';
22001 *p++ = '?';
22002 *p = '\0';
22003 return decode_mode_spec_buf;
22006 break;
22008 case 'm':
22009 obj = BVAR (b, mode_name);
22010 break;
22012 case 'n':
22013 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22014 return " Narrow";
22015 break;
22017 case 'p':
22019 ptrdiff_t pos = marker_position (w->start);
22020 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22022 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22024 if (pos <= BUF_BEGV (b))
22025 return "All";
22026 else
22027 return "Bottom";
22029 else if (pos <= BUF_BEGV (b))
22030 return "Top";
22031 else
22033 if (total > 1000000)
22034 /* Do it differently for a large value, to avoid overflow. */
22035 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22036 else
22037 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22038 /* We can't normally display a 3-digit number,
22039 so get us a 2-digit number that is close. */
22040 if (total == 100)
22041 total = 99;
22042 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22043 return decode_mode_spec_buf;
22047 /* Display percentage of size above the bottom of the screen. */
22048 case 'P':
22050 ptrdiff_t toppos = marker_position (w->start);
22051 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22052 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22054 if (botpos >= BUF_ZV (b))
22056 if (toppos <= BUF_BEGV (b))
22057 return "All";
22058 else
22059 return "Bottom";
22061 else
22063 if (total > 1000000)
22064 /* Do it differently for a large value, to avoid overflow. */
22065 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22066 else
22067 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22068 /* We can't normally display a 3-digit number,
22069 so get us a 2-digit number that is close. */
22070 if (total == 100)
22071 total = 99;
22072 if (toppos <= BUF_BEGV (b))
22073 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22074 else
22075 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22076 return decode_mode_spec_buf;
22080 case 's':
22081 /* status of process */
22082 obj = Fget_buffer_process (Fcurrent_buffer ());
22083 if (NILP (obj))
22084 return "no process";
22085 #ifndef MSDOS
22086 obj = Fsymbol_name (Fprocess_status (obj));
22087 #endif
22088 break;
22090 case '@':
22092 ptrdiff_t count = inhibit_garbage_collection ();
22093 Lisp_Object val = call1 (intern ("file-remote-p"),
22094 BVAR (current_buffer, directory));
22095 unbind_to (count, Qnil);
22097 if (NILP (val))
22098 return "-";
22099 else
22100 return "@";
22103 case 'z':
22104 /* coding-system (not including end-of-line format) */
22105 case 'Z':
22106 /* coding-system (including end-of-line type) */
22108 int eol_flag = (c == 'Z');
22109 char *p = decode_mode_spec_buf;
22111 if (! FRAME_WINDOW_P (f))
22113 /* No need to mention EOL here--the terminal never needs
22114 to do EOL conversion. */
22115 p = decode_mode_spec_coding (CODING_ID_NAME
22116 (FRAME_KEYBOARD_CODING (f)->id),
22117 p, 0);
22118 p = decode_mode_spec_coding (CODING_ID_NAME
22119 (FRAME_TERMINAL_CODING (f)->id),
22120 p, 0);
22122 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22123 p, eol_flag);
22125 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22126 #ifdef subprocesses
22127 obj = Fget_buffer_process (Fcurrent_buffer ());
22128 if (PROCESSP (obj))
22130 p = decode_mode_spec_coding
22131 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22132 p = decode_mode_spec_coding
22133 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22135 #endif /* subprocesses */
22136 #endif /* 0 */
22137 *p = 0;
22138 return decode_mode_spec_buf;
22142 if (STRINGP (obj))
22144 *string = obj;
22145 return SSDATA (obj);
22147 else
22148 return "";
22152 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22153 means count lines back from START_BYTE. But don't go beyond
22154 LIMIT_BYTE. Return the number of lines thus found (always
22155 nonnegative).
22157 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22158 either the position COUNT lines after/before START_BYTE, if we
22159 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22160 COUNT lines. */
22162 static ptrdiff_t
22163 display_count_lines (ptrdiff_t start_byte,
22164 ptrdiff_t limit_byte, ptrdiff_t count,
22165 ptrdiff_t *byte_pos_ptr)
22167 register unsigned char *cursor;
22168 unsigned char *base;
22170 register ptrdiff_t ceiling;
22171 register unsigned char *ceiling_addr;
22172 ptrdiff_t orig_count = count;
22174 /* If we are not in selective display mode,
22175 check only for newlines. */
22176 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22177 && !INTEGERP (BVAR (current_buffer, selective_display)));
22179 if (count > 0)
22181 while (start_byte < limit_byte)
22183 ceiling = BUFFER_CEILING_OF (start_byte);
22184 ceiling = min (limit_byte - 1, ceiling);
22185 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22186 base = (cursor = BYTE_POS_ADDR (start_byte));
22190 if (selective_display)
22192 while (*cursor != '\n' && *cursor != 015
22193 && ++cursor != ceiling_addr)
22194 continue;
22195 if (cursor == ceiling_addr)
22196 break;
22198 else
22200 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22201 if (! cursor)
22202 break;
22205 cursor++;
22207 if (--count == 0)
22209 start_byte += cursor - base;
22210 *byte_pos_ptr = start_byte;
22211 return orig_count;
22214 while (cursor < ceiling_addr);
22216 start_byte += ceiling_addr - base;
22219 else
22221 while (start_byte > limit_byte)
22223 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22224 ceiling = max (limit_byte, ceiling);
22225 ceiling_addr = BYTE_POS_ADDR (ceiling);
22226 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22227 while (1)
22229 if (selective_display)
22231 while (--cursor >= ceiling_addr
22232 && *cursor != '\n' && *cursor != 015)
22233 continue;
22234 if (cursor < ceiling_addr)
22235 break;
22237 else
22239 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22240 if (! cursor)
22241 break;
22244 if (++count == 0)
22246 start_byte += cursor - base + 1;
22247 *byte_pos_ptr = start_byte;
22248 /* When scanning backwards, we should
22249 not count the newline posterior to which we stop. */
22250 return - orig_count - 1;
22253 start_byte += ceiling_addr - base;
22257 *byte_pos_ptr = limit_byte;
22259 if (count < 0)
22260 return - orig_count + count;
22261 return orig_count - count;
22267 /***********************************************************************
22268 Displaying strings
22269 ***********************************************************************/
22271 /* Display a NUL-terminated string, starting with index START.
22273 If STRING is non-null, display that C string. Otherwise, the Lisp
22274 string LISP_STRING is displayed. There's a case that STRING is
22275 non-null and LISP_STRING is not nil. It means STRING is a string
22276 data of LISP_STRING. In that case, we display LISP_STRING while
22277 ignoring its text properties.
22279 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22280 FACE_STRING. Display STRING or LISP_STRING with the face at
22281 FACE_STRING_POS in FACE_STRING:
22283 Display the string in the environment given by IT, but use the
22284 standard display table, temporarily.
22286 FIELD_WIDTH is the minimum number of output glyphs to produce.
22287 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22288 with spaces. If STRING has more characters, more than FIELD_WIDTH
22289 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22291 PRECISION is the maximum number of characters to output from
22292 STRING. PRECISION < 0 means don't truncate the string.
22294 This is roughly equivalent to printf format specifiers:
22296 FIELD_WIDTH PRECISION PRINTF
22297 ----------------------------------------
22298 -1 -1 %s
22299 -1 10 %.10s
22300 10 -1 %10s
22301 20 10 %20.10s
22303 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22304 display them, and < 0 means obey the current buffer's value of
22305 enable_multibyte_characters.
22307 Value is the number of columns displayed. */
22309 static int
22310 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22311 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22312 int field_width, int precision, int max_x, int multibyte)
22314 int hpos_at_start = it->hpos;
22315 int saved_face_id = it->face_id;
22316 struct glyph_row *row = it->glyph_row;
22317 ptrdiff_t it_charpos;
22319 /* Initialize the iterator IT for iteration over STRING beginning
22320 with index START. */
22321 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22322 precision, field_width, multibyte);
22323 if (string && STRINGP (lisp_string))
22324 /* LISP_STRING is the one returned by decode_mode_spec. We should
22325 ignore its text properties. */
22326 it->stop_charpos = it->end_charpos;
22328 /* If displaying STRING, set up the face of the iterator from
22329 FACE_STRING, if that's given. */
22330 if (STRINGP (face_string))
22332 ptrdiff_t endptr;
22333 struct face *face;
22335 it->face_id
22336 = face_at_string_position (it->w, face_string, face_string_pos,
22337 0, it->region_beg_charpos,
22338 it->region_end_charpos,
22339 &endptr, it->base_face_id, 0);
22340 face = FACE_FROM_ID (it->f, it->face_id);
22341 it->face_box_p = face->box != FACE_NO_BOX;
22344 /* Set max_x to the maximum allowed X position. Don't let it go
22345 beyond the right edge of the window. */
22346 if (max_x <= 0)
22347 max_x = it->last_visible_x;
22348 else
22349 max_x = min (max_x, it->last_visible_x);
22351 /* Skip over display elements that are not visible. because IT->w is
22352 hscrolled. */
22353 if (it->current_x < it->first_visible_x)
22354 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22355 MOVE_TO_POS | MOVE_TO_X);
22357 row->ascent = it->max_ascent;
22358 row->height = it->max_ascent + it->max_descent;
22359 row->phys_ascent = it->max_phys_ascent;
22360 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22361 row->extra_line_spacing = it->max_extra_line_spacing;
22363 if (STRINGP (it->string))
22364 it_charpos = IT_STRING_CHARPOS (*it);
22365 else
22366 it_charpos = IT_CHARPOS (*it);
22368 /* This condition is for the case that we are called with current_x
22369 past last_visible_x. */
22370 while (it->current_x < max_x)
22372 int x_before, x, n_glyphs_before, i, nglyphs;
22374 /* Get the next display element. */
22375 if (!get_next_display_element (it))
22376 break;
22378 /* Produce glyphs. */
22379 x_before = it->current_x;
22380 n_glyphs_before = row->used[TEXT_AREA];
22381 PRODUCE_GLYPHS (it);
22383 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22384 i = 0;
22385 x = x_before;
22386 while (i < nglyphs)
22388 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22390 if (it->line_wrap != TRUNCATE
22391 && x + glyph->pixel_width > max_x)
22393 /* End of continued line or max_x reached. */
22394 if (CHAR_GLYPH_PADDING_P (*glyph))
22396 /* A wide character is unbreakable. */
22397 if (row->reversed_p)
22398 unproduce_glyphs (it, row->used[TEXT_AREA]
22399 - n_glyphs_before);
22400 row->used[TEXT_AREA] = n_glyphs_before;
22401 it->current_x = x_before;
22403 else
22405 if (row->reversed_p)
22406 unproduce_glyphs (it, row->used[TEXT_AREA]
22407 - (n_glyphs_before + i));
22408 row->used[TEXT_AREA] = n_glyphs_before + i;
22409 it->current_x = x;
22411 break;
22413 else if (x + glyph->pixel_width >= it->first_visible_x)
22415 /* Glyph is at least partially visible. */
22416 ++it->hpos;
22417 if (x < it->first_visible_x)
22418 row->x = x - it->first_visible_x;
22420 else
22422 /* Glyph is off the left margin of the display area.
22423 Should not happen. */
22424 emacs_abort ();
22427 row->ascent = max (row->ascent, it->max_ascent);
22428 row->height = max (row->height, it->max_ascent + it->max_descent);
22429 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22430 row->phys_height = max (row->phys_height,
22431 it->max_phys_ascent + it->max_phys_descent);
22432 row->extra_line_spacing = max (row->extra_line_spacing,
22433 it->max_extra_line_spacing);
22434 x += glyph->pixel_width;
22435 ++i;
22438 /* Stop if max_x reached. */
22439 if (i < nglyphs)
22440 break;
22442 /* Stop at line ends. */
22443 if (ITERATOR_AT_END_OF_LINE_P (it))
22445 it->continuation_lines_width = 0;
22446 break;
22449 set_iterator_to_next (it, 1);
22450 if (STRINGP (it->string))
22451 it_charpos = IT_STRING_CHARPOS (*it);
22452 else
22453 it_charpos = IT_CHARPOS (*it);
22455 /* Stop if truncating at the right edge. */
22456 if (it->line_wrap == TRUNCATE
22457 && it->current_x >= it->last_visible_x)
22459 /* Add truncation mark, but don't do it if the line is
22460 truncated at a padding space. */
22461 if (it_charpos < it->string_nchars)
22463 if (!FRAME_WINDOW_P (it->f))
22465 int ii, n;
22467 if (it->current_x > it->last_visible_x)
22469 if (!row->reversed_p)
22471 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22472 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22473 break;
22475 else
22477 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22478 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22479 break;
22480 unproduce_glyphs (it, ii + 1);
22481 ii = row->used[TEXT_AREA] - (ii + 1);
22483 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22485 row->used[TEXT_AREA] = ii;
22486 produce_special_glyphs (it, IT_TRUNCATION);
22489 produce_special_glyphs (it, IT_TRUNCATION);
22491 row->truncated_on_right_p = 1;
22493 break;
22497 /* Maybe insert a truncation at the left. */
22498 if (it->first_visible_x
22499 && it_charpos > 0)
22501 if (!FRAME_WINDOW_P (it->f)
22502 || (row->reversed_p
22503 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22504 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22505 insert_left_trunc_glyphs (it);
22506 row->truncated_on_left_p = 1;
22509 it->face_id = saved_face_id;
22511 /* Value is number of columns displayed. */
22512 return it->hpos - hpos_at_start;
22517 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22518 appears as an element of LIST or as the car of an element of LIST.
22519 If PROPVAL is a list, compare each element against LIST in that
22520 way, and return 1/2 if any element of PROPVAL is found in LIST.
22521 Otherwise return 0. This function cannot quit.
22522 The return value is 2 if the text is invisible but with an ellipsis
22523 and 1 if it's invisible and without an ellipsis. */
22526 invisible_p (register Lisp_Object propval, Lisp_Object list)
22528 register Lisp_Object tail, proptail;
22530 for (tail = list; CONSP (tail); tail = XCDR (tail))
22532 register Lisp_Object tem;
22533 tem = XCAR (tail);
22534 if (EQ (propval, tem))
22535 return 1;
22536 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22537 return NILP (XCDR (tem)) ? 1 : 2;
22540 if (CONSP (propval))
22542 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22544 Lisp_Object propelt;
22545 propelt = XCAR (proptail);
22546 for (tail = list; CONSP (tail); tail = XCDR (tail))
22548 register Lisp_Object tem;
22549 tem = XCAR (tail);
22550 if (EQ (propelt, tem))
22551 return 1;
22552 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22553 return NILP (XCDR (tem)) ? 1 : 2;
22558 return 0;
22561 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22562 doc: /* Non-nil if the property makes the text invisible.
22563 POS-OR-PROP can be a marker or number, in which case it is taken to be
22564 a position in the current buffer and the value of the `invisible' property
22565 is checked; or it can be some other value, which is then presumed to be the
22566 value of the `invisible' property of the text of interest.
22567 The non-nil value returned can be t for truly invisible text or something
22568 else if the text is replaced by an ellipsis. */)
22569 (Lisp_Object pos_or_prop)
22571 Lisp_Object prop
22572 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22573 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22574 : pos_or_prop);
22575 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22576 return (invis == 0 ? Qnil
22577 : invis == 1 ? Qt
22578 : make_number (invis));
22581 /* Calculate a width or height in pixels from a specification using
22582 the following elements:
22584 SPEC ::=
22585 NUM - a (fractional) multiple of the default font width/height
22586 (NUM) - specifies exactly NUM pixels
22587 UNIT - a fixed number of pixels, see below.
22588 ELEMENT - size of a display element in pixels, see below.
22589 (NUM . SPEC) - equals NUM * SPEC
22590 (+ SPEC SPEC ...) - add pixel values
22591 (- SPEC SPEC ...) - subtract pixel values
22592 (- SPEC) - negate pixel value
22594 NUM ::=
22595 INT or FLOAT - a number constant
22596 SYMBOL - use symbol's (buffer local) variable binding.
22598 UNIT ::=
22599 in - pixels per inch *)
22600 mm - pixels per 1/1000 meter *)
22601 cm - pixels per 1/100 meter *)
22602 width - width of current font in pixels.
22603 height - height of current font in pixels.
22605 *) using the ratio(s) defined in display-pixels-per-inch.
22607 ELEMENT ::=
22609 left-fringe - left fringe width in pixels
22610 right-fringe - right fringe width in pixels
22612 left-margin - left margin width in pixels
22613 right-margin - right margin width in pixels
22615 scroll-bar - scroll-bar area width in pixels
22617 Examples:
22619 Pixels corresponding to 5 inches:
22620 (5 . in)
22622 Total width of non-text areas on left side of window (if scroll-bar is on left):
22623 '(space :width (+ left-fringe left-margin scroll-bar))
22625 Align to first text column (in header line):
22626 '(space :align-to 0)
22628 Align to middle of text area minus half the width of variable `my-image'
22629 containing a loaded image:
22630 '(space :align-to (0.5 . (- text my-image)))
22632 Width of left margin minus width of 1 character in the default font:
22633 '(space :width (- left-margin 1))
22635 Width of left margin minus width of 2 characters in the current font:
22636 '(space :width (- left-margin (2 . width)))
22638 Center 1 character over left-margin (in header line):
22639 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22641 Different ways to express width of left fringe plus left margin minus one pixel:
22642 '(space :width (- (+ left-fringe left-margin) (1)))
22643 '(space :width (+ left-fringe left-margin (- (1))))
22644 '(space :width (+ left-fringe left-margin (-1)))
22648 static int
22649 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22650 struct font *font, int width_p, int *align_to)
22652 double pixels;
22654 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22655 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22657 if (NILP (prop))
22658 return OK_PIXELS (0);
22660 eassert (FRAME_LIVE_P (it->f));
22662 if (SYMBOLP (prop))
22664 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22666 char *unit = SSDATA (SYMBOL_NAME (prop));
22668 if (unit[0] == 'i' && unit[1] == 'n')
22669 pixels = 1.0;
22670 else if (unit[0] == 'm' && unit[1] == 'm')
22671 pixels = 25.4;
22672 else if (unit[0] == 'c' && unit[1] == 'm')
22673 pixels = 2.54;
22674 else
22675 pixels = 0;
22676 if (pixels > 0)
22678 double ppi = (width_p ? FRAME_RES_X (it->f)
22679 : FRAME_RES_Y (it->f));
22681 if (ppi > 0)
22682 return OK_PIXELS (ppi / pixels);
22683 return 0;
22687 #ifdef HAVE_WINDOW_SYSTEM
22688 if (EQ (prop, Qheight))
22689 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22690 if (EQ (prop, Qwidth))
22691 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22692 #else
22693 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22694 return OK_PIXELS (1);
22695 #endif
22697 if (EQ (prop, Qtext))
22698 return OK_PIXELS (width_p
22699 ? window_box_width (it->w, TEXT_AREA)
22700 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22702 if (align_to && *align_to < 0)
22704 *res = 0;
22705 if (EQ (prop, Qleft))
22706 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22707 if (EQ (prop, Qright))
22708 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22709 if (EQ (prop, Qcenter))
22710 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22711 + window_box_width (it->w, TEXT_AREA) / 2);
22712 if (EQ (prop, Qleft_fringe))
22713 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22714 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22715 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22716 if (EQ (prop, Qright_fringe))
22717 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22718 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22719 : window_box_right_offset (it->w, TEXT_AREA));
22720 if (EQ (prop, Qleft_margin))
22721 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22722 if (EQ (prop, Qright_margin))
22723 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22724 if (EQ (prop, Qscroll_bar))
22725 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22727 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22728 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22729 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22730 : 0)));
22732 else
22734 if (EQ (prop, Qleft_fringe))
22735 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22736 if (EQ (prop, Qright_fringe))
22737 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22738 if (EQ (prop, Qleft_margin))
22739 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22740 if (EQ (prop, Qright_margin))
22741 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22742 if (EQ (prop, Qscroll_bar))
22743 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22746 prop = buffer_local_value_1 (prop, it->w->contents);
22747 if (EQ (prop, Qunbound))
22748 prop = Qnil;
22751 if (INTEGERP (prop) || FLOATP (prop))
22753 int base_unit = (width_p
22754 ? FRAME_COLUMN_WIDTH (it->f)
22755 : FRAME_LINE_HEIGHT (it->f));
22756 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22759 if (CONSP (prop))
22761 Lisp_Object car = XCAR (prop);
22762 Lisp_Object cdr = XCDR (prop);
22764 if (SYMBOLP (car))
22766 #ifdef HAVE_WINDOW_SYSTEM
22767 if (FRAME_WINDOW_P (it->f)
22768 && valid_image_p (prop))
22770 ptrdiff_t id = lookup_image (it->f, prop);
22771 struct image *img = IMAGE_FROM_ID (it->f, id);
22773 return OK_PIXELS (width_p ? img->width : img->height);
22775 #endif
22776 if (EQ (car, Qplus) || EQ (car, Qminus))
22778 int first = 1;
22779 double px;
22781 pixels = 0;
22782 while (CONSP (cdr))
22784 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22785 font, width_p, align_to))
22786 return 0;
22787 if (first)
22788 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22789 else
22790 pixels += px;
22791 cdr = XCDR (cdr);
22793 if (EQ (car, Qminus))
22794 pixels = -pixels;
22795 return OK_PIXELS (pixels);
22798 car = buffer_local_value_1 (car, it->w->contents);
22799 if (EQ (car, Qunbound))
22800 car = Qnil;
22803 if (INTEGERP (car) || FLOATP (car))
22805 double fact;
22806 pixels = XFLOATINT (car);
22807 if (NILP (cdr))
22808 return OK_PIXELS (pixels);
22809 if (calc_pixel_width_or_height (&fact, it, cdr,
22810 font, width_p, align_to))
22811 return OK_PIXELS (pixels * fact);
22812 return 0;
22815 return 0;
22818 return 0;
22822 /***********************************************************************
22823 Glyph Display
22824 ***********************************************************************/
22826 #ifdef HAVE_WINDOW_SYSTEM
22828 #ifdef GLYPH_DEBUG
22830 void
22831 dump_glyph_string (struct glyph_string *s)
22833 fprintf (stderr, "glyph string\n");
22834 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22835 s->x, s->y, s->width, s->height);
22836 fprintf (stderr, " ybase = %d\n", s->ybase);
22837 fprintf (stderr, " hl = %d\n", s->hl);
22838 fprintf (stderr, " left overhang = %d, right = %d\n",
22839 s->left_overhang, s->right_overhang);
22840 fprintf (stderr, " nchars = %d\n", s->nchars);
22841 fprintf (stderr, " extends to end of line = %d\n",
22842 s->extends_to_end_of_line_p);
22843 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22844 fprintf (stderr, " bg width = %d\n", s->background_width);
22847 #endif /* GLYPH_DEBUG */
22849 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22850 of XChar2b structures for S; it can't be allocated in
22851 init_glyph_string because it must be allocated via `alloca'. W
22852 is the window on which S is drawn. ROW and AREA are the glyph row
22853 and area within the row from which S is constructed. START is the
22854 index of the first glyph structure covered by S. HL is a
22855 face-override for drawing S. */
22857 #ifdef HAVE_NTGUI
22858 #define OPTIONAL_HDC(hdc) HDC hdc,
22859 #define DECLARE_HDC(hdc) HDC hdc;
22860 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22861 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22862 #endif
22864 #ifndef OPTIONAL_HDC
22865 #define OPTIONAL_HDC(hdc)
22866 #define DECLARE_HDC(hdc)
22867 #define ALLOCATE_HDC(hdc, f)
22868 #define RELEASE_HDC(hdc, f)
22869 #endif
22871 static void
22872 init_glyph_string (struct glyph_string *s,
22873 OPTIONAL_HDC (hdc)
22874 XChar2b *char2b, struct window *w, struct glyph_row *row,
22875 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22877 memset (s, 0, sizeof *s);
22878 s->w = w;
22879 s->f = XFRAME (w->frame);
22880 #ifdef HAVE_NTGUI
22881 s->hdc = hdc;
22882 #endif
22883 s->display = FRAME_X_DISPLAY (s->f);
22884 s->window = FRAME_X_WINDOW (s->f);
22885 s->char2b = char2b;
22886 s->hl = hl;
22887 s->row = row;
22888 s->area = area;
22889 s->first_glyph = row->glyphs[area] + start;
22890 s->height = row->height;
22891 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22892 s->ybase = s->y + row->ascent;
22896 /* Append the list of glyph strings with head H and tail T to the list
22897 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22899 static void
22900 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22901 struct glyph_string *h, struct glyph_string *t)
22903 if (h)
22905 if (*head)
22906 (*tail)->next = h;
22907 else
22908 *head = h;
22909 h->prev = *tail;
22910 *tail = t;
22915 /* Prepend the list of glyph strings with head H and tail T to the
22916 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22917 result. */
22919 static void
22920 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22921 struct glyph_string *h, struct glyph_string *t)
22923 if (h)
22925 if (*head)
22926 (*head)->prev = t;
22927 else
22928 *tail = t;
22929 t->next = *head;
22930 *head = h;
22935 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22936 Set *HEAD and *TAIL to the resulting list. */
22938 static void
22939 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22940 struct glyph_string *s)
22942 s->next = s->prev = NULL;
22943 append_glyph_string_lists (head, tail, s, s);
22947 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22948 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22949 make sure that X resources for the face returned are allocated.
22950 Value is a pointer to a realized face that is ready for display if
22951 DISPLAY_P is non-zero. */
22953 static struct face *
22954 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22955 XChar2b *char2b, int display_p)
22957 struct face *face = FACE_FROM_ID (f, face_id);
22958 unsigned code = 0;
22960 if (face->font)
22962 code = face->font->driver->encode_char (face->font, c);
22964 if (code == FONT_INVALID_CODE)
22965 code = 0;
22967 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22969 /* Make sure X resources of the face are allocated. */
22970 #ifdef HAVE_X_WINDOWS
22971 if (display_p)
22972 #endif
22974 eassert (face != NULL);
22975 PREPARE_FACE_FOR_DISPLAY (f, face);
22978 return face;
22982 /* Get face and two-byte form of character glyph GLYPH on frame F.
22983 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22984 a pointer to a realized face that is ready for display. */
22986 static struct face *
22987 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22988 XChar2b *char2b, int *two_byte_p)
22990 struct face *face;
22991 unsigned code = 0;
22993 eassert (glyph->type == CHAR_GLYPH);
22994 face = FACE_FROM_ID (f, glyph->face_id);
22996 /* Make sure X resources of the face are allocated. */
22997 eassert (face != NULL);
22998 PREPARE_FACE_FOR_DISPLAY (f, face);
23000 if (two_byte_p)
23001 *two_byte_p = 0;
23003 if (face->font)
23005 if (CHAR_BYTE8_P (glyph->u.ch))
23006 code = CHAR_TO_BYTE8 (glyph->u.ch);
23007 else
23008 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23010 if (code == FONT_INVALID_CODE)
23011 code = 0;
23014 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23015 return face;
23019 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23020 Return 1 if FONT has a glyph for C, otherwise return 0. */
23022 static int
23023 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23025 unsigned code;
23027 if (CHAR_BYTE8_P (c))
23028 code = CHAR_TO_BYTE8 (c);
23029 else
23030 code = font->driver->encode_char (font, c);
23032 if (code == FONT_INVALID_CODE)
23033 return 0;
23034 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23035 return 1;
23039 /* Fill glyph string S with composition components specified by S->cmp.
23041 BASE_FACE is the base face of the composition.
23042 S->cmp_from is the index of the first component for S.
23044 OVERLAPS non-zero means S should draw the foreground only, and use
23045 its physical height for clipping. See also draw_glyphs.
23047 Value is the index of a component not in S. */
23049 static int
23050 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23051 int overlaps)
23053 int i;
23054 /* For all glyphs of this composition, starting at the offset
23055 S->cmp_from, until we reach the end of the definition or encounter a
23056 glyph that requires the different face, add it to S. */
23057 struct face *face;
23059 eassert (s);
23061 s->for_overlaps = overlaps;
23062 s->face = NULL;
23063 s->font = NULL;
23064 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23066 int c = COMPOSITION_GLYPH (s->cmp, i);
23068 /* TAB in a composition means display glyphs with padding space
23069 on the left or right. */
23070 if (c != '\t')
23072 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23073 -1, Qnil);
23075 face = get_char_face_and_encoding (s->f, c, face_id,
23076 s->char2b + i, 1);
23077 if (face)
23079 if (! s->face)
23081 s->face = face;
23082 s->font = s->face->font;
23084 else if (s->face != face)
23085 break;
23088 ++s->nchars;
23090 s->cmp_to = i;
23092 if (s->face == NULL)
23094 s->face = base_face->ascii_face;
23095 s->font = s->face->font;
23098 /* All glyph strings for the same composition has the same width,
23099 i.e. the width set for the first component of the composition. */
23100 s->width = s->first_glyph->pixel_width;
23102 /* If the specified font could not be loaded, use the frame's
23103 default font, but record the fact that we couldn't load it in
23104 the glyph string so that we can draw rectangles for the
23105 characters of the glyph string. */
23106 if (s->font == NULL)
23108 s->font_not_found_p = 1;
23109 s->font = FRAME_FONT (s->f);
23112 /* Adjust base line for subscript/superscript text. */
23113 s->ybase += s->first_glyph->voffset;
23115 /* This glyph string must always be drawn with 16-bit functions. */
23116 s->two_byte_p = 1;
23118 return s->cmp_to;
23121 static int
23122 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23123 int start, int end, int overlaps)
23125 struct glyph *glyph, *last;
23126 Lisp_Object lgstring;
23127 int i;
23129 s->for_overlaps = overlaps;
23130 glyph = s->row->glyphs[s->area] + start;
23131 last = s->row->glyphs[s->area] + end;
23132 s->cmp_id = glyph->u.cmp.id;
23133 s->cmp_from = glyph->slice.cmp.from;
23134 s->cmp_to = glyph->slice.cmp.to + 1;
23135 s->face = FACE_FROM_ID (s->f, face_id);
23136 lgstring = composition_gstring_from_id (s->cmp_id);
23137 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23138 glyph++;
23139 while (glyph < last
23140 && glyph->u.cmp.automatic
23141 && glyph->u.cmp.id == s->cmp_id
23142 && s->cmp_to == glyph->slice.cmp.from)
23143 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23145 for (i = s->cmp_from; i < s->cmp_to; i++)
23147 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23148 unsigned code = LGLYPH_CODE (lglyph);
23150 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23152 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23153 return glyph - s->row->glyphs[s->area];
23157 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23158 See the comment of fill_glyph_string for arguments.
23159 Value is the index of the first glyph not in S. */
23162 static int
23163 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23164 int start, int end, int overlaps)
23166 struct glyph *glyph, *last;
23167 int voffset;
23169 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23170 s->for_overlaps = overlaps;
23171 glyph = s->row->glyphs[s->area] + start;
23172 last = s->row->glyphs[s->area] + end;
23173 voffset = glyph->voffset;
23174 s->face = FACE_FROM_ID (s->f, face_id);
23175 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23176 s->nchars = 1;
23177 s->width = glyph->pixel_width;
23178 glyph++;
23179 while (glyph < last
23180 && glyph->type == GLYPHLESS_GLYPH
23181 && glyph->voffset == voffset
23182 && glyph->face_id == face_id)
23184 s->nchars++;
23185 s->width += glyph->pixel_width;
23186 glyph++;
23188 s->ybase += voffset;
23189 return glyph - s->row->glyphs[s->area];
23193 /* Fill glyph string S from a sequence of character glyphs.
23195 FACE_ID is the face id of the string. START is the index of the
23196 first glyph to consider, END is the index of the last + 1.
23197 OVERLAPS non-zero means S should draw the foreground only, and use
23198 its physical height for clipping. See also draw_glyphs.
23200 Value is the index of the first glyph not in S. */
23202 static int
23203 fill_glyph_string (struct glyph_string *s, int face_id,
23204 int start, int end, int overlaps)
23206 struct glyph *glyph, *last;
23207 int voffset;
23208 int glyph_not_available_p;
23210 eassert (s->f == XFRAME (s->w->frame));
23211 eassert (s->nchars == 0);
23212 eassert (start >= 0 && end > start);
23214 s->for_overlaps = overlaps;
23215 glyph = s->row->glyphs[s->area] + start;
23216 last = s->row->glyphs[s->area] + end;
23217 voffset = glyph->voffset;
23218 s->padding_p = glyph->padding_p;
23219 glyph_not_available_p = glyph->glyph_not_available_p;
23221 while (glyph < last
23222 && glyph->type == CHAR_GLYPH
23223 && glyph->voffset == voffset
23224 /* Same face id implies same font, nowadays. */
23225 && glyph->face_id == face_id
23226 && glyph->glyph_not_available_p == glyph_not_available_p)
23228 int two_byte_p;
23230 s->face = get_glyph_face_and_encoding (s->f, glyph,
23231 s->char2b + s->nchars,
23232 &two_byte_p);
23233 s->two_byte_p = two_byte_p;
23234 ++s->nchars;
23235 eassert (s->nchars <= end - start);
23236 s->width += glyph->pixel_width;
23237 if (glyph++->padding_p != s->padding_p)
23238 break;
23241 s->font = s->face->font;
23243 /* If the specified font could not be loaded, use the frame's font,
23244 but record the fact that we couldn't load it in
23245 S->font_not_found_p so that we can draw rectangles for the
23246 characters of the glyph string. */
23247 if (s->font == NULL || glyph_not_available_p)
23249 s->font_not_found_p = 1;
23250 s->font = FRAME_FONT (s->f);
23253 /* Adjust base line for subscript/superscript text. */
23254 s->ybase += voffset;
23256 eassert (s->face && s->face->gc);
23257 return glyph - s->row->glyphs[s->area];
23261 /* Fill glyph string S from image glyph S->first_glyph. */
23263 static void
23264 fill_image_glyph_string (struct glyph_string *s)
23266 eassert (s->first_glyph->type == IMAGE_GLYPH);
23267 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23268 eassert (s->img);
23269 s->slice = s->first_glyph->slice.img;
23270 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23271 s->font = s->face->font;
23272 s->width = s->first_glyph->pixel_width;
23274 /* Adjust base line for subscript/superscript text. */
23275 s->ybase += s->first_glyph->voffset;
23279 /* Fill glyph string S from a sequence of stretch glyphs.
23281 START is the index of the first glyph to consider,
23282 END is the index of the last + 1.
23284 Value is the index of the first glyph not in S. */
23286 static int
23287 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23289 struct glyph *glyph, *last;
23290 int voffset, face_id;
23292 eassert (s->first_glyph->type == STRETCH_GLYPH);
23294 glyph = s->row->glyphs[s->area] + start;
23295 last = s->row->glyphs[s->area] + end;
23296 face_id = glyph->face_id;
23297 s->face = FACE_FROM_ID (s->f, face_id);
23298 s->font = s->face->font;
23299 s->width = glyph->pixel_width;
23300 s->nchars = 1;
23301 voffset = glyph->voffset;
23303 for (++glyph;
23304 (glyph < last
23305 && glyph->type == STRETCH_GLYPH
23306 && glyph->voffset == voffset
23307 && glyph->face_id == face_id);
23308 ++glyph)
23309 s->width += glyph->pixel_width;
23311 /* Adjust base line for subscript/superscript text. */
23312 s->ybase += voffset;
23314 /* The case that face->gc == 0 is handled when drawing the glyph
23315 string by calling PREPARE_FACE_FOR_DISPLAY. */
23316 eassert (s->face);
23317 return glyph - s->row->glyphs[s->area];
23320 static struct font_metrics *
23321 get_per_char_metric (struct font *font, XChar2b *char2b)
23323 static struct font_metrics metrics;
23324 unsigned code;
23326 if (! font)
23327 return NULL;
23328 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23329 if (code == FONT_INVALID_CODE)
23330 return NULL;
23331 font->driver->text_extents (font, &code, 1, &metrics);
23332 return &metrics;
23335 /* EXPORT for RIF:
23336 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23337 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23338 assumed to be zero. */
23340 void
23341 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23343 *left = *right = 0;
23345 if (glyph->type == CHAR_GLYPH)
23347 struct face *face;
23348 XChar2b char2b;
23349 struct font_metrics *pcm;
23351 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23352 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23354 if (pcm->rbearing > pcm->width)
23355 *right = pcm->rbearing - pcm->width;
23356 if (pcm->lbearing < 0)
23357 *left = -pcm->lbearing;
23360 else if (glyph->type == COMPOSITE_GLYPH)
23362 if (! glyph->u.cmp.automatic)
23364 struct composition *cmp = composition_table[glyph->u.cmp.id];
23366 if (cmp->rbearing > cmp->pixel_width)
23367 *right = cmp->rbearing - cmp->pixel_width;
23368 if (cmp->lbearing < 0)
23369 *left = - cmp->lbearing;
23371 else
23373 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23374 struct font_metrics metrics;
23376 composition_gstring_width (gstring, glyph->slice.cmp.from,
23377 glyph->slice.cmp.to + 1, &metrics);
23378 if (metrics.rbearing > metrics.width)
23379 *right = metrics.rbearing - metrics.width;
23380 if (metrics.lbearing < 0)
23381 *left = - metrics.lbearing;
23387 /* Return the index of the first glyph preceding glyph string S that
23388 is overwritten by S because of S's left overhang. Value is -1
23389 if no glyphs are overwritten. */
23391 static int
23392 left_overwritten (struct glyph_string *s)
23394 int k;
23396 if (s->left_overhang)
23398 int x = 0, i;
23399 struct glyph *glyphs = s->row->glyphs[s->area];
23400 int first = s->first_glyph - glyphs;
23402 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23403 x -= glyphs[i].pixel_width;
23405 k = i + 1;
23407 else
23408 k = -1;
23410 return k;
23414 /* Return the index of the first glyph preceding glyph string S that
23415 is overwriting S because of its right overhang. Value is -1 if no
23416 glyph in front of S overwrites S. */
23418 static int
23419 left_overwriting (struct glyph_string *s)
23421 int i, k, x;
23422 struct glyph *glyphs = s->row->glyphs[s->area];
23423 int first = s->first_glyph - glyphs;
23425 k = -1;
23426 x = 0;
23427 for (i = first - 1; i >= 0; --i)
23429 int left, right;
23430 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23431 if (x + right > 0)
23432 k = i;
23433 x -= glyphs[i].pixel_width;
23436 return k;
23440 /* Return the index of the last glyph following glyph string S that is
23441 overwritten by S because of S's right overhang. Value is -1 if
23442 no such glyph is found. */
23444 static int
23445 right_overwritten (struct glyph_string *s)
23447 int k = -1;
23449 if (s->right_overhang)
23451 int x = 0, i;
23452 struct glyph *glyphs = s->row->glyphs[s->area];
23453 int first = (s->first_glyph - glyphs
23454 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23455 int end = s->row->used[s->area];
23457 for (i = first; i < end && s->right_overhang > x; ++i)
23458 x += glyphs[i].pixel_width;
23460 k = i;
23463 return k;
23467 /* Return the index of the last glyph following glyph string S that
23468 overwrites S because of its left overhang. Value is negative
23469 if no such glyph is found. */
23471 static int
23472 right_overwriting (struct glyph_string *s)
23474 int i, k, x;
23475 int end = s->row->used[s->area];
23476 struct glyph *glyphs = s->row->glyphs[s->area];
23477 int first = (s->first_glyph - glyphs
23478 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23480 k = -1;
23481 x = 0;
23482 for (i = first; i < end; ++i)
23484 int left, right;
23485 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23486 if (x - left < 0)
23487 k = i;
23488 x += glyphs[i].pixel_width;
23491 return k;
23495 /* Set background width of glyph string S. START is the index of the
23496 first glyph following S. LAST_X is the right-most x-position + 1
23497 in the drawing area. */
23499 static void
23500 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23502 /* If the face of this glyph string has to be drawn to the end of
23503 the drawing area, set S->extends_to_end_of_line_p. */
23505 if (start == s->row->used[s->area]
23506 && s->area == TEXT_AREA
23507 && ((s->row->fill_line_p
23508 && (s->hl == DRAW_NORMAL_TEXT
23509 || s->hl == DRAW_IMAGE_RAISED
23510 || s->hl == DRAW_IMAGE_SUNKEN))
23511 || s->hl == DRAW_MOUSE_FACE))
23512 s->extends_to_end_of_line_p = 1;
23514 /* If S extends its face to the end of the line, set its
23515 background_width to the distance to the right edge of the drawing
23516 area. */
23517 if (s->extends_to_end_of_line_p)
23518 s->background_width = last_x - s->x + 1;
23519 else
23520 s->background_width = s->width;
23524 /* Compute overhangs and x-positions for glyph string S and its
23525 predecessors, or successors. X is the starting x-position for S.
23526 BACKWARD_P non-zero means process predecessors. */
23528 static void
23529 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23531 if (backward_p)
23533 while (s)
23535 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23536 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23537 x -= s->width;
23538 s->x = x;
23539 s = s->prev;
23542 else
23544 while (s)
23546 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23547 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23548 s->x = x;
23549 x += s->width;
23550 s = s->next;
23557 /* The following macros are only called from draw_glyphs below.
23558 They reference the following parameters of that function directly:
23559 `w', `row', `area', and `overlap_p'
23560 as well as the following local variables:
23561 `s', `f', and `hdc' (in W32) */
23563 #ifdef HAVE_NTGUI
23564 /* On W32, silently add local `hdc' variable to argument list of
23565 init_glyph_string. */
23566 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23567 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23568 #else
23569 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23570 init_glyph_string (s, char2b, w, row, area, start, hl)
23571 #endif
23573 /* Add a glyph string for a stretch glyph to the list of strings
23574 between HEAD and TAIL. START is the index of the stretch glyph in
23575 row area AREA of glyph row ROW. END is the index of the last glyph
23576 in that glyph row area. X is the current output position assigned
23577 to the new glyph string constructed. HL overrides that face of the
23578 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23579 is the right-most x-position of the drawing area. */
23581 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23582 and below -- keep them on one line. */
23583 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23584 do \
23586 s = alloca (sizeof *s); \
23587 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23588 START = fill_stretch_glyph_string (s, START, END); \
23589 append_glyph_string (&HEAD, &TAIL, s); \
23590 s->x = (X); \
23592 while (0)
23595 /* Add a glyph string for an image glyph to the list of strings
23596 between HEAD and TAIL. START is the index of the image glyph in
23597 row area AREA of glyph row ROW. END is the index of the last glyph
23598 in that glyph row area. X is the current output position assigned
23599 to the new glyph string constructed. HL overrides that face of the
23600 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23601 is the right-most x-position of the drawing area. */
23603 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23604 do \
23606 s = alloca (sizeof *s); \
23607 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23608 fill_image_glyph_string (s); \
23609 append_glyph_string (&HEAD, &TAIL, s); \
23610 ++START; \
23611 s->x = (X); \
23613 while (0)
23616 /* Add a glyph string for a sequence of character glyphs to the list
23617 of strings between HEAD and TAIL. START is the index of the first
23618 glyph in row area AREA of glyph row ROW that is part of the new
23619 glyph string. END is the index of the last glyph in that glyph row
23620 area. X is the current output position assigned to the new glyph
23621 string constructed. HL overrides that face of the glyph; e.g. it
23622 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23623 right-most x-position of the drawing area. */
23625 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23626 do \
23628 int face_id; \
23629 XChar2b *char2b; \
23631 face_id = (row)->glyphs[area][START].face_id; \
23633 s = alloca (sizeof *s); \
23634 char2b = alloca ((END - START) * sizeof *char2b); \
23635 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23636 append_glyph_string (&HEAD, &TAIL, s); \
23637 s->x = (X); \
23638 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23640 while (0)
23643 /* Add a glyph string for a composite sequence to the list of strings
23644 between HEAD and TAIL. START is the index of the first glyph in
23645 row area AREA of glyph row ROW that is part of the new glyph
23646 string. END is the index of the last glyph in that glyph row area.
23647 X is the current output position assigned to the new glyph string
23648 constructed. HL overrides that face of the glyph; e.g. it is
23649 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23650 x-position of the drawing area. */
23652 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23653 do { \
23654 int face_id = (row)->glyphs[area][START].face_id; \
23655 struct face *base_face = FACE_FROM_ID (f, face_id); \
23656 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23657 struct composition *cmp = composition_table[cmp_id]; \
23658 XChar2b *char2b; \
23659 struct glyph_string *first_s = NULL; \
23660 int n; \
23662 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23664 /* Make glyph_strings for each glyph sequence that is drawable by \
23665 the same face, and append them to HEAD/TAIL. */ \
23666 for (n = 0; n < cmp->glyph_len;) \
23668 s = alloca (sizeof *s); \
23669 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23670 append_glyph_string (&(HEAD), &(TAIL), s); \
23671 s->cmp = cmp; \
23672 s->cmp_from = n; \
23673 s->x = (X); \
23674 if (n == 0) \
23675 first_s = s; \
23676 n = fill_composite_glyph_string (s, base_face, overlaps); \
23679 ++START; \
23680 s = first_s; \
23681 } while (0)
23684 /* Add a glyph string for a glyph-string sequence to the list of strings
23685 between HEAD and TAIL. */
23687 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23688 do { \
23689 int face_id; \
23690 XChar2b *char2b; \
23691 Lisp_Object gstring; \
23693 face_id = (row)->glyphs[area][START].face_id; \
23694 gstring = (composition_gstring_from_id \
23695 ((row)->glyphs[area][START].u.cmp.id)); \
23696 s = alloca (sizeof *s); \
23697 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23698 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23699 append_glyph_string (&(HEAD), &(TAIL), s); \
23700 s->x = (X); \
23701 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23702 } while (0)
23705 /* Add a glyph string for a sequence of glyphless character's glyphs
23706 to the list of strings between HEAD and TAIL. The meanings of
23707 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23709 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23710 do \
23712 int face_id; \
23714 face_id = (row)->glyphs[area][START].face_id; \
23716 s = alloca (sizeof *s); \
23717 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23718 append_glyph_string (&HEAD, &TAIL, s); \
23719 s->x = (X); \
23720 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23721 overlaps); \
23723 while (0)
23726 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23727 of AREA of glyph row ROW on window W between indices START and END.
23728 HL overrides the face for drawing glyph strings, e.g. it is
23729 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23730 x-positions of the drawing area.
23732 This is an ugly monster macro construct because we must use alloca
23733 to allocate glyph strings (because draw_glyphs can be called
23734 asynchronously). */
23736 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23737 do \
23739 HEAD = TAIL = NULL; \
23740 while (START < END) \
23742 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23743 switch (first_glyph->type) \
23745 case CHAR_GLYPH: \
23746 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23747 HL, X, LAST_X); \
23748 break; \
23750 case COMPOSITE_GLYPH: \
23751 if (first_glyph->u.cmp.automatic) \
23752 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23753 HL, X, LAST_X); \
23754 else \
23755 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23756 HL, X, LAST_X); \
23757 break; \
23759 case STRETCH_GLYPH: \
23760 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23761 HL, X, LAST_X); \
23762 break; \
23764 case IMAGE_GLYPH: \
23765 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23766 HL, X, LAST_X); \
23767 break; \
23769 case GLYPHLESS_GLYPH: \
23770 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23771 HL, X, LAST_X); \
23772 break; \
23774 default: \
23775 emacs_abort (); \
23778 if (s) \
23780 set_glyph_string_background_width (s, START, LAST_X); \
23781 (X) += s->width; \
23784 } while (0)
23787 /* Draw glyphs between START and END in AREA of ROW on window W,
23788 starting at x-position X. X is relative to AREA in W. HL is a
23789 face-override with the following meaning:
23791 DRAW_NORMAL_TEXT draw normally
23792 DRAW_CURSOR draw in cursor face
23793 DRAW_MOUSE_FACE draw in mouse face.
23794 DRAW_INVERSE_VIDEO draw in mode line face
23795 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23796 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23798 If OVERLAPS is non-zero, draw only the foreground of characters and
23799 clip to the physical height of ROW. Non-zero value also defines
23800 the overlapping part to be drawn:
23802 OVERLAPS_PRED overlap with preceding rows
23803 OVERLAPS_SUCC overlap with succeeding rows
23804 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23805 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23807 Value is the x-position reached, relative to AREA of W. */
23809 static int
23810 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23811 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23812 enum draw_glyphs_face hl, int overlaps)
23814 struct glyph_string *head, *tail;
23815 struct glyph_string *s;
23816 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23817 int i, j, x_reached, last_x, area_left = 0;
23818 struct frame *f = XFRAME (WINDOW_FRAME (w));
23819 DECLARE_HDC (hdc);
23821 ALLOCATE_HDC (hdc, f);
23823 /* Let's rather be paranoid than getting a SEGV. */
23824 end = min (end, row->used[area]);
23825 start = clip_to_bounds (0, start, end);
23827 /* Translate X to frame coordinates. Set last_x to the right
23828 end of the drawing area. */
23829 if (row->full_width_p)
23831 /* X is relative to the left edge of W, without scroll bars
23832 or fringes. */
23833 area_left = WINDOW_LEFT_EDGE_X (w);
23834 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23836 else
23838 area_left = window_box_left (w, area);
23839 last_x = area_left + window_box_width (w, area);
23841 x += area_left;
23843 /* Build a doubly-linked list of glyph_string structures between
23844 head and tail from what we have to draw. Note that the macro
23845 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23846 the reason we use a separate variable `i'. */
23847 i = start;
23848 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23849 if (tail)
23850 x_reached = tail->x + tail->background_width;
23851 else
23852 x_reached = x;
23854 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23855 the row, redraw some glyphs in front or following the glyph
23856 strings built above. */
23857 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23859 struct glyph_string *h, *t;
23860 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23861 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23862 int check_mouse_face = 0;
23863 int dummy_x = 0;
23865 /* If mouse highlighting is on, we may need to draw adjacent
23866 glyphs using mouse-face highlighting. */
23867 if (area == TEXT_AREA && row->mouse_face_p
23868 && hlinfo->mouse_face_beg_row >= 0
23869 && hlinfo->mouse_face_end_row >= 0)
23871 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23873 if (row_vpos >= hlinfo->mouse_face_beg_row
23874 && row_vpos <= hlinfo->mouse_face_end_row)
23876 check_mouse_face = 1;
23877 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23878 ? hlinfo->mouse_face_beg_col : 0;
23879 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23880 ? hlinfo->mouse_face_end_col
23881 : row->used[TEXT_AREA];
23885 /* Compute overhangs for all glyph strings. */
23886 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23887 for (s = head; s; s = s->next)
23888 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23890 /* Prepend glyph strings for glyphs in front of the first glyph
23891 string that are overwritten because of the first glyph
23892 string's left overhang. The background of all strings
23893 prepended must be drawn because the first glyph string
23894 draws over it. */
23895 i = left_overwritten (head);
23896 if (i >= 0)
23898 enum draw_glyphs_face overlap_hl;
23900 /* If this row contains mouse highlighting, attempt to draw
23901 the overlapped glyphs with the correct highlight. This
23902 code fails if the overlap encompasses more than one glyph
23903 and mouse-highlight spans only some of these glyphs.
23904 However, making it work perfectly involves a lot more
23905 code, and I don't know if the pathological case occurs in
23906 practice, so we'll stick to this for now. --- cyd */
23907 if (check_mouse_face
23908 && mouse_beg_col < start && mouse_end_col > i)
23909 overlap_hl = DRAW_MOUSE_FACE;
23910 else
23911 overlap_hl = DRAW_NORMAL_TEXT;
23913 j = i;
23914 BUILD_GLYPH_STRINGS (j, start, h, t,
23915 overlap_hl, dummy_x, last_x);
23916 start = i;
23917 compute_overhangs_and_x (t, head->x, 1);
23918 prepend_glyph_string_lists (&head, &tail, h, t);
23919 clip_head = head;
23922 /* Prepend glyph strings for glyphs in front of the first glyph
23923 string that overwrite that glyph string because of their
23924 right overhang. For these strings, only the foreground must
23925 be drawn, because it draws over the glyph string at `head'.
23926 The background must not be drawn because this would overwrite
23927 right overhangs of preceding glyphs for which no glyph
23928 strings exist. */
23929 i = left_overwriting (head);
23930 if (i >= 0)
23932 enum draw_glyphs_face overlap_hl;
23934 if (check_mouse_face
23935 && mouse_beg_col < start && mouse_end_col > i)
23936 overlap_hl = DRAW_MOUSE_FACE;
23937 else
23938 overlap_hl = DRAW_NORMAL_TEXT;
23940 clip_head = head;
23941 BUILD_GLYPH_STRINGS (i, start, h, t,
23942 overlap_hl, dummy_x, last_x);
23943 for (s = h; s; s = s->next)
23944 s->background_filled_p = 1;
23945 compute_overhangs_and_x (t, head->x, 1);
23946 prepend_glyph_string_lists (&head, &tail, h, t);
23949 /* Append glyphs strings for glyphs following the last glyph
23950 string tail that are overwritten by tail. The background of
23951 these strings has to be drawn because tail's foreground draws
23952 over it. */
23953 i = right_overwritten (tail);
23954 if (i >= 0)
23956 enum draw_glyphs_face overlap_hl;
23958 if (check_mouse_face
23959 && mouse_beg_col < i && mouse_end_col > end)
23960 overlap_hl = DRAW_MOUSE_FACE;
23961 else
23962 overlap_hl = DRAW_NORMAL_TEXT;
23964 BUILD_GLYPH_STRINGS (end, i, h, t,
23965 overlap_hl, x, last_x);
23966 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23967 we don't have `end = i;' here. */
23968 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23969 append_glyph_string_lists (&head, &tail, h, t);
23970 clip_tail = tail;
23973 /* Append glyph strings for glyphs following the last glyph
23974 string tail that overwrite tail. The foreground of such
23975 glyphs has to be drawn because it writes into the background
23976 of tail. The background must not be drawn because it could
23977 paint over the foreground of following glyphs. */
23978 i = right_overwriting (tail);
23979 if (i >= 0)
23981 enum draw_glyphs_face overlap_hl;
23982 if (check_mouse_face
23983 && mouse_beg_col < i && mouse_end_col > end)
23984 overlap_hl = DRAW_MOUSE_FACE;
23985 else
23986 overlap_hl = DRAW_NORMAL_TEXT;
23988 clip_tail = tail;
23989 i++; /* We must include the Ith glyph. */
23990 BUILD_GLYPH_STRINGS (end, i, h, t,
23991 overlap_hl, x, last_x);
23992 for (s = h; s; s = s->next)
23993 s->background_filled_p = 1;
23994 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23995 append_glyph_string_lists (&head, &tail, h, t);
23997 if (clip_head || clip_tail)
23998 for (s = head; s; s = s->next)
24000 s->clip_head = clip_head;
24001 s->clip_tail = clip_tail;
24005 /* Draw all strings. */
24006 for (s = head; s; s = s->next)
24007 FRAME_RIF (f)->draw_glyph_string (s);
24009 #ifndef HAVE_NS
24010 /* When focus a sole frame and move horizontally, this sets on_p to 0
24011 causing a failure to erase prev cursor position. */
24012 if (area == TEXT_AREA
24013 && !row->full_width_p
24014 /* When drawing overlapping rows, only the glyph strings'
24015 foreground is drawn, which doesn't erase a cursor
24016 completely. */
24017 && !overlaps)
24019 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24020 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24021 : (tail ? tail->x + tail->background_width : x));
24022 x0 -= area_left;
24023 x1 -= area_left;
24025 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24026 row->y, MATRIX_ROW_BOTTOM_Y (row));
24028 #endif
24030 /* Value is the x-position up to which drawn, relative to AREA of W.
24031 This doesn't include parts drawn because of overhangs. */
24032 if (row->full_width_p)
24033 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24034 else
24035 x_reached -= area_left;
24037 RELEASE_HDC (hdc, f);
24039 return x_reached;
24042 /* Expand row matrix if too narrow. Don't expand if area
24043 is not present. */
24045 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24047 if (!fonts_changed_p \
24048 && (it->glyph_row->glyphs[area] \
24049 < it->glyph_row->glyphs[area + 1])) \
24051 it->w->ncols_scale_factor++; \
24052 fonts_changed_p = 1; \
24056 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24057 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24059 static void
24060 append_glyph (struct it *it)
24062 struct glyph *glyph;
24063 enum glyph_row_area area = it->area;
24065 eassert (it->glyph_row);
24066 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24068 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24069 if (glyph < it->glyph_row->glyphs[area + 1])
24071 /* If the glyph row is reversed, we need to prepend the glyph
24072 rather than append it. */
24073 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24075 struct glyph *g;
24077 /* Make room for the additional glyph. */
24078 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24079 g[1] = *g;
24080 glyph = it->glyph_row->glyphs[area];
24082 glyph->charpos = CHARPOS (it->position);
24083 glyph->object = it->object;
24084 if (it->pixel_width > 0)
24086 glyph->pixel_width = it->pixel_width;
24087 glyph->padding_p = 0;
24089 else
24091 /* Assure at least 1-pixel width. Otherwise, cursor can't
24092 be displayed correctly. */
24093 glyph->pixel_width = 1;
24094 glyph->padding_p = 1;
24096 glyph->ascent = it->ascent;
24097 glyph->descent = it->descent;
24098 glyph->voffset = it->voffset;
24099 glyph->type = CHAR_GLYPH;
24100 glyph->avoid_cursor_p = it->avoid_cursor_p;
24101 glyph->multibyte_p = it->multibyte_p;
24102 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24104 /* In R2L rows, the left and the right box edges need to be
24105 drawn in reverse direction. */
24106 glyph->right_box_line_p = it->start_of_box_run_p;
24107 glyph->left_box_line_p = it->end_of_box_run_p;
24109 else
24111 glyph->left_box_line_p = it->start_of_box_run_p;
24112 glyph->right_box_line_p = it->end_of_box_run_p;
24114 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24115 || it->phys_descent > it->descent);
24116 glyph->glyph_not_available_p = it->glyph_not_available_p;
24117 glyph->face_id = it->face_id;
24118 glyph->u.ch = it->char_to_display;
24119 glyph->slice.img = null_glyph_slice;
24120 glyph->font_type = FONT_TYPE_UNKNOWN;
24121 if (it->bidi_p)
24123 glyph->resolved_level = it->bidi_it.resolved_level;
24124 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24125 emacs_abort ();
24126 glyph->bidi_type = it->bidi_it.type;
24128 else
24130 glyph->resolved_level = 0;
24131 glyph->bidi_type = UNKNOWN_BT;
24133 ++it->glyph_row->used[area];
24135 else
24136 IT_EXPAND_MATRIX_WIDTH (it, area);
24139 /* Store one glyph for the composition IT->cmp_it.id in
24140 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24141 non-null. */
24143 static void
24144 append_composite_glyph (struct it *it)
24146 struct glyph *glyph;
24147 enum glyph_row_area area = it->area;
24149 eassert (it->glyph_row);
24151 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24152 if (glyph < it->glyph_row->glyphs[area + 1])
24154 /* If the glyph row is reversed, we need to prepend the glyph
24155 rather than append it. */
24156 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24158 struct glyph *g;
24160 /* Make room for the new glyph. */
24161 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24162 g[1] = *g;
24163 glyph = it->glyph_row->glyphs[it->area];
24165 glyph->charpos = it->cmp_it.charpos;
24166 glyph->object = it->object;
24167 glyph->pixel_width = it->pixel_width;
24168 glyph->ascent = it->ascent;
24169 glyph->descent = it->descent;
24170 glyph->voffset = it->voffset;
24171 glyph->type = COMPOSITE_GLYPH;
24172 if (it->cmp_it.ch < 0)
24174 glyph->u.cmp.automatic = 0;
24175 glyph->u.cmp.id = it->cmp_it.id;
24176 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24178 else
24180 glyph->u.cmp.automatic = 1;
24181 glyph->u.cmp.id = it->cmp_it.id;
24182 glyph->slice.cmp.from = it->cmp_it.from;
24183 glyph->slice.cmp.to = it->cmp_it.to - 1;
24185 glyph->avoid_cursor_p = it->avoid_cursor_p;
24186 glyph->multibyte_p = it->multibyte_p;
24187 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24189 /* In R2L rows, the left and the right box edges need to be
24190 drawn in reverse direction. */
24191 glyph->right_box_line_p = it->start_of_box_run_p;
24192 glyph->left_box_line_p = it->end_of_box_run_p;
24194 else
24196 glyph->left_box_line_p = it->start_of_box_run_p;
24197 glyph->right_box_line_p = it->end_of_box_run_p;
24199 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24200 || it->phys_descent > it->descent);
24201 glyph->padding_p = 0;
24202 glyph->glyph_not_available_p = 0;
24203 glyph->face_id = it->face_id;
24204 glyph->font_type = FONT_TYPE_UNKNOWN;
24205 if (it->bidi_p)
24207 glyph->resolved_level = it->bidi_it.resolved_level;
24208 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24209 emacs_abort ();
24210 glyph->bidi_type = it->bidi_it.type;
24212 ++it->glyph_row->used[area];
24214 else
24215 IT_EXPAND_MATRIX_WIDTH (it, area);
24219 /* Change IT->ascent and IT->height according to the setting of
24220 IT->voffset. */
24222 static void
24223 take_vertical_position_into_account (struct it *it)
24225 if (it->voffset)
24227 if (it->voffset < 0)
24228 /* Increase the ascent so that we can display the text higher
24229 in the line. */
24230 it->ascent -= it->voffset;
24231 else
24232 /* Increase the descent so that we can display the text lower
24233 in the line. */
24234 it->descent += it->voffset;
24239 /* Produce glyphs/get display metrics for the image IT is loaded with.
24240 See the description of struct display_iterator in dispextern.h for
24241 an overview of struct display_iterator. */
24243 static void
24244 produce_image_glyph (struct it *it)
24246 struct image *img;
24247 struct face *face;
24248 int glyph_ascent, crop;
24249 struct glyph_slice slice;
24251 eassert (it->what == IT_IMAGE);
24253 face = FACE_FROM_ID (it->f, it->face_id);
24254 eassert (face);
24255 /* Make sure X resources of the face is loaded. */
24256 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24258 if (it->image_id < 0)
24260 /* Fringe bitmap. */
24261 it->ascent = it->phys_ascent = 0;
24262 it->descent = it->phys_descent = 0;
24263 it->pixel_width = 0;
24264 it->nglyphs = 0;
24265 return;
24268 img = IMAGE_FROM_ID (it->f, it->image_id);
24269 eassert (img);
24270 /* Make sure X resources of the image is loaded. */
24271 prepare_image_for_display (it->f, img);
24273 slice.x = slice.y = 0;
24274 slice.width = img->width;
24275 slice.height = img->height;
24277 if (INTEGERP (it->slice.x))
24278 slice.x = XINT (it->slice.x);
24279 else if (FLOATP (it->slice.x))
24280 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24282 if (INTEGERP (it->slice.y))
24283 slice.y = XINT (it->slice.y);
24284 else if (FLOATP (it->slice.y))
24285 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24287 if (INTEGERP (it->slice.width))
24288 slice.width = XINT (it->slice.width);
24289 else if (FLOATP (it->slice.width))
24290 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24292 if (INTEGERP (it->slice.height))
24293 slice.height = XINT (it->slice.height);
24294 else if (FLOATP (it->slice.height))
24295 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24297 if (slice.x >= img->width)
24298 slice.x = img->width;
24299 if (slice.y >= img->height)
24300 slice.y = img->height;
24301 if (slice.x + slice.width >= img->width)
24302 slice.width = img->width - slice.x;
24303 if (slice.y + slice.height > img->height)
24304 slice.height = img->height - slice.y;
24306 if (slice.width == 0 || slice.height == 0)
24307 return;
24309 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24311 it->descent = slice.height - glyph_ascent;
24312 if (slice.y == 0)
24313 it->descent += img->vmargin;
24314 if (slice.y + slice.height == img->height)
24315 it->descent += img->vmargin;
24316 it->phys_descent = it->descent;
24318 it->pixel_width = slice.width;
24319 if (slice.x == 0)
24320 it->pixel_width += img->hmargin;
24321 if (slice.x + slice.width == img->width)
24322 it->pixel_width += img->hmargin;
24324 /* It's quite possible for images to have an ascent greater than
24325 their height, so don't get confused in that case. */
24326 if (it->descent < 0)
24327 it->descent = 0;
24329 it->nglyphs = 1;
24331 if (face->box != FACE_NO_BOX)
24333 if (face->box_line_width > 0)
24335 if (slice.y == 0)
24336 it->ascent += face->box_line_width;
24337 if (slice.y + slice.height == img->height)
24338 it->descent += face->box_line_width;
24341 if (it->start_of_box_run_p && slice.x == 0)
24342 it->pixel_width += eabs (face->box_line_width);
24343 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24344 it->pixel_width += eabs (face->box_line_width);
24347 take_vertical_position_into_account (it);
24349 /* Automatically crop wide image glyphs at right edge so we can
24350 draw the cursor on same display row. */
24351 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24352 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24354 it->pixel_width -= crop;
24355 slice.width -= crop;
24358 if (it->glyph_row)
24360 struct glyph *glyph;
24361 enum glyph_row_area area = it->area;
24363 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24364 if (glyph < it->glyph_row->glyphs[area + 1])
24366 glyph->charpos = CHARPOS (it->position);
24367 glyph->object = it->object;
24368 glyph->pixel_width = it->pixel_width;
24369 glyph->ascent = glyph_ascent;
24370 glyph->descent = it->descent;
24371 glyph->voffset = it->voffset;
24372 glyph->type = IMAGE_GLYPH;
24373 glyph->avoid_cursor_p = it->avoid_cursor_p;
24374 glyph->multibyte_p = it->multibyte_p;
24375 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24377 /* In R2L rows, the left and the right box edges need to be
24378 drawn in reverse direction. */
24379 glyph->right_box_line_p = it->start_of_box_run_p;
24380 glyph->left_box_line_p = it->end_of_box_run_p;
24382 else
24384 glyph->left_box_line_p = it->start_of_box_run_p;
24385 glyph->right_box_line_p = it->end_of_box_run_p;
24387 glyph->overlaps_vertically_p = 0;
24388 glyph->padding_p = 0;
24389 glyph->glyph_not_available_p = 0;
24390 glyph->face_id = it->face_id;
24391 glyph->u.img_id = img->id;
24392 glyph->slice.img = slice;
24393 glyph->font_type = FONT_TYPE_UNKNOWN;
24394 if (it->bidi_p)
24396 glyph->resolved_level = it->bidi_it.resolved_level;
24397 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24398 emacs_abort ();
24399 glyph->bidi_type = it->bidi_it.type;
24401 ++it->glyph_row->used[area];
24403 else
24404 IT_EXPAND_MATRIX_WIDTH (it, area);
24409 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24410 of the glyph, WIDTH and HEIGHT are the width and height of the
24411 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24413 static void
24414 append_stretch_glyph (struct it *it, Lisp_Object object,
24415 int width, int height, int ascent)
24417 struct glyph *glyph;
24418 enum glyph_row_area area = it->area;
24420 eassert (ascent >= 0 && ascent <= height);
24422 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24423 if (glyph < it->glyph_row->glyphs[area + 1])
24425 /* If the glyph row is reversed, we need to prepend the glyph
24426 rather than append it. */
24427 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24429 struct glyph *g;
24431 /* Make room for the additional glyph. */
24432 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24433 g[1] = *g;
24434 glyph = it->glyph_row->glyphs[area];
24436 glyph->charpos = CHARPOS (it->position);
24437 glyph->object = object;
24438 glyph->pixel_width = width;
24439 glyph->ascent = ascent;
24440 glyph->descent = height - ascent;
24441 glyph->voffset = it->voffset;
24442 glyph->type = STRETCH_GLYPH;
24443 glyph->avoid_cursor_p = it->avoid_cursor_p;
24444 glyph->multibyte_p = it->multibyte_p;
24445 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24447 /* In R2L rows, the left and the right box edges need to be
24448 drawn in reverse direction. */
24449 glyph->right_box_line_p = it->start_of_box_run_p;
24450 glyph->left_box_line_p = it->end_of_box_run_p;
24452 else
24454 glyph->left_box_line_p = it->start_of_box_run_p;
24455 glyph->right_box_line_p = it->end_of_box_run_p;
24457 glyph->overlaps_vertically_p = 0;
24458 glyph->padding_p = 0;
24459 glyph->glyph_not_available_p = 0;
24460 glyph->face_id = it->face_id;
24461 glyph->u.stretch.ascent = ascent;
24462 glyph->u.stretch.height = height;
24463 glyph->slice.img = null_glyph_slice;
24464 glyph->font_type = FONT_TYPE_UNKNOWN;
24465 if (it->bidi_p)
24467 glyph->resolved_level = it->bidi_it.resolved_level;
24468 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24469 emacs_abort ();
24470 glyph->bidi_type = it->bidi_it.type;
24472 else
24474 glyph->resolved_level = 0;
24475 glyph->bidi_type = UNKNOWN_BT;
24477 ++it->glyph_row->used[area];
24479 else
24480 IT_EXPAND_MATRIX_WIDTH (it, area);
24483 #endif /* HAVE_WINDOW_SYSTEM */
24485 /* Produce a stretch glyph for iterator IT. IT->object is the value
24486 of the glyph property displayed. The value must be a list
24487 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24488 being recognized:
24490 1. `:width WIDTH' specifies that the space should be WIDTH *
24491 canonical char width wide. WIDTH may be an integer or floating
24492 point number.
24494 2. `:relative-width FACTOR' specifies that the width of the stretch
24495 should be computed from the width of the first character having the
24496 `glyph' property, and should be FACTOR times that width.
24498 3. `:align-to HPOS' specifies that the space should be wide enough
24499 to reach HPOS, a value in canonical character units.
24501 Exactly one of the above pairs must be present.
24503 4. `:height HEIGHT' specifies that the height of the stretch produced
24504 should be HEIGHT, measured in canonical character units.
24506 5. `:relative-height FACTOR' specifies that the height of the
24507 stretch should be FACTOR times the height of the characters having
24508 the glyph property.
24510 Either none or exactly one of 4 or 5 must be present.
24512 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24513 of the stretch should be used for the ascent of the stretch.
24514 ASCENT must be in the range 0 <= ASCENT <= 100. */
24516 void
24517 produce_stretch_glyph (struct it *it)
24519 /* (space :width WIDTH :height HEIGHT ...) */
24520 Lisp_Object prop, plist;
24521 int width = 0, height = 0, align_to = -1;
24522 int zero_width_ok_p = 0;
24523 double tem;
24524 struct font *font = NULL;
24526 #ifdef HAVE_WINDOW_SYSTEM
24527 int ascent = 0;
24528 int zero_height_ok_p = 0;
24530 if (FRAME_WINDOW_P (it->f))
24532 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24533 font = face->font ? face->font : FRAME_FONT (it->f);
24534 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24536 #endif
24538 /* List should start with `space'. */
24539 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24540 plist = XCDR (it->object);
24542 /* Compute the width of the stretch. */
24543 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24544 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24546 /* Absolute width `:width WIDTH' specified and valid. */
24547 zero_width_ok_p = 1;
24548 width = (int)tem;
24550 #ifdef HAVE_WINDOW_SYSTEM
24551 else if (FRAME_WINDOW_P (it->f)
24552 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24554 /* Relative width `:relative-width FACTOR' specified and valid.
24555 Compute the width of the characters having the `glyph'
24556 property. */
24557 struct it it2;
24558 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24560 it2 = *it;
24561 if (it->multibyte_p)
24562 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24563 else
24565 it2.c = it2.char_to_display = *p, it2.len = 1;
24566 if (! ASCII_CHAR_P (it2.c))
24567 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24570 it2.glyph_row = NULL;
24571 it2.what = IT_CHARACTER;
24572 x_produce_glyphs (&it2);
24573 width = NUMVAL (prop) * it2.pixel_width;
24575 #endif /* HAVE_WINDOW_SYSTEM */
24576 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24577 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24579 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24580 align_to = (align_to < 0
24582 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24583 else if (align_to < 0)
24584 align_to = window_box_left_offset (it->w, TEXT_AREA);
24585 width = max (0, (int)tem + align_to - it->current_x);
24586 zero_width_ok_p = 1;
24588 else
24589 /* Nothing specified -> width defaults to canonical char width. */
24590 width = FRAME_COLUMN_WIDTH (it->f);
24592 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24593 width = 1;
24595 #ifdef HAVE_WINDOW_SYSTEM
24596 /* Compute height. */
24597 if (FRAME_WINDOW_P (it->f))
24599 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24600 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24602 height = (int)tem;
24603 zero_height_ok_p = 1;
24605 else if (prop = Fplist_get (plist, QCrelative_height),
24606 NUMVAL (prop) > 0)
24607 height = FONT_HEIGHT (font) * NUMVAL (prop);
24608 else
24609 height = FONT_HEIGHT (font);
24611 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24612 height = 1;
24614 /* Compute percentage of height used for ascent. If
24615 `:ascent ASCENT' is present and valid, use that. Otherwise,
24616 derive the ascent from the font in use. */
24617 if (prop = Fplist_get (plist, QCascent),
24618 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24619 ascent = height * NUMVAL (prop) / 100.0;
24620 else if (!NILP (prop)
24621 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24622 ascent = min (max (0, (int)tem), height);
24623 else
24624 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24626 else
24627 #endif /* HAVE_WINDOW_SYSTEM */
24628 height = 1;
24630 if (width > 0 && it->line_wrap != TRUNCATE
24631 && it->current_x + width > it->last_visible_x)
24633 width = it->last_visible_x - it->current_x;
24634 #ifdef HAVE_WINDOW_SYSTEM
24635 /* Subtract one more pixel from the stretch width, but only on
24636 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24637 width -= FRAME_WINDOW_P (it->f);
24638 #endif
24641 if (width > 0 && height > 0 && it->glyph_row)
24643 Lisp_Object o_object = it->object;
24644 Lisp_Object object = it->stack[it->sp - 1].string;
24645 int n = width;
24647 if (!STRINGP (object))
24648 object = it->w->contents;
24649 #ifdef HAVE_WINDOW_SYSTEM
24650 if (FRAME_WINDOW_P (it->f))
24651 append_stretch_glyph (it, object, width, height, ascent);
24652 else
24653 #endif
24655 it->object = object;
24656 it->char_to_display = ' ';
24657 it->pixel_width = it->len = 1;
24658 while (n--)
24659 tty_append_glyph (it);
24660 it->object = o_object;
24664 it->pixel_width = width;
24665 #ifdef HAVE_WINDOW_SYSTEM
24666 if (FRAME_WINDOW_P (it->f))
24668 it->ascent = it->phys_ascent = ascent;
24669 it->descent = it->phys_descent = height - it->ascent;
24670 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24671 take_vertical_position_into_account (it);
24673 else
24674 #endif
24675 it->nglyphs = width;
24678 /* Get information about special display element WHAT in an
24679 environment described by IT. WHAT is one of IT_TRUNCATION or
24680 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24681 non-null glyph_row member. This function ensures that fields like
24682 face_id, c, len of IT are left untouched. */
24684 static void
24685 produce_special_glyphs (struct it *it, enum display_element_type what)
24687 struct it temp_it;
24688 Lisp_Object gc;
24689 GLYPH glyph;
24691 temp_it = *it;
24692 temp_it.object = make_number (0);
24693 memset (&temp_it.current, 0, sizeof temp_it.current);
24695 if (what == IT_CONTINUATION)
24697 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24698 if (it->bidi_it.paragraph_dir == R2L)
24699 SET_GLYPH_FROM_CHAR (glyph, '/');
24700 else
24701 SET_GLYPH_FROM_CHAR (glyph, '\\');
24702 if (it->dp
24703 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24705 /* FIXME: Should we mirror GC for R2L lines? */
24706 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24707 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24710 else if (what == IT_TRUNCATION)
24712 /* Truncation glyph. */
24713 SET_GLYPH_FROM_CHAR (glyph, '$');
24714 if (it->dp
24715 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24717 /* FIXME: Should we mirror GC for R2L lines? */
24718 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24719 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24722 else
24723 emacs_abort ();
24725 #ifdef HAVE_WINDOW_SYSTEM
24726 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24727 is turned off, we precede the truncation/continuation glyphs by a
24728 stretch glyph whose width is computed such that these special
24729 glyphs are aligned at the window margin, even when very different
24730 fonts are used in different glyph rows. */
24731 if (FRAME_WINDOW_P (temp_it.f)
24732 /* init_iterator calls this with it->glyph_row == NULL, and it
24733 wants only the pixel width of the truncation/continuation
24734 glyphs. */
24735 && temp_it.glyph_row
24736 /* insert_left_trunc_glyphs calls us at the beginning of the
24737 row, and it has its own calculation of the stretch glyph
24738 width. */
24739 && temp_it.glyph_row->used[TEXT_AREA] > 0
24740 && (temp_it.glyph_row->reversed_p
24741 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24742 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24744 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24746 if (stretch_width > 0)
24748 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24749 struct font *font =
24750 face->font ? face->font : FRAME_FONT (temp_it.f);
24751 int stretch_ascent =
24752 (((temp_it.ascent + temp_it.descent)
24753 * FONT_BASE (font)) / FONT_HEIGHT (font));
24755 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24756 temp_it.ascent + temp_it.descent,
24757 stretch_ascent);
24760 #endif
24762 temp_it.dp = NULL;
24763 temp_it.what = IT_CHARACTER;
24764 temp_it.len = 1;
24765 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24766 temp_it.face_id = GLYPH_FACE (glyph);
24767 temp_it.len = CHAR_BYTES (temp_it.c);
24769 PRODUCE_GLYPHS (&temp_it);
24770 it->pixel_width = temp_it.pixel_width;
24771 it->nglyphs = temp_it.pixel_width;
24774 #ifdef HAVE_WINDOW_SYSTEM
24776 /* Calculate line-height and line-spacing properties.
24777 An integer value specifies explicit pixel value.
24778 A float value specifies relative value to current face height.
24779 A cons (float . face-name) specifies relative value to
24780 height of specified face font.
24782 Returns height in pixels, or nil. */
24785 static Lisp_Object
24786 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24787 int boff, int override)
24789 Lisp_Object face_name = Qnil;
24790 int ascent, descent, height;
24792 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24793 return val;
24795 if (CONSP (val))
24797 face_name = XCAR (val);
24798 val = XCDR (val);
24799 if (!NUMBERP (val))
24800 val = make_number (1);
24801 if (NILP (face_name))
24803 height = it->ascent + it->descent;
24804 goto scale;
24808 if (NILP (face_name))
24810 font = FRAME_FONT (it->f);
24811 boff = FRAME_BASELINE_OFFSET (it->f);
24813 else if (EQ (face_name, Qt))
24815 override = 0;
24817 else
24819 int face_id;
24820 struct face *face;
24822 face_id = lookup_named_face (it->f, face_name, 0);
24823 if (face_id < 0)
24824 return make_number (-1);
24826 face = FACE_FROM_ID (it->f, face_id);
24827 font = face->font;
24828 if (font == NULL)
24829 return make_number (-1);
24830 boff = font->baseline_offset;
24831 if (font->vertical_centering)
24832 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24835 ascent = FONT_BASE (font) + boff;
24836 descent = FONT_DESCENT (font) - boff;
24838 if (override)
24840 it->override_ascent = ascent;
24841 it->override_descent = descent;
24842 it->override_boff = boff;
24845 height = ascent + descent;
24847 scale:
24848 if (FLOATP (val))
24849 height = (int)(XFLOAT_DATA (val) * height);
24850 else if (INTEGERP (val))
24851 height *= XINT (val);
24853 return make_number (height);
24857 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24858 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24859 and only if this is for a character for which no font was found.
24861 If the display method (it->glyphless_method) is
24862 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24863 length of the acronym or the hexadecimal string, UPPER_XOFF and
24864 UPPER_YOFF are pixel offsets for the upper part of the string,
24865 LOWER_XOFF and LOWER_YOFF are for the lower part.
24867 For the other display methods, LEN through LOWER_YOFF are zero. */
24869 static void
24870 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24871 short upper_xoff, short upper_yoff,
24872 short lower_xoff, short lower_yoff)
24874 struct glyph *glyph;
24875 enum glyph_row_area area = it->area;
24877 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24878 if (glyph < it->glyph_row->glyphs[area + 1])
24880 /* If the glyph row is reversed, we need to prepend the glyph
24881 rather than append it. */
24882 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24884 struct glyph *g;
24886 /* Make room for the additional glyph. */
24887 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24888 g[1] = *g;
24889 glyph = it->glyph_row->glyphs[area];
24891 glyph->charpos = CHARPOS (it->position);
24892 glyph->object = it->object;
24893 glyph->pixel_width = it->pixel_width;
24894 glyph->ascent = it->ascent;
24895 glyph->descent = it->descent;
24896 glyph->voffset = it->voffset;
24897 glyph->type = GLYPHLESS_GLYPH;
24898 glyph->u.glyphless.method = it->glyphless_method;
24899 glyph->u.glyphless.for_no_font = for_no_font;
24900 glyph->u.glyphless.len = len;
24901 glyph->u.glyphless.ch = it->c;
24902 glyph->slice.glyphless.upper_xoff = upper_xoff;
24903 glyph->slice.glyphless.upper_yoff = upper_yoff;
24904 glyph->slice.glyphless.lower_xoff = lower_xoff;
24905 glyph->slice.glyphless.lower_yoff = lower_yoff;
24906 glyph->avoid_cursor_p = it->avoid_cursor_p;
24907 glyph->multibyte_p = it->multibyte_p;
24908 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24910 /* In R2L rows, the left and the right box edges need to be
24911 drawn in reverse direction. */
24912 glyph->right_box_line_p = it->start_of_box_run_p;
24913 glyph->left_box_line_p = it->end_of_box_run_p;
24915 else
24917 glyph->left_box_line_p = it->start_of_box_run_p;
24918 glyph->right_box_line_p = it->end_of_box_run_p;
24920 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24921 || it->phys_descent > it->descent);
24922 glyph->padding_p = 0;
24923 glyph->glyph_not_available_p = 0;
24924 glyph->face_id = face_id;
24925 glyph->font_type = FONT_TYPE_UNKNOWN;
24926 if (it->bidi_p)
24928 glyph->resolved_level = it->bidi_it.resolved_level;
24929 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24930 emacs_abort ();
24931 glyph->bidi_type = it->bidi_it.type;
24933 ++it->glyph_row->used[area];
24935 else
24936 IT_EXPAND_MATRIX_WIDTH (it, area);
24940 /* Produce a glyph for a glyphless character for iterator IT.
24941 IT->glyphless_method specifies which method to use for displaying
24942 the character. See the description of enum
24943 glyphless_display_method in dispextern.h for the detail.
24945 FOR_NO_FONT is nonzero if and only if this is for a character for
24946 which no font was found. ACRONYM, if non-nil, is an acronym string
24947 for the character. */
24949 static void
24950 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24952 int face_id;
24953 struct face *face;
24954 struct font *font;
24955 int base_width, base_height, width, height;
24956 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24957 int len;
24959 /* Get the metrics of the base font. We always refer to the current
24960 ASCII face. */
24961 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24962 font = face->font ? face->font : FRAME_FONT (it->f);
24963 it->ascent = FONT_BASE (font) + font->baseline_offset;
24964 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24965 base_height = it->ascent + it->descent;
24966 base_width = font->average_width;
24968 /* Get a face ID for the glyph by utilizing a cache (the same way as
24969 done for `escape-glyph' in get_next_display_element). */
24970 if (it->f == last_glyphless_glyph_frame
24971 && it->face_id == last_glyphless_glyph_face_id)
24973 face_id = last_glyphless_glyph_merged_face_id;
24975 else
24977 /* Merge the `glyphless-char' face into the current face. */
24978 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24979 last_glyphless_glyph_frame = it->f;
24980 last_glyphless_glyph_face_id = it->face_id;
24981 last_glyphless_glyph_merged_face_id = face_id;
24984 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24986 it->pixel_width = THIN_SPACE_WIDTH;
24987 len = 0;
24988 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24990 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24992 width = CHAR_WIDTH (it->c);
24993 if (width == 0)
24994 width = 1;
24995 else if (width > 4)
24996 width = 4;
24997 it->pixel_width = base_width * width;
24998 len = 0;
24999 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25001 else
25003 char buf[7];
25004 const char *str;
25005 unsigned int code[6];
25006 int upper_len;
25007 int ascent, descent;
25008 struct font_metrics metrics_upper, metrics_lower;
25010 face = FACE_FROM_ID (it->f, face_id);
25011 font = face->font ? face->font : FRAME_FONT (it->f);
25012 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25014 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25016 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25017 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25018 if (CONSP (acronym))
25019 acronym = XCAR (acronym);
25020 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25022 else
25024 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25025 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25026 str = buf;
25028 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25029 code[len] = font->driver->encode_char (font, str[len]);
25030 upper_len = (len + 1) / 2;
25031 font->driver->text_extents (font, code, upper_len,
25032 &metrics_upper);
25033 font->driver->text_extents (font, code + upper_len, len - upper_len,
25034 &metrics_lower);
25038 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25039 width = max (metrics_upper.width, metrics_lower.width) + 4;
25040 upper_xoff = upper_yoff = 2; /* the typical case */
25041 if (base_width >= width)
25043 /* Align the upper to the left, the lower to the right. */
25044 it->pixel_width = base_width;
25045 lower_xoff = base_width - 2 - metrics_lower.width;
25047 else
25049 /* Center the shorter one. */
25050 it->pixel_width = width;
25051 if (metrics_upper.width >= metrics_lower.width)
25052 lower_xoff = (width - metrics_lower.width) / 2;
25053 else
25055 /* FIXME: This code doesn't look right. It formerly was
25056 missing the "lower_xoff = 0;", which couldn't have
25057 been right since it left lower_xoff uninitialized. */
25058 lower_xoff = 0;
25059 upper_xoff = (width - metrics_upper.width) / 2;
25063 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25064 top, bottom, and between upper and lower strings. */
25065 height = (metrics_upper.ascent + metrics_upper.descent
25066 + metrics_lower.ascent + metrics_lower.descent) + 5;
25067 /* Center vertically.
25068 H:base_height, D:base_descent
25069 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25071 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25072 descent = D - H/2 + h/2;
25073 lower_yoff = descent - 2 - ld;
25074 upper_yoff = lower_yoff - la - 1 - ud; */
25075 ascent = - (it->descent - (base_height + height + 1) / 2);
25076 descent = it->descent - (base_height - height) / 2;
25077 lower_yoff = descent - 2 - metrics_lower.descent;
25078 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25079 - metrics_upper.descent);
25080 /* Don't make the height shorter than the base height. */
25081 if (height > base_height)
25083 it->ascent = ascent;
25084 it->descent = descent;
25088 it->phys_ascent = it->ascent;
25089 it->phys_descent = it->descent;
25090 if (it->glyph_row)
25091 append_glyphless_glyph (it, face_id, for_no_font, len,
25092 upper_xoff, upper_yoff,
25093 lower_xoff, lower_yoff);
25094 it->nglyphs = 1;
25095 take_vertical_position_into_account (it);
25099 /* RIF:
25100 Produce glyphs/get display metrics for the display element IT is
25101 loaded with. See the description of struct it in dispextern.h
25102 for an overview of struct it. */
25104 void
25105 x_produce_glyphs (struct it *it)
25107 int extra_line_spacing = it->extra_line_spacing;
25109 it->glyph_not_available_p = 0;
25111 if (it->what == IT_CHARACTER)
25113 XChar2b char2b;
25114 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25115 struct font *font = face->font;
25116 struct font_metrics *pcm = NULL;
25117 int boff; /* baseline offset */
25119 if (font == NULL)
25121 /* When no suitable font is found, display this character by
25122 the method specified in the first extra slot of
25123 Vglyphless_char_display. */
25124 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25126 eassert (it->what == IT_GLYPHLESS);
25127 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25128 goto done;
25131 boff = font->baseline_offset;
25132 if (font->vertical_centering)
25133 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25135 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25137 int stretched_p;
25139 it->nglyphs = 1;
25141 if (it->override_ascent >= 0)
25143 it->ascent = it->override_ascent;
25144 it->descent = it->override_descent;
25145 boff = it->override_boff;
25147 else
25149 it->ascent = FONT_BASE (font) + boff;
25150 it->descent = FONT_DESCENT (font) - boff;
25153 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25155 pcm = get_per_char_metric (font, &char2b);
25156 if (pcm->width == 0
25157 && pcm->rbearing == 0 && pcm->lbearing == 0)
25158 pcm = NULL;
25161 if (pcm)
25163 it->phys_ascent = pcm->ascent + boff;
25164 it->phys_descent = pcm->descent - boff;
25165 it->pixel_width = pcm->width;
25167 else
25169 it->glyph_not_available_p = 1;
25170 it->phys_ascent = it->ascent;
25171 it->phys_descent = it->descent;
25172 it->pixel_width = font->space_width;
25175 if (it->constrain_row_ascent_descent_p)
25177 if (it->descent > it->max_descent)
25179 it->ascent += it->descent - it->max_descent;
25180 it->descent = it->max_descent;
25182 if (it->ascent > it->max_ascent)
25184 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25185 it->ascent = it->max_ascent;
25187 it->phys_ascent = min (it->phys_ascent, it->ascent);
25188 it->phys_descent = min (it->phys_descent, it->descent);
25189 extra_line_spacing = 0;
25192 /* If this is a space inside a region of text with
25193 `space-width' property, change its width. */
25194 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25195 if (stretched_p)
25196 it->pixel_width *= XFLOATINT (it->space_width);
25198 /* If face has a box, add the box thickness to the character
25199 height. If character has a box line to the left and/or
25200 right, add the box line width to the character's width. */
25201 if (face->box != FACE_NO_BOX)
25203 int thick = face->box_line_width;
25205 if (thick > 0)
25207 it->ascent += thick;
25208 it->descent += thick;
25210 else
25211 thick = -thick;
25213 if (it->start_of_box_run_p)
25214 it->pixel_width += thick;
25215 if (it->end_of_box_run_p)
25216 it->pixel_width += thick;
25219 /* If face has an overline, add the height of the overline
25220 (1 pixel) and a 1 pixel margin to the character height. */
25221 if (face->overline_p)
25222 it->ascent += overline_margin;
25224 if (it->constrain_row_ascent_descent_p)
25226 if (it->ascent > it->max_ascent)
25227 it->ascent = it->max_ascent;
25228 if (it->descent > it->max_descent)
25229 it->descent = it->max_descent;
25232 take_vertical_position_into_account (it);
25234 /* If we have to actually produce glyphs, do it. */
25235 if (it->glyph_row)
25237 if (stretched_p)
25239 /* Translate a space with a `space-width' property
25240 into a stretch glyph. */
25241 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25242 / FONT_HEIGHT (font));
25243 append_stretch_glyph (it, it->object, it->pixel_width,
25244 it->ascent + it->descent, ascent);
25246 else
25247 append_glyph (it);
25249 /* If characters with lbearing or rbearing are displayed
25250 in this line, record that fact in a flag of the
25251 glyph row. This is used to optimize X output code. */
25252 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25253 it->glyph_row->contains_overlapping_glyphs_p = 1;
25255 if (! stretched_p && it->pixel_width == 0)
25256 /* We assure that all visible glyphs have at least 1-pixel
25257 width. */
25258 it->pixel_width = 1;
25260 else if (it->char_to_display == '\n')
25262 /* A newline has no width, but we need the height of the
25263 line. But if previous part of the line sets a height,
25264 don't increase that height */
25266 Lisp_Object height;
25267 Lisp_Object total_height = Qnil;
25269 it->override_ascent = -1;
25270 it->pixel_width = 0;
25271 it->nglyphs = 0;
25273 height = get_it_property (it, Qline_height);
25274 /* Split (line-height total-height) list */
25275 if (CONSP (height)
25276 && CONSP (XCDR (height))
25277 && NILP (XCDR (XCDR (height))))
25279 total_height = XCAR (XCDR (height));
25280 height = XCAR (height);
25282 height = calc_line_height_property (it, height, font, boff, 1);
25284 if (it->override_ascent >= 0)
25286 it->ascent = it->override_ascent;
25287 it->descent = it->override_descent;
25288 boff = it->override_boff;
25290 else
25292 it->ascent = FONT_BASE (font) + boff;
25293 it->descent = FONT_DESCENT (font) - boff;
25296 if (EQ (height, Qt))
25298 if (it->descent > it->max_descent)
25300 it->ascent += it->descent - it->max_descent;
25301 it->descent = it->max_descent;
25303 if (it->ascent > it->max_ascent)
25305 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25306 it->ascent = it->max_ascent;
25308 it->phys_ascent = min (it->phys_ascent, it->ascent);
25309 it->phys_descent = min (it->phys_descent, it->descent);
25310 it->constrain_row_ascent_descent_p = 1;
25311 extra_line_spacing = 0;
25313 else
25315 Lisp_Object spacing;
25317 it->phys_ascent = it->ascent;
25318 it->phys_descent = it->descent;
25320 if ((it->max_ascent > 0 || it->max_descent > 0)
25321 && face->box != FACE_NO_BOX
25322 && face->box_line_width > 0)
25324 it->ascent += face->box_line_width;
25325 it->descent += face->box_line_width;
25327 if (!NILP (height)
25328 && XINT (height) > it->ascent + it->descent)
25329 it->ascent = XINT (height) - it->descent;
25331 if (!NILP (total_height))
25332 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25333 else
25335 spacing = get_it_property (it, Qline_spacing);
25336 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25338 if (INTEGERP (spacing))
25340 extra_line_spacing = XINT (spacing);
25341 if (!NILP (total_height))
25342 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25346 else /* i.e. (it->char_to_display == '\t') */
25348 if (font->space_width > 0)
25350 int tab_width = it->tab_width * font->space_width;
25351 int x = it->current_x + it->continuation_lines_width;
25352 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25354 /* If the distance from the current position to the next tab
25355 stop is less than a space character width, use the
25356 tab stop after that. */
25357 if (next_tab_x - x < font->space_width)
25358 next_tab_x += tab_width;
25360 it->pixel_width = next_tab_x - x;
25361 it->nglyphs = 1;
25362 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25363 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25365 if (it->glyph_row)
25367 append_stretch_glyph (it, it->object, it->pixel_width,
25368 it->ascent + it->descent, it->ascent);
25371 else
25373 it->pixel_width = 0;
25374 it->nglyphs = 1;
25378 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25380 /* A static composition.
25382 Note: A composition is represented as one glyph in the
25383 glyph matrix. There are no padding glyphs.
25385 Important note: pixel_width, ascent, and descent are the
25386 values of what is drawn by draw_glyphs (i.e. the values of
25387 the overall glyphs composed). */
25388 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25389 int boff; /* baseline offset */
25390 struct composition *cmp = composition_table[it->cmp_it.id];
25391 int glyph_len = cmp->glyph_len;
25392 struct font *font = face->font;
25394 it->nglyphs = 1;
25396 /* If we have not yet calculated pixel size data of glyphs of
25397 the composition for the current face font, calculate them
25398 now. Theoretically, we have to check all fonts for the
25399 glyphs, but that requires much time and memory space. So,
25400 here we check only the font of the first glyph. This may
25401 lead to incorrect display, but it's very rare, and C-l
25402 (recenter-top-bottom) can correct the display anyway. */
25403 if (! cmp->font || cmp->font != font)
25405 /* Ascent and descent of the font of the first character
25406 of this composition (adjusted by baseline offset).
25407 Ascent and descent of overall glyphs should not be less
25408 than these, respectively. */
25409 int font_ascent, font_descent, font_height;
25410 /* Bounding box of the overall glyphs. */
25411 int leftmost, rightmost, lowest, highest;
25412 int lbearing, rbearing;
25413 int i, width, ascent, descent;
25414 int left_padded = 0, right_padded = 0;
25415 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25416 XChar2b char2b;
25417 struct font_metrics *pcm;
25418 int font_not_found_p;
25419 ptrdiff_t pos;
25421 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25422 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25423 break;
25424 if (glyph_len < cmp->glyph_len)
25425 right_padded = 1;
25426 for (i = 0; i < glyph_len; i++)
25428 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25429 break;
25430 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25432 if (i > 0)
25433 left_padded = 1;
25435 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25436 : IT_CHARPOS (*it));
25437 /* If no suitable font is found, use the default font. */
25438 font_not_found_p = font == NULL;
25439 if (font_not_found_p)
25441 face = face->ascii_face;
25442 font = face->font;
25444 boff = font->baseline_offset;
25445 if (font->vertical_centering)
25446 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25447 font_ascent = FONT_BASE (font) + boff;
25448 font_descent = FONT_DESCENT (font) - boff;
25449 font_height = FONT_HEIGHT (font);
25451 cmp->font = font;
25453 pcm = NULL;
25454 if (! font_not_found_p)
25456 get_char_face_and_encoding (it->f, c, it->face_id,
25457 &char2b, 0);
25458 pcm = get_per_char_metric (font, &char2b);
25461 /* Initialize the bounding box. */
25462 if (pcm)
25464 width = cmp->glyph_len > 0 ? pcm->width : 0;
25465 ascent = pcm->ascent;
25466 descent = pcm->descent;
25467 lbearing = pcm->lbearing;
25468 rbearing = pcm->rbearing;
25470 else
25472 width = cmp->glyph_len > 0 ? font->space_width : 0;
25473 ascent = FONT_BASE (font);
25474 descent = FONT_DESCENT (font);
25475 lbearing = 0;
25476 rbearing = width;
25479 rightmost = width;
25480 leftmost = 0;
25481 lowest = - descent + boff;
25482 highest = ascent + boff;
25484 if (! font_not_found_p
25485 && font->default_ascent
25486 && CHAR_TABLE_P (Vuse_default_ascent)
25487 && !NILP (Faref (Vuse_default_ascent,
25488 make_number (it->char_to_display))))
25489 highest = font->default_ascent + boff;
25491 /* Draw the first glyph at the normal position. It may be
25492 shifted to right later if some other glyphs are drawn
25493 at the left. */
25494 cmp->offsets[i * 2] = 0;
25495 cmp->offsets[i * 2 + 1] = boff;
25496 cmp->lbearing = lbearing;
25497 cmp->rbearing = rbearing;
25499 /* Set cmp->offsets for the remaining glyphs. */
25500 for (i++; i < glyph_len; i++)
25502 int left, right, btm, top;
25503 int ch = COMPOSITION_GLYPH (cmp, i);
25504 int face_id;
25505 struct face *this_face;
25507 if (ch == '\t')
25508 ch = ' ';
25509 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25510 this_face = FACE_FROM_ID (it->f, face_id);
25511 font = this_face->font;
25513 if (font == NULL)
25514 pcm = NULL;
25515 else
25517 get_char_face_and_encoding (it->f, ch, face_id,
25518 &char2b, 0);
25519 pcm = get_per_char_metric (font, &char2b);
25521 if (! pcm)
25522 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25523 else
25525 width = pcm->width;
25526 ascent = pcm->ascent;
25527 descent = pcm->descent;
25528 lbearing = pcm->lbearing;
25529 rbearing = pcm->rbearing;
25530 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25532 /* Relative composition with or without
25533 alternate chars. */
25534 left = (leftmost + rightmost - width) / 2;
25535 btm = - descent + boff;
25536 if (font->relative_compose
25537 && (! CHAR_TABLE_P (Vignore_relative_composition)
25538 || NILP (Faref (Vignore_relative_composition,
25539 make_number (ch)))))
25542 if (- descent >= font->relative_compose)
25543 /* One extra pixel between two glyphs. */
25544 btm = highest + 1;
25545 else if (ascent <= 0)
25546 /* One extra pixel between two glyphs. */
25547 btm = lowest - 1 - ascent - descent;
25550 else
25552 /* A composition rule is specified by an integer
25553 value that encodes global and new reference
25554 points (GREF and NREF). GREF and NREF are
25555 specified by numbers as below:
25557 0---1---2 -- ascent
25561 9--10--11 -- center
25563 ---3---4---5--- baseline
25565 6---7---8 -- descent
25567 int rule = COMPOSITION_RULE (cmp, i);
25568 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25570 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25571 grefx = gref % 3, nrefx = nref % 3;
25572 grefy = gref / 3, nrefy = nref / 3;
25573 if (xoff)
25574 xoff = font_height * (xoff - 128) / 256;
25575 if (yoff)
25576 yoff = font_height * (yoff - 128) / 256;
25578 left = (leftmost
25579 + grefx * (rightmost - leftmost) / 2
25580 - nrefx * width / 2
25581 + xoff);
25583 btm = ((grefy == 0 ? highest
25584 : grefy == 1 ? 0
25585 : grefy == 2 ? lowest
25586 : (highest + lowest) / 2)
25587 - (nrefy == 0 ? ascent + descent
25588 : nrefy == 1 ? descent - boff
25589 : nrefy == 2 ? 0
25590 : (ascent + descent) / 2)
25591 + yoff);
25594 cmp->offsets[i * 2] = left;
25595 cmp->offsets[i * 2 + 1] = btm + descent;
25597 /* Update the bounding box of the overall glyphs. */
25598 if (width > 0)
25600 right = left + width;
25601 if (left < leftmost)
25602 leftmost = left;
25603 if (right > rightmost)
25604 rightmost = right;
25606 top = btm + descent + ascent;
25607 if (top > highest)
25608 highest = top;
25609 if (btm < lowest)
25610 lowest = btm;
25612 if (cmp->lbearing > left + lbearing)
25613 cmp->lbearing = left + lbearing;
25614 if (cmp->rbearing < left + rbearing)
25615 cmp->rbearing = left + rbearing;
25619 /* If there are glyphs whose x-offsets are negative,
25620 shift all glyphs to the right and make all x-offsets
25621 non-negative. */
25622 if (leftmost < 0)
25624 for (i = 0; i < cmp->glyph_len; i++)
25625 cmp->offsets[i * 2] -= leftmost;
25626 rightmost -= leftmost;
25627 cmp->lbearing -= leftmost;
25628 cmp->rbearing -= leftmost;
25631 if (left_padded && cmp->lbearing < 0)
25633 for (i = 0; i < cmp->glyph_len; i++)
25634 cmp->offsets[i * 2] -= cmp->lbearing;
25635 rightmost -= cmp->lbearing;
25636 cmp->rbearing -= cmp->lbearing;
25637 cmp->lbearing = 0;
25639 if (right_padded && rightmost < cmp->rbearing)
25641 rightmost = cmp->rbearing;
25644 cmp->pixel_width = rightmost;
25645 cmp->ascent = highest;
25646 cmp->descent = - lowest;
25647 if (cmp->ascent < font_ascent)
25648 cmp->ascent = font_ascent;
25649 if (cmp->descent < font_descent)
25650 cmp->descent = font_descent;
25653 if (it->glyph_row
25654 && (cmp->lbearing < 0
25655 || cmp->rbearing > cmp->pixel_width))
25656 it->glyph_row->contains_overlapping_glyphs_p = 1;
25658 it->pixel_width = cmp->pixel_width;
25659 it->ascent = it->phys_ascent = cmp->ascent;
25660 it->descent = it->phys_descent = cmp->descent;
25661 if (face->box != FACE_NO_BOX)
25663 int thick = face->box_line_width;
25665 if (thick > 0)
25667 it->ascent += thick;
25668 it->descent += thick;
25670 else
25671 thick = - thick;
25673 if (it->start_of_box_run_p)
25674 it->pixel_width += thick;
25675 if (it->end_of_box_run_p)
25676 it->pixel_width += thick;
25679 /* If face has an overline, add the height of the overline
25680 (1 pixel) and a 1 pixel margin to the character height. */
25681 if (face->overline_p)
25682 it->ascent += overline_margin;
25684 take_vertical_position_into_account (it);
25685 if (it->ascent < 0)
25686 it->ascent = 0;
25687 if (it->descent < 0)
25688 it->descent = 0;
25690 if (it->glyph_row && cmp->glyph_len > 0)
25691 append_composite_glyph (it);
25693 else if (it->what == IT_COMPOSITION)
25695 /* A dynamic (automatic) composition. */
25696 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25697 Lisp_Object gstring;
25698 struct font_metrics metrics;
25700 it->nglyphs = 1;
25702 gstring = composition_gstring_from_id (it->cmp_it.id);
25703 it->pixel_width
25704 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25705 &metrics);
25706 if (it->glyph_row
25707 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25708 it->glyph_row->contains_overlapping_glyphs_p = 1;
25709 it->ascent = it->phys_ascent = metrics.ascent;
25710 it->descent = it->phys_descent = metrics.descent;
25711 if (face->box != FACE_NO_BOX)
25713 int thick = face->box_line_width;
25715 if (thick > 0)
25717 it->ascent += thick;
25718 it->descent += thick;
25720 else
25721 thick = - thick;
25723 if (it->start_of_box_run_p)
25724 it->pixel_width += thick;
25725 if (it->end_of_box_run_p)
25726 it->pixel_width += thick;
25728 /* If face has an overline, add the height of the overline
25729 (1 pixel) and a 1 pixel margin to the character height. */
25730 if (face->overline_p)
25731 it->ascent += overline_margin;
25732 take_vertical_position_into_account (it);
25733 if (it->ascent < 0)
25734 it->ascent = 0;
25735 if (it->descent < 0)
25736 it->descent = 0;
25738 if (it->glyph_row)
25739 append_composite_glyph (it);
25741 else if (it->what == IT_GLYPHLESS)
25742 produce_glyphless_glyph (it, 0, Qnil);
25743 else if (it->what == IT_IMAGE)
25744 produce_image_glyph (it);
25745 else if (it->what == IT_STRETCH)
25746 produce_stretch_glyph (it);
25748 done:
25749 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25750 because this isn't true for images with `:ascent 100'. */
25751 eassert (it->ascent >= 0 && it->descent >= 0);
25752 if (it->area == TEXT_AREA)
25753 it->current_x += it->pixel_width;
25755 if (extra_line_spacing > 0)
25757 it->descent += extra_line_spacing;
25758 if (extra_line_spacing > it->max_extra_line_spacing)
25759 it->max_extra_line_spacing = extra_line_spacing;
25762 it->max_ascent = max (it->max_ascent, it->ascent);
25763 it->max_descent = max (it->max_descent, it->descent);
25764 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25765 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25768 /* EXPORT for RIF:
25769 Output LEN glyphs starting at START at the nominal cursor position.
25770 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25771 being updated, and UPDATED_AREA is the area of that row being updated. */
25773 void
25774 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25775 struct glyph *start, enum glyph_row_area updated_area, int len)
25777 int x, hpos, chpos = w->phys_cursor.hpos;
25779 eassert (updated_row);
25780 /* When the window is hscrolled, cursor hpos can legitimately be out
25781 of bounds, but we draw the cursor at the corresponding window
25782 margin in that case. */
25783 if (!updated_row->reversed_p && chpos < 0)
25784 chpos = 0;
25785 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25786 chpos = updated_row->used[TEXT_AREA] - 1;
25788 block_input ();
25790 /* Write glyphs. */
25792 hpos = start - updated_row->glyphs[updated_area];
25793 x = draw_glyphs (w, output_cursor.x,
25794 updated_row, updated_area,
25795 hpos, hpos + len,
25796 DRAW_NORMAL_TEXT, 0);
25798 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25799 if (updated_area == TEXT_AREA
25800 && w->phys_cursor_on_p
25801 && w->phys_cursor.vpos == output_cursor.vpos
25802 && chpos >= hpos
25803 && chpos < hpos + len)
25804 w->phys_cursor_on_p = 0;
25806 unblock_input ();
25808 /* Advance the output cursor. */
25809 output_cursor.hpos += len;
25810 output_cursor.x = x;
25814 /* EXPORT for RIF:
25815 Insert LEN glyphs from START at the nominal cursor position. */
25817 void
25818 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25819 struct glyph *start, enum glyph_row_area updated_area, int len)
25821 struct frame *f;
25822 int line_height, shift_by_width, shifted_region_width;
25823 struct glyph_row *row;
25824 struct glyph *glyph;
25825 int frame_x, frame_y;
25826 ptrdiff_t hpos;
25828 eassert (updated_row);
25829 block_input ();
25830 f = XFRAME (WINDOW_FRAME (w));
25832 /* Get the height of the line we are in. */
25833 row = updated_row;
25834 line_height = row->height;
25836 /* Get the width of the glyphs to insert. */
25837 shift_by_width = 0;
25838 for (glyph = start; glyph < start + len; ++glyph)
25839 shift_by_width += glyph->pixel_width;
25841 /* Get the width of the region to shift right. */
25842 shifted_region_width = (window_box_width (w, updated_area)
25843 - output_cursor.x
25844 - shift_by_width);
25846 /* Shift right. */
25847 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25848 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25850 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25851 line_height, shift_by_width);
25853 /* Write the glyphs. */
25854 hpos = start - row->glyphs[updated_area];
25855 draw_glyphs (w, output_cursor.x, row, updated_area,
25856 hpos, hpos + len,
25857 DRAW_NORMAL_TEXT, 0);
25859 /* Advance the output cursor. */
25860 output_cursor.hpos += len;
25861 output_cursor.x += shift_by_width;
25862 unblock_input ();
25866 /* EXPORT for RIF:
25867 Erase the current text line from the nominal cursor position
25868 (inclusive) to pixel column TO_X (exclusive). The idea is that
25869 everything from TO_X onward is already erased.
25871 TO_X is a pixel position relative to UPDATED_AREA of currently
25872 updated window W. TO_X == -1 means clear to the end of this area. */
25874 void
25875 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25876 enum glyph_row_area updated_area, int to_x)
25878 struct frame *f;
25879 int max_x, min_y, max_y;
25880 int from_x, from_y, to_y;
25882 eassert (updated_row);
25883 f = XFRAME (w->frame);
25885 if (updated_row->full_width_p)
25886 max_x = WINDOW_TOTAL_WIDTH (w);
25887 else
25888 max_x = window_box_width (w, updated_area);
25889 max_y = window_text_bottom_y (w);
25891 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25892 of window. For TO_X > 0, truncate to end of drawing area. */
25893 if (to_x == 0)
25894 return;
25895 else if (to_x < 0)
25896 to_x = max_x;
25897 else
25898 to_x = min (to_x, max_x);
25900 to_y = min (max_y, output_cursor.y + updated_row->height);
25902 /* Notice if the cursor will be cleared by this operation. */
25903 if (!updated_row->full_width_p)
25904 notice_overwritten_cursor (w, updated_area,
25905 output_cursor.x, -1,
25906 updated_row->y,
25907 MATRIX_ROW_BOTTOM_Y (updated_row));
25909 from_x = output_cursor.x;
25911 /* Translate to frame coordinates. */
25912 if (updated_row->full_width_p)
25914 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25915 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25917 else
25919 int area_left = window_box_left (w, updated_area);
25920 from_x += area_left;
25921 to_x += area_left;
25924 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25925 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25926 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25928 /* Prevent inadvertently clearing to end of the X window. */
25929 if (to_x > from_x && to_y > from_y)
25931 block_input ();
25932 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25933 to_x - from_x, to_y - from_y);
25934 unblock_input ();
25938 #endif /* HAVE_WINDOW_SYSTEM */
25942 /***********************************************************************
25943 Cursor types
25944 ***********************************************************************/
25946 /* Value is the internal representation of the specified cursor type
25947 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25948 of the bar cursor. */
25950 static enum text_cursor_kinds
25951 get_specified_cursor_type (Lisp_Object arg, int *width)
25953 enum text_cursor_kinds type;
25955 if (NILP (arg))
25956 return NO_CURSOR;
25958 if (EQ (arg, Qbox))
25959 return FILLED_BOX_CURSOR;
25961 if (EQ (arg, Qhollow))
25962 return HOLLOW_BOX_CURSOR;
25964 if (EQ (arg, Qbar))
25966 *width = 2;
25967 return BAR_CURSOR;
25970 if (CONSP (arg)
25971 && EQ (XCAR (arg), Qbar)
25972 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25974 *width = XINT (XCDR (arg));
25975 return BAR_CURSOR;
25978 if (EQ (arg, Qhbar))
25980 *width = 2;
25981 return HBAR_CURSOR;
25984 if (CONSP (arg)
25985 && EQ (XCAR (arg), Qhbar)
25986 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25988 *width = XINT (XCDR (arg));
25989 return HBAR_CURSOR;
25992 /* Treat anything unknown as "hollow box cursor".
25993 It was bad to signal an error; people have trouble fixing
25994 .Xdefaults with Emacs, when it has something bad in it. */
25995 type = HOLLOW_BOX_CURSOR;
25997 return type;
26000 /* Set the default cursor types for specified frame. */
26001 void
26002 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26004 int width = 1;
26005 Lisp_Object tem;
26007 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26008 FRAME_CURSOR_WIDTH (f) = width;
26010 /* By default, set up the blink-off state depending on the on-state. */
26012 tem = Fassoc (arg, Vblink_cursor_alist);
26013 if (!NILP (tem))
26015 FRAME_BLINK_OFF_CURSOR (f)
26016 = get_specified_cursor_type (XCDR (tem), &width);
26017 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26019 else
26020 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26022 /* Make sure the cursor gets redrawn. */
26023 cursor_type_changed = 1;
26027 #ifdef HAVE_WINDOW_SYSTEM
26029 /* Return the cursor we want to be displayed in window W. Return
26030 width of bar/hbar cursor through WIDTH arg. Return with
26031 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26032 (i.e. if the `system caret' should track this cursor).
26034 In a mini-buffer window, we want the cursor only to appear if we
26035 are reading input from this window. For the selected window, we
26036 want the cursor type given by the frame parameter or buffer local
26037 setting of cursor-type. If explicitly marked off, draw no cursor.
26038 In all other cases, we want a hollow box cursor. */
26040 static enum text_cursor_kinds
26041 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26042 int *active_cursor)
26044 struct frame *f = XFRAME (w->frame);
26045 struct buffer *b = XBUFFER (w->contents);
26046 int cursor_type = DEFAULT_CURSOR;
26047 Lisp_Object alt_cursor;
26048 int non_selected = 0;
26050 *active_cursor = 1;
26052 /* Echo area */
26053 if (cursor_in_echo_area
26054 && FRAME_HAS_MINIBUF_P (f)
26055 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26057 if (w == XWINDOW (echo_area_window))
26059 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26061 *width = FRAME_CURSOR_WIDTH (f);
26062 return FRAME_DESIRED_CURSOR (f);
26064 else
26065 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26068 *active_cursor = 0;
26069 non_selected = 1;
26072 /* Detect a nonselected window or nonselected frame. */
26073 else if (w != XWINDOW (f->selected_window)
26074 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26076 *active_cursor = 0;
26078 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26079 return NO_CURSOR;
26081 non_selected = 1;
26084 /* Never display a cursor in a window in which cursor-type is nil. */
26085 if (NILP (BVAR (b, cursor_type)))
26086 return NO_CURSOR;
26088 /* Get the normal cursor type for this window. */
26089 if (EQ (BVAR (b, cursor_type), Qt))
26091 cursor_type = FRAME_DESIRED_CURSOR (f);
26092 *width = FRAME_CURSOR_WIDTH (f);
26094 else
26095 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26097 /* Use cursor-in-non-selected-windows instead
26098 for non-selected window or frame. */
26099 if (non_selected)
26101 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26102 if (!EQ (Qt, alt_cursor))
26103 return get_specified_cursor_type (alt_cursor, width);
26104 /* t means modify the normal cursor type. */
26105 if (cursor_type == FILLED_BOX_CURSOR)
26106 cursor_type = HOLLOW_BOX_CURSOR;
26107 else if (cursor_type == BAR_CURSOR && *width > 1)
26108 --*width;
26109 return cursor_type;
26112 /* Use normal cursor if not blinked off. */
26113 if (!w->cursor_off_p)
26115 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26117 if (cursor_type == FILLED_BOX_CURSOR)
26119 /* Using a block cursor on large images can be very annoying.
26120 So use a hollow cursor for "large" images.
26121 If image is not transparent (no mask), also use hollow cursor. */
26122 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26123 if (img != NULL && IMAGEP (img->spec))
26125 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26126 where N = size of default frame font size.
26127 This should cover most of the "tiny" icons people may use. */
26128 if (!img->mask
26129 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26130 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26131 cursor_type = HOLLOW_BOX_CURSOR;
26134 else if (cursor_type != NO_CURSOR)
26136 /* Display current only supports BOX and HOLLOW cursors for images.
26137 So for now, unconditionally use a HOLLOW cursor when cursor is
26138 not a solid box cursor. */
26139 cursor_type = HOLLOW_BOX_CURSOR;
26142 return cursor_type;
26145 /* Cursor is blinked off, so determine how to "toggle" it. */
26147 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26148 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26149 return get_specified_cursor_type (XCDR (alt_cursor), width);
26151 /* Then see if frame has specified a specific blink off cursor type. */
26152 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26154 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26155 return FRAME_BLINK_OFF_CURSOR (f);
26158 #if 0
26159 /* Some people liked having a permanently visible blinking cursor,
26160 while others had very strong opinions against it. So it was
26161 decided to remove it. KFS 2003-09-03 */
26163 /* Finally perform built-in cursor blinking:
26164 filled box <-> hollow box
26165 wide [h]bar <-> narrow [h]bar
26166 narrow [h]bar <-> no cursor
26167 other type <-> no cursor */
26169 if (cursor_type == FILLED_BOX_CURSOR)
26170 return HOLLOW_BOX_CURSOR;
26172 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26174 *width = 1;
26175 return cursor_type;
26177 #endif
26179 return NO_CURSOR;
26183 /* Notice when the text cursor of window W has been completely
26184 overwritten by a drawing operation that outputs glyphs in AREA
26185 starting at X0 and ending at X1 in the line starting at Y0 and
26186 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26187 the rest of the line after X0 has been written. Y coordinates
26188 are window-relative. */
26190 static void
26191 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26192 int x0, int x1, int y0, int y1)
26194 int cx0, cx1, cy0, cy1;
26195 struct glyph_row *row;
26197 if (!w->phys_cursor_on_p)
26198 return;
26199 if (area != TEXT_AREA)
26200 return;
26202 if (w->phys_cursor.vpos < 0
26203 || w->phys_cursor.vpos >= w->current_matrix->nrows
26204 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26205 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26206 return;
26208 if (row->cursor_in_fringe_p)
26210 row->cursor_in_fringe_p = 0;
26211 draw_fringe_bitmap (w, row, row->reversed_p);
26212 w->phys_cursor_on_p = 0;
26213 return;
26216 cx0 = w->phys_cursor.x;
26217 cx1 = cx0 + w->phys_cursor_width;
26218 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26219 return;
26221 /* The cursor image will be completely removed from the
26222 screen if the output area intersects the cursor area in
26223 y-direction. When we draw in [y0 y1[, and some part of
26224 the cursor is at y < y0, that part must have been drawn
26225 before. When scrolling, the cursor is erased before
26226 actually scrolling, so we don't come here. When not
26227 scrolling, the rows above the old cursor row must have
26228 changed, and in this case these rows must have written
26229 over the cursor image.
26231 Likewise if part of the cursor is below y1, with the
26232 exception of the cursor being in the first blank row at
26233 the buffer and window end because update_text_area
26234 doesn't draw that row. (Except when it does, but
26235 that's handled in update_text_area.) */
26237 cy0 = w->phys_cursor.y;
26238 cy1 = cy0 + w->phys_cursor_height;
26239 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26240 return;
26242 w->phys_cursor_on_p = 0;
26245 #endif /* HAVE_WINDOW_SYSTEM */
26248 /************************************************************************
26249 Mouse Face
26250 ************************************************************************/
26252 #ifdef HAVE_WINDOW_SYSTEM
26254 /* EXPORT for RIF:
26255 Fix the display of area AREA of overlapping row ROW in window W
26256 with respect to the overlapping part OVERLAPS. */
26258 void
26259 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26260 enum glyph_row_area area, int overlaps)
26262 int i, x;
26264 block_input ();
26266 x = 0;
26267 for (i = 0; i < row->used[area];)
26269 if (row->glyphs[area][i].overlaps_vertically_p)
26271 int start = i, start_x = x;
26275 x += row->glyphs[area][i].pixel_width;
26276 ++i;
26278 while (i < row->used[area]
26279 && row->glyphs[area][i].overlaps_vertically_p);
26281 draw_glyphs (w, start_x, row, area,
26282 start, i,
26283 DRAW_NORMAL_TEXT, overlaps);
26285 else
26287 x += row->glyphs[area][i].pixel_width;
26288 ++i;
26292 unblock_input ();
26296 /* EXPORT:
26297 Draw the cursor glyph of window W in glyph row ROW. See the
26298 comment of draw_glyphs for the meaning of HL. */
26300 void
26301 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26302 enum draw_glyphs_face hl)
26304 /* If cursor hpos is out of bounds, don't draw garbage. This can
26305 happen in mini-buffer windows when switching between echo area
26306 glyphs and mini-buffer. */
26307 if ((row->reversed_p
26308 ? (w->phys_cursor.hpos >= 0)
26309 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26311 int on_p = w->phys_cursor_on_p;
26312 int x1;
26313 int hpos = w->phys_cursor.hpos;
26315 /* When the window is hscrolled, cursor hpos can legitimately be
26316 out of bounds, but we draw the cursor at the corresponding
26317 window margin in that case. */
26318 if (!row->reversed_p && hpos < 0)
26319 hpos = 0;
26320 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26321 hpos = row->used[TEXT_AREA] - 1;
26323 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26324 hl, 0);
26325 w->phys_cursor_on_p = on_p;
26327 if (hl == DRAW_CURSOR)
26328 w->phys_cursor_width = x1 - w->phys_cursor.x;
26329 /* When we erase the cursor, and ROW is overlapped by other
26330 rows, make sure that these overlapping parts of other rows
26331 are redrawn. */
26332 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26334 w->phys_cursor_width = x1 - w->phys_cursor.x;
26336 if (row > w->current_matrix->rows
26337 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26338 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26339 OVERLAPS_ERASED_CURSOR);
26341 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26342 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26343 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26344 OVERLAPS_ERASED_CURSOR);
26350 /* EXPORT:
26351 Erase the image of a cursor of window W from the screen. */
26353 void
26354 erase_phys_cursor (struct window *w)
26356 struct frame *f = XFRAME (w->frame);
26357 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26358 int hpos = w->phys_cursor.hpos;
26359 int vpos = w->phys_cursor.vpos;
26360 int mouse_face_here_p = 0;
26361 struct glyph_matrix *active_glyphs = w->current_matrix;
26362 struct glyph_row *cursor_row;
26363 struct glyph *cursor_glyph;
26364 enum draw_glyphs_face hl;
26366 /* No cursor displayed or row invalidated => nothing to do on the
26367 screen. */
26368 if (w->phys_cursor_type == NO_CURSOR)
26369 goto mark_cursor_off;
26371 /* VPOS >= active_glyphs->nrows means that window has been resized.
26372 Don't bother to erase the cursor. */
26373 if (vpos >= active_glyphs->nrows)
26374 goto mark_cursor_off;
26376 /* If row containing cursor is marked invalid, there is nothing we
26377 can do. */
26378 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26379 if (!cursor_row->enabled_p)
26380 goto mark_cursor_off;
26382 /* If line spacing is > 0, old cursor may only be partially visible in
26383 window after split-window. So adjust visible height. */
26384 cursor_row->visible_height = min (cursor_row->visible_height,
26385 window_text_bottom_y (w) - cursor_row->y);
26387 /* If row is completely invisible, don't attempt to delete a cursor which
26388 isn't there. This can happen if cursor is at top of a window, and
26389 we switch to a buffer with a header line in that window. */
26390 if (cursor_row->visible_height <= 0)
26391 goto mark_cursor_off;
26393 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26394 if (cursor_row->cursor_in_fringe_p)
26396 cursor_row->cursor_in_fringe_p = 0;
26397 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26398 goto mark_cursor_off;
26401 /* This can happen when the new row is shorter than the old one.
26402 In this case, either draw_glyphs or clear_end_of_line
26403 should have cleared the cursor. Note that we wouldn't be
26404 able to erase the cursor in this case because we don't have a
26405 cursor glyph at hand. */
26406 if ((cursor_row->reversed_p
26407 ? (w->phys_cursor.hpos < 0)
26408 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26409 goto mark_cursor_off;
26411 /* When the window is hscrolled, cursor hpos can legitimately be out
26412 of bounds, but we draw the cursor at the corresponding window
26413 margin in that case. */
26414 if (!cursor_row->reversed_p && hpos < 0)
26415 hpos = 0;
26416 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26417 hpos = cursor_row->used[TEXT_AREA] - 1;
26419 /* If the cursor is in the mouse face area, redisplay that when
26420 we clear the cursor. */
26421 if (! NILP (hlinfo->mouse_face_window)
26422 && coords_in_mouse_face_p (w, hpos, vpos)
26423 /* Don't redraw the cursor's spot in mouse face if it is at the
26424 end of a line (on a newline). The cursor appears there, but
26425 mouse highlighting does not. */
26426 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26427 mouse_face_here_p = 1;
26429 /* Maybe clear the display under the cursor. */
26430 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26432 int x, y, left_x;
26433 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26434 int width;
26436 cursor_glyph = get_phys_cursor_glyph (w);
26437 if (cursor_glyph == NULL)
26438 goto mark_cursor_off;
26440 width = cursor_glyph->pixel_width;
26441 left_x = window_box_left_offset (w, TEXT_AREA);
26442 x = w->phys_cursor.x;
26443 if (x < left_x)
26444 width -= left_x - x;
26445 width = min (width, window_box_width (w, TEXT_AREA) - x);
26446 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26447 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26449 if (width > 0)
26450 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26453 /* Erase the cursor by redrawing the character underneath it. */
26454 if (mouse_face_here_p)
26455 hl = DRAW_MOUSE_FACE;
26456 else
26457 hl = DRAW_NORMAL_TEXT;
26458 draw_phys_cursor_glyph (w, cursor_row, hl);
26460 mark_cursor_off:
26461 w->phys_cursor_on_p = 0;
26462 w->phys_cursor_type = NO_CURSOR;
26466 /* EXPORT:
26467 Display or clear cursor of window W. If ON is zero, clear the
26468 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26469 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26471 void
26472 display_and_set_cursor (struct window *w, bool on,
26473 int hpos, int vpos, int x, int y)
26475 struct frame *f = XFRAME (w->frame);
26476 int new_cursor_type;
26477 int new_cursor_width;
26478 int active_cursor;
26479 struct glyph_row *glyph_row;
26480 struct glyph *glyph;
26482 /* This is pointless on invisible frames, and dangerous on garbaged
26483 windows and frames; in the latter case, the frame or window may
26484 be in the midst of changing its size, and x and y may be off the
26485 window. */
26486 if (! FRAME_VISIBLE_P (f)
26487 || FRAME_GARBAGED_P (f)
26488 || vpos >= w->current_matrix->nrows
26489 || hpos >= w->current_matrix->matrix_w)
26490 return;
26492 /* If cursor is off and we want it off, return quickly. */
26493 if (!on && !w->phys_cursor_on_p)
26494 return;
26496 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26497 /* If cursor row is not enabled, we don't really know where to
26498 display the cursor. */
26499 if (!glyph_row->enabled_p)
26501 w->phys_cursor_on_p = 0;
26502 return;
26505 glyph = NULL;
26506 if (!glyph_row->exact_window_width_line_p
26507 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26508 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26510 eassert (input_blocked_p ());
26512 /* Set new_cursor_type to the cursor we want to be displayed. */
26513 new_cursor_type = get_window_cursor_type (w, glyph,
26514 &new_cursor_width, &active_cursor);
26516 /* If cursor is currently being shown and we don't want it to be or
26517 it is in the wrong place, or the cursor type is not what we want,
26518 erase it. */
26519 if (w->phys_cursor_on_p
26520 && (!on
26521 || w->phys_cursor.x != x
26522 || w->phys_cursor.y != y
26523 || new_cursor_type != w->phys_cursor_type
26524 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26525 && new_cursor_width != w->phys_cursor_width)))
26526 erase_phys_cursor (w);
26528 /* Don't check phys_cursor_on_p here because that flag is only set
26529 to zero in some cases where we know that the cursor has been
26530 completely erased, to avoid the extra work of erasing the cursor
26531 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26532 still not be visible, or it has only been partly erased. */
26533 if (on)
26535 w->phys_cursor_ascent = glyph_row->ascent;
26536 w->phys_cursor_height = glyph_row->height;
26538 /* Set phys_cursor_.* before x_draw_.* is called because some
26539 of them may need the information. */
26540 w->phys_cursor.x = x;
26541 w->phys_cursor.y = glyph_row->y;
26542 w->phys_cursor.hpos = hpos;
26543 w->phys_cursor.vpos = vpos;
26546 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26547 new_cursor_type, new_cursor_width,
26548 on, active_cursor);
26552 /* Switch the display of W's cursor on or off, according to the value
26553 of ON. */
26555 static void
26556 update_window_cursor (struct window *w, bool on)
26558 /* Don't update cursor in windows whose frame is in the process
26559 of being deleted. */
26560 if (w->current_matrix)
26562 int hpos = w->phys_cursor.hpos;
26563 int vpos = w->phys_cursor.vpos;
26564 struct glyph_row *row;
26566 if (vpos >= w->current_matrix->nrows
26567 || hpos >= w->current_matrix->matrix_w)
26568 return;
26570 row = MATRIX_ROW (w->current_matrix, vpos);
26572 /* When the window is hscrolled, cursor hpos can legitimately be
26573 out of bounds, but we draw the cursor at the corresponding
26574 window margin in that case. */
26575 if (!row->reversed_p && hpos < 0)
26576 hpos = 0;
26577 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26578 hpos = row->used[TEXT_AREA] - 1;
26580 block_input ();
26581 display_and_set_cursor (w, on, hpos, vpos,
26582 w->phys_cursor.x, w->phys_cursor.y);
26583 unblock_input ();
26588 /* Call update_window_cursor with parameter ON_P on all leaf windows
26589 in the window tree rooted at W. */
26591 static void
26592 update_cursor_in_window_tree (struct window *w, bool on_p)
26594 while (w)
26596 if (WINDOWP (w->contents))
26597 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26598 else
26599 update_window_cursor (w, on_p);
26601 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26606 /* EXPORT:
26607 Display the cursor on window W, or clear it, according to ON_P.
26608 Don't change the cursor's position. */
26610 void
26611 x_update_cursor (struct frame *f, bool on_p)
26613 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26617 /* EXPORT:
26618 Clear the cursor of window W to background color, and mark the
26619 cursor as not shown. This is used when the text where the cursor
26620 is about to be rewritten. */
26622 void
26623 x_clear_cursor (struct window *w)
26625 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26626 update_window_cursor (w, 0);
26629 #endif /* HAVE_WINDOW_SYSTEM */
26631 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26632 and MSDOS. */
26633 static void
26634 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26635 int start_hpos, int end_hpos,
26636 enum draw_glyphs_face draw)
26638 #ifdef HAVE_WINDOW_SYSTEM
26639 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26641 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26642 return;
26644 #endif
26645 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26646 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26647 #endif
26650 /* Display the active region described by mouse_face_* according to DRAW. */
26652 static void
26653 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26655 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26656 struct frame *f = XFRAME (WINDOW_FRAME (w));
26658 if (/* If window is in the process of being destroyed, don't bother
26659 to do anything. */
26660 w->current_matrix != NULL
26661 /* Don't update mouse highlight if hidden */
26662 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26663 /* Recognize when we are called to operate on rows that don't exist
26664 anymore. This can happen when a window is split. */
26665 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26667 int phys_cursor_on_p = w->phys_cursor_on_p;
26668 struct glyph_row *row, *first, *last;
26670 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26671 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26673 for (row = first; row <= last && row->enabled_p; ++row)
26675 int start_hpos, end_hpos, start_x;
26677 /* For all but the first row, the highlight starts at column 0. */
26678 if (row == first)
26680 /* R2L rows have BEG and END in reversed order, but the
26681 screen drawing geometry is always left to right. So
26682 we need to mirror the beginning and end of the
26683 highlighted area in R2L rows. */
26684 if (!row->reversed_p)
26686 start_hpos = hlinfo->mouse_face_beg_col;
26687 start_x = hlinfo->mouse_face_beg_x;
26689 else if (row == last)
26691 start_hpos = hlinfo->mouse_face_end_col;
26692 start_x = hlinfo->mouse_face_end_x;
26694 else
26696 start_hpos = 0;
26697 start_x = 0;
26700 else if (row->reversed_p && row == last)
26702 start_hpos = hlinfo->mouse_face_end_col;
26703 start_x = hlinfo->mouse_face_end_x;
26705 else
26707 start_hpos = 0;
26708 start_x = 0;
26711 if (row == last)
26713 if (!row->reversed_p)
26714 end_hpos = hlinfo->mouse_face_end_col;
26715 else if (row == first)
26716 end_hpos = hlinfo->mouse_face_beg_col;
26717 else
26719 end_hpos = row->used[TEXT_AREA];
26720 if (draw == DRAW_NORMAL_TEXT)
26721 row->fill_line_p = 1; /* Clear to end of line */
26724 else if (row->reversed_p && row == first)
26725 end_hpos = hlinfo->mouse_face_beg_col;
26726 else
26728 end_hpos = row->used[TEXT_AREA];
26729 if (draw == DRAW_NORMAL_TEXT)
26730 row->fill_line_p = 1; /* Clear to end of line */
26733 if (end_hpos > start_hpos)
26735 draw_row_with_mouse_face (w, start_x, row,
26736 start_hpos, end_hpos, draw);
26738 row->mouse_face_p
26739 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26743 #ifdef HAVE_WINDOW_SYSTEM
26744 /* When we've written over the cursor, arrange for it to
26745 be displayed again. */
26746 if (FRAME_WINDOW_P (f)
26747 && phys_cursor_on_p && !w->phys_cursor_on_p)
26749 int hpos = w->phys_cursor.hpos;
26751 /* When the window is hscrolled, cursor hpos can legitimately be
26752 out of bounds, but we draw the cursor at the corresponding
26753 window margin in that case. */
26754 if (!row->reversed_p && hpos < 0)
26755 hpos = 0;
26756 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26757 hpos = row->used[TEXT_AREA] - 1;
26759 block_input ();
26760 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26761 w->phys_cursor.x, w->phys_cursor.y);
26762 unblock_input ();
26764 #endif /* HAVE_WINDOW_SYSTEM */
26767 #ifdef HAVE_WINDOW_SYSTEM
26768 /* Change the mouse cursor. */
26769 if (FRAME_WINDOW_P (f))
26771 if (draw == DRAW_NORMAL_TEXT
26772 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26773 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26774 else if (draw == DRAW_MOUSE_FACE)
26775 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26776 else
26777 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26779 #endif /* HAVE_WINDOW_SYSTEM */
26782 /* EXPORT:
26783 Clear out the mouse-highlighted active region.
26784 Redraw it un-highlighted first. Value is non-zero if mouse
26785 face was actually drawn unhighlighted. */
26788 clear_mouse_face (Mouse_HLInfo *hlinfo)
26790 int cleared = 0;
26792 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26794 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26795 cleared = 1;
26798 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26799 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26800 hlinfo->mouse_face_window = Qnil;
26801 hlinfo->mouse_face_overlay = Qnil;
26802 return cleared;
26805 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26806 within the mouse face on that window. */
26807 static int
26808 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26810 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26812 /* Quickly resolve the easy cases. */
26813 if (!(WINDOWP (hlinfo->mouse_face_window)
26814 && XWINDOW (hlinfo->mouse_face_window) == w))
26815 return 0;
26816 if (vpos < hlinfo->mouse_face_beg_row
26817 || vpos > hlinfo->mouse_face_end_row)
26818 return 0;
26819 if (vpos > hlinfo->mouse_face_beg_row
26820 && vpos < hlinfo->mouse_face_end_row)
26821 return 1;
26823 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26825 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26827 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26828 return 1;
26830 else if ((vpos == hlinfo->mouse_face_beg_row
26831 && hpos >= hlinfo->mouse_face_beg_col)
26832 || (vpos == hlinfo->mouse_face_end_row
26833 && hpos < hlinfo->mouse_face_end_col))
26834 return 1;
26836 else
26838 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26840 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26841 return 1;
26843 else if ((vpos == hlinfo->mouse_face_beg_row
26844 && hpos <= hlinfo->mouse_face_beg_col)
26845 || (vpos == hlinfo->mouse_face_end_row
26846 && hpos > hlinfo->mouse_face_end_col))
26847 return 1;
26849 return 0;
26853 /* EXPORT:
26854 Non-zero if physical cursor of window W is within mouse face. */
26857 cursor_in_mouse_face_p (struct window *w)
26859 int hpos = w->phys_cursor.hpos;
26860 int vpos = w->phys_cursor.vpos;
26861 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26863 /* When the window is hscrolled, cursor hpos can legitimately be out
26864 of bounds, but we draw the cursor at the corresponding window
26865 margin in that case. */
26866 if (!row->reversed_p && hpos < 0)
26867 hpos = 0;
26868 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26869 hpos = row->used[TEXT_AREA] - 1;
26871 return coords_in_mouse_face_p (w, hpos, vpos);
26876 /* Find the glyph rows START_ROW and END_ROW of window W that display
26877 characters between buffer positions START_CHARPOS and END_CHARPOS
26878 (excluding END_CHARPOS). DISP_STRING is a display string that
26879 covers these buffer positions. This is similar to
26880 row_containing_pos, but is more accurate when bidi reordering makes
26881 buffer positions change non-linearly with glyph rows. */
26882 static void
26883 rows_from_pos_range (struct window *w,
26884 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26885 Lisp_Object disp_string,
26886 struct glyph_row **start, struct glyph_row **end)
26888 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26889 int last_y = window_text_bottom_y (w);
26890 struct glyph_row *row;
26892 *start = NULL;
26893 *end = NULL;
26895 while (!first->enabled_p
26896 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26897 first++;
26899 /* Find the START row. */
26900 for (row = first;
26901 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26902 row++)
26904 /* A row can potentially be the START row if the range of the
26905 characters it displays intersects the range
26906 [START_CHARPOS..END_CHARPOS). */
26907 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26908 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26909 /* See the commentary in row_containing_pos, for the
26910 explanation of the complicated way to check whether
26911 some position is beyond the end of the characters
26912 displayed by a row. */
26913 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26914 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26915 && !row->ends_at_zv_p
26916 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26917 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26918 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26919 && !row->ends_at_zv_p
26920 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26922 /* Found a candidate row. Now make sure at least one of the
26923 glyphs it displays has a charpos from the range
26924 [START_CHARPOS..END_CHARPOS).
26926 This is not obvious because bidi reordering could make
26927 buffer positions of a row be 1,2,3,102,101,100, and if we
26928 want to highlight characters in [50..60), we don't want
26929 this row, even though [50..60) does intersect [1..103),
26930 the range of character positions given by the row's start
26931 and end positions. */
26932 struct glyph *g = row->glyphs[TEXT_AREA];
26933 struct glyph *e = g + row->used[TEXT_AREA];
26935 while (g < e)
26937 if (((BUFFERP (g->object) || INTEGERP (g->object))
26938 && start_charpos <= g->charpos && g->charpos < end_charpos)
26939 /* A glyph that comes from DISP_STRING is by
26940 definition to be highlighted. */
26941 || EQ (g->object, disp_string))
26942 *start = row;
26943 g++;
26945 if (*start)
26946 break;
26950 /* Find the END row. */
26951 if (!*start
26952 /* If the last row is partially visible, start looking for END
26953 from that row, instead of starting from FIRST. */
26954 && !(row->enabled_p
26955 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26956 row = first;
26957 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26959 struct glyph_row *next = row + 1;
26960 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26962 if (!next->enabled_p
26963 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26964 /* The first row >= START whose range of displayed characters
26965 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26966 is the row END + 1. */
26967 || (start_charpos < next_start
26968 && end_charpos < next_start)
26969 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26970 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26971 && !next->ends_at_zv_p
26972 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26973 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26974 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26975 && !next->ends_at_zv_p
26976 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26978 *end = row;
26979 break;
26981 else
26983 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26984 but none of the characters it displays are in the range, it is
26985 also END + 1. */
26986 struct glyph *g = next->glyphs[TEXT_AREA];
26987 struct glyph *s = g;
26988 struct glyph *e = g + next->used[TEXT_AREA];
26990 while (g < e)
26992 if (((BUFFERP (g->object) || INTEGERP (g->object))
26993 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26994 /* If the buffer position of the first glyph in
26995 the row is equal to END_CHARPOS, it means
26996 the last character to be highlighted is the
26997 newline of ROW, and we must consider NEXT as
26998 END, not END+1. */
26999 || (((!next->reversed_p && g == s)
27000 || (next->reversed_p && g == e - 1))
27001 && (g->charpos == end_charpos
27002 /* Special case for when NEXT is an
27003 empty line at ZV. */
27004 || (g->charpos == -1
27005 && !row->ends_at_zv_p
27006 && next_start == end_charpos)))))
27007 /* A glyph that comes from DISP_STRING is by
27008 definition to be highlighted. */
27009 || EQ (g->object, disp_string))
27010 break;
27011 g++;
27013 if (g == e)
27015 *end = row;
27016 break;
27018 /* The first row that ends at ZV must be the last to be
27019 highlighted. */
27020 else if (next->ends_at_zv_p)
27022 *end = next;
27023 break;
27029 /* This function sets the mouse_face_* elements of HLINFO, assuming
27030 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27031 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27032 for the overlay or run of text properties specifying the mouse
27033 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27034 before-string and after-string that must also be highlighted.
27035 DISP_STRING, if non-nil, is a display string that may cover some
27036 or all of the highlighted text. */
27038 static void
27039 mouse_face_from_buffer_pos (Lisp_Object window,
27040 Mouse_HLInfo *hlinfo,
27041 ptrdiff_t mouse_charpos,
27042 ptrdiff_t start_charpos,
27043 ptrdiff_t end_charpos,
27044 Lisp_Object before_string,
27045 Lisp_Object after_string,
27046 Lisp_Object disp_string)
27048 struct window *w = XWINDOW (window);
27049 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27050 struct glyph_row *r1, *r2;
27051 struct glyph *glyph, *end;
27052 ptrdiff_t ignore, pos;
27053 int x;
27055 eassert (NILP (disp_string) || STRINGP (disp_string));
27056 eassert (NILP (before_string) || STRINGP (before_string));
27057 eassert (NILP (after_string) || STRINGP (after_string));
27059 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27060 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27061 if (r1 == NULL)
27062 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27063 /* If the before-string or display-string contains newlines,
27064 rows_from_pos_range skips to its last row. Move back. */
27065 if (!NILP (before_string) || !NILP (disp_string))
27067 struct glyph_row *prev;
27068 while ((prev = r1 - 1, prev >= first)
27069 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27070 && prev->used[TEXT_AREA] > 0)
27072 struct glyph *beg = prev->glyphs[TEXT_AREA];
27073 glyph = beg + prev->used[TEXT_AREA];
27074 while (--glyph >= beg && INTEGERP (glyph->object));
27075 if (glyph < beg
27076 || !(EQ (glyph->object, before_string)
27077 || EQ (glyph->object, disp_string)))
27078 break;
27079 r1 = prev;
27082 if (r2 == NULL)
27084 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27085 hlinfo->mouse_face_past_end = 1;
27087 else if (!NILP (after_string))
27089 /* If the after-string has newlines, advance to its last row. */
27090 struct glyph_row *next;
27091 struct glyph_row *last
27092 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27094 for (next = r2 + 1;
27095 next <= last
27096 && next->used[TEXT_AREA] > 0
27097 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27098 ++next)
27099 r2 = next;
27101 /* The rest of the display engine assumes that mouse_face_beg_row is
27102 either above mouse_face_end_row or identical to it. But with
27103 bidi-reordered continued lines, the row for START_CHARPOS could
27104 be below the row for END_CHARPOS. If so, swap the rows and store
27105 them in correct order. */
27106 if (r1->y > r2->y)
27108 struct glyph_row *tem = r2;
27110 r2 = r1;
27111 r1 = tem;
27114 hlinfo->mouse_face_beg_y = r1->y;
27115 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27116 hlinfo->mouse_face_end_y = r2->y;
27117 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27119 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27120 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27121 could be anywhere in the row and in any order. The strategy
27122 below is to find the leftmost and the rightmost glyph that
27123 belongs to either of these 3 strings, or whose position is
27124 between START_CHARPOS and END_CHARPOS, and highlight all the
27125 glyphs between those two. This may cover more than just the text
27126 between START_CHARPOS and END_CHARPOS if the range of characters
27127 strides the bidi level boundary, e.g. if the beginning is in R2L
27128 text while the end is in L2R text or vice versa. */
27129 if (!r1->reversed_p)
27131 /* This row is in a left to right paragraph. Scan it left to
27132 right. */
27133 glyph = r1->glyphs[TEXT_AREA];
27134 end = glyph + r1->used[TEXT_AREA];
27135 x = r1->x;
27137 /* Skip truncation glyphs at the start of the glyph row. */
27138 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27139 for (; glyph < end
27140 && INTEGERP (glyph->object)
27141 && glyph->charpos < 0;
27142 ++glyph)
27143 x += glyph->pixel_width;
27145 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27146 or DISP_STRING, and the first glyph from buffer whose
27147 position is between START_CHARPOS and END_CHARPOS. */
27148 for (; glyph < end
27149 && !INTEGERP (glyph->object)
27150 && !EQ (glyph->object, disp_string)
27151 && !(BUFFERP (glyph->object)
27152 && (glyph->charpos >= start_charpos
27153 && glyph->charpos < end_charpos));
27154 ++glyph)
27156 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27157 are present at buffer positions between START_CHARPOS and
27158 END_CHARPOS, or if they come from an overlay. */
27159 if (EQ (glyph->object, before_string))
27161 pos = string_buffer_position (before_string,
27162 start_charpos);
27163 /* If pos == 0, it means before_string came from an
27164 overlay, not from a buffer position. */
27165 if (!pos || (pos >= start_charpos && pos < end_charpos))
27166 break;
27168 else if (EQ (glyph->object, after_string))
27170 pos = string_buffer_position (after_string, end_charpos);
27171 if (!pos || (pos >= start_charpos && pos < end_charpos))
27172 break;
27174 x += glyph->pixel_width;
27176 hlinfo->mouse_face_beg_x = x;
27177 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27179 else
27181 /* This row is in a right to left paragraph. Scan it right to
27182 left. */
27183 struct glyph *g;
27185 end = r1->glyphs[TEXT_AREA] - 1;
27186 glyph = end + r1->used[TEXT_AREA];
27188 /* Skip truncation glyphs at the start of the glyph row. */
27189 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27190 for (; glyph > end
27191 && INTEGERP (glyph->object)
27192 && glyph->charpos < 0;
27193 --glyph)
27196 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27197 or DISP_STRING, and the first glyph from buffer whose
27198 position is between START_CHARPOS and END_CHARPOS. */
27199 for (; glyph > end
27200 && !INTEGERP (glyph->object)
27201 && !EQ (glyph->object, disp_string)
27202 && !(BUFFERP (glyph->object)
27203 && (glyph->charpos >= start_charpos
27204 && glyph->charpos < end_charpos));
27205 --glyph)
27207 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27208 are present at buffer positions between START_CHARPOS and
27209 END_CHARPOS, or if they come from an overlay. */
27210 if (EQ (glyph->object, before_string))
27212 pos = string_buffer_position (before_string, start_charpos);
27213 /* If pos == 0, it means before_string came from an
27214 overlay, not from a buffer position. */
27215 if (!pos || (pos >= start_charpos && pos < end_charpos))
27216 break;
27218 else if (EQ (glyph->object, after_string))
27220 pos = string_buffer_position (after_string, end_charpos);
27221 if (!pos || (pos >= start_charpos && pos < end_charpos))
27222 break;
27226 glyph++; /* first glyph to the right of the highlighted area */
27227 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27228 x += g->pixel_width;
27229 hlinfo->mouse_face_beg_x = x;
27230 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27233 /* If the highlight ends in a different row, compute GLYPH and END
27234 for the end row. Otherwise, reuse the values computed above for
27235 the row where the highlight begins. */
27236 if (r2 != r1)
27238 if (!r2->reversed_p)
27240 glyph = r2->glyphs[TEXT_AREA];
27241 end = glyph + r2->used[TEXT_AREA];
27242 x = r2->x;
27244 else
27246 end = r2->glyphs[TEXT_AREA] - 1;
27247 glyph = end + r2->used[TEXT_AREA];
27251 if (!r2->reversed_p)
27253 /* Skip truncation and continuation glyphs near the end of the
27254 row, and also blanks and stretch glyphs inserted by
27255 extend_face_to_end_of_line. */
27256 while (end > glyph
27257 && INTEGERP ((end - 1)->object))
27258 --end;
27259 /* Scan the rest of the glyph row from the end, looking for the
27260 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27261 DISP_STRING, or whose position is between START_CHARPOS
27262 and END_CHARPOS */
27263 for (--end;
27264 end > glyph
27265 && !INTEGERP (end->object)
27266 && !EQ (end->object, disp_string)
27267 && !(BUFFERP (end->object)
27268 && (end->charpos >= start_charpos
27269 && end->charpos < end_charpos));
27270 --end)
27272 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27273 are present at buffer positions between START_CHARPOS and
27274 END_CHARPOS, or if they come from an overlay. */
27275 if (EQ (end->object, before_string))
27277 pos = string_buffer_position (before_string, start_charpos);
27278 if (!pos || (pos >= start_charpos && pos < end_charpos))
27279 break;
27281 else if (EQ (end->object, after_string))
27283 pos = string_buffer_position (after_string, end_charpos);
27284 if (!pos || (pos >= start_charpos && pos < end_charpos))
27285 break;
27288 /* Find the X coordinate of the last glyph to be highlighted. */
27289 for (; glyph <= end; ++glyph)
27290 x += glyph->pixel_width;
27292 hlinfo->mouse_face_end_x = x;
27293 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27295 else
27297 /* Skip truncation and continuation glyphs near the end of the
27298 row, and also blanks and stretch glyphs inserted by
27299 extend_face_to_end_of_line. */
27300 x = r2->x;
27301 end++;
27302 while (end < glyph
27303 && INTEGERP (end->object))
27305 x += end->pixel_width;
27306 ++end;
27308 /* Scan the rest of the glyph row from the end, looking for the
27309 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27310 DISP_STRING, or whose position is between START_CHARPOS
27311 and END_CHARPOS */
27312 for ( ;
27313 end < glyph
27314 && !INTEGERP (end->object)
27315 && !EQ (end->object, disp_string)
27316 && !(BUFFERP (end->object)
27317 && (end->charpos >= start_charpos
27318 && end->charpos < end_charpos));
27319 ++end)
27321 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27322 are present at buffer positions between START_CHARPOS and
27323 END_CHARPOS, or if they come from an overlay. */
27324 if (EQ (end->object, before_string))
27326 pos = string_buffer_position (before_string, start_charpos);
27327 if (!pos || (pos >= start_charpos && pos < end_charpos))
27328 break;
27330 else if (EQ (end->object, after_string))
27332 pos = string_buffer_position (after_string, end_charpos);
27333 if (!pos || (pos >= start_charpos && pos < end_charpos))
27334 break;
27336 x += end->pixel_width;
27338 /* If we exited the above loop because we arrived at the last
27339 glyph of the row, and its buffer position is still not in
27340 range, it means the last character in range is the preceding
27341 newline. Bump the end column and x values to get past the
27342 last glyph. */
27343 if (end == glyph
27344 && BUFFERP (end->object)
27345 && (end->charpos < start_charpos
27346 || end->charpos >= end_charpos))
27348 x += end->pixel_width;
27349 ++end;
27351 hlinfo->mouse_face_end_x = x;
27352 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27355 hlinfo->mouse_face_window = window;
27356 hlinfo->mouse_face_face_id
27357 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27358 mouse_charpos + 1,
27359 !hlinfo->mouse_face_hidden, -1);
27360 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27363 /* The following function is not used anymore (replaced with
27364 mouse_face_from_string_pos), but I leave it here for the time
27365 being, in case someone would. */
27367 #if 0 /* not used */
27369 /* Find the position of the glyph for position POS in OBJECT in
27370 window W's current matrix, and return in *X, *Y the pixel
27371 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27373 RIGHT_P non-zero means return the position of the right edge of the
27374 glyph, RIGHT_P zero means return the left edge position.
27376 If no glyph for POS exists in the matrix, return the position of
27377 the glyph with the next smaller position that is in the matrix, if
27378 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27379 exists in the matrix, return the position of the glyph with the
27380 next larger position in OBJECT.
27382 Value is non-zero if a glyph was found. */
27384 static int
27385 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27386 int *hpos, int *vpos, int *x, int *y, int right_p)
27388 int yb = window_text_bottom_y (w);
27389 struct glyph_row *r;
27390 struct glyph *best_glyph = NULL;
27391 struct glyph_row *best_row = NULL;
27392 int best_x = 0;
27394 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27395 r->enabled_p && r->y < yb;
27396 ++r)
27398 struct glyph *g = r->glyphs[TEXT_AREA];
27399 struct glyph *e = g + r->used[TEXT_AREA];
27400 int gx;
27402 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27403 if (EQ (g->object, object))
27405 if (g->charpos == pos)
27407 best_glyph = g;
27408 best_x = gx;
27409 best_row = r;
27410 goto found;
27412 else if (best_glyph == NULL
27413 || ((eabs (g->charpos - pos)
27414 < eabs (best_glyph->charpos - pos))
27415 && (right_p
27416 ? g->charpos < pos
27417 : g->charpos > pos)))
27419 best_glyph = g;
27420 best_x = gx;
27421 best_row = r;
27426 found:
27428 if (best_glyph)
27430 *x = best_x;
27431 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27433 if (right_p)
27435 *x += best_glyph->pixel_width;
27436 ++*hpos;
27439 *y = best_row->y;
27440 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27443 return best_glyph != NULL;
27445 #endif /* not used */
27447 /* Find the positions of the first and the last glyphs in window W's
27448 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27449 (assumed to be a string), and return in HLINFO's mouse_face_*
27450 members the pixel and column/row coordinates of those glyphs. */
27452 static void
27453 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27454 Lisp_Object object,
27455 ptrdiff_t startpos, ptrdiff_t endpos)
27457 int yb = window_text_bottom_y (w);
27458 struct glyph_row *r;
27459 struct glyph *g, *e;
27460 int gx;
27461 int found = 0;
27463 /* Find the glyph row with at least one position in the range
27464 [STARTPOS..ENDPOS], and the first glyph in that row whose
27465 position belongs to that range. */
27466 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27467 r->enabled_p && r->y < yb;
27468 ++r)
27470 if (!r->reversed_p)
27472 g = r->glyphs[TEXT_AREA];
27473 e = g + r->used[TEXT_AREA];
27474 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27475 if (EQ (g->object, object)
27476 && startpos <= g->charpos && g->charpos <= endpos)
27478 hlinfo->mouse_face_beg_row
27479 = MATRIX_ROW_VPOS (r, w->current_matrix);
27480 hlinfo->mouse_face_beg_y = r->y;
27481 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27482 hlinfo->mouse_face_beg_x = gx;
27483 found = 1;
27484 break;
27487 else
27489 struct glyph *g1;
27491 e = r->glyphs[TEXT_AREA];
27492 g = e + r->used[TEXT_AREA];
27493 for ( ; g > e; --g)
27494 if (EQ ((g-1)->object, object)
27495 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27497 hlinfo->mouse_face_beg_row
27498 = MATRIX_ROW_VPOS (r, w->current_matrix);
27499 hlinfo->mouse_face_beg_y = r->y;
27500 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27501 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27502 gx += g1->pixel_width;
27503 hlinfo->mouse_face_beg_x = gx;
27504 found = 1;
27505 break;
27508 if (found)
27509 break;
27512 if (!found)
27513 return;
27515 /* Starting with the next row, look for the first row which does NOT
27516 include any glyphs whose positions are in the range. */
27517 for (++r; r->enabled_p && r->y < yb; ++r)
27519 g = r->glyphs[TEXT_AREA];
27520 e = g + r->used[TEXT_AREA];
27521 found = 0;
27522 for ( ; g < e; ++g)
27523 if (EQ (g->object, object)
27524 && startpos <= g->charpos && g->charpos <= endpos)
27526 found = 1;
27527 break;
27529 if (!found)
27530 break;
27533 /* The highlighted region ends on the previous row. */
27534 r--;
27536 /* Set the end row and its vertical pixel coordinate. */
27537 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27538 hlinfo->mouse_face_end_y = r->y;
27540 /* Compute and set the end column and the end column's horizontal
27541 pixel coordinate. */
27542 if (!r->reversed_p)
27544 g = r->glyphs[TEXT_AREA];
27545 e = g + r->used[TEXT_AREA];
27546 for ( ; e > g; --e)
27547 if (EQ ((e-1)->object, object)
27548 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27549 break;
27550 hlinfo->mouse_face_end_col = e - g;
27552 for (gx = r->x; g < e; ++g)
27553 gx += g->pixel_width;
27554 hlinfo->mouse_face_end_x = gx;
27556 else
27558 e = r->glyphs[TEXT_AREA];
27559 g = e + r->used[TEXT_AREA];
27560 for (gx = r->x ; e < g; ++e)
27562 if (EQ (e->object, object)
27563 && startpos <= e->charpos && e->charpos <= endpos)
27564 break;
27565 gx += e->pixel_width;
27567 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27568 hlinfo->mouse_face_end_x = gx;
27572 #ifdef HAVE_WINDOW_SYSTEM
27574 /* See if position X, Y is within a hot-spot of an image. */
27576 static int
27577 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27579 if (!CONSP (hot_spot))
27580 return 0;
27582 if (EQ (XCAR (hot_spot), Qrect))
27584 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27585 Lisp_Object rect = XCDR (hot_spot);
27586 Lisp_Object tem;
27587 if (!CONSP (rect))
27588 return 0;
27589 if (!CONSP (XCAR (rect)))
27590 return 0;
27591 if (!CONSP (XCDR (rect)))
27592 return 0;
27593 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27594 return 0;
27595 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27596 return 0;
27597 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27598 return 0;
27599 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27600 return 0;
27601 return 1;
27603 else if (EQ (XCAR (hot_spot), Qcircle))
27605 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27606 Lisp_Object circ = XCDR (hot_spot);
27607 Lisp_Object lr, lx0, ly0;
27608 if (CONSP (circ)
27609 && CONSP (XCAR (circ))
27610 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27611 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27612 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27614 double r = XFLOATINT (lr);
27615 double dx = XINT (lx0) - x;
27616 double dy = XINT (ly0) - y;
27617 return (dx * dx + dy * dy <= r * r);
27620 else if (EQ (XCAR (hot_spot), Qpoly))
27622 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27623 if (VECTORP (XCDR (hot_spot)))
27625 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27626 Lisp_Object *poly = v->contents;
27627 ptrdiff_t n = v->header.size;
27628 ptrdiff_t i;
27629 int inside = 0;
27630 Lisp_Object lx, ly;
27631 int x0, y0;
27633 /* Need an even number of coordinates, and at least 3 edges. */
27634 if (n < 6 || n & 1)
27635 return 0;
27637 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27638 If count is odd, we are inside polygon. Pixels on edges
27639 may or may not be included depending on actual geometry of the
27640 polygon. */
27641 if ((lx = poly[n-2], !INTEGERP (lx))
27642 || (ly = poly[n-1], !INTEGERP (lx)))
27643 return 0;
27644 x0 = XINT (lx), y0 = XINT (ly);
27645 for (i = 0; i < n; i += 2)
27647 int x1 = x0, y1 = y0;
27648 if ((lx = poly[i], !INTEGERP (lx))
27649 || (ly = poly[i+1], !INTEGERP (ly)))
27650 return 0;
27651 x0 = XINT (lx), y0 = XINT (ly);
27653 /* Does this segment cross the X line? */
27654 if (x0 >= x)
27656 if (x1 >= x)
27657 continue;
27659 else if (x1 < x)
27660 continue;
27661 if (y > y0 && y > y1)
27662 continue;
27663 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27664 inside = !inside;
27666 return inside;
27669 return 0;
27672 Lisp_Object
27673 find_hot_spot (Lisp_Object map, int x, int y)
27675 while (CONSP (map))
27677 if (CONSP (XCAR (map))
27678 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27679 return XCAR (map);
27680 map = XCDR (map);
27683 return Qnil;
27686 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27687 3, 3, 0,
27688 doc: /* Lookup in image map MAP coordinates X and Y.
27689 An image map is an alist where each element has the format (AREA ID PLIST).
27690 An AREA is specified as either a rectangle, a circle, or a polygon:
27691 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27692 pixel coordinates of the upper left and bottom right corners.
27693 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27694 and the radius of the circle; r may be a float or integer.
27695 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27696 vector describes one corner in the polygon.
27697 Returns the alist element for the first matching AREA in MAP. */)
27698 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27700 if (NILP (map))
27701 return Qnil;
27703 CHECK_NUMBER (x);
27704 CHECK_NUMBER (y);
27706 return find_hot_spot (map,
27707 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27708 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27712 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27713 static void
27714 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27716 /* Do not change cursor shape while dragging mouse. */
27717 if (!NILP (do_mouse_tracking))
27718 return;
27720 if (!NILP (pointer))
27722 if (EQ (pointer, Qarrow))
27723 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27724 else if (EQ (pointer, Qhand))
27725 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27726 else if (EQ (pointer, Qtext))
27727 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27728 else if (EQ (pointer, intern ("hdrag")))
27729 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27730 #ifdef HAVE_X_WINDOWS
27731 else if (EQ (pointer, intern ("vdrag")))
27732 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27733 #endif
27734 else if (EQ (pointer, intern ("hourglass")))
27735 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27736 else if (EQ (pointer, Qmodeline))
27737 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27738 else
27739 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27742 if (cursor != No_Cursor)
27743 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27746 #endif /* HAVE_WINDOW_SYSTEM */
27748 /* Take proper action when mouse has moved to the mode or header line
27749 or marginal area AREA of window W, x-position X and y-position Y.
27750 X is relative to the start of the text display area of W, so the
27751 width of bitmap areas and scroll bars must be subtracted to get a
27752 position relative to the start of the mode line. */
27754 static void
27755 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27756 enum window_part area)
27758 struct window *w = XWINDOW (window);
27759 struct frame *f = XFRAME (w->frame);
27760 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27761 #ifdef HAVE_WINDOW_SYSTEM
27762 Display_Info *dpyinfo;
27763 #endif
27764 Cursor cursor = No_Cursor;
27765 Lisp_Object pointer = Qnil;
27766 int dx, dy, width, height;
27767 ptrdiff_t charpos;
27768 Lisp_Object string, object = Qnil;
27769 Lisp_Object pos IF_LINT (= Qnil), help;
27771 Lisp_Object mouse_face;
27772 int original_x_pixel = x;
27773 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27774 struct glyph_row *row IF_LINT (= 0);
27776 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27778 int x0;
27779 struct glyph *end;
27781 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27782 returns them in row/column units! */
27783 string = mode_line_string (w, area, &x, &y, &charpos,
27784 &object, &dx, &dy, &width, &height);
27786 row = (area == ON_MODE_LINE
27787 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27788 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27790 /* Find the glyph under the mouse pointer. */
27791 if (row->mode_line_p && row->enabled_p)
27793 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27794 end = glyph + row->used[TEXT_AREA];
27796 for (x0 = original_x_pixel;
27797 glyph < end && x0 >= glyph->pixel_width;
27798 ++glyph)
27799 x0 -= glyph->pixel_width;
27801 if (glyph >= end)
27802 glyph = NULL;
27805 else
27807 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27808 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27809 returns them in row/column units! */
27810 string = marginal_area_string (w, area, &x, &y, &charpos,
27811 &object, &dx, &dy, &width, &height);
27814 help = Qnil;
27816 #ifdef HAVE_WINDOW_SYSTEM
27817 if (IMAGEP (object))
27819 Lisp_Object image_map, hotspot;
27820 if ((image_map = Fplist_get (XCDR (object), QCmap),
27821 !NILP (image_map))
27822 && (hotspot = find_hot_spot (image_map, dx, dy),
27823 CONSP (hotspot))
27824 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27826 Lisp_Object plist;
27828 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27829 If so, we could look for mouse-enter, mouse-leave
27830 properties in PLIST (and do something...). */
27831 hotspot = XCDR (hotspot);
27832 if (CONSP (hotspot)
27833 && (plist = XCAR (hotspot), CONSP (plist)))
27835 pointer = Fplist_get (plist, Qpointer);
27836 if (NILP (pointer))
27837 pointer = Qhand;
27838 help = Fplist_get (plist, Qhelp_echo);
27839 if (!NILP (help))
27841 help_echo_string = help;
27842 XSETWINDOW (help_echo_window, w);
27843 help_echo_object = w->contents;
27844 help_echo_pos = charpos;
27848 if (NILP (pointer))
27849 pointer = Fplist_get (XCDR (object), QCpointer);
27851 #endif /* HAVE_WINDOW_SYSTEM */
27853 if (STRINGP (string))
27854 pos = make_number (charpos);
27856 /* Set the help text and mouse pointer. If the mouse is on a part
27857 of the mode line without any text (e.g. past the right edge of
27858 the mode line text), use the default help text and pointer. */
27859 if (STRINGP (string) || area == ON_MODE_LINE)
27861 /* Arrange to display the help by setting the global variables
27862 help_echo_string, help_echo_object, and help_echo_pos. */
27863 if (NILP (help))
27865 if (STRINGP (string))
27866 help = Fget_text_property (pos, Qhelp_echo, string);
27868 if (!NILP (help))
27870 help_echo_string = help;
27871 XSETWINDOW (help_echo_window, w);
27872 help_echo_object = string;
27873 help_echo_pos = charpos;
27875 else if (area == ON_MODE_LINE)
27877 Lisp_Object default_help
27878 = buffer_local_value_1 (Qmode_line_default_help_echo,
27879 w->contents);
27881 if (STRINGP (default_help))
27883 help_echo_string = default_help;
27884 XSETWINDOW (help_echo_window, w);
27885 help_echo_object = Qnil;
27886 help_echo_pos = -1;
27891 #ifdef HAVE_WINDOW_SYSTEM
27892 /* Change the mouse pointer according to what is under it. */
27893 if (FRAME_WINDOW_P (f))
27895 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27896 if (STRINGP (string))
27898 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27900 if (NILP (pointer))
27901 pointer = Fget_text_property (pos, Qpointer, string);
27903 /* Change the mouse pointer according to what is under X/Y. */
27904 if (NILP (pointer)
27905 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27907 Lisp_Object map;
27908 map = Fget_text_property (pos, Qlocal_map, string);
27909 if (!KEYMAPP (map))
27910 map = Fget_text_property (pos, Qkeymap, string);
27911 if (!KEYMAPP (map))
27912 cursor = dpyinfo->vertical_scroll_bar_cursor;
27915 else
27916 /* Default mode-line pointer. */
27917 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27919 #endif
27922 /* Change the mouse face according to what is under X/Y. */
27923 if (STRINGP (string))
27925 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27926 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27927 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27928 && glyph)
27930 Lisp_Object b, e;
27932 struct glyph * tmp_glyph;
27934 int gpos;
27935 int gseq_length;
27936 int total_pixel_width;
27937 ptrdiff_t begpos, endpos, ignore;
27939 int vpos, hpos;
27941 b = Fprevious_single_property_change (make_number (charpos + 1),
27942 Qmouse_face, string, Qnil);
27943 if (NILP (b))
27944 begpos = 0;
27945 else
27946 begpos = XINT (b);
27948 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27949 if (NILP (e))
27950 endpos = SCHARS (string);
27951 else
27952 endpos = XINT (e);
27954 /* Calculate the glyph position GPOS of GLYPH in the
27955 displayed string, relative to the beginning of the
27956 highlighted part of the string.
27958 Note: GPOS is different from CHARPOS. CHARPOS is the
27959 position of GLYPH in the internal string object. A mode
27960 line string format has structures which are converted to
27961 a flattened string by the Emacs Lisp interpreter. The
27962 internal string is an element of those structures. The
27963 displayed string is the flattened string. */
27964 tmp_glyph = row_start_glyph;
27965 while (tmp_glyph < glyph
27966 && (!(EQ (tmp_glyph->object, glyph->object)
27967 && begpos <= tmp_glyph->charpos
27968 && tmp_glyph->charpos < endpos)))
27969 tmp_glyph++;
27970 gpos = glyph - tmp_glyph;
27972 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27973 the highlighted part of the displayed string to which
27974 GLYPH belongs. Note: GSEQ_LENGTH is different from
27975 SCHARS (STRING), because the latter returns the length of
27976 the internal string. */
27977 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27978 tmp_glyph > glyph
27979 && (!(EQ (tmp_glyph->object, glyph->object)
27980 && begpos <= tmp_glyph->charpos
27981 && tmp_glyph->charpos < endpos));
27982 tmp_glyph--)
27984 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27986 /* Calculate the total pixel width of all the glyphs between
27987 the beginning of the highlighted area and GLYPH. */
27988 total_pixel_width = 0;
27989 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27990 total_pixel_width += tmp_glyph->pixel_width;
27992 /* Pre calculation of re-rendering position. Note: X is in
27993 column units here, after the call to mode_line_string or
27994 marginal_area_string. */
27995 hpos = x - gpos;
27996 vpos = (area == ON_MODE_LINE
27997 ? (w->current_matrix)->nrows - 1
27998 : 0);
28000 /* If GLYPH's position is included in the region that is
28001 already drawn in mouse face, we have nothing to do. */
28002 if ( EQ (window, hlinfo->mouse_face_window)
28003 && (!row->reversed_p
28004 ? (hlinfo->mouse_face_beg_col <= hpos
28005 && hpos < hlinfo->mouse_face_end_col)
28006 /* In R2L rows we swap BEG and END, see below. */
28007 : (hlinfo->mouse_face_end_col <= hpos
28008 && hpos < hlinfo->mouse_face_beg_col))
28009 && hlinfo->mouse_face_beg_row == vpos )
28010 return;
28012 if (clear_mouse_face (hlinfo))
28013 cursor = No_Cursor;
28015 if (!row->reversed_p)
28017 hlinfo->mouse_face_beg_col = hpos;
28018 hlinfo->mouse_face_beg_x = original_x_pixel
28019 - (total_pixel_width + dx);
28020 hlinfo->mouse_face_end_col = hpos + gseq_length;
28021 hlinfo->mouse_face_end_x = 0;
28023 else
28025 /* In R2L rows, show_mouse_face expects BEG and END
28026 coordinates to be swapped. */
28027 hlinfo->mouse_face_end_col = hpos;
28028 hlinfo->mouse_face_end_x = original_x_pixel
28029 - (total_pixel_width + dx);
28030 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28031 hlinfo->mouse_face_beg_x = 0;
28034 hlinfo->mouse_face_beg_row = vpos;
28035 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28036 hlinfo->mouse_face_beg_y = 0;
28037 hlinfo->mouse_face_end_y = 0;
28038 hlinfo->mouse_face_past_end = 0;
28039 hlinfo->mouse_face_window = window;
28041 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28042 charpos,
28043 0, 0, 0,
28044 &ignore,
28045 glyph->face_id,
28047 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28049 if (NILP (pointer))
28050 pointer = Qhand;
28052 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28053 clear_mouse_face (hlinfo);
28055 #ifdef HAVE_WINDOW_SYSTEM
28056 if (FRAME_WINDOW_P (f))
28057 define_frame_cursor1 (f, cursor, pointer);
28058 #endif
28062 /* EXPORT:
28063 Take proper action when the mouse has moved to position X, Y on
28064 frame F with regards to highlighting portions of display that have
28065 mouse-face properties. Also de-highlight portions of display where
28066 the mouse was before, set the mouse pointer shape as appropriate
28067 for the mouse coordinates, and activate help echo (tooltips).
28068 X and Y can be negative or out of range. */
28070 void
28071 note_mouse_highlight (struct frame *f, int x, int y)
28073 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28074 enum window_part part = ON_NOTHING;
28075 Lisp_Object window;
28076 struct window *w;
28077 Cursor cursor = No_Cursor;
28078 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28079 struct buffer *b;
28081 /* When a menu is active, don't highlight because this looks odd. */
28082 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28083 if (popup_activated ())
28084 return;
28085 #endif
28087 if (!f->glyphs_initialized_p
28088 || f->pointer_invisible)
28089 return;
28091 hlinfo->mouse_face_mouse_x = x;
28092 hlinfo->mouse_face_mouse_y = y;
28093 hlinfo->mouse_face_mouse_frame = f;
28095 if (hlinfo->mouse_face_defer)
28096 return;
28098 /* Which window is that in? */
28099 window = window_from_coordinates (f, x, y, &part, 1);
28101 /* If displaying active text in another window, clear that. */
28102 if (! EQ (window, hlinfo->mouse_face_window)
28103 /* Also clear if we move out of text area in same window. */
28104 || (!NILP (hlinfo->mouse_face_window)
28105 && !NILP (window)
28106 && part != ON_TEXT
28107 && part != ON_MODE_LINE
28108 && part != ON_HEADER_LINE))
28109 clear_mouse_face (hlinfo);
28111 /* Not on a window -> return. */
28112 if (!WINDOWP (window))
28113 return;
28115 /* Reset help_echo_string. It will get recomputed below. */
28116 help_echo_string = Qnil;
28118 /* Convert to window-relative pixel coordinates. */
28119 w = XWINDOW (window);
28120 frame_to_window_pixel_xy (w, &x, &y);
28122 #ifdef HAVE_WINDOW_SYSTEM
28123 /* Handle tool-bar window differently since it doesn't display a
28124 buffer. */
28125 if (EQ (window, f->tool_bar_window))
28127 note_tool_bar_highlight (f, x, y);
28128 return;
28130 #endif
28132 /* Mouse is on the mode, header line or margin? */
28133 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28134 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28136 note_mode_line_or_margin_highlight (window, x, y, part);
28137 return;
28140 #ifdef HAVE_WINDOW_SYSTEM
28141 if (part == ON_VERTICAL_BORDER)
28143 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28144 help_echo_string = build_string ("drag-mouse-1: resize");
28146 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28147 || part == ON_SCROLL_BAR)
28148 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28149 else
28150 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28151 #endif
28153 /* Are we in a window whose display is up to date?
28154 And verify the buffer's text has not changed. */
28155 b = XBUFFER (w->contents);
28156 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28158 int hpos, vpos, dx, dy, area = LAST_AREA;
28159 ptrdiff_t pos;
28160 struct glyph *glyph;
28161 Lisp_Object object;
28162 Lisp_Object mouse_face = Qnil, position;
28163 Lisp_Object *overlay_vec = NULL;
28164 ptrdiff_t i, noverlays;
28165 struct buffer *obuf;
28166 ptrdiff_t obegv, ozv;
28167 int same_region;
28169 /* Find the glyph under X/Y. */
28170 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28172 #ifdef HAVE_WINDOW_SYSTEM
28173 /* Look for :pointer property on image. */
28174 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28176 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28177 if (img != NULL && IMAGEP (img->spec))
28179 Lisp_Object image_map, hotspot;
28180 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28181 !NILP (image_map))
28182 && (hotspot = find_hot_spot (image_map,
28183 glyph->slice.img.x + dx,
28184 glyph->slice.img.y + dy),
28185 CONSP (hotspot))
28186 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28188 Lisp_Object plist;
28190 /* Could check XCAR (hotspot) to see if we enter/leave
28191 this hot-spot.
28192 If so, we could look for mouse-enter, mouse-leave
28193 properties in PLIST (and do something...). */
28194 hotspot = XCDR (hotspot);
28195 if (CONSP (hotspot)
28196 && (plist = XCAR (hotspot), CONSP (plist)))
28198 pointer = Fplist_get (plist, Qpointer);
28199 if (NILP (pointer))
28200 pointer = Qhand;
28201 help_echo_string = Fplist_get (plist, Qhelp_echo);
28202 if (!NILP (help_echo_string))
28204 help_echo_window = window;
28205 help_echo_object = glyph->object;
28206 help_echo_pos = glyph->charpos;
28210 if (NILP (pointer))
28211 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28214 #endif /* HAVE_WINDOW_SYSTEM */
28216 /* Clear mouse face if X/Y not over text. */
28217 if (glyph == NULL
28218 || area != TEXT_AREA
28219 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28220 /* Glyph's OBJECT is an integer for glyphs inserted by the
28221 display engine for its internal purposes, like truncation
28222 and continuation glyphs and blanks beyond the end of
28223 line's text on text terminals. If we are over such a
28224 glyph, we are not over any text. */
28225 || INTEGERP (glyph->object)
28226 /* R2L rows have a stretch glyph at their front, which
28227 stands for no text, whereas L2R rows have no glyphs at
28228 all beyond the end of text. Treat such stretch glyphs
28229 like we do with NULL glyphs in L2R rows. */
28230 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28231 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28232 && glyph->type == STRETCH_GLYPH
28233 && glyph->avoid_cursor_p))
28235 if (clear_mouse_face (hlinfo))
28236 cursor = No_Cursor;
28237 #ifdef HAVE_WINDOW_SYSTEM
28238 if (FRAME_WINDOW_P (f) && NILP (pointer))
28240 if (area != TEXT_AREA)
28241 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28242 else
28243 pointer = Vvoid_text_area_pointer;
28245 #endif
28246 goto set_cursor;
28249 pos = glyph->charpos;
28250 object = glyph->object;
28251 if (!STRINGP (object) && !BUFFERP (object))
28252 goto set_cursor;
28254 /* If we get an out-of-range value, return now; avoid an error. */
28255 if (BUFFERP (object) && pos > BUF_Z (b))
28256 goto set_cursor;
28258 /* Make the window's buffer temporarily current for
28259 overlays_at and compute_char_face. */
28260 obuf = current_buffer;
28261 current_buffer = b;
28262 obegv = BEGV;
28263 ozv = ZV;
28264 BEGV = BEG;
28265 ZV = Z;
28267 /* Is this char mouse-active or does it have help-echo? */
28268 position = make_number (pos);
28270 if (BUFFERP (object))
28272 /* Put all the overlays we want in a vector in overlay_vec. */
28273 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28274 /* Sort overlays into increasing priority order. */
28275 noverlays = sort_overlays (overlay_vec, noverlays, w);
28277 else
28278 noverlays = 0;
28280 if (NILP (Vmouse_highlight))
28282 clear_mouse_face (hlinfo);
28283 goto check_help_echo;
28286 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28288 if (same_region)
28289 cursor = No_Cursor;
28291 /* Check mouse-face highlighting. */
28292 if (! same_region
28293 /* If there exists an overlay with mouse-face overlapping
28294 the one we are currently highlighting, we have to
28295 check if we enter the overlapping overlay, and then
28296 highlight only that. */
28297 || (OVERLAYP (hlinfo->mouse_face_overlay)
28298 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28300 /* Find the highest priority overlay with a mouse-face. */
28301 Lisp_Object overlay = Qnil;
28302 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28304 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28305 if (!NILP (mouse_face))
28306 overlay = overlay_vec[i];
28309 /* If we're highlighting the same overlay as before, there's
28310 no need to do that again. */
28311 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28312 goto check_help_echo;
28313 hlinfo->mouse_face_overlay = overlay;
28315 /* Clear the display of the old active region, if any. */
28316 if (clear_mouse_face (hlinfo))
28317 cursor = No_Cursor;
28319 /* If no overlay applies, get a text property. */
28320 if (NILP (overlay))
28321 mouse_face = Fget_text_property (position, Qmouse_face, object);
28323 /* Next, compute the bounds of the mouse highlighting and
28324 display it. */
28325 if (!NILP (mouse_face) && STRINGP (object))
28327 /* The mouse-highlighting comes from a display string
28328 with a mouse-face. */
28329 Lisp_Object s, e;
28330 ptrdiff_t ignore;
28332 s = Fprevious_single_property_change
28333 (make_number (pos + 1), Qmouse_face, object, Qnil);
28334 e = Fnext_single_property_change
28335 (position, Qmouse_face, object, Qnil);
28336 if (NILP (s))
28337 s = make_number (0);
28338 if (NILP (e))
28339 e = make_number (SCHARS (object) - 1);
28340 mouse_face_from_string_pos (w, hlinfo, object,
28341 XINT (s), XINT (e));
28342 hlinfo->mouse_face_past_end = 0;
28343 hlinfo->mouse_face_window = window;
28344 hlinfo->mouse_face_face_id
28345 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28346 glyph->face_id, 1);
28347 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28348 cursor = No_Cursor;
28350 else
28352 /* The mouse-highlighting, if any, comes from an overlay
28353 or text property in the buffer. */
28354 Lisp_Object buffer IF_LINT (= Qnil);
28355 Lisp_Object disp_string IF_LINT (= Qnil);
28357 if (STRINGP (object))
28359 /* If we are on a display string with no mouse-face,
28360 check if the text under it has one. */
28361 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28362 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28363 pos = string_buffer_position (object, start);
28364 if (pos > 0)
28366 mouse_face = get_char_property_and_overlay
28367 (make_number (pos), Qmouse_face, w->contents, &overlay);
28368 buffer = w->contents;
28369 disp_string = object;
28372 else
28374 buffer = object;
28375 disp_string = Qnil;
28378 if (!NILP (mouse_face))
28380 Lisp_Object before, after;
28381 Lisp_Object before_string, after_string;
28382 /* To correctly find the limits of mouse highlight
28383 in a bidi-reordered buffer, we must not use the
28384 optimization of limiting the search in
28385 previous-single-property-change and
28386 next-single-property-change, because
28387 rows_from_pos_range needs the real start and end
28388 positions to DTRT in this case. That's because
28389 the first row visible in a window does not
28390 necessarily display the character whose position
28391 is the smallest. */
28392 Lisp_Object lim1 =
28393 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28394 ? Fmarker_position (w->start)
28395 : Qnil;
28396 Lisp_Object lim2 =
28397 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28398 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28399 : Qnil;
28401 if (NILP (overlay))
28403 /* Handle the text property case. */
28404 before = Fprevious_single_property_change
28405 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28406 after = Fnext_single_property_change
28407 (make_number (pos), Qmouse_face, buffer, lim2);
28408 before_string = after_string = Qnil;
28410 else
28412 /* Handle the overlay case. */
28413 before = Foverlay_start (overlay);
28414 after = Foverlay_end (overlay);
28415 before_string = Foverlay_get (overlay, Qbefore_string);
28416 after_string = Foverlay_get (overlay, Qafter_string);
28418 if (!STRINGP (before_string)) before_string = Qnil;
28419 if (!STRINGP (after_string)) after_string = Qnil;
28422 mouse_face_from_buffer_pos (window, hlinfo, pos,
28423 NILP (before)
28425 : XFASTINT (before),
28426 NILP (after)
28427 ? BUF_Z (XBUFFER (buffer))
28428 : XFASTINT (after),
28429 before_string, after_string,
28430 disp_string);
28431 cursor = No_Cursor;
28436 check_help_echo:
28438 /* Look for a `help-echo' property. */
28439 if (NILP (help_echo_string)) {
28440 Lisp_Object help, overlay;
28442 /* Check overlays first. */
28443 help = overlay = Qnil;
28444 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28446 overlay = overlay_vec[i];
28447 help = Foverlay_get (overlay, Qhelp_echo);
28450 if (!NILP (help))
28452 help_echo_string = help;
28453 help_echo_window = window;
28454 help_echo_object = overlay;
28455 help_echo_pos = pos;
28457 else
28459 Lisp_Object obj = glyph->object;
28460 ptrdiff_t charpos = glyph->charpos;
28462 /* Try text properties. */
28463 if (STRINGP (obj)
28464 && charpos >= 0
28465 && charpos < SCHARS (obj))
28467 help = Fget_text_property (make_number (charpos),
28468 Qhelp_echo, obj);
28469 if (NILP (help))
28471 /* If the string itself doesn't specify a help-echo,
28472 see if the buffer text ``under'' it does. */
28473 struct glyph_row *r
28474 = MATRIX_ROW (w->current_matrix, vpos);
28475 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28476 ptrdiff_t p = string_buffer_position (obj, start);
28477 if (p > 0)
28479 help = Fget_char_property (make_number (p),
28480 Qhelp_echo, w->contents);
28481 if (!NILP (help))
28483 charpos = p;
28484 obj = w->contents;
28489 else if (BUFFERP (obj)
28490 && charpos >= BEGV
28491 && charpos < ZV)
28492 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28493 obj);
28495 if (!NILP (help))
28497 help_echo_string = help;
28498 help_echo_window = window;
28499 help_echo_object = obj;
28500 help_echo_pos = charpos;
28505 #ifdef HAVE_WINDOW_SYSTEM
28506 /* Look for a `pointer' property. */
28507 if (FRAME_WINDOW_P (f) && NILP (pointer))
28509 /* Check overlays first. */
28510 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28511 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28513 if (NILP (pointer))
28515 Lisp_Object obj = glyph->object;
28516 ptrdiff_t charpos = glyph->charpos;
28518 /* Try text properties. */
28519 if (STRINGP (obj)
28520 && charpos >= 0
28521 && charpos < SCHARS (obj))
28523 pointer = Fget_text_property (make_number (charpos),
28524 Qpointer, obj);
28525 if (NILP (pointer))
28527 /* If the string itself doesn't specify a pointer,
28528 see if the buffer text ``under'' it does. */
28529 struct glyph_row *r
28530 = MATRIX_ROW (w->current_matrix, vpos);
28531 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28532 ptrdiff_t p = string_buffer_position (obj, start);
28533 if (p > 0)
28534 pointer = Fget_char_property (make_number (p),
28535 Qpointer, w->contents);
28538 else if (BUFFERP (obj)
28539 && charpos >= BEGV
28540 && charpos < ZV)
28541 pointer = Fget_text_property (make_number (charpos),
28542 Qpointer, obj);
28545 #endif /* HAVE_WINDOW_SYSTEM */
28547 BEGV = obegv;
28548 ZV = ozv;
28549 current_buffer = obuf;
28552 set_cursor:
28554 #ifdef HAVE_WINDOW_SYSTEM
28555 if (FRAME_WINDOW_P (f))
28556 define_frame_cursor1 (f, cursor, pointer);
28557 #else
28558 /* This is here to prevent a compiler error, about "label at end of
28559 compound statement". */
28560 return;
28561 #endif
28565 /* EXPORT for RIF:
28566 Clear any mouse-face on window W. This function is part of the
28567 redisplay interface, and is called from try_window_id and similar
28568 functions to ensure the mouse-highlight is off. */
28570 void
28571 x_clear_window_mouse_face (struct window *w)
28573 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28574 Lisp_Object window;
28576 block_input ();
28577 XSETWINDOW (window, w);
28578 if (EQ (window, hlinfo->mouse_face_window))
28579 clear_mouse_face (hlinfo);
28580 unblock_input ();
28584 /* EXPORT:
28585 Just discard the mouse face information for frame F, if any.
28586 This is used when the size of F is changed. */
28588 void
28589 cancel_mouse_face (struct frame *f)
28591 Lisp_Object window;
28592 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28594 window = hlinfo->mouse_face_window;
28595 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28597 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28598 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28599 hlinfo->mouse_face_window = Qnil;
28605 /***********************************************************************
28606 Exposure Events
28607 ***********************************************************************/
28609 #ifdef HAVE_WINDOW_SYSTEM
28611 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28612 which intersects rectangle R. R is in window-relative coordinates. */
28614 static void
28615 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28616 enum glyph_row_area area)
28618 struct glyph *first = row->glyphs[area];
28619 struct glyph *end = row->glyphs[area] + row->used[area];
28620 struct glyph *last;
28621 int first_x, start_x, x;
28623 if (area == TEXT_AREA && row->fill_line_p)
28624 /* If row extends face to end of line write the whole line. */
28625 draw_glyphs (w, 0, row, area,
28626 0, row->used[area],
28627 DRAW_NORMAL_TEXT, 0);
28628 else
28630 /* Set START_X to the window-relative start position for drawing glyphs of
28631 AREA. The first glyph of the text area can be partially visible.
28632 The first glyphs of other areas cannot. */
28633 start_x = window_box_left_offset (w, area);
28634 x = start_x;
28635 if (area == TEXT_AREA)
28636 x += row->x;
28638 /* Find the first glyph that must be redrawn. */
28639 while (first < end
28640 && x + first->pixel_width < r->x)
28642 x += first->pixel_width;
28643 ++first;
28646 /* Find the last one. */
28647 last = first;
28648 first_x = x;
28649 while (last < end
28650 && x < r->x + r->width)
28652 x += last->pixel_width;
28653 ++last;
28656 /* Repaint. */
28657 if (last > first)
28658 draw_glyphs (w, first_x - start_x, row, area,
28659 first - row->glyphs[area], last - row->glyphs[area],
28660 DRAW_NORMAL_TEXT, 0);
28665 /* Redraw the parts of the glyph row ROW on window W intersecting
28666 rectangle R. R is in window-relative coordinates. Value is
28667 non-zero if mouse-face was overwritten. */
28669 static int
28670 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28672 eassert (row->enabled_p);
28674 if (row->mode_line_p || w->pseudo_window_p)
28675 draw_glyphs (w, 0, row, TEXT_AREA,
28676 0, row->used[TEXT_AREA],
28677 DRAW_NORMAL_TEXT, 0);
28678 else
28680 if (row->used[LEFT_MARGIN_AREA])
28681 expose_area (w, row, r, LEFT_MARGIN_AREA);
28682 if (row->used[TEXT_AREA])
28683 expose_area (w, row, r, TEXT_AREA);
28684 if (row->used[RIGHT_MARGIN_AREA])
28685 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28686 draw_row_fringe_bitmaps (w, row);
28689 return row->mouse_face_p;
28693 /* Redraw those parts of glyphs rows during expose event handling that
28694 overlap other rows. Redrawing of an exposed line writes over parts
28695 of lines overlapping that exposed line; this function fixes that.
28697 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28698 row in W's current matrix that is exposed and overlaps other rows.
28699 LAST_OVERLAPPING_ROW is the last such row. */
28701 static void
28702 expose_overlaps (struct window *w,
28703 struct glyph_row *first_overlapping_row,
28704 struct glyph_row *last_overlapping_row,
28705 XRectangle *r)
28707 struct glyph_row *row;
28709 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28710 if (row->overlapping_p)
28712 eassert (row->enabled_p && !row->mode_line_p);
28714 row->clip = r;
28715 if (row->used[LEFT_MARGIN_AREA])
28716 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28718 if (row->used[TEXT_AREA])
28719 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28721 if (row->used[RIGHT_MARGIN_AREA])
28722 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28723 row->clip = NULL;
28728 /* Return non-zero if W's cursor intersects rectangle R. */
28730 static int
28731 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28733 XRectangle cr, result;
28734 struct glyph *cursor_glyph;
28735 struct glyph_row *row;
28737 if (w->phys_cursor.vpos >= 0
28738 && w->phys_cursor.vpos < w->current_matrix->nrows
28739 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28740 row->enabled_p)
28741 && row->cursor_in_fringe_p)
28743 /* Cursor is in the fringe. */
28744 cr.x = window_box_right_offset (w,
28745 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28746 ? RIGHT_MARGIN_AREA
28747 : TEXT_AREA));
28748 cr.y = row->y;
28749 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28750 cr.height = row->height;
28751 return x_intersect_rectangles (&cr, r, &result);
28754 cursor_glyph = get_phys_cursor_glyph (w);
28755 if (cursor_glyph)
28757 /* r is relative to W's box, but w->phys_cursor.x is relative
28758 to left edge of W's TEXT area. Adjust it. */
28759 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28760 cr.y = w->phys_cursor.y;
28761 cr.width = cursor_glyph->pixel_width;
28762 cr.height = w->phys_cursor_height;
28763 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28764 I assume the effect is the same -- and this is portable. */
28765 return x_intersect_rectangles (&cr, r, &result);
28767 /* If we don't understand the format, pretend we're not in the hot-spot. */
28768 return 0;
28772 /* EXPORT:
28773 Draw a vertical window border to the right of window W if W doesn't
28774 have vertical scroll bars. */
28776 void
28777 x_draw_vertical_border (struct window *w)
28779 struct frame *f = XFRAME (WINDOW_FRAME (w));
28781 /* We could do better, if we knew what type of scroll-bar the adjacent
28782 windows (on either side) have... But we don't :-(
28783 However, I think this works ok. ++KFS 2003-04-25 */
28785 /* Redraw borders between horizontally adjacent windows. Don't
28786 do it for frames with vertical scroll bars because either the
28787 right scroll bar of a window, or the left scroll bar of its
28788 neighbor will suffice as a border. */
28789 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28790 return;
28792 /* Note: It is necessary to redraw both the left and the right
28793 borders, for when only this single window W is being
28794 redisplayed. */
28795 if (!WINDOW_RIGHTMOST_P (w)
28796 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28798 int x0, x1, y0, y1;
28800 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28801 y1 -= 1;
28803 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28804 x1 -= 1;
28806 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28808 if (!WINDOW_LEFTMOST_P (w)
28809 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28811 int x0, x1, y0, y1;
28813 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28814 y1 -= 1;
28816 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28817 x0 -= 1;
28819 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28824 /* Redraw the part of window W intersection rectangle FR. Pixel
28825 coordinates in FR are frame-relative. Call this function with
28826 input blocked. Value is non-zero if the exposure overwrites
28827 mouse-face. */
28829 static int
28830 expose_window (struct window *w, XRectangle *fr)
28832 struct frame *f = XFRAME (w->frame);
28833 XRectangle wr, r;
28834 int mouse_face_overwritten_p = 0;
28836 /* If window is not yet fully initialized, do nothing. This can
28837 happen when toolkit scroll bars are used and a window is split.
28838 Reconfiguring the scroll bar will generate an expose for a newly
28839 created window. */
28840 if (w->current_matrix == NULL)
28841 return 0;
28843 /* When we're currently updating the window, display and current
28844 matrix usually don't agree. Arrange for a thorough display
28845 later. */
28846 if (w->must_be_updated_p)
28848 SET_FRAME_GARBAGED (f);
28849 return 0;
28852 /* Frame-relative pixel rectangle of W. */
28853 wr.x = WINDOW_LEFT_EDGE_X (w);
28854 wr.y = WINDOW_TOP_EDGE_Y (w);
28855 wr.width = WINDOW_TOTAL_WIDTH (w);
28856 wr.height = WINDOW_TOTAL_HEIGHT (w);
28858 if (x_intersect_rectangles (fr, &wr, &r))
28860 int yb = window_text_bottom_y (w);
28861 struct glyph_row *row;
28862 int cursor_cleared_p, phys_cursor_on_p;
28863 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28865 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28866 r.x, r.y, r.width, r.height));
28868 /* Convert to window coordinates. */
28869 r.x -= WINDOW_LEFT_EDGE_X (w);
28870 r.y -= WINDOW_TOP_EDGE_Y (w);
28872 /* Turn off the cursor. */
28873 if (!w->pseudo_window_p
28874 && phys_cursor_in_rect_p (w, &r))
28876 x_clear_cursor (w);
28877 cursor_cleared_p = 1;
28879 else
28880 cursor_cleared_p = 0;
28882 /* If the row containing the cursor extends face to end of line,
28883 then expose_area might overwrite the cursor outside the
28884 rectangle and thus notice_overwritten_cursor might clear
28885 w->phys_cursor_on_p. We remember the original value and
28886 check later if it is changed. */
28887 phys_cursor_on_p = w->phys_cursor_on_p;
28889 /* Update lines intersecting rectangle R. */
28890 first_overlapping_row = last_overlapping_row = NULL;
28891 for (row = w->current_matrix->rows;
28892 row->enabled_p;
28893 ++row)
28895 int y0 = row->y;
28896 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28898 if ((y0 >= r.y && y0 < r.y + r.height)
28899 || (y1 > r.y && y1 < r.y + r.height)
28900 || (r.y >= y0 && r.y < y1)
28901 || (r.y + r.height > y0 && r.y + r.height < y1))
28903 /* A header line may be overlapping, but there is no need
28904 to fix overlapping areas for them. KFS 2005-02-12 */
28905 if (row->overlapping_p && !row->mode_line_p)
28907 if (first_overlapping_row == NULL)
28908 first_overlapping_row = row;
28909 last_overlapping_row = row;
28912 row->clip = fr;
28913 if (expose_line (w, row, &r))
28914 mouse_face_overwritten_p = 1;
28915 row->clip = NULL;
28917 else if (row->overlapping_p)
28919 /* We must redraw a row overlapping the exposed area. */
28920 if (y0 < r.y
28921 ? y0 + row->phys_height > r.y
28922 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28924 if (first_overlapping_row == NULL)
28925 first_overlapping_row = row;
28926 last_overlapping_row = row;
28930 if (y1 >= yb)
28931 break;
28934 /* Display the mode line if there is one. */
28935 if (WINDOW_WANTS_MODELINE_P (w)
28936 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28937 row->enabled_p)
28938 && row->y < r.y + r.height)
28940 if (expose_line (w, row, &r))
28941 mouse_face_overwritten_p = 1;
28944 if (!w->pseudo_window_p)
28946 /* Fix the display of overlapping rows. */
28947 if (first_overlapping_row)
28948 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28949 fr);
28951 /* Draw border between windows. */
28952 x_draw_vertical_border (w);
28954 /* Turn the cursor on again. */
28955 if (cursor_cleared_p
28956 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28957 update_window_cursor (w, 1);
28961 return mouse_face_overwritten_p;
28966 /* Redraw (parts) of all windows in the window tree rooted at W that
28967 intersect R. R contains frame pixel coordinates. Value is
28968 non-zero if the exposure overwrites mouse-face. */
28970 static int
28971 expose_window_tree (struct window *w, XRectangle *r)
28973 struct frame *f = XFRAME (w->frame);
28974 int mouse_face_overwritten_p = 0;
28976 while (w && !FRAME_GARBAGED_P (f))
28978 if (WINDOWP (w->contents))
28979 mouse_face_overwritten_p
28980 |= expose_window_tree (XWINDOW (w->contents), r);
28981 else
28982 mouse_face_overwritten_p |= expose_window (w, r);
28984 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28987 return mouse_face_overwritten_p;
28991 /* EXPORT:
28992 Redisplay an exposed area of frame F. X and Y are the upper-left
28993 corner of the exposed rectangle. W and H are width and height of
28994 the exposed area. All are pixel values. W or H zero means redraw
28995 the entire frame. */
28997 void
28998 expose_frame (struct frame *f, int x, int y, int w, int h)
29000 XRectangle r;
29001 int mouse_face_overwritten_p = 0;
29003 TRACE ((stderr, "expose_frame "));
29005 /* No need to redraw if frame will be redrawn soon. */
29006 if (FRAME_GARBAGED_P (f))
29008 TRACE ((stderr, " garbaged\n"));
29009 return;
29012 /* If basic faces haven't been realized yet, there is no point in
29013 trying to redraw anything. This can happen when we get an expose
29014 event while Emacs is starting, e.g. by moving another window. */
29015 if (FRAME_FACE_CACHE (f) == NULL
29016 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29018 TRACE ((stderr, " no faces\n"));
29019 return;
29022 if (w == 0 || h == 0)
29024 r.x = r.y = 0;
29025 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29026 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29028 else
29030 r.x = x;
29031 r.y = y;
29032 r.width = w;
29033 r.height = h;
29036 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29037 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29039 if (WINDOWP (f->tool_bar_window))
29040 mouse_face_overwritten_p
29041 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29043 #ifdef HAVE_X_WINDOWS
29044 #ifndef MSDOS
29045 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29046 if (WINDOWP (f->menu_bar_window))
29047 mouse_face_overwritten_p
29048 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29049 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29050 #endif
29051 #endif
29053 /* Some window managers support a focus-follows-mouse style with
29054 delayed raising of frames. Imagine a partially obscured frame,
29055 and moving the mouse into partially obscured mouse-face on that
29056 frame. The visible part of the mouse-face will be highlighted,
29057 then the WM raises the obscured frame. With at least one WM, KDE
29058 2.1, Emacs is not getting any event for the raising of the frame
29059 (even tried with SubstructureRedirectMask), only Expose events.
29060 These expose events will draw text normally, i.e. not
29061 highlighted. Which means we must redo the highlight here.
29062 Subsume it under ``we love X''. --gerd 2001-08-15 */
29063 /* Included in Windows version because Windows most likely does not
29064 do the right thing if any third party tool offers
29065 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29066 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29068 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29069 if (f == hlinfo->mouse_face_mouse_frame)
29071 int mouse_x = hlinfo->mouse_face_mouse_x;
29072 int mouse_y = hlinfo->mouse_face_mouse_y;
29073 clear_mouse_face (hlinfo);
29074 note_mouse_highlight (f, mouse_x, mouse_y);
29080 /* EXPORT:
29081 Determine the intersection of two rectangles R1 and R2. Return
29082 the intersection in *RESULT. Value is non-zero if RESULT is not
29083 empty. */
29086 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29088 XRectangle *left, *right;
29089 XRectangle *upper, *lower;
29090 int intersection_p = 0;
29092 /* Rearrange so that R1 is the left-most rectangle. */
29093 if (r1->x < r2->x)
29094 left = r1, right = r2;
29095 else
29096 left = r2, right = r1;
29098 /* X0 of the intersection is right.x0, if this is inside R1,
29099 otherwise there is no intersection. */
29100 if (right->x <= left->x + left->width)
29102 result->x = right->x;
29104 /* The right end of the intersection is the minimum of
29105 the right ends of left and right. */
29106 result->width = (min (left->x + left->width, right->x + right->width)
29107 - result->x);
29109 /* Same game for Y. */
29110 if (r1->y < r2->y)
29111 upper = r1, lower = r2;
29112 else
29113 upper = r2, lower = r1;
29115 /* The upper end of the intersection is lower.y0, if this is inside
29116 of upper. Otherwise, there is no intersection. */
29117 if (lower->y <= upper->y + upper->height)
29119 result->y = lower->y;
29121 /* The lower end of the intersection is the minimum of the lower
29122 ends of upper and lower. */
29123 result->height = (min (lower->y + lower->height,
29124 upper->y + upper->height)
29125 - result->y);
29126 intersection_p = 1;
29130 return intersection_p;
29133 #endif /* HAVE_WINDOW_SYSTEM */
29136 /***********************************************************************
29137 Initialization
29138 ***********************************************************************/
29140 void
29141 syms_of_xdisp (void)
29143 Vwith_echo_area_save_vector = Qnil;
29144 staticpro (&Vwith_echo_area_save_vector);
29146 Vmessage_stack = Qnil;
29147 staticpro (&Vmessage_stack);
29149 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29150 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29152 message_dolog_marker1 = Fmake_marker ();
29153 staticpro (&message_dolog_marker1);
29154 message_dolog_marker2 = Fmake_marker ();
29155 staticpro (&message_dolog_marker2);
29156 message_dolog_marker3 = Fmake_marker ();
29157 staticpro (&message_dolog_marker3);
29159 #ifdef GLYPH_DEBUG
29160 defsubr (&Sdump_frame_glyph_matrix);
29161 defsubr (&Sdump_glyph_matrix);
29162 defsubr (&Sdump_glyph_row);
29163 defsubr (&Sdump_tool_bar_row);
29164 defsubr (&Strace_redisplay);
29165 defsubr (&Strace_to_stderr);
29166 #endif
29167 #ifdef HAVE_WINDOW_SYSTEM
29168 defsubr (&Stool_bar_lines_needed);
29169 defsubr (&Slookup_image_map);
29170 #endif
29171 defsubr (&Sline_pixel_height);
29172 defsubr (&Sformat_mode_line);
29173 defsubr (&Sinvisible_p);
29174 defsubr (&Scurrent_bidi_paragraph_direction);
29175 defsubr (&Smove_point_visually);
29177 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29178 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29179 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29180 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29181 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29182 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29183 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29184 DEFSYM (Qeval, "eval");
29185 DEFSYM (QCdata, ":data");
29186 DEFSYM (Qdisplay, "display");
29187 DEFSYM (Qspace_width, "space-width");
29188 DEFSYM (Qraise, "raise");
29189 DEFSYM (Qslice, "slice");
29190 DEFSYM (Qspace, "space");
29191 DEFSYM (Qmargin, "margin");
29192 DEFSYM (Qpointer, "pointer");
29193 DEFSYM (Qleft_margin, "left-margin");
29194 DEFSYM (Qright_margin, "right-margin");
29195 DEFSYM (Qcenter, "center");
29196 DEFSYM (Qline_height, "line-height");
29197 DEFSYM (QCalign_to, ":align-to");
29198 DEFSYM (QCrelative_width, ":relative-width");
29199 DEFSYM (QCrelative_height, ":relative-height");
29200 DEFSYM (QCeval, ":eval");
29201 DEFSYM (QCpropertize, ":propertize");
29202 DEFSYM (QCfile, ":file");
29203 DEFSYM (Qfontified, "fontified");
29204 DEFSYM (Qfontification_functions, "fontification-functions");
29205 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29206 DEFSYM (Qescape_glyph, "escape-glyph");
29207 DEFSYM (Qnobreak_space, "nobreak-space");
29208 DEFSYM (Qimage, "image");
29209 DEFSYM (Qtext, "text");
29210 DEFSYM (Qboth, "both");
29211 DEFSYM (Qboth_horiz, "both-horiz");
29212 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29213 DEFSYM (QCmap, ":map");
29214 DEFSYM (QCpointer, ":pointer");
29215 DEFSYM (Qrect, "rect");
29216 DEFSYM (Qcircle, "circle");
29217 DEFSYM (Qpoly, "poly");
29218 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29219 DEFSYM (Qgrow_only, "grow-only");
29220 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29221 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29222 DEFSYM (Qposition, "position");
29223 DEFSYM (Qbuffer_position, "buffer-position");
29224 DEFSYM (Qobject, "object");
29225 DEFSYM (Qbar, "bar");
29226 DEFSYM (Qhbar, "hbar");
29227 DEFSYM (Qbox, "box");
29228 DEFSYM (Qhollow, "hollow");
29229 DEFSYM (Qhand, "hand");
29230 DEFSYM (Qarrow, "arrow");
29231 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29233 list_of_error = list1 (list2 (intern_c_string ("error"),
29234 intern_c_string ("void-variable")));
29235 staticpro (&list_of_error);
29237 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29238 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29239 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29240 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29242 echo_buffer[0] = echo_buffer[1] = Qnil;
29243 staticpro (&echo_buffer[0]);
29244 staticpro (&echo_buffer[1]);
29246 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29247 staticpro (&echo_area_buffer[0]);
29248 staticpro (&echo_area_buffer[1]);
29250 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29251 staticpro (&Vmessages_buffer_name);
29253 mode_line_proptrans_alist = Qnil;
29254 staticpro (&mode_line_proptrans_alist);
29255 mode_line_string_list = Qnil;
29256 staticpro (&mode_line_string_list);
29257 mode_line_string_face = Qnil;
29258 staticpro (&mode_line_string_face);
29259 mode_line_string_face_prop = Qnil;
29260 staticpro (&mode_line_string_face_prop);
29261 Vmode_line_unwind_vector = Qnil;
29262 staticpro (&Vmode_line_unwind_vector);
29264 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29266 help_echo_string = Qnil;
29267 staticpro (&help_echo_string);
29268 help_echo_object = Qnil;
29269 staticpro (&help_echo_object);
29270 help_echo_window = Qnil;
29271 staticpro (&help_echo_window);
29272 previous_help_echo_string = Qnil;
29273 staticpro (&previous_help_echo_string);
29274 help_echo_pos = -1;
29276 DEFSYM (Qright_to_left, "right-to-left");
29277 DEFSYM (Qleft_to_right, "left-to-right");
29279 #ifdef HAVE_WINDOW_SYSTEM
29280 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29281 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29282 For example, if a block cursor is over a tab, it will be drawn as
29283 wide as that tab on the display. */);
29284 x_stretch_cursor_p = 0;
29285 #endif
29287 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29288 doc: /* Non-nil means highlight trailing whitespace.
29289 The face used for trailing whitespace is `trailing-whitespace'. */);
29290 Vshow_trailing_whitespace = Qnil;
29292 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29293 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29294 If the value is t, Emacs highlights non-ASCII chars which have the
29295 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29296 or `escape-glyph' face respectively.
29298 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29299 U+2011 (non-breaking hyphen) are affected.
29301 Any other non-nil value means to display these characters as a escape
29302 glyph followed by an ordinary space or hyphen.
29304 A value of nil means no special handling of these characters. */);
29305 Vnobreak_char_display = Qt;
29307 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29308 doc: /* The pointer shape to show in void text areas.
29309 A value of nil means to show the text pointer. Other options are `arrow',
29310 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29311 Vvoid_text_area_pointer = Qarrow;
29313 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29314 doc: /* Non-nil means don't actually do any redisplay.
29315 This is used for internal purposes. */);
29316 Vinhibit_redisplay = Qnil;
29318 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29319 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29320 Vglobal_mode_string = Qnil;
29322 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29323 doc: /* Marker for where to display an arrow on top of the buffer text.
29324 This must be the beginning of a line in order to work.
29325 See also `overlay-arrow-string'. */);
29326 Voverlay_arrow_position = Qnil;
29328 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29329 doc: /* String to display as an arrow in non-window frames.
29330 See also `overlay-arrow-position'. */);
29331 Voverlay_arrow_string = build_pure_c_string ("=>");
29333 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29334 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29335 The symbols on this list are examined during redisplay to determine
29336 where to display overlay arrows. */);
29337 Voverlay_arrow_variable_list
29338 = list1 (intern_c_string ("overlay-arrow-position"));
29340 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29341 doc: /* The number of lines to try scrolling a window by when point moves out.
29342 If that fails to bring point back on frame, point is centered instead.
29343 If this is zero, point is always centered after it moves off frame.
29344 If you want scrolling to always be a line at a time, you should set
29345 `scroll-conservatively' to a large value rather than set this to 1. */);
29347 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29348 doc: /* Scroll up to this many lines, to bring point back on screen.
29349 If point moves off-screen, redisplay will scroll by up to
29350 `scroll-conservatively' lines in order to bring point just barely
29351 onto the screen again. If that cannot be done, then redisplay
29352 recenters point as usual.
29354 If the value is greater than 100, redisplay will never recenter point,
29355 but will always scroll just enough text to bring point into view, even
29356 if you move far away.
29358 A value of zero means always recenter point if it moves off screen. */);
29359 scroll_conservatively = 0;
29361 DEFVAR_INT ("scroll-margin", scroll_margin,
29362 doc: /* Number of lines of margin at the top and bottom of a window.
29363 Recenter the window whenever point gets within this many lines
29364 of the top or bottom of the window. */);
29365 scroll_margin = 0;
29367 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29368 doc: /* Pixels per inch value for non-window system displays.
29369 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29370 Vdisplay_pixels_per_inch = make_float (72.0);
29372 #ifdef GLYPH_DEBUG
29373 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29374 #endif
29376 DEFVAR_LISP ("truncate-partial-width-windows",
29377 Vtruncate_partial_width_windows,
29378 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29379 For an integer value, truncate lines in each window narrower than the
29380 full frame width, provided the window width is less than that integer;
29381 otherwise, respect the value of `truncate-lines'.
29383 For any other non-nil value, truncate lines in all windows that do
29384 not span the full frame width.
29386 A value of nil means to respect the value of `truncate-lines'.
29388 If `word-wrap' is enabled, you might want to reduce this. */);
29389 Vtruncate_partial_width_windows = make_number (50);
29391 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29392 doc: /* Maximum buffer size for which line number should be displayed.
29393 If the buffer is bigger than this, the line number does not appear
29394 in the mode line. A value of nil means no limit. */);
29395 Vline_number_display_limit = Qnil;
29397 DEFVAR_INT ("line-number-display-limit-width",
29398 line_number_display_limit_width,
29399 doc: /* Maximum line width (in characters) for line number display.
29400 If the average length of the lines near point is bigger than this, then the
29401 line number may be omitted from the mode line. */);
29402 line_number_display_limit_width = 200;
29404 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29405 doc: /* Non-nil means highlight region even in nonselected windows. */);
29406 highlight_nonselected_windows = 0;
29408 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29409 doc: /* Non-nil if more than one frame is visible on this display.
29410 Minibuffer-only frames don't count, but iconified frames do.
29411 This variable is not guaranteed to be accurate except while processing
29412 `frame-title-format' and `icon-title-format'. */);
29414 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29415 doc: /* Template for displaying the title bar of visible frames.
29416 \(Assuming the window manager supports this feature.)
29418 This variable has the same structure as `mode-line-format', except that
29419 the %c and %l constructs are ignored. It is used only on frames for
29420 which no explicit name has been set \(see `modify-frame-parameters'). */);
29422 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29423 doc: /* Template for displaying the title bar of an iconified frame.
29424 \(Assuming the window manager supports this feature.)
29425 This variable has the same structure as `mode-line-format' (which see),
29426 and is used only on frames for which no explicit name has been set
29427 \(see `modify-frame-parameters'). */);
29428 Vicon_title_format
29429 = Vframe_title_format
29430 = listn (CONSTYPE_PURE, 3,
29431 intern_c_string ("multiple-frames"),
29432 build_pure_c_string ("%b"),
29433 listn (CONSTYPE_PURE, 4,
29434 empty_unibyte_string,
29435 intern_c_string ("invocation-name"),
29436 build_pure_c_string ("@"),
29437 intern_c_string ("system-name")));
29439 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29440 doc: /* Maximum number of lines to keep in the message log buffer.
29441 If nil, disable message logging. If t, log messages but don't truncate
29442 the buffer when it becomes large. */);
29443 Vmessage_log_max = make_number (1000);
29445 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29446 doc: /* Functions called before redisplay, if window sizes have changed.
29447 The value should be a list of functions that take one argument.
29448 Just before redisplay, for each frame, if any of its windows have changed
29449 size since the last redisplay, or have been split or deleted,
29450 all the functions in the list are called, with the frame as argument. */);
29451 Vwindow_size_change_functions = Qnil;
29453 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29454 doc: /* List of functions to call before redisplaying a window with scrolling.
29455 Each function is called with two arguments, the window and its new
29456 display-start position. Note that these functions are also called by
29457 `set-window-buffer'. Also note that the value of `window-end' is not
29458 valid when these functions are called.
29460 Warning: Do not use this feature to alter the way the window
29461 is scrolled. It is not designed for that, and such use probably won't
29462 work. */);
29463 Vwindow_scroll_functions = Qnil;
29465 DEFVAR_LISP ("window-text-change-functions",
29466 Vwindow_text_change_functions,
29467 doc: /* Functions to call in redisplay when text in the window might change. */);
29468 Vwindow_text_change_functions = Qnil;
29470 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29471 doc: /* Functions called when redisplay of a window reaches the end trigger.
29472 Each function is called with two arguments, the window and the end trigger value.
29473 See `set-window-redisplay-end-trigger'. */);
29474 Vredisplay_end_trigger_functions = Qnil;
29476 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29477 doc: /* Non-nil means autoselect window with mouse pointer.
29478 If nil, do not autoselect windows.
29479 A positive number means delay autoselection by that many seconds: a
29480 window is autoselected only after the mouse has remained in that
29481 window for the duration of the delay.
29482 A negative number has a similar effect, but causes windows to be
29483 autoselected only after the mouse has stopped moving. \(Because of
29484 the way Emacs compares mouse events, you will occasionally wait twice
29485 that time before the window gets selected.\)
29486 Any other value means to autoselect window instantaneously when the
29487 mouse pointer enters it.
29489 Autoselection selects the minibuffer only if it is active, and never
29490 unselects the minibuffer if it is active.
29492 When customizing this variable make sure that the actual value of
29493 `focus-follows-mouse' matches the behavior of your window manager. */);
29494 Vmouse_autoselect_window = Qnil;
29496 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29497 doc: /* Non-nil means automatically resize tool-bars.
29498 This dynamically changes the tool-bar's height to the minimum height
29499 that is needed to make all tool-bar items visible.
29500 If value is `grow-only', the tool-bar's height is only increased
29501 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29502 Vauto_resize_tool_bars = Qt;
29504 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29505 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29506 auto_raise_tool_bar_buttons_p = 1;
29508 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29509 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29510 make_cursor_line_fully_visible_p = 1;
29512 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29513 doc: /* Border below tool-bar in pixels.
29514 If an integer, use it as the height of the border.
29515 If it is one of `internal-border-width' or `border-width', use the
29516 value of the corresponding frame parameter.
29517 Otherwise, no border is added below the tool-bar. */);
29518 Vtool_bar_border = Qinternal_border_width;
29520 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29521 doc: /* Margin around tool-bar buttons in pixels.
29522 If an integer, use that for both horizontal and vertical margins.
29523 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29524 HORZ specifying the horizontal margin, and VERT specifying the
29525 vertical margin. */);
29526 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29528 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29529 doc: /* Relief thickness of tool-bar buttons. */);
29530 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29532 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29533 doc: /* Tool bar style to use.
29534 It can be one of
29535 image - show images only
29536 text - show text only
29537 both - show both, text below image
29538 both-horiz - show text to the right of the image
29539 text-image-horiz - show text to the left of the image
29540 any other - use system default or image if no system default.
29542 This variable only affects the GTK+ toolkit version of Emacs. */);
29543 Vtool_bar_style = Qnil;
29545 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29546 doc: /* Maximum number of characters a label can have to be shown.
29547 The tool bar style must also show labels for this to have any effect, see
29548 `tool-bar-style'. */);
29549 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29551 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29552 doc: /* List of functions to call to fontify regions of text.
29553 Each function is called with one argument POS. Functions must
29554 fontify a region starting at POS in the current buffer, and give
29555 fontified regions the property `fontified'. */);
29556 Vfontification_functions = Qnil;
29557 Fmake_variable_buffer_local (Qfontification_functions);
29559 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29560 unibyte_display_via_language_environment,
29561 doc: /* Non-nil means display unibyte text according to language environment.
29562 Specifically, this means that raw bytes in the range 160-255 decimal
29563 are displayed by converting them to the equivalent multibyte characters
29564 according to the current language environment. As a result, they are
29565 displayed according to the current fontset.
29567 Note that this variable affects only how these bytes are displayed,
29568 but does not change the fact they are interpreted as raw bytes. */);
29569 unibyte_display_via_language_environment = 0;
29571 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29572 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29573 If a float, it specifies a fraction of the mini-window frame's height.
29574 If an integer, it specifies a number of lines. */);
29575 Vmax_mini_window_height = make_float (0.25);
29577 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29578 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29579 A value of nil means don't automatically resize mini-windows.
29580 A value of t means resize them to fit the text displayed in them.
29581 A value of `grow-only', the default, means let mini-windows grow only;
29582 they return to their normal size when the minibuffer is closed, or the
29583 echo area becomes empty. */);
29584 Vresize_mini_windows = Qgrow_only;
29586 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29587 doc: /* Alist specifying how to blink the cursor off.
29588 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29589 `cursor-type' frame-parameter or variable equals ON-STATE,
29590 comparing using `equal', Emacs uses OFF-STATE to specify
29591 how to blink it off. ON-STATE and OFF-STATE are values for
29592 the `cursor-type' frame parameter.
29594 If a frame's ON-STATE has no entry in this list,
29595 the frame's other specifications determine how to blink the cursor off. */);
29596 Vblink_cursor_alist = Qnil;
29598 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29599 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29600 If non-nil, windows are automatically scrolled horizontally to make
29601 point visible. */);
29602 automatic_hscrolling_p = 1;
29603 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29605 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29606 doc: /* How many columns away from the window edge point is allowed to get
29607 before automatic hscrolling will horizontally scroll the window. */);
29608 hscroll_margin = 5;
29610 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29611 doc: /* How many columns to scroll the window when point gets too close to the edge.
29612 When point is less than `hscroll-margin' columns from the window
29613 edge, automatic hscrolling will scroll the window by the amount of columns
29614 determined by this variable. If its value is a positive integer, scroll that
29615 many columns. If it's a positive floating-point number, it specifies the
29616 fraction of the window's width to scroll. If it's nil or zero, point will be
29617 centered horizontally after the scroll. Any other value, including negative
29618 numbers, are treated as if the value were zero.
29620 Automatic hscrolling always moves point outside the scroll margin, so if
29621 point was more than scroll step columns inside the margin, the window will
29622 scroll more than the value given by the scroll step.
29624 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29625 and `scroll-right' overrides this variable's effect. */);
29626 Vhscroll_step = make_number (0);
29628 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29629 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29630 Bind this around calls to `message' to let it take effect. */);
29631 message_truncate_lines = 0;
29633 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29634 doc: /* Normal hook run to update the menu bar definitions.
29635 Redisplay runs this hook before it redisplays the menu bar.
29636 This is used to update submenus such as Buffers,
29637 whose contents depend on various data. */);
29638 Vmenu_bar_update_hook = Qnil;
29640 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29641 doc: /* Frame for which we are updating a menu.
29642 The enable predicate for a menu binding should check this variable. */);
29643 Vmenu_updating_frame = Qnil;
29645 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29646 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29647 inhibit_menubar_update = 0;
29649 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29650 doc: /* Prefix prepended to all continuation lines at display time.
29651 The value may be a string, an image, or a stretch-glyph; it is
29652 interpreted in the same way as the value of a `display' text property.
29654 This variable is overridden by any `wrap-prefix' text or overlay
29655 property.
29657 To add a prefix to non-continuation lines, use `line-prefix'. */);
29658 Vwrap_prefix = Qnil;
29659 DEFSYM (Qwrap_prefix, "wrap-prefix");
29660 Fmake_variable_buffer_local (Qwrap_prefix);
29662 DEFVAR_LISP ("line-prefix", Vline_prefix,
29663 doc: /* Prefix prepended to all non-continuation lines at display time.
29664 The value may be a string, an image, or a stretch-glyph; it is
29665 interpreted in the same way as the value of a `display' text property.
29667 This variable is overridden by any `line-prefix' text or overlay
29668 property.
29670 To add a prefix to continuation lines, use `wrap-prefix'. */);
29671 Vline_prefix = Qnil;
29672 DEFSYM (Qline_prefix, "line-prefix");
29673 Fmake_variable_buffer_local (Qline_prefix);
29675 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29676 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29677 inhibit_eval_during_redisplay = 0;
29679 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29680 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29681 inhibit_free_realized_faces = 0;
29683 #ifdef GLYPH_DEBUG
29684 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29685 doc: /* Inhibit try_window_id display optimization. */);
29686 inhibit_try_window_id = 0;
29688 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29689 doc: /* Inhibit try_window_reusing display optimization. */);
29690 inhibit_try_window_reusing = 0;
29692 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29693 doc: /* Inhibit try_cursor_movement display optimization. */);
29694 inhibit_try_cursor_movement = 0;
29695 #endif /* GLYPH_DEBUG */
29697 DEFVAR_INT ("overline-margin", overline_margin,
29698 doc: /* Space between overline and text, in pixels.
29699 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29700 margin to the character height. */);
29701 overline_margin = 2;
29703 DEFVAR_INT ("underline-minimum-offset",
29704 underline_minimum_offset,
29705 doc: /* Minimum distance between baseline and underline.
29706 This can improve legibility of underlined text at small font sizes,
29707 particularly when using variable `x-use-underline-position-properties'
29708 with fonts that specify an UNDERLINE_POSITION relatively close to the
29709 baseline. The default value is 1. */);
29710 underline_minimum_offset = 1;
29712 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29713 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29714 This feature only works when on a window system that can change
29715 cursor shapes. */);
29716 display_hourglass_p = 1;
29718 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29719 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29720 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29722 hourglass_atimer = NULL;
29723 hourglass_shown_p = 0;
29725 DEFSYM (Qglyphless_char, "glyphless-char");
29726 DEFSYM (Qhex_code, "hex-code");
29727 DEFSYM (Qempty_box, "empty-box");
29728 DEFSYM (Qthin_space, "thin-space");
29729 DEFSYM (Qzero_width, "zero-width");
29731 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29732 /* Intern this now in case it isn't already done.
29733 Setting this variable twice is harmless.
29734 But don't staticpro it here--that is done in alloc.c. */
29735 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29736 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29738 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29739 doc: /* Char-table defining glyphless characters.
29740 Each element, if non-nil, should be one of the following:
29741 an ASCII acronym string: display this string in a box
29742 `hex-code': display the hexadecimal code of a character in a box
29743 `empty-box': display as an empty box
29744 `thin-space': display as 1-pixel width space
29745 `zero-width': don't display
29746 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29747 display method for graphical terminals and text terminals respectively.
29748 GRAPHICAL and TEXT should each have one of the values listed above.
29750 The char-table has one extra slot to control the display of a character for
29751 which no font is found. This slot only takes effect on graphical terminals.
29752 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29753 `thin-space'. The default is `empty-box'. */);
29754 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29755 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29756 Qempty_box);
29758 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29759 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29760 Vdebug_on_message = Qnil;
29764 /* Initialize this module when Emacs starts. */
29766 void
29767 init_xdisp (void)
29769 current_header_line_height = current_mode_line_height = -1;
29771 CHARPOS (this_line_start_pos) = 0;
29773 if (!noninteractive)
29775 struct window *m = XWINDOW (minibuf_window);
29776 Lisp_Object frame = m->frame;
29777 struct frame *f = XFRAME (frame);
29778 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29779 struct window *r = XWINDOW (root);
29780 int i;
29782 echo_area_window = minibuf_window;
29784 r->top_line = FRAME_TOP_MARGIN (f);
29785 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29786 r->total_cols = FRAME_COLS (f);
29788 m->top_line = FRAME_LINES (f) - 1;
29789 m->total_lines = 1;
29790 m->total_cols = FRAME_COLS (f);
29792 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29793 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29794 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29796 /* The default ellipsis glyphs `...'. */
29797 for (i = 0; i < 3; ++i)
29798 default_invis_vector[i] = make_number ('.');
29802 /* Allocate the buffer for frame titles.
29803 Also used for `format-mode-line'. */
29804 int size = 100;
29805 mode_line_noprop_buf = xmalloc (size);
29806 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29807 mode_line_noprop_ptr = mode_line_noprop_buf;
29808 mode_line_target = MODE_LINE_DISPLAY;
29811 help_echo_showing_p = 0;
29814 /* Platform-independent portion of hourglass implementation. */
29816 /* Cancel a currently active hourglass timer, and start a new one. */
29817 void
29818 start_hourglass (void)
29820 #if defined (HAVE_WINDOW_SYSTEM)
29821 EMACS_TIME delay;
29823 cancel_hourglass ();
29825 if (INTEGERP (Vhourglass_delay)
29826 && XINT (Vhourglass_delay) > 0)
29827 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29828 TYPE_MAXIMUM (time_t)),
29830 else if (FLOATP (Vhourglass_delay)
29831 && XFLOAT_DATA (Vhourglass_delay) > 0)
29832 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29833 else
29834 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29836 #ifdef HAVE_NTGUI
29838 extern void w32_note_current_window (void);
29839 w32_note_current_window ();
29841 #endif /* HAVE_NTGUI */
29843 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29844 show_hourglass, NULL);
29845 #endif
29849 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29850 shown. */
29851 void
29852 cancel_hourglass (void)
29854 #if defined (HAVE_WINDOW_SYSTEM)
29855 if (hourglass_atimer)
29857 cancel_atimer (hourglass_atimer);
29858 hourglass_atimer = NULL;
29861 if (hourglass_shown_p)
29862 hide_hourglass ();
29863 #endif