Auto-commit of generated files.
[emacs.git] / src / xdisp.c
blobc4f7b205119f4d50bc8a561c1a91c0aedfd9d646
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22 Redisplay.
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
53 expose_window (asynchronous) |
55 X expose events -----+
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
84 . try_cursor_movement
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
90 . try_window_reusing_current_matrix
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
96 . try_window_id
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
102 . try_window
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
115 Desired matrices.
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
160 Frame matrices.
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
181 Bidirectional display.
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
224 Bidirectional display and character compositions
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
251 Moving an iterator in bidirectional text
252 without producing glyphs
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
277 #include "lisp.h"
278 #include "atimer.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301 #ifdef HAVE_WINDOW_SYSTEM
302 #include TERM_HEADER
303 #endif /* HAVE_WINDOW_SYSTEM */
305 #ifndef FRAME_X_OUTPUT
306 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
307 #endif
309 #define INFINITY 10000000
311 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
312 Lisp_Object Qwindow_scroll_functions;
313 static Lisp_Object Qwindow_text_change_functions;
314 static Lisp_Object Qredisplay_end_trigger_functions;
315 Lisp_Object Qinhibit_point_motion_hooks;
316 static Lisp_Object QCeval, QCpropertize;
317 Lisp_Object QCfile, QCdata;
318 static Lisp_Object Qfontified;
319 static Lisp_Object Qgrow_only;
320 static Lisp_Object Qinhibit_eval_during_redisplay;
321 static Lisp_Object Qbuffer_position, Qposition, Qobject;
322 static Lisp_Object Qright_to_left, Qleft_to_right;
324 /* Cursor shapes. */
325 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
327 /* Pointer shapes. */
328 static Lisp_Object Qarrow, Qhand;
329 Lisp_Object Qtext;
331 /* Holds the list (error). */
332 static Lisp_Object list_of_error;
334 static Lisp_Object Qfontification_functions;
336 static Lisp_Object Qwrap_prefix;
337 static Lisp_Object Qline_prefix;
338 static Lisp_Object Qredisplay_internal;
340 /* Non-nil means don't actually do any redisplay. */
342 Lisp_Object Qinhibit_redisplay;
344 /* Names of text properties relevant for redisplay. */
346 Lisp_Object Qdisplay;
348 Lisp_Object Qspace, QCalign_to;
349 static Lisp_Object QCrelative_width, QCrelative_height;
350 Lisp_Object Qleft_margin, Qright_margin;
351 static Lisp_Object Qspace_width, Qraise;
352 static Lisp_Object Qslice;
353 Lisp_Object Qcenter;
354 static Lisp_Object Qmargin, Qpointer;
355 static Lisp_Object Qline_height;
357 #ifdef HAVE_WINDOW_SYSTEM
359 /* Test if overflow newline into fringe. Called with iterator IT
360 at or past right window margin, and with IT->current_x set. */
362 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
363 (!NILP (Voverflow_newline_into_fringe) \
364 && FRAME_WINDOW_P ((IT)->f) \
365 && ((IT)->bidi_it.paragraph_dir == R2L \
366 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
367 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
368 && (IT)->current_x == (IT)->last_visible_x)
370 #else /* !HAVE_WINDOW_SYSTEM */
371 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
372 #endif /* HAVE_WINDOW_SYSTEM */
374 /* Test if the display element loaded in IT, or the underlying buffer
375 or string character, is a space or a TAB character. This is used
376 to determine where word wrapping can occur. */
378 #define IT_DISPLAYING_WHITESPACE(it) \
379 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
380 || ((STRINGP (it->string) \
381 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
382 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
383 || (it->s \
384 && (it->s[IT_BYTEPOS (*it)] == ' ' \
385 || it->s[IT_BYTEPOS (*it)] == '\t')) \
386 || (IT_BYTEPOS (*it) < ZV_BYTE \
387 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
388 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
390 /* Name of the face used to highlight trailing whitespace. */
392 static Lisp_Object Qtrailing_whitespace;
394 /* Name and number of the face used to highlight escape glyphs. */
396 static Lisp_Object Qescape_glyph;
398 /* Name and number of the face used to highlight non-breaking spaces. */
400 static Lisp_Object Qnobreak_space;
402 /* The symbol `image' which is the car of the lists used to represent
403 images in Lisp. Also a tool bar style. */
405 Lisp_Object Qimage;
407 /* The image map types. */
408 Lisp_Object QCmap;
409 static Lisp_Object QCpointer;
410 static Lisp_Object Qrect, Qcircle, Qpoly;
412 /* Tool bar styles */
413 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
415 /* Non-zero means print newline to stdout before next mini-buffer
416 message. */
418 int noninteractive_need_newline;
420 /* Non-zero means print newline to message log before next message. */
422 static int message_log_need_newline;
424 /* Three markers that message_dolog uses.
425 It could allocate them itself, but that causes trouble
426 in handling memory-full errors. */
427 static Lisp_Object message_dolog_marker1;
428 static Lisp_Object message_dolog_marker2;
429 static Lisp_Object message_dolog_marker3;
431 /* The buffer position of the first character appearing entirely or
432 partially on the line of the selected window which contains the
433 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
434 redisplay optimization in redisplay_internal. */
436 static struct text_pos this_line_start_pos;
438 /* Number of characters past the end of the line above, including the
439 terminating newline. */
441 static struct text_pos this_line_end_pos;
443 /* The vertical positions and the height of this line. */
445 static int this_line_vpos;
446 static int this_line_y;
447 static int this_line_pixel_height;
449 /* X position at which this display line starts. Usually zero;
450 negative if first character is partially visible. */
452 static int this_line_start_x;
454 /* The smallest character position seen by move_it_* functions as they
455 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
456 hscrolled lines, see display_line. */
458 static struct text_pos this_line_min_pos;
460 /* Buffer that this_line_.* variables are referring to. */
462 static struct buffer *this_line_buffer;
465 /* Values of those variables at last redisplay are stored as
466 properties on `overlay-arrow-position' symbol. However, if
467 Voverlay_arrow_position is a marker, last-arrow-position is its
468 numerical position. */
470 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
472 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
473 properties on a symbol in overlay-arrow-variable-list. */
475 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
477 Lisp_Object Qmenu_bar_update_hook;
479 /* Nonzero if an overlay arrow has been displayed in this window. */
481 static int overlay_arrow_seen;
483 /* Vector containing glyphs for an ellipsis `...'. */
485 static Lisp_Object default_invis_vector[3];
487 /* This is the window where the echo area message was displayed. It
488 is always a mini-buffer window, but it may not be the same window
489 currently active as a mini-buffer. */
491 Lisp_Object echo_area_window;
493 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
494 pushes the current message and the value of
495 message_enable_multibyte on the stack, the function restore_message
496 pops the stack and displays MESSAGE again. */
498 static Lisp_Object Vmessage_stack;
500 /* Nonzero means multibyte characters were enabled when the echo area
501 message was specified. */
503 static int message_enable_multibyte;
505 /* Nonzero if we should redraw the mode lines on the next redisplay. */
507 int update_mode_lines;
509 /* Nonzero if window sizes or contents have changed since last
510 redisplay that finished. */
512 int windows_or_buffers_changed;
514 /* Nonzero after display_mode_line if %l was used and it displayed a
515 line number. */
517 static int line_number_displayed;
519 /* The name of the *Messages* buffer, a string. */
521 static Lisp_Object Vmessages_buffer_name;
523 /* Current, index 0, and last displayed echo area message. Either
524 buffers from echo_buffers, or nil to indicate no message. */
526 Lisp_Object echo_area_buffer[2];
528 /* The buffers referenced from echo_area_buffer. */
530 static Lisp_Object echo_buffer[2];
532 /* A vector saved used in with_area_buffer to reduce consing. */
534 static Lisp_Object Vwith_echo_area_save_vector;
536 /* Non-zero means display_echo_area should display the last echo area
537 message again. Set by redisplay_preserve_echo_area. */
539 static int display_last_displayed_message_p;
541 /* Nonzero if echo area is being used by print; zero if being used by
542 message. */
544 static int message_buf_print;
546 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
548 static Lisp_Object Qinhibit_menubar_update;
549 static Lisp_Object Qmessage_truncate_lines;
551 /* Set to 1 in clear_message to make redisplay_internal aware
552 of an emptied echo area. */
554 static int message_cleared_p;
556 /* A scratch glyph row with contents used for generating truncation
557 glyphs. Also used in direct_output_for_insert. */
559 #define MAX_SCRATCH_GLYPHS 100
560 static struct glyph_row scratch_glyph_row;
561 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
563 /* Ascent and height of the last line processed by move_it_to. */
565 static int last_height;
567 /* Non-zero if there's a help-echo in the echo area. */
569 int help_echo_showing_p;
571 /* The maximum distance to look ahead for text properties. Values
572 that are too small let us call compute_char_face and similar
573 functions too often which is expensive. Values that are too large
574 let us call compute_char_face and alike too often because we
575 might not be interested in text properties that far away. */
577 #define TEXT_PROP_DISTANCE_LIMIT 100
579 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
580 iterator state and later restore it. This is needed because the
581 bidi iterator on bidi.c keeps a stacked cache of its states, which
582 is really a singleton. When we use scratch iterator objects to
583 move around the buffer, we can cause the bidi cache to be pushed or
584 popped, and therefore we need to restore the cache state when we
585 return to the original iterator. */
586 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
587 do { \
588 if (CACHE) \
589 bidi_unshelve_cache (CACHE, 1); \
590 ITCOPY = ITORIG; \
591 CACHE = bidi_shelve_cache (); \
592 } while (0)
594 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
595 do { \
596 if (pITORIG != pITCOPY) \
597 *(pITORIG) = *(pITCOPY); \
598 bidi_unshelve_cache (CACHE, 0); \
599 CACHE = NULL; \
600 } while (0)
602 #ifdef GLYPH_DEBUG
604 /* Non-zero means print traces of redisplay if compiled with
605 GLYPH_DEBUG defined. */
607 int trace_redisplay_p;
609 #endif /* GLYPH_DEBUG */
611 #ifdef DEBUG_TRACE_MOVE
612 /* Non-zero means trace with TRACE_MOVE to stderr. */
613 int trace_move;
615 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
616 #else
617 #define TRACE_MOVE(x) (void) 0
618 #endif
620 static Lisp_Object Qauto_hscroll_mode;
622 /* Buffer being redisplayed -- for redisplay_window_error. */
624 static struct buffer *displayed_buffer;
626 /* Value returned from text property handlers (see below). */
628 enum prop_handled
630 HANDLED_NORMALLY,
631 HANDLED_RECOMPUTE_PROPS,
632 HANDLED_OVERLAY_STRING_CONSUMED,
633 HANDLED_RETURN
636 /* A description of text properties that redisplay is interested
637 in. */
639 struct props
641 /* The name of the property. */
642 Lisp_Object *name;
644 /* A unique index for the property. */
645 enum prop_idx idx;
647 /* A handler function called to set up iterator IT from the property
648 at IT's current position. Value is used to steer handle_stop. */
649 enum prop_handled (*handler) (struct it *it);
652 static enum prop_handled handle_face_prop (struct it *);
653 static enum prop_handled handle_invisible_prop (struct it *);
654 static enum prop_handled handle_display_prop (struct it *);
655 static enum prop_handled handle_composition_prop (struct it *);
656 static enum prop_handled handle_overlay_change (struct it *);
657 static enum prop_handled handle_fontified_prop (struct it *);
659 /* Properties handled by iterators. */
661 static struct props it_props[] =
663 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
664 /* Handle `face' before `display' because some sub-properties of
665 `display' need to know the face. */
666 {&Qface, FACE_PROP_IDX, handle_face_prop},
667 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
668 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
669 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
670 {NULL, 0, NULL}
673 /* Value is the position described by X. If X is a marker, value is
674 the marker_position of X. Otherwise, value is X. */
676 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
678 /* Enumeration returned by some move_it_.* functions internally. */
680 enum move_it_result
682 /* Not used. Undefined value. */
683 MOVE_UNDEFINED,
685 /* Move ended at the requested buffer position or ZV. */
686 MOVE_POS_MATCH_OR_ZV,
688 /* Move ended at the requested X pixel position. */
689 MOVE_X_REACHED,
691 /* Move within a line ended at the end of a line that must be
692 continued. */
693 MOVE_LINE_CONTINUED,
695 /* Move within a line ended at the end of a line that would
696 be displayed truncated. */
697 MOVE_LINE_TRUNCATED,
699 /* Move within a line ended at a line end. */
700 MOVE_NEWLINE_OR_CR
703 /* This counter is used to clear the face cache every once in a while
704 in redisplay_internal. It is incremented for each redisplay.
705 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
706 cleared. */
708 #define CLEAR_FACE_CACHE_COUNT 500
709 static int clear_face_cache_count;
711 /* Similarly for the image cache. */
713 #ifdef HAVE_WINDOW_SYSTEM
714 #define CLEAR_IMAGE_CACHE_COUNT 101
715 static int clear_image_cache_count;
717 /* Null glyph slice */
718 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
719 #endif
721 /* True while redisplay_internal is in progress. */
723 bool redisplaying_p;
725 static Lisp_Object Qinhibit_free_realized_faces;
726 static Lisp_Object Qmode_line_default_help_echo;
728 /* If a string, XTread_socket generates an event to display that string.
729 (The display is done in read_char.) */
731 Lisp_Object help_echo_string;
732 Lisp_Object help_echo_window;
733 Lisp_Object help_echo_object;
734 ptrdiff_t help_echo_pos;
736 /* Temporary variable for XTread_socket. */
738 Lisp_Object previous_help_echo_string;
740 /* Platform-independent portion of hourglass implementation. */
742 #ifdef HAVE_WINDOW_SYSTEM
744 /* Non-zero means an hourglass cursor is currently shown. */
745 int hourglass_shown_p;
747 /* If non-null, an asynchronous timer that, when it expires, displays
748 an hourglass cursor on all frames. */
749 struct atimer *hourglass_atimer;
751 #endif /* HAVE_WINDOW_SYSTEM */
753 /* Name of the face used to display glyphless characters. */
754 Lisp_Object Qglyphless_char;
756 /* Symbol for the purpose of Vglyphless_char_display. */
757 static Lisp_Object Qglyphless_char_display;
759 /* Method symbols for Vglyphless_char_display. */
760 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
762 /* Default number of seconds to wait before displaying an hourglass
763 cursor. */
764 #define DEFAULT_HOURGLASS_DELAY 1
766 #ifdef HAVE_WINDOW_SYSTEM
768 /* Default pixel width of `thin-space' display method. */
769 #define THIN_SPACE_WIDTH 1
771 #endif /* HAVE_WINDOW_SYSTEM */
773 /* Function prototypes. */
775 static void setup_for_ellipsis (struct it *, int);
776 static void set_iterator_to_next (struct it *, int);
777 static void mark_window_display_accurate_1 (struct window *, int);
778 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
779 static int display_prop_string_p (Lisp_Object, Lisp_Object);
780 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
781 static int cursor_row_p (struct glyph_row *);
782 static int redisplay_mode_lines (Lisp_Object, int);
783 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
785 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
787 static void handle_line_prefix (struct it *);
789 static void pint2str (char *, int, ptrdiff_t);
790 static void pint2hrstr (char *, int, ptrdiff_t);
791 static struct text_pos run_window_scroll_functions (Lisp_Object,
792 struct text_pos);
793 static int text_outside_line_unchanged_p (struct window *,
794 ptrdiff_t, ptrdiff_t);
795 static void store_mode_line_noprop_char (char);
796 static int store_mode_line_noprop (const char *, int, int);
797 static void handle_stop (struct it *);
798 static void handle_stop_backwards (struct it *, ptrdiff_t);
799 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
800 static void ensure_echo_area_buffers (void);
801 static void unwind_with_echo_area_buffer (Lisp_Object);
802 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
803 static int with_echo_area_buffer (struct window *, int,
804 int (*) (ptrdiff_t, Lisp_Object),
805 ptrdiff_t, Lisp_Object);
806 static void clear_garbaged_frames (void);
807 static int current_message_1 (ptrdiff_t, Lisp_Object);
808 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
809 static void set_message (Lisp_Object);
810 static int set_message_1 (ptrdiff_t, Lisp_Object);
811 static int display_echo_area (struct window *);
812 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
813 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
814 static void unwind_redisplay (void);
815 static int string_char_and_length (const unsigned char *, int *);
816 static struct text_pos display_prop_end (struct it *, Lisp_Object,
817 struct text_pos);
818 static int compute_window_start_on_continuation_line (struct window *);
819 static void insert_left_trunc_glyphs (struct it *);
820 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
821 Lisp_Object);
822 static void extend_face_to_end_of_line (struct it *);
823 static int append_space_for_newline (struct it *, int);
824 static int cursor_row_fully_visible_p (struct window *, int, int);
825 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
826 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
827 static int trailing_whitespace_p (ptrdiff_t);
828 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
829 static void push_it (struct it *, struct text_pos *);
830 static void iterate_out_of_display_property (struct it *);
831 static void pop_it (struct it *);
832 static void sync_frame_with_window_matrix_rows (struct window *);
833 static void redisplay_internal (void);
834 static int echo_area_display (int);
835 static void redisplay_windows (Lisp_Object);
836 static void redisplay_window (Lisp_Object, int);
837 static Lisp_Object redisplay_window_error (Lisp_Object);
838 static Lisp_Object redisplay_window_0 (Lisp_Object);
839 static Lisp_Object redisplay_window_1 (Lisp_Object);
840 static int set_cursor_from_row (struct window *, struct glyph_row *,
841 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
842 int, int);
843 static int update_menu_bar (struct frame *, int, int);
844 static int try_window_reusing_current_matrix (struct window *);
845 static int try_window_id (struct window *);
846 static int display_line (struct it *);
847 static int display_mode_lines (struct window *);
848 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
849 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
850 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
851 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
852 static void display_menu_bar (struct window *);
853 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
854 ptrdiff_t *);
855 static int display_string (const char *, Lisp_Object, Lisp_Object,
856 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
857 static void compute_line_metrics (struct it *);
858 static void run_redisplay_end_trigger_hook (struct it *);
859 static int get_overlay_strings (struct it *, ptrdiff_t);
860 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
861 static void next_overlay_string (struct it *);
862 static void reseat (struct it *, struct text_pos, int);
863 static void reseat_1 (struct it *, struct text_pos, int);
864 static void back_to_previous_visible_line_start (struct it *);
865 static void reseat_at_next_visible_line_start (struct it *, int);
866 static int next_element_from_ellipsis (struct it *);
867 static int next_element_from_display_vector (struct it *);
868 static int next_element_from_string (struct it *);
869 static int next_element_from_c_string (struct it *);
870 static int next_element_from_buffer (struct it *);
871 static int next_element_from_composition (struct it *);
872 static int next_element_from_image (struct it *);
873 static int next_element_from_stretch (struct it *);
874 static void load_overlay_strings (struct it *, ptrdiff_t);
875 static int init_from_display_pos (struct it *, struct window *,
876 struct display_pos *);
877 static void reseat_to_string (struct it *, const char *,
878 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
879 static int get_next_display_element (struct it *);
880 static enum move_it_result
881 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
882 enum move_operation_enum);
883 static void get_visually_first_element (struct it *);
884 static void init_to_row_start (struct it *, struct window *,
885 struct glyph_row *);
886 static int init_to_row_end (struct it *, struct window *,
887 struct glyph_row *);
888 static void back_to_previous_line_start (struct it *);
889 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
890 static struct text_pos string_pos_nchars_ahead (struct text_pos,
891 Lisp_Object, ptrdiff_t);
892 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
893 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
894 static ptrdiff_t number_of_chars (const char *, bool);
895 static void compute_stop_pos (struct it *);
896 static void compute_string_pos (struct text_pos *, struct text_pos,
897 Lisp_Object);
898 static int face_before_or_after_it_pos (struct it *, int);
899 static ptrdiff_t next_overlay_change (ptrdiff_t);
900 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
901 Lisp_Object, struct text_pos *, ptrdiff_t, int);
902 static int handle_single_display_spec (struct it *, Lisp_Object,
903 Lisp_Object, Lisp_Object,
904 struct text_pos *, ptrdiff_t, int, int);
905 static int underlying_face_id (struct it *);
906 static int in_ellipses_for_invisible_text_p (struct display_pos *,
907 struct window *);
909 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
910 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
912 #ifdef HAVE_WINDOW_SYSTEM
914 static void x_consider_frame_title (Lisp_Object);
915 static void update_tool_bar (struct frame *, int);
916 static int redisplay_tool_bar (struct frame *);
917 static void notice_overwritten_cursor (struct window *,
918 enum glyph_row_area,
919 int, int, int, int);
920 static void append_stretch_glyph (struct it *, Lisp_Object,
921 int, int, int);
924 #endif /* HAVE_WINDOW_SYSTEM */
926 static void produce_special_glyphs (struct it *, enum display_element_type);
927 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
928 static int coords_in_mouse_face_p (struct window *, int, int);
932 /***********************************************************************
933 Window display dimensions
934 ***********************************************************************/
936 /* Return the bottom boundary y-position for text lines in window W.
937 This is the first y position at which a line cannot start.
938 It is relative to the top of the window.
940 This is the height of W minus the height of a mode line, if any. */
943 window_text_bottom_y (struct window *w)
945 int height = WINDOW_TOTAL_HEIGHT (w);
947 if (WINDOW_WANTS_MODELINE_P (w))
948 height -= CURRENT_MODE_LINE_HEIGHT (w);
949 return height;
952 /* Return the pixel width of display area AREA of window W.
953 ANY_AREA means return the total width of W, not including
954 fringes to the left and right of the window. */
957 window_box_width (struct window *w, enum glyph_row_area area)
959 int cols = w->total_cols;
960 int pixels = 0;
962 if (!w->pseudo_window_p)
964 cols -= WINDOW_SCROLL_BAR_COLS (w);
966 if (area == TEXT_AREA)
968 cols -= max (0, w->left_margin_cols);
969 cols -= max (0, w->right_margin_cols);
970 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
972 else if (area == LEFT_MARGIN_AREA)
974 cols = max (0, w->left_margin_cols);
975 pixels = 0;
977 else if (area == RIGHT_MARGIN_AREA)
979 cols = max (0, w->right_margin_cols);
980 pixels = 0;
984 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
988 /* Return the pixel height of the display area of window W, not
989 including mode lines of W, if any. */
992 window_box_height (struct window *w)
994 struct frame *f = XFRAME (w->frame);
995 int height = WINDOW_TOTAL_HEIGHT (w);
997 eassert (height >= 0);
999 /* Note: the code below that determines the mode-line/header-line
1000 height is essentially the same as that contained in the macro
1001 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1002 the appropriate glyph row has its `mode_line_p' flag set,
1003 and if it doesn't, uses estimate_mode_line_height instead. */
1005 if (WINDOW_WANTS_MODELINE_P (w))
1007 struct glyph_row *ml_row
1008 = (w->current_matrix && w->current_matrix->rows
1009 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1010 : 0);
1011 if (ml_row && ml_row->mode_line_p)
1012 height -= ml_row->height;
1013 else
1014 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1017 if (WINDOW_WANTS_HEADER_LINE_P (w))
1019 struct glyph_row *hl_row
1020 = (w->current_matrix && w->current_matrix->rows
1021 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1022 : 0);
1023 if (hl_row && hl_row->mode_line_p)
1024 height -= hl_row->height;
1025 else
1026 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1029 /* With a very small font and a mode-line that's taller than
1030 default, we might end up with a negative height. */
1031 return max (0, height);
1034 /* Return the window-relative coordinate of the left edge of display
1035 area AREA of window W. ANY_AREA means return the left edge of the
1036 whole window, to the right of the left fringe of W. */
1039 window_box_left_offset (struct window *w, enum glyph_row_area area)
1041 int x;
1043 if (w->pseudo_window_p)
1044 return 0;
1046 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1048 if (area == TEXT_AREA)
1049 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1050 + window_box_width (w, LEFT_MARGIN_AREA));
1051 else if (area == RIGHT_MARGIN_AREA)
1052 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1053 + window_box_width (w, LEFT_MARGIN_AREA)
1054 + window_box_width (w, TEXT_AREA)
1055 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1057 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1058 else if (area == LEFT_MARGIN_AREA
1059 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1060 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1062 return x;
1066 /* Return the window-relative coordinate of the right edge of display
1067 area AREA of window W. ANY_AREA means return the right edge of the
1068 whole window, to the left of the right fringe of W. */
1071 window_box_right_offset (struct window *w, enum glyph_row_area area)
1073 return window_box_left_offset (w, area) + window_box_width (w, area);
1076 /* Return the frame-relative coordinate of the left edge of display
1077 area AREA of window W. ANY_AREA means return the left edge of the
1078 whole window, to the right of the left fringe of W. */
1081 window_box_left (struct window *w, enum glyph_row_area area)
1083 struct frame *f = XFRAME (w->frame);
1084 int x;
1086 if (w->pseudo_window_p)
1087 return FRAME_INTERNAL_BORDER_WIDTH (f);
1089 x = (WINDOW_LEFT_EDGE_X (w)
1090 + window_box_left_offset (w, area));
1092 return x;
1096 /* Return the frame-relative coordinate of the right edge of display
1097 area AREA of window W. ANY_AREA means return the right edge of the
1098 whole window, to the left of the right fringe of W. */
1101 window_box_right (struct window *w, enum glyph_row_area area)
1103 return window_box_left (w, area) + window_box_width (w, area);
1106 /* Get the bounding box of the display area AREA of window W, without
1107 mode lines, in frame-relative coordinates. ANY_AREA means the
1108 whole window, not including the left and right fringes of
1109 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1110 coordinates of the upper-left corner of the box. Return in
1111 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1113 void
1114 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1115 int *box_y, int *box_width, int *box_height)
1117 if (box_width)
1118 *box_width = window_box_width (w, area);
1119 if (box_height)
1120 *box_height = window_box_height (w);
1121 if (box_x)
1122 *box_x = window_box_left (w, area);
1123 if (box_y)
1125 *box_y = WINDOW_TOP_EDGE_Y (w);
1126 if (WINDOW_WANTS_HEADER_LINE_P (w))
1127 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1131 #ifdef HAVE_WINDOW_SYSTEM
1133 /* Get the bounding box of the display area AREA of window W, without
1134 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1135 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1136 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1137 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1138 box. */
1140 static void
1141 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1142 int *bottom_right_x, int *bottom_right_y)
1144 window_box (w, ANY_AREA, top_left_x, top_left_y,
1145 bottom_right_x, bottom_right_y);
1146 *bottom_right_x += *top_left_x;
1147 *bottom_right_y += *top_left_y;
1150 #endif /* HAVE_WINDOW_SYSTEM */
1152 /***********************************************************************
1153 Utilities
1154 ***********************************************************************/
1156 /* Return the bottom y-position of the line the iterator IT is in.
1157 This can modify IT's settings. */
1160 line_bottom_y (struct it *it)
1162 int line_height = it->max_ascent + it->max_descent;
1163 int line_top_y = it->current_y;
1165 if (line_height == 0)
1167 if (last_height)
1168 line_height = last_height;
1169 else if (IT_CHARPOS (*it) < ZV)
1171 move_it_by_lines (it, 1);
1172 line_height = (it->max_ascent || it->max_descent
1173 ? it->max_ascent + it->max_descent
1174 : last_height);
1176 else
1178 struct glyph_row *row = it->glyph_row;
1180 /* Use the default character height. */
1181 it->glyph_row = NULL;
1182 it->what = IT_CHARACTER;
1183 it->c = ' ';
1184 it->len = 1;
1185 PRODUCE_GLYPHS (it);
1186 line_height = it->ascent + it->descent;
1187 it->glyph_row = row;
1191 return line_top_y + line_height;
1194 DEFUN ("line-pixel-height", Fline_pixel_height,
1195 Sline_pixel_height, 0, 0, 0,
1196 doc: /* Return height in pixels of text line in the selected window.
1198 Value is the height in pixels of the line at point. */)
1199 (void)
1201 struct it it;
1202 struct text_pos pt;
1203 struct window *w = XWINDOW (selected_window);
1205 SET_TEXT_POS (pt, PT, PT_BYTE);
1206 start_display (&it, w, pt);
1207 it.vpos = it.current_y = 0;
1208 last_height = 0;
1209 return make_number (line_bottom_y (&it));
1212 /* Return the default pixel height of text lines in window W. The
1213 value is the canonical height of the W frame's default font, plus
1214 any extra space required by the line-spacing variable or frame
1215 parameter.
1217 Implementation note: this ignores any line-spacing text properties
1218 put on the newline characters. This is because those properties
1219 only affect the _screen_ line ending in the newline (i.e., in a
1220 continued line, only the last screen line will be affected), which
1221 means only a small number of lines in a buffer can ever use this
1222 feature. Since this function is used to compute the default pixel
1223 equivalent of text lines in a window, we can safely ignore those
1224 few lines. For the same reasons, we ignore the line-height
1225 properties. */
1227 default_line_pixel_height (struct window *w)
1229 struct frame *f = WINDOW_XFRAME (w);
1230 int height = FRAME_LINE_HEIGHT (f);
1232 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1234 struct buffer *b = XBUFFER (w->contents);
1235 Lisp_Object val = BVAR (b, extra_line_spacing);
1237 if (NILP (val))
1238 val = BVAR (&buffer_defaults, extra_line_spacing);
1239 if (!NILP (val))
1241 if (RANGED_INTEGERP (0, val, INT_MAX))
1242 height += XFASTINT (val);
1243 else if (FLOATP (val))
1245 int addon = XFLOAT_DATA (val) * height + 0.5;
1247 if (addon >= 0)
1248 height += addon;
1251 else
1252 height += f->extra_line_spacing;
1255 return height;
1258 /* Subroutine of pos_visible_p below. Extracts a display string, if
1259 any, from the display spec given as its argument. */
1260 static Lisp_Object
1261 string_from_display_spec (Lisp_Object spec)
1263 if (CONSP (spec))
1265 while (CONSP (spec))
1267 if (STRINGP (XCAR (spec)))
1268 return XCAR (spec);
1269 spec = XCDR (spec);
1272 else if (VECTORP (spec))
1274 ptrdiff_t i;
1276 for (i = 0; i < ASIZE (spec); i++)
1278 if (STRINGP (AREF (spec, i)))
1279 return AREF (spec, i);
1281 return Qnil;
1284 return spec;
1288 /* Limit insanely large values of W->hscroll on frame F to the largest
1289 value that will still prevent first_visible_x and last_visible_x of
1290 'struct it' from overflowing an int. */
1291 static int
1292 window_hscroll_limited (struct window *w, struct frame *f)
1294 ptrdiff_t window_hscroll = w->hscroll;
1295 int window_text_width = window_box_width (w, TEXT_AREA);
1296 int colwidth = FRAME_COLUMN_WIDTH (f);
1298 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1299 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1301 return window_hscroll;
1304 /* Return 1 if position CHARPOS is visible in window W.
1305 CHARPOS < 0 means return info about WINDOW_END position.
1306 If visible, set *X and *Y to pixel coordinates of top left corner.
1307 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1308 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1311 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1312 int *rtop, int *rbot, int *rowh, int *vpos)
1314 struct it it;
1315 void *itdata = bidi_shelve_cache ();
1316 struct text_pos top;
1317 int visible_p = 0;
1318 struct buffer *old_buffer = NULL;
1320 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1321 return visible_p;
1323 if (XBUFFER (w->contents) != current_buffer)
1325 old_buffer = current_buffer;
1326 set_buffer_internal_1 (XBUFFER (w->contents));
1329 SET_TEXT_POS_FROM_MARKER (top, w->start);
1330 /* Scrolling a minibuffer window via scroll bar when the echo area
1331 shows long text sometimes resets the minibuffer contents behind
1332 our backs. */
1333 if (CHARPOS (top) > ZV)
1334 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1336 /* Compute exact mode line heights. */
1337 if (WINDOW_WANTS_MODELINE_P (w))
1338 w->mode_line_height
1339 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1340 BVAR (current_buffer, mode_line_format));
1342 if (WINDOW_WANTS_HEADER_LINE_P (w))
1343 w->header_line_height
1344 = display_mode_line (w, HEADER_LINE_FACE_ID,
1345 BVAR (current_buffer, header_line_format));
1347 start_display (&it, w, top);
1348 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1349 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1351 if (charpos >= 0
1352 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1353 && IT_CHARPOS (it) >= charpos)
1354 /* When scanning backwards under bidi iteration, move_it_to
1355 stops at or _before_ CHARPOS, because it stops at or to
1356 the _right_ of the character at CHARPOS. */
1357 || (it.bidi_p && it.bidi_it.scan_dir == -1
1358 && IT_CHARPOS (it) <= charpos)))
1360 /* We have reached CHARPOS, or passed it. How the call to
1361 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1362 or covered by a display property, move_it_to stops at the end
1363 of the invisible text, to the right of CHARPOS. (ii) If
1364 CHARPOS is in a display vector, move_it_to stops on its last
1365 glyph. */
1366 int top_x = it.current_x;
1367 int top_y = it.current_y;
1368 /* Calling line_bottom_y may change it.method, it.position, etc. */
1369 enum it_method it_method = it.method;
1370 int bottom_y = (last_height = 0, line_bottom_y (&it));
1371 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1373 if (top_y < window_top_y)
1374 visible_p = bottom_y > window_top_y;
1375 else if (top_y < it.last_visible_y)
1376 visible_p = 1;
1377 if (bottom_y >= it.last_visible_y
1378 && it.bidi_p && it.bidi_it.scan_dir == -1
1379 && IT_CHARPOS (it) < charpos)
1381 /* When the last line of the window is scanned backwards
1382 under bidi iteration, we could be duped into thinking
1383 that we have passed CHARPOS, when in fact move_it_to
1384 simply stopped short of CHARPOS because it reached
1385 last_visible_y. To see if that's what happened, we call
1386 move_it_to again with a slightly larger vertical limit,
1387 and see if it actually moved vertically; if it did, we
1388 didn't really reach CHARPOS, which is beyond window end. */
1389 struct it save_it = it;
1390 /* Why 10? because we don't know how many canonical lines
1391 will the height of the next line(s) be. So we guess. */
1392 int ten_more_lines = 10 * default_line_pixel_height (w);
1394 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1395 MOVE_TO_POS | MOVE_TO_Y);
1396 if (it.current_y > top_y)
1397 visible_p = 0;
1399 it = save_it;
1401 if (visible_p)
1403 if (it_method == GET_FROM_DISPLAY_VECTOR)
1405 /* We stopped on the last glyph of a display vector.
1406 Try and recompute. Hack alert! */
1407 if (charpos < 2 || top.charpos >= charpos)
1408 top_x = it.glyph_row->x;
1409 else
1411 struct it it2, it2_prev;
1412 /* The idea is to get to the previous buffer
1413 position, consume the character there, and use
1414 the pixel coordinates we get after that. But if
1415 the previous buffer position is also displayed
1416 from a display vector, we need to consume all of
1417 the glyphs from that display vector. */
1418 start_display (&it2, w, top);
1419 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1420 /* If we didn't get to CHARPOS - 1, there's some
1421 replacing display property at that position, and
1422 we stopped after it. That is exactly the place
1423 whose coordinates we want. */
1424 if (IT_CHARPOS (it2) != charpos - 1)
1425 it2_prev = it2;
1426 else
1428 /* Iterate until we get out of the display
1429 vector that displays the character at
1430 CHARPOS - 1. */
1431 do {
1432 get_next_display_element (&it2);
1433 PRODUCE_GLYPHS (&it2);
1434 it2_prev = it2;
1435 set_iterator_to_next (&it2, 1);
1436 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1437 && IT_CHARPOS (it2) < charpos);
1439 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1440 || it2_prev.current_x > it2_prev.last_visible_x)
1441 top_x = it.glyph_row->x;
1442 else
1444 top_x = it2_prev.current_x;
1445 top_y = it2_prev.current_y;
1449 else if (IT_CHARPOS (it) != charpos)
1451 Lisp_Object cpos = make_number (charpos);
1452 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1453 Lisp_Object string = string_from_display_spec (spec);
1454 struct text_pos tpos;
1455 int replacing_spec_p;
1456 bool newline_in_string
1457 = (STRINGP (string)
1458 && memchr (SDATA (string), '\n', SBYTES (string)));
1460 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1461 replacing_spec_p
1462 = (!NILP (spec)
1463 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1464 charpos, FRAME_WINDOW_P (it.f)));
1465 /* The tricky code below is needed because there's a
1466 discrepancy between move_it_to and how we set cursor
1467 when PT is at the beginning of a portion of text
1468 covered by a display property or an overlay with a
1469 display property, or the display line ends in a
1470 newline from a display string. move_it_to will stop
1471 _after_ such display strings, whereas
1472 set_cursor_from_row conspires with cursor_row_p to
1473 place the cursor on the first glyph produced from the
1474 display string. */
1476 /* We have overshoot PT because it is covered by a
1477 display property that replaces the text it covers.
1478 If the string includes embedded newlines, we are also
1479 in the wrong display line. Backtrack to the correct
1480 line, where the display property begins. */
1481 if (replacing_spec_p)
1483 Lisp_Object startpos, endpos;
1484 EMACS_INT start, end;
1485 struct it it3;
1486 int it3_moved;
1488 /* Find the first and the last buffer positions
1489 covered by the display string. */
1490 endpos =
1491 Fnext_single_char_property_change (cpos, Qdisplay,
1492 Qnil, Qnil);
1493 startpos =
1494 Fprevious_single_char_property_change (endpos, Qdisplay,
1495 Qnil, Qnil);
1496 start = XFASTINT (startpos);
1497 end = XFASTINT (endpos);
1498 /* Move to the last buffer position before the
1499 display property. */
1500 start_display (&it3, w, top);
1501 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1502 /* Move forward one more line if the position before
1503 the display string is a newline or if it is the
1504 rightmost character on a line that is
1505 continued or word-wrapped. */
1506 if (it3.method == GET_FROM_BUFFER
1507 && (it3.c == '\n'
1508 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1509 move_it_by_lines (&it3, 1);
1510 else if (move_it_in_display_line_to (&it3, -1,
1511 it3.current_x
1512 + it3.pixel_width,
1513 MOVE_TO_X)
1514 == MOVE_LINE_CONTINUED)
1516 move_it_by_lines (&it3, 1);
1517 /* When we are under word-wrap, the #$@%!
1518 move_it_by_lines moves 2 lines, so we need to
1519 fix that up. */
1520 if (it3.line_wrap == WORD_WRAP)
1521 move_it_by_lines (&it3, -1);
1524 /* Record the vertical coordinate of the display
1525 line where we wound up. */
1526 top_y = it3.current_y;
1527 if (it3.bidi_p)
1529 /* When characters are reordered for display,
1530 the character displayed to the left of the
1531 display string could be _after_ the display
1532 property in the logical order. Use the
1533 smallest vertical position of these two. */
1534 start_display (&it3, w, top);
1535 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1536 if (it3.current_y < top_y)
1537 top_y = it3.current_y;
1539 /* Move from the top of the window to the beginning
1540 of the display line where the display string
1541 begins. */
1542 start_display (&it3, w, top);
1543 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1544 /* If it3_moved stays zero after the 'while' loop
1545 below, that means we already were at a newline
1546 before the loop (e.g., the display string begins
1547 with a newline), so we don't need to (and cannot)
1548 inspect the glyphs of it3.glyph_row, because
1549 PRODUCE_GLYPHS will not produce anything for a
1550 newline, and thus it3.glyph_row stays at its
1551 stale content it got at top of the window. */
1552 it3_moved = 0;
1553 /* Finally, advance the iterator until we hit the
1554 first display element whose character position is
1555 CHARPOS, or until the first newline from the
1556 display string, which signals the end of the
1557 display line. */
1558 while (get_next_display_element (&it3))
1560 PRODUCE_GLYPHS (&it3);
1561 if (IT_CHARPOS (it3) == charpos
1562 || ITERATOR_AT_END_OF_LINE_P (&it3))
1563 break;
1564 it3_moved = 1;
1565 set_iterator_to_next (&it3, 0);
1567 top_x = it3.current_x - it3.pixel_width;
1568 /* Normally, we would exit the above loop because we
1569 found the display element whose character
1570 position is CHARPOS. For the contingency that we
1571 didn't, and stopped at the first newline from the
1572 display string, move back over the glyphs
1573 produced from the string, until we find the
1574 rightmost glyph not from the string. */
1575 if (it3_moved
1576 && newline_in_string
1577 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1579 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1580 + it3.glyph_row->used[TEXT_AREA];
1582 while (EQ ((g - 1)->object, string))
1584 --g;
1585 top_x -= g->pixel_width;
1587 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1588 + it3.glyph_row->used[TEXT_AREA]);
1593 *x = top_x;
1594 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1595 *rtop = max (0, window_top_y - top_y);
1596 *rbot = max (0, bottom_y - it.last_visible_y);
1597 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1598 - max (top_y, window_top_y)));
1599 *vpos = it.vpos;
1602 else
1604 /* We were asked to provide info about WINDOW_END. */
1605 struct it it2;
1606 void *it2data = NULL;
1608 SAVE_IT (it2, it, it2data);
1609 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1610 move_it_by_lines (&it, 1);
1611 if (charpos < IT_CHARPOS (it)
1612 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1614 visible_p = 1;
1615 RESTORE_IT (&it2, &it2, it2data);
1616 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1617 *x = it2.current_x;
1618 *y = it2.current_y + it2.max_ascent - it2.ascent;
1619 *rtop = max (0, -it2.current_y);
1620 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1621 - it.last_visible_y));
1622 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1623 it.last_visible_y)
1624 - max (it2.current_y,
1625 WINDOW_HEADER_LINE_HEIGHT (w))));
1626 *vpos = it2.vpos;
1628 else
1629 bidi_unshelve_cache (it2data, 1);
1631 bidi_unshelve_cache (itdata, 0);
1633 if (old_buffer)
1634 set_buffer_internal_1 (old_buffer);
1636 if (visible_p && w->hscroll > 0)
1637 *x -=
1638 window_hscroll_limited (w, WINDOW_XFRAME (w))
1639 * WINDOW_FRAME_COLUMN_WIDTH (w);
1641 #if 0
1642 /* Debugging code. */
1643 if (visible_p)
1644 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1645 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1646 else
1647 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1648 #endif
1650 return visible_p;
1654 /* Return the next character from STR. Return in *LEN the length of
1655 the character. This is like STRING_CHAR_AND_LENGTH but never
1656 returns an invalid character. If we find one, we return a `?', but
1657 with the length of the invalid character. */
1659 static int
1660 string_char_and_length (const unsigned char *str, int *len)
1662 int c;
1664 c = STRING_CHAR_AND_LENGTH (str, *len);
1665 if (!CHAR_VALID_P (c))
1666 /* We may not change the length here because other places in Emacs
1667 don't use this function, i.e. they silently accept invalid
1668 characters. */
1669 c = '?';
1671 return c;
1676 /* Given a position POS containing a valid character and byte position
1677 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1679 static struct text_pos
1680 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1682 eassert (STRINGP (string) && nchars >= 0);
1684 if (STRING_MULTIBYTE (string))
1686 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1687 int len;
1689 while (nchars--)
1691 string_char_and_length (p, &len);
1692 p += len;
1693 CHARPOS (pos) += 1;
1694 BYTEPOS (pos) += len;
1697 else
1698 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1700 return pos;
1704 /* Value is the text position, i.e. character and byte position,
1705 for character position CHARPOS in STRING. */
1707 static struct text_pos
1708 string_pos (ptrdiff_t charpos, Lisp_Object string)
1710 struct text_pos pos;
1711 eassert (STRINGP (string));
1712 eassert (charpos >= 0);
1713 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1714 return pos;
1718 /* Value is a text position, i.e. character and byte position, for
1719 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1720 means recognize multibyte characters. */
1722 static struct text_pos
1723 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1725 struct text_pos pos;
1727 eassert (s != NULL);
1728 eassert (charpos >= 0);
1730 if (multibyte_p)
1732 int len;
1734 SET_TEXT_POS (pos, 0, 0);
1735 while (charpos--)
1737 string_char_and_length ((const unsigned char *) s, &len);
1738 s += len;
1739 CHARPOS (pos) += 1;
1740 BYTEPOS (pos) += len;
1743 else
1744 SET_TEXT_POS (pos, charpos, charpos);
1746 return pos;
1750 /* Value is the number of characters in C string S. MULTIBYTE_P
1751 non-zero means recognize multibyte characters. */
1753 static ptrdiff_t
1754 number_of_chars (const char *s, bool multibyte_p)
1756 ptrdiff_t nchars;
1758 if (multibyte_p)
1760 ptrdiff_t rest = strlen (s);
1761 int len;
1762 const unsigned char *p = (const unsigned char *) s;
1764 for (nchars = 0; rest > 0; ++nchars)
1766 string_char_and_length (p, &len);
1767 rest -= len, p += len;
1770 else
1771 nchars = strlen (s);
1773 return nchars;
1777 /* Compute byte position NEWPOS->bytepos corresponding to
1778 NEWPOS->charpos. POS is a known position in string STRING.
1779 NEWPOS->charpos must be >= POS.charpos. */
1781 static void
1782 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1784 eassert (STRINGP (string));
1785 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1787 if (STRING_MULTIBYTE (string))
1788 *newpos = string_pos_nchars_ahead (pos, string,
1789 CHARPOS (*newpos) - CHARPOS (pos));
1790 else
1791 BYTEPOS (*newpos) = CHARPOS (*newpos);
1794 /* EXPORT:
1795 Return an estimation of the pixel height of mode or header lines on
1796 frame F. FACE_ID specifies what line's height to estimate. */
1799 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1801 #ifdef HAVE_WINDOW_SYSTEM
1802 if (FRAME_WINDOW_P (f))
1804 int height = FONT_HEIGHT (FRAME_FONT (f));
1806 /* This function is called so early when Emacs starts that the face
1807 cache and mode line face are not yet initialized. */
1808 if (FRAME_FACE_CACHE (f))
1810 struct face *face = FACE_FROM_ID (f, face_id);
1811 if (face)
1813 if (face->font)
1814 height = FONT_HEIGHT (face->font);
1815 if (face->box_line_width > 0)
1816 height += 2 * face->box_line_width;
1820 return height;
1822 #endif
1824 return 1;
1827 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1828 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1829 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1830 not force the value into range. */
1832 void
1833 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1834 int *x, int *y, NativeRectangle *bounds, int noclip)
1837 #ifdef HAVE_WINDOW_SYSTEM
1838 if (FRAME_WINDOW_P (f))
1840 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1841 even for negative values. */
1842 if (pix_x < 0)
1843 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1844 if (pix_y < 0)
1845 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1847 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1848 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1850 if (bounds)
1851 STORE_NATIVE_RECT (*bounds,
1852 FRAME_COL_TO_PIXEL_X (f, pix_x),
1853 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1854 FRAME_COLUMN_WIDTH (f) - 1,
1855 FRAME_LINE_HEIGHT (f) - 1);
1857 if (!noclip)
1859 if (pix_x < 0)
1860 pix_x = 0;
1861 else if (pix_x > FRAME_TOTAL_COLS (f))
1862 pix_x = FRAME_TOTAL_COLS (f);
1864 if (pix_y < 0)
1865 pix_y = 0;
1866 else if (pix_y > FRAME_LINES (f))
1867 pix_y = FRAME_LINES (f);
1870 #endif
1872 *x = pix_x;
1873 *y = pix_y;
1877 /* Find the glyph under window-relative coordinates X/Y in window W.
1878 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1879 strings. Return in *HPOS and *VPOS the row and column number of
1880 the glyph found. Return in *AREA the glyph area containing X.
1881 Value is a pointer to the glyph found or null if X/Y is not on
1882 text, or we can't tell because W's current matrix is not up to
1883 date. */
1885 static
1886 struct glyph *
1887 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1888 int *dx, int *dy, int *area)
1890 struct glyph *glyph, *end;
1891 struct glyph_row *row = NULL;
1892 int x0, i;
1894 /* Find row containing Y. Give up if some row is not enabled. */
1895 for (i = 0; i < w->current_matrix->nrows; ++i)
1897 row = MATRIX_ROW (w->current_matrix, i);
1898 if (!row->enabled_p)
1899 return NULL;
1900 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1901 break;
1904 *vpos = i;
1905 *hpos = 0;
1907 /* Give up if Y is not in the window. */
1908 if (i == w->current_matrix->nrows)
1909 return NULL;
1911 /* Get the glyph area containing X. */
1912 if (w->pseudo_window_p)
1914 *area = TEXT_AREA;
1915 x0 = 0;
1917 else
1919 if (x < window_box_left_offset (w, TEXT_AREA))
1921 *area = LEFT_MARGIN_AREA;
1922 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1924 else if (x < window_box_right_offset (w, TEXT_AREA))
1926 *area = TEXT_AREA;
1927 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1929 else
1931 *area = RIGHT_MARGIN_AREA;
1932 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1936 /* Find glyph containing X. */
1937 glyph = row->glyphs[*area];
1938 end = glyph + row->used[*area];
1939 x -= x0;
1940 while (glyph < end && x >= glyph->pixel_width)
1942 x -= glyph->pixel_width;
1943 ++glyph;
1946 if (glyph == end)
1947 return NULL;
1949 if (dx)
1951 *dx = x;
1952 *dy = y - (row->y + row->ascent - glyph->ascent);
1955 *hpos = glyph - row->glyphs[*area];
1956 return glyph;
1959 /* Convert frame-relative x/y to coordinates relative to window W.
1960 Takes pseudo-windows into account. */
1962 static void
1963 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1965 if (w->pseudo_window_p)
1967 /* A pseudo-window is always full-width, and starts at the
1968 left edge of the frame, plus a frame border. */
1969 struct frame *f = XFRAME (w->frame);
1970 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1971 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1973 else
1975 *x -= WINDOW_LEFT_EDGE_X (w);
1976 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1980 #ifdef HAVE_WINDOW_SYSTEM
1982 /* EXPORT:
1983 Return in RECTS[] at most N clipping rectangles for glyph string S.
1984 Return the number of stored rectangles. */
1987 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1989 XRectangle r;
1991 if (n <= 0)
1992 return 0;
1994 if (s->row->full_width_p)
1996 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1997 r.x = WINDOW_LEFT_EDGE_X (s->w);
1998 r.width = WINDOW_TOTAL_WIDTH (s->w);
2000 /* Unless displaying a mode or menu bar line, which are always
2001 fully visible, clip to the visible part of the row. */
2002 if (s->w->pseudo_window_p)
2003 r.height = s->row->visible_height;
2004 else
2005 r.height = s->height;
2007 else
2009 /* This is a text line that may be partially visible. */
2010 r.x = window_box_left (s->w, s->area);
2011 r.width = window_box_width (s->w, s->area);
2012 r.height = s->row->visible_height;
2015 if (s->clip_head)
2016 if (r.x < s->clip_head->x)
2018 if (r.width >= s->clip_head->x - r.x)
2019 r.width -= s->clip_head->x - r.x;
2020 else
2021 r.width = 0;
2022 r.x = s->clip_head->x;
2024 if (s->clip_tail)
2025 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2027 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2028 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2029 else
2030 r.width = 0;
2033 /* If S draws overlapping rows, it's sufficient to use the top and
2034 bottom of the window for clipping because this glyph string
2035 intentionally draws over other lines. */
2036 if (s->for_overlaps)
2038 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2039 r.height = window_text_bottom_y (s->w) - r.y;
2041 /* Alas, the above simple strategy does not work for the
2042 environments with anti-aliased text: if the same text is
2043 drawn onto the same place multiple times, it gets thicker.
2044 If the overlap we are processing is for the erased cursor, we
2045 take the intersection with the rectangle of the cursor. */
2046 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2048 XRectangle rc, r_save = r;
2050 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2051 rc.y = s->w->phys_cursor.y;
2052 rc.width = s->w->phys_cursor_width;
2053 rc.height = s->w->phys_cursor_height;
2055 x_intersect_rectangles (&r_save, &rc, &r);
2058 else
2060 /* Don't use S->y for clipping because it doesn't take partially
2061 visible lines into account. For example, it can be negative for
2062 partially visible lines at the top of a window. */
2063 if (!s->row->full_width_p
2064 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2065 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2066 else
2067 r.y = max (0, s->row->y);
2070 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2072 /* If drawing the cursor, don't let glyph draw outside its
2073 advertised boundaries. Cleartype does this under some circumstances. */
2074 if (s->hl == DRAW_CURSOR)
2076 struct glyph *glyph = s->first_glyph;
2077 int height, max_y;
2079 if (s->x > r.x)
2081 r.width -= s->x - r.x;
2082 r.x = s->x;
2084 r.width = min (r.width, glyph->pixel_width);
2086 /* If r.y is below window bottom, ensure that we still see a cursor. */
2087 height = min (glyph->ascent + glyph->descent,
2088 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2089 max_y = window_text_bottom_y (s->w) - height;
2090 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2091 if (s->ybase - glyph->ascent > max_y)
2093 r.y = max_y;
2094 r.height = height;
2096 else
2098 /* Don't draw cursor glyph taller than our actual glyph. */
2099 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2100 if (height < r.height)
2102 max_y = r.y + r.height;
2103 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2104 r.height = min (max_y - r.y, height);
2109 if (s->row->clip)
2111 XRectangle r_save = r;
2113 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2114 r.width = 0;
2117 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2118 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2120 #ifdef CONVERT_FROM_XRECT
2121 CONVERT_FROM_XRECT (r, *rects);
2122 #else
2123 *rects = r;
2124 #endif
2125 return 1;
2127 else
2129 /* If we are processing overlapping and allowed to return
2130 multiple clipping rectangles, we exclude the row of the glyph
2131 string from the clipping rectangle. This is to avoid drawing
2132 the same text on the environment with anti-aliasing. */
2133 #ifdef CONVERT_FROM_XRECT
2134 XRectangle rs[2];
2135 #else
2136 XRectangle *rs = rects;
2137 #endif
2138 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2140 if (s->for_overlaps & OVERLAPS_PRED)
2142 rs[i] = r;
2143 if (r.y + r.height > row_y)
2145 if (r.y < row_y)
2146 rs[i].height = row_y - r.y;
2147 else
2148 rs[i].height = 0;
2150 i++;
2152 if (s->for_overlaps & OVERLAPS_SUCC)
2154 rs[i] = r;
2155 if (r.y < row_y + s->row->visible_height)
2157 if (r.y + r.height > row_y + s->row->visible_height)
2159 rs[i].y = row_y + s->row->visible_height;
2160 rs[i].height = r.y + r.height - rs[i].y;
2162 else
2163 rs[i].height = 0;
2165 i++;
2168 n = i;
2169 #ifdef CONVERT_FROM_XRECT
2170 for (i = 0; i < n; i++)
2171 CONVERT_FROM_XRECT (rs[i], rects[i]);
2172 #endif
2173 return n;
2177 /* EXPORT:
2178 Return in *NR the clipping rectangle for glyph string S. */
2180 void
2181 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2183 get_glyph_string_clip_rects (s, nr, 1);
2187 /* EXPORT:
2188 Return the position and height of the phys cursor in window W.
2189 Set w->phys_cursor_width to width of phys cursor.
2192 void
2193 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2194 struct glyph *glyph, int *xp, int *yp, int *heightp)
2196 struct frame *f = XFRAME (WINDOW_FRAME (w));
2197 int x, y, wd, h, h0, y0;
2199 /* Compute the width of the rectangle to draw. If on a stretch
2200 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2201 rectangle as wide as the glyph, but use a canonical character
2202 width instead. */
2203 wd = glyph->pixel_width - 1;
2204 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2205 wd++; /* Why? */
2206 #endif
2208 x = w->phys_cursor.x;
2209 if (x < 0)
2211 wd += x;
2212 x = 0;
2215 if (glyph->type == STRETCH_GLYPH
2216 && !x_stretch_cursor_p)
2217 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2218 w->phys_cursor_width = wd;
2220 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2222 /* If y is below window bottom, ensure that we still see a cursor. */
2223 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2225 h = max (h0, glyph->ascent + glyph->descent);
2226 h0 = min (h0, glyph->ascent + glyph->descent);
2228 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2229 if (y < y0)
2231 h = max (h - (y0 - y) + 1, h0);
2232 y = y0 - 1;
2234 else
2236 y0 = window_text_bottom_y (w) - h0;
2237 if (y > y0)
2239 h += y - y0;
2240 y = y0;
2244 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2245 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2246 *heightp = h;
2250 * Remember which glyph the mouse is over.
2253 void
2254 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2256 Lisp_Object window;
2257 struct window *w;
2258 struct glyph_row *r, *gr, *end_row;
2259 enum window_part part;
2260 enum glyph_row_area area;
2261 int x, y, width, height;
2263 /* Try to determine frame pixel position and size of the glyph under
2264 frame pixel coordinates X/Y on frame F. */
2266 if (!f->glyphs_initialized_p
2267 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2268 NILP (window)))
2270 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2271 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2272 goto virtual_glyph;
2275 w = XWINDOW (window);
2276 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2277 height = WINDOW_FRAME_LINE_HEIGHT (w);
2279 x = window_relative_x_coord (w, part, gx);
2280 y = gy - WINDOW_TOP_EDGE_Y (w);
2282 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2283 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2285 if (w->pseudo_window_p)
2287 area = TEXT_AREA;
2288 part = ON_MODE_LINE; /* Don't adjust margin. */
2289 goto text_glyph;
2292 switch (part)
2294 case ON_LEFT_MARGIN:
2295 area = LEFT_MARGIN_AREA;
2296 goto text_glyph;
2298 case ON_RIGHT_MARGIN:
2299 area = RIGHT_MARGIN_AREA;
2300 goto text_glyph;
2302 case ON_HEADER_LINE:
2303 case ON_MODE_LINE:
2304 gr = (part == ON_HEADER_LINE
2305 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2306 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2307 gy = gr->y;
2308 area = TEXT_AREA;
2309 goto text_glyph_row_found;
2311 case ON_TEXT:
2312 area = TEXT_AREA;
2314 text_glyph:
2315 gr = 0; gy = 0;
2316 for (; r <= end_row && r->enabled_p; ++r)
2317 if (r->y + r->height > y)
2319 gr = r; gy = r->y;
2320 break;
2323 text_glyph_row_found:
2324 if (gr && gy <= y)
2326 struct glyph *g = gr->glyphs[area];
2327 struct glyph *end = g + gr->used[area];
2329 height = gr->height;
2330 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2331 if (gx + g->pixel_width > x)
2332 break;
2334 if (g < end)
2336 if (g->type == IMAGE_GLYPH)
2338 /* Don't remember when mouse is over image, as
2339 image may have hot-spots. */
2340 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2341 return;
2343 width = g->pixel_width;
2345 else
2347 /* Use nominal char spacing at end of line. */
2348 x -= gx;
2349 gx += (x / width) * width;
2352 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2353 gx += window_box_left_offset (w, area);
2355 else
2357 /* Use nominal line height at end of window. */
2358 gx = (x / width) * width;
2359 y -= gy;
2360 gy += (y / height) * height;
2362 break;
2364 case ON_LEFT_FRINGE:
2365 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2366 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2367 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2368 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2369 goto row_glyph;
2371 case ON_RIGHT_FRINGE:
2372 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2373 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2374 : window_box_right_offset (w, TEXT_AREA));
2375 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2376 goto row_glyph;
2378 case ON_SCROLL_BAR:
2379 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2381 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2382 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2383 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2384 : 0)));
2385 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2387 row_glyph:
2388 gr = 0, gy = 0;
2389 for (; r <= end_row && r->enabled_p; ++r)
2390 if (r->y + r->height > y)
2392 gr = r; gy = r->y;
2393 break;
2396 if (gr && gy <= y)
2397 height = gr->height;
2398 else
2400 /* Use nominal line height at end of window. */
2401 y -= gy;
2402 gy += (y / height) * height;
2404 break;
2406 default:
2408 virtual_glyph:
2409 /* If there is no glyph under the mouse, then we divide the screen
2410 into a grid of the smallest glyph in the frame, and use that
2411 as our "glyph". */
2413 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2414 round down even for negative values. */
2415 if (gx < 0)
2416 gx -= width - 1;
2417 if (gy < 0)
2418 gy -= height - 1;
2420 gx = (gx / width) * width;
2421 gy = (gy / height) * height;
2423 goto store_rect;
2426 gx += WINDOW_LEFT_EDGE_X (w);
2427 gy += WINDOW_TOP_EDGE_Y (w);
2429 store_rect:
2430 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2432 /* Visible feedback for debugging. */
2433 #if 0
2434 #if HAVE_X_WINDOWS
2435 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2436 f->output_data.x->normal_gc,
2437 gx, gy, width, height);
2438 #endif
2439 #endif
2443 #endif /* HAVE_WINDOW_SYSTEM */
2445 static void
2446 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2448 eassert (w);
2449 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2450 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2451 w->window_end_vpos
2452 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2455 /***********************************************************************
2456 Lisp form evaluation
2457 ***********************************************************************/
2459 /* Error handler for safe_eval and safe_call. */
2461 static Lisp_Object
2462 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2464 add_to_log ("Error during redisplay: %S signaled %S",
2465 Flist (nargs, args), arg);
2466 return Qnil;
2469 /* Call function FUNC with the rest of NARGS - 1 arguments
2470 following. Return the result, or nil if something went
2471 wrong. Prevent redisplay during the evaluation. */
2473 Lisp_Object
2474 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2476 Lisp_Object val;
2478 if (inhibit_eval_during_redisplay)
2479 val = Qnil;
2480 else
2482 va_list ap;
2483 ptrdiff_t i;
2484 ptrdiff_t count = SPECPDL_INDEX ();
2485 struct gcpro gcpro1;
2486 Lisp_Object *args = alloca (nargs * word_size);
2488 args[0] = func;
2489 va_start (ap, func);
2490 for (i = 1; i < nargs; i++)
2491 args[i] = va_arg (ap, Lisp_Object);
2492 va_end (ap);
2494 GCPRO1 (args[0]);
2495 gcpro1.nvars = nargs;
2496 specbind (Qinhibit_redisplay, Qt);
2497 /* Use Qt to ensure debugger does not run,
2498 so there is no possibility of wanting to redisplay. */
2499 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2500 safe_eval_handler);
2501 UNGCPRO;
2502 val = unbind_to (count, val);
2505 return val;
2509 /* Call function FN with one argument ARG.
2510 Return the result, or nil if something went wrong. */
2512 Lisp_Object
2513 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2515 return safe_call (2, fn, arg);
2518 static Lisp_Object Qeval;
2520 Lisp_Object
2521 safe_eval (Lisp_Object sexpr)
2523 return safe_call1 (Qeval, sexpr);
2526 /* Call function FN with two arguments ARG1 and ARG2.
2527 Return the result, or nil if something went wrong. */
2529 Lisp_Object
2530 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2532 return safe_call (3, fn, arg1, arg2);
2537 /***********************************************************************
2538 Debugging
2539 ***********************************************************************/
2541 #if 0
2543 /* Define CHECK_IT to perform sanity checks on iterators.
2544 This is for debugging. It is too slow to do unconditionally. */
2546 static void
2547 check_it (struct it *it)
2549 if (it->method == GET_FROM_STRING)
2551 eassert (STRINGP (it->string));
2552 eassert (IT_STRING_CHARPOS (*it) >= 0);
2554 else
2556 eassert (IT_STRING_CHARPOS (*it) < 0);
2557 if (it->method == GET_FROM_BUFFER)
2559 /* Check that character and byte positions agree. */
2560 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2564 if (it->dpvec)
2565 eassert (it->current.dpvec_index >= 0);
2566 else
2567 eassert (it->current.dpvec_index < 0);
2570 #define CHECK_IT(IT) check_it ((IT))
2572 #else /* not 0 */
2574 #define CHECK_IT(IT) (void) 0
2576 #endif /* not 0 */
2579 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2581 /* Check that the window end of window W is what we expect it
2582 to be---the last row in the current matrix displaying text. */
2584 static void
2585 check_window_end (struct window *w)
2587 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2589 struct glyph_row *row;
2590 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2591 !row->enabled_p
2592 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2593 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2597 #define CHECK_WINDOW_END(W) check_window_end ((W))
2599 #else
2601 #define CHECK_WINDOW_END(W) (void) 0
2603 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2605 /* Return mark position if current buffer has the region of non-zero length,
2606 or -1 otherwise. */
2608 static ptrdiff_t
2609 markpos_of_region (void)
2611 if (!NILP (Vtransient_mark_mode)
2612 && !NILP (BVAR (current_buffer, mark_active))
2613 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2615 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2617 if (markpos != PT)
2618 return markpos;
2620 return -1;
2623 /***********************************************************************
2624 Iterator initialization
2625 ***********************************************************************/
2627 /* Initialize IT for displaying current_buffer in window W, starting
2628 at character position CHARPOS. CHARPOS < 0 means that no buffer
2629 position is specified which is useful when the iterator is assigned
2630 a position later. BYTEPOS is the byte position corresponding to
2631 CHARPOS.
2633 If ROW is not null, calls to produce_glyphs with IT as parameter
2634 will produce glyphs in that row.
2636 BASE_FACE_ID is the id of a base face to use. It must be one of
2637 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2638 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2639 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2641 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2642 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2643 will be initialized to use the corresponding mode line glyph row of
2644 the desired matrix of W. */
2646 void
2647 init_iterator (struct it *it, struct window *w,
2648 ptrdiff_t charpos, ptrdiff_t bytepos,
2649 struct glyph_row *row, enum face_id base_face_id)
2651 ptrdiff_t markpos;
2652 enum face_id remapped_base_face_id = base_face_id;
2654 /* Some precondition checks. */
2655 eassert (w != NULL && it != NULL);
2656 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2657 && charpos <= ZV));
2659 /* If face attributes have been changed since the last redisplay,
2660 free realized faces now because they depend on face definitions
2661 that might have changed. Don't free faces while there might be
2662 desired matrices pending which reference these faces. */
2663 if (face_change_count && !inhibit_free_realized_faces)
2665 face_change_count = 0;
2666 free_all_realized_faces (Qnil);
2669 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2670 if (! NILP (Vface_remapping_alist))
2671 remapped_base_face_id
2672 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2674 /* Use one of the mode line rows of W's desired matrix if
2675 appropriate. */
2676 if (row == NULL)
2678 if (base_face_id == MODE_LINE_FACE_ID
2679 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2680 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2681 else if (base_face_id == HEADER_LINE_FACE_ID)
2682 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2685 /* Clear IT. */
2686 memset (it, 0, sizeof *it);
2687 it->current.overlay_string_index = -1;
2688 it->current.dpvec_index = -1;
2689 it->base_face_id = remapped_base_face_id;
2690 it->string = Qnil;
2691 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2692 it->paragraph_embedding = L2R;
2693 it->bidi_it.string.lstring = Qnil;
2694 it->bidi_it.string.s = NULL;
2695 it->bidi_it.string.bufpos = 0;
2696 it->bidi_it.w = w;
2698 /* The window in which we iterate over current_buffer: */
2699 XSETWINDOW (it->window, w);
2700 it->w = w;
2701 it->f = XFRAME (w->frame);
2703 it->cmp_it.id = -1;
2705 /* Extra space between lines (on window systems only). */
2706 if (base_face_id == DEFAULT_FACE_ID
2707 && FRAME_WINDOW_P (it->f))
2709 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2710 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2711 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2712 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2713 * FRAME_LINE_HEIGHT (it->f));
2714 else if (it->f->extra_line_spacing > 0)
2715 it->extra_line_spacing = it->f->extra_line_spacing;
2716 it->max_extra_line_spacing = 0;
2719 /* If realized faces have been removed, e.g. because of face
2720 attribute changes of named faces, recompute them. When running
2721 in batch mode, the face cache of the initial frame is null. If
2722 we happen to get called, make a dummy face cache. */
2723 if (FRAME_FACE_CACHE (it->f) == NULL)
2724 init_frame_faces (it->f);
2725 if (FRAME_FACE_CACHE (it->f)->used == 0)
2726 recompute_basic_faces (it->f);
2728 /* Current value of the `slice', `space-width', and 'height' properties. */
2729 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2730 it->space_width = Qnil;
2731 it->font_height = Qnil;
2732 it->override_ascent = -1;
2734 /* Are control characters displayed as `^C'? */
2735 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2737 /* -1 means everything between a CR and the following line end
2738 is invisible. >0 means lines indented more than this value are
2739 invisible. */
2740 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2741 ? (clip_to_bounds
2742 (-1, XINT (BVAR (current_buffer, selective_display)),
2743 PTRDIFF_MAX))
2744 : (!NILP (BVAR (current_buffer, selective_display))
2745 ? -1 : 0));
2746 it->selective_display_ellipsis_p
2747 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2749 /* Display table to use. */
2750 it->dp = window_display_table (w);
2752 /* Are multibyte characters enabled in current_buffer? */
2753 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2755 /* If visible region is of non-zero length, set IT->region_beg_charpos
2756 and IT->region_end_charpos to the start and end of a visible region
2757 in window IT->w. Set both to -1 to indicate no region. */
2758 markpos = markpos_of_region ();
2759 if (markpos >= 0
2760 /* Maybe highlight only in selected window. */
2761 && (/* Either show region everywhere. */
2762 highlight_nonselected_windows
2763 /* Or show region in the selected window. */
2764 || w == XWINDOW (selected_window)
2765 /* Or show the region if we are in the mini-buffer and W is
2766 the window the mini-buffer refers to. */
2767 || (MINI_WINDOW_P (XWINDOW (selected_window))
2768 && WINDOWP (minibuf_selected_window)
2769 && w == XWINDOW (minibuf_selected_window))))
2771 it->region_beg_charpos = min (PT, markpos);
2772 it->region_end_charpos = max (PT, markpos);
2774 else
2775 it->region_beg_charpos = it->region_end_charpos = -1;
2777 /* Get the position at which the redisplay_end_trigger hook should
2778 be run, if it is to be run at all. */
2779 if (MARKERP (w->redisplay_end_trigger)
2780 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2781 it->redisplay_end_trigger_charpos
2782 = marker_position (w->redisplay_end_trigger);
2783 else if (INTEGERP (w->redisplay_end_trigger))
2784 it->redisplay_end_trigger_charpos =
2785 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2787 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2789 /* Are lines in the display truncated? */
2790 if (base_face_id != DEFAULT_FACE_ID
2791 || it->w->hscroll
2792 || (! WINDOW_FULL_WIDTH_P (it->w)
2793 && ((!NILP (Vtruncate_partial_width_windows)
2794 && !INTEGERP (Vtruncate_partial_width_windows))
2795 || (INTEGERP (Vtruncate_partial_width_windows)
2796 && (WINDOW_TOTAL_COLS (it->w)
2797 < XINT (Vtruncate_partial_width_windows))))))
2798 it->line_wrap = TRUNCATE;
2799 else if (NILP (BVAR (current_buffer, truncate_lines)))
2800 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2801 ? WINDOW_WRAP : WORD_WRAP;
2802 else
2803 it->line_wrap = TRUNCATE;
2805 /* Get dimensions of truncation and continuation glyphs. These are
2806 displayed as fringe bitmaps under X, but we need them for such
2807 frames when the fringes are turned off. But leave the dimensions
2808 zero for tooltip frames, as these glyphs look ugly there and also
2809 sabotage calculations of tooltip dimensions in x-show-tip. */
2810 #ifdef HAVE_WINDOW_SYSTEM
2811 if (!(FRAME_WINDOW_P (it->f)
2812 && FRAMEP (tip_frame)
2813 && it->f == XFRAME (tip_frame)))
2814 #endif
2816 if (it->line_wrap == TRUNCATE)
2818 /* We will need the truncation glyph. */
2819 eassert (it->glyph_row == NULL);
2820 produce_special_glyphs (it, IT_TRUNCATION);
2821 it->truncation_pixel_width = it->pixel_width;
2823 else
2825 /* We will need the continuation glyph. */
2826 eassert (it->glyph_row == NULL);
2827 produce_special_glyphs (it, IT_CONTINUATION);
2828 it->continuation_pixel_width = it->pixel_width;
2832 /* Reset these values to zero because the produce_special_glyphs
2833 above has changed them. */
2834 it->pixel_width = it->ascent = it->descent = 0;
2835 it->phys_ascent = it->phys_descent = 0;
2837 /* Set this after getting the dimensions of truncation and
2838 continuation glyphs, so that we don't produce glyphs when calling
2839 produce_special_glyphs, above. */
2840 it->glyph_row = row;
2841 it->area = TEXT_AREA;
2843 /* Forget any previous info about this row being reversed. */
2844 if (it->glyph_row)
2845 it->glyph_row->reversed_p = 0;
2847 /* Get the dimensions of the display area. The display area
2848 consists of the visible window area plus a horizontally scrolled
2849 part to the left of the window. All x-values are relative to the
2850 start of this total display area. */
2851 if (base_face_id != DEFAULT_FACE_ID)
2853 /* Mode lines, menu bar in terminal frames. */
2854 it->first_visible_x = 0;
2855 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2857 else
2859 it->first_visible_x =
2860 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2861 it->last_visible_x = (it->first_visible_x
2862 + window_box_width (w, TEXT_AREA));
2864 /* If we truncate lines, leave room for the truncation glyph(s) at
2865 the right margin. Otherwise, leave room for the continuation
2866 glyph(s). Done only if the window has no fringes. Since we
2867 don't know at this point whether there will be any R2L lines in
2868 the window, we reserve space for truncation/continuation glyphs
2869 even if only one of the fringes is absent. */
2870 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2871 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2873 if (it->line_wrap == TRUNCATE)
2874 it->last_visible_x -= it->truncation_pixel_width;
2875 else
2876 it->last_visible_x -= it->continuation_pixel_width;
2879 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2880 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2883 /* Leave room for a border glyph. */
2884 if (!FRAME_WINDOW_P (it->f)
2885 && !WINDOW_RIGHTMOST_P (it->w))
2886 it->last_visible_x -= 1;
2888 it->last_visible_y = window_text_bottom_y (w);
2890 /* For mode lines and alike, arrange for the first glyph having a
2891 left box line if the face specifies a box. */
2892 if (base_face_id != DEFAULT_FACE_ID)
2894 struct face *face;
2896 it->face_id = remapped_base_face_id;
2898 /* If we have a boxed mode line, make the first character appear
2899 with a left box line. */
2900 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2901 if (face->box != FACE_NO_BOX)
2902 it->start_of_box_run_p = 1;
2905 /* If a buffer position was specified, set the iterator there,
2906 getting overlays and face properties from that position. */
2907 if (charpos >= BUF_BEG (current_buffer))
2909 it->end_charpos = ZV;
2910 eassert (charpos == BYTE_TO_CHAR (bytepos));
2911 IT_CHARPOS (*it) = charpos;
2912 IT_BYTEPOS (*it) = bytepos;
2914 /* We will rely on `reseat' to set this up properly, via
2915 handle_face_prop. */
2916 it->face_id = it->base_face_id;
2918 it->start = it->current;
2919 /* Do we need to reorder bidirectional text? Not if this is a
2920 unibyte buffer: by definition, none of the single-byte
2921 characters are strong R2L, so no reordering is needed. And
2922 bidi.c doesn't support unibyte buffers anyway. Also, don't
2923 reorder while we are loading loadup.el, since the tables of
2924 character properties needed for reordering are not yet
2925 available. */
2926 it->bidi_p =
2927 NILP (Vpurify_flag)
2928 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2929 && it->multibyte_p;
2931 /* If we are to reorder bidirectional text, init the bidi
2932 iterator. */
2933 if (it->bidi_p)
2935 /* Note the paragraph direction that this buffer wants to
2936 use. */
2937 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2938 Qleft_to_right))
2939 it->paragraph_embedding = L2R;
2940 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2941 Qright_to_left))
2942 it->paragraph_embedding = R2L;
2943 else
2944 it->paragraph_embedding = NEUTRAL_DIR;
2945 bidi_unshelve_cache (NULL, 0);
2946 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2947 &it->bidi_it);
2950 /* Compute faces etc. */
2951 reseat (it, it->current.pos, 1);
2954 CHECK_IT (it);
2958 /* Initialize IT for the display of window W with window start POS. */
2960 void
2961 start_display (struct it *it, struct window *w, struct text_pos pos)
2963 struct glyph_row *row;
2964 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2966 row = w->desired_matrix->rows + first_vpos;
2967 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2968 it->first_vpos = first_vpos;
2970 /* Don't reseat to previous visible line start if current start
2971 position is in a string or image. */
2972 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2974 int start_at_line_beg_p;
2975 int first_y = it->current_y;
2977 /* If window start is not at a line start, skip forward to POS to
2978 get the correct continuation lines width. */
2979 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2980 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2981 if (!start_at_line_beg_p)
2983 int new_x;
2985 reseat_at_previous_visible_line_start (it);
2986 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2988 new_x = it->current_x + it->pixel_width;
2990 /* If lines are continued, this line may end in the middle
2991 of a multi-glyph character (e.g. a control character
2992 displayed as \003, or in the middle of an overlay
2993 string). In this case move_it_to above will not have
2994 taken us to the start of the continuation line but to the
2995 end of the continued line. */
2996 if (it->current_x > 0
2997 && it->line_wrap != TRUNCATE /* Lines are continued. */
2998 && (/* And glyph doesn't fit on the line. */
2999 new_x > it->last_visible_x
3000 /* Or it fits exactly and we're on a window
3001 system frame. */
3002 || (new_x == it->last_visible_x
3003 && FRAME_WINDOW_P (it->f)
3004 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3005 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3006 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3008 if ((it->current.dpvec_index >= 0
3009 || it->current.overlay_string_index >= 0)
3010 /* If we are on a newline from a display vector or
3011 overlay string, then we are already at the end of
3012 a screen line; no need to go to the next line in
3013 that case, as this line is not really continued.
3014 (If we do go to the next line, C-e will not DTRT.) */
3015 && it->c != '\n')
3017 set_iterator_to_next (it, 1);
3018 move_it_in_display_line_to (it, -1, -1, 0);
3021 it->continuation_lines_width += it->current_x;
3023 /* If the character at POS is displayed via a display
3024 vector, move_it_to above stops at the final glyph of
3025 IT->dpvec. To make the caller redisplay that character
3026 again (a.k.a. start at POS), we need to reset the
3027 dpvec_index to the beginning of IT->dpvec. */
3028 else if (it->current.dpvec_index >= 0)
3029 it->current.dpvec_index = 0;
3031 /* We're starting a new display line, not affected by the
3032 height of the continued line, so clear the appropriate
3033 fields in the iterator structure. */
3034 it->max_ascent = it->max_descent = 0;
3035 it->max_phys_ascent = it->max_phys_descent = 0;
3037 it->current_y = first_y;
3038 it->vpos = 0;
3039 it->current_x = it->hpos = 0;
3045 /* Return 1 if POS is a position in ellipses displayed for invisible
3046 text. W is the window we display, for text property lookup. */
3048 static int
3049 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3051 Lisp_Object prop, window;
3052 int ellipses_p = 0;
3053 ptrdiff_t charpos = CHARPOS (pos->pos);
3055 /* If POS specifies a position in a display vector, this might
3056 be for an ellipsis displayed for invisible text. We won't
3057 get the iterator set up for delivering that ellipsis unless
3058 we make sure that it gets aware of the invisible text. */
3059 if (pos->dpvec_index >= 0
3060 && pos->overlay_string_index < 0
3061 && CHARPOS (pos->string_pos) < 0
3062 && charpos > BEGV
3063 && (XSETWINDOW (window, w),
3064 prop = Fget_char_property (make_number (charpos),
3065 Qinvisible, window),
3066 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3068 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3069 window);
3070 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3073 return ellipses_p;
3077 /* Initialize IT for stepping through current_buffer in window W,
3078 starting at position POS that includes overlay string and display
3079 vector/ control character translation position information. Value
3080 is zero if there are overlay strings with newlines at POS. */
3082 static int
3083 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3085 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3086 int i, overlay_strings_with_newlines = 0;
3088 /* If POS specifies a position in a display vector, this might
3089 be for an ellipsis displayed for invisible text. We won't
3090 get the iterator set up for delivering that ellipsis unless
3091 we make sure that it gets aware of the invisible text. */
3092 if (in_ellipses_for_invisible_text_p (pos, w))
3094 --charpos;
3095 bytepos = 0;
3098 /* Keep in mind: the call to reseat in init_iterator skips invisible
3099 text, so we might end up at a position different from POS. This
3100 is only a problem when POS is a row start after a newline and an
3101 overlay starts there with an after-string, and the overlay has an
3102 invisible property. Since we don't skip invisible text in
3103 display_line and elsewhere immediately after consuming the
3104 newline before the row start, such a POS will not be in a string,
3105 but the call to init_iterator below will move us to the
3106 after-string. */
3107 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3109 /* This only scans the current chunk -- it should scan all chunks.
3110 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3111 to 16 in 22.1 to make this a lesser problem. */
3112 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3114 const char *s = SSDATA (it->overlay_strings[i]);
3115 const char *e = s + SBYTES (it->overlay_strings[i]);
3117 while (s < e && *s != '\n')
3118 ++s;
3120 if (s < e)
3122 overlay_strings_with_newlines = 1;
3123 break;
3127 /* If position is within an overlay string, set up IT to the right
3128 overlay string. */
3129 if (pos->overlay_string_index >= 0)
3131 int relative_index;
3133 /* If the first overlay string happens to have a `display'
3134 property for an image, the iterator will be set up for that
3135 image, and we have to undo that setup first before we can
3136 correct the overlay string index. */
3137 if (it->method == GET_FROM_IMAGE)
3138 pop_it (it);
3140 /* We already have the first chunk of overlay strings in
3141 IT->overlay_strings. Load more until the one for
3142 pos->overlay_string_index is in IT->overlay_strings. */
3143 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3145 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3146 it->current.overlay_string_index = 0;
3147 while (n--)
3149 load_overlay_strings (it, 0);
3150 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3154 it->current.overlay_string_index = pos->overlay_string_index;
3155 relative_index = (it->current.overlay_string_index
3156 % OVERLAY_STRING_CHUNK_SIZE);
3157 it->string = it->overlay_strings[relative_index];
3158 eassert (STRINGP (it->string));
3159 it->current.string_pos = pos->string_pos;
3160 it->method = GET_FROM_STRING;
3161 it->end_charpos = SCHARS (it->string);
3162 /* Set up the bidi iterator for this overlay string. */
3163 if (it->bidi_p)
3165 it->bidi_it.string.lstring = it->string;
3166 it->bidi_it.string.s = NULL;
3167 it->bidi_it.string.schars = SCHARS (it->string);
3168 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3169 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3170 it->bidi_it.string.unibyte = !it->multibyte_p;
3171 it->bidi_it.w = it->w;
3172 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3173 FRAME_WINDOW_P (it->f), &it->bidi_it);
3175 /* Synchronize the state of the bidi iterator with
3176 pos->string_pos. For any string position other than
3177 zero, this will be done automagically when we resume
3178 iteration over the string and get_visually_first_element
3179 is called. But if string_pos is zero, and the string is
3180 to be reordered for display, we need to resync manually,
3181 since it could be that the iteration state recorded in
3182 pos ended at string_pos of 0 moving backwards in string. */
3183 if (CHARPOS (pos->string_pos) == 0)
3185 get_visually_first_element (it);
3186 if (IT_STRING_CHARPOS (*it) != 0)
3187 do {
3188 /* Paranoia. */
3189 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3190 bidi_move_to_visually_next (&it->bidi_it);
3191 } while (it->bidi_it.charpos != 0);
3193 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3194 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3198 if (CHARPOS (pos->string_pos) >= 0)
3200 /* Recorded position is not in an overlay string, but in another
3201 string. This can only be a string from a `display' property.
3202 IT should already be filled with that string. */
3203 it->current.string_pos = pos->string_pos;
3204 eassert (STRINGP (it->string));
3205 if (it->bidi_p)
3206 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3207 FRAME_WINDOW_P (it->f), &it->bidi_it);
3210 /* Restore position in display vector translations, control
3211 character translations or ellipses. */
3212 if (pos->dpvec_index >= 0)
3214 if (it->dpvec == NULL)
3215 get_next_display_element (it);
3216 eassert (it->dpvec && it->current.dpvec_index == 0);
3217 it->current.dpvec_index = pos->dpvec_index;
3220 CHECK_IT (it);
3221 return !overlay_strings_with_newlines;
3225 /* Initialize IT for stepping through current_buffer in window W
3226 starting at ROW->start. */
3228 static void
3229 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3231 init_from_display_pos (it, w, &row->start);
3232 it->start = row->start;
3233 it->continuation_lines_width = row->continuation_lines_width;
3234 CHECK_IT (it);
3238 /* Initialize IT for stepping through current_buffer in window W
3239 starting in the line following ROW, i.e. starting at ROW->end.
3240 Value is zero if there are overlay strings with newlines at ROW's
3241 end position. */
3243 static int
3244 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3246 int success = 0;
3248 if (init_from_display_pos (it, w, &row->end))
3250 if (row->continued_p)
3251 it->continuation_lines_width
3252 = row->continuation_lines_width + row->pixel_width;
3253 CHECK_IT (it);
3254 success = 1;
3257 return success;
3263 /***********************************************************************
3264 Text properties
3265 ***********************************************************************/
3267 /* Called when IT reaches IT->stop_charpos. Handle text property and
3268 overlay changes. Set IT->stop_charpos to the next position where
3269 to stop. */
3271 static void
3272 handle_stop (struct it *it)
3274 enum prop_handled handled;
3275 int handle_overlay_change_p;
3276 struct props *p;
3278 it->dpvec = NULL;
3279 it->current.dpvec_index = -1;
3280 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3281 it->ignore_overlay_strings_at_pos_p = 0;
3282 it->ellipsis_p = 0;
3284 /* Use face of preceding text for ellipsis (if invisible) */
3285 if (it->selective_display_ellipsis_p)
3286 it->saved_face_id = it->face_id;
3290 handled = HANDLED_NORMALLY;
3292 /* Call text property handlers. */
3293 for (p = it_props; p->handler; ++p)
3295 handled = p->handler (it);
3297 if (handled == HANDLED_RECOMPUTE_PROPS)
3298 break;
3299 else if (handled == HANDLED_RETURN)
3301 /* We still want to show before and after strings from
3302 overlays even if the actual buffer text is replaced. */
3303 if (!handle_overlay_change_p
3304 || it->sp > 1
3305 /* Don't call get_overlay_strings_1 if we already
3306 have overlay strings loaded, because doing so
3307 will load them again and push the iterator state
3308 onto the stack one more time, which is not
3309 expected by the rest of the code that processes
3310 overlay strings. */
3311 || (it->current.overlay_string_index < 0
3312 ? !get_overlay_strings_1 (it, 0, 0)
3313 : 0))
3315 if (it->ellipsis_p)
3316 setup_for_ellipsis (it, 0);
3317 /* When handling a display spec, we might load an
3318 empty string. In that case, discard it here. We
3319 used to discard it in handle_single_display_spec,
3320 but that causes get_overlay_strings_1, above, to
3321 ignore overlay strings that we must check. */
3322 if (STRINGP (it->string) && !SCHARS (it->string))
3323 pop_it (it);
3324 return;
3326 else if (STRINGP (it->string) && !SCHARS (it->string))
3327 pop_it (it);
3328 else
3330 it->ignore_overlay_strings_at_pos_p = 1;
3331 it->string_from_display_prop_p = 0;
3332 it->from_disp_prop_p = 0;
3333 handle_overlay_change_p = 0;
3335 handled = HANDLED_RECOMPUTE_PROPS;
3336 break;
3338 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3339 handle_overlay_change_p = 0;
3342 if (handled != HANDLED_RECOMPUTE_PROPS)
3344 /* Don't check for overlay strings below when set to deliver
3345 characters from a display vector. */
3346 if (it->method == GET_FROM_DISPLAY_VECTOR)
3347 handle_overlay_change_p = 0;
3349 /* Handle overlay changes.
3350 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3351 if it finds overlays. */
3352 if (handle_overlay_change_p)
3353 handled = handle_overlay_change (it);
3356 if (it->ellipsis_p)
3358 setup_for_ellipsis (it, 0);
3359 break;
3362 while (handled == HANDLED_RECOMPUTE_PROPS);
3364 /* Determine where to stop next. */
3365 if (handled == HANDLED_NORMALLY)
3366 compute_stop_pos (it);
3370 /* Compute IT->stop_charpos from text property and overlay change
3371 information for IT's current position. */
3373 static void
3374 compute_stop_pos (struct it *it)
3376 register INTERVAL iv, next_iv;
3377 Lisp_Object object, limit, position;
3378 ptrdiff_t charpos, bytepos;
3380 if (STRINGP (it->string))
3382 /* Strings are usually short, so don't limit the search for
3383 properties. */
3384 it->stop_charpos = it->end_charpos;
3385 object = it->string;
3386 limit = Qnil;
3387 charpos = IT_STRING_CHARPOS (*it);
3388 bytepos = IT_STRING_BYTEPOS (*it);
3390 else
3392 ptrdiff_t pos;
3394 /* If end_charpos is out of range for some reason, such as a
3395 misbehaving display function, rationalize it (Bug#5984). */
3396 if (it->end_charpos > ZV)
3397 it->end_charpos = ZV;
3398 it->stop_charpos = it->end_charpos;
3400 /* If next overlay change is in front of the current stop pos
3401 (which is IT->end_charpos), stop there. Note: value of
3402 next_overlay_change is point-max if no overlay change
3403 follows. */
3404 charpos = IT_CHARPOS (*it);
3405 bytepos = IT_BYTEPOS (*it);
3406 pos = next_overlay_change (charpos);
3407 if (pos < it->stop_charpos)
3408 it->stop_charpos = pos;
3410 /* If showing the region, we have to stop at the region
3411 start or end because the face might change there. */
3412 if (it->region_beg_charpos > 0)
3414 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3415 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3416 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3417 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3420 /* Set up variables for computing the stop position from text
3421 property changes. */
3422 XSETBUFFER (object, current_buffer);
3423 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3426 /* Get the interval containing IT's position. Value is a null
3427 interval if there isn't such an interval. */
3428 position = make_number (charpos);
3429 iv = validate_interval_range (object, &position, &position, 0);
3430 if (iv)
3432 Lisp_Object values_here[LAST_PROP_IDX];
3433 struct props *p;
3435 /* Get properties here. */
3436 for (p = it_props; p->handler; ++p)
3437 values_here[p->idx] = textget (iv->plist, *p->name);
3439 /* Look for an interval following iv that has different
3440 properties. */
3441 for (next_iv = next_interval (iv);
3442 (next_iv
3443 && (NILP (limit)
3444 || XFASTINT (limit) > next_iv->position));
3445 next_iv = next_interval (next_iv))
3447 for (p = it_props; p->handler; ++p)
3449 Lisp_Object new_value;
3451 new_value = textget (next_iv->plist, *p->name);
3452 if (!EQ (values_here[p->idx], new_value))
3453 break;
3456 if (p->handler)
3457 break;
3460 if (next_iv)
3462 if (INTEGERP (limit)
3463 && next_iv->position >= XFASTINT (limit))
3464 /* No text property change up to limit. */
3465 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3466 else
3467 /* Text properties change in next_iv. */
3468 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3472 if (it->cmp_it.id < 0)
3474 ptrdiff_t stoppos = it->end_charpos;
3476 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3477 stoppos = -1;
3478 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3479 stoppos, it->string);
3482 eassert (STRINGP (it->string)
3483 || (it->stop_charpos >= BEGV
3484 && it->stop_charpos >= IT_CHARPOS (*it)));
3488 /* Return the position of the next overlay change after POS in
3489 current_buffer. Value is point-max if no overlay change
3490 follows. This is like `next-overlay-change' but doesn't use
3491 xmalloc. */
3493 static ptrdiff_t
3494 next_overlay_change (ptrdiff_t pos)
3496 ptrdiff_t i, noverlays;
3497 ptrdiff_t endpos;
3498 Lisp_Object *overlays;
3500 /* Get all overlays at the given position. */
3501 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3503 /* If any of these overlays ends before endpos,
3504 use its ending point instead. */
3505 for (i = 0; i < noverlays; ++i)
3507 Lisp_Object oend;
3508 ptrdiff_t oendpos;
3510 oend = OVERLAY_END (overlays[i]);
3511 oendpos = OVERLAY_POSITION (oend);
3512 endpos = min (endpos, oendpos);
3515 return endpos;
3518 /* How many characters forward to search for a display property or
3519 display string. Searching too far forward makes the bidi display
3520 sluggish, especially in small windows. */
3521 #define MAX_DISP_SCAN 250
3523 /* Return the character position of a display string at or after
3524 position specified by POSITION. If no display string exists at or
3525 after POSITION, return ZV. A display string is either an overlay
3526 with `display' property whose value is a string, or a `display'
3527 text property whose value is a string. STRING is data about the
3528 string to iterate; if STRING->lstring is nil, we are iterating a
3529 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3530 on a GUI frame. DISP_PROP is set to zero if we searched
3531 MAX_DISP_SCAN characters forward without finding any display
3532 strings, non-zero otherwise. It is set to 2 if the display string
3533 uses any kind of `(space ...)' spec that will produce a stretch of
3534 white space in the text area. */
3535 ptrdiff_t
3536 compute_display_string_pos (struct text_pos *position,
3537 struct bidi_string_data *string,
3538 struct window *w,
3539 int frame_window_p, int *disp_prop)
3541 /* OBJECT = nil means current buffer. */
3542 Lisp_Object object, object1;
3543 Lisp_Object pos, spec, limpos;
3544 int string_p = (string && (STRINGP (string->lstring) || string->s));
3545 ptrdiff_t eob = string_p ? string->schars : ZV;
3546 ptrdiff_t begb = string_p ? 0 : BEGV;
3547 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3548 ptrdiff_t lim =
3549 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3550 struct text_pos tpos;
3551 int rv = 0;
3553 if (string && STRINGP (string->lstring))
3554 object1 = object = string->lstring;
3555 else if (w && !string_p)
3557 XSETWINDOW (object, w);
3558 object1 = Qnil;
3560 else
3561 object1 = object = Qnil;
3563 *disp_prop = 1;
3565 if (charpos >= eob
3566 /* We don't support display properties whose values are strings
3567 that have display string properties. */
3568 || string->from_disp_str
3569 /* C strings cannot have display properties. */
3570 || (string->s && !STRINGP (object)))
3572 *disp_prop = 0;
3573 return eob;
3576 /* If the character at CHARPOS is where the display string begins,
3577 return CHARPOS. */
3578 pos = make_number (charpos);
3579 if (STRINGP (object))
3580 bufpos = string->bufpos;
3581 else
3582 bufpos = charpos;
3583 tpos = *position;
3584 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3585 && (charpos <= begb
3586 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3587 object),
3588 spec))
3589 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3590 frame_window_p)))
3592 if (rv == 2)
3593 *disp_prop = 2;
3594 return charpos;
3597 /* Look forward for the first character with a `display' property
3598 that will replace the underlying text when displayed. */
3599 limpos = make_number (lim);
3600 do {
3601 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3602 CHARPOS (tpos) = XFASTINT (pos);
3603 if (CHARPOS (tpos) >= lim)
3605 *disp_prop = 0;
3606 break;
3608 if (STRINGP (object))
3609 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3610 else
3611 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3612 spec = Fget_char_property (pos, Qdisplay, object);
3613 if (!STRINGP (object))
3614 bufpos = CHARPOS (tpos);
3615 } while (NILP (spec)
3616 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3617 bufpos, frame_window_p)));
3618 if (rv == 2)
3619 *disp_prop = 2;
3621 return CHARPOS (tpos);
3624 /* Return the character position of the end of the display string that
3625 started at CHARPOS. If there's no display string at CHARPOS,
3626 return -1. A display string is either an overlay with `display'
3627 property whose value is a string or a `display' text property whose
3628 value is a string. */
3629 ptrdiff_t
3630 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3632 /* OBJECT = nil means current buffer. */
3633 Lisp_Object object =
3634 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3635 Lisp_Object pos = make_number (charpos);
3636 ptrdiff_t eob =
3637 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3639 if (charpos >= eob || (string->s && !STRINGP (object)))
3640 return eob;
3642 /* It could happen that the display property or overlay was removed
3643 since we found it in compute_display_string_pos above. One way
3644 this can happen is if JIT font-lock was called (through
3645 handle_fontified_prop), and jit-lock-functions remove text
3646 properties or overlays from the portion of buffer that includes
3647 CHARPOS. Muse mode is known to do that, for example. In this
3648 case, we return -1 to the caller, to signal that no display
3649 string is actually present at CHARPOS. See bidi_fetch_char for
3650 how this is handled.
3652 An alternative would be to never look for display properties past
3653 it->stop_charpos. But neither compute_display_string_pos nor
3654 bidi_fetch_char that calls it know or care where the next
3655 stop_charpos is. */
3656 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3657 return -1;
3659 /* Look forward for the first character where the `display' property
3660 changes. */
3661 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3663 return XFASTINT (pos);
3668 /***********************************************************************
3669 Fontification
3670 ***********************************************************************/
3672 /* Handle changes in the `fontified' property of the current buffer by
3673 calling hook functions from Qfontification_functions to fontify
3674 regions of text. */
3676 static enum prop_handled
3677 handle_fontified_prop (struct it *it)
3679 Lisp_Object prop, pos;
3680 enum prop_handled handled = HANDLED_NORMALLY;
3682 if (!NILP (Vmemory_full))
3683 return handled;
3685 /* Get the value of the `fontified' property at IT's current buffer
3686 position. (The `fontified' property doesn't have a special
3687 meaning in strings.) If the value is nil, call functions from
3688 Qfontification_functions. */
3689 if (!STRINGP (it->string)
3690 && it->s == NULL
3691 && !NILP (Vfontification_functions)
3692 && !NILP (Vrun_hooks)
3693 && (pos = make_number (IT_CHARPOS (*it)),
3694 prop = Fget_char_property (pos, Qfontified, Qnil),
3695 /* Ignore the special cased nil value always present at EOB since
3696 no amount of fontifying will be able to change it. */
3697 NILP (prop) && IT_CHARPOS (*it) < Z))
3699 ptrdiff_t count = SPECPDL_INDEX ();
3700 Lisp_Object val;
3701 struct buffer *obuf = current_buffer;
3702 int begv = BEGV, zv = ZV;
3703 int old_clip_changed = current_buffer->clip_changed;
3705 val = Vfontification_functions;
3706 specbind (Qfontification_functions, Qnil);
3708 eassert (it->end_charpos == ZV);
3710 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3711 safe_call1 (val, pos);
3712 else
3714 Lisp_Object fns, fn;
3715 struct gcpro gcpro1, gcpro2;
3717 fns = Qnil;
3718 GCPRO2 (val, fns);
3720 for (; CONSP (val); val = XCDR (val))
3722 fn = XCAR (val);
3724 if (EQ (fn, Qt))
3726 /* A value of t indicates this hook has a local
3727 binding; it means to run the global binding too.
3728 In a global value, t should not occur. If it
3729 does, we must ignore it to avoid an endless
3730 loop. */
3731 for (fns = Fdefault_value (Qfontification_functions);
3732 CONSP (fns);
3733 fns = XCDR (fns))
3735 fn = XCAR (fns);
3736 if (!EQ (fn, Qt))
3737 safe_call1 (fn, pos);
3740 else
3741 safe_call1 (fn, pos);
3744 UNGCPRO;
3747 unbind_to (count, Qnil);
3749 /* Fontification functions routinely call `save-restriction'.
3750 Normally, this tags clip_changed, which can confuse redisplay
3751 (see discussion in Bug#6671). Since we don't perform any
3752 special handling of fontification changes in the case where
3753 `save-restriction' isn't called, there's no point doing so in
3754 this case either. So, if the buffer's restrictions are
3755 actually left unchanged, reset clip_changed. */
3756 if (obuf == current_buffer)
3758 if (begv == BEGV && zv == ZV)
3759 current_buffer->clip_changed = old_clip_changed;
3761 /* There isn't much we can reasonably do to protect against
3762 misbehaving fontification, but here's a fig leaf. */
3763 else if (BUFFER_LIVE_P (obuf))
3764 set_buffer_internal_1 (obuf);
3766 /* The fontification code may have added/removed text.
3767 It could do even a lot worse, but let's at least protect against
3768 the most obvious case where only the text past `pos' gets changed',
3769 as is/was done in grep.el where some escapes sequences are turned
3770 into face properties (bug#7876). */
3771 it->end_charpos = ZV;
3773 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3774 something. This avoids an endless loop if they failed to
3775 fontify the text for which reason ever. */
3776 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3777 handled = HANDLED_RECOMPUTE_PROPS;
3780 return handled;
3785 /***********************************************************************
3786 Faces
3787 ***********************************************************************/
3789 /* Set up iterator IT from face properties at its current position.
3790 Called from handle_stop. */
3792 static enum prop_handled
3793 handle_face_prop (struct it *it)
3795 int new_face_id;
3796 ptrdiff_t next_stop;
3798 if (!STRINGP (it->string))
3800 new_face_id
3801 = face_at_buffer_position (it->w,
3802 IT_CHARPOS (*it),
3803 it->region_beg_charpos,
3804 it->region_end_charpos,
3805 &next_stop,
3806 (IT_CHARPOS (*it)
3807 + TEXT_PROP_DISTANCE_LIMIT),
3808 0, it->base_face_id);
3810 /* Is this a start of a run of characters with box face?
3811 Caveat: this can be called for a freshly initialized
3812 iterator; face_id is -1 in this case. We know that the new
3813 face will not change until limit, i.e. if the new face has a
3814 box, all characters up to limit will have one. But, as
3815 usual, we don't know whether limit is really the end. */
3816 if (new_face_id != it->face_id)
3818 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3819 /* If it->face_id is -1, old_face below will be NULL, see
3820 the definition of FACE_FROM_ID. This will happen if this
3821 is the initial call that gets the face. */
3822 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3824 /* If the value of face_id of the iterator is -1, we have to
3825 look in front of IT's position and see whether there is a
3826 face there that's different from new_face_id. */
3827 if (!old_face && IT_CHARPOS (*it) > BEG)
3829 int prev_face_id = face_before_it_pos (it);
3831 old_face = FACE_FROM_ID (it->f, prev_face_id);
3834 /* If the new face has a box, but the old face does not,
3835 this is the start of a run of characters with box face,
3836 i.e. this character has a shadow on the left side. */
3837 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3838 && (old_face == NULL || !old_face->box));
3839 it->face_box_p = new_face->box != FACE_NO_BOX;
3842 else
3844 int base_face_id;
3845 ptrdiff_t bufpos;
3846 int i;
3847 Lisp_Object from_overlay
3848 = (it->current.overlay_string_index >= 0
3849 ? it->string_overlays[it->current.overlay_string_index
3850 % OVERLAY_STRING_CHUNK_SIZE]
3851 : Qnil);
3853 /* See if we got to this string directly or indirectly from
3854 an overlay property. That includes the before-string or
3855 after-string of an overlay, strings in display properties
3856 provided by an overlay, their text properties, etc.
3858 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3859 if (! NILP (from_overlay))
3860 for (i = it->sp - 1; i >= 0; i--)
3862 if (it->stack[i].current.overlay_string_index >= 0)
3863 from_overlay
3864 = it->string_overlays[it->stack[i].current.overlay_string_index
3865 % OVERLAY_STRING_CHUNK_SIZE];
3866 else if (! NILP (it->stack[i].from_overlay))
3867 from_overlay = it->stack[i].from_overlay;
3869 if (!NILP (from_overlay))
3870 break;
3873 if (! NILP (from_overlay))
3875 bufpos = IT_CHARPOS (*it);
3876 /* For a string from an overlay, the base face depends
3877 only on text properties and ignores overlays. */
3878 base_face_id
3879 = face_for_overlay_string (it->w,
3880 IT_CHARPOS (*it),
3881 it->region_beg_charpos,
3882 it->region_end_charpos,
3883 &next_stop,
3884 (IT_CHARPOS (*it)
3885 + TEXT_PROP_DISTANCE_LIMIT),
3887 from_overlay);
3889 else
3891 bufpos = 0;
3893 /* For strings from a `display' property, use the face at
3894 IT's current buffer position as the base face to merge
3895 with, so that overlay strings appear in the same face as
3896 surrounding text, unless they specify their own faces.
3897 For strings from wrap-prefix and line-prefix properties,
3898 use the default face, possibly remapped via
3899 Vface_remapping_alist. */
3900 base_face_id = it->string_from_prefix_prop_p
3901 ? (!NILP (Vface_remapping_alist)
3902 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3903 : DEFAULT_FACE_ID)
3904 : underlying_face_id (it);
3907 new_face_id = face_at_string_position (it->w,
3908 it->string,
3909 IT_STRING_CHARPOS (*it),
3910 bufpos,
3911 it->region_beg_charpos,
3912 it->region_end_charpos,
3913 &next_stop,
3914 base_face_id, 0);
3916 /* Is this a start of a run of characters with box? Caveat:
3917 this can be called for a freshly allocated iterator; face_id
3918 is -1 is this case. We know that the new face will not
3919 change until the next check pos, i.e. if the new face has a
3920 box, all characters up to that position will have a
3921 box. But, as usual, we don't know whether that position
3922 is really the end. */
3923 if (new_face_id != it->face_id)
3925 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3926 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3928 /* If new face has a box but old face hasn't, this is the
3929 start of a run of characters with box, i.e. it has a
3930 shadow on the left side. */
3931 it->start_of_box_run_p
3932 = new_face->box && (old_face == NULL || !old_face->box);
3933 it->face_box_p = new_face->box != FACE_NO_BOX;
3937 it->face_id = new_face_id;
3938 return HANDLED_NORMALLY;
3942 /* Return the ID of the face ``underlying'' IT's current position,
3943 which is in a string. If the iterator is associated with a
3944 buffer, return the face at IT's current buffer position.
3945 Otherwise, use the iterator's base_face_id. */
3947 static int
3948 underlying_face_id (struct it *it)
3950 int face_id = it->base_face_id, i;
3952 eassert (STRINGP (it->string));
3954 for (i = it->sp - 1; i >= 0; --i)
3955 if (NILP (it->stack[i].string))
3956 face_id = it->stack[i].face_id;
3958 return face_id;
3962 /* Compute the face one character before or after the current position
3963 of IT, in the visual order. BEFORE_P non-zero means get the face
3964 in front (to the left in L2R paragraphs, to the right in R2L
3965 paragraphs) of IT's screen position. Value is the ID of the face. */
3967 static int
3968 face_before_or_after_it_pos (struct it *it, int before_p)
3970 int face_id, limit;
3971 ptrdiff_t next_check_charpos;
3972 struct it it_copy;
3973 void *it_copy_data = NULL;
3975 eassert (it->s == NULL);
3977 if (STRINGP (it->string))
3979 ptrdiff_t bufpos, charpos;
3980 int base_face_id;
3982 /* No face change past the end of the string (for the case
3983 we are padding with spaces). No face change before the
3984 string start. */
3985 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3986 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3987 return it->face_id;
3989 if (!it->bidi_p)
3991 /* Set charpos to the position before or after IT's current
3992 position, in the logical order, which in the non-bidi
3993 case is the same as the visual order. */
3994 if (before_p)
3995 charpos = IT_STRING_CHARPOS (*it) - 1;
3996 else if (it->what == IT_COMPOSITION)
3997 /* For composition, we must check the character after the
3998 composition. */
3999 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4000 else
4001 charpos = IT_STRING_CHARPOS (*it) + 1;
4003 else
4005 if (before_p)
4007 /* With bidi iteration, the character before the current
4008 in the visual order cannot be found by simple
4009 iteration, because "reverse" reordering is not
4010 supported. Instead, we need to use the move_it_*
4011 family of functions. */
4012 /* Ignore face changes before the first visible
4013 character on this display line. */
4014 if (it->current_x <= it->first_visible_x)
4015 return it->face_id;
4016 SAVE_IT (it_copy, *it, it_copy_data);
4017 /* Implementation note: Since move_it_in_display_line
4018 works in the iterator geometry, and thinks the first
4019 character is always the leftmost, even in R2L lines,
4020 we don't need to distinguish between the R2L and L2R
4021 cases here. */
4022 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4023 it_copy.current_x - 1, MOVE_TO_X);
4024 charpos = IT_STRING_CHARPOS (it_copy);
4025 RESTORE_IT (it, it, it_copy_data);
4027 else
4029 /* Set charpos to the string position of the character
4030 that comes after IT's current position in the visual
4031 order. */
4032 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4034 it_copy = *it;
4035 while (n--)
4036 bidi_move_to_visually_next (&it_copy.bidi_it);
4038 charpos = it_copy.bidi_it.charpos;
4041 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4043 if (it->current.overlay_string_index >= 0)
4044 bufpos = IT_CHARPOS (*it);
4045 else
4046 bufpos = 0;
4048 base_face_id = underlying_face_id (it);
4050 /* Get the face for ASCII, or unibyte. */
4051 face_id = face_at_string_position (it->w,
4052 it->string,
4053 charpos,
4054 bufpos,
4055 it->region_beg_charpos,
4056 it->region_end_charpos,
4057 &next_check_charpos,
4058 base_face_id, 0);
4060 /* Correct the face for charsets different from ASCII. Do it
4061 for the multibyte case only. The face returned above is
4062 suitable for unibyte text if IT->string is unibyte. */
4063 if (STRING_MULTIBYTE (it->string))
4065 struct text_pos pos1 = string_pos (charpos, it->string);
4066 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4067 int c, len;
4068 struct face *face = FACE_FROM_ID (it->f, face_id);
4070 c = string_char_and_length (p, &len);
4071 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4074 else
4076 struct text_pos pos;
4078 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4079 || (IT_CHARPOS (*it) <= BEGV && before_p))
4080 return it->face_id;
4082 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4083 pos = it->current.pos;
4085 if (!it->bidi_p)
4087 if (before_p)
4088 DEC_TEXT_POS (pos, it->multibyte_p);
4089 else
4091 if (it->what == IT_COMPOSITION)
4093 /* For composition, we must check the position after
4094 the composition. */
4095 pos.charpos += it->cmp_it.nchars;
4096 pos.bytepos += it->len;
4098 else
4099 INC_TEXT_POS (pos, it->multibyte_p);
4102 else
4104 if (before_p)
4106 /* With bidi iteration, the character before the current
4107 in the visual order cannot be found by simple
4108 iteration, because "reverse" reordering is not
4109 supported. Instead, we need to use the move_it_*
4110 family of functions. */
4111 /* Ignore face changes before the first visible
4112 character on this display line. */
4113 if (it->current_x <= it->first_visible_x)
4114 return it->face_id;
4115 SAVE_IT (it_copy, *it, it_copy_data);
4116 /* Implementation note: Since move_it_in_display_line
4117 works in the iterator geometry, and thinks the first
4118 character is always the leftmost, even in R2L lines,
4119 we don't need to distinguish between the R2L and L2R
4120 cases here. */
4121 move_it_in_display_line (&it_copy, ZV,
4122 it_copy.current_x - 1, MOVE_TO_X);
4123 pos = it_copy.current.pos;
4124 RESTORE_IT (it, it, it_copy_data);
4126 else
4128 /* Set charpos to the buffer position of the character
4129 that comes after IT's current position in the visual
4130 order. */
4131 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4133 it_copy = *it;
4134 while (n--)
4135 bidi_move_to_visually_next (&it_copy.bidi_it);
4137 SET_TEXT_POS (pos,
4138 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4141 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4143 /* Determine face for CHARSET_ASCII, or unibyte. */
4144 face_id = face_at_buffer_position (it->w,
4145 CHARPOS (pos),
4146 it->region_beg_charpos,
4147 it->region_end_charpos,
4148 &next_check_charpos,
4149 limit, 0, -1);
4151 /* Correct the face for charsets different from ASCII. Do it
4152 for the multibyte case only. The face returned above is
4153 suitable for unibyte text if current_buffer is unibyte. */
4154 if (it->multibyte_p)
4156 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4157 struct face *face = FACE_FROM_ID (it->f, face_id);
4158 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4162 return face_id;
4167 /***********************************************************************
4168 Invisible text
4169 ***********************************************************************/
4171 /* Set up iterator IT from invisible properties at its current
4172 position. Called from handle_stop. */
4174 static enum prop_handled
4175 handle_invisible_prop (struct it *it)
4177 enum prop_handled handled = HANDLED_NORMALLY;
4178 int invis_p;
4179 Lisp_Object prop;
4181 if (STRINGP (it->string))
4183 Lisp_Object end_charpos, limit, charpos;
4185 /* Get the value of the invisible text property at the
4186 current position. Value will be nil if there is no such
4187 property. */
4188 charpos = make_number (IT_STRING_CHARPOS (*it));
4189 prop = Fget_text_property (charpos, Qinvisible, it->string);
4190 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4192 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4194 /* Record whether we have to display an ellipsis for the
4195 invisible text. */
4196 int display_ellipsis_p = (invis_p == 2);
4197 ptrdiff_t len, endpos;
4199 handled = HANDLED_RECOMPUTE_PROPS;
4201 /* Get the position at which the next visible text can be
4202 found in IT->string, if any. */
4203 endpos = len = SCHARS (it->string);
4204 XSETINT (limit, len);
4207 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4208 it->string, limit);
4209 if (INTEGERP (end_charpos))
4211 endpos = XFASTINT (end_charpos);
4212 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4213 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4214 if (invis_p == 2)
4215 display_ellipsis_p = 1;
4218 while (invis_p && endpos < len);
4220 if (display_ellipsis_p)
4221 it->ellipsis_p = 1;
4223 if (endpos < len)
4225 /* Text at END_CHARPOS is visible. Move IT there. */
4226 struct text_pos old;
4227 ptrdiff_t oldpos;
4229 old = it->current.string_pos;
4230 oldpos = CHARPOS (old);
4231 if (it->bidi_p)
4233 if (it->bidi_it.first_elt
4234 && it->bidi_it.charpos < SCHARS (it->string))
4235 bidi_paragraph_init (it->paragraph_embedding,
4236 &it->bidi_it, 1);
4237 /* Bidi-iterate out of the invisible text. */
4240 bidi_move_to_visually_next (&it->bidi_it);
4242 while (oldpos <= it->bidi_it.charpos
4243 && it->bidi_it.charpos < endpos);
4245 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4246 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4247 if (IT_CHARPOS (*it) >= endpos)
4248 it->prev_stop = endpos;
4250 else
4252 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4253 compute_string_pos (&it->current.string_pos, old, it->string);
4256 else
4258 /* The rest of the string is invisible. If this is an
4259 overlay string, proceed with the next overlay string
4260 or whatever comes and return a character from there. */
4261 if (it->current.overlay_string_index >= 0
4262 && !display_ellipsis_p)
4264 next_overlay_string (it);
4265 /* Don't check for overlay strings when we just
4266 finished processing them. */
4267 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4269 else
4271 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4272 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4277 else
4279 ptrdiff_t newpos, next_stop, start_charpos, tem;
4280 Lisp_Object pos, overlay;
4282 /* First of all, is there invisible text at this position? */
4283 tem = start_charpos = IT_CHARPOS (*it);
4284 pos = make_number (tem);
4285 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4286 &overlay);
4287 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4289 /* If we are on invisible text, skip over it. */
4290 if (invis_p && start_charpos < it->end_charpos)
4292 /* Record whether we have to display an ellipsis for the
4293 invisible text. */
4294 int display_ellipsis_p = invis_p == 2;
4296 handled = HANDLED_RECOMPUTE_PROPS;
4298 /* Loop skipping over invisible text. The loop is left at
4299 ZV or with IT on the first char being visible again. */
4302 /* Try to skip some invisible text. Return value is the
4303 position reached which can be equal to where we start
4304 if there is nothing invisible there. This skips both
4305 over invisible text properties and overlays with
4306 invisible property. */
4307 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4309 /* If we skipped nothing at all we weren't at invisible
4310 text in the first place. If everything to the end of
4311 the buffer was skipped, end the loop. */
4312 if (newpos == tem || newpos >= ZV)
4313 invis_p = 0;
4314 else
4316 /* We skipped some characters but not necessarily
4317 all there are. Check if we ended up on visible
4318 text. Fget_char_property returns the property of
4319 the char before the given position, i.e. if we
4320 get invis_p = 0, this means that the char at
4321 newpos is visible. */
4322 pos = make_number (newpos);
4323 prop = Fget_char_property (pos, Qinvisible, it->window);
4324 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4327 /* If we ended up on invisible text, proceed to
4328 skip starting with next_stop. */
4329 if (invis_p)
4330 tem = next_stop;
4332 /* If there are adjacent invisible texts, don't lose the
4333 second one's ellipsis. */
4334 if (invis_p == 2)
4335 display_ellipsis_p = 1;
4337 while (invis_p);
4339 /* The position newpos is now either ZV or on visible text. */
4340 if (it->bidi_p)
4342 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4343 int on_newline =
4344 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4345 int after_newline =
4346 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4348 /* If the invisible text ends on a newline or on a
4349 character after a newline, we can avoid the costly,
4350 character by character, bidi iteration to NEWPOS, and
4351 instead simply reseat the iterator there. That's
4352 because all bidi reordering information is tossed at
4353 the newline. This is a big win for modes that hide
4354 complete lines, like Outline, Org, etc. */
4355 if (on_newline || after_newline)
4357 struct text_pos tpos;
4358 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4360 SET_TEXT_POS (tpos, newpos, bpos);
4361 reseat_1 (it, tpos, 0);
4362 /* If we reseat on a newline/ZV, we need to prep the
4363 bidi iterator for advancing to the next character
4364 after the newline/EOB, keeping the current paragraph
4365 direction (so that PRODUCE_GLYPHS does TRT wrt
4366 prepending/appending glyphs to a glyph row). */
4367 if (on_newline)
4369 it->bidi_it.first_elt = 0;
4370 it->bidi_it.paragraph_dir = pdir;
4371 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4372 it->bidi_it.nchars = 1;
4373 it->bidi_it.ch_len = 1;
4376 else /* Must use the slow method. */
4378 /* With bidi iteration, the region of invisible text
4379 could start and/or end in the middle of a
4380 non-base embedding level. Therefore, we need to
4381 skip invisible text using the bidi iterator,
4382 starting at IT's current position, until we find
4383 ourselves outside of the invisible text.
4384 Skipping invisible text _after_ bidi iteration
4385 avoids affecting the visual order of the
4386 displayed text when invisible properties are
4387 added or removed. */
4388 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4390 /* If we were `reseat'ed to a new paragraph,
4391 determine the paragraph base direction. We
4392 need to do it now because
4393 next_element_from_buffer may not have a
4394 chance to do it, if we are going to skip any
4395 text at the beginning, which resets the
4396 FIRST_ELT flag. */
4397 bidi_paragraph_init (it->paragraph_embedding,
4398 &it->bidi_it, 1);
4402 bidi_move_to_visually_next (&it->bidi_it);
4404 while (it->stop_charpos <= it->bidi_it.charpos
4405 && it->bidi_it.charpos < newpos);
4406 IT_CHARPOS (*it) = it->bidi_it.charpos;
4407 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4408 /* If we overstepped NEWPOS, record its position in
4409 the iterator, so that we skip invisible text if
4410 later the bidi iteration lands us in the
4411 invisible region again. */
4412 if (IT_CHARPOS (*it) >= newpos)
4413 it->prev_stop = newpos;
4416 else
4418 IT_CHARPOS (*it) = newpos;
4419 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4422 /* If there are before-strings at the start of invisible
4423 text, and the text is invisible because of a text
4424 property, arrange to show before-strings because 20.x did
4425 it that way. (If the text is invisible because of an
4426 overlay property instead of a text property, this is
4427 already handled in the overlay code.) */
4428 if (NILP (overlay)
4429 && get_overlay_strings (it, it->stop_charpos))
4431 handled = HANDLED_RECOMPUTE_PROPS;
4432 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4434 else if (display_ellipsis_p)
4436 /* Make sure that the glyphs of the ellipsis will get
4437 correct `charpos' values. If we would not update
4438 it->position here, the glyphs would belong to the
4439 last visible character _before_ the invisible
4440 text, which confuses `set_cursor_from_row'.
4442 We use the last invisible position instead of the
4443 first because this way the cursor is always drawn on
4444 the first "." of the ellipsis, whenever PT is inside
4445 the invisible text. Otherwise the cursor would be
4446 placed _after_ the ellipsis when the point is after the
4447 first invisible character. */
4448 if (!STRINGP (it->object))
4450 it->position.charpos = newpos - 1;
4451 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4453 it->ellipsis_p = 1;
4454 /* Let the ellipsis display before
4455 considering any properties of the following char.
4456 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4457 handled = HANDLED_RETURN;
4462 return handled;
4466 /* Make iterator IT return `...' next.
4467 Replaces LEN characters from buffer. */
4469 static void
4470 setup_for_ellipsis (struct it *it, int len)
4472 /* Use the display table definition for `...'. Invalid glyphs
4473 will be handled by the method returning elements from dpvec. */
4474 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4476 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4477 it->dpvec = v->contents;
4478 it->dpend = v->contents + v->header.size;
4480 else
4482 /* Default `...'. */
4483 it->dpvec = default_invis_vector;
4484 it->dpend = default_invis_vector + 3;
4487 it->dpvec_char_len = len;
4488 it->current.dpvec_index = 0;
4489 it->dpvec_face_id = -1;
4491 /* Remember the current face id in case glyphs specify faces.
4492 IT's face is restored in set_iterator_to_next.
4493 saved_face_id was set to preceding char's face in handle_stop. */
4494 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4495 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4497 it->method = GET_FROM_DISPLAY_VECTOR;
4498 it->ellipsis_p = 1;
4503 /***********************************************************************
4504 'display' property
4505 ***********************************************************************/
4507 /* Set up iterator IT from `display' property at its current position.
4508 Called from handle_stop.
4509 We return HANDLED_RETURN if some part of the display property
4510 overrides the display of the buffer text itself.
4511 Otherwise we return HANDLED_NORMALLY. */
4513 static enum prop_handled
4514 handle_display_prop (struct it *it)
4516 Lisp_Object propval, object, overlay;
4517 struct text_pos *position;
4518 ptrdiff_t bufpos;
4519 /* Nonzero if some property replaces the display of the text itself. */
4520 int display_replaced_p = 0;
4522 if (STRINGP (it->string))
4524 object = it->string;
4525 position = &it->current.string_pos;
4526 bufpos = CHARPOS (it->current.pos);
4528 else
4530 XSETWINDOW (object, it->w);
4531 position = &it->current.pos;
4532 bufpos = CHARPOS (*position);
4535 /* Reset those iterator values set from display property values. */
4536 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4537 it->space_width = Qnil;
4538 it->font_height = Qnil;
4539 it->voffset = 0;
4541 /* We don't support recursive `display' properties, i.e. string
4542 values that have a string `display' property, that have a string
4543 `display' property etc. */
4544 if (!it->string_from_display_prop_p)
4545 it->area = TEXT_AREA;
4547 propval = get_char_property_and_overlay (make_number (position->charpos),
4548 Qdisplay, object, &overlay);
4549 if (NILP (propval))
4550 return HANDLED_NORMALLY;
4551 /* Now OVERLAY is the overlay that gave us this property, or nil
4552 if it was a text property. */
4554 if (!STRINGP (it->string))
4555 object = it->w->contents;
4557 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4558 position, bufpos,
4559 FRAME_WINDOW_P (it->f));
4561 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4564 /* Subroutine of handle_display_prop. Returns non-zero if the display
4565 specification in SPEC is a replacing specification, i.e. it would
4566 replace the text covered by `display' property with something else,
4567 such as an image or a display string. If SPEC includes any kind or
4568 `(space ...) specification, the value is 2; this is used by
4569 compute_display_string_pos, which see.
4571 See handle_single_display_spec for documentation of arguments.
4572 frame_window_p is non-zero if the window being redisplayed is on a
4573 GUI frame; this argument is used only if IT is NULL, see below.
4575 IT can be NULL, if this is called by the bidi reordering code
4576 through compute_display_string_pos, which see. In that case, this
4577 function only examines SPEC, but does not otherwise "handle" it, in
4578 the sense that it doesn't set up members of IT from the display
4579 spec. */
4580 static int
4581 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4582 Lisp_Object overlay, struct text_pos *position,
4583 ptrdiff_t bufpos, int frame_window_p)
4585 int replacing_p = 0;
4586 int rv;
4588 if (CONSP (spec)
4589 /* Simple specifications. */
4590 && !EQ (XCAR (spec), Qimage)
4591 && !EQ (XCAR (spec), Qspace)
4592 && !EQ (XCAR (spec), Qwhen)
4593 && !EQ (XCAR (spec), Qslice)
4594 && !EQ (XCAR (spec), Qspace_width)
4595 && !EQ (XCAR (spec), Qheight)
4596 && !EQ (XCAR (spec), Qraise)
4597 /* Marginal area specifications. */
4598 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4599 && !EQ (XCAR (spec), Qleft_fringe)
4600 && !EQ (XCAR (spec), Qright_fringe)
4601 && !NILP (XCAR (spec)))
4603 for (; CONSP (spec); spec = XCDR (spec))
4605 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4606 overlay, position, bufpos,
4607 replacing_p, frame_window_p)))
4609 replacing_p = rv;
4610 /* If some text in a string is replaced, `position' no
4611 longer points to the position of `object'. */
4612 if (!it || STRINGP (object))
4613 break;
4617 else if (VECTORP (spec))
4619 ptrdiff_t i;
4620 for (i = 0; i < ASIZE (spec); ++i)
4621 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4622 overlay, position, bufpos,
4623 replacing_p, frame_window_p)))
4625 replacing_p = rv;
4626 /* If some text in a string is replaced, `position' no
4627 longer points to the position of `object'. */
4628 if (!it || STRINGP (object))
4629 break;
4632 else
4634 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4635 position, bufpos, 0,
4636 frame_window_p)))
4637 replacing_p = rv;
4640 return replacing_p;
4643 /* Value is the position of the end of the `display' property starting
4644 at START_POS in OBJECT. */
4646 static struct text_pos
4647 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4649 Lisp_Object end;
4650 struct text_pos end_pos;
4652 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4653 Qdisplay, object, Qnil);
4654 CHARPOS (end_pos) = XFASTINT (end);
4655 if (STRINGP (object))
4656 compute_string_pos (&end_pos, start_pos, it->string);
4657 else
4658 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4660 return end_pos;
4664 /* Set up IT from a single `display' property specification SPEC. OBJECT
4665 is the object in which the `display' property was found. *POSITION
4666 is the position in OBJECT at which the `display' property was found.
4667 BUFPOS is the buffer position of OBJECT (different from POSITION if
4668 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4669 previously saw a display specification which already replaced text
4670 display with something else, for example an image; we ignore such
4671 properties after the first one has been processed.
4673 OVERLAY is the overlay this `display' property came from,
4674 or nil if it was a text property.
4676 If SPEC is a `space' or `image' specification, and in some other
4677 cases too, set *POSITION to the position where the `display'
4678 property ends.
4680 If IT is NULL, only examine the property specification in SPEC, but
4681 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4682 is intended to be displayed in a window on a GUI frame.
4684 Value is non-zero if something was found which replaces the display
4685 of buffer or string text. */
4687 static int
4688 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4689 Lisp_Object overlay, struct text_pos *position,
4690 ptrdiff_t bufpos, int display_replaced_p,
4691 int frame_window_p)
4693 Lisp_Object form;
4694 Lisp_Object location, value;
4695 struct text_pos start_pos = *position;
4696 int valid_p;
4698 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4699 If the result is non-nil, use VALUE instead of SPEC. */
4700 form = Qt;
4701 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4703 spec = XCDR (spec);
4704 if (!CONSP (spec))
4705 return 0;
4706 form = XCAR (spec);
4707 spec = XCDR (spec);
4710 if (!NILP (form) && !EQ (form, Qt))
4712 ptrdiff_t count = SPECPDL_INDEX ();
4713 struct gcpro gcpro1;
4715 /* Bind `object' to the object having the `display' property, a
4716 buffer or string. Bind `position' to the position in the
4717 object where the property was found, and `buffer-position'
4718 to the current position in the buffer. */
4720 if (NILP (object))
4721 XSETBUFFER (object, current_buffer);
4722 specbind (Qobject, object);
4723 specbind (Qposition, make_number (CHARPOS (*position)));
4724 specbind (Qbuffer_position, make_number (bufpos));
4725 GCPRO1 (form);
4726 form = safe_eval (form);
4727 UNGCPRO;
4728 unbind_to (count, Qnil);
4731 if (NILP (form))
4732 return 0;
4734 /* Handle `(height HEIGHT)' specifications. */
4735 if (CONSP (spec)
4736 && EQ (XCAR (spec), Qheight)
4737 && CONSP (XCDR (spec)))
4739 if (it)
4741 if (!FRAME_WINDOW_P (it->f))
4742 return 0;
4744 it->font_height = XCAR (XCDR (spec));
4745 if (!NILP (it->font_height))
4747 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4748 int new_height = -1;
4750 if (CONSP (it->font_height)
4751 && (EQ (XCAR (it->font_height), Qplus)
4752 || EQ (XCAR (it->font_height), Qminus))
4753 && CONSP (XCDR (it->font_height))
4754 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4756 /* `(+ N)' or `(- N)' where N is an integer. */
4757 int steps = XINT (XCAR (XCDR (it->font_height)));
4758 if (EQ (XCAR (it->font_height), Qplus))
4759 steps = - steps;
4760 it->face_id = smaller_face (it->f, it->face_id, steps);
4762 else if (FUNCTIONP (it->font_height))
4764 /* Call function with current height as argument.
4765 Value is the new height. */
4766 Lisp_Object height;
4767 height = safe_call1 (it->font_height,
4768 face->lface[LFACE_HEIGHT_INDEX]);
4769 if (NUMBERP (height))
4770 new_height = XFLOATINT (height);
4772 else if (NUMBERP (it->font_height))
4774 /* Value is a multiple of the canonical char height. */
4775 struct face *f;
4777 f = FACE_FROM_ID (it->f,
4778 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4779 new_height = (XFLOATINT (it->font_height)
4780 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4782 else
4784 /* Evaluate IT->font_height with `height' bound to the
4785 current specified height to get the new height. */
4786 ptrdiff_t count = SPECPDL_INDEX ();
4788 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4789 value = safe_eval (it->font_height);
4790 unbind_to (count, Qnil);
4792 if (NUMBERP (value))
4793 new_height = XFLOATINT (value);
4796 if (new_height > 0)
4797 it->face_id = face_with_height (it->f, it->face_id, new_height);
4801 return 0;
4804 /* Handle `(space-width WIDTH)'. */
4805 if (CONSP (spec)
4806 && EQ (XCAR (spec), Qspace_width)
4807 && CONSP (XCDR (spec)))
4809 if (it)
4811 if (!FRAME_WINDOW_P (it->f))
4812 return 0;
4814 value = XCAR (XCDR (spec));
4815 if (NUMBERP (value) && XFLOATINT (value) > 0)
4816 it->space_width = value;
4819 return 0;
4822 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4823 if (CONSP (spec)
4824 && EQ (XCAR (spec), Qslice))
4826 Lisp_Object tem;
4828 if (it)
4830 if (!FRAME_WINDOW_P (it->f))
4831 return 0;
4833 if (tem = XCDR (spec), CONSP (tem))
4835 it->slice.x = XCAR (tem);
4836 if (tem = XCDR (tem), CONSP (tem))
4838 it->slice.y = XCAR (tem);
4839 if (tem = XCDR (tem), CONSP (tem))
4841 it->slice.width = XCAR (tem);
4842 if (tem = XCDR (tem), CONSP (tem))
4843 it->slice.height = XCAR (tem);
4849 return 0;
4852 /* Handle `(raise FACTOR)'. */
4853 if (CONSP (spec)
4854 && EQ (XCAR (spec), Qraise)
4855 && CONSP (XCDR (spec)))
4857 if (it)
4859 if (!FRAME_WINDOW_P (it->f))
4860 return 0;
4862 #ifdef HAVE_WINDOW_SYSTEM
4863 value = XCAR (XCDR (spec));
4864 if (NUMBERP (value))
4866 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4867 it->voffset = - (XFLOATINT (value)
4868 * (FONT_HEIGHT (face->font)));
4870 #endif /* HAVE_WINDOW_SYSTEM */
4873 return 0;
4876 /* Don't handle the other kinds of display specifications
4877 inside a string that we got from a `display' property. */
4878 if (it && it->string_from_display_prop_p)
4879 return 0;
4881 /* Characters having this form of property are not displayed, so
4882 we have to find the end of the property. */
4883 if (it)
4885 start_pos = *position;
4886 *position = display_prop_end (it, object, start_pos);
4888 value = Qnil;
4890 /* Stop the scan at that end position--we assume that all
4891 text properties change there. */
4892 if (it)
4893 it->stop_charpos = position->charpos;
4895 /* Handle `(left-fringe BITMAP [FACE])'
4896 and `(right-fringe BITMAP [FACE])'. */
4897 if (CONSP (spec)
4898 && (EQ (XCAR (spec), Qleft_fringe)
4899 || EQ (XCAR (spec), Qright_fringe))
4900 && CONSP (XCDR (spec)))
4902 int fringe_bitmap;
4904 if (it)
4906 if (!FRAME_WINDOW_P (it->f))
4907 /* If we return here, POSITION has been advanced
4908 across the text with this property. */
4910 /* Synchronize the bidi iterator with POSITION. This is
4911 needed because we are not going to push the iterator
4912 on behalf of this display property, so there will be
4913 no pop_it call to do this synchronization for us. */
4914 if (it->bidi_p)
4916 it->position = *position;
4917 iterate_out_of_display_property (it);
4918 *position = it->position;
4920 return 1;
4923 else if (!frame_window_p)
4924 return 1;
4926 #ifdef HAVE_WINDOW_SYSTEM
4927 value = XCAR (XCDR (spec));
4928 if (!SYMBOLP (value)
4929 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4930 /* If we return here, POSITION has been advanced
4931 across the text with this property. */
4933 if (it && it->bidi_p)
4935 it->position = *position;
4936 iterate_out_of_display_property (it);
4937 *position = it->position;
4939 return 1;
4942 if (it)
4944 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4946 if (CONSP (XCDR (XCDR (spec))))
4948 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4949 int face_id2 = lookup_derived_face (it->f, face_name,
4950 FRINGE_FACE_ID, 0);
4951 if (face_id2 >= 0)
4952 face_id = face_id2;
4955 /* Save current settings of IT so that we can restore them
4956 when we are finished with the glyph property value. */
4957 push_it (it, position);
4959 it->area = TEXT_AREA;
4960 it->what = IT_IMAGE;
4961 it->image_id = -1; /* no image */
4962 it->position = start_pos;
4963 it->object = NILP (object) ? it->w->contents : object;
4964 it->method = GET_FROM_IMAGE;
4965 it->from_overlay = Qnil;
4966 it->face_id = face_id;
4967 it->from_disp_prop_p = 1;
4969 /* Say that we haven't consumed the characters with
4970 `display' property yet. The call to pop_it in
4971 set_iterator_to_next will clean this up. */
4972 *position = start_pos;
4974 if (EQ (XCAR (spec), Qleft_fringe))
4976 it->left_user_fringe_bitmap = fringe_bitmap;
4977 it->left_user_fringe_face_id = face_id;
4979 else
4981 it->right_user_fringe_bitmap = fringe_bitmap;
4982 it->right_user_fringe_face_id = face_id;
4985 #endif /* HAVE_WINDOW_SYSTEM */
4986 return 1;
4989 /* Prepare to handle `((margin left-margin) ...)',
4990 `((margin right-margin) ...)' and `((margin nil) ...)'
4991 prefixes for display specifications. */
4992 location = Qunbound;
4993 if (CONSP (spec) && CONSP (XCAR (spec)))
4995 Lisp_Object tem;
4997 value = XCDR (spec);
4998 if (CONSP (value))
4999 value = XCAR (value);
5001 tem = XCAR (spec);
5002 if (EQ (XCAR (tem), Qmargin)
5003 && (tem = XCDR (tem),
5004 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5005 (NILP (tem)
5006 || EQ (tem, Qleft_margin)
5007 || EQ (tem, Qright_margin))))
5008 location = tem;
5011 if (EQ (location, Qunbound))
5013 location = Qnil;
5014 value = spec;
5017 /* After this point, VALUE is the property after any
5018 margin prefix has been stripped. It must be a string,
5019 an image specification, or `(space ...)'.
5021 LOCATION specifies where to display: `left-margin',
5022 `right-margin' or nil. */
5024 valid_p = (STRINGP (value)
5025 #ifdef HAVE_WINDOW_SYSTEM
5026 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5027 && valid_image_p (value))
5028 #endif /* not HAVE_WINDOW_SYSTEM */
5029 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5031 if (valid_p && !display_replaced_p)
5033 int retval = 1;
5035 if (!it)
5037 /* Callers need to know whether the display spec is any kind
5038 of `(space ...)' spec that is about to affect text-area
5039 display. */
5040 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5041 retval = 2;
5042 return retval;
5045 /* Save current settings of IT so that we can restore them
5046 when we are finished with the glyph property value. */
5047 push_it (it, position);
5048 it->from_overlay = overlay;
5049 it->from_disp_prop_p = 1;
5051 if (NILP (location))
5052 it->area = TEXT_AREA;
5053 else if (EQ (location, Qleft_margin))
5054 it->area = LEFT_MARGIN_AREA;
5055 else
5056 it->area = RIGHT_MARGIN_AREA;
5058 if (STRINGP (value))
5060 it->string = value;
5061 it->multibyte_p = STRING_MULTIBYTE (it->string);
5062 it->current.overlay_string_index = -1;
5063 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5064 it->end_charpos = it->string_nchars = SCHARS (it->string);
5065 it->method = GET_FROM_STRING;
5066 it->stop_charpos = 0;
5067 it->prev_stop = 0;
5068 it->base_level_stop = 0;
5069 it->string_from_display_prop_p = 1;
5070 /* Say that we haven't consumed the characters with
5071 `display' property yet. The call to pop_it in
5072 set_iterator_to_next will clean this up. */
5073 if (BUFFERP (object))
5074 *position = start_pos;
5076 /* Force paragraph direction to be that of the parent
5077 object. If the parent object's paragraph direction is
5078 not yet determined, default to L2R. */
5079 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5080 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5081 else
5082 it->paragraph_embedding = L2R;
5084 /* Set up the bidi iterator for this display string. */
5085 if (it->bidi_p)
5087 it->bidi_it.string.lstring = it->string;
5088 it->bidi_it.string.s = NULL;
5089 it->bidi_it.string.schars = it->end_charpos;
5090 it->bidi_it.string.bufpos = bufpos;
5091 it->bidi_it.string.from_disp_str = 1;
5092 it->bidi_it.string.unibyte = !it->multibyte_p;
5093 it->bidi_it.w = it->w;
5094 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5097 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5099 it->method = GET_FROM_STRETCH;
5100 it->object = value;
5101 *position = it->position = start_pos;
5102 retval = 1 + (it->area == TEXT_AREA);
5104 #ifdef HAVE_WINDOW_SYSTEM
5105 else
5107 it->what = IT_IMAGE;
5108 it->image_id = lookup_image (it->f, value);
5109 it->position = start_pos;
5110 it->object = NILP (object) ? it->w->contents : object;
5111 it->method = GET_FROM_IMAGE;
5113 /* Say that we haven't consumed the characters with
5114 `display' property yet. The call to pop_it in
5115 set_iterator_to_next will clean this up. */
5116 *position = start_pos;
5118 #endif /* HAVE_WINDOW_SYSTEM */
5120 return retval;
5123 /* Invalid property or property not supported. Restore
5124 POSITION to what it was before. */
5125 *position = start_pos;
5126 return 0;
5129 /* Check if PROP is a display property value whose text should be
5130 treated as intangible. OVERLAY is the overlay from which PROP
5131 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5132 specify the buffer position covered by PROP. */
5135 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5136 ptrdiff_t charpos, ptrdiff_t bytepos)
5138 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5139 struct text_pos position;
5141 SET_TEXT_POS (position, charpos, bytepos);
5142 return handle_display_spec (NULL, prop, Qnil, overlay,
5143 &position, charpos, frame_window_p);
5147 /* Return 1 if PROP is a display sub-property value containing STRING.
5149 Implementation note: this and the following function are really
5150 special cases of handle_display_spec and
5151 handle_single_display_spec, and should ideally use the same code.
5152 Until they do, these two pairs must be consistent and must be
5153 modified in sync. */
5155 static int
5156 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5158 if (EQ (string, prop))
5159 return 1;
5161 /* Skip over `when FORM'. */
5162 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5164 prop = XCDR (prop);
5165 if (!CONSP (prop))
5166 return 0;
5167 /* Actually, the condition following `when' should be eval'ed,
5168 like handle_single_display_spec does, and we should return
5169 zero if it evaluates to nil. However, this function is
5170 called only when the buffer was already displayed and some
5171 glyph in the glyph matrix was found to come from a display
5172 string. Therefore, the condition was already evaluated, and
5173 the result was non-nil, otherwise the display string wouldn't
5174 have been displayed and we would have never been called for
5175 this property. Thus, we can skip the evaluation and assume
5176 its result is non-nil. */
5177 prop = XCDR (prop);
5180 if (CONSP (prop))
5181 /* Skip over `margin LOCATION'. */
5182 if (EQ (XCAR (prop), Qmargin))
5184 prop = XCDR (prop);
5185 if (!CONSP (prop))
5186 return 0;
5188 prop = XCDR (prop);
5189 if (!CONSP (prop))
5190 return 0;
5193 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5197 /* Return 1 if STRING appears in the `display' property PROP. */
5199 static int
5200 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5202 if (CONSP (prop)
5203 && !EQ (XCAR (prop), Qwhen)
5204 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5206 /* A list of sub-properties. */
5207 while (CONSP (prop))
5209 if (single_display_spec_string_p (XCAR (prop), string))
5210 return 1;
5211 prop = XCDR (prop);
5214 else if (VECTORP (prop))
5216 /* A vector of sub-properties. */
5217 ptrdiff_t i;
5218 for (i = 0; i < ASIZE (prop); ++i)
5219 if (single_display_spec_string_p (AREF (prop, i), string))
5220 return 1;
5222 else
5223 return single_display_spec_string_p (prop, string);
5225 return 0;
5228 /* Look for STRING in overlays and text properties in the current
5229 buffer, between character positions FROM and TO (excluding TO).
5230 BACK_P non-zero means look back (in this case, TO is supposed to be
5231 less than FROM).
5232 Value is the first character position where STRING was found, or
5233 zero if it wasn't found before hitting TO.
5235 This function may only use code that doesn't eval because it is
5236 called asynchronously from note_mouse_highlight. */
5238 static ptrdiff_t
5239 string_buffer_position_lim (Lisp_Object string,
5240 ptrdiff_t from, ptrdiff_t to, int back_p)
5242 Lisp_Object limit, prop, pos;
5243 int found = 0;
5245 pos = make_number (max (from, BEGV));
5247 if (!back_p) /* looking forward */
5249 limit = make_number (min (to, ZV));
5250 while (!found && !EQ (pos, limit))
5252 prop = Fget_char_property (pos, Qdisplay, Qnil);
5253 if (!NILP (prop) && display_prop_string_p (prop, string))
5254 found = 1;
5255 else
5256 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5257 limit);
5260 else /* looking back */
5262 limit = make_number (max (to, BEGV));
5263 while (!found && !EQ (pos, limit))
5265 prop = Fget_char_property (pos, Qdisplay, Qnil);
5266 if (!NILP (prop) && display_prop_string_p (prop, string))
5267 found = 1;
5268 else
5269 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5270 limit);
5274 return found ? XINT (pos) : 0;
5277 /* Determine which buffer position in current buffer STRING comes from.
5278 AROUND_CHARPOS is an approximate position where it could come from.
5279 Value is the buffer position or 0 if it couldn't be determined.
5281 This function is necessary because we don't record buffer positions
5282 in glyphs generated from strings (to keep struct glyph small).
5283 This function may only use code that doesn't eval because it is
5284 called asynchronously from note_mouse_highlight. */
5286 static ptrdiff_t
5287 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5289 const int MAX_DISTANCE = 1000;
5290 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5291 around_charpos + MAX_DISTANCE,
5294 if (!found)
5295 found = string_buffer_position_lim (string, around_charpos,
5296 around_charpos - MAX_DISTANCE, 1);
5297 return found;
5302 /***********************************************************************
5303 `composition' property
5304 ***********************************************************************/
5306 /* Set up iterator IT from `composition' property at its current
5307 position. Called from handle_stop. */
5309 static enum prop_handled
5310 handle_composition_prop (struct it *it)
5312 Lisp_Object prop, string;
5313 ptrdiff_t pos, pos_byte, start, end;
5315 if (STRINGP (it->string))
5317 unsigned char *s;
5319 pos = IT_STRING_CHARPOS (*it);
5320 pos_byte = IT_STRING_BYTEPOS (*it);
5321 string = it->string;
5322 s = SDATA (string) + pos_byte;
5323 it->c = STRING_CHAR (s);
5325 else
5327 pos = IT_CHARPOS (*it);
5328 pos_byte = IT_BYTEPOS (*it);
5329 string = Qnil;
5330 it->c = FETCH_CHAR (pos_byte);
5333 /* If there's a valid composition and point is not inside of the
5334 composition (in the case that the composition is from the current
5335 buffer), draw a glyph composed from the composition components. */
5336 if (find_composition (pos, -1, &start, &end, &prop, string)
5337 && composition_valid_p (start, end, prop)
5338 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5340 if (start < pos)
5341 /* As we can't handle this situation (perhaps font-lock added
5342 a new composition), we just return here hoping that next
5343 redisplay will detect this composition much earlier. */
5344 return HANDLED_NORMALLY;
5345 if (start != pos)
5347 if (STRINGP (it->string))
5348 pos_byte = string_char_to_byte (it->string, start);
5349 else
5350 pos_byte = CHAR_TO_BYTE (start);
5352 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5353 prop, string);
5355 if (it->cmp_it.id >= 0)
5357 it->cmp_it.ch = -1;
5358 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5359 it->cmp_it.nglyphs = -1;
5363 return HANDLED_NORMALLY;
5368 /***********************************************************************
5369 Overlay strings
5370 ***********************************************************************/
5372 /* The following structure is used to record overlay strings for
5373 later sorting in load_overlay_strings. */
5375 struct overlay_entry
5377 Lisp_Object overlay;
5378 Lisp_Object string;
5379 EMACS_INT priority;
5380 int after_string_p;
5384 /* Set up iterator IT from overlay strings at its current position.
5385 Called from handle_stop. */
5387 static enum prop_handled
5388 handle_overlay_change (struct it *it)
5390 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5391 return HANDLED_RECOMPUTE_PROPS;
5392 else
5393 return HANDLED_NORMALLY;
5397 /* Set up the next overlay string for delivery by IT, if there is an
5398 overlay string to deliver. Called by set_iterator_to_next when the
5399 end of the current overlay string is reached. If there are more
5400 overlay strings to display, IT->string and
5401 IT->current.overlay_string_index are set appropriately here.
5402 Otherwise IT->string is set to nil. */
5404 static void
5405 next_overlay_string (struct it *it)
5407 ++it->current.overlay_string_index;
5408 if (it->current.overlay_string_index == it->n_overlay_strings)
5410 /* No more overlay strings. Restore IT's settings to what
5411 they were before overlay strings were processed, and
5412 continue to deliver from current_buffer. */
5414 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5415 pop_it (it);
5416 eassert (it->sp > 0
5417 || (NILP (it->string)
5418 && it->method == GET_FROM_BUFFER
5419 && it->stop_charpos >= BEGV
5420 && it->stop_charpos <= it->end_charpos));
5421 it->current.overlay_string_index = -1;
5422 it->n_overlay_strings = 0;
5423 it->overlay_strings_charpos = -1;
5424 /* If there's an empty display string on the stack, pop the
5425 stack, to resync the bidi iterator with IT's position. Such
5426 empty strings are pushed onto the stack in
5427 get_overlay_strings_1. */
5428 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5429 pop_it (it);
5431 /* If we're at the end of the buffer, record that we have
5432 processed the overlay strings there already, so that
5433 next_element_from_buffer doesn't try it again. */
5434 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5435 it->overlay_strings_at_end_processed_p = 1;
5437 else
5439 /* There are more overlay strings to process. If
5440 IT->current.overlay_string_index has advanced to a position
5441 where we must load IT->overlay_strings with more strings, do
5442 it. We must load at the IT->overlay_strings_charpos where
5443 IT->n_overlay_strings was originally computed; when invisible
5444 text is present, this might not be IT_CHARPOS (Bug#7016). */
5445 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5447 if (it->current.overlay_string_index && i == 0)
5448 load_overlay_strings (it, it->overlay_strings_charpos);
5450 /* Initialize IT to deliver display elements from the overlay
5451 string. */
5452 it->string = it->overlay_strings[i];
5453 it->multibyte_p = STRING_MULTIBYTE (it->string);
5454 SET_TEXT_POS (it->current.string_pos, 0, 0);
5455 it->method = GET_FROM_STRING;
5456 it->stop_charpos = 0;
5457 it->end_charpos = SCHARS (it->string);
5458 if (it->cmp_it.stop_pos >= 0)
5459 it->cmp_it.stop_pos = 0;
5460 it->prev_stop = 0;
5461 it->base_level_stop = 0;
5463 /* Set up the bidi iterator for this overlay string. */
5464 if (it->bidi_p)
5466 it->bidi_it.string.lstring = it->string;
5467 it->bidi_it.string.s = NULL;
5468 it->bidi_it.string.schars = SCHARS (it->string);
5469 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5470 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5471 it->bidi_it.string.unibyte = !it->multibyte_p;
5472 it->bidi_it.w = it->w;
5473 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5477 CHECK_IT (it);
5481 /* Compare two overlay_entry structures E1 and E2. Used as a
5482 comparison function for qsort in load_overlay_strings. Overlay
5483 strings for the same position are sorted so that
5485 1. All after-strings come in front of before-strings, except
5486 when they come from the same overlay.
5488 2. Within after-strings, strings are sorted so that overlay strings
5489 from overlays with higher priorities come first.
5491 2. Within before-strings, strings are sorted so that overlay
5492 strings from overlays with higher priorities come last.
5494 Value is analogous to strcmp. */
5497 static int
5498 compare_overlay_entries (const void *e1, const void *e2)
5500 struct overlay_entry const *entry1 = e1;
5501 struct overlay_entry const *entry2 = e2;
5502 int result;
5504 if (entry1->after_string_p != entry2->after_string_p)
5506 /* Let after-strings appear in front of before-strings if
5507 they come from different overlays. */
5508 if (EQ (entry1->overlay, entry2->overlay))
5509 result = entry1->after_string_p ? 1 : -1;
5510 else
5511 result = entry1->after_string_p ? -1 : 1;
5513 else if (entry1->priority != entry2->priority)
5515 if (entry1->after_string_p)
5516 /* After-strings sorted in order of decreasing priority. */
5517 result = entry2->priority < entry1->priority ? -1 : 1;
5518 else
5519 /* Before-strings sorted in order of increasing priority. */
5520 result = entry1->priority < entry2->priority ? -1 : 1;
5522 else
5523 result = 0;
5525 return result;
5529 /* Load the vector IT->overlay_strings with overlay strings from IT's
5530 current buffer position, or from CHARPOS if that is > 0. Set
5531 IT->n_overlays to the total number of overlay strings found.
5533 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5534 a time. On entry into load_overlay_strings,
5535 IT->current.overlay_string_index gives the number of overlay
5536 strings that have already been loaded by previous calls to this
5537 function.
5539 IT->add_overlay_start contains an additional overlay start
5540 position to consider for taking overlay strings from, if non-zero.
5541 This position comes into play when the overlay has an `invisible'
5542 property, and both before and after-strings. When we've skipped to
5543 the end of the overlay, because of its `invisible' property, we
5544 nevertheless want its before-string to appear.
5545 IT->add_overlay_start will contain the overlay start position
5546 in this case.
5548 Overlay strings are sorted so that after-string strings come in
5549 front of before-string strings. Within before and after-strings,
5550 strings are sorted by overlay priority. See also function
5551 compare_overlay_entries. */
5553 static void
5554 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5556 Lisp_Object overlay, window, str, invisible;
5557 struct Lisp_Overlay *ov;
5558 ptrdiff_t start, end;
5559 ptrdiff_t size = 20;
5560 ptrdiff_t n = 0, i, j;
5561 int invis_p;
5562 struct overlay_entry *entries = alloca (size * sizeof *entries);
5563 USE_SAFE_ALLOCA;
5565 if (charpos <= 0)
5566 charpos = IT_CHARPOS (*it);
5568 /* Append the overlay string STRING of overlay OVERLAY to vector
5569 `entries' which has size `size' and currently contains `n'
5570 elements. AFTER_P non-zero means STRING is an after-string of
5571 OVERLAY. */
5572 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5573 do \
5575 Lisp_Object priority; \
5577 if (n == size) \
5579 struct overlay_entry *old = entries; \
5580 SAFE_NALLOCA (entries, 2, size); \
5581 memcpy (entries, old, size * sizeof *entries); \
5582 size *= 2; \
5585 entries[n].string = (STRING); \
5586 entries[n].overlay = (OVERLAY); \
5587 priority = Foverlay_get ((OVERLAY), Qpriority); \
5588 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5589 entries[n].after_string_p = (AFTER_P); \
5590 ++n; \
5592 while (0)
5594 /* Process overlay before the overlay center. */
5595 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5597 XSETMISC (overlay, ov);
5598 eassert (OVERLAYP (overlay));
5599 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5600 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5602 if (end < charpos)
5603 break;
5605 /* Skip this overlay if it doesn't start or end at IT's current
5606 position. */
5607 if (end != charpos && start != charpos)
5608 continue;
5610 /* Skip this overlay if it doesn't apply to IT->w. */
5611 window = Foverlay_get (overlay, Qwindow);
5612 if (WINDOWP (window) && XWINDOW (window) != it->w)
5613 continue;
5615 /* If the text ``under'' the overlay is invisible, both before-
5616 and after-strings from this overlay are visible; start and
5617 end position are indistinguishable. */
5618 invisible = Foverlay_get (overlay, Qinvisible);
5619 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5621 /* If overlay has a non-empty before-string, record it. */
5622 if ((start == charpos || (end == charpos && invis_p))
5623 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5624 && SCHARS (str))
5625 RECORD_OVERLAY_STRING (overlay, str, 0);
5627 /* If overlay has a non-empty after-string, record it. */
5628 if ((end == charpos || (start == charpos && invis_p))
5629 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5630 && SCHARS (str))
5631 RECORD_OVERLAY_STRING (overlay, str, 1);
5634 /* Process overlays after the overlay center. */
5635 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5637 XSETMISC (overlay, ov);
5638 eassert (OVERLAYP (overlay));
5639 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5640 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5642 if (start > charpos)
5643 break;
5645 /* Skip this overlay if it doesn't start or end at IT's current
5646 position. */
5647 if (end != charpos && start != charpos)
5648 continue;
5650 /* Skip this overlay if it doesn't apply to IT->w. */
5651 window = Foverlay_get (overlay, Qwindow);
5652 if (WINDOWP (window) && XWINDOW (window) != it->w)
5653 continue;
5655 /* If the text ``under'' the overlay is invisible, it has a zero
5656 dimension, and both before- and after-strings apply. */
5657 invisible = Foverlay_get (overlay, Qinvisible);
5658 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5660 /* If overlay has a non-empty before-string, record it. */
5661 if ((start == charpos || (end == charpos && invis_p))
5662 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5663 && SCHARS (str))
5664 RECORD_OVERLAY_STRING (overlay, str, 0);
5666 /* If overlay has a non-empty after-string, record it. */
5667 if ((end == charpos || (start == charpos && invis_p))
5668 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5669 && SCHARS (str))
5670 RECORD_OVERLAY_STRING (overlay, str, 1);
5673 #undef RECORD_OVERLAY_STRING
5675 /* Sort entries. */
5676 if (n > 1)
5677 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5679 /* Record number of overlay strings, and where we computed it. */
5680 it->n_overlay_strings = n;
5681 it->overlay_strings_charpos = charpos;
5683 /* IT->current.overlay_string_index is the number of overlay strings
5684 that have already been consumed by IT. Copy some of the
5685 remaining overlay strings to IT->overlay_strings. */
5686 i = 0;
5687 j = it->current.overlay_string_index;
5688 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5690 it->overlay_strings[i] = entries[j].string;
5691 it->string_overlays[i++] = entries[j++].overlay;
5694 CHECK_IT (it);
5695 SAFE_FREE ();
5699 /* Get the first chunk of overlay strings at IT's current buffer
5700 position, or at CHARPOS if that is > 0. Value is non-zero if at
5701 least one overlay string was found. */
5703 static int
5704 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5706 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5707 process. This fills IT->overlay_strings with strings, and sets
5708 IT->n_overlay_strings to the total number of strings to process.
5709 IT->pos.overlay_string_index has to be set temporarily to zero
5710 because load_overlay_strings needs this; it must be set to -1
5711 when no overlay strings are found because a zero value would
5712 indicate a position in the first overlay string. */
5713 it->current.overlay_string_index = 0;
5714 load_overlay_strings (it, charpos);
5716 /* If we found overlay strings, set up IT to deliver display
5717 elements from the first one. Otherwise set up IT to deliver
5718 from current_buffer. */
5719 if (it->n_overlay_strings)
5721 /* Make sure we know settings in current_buffer, so that we can
5722 restore meaningful values when we're done with the overlay
5723 strings. */
5724 if (compute_stop_p)
5725 compute_stop_pos (it);
5726 eassert (it->face_id >= 0);
5728 /* Save IT's settings. They are restored after all overlay
5729 strings have been processed. */
5730 eassert (!compute_stop_p || it->sp == 0);
5732 /* When called from handle_stop, there might be an empty display
5733 string loaded. In that case, don't bother saving it. But
5734 don't use this optimization with the bidi iterator, since we
5735 need the corresponding pop_it call to resync the bidi
5736 iterator's position with IT's position, after we are done
5737 with the overlay strings. (The corresponding call to pop_it
5738 in case of an empty display string is in
5739 next_overlay_string.) */
5740 if (!(!it->bidi_p
5741 && STRINGP (it->string) && !SCHARS (it->string)))
5742 push_it (it, NULL);
5744 /* Set up IT to deliver display elements from the first overlay
5745 string. */
5746 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5747 it->string = it->overlay_strings[0];
5748 it->from_overlay = Qnil;
5749 it->stop_charpos = 0;
5750 eassert (STRINGP (it->string));
5751 it->end_charpos = SCHARS (it->string);
5752 it->prev_stop = 0;
5753 it->base_level_stop = 0;
5754 it->multibyte_p = STRING_MULTIBYTE (it->string);
5755 it->method = GET_FROM_STRING;
5756 it->from_disp_prop_p = 0;
5758 /* Force paragraph direction to be that of the parent
5759 buffer. */
5760 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5761 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5762 else
5763 it->paragraph_embedding = L2R;
5765 /* Set up the bidi iterator for this overlay string. */
5766 if (it->bidi_p)
5768 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5770 it->bidi_it.string.lstring = it->string;
5771 it->bidi_it.string.s = NULL;
5772 it->bidi_it.string.schars = SCHARS (it->string);
5773 it->bidi_it.string.bufpos = pos;
5774 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5775 it->bidi_it.string.unibyte = !it->multibyte_p;
5776 it->bidi_it.w = it->w;
5777 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5779 return 1;
5782 it->current.overlay_string_index = -1;
5783 return 0;
5786 static int
5787 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5789 it->string = Qnil;
5790 it->method = GET_FROM_BUFFER;
5792 (void) get_overlay_strings_1 (it, charpos, 1);
5794 CHECK_IT (it);
5796 /* Value is non-zero if we found at least one overlay string. */
5797 return STRINGP (it->string);
5802 /***********************************************************************
5803 Saving and restoring state
5804 ***********************************************************************/
5806 /* Save current settings of IT on IT->stack. Called, for example,
5807 before setting up IT for an overlay string, to be able to restore
5808 IT's settings to what they were after the overlay string has been
5809 processed. If POSITION is non-NULL, it is the position to save on
5810 the stack instead of IT->position. */
5812 static void
5813 push_it (struct it *it, struct text_pos *position)
5815 struct iterator_stack_entry *p;
5817 eassert (it->sp < IT_STACK_SIZE);
5818 p = it->stack + it->sp;
5820 p->stop_charpos = it->stop_charpos;
5821 p->prev_stop = it->prev_stop;
5822 p->base_level_stop = it->base_level_stop;
5823 p->cmp_it = it->cmp_it;
5824 eassert (it->face_id >= 0);
5825 p->face_id = it->face_id;
5826 p->string = it->string;
5827 p->method = it->method;
5828 p->from_overlay = it->from_overlay;
5829 switch (p->method)
5831 case GET_FROM_IMAGE:
5832 p->u.image.object = it->object;
5833 p->u.image.image_id = it->image_id;
5834 p->u.image.slice = it->slice;
5835 break;
5836 case GET_FROM_STRETCH:
5837 p->u.stretch.object = it->object;
5838 break;
5840 p->position = position ? *position : it->position;
5841 p->current = it->current;
5842 p->end_charpos = it->end_charpos;
5843 p->string_nchars = it->string_nchars;
5844 p->area = it->area;
5845 p->multibyte_p = it->multibyte_p;
5846 p->avoid_cursor_p = it->avoid_cursor_p;
5847 p->space_width = it->space_width;
5848 p->font_height = it->font_height;
5849 p->voffset = it->voffset;
5850 p->string_from_display_prop_p = it->string_from_display_prop_p;
5851 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5852 p->display_ellipsis_p = 0;
5853 p->line_wrap = it->line_wrap;
5854 p->bidi_p = it->bidi_p;
5855 p->paragraph_embedding = it->paragraph_embedding;
5856 p->from_disp_prop_p = it->from_disp_prop_p;
5857 ++it->sp;
5859 /* Save the state of the bidi iterator as well. */
5860 if (it->bidi_p)
5861 bidi_push_it (&it->bidi_it);
5864 static void
5865 iterate_out_of_display_property (struct it *it)
5867 int buffer_p = !STRINGP (it->string);
5868 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5869 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5871 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5873 /* Maybe initialize paragraph direction. If we are at the beginning
5874 of a new paragraph, next_element_from_buffer may not have a
5875 chance to do that. */
5876 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5877 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5878 /* prev_stop can be zero, so check against BEGV as well. */
5879 while (it->bidi_it.charpos >= bob
5880 && it->prev_stop <= it->bidi_it.charpos
5881 && it->bidi_it.charpos < CHARPOS (it->position)
5882 && it->bidi_it.charpos < eob)
5883 bidi_move_to_visually_next (&it->bidi_it);
5884 /* Record the stop_pos we just crossed, for when we cross it
5885 back, maybe. */
5886 if (it->bidi_it.charpos > CHARPOS (it->position))
5887 it->prev_stop = CHARPOS (it->position);
5888 /* If we ended up not where pop_it put us, resync IT's
5889 positional members with the bidi iterator. */
5890 if (it->bidi_it.charpos != CHARPOS (it->position))
5891 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5892 if (buffer_p)
5893 it->current.pos = it->position;
5894 else
5895 it->current.string_pos = it->position;
5898 /* Restore IT's settings from IT->stack. Called, for example, when no
5899 more overlay strings must be processed, and we return to delivering
5900 display elements from a buffer, or when the end of a string from a
5901 `display' property is reached and we return to delivering display
5902 elements from an overlay string, or from a buffer. */
5904 static void
5905 pop_it (struct it *it)
5907 struct iterator_stack_entry *p;
5908 int from_display_prop = it->from_disp_prop_p;
5910 eassert (it->sp > 0);
5911 --it->sp;
5912 p = it->stack + it->sp;
5913 it->stop_charpos = p->stop_charpos;
5914 it->prev_stop = p->prev_stop;
5915 it->base_level_stop = p->base_level_stop;
5916 it->cmp_it = p->cmp_it;
5917 it->face_id = p->face_id;
5918 it->current = p->current;
5919 it->position = p->position;
5920 it->string = p->string;
5921 it->from_overlay = p->from_overlay;
5922 if (NILP (it->string))
5923 SET_TEXT_POS (it->current.string_pos, -1, -1);
5924 it->method = p->method;
5925 switch (it->method)
5927 case GET_FROM_IMAGE:
5928 it->image_id = p->u.image.image_id;
5929 it->object = p->u.image.object;
5930 it->slice = p->u.image.slice;
5931 break;
5932 case GET_FROM_STRETCH:
5933 it->object = p->u.stretch.object;
5934 break;
5935 case GET_FROM_BUFFER:
5936 it->object = it->w->contents;
5937 break;
5938 case GET_FROM_STRING:
5939 it->object = it->string;
5940 break;
5941 case GET_FROM_DISPLAY_VECTOR:
5942 if (it->s)
5943 it->method = GET_FROM_C_STRING;
5944 else if (STRINGP (it->string))
5945 it->method = GET_FROM_STRING;
5946 else
5948 it->method = GET_FROM_BUFFER;
5949 it->object = it->w->contents;
5952 it->end_charpos = p->end_charpos;
5953 it->string_nchars = p->string_nchars;
5954 it->area = p->area;
5955 it->multibyte_p = p->multibyte_p;
5956 it->avoid_cursor_p = p->avoid_cursor_p;
5957 it->space_width = p->space_width;
5958 it->font_height = p->font_height;
5959 it->voffset = p->voffset;
5960 it->string_from_display_prop_p = p->string_from_display_prop_p;
5961 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5962 it->line_wrap = p->line_wrap;
5963 it->bidi_p = p->bidi_p;
5964 it->paragraph_embedding = p->paragraph_embedding;
5965 it->from_disp_prop_p = p->from_disp_prop_p;
5966 if (it->bidi_p)
5968 bidi_pop_it (&it->bidi_it);
5969 /* Bidi-iterate until we get out of the portion of text, if any,
5970 covered by a `display' text property or by an overlay with
5971 `display' property. (We cannot just jump there, because the
5972 internal coherency of the bidi iterator state can not be
5973 preserved across such jumps.) We also must determine the
5974 paragraph base direction if the overlay we just processed is
5975 at the beginning of a new paragraph. */
5976 if (from_display_prop
5977 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5978 iterate_out_of_display_property (it);
5980 eassert ((BUFFERP (it->object)
5981 && IT_CHARPOS (*it) == it->bidi_it.charpos
5982 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5983 || (STRINGP (it->object)
5984 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5985 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5986 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5992 /***********************************************************************
5993 Moving over lines
5994 ***********************************************************************/
5996 /* Set IT's current position to the previous line start. */
5998 static void
5999 back_to_previous_line_start (struct it *it)
6001 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6003 DEC_BOTH (cp, bp);
6004 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6008 /* Move IT to the next line start.
6010 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6011 we skipped over part of the text (as opposed to moving the iterator
6012 continuously over the text). Otherwise, don't change the value
6013 of *SKIPPED_P.
6015 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6016 iterator on the newline, if it was found.
6018 Newlines may come from buffer text, overlay strings, or strings
6019 displayed via the `display' property. That's the reason we can't
6020 simply use find_newline_no_quit.
6022 Note that this function may not skip over invisible text that is so
6023 because of text properties and immediately follows a newline. If
6024 it would, function reseat_at_next_visible_line_start, when called
6025 from set_iterator_to_next, would effectively make invisible
6026 characters following a newline part of the wrong glyph row, which
6027 leads to wrong cursor motion. */
6029 static int
6030 forward_to_next_line_start (struct it *it, int *skipped_p,
6031 struct bidi_it *bidi_it_prev)
6033 ptrdiff_t old_selective;
6034 int newline_found_p, n;
6035 const int MAX_NEWLINE_DISTANCE = 500;
6037 /* If already on a newline, just consume it to avoid unintended
6038 skipping over invisible text below. */
6039 if (it->what == IT_CHARACTER
6040 && it->c == '\n'
6041 && CHARPOS (it->position) == IT_CHARPOS (*it))
6043 if (it->bidi_p && bidi_it_prev)
6044 *bidi_it_prev = it->bidi_it;
6045 set_iterator_to_next (it, 0);
6046 it->c = 0;
6047 return 1;
6050 /* Don't handle selective display in the following. It's (a)
6051 unnecessary because it's done by the caller, and (b) leads to an
6052 infinite recursion because next_element_from_ellipsis indirectly
6053 calls this function. */
6054 old_selective = it->selective;
6055 it->selective = 0;
6057 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6058 from buffer text. */
6059 for (n = newline_found_p = 0;
6060 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6061 n += STRINGP (it->string) ? 0 : 1)
6063 if (!get_next_display_element (it))
6064 return 0;
6065 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6066 if (newline_found_p && it->bidi_p && bidi_it_prev)
6067 *bidi_it_prev = it->bidi_it;
6068 set_iterator_to_next (it, 0);
6071 /* If we didn't find a newline near enough, see if we can use a
6072 short-cut. */
6073 if (!newline_found_p)
6075 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6076 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6077 1, &bytepos);
6078 Lisp_Object pos;
6080 eassert (!STRINGP (it->string));
6082 /* If there isn't any `display' property in sight, and no
6083 overlays, we can just use the position of the newline in
6084 buffer text. */
6085 if (it->stop_charpos >= limit
6086 || ((pos = Fnext_single_property_change (make_number (start),
6087 Qdisplay, Qnil,
6088 make_number (limit)),
6089 NILP (pos))
6090 && next_overlay_change (start) == ZV))
6092 if (!it->bidi_p)
6094 IT_CHARPOS (*it) = limit;
6095 IT_BYTEPOS (*it) = bytepos;
6097 else
6099 struct bidi_it bprev;
6101 /* Help bidi.c avoid expensive searches for display
6102 properties and overlays, by telling it that there are
6103 none up to `limit'. */
6104 if (it->bidi_it.disp_pos < limit)
6106 it->bidi_it.disp_pos = limit;
6107 it->bidi_it.disp_prop = 0;
6109 do {
6110 bprev = it->bidi_it;
6111 bidi_move_to_visually_next (&it->bidi_it);
6112 } while (it->bidi_it.charpos != limit);
6113 IT_CHARPOS (*it) = limit;
6114 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6115 if (bidi_it_prev)
6116 *bidi_it_prev = bprev;
6118 *skipped_p = newline_found_p = 1;
6120 else
6122 while (get_next_display_element (it)
6123 && !newline_found_p)
6125 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6126 if (newline_found_p && it->bidi_p && bidi_it_prev)
6127 *bidi_it_prev = it->bidi_it;
6128 set_iterator_to_next (it, 0);
6133 it->selective = old_selective;
6134 return newline_found_p;
6138 /* Set IT's current position to the previous visible line start. Skip
6139 invisible text that is so either due to text properties or due to
6140 selective display. Caution: this does not change IT->current_x and
6141 IT->hpos. */
6143 static void
6144 back_to_previous_visible_line_start (struct it *it)
6146 while (IT_CHARPOS (*it) > BEGV)
6148 back_to_previous_line_start (it);
6150 if (IT_CHARPOS (*it) <= BEGV)
6151 break;
6153 /* If selective > 0, then lines indented more than its value are
6154 invisible. */
6155 if (it->selective > 0
6156 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6157 it->selective))
6158 continue;
6160 /* Check the newline before point for invisibility. */
6162 Lisp_Object prop;
6163 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6164 Qinvisible, it->window);
6165 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6166 continue;
6169 if (IT_CHARPOS (*it) <= BEGV)
6170 break;
6173 struct it it2;
6174 void *it2data = NULL;
6175 ptrdiff_t pos;
6176 ptrdiff_t beg, end;
6177 Lisp_Object val, overlay;
6179 SAVE_IT (it2, *it, it2data);
6181 /* If newline is part of a composition, continue from start of composition */
6182 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6183 && beg < IT_CHARPOS (*it))
6184 goto replaced;
6186 /* If newline is replaced by a display property, find start of overlay
6187 or interval and continue search from that point. */
6188 pos = --IT_CHARPOS (it2);
6189 --IT_BYTEPOS (it2);
6190 it2.sp = 0;
6191 bidi_unshelve_cache (NULL, 0);
6192 it2.string_from_display_prop_p = 0;
6193 it2.from_disp_prop_p = 0;
6194 if (handle_display_prop (&it2) == HANDLED_RETURN
6195 && !NILP (val = get_char_property_and_overlay
6196 (make_number (pos), Qdisplay, Qnil, &overlay))
6197 && (OVERLAYP (overlay)
6198 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6199 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6201 RESTORE_IT (it, it, it2data);
6202 goto replaced;
6205 /* Newline is not replaced by anything -- so we are done. */
6206 RESTORE_IT (it, it, it2data);
6207 break;
6209 replaced:
6210 if (beg < BEGV)
6211 beg = BEGV;
6212 IT_CHARPOS (*it) = beg;
6213 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6217 it->continuation_lines_width = 0;
6219 eassert (IT_CHARPOS (*it) >= BEGV);
6220 eassert (IT_CHARPOS (*it) == BEGV
6221 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6222 CHECK_IT (it);
6226 /* Reseat iterator IT at the previous visible line start. Skip
6227 invisible text that is so either due to text properties or due to
6228 selective display. At the end, update IT's overlay information,
6229 face information etc. */
6231 void
6232 reseat_at_previous_visible_line_start (struct it *it)
6234 back_to_previous_visible_line_start (it);
6235 reseat (it, it->current.pos, 1);
6236 CHECK_IT (it);
6240 /* Reseat iterator IT on the next visible line start in the current
6241 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6242 preceding the line start. Skip over invisible text that is so
6243 because of selective display. Compute faces, overlays etc at the
6244 new position. Note that this function does not skip over text that
6245 is invisible because of text properties. */
6247 static void
6248 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6250 int newline_found_p, skipped_p = 0;
6251 struct bidi_it bidi_it_prev;
6253 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6255 /* Skip over lines that are invisible because they are indented
6256 more than the value of IT->selective. */
6257 if (it->selective > 0)
6258 while (IT_CHARPOS (*it) < ZV
6259 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6260 it->selective))
6262 eassert (IT_BYTEPOS (*it) == BEGV
6263 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6264 newline_found_p =
6265 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6268 /* Position on the newline if that's what's requested. */
6269 if (on_newline_p && newline_found_p)
6271 if (STRINGP (it->string))
6273 if (IT_STRING_CHARPOS (*it) > 0)
6275 if (!it->bidi_p)
6277 --IT_STRING_CHARPOS (*it);
6278 --IT_STRING_BYTEPOS (*it);
6280 else
6282 /* We need to restore the bidi iterator to the state
6283 it had on the newline, and resync the IT's
6284 position with that. */
6285 it->bidi_it = bidi_it_prev;
6286 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6287 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6291 else if (IT_CHARPOS (*it) > BEGV)
6293 if (!it->bidi_p)
6295 --IT_CHARPOS (*it);
6296 --IT_BYTEPOS (*it);
6298 else
6300 /* We need to restore the bidi iterator to the state it
6301 had on the newline and resync IT with that. */
6302 it->bidi_it = bidi_it_prev;
6303 IT_CHARPOS (*it) = it->bidi_it.charpos;
6304 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6306 reseat (it, it->current.pos, 0);
6309 else if (skipped_p)
6310 reseat (it, it->current.pos, 0);
6312 CHECK_IT (it);
6317 /***********************************************************************
6318 Changing an iterator's position
6319 ***********************************************************************/
6321 /* Change IT's current position to POS in current_buffer. If FORCE_P
6322 is non-zero, always check for text properties at the new position.
6323 Otherwise, text properties are only looked up if POS >=
6324 IT->check_charpos of a property. */
6326 static void
6327 reseat (struct it *it, struct text_pos pos, int force_p)
6329 ptrdiff_t original_pos = IT_CHARPOS (*it);
6331 reseat_1 (it, pos, 0);
6333 /* Determine where to check text properties. Avoid doing it
6334 where possible because text property lookup is very expensive. */
6335 if (force_p
6336 || CHARPOS (pos) > it->stop_charpos
6337 || CHARPOS (pos) < original_pos)
6339 if (it->bidi_p)
6341 /* For bidi iteration, we need to prime prev_stop and
6342 base_level_stop with our best estimations. */
6343 /* Implementation note: Of course, POS is not necessarily a
6344 stop position, so assigning prev_pos to it is a lie; we
6345 should have called compute_stop_backwards. However, if
6346 the current buffer does not include any R2L characters,
6347 that call would be a waste of cycles, because the
6348 iterator will never move back, and thus never cross this
6349 "fake" stop position. So we delay that backward search
6350 until the time we really need it, in next_element_from_buffer. */
6351 if (CHARPOS (pos) != it->prev_stop)
6352 it->prev_stop = CHARPOS (pos);
6353 if (CHARPOS (pos) < it->base_level_stop)
6354 it->base_level_stop = 0; /* meaning it's unknown */
6355 handle_stop (it);
6357 else
6359 handle_stop (it);
6360 it->prev_stop = it->base_level_stop = 0;
6365 CHECK_IT (it);
6369 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6370 IT->stop_pos to POS, also. */
6372 static void
6373 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6375 /* Don't call this function when scanning a C string. */
6376 eassert (it->s == NULL);
6378 /* POS must be a reasonable value. */
6379 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6381 it->current.pos = it->position = pos;
6382 it->end_charpos = ZV;
6383 it->dpvec = NULL;
6384 it->current.dpvec_index = -1;
6385 it->current.overlay_string_index = -1;
6386 IT_STRING_CHARPOS (*it) = -1;
6387 IT_STRING_BYTEPOS (*it) = -1;
6388 it->string = Qnil;
6389 it->method = GET_FROM_BUFFER;
6390 it->object = it->w->contents;
6391 it->area = TEXT_AREA;
6392 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6393 it->sp = 0;
6394 it->string_from_display_prop_p = 0;
6395 it->string_from_prefix_prop_p = 0;
6397 it->from_disp_prop_p = 0;
6398 it->face_before_selective_p = 0;
6399 if (it->bidi_p)
6401 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6402 &it->bidi_it);
6403 bidi_unshelve_cache (NULL, 0);
6404 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6405 it->bidi_it.string.s = NULL;
6406 it->bidi_it.string.lstring = Qnil;
6407 it->bidi_it.string.bufpos = 0;
6408 it->bidi_it.string.unibyte = 0;
6409 it->bidi_it.w = it->w;
6412 if (set_stop_p)
6414 it->stop_charpos = CHARPOS (pos);
6415 it->base_level_stop = CHARPOS (pos);
6417 /* This make the information stored in it->cmp_it invalidate. */
6418 it->cmp_it.id = -1;
6422 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6423 If S is non-null, it is a C string to iterate over. Otherwise,
6424 STRING gives a Lisp string to iterate over.
6426 If PRECISION > 0, don't return more then PRECISION number of
6427 characters from the string.
6429 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6430 characters have been returned. FIELD_WIDTH < 0 means an infinite
6431 field width.
6433 MULTIBYTE = 0 means disable processing of multibyte characters,
6434 MULTIBYTE > 0 means enable it,
6435 MULTIBYTE < 0 means use IT->multibyte_p.
6437 IT must be initialized via a prior call to init_iterator before
6438 calling this function. */
6440 static void
6441 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6442 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6443 int multibyte)
6445 /* No region in strings. */
6446 it->region_beg_charpos = it->region_end_charpos = -1;
6448 /* No text property checks performed by default, but see below. */
6449 it->stop_charpos = -1;
6451 /* Set iterator position and end position. */
6452 memset (&it->current, 0, sizeof it->current);
6453 it->current.overlay_string_index = -1;
6454 it->current.dpvec_index = -1;
6455 eassert (charpos >= 0);
6457 /* If STRING is specified, use its multibyteness, otherwise use the
6458 setting of MULTIBYTE, if specified. */
6459 if (multibyte >= 0)
6460 it->multibyte_p = multibyte > 0;
6462 /* Bidirectional reordering of strings is controlled by the default
6463 value of bidi-display-reordering. Don't try to reorder while
6464 loading loadup.el, as the necessary character property tables are
6465 not yet available. */
6466 it->bidi_p =
6467 NILP (Vpurify_flag)
6468 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6470 if (s == NULL)
6472 eassert (STRINGP (string));
6473 it->string = string;
6474 it->s = NULL;
6475 it->end_charpos = it->string_nchars = SCHARS (string);
6476 it->method = GET_FROM_STRING;
6477 it->current.string_pos = string_pos (charpos, string);
6479 if (it->bidi_p)
6481 it->bidi_it.string.lstring = string;
6482 it->bidi_it.string.s = NULL;
6483 it->bidi_it.string.schars = it->end_charpos;
6484 it->bidi_it.string.bufpos = 0;
6485 it->bidi_it.string.from_disp_str = 0;
6486 it->bidi_it.string.unibyte = !it->multibyte_p;
6487 it->bidi_it.w = it->w;
6488 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6489 FRAME_WINDOW_P (it->f), &it->bidi_it);
6492 else
6494 it->s = (const unsigned char *) s;
6495 it->string = Qnil;
6497 /* Note that we use IT->current.pos, not it->current.string_pos,
6498 for displaying C strings. */
6499 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6500 if (it->multibyte_p)
6502 it->current.pos = c_string_pos (charpos, s, 1);
6503 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6505 else
6507 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6508 it->end_charpos = it->string_nchars = strlen (s);
6511 if (it->bidi_p)
6513 it->bidi_it.string.lstring = Qnil;
6514 it->bidi_it.string.s = (const unsigned char *) s;
6515 it->bidi_it.string.schars = it->end_charpos;
6516 it->bidi_it.string.bufpos = 0;
6517 it->bidi_it.string.from_disp_str = 0;
6518 it->bidi_it.string.unibyte = !it->multibyte_p;
6519 it->bidi_it.w = it->w;
6520 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6521 &it->bidi_it);
6523 it->method = GET_FROM_C_STRING;
6526 /* PRECISION > 0 means don't return more than PRECISION characters
6527 from the string. */
6528 if (precision > 0 && it->end_charpos - charpos > precision)
6530 it->end_charpos = it->string_nchars = charpos + precision;
6531 if (it->bidi_p)
6532 it->bidi_it.string.schars = it->end_charpos;
6535 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6536 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6537 FIELD_WIDTH < 0 means infinite field width. This is useful for
6538 padding with `-' at the end of a mode line. */
6539 if (field_width < 0)
6540 field_width = INFINITY;
6541 /* Implementation note: We deliberately don't enlarge
6542 it->bidi_it.string.schars here to fit it->end_charpos, because
6543 the bidi iterator cannot produce characters out of thin air. */
6544 if (field_width > it->end_charpos - charpos)
6545 it->end_charpos = charpos + field_width;
6547 /* Use the standard display table for displaying strings. */
6548 if (DISP_TABLE_P (Vstandard_display_table))
6549 it->dp = XCHAR_TABLE (Vstandard_display_table);
6551 it->stop_charpos = charpos;
6552 it->prev_stop = charpos;
6553 it->base_level_stop = 0;
6554 if (it->bidi_p)
6556 it->bidi_it.first_elt = 1;
6557 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6558 it->bidi_it.disp_pos = -1;
6560 if (s == NULL && it->multibyte_p)
6562 ptrdiff_t endpos = SCHARS (it->string);
6563 if (endpos > it->end_charpos)
6564 endpos = it->end_charpos;
6565 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6566 it->string);
6568 CHECK_IT (it);
6573 /***********************************************************************
6574 Iteration
6575 ***********************************************************************/
6577 /* Map enum it_method value to corresponding next_element_from_* function. */
6579 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6581 next_element_from_buffer,
6582 next_element_from_display_vector,
6583 next_element_from_string,
6584 next_element_from_c_string,
6585 next_element_from_image,
6586 next_element_from_stretch
6589 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6592 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6593 (possibly with the following characters). */
6595 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6596 ((IT)->cmp_it.id >= 0 \
6597 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6598 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6599 END_CHARPOS, (IT)->w, \
6600 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6601 (IT)->string)))
6604 /* Lookup the char-table Vglyphless_char_display for character C (-1
6605 if we want information for no-font case), and return the display
6606 method symbol. By side-effect, update it->what and
6607 it->glyphless_method. This function is called from
6608 get_next_display_element for each character element, and from
6609 x_produce_glyphs when no suitable font was found. */
6611 Lisp_Object
6612 lookup_glyphless_char_display (int c, struct it *it)
6614 Lisp_Object glyphless_method = Qnil;
6616 if (CHAR_TABLE_P (Vglyphless_char_display)
6617 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6619 if (c >= 0)
6621 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6622 if (CONSP (glyphless_method))
6623 glyphless_method = FRAME_WINDOW_P (it->f)
6624 ? XCAR (glyphless_method)
6625 : XCDR (glyphless_method);
6627 else
6628 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6631 retry:
6632 if (NILP (glyphless_method))
6634 if (c >= 0)
6635 /* The default is to display the character by a proper font. */
6636 return Qnil;
6637 /* The default for the no-font case is to display an empty box. */
6638 glyphless_method = Qempty_box;
6640 if (EQ (glyphless_method, Qzero_width))
6642 if (c >= 0)
6643 return glyphless_method;
6644 /* This method can't be used for the no-font case. */
6645 glyphless_method = Qempty_box;
6647 if (EQ (glyphless_method, Qthin_space))
6648 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6649 else if (EQ (glyphless_method, Qempty_box))
6650 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6651 else if (EQ (glyphless_method, Qhex_code))
6652 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6653 else if (STRINGP (glyphless_method))
6654 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6655 else
6657 /* Invalid value. We use the default method. */
6658 glyphless_method = Qnil;
6659 goto retry;
6661 it->what = IT_GLYPHLESS;
6662 return glyphless_method;
6665 /* Merge escape glyph face and cache the result. */
6667 static struct frame *last_escape_glyph_frame = NULL;
6668 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6669 static int last_escape_glyph_merged_face_id = 0;
6671 static int
6672 merge_escape_glyph_face (struct it *it)
6674 int face_id;
6676 if (it->f == last_escape_glyph_frame
6677 && it->face_id == last_escape_glyph_face_id)
6678 face_id = last_escape_glyph_merged_face_id;
6679 else
6681 /* Merge the `escape-glyph' face into the current face. */
6682 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6683 last_escape_glyph_frame = it->f;
6684 last_escape_glyph_face_id = it->face_id;
6685 last_escape_glyph_merged_face_id = face_id;
6687 return face_id;
6690 /* Likewise for glyphless glyph face. */
6692 static struct frame *last_glyphless_glyph_frame = NULL;
6693 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6694 static int last_glyphless_glyph_merged_face_id = 0;
6697 merge_glyphless_glyph_face (struct it *it)
6699 int face_id;
6701 if (it->f == last_glyphless_glyph_frame
6702 && it->face_id == last_glyphless_glyph_face_id)
6703 face_id = last_glyphless_glyph_merged_face_id;
6704 else
6706 /* Merge the `glyphless-char' face into the current face. */
6707 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6708 last_glyphless_glyph_frame = it->f;
6709 last_glyphless_glyph_face_id = it->face_id;
6710 last_glyphless_glyph_merged_face_id = face_id;
6712 return face_id;
6715 /* Load IT's display element fields with information about the next
6716 display element from the current position of IT. Value is zero if
6717 end of buffer (or C string) is reached. */
6719 static int
6720 get_next_display_element (struct it *it)
6722 /* Non-zero means that we found a display element. Zero means that
6723 we hit the end of what we iterate over. Performance note: the
6724 function pointer `method' used here turns out to be faster than
6725 using a sequence of if-statements. */
6726 int success_p;
6728 get_next:
6729 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6731 if (it->what == IT_CHARACTER)
6733 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6734 and only if (a) the resolved directionality of that character
6735 is R..." */
6736 /* FIXME: Do we need an exception for characters from display
6737 tables? */
6738 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6739 it->c = bidi_mirror_char (it->c);
6740 /* Map via display table or translate control characters.
6741 IT->c, IT->len etc. have been set to the next character by
6742 the function call above. If we have a display table, and it
6743 contains an entry for IT->c, translate it. Don't do this if
6744 IT->c itself comes from a display table, otherwise we could
6745 end up in an infinite recursion. (An alternative could be to
6746 count the recursion depth of this function and signal an
6747 error when a certain maximum depth is reached.) Is it worth
6748 it? */
6749 if (success_p && it->dpvec == NULL)
6751 Lisp_Object dv;
6752 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6753 int nonascii_space_p = 0;
6754 int nonascii_hyphen_p = 0;
6755 int c = it->c; /* This is the character to display. */
6757 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6759 eassert (SINGLE_BYTE_CHAR_P (c));
6760 if (unibyte_display_via_language_environment)
6762 c = DECODE_CHAR (unibyte, c);
6763 if (c < 0)
6764 c = BYTE8_TO_CHAR (it->c);
6766 else
6767 c = BYTE8_TO_CHAR (it->c);
6770 if (it->dp
6771 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6772 VECTORP (dv)))
6774 struct Lisp_Vector *v = XVECTOR (dv);
6776 /* Return the first character from the display table
6777 entry, if not empty. If empty, don't display the
6778 current character. */
6779 if (v->header.size)
6781 it->dpvec_char_len = it->len;
6782 it->dpvec = v->contents;
6783 it->dpend = v->contents + v->header.size;
6784 it->current.dpvec_index = 0;
6785 it->dpvec_face_id = -1;
6786 it->saved_face_id = it->face_id;
6787 it->method = GET_FROM_DISPLAY_VECTOR;
6788 it->ellipsis_p = 0;
6790 else
6792 set_iterator_to_next (it, 0);
6794 goto get_next;
6797 if (! NILP (lookup_glyphless_char_display (c, it)))
6799 if (it->what == IT_GLYPHLESS)
6800 goto done;
6801 /* Don't display this character. */
6802 set_iterator_to_next (it, 0);
6803 goto get_next;
6806 /* If `nobreak-char-display' is non-nil, we display
6807 non-ASCII spaces and hyphens specially. */
6808 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6810 if (c == 0xA0)
6811 nonascii_space_p = 1;
6812 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6813 nonascii_hyphen_p = 1;
6816 /* Translate control characters into `\003' or `^C' form.
6817 Control characters coming from a display table entry are
6818 currently not translated because we use IT->dpvec to hold
6819 the translation. This could easily be changed but I
6820 don't believe that it is worth doing.
6822 The characters handled by `nobreak-char-display' must be
6823 translated too.
6825 Non-printable characters and raw-byte characters are also
6826 translated to octal form. */
6827 if (((c < ' ' || c == 127) /* ASCII control chars */
6828 ? (it->area != TEXT_AREA
6829 /* In mode line, treat \n, \t like other crl chars. */
6830 || (c != '\t'
6831 && it->glyph_row
6832 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6833 || (c != '\n' && c != '\t'))
6834 : (nonascii_space_p
6835 || nonascii_hyphen_p
6836 || CHAR_BYTE8_P (c)
6837 || ! CHAR_PRINTABLE_P (c))))
6839 /* C is a control character, non-ASCII space/hyphen,
6840 raw-byte, or a non-printable character which must be
6841 displayed either as '\003' or as `^C' where the '\\'
6842 and '^' can be defined in the display table. Fill
6843 IT->ctl_chars with glyphs for what we have to
6844 display. Then, set IT->dpvec to these glyphs. */
6845 Lisp_Object gc;
6846 int ctl_len;
6847 int face_id;
6848 int lface_id = 0;
6849 int escape_glyph;
6851 /* Handle control characters with ^. */
6853 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6855 int g;
6857 g = '^'; /* default glyph for Control */
6858 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6859 if (it->dp
6860 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6862 g = GLYPH_CODE_CHAR (gc);
6863 lface_id = GLYPH_CODE_FACE (gc);
6866 face_id = (lface_id
6867 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6868 : merge_escape_glyph_face (it));
6870 XSETINT (it->ctl_chars[0], g);
6871 XSETINT (it->ctl_chars[1], c ^ 0100);
6872 ctl_len = 2;
6873 goto display_control;
6876 /* Handle non-ascii space in the mode where it only gets
6877 highlighting. */
6879 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6881 /* Merge `nobreak-space' into the current face. */
6882 face_id = merge_faces (it->f, Qnobreak_space, 0,
6883 it->face_id);
6884 XSETINT (it->ctl_chars[0], ' ');
6885 ctl_len = 1;
6886 goto display_control;
6889 /* Handle sequences that start with the "escape glyph". */
6891 /* the default escape glyph is \. */
6892 escape_glyph = '\\';
6894 if (it->dp
6895 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6897 escape_glyph = GLYPH_CODE_CHAR (gc);
6898 lface_id = GLYPH_CODE_FACE (gc);
6901 face_id = (lface_id
6902 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6903 : merge_escape_glyph_face (it));
6905 /* Draw non-ASCII hyphen with just highlighting: */
6907 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6909 XSETINT (it->ctl_chars[0], '-');
6910 ctl_len = 1;
6911 goto display_control;
6914 /* Draw non-ASCII space/hyphen with escape glyph: */
6916 if (nonascii_space_p || nonascii_hyphen_p)
6918 XSETINT (it->ctl_chars[0], escape_glyph);
6919 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6920 ctl_len = 2;
6921 goto display_control;
6925 char str[10];
6926 int len, i;
6928 if (CHAR_BYTE8_P (c))
6929 /* Display \200 instead of \17777600. */
6930 c = CHAR_TO_BYTE8 (c);
6931 len = sprintf (str, "%03o", c);
6933 XSETINT (it->ctl_chars[0], escape_glyph);
6934 for (i = 0; i < len; i++)
6935 XSETINT (it->ctl_chars[i + 1], str[i]);
6936 ctl_len = len + 1;
6939 display_control:
6940 /* Set up IT->dpvec and return first character from it. */
6941 it->dpvec_char_len = it->len;
6942 it->dpvec = it->ctl_chars;
6943 it->dpend = it->dpvec + ctl_len;
6944 it->current.dpvec_index = 0;
6945 it->dpvec_face_id = face_id;
6946 it->saved_face_id = it->face_id;
6947 it->method = GET_FROM_DISPLAY_VECTOR;
6948 it->ellipsis_p = 0;
6949 goto get_next;
6951 it->char_to_display = c;
6953 else if (success_p)
6955 it->char_to_display = it->c;
6959 /* Adjust face id for a multibyte character. There are no multibyte
6960 character in unibyte text. */
6961 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6962 && it->multibyte_p
6963 && success_p
6964 && FRAME_WINDOW_P (it->f))
6966 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6968 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6970 /* Automatic composition with glyph-string. */
6971 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6973 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6975 else
6977 ptrdiff_t pos = (it->s ? -1
6978 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6979 : IT_CHARPOS (*it));
6980 int c;
6982 if (it->what == IT_CHARACTER)
6983 c = it->char_to_display;
6984 else
6986 struct composition *cmp = composition_table[it->cmp_it.id];
6987 int i;
6989 c = ' ';
6990 for (i = 0; i < cmp->glyph_len; i++)
6991 /* TAB in a composition means display glyphs with
6992 padding space on the left or right. */
6993 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6994 break;
6996 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7000 done:
7001 /* Is this character the last one of a run of characters with
7002 box? If yes, set IT->end_of_box_run_p to 1. */
7003 if (it->face_box_p
7004 && it->s == NULL)
7006 if (it->method == GET_FROM_STRING && it->sp)
7008 int face_id = underlying_face_id (it);
7009 struct face *face = FACE_FROM_ID (it->f, face_id);
7011 if (face)
7013 if (face->box == FACE_NO_BOX)
7015 /* If the box comes from face properties in a
7016 display string, check faces in that string. */
7017 int string_face_id = face_after_it_pos (it);
7018 it->end_of_box_run_p
7019 = (FACE_FROM_ID (it->f, string_face_id)->box
7020 == FACE_NO_BOX);
7022 /* Otherwise, the box comes from the underlying face.
7023 If this is the last string character displayed, check
7024 the next buffer location. */
7025 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7026 && (it->current.overlay_string_index
7027 == it->n_overlay_strings - 1))
7029 ptrdiff_t ignore;
7030 int next_face_id;
7031 struct text_pos pos = it->current.pos;
7032 INC_TEXT_POS (pos, it->multibyte_p);
7034 next_face_id = face_at_buffer_position
7035 (it->w, CHARPOS (pos), it->region_beg_charpos,
7036 it->region_end_charpos, &ignore,
7037 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7038 -1);
7039 it->end_of_box_run_p
7040 = (FACE_FROM_ID (it->f, next_face_id)->box
7041 == FACE_NO_BOX);
7045 /* next_element_from_display_vector sets this flag according to
7046 faces of the display vector glyphs, see there. */
7047 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7049 int face_id = face_after_it_pos (it);
7050 it->end_of_box_run_p
7051 = (face_id != it->face_id
7052 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7055 /* If we reached the end of the object we've been iterating (e.g., a
7056 display string or an overlay string), and there's something on
7057 IT->stack, proceed with what's on the stack. It doesn't make
7058 sense to return zero if there's unprocessed stuff on the stack,
7059 because otherwise that stuff will never be displayed. */
7060 if (!success_p && it->sp > 0)
7062 set_iterator_to_next (it, 0);
7063 success_p = get_next_display_element (it);
7066 /* Value is 0 if end of buffer or string reached. */
7067 return success_p;
7071 /* Move IT to the next display element.
7073 RESEAT_P non-zero means if called on a newline in buffer text,
7074 skip to the next visible line start.
7076 Functions get_next_display_element and set_iterator_to_next are
7077 separate because I find this arrangement easier to handle than a
7078 get_next_display_element function that also increments IT's
7079 position. The way it is we can first look at an iterator's current
7080 display element, decide whether it fits on a line, and if it does,
7081 increment the iterator position. The other way around we probably
7082 would either need a flag indicating whether the iterator has to be
7083 incremented the next time, or we would have to implement a
7084 decrement position function which would not be easy to write. */
7086 void
7087 set_iterator_to_next (struct it *it, int reseat_p)
7089 /* Reset flags indicating start and end of a sequence of characters
7090 with box. Reset them at the start of this function because
7091 moving the iterator to a new position might set them. */
7092 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7094 switch (it->method)
7096 case GET_FROM_BUFFER:
7097 /* The current display element of IT is a character from
7098 current_buffer. Advance in the buffer, and maybe skip over
7099 invisible lines that are so because of selective display. */
7100 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7101 reseat_at_next_visible_line_start (it, 0);
7102 else if (it->cmp_it.id >= 0)
7104 /* We are currently getting glyphs from a composition. */
7105 int i;
7107 if (! it->bidi_p)
7109 IT_CHARPOS (*it) += it->cmp_it.nchars;
7110 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7111 if (it->cmp_it.to < it->cmp_it.nglyphs)
7113 it->cmp_it.from = it->cmp_it.to;
7115 else
7117 it->cmp_it.id = -1;
7118 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7119 IT_BYTEPOS (*it),
7120 it->end_charpos, Qnil);
7123 else if (! it->cmp_it.reversed_p)
7125 /* Composition created while scanning forward. */
7126 /* Update IT's char/byte positions to point to the first
7127 character of the next grapheme cluster, or to the
7128 character visually after the current composition. */
7129 for (i = 0; i < it->cmp_it.nchars; i++)
7130 bidi_move_to_visually_next (&it->bidi_it);
7131 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7132 IT_CHARPOS (*it) = it->bidi_it.charpos;
7134 if (it->cmp_it.to < it->cmp_it.nglyphs)
7136 /* Proceed to the next grapheme cluster. */
7137 it->cmp_it.from = it->cmp_it.to;
7139 else
7141 /* No more grapheme clusters in this composition.
7142 Find the next stop position. */
7143 ptrdiff_t stop = it->end_charpos;
7144 if (it->bidi_it.scan_dir < 0)
7145 /* Now we are scanning backward and don't know
7146 where to stop. */
7147 stop = -1;
7148 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7149 IT_BYTEPOS (*it), stop, Qnil);
7152 else
7154 /* Composition created while scanning backward. */
7155 /* Update IT's char/byte positions to point to the last
7156 character of the previous grapheme cluster, or the
7157 character visually after the current composition. */
7158 for (i = 0; i < it->cmp_it.nchars; i++)
7159 bidi_move_to_visually_next (&it->bidi_it);
7160 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7161 IT_CHARPOS (*it) = it->bidi_it.charpos;
7162 if (it->cmp_it.from > 0)
7164 /* Proceed to the previous grapheme cluster. */
7165 it->cmp_it.to = it->cmp_it.from;
7167 else
7169 /* No more grapheme clusters in this composition.
7170 Find the next stop position. */
7171 ptrdiff_t stop = it->end_charpos;
7172 if (it->bidi_it.scan_dir < 0)
7173 /* Now we are scanning backward and don't know
7174 where to stop. */
7175 stop = -1;
7176 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7177 IT_BYTEPOS (*it), stop, Qnil);
7181 else
7183 eassert (it->len != 0);
7185 if (!it->bidi_p)
7187 IT_BYTEPOS (*it) += it->len;
7188 IT_CHARPOS (*it) += 1;
7190 else
7192 int prev_scan_dir = it->bidi_it.scan_dir;
7193 /* If this is a new paragraph, determine its base
7194 direction (a.k.a. its base embedding level). */
7195 if (it->bidi_it.new_paragraph)
7196 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7197 bidi_move_to_visually_next (&it->bidi_it);
7198 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7199 IT_CHARPOS (*it) = it->bidi_it.charpos;
7200 if (prev_scan_dir != it->bidi_it.scan_dir)
7202 /* As the scan direction was changed, we must
7203 re-compute the stop position for composition. */
7204 ptrdiff_t stop = it->end_charpos;
7205 if (it->bidi_it.scan_dir < 0)
7206 stop = -1;
7207 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7208 IT_BYTEPOS (*it), stop, Qnil);
7211 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7213 break;
7215 case GET_FROM_C_STRING:
7216 /* Current display element of IT is from a C string. */
7217 if (!it->bidi_p
7218 /* If the string position is beyond string's end, it means
7219 next_element_from_c_string is padding the string with
7220 blanks, in which case we bypass the bidi iterator,
7221 because it cannot deal with such virtual characters. */
7222 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7224 IT_BYTEPOS (*it) += it->len;
7225 IT_CHARPOS (*it) += 1;
7227 else
7229 bidi_move_to_visually_next (&it->bidi_it);
7230 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7231 IT_CHARPOS (*it) = it->bidi_it.charpos;
7233 break;
7235 case GET_FROM_DISPLAY_VECTOR:
7236 /* Current display element of IT is from a display table entry.
7237 Advance in the display table definition. Reset it to null if
7238 end reached, and continue with characters from buffers/
7239 strings. */
7240 ++it->current.dpvec_index;
7242 /* Restore face of the iterator to what they were before the
7243 display vector entry (these entries may contain faces). */
7244 it->face_id = it->saved_face_id;
7246 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7248 int recheck_faces = it->ellipsis_p;
7250 if (it->s)
7251 it->method = GET_FROM_C_STRING;
7252 else if (STRINGP (it->string))
7253 it->method = GET_FROM_STRING;
7254 else
7256 it->method = GET_FROM_BUFFER;
7257 it->object = it->w->contents;
7260 it->dpvec = NULL;
7261 it->current.dpvec_index = -1;
7263 /* Skip over characters which were displayed via IT->dpvec. */
7264 if (it->dpvec_char_len < 0)
7265 reseat_at_next_visible_line_start (it, 1);
7266 else if (it->dpvec_char_len > 0)
7268 if (it->method == GET_FROM_STRING
7269 && it->current.overlay_string_index >= 0
7270 && it->n_overlay_strings > 0)
7271 it->ignore_overlay_strings_at_pos_p = 1;
7272 it->len = it->dpvec_char_len;
7273 set_iterator_to_next (it, reseat_p);
7276 /* Maybe recheck faces after display vector */
7277 if (recheck_faces)
7278 it->stop_charpos = IT_CHARPOS (*it);
7280 break;
7282 case GET_FROM_STRING:
7283 /* Current display element is a character from a Lisp string. */
7284 eassert (it->s == NULL && STRINGP (it->string));
7285 /* Don't advance past string end. These conditions are true
7286 when set_iterator_to_next is called at the end of
7287 get_next_display_element, in which case the Lisp string is
7288 already exhausted, and all we want is pop the iterator
7289 stack. */
7290 if (it->current.overlay_string_index >= 0)
7292 /* This is an overlay string, so there's no padding with
7293 spaces, and the number of characters in the string is
7294 where the string ends. */
7295 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7296 goto consider_string_end;
7298 else
7300 /* Not an overlay string. There could be padding, so test
7301 against it->end_charpos . */
7302 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7303 goto consider_string_end;
7305 if (it->cmp_it.id >= 0)
7307 int i;
7309 if (! it->bidi_p)
7311 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7312 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7313 if (it->cmp_it.to < it->cmp_it.nglyphs)
7314 it->cmp_it.from = it->cmp_it.to;
7315 else
7317 it->cmp_it.id = -1;
7318 composition_compute_stop_pos (&it->cmp_it,
7319 IT_STRING_CHARPOS (*it),
7320 IT_STRING_BYTEPOS (*it),
7321 it->end_charpos, it->string);
7324 else if (! it->cmp_it.reversed_p)
7326 for (i = 0; i < it->cmp_it.nchars; i++)
7327 bidi_move_to_visually_next (&it->bidi_it);
7328 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7329 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7331 if (it->cmp_it.to < it->cmp_it.nglyphs)
7332 it->cmp_it.from = it->cmp_it.to;
7333 else
7335 ptrdiff_t stop = it->end_charpos;
7336 if (it->bidi_it.scan_dir < 0)
7337 stop = -1;
7338 composition_compute_stop_pos (&it->cmp_it,
7339 IT_STRING_CHARPOS (*it),
7340 IT_STRING_BYTEPOS (*it), stop,
7341 it->string);
7344 else
7346 for (i = 0; i < it->cmp_it.nchars; i++)
7347 bidi_move_to_visually_next (&it->bidi_it);
7348 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7349 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7350 if (it->cmp_it.from > 0)
7351 it->cmp_it.to = it->cmp_it.from;
7352 else
7354 ptrdiff_t stop = it->end_charpos;
7355 if (it->bidi_it.scan_dir < 0)
7356 stop = -1;
7357 composition_compute_stop_pos (&it->cmp_it,
7358 IT_STRING_CHARPOS (*it),
7359 IT_STRING_BYTEPOS (*it), stop,
7360 it->string);
7364 else
7366 if (!it->bidi_p
7367 /* If the string position is beyond string's end, it
7368 means next_element_from_string is padding the string
7369 with blanks, in which case we bypass the bidi
7370 iterator, because it cannot deal with such virtual
7371 characters. */
7372 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7374 IT_STRING_BYTEPOS (*it) += it->len;
7375 IT_STRING_CHARPOS (*it) += 1;
7377 else
7379 int prev_scan_dir = it->bidi_it.scan_dir;
7381 bidi_move_to_visually_next (&it->bidi_it);
7382 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7383 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7384 if (prev_scan_dir != it->bidi_it.scan_dir)
7386 ptrdiff_t stop = it->end_charpos;
7388 if (it->bidi_it.scan_dir < 0)
7389 stop = -1;
7390 composition_compute_stop_pos (&it->cmp_it,
7391 IT_STRING_CHARPOS (*it),
7392 IT_STRING_BYTEPOS (*it), stop,
7393 it->string);
7398 consider_string_end:
7400 if (it->current.overlay_string_index >= 0)
7402 /* IT->string is an overlay string. Advance to the
7403 next, if there is one. */
7404 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7406 it->ellipsis_p = 0;
7407 next_overlay_string (it);
7408 if (it->ellipsis_p)
7409 setup_for_ellipsis (it, 0);
7412 else
7414 /* IT->string is not an overlay string. If we reached
7415 its end, and there is something on IT->stack, proceed
7416 with what is on the stack. This can be either another
7417 string, this time an overlay string, or a buffer. */
7418 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7419 && it->sp > 0)
7421 pop_it (it);
7422 if (it->method == GET_FROM_STRING)
7423 goto consider_string_end;
7426 break;
7428 case GET_FROM_IMAGE:
7429 case GET_FROM_STRETCH:
7430 /* The position etc with which we have to proceed are on
7431 the stack. The position may be at the end of a string,
7432 if the `display' property takes up the whole string. */
7433 eassert (it->sp > 0);
7434 pop_it (it);
7435 if (it->method == GET_FROM_STRING)
7436 goto consider_string_end;
7437 break;
7439 default:
7440 /* There are no other methods defined, so this should be a bug. */
7441 emacs_abort ();
7444 eassert (it->method != GET_FROM_STRING
7445 || (STRINGP (it->string)
7446 && IT_STRING_CHARPOS (*it) >= 0));
7449 /* Load IT's display element fields with information about the next
7450 display element which comes from a display table entry or from the
7451 result of translating a control character to one of the forms `^C'
7452 or `\003'.
7454 IT->dpvec holds the glyphs to return as characters.
7455 IT->saved_face_id holds the face id before the display vector--it
7456 is restored into IT->face_id in set_iterator_to_next. */
7458 static int
7459 next_element_from_display_vector (struct it *it)
7461 Lisp_Object gc;
7462 int prev_face_id = it->face_id;
7463 int next_face_id;
7465 /* Precondition. */
7466 eassert (it->dpvec && it->current.dpvec_index >= 0);
7468 it->face_id = it->saved_face_id;
7470 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7471 That seemed totally bogus - so I changed it... */
7472 gc = it->dpvec[it->current.dpvec_index];
7474 if (GLYPH_CODE_P (gc))
7476 struct face *this_face, *prev_face, *next_face;
7478 it->c = GLYPH_CODE_CHAR (gc);
7479 it->len = CHAR_BYTES (it->c);
7481 /* The entry may contain a face id to use. Such a face id is
7482 the id of a Lisp face, not a realized face. A face id of
7483 zero means no face is specified. */
7484 if (it->dpvec_face_id >= 0)
7485 it->face_id = it->dpvec_face_id;
7486 else
7488 int lface_id = GLYPH_CODE_FACE (gc);
7489 if (lface_id > 0)
7490 it->face_id = merge_faces (it->f, Qt, lface_id,
7491 it->saved_face_id);
7494 /* Glyphs in the display vector could have the box face, so we
7495 need to set the related flags in the iterator, as
7496 appropriate. */
7497 this_face = FACE_FROM_ID (it->f, it->face_id);
7498 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7500 /* Is this character the first character of a box-face run? */
7501 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7502 && (!prev_face
7503 || prev_face->box == FACE_NO_BOX));
7505 /* For the last character of the box-face run, we need to look
7506 either at the next glyph from the display vector, or at the
7507 face we saw before the display vector. */
7508 next_face_id = it->saved_face_id;
7509 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7511 if (it->dpvec_face_id >= 0)
7512 next_face_id = it->dpvec_face_id;
7513 else
7515 int lface_id =
7516 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7518 if (lface_id > 0)
7519 next_face_id = merge_faces (it->f, Qt, lface_id,
7520 it->saved_face_id);
7523 next_face = FACE_FROM_ID (it->f, next_face_id);
7524 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7525 && (!next_face
7526 || next_face->box == FACE_NO_BOX));
7527 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7529 else
7530 /* Display table entry is invalid. Return a space. */
7531 it->c = ' ', it->len = 1;
7533 /* Don't change position and object of the iterator here. They are
7534 still the values of the character that had this display table
7535 entry or was translated, and that's what we want. */
7536 it->what = IT_CHARACTER;
7537 return 1;
7540 /* Get the first element of string/buffer in the visual order, after
7541 being reseated to a new position in a string or a buffer. */
7542 static void
7543 get_visually_first_element (struct it *it)
7545 int string_p = STRINGP (it->string) || it->s;
7546 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7547 ptrdiff_t bob = (string_p ? 0 : BEGV);
7549 if (STRINGP (it->string))
7551 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7552 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7554 else
7556 it->bidi_it.charpos = IT_CHARPOS (*it);
7557 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7560 if (it->bidi_it.charpos == eob)
7562 /* Nothing to do, but reset the FIRST_ELT flag, like
7563 bidi_paragraph_init does, because we are not going to
7564 call it. */
7565 it->bidi_it.first_elt = 0;
7567 else if (it->bidi_it.charpos == bob
7568 || (!string_p
7569 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7570 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7572 /* If we are at the beginning of a line/string, we can produce
7573 the next element right away. */
7574 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7575 bidi_move_to_visually_next (&it->bidi_it);
7577 else
7579 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7581 /* We need to prime the bidi iterator starting at the line's or
7582 string's beginning, before we will be able to produce the
7583 next element. */
7584 if (string_p)
7585 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7586 else
7587 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7588 IT_BYTEPOS (*it), -1,
7589 &it->bidi_it.bytepos);
7590 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7593 /* Now return to buffer/string position where we were asked
7594 to get the next display element, and produce that. */
7595 bidi_move_to_visually_next (&it->bidi_it);
7597 while (it->bidi_it.bytepos != orig_bytepos
7598 && it->bidi_it.charpos < eob);
7601 /* Adjust IT's position information to where we ended up. */
7602 if (STRINGP (it->string))
7604 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7605 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7607 else
7609 IT_CHARPOS (*it) = it->bidi_it.charpos;
7610 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7613 if (STRINGP (it->string) || !it->s)
7615 ptrdiff_t stop, charpos, bytepos;
7617 if (STRINGP (it->string))
7619 eassert (!it->s);
7620 stop = SCHARS (it->string);
7621 if (stop > it->end_charpos)
7622 stop = it->end_charpos;
7623 charpos = IT_STRING_CHARPOS (*it);
7624 bytepos = IT_STRING_BYTEPOS (*it);
7626 else
7628 stop = it->end_charpos;
7629 charpos = IT_CHARPOS (*it);
7630 bytepos = IT_BYTEPOS (*it);
7632 if (it->bidi_it.scan_dir < 0)
7633 stop = -1;
7634 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7635 it->string);
7639 /* Load IT with the next display element from Lisp string IT->string.
7640 IT->current.string_pos is the current position within the string.
7641 If IT->current.overlay_string_index >= 0, the Lisp string is an
7642 overlay string. */
7644 static int
7645 next_element_from_string (struct it *it)
7647 struct text_pos position;
7649 eassert (STRINGP (it->string));
7650 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7651 eassert (IT_STRING_CHARPOS (*it) >= 0);
7652 position = it->current.string_pos;
7654 /* With bidi reordering, the character to display might not be the
7655 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7656 that we were reseat()ed to a new string, whose paragraph
7657 direction is not known. */
7658 if (it->bidi_p && it->bidi_it.first_elt)
7660 get_visually_first_element (it);
7661 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7664 /* Time to check for invisible text? */
7665 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7667 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7669 if (!(!it->bidi_p
7670 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7671 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7673 /* With bidi non-linear iteration, we could find
7674 ourselves far beyond the last computed stop_charpos,
7675 with several other stop positions in between that we
7676 missed. Scan them all now, in buffer's logical
7677 order, until we find and handle the last stop_charpos
7678 that precedes our current position. */
7679 handle_stop_backwards (it, it->stop_charpos);
7680 return GET_NEXT_DISPLAY_ELEMENT (it);
7682 else
7684 if (it->bidi_p)
7686 /* Take note of the stop position we just moved
7687 across, for when we will move back across it. */
7688 it->prev_stop = it->stop_charpos;
7689 /* If we are at base paragraph embedding level, take
7690 note of the last stop position seen at this
7691 level. */
7692 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7693 it->base_level_stop = it->stop_charpos;
7695 handle_stop (it);
7697 /* Since a handler may have changed IT->method, we must
7698 recurse here. */
7699 return GET_NEXT_DISPLAY_ELEMENT (it);
7702 else if (it->bidi_p
7703 /* If we are before prev_stop, we may have overstepped
7704 on our way backwards a stop_pos, and if so, we need
7705 to handle that stop_pos. */
7706 && IT_STRING_CHARPOS (*it) < it->prev_stop
7707 /* We can sometimes back up for reasons that have nothing
7708 to do with bidi reordering. E.g., compositions. The
7709 code below is only needed when we are above the base
7710 embedding level, so test for that explicitly. */
7711 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7713 /* If we lost track of base_level_stop, we have no better
7714 place for handle_stop_backwards to start from than string
7715 beginning. This happens, e.g., when we were reseated to
7716 the previous screenful of text by vertical-motion. */
7717 if (it->base_level_stop <= 0
7718 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7719 it->base_level_stop = 0;
7720 handle_stop_backwards (it, it->base_level_stop);
7721 return GET_NEXT_DISPLAY_ELEMENT (it);
7725 if (it->current.overlay_string_index >= 0)
7727 /* Get the next character from an overlay string. In overlay
7728 strings, there is no field width or padding with spaces to
7729 do. */
7730 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7732 it->what = IT_EOB;
7733 return 0;
7735 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7736 IT_STRING_BYTEPOS (*it),
7737 it->bidi_it.scan_dir < 0
7738 ? -1
7739 : SCHARS (it->string))
7740 && next_element_from_composition (it))
7742 return 1;
7744 else if (STRING_MULTIBYTE (it->string))
7746 const unsigned char *s = (SDATA (it->string)
7747 + IT_STRING_BYTEPOS (*it));
7748 it->c = string_char_and_length (s, &it->len);
7750 else
7752 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7753 it->len = 1;
7756 else
7758 /* Get the next character from a Lisp string that is not an
7759 overlay string. Such strings come from the mode line, for
7760 example. We may have to pad with spaces, or truncate the
7761 string. See also next_element_from_c_string. */
7762 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7764 it->what = IT_EOB;
7765 return 0;
7767 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7769 /* Pad with spaces. */
7770 it->c = ' ', it->len = 1;
7771 CHARPOS (position) = BYTEPOS (position) = -1;
7773 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7774 IT_STRING_BYTEPOS (*it),
7775 it->bidi_it.scan_dir < 0
7776 ? -1
7777 : it->string_nchars)
7778 && next_element_from_composition (it))
7780 return 1;
7782 else if (STRING_MULTIBYTE (it->string))
7784 const unsigned char *s = (SDATA (it->string)
7785 + IT_STRING_BYTEPOS (*it));
7786 it->c = string_char_and_length (s, &it->len);
7788 else
7790 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7791 it->len = 1;
7795 /* Record what we have and where it came from. */
7796 it->what = IT_CHARACTER;
7797 it->object = it->string;
7798 it->position = position;
7799 return 1;
7803 /* Load IT with next display element from C string IT->s.
7804 IT->string_nchars is the maximum number of characters to return
7805 from the string. IT->end_charpos may be greater than
7806 IT->string_nchars when this function is called, in which case we
7807 may have to return padding spaces. Value is zero if end of string
7808 reached, including padding spaces. */
7810 static int
7811 next_element_from_c_string (struct it *it)
7813 int success_p = 1;
7815 eassert (it->s);
7816 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7817 it->what = IT_CHARACTER;
7818 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7819 it->object = Qnil;
7821 /* With bidi reordering, the character to display might not be the
7822 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7823 we were reseated to a new string, whose paragraph direction is
7824 not known. */
7825 if (it->bidi_p && it->bidi_it.first_elt)
7826 get_visually_first_element (it);
7828 /* IT's position can be greater than IT->string_nchars in case a
7829 field width or precision has been specified when the iterator was
7830 initialized. */
7831 if (IT_CHARPOS (*it) >= it->end_charpos)
7833 /* End of the game. */
7834 it->what = IT_EOB;
7835 success_p = 0;
7837 else if (IT_CHARPOS (*it) >= it->string_nchars)
7839 /* Pad with spaces. */
7840 it->c = ' ', it->len = 1;
7841 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7843 else if (it->multibyte_p)
7844 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7845 else
7846 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7848 return success_p;
7852 /* Set up IT to return characters from an ellipsis, if appropriate.
7853 The definition of the ellipsis glyphs may come from a display table
7854 entry. This function fills IT with the first glyph from the
7855 ellipsis if an ellipsis is to be displayed. */
7857 static int
7858 next_element_from_ellipsis (struct it *it)
7860 if (it->selective_display_ellipsis_p)
7861 setup_for_ellipsis (it, it->len);
7862 else
7864 /* The face at the current position may be different from the
7865 face we find after the invisible text. Remember what it
7866 was in IT->saved_face_id, and signal that it's there by
7867 setting face_before_selective_p. */
7868 it->saved_face_id = it->face_id;
7869 it->method = GET_FROM_BUFFER;
7870 it->object = it->w->contents;
7871 reseat_at_next_visible_line_start (it, 1);
7872 it->face_before_selective_p = 1;
7875 return GET_NEXT_DISPLAY_ELEMENT (it);
7879 /* Deliver an image display element. The iterator IT is already
7880 filled with image information (done in handle_display_prop). Value
7881 is always 1. */
7884 static int
7885 next_element_from_image (struct it *it)
7887 it->what = IT_IMAGE;
7888 it->ignore_overlay_strings_at_pos_p = 0;
7889 return 1;
7893 /* Fill iterator IT with next display element from a stretch glyph
7894 property. IT->object is the value of the text property. Value is
7895 always 1. */
7897 static int
7898 next_element_from_stretch (struct it *it)
7900 it->what = IT_STRETCH;
7901 return 1;
7904 /* Scan backwards from IT's current position until we find a stop
7905 position, or until BEGV. This is called when we find ourself
7906 before both the last known prev_stop and base_level_stop while
7907 reordering bidirectional text. */
7909 static void
7910 compute_stop_pos_backwards (struct it *it)
7912 const int SCAN_BACK_LIMIT = 1000;
7913 struct text_pos pos;
7914 struct display_pos save_current = it->current;
7915 struct text_pos save_position = it->position;
7916 ptrdiff_t charpos = IT_CHARPOS (*it);
7917 ptrdiff_t where_we_are = charpos;
7918 ptrdiff_t save_stop_pos = it->stop_charpos;
7919 ptrdiff_t save_end_pos = it->end_charpos;
7921 eassert (NILP (it->string) && !it->s);
7922 eassert (it->bidi_p);
7923 it->bidi_p = 0;
7926 it->end_charpos = min (charpos + 1, ZV);
7927 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7928 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7929 reseat_1 (it, pos, 0);
7930 compute_stop_pos (it);
7931 /* We must advance forward, right? */
7932 if (it->stop_charpos <= charpos)
7933 emacs_abort ();
7935 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7937 if (it->stop_charpos <= where_we_are)
7938 it->prev_stop = it->stop_charpos;
7939 else
7940 it->prev_stop = BEGV;
7941 it->bidi_p = 1;
7942 it->current = save_current;
7943 it->position = save_position;
7944 it->stop_charpos = save_stop_pos;
7945 it->end_charpos = save_end_pos;
7948 /* Scan forward from CHARPOS in the current buffer/string, until we
7949 find a stop position > current IT's position. Then handle the stop
7950 position before that. This is called when we bump into a stop
7951 position while reordering bidirectional text. CHARPOS should be
7952 the last previously processed stop_pos (or BEGV/0, if none were
7953 processed yet) whose position is less that IT's current
7954 position. */
7956 static void
7957 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7959 int bufp = !STRINGP (it->string);
7960 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7961 struct display_pos save_current = it->current;
7962 struct text_pos save_position = it->position;
7963 struct text_pos pos1;
7964 ptrdiff_t next_stop;
7966 /* Scan in strict logical order. */
7967 eassert (it->bidi_p);
7968 it->bidi_p = 0;
7971 it->prev_stop = charpos;
7972 if (bufp)
7974 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7975 reseat_1 (it, pos1, 0);
7977 else
7978 it->current.string_pos = string_pos (charpos, it->string);
7979 compute_stop_pos (it);
7980 /* We must advance forward, right? */
7981 if (it->stop_charpos <= it->prev_stop)
7982 emacs_abort ();
7983 charpos = it->stop_charpos;
7985 while (charpos <= where_we_are);
7987 it->bidi_p = 1;
7988 it->current = save_current;
7989 it->position = save_position;
7990 next_stop = it->stop_charpos;
7991 it->stop_charpos = it->prev_stop;
7992 handle_stop (it);
7993 it->stop_charpos = next_stop;
7996 /* Load IT with the next display element from current_buffer. Value
7997 is zero if end of buffer reached. IT->stop_charpos is the next
7998 position at which to stop and check for text properties or buffer
7999 end. */
8001 static int
8002 next_element_from_buffer (struct it *it)
8004 int success_p = 1;
8006 eassert (IT_CHARPOS (*it) >= BEGV);
8007 eassert (NILP (it->string) && !it->s);
8008 eassert (!it->bidi_p
8009 || (EQ (it->bidi_it.string.lstring, Qnil)
8010 && it->bidi_it.string.s == NULL));
8012 /* With bidi reordering, the character to display might not be the
8013 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8014 we were reseat()ed to a new buffer position, which is potentially
8015 a different paragraph. */
8016 if (it->bidi_p && it->bidi_it.first_elt)
8018 get_visually_first_element (it);
8019 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8022 if (IT_CHARPOS (*it) >= it->stop_charpos)
8024 if (IT_CHARPOS (*it) >= it->end_charpos)
8026 int overlay_strings_follow_p;
8028 /* End of the game, except when overlay strings follow that
8029 haven't been returned yet. */
8030 if (it->overlay_strings_at_end_processed_p)
8031 overlay_strings_follow_p = 0;
8032 else
8034 it->overlay_strings_at_end_processed_p = 1;
8035 overlay_strings_follow_p = get_overlay_strings (it, 0);
8038 if (overlay_strings_follow_p)
8039 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8040 else
8042 it->what = IT_EOB;
8043 it->position = it->current.pos;
8044 success_p = 0;
8047 else if (!(!it->bidi_p
8048 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8049 || IT_CHARPOS (*it) == it->stop_charpos))
8051 /* With bidi non-linear iteration, we could find ourselves
8052 far beyond the last computed stop_charpos, with several
8053 other stop positions in between that we missed. Scan
8054 them all now, in buffer's logical order, until we find
8055 and handle the last stop_charpos that precedes our
8056 current position. */
8057 handle_stop_backwards (it, it->stop_charpos);
8058 return GET_NEXT_DISPLAY_ELEMENT (it);
8060 else
8062 if (it->bidi_p)
8064 /* Take note of the stop position we just moved across,
8065 for when we will move back across it. */
8066 it->prev_stop = it->stop_charpos;
8067 /* If we are at base paragraph embedding level, take
8068 note of the last stop position seen at this
8069 level. */
8070 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8071 it->base_level_stop = it->stop_charpos;
8073 handle_stop (it);
8074 return GET_NEXT_DISPLAY_ELEMENT (it);
8077 else if (it->bidi_p
8078 /* If we are before prev_stop, we may have overstepped on
8079 our way backwards a stop_pos, and if so, we need to
8080 handle that stop_pos. */
8081 && IT_CHARPOS (*it) < it->prev_stop
8082 /* We can sometimes back up for reasons that have nothing
8083 to do with bidi reordering. E.g., compositions. The
8084 code below is only needed when we are above the base
8085 embedding level, so test for that explicitly. */
8086 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8088 if (it->base_level_stop <= 0
8089 || IT_CHARPOS (*it) < it->base_level_stop)
8091 /* If we lost track of base_level_stop, we need to find
8092 prev_stop by looking backwards. This happens, e.g., when
8093 we were reseated to the previous screenful of text by
8094 vertical-motion. */
8095 it->base_level_stop = BEGV;
8096 compute_stop_pos_backwards (it);
8097 handle_stop_backwards (it, it->prev_stop);
8099 else
8100 handle_stop_backwards (it, it->base_level_stop);
8101 return GET_NEXT_DISPLAY_ELEMENT (it);
8103 else
8105 /* No face changes, overlays etc. in sight, so just return a
8106 character from current_buffer. */
8107 unsigned char *p;
8108 ptrdiff_t stop;
8110 /* Maybe run the redisplay end trigger hook. Performance note:
8111 This doesn't seem to cost measurable time. */
8112 if (it->redisplay_end_trigger_charpos
8113 && it->glyph_row
8114 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8115 run_redisplay_end_trigger_hook (it);
8117 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8118 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8119 stop)
8120 && next_element_from_composition (it))
8122 return 1;
8125 /* Get the next character, maybe multibyte. */
8126 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8127 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8128 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8129 else
8130 it->c = *p, it->len = 1;
8132 /* Record what we have and where it came from. */
8133 it->what = IT_CHARACTER;
8134 it->object = it->w->contents;
8135 it->position = it->current.pos;
8137 /* Normally we return the character found above, except when we
8138 really want to return an ellipsis for selective display. */
8139 if (it->selective)
8141 if (it->c == '\n')
8143 /* A value of selective > 0 means hide lines indented more
8144 than that number of columns. */
8145 if (it->selective > 0
8146 && IT_CHARPOS (*it) + 1 < ZV
8147 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8148 IT_BYTEPOS (*it) + 1,
8149 it->selective))
8151 success_p = next_element_from_ellipsis (it);
8152 it->dpvec_char_len = -1;
8155 else if (it->c == '\r' && it->selective == -1)
8157 /* A value of selective == -1 means that everything from the
8158 CR to the end of the line is invisible, with maybe an
8159 ellipsis displayed for it. */
8160 success_p = next_element_from_ellipsis (it);
8161 it->dpvec_char_len = -1;
8166 /* Value is zero if end of buffer reached. */
8167 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8168 return success_p;
8172 /* Run the redisplay end trigger hook for IT. */
8174 static void
8175 run_redisplay_end_trigger_hook (struct it *it)
8177 Lisp_Object args[3];
8179 /* IT->glyph_row should be non-null, i.e. we should be actually
8180 displaying something, or otherwise we should not run the hook. */
8181 eassert (it->glyph_row);
8183 /* Set up hook arguments. */
8184 args[0] = Qredisplay_end_trigger_functions;
8185 args[1] = it->window;
8186 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8187 it->redisplay_end_trigger_charpos = 0;
8189 /* Since we are *trying* to run these functions, don't try to run
8190 them again, even if they get an error. */
8191 wset_redisplay_end_trigger (it->w, Qnil);
8192 Frun_hook_with_args (3, args);
8194 /* Notice if it changed the face of the character we are on. */
8195 handle_face_prop (it);
8199 /* Deliver a composition display element. Unlike the other
8200 next_element_from_XXX, this function is not registered in the array
8201 get_next_element[]. It is called from next_element_from_buffer and
8202 next_element_from_string when necessary. */
8204 static int
8205 next_element_from_composition (struct it *it)
8207 it->what = IT_COMPOSITION;
8208 it->len = it->cmp_it.nbytes;
8209 if (STRINGP (it->string))
8211 if (it->c < 0)
8213 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8214 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8215 return 0;
8217 it->position = it->current.string_pos;
8218 it->object = it->string;
8219 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8220 IT_STRING_BYTEPOS (*it), it->string);
8222 else
8224 if (it->c < 0)
8226 IT_CHARPOS (*it) += it->cmp_it.nchars;
8227 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8228 if (it->bidi_p)
8230 if (it->bidi_it.new_paragraph)
8231 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8232 /* Resync the bidi iterator with IT's new position.
8233 FIXME: this doesn't support bidirectional text. */
8234 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8235 bidi_move_to_visually_next (&it->bidi_it);
8237 return 0;
8239 it->position = it->current.pos;
8240 it->object = it->w->contents;
8241 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8242 IT_BYTEPOS (*it), Qnil);
8244 return 1;
8249 /***********************************************************************
8250 Moving an iterator without producing glyphs
8251 ***********************************************************************/
8253 /* Check if iterator is at a position corresponding to a valid buffer
8254 position after some move_it_ call. */
8256 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8257 ((it)->method == GET_FROM_STRING \
8258 ? IT_STRING_CHARPOS (*it) == 0 \
8259 : 1)
8262 /* Move iterator IT to a specified buffer or X position within one
8263 line on the display without producing glyphs.
8265 OP should be a bit mask including some or all of these bits:
8266 MOVE_TO_X: Stop upon reaching x-position TO_X.
8267 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8268 Regardless of OP's value, stop upon reaching the end of the display line.
8270 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8271 This means, in particular, that TO_X includes window's horizontal
8272 scroll amount.
8274 The return value has several possible values that
8275 say what condition caused the scan to stop:
8277 MOVE_POS_MATCH_OR_ZV
8278 - when TO_POS or ZV was reached.
8280 MOVE_X_REACHED
8281 -when TO_X was reached before TO_POS or ZV were reached.
8283 MOVE_LINE_CONTINUED
8284 - when we reached the end of the display area and the line must
8285 be continued.
8287 MOVE_LINE_TRUNCATED
8288 - when we reached the end of the display area and the line is
8289 truncated.
8291 MOVE_NEWLINE_OR_CR
8292 - when we stopped at a line end, i.e. a newline or a CR and selective
8293 display is on. */
8295 static enum move_it_result
8296 move_it_in_display_line_to (struct it *it,
8297 ptrdiff_t to_charpos, int to_x,
8298 enum move_operation_enum op)
8300 enum move_it_result result = MOVE_UNDEFINED;
8301 struct glyph_row *saved_glyph_row;
8302 struct it wrap_it, atpos_it, atx_it, ppos_it;
8303 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8304 void *ppos_data = NULL;
8305 int may_wrap = 0;
8306 enum it_method prev_method = it->method;
8307 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8308 int saw_smaller_pos = prev_pos < to_charpos;
8310 /* Don't produce glyphs in produce_glyphs. */
8311 saved_glyph_row = it->glyph_row;
8312 it->glyph_row = NULL;
8314 /* Use wrap_it to save a copy of IT wherever a word wrap could
8315 occur. Use atpos_it to save a copy of IT at the desired buffer
8316 position, if found, so that we can scan ahead and check if the
8317 word later overshoots the window edge. Use atx_it similarly, for
8318 pixel positions. */
8319 wrap_it.sp = -1;
8320 atpos_it.sp = -1;
8321 atx_it.sp = -1;
8323 /* Use ppos_it under bidi reordering to save a copy of IT for the
8324 position > CHARPOS that is the closest to CHARPOS. We restore
8325 that position in IT when we have scanned the entire display line
8326 without finding a match for CHARPOS and all the character
8327 positions are greater than CHARPOS. */
8328 if (it->bidi_p)
8330 SAVE_IT (ppos_it, *it, ppos_data);
8331 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8332 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8333 SAVE_IT (ppos_it, *it, ppos_data);
8336 #define BUFFER_POS_REACHED_P() \
8337 ((op & MOVE_TO_POS) != 0 \
8338 && BUFFERP (it->object) \
8339 && (IT_CHARPOS (*it) == to_charpos \
8340 || ((!it->bidi_p \
8341 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8342 && IT_CHARPOS (*it) > to_charpos) \
8343 || (it->what == IT_COMPOSITION \
8344 && ((IT_CHARPOS (*it) > to_charpos \
8345 && to_charpos >= it->cmp_it.charpos) \
8346 || (IT_CHARPOS (*it) < to_charpos \
8347 && to_charpos <= it->cmp_it.charpos)))) \
8348 && (it->method == GET_FROM_BUFFER \
8349 || (it->method == GET_FROM_DISPLAY_VECTOR \
8350 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8352 /* If there's a line-/wrap-prefix, handle it. */
8353 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8354 && it->current_y < it->last_visible_y)
8355 handle_line_prefix (it);
8357 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8358 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8360 while (1)
8362 int x, i, ascent = 0, descent = 0;
8364 /* Utility macro to reset an iterator with x, ascent, and descent. */
8365 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8366 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8367 (IT)->max_descent = descent)
8369 /* Stop if we move beyond TO_CHARPOS (after an image or a
8370 display string or stretch glyph). */
8371 if ((op & MOVE_TO_POS) != 0
8372 && BUFFERP (it->object)
8373 && it->method == GET_FROM_BUFFER
8374 && (((!it->bidi_p
8375 /* When the iterator is at base embedding level, we
8376 are guaranteed that characters are delivered for
8377 display in strictly increasing order of their
8378 buffer positions. */
8379 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8380 && IT_CHARPOS (*it) > to_charpos)
8381 || (it->bidi_p
8382 && (prev_method == GET_FROM_IMAGE
8383 || prev_method == GET_FROM_STRETCH
8384 || prev_method == GET_FROM_STRING)
8385 /* Passed TO_CHARPOS from left to right. */
8386 && ((prev_pos < to_charpos
8387 && IT_CHARPOS (*it) > to_charpos)
8388 /* Passed TO_CHARPOS from right to left. */
8389 || (prev_pos > to_charpos
8390 && IT_CHARPOS (*it) < to_charpos)))))
8392 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8394 result = MOVE_POS_MATCH_OR_ZV;
8395 break;
8397 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8398 /* If wrap_it is valid, the current position might be in a
8399 word that is wrapped. So, save the iterator in
8400 atpos_it and continue to see if wrapping happens. */
8401 SAVE_IT (atpos_it, *it, atpos_data);
8404 /* Stop when ZV reached.
8405 We used to stop here when TO_CHARPOS reached as well, but that is
8406 too soon if this glyph does not fit on this line. So we handle it
8407 explicitly below. */
8408 if (!get_next_display_element (it))
8410 result = MOVE_POS_MATCH_OR_ZV;
8411 break;
8414 if (it->line_wrap == TRUNCATE)
8416 if (BUFFER_POS_REACHED_P ())
8418 result = MOVE_POS_MATCH_OR_ZV;
8419 break;
8422 else
8424 if (it->line_wrap == WORD_WRAP)
8426 if (IT_DISPLAYING_WHITESPACE (it))
8427 may_wrap = 1;
8428 else if (may_wrap)
8430 /* We have reached a glyph that follows one or more
8431 whitespace characters. If the position is
8432 already found, we are done. */
8433 if (atpos_it.sp >= 0)
8435 RESTORE_IT (it, &atpos_it, atpos_data);
8436 result = MOVE_POS_MATCH_OR_ZV;
8437 goto done;
8439 if (atx_it.sp >= 0)
8441 RESTORE_IT (it, &atx_it, atx_data);
8442 result = MOVE_X_REACHED;
8443 goto done;
8445 /* Otherwise, we can wrap here. */
8446 SAVE_IT (wrap_it, *it, wrap_data);
8447 may_wrap = 0;
8452 /* Remember the line height for the current line, in case
8453 the next element doesn't fit on the line. */
8454 ascent = it->max_ascent;
8455 descent = it->max_descent;
8457 /* The call to produce_glyphs will get the metrics of the
8458 display element IT is loaded with. Record the x-position
8459 before this display element, in case it doesn't fit on the
8460 line. */
8461 x = it->current_x;
8463 PRODUCE_GLYPHS (it);
8465 if (it->area != TEXT_AREA)
8467 prev_method = it->method;
8468 if (it->method == GET_FROM_BUFFER)
8469 prev_pos = IT_CHARPOS (*it);
8470 set_iterator_to_next (it, 1);
8471 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8472 SET_TEXT_POS (this_line_min_pos,
8473 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8474 if (it->bidi_p
8475 && (op & MOVE_TO_POS)
8476 && IT_CHARPOS (*it) > to_charpos
8477 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8478 SAVE_IT (ppos_it, *it, ppos_data);
8479 continue;
8482 /* The number of glyphs we get back in IT->nglyphs will normally
8483 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8484 character on a terminal frame, or (iii) a line end. For the
8485 second case, IT->nglyphs - 1 padding glyphs will be present.
8486 (On X frames, there is only one glyph produced for a
8487 composite character.)
8489 The behavior implemented below means, for continuation lines,
8490 that as many spaces of a TAB as fit on the current line are
8491 displayed there. For terminal frames, as many glyphs of a
8492 multi-glyph character are displayed in the current line, too.
8493 This is what the old redisplay code did, and we keep it that
8494 way. Under X, the whole shape of a complex character must
8495 fit on the line or it will be completely displayed in the
8496 next line.
8498 Note that both for tabs and padding glyphs, all glyphs have
8499 the same width. */
8500 if (it->nglyphs)
8502 /* More than one glyph or glyph doesn't fit on line. All
8503 glyphs have the same width. */
8504 int single_glyph_width = it->pixel_width / it->nglyphs;
8505 int new_x;
8506 int x_before_this_char = x;
8507 int hpos_before_this_char = it->hpos;
8509 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8511 new_x = x + single_glyph_width;
8513 /* We want to leave anything reaching TO_X to the caller. */
8514 if ((op & MOVE_TO_X) && new_x > to_x)
8516 if (BUFFER_POS_REACHED_P ())
8518 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8519 goto buffer_pos_reached;
8520 if (atpos_it.sp < 0)
8522 SAVE_IT (atpos_it, *it, atpos_data);
8523 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8526 else
8528 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8530 it->current_x = x;
8531 result = MOVE_X_REACHED;
8532 break;
8534 if (atx_it.sp < 0)
8536 SAVE_IT (atx_it, *it, atx_data);
8537 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8542 if (/* Lines are continued. */
8543 it->line_wrap != TRUNCATE
8544 && (/* And glyph doesn't fit on the line. */
8545 new_x > it->last_visible_x
8546 /* Or it fits exactly and we're on a window
8547 system frame. */
8548 || (new_x == it->last_visible_x
8549 && FRAME_WINDOW_P (it->f)
8550 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8551 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8552 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8554 if (/* IT->hpos == 0 means the very first glyph
8555 doesn't fit on the line, e.g. a wide image. */
8556 it->hpos == 0
8557 || (new_x == it->last_visible_x
8558 && FRAME_WINDOW_P (it->f)))
8560 ++it->hpos;
8561 it->current_x = new_x;
8563 /* The character's last glyph just barely fits
8564 in this row. */
8565 if (i == it->nglyphs - 1)
8567 /* If this is the destination position,
8568 return a position *before* it in this row,
8569 now that we know it fits in this row. */
8570 if (BUFFER_POS_REACHED_P ())
8572 if (it->line_wrap != WORD_WRAP
8573 || wrap_it.sp < 0)
8575 it->hpos = hpos_before_this_char;
8576 it->current_x = x_before_this_char;
8577 result = MOVE_POS_MATCH_OR_ZV;
8578 break;
8580 if (it->line_wrap == WORD_WRAP
8581 && atpos_it.sp < 0)
8583 SAVE_IT (atpos_it, *it, atpos_data);
8584 atpos_it.current_x = x_before_this_char;
8585 atpos_it.hpos = hpos_before_this_char;
8589 prev_method = it->method;
8590 if (it->method == GET_FROM_BUFFER)
8591 prev_pos = IT_CHARPOS (*it);
8592 set_iterator_to_next (it, 1);
8593 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8594 SET_TEXT_POS (this_line_min_pos,
8595 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8596 /* On graphical terminals, newlines may
8597 "overflow" into the fringe if
8598 overflow-newline-into-fringe is non-nil.
8599 On text terminals, and on graphical
8600 terminals with no right margin, newlines
8601 may overflow into the last glyph on the
8602 display line.*/
8603 if (!FRAME_WINDOW_P (it->f)
8604 || ((it->bidi_p
8605 && it->bidi_it.paragraph_dir == R2L)
8606 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8607 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8608 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8610 if (!get_next_display_element (it))
8612 result = MOVE_POS_MATCH_OR_ZV;
8613 break;
8615 if (BUFFER_POS_REACHED_P ())
8617 if (ITERATOR_AT_END_OF_LINE_P (it))
8618 result = MOVE_POS_MATCH_OR_ZV;
8619 else
8620 result = MOVE_LINE_CONTINUED;
8621 break;
8623 if (ITERATOR_AT_END_OF_LINE_P (it)
8624 && (it->line_wrap != WORD_WRAP
8625 || wrap_it.sp < 0))
8627 result = MOVE_NEWLINE_OR_CR;
8628 break;
8633 else
8634 IT_RESET_X_ASCENT_DESCENT (it);
8636 if (wrap_it.sp >= 0)
8638 RESTORE_IT (it, &wrap_it, wrap_data);
8639 atpos_it.sp = -1;
8640 atx_it.sp = -1;
8643 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8644 IT_CHARPOS (*it)));
8645 result = MOVE_LINE_CONTINUED;
8646 break;
8649 if (BUFFER_POS_REACHED_P ())
8651 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8652 goto buffer_pos_reached;
8653 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8655 SAVE_IT (atpos_it, *it, atpos_data);
8656 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8660 if (new_x > it->first_visible_x)
8662 /* Glyph is visible. Increment number of glyphs that
8663 would be displayed. */
8664 ++it->hpos;
8668 if (result != MOVE_UNDEFINED)
8669 break;
8671 else if (BUFFER_POS_REACHED_P ())
8673 buffer_pos_reached:
8674 IT_RESET_X_ASCENT_DESCENT (it);
8675 result = MOVE_POS_MATCH_OR_ZV;
8676 break;
8678 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8680 /* Stop when TO_X specified and reached. This check is
8681 necessary here because of lines consisting of a line end,
8682 only. The line end will not produce any glyphs and we
8683 would never get MOVE_X_REACHED. */
8684 eassert (it->nglyphs == 0);
8685 result = MOVE_X_REACHED;
8686 break;
8689 /* Is this a line end? If yes, we're done. */
8690 if (ITERATOR_AT_END_OF_LINE_P (it))
8692 /* If we are past TO_CHARPOS, but never saw any character
8693 positions smaller than TO_CHARPOS, return
8694 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8695 did. */
8696 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8698 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8700 if (IT_CHARPOS (ppos_it) < ZV)
8702 RESTORE_IT (it, &ppos_it, ppos_data);
8703 result = MOVE_POS_MATCH_OR_ZV;
8705 else
8706 goto buffer_pos_reached;
8708 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8709 && IT_CHARPOS (*it) > to_charpos)
8710 goto buffer_pos_reached;
8711 else
8712 result = MOVE_NEWLINE_OR_CR;
8714 else
8715 result = MOVE_NEWLINE_OR_CR;
8716 break;
8719 prev_method = it->method;
8720 if (it->method == GET_FROM_BUFFER)
8721 prev_pos = IT_CHARPOS (*it);
8722 /* The current display element has been consumed. Advance
8723 to the next. */
8724 set_iterator_to_next (it, 1);
8725 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8726 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8727 if (IT_CHARPOS (*it) < to_charpos)
8728 saw_smaller_pos = 1;
8729 if (it->bidi_p
8730 && (op & MOVE_TO_POS)
8731 && IT_CHARPOS (*it) >= to_charpos
8732 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8733 SAVE_IT (ppos_it, *it, ppos_data);
8735 /* Stop if lines are truncated and IT's current x-position is
8736 past the right edge of the window now. */
8737 if (it->line_wrap == TRUNCATE
8738 && it->current_x >= it->last_visible_x)
8740 if (!FRAME_WINDOW_P (it->f)
8741 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8742 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8743 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8744 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8746 int at_eob_p = 0;
8748 if ((at_eob_p = !get_next_display_element (it))
8749 || BUFFER_POS_REACHED_P ()
8750 /* If we are past TO_CHARPOS, but never saw any
8751 character positions smaller than TO_CHARPOS,
8752 return MOVE_POS_MATCH_OR_ZV, like the
8753 unidirectional display did. */
8754 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8755 && !saw_smaller_pos
8756 && IT_CHARPOS (*it) > to_charpos))
8758 if (it->bidi_p
8759 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8760 RESTORE_IT (it, &ppos_it, ppos_data);
8761 result = MOVE_POS_MATCH_OR_ZV;
8762 break;
8764 if (ITERATOR_AT_END_OF_LINE_P (it))
8766 result = MOVE_NEWLINE_OR_CR;
8767 break;
8770 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8771 && !saw_smaller_pos
8772 && IT_CHARPOS (*it) > to_charpos)
8774 if (IT_CHARPOS (ppos_it) < ZV)
8775 RESTORE_IT (it, &ppos_it, ppos_data);
8776 result = MOVE_POS_MATCH_OR_ZV;
8777 break;
8779 result = MOVE_LINE_TRUNCATED;
8780 break;
8782 #undef IT_RESET_X_ASCENT_DESCENT
8785 #undef BUFFER_POS_REACHED_P
8787 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8788 restore the saved iterator. */
8789 if (atpos_it.sp >= 0)
8790 RESTORE_IT (it, &atpos_it, atpos_data);
8791 else if (atx_it.sp >= 0)
8792 RESTORE_IT (it, &atx_it, atx_data);
8794 done:
8796 if (atpos_data)
8797 bidi_unshelve_cache (atpos_data, 1);
8798 if (atx_data)
8799 bidi_unshelve_cache (atx_data, 1);
8800 if (wrap_data)
8801 bidi_unshelve_cache (wrap_data, 1);
8802 if (ppos_data)
8803 bidi_unshelve_cache (ppos_data, 1);
8805 /* Restore the iterator settings altered at the beginning of this
8806 function. */
8807 it->glyph_row = saved_glyph_row;
8808 return result;
8811 /* For external use. */
8812 void
8813 move_it_in_display_line (struct it *it,
8814 ptrdiff_t to_charpos, int to_x,
8815 enum move_operation_enum op)
8817 if (it->line_wrap == WORD_WRAP
8818 && (op & MOVE_TO_X))
8820 struct it save_it;
8821 void *save_data = NULL;
8822 int skip;
8824 SAVE_IT (save_it, *it, save_data);
8825 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8826 /* When word-wrap is on, TO_X may lie past the end
8827 of a wrapped line. Then it->current is the
8828 character on the next line, so backtrack to the
8829 space before the wrap point. */
8830 if (skip == MOVE_LINE_CONTINUED)
8832 int prev_x = max (it->current_x - 1, 0);
8833 RESTORE_IT (it, &save_it, save_data);
8834 move_it_in_display_line_to
8835 (it, -1, prev_x, MOVE_TO_X);
8837 else
8838 bidi_unshelve_cache (save_data, 1);
8840 else
8841 move_it_in_display_line_to (it, to_charpos, to_x, op);
8845 /* Move IT forward until it satisfies one or more of the criteria in
8846 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8848 OP is a bit-mask that specifies where to stop, and in particular,
8849 which of those four position arguments makes a difference. See the
8850 description of enum move_operation_enum.
8852 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8853 screen line, this function will set IT to the next position that is
8854 displayed to the right of TO_CHARPOS on the screen. */
8856 void
8857 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8859 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8860 int line_height, line_start_x = 0, reached = 0;
8861 void *backup_data = NULL;
8863 for (;;)
8865 if (op & MOVE_TO_VPOS)
8867 /* If no TO_CHARPOS and no TO_X specified, stop at the
8868 start of the line TO_VPOS. */
8869 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8871 if (it->vpos == to_vpos)
8873 reached = 1;
8874 break;
8876 else
8877 skip = move_it_in_display_line_to (it, -1, -1, 0);
8879 else
8881 /* TO_VPOS >= 0 means stop at TO_X in the line at
8882 TO_VPOS, or at TO_POS, whichever comes first. */
8883 if (it->vpos == to_vpos)
8885 reached = 2;
8886 break;
8889 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8891 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8893 reached = 3;
8894 break;
8896 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8898 /* We have reached TO_X but not in the line we want. */
8899 skip = move_it_in_display_line_to (it, to_charpos,
8900 -1, MOVE_TO_POS);
8901 if (skip == MOVE_POS_MATCH_OR_ZV)
8903 reached = 4;
8904 break;
8909 else if (op & MOVE_TO_Y)
8911 struct it it_backup;
8913 if (it->line_wrap == WORD_WRAP)
8914 SAVE_IT (it_backup, *it, backup_data);
8916 /* TO_Y specified means stop at TO_X in the line containing
8917 TO_Y---or at TO_CHARPOS if this is reached first. The
8918 problem is that we can't really tell whether the line
8919 contains TO_Y before we have completely scanned it, and
8920 this may skip past TO_X. What we do is to first scan to
8921 TO_X.
8923 If TO_X is not specified, use a TO_X of zero. The reason
8924 is to make the outcome of this function more predictable.
8925 If we didn't use TO_X == 0, we would stop at the end of
8926 the line which is probably not what a caller would expect
8927 to happen. */
8928 skip = move_it_in_display_line_to
8929 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8930 (MOVE_TO_X | (op & MOVE_TO_POS)));
8932 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8933 if (skip == MOVE_POS_MATCH_OR_ZV)
8934 reached = 5;
8935 else if (skip == MOVE_X_REACHED)
8937 /* If TO_X was reached, we want to know whether TO_Y is
8938 in the line. We know this is the case if the already
8939 scanned glyphs make the line tall enough. Otherwise,
8940 we must check by scanning the rest of the line. */
8941 line_height = it->max_ascent + it->max_descent;
8942 if (to_y >= it->current_y
8943 && to_y < it->current_y + line_height)
8945 reached = 6;
8946 break;
8948 SAVE_IT (it_backup, *it, backup_data);
8949 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8950 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8951 op & MOVE_TO_POS);
8952 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8953 line_height = it->max_ascent + it->max_descent;
8954 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8956 if (to_y >= it->current_y
8957 && to_y < it->current_y + line_height)
8959 /* If TO_Y is in this line and TO_X was reached
8960 above, we scanned too far. We have to restore
8961 IT's settings to the ones before skipping. But
8962 keep the more accurate values of max_ascent and
8963 max_descent we've found while skipping the rest
8964 of the line, for the sake of callers, such as
8965 pos_visible_p, that need to know the line
8966 height. */
8967 int max_ascent = it->max_ascent;
8968 int max_descent = it->max_descent;
8970 RESTORE_IT (it, &it_backup, backup_data);
8971 it->max_ascent = max_ascent;
8972 it->max_descent = max_descent;
8973 reached = 6;
8975 else
8977 skip = skip2;
8978 if (skip == MOVE_POS_MATCH_OR_ZV)
8979 reached = 7;
8982 else
8984 /* Check whether TO_Y is in this line. */
8985 line_height = it->max_ascent + it->max_descent;
8986 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8988 if (to_y >= it->current_y
8989 && to_y < it->current_y + line_height)
8991 /* When word-wrap is on, TO_X may lie past the end
8992 of a wrapped line. Then it->current is the
8993 character on the next line, so backtrack to the
8994 space before the wrap point. */
8995 if (skip == MOVE_LINE_CONTINUED
8996 && it->line_wrap == WORD_WRAP)
8998 int prev_x = max (it->current_x - 1, 0);
8999 RESTORE_IT (it, &it_backup, backup_data);
9000 skip = move_it_in_display_line_to
9001 (it, -1, prev_x, MOVE_TO_X);
9003 reached = 6;
9007 if (reached)
9008 break;
9010 else if (BUFFERP (it->object)
9011 && (it->method == GET_FROM_BUFFER
9012 || it->method == GET_FROM_STRETCH)
9013 && IT_CHARPOS (*it) >= to_charpos
9014 /* Under bidi iteration, a call to set_iterator_to_next
9015 can scan far beyond to_charpos if the initial
9016 portion of the next line needs to be reordered. In
9017 that case, give move_it_in_display_line_to another
9018 chance below. */
9019 && !(it->bidi_p
9020 && it->bidi_it.scan_dir == -1))
9021 skip = MOVE_POS_MATCH_OR_ZV;
9022 else
9023 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9025 switch (skip)
9027 case MOVE_POS_MATCH_OR_ZV:
9028 reached = 8;
9029 goto out;
9031 case MOVE_NEWLINE_OR_CR:
9032 set_iterator_to_next (it, 1);
9033 it->continuation_lines_width = 0;
9034 break;
9036 case MOVE_LINE_TRUNCATED:
9037 it->continuation_lines_width = 0;
9038 reseat_at_next_visible_line_start (it, 0);
9039 if ((op & MOVE_TO_POS) != 0
9040 && IT_CHARPOS (*it) > to_charpos)
9042 reached = 9;
9043 goto out;
9045 break;
9047 case MOVE_LINE_CONTINUED:
9048 /* For continued lines ending in a tab, some of the glyphs
9049 associated with the tab are displayed on the current
9050 line. Since it->current_x does not include these glyphs,
9051 we use it->last_visible_x instead. */
9052 if (it->c == '\t')
9054 it->continuation_lines_width += it->last_visible_x;
9055 /* When moving by vpos, ensure that the iterator really
9056 advances to the next line (bug#847, bug#969). Fixme:
9057 do we need to do this in other circumstances? */
9058 if (it->current_x != it->last_visible_x
9059 && (op & MOVE_TO_VPOS)
9060 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9062 line_start_x = it->current_x + it->pixel_width
9063 - it->last_visible_x;
9064 set_iterator_to_next (it, 0);
9067 else
9068 it->continuation_lines_width += it->current_x;
9069 break;
9071 default:
9072 emacs_abort ();
9075 /* Reset/increment for the next run. */
9076 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9077 it->current_x = line_start_x;
9078 line_start_x = 0;
9079 it->hpos = 0;
9080 it->current_y += it->max_ascent + it->max_descent;
9081 ++it->vpos;
9082 last_height = it->max_ascent + it->max_descent;
9083 it->max_ascent = it->max_descent = 0;
9086 out:
9088 /* On text terminals, we may stop at the end of a line in the middle
9089 of a multi-character glyph. If the glyph itself is continued,
9090 i.e. it is actually displayed on the next line, don't treat this
9091 stopping point as valid; move to the next line instead (unless
9092 that brings us offscreen). */
9093 if (!FRAME_WINDOW_P (it->f)
9094 && op & MOVE_TO_POS
9095 && IT_CHARPOS (*it) == to_charpos
9096 && it->what == IT_CHARACTER
9097 && it->nglyphs > 1
9098 && it->line_wrap == WINDOW_WRAP
9099 && it->current_x == it->last_visible_x - 1
9100 && it->c != '\n'
9101 && it->c != '\t'
9102 && it->vpos < it->w->window_end_vpos)
9104 it->continuation_lines_width += it->current_x;
9105 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9106 it->current_y += it->max_ascent + it->max_descent;
9107 ++it->vpos;
9108 last_height = it->max_ascent + it->max_descent;
9111 if (backup_data)
9112 bidi_unshelve_cache (backup_data, 1);
9114 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9118 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9120 If DY > 0, move IT backward at least that many pixels. DY = 0
9121 means move IT backward to the preceding line start or BEGV. This
9122 function may move over more than DY pixels if IT->current_y - DY
9123 ends up in the middle of a line; in this case IT->current_y will be
9124 set to the top of the line moved to. */
9126 void
9127 move_it_vertically_backward (struct it *it, int dy)
9129 int nlines, h;
9130 struct it it2, it3;
9131 void *it2data = NULL, *it3data = NULL;
9132 ptrdiff_t start_pos;
9133 int nchars_per_row
9134 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9135 ptrdiff_t pos_limit;
9137 move_further_back:
9138 eassert (dy >= 0);
9140 start_pos = IT_CHARPOS (*it);
9142 /* Estimate how many newlines we must move back. */
9143 nlines = max (1, dy / default_line_pixel_height (it->w));
9144 if (it->line_wrap == TRUNCATE)
9145 pos_limit = BEGV;
9146 else
9147 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9149 /* Set the iterator's position that many lines back. But don't go
9150 back more than NLINES full screen lines -- this wins a day with
9151 buffers which have very long lines. */
9152 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9153 back_to_previous_visible_line_start (it);
9155 /* Reseat the iterator here. When moving backward, we don't want
9156 reseat to skip forward over invisible text, set up the iterator
9157 to deliver from overlay strings at the new position etc. So,
9158 use reseat_1 here. */
9159 reseat_1 (it, it->current.pos, 1);
9161 /* We are now surely at a line start. */
9162 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9163 reordering is in effect. */
9164 it->continuation_lines_width = 0;
9166 /* Move forward and see what y-distance we moved. First move to the
9167 start of the next line so that we get its height. We need this
9168 height to be able to tell whether we reached the specified
9169 y-distance. */
9170 SAVE_IT (it2, *it, it2data);
9171 it2.max_ascent = it2.max_descent = 0;
9174 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9175 MOVE_TO_POS | MOVE_TO_VPOS);
9177 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9178 /* If we are in a display string which starts at START_POS,
9179 and that display string includes a newline, and we are
9180 right after that newline (i.e. at the beginning of a
9181 display line), exit the loop, because otherwise we will
9182 infloop, since move_it_to will see that it is already at
9183 START_POS and will not move. */
9184 || (it2.method == GET_FROM_STRING
9185 && IT_CHARPOS (it2) == start_pos
9186 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9187 eassert (IT_CHARPOS (*it) >= BEGV);
9188 SAVE_IT (it3, it2, it3data);
9190 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9191 eassert (IT_CHARPOS (*it) >= BEGV);
9192 /* H is the actual vertical distance from the position in *IT
9193 and the starting position. */
9194 h = it2.current_y - it->current_y;
9195 /* NLINES is the distance in number of lines. */
9196 nlines = it2.vpos - it->vpos;
9198 /* Correct IT's y and vpos position
9199 so that they are relative to the starting point. */
9200 it->vpos -= nlines;
9201 it->current_y -= h;
9203 if (dy == 0)
9205 /* DY == 0 means move to the start of the screen line. The
9206 value of nlines is > 0 if continuation lines were involved,
9207 or if the original IT position was at start of a line. */
9208 RESTORE_IT (it, it, it2data);
9209 if (nlines > 0)
9210 move_it_by_lines (it, nlines);
9211 /* The above code moves us to some position NLINES down,
9212 usually to its first glyph (leftmost in an L2R line), but
9213 that's not necessarily the start of the line, under bidi
9214 reordering. We want to get to the character position
9215 that is immediately after the newline of the previous
9216 line. */
9217 if (it->bidi_p
9218 && !it->continuation_lines_width
9219 && !STRINGP (it->string)
9220 && IT_CHARPOS (*it) > BEGV
9221 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9223 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9225 DEC_BOTH (cp, bp);
9226 cp = find_newline_no_quit (cp, bp, -1, NULL);
9227 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9229 bidi_unshelve_cache (it3data, 1);
9231 else
9233 /* The y-position we try to reach, relative to *IT.
9234 Note that H has been subtracted in front of the if-statement. */
9235 int target_y = it->current_y + h - dy;
9236 int y0 = it3.current_y;
9237 int y1;
9238 int line_height;
9240 RESTORE_IT (&it3, &it3, it3data);
9241 y1 = line_bottom_y (&it3);
9242 line_height = y1 - y0;
9243 RESTORE_IT (it, it, it2data);
9244 /* If we did not reach target_y, try to move further backward if
9245 we can. If we moved too far backward, try to move forward. */
9246 if (target_y < it->current_y
9247 /* This is heuristic. In a window that's 3 lines high, with
9248 a line height of 13 pixels each, recentering with point
9249 on the bottom line will try to move -39/2 = 19 pixels
9250 backward. Try to avoid moving into the first line. */
9251 && (it->current_y - target_y
9252 > min (window_box_height (it->w), line_height * 2 / 3))
9253 && IT_CHARPOS (*it) > BEGV)
9255 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9256 target_y - it->current_y));
9257 dy = it->current_y - target_y;
9258 goto move_further_back;
9260 else if (target_y >= it->current_y + line_height
9261 && IT_CHARPOS (*it) < ZV)
9263 /* Should move forward by at least one line, maybe more.
9265 Note: Calling move_it_by_lines can be expensive on
9266 terminal frames, where compute_motion is used (via
9267 vmotion) to do the job, when there are very long lines
9268 and truncate-lines is nil. That's the reason for
9269 treating terminal frames specially here. */
9271 if (!FRAME_WINDOW_P (it->f))
9272 move_it_vertically (it, target_y - (it->current_y + line_height));
9273 else
9277 move_it_by_lines (it, 1);
9279 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9286 /* Move IT by a specified amount of pixel lines DY. DY negative means
9287 move backwards. DY = 0 means move to start of screen line. At the
9288 end, IT will be on the start of a screen line. */
9290 void
9291 move_it_vertically (struct it *it, int dy)
9293 if (dy <= 0)
9294 move_it_vertically_backward (it, -dy);
9295 else
9297 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9298 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9299 MOVE_TO_POS | MOVE_TO_Y);
9300 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9302 /* If buffer ends in ZV without a newline, move to the start of
9303 the line to satisfy the post-condition. */
9304 if (IT_CHARPOS (*it) == ZV
9305 && ZV > BEGV
9306 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9307 move_it_by_lines (it, 0);
9312 /* Move iterator IT past the end of the text line it is in. */
9314 void
9315 move_it_past_eol (struct it *it)
9317 enum move_it_result rc;
9319 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9320 if (rc == MOVE_NEWLINE_OR_CR)
9321 set_iterator_to_next (it, 0);
9325 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9326 negative means move up. DVPOS == 0 means move to the start of the
9327 screen line.
9329 Optimization idea: If we would know that IT->f doesn't use
9330 a face with proportional font, we could be faster for
9331 truncate-lines nil. */
9333 void
9334 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9337 /* The commented-out optimization uses vmotion on terminals. This
9338 gives bad results, because elements like it->what, on which
9339 callers such as pos_visible_p rely, aren't updated. */
9340 /* struct position pos;
9341 if (!FRAME_WINDOW_P (it->f))
9343 struct text_pos textpos;
9345 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9346 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9347 reseat (it, textpos, 1);
9348 it->vpos += pos.vpos;
9349 it->current_y += pos.vpos;
9351 else */
9353 if (dvpos == 0)
9355 /* DVPOS == 0 means move to the start of the screen line. */
9356 move_it_vertically_backward (it, 0);
9357 /* Let next call to line_bottom_y calculate real line height */
9358 last_height = 0;
9360 else if (dvpos > 0)
9362 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9363 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9365 /* Only move to the next buffer position if we ended up in a
9366 string from display property, not in an overlay string
9367 (before-string or after-string). That is because the
9368 latter don't conceal the underlying buffer position, so
9369 we can ask to move the iterator to the exact position we
9370 are interested in. Note that, even if we are already at
9371 IT_CHARPOS (*it), the call below is not a no-op, as it
9372 will detect that we are at the end of the string, pop the
9373 iterator, and compute it->current_x and it->hpos
9374 correctly. */
9375 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9376 -1, -1, -1, MOVE_TO_POS);
9379 else
9381 struct it it2;
9382 void *it2data = NULL;
9383 ptrdiff_t start_charpos, i;
9384 int nchars_per_row
9385 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9386 ptrdiff_t pos_limit;
9388 /* Start at the beginning of the screen line containing IT's
9389 position. This may actually move vertically backwards,
9390 in case of overlays, so adjust dvpos accordingly. */
9391 dvpos += it->vpos;
9392 move_it_vertically_backward (it, 0);
9393 dvpos -= it->vpos;
9395 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9396 screen lines, and reseat the iterator there. */
9397 start_charpos = IT_CHARPOS (*it);
9398 if (it->line_wrap == TRUNCATE)
9399 pos_limit = BEGV;
9400 else
9401 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9402 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9403 back_to_previous_visible_line_start (it);
9404 reseat (it, it->current.pos, 1);
9406 /* Move further back if we end up in a string or an image. */
9407 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9409 /* First try to move to start of display line. */
9410 dvpos += it->vpos;
9411 move_it_vertically_backward (it, 0);
9412 dvpos -= it->vpos;
9413 if (IT_POS_VALID_AFTER_MOVE_P (it))
9414 break;
9415 /* If start of line is still in string or image,
9416 move further back. */
9417 back_to_previous_visible_line_start (it);
9418 reseat (it, it->current.pos, 1);
9419 dvpos--;
9422 it->current_x = it->hpos = 0;
9424 /* Above call may have moved too far if continuation lines
9425 are involved. Scan forward and see if it did. */
9426 SAVE_IT (it2, *it, it2data);
9427 it2.vpos = it2.current_y = 0;
9428 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9429 it->vpos -= it2.vpos;
9430 it->current_y -= it2.current_y;
9431 it->current_x = it->hpos = 0;
9433 /* If we moved too far back, move IT some lines forward. */
9434 if (it2.vpos > -dvpos)
9436 int delta = it2.vpos + dvpos;
9438 RESTORE_IT (&it2, &it2, it2data);
9439 SAVE_IT (it2, *it, it2data);
9440 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9441 /* Move back again if we got too far ahead. */
9442 if (IT_CHARPOS (*it) >= start_charpos)
9443 RESTORE_IT (it, &it2, it2data);
9444 else
9445 bidi_unshelve_cache (it2data, 1);
9447 else
9448 RESTORE_IT (it, it, it2data);
9452 /* Return 1 if IT points into the middle of a display vector. */
9455 in_display_vector_p (struct it *it)
9457 return (it->method == GET_FROM_DISPLAY_VECTOR
9458 && it->current.dpvec_index > 0
9459 && it->dpvec + it->current.dpvec_index != it->dpend);
9463 /***********************************************************************
9464 Messages
9465 ***********************************************************************/
9468 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9469 to *Messages*. */
9471 void
9472 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9474 Lisp_Object args[3];
9475 Lisp_Object msg, fmt;
9476 char *buffer;
9477 ptrdiff_t len;
9478 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9479 USE_SAFE_ALLOCA;
9481 fmt = msg = Qnil;
9482 GCPRO4 (fmt, msg, arg1, arg2);
9484 args[0] = fmt = build_string (format);
9485 args[1] = arg1;
9486 args[2] = arg2;
9487 msg = Fformat (3, args);
9489 len = SBYTES (msg) + 1;
9490 buffer = SAFE_ALLOCA (len);
9491 memcpy (buffer, SDATA (msg), len);
9493 message_dolog (buffer, len - 1, 1, 0);
9494 SAFE_FREE ();
9496 UNGCPRO;
9500 /* Output a newline in the *Messages* buffer if "needs" one. */
9502 void
9503 message_log_maybe_newline (void)
9505 if (message_log_need_newline)
9506 message_dolog ("", 0, 1, 0);
9510 /* Add a string M of length NBYTES to the message log, optionally
9511 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9512 true, means interpret the contents of M as multibyte. This
9513 function calls low-level routines in order to bypass text property
9514 hooks, etc. which might not be safe to run.
9516 This may GC (insert may run before/after change hooks),
9517 so the buffer M must NOT point to a Lisp string. */
9519 void
9520 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9522 const unsigned char *msg = (const unsigned char *) m;
9524 if (!NILP (Vmemory_full))
9525 return;
9527 if (!NILP (Vmessage_log_max))
9529 struct buffer *oldbuf;
9530 Lisp_Object oldpoint, oldbegv, oldzv;
9531 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9532 ptrdiff_t point_at_end = 0;
9533 ptrdiff_t zv_at_end = 0;
9534 Lisp_Object old_deactivate_mark;
9535 bool shown;
9536 struct gcpro gcpro1;
9538 old_deactivate_mark = Vdeactivate_mark;
9539 oldbuf = current_buffer;
9541 /* Ensure the Messages buffer exists, and switch to it.
9542 If we created it, set the major-mode. */
9544 int newbuffer = 0;
9545 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9547 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9549 if (newbuffer &&
9550 !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9551 call0 (intern ("messages-buffer-mode"));
9554 bset_undo_list (current_buffer, Qt);
9556 oldpoint = message_dolog_marker1;
9557 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9558 oldbegv = message_dolog_marker2;
9559 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9560 oldzv = message_dolog_marker3;
9561 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9562 GCPRO1 (old_deactivate_mark);
9564 if (PT == Z)
9565 point_at_end = 1;
9566 if (ZV == Z)
9567 zv_at_end = 1;
9569 BEGV = BEG;
9570 BEGV_BYTE = BEG_BYTE;
9571 ZV = Z;
9572 ZV_BYTE = Z_BYTE;
9573 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9575 /* Insert the string--maybe converting multibyte to single byte
9576 or vice versa, so that all the text fits the buffer. */
9577 if (multibyte
9578 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9580 ptrdiff_t i;
9581 int c, char_bytes;
9582 char work[1];
9584 /* Convert a multibyte string to single-byte
9585 for the *Message* buffer. */
9586 for (i = 0; i < nbytes; i += char_bytes)
9588 c = string_char_and_length (msg + i, &char_bytes);
9589 work[0] = (ASCII_CHAR_P (c)
9591 : multibyte_char_to_unibyte (c));
9592 insert_1_both (work, 1, 1, 1, 0, 0);
9595 else if (! multibyte
9596 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9598 ptrdiff_t i;
9599 int c, char_bytes;
9600 unsigned char str[MAX_MULTIBYTE_LENGTH];
9601 /* Convert a single-byte string to multibyte
9602 for the *Message* buffer. */
9603 for (i = 0; i < nbytes; i++)
9605 c = msg[i];
9606 MAKE_CHAR_MULTIBYTE (c);
9607 char_bytes = CHAR_STRING (c, str);
9608 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9611 else if (nbytes)
9612 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9614 if (nlflag)
9616 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9617 printmax_t dups;
9619 insert_1_both ("\n", 1, 1, 1, 0, 0);
9621 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9622 this_bol = PT;
9623 this_bol_byte = PT_BYTE;
9625 /* See if this line duplicates the previous one.
9626 If so, combine duplicates. */
9627 if (this_bol > BEG)
9629 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9630 prev_bol = PT;
9631 prev_bol_byte = PT_BYTE;
9633 dups = message_log_check_duplicate (prev_bol_byte,
9634 this_bol_byte);
9635 if (dups)
9637 del_range_both (prev_bol, prev_bol_byte,
9638 this_bol, this_bol_byte, 0);
9639 if (dups > 1)
9641 char dupstr[sizeof " [ times]"
9642 + INT_STRLEN_BOUND (printmax_t)];
9644 /* If you change this format, don't forget to also
9645 change message_log_check_duplicate. */
9646 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9647 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9648 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9653 /* If we have more than the desired maximum number of lines
9654 in the *Messages* buffer now, delete the oldest ones.
9655 This is safe because we don't have undo in this buffer. */
9657 if (NATNUMP (Vmessage_log_max))
9659 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9660 -XFASTINT (Vmessage_log_max) - 1, 0);
9661 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9664 BEGV = marker_position (oldbegv);
9665 BEGV_BYTE = marker_byte_position (oldbegv);
9667 if (zv_at_end)
9669 ZV = Z;
9670 ZV_BYTE = Z_BYTE;
9672 else
9674 ZV = marker_position (oldzv);
9675 ZV_BYTE = marker_byte_position (oldzv);
9678 if (point_at_end)
9679 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9680 else
9681 /* We can't do Fgoto_char (oldpoint) because it will run some
9682 Lisp code. */
9683 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9684 marker_byte_position (oldpoint));
9686 UNGCPRO;
9687 unchain_marker (XMARKER (oldpoint));
9688 unchain_marker (XMARKER (oldbegv));
9689 unchain_marker (XMARKER (oldzv));
9691 shown = buffer_window_count (current_buffer) > 0;
9692 set_buffer_internal (oldbuf);
9693 /* We called insert_1_both above with its 5th argument (PREPARE)
9694 zero, which prevents insert_1_both from calling
9695 prepare_to_modify_buffer, which in turns prevents us from
9696 incrementing windows_or_buffers_changed even if *Messages* is
9697 shown in some window. So we must manually incrementing
9698 windows_or_buffers_changed here to make up for that. */
9699 if (shown)
9700 windows_or_buffers_changed++;
9701 else
9702 windows_or_buffers_changed = old_windows_or_buffers_changed;
9703 message_log_need_newline = !nlflag;
9704 Vdeactivate_mark = old_deactivate_mark;
9709 /* We are at the end of the buffer after just having inserted a newline.
9710 (Note: We depend on the fact we won't be crossing the gap.)
9711 Check to see if the most recent message looks a lot like the previous one.
9712 Return 0 if different, 1 if the new one should just replace it, or a
9713 value N > 1 if we should also append " [N times]". */
9715 static intmax_t
9716 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9718 ptrdiff_t i;
9719 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9720 int seen_dots = 0;
9721 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9722 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9724 for (i = 0; i < len; i++)
9726 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9727 seen_dots = 1;
9728 if (p1[i] != p2[i])
9729 return seen_dots;
9731 p1 += len;
9732 if (*p1 == '\n')
9733 return 2;
9734 if (*p1++ == ' ' && *p1++ == '[')
9736 char *pend;
9737 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9738 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9739 return n + 1;
9741 return 0;
9745 /* Display an echo area message M with a specified length of NBYTES
9746 bytes. The string may include null characters. If M is not a
9747 string, clear out any existing message, and let the mini-buffer
9748 text show through.
9750 This function cancels echoing. */
9752 void
9753 message3 (Lisp_Object m)
9755 struct gcpro gcpro1;
9757 GCPRO1 (m);
9758 clear_message (1,1);
9759 cancel_echoing ();
9761 /* First flush out any partial line written with print. */
9762 message_log_maybe_newline ();
9763 if (STRINGP (m))
9765 ptrdiff_t nbytes = SBYTES (m);
9766 bool multibyte = STRING_MULTIBYTE (m);
9767 USE_SAFE_ALLOCA;
9768 char *buffer = SAFE_ALLOCA (nbytes);
9769 memcpy (buffer, SDATA (m), nbytes);
9770 message_dolog (buffer, nbytes, 1, multibyte);
9771 SAFE_FREE ();
9773 message3_nolog (m);
9775 UNGCPRO;
9779 /* The non-logging version of message3.
9780 This does not cancel echoing, because it is used for echoing.
9781 Perhaps we need to make a separate function for echoing
9782 and make this cancel echoing. */
9784 void
9785 message3_nolog (Lisp_Object m)
9787 struct frame *sf = SELECTED_FRAME ();
9789 if (FRAME_INITIAL_P (sf))
9791 if (noninteractive_need_newline)
9792 putc ('\n', stderr);
9793 noninteractive_need_newline = 0;
9794 if (STRINGP (m))
9795 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9796 if (cursor_in_echo_area == 0)
9797 fprintf (stderr, "\n");
9798 fflush (stderr);
9800 /* Error messages get reported properly by cmd_error, so this must be just an
9801 informative message; if the frame hasn't really been initialized yet, just
9802 toss it. */
9803 else if (INTERACTIVE && sf->glyphs_initialized_p)
9805 /* Get the frame containing the mini-buffer
9806 that the selected frame is using. */
9807 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9808 Lisp_Object frame = XWINDOW (mini_window)->frame;
9809 struct frame *f = XFRAME (frame);
9811 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9812 Fmake_frame_visible (frame);
9814 if (STRINGP (m) && SCHARS (m) > 0)
9816 set_message (m);
9817 if (minibuffer_auto_raise)
9818 Fraise_frame (frame);
9819 /* Assume we are not echoing.
9820 (If we are, echo_now will override this.) */
9821 echo_message_buffer = Qnil;
9823 else
9824 clear_message (1, 1);
9826 do_pending_window_change (0);
9827 echo_area_display (1);
9828 do_pending_window_change (0);
9829 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9830 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9835 /* Display a null-terminated echo area message M. If M is 0, clear
9836 out any existing message, and let the mini-buffer text show through.
9838 The buffer M must continue to exist until after the echo area gets
9839 cleared or some other message gets displayed there. Do not pass
9840 text that is stored in a Lisp string. Do not pass text in a buffer
9841 that was alloca'd. */
9843 void
9844 message1 (const char *m)
9846 message3 (m ? build_unibyte_string (m) : Qnil);
9850 /* The non-logging counterpart of message1. */
9852 void
9853 message1_nolog (const char *m)
9855 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9858 /* Display a message M which contains a single %s
9859 which gets replaced with STRING. */
9861 void
9862 message_with_string (const char *m, Lisp_Object string, int log)
9864 CHECK_STRING (string);
9866 if (noninteractive)
9868 if (m)
9870 if (noninteractive_need_newline)
9871 putc ('\n', stderr);
9872 noninteractive_need_newline = 0;
9873 fprintf (stderr, m, SDATA (string));
9874 if (!cursor_in_echo_area)
9875 fprintf (stderr, "\n");
9876 fflush (stderr);
9879 else if (INTERACTIVE)
9881 /* The frame whose minibuffer we're going to display the message on.
9882 It may be larger than the selected frame, so we need
9883 to use its buffer, not the selected frame's buffer. */
9884 Lisp_Object mini_window;
9885 struct frame *f, *sf = SELECTED_FRAME ();
9887 /* Get the frame containing the minibuffer
9888 that the selected frame is using. */
9889 mini_window = FRAME_MINIBUF_WINDOW (sf);
9890 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9892 /* Error messages get reported properly by cmd_error, so this must be
9893 just an informative message; if the frame hasn't really been
9894 initialized yet, just toss it. */
9895 if (f->glyphs_initialized_p)
9897 Lisp_Object args[2], msg;
9898 struct gcpro gcpro1, gcpro2;
9900 args[0] = build_string (m);
9901 args[1] = msg = string;
9902 GCPRO2 (args[0], msg);
9903 gcpro1.nvars = 2;
9905 msg = Fformat (2, args);
9907 if (log)
9908 message3 (msg);
9909 else
9910 message3_nolog (msg);
9912 UNGCPRO;
9914 /* Print should start at the beginning of the message
9915 buffer next time. */
9916 message_buf_print = 0;
9922 /* Dump an informative message to the minibuf. If M is 0, clear out
9923 any existing message, and let the mini-buffer text show through. */
9925 static void
9926 vmessage (const char *m, va_list ap)
9928 if (noninteractive)
9930 if (m)
9932 if (noninteractive_need_newline)
9933 putc ('\n', stderr);
9934 noninteractive_need_newline = 0;
9935 vfprintf (stderr, m, ap);
9936 if (cursor_in_echo_area == 0)
9937 fprintf (stderr, "\n");
9938 fflush (stderr);
9941 else if (INTERACTIVE)
9943 /* The frame whose mini-buffer we're going to display the message
9944 on. It may be larger than the selected frame, so we need to
9945 use its buffer, not the selected frame's buffer. */
9946 Lisp_Object mini_window;
9947 struct frame *f, *sf = SELECTED_FRAME ();
9949 /* Get the frame containing the mini-buffer
9950 that the selected frame is using. */
9951 mini_window = FRAME_MINIBUF_WINDOW (sf);
9952 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9954 /* Error messages get reported properly by cmd_error, so this must be
9955 just an informative message; if the frame hasn't really been
9956 initialized yet, just toss it. */
9957 if (f->glyphs_initialized_p)
9959 if (m)
9961 ptrdiff_t len;
9962 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9963 char *message_buf = alloca (maxsize + 1);
9965 len = doprnt (message_buf, maxsize, m, 0, ap);
9967 message3 (make_string (message_buf, len));
9969 else
9970 message1 (0);
9972 /* Print should start at the beginning of the message
9973 buffer next time. */
9974 message_buf_print = 0;
9979 void
9980 message (const char *m, ...)
9982 va_list ap;
9983 va_start (ap, m);
9984 vmessage (m, ap);
9985 va_end (ap);
9989 #if 0
9990 /* The non-logging version of message. */
9992 void
9993 message_nolog (const char *m, ...)
9995 Lisp_Object old_log_max;
9996 va_list ap;
9997 va_start (ap, m);
9998 old_log_max = Vmessage_log_max;
9999 Vmessage_log_max = Qnil;
10000 vmessage (m, ap);
10001 Vmessage_log_max = old_log_max;
10002 va_end (ap);
10004 #endif
10007 /* Display the current message in the current mini-buffer. This is
10008 only called from error handlers in process.c, and is not time
10009 critical. */
10011 void
10012 update_echo_area (void)
10014 if (!NILP (echo_area_buffer[0]))
10016 Lisp_Object string;
10017 string = Fcurrent_message ();
10018 message3 (string);
10023 /* Make sure echo area buffers in `echo_buffers' are live.
10024 If they aren't, make new ones. */
10026 static void
10027 ensure_echo_area_buffers (void)
10029 int i;
10031 for (i = 0; i < 2; ++i)
10032 if (!BUFFERP (echo_buffer[i])
10033 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10035 char name[30];
10036 Lisp_Object old_buffer;
10037 int j;
10039 old_buffer = echo_buffer[i];
10040 echo_buffer[i] = Fget_buffer_create
10041 (make_formatted_string (name, " *Echo Area %d*", i));
10042 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10043 /* to force word wrap in echo area -
10044 it was decided to postpone this*/
10045 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10047 for (j = 0; j < 2; ++j)
10048 if (EQ (old_buffer, echo_area_buffer[j]))
10049 echo_area_buffer[j] = echo_buffer[i];
10054 /* Call FN with args A1..A2 with either the current or last displayed
10055 echo_area_buffer as current buffer.
10057 WHICH zero means use the current message buffer
10058 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10059 from echo_buffer[] and clear it.
10061 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10062 suitable buffer from echo_buffer[] and clear it.
10064 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10065 that the current message becomes the last displayed one, make
10066 choose a suitable buffer for echo_area_buffer[0], and clear it.
10068 Value is what FN returns. */
10070 static int
10071 with_echo_area_buffer (struct window *w, int which,
10072 int (*fn) (ptrdiff_t, Lisp_Object),
10073 ptrdiff_t a1, Lisp_Object a2)
10075 Lisp_Object buffer;
10076 int this_one, the_other, clear_buffer_p, rc;
10077 ptrdiff_t count = SPECPDL_INDEX ();
10079 /* If buffers aren't live, make new ones. */
10080 ensure_echo_area_buffers ();
10082 clear_buffer_p = 0;
10084 if (which == 0)
10085 this_one = 0, the_other = 1;
10086 else if (which > 0)
10087 this_one = 1, the_other = 0;
10088 else
10090 this_one = 0, the_other = 1;
10091 clear_buffer_p = 1;
10093 /* We need a fresh one in case the current echo buffer equals
10094 the one containing the last displayed echo area message. */
10095 if (!NILP (echo_area_buffer[this_one])
10096 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10097 echo_area_buffer[this_one] = Qnil;
10100 /* Choose a suitable buffer from echo_buffer[] is we don't
10101 have one. */
10102 if (NILP (echo_area_buffer[this_one]))
10104 echo_area_buffer[this_one]
10105 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10106 ? echo_buffer[the_other]
10107 : echo_buffer[this_one]);
10108 clear_buffer_p = 1;
10111 buffer = echo_area_buffer[this_one];
10113 /* Don't get confused by reusing the buffer used for echoing
10114 for a different purpose. */
10115 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10116 cancel_echoing ();
10118 record_unwind_protect (unwind_with_echo_area_buffer,
10119 with_echo_area_buffer_unwind_data (w));
10121 /* Make the echo area buffer current. Note that for display
10122 purposes, it is not necessary that the displayed window's buffer
10123 == current_buffer, except for text property lookup. So, let's
10124 only set that buffer temporarily here without doing a full
10125 Fset_window_buffer. We must also change w->pointm, though,
10126 because otherwise an assertions in unshow_buffer fails, and Emacs
10127 aborts. */
10128 set_buffer_internal_1 (XBUFFER (buffer));
10129 if (w)
10131 wset_buffer (w, buffer);
10132 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10135 bset_undo_list (current_buffer, Qt);
10136 bset_read_only (current_buffer, Qnil);
10137 specbind (Qinhibit_read_only, Qt);
10138 specbind (Qinhibit_modification_hooks, Qt);
10140 if (clear_buffer_p && Z > BEG)
10141 del_range (BEG, Z);
10143 eassert (BEGV >= BEG);
10144 eassert (ZV <= Z && ZV >= BEGV);
10146 rc = fn (a1, a2);
10148 eassert (BEGV >= BEG);
10149 eassert (ZV <= Z && ZV >= BEGV);
10151 unbind_to (count, Qnil);
10152 return rc;
10156 /* Save state that should be preserved around the call to the function
10157 FN called in with_echo_area_buffer. */
10159 static Lisp_Object
10160 with_echo_area_buffer_unwind_data (struct window *w)
10162 int i = 0;
10163 Lisp_Object vector, tmp;
10165 /* Reduce consing by keeping one vector in
10166 Vwith_echo_area_save_vector. */
10167 vector = Vwith_echo_area_save_vector;
10168 Vwith_echo_area_save_vector = Qnil;
10170 if (NILP (vector))
10171 vector = Fmake_vector (make_number (9), Qnil);
10173 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10174 ASET (vector, i, Vdeactivate_mark); ++i;
10175 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10177 if (w)
10179 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10180 ASET (vector, i, w->contents); ++i;
10181 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10182 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10183 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10184 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10186 else
10188 int end = i + 6;
10189 for (; i < end; ++i)
10190 ASET (vector, i, Qnil);
10193 eassert (i == ASIZE (vector));
10194 return vector;
10198 /* Restore global state from VECTOR which was created by
10199 with_echo_area_buffer_unwind_data. */
10201 static void
10202 unwind_with_echo_area_buffer (Lisp_Object vector)
10204 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10205 Vdeactivate_mark = AREF (vector, 1);
10206 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10208 if (WINDOWP (AREF (vector, 3)))
10210 struct window *w;
10211 Lisp_Object buffer;
10213 w = XWINDOW (AREF (vector, 3));
10214 buffer = AREF (vector, 4);
10216 wset_buffer (w, buffer);
10217 set_marker_both (w->pointm, buffer,
10218 XFASTINT (AREF (vector, 5)),
10219 XFASTINT (AREF (vector, 6)));
10220 set_marker_both (w->start, buffer,
10221 XFASTINT (AREF (vector, 7)),
10222 XFASTINT (AREF (vector, 8)));
10225 Vwith_echo_area_save_vector = vector;
10229 /* Set up the echo area for use by print functions. MULTIBYTE_P
10230 non-zero means we will print multibyte. */
10232 void
10233 setup_echo_area_for_printing (int multibyte_p)
10235 /* If we can't find an echo area any more, exit. */
10236 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10237 Fkill_emacs (Qnil);
10239 ensure_echo_area_buffers ();
10241 if (!message_buf_print)
10243 /* A message has been output since the last time we printed.
10244 Choose a fresh echo area buffer. */
10245 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10246 echo_area_buffer[0] = echo_buffer[1];
10247 else
10248 echo_area_buffer[0] = echo_buffer[0];
10250 /* Switch to that buffer and clear it. */
10251 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10252 bset_truncate_lines (current_buffer, Qnil);
10254 if (Z > BEG)
10256 ptrdiff_t count = SPECPDL_INDEX ();
10257 specbind (Qinhibit_read_only, Qt);
10258 /* Note that undo recording is always disabled. */
10259 del_range (BEG, Z);
10260 unbind_to (count, Qnil);
10262 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10264 /* Set up the buffer for the multibyteness we need. */
10265 if (multibyte_p
10266 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10267 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10269 /* Raise the frame containing the echo area. */
10270 if (minibuffer_auto_raise)
10272 struct frame *sf = SELECTED_FRAME ();
10273 Lisp_Object mini_window;
10274 mini_window = FRAME_MINIBUF_WINDOW (sf);
10275 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10278 message_log_maybe_newline ();
10279 message_buf_print = 1;
10281 else
10283 if (NILP (echo_area_buffer[0]))
10285 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10286 echo_area_buffer[0] = echo_buffer[1];
10287 else
10288 echo_area_buffer[0] = echo_buffer[0];
10291 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10293 /* Someone switched buffers between print requests. */
10294 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10295 bset_truncate_lines (current_buffer, Qnil);
10301 /* Display an echo area message in window W. Value is non-zero if W's
10302 height is changed. If display_last_displayed_message_p is
10303 non-zero, display the message that was last displayed, otherwise
10304 display the current message. */
10306 static int
10307 display_echo_area (struct window *w)
10309 int i, no_message_p, window_height_changed_p;
10311 /* Temporarily disable garbage collections while displaying the echo
10312 area. This is done because a GC can print a message itself.
10313 That message would modify the echo area buffer's contents while a
10314 redisplay of the buffer is going on, and seriously confuse
10315 redisplay. */
10316 ptrdiff_t count = inhibit_garbage_collection ();
10318 /* If there is no message, we must call display_echo_area_1
10319 nevertheless because it resizes the window. But we will have to
10320 reset the echo_area_buffer in question to nil at the end because
10321 with_echo_area_buffer will sets it to an empty buffer. */
10322 i = display_last_displayed_message_p ? 1 : 0;
10323 no_message_p = NILP (echo_area_buffer[i]);
10325 window_height_changed_p
10326 = with_echo_area_buffer (w, display_last_displayed_message_p,
10327 display_echo_area_1,
10328 (intptr_t) w, Qnil);
10330 if (no_message_p)
10331 echo_area_buffer[i] = Qnil;
10333 unbind_to (count, Qnil);
10334 return window_height_changed_p;
10338 /* Helper for display_echo_area. Display the current buffer which
10339 contains the current echo area message in window W, a mini-window,
10340 a pointer to which is passed in A1. A2..A4 are currently not used.
10341 Change the height of W so that all of the message is displayed.
10342 Value is non-zero if height of W was changed. */
10344 static int
10345 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10347 intptr_t i1 = a1;
10348 struct window *w = (struct window *) i1;
10349 Lisp_Object window;
10350 struct text_pos start;
10351 int window_height_changed_p = 0;
10353 /* Do this before displaying, so that we have a large enough glyph
10354 matrix for the display. If we can't get enough space for the
10355 whole text, display the last N lines. That works by setting w->start. */
10356 window_height_changed_p = resize_mini_window (w, 0);
10358 /* Use the starting position chosen by resize_mini_window. */
10359 SET_TEXT_POS_FROM_MARKER (start, w->start);
10361 /* Display. */
10362 clear_glyph_matrix (w->desired_matrix);
10363 XSETWINDOW (window, w);
10364 try_window (window, start, 0);
10366 return window_height_changed_p;
10370 /* Resize the echo area window to exactly the size needed for the
10371 currently displayed message, if there is one. If a mini-buffer
10372 is active, don't shrink it. */
10374 void
10375 resize_echo_area_exactly (void)
10377 if (BUFFERP (echo_area_buffer[0])
10378 && WINDOWP (echo_area_window))
10380 struct window *w = XWINDOW (echo_area_window);
10381 int resized_p;
10382 Lisp_Object resize_exactly;
10384 if (minibuf_level == 0)
10385 resize_exactly = Qt;
10386 else
10387 resize_exactly = Qnil;
10389 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10390 (intptr_t) w, resize_exactly);
10391 if (resized_p)
10393 ++windows_or_buffers_changed;
10394 ++update_mode_lines;
10395 redisplay_internal ();
10401 /* Callback function for with_echo_area_buffer, when used from
10402 resize_echo_area_exactly. A1 contains a pointer to the window to
10403 resize, EXACTLY non-nil means resize the mini-window exactly to the
10404 size of the text displayed. A3 and A4 are not used. Value is what
10405 resize_mini_window returns. */
10407 static int
10408 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10410 intptr_t i1 = a1;
10411 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10415 /* Resize mini-window W to fit the size of its contents. EXACT_P
10416 means size the window exactly to the size needed. Otherwise, it's
10417 only enlarged until W's buffer is empty.
10419 Set W->start to the right place to begin display. If the whole
10420 contents fit, start at the beginning. Otherwise, start so as
10421 to make the end of the contents appear. This is particularly
10422 important for y-or-n-p, but seems desirable generally.
10424 Value is non-zero if the window height has been changed. */
10427 resize_mini_window (struct window *w, int exact_p)
10429 struct frame *f = XFRAME (w->frame);
10430 int window_height_changed_p = 0;
10432 eassert (MINI_WINDOW_P (w));
10434 /* By default, start display at the beginning. */
10435 set_marker_both (w->start, w->contents,
10436 BUF_BEGV (XBUFFER (w->contents)),
10437 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10439 /* Don't resize windows while redisplaying a window; it would
10440 confuse redisplay functions when the size of the window they are
10441 displaying changes from under them. Such a resizing can happen,
10442 for instance, when which-func prints a long message while
10443 we are running fontification-functions. We're running these
10444 functions with safe_call which binds inhibit-redisplay to t. */
10445 if (!NILP (Vinhibit_redisplay))
10446 return 0;
10448 /* Nil means don't try to resize. */
10449 if (NILP (Vresize_mini_windows)
10450 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10451 return 0;
10453 if (!FRAME_MINIBUF_ONLY_P (f))
10455 struct it it;
10456 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10457 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10458 int height;
10459 EMACS_INT max_height;
10460 int unit = FRAME_LINE_HEIGHT (f);
10461 struct text_pos start;
10462 struct buffer *old_current_buffer = NULL;
10464 if (current_buffer != XBUFFER (w->contents))
10466 old_current_buffer = current_buffer;
10467 set_buffer_internal (XBUFFER (w->contents));
10470 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10472 /* Compute the max. number of lines specified by the user. */
10473 if (FLOATP (Vmax_mini_window_height))
10474 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10475 else if (INTEGERP (Vmax_mini_window_height))
10476 max_height = XINT (Vmax_mini_window_height);
10477 else
10478 max_height = total_height / 4;
10480 /* Correct that max. height if it's bogus. */
10481 max_height = clip_to_bounds (1, max_height, total_height);
10483 /* Find out the height of the text in the window. */
10484 if (it.line_wrap == TRUNCATE)
10485 height = 1;
10486 else
10488 last_height = 0;
10489 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10490 if (it.max_ascent == 0 && it.max_descent == 0)
10491 height = it.current_y + last_height;
10492 else
10493 height = it.current_y + it.max_ascent + it.max_descent;
10494 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10495 height = (height + unit - 1) / unit;
10498 /* Compute a suitable window start. */
10499 if (height > max_height)
10501 height = max_height;
10502 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10503 move_it_vertically_backward (&it, (height - 1) * unit);
10504 start = it.current.pos;
10506 else
10507 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10508 SET_MARKER_FROM_TEXT_POS (w->start, start);
10510 if (EQ (Vresize_mini_windows, Qgrow_only))
10512 /* Let it grow only, until we display an empty message, in which
10513 case the window shrinks again. */
10514 if (height > WINDOW_TOTAL_LINES (w))
10516 int old_height = WINDOW_TOTAL_LINES (w);
10518 FRAME_WINDOWS_FROZEN (f) = 1;
10519 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10520 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10522 else if (height < WINDOW_TOTAL_LINES (w)
10523 && (exact_p || BEGV == ZV))
10525 int old_height = WINDOW_TOTAL_LINES (w);
10527 FRAME_WINDOWS_FROZEN (f) = 0;
10528 shrink_mini_window (w);
10529 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10532 else
10534 /* Always resize to exact size needed. */
10535 if (height > WINDOW_TOTAL_LINES (w))
10537 int old_height = WINDOW_TOTAL_LINES (w);
10539 FRAME_WINDOWS_FROZEN (f) = 1;
10540 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10541 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10543 else if (height < WINDOW_TOTAL_LINES (w))
10545 int old_height = WINDOW_TOTAL_LINES (w);
10547 FRAME_WINDOWS_FROZEN (f) = 0;
10548 shrink_mini_window (w);
10550 if (height)
10552 FRAME_WINDOWS_FROZEN (f) = 1;
10553 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10556 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10560 if (old_current_buffer)
10561 set_buffer_internal (old_current_buffer);
10564 return window_height_changed_p;
10568 /* Value is the current message, a string, or nil if there is no
10569 current message. */
10571 Lisp_Object
10572 current_message (void)
10574 Lisp_Object msg;
10576 if (!BUFFERP (echo_area_buffer[0]))
10577 msg = Qnil;
10578 else
10580 with_echo_area_buffer (0, 0, current_message_1,
10581 (intptr_t) &msg, Qnil);
10582 if (NILP (msg))
10583 echo_area_buffer[0] = Qnil;
10586 return msg;
10590 static int
10591 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10593 intptr_t i1 = a1;
10594 Lisp_Object *msg = (Lisp_Object *) i1;
10596 if (Z > BEG)
10597 *msg = make_buffer_string (BEG, Z, 1);
10598 else
10599 *msg = Qnil;
10600 return 0;
10604 /* Push the current message on Vmessage_stack for later restoration
10605 by restore_message. Value is non-zero if the current message isn't
10606 empty. This is a relatively infrequent operation, so it's not
10607 worth optimizing. */
10609 bool
10610 push_message (void)
10612 Lisp_Object msg = current_message ();
10613 Vmessage_stack = Fcons (msg, Vmessage_stack);
10614 return STRINGP (msg);
10618 /* Restore message display from the top of Vmessage_stack. */
10620 void
10621 restore_message (void)
10623 eassert (CONSP (Vmessage_stack));
10624 message3_nolog (XCAR (Vmessage_stack));
10628 /* Handler for unwind-protect calling pop_message. */
10630 void
10631 pop_message_unwind (void)
10633 /* Pop the top-most entry off Vmessage_stack. */
10634 eassert (CONSP (Vmessage_stack));
10635 Vmessage_stack = XCDR (Vmessage_stack);
10639 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10640 exits. If the stack is not empty, we have a missing pop_message
10641 somewhere. */
10643 void
10644 check_message_stack (void)
10646 if (!NILP (Vmessage_stack))
10647 emacs_abort ();
10651 /* Truncate to NCHARS what will be displayed in the echo area the next
10652 time we display it---but don't redisplay it now. */
10654 void
10655 truncate_echo_area (ptrdiff_t nchars)
10657 if (nchars == 0)
10658 echo_area_buffer[0] = Qnil;
10659 else if (!noninteractive
10660 && INTERACTIVE
10661 && !NILP (echo_area_buffer[0]))
10663 struct frame *sf = SELECTED_FRAME ();
10664 /* Error messages get reported properly by cmd_error, so this must be
10665 just an informative message; if the frame hasn't really been
10666 initialized yet, just toss it. */
10667 if (sf->glyphs_initialized_p)
10668 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10673 /* Helper function for truncate_echo_area. Truncate the current
10674 message to at most NCHARS characters. */
10676 static int
10677 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10679 if (BEG + nchars < Z)
10680 del_range (BEG + nchars, Z);
10681 if (Z == BEG)
10682 echo_area_buffer[0] = Qnil;
10683 return 0;
10686 /* Set the current message to STRING. */
10688 static void
10689 set_message (Lisp_Object string)
10691 eassert (STRINGP (string));
10693 message_enable_multibyte = STRING_MULTIBYTE (string);
10695 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10696 message_buf_print = 0;
10697 help_echo_showing_p = 0;
10699 if (STRINGP (Vdebug_on_message)
10700 && STRINGP (string)
10701 && fast_string_match (Vdebug_on_message, string) >= 0)
10702 call_debugger (list2 (Qerror, string));
10706 /* Helper function for set_message. First argument is ignored and second
10707 argument has the same meaning as for set_message.
10708 This function is called with the echo area buffer being current. */
10710 static int
10711 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10713 eassert (STRINGP (string));
10715 /* Change multibyteness of the echo buffer appropriately. */
10716 if (message_enable_multibyte
10717 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10718 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10720 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10721 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10722 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10724 /* Insert new message at BEG. */
10725 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10727 /* This function takes care of single/multibyte conversion.
10728 We just have to ensure that the echo area buffer has the right
10729 setting of enable_multibyte_characters. */
10730 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10732 return 0;
10736 /* Clear messages. CURRENT_P non-zero means clear the current
10737 message. LAST_DISPLAYED_P non-zero means clear the message
10738 last displayed. */
10740 void
10741 clear_message (int current_p, int last_displayed_p)
10743 if (current_p)
10745 echo_area_buffer[0] = Qnil;
10746 message_cleared_p = 1;
10749 if (last_displayed_p)
10750 echo_area_buffer[1] = Qnil;
10752 message_buf_print = 0;
10755 /* Clear garbaged frames.
10757 This function is used where the old redisplay called
10758 redraw_garbaged_frames which in turn called redraw_frame which in
10759 turn called clear_frame. The call to clear_frame was a source of
10760 flickering. I believe a clear_frame is not necessary. It should
10761 suffice in the new redisplay to invalidate all current matrices,
10762 and ensure a complete redisplay of all windows. */
10764 static void
10765 clear_garbaged_frames (void)
10767 if (frame_garbaged)
10769 Lisp_Object tail, frame;
10770 int changed_count = 0;
10772 FOR_EACH_FRAME (tail, frame)
10774 struct frame *f = XFRAME (frame);
10776 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10778 if (f->resized_p)
10779 redraw_frame (f);
10780 else
10781 clear_current_matrices (f);
10782 changed_count++;
10783 f->garbaged = 0;
10784 f->resized_p = 0;
10788 frame_garbaged = 0;
10789 if (changed_count)
10790 ++windows_or_buffers_changed;
10795 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10796 is non-zero update selected_frame. Value is non-zero if the
10797 mini-windows height has been changed. */
10799 static int
10800 echo_area_display (int update_frame_p)
10802 Lisp_Object mini_window;
10803 struct window *w;
10804 struct frame *f;
10805 int window_height_changed_p = 0;
10806 struct frame *sf = SELECTED_FRAME ();
10808 mini_window = FRAME_MINIBUF_WINDOW (sf);
10809 w = XWINDOW (mini_window);
10810 f = XFRAME (WINDOW_FRAME (w));
10812 /* Don't display if frame is invisible or not yet initialized. */
10813 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10814 return 0;
10816 #ifdef HAVE_WINDOW_SYSTEM
10817 /* When Emacs starts, selected_frame may be the initial terminal
10818 frame. If we let this through, a message would be displayed on
10819 the terminal. */
10820 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10821 return 0;
10822 #endif /* HAVE_WINDOW_SYSTEM */
10824 /* Redraw garbaged frames. */
10825 clear_garbaged_frames ();
10827 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10829 echo_area_window = mini_window;
10830 window_height_changed_p = display_echo_area (w);
10831 w->must_be_updated_p = 1;
10833 /* Update the display, unless called from redisplay_internal.
10834 Also don't update the screen during redisplay itself. The
10835 update will happen at the end of redisplay, and an update
10836 here could cause confusion. */
10837 if (update_frame_p && !redisplaying_p)
10839 int n = 0;
10841 /* If the display update has been interrupted by pending
10842 input, update mode lines in the frame. Due to the
10843 pending input, it might have been that redisplay hasn't
10844 been called, so that mode lines above the echo area are
10845 garbaged. This looks odd, so we prevent it here. */
10846 if (!display_completed)
10847 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10849 if (window_height_changed_p
10850 /* Don't do this if Emacs is shutting down. Redisplay
10851 needs to run hooks. */
10852 && !NILP (Vrun_hooks))
10854 /* Must update other windows. Likewise as in other
10855 cases, don't let this update be interrupted by
10856 pending input. */
10857 ptrdiff_t count = SPECPDL_INDEX ();
10858 specbind (Qredisplay_dont_pause, Qt);
10859 windows_or_buffers_changed = 1;
10860 redisplay_internal ();
10861 unbind_to (count, Qnil);
10863 else if (FRAME_WINDOW_P (f) && n == 0)
10865 /* Window configuration is the same as before.
10866 Can do with a display update of the echo area,
10867 unless we displayed some mode lines. */
10868 update_single_window (w, 1);
10869 flush_frame (f);
10871 else
10872 update_frame (f, 1, 1);
10874 /* If cursor is in the echo area, make sure that the next
10875 redisplay displays the minibuffer, so that the cursor will
10876 be replaced with what the minibuffer wants. */
10877 if (cursor_in_echo_area)
10878 ++windows_or_buffers_changed;
10881 else if (!EQ (mini_window, selected_window))
10882 windows_or_buffers_changed++;
10884 /* Last displayed message is now the current message. */
10885 echo_area_buffer[1] = echo_area_buffer[0];
10886 /* Inform read_char that we're not echoing. */
10887 echo_message_buffer = Qnil;
10889 /* Prevent redisplay optimization in redisplay_internal by resetting
10890 this_line_start_pos. This is done because the mini-buffer now
10891 displays the message instead of its buffer text. */
10892 if (EQ (mini_window, selected_window))
10893 CHARPOS (this_line_start_pos) = 0;
10895 return window_height_changed_p;
10898 /* Nonzero if the current window's buffer is shown in more than one
10899 window and was modified since last redisplay. */
10901 static int
10902 buffer_shared_and_changed (void)
10904 return (buffer_window_count (current_buffer) > 1
10905 && UNCHANGED_MODIFIED < MODIFF);
10908 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10909 is enabled and mark of W's buffer was changed since last W's update. */
10911 static int
10912 window_buffer_changed (struct window *w)
10914 struct buffer *b = XBUFFER (w->contents);
10916 eassert (BUFFER_LIVE_P (b));
10918 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10919 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10920 != (w->region_showing != 0)));
10923 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10925 static int
10926 mode_line_update_needed (struct window *w)
10928 return (w->column_number_displayed != -1
10929 && !(PT == w->last_point && !window_outdated (w))
10930 && (w->column_number_displayed != current_column ()));
10933 /* Nonzero if window start of W is frozen and may not be changed during
10934 redisplay. */
10936 static bool
10937 window_frozen_p (struct window *w)
10939 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10941 Lisp_Object window;
10943 XSETWINDOW (window, w);
10944 if (MINI_WINDOW_P (w))
10945 return 0;
10946 else if (EQ (window, selected_window))
10947 return 0;
10948 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10949 && EQ (window, Vminibuf_scroll_window))
10950 /* This special window can't be frozen too. */
10951 return 0;
10952 else
10953 return 1;
10955 return 0;
10958 /***********************************************************************
10959 Mode Lines and Frame Titles
10960 ***********************************************************************/
10962 /* A buffer for constructing non-propertized mode-line strings and
10963 frame titles in it; allocated from the heap in init_xdisp and
10964 resized as needed in store_mode_line_noprop_char. */
10966 static char *mode_line_noprop_buf;
10968 /* The buffer's end, and a current output position in it. */
10970 static char *mode_line_noprop_buf_end;
10971 static char *mode_line_noprop_ptr;
10973 #define MODE_LINE_NOPROP_LEN(start) \
10974 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10976 static enum {
10977 MODE_LINE_DISPLAY = 0,
10978 MODE_LINE_TITLE,
10979 MODE_LINE_NOPROP,
10980 MODE_LINE_STRING
10981 } mode_line_target;
10983 /* Alist that caches the results of :propertize.
10984 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10985 static Lisp_Object mode_line_proptrans_alist;
10987 /* List of strings making up the mode-line. */
10988 static Lisp_Object mode_line_string_list;
10990 /* Base face property when building propertized mode line string. */
10991 static Lisp_Object mode_line_string_face;
10992 static Lisp_Object mode_line_string_face_prop;
10995 /* Unwind data for mode line strings */
10997 static Lisp_Object Vmode_line_unwind_vector;
10999 static Lisp_Object
11000 format_mode_line_unwind_data (struct frame *target_frame,
11001 struct buffer *obuf,
11002 Lisp_Object owin,
11003 int save_proptrans)
11005 Lisp_Object vector, tmp;
11007 /* Reduce consing by keeping one vector in
11008 Vwith_echo_area_save_vector. */
11009 vector = Vmode_line_unwind_vector;
11010 Vmode_line_unwind_vector = Qnil;
11012 if (NILP (vector))
11013 vector = Fmake_vector (make_number (10), Qnil);
11015 ASET (vector, 0, make_number (mode_line_target));
11016 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11017 ASET (vector, 2, mode_line_string_list);
11018 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11019 ASET (vector, 4, mode_line_string_face);
11020 ASET (vector, 5, mode_line_string_face_prop);
11022 if (obuf)
11023 XSETBUFFER (tmp, obuf);
11024 else
11025 tmp = Qnil;
11026 ASET (vector, 6, tmp);
11027 ASET (vector, 7, owin);
11028 if (target_frame)
11030 /* Similarly to `with-selected-window', if the operation selects
11031 a window on another frame, we must restore that frame's
11032 selected window, and (for a tty) the top-frame. */
11033 ASET (vector, 8, target_frame->selected_window);
11034 if (FRAME_TERMCAP_P (target_frame))
11035 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11038 return vector;
11041 static void
11042 unwind_format_mode_line (Lisp_Object vector)
11044 Lisp_Object old_window = AREF (vector, 7);
11045 Lisp_Object target_frame_window = AREF (vector, 8);
11046 Lisp_Object old_top_frame = AREF (vector, 9);
11048 mode_line_target = XINT (AREF (vector, 0));
11049 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11050 mode_line_string_list = AREF (vector, 2);
11051 if (! EQ (AREF (vector, 3), Qt))
11052 mode_line_proptrans_alist = AREF (vector, 3);
11053 mode_line_string_face = AREF (vector, 4);
11054 mode_line_string_face_prop = AREF (vector, 5);
11056 /* Select window before buffer, since it may change the buffer. */
11057 if (!NILP (old_window))
11059 /* If the operation that we are unwinding had selected a window
11060 on a different frame, reset its frame-selected-window. For a
11061 text terminal, reset its top-frame if necessary. */
11062 if (!NILP (target_frame_window))
11064 Lisp_Object frame
11065 = WINDOW_FRAME (XWINDOW (target_frame_window));
11067 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11068 Fselect_window (target_frame_window, Qt);
11070 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11071 Fselect_frame (old_top_frame, Qt);
11074 Fselect_window (old_window, Qt);
11077 if (!NILP (AREF (vector, 6)))
11079 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11080 ASET (vector, 6, Qnil);
11083 Vmode_line_unwind_vector = vector;
11087 /* Store a single character C for the frame title in mode_line_noprop_buf.
11088 Re-allocate mode_line_noprop_buf if necessary. */
11090 static void
11091 store_mode_line_noprop_char (char c)
11093 /* If output position has reached the end of the allocated buffer,
11094 increase the buffer's size. */
11095 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11097 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11098 ptrdiff_t size = len;
11099 mode_line_noprop_buf =
11100 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11101 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11102 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11105 *mode_line_noprop_ptr++ = c;
11109 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11110 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11111 characters that yield more columns than PRECISION; PRECISION <= 0
11112 means copy the whole string. Pad with spaces until FIELD_WIDTH
11113 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11114 pad. Called from display_mode_element when it is used to build a
11115 frame title. */
11117 static int
11118 store_mode_line_noprop (const char *string, int field_width, int precision)
11120 const unsigned char *str = (const unsigned char *) string;
11121 int n = 0;
11122 ptrdiff_t dummy, nbytes;
11124 /* Copy at most PRECISION chars from STR. */
11125 nbytes = strlen (string);
11126 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11127 while (nbytes--)
11128 store_mode_line_noprop_char (*str++);
11130 /* Fill up with spaces until FIELD_WIDTH reached. */
11131 while (field_width > 0
11132 && n < field_width)
11134 store_mode_line_noprop_char (' ');
11135 ++n;
11138 return n;
11141 /***********************************************************************
11142 Frame Titles
11143 ***********************************************************************/
11145 #ifdef HAVE_WINDOW_SYSTEM
11147 /* Set the title of FRAME, if it has changed. The title format is
11148 Vicon_title_format if FRAME is iconified, otherwise it is
11149 frame_title_format. */
11151 static void
11152 x_consider_frame_title (Lisp_Object frame)
11154 struct frame *f = XFRAME (frame);
11156 if (FRAME_WINDOW_P (f)
11157 || FRAME_MINIBUF_ONLY_P (f)
11158 || f->explicit_name)
11160 /* Do we have more than one visible frame on this X display? */
11161 Lisp_Object tail, other_frame, fmt;
11162 ptrdiff_t title_start;
11163 char *title;
11164 ptrdiff_t len;
11165 struct it it;
11166 ptrdiff_t count = SPECPDL_INDEX ();
11168 FOR_EACH_FRAME (tail, other_frame)
11170 struct frame *tf = XFRAME (other_frame);
11172 if (tf != f
11173 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11174 && !FRAME_MINIBUF_ONLY_P (tf)
11175 && !EQ (other_frame, tip_frame)
11176 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11177 break;
11180 /* Set global variable indicating that multiple frames exist. */
11181 multiple_frames = CONSP (tail);
11183 /* Switch to the buffer of selected window of the frame. Set up
11184 mode_line_target so that display_mode_element will output into
11185 mode_line_noprop_buf; then display the title. */
11186 record_unwind_protect (unwind_format_mode_line,
11187 format_mode_line_unwind_data
11188 (f, current_buffer, selected_window, 0));
11190 Fselect_window (f->selected_window, Qt);
11191 set_buffer_internal_1
11192 (XBUFFER (XWINDOW (f->selected_window)->contents));
11193 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11195 mode_line_target = MODE_LINE_TITLE;
11196 title_start = MODE_LINE_NOPROP_LEN (0);
11197 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11198 NULL, DEFAULT_FACE_ID);
11199 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11200 len = MODE_LINE_NOPROP_LEN (title_start);
11201 title = mode_line_noprop_buf + title_start;
11202 unbind_to (count, Qnil);
11204 /* Set the title only if it's changed. This avoids consing in
11205 the common case where it hasn't. (If it turns out that we've
11206 already wasted too much time by walking through the list with
11207 display_mode_element, then we might need to optimize at a
11208 higher level than this.) */
11209 if (! STRINGP (f->name)
11210 || SBYTES (f->name) != len
11211 || memcmp (title, SDATA (f->name), len) != 0)
11212 x_implicitly_set_name (f, make_string (title, len), Qnil);
11216 #endif /* not HAVE_WINDOW_SYSTEM */
11219 /***********************************************************************
11220 Menu Bars
11221 ***********************************************************************/
11224 /* Prepare for redisplay by updating menu-bar item lists when
11225 appropriate. This can call eval. */
11227 void
11228 prepare_menu_bars (void)
11230 int all_windows;
11231 struct gcpro gcpro1, gcpro2;
11232 struct frame *f;
11233 Lisp_Object tooltip_frame;
11235 #ifdef HAVE_WINDOW_SYSTEM
11236 tooltip_frame = tip_frame;
11237 #else
11238 tooltip_frame = Qnil;
11239 #endif
11241 /* Update all frame titles based on their buffer names, etc. We do
11242 this before the menu bars so that the buffer-menu will show the
11243 up-to-date frame titles. */
11244 #ifdef HAVE_WINDOW_SYSTEM
11245 if (windows_or_buffers_changed || update_mode_lines)
11247 Lisp_Object tail, frame;
11249 FOR_EACH_FRAME (tail, frame)
11251 f = XFRAME (frame);
11252 if (!EQ (frame, tooltip_frame)
11253 && (FRAME_ICONIFIED_P (f)
11254 || FRAME_VISIBLE_P (f) == 1
11255 /* Exclude TTY frames that are obscured because they
11256 are not the top frame on their console. This is
11257 because x_consider_frame_title actually switches
11258 to the frame, which for TTY frames means it is
11259 marked as garbaged, and will be completely
11260 redrawn on the next redisplay cycle. This causes
11261 TTY frames to be completely redrawn, when there
11262 are more than one of them, even though nothing
11263 should be changed on display. */
11264 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11265 x_consider_frame_title (frame);
11268 #endif /* HAVE_WINDOW_SYSTEM */
11270 /* Update the menu bar item lists, if appropriate. This has to be
11271 done before any actual redisplay or generation of display lines. */
11272 all_windows = (update_mode_lines
11273 || buffer_shared_and_changed ()
11274 || windows_or_buffers_changed);
11275 if (all_windows)
11277 Lisp_Object tail, frame;
11278 ptrdiff_t count = SPECPDL_INDEX ();
11279 /* 1 means that update_menu_bar has run its hooks
11280 so any further calls to update_menu_bar shouldn't do so again. */
11281 int menu_bar_hooks_run = 0;
11283 record_unwind_save_match_data ();
11285 FOR_EACH_FRAME (tail, frame)
11287 f = XFRAME (frame);
11289 /* Ignore tooltip frame. */
11290 if (EQ (frame, tooltip_frame))
11291 continue;
11293 /* If a window on this frame changed size, report that to
11294 the user and clear the size-change flag. */
11295 if (FRAME_WINDOW_SIZES_CHANGED (f))
11297 Lisp_Object functions;
11299 /* Clear flag first in case we get an error below. */
11300 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11301 functions = Vwindow_size_change_functions;
11302 GCPRO2 (tail, functions);
11304 while (CONSP (functions))
11306 if (!EQ (XCAR (functions), Qt))
11307 call1 (XCAR (functions), frame);
11308 functions = XCDR (functions);
11310 UNGCPRO;
11313 GCPRO1 (tail);
11314 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11315 #ifdef HAVE_WINDOW_SYSTEM
11316 update_tool_bar (f, 0);
11317 #endif
11318 #ifdef HAVE_NS
11319 if (windows_or_buffers_changed
11320 && FRAME_NS_P (f))
11321 ns_set_doc_edited
11322 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11323 #endif
11324 UNGCPRO;
11327 unbind_to (count, Qnil);
11329 else
11331 struct frame *sf = SELECTED_FRAME ();
11332 update_menu_bar (sf, 1, 0);
11333 #ifdef HAVE_WINDOW_SYSTEM
11334 update_tool_bar (sf, 1);
11335 #endif
11340 /* Update the menu bar item list for frame F. This has to be done
11341 before we start to fill in any display lines, because it can call
11342 eval.
11344 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11346 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11347 already ran the menu bar hooks for this redisplay, so there
11348 is no need to run them again. The return value is the
11349 updated value of this flag, to pass to the next call. */
11351 static int
11352 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11354 Lisp_Object window;
11355 register struct window *w;
11357 /* If called recursively during a menu update, do nothing. This can
11358 happen when, for instance, an activate-menubar-hook causes a
11359 redisplay. */
11360 if (inhibit_menubar_update)
11361 return hooks_run;
11363 window = FRAME_SELECTED_WINDOW (f);
11364 w = XWINDOW (window);
11366 if (FRAME_WINDOW_P (f)
11368 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11369 || defined (HAVE_NS) || defined (USE_GTK)
11370 FRAME_EXTERNAL_MENU_BAR (f)
11371 #else
11372 FRAME_MENU_BAR_LINES (f) > 0
11373 #endif
11374 : FRAME_MENU_BAR_LINES (f) > 0)
11376 /* If the user has switched buffers or windows, we need to
11377 recompute to reflect the new bindings. But we'll
11378 recompute when update_mode_lines is set too; that means
11379 that people can use force-mode-line-update to request
11380 that the menu bar be recomputed. The adverse effect on
11381 the rest of the redisplay algorithm is about the same as
11382 windows_or_buffers_changed anyway. */
11383 if (windows_or_buffers_changed
11384 /* This used to test w->update_mode_line, but we believe
11385 there is no need to recompute the menu in that case. */
11386 || update_mode_lines
11387 || window_buffer_changed (w))
11389 struct buffer *prev = current_buffer;
11390 ptrdiff_t count = SPECPDL_INDEX ();
11392 specbind (Qinhibit_menubar_update, Qt);
11394 set_buffer_internal_1 (XBUFFER (w->contents));
11395 if (save_match_data)
11396 record_unwind_save_match_data ();
11397 if (NILP (Voverriding_local_map_menu_flag))
11399 specbind (Qoverriding_terminal_local_map, Qnil);
11400 specbind (Qoverriding_local_map, Qnil);
11403 if (!hooks_run)
11405 /* Run the Lucid hook. */
11406 safe_run_hooks (Qactivate_menubar_hook);
11408 /* If it has changed current-menubar from previous value,
11409 really recompute the menu-bar from the value. */
11410 if (! NILP (Vlucid_menu_bar_dirty_flag))
11411 call0 (Qrecompute_lucid_menubar);
11413 safe_run_hooks (Qmenu_bar_update_hook);
11415 hooks_run = 1;
11418 XSETFRAME (Vmenu_updating_frame, f);
11419 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11421 /* Redisplay the menu bar in case we changed it. */
11422 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11423 || defined (HAVE_NS) || defined (USE_GTK)
11424 if (FRAME_WINDOW_P (f))
11426 #if defined (HAVE_NS)
11427 /* All frames on Mac OS share the same menubar. So only
11428 the selected frame should be allowed to set it. */
11429 if (f == SELECTED_FRAME ())
11430 #endif
11431 set_frame_menubar (f, 0, 0);
11433 else
11434 /* On a terminal screen, the menu bar is an ordinary screen
11435 line, and this makes it get updated. */
11436 w->update_mode_line = 1;
11437 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11438 /* In the non-toolkit version, the menu bar is an ordinary screen
11439 line, and this makes it get updated. */
11440 w->update_mode_line = 1;
11441 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11443 unbind_to (count, Qnil);
11444 set_buffer_internal_1 (prev);
11448 return hooks_run;
11451 /***********************************************************************
11452 Tool-bars
11453 ***********************************************************************/
11455 #ifdef HAVE_WINDOW_SYSTEM
11457 /* Tool-bar item index of the item on which a mouse button was pressed
11458 or -1. */
11460 int last_tool_bar_item;
11462 /* Select `frame' temporarily without running all the code in
11463 do_switch_frame.
11464 FIXME: Maybe do_switch_frame should be trimmed down similarly
11465 when `norecord' is set. */
11466 static void
11467 fast_set_selected_frame (Lisp_Object frame)
11469 if (!EQ (selected_frame, frame))
11471 selected_frame = frame;
11472 selected_window = XFRAME (frame)->selected_window;
11476 /* Update the tool-bar item list for frame F. This has to be done
11477 before we start to fill in any display lines. Called from
11478 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11479 and restore it here. */
11481 static void
11482 update_tool_bar (struct frame *f, int save_match_data)
11484 #if defined (USE_GTK) || defined (HAVE_NS)
11485 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11486 #else
11487 int do_update = WINDOWP (f->tool_bar_window)
11488 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11489 #endif
11491 if (do_update)
11493 Lisp_Object window;
11494 struct window *w;
11496 window = FRAME_SELECTED_WINDOW (f);
11497 w = XWINDOW (window);
11499 /* If the user has switched buffers or windows, we need to
11500 recompute to reflect the new bindings. But we'll
11501 recompute when update_mode_lines is set too; that means
11502 that people can use force-mode-line-update to request
11503 that the menu bar be recomputed. The adverse effect on
11504 the rest of the redisplay algorithm is about the same as
11505 windows_or_buffers_changed anyway. */
11506 if (windows_or_buffers_changed
11507 || w->update_mode_line
11508 || update_mode_lines
11509 || window_buffer_changed (w))
11511 struct buffer *prev = current_buffer;
11512 ptrdiff_t count = SPECPDL_INDEX ();
11513 Lisp_Object frame, new_tool_bar;
11514 int new_n_tool_bar;
11515 struct gcpro gcpro1;
11517 /* Set current_buffer to the buffer of the selected
11518 window of the frame, so that we get the right local
11519 keymaps. */
11520 set_buffer_internal_1 (XBUFFER (w->contents));
11522 /* Save match data, if we must. */
11523 if (save_match_data)
11524 record_unwind_save_match_data ();
11526 /* Make sure that we don't accidentally use bogus keymaps. */
11527 if (NILP (Voverriding_local_map_menu_flag))
11529 specbind (Qoverriding_terminal_local_map, Qnil);
11530 specbind (Qoverriding_local_map, Qnil);
11533 GCPRO1 (new_tool_bar);
11535 /* We must temporarily set the selected frame to this frame
11536 before calling tool_bar_items, because the calculation of
11537 the tool-bar keymap uses the selected frame (see
11538 `tool-bar-make-keymap' in tool-bar.el). */
11539 eassert (EQ (selected_window,
11540 /* Since we only explicitly preserve selected_frame,
11541 check that selected_window would be redundant. */
11542 XFRAME (selected_frame)->selected_window));
11543 record_unwind_protect (fast_set_selected_frame, selected_frame);
11544 XSETFRAME (frame, f);
11545 fast_set_selected_frame (frame);
11547 /* Build desired tool-bar items from keymaps. */
11548 new_tool_bar
11549 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11550 &new_n_tool_bar);
11552 /* Redisplay the tool-bar if we changed it. */
11553 if (new_n_tool_bar != f->n_tool_bar_items
11554 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11556 /* Redisplay that happens asynchronously due to an expose event
11557 may access f->tool_bar_items. Make sure we update both
11558 variables within BLOCK_INPUT so no such event interrupts. */
11559 block_input ();
11560 fset_tool_bar_items (f, new_tool_bar);
11561 f->n_tool_bar_items = new_n_tool_bar;
11562 w->update_mode_line = 1;
11563 unblock_input ();
11566 UNGCPRO;
11568 unbind_to (count, Qnil);
11569 set_buffer_internal_1 (prev);
11574 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11576 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11577 F's desired tool-bar contents. F->tool_bar_items must have
11578 been set up previously by calling prepare_menu_bars. */
11580 static void
11581 build_desired_tool_bar_string (struct frame *f)
11583 int i, size, size_needed;
11584 struct gcpro gcpro1, gcpro2, gcpro3;
11585 Lisp_Object image, plist, props;
11587 image = plist = props = Qnil;
11588 GCPRO3 (image, plist, props);
11590 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11591 Otherwise, make a new string. */
11593 /* The size of the string we might be able to reuse. */
11594 size = (STRINGP (f->desired_tool_bar_string)
11595 ? SCHARS (f->desired_tool_bar_string)
11596 : 0);
11598 /* We need one space in the string for each image. */
11599 size_needed = f->n_tool_bar_items;
11601 /* Reuse f->desired_tool_bar_string, if possible. */
11602 if (size < size_needed || NILP (f->desired_tool_bar_string))
11603 fset_desired_tool_bar_string
11604 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11605 else
11607 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11608 Fremove_text_properties (make_number (0), make_number (size),
11609 props, f->desired_tool_bar_string);
11612 /* Put a `display' property on the string for the images to display,
11613 put a `menu_item' property on tool-bar items with a value that
11614 is the index of the item in F's tool-bar item vector. */
11615 for (i = 0; i < f->n_tool_bar_items; ++i)
11617 #define PROP(IDX) \
11618 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11620 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11621 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11622 int hmargin, vmargin, relief, idx, end;
11624 /* If image is a vector, choose the image according to the
11625 button state. */
11626 image = PROP (TOOL_BAR_ITEM_IMAGES);
11627 if (VECTORP (image))
11629 if (enabled_p)
11630 idx = (selected_p
11631 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11632 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11633 else
11634 idx = (selected_p
11635 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11636 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11638 eassert (ASIZE (image) >= idx);
11639 image = AREF (image, idx);
11641 else
11642 idx = -1;
11644 /* Ignore invalid image specifications. */
11645 if (!valid_image_p (image))
11646 continue;
11648 /* Display the tool-bar button pressed, or depressed. */
11649 plist = Fcopy_sequence (XCDR (image));
11651 /* Compute margin and relief to draw. */
11652 relief = (tool_bar_button_relief >= 0
11653 ? tool_bar_button_relief
11654 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11655 hmargin = vmargin = relief;
11657 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11658 INT_MAX - max (hmargin, vmargin)))
11660 hmargin += XFASTINT (Vtool_bar_button_margin);
11661 vmargin += XFASTINT (Vtool_bar_button_margin);
11663 else if (CONSP (Vtool_bar_button_margin))
11665 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11666 INT_MAX - hmargin))
11667 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11669 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11670 INT_MAX - vmargin))
11671 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11674 if (auto_raise_tool_bar_buttons_p)
11676 /* Add a `:relief' property to the image spec if the item is
11677 selected. */
11678 if (selected_p)
11680 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11681 hmargin -= relief;
11682 vmargin -= relief;
11685 else
11687 /* If image is selected, display it pressed, i.e. with a
11688 negative relief. If it's not selected, display it with a
11689 raised relief. */
11690 plist = Fplist_put (plist, QCrelief,
11691 (selected_p
11692 ? make_number (-relief)
11693 : make_number (relief)));
11694 hmargin -= relief;
11695 vmargin -= relief;
11698 /* Put a margin around the image. */
11699 if (hmargin || vmargin)
11701 if (hmargin == vmargin)
11702 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11703 else
11704 plist = Fplist_put (plist, QCmargin,
11705 Fcons (make_number (hmargin),
11706 make_number (vmargin)));
11709 /* If button is not enabled, and we don't have special images
11710 for the disabled state, make the image appear disabled by
11711 applying an appropriate algorithm to it. */
11712 if (!enabled_p && idx < 0)
11713 plist = Fplist_put (plist, QCconversion, Qdisabled);
11715 /* Put a `display' text property on the string for the image to
11716 display. Put a `menu-item' property on the string that gives
11717 the start of this item's properties in the tool-bar items
11718 vector. */
11719 image = Fcons (Qimage, plist);
11720 props = list4 (Qdisplay, image,
11721 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11723 /* Let the last image hide all remaining spaces in the tool bar
11724 string. The string can be longer than needed when we reuse a
11725 previous string. */
11726 if (i + 1 == f->n_tool_bar_items)
11727 end = SCHARS (f->desired_tool_bar_string);
11728 else
11729 end = i + 1;
11730 Fadd_text_properties (make_number (i), make_number (end),
11731 props, f->desired_tool_bar_string);
11732 #undef PROP
11735 UNGCPRO;
11739 /* Display one line of the tool-bar of frame IT->f.
11741 HEIGHT specifies the desired height of the tool-bar line.
11742 If the actual height of the glyph row is less than HEIGHT, the
11743 row's height is increased to HEIGHT, and the icons are centered
11744 vertically in the new height.
11746 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11747 count a final empty row in case the tool-bar width exactly matches
11748 the window width.
11751 static void
11752 display_tool_bar_line (struct it *it, int height)
11754 struct glyph_row *row = it->glyph_row;
11755 int max_x = it->last_visible_x;
11756 struct glyph *last;
11758 prepare_desired_row (row);
11759 row->y = it->current_y;
11761 /* Note that this isn't made use of if the face hasn't a box,
11762 so there's no need to check the face here. */
11763 it->start_of_box_run_p = 1;
11765 while (it->current_x < max_x)
11767 int x, n_glyphs_before, i, nglyphs;
11768 struct it it_before;
11770 /* Get the next display element. */
11771 if (!get_next_display_element (it))
11773 /* Don't count empty row if we are counting needed tool-bar lines. */
11774 if (height < 0 && !it->hpos)
11775 return;
11776 break;
11779 /* Produce glyphs. */
11780 n_glyphs_before = row->used[TEXT_AREA];
11781 it_before = *it;
11783 PRODUCE_GLYPHS (it);
11785 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11786 i = 0;
11787 x = it_before.current_x;
11788 while (i < nglyphs)
11790 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11792 if (x + glyph->pixel_width > max_x)
11794 /* Glyph doesn't fit on line. Backtrack. */
11795 row->used[TEXT_AREA] = n_glyphs_before;
11796 *it = it_before;
11797 /* If this is the only glyph on this line, it will never fit on the
11798 tool-bar, so skip it. But ensure there is at least one glyph,
11799 so we don't accidentally disable the tool-bar. */
11800 if (n_glyphs_before == 0
11801 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11802 break;
11803 goto out;
11806 ++it->hpos;
11807 x += glyph->pixel_width;
11808 ++i;
11811 /* Stop at line end. */
11812 if (ITERATOR_AT_END_OF_LINE_P (it))
11813 break;
11815 set_iterator_to_next (it, 1);
11818 out:;
11820 row->displays_text_p = row->used[TEXT_AREA] != 0;
11822 /* Use default face for the border below the tool bar.
11824 FIXME: When auto-resize-tool-bars is grow-only, there is
11825 no additional border below the possibly empty tool-bar lines.
11826 So to make the extra empty lines look "normal", we have to
11827 use the tool-bar face for the border too. */
11828 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11829 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11830 it->face_id = DEFAULT_FACE_ID;
11832 extend_face_to_end_of_line (it);
11833 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11834 last->right_box_line_p = 1;
11835 if (last == row->glyphs[TEXT_AREA])
11836 last->left_box_line_p = 1;
11838 /* Make line the desired height and center it vertically. */
11839 if ((height -= it->max_ascent + it->max_descent) > 0)
11841 /* Don't add more than one line height. */
11842 height %= FRAME_LINE_HEIGHT (it->f);
11843 it->max_ascent += height / 2;
11844 it->max_descent += (height + 1) / 2;
11847 compute_line_metrics (it);
11849 /* If line is empty, make it occupy the rest of the tool-bar. */
11850 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11852 row->height = row->phys_height = it->last_visible_y - row->y;
11853 row->visible_height = row->height;
11854 row->ascent = row->phys_ascent = 0;
11855 row->extra_line_spacing = 0;
11858 row->full_width_p = 1;
11859 row->continued_p = 0;
11860 row->truncated_on_left_p = 0;
11861 row->truncated_on_right_p = 0;
11863 it->current_x = it->hpos = 0;
11864 it->current_y += row->height;
11865 ++it->vpos;
11866 ++it->glyph_row;
11870 /* Max tool-bar height. */
11872 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11873 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11875 /* Value is the number of screen lines needed to make all tool-bar
11876 items of frame F visible. The number of actual rows needed is
11877 returned in *N_ROWS if non-NULL. */
11879 static int
11880 tool_bar_lines_needed (struct frame *f, int *n_rows)
11882 struct window *w = XWINDOW (f->tool_bar_window);
11883 struct it it;
11884 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11885 the desired matrix, so use (unused) mode-line row as temporary row to
11886 avoid destroying the first tool-bar row. */
11887 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11889 /* Initialize an iterator for iteration over
11890 F->desired_tool_bar_string in the tool-bar window of frame F. */
11891 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11892 it.first_visible_x = 0;
11893 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11894 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11895 it.paragraph_embedding = L2R;
11897 while (!ITERATOR_AT_END_P (&it))
11899 clear_glyph_row (temp_row);
11900 it.glyph_row = temp_row;
11901 display_tool_bar_line (&it, -1);
11903 clear_glyph_row (temp_row);
11905 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11906 if (n_rows)
11907 *n_rows = it.vpos > 0 ? it.vpos : -1;
11909 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11912 #endif /* !USE_GTK && !HAVE_NS */
11914 #if defined USE_GTK || defined HAVE_NS
11915 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
11916 #endif
11918 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11919 0, 1, 0,
11920 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11921 If FRAME is nil or omitted, use the selected frame. */)
11922 (Lisp_Object frame)
11924 int nlines = 0;
11925 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11926 struct frame *f = decode_any_frame (frame);
11927 struct window *w;
11929 if (WINDOWP (f->tool_bar_window)
11930 && (w = XWINDOW (f->tool_bar_window),
11931 WINDOW_TOTAL_LINES (w) > 0))
11933 update_tool_bar (f, 1);
11934 if (f->n_tool_bar_items)
11936 build_desired_tool_bar_string (f);
11937 nlines = tool_bar_lines_needed (f, NULL);
11940 #endif
11941 return make_number (nlines);
11945 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11946 height should be changed. */
11948 static int
11949 redisplay_tool_bar (struct frame *f)
11951 #if defined (USE_GTK) || defined (HAVE_NS)
11953 if (FRAME_EXTERNAL_TOOL_BAR (f))
11954 update_frame_tool_bar (f);
11955 return 0;
11957 #else /* !USE_GTK && !HAVE_NS */
11959 struct window *w;
11960 struct it it;
11961 struct glyph_row *row;
11963 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11964 do anything. This means you must start with tool-bar-lines
11965 non-zero to get the auto-sizing effect. Or in other words, you
11966 can turn off tool-bars by specifying tool-bar-lines zero. */
11967 if (!WINDOWP (f->tool_bar_window)
11968 || (w = XWINDOW (f->tool_bar_window),
11969 WINDOW_TOTAL_LINES (w) == 0))
11970 return 0;
11972 /* Set up an iterator for the tool-bar window. */
11973 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11974 it.first_visible_x = 0;
11975 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11976 row = it.glyph_row;
11978 /* Build a string that represents the contents of the tool-bar. */
11979 build_desired_tool_bar_string (f);
11980 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11981 /* FIXME: This should be controlled by a user option. But it
11982 doesn't make sense to have an R2L tool bar if the menu bar cannot
11983 be drawn also R2L, and making the menu bar R2L is tricky due
11984 toolkit-specific code that implements it. If an R2L tool bar is
11985 ever supported, display_tool_bar_line should also be augmented to
11986 call unproduce_glyphs like display_line and display_string
11987 do. */
11988 it.paragraph_embedding = L2R;
11990 if (f->n_tool_bar_rows == 0)
11992 int nlines;
11994 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11995 nlines != WINDOW_TOTAL_LINES (w)))
11997 Lisp_Object frame;
11998 int old_height = WINDOW_TOTAL_LINES (w);
12000 XSETFRAME (frame, f);
12001 Fmodify_frame_parameters (frame,
12002 list1 (Fcons (Qtool_bar_lines,
12003 make_number (nlines))));
12004 if (WINDOW_TOTAL_LINES (w) != old_height)
12006 clear_glyph_matrix (w->desired_matrix);
12007 f->fonts_changed = 1;
12008 return 1;
12013 /* Display as many lines as needed to display all tool-bar items. */
12015 if (f->n_tool_bar_rows > 0)
12017 int border, rows, height, extra;
12019 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12020 border = XINT (Vtool_bar_border);
12021 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12022 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12023 else if (EQ (Vtool_bar_border, Qborder_width))
12024 border = f->border_width;
12025 else
12026 border = 0;
12027 if (border < 0)
12028 border = 0;
12030 rows = f->n_tool_bar_rows;
12031 height = max (1, (it.last_visible_y - border) / rows);
12032 extra = it.last_visible_y - border - height * rows;
12034 while (it.current_y < it.last_visible_y)
12036 int h = 0;
12037 if (extra > 0 && rows-- > 0)
12039 h = (extra + rows - 1) / rows;
12040 extra -= h;
12042 display_tool_bar_line (&it, height + h);
12045 else
12047 while (it.current_y < it.last_visible_y)
12048 display_tool_bar_line (&it, 0);
12051 /* It doesn't make much sense to try scrolling in the tool-bar
12052 window, so don't do it. */
12053 w->desired_matrix->no_scrolling_p = 1;
12054 w->must_be_updated_p = 1;
12056 if (!NILP (Vauto_resize_tool_bars))
12058 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12059 int change_height_p = 0;
12061 /* If we couldn't display everything, change the tool-bar's
12062 height if there is room for more. */
12063 if (IT_STRING_CHARPOS (it) < it.end_charpos
12064 && it.current_y < max_tool_bar_height)
12065 change_height_p = 1;
12067 row = it.glyph_row - 1;
12069 /* If there are blank lines at the end, except for a partially
12070 visible blank line at the end that is smaller than
12071 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12072 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12073 && row->height >= FRAME_LINE_HEIGHT (f))
12074 change_height_p = 1;
12076 /* If row displays tool-bar items, but is partially visible,
12077 change the tool-bar's height. */
12078 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12079 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12080 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12081 change_height_p = 1;
12083 /* Resize windows as needed by changing the `tool-bar-lines'
12084 frame parameter. */
12085 if (change_height_p)
12087 Lisp_Object frame;
12088 int old_height = WINDOW_TOTAL_LINES (w);
12089 int nrows;
12090 int nlines = tool_bar_lines_needed (f, &nrows);
12092 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12093 && !f->minimize_tool_bar_window_p)
12094 ? (nlines > old_height)
12095 : (nlines != old_height));
12096 f->minimize_tool_bar_window_p = 0;
12098 if (change_height_p)
12100 XSETFRAME (frame, f);
12101 Fmodify_frame_parameters (frame,
12102 list1 (Fcons (Qtool_bar_lines,
12103 make_number (nlines))));
12104 if (WINDOW_TOTAL_LINES (w) != old_height)
12106 clear_glyph_matrix (w->desired_matrix);
12107 f->n_tool_bar_rows = nrows;
12108 f->fonts_changed = 1;
12109 return 1;
12115 f->minimize_tool_bar_window_p = 0;
12116 return 0;
12118 #endif /* USE_GTK || HAVE_NS */
12121 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12123 /* Get information about the tool-bar item which is displayed in GLYPH
12124 on frame F. Return in *PROP_IDX the index where tool-bar item
12125 properties start in F->tool_bar_items. Value is zero if
12126 GLYPH doesn't display a tool-bar item. */
12128 static int
12129 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12131 Lisp_Object prop;
12132 int success_p;
12133 int charpos;
12135 /* This function can be called asynchronously, which means we must
12136 exclude any possibility that Fget_text_property signals an
12137 error. */
12138 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12139 charpos = max (0, charpos);
12141 /* Get the text property `menu-item' at pos. The value of that
12142 property is the start index of this item's properties in
12143 F->tool_bar_items. */
12144 prop = Fget_text_property (make_number (charpos),
12145 Qmenu_item, f->current_tool_bar_string);
12146 if (INTEGERP (prop))
12148 *prop_idx = XINT (prop);
12149 success_p = 1;
12151 else
12152 success_p = 0;
12154 return success_p;
12158 /* Get information about the tool-bar item at position X/Y on frame F.
12159 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12160 the current matrix of the tool-bar window of F, or NULL if not
12161 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12162 item in F->tool_bar_items. Value is
12164 -1 if X/Y is not on a tool-bar item
12165 0 if X/Y is on the same item that was highlighted before.
12166 1 otherwise. */
12168 static int
12169 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12170 int *hpos, int *vpos, int *prop_idx)
12172 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12173 struct window *w = XWINDOW (f->tool_bar_window);
12174 int area;
12176 /* Find the glyph under X/Y. */
12177 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12178 if (*glyph == NULL)
12179 return -1;
12181 /* Get the start of this tool-bar item's properties in
12182 f->tool_bar_items. */
12183 if (!tool_bar_item_info (f, *glyph, prop_idx))
12184 return -1;
12186 /* Is mouse on the highlighted item? */
12187 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12188 && *vpos >= hlinfo->mouse_face_beg_row
12189 && *vpos <= hlinfo->mouse_face_end_row
12190 && (*vpos > hlinfo->mouse_face_beg_row
12191 || *hpos >= hlinfo->mouse_face_beg_col)
12192 && (*vpos < hlinfo->mouse_face_end_row
12193 || *hpos < hlinfo->mouse_face_end_col
12194 || hlinfo->mouse_face_past_end))
12195 return 0;
12197 return 1;
12201 /* EXPORT:
12202 Handle mouse button event on the tool-bar of frame F, at
12203 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12204 0 for button release. MODIFIERS is event modifiers for button
12205 release. */
12207 void
12208 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12209 int modifiers)
12211 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12212 struct window *w = XWINDOW (f->tool_bar_window);
12213 int hpos, vpos, prop_idx;
12214 struct glyph *glyph;
12215 Lisp_Object enabled_p;
12216 int ts;
12218 /* If not on the highlighted tool-bar item, and mouse-highlight is
12219 non-nil, return. This is so we generate the tool-bar button
12220 click only when the mouse button is released on the same item as
12221 where it was pressed. However, when mouse-highlight is disabled,
12222 generate the click when the button is released regardless of the
12223 highlight, since tool-bar items are not highlighted in that
12224 case. */
12225 frame_to_window_pixel_xy (w, &x, &y);
12226 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12227 if (ts == -1
12228 || (ts != 0 && !NILP (Vmouse_highlight)))
12229 return;
12231 /* When mouse-highlight is off, generate the click for the item
12232 where the button was pressed, disregarding where it was
12233 released. */
12234 if (NILP (Vmouse_highlight) && !down_p)
12235 prop_idx = last_tool_bar_item;
12237 /* If item is disabled, do nothing. */
12238 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12239 if (NILP (enabled_p))
12240 return;
12242 if (down_p)
12244 /* Show item in pressed state. */
12245 if (!NILP (Vmouse_highlight))
12246 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12247 last_tool_bar_item = prop_idx;
12249 else
12251 Lisp_Object key, frame;
12252 struct input_event event;
12253 EVENT_INIT (event);
12255 /* Show item in released state. */
12256 if (!NILP (Vmouse_highlight))
12257 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12259 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12261 XSETFRAME (frame, f);
12262 event.kind = TOOL_BAR_EVENT;
12263 event.frame_or_window = frame;
12264 event.arg = frame;
12265 kbd_buffer_store_event (&event);
12267 event.kind = TOOL_BAR_EVENT;
12268 event.frame_or_window = frame;
12269 event.arg = key;
12270 event.modifiers = modifiers;
12271 kbd_buffer_store_event (&event);
12272 last_tool_bar_item = -1;
12277 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12278 tool-bar window-relative coordinates X/Y. Called from
12279 note_mouse_highlight. */
12281 static void
12282 note_tool_bar_highlight (struct frame *f, int x, int y)
12284 Lisp_Object window = f->tool_bar_window;
12285 struct window *w = XWINDOW (window);
12286 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12287 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12288 int hpos, vpos;
12289 struct glyph *glyph;
12290 struct glyph_row *row;
12291 int i;
12292 Lisp_Object enabled_p;
12293 int prop_idx;
12294 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12295 int mouse_down_p, rc;
12297 /* Function note_mouse_highlight is called with negative X/Y
12298 values when mouse moves outside of the frame. */
12299 if (x <= 0 || y <= 0)
12301 clear_mouse_face (hlinfo);
12302 return;
12305 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12306 if (rc < 0)
12308 /* Not on tool-bar item. */
12309 clear_mouse_face (hlinfo);
12310 return;
12312 else if (rc == 0)
12313 /* On same tool-bar item as before. */
12314 goto set_help_echo;
12316 clear_mouse_face (hlinfo);
12318 /* Mouse is down, but on different tool-bar item? */
12319 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12320 && f == dpyinfo->last_mouse_frame);
12322 if (mouse_down_p
12323 && last_tool_bar_item != prop_idx)
12324 return;
12326 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12328 /* If tool-bar item is not enabled, don't highlight it. */
12329 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12330 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12332 /* Compute the x-position of the glyph. In front and past the
12333 image is a space. We include this in the highlighted area. */
12334 row = MATRIX_ROW (w->current_matrix, vpos);
12335 for (i = x = 0; i < hpos; ++i)
12336 x += row->glyphs[TEXT_AREA][i].pixel_width;
12338 /* Record this as the current active region. */
12339 hlinfo->mouse_face_beg_col = hpos;
12340 hlinfo->mouse_face_beg_row = vpos;
12341 hlinfo->mouse_face_beg_x = x;
12342 hlinfo->mouse_face_past_end = 0;
12344 hlinfo->mouse_face_end_col = hpos + 1;
12345 hlinfo->mouse_face_end_row = vpos;
12346 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12347 hlinfo->mouse_face_window = window;
12348 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12350 /* Display it as active. */
12351 show_mouse_face (hlinfo, draw);
12354 set_help_echo:
12356 /* Set help_echo_string to a help string to display for this tool-bar item.
12357 XTread_socket does the rest. */
12358 help_echo_object = help_echo_window = Qnil;
12359 help_echo_pos = -1;
12360 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12361 if (NILP (help_echo_string))
12362 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12365 #endif /* !USE_GTK && !HAVE_NS */
12367 #endif /* HAVE_WINDOW_SYSTEM */
12371 /************************************************************************
12372 Horizontal scrolling
12373 ************************************************************************/
12375 static int hscroll_window_tree (Lisp_Object);
12376 static int hscroll_windows (Lisp_Object);
12378 /* For all leaf windows in the window tree rooted at WINDOW, set their
12379 hscroll value so that PT is (i) visible in the window, and (ii) so
12380 that it is not within a certain margin at the window's left and
12381 right border. Value is non-zero if any window's hscroll has been
12382 changed. */
12384 static int
12385 hscroll_window_tree (Lisp_Object window)
12387 int hscrolled_p = 0;
12388 int hscroll_relative_p = FLOATP (Vhscroll_step);
12389 int hscroll_step_abs = 0;
12390 double hscroll_step_rel = 0;
12392 if (hscroll_relative_p)
12394 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12395 if (hscroll_step_rel < 0)
12397 hscroll_relative_p = 0;
12398 hscroll_step_abs = 0;
12401 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12403 hscroll_step_abs = XINT (Vhscroll_step);
12404 if (hscroll_step_abs < 0)
12405 hscroll_step_abs = 0;
12407 else
12408 hscroll_step_abs = 0;
12410 while (WINDOWP (window))
12412 struct window *w = XWINDOW (window);
12414 if (WINDOWP (w->contents))
12415 hscrolled_p |= hscroll_window_tree (w->contents);
12416 else if (w->cursor.vpos >= 0)
12418 int h_margin;
12419 int text_area_width;
12420 struct glyph_row *current_cursor_row
12421 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12422 struct glyph_row *desired_cursor_row
12423 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12424 struct glyph_row *cursor_row
12425 = (desired_cursor_row->enabled_p
12426 ? desired_cursor_row
12427 : current_cursor_row);
12428 int row_r2l_p = cursor_row->reversed_p;
12430 text_area_width = window_box_width (w, TEXT_AREA);
12432 /* Scroll when cursor is inside this scroll margin. */
12433 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12435 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12436 /* For left-to-right rows, hscroll when cursor is either
12437 (i) inside the right hscroll margin, or (ii) if it is
12438 inside the left margin and the window is already
12439 hscrolled. */
12440 && ((!row_r2l_p
12441 && ((w->hscroll
12442 && w->cursor.x <= h_margin)
12443 || (cursor_row->enabled_p
12444 && cursor_row->truncated_on_right_p
12445 && (w->cursor.x >= text_area_width - h_margin))))
12446 /* For right-to-left rows, the logic is similar,
12447 except that rules for scrolling to left and right
12448 are reversed. E.g., if cursor.x <= h_margin, we
12449 need to hscroll "to the right" unconditionally,
12450 and that will scroll the screen to the left so as
12451 to reveal the next portion of the row. */
12452 || (row_r2l_p
12453 && ((cursor_row->enabled_p
12454 /* FIXME: It is confusing to set the
12455 truncated_on_right_p flag when R2L rows
12456 are actually truncated on the left. */
12457 && cursor_row->truncated_on_right_p
12458 && w->cursor.x <= h_margin)
12459 || (w->hscroll
12460 && (w->cursor.x >= text_area_width - h_margin))))))
12462 struct it it;
12463 ptrdiff_t hscroll;
12464 struct buffer *saved_current_buffer;
12465 ptrdiff_t pt;
12466 int wanted_x;
12468 /* Find point in a display of infinite width. */
12469 saved_current_buffer = current_buffer;
12470 current_buffer = XBUFFER (w->contents);
12472 if (w == XWINDOW (selected_window))
12473 pt = PT;
12474 else
12475 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12477 /* Move iterator to pt starting at cursor_row->start in
12478 a line with infinite width. */
12479 init_to_row_start (&it, w, cursor_row);
12480 it.last_visible_x = INFINITY;
12481 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12482 current_buffer = saved_current_buffer;
12484 /* Position cursor in window. */
12485 if (!hscroll_relative_p && hscroll_step_abs == 0)
12486 hscroll = max (0, (it.current_x
12487 - (ITERATOR_AT_END_OF_LINE_P (&it)
12488 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12489 : (text_area_width / 2))))
12490 / FRAME_COLUMN_WIDTH (it.f);
12491 else if ((!row_r2l_p
12492 && w->cursor.x >= text_area_width - h_margin)
12493 || (row_r2l_p && w->cursor.x <= h_margin))
12495 if (hscroll_relative_p)
12496 wanted_x = text_area_width * (1 - hscroll_step_rel)
12497 - h_margin;
12498 else
12499 wanted_x = text_area_width
12500 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12501 - h_margin;
12502 hscroll
12503 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12505 else
12507 if (hscroll_relative_p)
12508 wanted_x = text_area_width * hscroll_step_rel
12509 + h_margin;
12510 else
12511 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12512 + h_margin;
12513 hscroll
12514 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12516 hscroll = max (hscroll, w->min_hscroll);
12518 /* Don't prevent redisplay optimizations if hscroll
12519 hasn't changed, as it will unnecessarily slow down
12520 redisplay. */
12521 if (w->hscroll != hscroll)
12523 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12524 w->hscroll = hscroll;
12525 hscrolled_p = 1;
12530 window = w->next;
12533 /* Value is non-zero if hscroll of any leaf window has been changed. */
12534 return hscrolled_p;
12538 /* Set hscroll so that cursor is visible and not inside horizontal
12539 scroll margins for all windows in the tree rooted at WINDOW. See
12540 also hscroll_window_tree above. Value is non-zero if any window's
12541 hscroll has been changed. If it has, desired matrices on the frame
12542 of WINDOW are cleared. */
12544 static int
12545 hscroll_windows (Lisp_Object window)
12547 int hscrolled_p = hscroll_window_tree (window);
12548 if (hscrolled_p)
12549 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12550 return hscrolled_p;
12555 /************************************************************************
12556 Redisplay
12557 ************************************************************************/
12559 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12560 to a non-zero value. This is sometimes handy to have in a debugger
12561 session. */
12563 #ifdef GLYPH_DEBUG
12565 /* First and last unchanged row for try_window_id. */
12567 static int debug_first_unchanged_at_end_vpos;
12568 static int debug_last_unchanged_at_beg_vpos;
12570 /* Delta vpos and y. */
12572 static int debug_dvpos, debug_dy;
12574 /* Delta in characters and bytes for try_window_id. */
12576 static ptrdiff_t debug_delta, debug_delta_bytes;
12578 /* Values of window_end_pos and window_end_vpos at the end of
12579 try_window_id. */
12581 static ptrdiff_t debug_end_vpos;
12583 /* Append a string to W->desired_matrix->method. FMT is a printf
12584 format string. If trace_redisplay_p is non-zero also printf the
12585 resulting string to stderr. */
12587 static void debug_method_add (struct window *, char const *, ...)
12588 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12590 static void
12591 debug_method_add (struct window *w, char const *fmt, ...)
12593 void *ptr = w;
12594 char *method = w->desired_matrix->method;
12595 int len = strlen (method);
12596 int size = sizeof w->desired_matrix->method;
12597 int remaining = size - len - 1;
12598 va_list ap;
12600 if (len && remaining)
12602 method[len] = '|';
12603 --remaining, ++len;
12606 va_start (ap, fmt);
12607 vsnprintf (method + len, remaining + 1, fmt, ap);
12608 va_end (ap);
12610 if (trace_redisplay_p)
12611 fprintf (stderr, "%p (%s): %s\n",
12612 ptr,
12613 ((BUFFERP (w->contents)
12614 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12615 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12616 : "no buffer"),
12617 method + len);
12620 #endif /* GLYPH_DEBUG */
12623 /* Value is non-zero if all changes in window W, which displays
12624 current_buffer, are in the text between START and END. START is a
12625 buffer position, END is given as a distance from Z. Used in
12626 redisplay_internal for display optimization. */
12628 static int
12629 text_outside_line_unchanged_p (struct window *w,
12630 ptrdiff_t start, ptrdiff_t end)
12632 int unchanged_p = 1;
12634 /* If text or overlays have changed, see where. */
12635 if (window_outdated (w))
12637 /* Gap in the line? */
12638 if (GPT < start || Z - GPT < end)
12639 unchanged_p = 0;
12641 /* Changes start in front of the line, or end after it? */
12642 if (unchanged_p
12643 && (BEG_UNCHANGED < start - 1
12644 || END_UNCHANGED < end))
12645 unchanged_p = 0;
12647 /* If selective display, can't optimize if changes start at the
12648 beginning of the line. */
12649 if (unchanged_p
12650 && INTEGERP (BVAR (current_buffer, selective_display))
12651 && XINT (BVAR (current_buffer, selective_display)) > 0
12652 && (BEG_UNCHANGED < start || GPT <= start))
12653 unchanged_p = 0;
12655 /* If there are overlays at the start or end of the line, these
12656 may have overlay strings with newlines in them. A change at
12657 START, for instance, may actually concern the display of such
12658 overlay strings as well, and they are displayed on different
12659 lines. So, quickly rule out this case. (For the future, it
12660 might be desirable to implement something more telling than
12661 just BEG/END_UNCHANGED.) */
12662 if (unchanged_p)
12664 if (BEG + BEG_UNCHANGED == start
12665 && overlay_touches_p (start))
12666 unchanged_p = 0;
12667 if (END_UNCHANGED == end
12668 && overlay_touches_p (Z - end))
12669 unchanged_p = 0;
12672 /* Under bidi reordering, adding or deleting a character in the
12673 beginning of a paragraph, before the first strong directional
12674 character, can change the base direction of the paragraph (unless
12675 the buffer specifies a fixed paragraph direction), which will
12676 require to redisplay the whole paragraph. It might be worthwhile
12677 to find the paragraph limits and widen the range of redisplayed
12678 lines to that, but for now just give up this optimization. */
12679 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12680 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12681 unchanged_p = 0;
12684 return unchanged_p;
12688 /* Do a frame update, taking possible shortcuts into account. This is
12689 the main external entry point for redisplay.
12691 If the last redisplay displayed an echo area message and that message
12692 is no longer requested, we clear the echo area or bring back the
12693 mini-buffer if that is in use. */
12695 void
12696 redisplay (void)
12698 redisplay_internal ();
12702 static Lisp_Object
12703 overlay_arrow_string_or_property (Lisp_Object var)
12705 Lisp_Object val;
12707 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12708 return val;
12710 return Voverlay_arrow_string;
12713 /* Return 1 if there are any overlay-arrows in current_buffer. */
12714 static int
12715 overlay_arrow_in_current_buffer_p (void)
12717 Lisp_Object vlist;
12719 for (vlist = Voverlay_arrow_variable_list;
12720 CONSP (vlist);
12721 vlist = XCDR (vlist))
12723 Lisp_Object var = XCAR (vlist);
12724 Lisp_Object val;
12726 if (!SYMBOLP (var))
12727 continue;
12728 val = find_symbol_value (var);
12729 if (MARKERP (val)
12730 && current_buffer == XMARKER (val)->buffer)
12731 return 1;
12733 return 0;
12737 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12738 has changed. */
12740 static int
12741 overlay_arrows_changed_p (void)
12743 Lisp_Object vlist;
12745 for (vlist = Voverlay_arrow_variable_list;
12746 CONSP (vlist);
12747 vlist = XCDR (vlist))
12749 Lisp_Object var = XCAR (vlist);
12750 Lisp_Object val, pstr;
12752 if (!SYMBOLP (var))
12753 continue;
12754 val = find_symbol_value (var);
12755 if (!MARKERP (val))
12756 continue;
12757 if (! EQ (COERCE_MARKER (val),
12758 Fget (var, Qlast_arrow_position))
12759 || ! (pstr = overlay_arrow_string_or_property (var),
12760 EQ (pstr, Fget (var, Qlast_arrow_string))))
12761 return 1;
12763 return 0;
12766 /* Mark overlay arrows to be updated on next redisplay. */
12768 static void
12769 update_overlay_arrows (int up_to_date)
12771 Lisp_Object vlist;
12773 for (vlist = Voverlay_arrow_variable_list;
12774 CONSP (vlist);
12775 vlist = XCDR (vlist))
12777 Lisp_Object var = XCAR (vlist);
12779 if (!SYMBOLP (var))
12780 continue;
12782 if (up_to_date > 0)
12784 Lisp_Object val = find_symbol_value (var);
12785 Fput (var, Qlast_arrow_position,
12786 COERCE_MARKER (val));
12787 Fput (var, Qlast_arrow_string,
12788 overlay_arrow_string_or_property (var));
12790 else if (up_to_date < 0
12791 || !NILP (Fget (var, Qlast_arrow_position)))
12793 Fput (var, Qlast_arrow_position, Qt);
12794 Fput (var, Qlast_arrow_string, Qt);
12800 /* Return overlay arrow string to display at row.
12801 Return integer (bitmap number) for arrow bitmap in left fringe.
12802 Return nil if no overlay arrow. */
12804 static Lisp_Object
12805 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12807 Lisp_Object vlist;
12809 for (vlist = Voverlay_arrow_variable_list;
12810 CONSP (vlist);
12811 vlist = XCDR (vlist))
12813 Lisp_Object var = XCAR (vlist);
12814 Lisp_Object val;
12816 if (!SYMBOLP (var))
12817 continue;
12819 val = find_symbol_value (var);
12821 if (MARKERP (val)
12822 && current_buffer == XMARKER (val)->buffer
12823 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12825 if (FRAME_WINDOW_P (it->f)
12826 /* FIXME: if ROW->reversed_p is set, this should test
12827 the right fringe, not the left one. */
12828 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12830 #ifdef HAVE_WINDOW_SYSTEM
12831 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12833 int fringe_bitmap;
12834 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12835 return make_number (fringe_bitmap);
12837 #endif
12838 return make_number (-1); /* Use default arrow bitmap. */
12840 return overlay_arrow_string_or_property (var);
12844 return Qnil;
12847 /* Return 1 if point moved out of or into a composition. Otherwise
12848 return 0. PREV_BUF and PREV_PT are the last point buffer and
12849 position. BUF and PT are the current point buffer and position. */
12851 static int
12852 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12853 struct buffer *buf, ptrdiff_t pt)
12855 ptrdiff_t start, end;
12856 Lisp_Object prop;
12857 Lisp_Object buffer;
12859 XSETBUFFER (buffer, buf);
12860 /* Check a composition at the last point if point moved within the
12861 same buffer. */
12862 if (prev_buf == buf)
12864 if (prev_pt == pt)
12865 /* Point didn't move. */
12866 return 0;
12868 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12869 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12870 && composition_valid_p (start, end, prop)
12871 && start < prev_pt && end > prev_pt)
12872 /* The last point was within the composition. Return 1 iff
12873 point moved out of the composition. */
12874 return (pt <= start || pt >= end);
12877 /* Check a composition at the current point. */
12878 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12879 && find_composition (pt, -1, &start, &end, &prop, buffer)
12880 && composition_valid_p (start, end, prop)
12881 && start < pt && end > pt);
12884 /* Reconsider the clip changes of buffer which is displayed in W. */
12886 static void
12887 reconsider_clip_changes (struct window *w)
12889 struct buffer *b = XBUFFER (w->contents);
12891 if (b->clip_changed
12892 && w->window_end_valid
12893 && w->current_matrix->buffer == b
12894 && w->current_matrix->zv == BUF_ZV (b)
12895 && w->current_matrix->begv == BUF_BEGV (b))
12896 b->clip_changed = 0;
12898 /* If display wasn't paused, and W is not a tool bar window, see if
12899 point has been moved into or out of a composition. In that case,
12900 we set b->clip_changed to 1 to force updating the screen. If
12901 b->clip_changed has already been set to 1, we can skip this
12902 check. */
12903 if (!b->clip_changed && w->window_end_valid)
12905 ptrdiff_t pt = (w == XWINDOW (selected_window)
12906 ? PT : marker_position (w->pointm));
12908 if ((w->current_matrix->buffer != b || pt != w->last_point)
12909 && check_point_in_composition (w->current_matrix->buffer,
12910 w->last_point, b, pt))
12911 b->clip_changed = 1;
12915 #define STOP_POLLING \
12916 do { if (! polling_stopped_here) stop_polling (); \
12917 polling_stopped_here = 1; } while (0)
12919 #define RESUME_POLLING \
12920 do { if (polling_stopped_here) start_polling (); \
12921 polling_stopped_here = 0; } while (0)
12924 /* Perhaps in the future avoid recentering windows if it
12925 is not necessary; currently that causes some problems. */
12927 static void
12928 redisplay_internal (void)
12930 struct window *w = XWINDOW (selected_window);
12931 struct window *sw;
12932 struct frame *fr;
12933 int pending;
12934 bool must_finish = 0, match_p;
12935 struct text_pos tlbufpos, tlendpos;
12936 int number_of_visible_frames;
12937 ptrdiff_t count;
12938 struct frame *sf;
12939 int polling_stopped_here = 0;
12940 Lisp_Object tail, frame;
12942 /* Non-zero means redisplay has to consider all windows on all
12943 frames. Zero means, only selected_window is considered. */
12944 int consider_all_windows_p;
12946 /* Non-zero means redisplay has to redisplay the miniwindow. */
12947 int update_miniwindow_p = 0;
12949 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12951 /* No redisplay if running in batch mode or frame is not yet fully
12952 initialized, or redisplay is explicitly turned off by setting
12953 Vinhibit_redisplay. */
12954 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12955 || !NILP (Vinhibit_redisplay))
12956 return;
12958 /* Don't examine these until after testing Vinhibit_redisplay.
12959 When Emacs is shutting down, perhaps because its connection to
12960 X has dropped, we should not look at them at all. */
12961 fr = XFRAME (w->frame);
12962 sf = SELECTED_FRAME ();
12964 if (!fr->glyphs_initialized_p)
12965 return;
12967 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12968 if (popup_activated ())
12969 return;
12970 #endif
12972 /* I don't think this happens but let's be paranoid. */
12973 if (redisplaying_p)
12974 return;
12976 /* Record a function that clears redisplaying_p
12977 when we leave this function. */
12978 count = SPECPDL_INDEX ();
12979 record_unwind_protect_void (unwind_redisplay);
12980 redisplaying_p = 1;
12981 specbind (Qinhibit_free_realized_faces, Qnil);
12983 /* Record this function, so it appears on the profiler's backtraces. */
12984 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12986 FOR_EACH_FRAME (tail, frame)
12987 XFRAME (frame)->already_hscrolled_p = 0;
12989 retry:
12990 /* Remember the currently selected window. */
12991 sw = w;
12993 pending = 0;
12994 last_escape_glyph_frame = NULL;
12995 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12996 last_glyphless_glyph_frame = NULL;
12997 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12999 /* If face_change_count is non-zero, init_iterator will free all
13000 realized faces, which includes the faces referenced from current
13001 matrices. So, we can't reuse current matrices in this case. */
13002 if (face_change_count)
13003 ++windows_or_buffers_changed;
13005 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13006 && FRAME_TTY (sf)->previous_frame != sf)
13008 /* Since frames on a single ASCII terminal share the same
13009 display area, displaying a different frame means redisplay
13010 the whole thing. */
13011 windows_or_buffers_changed++;
13012 SET_FRAME_GARBAGED (sf);
13013 #ifndef DOS_NT
13014 set_tty_color_mode (FRAME_TTY (sf), sf);
13015 #endif
13016 FRAME_TTY (sf)->previous_frame = sf;
13019 /* Set the visible flags for all frames. Do this before checking for
13020 resized or garbaged frames; they want to know if their frames are
13021 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13022 number_of_visible_frames = 0;
13024 FOR_EACH_FRAME (tail, frame)
13026 struct frame *f = XFRAME (frame);
13028 if (FRAME_VISIBLE_P (f))
13030 ++number_of_visible_frames;
13031 /* Adjust matrices for visible frames only. */
13032 if (f->fonts_changed)
13034 adjust_frame_glyphs (f);
13035 f->fonts_changed = 0;
13037 /* If cursor type has been changed on the frame
13038 other than selected, consider all frames. */
13039 if (f != sf && f->cursor_type_changed)
13040 update_mode_lines++;
13042 clear_desired_matrices (f);
13045 /* Notice any pending interrupt request to change frame size. */
13046 do_pending_window_change (1);
13048 /* do_pending_window_change could change the selected_window due to
13049 frame resizing which makes the selected window too small. */
13050 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13051 sw = w;
13053 /* Clear frames marked as garbaged. */
13054 clear_garbaged_frames ();
13056 /* Build menubar and tool-bar items. */
13057 if (NILP (Vmemory_full))
13058 prepare_menu_bars ();
13060 if (windows_or_buffers_changed)
13061 update_mode_lines++;
13063 reconsider_clip_changes (w);
13065 /* In most cases selected window displays current buffer. */
13066 match_p = XBUFFER (w->contents) == current_buffer;
13067 if (match_p)
13069 ptrdiff_t count1;
13071 /* Detect case that we need to write or remove a star in the mode line. */
13072 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13074 w->update_mode_line = 1;
13075 if (buffer_shared_and_changed ())
13076 update_mode_lines++;
13079 /* Avoid invocation of point motion hooks by `current_column' below. */
13080 count1 = SPECPDL_INDEX ();
13081 specbind (Qinhibit_point_motion_hooks, Qt);
13083 if (mode_line_update_needed (w))
13084 w->update_mode_line = 1;
13086 unbind_to (count1, Qnil);
13089 consider_all_windows_p = (update_mode_lines
13090 || buffer_shared_and_changed ());
13092 /* If specs for an arrow have changed, do thorough redisplay
13093 to ensure we remove any arrow that should no longer exist. */
13094 if (overlay_arrows_changed_p ())
13095 consider_all_windows_p = windows_or_buffers_changed = 1;
13097 /* Normally the message* functions will have already displayed and
13098 updated the echo area, but the frame may have been trashed, or
13099 the update may have been preempted, so display the echo area
13100 again here. Checking message_cleared_p captures the case that
13101 the echo area should be cleared. */
13102 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13103 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13104 || (message_cleared_p
13105 && minibuf_level == 0
13106 /* If the mini-window is currently selected, this means the
13107 echo-area doesn't show through. */
13108 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13110 int window_height_changed_p = echo_area_display (0);
13112 if (message_cleared_p)
13113 update_miniwindow_p = 1;
13115 must_finish = 1;
13117 /* If we don't display the current message, don't clear the
13118 message_cleared_p flag, because, if we did, we wouldn't clear
13119 the echo area in the next redisplay which doesn't preserve
13120 the echo area. */
13121 if (!display_last_displayed_message_p)
13122 message_cleared_p = 0;
13124 if (window_height_changed_p)
13126 consider_all_windows_p = 1;
13127 ++update_mode_lines;
13128 ++windows_or_buffers_changed;
13130 /* If window configuration was changed, frames may have been
13131 marked garbaged. Clear them or we will experience
13132 surprises wrt scrolling. */
13133 clear_garbaged_frames ();
13136 else if (EQ (selected_window, minibuf_window)
13137 && (current_buffer->clip_changed || window_outdated (w))
13138 && resize_mini_window (w, 0))
13140 /* Resized active mini-window to fit the size of what it is
13141 showing if its contents might have changed. */
13142 must_finish = 1;
13143 /* FIXME: this causes all frames to be updated, which seems unnecessary
13144 since only the current frame needs to be considered. This function
13145 needs to be rewritten with two variables, consider_all_windows and
13146 consider_all_frames. */
13147 consider_all_windows_p = 1;
13148 ++windows_or_buffers_changed;
13149 ++update_mode_lines;
13151 /* If window configuration was changed, frames may have been
13152 marked garbaged. Clear them or we will experience
13153 surprises wrt scrolling. */
13154 clear_garbaged_frames ();
13157 /* If showing the region, and mark has changed, we must redisplay
13158 the whole window. The assignment to this_line_start_pos prevents
13159 the optimization directly below this if-statement. */
13160 if (((!NILP (Vtransient_mark_mode)
13161 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13162 != (w->region_showing > 0))
13163 || (w->region_showing
13164 && w->region_showing
13165 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13166 CHARPOS (this_line_start_pos) = 0;
13168 /* Optimize the case that only the line containing the cursor in the
13169 selected window has changed. Variables starting with this_ are
13170 set in display_line and record information about the line
13171 containing the cursor. */
13172 tlbufpos = this_line_start_pos;
13173 tlendpos = this_line_end_pos;
13174 if (!consider_all_windows_p
13175 && CHARPOS (tlbufpos) > 0
13176 && !w->update_mode_line
13177 && !current_buffer->clip_changed
13178 && !current_buffer->prevent_redisplay_optimizations_p
13179 && FRAME_VISIBLE_P (XFRAME (w->frame))
13180 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13181 && !XFRAME (w->frame)->cursor_type_changed
13182 /* Make sure recorded data applies to current buffer, etc. */
13183 && this_line_buffer == current_buffer
13184 && match_p
13185 && !w->force_start
13186 && !w->optional_new_start
13187 /* Point must be on the line that we have info recorded about. */
13188 && PT >= CHARPOS (tlbufpos)
13189 && PT <= Z - CHARPOS (tlendpos)
13190 /* All text outside that line, including its final newline,
13191 must be unchanged. */
13192 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13193 CHARPOS (tlendpos)))
13195 if (CHARPOS (tlbufpos) > BEGV
13196 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13197 && (CHARPOS (tlbufpos) == ZV
13198 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13199 /* Former continuation line has disappeared by becoming empty. */
13200 goto cancel;
13201 else if (window_outdated (w) || MINI_WINDOW_P (w))
13203 /* We have to handle the case of continuation around a
13204 wide-column character (see the comment in indent.c around
13205 line 1340).
13207 For instance, in the following case:
13209 -------- Insert --------
13210 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13211 J_I_ ==> J_I_ `^^' are cursors.
13212 ^^ ^^
13213 -------- --------
13215 As we have to redraw the line above, we cannot use this
13216 optimization. */
13218 struct it it;
13219 int line_height_before = this_line_pixel_height;
13221 /* Note that start_display will handle the case that the
13222 line starting at tlbufpos is a continuation line. */
13223 start_display (&it, w, tlbufpos);
13225 /* Implementation note: It this still necessary? */
13226 if (it.current_x != this_line_start_x)
13227 goto cancel;
13229 TRACE ((stderr, "trying display optimization 1\n"));
13230 w->cursor.vpos = -1;
13231 overlay_arrow_seen = 0;
13232 it.vpos = this_line_vpos;
13233 it.current_y = this_line_y;
13234 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13235 display_line (&it);
13237 /* If line contains point, is not continued,
13238 and ends at same distance from eob as before, we win. */
13239 if (w->cursor.vpos >= 0
13240 /* Line is not continued, otherwise this_line_start_pos
13241 would have been set to 0 in display_line. */
13242 && CHARPOS (this_line_start_pos)
13243 /* Line ends as before. */
13244 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13245 /* Line has same height as before. Otherwise other lines
13246 would have to be shifted up or down. */
13247 && this_line_pixel_height == line_height_before)
13249 /* If this is not the window's last line, we must adjust
13250 the charstarts of the lines below. */
13251 if (it.current_y < it.last_visible_y)
13253 struct glyph_row *row
13254 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13255 ptrdiff_t delta, delta_bytes;
13257 /* We used to distinguish between two cases here,
13258 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13259 when the line ends in a newline or the end of the
13260 buffer's accessible portion. But both cases did
13261 the same, so they were collapsed. */
13262 delta = (Z
13263 - CHARPOS (tlendpos)
13264 - MATRIX_ROW_START_CHARPOS (row));
13265 delta_bytes = (Z_BYTE
13266 - BYTEPOS (tlendpos)
13267 - MATRIX_ROW_START_BYTEPOS (row));
13269 increment_matrix_positions (w->current_matrix,
13270 this_line_vpos + 1,
13271 w->current_matrix->nrows,
13272 delta, delta_bytes);
13275 /* If this row displays text now but previously didn't,
13276 or vice versa, w->window_end_vpos may have to be
13277 adjusted. */
13278 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13280 if (w->window_end_vpos < this_line_vpos)
13281 w->window_end_vpos = this_line_vpos;
13283 else if (w->window_end_vpos == this_line_vpos
13284 && this_line_vpos > 0)
13285 w->window_end_vpos = this_line_vpos - 1;
13286 w->window_end_valid = 0;
13288 /* Update hint: No need to try to scroll in update_window. */
13289 w->desired_matrix->no_scrolling_p = 1;
13291 #ifdef GLYPH_DEBUG
13292 *w->desired_matrix->method = 0;
13293 debug_method_add (w, "optimization 1");
13294 #endif
13295 #ifdef HAVE_WINDOW_SYSTEM
13296 update_window_fringes (w, 0);
13297 #endif
13298 goto update;
13300 else
13301 goto cancel;
13303 else if (/* Cursor position hasn't changed. */
13304 PT == w->last_point
13305 /* Make sure the cursor was last displayed
13306 in this window. Otherwise we have to reposition it. */
13307 && 0 <= w->cursor.vpos
13308 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13310 if (!must_finish)
13312 do_pending_window_change (1);
13313 /* If selected_window changed, redisplay again. */
13314 if (WINDOWP (selected_window)
13315 && (w = XWINDOW (selected_window)) != sw)
13316 goto retry;
13318 /* We used to always goto end_of_redisplay here, but this
13319 isn't enough if we have a blinking cursor. */
13320 if (w->cursor_off_p == w->last_cursor_off_p)
13321 goto end_of_redisplay;
13323 goto update;
13325 /* If highlighting the region, or if the cursor is in the echo area,
13326 then we can't just move the cursor. */
13327 else if (! (!NILP (Vtransient_mark_mode)
13328 && !NILP (BVAR (current_buffer, mark_active)))
13329 && (EQ (selected_window,
13330 BVAR (current_buffer, last_selected_window))
13331 || highlight_nonselected_windows)
13332 && !w->region_showing
13333 && NILP (Vshow_trailing_whitespace)
13334 && !cursor_in_echo_area)
13336 struct it it;
13337 struct glyph_row *row;
13339 /* Skip from tlbufpos to PT and see where it is. Note that
13340 PT may be in invisible text. If so, we will end at the
13341 next visible position. */
13342 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13343 NULL, DEFAULT_FACE_ID);
13344 it.current_x = this_line_start_x;
13345 it.current_y = this_line_y;
13346 it.vpos = this_line_vpos;
13348 /* The call to move_it_to stops in front of PT, but
13349 moves over before-strings. */
13350 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13352 if (it.vpos == this_line_vpos
13353 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13354 row->enabled_p))
13356 eassert (this_line_vpos == it.vpos);
13357 eassert (this_line_y == it.current_y);
13358 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13359 #ifdef GLYPH_DEBUG
13360 *w->desired_matrix->method = 0;
13361 debug_method_add (w, "optimization 3");
13362 #endif
13363 goto update;
13365 else
13366 goto cancel;
13369 cancel:
13370 /* Text changed drastically or point moved off of line. */
13371 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13374 CHARPOS (this_line_start_pos) = 0;
13375 consider_all_windows_p |= buffer_shared_and_changed ();
13376 ++clear_face_cache_count;
13377 #ifdef HAVE_WINDOW_SYSTEM
13378 ++clear_image_cache_count;
13379 #endif
13381 /* Build desired matrices, and update the display. If
13382 consider_all_windows_p is non-zero, do it for all windows on all
13383 frames. Otherwise do it for selected_window, only. */
13385 if (consider_all_windows_p)
13387 FOR_EACH_FRAME (tail, frame)
13388 XFRAME (frame)->updated_p = 0;
13390 FOR_EACH_FRAME (tail, frame)
13392 struct frame *f = XFRAME (frame);
13394 /* We don't have to do anything for unselected terminal
13395 frames. */
13396 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13397 && !EQ (FRAME_TTY (f)->top_frame, frame))
13398 continue;
13400 retry_frame:
13402 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13404 /* Mark all the scroll bars to be removed; we'll redeem
13405 the ones we want when we redisplay their windows. */
13406 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13407 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13409 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13410 redisplay_windows (FRAME_ROOT_WINDOW (f));
13412 /* The X error handler may have deleted that frame. */
13413 if (!FRAME_LIVE_P (f))
13414 continue;
13416 /* Any scroll bars which redisplay_windows should have
13417 nuked should now go away. */
13418 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13419 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13421 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13423 /* If fonts changed on visible frame, display again. */
13424 if (f->fonts_changed)
13426 adjust_frame_glyphs (f);
13427 f->fonts_changed = 0;
13428 goto retry_frame;
13431 /* See if we have to hscroll. */
13432 if (!f->already_hscrolled_p)
13434 f->already_hscrolled_p = 1;
13435 if (hscroll_windows (f->root_window))
13436 goto retry_frame;
13439 /* Prevent various kinds of signals during display
13440 update. stdio is not robust about handling
13441 signals, which can cause an apparent I/O
13442 error. */
13443 if (interrupt_input)
13444 unrequest_sigio ();
13445 STOP_POLLING;
13447 /* Update the display. */
13448 set_window_update_flags (XWINDOW (f->root_window), 1);
13449 pending |= update_frame (f, 0, 0);
13450 f->cursor_type_changed = 0;
13451 f->updated_p = 1;
13456 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13458 if (!pending)
13460 /* Do the mark_window_display_accurate after all windows have
13461 been redisplayed because this call resets flags in buffers
13462 which are needed for proper redisplay. */
13463 FOR_EACH_FRAME (tail, frame)
13465 struct frame *f = XFRAME (frame);
13466 if (f->updated_p)
13468 mark_window_display_accurate (f->root_window, 1);
13469 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13470 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13475 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13477 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13478 struct frame *mini_frame;
13480 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13481 /* Use list_of_error, not Qerror, so that
13482 we catch only errors and don't run the debugger. */
13483 internal_condition_case_1 (redisplay_window_1, selected_window,
13484 list_of_error,
13485 redisplay_window_error);
13486 if (update_miniwindow_p)
13487 internal_condition_case_1 (redisplay_window_1, mini_window,
13488 list_of_error,
13489 redisplay_window_error);
13491 /* Compare desired and current matrices, perform output. */
13493 update:
13494 /* If fonts changed, display again. */
13495 if (sf->fonts_changed)
13496 goto retry;
13498 /* Prevent various kinds of signals during display update.
13499 stdio is not robust about handling signals,
13500 which can cause an apparent I/O error. */
13501 if (interrupt_input)
13502 unrequest_sigio ();
13503 STOP_POLLING;
13505 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13507 if (hscroll_windows (selected_window))
13508 goto retry;
13510 XWINDOW (selected_window)->must_be_updated_p = 1;
13511 pending = update_frame (sf, 0, 0);
13512 sf->cursor_type_changed = 0;
13515 /* We may have called echo_area_display at the top of this
13516 function. If the echo area is on another frame, that may
13517 have put text on a frame other than the selected one, so the
13518 above call to update_frame would not have caught it. Catch
13519 it here. */
13520 mini_window = FRAME_MINIBUF_WINDOW (sf);
13521 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13523 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13525 XWINDOW (mini_window)->must_be_updated_p = 1;
13526 pending |= update_frame (mini_frame, 0, 0);
13527 mini_frame->cursor_type_changed = 0;
13528 if (!pending && hscroll_windows (mini_window))
13529 goto retry;
13533 /* If display was paused because of pending input, make sure we do a
13534 thorough update the next time. */
13535 if (pending)
13537 /* Prevent the optimization at the beginning of
13538 redisplay_internal that tries a single-line update of the
13539 line containing the cursor in the selected window. */
13540 CHARPOS (this_line_start_pos) = 0;
13542 /* Let the overlay arrow be updated the next time. */
13543 update_overlay_arrows (0);
13545 /* If we pause after scrolling, some rows in the current
13546 matrices of some windows are not valid. */
13547 if (!WINDOW_FULL_WIDTH_P (w)
13548 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13549 update_mode_lines = 1;
13551 else
13553 if (!consider_all_windows_p)
13555 /* This has already been done above if
13556 consider_all_windows_p is set. */
13557 mark_window_display_accurate_1 (w, 1);
13559 /* Say overlay arrows are up to date. */
13560 update_overlay_arrows (1);
13562 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13563 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13566 update_mode_lines = 0;
13567 windows_or_buffers_changed = 0;
13570 /* Start SIGIO interrupts coming again. Having them off during the
13571 code above makes it less likely one will discard output, but not
13572 impossible, since there might be stuff in the system buffer here.
13573 But it is much hairier to try to do anything about that. */
13574 if (interrupt_input)
13575 request_sigio ();
13576 RESUME_POLLING;
13578 /* If a frame has become visible which was not before, redisplay
13579 again, so that we display it. Expose events for such a frame
13580 (which it gets when becoming visible) don't call the parts of
13581 redisplay constructing glyphs, so simply exposing a frame won't
13582 display anything in this case. So, we have to display these
13583 frames here explicitly. */
13584 if (!pending)
13586 int new_count = 0;
13588 FOR_EACH_FRAME (tail, frame)
13590 int this_is_visible = 0;
13592 if (XFRAME (frame)->visible)
13593 this_is_visible = 1;
13595 if (this_is_visible)
13596 new_count++;
13599 if (new_count != number_of_visible_frames)
13600 windows_or_buffers_changed++;
13603 /* Change frame size now if a change is pending. */
13604 do_pending_window_change (1);
13606 /* If we just did a pending size change, or have additional
13607 visible frames, or selected_window changed, redisplay again. */
13608 if ((windows_or_buffers_changed && !pending)
13609 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13610 goto retry;
13612 /* Clear the face and image caches.
13614 We used to do this only if consider_all_windows_p. But the cache
13615 needs to be cleared if a timer creates images in the current
13616 buffer (e.g. the test case in Bug#6230). */
13618 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13620 clear_face_cache (0);
13621 clear_face_cache_count = 0;
13624 #ifdef HAVE_WINDOW_SYSTEM
13625 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13627 clear_image_caches (Qnil);
13628 clear_image_cache_count = 0;
13630 #endif /* HAVE_WINDOW_SYSTEM */
13632 end_of_redisplay:
13633 unbind_to (count, Qnil);
13634 RESUME_POLLING;
13638 /* Redisplay, but leave alone any recent echo area message unless
13639 another message has been requested in its place.
13641 This is useful in situations where you need to redisplay but no
13642 user action has occurred, making it inappropriate for the message
13643 area to be cleared. See tracking_off and
13644 wait_reading_process_output for examples of these situations.
13646 FROM_WHERE is an integer saying from where this function was
13647 called. This is useful for debugging. */
13649 void
13650 redisplay_preserve_echo_area (int from_where)
13652 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13654 if (!NILP (echo_area_buffer[1]))
13656 /* We have a previously displayed message, but no current
13657 message. Redisplay the previous message. */
13658 display_last_displayed_message_p = 1;
13659 redisplay_internal ();
13660 display_last_displayed_message_p = 0;
13662 else
13663 redisplay_internal ();
13665 flush_frame (SELECTED_FRAME ());
13669 /* Function registered with record_unwind_protect in redisplay_internal. */
13671 static void
13672 unwind_redisplay (void)
13674 redisplaying_p = 0;
13678 /* Mark the display of leaf window W as accurate or inaccurate.
13679 If ACCURATE_P is non-zero mark display of W as accurate. If
13680 ACCURATE_P is zero, arrange for W to be redisplayed the next
13681 time redisplay_internal is called. */
13683 static void
13684 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13686 struct buffer *b = XBUFFER (w->contents);
13688 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13689 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13690 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13692 if (accurate_p)
13694 b->clip_changed = 0;
13695 b->prevent_redisplay_optimizations_p = 0;
13697 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13698 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13699 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13700 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13702 w->current_matrix->buffer = b;
13703 w->current_matrix->begv = BUF_BEGV (b);
13704 w->current_matrix->zv = BUF_ZV (b);
13706 w->last_cursor_vpos = w->cursor.vpos;
13707 w->last_cursor_off_p = w->cursor_off_p;
13709 if (w == XWINDOW (selected_window))
13710 w->last_point = BUF_PT (b);
13711 else
13712 w->last_point = marker_position (w->pointm);
13714 w->window_end_valid = 1;
13715 w->update_mode_line = 0;
13720 /* Mark the display of windows in the window tree rooted at WINDOW as
13721 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13722 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13723 be redisplayed the next time redisplay_internal is called. */
13725 void
13726 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13728 struct window *w;
13730 for (; !NILP (window); window = w->next)
13732 w = XWINDOW (window);
13733 if (WINDOWP (w->contents))
13734 mark_window_display_accurate (w->contents, accurate_p);
13735 else
13736 mark_window_display_accurate_1 (w, accurate_p);
13739 if (accurate_p)
13740 update_overlay_arrows (1);
13741 else
13742 /* Force a thorough redisplay the next time by setting
13743 last_arrow_position and last_arrow_string to t, which is
13744 unequal to any useful value of Voverlay_arrow_... */
13745 update_overlay_arrows (-1);
13749 /* Return value in display table DP (Lisp_Char_Table *) for character
13750 C. Since a display table doesn't have any parent, we don't have to
13751 follow parent. Do not call this function directly but use the
13752 macro DISP_CHAR_VECTOR. */
13754 Lisp_Object
13755 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13757 Lisp_Object val;
13759 if (ASCII_CHAR_P (c))
13761 val = dp->ascii;
13762 if (SUB_CHAR_TABLE_P (val))
13763 val = XSUB_CHAR_TABLE (val)->contents[c];
13765 else
13767 Lisp_Object table;
13769 XSETCHAR_TABLE (table, dp);
13770 val = char_table_ref (table, c);
13772 if (NILP (val))
13773 val = dp->defalt;
13774 return val;
13779 /***********************************************************************
13780 Window Redisplay
13781 ***********************************************************************/
13783 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13785 static void
13786 redisplay_windows (Lisp_Object window)
13788 while (!NILP (window))
13790 struct window *w = XWINDOW (window);
13792 if (WINDOWP (w->contents))
13793 redisplay_windows (w->contents);
13794 else if (BUFFERP (w->contents))
13796 displayed_buffer = XBUFFER (w->contents);
13797 /* Use list_of_error, not Qerror, so that
13798 we catch only errors and don't run the debugger. */
13799 internal_condition_case_1 (redisplay_window_0, window,
13800 list_of_error,
13801 redisplay_window_error);
13804 window = w->next;
13808 static Lisp_Object
13809 redisplay_window_error (Lisp_Object ignore)
13811 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13812 return Qnil;
13815 static Lisp_Object
13816 redisplay_window_0 (Lisp_Object window)
13818 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13819 redisplay_window (window, 0);
13820 return Qnil;
13823 static Lisp_Object
13824 redisplay_window_1 (Lisp_Object window)
13826 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13827 redisplay_window (window, 1);
13828 return Qnil;
13832 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13833 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13834 which positions recorded in ROW differ from current buffer
13835 positions.
13837 Return 0 if cursor is not on this row, 1 otherwise. */
13839 static int
13840 set_cursor_from_row (struct window *w, struct glyph_row *row,
13841 struct glyph_matrix *matrix,
13842 ptrdiff_t delta, ptrdiff_t delta_bytes,
13843 int dy, int dvpos)
13845 struct glyph *glyph = row->glyphs[TEXT_AREA];
13846 struct glyph *end = glyph + row->used[TEXT_AREA];
13847 struct glyph *cursor = NULL;
13848 /* The last known character position in row. */
13849 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13850 int x = row->x;
13851 ptrdiff_t pt_old = PT - delta;
13852 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13853 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13854 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13855 /* A glyph beyond the edge of TEXT_AREA which we should never
13856 touch. */
13857 struct glyph *glyphs_end = end;
13858 /* Non-zero means we've found a match for cursor position, but that
13859 glyph has the avoid_cursor_p flag set. */
13860 int match_with_avoid_cursor = 0;
13861 /* Non-zero means we've seen at least one glyph that came from a
13862 display string. */
13863 int string_seen = 0;
13864 /* Largest and smallest buffer positions seen so far during scan of
13865 glyph row. */
13866 ptrdiff_t bpos_max = pos_before;
13867 ptrdiff_t bpos_min = pos_after;
13868 /* Last buffer position covered by an overlay string with an integer
13869 `cursor' property. */
13870 ptrdiff_t bpos_covered = 0;
13871 /* Non-zero means the display string on which to display the cursor
13872 comes from a text property, not from an overlay. */
13873 int string_from_text_prop = 0;
13875 /* Don't even try doing anything if called for a mode-line or
13876 header-line row, since the rest of the code isn't prepared to
13877 deal with such calamities. */
13878 eassert (!row->mode_line_p);
13879 if (row->mode_line_p)
13880 return 0;
13882 /* Skip over glyphs not having an object at the start and the end of
13883 the row. These are special glyphs like truncation marks on
13884 terminal frames. */
13885 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13887 if (!row->reversed_p)
13889 while (glyph < end
13890 && INTEGERP (glyph->object)
13891 && glyph->charpos < 0)
13893 x += glyph->pixel_width;
13894 ++glyph;
13896 while (end > glyph
13897 && INTEGERP ((end - 1)->object)
13898 /* CHARPOS is zero for blanks and stretch glyphs
13899 inserted by extend_face_to_end_of_line. */
13900 && (end - 1)->charpos <= 0)
13901 --end;
13902 glyph_before = glyph - 1;
13903 glyph_after = end;
13905 else
13907 struct glyph *g;
13909 /* If the glyph row is reversed, we need to process it from back
13910 to front, so swap the edge pointers. */
13911 glyphs_end = end = glyph - 1;
13912 glyph += row->used[TEXT_AREA] - 1;
13914 while (glyph > end + 1
13915 && INTEGERP (glyph->object)
13916 && glyph->charpos < 0)
13918 --glyph;
13919 x -= glyph->pixel_width;
13921 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13922 --glyph;
13923 /* By default, in reversed rows we put the cursor on the
13924 rightmost (first in the reading order) glyph. */
13925 for (g = end + 1; g < glyph; g++)
13926 x += g->pixel_width;
13927 while (end < glyph
13928 && INTEGERP ((end + 1)->object)
13929 && (end + 1)->charpos <= 0)
13930 ++end;
13931 glyph_before = glyph + 1;
13932 glyph_after = end;
13935 else if (row->reversed_p)
13937 /* In R2L rows that don't display text, put the cursor on the
13938 rightmost glyph. Case in point: an empty last line that is
13939 part of an R2L paragraph. */
13940 cursor = end - 1;
13941 /* Avoid placing the cursor on the last glyph of the row, where
13942 on terminal frames we hold the vertical border between
13943 adjacent windows. */
13944 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13945 && !WINDOW_RIGHTMOST_P (w)
13946 && cursor == row->glyphs[LAST_AREA] - 1)
13947 cursor--;
13948 x = -1; /* will be computed below, at label compute_x */
13951 /* Step 1: Try to find the glyph whose character position
13952 corresponds to point. If that's not possible, find 2 glyphs
13953 whose character positions are the closest to point, one before
13954 point, the other after it. */
13955 if (!row->reversed_p)
13956 while (/* not marched to end of glyph row */
13957 glyph < end
13958 /* glyph was not inserted by redisplay for internal purposes */
13959 && !INTEGERP (glyph->object))
13961 if (BUFFERP (glyph->object))
13963 ptrdiff_t dpos = glyph->charpos - pt_old;
13965 if (glyph->charpos > bpos_max)
13966 bpos_max = glyph->charpos;
13967 if (glyph->charpos < bpos_min)
13968 bpos_min = glyph->charpos;
13969 if (!glyph->avoid_cursor_p)
13971 /* If we hit point, we've found the glyph on which to
13972 display the cursor. */
13973 if (dpos == 0)
13975 match_with_avoid_cursor = 0;
13976 break;
13978 /* See if we've found a better approximation to
13979 POS_BEFORE or to POS_AFTER. */
13980 if (0 > dpos && dpos > pos_before - pt_old)
13982 pos_before = glyph->charpos;
13983 glyph_before = glyph;
13985 else if (0 < dpos && dpos < pos_after - pt_old)
13987 pos_after = glyph->charpos;
13988 glyph_after = glyph;
13991 else if (dpos == 0)
13992 match_with_avoid_cursor = 1;
13994 else if (STRINGP (glyph->object))
13996 Lisp_Object chprop;
13997 ptrdiff_t glyph_pos = glyph->charpos;
13999 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14000 glyph->object);
14001 if (!NILP (chprop))
14003 /* If the string came from a `display' text property,
14004 look up the buffer position of that property and
14005 use that position to update bpos_max, as if we
14006 actually saw such a position in one of the row's
14007 glyphs. This helps with supporting integer values
14008 of `cursor' property on the display string in
14009 situations where most or all of the row's buffer
14010 text is completely covered by display properties,
14011 so that no glyph with valid buffer positions is
14012 ever seen in the row. */
14013 ptrdiff_t prop_pos =
14014 string_buffer_position_lim (glyph->object, pos_before,
14015 pos_after, 0);
14017 if (prop_pos >= pos_before)
14018 bpos_max = prop_pos - 1;
14020 if (INTEGERP (chprop))
14022 bpos_covered = bpos_max + XINT (chprop);
14023 /* If the `cursor' property covers buffer positions up
14024 to and including point, we should display cursor on
14025 this glyph. Note that, if a `cursor' property on one
14026 of the string's characters has an integer value, we
14027 will break out of the loop below _before_ we get to
14028 the position match above. IOW, integer values of
14029 the `cursor' property override the "exact match for
14030 point" strategy of positioning the cursor. */
14031 /* Implementation note: bpos_max == pt_old when, e.g.,
14032 we are in an empty line, where bpos_max is set to
14033 MATRIX_ROW_START_CHARPOS, see above. */
14034 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14036 cursor = glyph;
14037 break;
14041 string_seen = 1;
14043 x += glyph->pixel_width;
14044 ++glyph;
14046 else if (glyph > end) /* row is reversed */
14047 while (!INTEGERP (glyph->object))
14049 if (BUFFERP (glyph->object))
14051 ptrdiff_t dpos = glyph->charpos - pt_old;
14053 if (glyph->charpos > bpos_max)
14054 bpos_max = glyph->charpos;
14055 if (glyph->charpos < bpos_min)
14056 bpos_min = glyph->charpos;
14057 if (!glyph->avoid_cursor_p)
14059 if (dpos == 0)
14061 match_with_avoid_cursor = 0;
14062 break;
14064 if (0 > dpos && dpos > pos_before - pt_old)
14066 pos_before = glyph->charpos;
14067 glyph_before = glyph;
14069 else if (0 < dpos && dpos < pos_after - pt_old)
14071 pos_after = glyph->charpos;
14072 glyph_after = glyph;
14075 else if (dpos == 0)
14076 match_with_avoid_cursor = 1;
14078 else if (STRINGP (glyph->object))
14080 Lisp_Object chprop;
14081 ptrdiff_t glyph_pos = glyph->charpos;
14083 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14084 glyph->object);
14085 if (!NILP (chprop))
14087 ptrdiff_t prop_pos =
14088 string_buffer_position_lim (glyph->object, pos_before,
14089 pos_after, 0);
14091 if (prop_pos >= pos_before)
14092 bpos_max = prop_pos - 1;
14094 if (INTEGERP (chprop))
14096 bpos_covered = bpos_max + XINT (chprop);
14097 /* If the `cursor' property covers buffer positions up
14098 to and including point, we should display cursor on
14099 this glyph. */
14100 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14102 cursor = glyph;
14103 break;
14106 string_seen = 1;
14108 --glyph;
14109 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14111 x--; /* can't use any pixel_width */
14112 break;
14114 x -= glyph->pixel_width;
14117 /* Step 2: If we didn't find an exact match for point, we need to
14118 look for a proper place to put the cursor among glyphs between
14119 GLYPH_BEFORE and GLYPH_AFTER. */
14120 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14121 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14122 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14124 /* An empty line has a single glyph whose OBJECT is zero and
14125 whose CHARPOS is the position of a newline on that line.
14126 Note that on a TTY, there are more glyphs after that, which
14127 were produced by extend_face_to_end_of_line, but their
14128 CHARPOS is zero or negative. */
14129 int empty_line_p =
14130 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14131 && INTEGERP (glyph->object) && glyph->charpos > 0
14132 /* On a TTY, continued and truncated rows also have a glyph at
14133 their end whose OBJECT is zero and whose CHARPOS is
14134 positive (the continuation and truncation glyphs), but such
14135 rows are obviously not "empty". */
14136 && !(row->continued_p || row->truncated_on_right_p);
14138 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14140 ptrdiff_t ellipsis_pos;
14142 /* Scan back over the ellipsis glyphs. */
14143 if (!row->reversed_p)
14145 ellipsis_pos = (glyph - 1)->charpos;
14146 while (glyph > row->glyphs[TEXT_AREA]
14147 && (glyph - 1)->charpos == ellipsis_pos)
14148 glyph--, x -= glyph->pixel_width;
14149 /* That loop always goes one position too far, including
14150 the glyph before the ellipsis. So scan forward over
14151 that one. */
14152 x += glyph->pixel_width;
14153 glyph++;
14155 else /* row is reversed */
14157 ellipsis_pos = (glyph + 1)->charpos;
14158 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14159 && (glyph + 1)->charpos == ellipsis_pos)
14160 glyph++, x += glyph->pixel_width;
14161 x -= glyph->pixel_width;
14162 glyph--;
14165 else if (match_with_avoid_cursor)
14167 cursor = glyph_after;
14168 x = -1;
14170 else if (string_seen)
14172 int incr = row->reversed_p ? -1 : +1;
14174 /* Need to find the glyph that came out of a string which is
14175 present at point. That glyph is somewhere between
14176 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14177 positioned between POS_BEFORE and POS_AFTER in the
14178 buffer. */
14179 struct glyph *start, *stop;
14180 ptrdiff_t pos = pos_before;
14182 x = -1;
14184 /* If the row ends in a newline from a display string,
14185 reordering could have moved the glyphs belonging to the
14186 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14187 in this case we extend the search to the last glyph in
14188 the row that was not inserted by redisplay. */
14189 if (row->ends_in_newline_from_string_p)
14191 glyph_after = end;
14192 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14195 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14196 correspond to POS_BEFORE and POS_AFTER, respectively. We
14197 need START and STOP in the order that corresponds to the
14198 row's direction as given by its reversed_p flag. If the
14199 directionality of characters between POS_BEFORE and
14200 POS_AFTER is the opposite of the row's base direction,
14201 these characters will have been reordered for display,
14202 and we need to reverse START and STOP. */
14203 if (!row->reversed_p)
14205 start = min (glyph_before, glyph_after);
14206 stop = max (glyph_before, glyph_after);
14208 else
14210 start = max (glyph_before, glyph_after);
14211 stop = min (glyph_before, glyph_after);
14213 for (glyph = start + incr;
14214 row->reversed_p ? glyph > stop : glyph < stop; )
14217 /* Any glyphs that come from the buffer are here because
14218 of bidi reordering. Skip them, and only pay
14219 attention to glyphs that came from some string. */
14220 if (STRINGP (glyph->object))
14222 Lisp_Object str;
14223 ptrdiff_t tem;
14224 /* If the display property covers the newline, we
14225 need to search for it one position farther. */
14226 ptrdiff_t lim = pos_after
14227 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14229 string_from_text_prop = 0;
14230 str = glyph->object;
14231 tem = string_buffer_position_lim (str, pos, lim, 0);
14232 if (tem == 0 /* from overlay */
14233 || pos <= tem)
14235 /* If the string from which this glyph came is
14236 found in the buffer at point, or at position
14237 that is closer to point than pos_after, then
14238 we've found the glyph we've been looking for.
14239 If it comes from an overlay (tem == 0), and
14240 it has the `cursor' property on one of its
14241 glyphs, record that glyph as a candidate for
14242 displaying the cursor. (As in the
14243 unidirectional version, we will display the
14244 cursor on the last candidate we find.) */
14245 if (tem == 0
14246 || tem == pt_old
14247 || (tem - pt_old > 0 && tem < pos_after))
14249 /* The glyphs from this string could have
14250 been reordered. Find the one with the
14251 smallest string position. Or there could
14252 be a character in the string with the
14253 `cursor' property, which means display
14254 cursor on that character's glyph. */
14255 ptrdiff_t strpos = glyph->charpos;
14257 if (tem)
14259 cursor = glyph;
14260 string_from_text_prop = 1;
14262 for ( ;
14263 (row->reversed_p ? glyph > stop : glyph < stop)
14264 && EQ (glyph->object, str);
14265 glyph += incr)
14267 Lisp_Object cprop;
14268 ptrdiff_t gpos = glyph->charpos;
14270 cprop = Fget_char_property (make_number (gpos),
14271 Qcursor,
14272 glyph->object);
14273 if (!NILP (cprop))
14275 cursor = glyph;
14276 break;
14278 if (tem && glyph->charpos < strpos)
14280 strpos = glyph->charpos;
14281 cursor = glyph;
14285 if (tem == pt_old
14286 || (tem - pt_old > 0 && tem < pos_after))
14287 goto compute_x;
14289 if (tem)
14290 pos = tem + 1; /* don't find previous instances */
14292 /* This string is not what we want; skip all of the
14293 glyphs that came from it. */
14294 while ((row->reversed_p ? glyph > stop : glyph < stop)
14295 && EQ (glyph->object, str))
14296 glyph += incr;
14298 else
14299 glyph += incr;
14302 /* If we reached the end of the line, and END was from a string,
14303 the cursor is not on this line. */
14304 if (cursor == NULL
14305 && (row->reversed_p ? glyph <= end : glyph >= end)
14306 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14307 && STRINGP (end->object)
14308 && row->continued_p)
14309 return 0;
14311 /* A truncated row may not include PT among its character positions.
14312 Setting the cursor inside the scroll margin will trigger
14313 recalculation of hscroll in hscroll_window_tree. But if a
14314 display string covers point, defer to the string-handling
14315 code below to figure this out. */
14316 else if (row->truncated_on_left_p && pt_old < bpos_min)
14318 cursor = glyph_before;
14319 x = -1;
14321 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14322 /* Zero-width characters produce no glyphs. */
14323 || (!empty_line_p
14324 && (row->reversed_p
14325 ? glyph_after > glyphs_end
14326 : glyph_after < glyphs_end)))
14328 cursor = glyph_after;
14329 x = -1;
14333 compute_x:
14334 if (cursor != NULL)
14335 glyph = cursor;
14336 else if (glyph == glyphs_end
14337 && pos_before == pos_after
14338 && STRINGP ((row->reversed_p
14339 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14340 : row->glyphs[TEXT_AREA])->object))
14342 /* If all the glyphs of this row came from strings, put the
14343 cursor on the first glyph of the row. This avoids having the
14344 cursor outside of the text area in this very rare and hard
14345 use case. */
14346 glyph =
14347 row->reversed_p
14348 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14349 : row->glyphs[TEXT_AREA];
14351 if (x < 0)
14353 struct glyph *g;
14355 /* Need to compute x that corresponds to GLYPH. */
14356 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14358 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14359 emacs_abort ();
14360 x += g->pixel_width;
14364 /* ROW could be part of a continued line, which, under bidi
14365 reordering, might have other rows whose start and end charpos
14366 occlude point. Only set w->cursor if we found a better
14367 approximation to the cursor position than we have from previously
14368 examined candidate rows belonging to the same continued line. */
14369 if (/* we already have a candidate row */
14370 w->cursor.vpos >= 0
14371 /* that candidate is not the row we are processing */
14372 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14373 /* Make sure cursor.vpos specifies a row whose start and end
14374 charpos occlude point, and it is valid candidate for being a
14375 cursor-row. This is because some callers of this function
14376 leave cursor.vpos at the row where the cursor was displayed
14377 during the last redisplay cycle. */
14378 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14379 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14380 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14382 struct glyph *g1 =
14383 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14385 /* Don't consider glyphs that are outside TEXT_AREA. */
14386 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14387 return 0;
14388 /* Keep the candidate whose buffer position is the closest to
14389 point or has the `cursor' property. */
14390 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14391 w->cursor.hpos >= 0
14392 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14393 && ((BUFFERP (g1->object)
14394 && (g1->charpos == pt_old /* an exact match always wins */
14395 || (BUFFERP (glyph->object)
14396 && eabs (g1->charpos - pt_old)
14397 < eabs (glyph->charpos - pt_old))))
14398 /* previous candidate is a glyph from a string that has
14399 a non-nil `cursor' property */
14400 || (STRINGP (g1->object)
14401 && (!NILP (Fget_char_property (make_number (g1->charpos),
14402 Qcursor, g1->object))
14403 /* previous candidate is from the same display
14404 string as this one, and the display string
14405 came from a text property */
14406 || (EQ (g1->object, glyph->object)
14407 && string_from_text_prop)
14408 /* this candidate is from newline and its
14409 position is not an exact match */
14410 || (INTEGERP (glyph->object)
14411 && glyph->charpos != pt_old)))))
14412 return 0;
14413 /* If this candidate gives an exact match, use that. */
14414 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14415 /* If this candidate is a glyph created for the
14416 terminating newline of a line, and point is on that
14417 newline, it wins because it's an exact match. */
14418 || (!row->continued_p
14419 && INTEGERP (glyph->object)
14420 && glyph->charpos == 0
14421 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14422 /* Otherwise, keep the candidate that comes from a row
14423 spanning less buffer positions. This may win when one or
14424 both candidate positions are on glyphs that came from
14425 display strings, for which we cannot compare buffer
14426 positions. */
14427 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14428 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14429 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14430 return 0;
14432 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14433 w->cursor.x = x;
14434 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14435 w->cursor.y = row->y + dy;
14437 if (w == XWINDOW (selected_window))
14439 if (!row->continued_p
14440 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14441 && row->x == 0)
14443 this_line_buffer = XBUFFER (w->contents);
14445 CHARPOS (this_line_start_pos)
14446 = MATRIX_ROW_START_CHARPOS (row) + delta;
14447 BYTEPOS (this_line_start_pos)
14448 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14450 CHARPOS (this_line_end_pos)
14451 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14452 BYTEPOS (this_line_end_pos)
14453 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14455 this_line_y = w->cursor.y;
14456 this_line_pixel_height = row->height;
14457 this_line_vpos = w->cursor.vpos;
14458 this_line_start_x = row->x;
14460 else
14461 CHARPOS (this_line_start_pos) = 0;
14464 return 1;
14468 /* Run window scroll functions, if any, for WINDOW with new window
14469 start STARTP. Sets the window start of WINDOW to that position.
14471 We assume that the window's buffer is really current. */
14473 static struct text_pos
14474 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14476 struct window *w = XWINDOW (window);
14477 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14479 eassert (current_buffer == XBUFFER (w->contents));
14481 if (!NILP (Vwindow_scroll_functions))
14483 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14484 make_number (CHARPOS (startp)));
14485 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14486 /* In case the hook functions switch buffers. */
14487 set_buffer_internal (XBUFFER (w->contents));
14490 return startp;
14494 /* Make sure the line containing the cursor is fully visible.
14495 A value of 1 means there is nothing to be done.
14496 (Either the line is fully visible, or it cannot be made so,
14497 or we cannot tell.)
14499 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14500 is higher than window.
14502 A value of 0 means the caller should do scrolling
14503 as if point had gone off the screen. */
14505 static int
14506 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14508 struct glyph_matrix *matrix;
14509 struct glyph_row *row;
14510 int window_height;
14512 if (!make_cursor_line_fully_visible_p)
14513 return 1;
14515 /* It's not always possible to find the cursor, e.g, when a window
14516 is full of overlay strings. Don't do anything in that case. */
14517 if (w->cursor.vpos < 0)
14518 return 1;
14520 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14521 row = MATRIX_ROW (matrix, w->cursor.vpos);
14523 /* If the cursor row is not partially visible, there's nothing to do. */
14524 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14525 return 1;
14527 /* If the row the cursor is in is taller than the window's height,
14528 it's not clear what to do, so do nothing. */
14529 window_height = window_box_height (w);
14530 if (row->height >= window_height)
14532 if (!force_p || MINI_WINDOW_P (w)
14533 || w->vscroll || w->cursor.vpos == 0)
14534 return 1;
14536 return 0;
14540 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14541 non-zero means only WINDOW is redisplayed in redisplay_internal.
14542 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14543 in redisplay_window to bring a partially visible line into view in
14544 the case that only the cursor has moved.
14546 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14547 last screen line's vertical height extends past the end of the screen.
14549 Value is
14551 1 if scrolling succeeded
14553 0 if scrolling didn't find point.
14555 -1 if new fonts have been loaded so that we must interrupt
14556 redisplay, adjust glyph matrices, and try again. */
14558 enum
14560 SCROLLING_SUCCESS,
14561 SCROLLING_FAILED,
14562 SCROLLING_NEED_LARGER_MATRICES
14565 /* If scroll-conservatively is more than this, never recenter.
14567 If you change this, don't forget to update the doc string of
14568 `scroll-conservatively' and the Emacs manual. */
14569 #define SCROLL_LIMIT 100
14571 static int
14572 try_scrolling (Lisp_Object window, int just_this_one_p,
14573 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14574 int temp_scroll_step, int last_line_misfit)
14576 struct window *w = XWINDOW (window);
14577 struct frame *f = XFRAME (w->frame);
14578 struct text_pos pos, startp;
14579 struct it it;
14580 int this_scroll_margin, scroll_max, rc, height;
14581 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14582 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14583 Lisp_Object aggressive;
14584 /* We will never try scrolling more than this number of lines. */
14585 int scroll_limit = SCROLL_LIMIT;
14586 int frame_line_height = default_line_pixel_height (w);
14587 int window_total_lines
14588 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14590 #ifdef GLYPH_DEBUG
14591 debug_method_add (w, "try_scrolling");
14592 #endif
14594 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14596 /* Compute scroll margin height in pixels. We scroll when point is
14597 within this distance from the top or bottom of the window. */
14598 if (scroll_margin > 0)
14599 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14600 * frame_line_height;
14601 else
14602 this_scroll_margin = 0;
14604 /* Force arg_scroll_conservatively to have a reasonable value, to
14605 avoid scrolling too far away with slow move_it_* functions. Note
14606 that the user can supply scroll-conservatively equal to
14607 `most-positive-fixnum', which can be larger than INT_MAX. */
14608 if (arg_scroll_conservatively > scroll_limit)
14610 arg_scroll_conservatively = scroll_limit + 1;
14611 scroll_max = scroll_limit * frame_line_height;
14613 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14614 /* Compute how much we should try to scroll maximally to bring
14615 point into view. */
14616 scroll_max = (max (scroll_step,
14617 max (arg_scroll_conservatively, temp_scroll_step))
14618 * frame_line_height);
14619 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14620 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14621 /* We're trying to scroll because of aggressive scrolling but no
14622 scroll_step is set. Choose an arbitrary one. */
14623 scroll_max = 10 * frame_line_height;
14624 else
14625 scroll_max = 0;
14627 too_near_end:
14629 /* Decide whether to scroll down. */
14630 if (PT > CHARPOS (startp))
14632 int scroll_margin_y;
14634 /* Compute the pixel ypos of the scroll margin, then move IT to
14635 either that ypos or PT, whichever comes first. */
14636 start_display (&it, w, startp);
14637 scroll_margin_y = it.last_visible_y - this_scroll_margin
14638 - frame_line_height * extra_scroll_margin_lines;
14639 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14640 (MOVE_TO_POS | MOVE_TO_Y));
14642 if (PT > CHARPOS (it.current.pos))
14644 int y0 = line_bottom_y (&it);
14645 /* Compute how many pixels below window bottom to stop searching
14646 for PT. This avoids costly search for PT that is far away if
14647 the user limited scrolling by a small number of lines, but
14648 always finds PT if scroll_conservatively is set to a large
14649 number, such as most-positive-fixnum. */
14650 int slack = max (scroll_max, 10 * frame_line_height);
14651 int y_to_move = it.last_visible_y + slack;
14653 /* Compute the distance from the scroll margin to PT or to
14654 the scroll limit, whichever comes first. This should
14655 include the height of the cursor line, to make that line
14656 fully visible. */
14657 move_it_to (&it, PT, -1, y_to_move,
14658 -1, MOVE_TO_POS | MOVE_TO_Y);
14659 dy = line_bottom_y (&it) - y0;
14661 if (dy > scroll_max)
14662 return SCROLLING_FAILED;
14664 if (dy > 0)
14665 scroll_down_p = 1;
14669 if (scroll_down_p)
14671 /* Point is in or below the bottom scroll margin, so move the
14672 window start down. If scrolling conservatively, move it just
14673 enough down to make point visible. If scroll_step is set,
14674 move it down by scroll_step. */
14675 if (arg_scroll_conservatively)
14676 amount_to_scroll
14677 = min (max (dy, frame_line_height),
14678 frame_line_height * arg_scroll_conservatively);
14679 else if (scroll_step || temp_scroll_step)
14680 amount_to_scroll = scroll_max;
14681 else
14683 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14684 height = WINDOW_BOX_TEXT_HEIGHT (w);
14685 if (NUMBERP (aggressive))
14687 double float_amount = XFLOATINT (aggressive) * height;
14688 int aggressive_scroll = float_amount;
14689 if (aggressive_scroll == 0 && float_amount > 0)
14690 aggressive_scroll = 1;
14691 /* Don't let point enter the scroll margin near top of
14692 the window. This could happen if the value of
14693 scroll_up_aggressively is too large and there are
14694 non-zero margins, because scroll_up_aggressively
14695 means put point that fraction of window height
14696 _from_the_bottom_margin_. */
14697 if (aggressive_scroll + 2*this_scroll_margin > height)
14698 aggressive_scroll = height - 2*this_scroll_margin;
14699 amount_to_scroll = dy + aggressive_scroll;
14703 if (amount_to_scroll <= 0)
14704 return SCROLLING_FAILED;
14706 start_display (&it, w, startp);
14707 if (arg_scroll_conservatively <= scroll_limit)
14708 move_it_vertically (&it, amount_to_scroll);
14709 else
14711 /* Extra precision for users who set scroll-conservatively
14712 to a large number: make sure the amount we scroll
14713 the window start is never less than amount_to_scroll,
14714 which was computed as distance from window bottom to
14715 point. This matters when lines at window top and lines
14716 below window bottom have different height. */
14717 struct it it1;
14718 void *it1data = NULL;
14719 /* We use a temporary it1 because line_bottom_y can modify
14720 its argument, if it moves one line down; see there. */
14721 int start_y;
14723 SAVE_IT (it1, it, it1data);
14724 start_y = line_bottom_y (&it1);
14725 do {
14726 RESTORE_IT (&it, &it, it1data);
14727 move_it_by_lines (&it, 1);
14728 SAVE_IT (it1, it, it1data);
14729 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14732 /* If STARTP is unchanged, move it down another screen line. */
14733 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14734 move_it_by_lines (&it, 1);
14735 startp = it.current.pos;
14737 else
14739 struct text_pos scroll_margin_pos = startp;
14740 int y_offset = 0;
14742 /* See if point is inside the scroll margin at the top of the
14743 window. */
14744 if (this_scroll_margin)
14746 int y_start;
14748 start_display (&it, w, startp);
14749 y_start = it.current_y;
14750 move_it_vertically (&it, this_scroll_margin);
14751 scroll_margin_pos = it.current.pos;
14752 /* If we didn't move enough before hitting ZV, request
14753 additional amount of scroll, to move point out of the
14754 scroll margin. */
14755 if (IT_CHARPOS (it) == ZV
14756 && it.current_y - y_start < this_scroll_margin)
14757 y_offset = this_scroll_margin - (it.current_y - y_start);
14760 if (PT < CHARPOS (scroll_margin_pos))
14762 /* Point is in the scroll margin at the top of the window or
14763 above what is displayed in the window. */
14764 int y0, y_to_move;
14766 /* Compute the vertical distance from PT to the scroll
14767 margin position. Move as far as scroll_max allows, or
14768 one screenful, or 10 screen lines, whichever is largest.
14769 Give up if distance is greater than scroll_max or if we
14770 didn't reach the scroll margin position. */
14771 SET_TEXT_POS (pos, PT, PT_BYTE);
14772 start_display (&it, w, pos);
14773 y0 = it.current_y;
14774 y_to_move = max (it.last_visible_y,
14775 max (scroll_max, 10 * frame_line_height));
14776 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14777 y_to_move, -1,
14778 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14779 dy = it.current_y - y0;
14780 if (dy > scroll_max
14781 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14782 return SCROLLING_FAILED;
14784 /* Additional scroll for when ZV was too close to point. */
14785 dy += y_offset;
14787 /* Compute new window start. */
14788 start_display (&it, w, startp);
14790 if (arg_scroll_conservatively)
14791 amount_to_scroll = max (dy, frame_line_height *
14792 max (scroll_step, temp_scroll_step));
14793 else if (scroll_step || temp_scroll_step)
14794 amount_to_scroll = scroll_max;
14795 else
14797 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14798 height = WINDOW_BOX_TEXT_HEIGHT (w);
14799 if (NUMBERP (aggressive))
14801 double float_amount = XFLOATINT (aggressive) * height;
14802 int aggressive_scroll = float_amount;
14803 if (aggressive_scroll == 0 && float_amount > 0)
14804 aggressive_scroll = 1;
14805 /* Don't let point enter the scroll margin near
14806 bottom of the window, if the value of
14807 scroll_down_aggressively happens to be too
14808 large. */
14809 if (aggressive_scroll + 2*this_scroll_margin > height)
14810 aggressive_scroll = height - 2*this_scroll_margin;
14811 amount_to_scroll = dy + aggressive_scroll;
14815 if (amount_to_scroll <= 0)
14816 return SCROLLING_FAILED;
14818 move_it_vertically_backward (&it, amount_to_scroll);
14819 startp = it.current.pos;
14823 /* Run window scroll functions. */
14824 startp = run_window_scroll_functions (window, startp);
14826 /* Display the window. Give up if new fonts are loaded, or if point
14827 doesn't appear. */
14828 if (!try_window (window, startp, 0))
14829 rc = SCROLLING_NEED_LARGER_MATRICES;
14830 else if (w->cursor.vpos < 0)
14832 clear_glyph_matrix (w->desired_matrix);
14833 rc = SCROLLING_FAILED;
14835 else
14837 /* Maybe forget recorded base line for line number display. */
14838 if (!just_this_one_p
14839 || current_buffer->clip_changed
14840 || BEG_UNCHANGED < CHARPOS (startp))
14841 w->base_line_number = 0;
14843 /* If cursor ends up on a partially visible line,
14844 treat that as being off the bottom of the screen. */
14845 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14846 /* It's possible that the cursor is on the first line of the
14847 buffer, which is partially obscured due to a vscroll
14848 (Bug#7537). In that case, avoid looping forever . */
14849 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14851 clear_glyph_matrix (w->desired_matrix);
14852 ++extra_scroll_margin_lines;
14853 goto too_near_end;
14855 rc = SCROLLING_SUCCESS;
14858 return rc;
14862 /* Compute a suitable window start for window W if display of W starts
14863 on a continuation line. Value is non-zero if a new window start
14864 was computed.
14866 The new window start will be computed, based on W's width, starting
14867 from the start of the continued line. It is the start of the
14868 screen line with the minimum distance from the old start W->start. */
14870 static int
14871 compute_window_start_on_continuation_line (struct window *w)
14873 struct text_pos pos, start_pos;
14874 int window_start_changed_p = 0;
14876 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14878 /* If window start is on a continuation line... Window start may be
14879 < BEGV in case there's invisible text at the start of the
14880 buffer (M-x rmail, for example). */
14881 if (CHARPOS (start_pos) > BEGV
14882 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14884 struct it it;
14885 struct glyph_row *row;
14887 /* Handle the case that the window start is out of range. */
14888 if (CHARPOS (start_pos) < BEGV)
14889 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14890 else if (CHARPOS (start_pos) > ZV)
14891 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14893 /* Find the start of the continued line. This should be fast
14894 because find_newline is fast (newline cache). */
14895 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14896 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14897 row, DEFAULT_FACE_ID);
14898 reseat_at_previous_visible_line_start (&it);
14900 /* If the line start is "too far" away from the window start,
14901 say it takes too much time to compute a new window start. */
14902 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14903 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14905 int min_distance, distance;
14907 /* Move forward by display lines to find the new window
14908 start. If window width was enlarged, the new start can
14909 be expected to be > the old start. If window width was
14910 decreased, the new window start will be < the old start.
14911 So, we're looking for the display line start with the
14912 minimum distance from the old window start. */
14913 pos = it.current.pos;
14914 min_distance = INFINITY;
14915 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14916 distance < min_distance)
14918 min_distance = distance;
14919 pos = it.current.pos;
14920 if (it.line_wrap == WORD_WRAP)
14922 /* Under WORD_WRAP, move_it_by_lines is likely to
14923 overshoot and stop not at the first, but the
14924 second character from the left margin. So in
14925 that case, we need a more tight control on the X
14926 coordinate of the iterator than move_it_by_lines
14927 promises in its contract. The method is to first
14928 go to the last (rightmost) visible character of a
14929 line, then move to the leftmost character on the
14930 next line in a separate call. */
14931 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14932 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14933 move_it_to (&it, ZV, 0,
14934 it.current_y + it.max_ascent + it.max_descent, -1,
14935 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14937 else
14938 move_it_by_lines (&it, 1);
14941 /* Set the window start there. */
14942 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14943 window_start_changed_p = 1;
14947 return window_start_changed_p;
14951 /* Try cursor movement in case text has not changed in window WINDOW,
14952 with window start STARTP. Value is
14954 CURSOR_MOVEMENT_SUCCESS if successful
14956 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14958 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14959 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14960 we want to scroll as if scroll-step were set to 1. See the code.
14962 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14963 which case we have to abort this redisplay, and adjust matrices
14964 first. */
14966 enum
14968 CURSOR_MOVEMENT_SUCCESS,
14969 CURSOR_MOVEMENT_CANNOT_BE_USED,
14970 CURSOR_MOVEMENT_MUST_SCROLL,
14971 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14974 static int
14975 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14977 struct window *w = XWINDOW (window);
14978 struct frame *f = XFRAME (w->frame);
14979 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14981 #ifdef GLYPH_DEBUG
14982 if (inhibit_try_cursor_movement)
14983 return rc;
14984 #endif
14986 /* Previously, there was a check for Lisp integer in the
14987 if-statement below. Now, this field is converted to
14988 ptrdiff_t, thus zero means invalid position in a buffer. */
14989 eassert (w->last_point > 0);
14990 /* Likewise there was a check whether window_end_vpos is nil or larger
14991 than the window. Now window_end_vpos is int and so never nil, but
14992 let's leave eassert to check whether it fits in the window. */
14993 eassert (w->window_end_vpos < w->current_matrix->nrows);
14995 /* Handle case where text has not changed, only point, and it has
14996 not moved off the frame. */
14997 if (/* Point may be in this window. */
14998 PT >= CHARPOS (startp)
14999 /* Selective display hasn't changed. */
15000 && !current_buffer->clip_changed
15001 /* Function force-mode-line-update is used to force a thorough
15002 redisplay. It sets either windows_or_buffers_changed or
15003 update_mode_lines. So don't take a shortcut here for these
15004 cases. */
15005 && !update_mode_lines
15006 && !windows_or_buffers_changed
15007 && !f->cursor_type_changed
15008 /* Can't use this case if highlighting a region. When a
15009 region exists, cursor movement has to do more than just
15010 set the cursor. */
15011 && markpos_of_region () < 0
15012 && !w->region_showing
15013 && NILP (Vshow_trailing_whitespace)
15014 /* This code is not used for mini-buffer for the sake of the case
15015 of redisplaying to replace an echo area message; since in
15016 that case the mini-buffer contents per se are usually
15017 unchanged. This code is of no real use in the mini-buffer
15018 since the handling of this_line_start_pos, etc., in redisplay
15019 handles the same cases. */
15020 && !EQ (window, minibuf_window)
15021 && (FRAME_WINDOW_P (f)
15022 || !overlay_arrow_in_current_buffer_p ()))
15024 int this_scroll_margin, top_scroll_margin;
15025 struct glyph_row *row = NULL;
15026 int frame_line_height = default_line_pixel_height (w);
15027 int window_total_lines
15028 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15030 #ifdef GLYPH_DEBUG
15031 debug_method_add (w, "cursor movement");
15032 #endif
15034 /* Scroll if point within this distance from the top or bottom
15035 of the window. This is a pixel value. */
15036 if (scroll_margin > 0)
15038 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15039 this_scroll_margin *= frame_line_height;
15041 else
15042 this_scroll_margin = 0;
15044 top_scroll_margin = this_scroll_margin;
15045 if (WINDOW_WANTS_HEADER_LINE_P (w))
15046 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15048 /* Start with the row the cursor was displayed during the last
15049 not paused redisplay. Give up if that row is not valid. */
15050 if (w->last_cursor_vpos < 0
15051 || w->last_cursor_vpos >= w->current_matrix->nrows)
15052 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15053 else
15055 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15056 if (row->mode_line_p)
15057 ++row;
15058 if (!row->enabled_p)
15059 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15062 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15064 int scroll_p = 0, must_scroll = 0;
15065 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15067 if (PT > w->last_point)
15069 /* Point has moved forward. */
15070 while (MATRIX_ROW_END_CHARPOS (row) < PT
15071 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15073 eassert (row->enabled_p);
15074 ++row;
15077 /* If the end position of a row equals the start
15078 position of the next row, and PT is at that position,
15079 we would rather display cursor in the next line. */
15080 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15081 && MATRIX_ROW_END_CHARPOS (row) == PT
15082 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15083 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15084 && !cursor_row_p (row))
15085 ++row;
15087 /* If within the scroll margin, scroll. Note that
15088 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15089 the next line would be drawn, and that
15090 this_scroll_margin can be zero. */
15091 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15092 || PT > MATRIX_ROW_END_CHARPOS (row)
15093 /* Line is completely visible last line in window
15094 and PT is to be set in the next line. */
15095 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15096 && PT == MATRIX_ROW_END_CHARPOS (row)
15097 && !row->ends_at_zv_p
15098 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15099 scroll_p = 1;
15101 else if (PT < w->last_point)
15103 /* Cursor has to be moved backward. Note that PT >=
15104 CHARPOS (startp) because of the outer if-statement. */
15105 while (!row->mode_line_p
15106 && (MATRIX_ROW_START_CHARPOS (row) > PT
15107 || (MATRIX_ROW_START_CHARPOS (row) == PT
15108 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15109 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15110 row > w->current_matrix->rows
15111 && (row-1)->ends_in_newline_from_string_p))))
15112 && (row->y > top_scroll_margin
15113 || CHARPOS (startp) == BEGV))
15115 eassert (row->enabled_p);
15116 --row;
15119 /* Consider the following case: Window starts at BEGV,
15120 there is invisible, intangible text at BEGV, so that
15121 display starts at some point START > BEGV. It can
15122 happen that we are called with PT somewhere between
15123 BEGV and START. Try to handle that case. */
15124 if (row < w->current_matrix->rows
15125 || row->mode_line_p)
15127 row = w->current_matrix->rows;
15128 if (row->mode_line_p)
15129 ++row;
15132 /* Due to newlines in overlay strings, we may have to
15133 skip forward over overlay strings. */
15134 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15135 && MATRIX_ROW_END_CHARPOS (row) == PT
15136 && !cursor_row_p (row))
15137 ++row;
15139 /* If within the scroll margin, scroll. */
15140 if (row->y < top_scroll_margin
15141 && CHARPOS (startp) != BEGV)
15142 scroll_p = 1;
15144 else
15146 /* Cursor did not move. So don't scroll even if cursor line
15147 is partially visible, as it was so before. */
15148 rc = CURSOR_MOVEMENT_SUCCESS;
15151 if (PT < MATRIX_ROW_START_CHARPOS (row)
15152 || PT > MATRIX_ROW_END_CHARPOS (row))
15154 /* if PT is not in the glyph row, give up. */
15155 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15156 must_scroll = 1;
15158 else if (rc != CURSOR_MOVEMENT_SUCCESS
15159 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15161 struct glyph_row *row1;
15163 /* If rows are bidi-reordered and point moved, back up
15164 until we find a row that does not belong to a
15165 continuation line. This is because we must consider
15166 all rows of a continued line as candidates for the
15167 new cursor positioning, since row start and end
15168 positions change non-linearly with vertical position
15169 in such rows. */
15170 /* FIXME: Revisit this when glyph ``spilling'' in
15171 continuation lines' rows is implemented for
15172 bidi-reordered rows. */
15173 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15174 MATRIX_ROW_CONTINUATION_LINE_P (row);
15175 --row)
15177 /* If we hit the beginning of the displayed portion
15178 without finding the first row of a continued
15179 line, give up. */
15180 if (row <= row1)
15182 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15183 break;
15185 eassert (row->enabled_p);
15188 if (must_scroll)
15190 else if (rc != CURSOR_MOVEMENT_SUCCESS
15191 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15192 /* Make sure this isn't a header line by any chance, since
15193 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15194 && !row->mode_line_p
15195 && make_cursor_line_fully_visible_p)
15197 if (PT == MATRIX_ROW_END_CHARPOS (row)
15198 && !row->ends_at_zv_p
15199 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15200 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15201 else if (row->height > window_box_height (w))
15203 /* If we end up in a partially visible line, let's
15204 make it fully visible, except when it's taller
15205 than the window, in which case we can't do much
15206 about it. */
15207 *scroll_step = 1;
15208 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15210 else
15212 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15213 if (!cursor_row_fully_visible_p (w, 0, 1))
15214 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15215 else
15216 rc = CURSOR_MOVEMENT_SUCCESS;
15219 else if (scroll_p)
15220 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15221 else if (rc != CURSOR_MOVEMENT_SUCCESS
15222 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15224 /* With bidi-reordered rows, there could be more than
15225 one candidate row whose start and end positions
15226 occlude point. We need to let set_cursor_from_row
15227 find the best candidate. */
15228 /* FIXME: Revisit this when glyph ``spilling'' in
15229 continuation lines' rows is implemented for
15230 bidi-reordered rows. */
15231 int rv = 0;
15235 int at_zv_p = 0, exact_match_p = 0;
15237 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15238 && PT <= MATRIX_ROW_END_CHARPOS (row)
15239 && cursor_row_p (row))
15240 rv |= set_cursor_from_row (w, row, w->current_matrix,
15241 0, 0, 0, 0);
15242 /* As soon as we've found the exact match for point,
15243 or the first suitable row whose ends_at_zv_p flag
15244 is set, we are done. */
15245 at_zv_p =
15246 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15247 if (rv && !at_zv_p
15248 && w->cursor.hpos >= 0
15249 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15250 w->cursor.vpos))
15252 struct glyph_row *candidate =
15253 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15254 struct glyph *g =
15255 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15256 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15258 exact_match_p =
15259 (BUFFERP (g->object) && g->charpos == PT)
15260 || (INTEGERP (g->object)
15261 && (g->charpos == PT
15262 || (g->charpos == 0 && endpos - 1 == PT)));
15264 if (rv && (at_zv_p || exact_match_p))
15266 rc = CURSOR_MOVEMENT_SUCCESS;
15267 break;
15269 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15270 break;
15271 ++row;
15273 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15274 || row->continued_p)
15275 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15276 || (MATRIX_ROW_START_CHARPOS (row) == PT
15277 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15278 /* If we didn't find any candidate rows, or exited the
15279 loop before all the candidates were examined, signal
15280 to the caller that this method failed. */
15281 if (rc != CURSOR_MOVEMENT_SUCCESS
15282 && !(rv
15283 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15284 && !row->continued_p))
15285 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15286 else if (rv)
15287 rc = CURSOR_MOVEMENT_SUCCESS;
15289 else
15293 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15295 rc = CURSOR_MOVEMENT_SUCCESS;
15296 break;
15298 ++row;
15300 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15301 && MATRIX_ROW_START_CHARPOS (row) == PT
15302 && cursor_row_p (row));
15307 return rc;
15310 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15311 static
15312 #endif
15313 void
15314 set_vertical_scroll_bar (struct window *w)
15316 ptrdiff_t start, end, whole;
15318 /* Calculate the start and end positions for the current window.
15319 At some point, it would be nice to choose between scrollbars
15320 which reflect the whole buffer size, with special markers
15321 indicating narrowing, and scrollbars which reflect only the
15322 visible region.
15324 Note that mini-buffers sometimes aren't displaying any text. */
15325 if (!MINI_WINDOW_P (w)
15326 || (w == XWINDOW (minibuf_window)
15327 && NILP (echo_area_buffer[0])))
15329 struct buffer *buf = XBUFFER (w->contents);
15330 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15331 start = marker_position (w->start) - BUF_BEGV (buf);
15332 /* I don't think this is guaranteed to be right. For the
15333 moment, we'll pretend it is. */
15334 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15336 if (end < start)
15337 end = start;
15338 if (whole < (end - start))
15339 whole = end - start;
15341 else
15342 start = end = whole = 0;
15344 /* Indicate what this scroll bar ought to be displaying now. */
15345 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15346 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15347 (w, end - start, whole, start);
15351 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15352 selected_window is redisplayed.
15354 We can return without actually redisplaying the window if fonts has been
15355 changed on window's frame. In that case, redisplay_internal will retry. */
15357 static void
15358 redisplay_window (Lisp_Object window, int just_this_one_p)
15360 struct window *w = XWINDOW (window);
15361 struct frame *f = XFRAME (w->frame);
15362 struct buffer *buffer = XBUFFER (w->contents);
15363 struct buffer *old = current_buffer;
15364 struct text_pos lpoint, opoint, startp;
15365 int update_mode_line;
15366 int tem;
15367 struct it it;
15368 /* Record it now because it's overwritten. */
15369 int current_matrix_up_to_date_p = 0;
15370 int used_current_matrix_p = 0;
15371 /* This is less strict than current_matrix_up_to_date_p.
15372 It indicates that the buffer contents and narrowing are unchanged. */
15373 int buffer_unchanged_p = 0;
15374 int temp_scroll_step = 0;
15375 ptrdiff_t count = SPECPDL_INDEX ();
15376 int rc;
15377 int centering_position = -1;
15378 int last_line_misfit = 0;
15379 ptrdiff_t beg_unchanged, end_unchanged;
15380 int frame_line_height;
15382 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15383 opoint = lpoint;
15385 #ifdef GLYPH_DEBUG
15386 *w->desired_matrix->method = 0;
15387 #endif
15389 /* Make sure that both W's markers are valid. */
15390 eassert (XMARKER (w->start)->buffer == buffer);
15391 eassert (XMARKER (w->pointm)->buffer == buffer);
15393 restart:
15394 reconsider_clip_changes (w);
15395 frame_line_height = default_line_pixel_height (w);
15397 /* Has the mode line to be updated? */
15398 update_mode_line = (w->update_mode_line
15399 || update_mode_lines
15400 || buffer->clip_changed
15401 || buffer->prevent_redisplay_optimizations_p);
15403 if (MINI_WINDOW_P (w))
15405 if (w == XWINDOW (echo_area_window)
15406 && !NILP (echo_area_buffer[0]))
15408 if (update_mode_line)
15409 /* We may have to update a tty frame's menu bar or a
15410 tool-bar. Example `M-x C-h C-h C-g'. */
15411 goto finish_menu_bars;
15412 else
15413 /* We've already displayed the echo area glyphs in this window. */
15414 goto finish_scroll_bars;
15416 else if ((w != XWINDOW (minibuf_window)
15417 || minibuf_level == 0)
15418 /* When buffer is nonempty, redisplay window normally. */
15419 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15420 /* Quail displays non-mini buffers in minibuffer window.
15421 In that case, redisplay the window normally. */
15422 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15424 /* W is a mini-buffer window, but it's not active, so clear
15425 it. */
15426 int yb = window_text_bottom_y (w);
15427 struct glyph_row *row;
15428 int y;
15430 for (y = 0, row = w->desired_matrix->rows;
15431 y < yb;
15432 y += row->height, ++row)
15433 blank_row (w, row, y);
15434 goto finish_scroll_bars;
15437 clear_glyph_matrix (w->desired_matrix);
15440 /* Otherwise set up data on this window; select its buffer and point
15441 value. */
15442 /* Really select the buffer, for the sake of buffer-local
15443 variables. */
15444 set_buffer_internal_1 (XBUFFER (w->contents));
15446 current_matrix_up_to_date_p
15447 = (w->window_end_valid
15448 && !current_buffer->clip_changed
15449 && !current_buffer->prevent_redisplay_optimizations_p
15450 && !window_outdated (w));
15452 /* Run the window-bottom-change-functions
15453 if it is possible that the text on the screen has changed
15454 (either due to modification of the text, or any other reason). */
15455 if (!current_matrix_up_to_date_p
15456 && !NILP (Vwindow_text_change_functions))
15458 safe_run_hooks (Qwindow_text_change_functions);
15459 goto restart;
15462 beg_unchanged = BEG_UNCHANGED;
15463 end_unchanged = END_UNCHANGED;
15465 SET_TEXT_POS (opoint, PT, PT_BYTE);
15467 specbind (Qinhibit_point_motion_hooks, Qt);
15469 buffer_unchanged_p
15470 = (w->window_end_valid
15471 && !current_buffer->clip_changed
15472 && !window_outdated (w));
15474 /* When windows_or_buffers_changed is non-zero, we can't rely
15475 on the window end being valid, so set it to zero there. */
15476 if (windows_or_buffers_changed)
15478 /* If window starts on a continuation line, maybe adjust the
15479 window start in case the window's width changed. */
15480 if (XMARKER (w->start)->buffer == current_buffer)
15481 compute_window_start_on_continuation_line (w);
15483 w->window_end_valid = 0;
15484 /* If so, we also can't rely on current matrix
15485 and should not fool try_cursor_movement below. */
15486 current_matrix_up_to_date_p = 0;
15489 /* Some sanity checks. */
15490 CHECK_WINDOW_END (w);
15491 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15492 emacs_abort ();
15493 if (BYTEPOS (opoint) < CHARPOS (opoint))
15494 emacs_abort ();
15496 if (mode_line_update_needed (w))
15497 update_mode_line = 1;
15499 /* Point refers normally to the selected window. For any other
15500 window, set up appropriate value. */
15501 if (!EQ (window, selected_window))
15503 ptrdiff_t new_pt = marker_position (w->pointm);
15504 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15505 if (new_pt < BEGV)
15507 new_pt = BEGV;
15508 new_pt_byte = BEGV_BYTE;
15509 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15511 else if (new_pt > (ZV - 1))
15513 new_pt = ZV;
15514 new_pt_byte = ZV_BYTE;
15515 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15518 /* We don't use SET_PT so that the point-motion hooks don't run. */
15519 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15522 /* If any of the character widths specified in the display table
15523 have changed, invalidate the width run cache. It's true that
15524 this may be a bit late to catch such changes, but the rest of
15525 redisplay goes (non-fatally) haywire when the display table is
15526 changed, so why should we worry about doing any better? */
15527 if (current_buffer->width_run_cache)
15529 struct Lisp_Char_Table *disptab = buffer_display_table ();
15531 if (! disptab_matches_widthtab
15532 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15534 invalidate_region_cache (current_buffer,
15535 current_buffer->width_run_cache,
15536 BEG, Z);
15537 recompute_width_table (current_buffer, disptab);
15541 /* If window-start is screwed up, choose a new one. */
15542 if (XMARKER (w->start)->buffer != current_buffer)
15543 goto recenter;
15545 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15547 /* If someone specified a new starting point but did not insist,
15548 check whether it can be used. */
15549 if (w->optional_new_start
15550 && CHARPOS (startp) >= BEGV
15551 && CHARPOS (startp) <= ZV)
15553 w->optional_new_start = 0;
15554 start_display (&it, w, startp);
15555 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15556 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15557 if (IT_CHARPOS (it) == PT)
15558 w->force_start = 1;
15559 /* IT may overshoot PT if text at PT is invisible. */
15560 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15561 w->force_start = 1;
15564 force_start:
15566 /* Handle case where place to start displaying has been specified,
15567 unless the specified location is outside the accessible range. */
15568 if (w->force_start || window_frozen_p (w))
15570 /* We set this later on if we have to adjust point. */
15571 int new_vpos = -1;
15573 w->force_start = 0;
15574 w->vscroll = 0;
15575 w->window_end_valid = 0;
15577 /* Forget any recorded base line for line number display. */
15578 if (!buffer_unchanged_p)
15579 w->base_line_number = 0;
15581 /* Redisplay the mode line. Select the buffer properly for that.
15582 Also, run the hook window-scroll-functions
15583 because we have scrolled. */
15584 /* Note, we do this after clearing force_start because
15585 if there's an error, it is better to forget about force_start
15586 than to get into an infinite loop calling the hook functions
15587 and having them get more errors. */
15588 if (!update_mode_line
15589 || ! NILP (Vwindow_scroll_functions))
15591 update_mode_line = 1;
15592 w->update_mode_line = 1;
15593 startp = run_window_scroll_functions (window, startp);
15596 if (CHARPOS (startp) < BEGV)
15597 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15598 else if (CHARPOS (startp) > ZV)
15599 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15601 /* Redisplay, then check if cursor has been set during the
15602 redisplay. Give up if new fonts were loaded. */
15603 /* We used to issue a CHECK_MARGINS argument to try_window here,
15604 but this causes scrolling to fail when point begins inside
15605 the scroll margin (bug#148) -- cyd */
15606 if (!try_window (window, startp, 0))
15608 w->force_start = 1;
15609 clear_glyph_matrix (w->desired_matrix);
15610 goto need_larger_matrices;
15613 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15615 /* If point does not appear, try to move point so it does
15616 appear. The desired matrix has been built above, so we
15617 can use it here. */
15618 new_vpos = window_box_height (w) / 2;
15621 if (!cursor_row_fully_visible_p (w, 0, 0))
15623 /* Point does appear, but on a line partly visible at end of window.
15624 Move it back to a fully-visible line. */
15625 new_vpos = window_box_height (w);
15627 else if (w->cursor.vpos >=0)
15629 /* Some people insist on not letting point enter the scroll
15630 margin, even though this part handles windows that didn't
15631 scroll at all. */
15632 int window_total_lines
15633 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15634 int margin = min (scroll_margin, window_total_lines / 4);
15635 int pixel_margin = margin * frame_line_height;
15636 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15638 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15639 below, which finds the row to move point to, advances by
15640 the Y coordinate of the _next_ row, see the definition of
15641 MATRIX_ROW_BOTTOM_Y. */
15642 if (w->cursor.vpos < margin + header_line)
15644 w->cursor.vpos = -1;
15645 clear_glyph_matrix (w->desired_matrix);
15646 goto try_to_scroll;
15648 else
15650 int window_height = window_box_height (w);
15652 if (header_line)
15653 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15654 if (w->cursor.y >= window_height - pixel_margin)
15656 w->cursor.vpos = -1;
15657 clear_glyph_matrix (w->desired_matrix);
15658 goto try_to_scroll;
15663 /* If we need to move point for either of the above reasons,
15664 now actually do it. */
15665 if (new_vpos >= 0)
15667 struct glyph_row *row;
15669 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15670 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15671 ++row;
15673 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15674 MATRIX_ROW_START_BYTEPOS (row));
15676 if (w != XWINDOW (selected_window))
15677 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15678 else if (current_buffer == old)
15679 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15681 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15683 /* If we are highlighting the region, then we just changed
15684 the region, so redisplay to show it. */
15685 if (markpos_of_region () >= 0)
15687 clear_glyph_matrix (w->desired_matrix);
15688 if (!try_window (window, startp, 0))
15689 goto need_larger_matrices;
15693 #ifdef GLYPH_DEBUG
15694 debug_method_add (w, "forced window start");
15695 #endif
15696 goto done;
15699 /* Handle case where text has not changed, only point, and it has
15700 not moved off the frame, and we are not retrying after hscroll.
15701 (current_matrix_up_to_date_p is nonzero when retrying.) */
15702 if (current_matrix_up_to_date_p
15703 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15704 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15706 switch (rc)
15708 case CURSOR_MOVEMENT_SUCCESS:
15709 used_current_matrix_p = 1;
15710 goto done;
15712 case CURSOR_MOVEMENT_MUST_SCROLL:
15713 goto try_to_scroll;
15715 default:
15716 emacs_abort ();
15719 /* If current starting point was originally the beginning of a line
15720 but no longer is, find a new starting point. */
15721 else if (w->start_at_line_beg
15722 && !(CHARPOS (startp) <= BEGV
15723 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15725 #ifdef GLYPH_DEBUG
15726 debug_method_add (w, "recenter 1");
15727 #endif
15728 goto recenter;
15731 /* Try scrolling with try_window_id. Value is > 0 if update has
15732 been done, it is -1 if we know that the same window start will
15733 not work. It is 0 if unsuccessful for some other reason. */
15734 else if ((tem = try_window_id (w)) != 0)
15736 #ifdef GLYPH_DEBUG
15737 debug_method_add (w, "try_window_id %d", tem);
15738 #endif
15740 if (f->fonts_changed)
15741 goto need_larger_matrices;
15742 if (tem > 0)
15743 goto done;
15745 /* Otherwise try_window_id has returned -1 which means that we
15746 don't want the alternative below this comment to execute. */
15748 else if (CHARPOS (startp) >= BEGV
15749 && CHARPOS (startp) <= ZV
15750 && PT >= CHARPOS (startp)
15751 && (CHARPOS (startp) < ZV
15752 /* Avoid starting at end of buffer. */
15753 || CHARPOS (startp) == BEGV
15754 || !window_outdated (w)))
15756 int d1, d2, d3, d4, d5, d6;
15758 /* If first window line is a continuation line, and window start
15759 is inside the modified region, but the first change is before
15760 current window start, we must select a new window start.
15762 However, if this is the result of a down-mouse event (e.g. by
15763 extending the mouse-drag-overlay), we don't want to select a
15764 new window start, since that would change the position under
15765 the mouse, resulting in an unwanted mouse-movement rather
15766 than a simple mouse-click. */
15767 if (!w->start_at_line_beg
15768 && NILP (do_mouse_tracking)
15769 && CHARPOS (startp) > BEGV
15770 && CHARPOS (startp) > BEG + beg_unchanged
15771 && CHARPOS (startp) <= Z - end_unchanged
15772 /* Even if w->start_at_line_beg is nil, a new window may
15773 start at a line_beg, since that's how set_buffer_window
15774 sets it. So, we need to check the return value of
15775 compute_window_start_on_continuation_line. (See also
15776 bug#197). */
15777 && XMARKER (w->start)->buffer == current_buffer
15778 && compute_window_start_on_continuation_line (w)
15779 /* It doesn't make sense to force the window start like we
15780 do at label force_start if it is already known that point
15781 will not be visible in the resulting window, because
15782 doing so will move point from its correct position
15783 instead of scrolling the window to bring point into view.
15784 See bug#9324. */
15785 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15787 w->force_start = 1;
15788 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15789 goto force_start;
15792 #ifdef GLYPH_DEBUG
15793 debug_method_add (w, "same window start");
15794 #endif
15796 /* Try to redisplay starting at same place as before.
15797 If point has not moved off frame, accept the results. */
15798 if (!current_matrix_up_to_date_p
15799 /* Don't use try_window_reusing_current_matrix in this case
15800 because a window scroll function can have changed the
15801 buffer. */
15802 || !NILP (Vwindow_scroll_functions)
15803 || MINI_WINDOW_P (w)
15804 || !(used_current_matrix_p
15805 = try_window_reusing_current_matrix (w)))
15807 IF_DEBUG (debug_method_add (w, "1"));
15808 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15809 /* -1 means we need to scroll.
15810 0 means we need new matrices, but fonts_changed
15811 is set in that case, so we will detect it below. */
15812 goto try_to_scroll;
15815 if (f->fonts_changed)
15816 goto need_larger_matrices;
15818 if (w->cursor.vpos >= 0)
15820 if (!just_this_one_p
15821 || current_buffer->clip_changed
15822 || BEG_UNCHANGED < CHARPOS (startp))
15823 /* Forget any recorded base line for line number display. */
15824 w->base_line_number = 0;
15826 if (!cursor_row_fully_visible_p (w, 1, 0))
15828 clear_glyph_matrix (w->desired_matrix);
15829 last_line_misfit = 1;
15831 /* Drop through and scroll. */
15832 else
15833 goto done;
15835 else
15836 clear_glyph_matrix (w->desired_matrix);
15839 try_to_scroll:
15841 /* Redisplay the mode line. Select the buffer properly for that. */
15842 if (!update_mode_line)
15844 update_mode_line = 1;
15845 w->update_mode_line = 1;
15848 /* Try to scroll by specified few lines. */
15849 if ((scroll_conservatively
15850 || emacs_scroll_step
15851 || temp_scroll_step
15852 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15853 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15854 && CHARPOS (startp) >= BEGV
15855 && CHARPOS (startp) <= ZV)
15857 /* The function returns -1 if new fonts were loaded, 1 if
15858 successful, 0 if not successful. */
15859 int ss = try_scrolling (window, just_this_one_p,
15860 scroll_conservatively,
15861 emacs_scroll_step,
15862 temp_scroll_step, last_line_misfit);
15863 switch (ss)
15865 case SCROLLING_SUCCESS:
15866 goto done;
15868 case SCROLLING_NEED_LARGER_MATRICES:
15869 goto need_larger_matrices;
15871 case SCROLLING_FAILED:
15872 break;
15874 default:
15875 emacs_abort ();
15879 /* Finally, just choose a place to start which positions point
15880 according to user preferences. */
15882 recenter:
15884 #ifdef GLYPH_DEBUG
15885 debug_method_add (w, "recenter");
15886 #endif
15888 /* Forget any previously recorded base line for line number display. */
15889 if (!buffer_unchanged_p)
15890 w->base_line_number = 0;
15892 /* Determine the window start relative to point. */
15893 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15894 it.current_y = it.last_visible_y;
15895 if (centering_position < 0)
15897 int window_total_lines
15898 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15899 int margin =
15900 scroll_margin > 0
15901 ? min (scroll_margin, window_total_lines / 4)
15902 : 0;
15903 ptrdiff_t margin_pos = CHARPOS (startp);
15904 Lisp_Object aggressive;
15905 int scrolling_up;
15907 /* If there is a scroll margin at the top of the window, find
15908 its character position. */
15909 if (margin
15910 /* Cannot call start_display if startp is not in the
15911 accessible region of the buffer. This can happen when we
15912 have just switched to a different buffer and/or changed
15913 its restriction. In that case, startp is initialized to
15914 the character position 1 (BEGV) because we did not yet
15915 have chance to display the buffer even once. */
15916 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15918 struct it it1;
15919 void *it1data = NULL;
15921 SAVE_IT (it1, it, it1data);
15922 start_display (&it1, w, startp);
15923 move_it_vertically (&it1, margin * frame_line_height);
15924 margin_pos = IT_CHARPOS (it1);
15925 RESTORE_IT (&it, &it, it1data);
15927 scrolling_up = PT > margin_pos;
15928 aggressive =
15929 scrolling_up
15930 ? BVAR (current_buffer, scroll_up_aggressively)
15931 : BVAR (current_buffer, scroll_down_aggressively);
15933 if (!MINI_WINDOW_P (w)
15934 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15936 int pt_offset = 0;
15938 /* Setting scroll-conservatively overrides
15939 scroll-*-aggressively. */
15940 if (!scroll_conservatively && NUMBERP (aggressive))
15942 double float_amount = XFLOATINT (aggressive);
15944 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15945 if (pt_offset == 0 && float_amount > 0)
15946 pt_offset = 1;
15947 if (pt_offset && margin > 0)
15948 margin -= 1;
15950 /* Compute how much to move the window start backward from
15951 point so that point will be displayed where the user
15952 wants it. */
15953 if (scrolling_up)
15955 centering_position = it.last_visible_y;
15956 if (pt_offset)
15957 centering_position -= pt_offset;
15958 centering_position -=
15959 frame_line_height * (1 + margin + (last_line_misfit != 0))
15960 + WINDOW_HEADER_LINE_HEIGHT (w);
15961 /* Don't let point enter the scroll margin near top of
15962 the window. */
15963 if (centering_position < margin * frame_line_height)
15964 centering_position = margin * frame_line_height;
15966 else
15967 centering_position = margin * frame_line_height + pt_offset;
15969 else
15970 /* Set the window start half the height of the window backward
15971 from point. */
15972 centering_position = window_box_height (w) / 2;
15974 move_it_vertically_backward (&it, centering_position);
15976 eassert (IT_CHARPOS (it) >= BEGV);
15978 /* The function move_it_vertically_backward may move over more
15979 than the specified y-distance. If it->w is small, e.g. a
15980 mini-buffer window, we may end up in front of the window's
15981 display area. Start displaying at the start of the line
15982 containing PT in this case. */
15983 if (it.current_y <= 0)
15985 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15986 move_it_vertically_backward (&it, 0);
15987 it.current_y = 0;
15990 it.current_x = it.hpos = 0;
15992 /* Set the window start position here explicitly, to avoid an
15993 infinite loop in case the functions in window-scroll-functions
15994 get errors. */
15995 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15997 /* Run scroll hooks. */
15998 startp = run_window_scroll_functions (window, it.current.pos);
16000 /* Redisplay the window. */
16001 if (!current_matrix_up_to_date_p
16002 || windows_or_buffers_changed
16003 || f->cursor_type_changed
16004 /* Don't use try_window_reusing_current_matrix in this case
16005 because it can have changed the buffer. */
16006 || !NILP (Vwindow_scroll_functions)
16007 || !just_this_one_p
16008 || MINI_WINDOW_P (w)
16009 || !(used_current_matrix_p
16010 = try_window_reusing_current_matrix (w)))
16011 try_window (window, startp, 0);
16013 /* If new fonts have been loaded (due to fontsets), give up. We
16014 have to start a new redisplay since we need to re-adjust glyph
16015 matrices. */
16016 if (f->fonts_changed)
16017 goto need_larger_matrices;
16019 /* If cursor did not appear assume that the middle of the window is
16020 in the first line of the window. Do it again with the next line.
16021 (Imagine a window of height 100, displaying two lines of height
16022 60. Moving back 50 from it->last_visible_y will end in the first
16023 line.) */
16024 if (w->cursor.vpos < 0)
16026 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16028 clear_glyph_matrix (w->desired_matrix);
16029 move_it_by_lines (&it, 1);
16030 try_window (window, it.current.pos, 0);
16032 else if (PT < IT_CHARPOS (it))
16034 clear_glyph_matrix (w->desired_matrix);
16035 move_it_by_lines (&it, -1);
16036 try_window (window, it.current.pos, 0);
16038 else
16040 /* Not much we can do about it. */
16044 /* Consider the following case: Window starts at BEGV, there is
16045 invisible, intangible text at BEGV, so that display starts at
16046 some point START > BEGV. It can happen that we are called with
16047 PT somewhere between BEGV and START. Try to handle that case. */
16048 if (w->cursor.vpos < 0)
16050 struct glyph_row *row = w->current_matrix->rows;
16051 if (row->mode_line_p)
16052 ++row;
16053 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16056 if (!cursor_row_fully_visible_p (w, 0, 0))
16058 /* If vscroll is enabled, disable it and try again. */
16059 if (w->vscroll)
16061 w->vscroll = 0;
16062 clear_glyph_matrix (w->desired_matrix);
16063 goto recenter;
16066 /* Users who set scroll-conservatively to a large number want
16067 point just above/below the scroll margin. If we ended up
16068 with point's row partially visible, move the window start to
16069 make that row fully visible and out of the margin. */
16070 if (scroll_conservatively > SCROLL_LIMIT)
16072 int window_total_lines
16073 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16074 int margin =
16075 scroll_margin > 0
16076 ? min (scroll_margin, window_total_lines / 4)
16077 : 0;
16078 int move_down = w->cursor.vpos >= window_total_lines / 2;
16080 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16081 clear_glyph_matrix (w->desired_matrix);
16082 if (1 == try_window (window, it.current.pos,
16083 TRY_WINDOW_CHECK_MARGINS))
16084 goto done;
16087 /* If centering point failed to make the whole line visible,
16088 put point at the top instead. That has to make the whole line
16089 visible, if it can be done. */
16090 if (centering_position == 0)
16091 goto done;
16093 clear_glyph_matrix (w->desired_matrix);
16094 centering_position = 0;
16095 goto recenter;
16098 done:
16100 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16101 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16102 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16104 /* Display the mode line, if we must. */
16105 if ((update_mode_line
16106 /* If window not full width, must redo its mode line
16107 if (a) the window to its side is being redone and
16108 (b) we do a frame-based redisplay. This is a consequence
16109 of how inverted lines are drawn in frame-based redisplay. */
16110 || (!just_this_one_p
16111 && !FRAME_WINDOW_P (f)
16112 && !WINDOW_FULL_WIDTH_P (w))
16113 /* Line number to display. */
16114 || w->base_line_pos > 0
16115 /* Column number is displayed and different from the one displayed. */
16116 || (w->column_number_displayed != -1
16117 && (w->column_number_displayed != current_column ())))
16118 /* This means that the window has a mode line. */
16119 && (WINDOW_WANTS_MODELINE_P (w)
16120 || WINDOW_WANTS_HEADER_LINE_P (w)))
16122 display_mode_lines (w);
16124 /* If mode line height has changed, arrange for a thorough
16125 immediate redisplay using the correct mode line height. */
16126 if (WINDOW_WANTS_MODELINE_P (w)
16127 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16129 f->fonts_changed = 1;
16130 w->mode_line_height = -1;
16131 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16132 = DESIRED_MODE_LINE_HEIGHT (w);
16135 /* If header line height has changed, arrange for a thorough
16136 immediate redisplay using the correct header line height. */
16137 if (WINDOW_WANTS_HEADER_LINE_P (w)
16138 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16140 f->fonts_changed = 1;
16141 w->header_line_height = -1;
16142 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16143 = DESIRED_HEADER_LINE_HEIGHT (w);
16146 if (f->fonts_changed)
16147 goto need_larger_matrices;
16150 if (!line_number_displayed && w->base_line_pos != -1)
16152 w->base_line_pos = 0;
16153 w->base_line_number = 0;
16156 finish_menu_bars:
16158 /* When we reach a frame's selected window, redo the frame's menu bar. */
16159 if (update_mode_line
16160 && EQ (FRAME_SELECTED_WINDOW (f), window))
16162 int redisplay_menu_p = 0;
16164 if (FRAME_WINDOW_P (f))
16166 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16167 || defined (HAVE_NS) || defined (USE_GTK)
16168 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16169 #else
16170 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16171 #endif
16173 else
16174 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16176 if (redisplay_menu_p)
16177 display_menu_bar (w);
16179 #ifdef HAVE_WINDOW_SYSTEM
16180 if (FRAME_WINDOW_P (f))
16182 #if defined (USE_GTK) || defined (HAVE_NS)
16183 if (FRAME_EXTERNAL_TOOL_BAR (f))
16184 redisplay_tool_bar (f);
16185 #else
16186 if (WINDOWP (f->tool_bar_window)
16187 && (FRAME_TOOL_BAR_LINES (f) > 0
16188 || !NILP (Vauto_resize_tool_bars))
16189 && redisplay_tool_bar (f))
16190 ignore_mouse_drag_p = 1;
16191 #endif
16193 #endif
16196 #ifdef HAVE_WINDOW_SYSTEM
16197 if (FRAME_WINDOW_P (f)
16198 && update_window_fringes (w, (just_this_one_p
16199 || (!used_current_matrix_p && !overlay_arrow_seen)
16200 || w->pseudo_window_p)))
16202 update_begin (f);
16203 block_input ();
16204 if (draw_window_fringes (w, 1))
16205 x_draw_vertical_border (w);
16206 unblock_input ();
16207 update_end (f);
16209 #endif /* HAVE_WINDOW_SYSTEM */
16211 /* We go to this label, with fonts_changed set, if it is
16212 necessary to try again using larger glyph matrices.
16213 We have to redeem the scroll bar even in this case,
16214 because the loop in redisplay_internal expects that. */
16215 need_larger_matrices:
16217 finish_scroll_bars:
16219 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16221 /* Set the thumb's position and size. */
16222 set_vertical_scroll_bar (w);
16224 /* Note that we actually used the scroll bar attached to this
16225 window, so it shouldn't be deleted at the end of redisplay. */
16226 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16227 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16230 /* Restore current_buffer and value of point in it. The window
16231 update may have changed the buffer, so first make sure `opoint'
16232 is still valid (Bug#6177). */
16233 if (CHARPOS (opoint) < BEGV)
16234 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16235 else if (CHARPOS (opoint) > ZV)
16236 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16237 else
16238 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16240 set_buffer_internal_1 (old);
16241 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16242 shorter. This can be caused by log truncation in *Messages*. */
16243 if (CHARPOS (lpoint) <= ZV)
16244 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16246 unbind_to (count, Qnil);
16250 /* Build the complete desired matrix of WINDOW with a window start
16251 buffer position POS.
16253 Value is 1 if successful. It is zero if fonts were loaded during
16254 redisplay which makes re-adjusting glyph matrices necessary, and -1
16255 if point would appear in the scroll margins.
16256 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16257 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16258 set in FLAGS.) */
16261 try_window (Lisp_Object window, struct text_pos pos, int flags)
16263 struct window *w = XWINDOW (window);
16264 struct it it;
16265 struct glyph_row *last_text_row = NULL;
16266 struct frame *f = XFRAME (w->frame);
16267 int frame_line_height = default_line_pixel_height (w);
16269 /* Make POS the new window start. */
16270 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16272 /* Mark cursor position as unknown. No overlay arrow seen. */
16273 w->cursor.vpos = -1;
16274 overlay_arrow_seen = 0;
16276 /* Initialize iterator and info to start at POS. */
16277 start_display (&it, w, pos);
16279 /* Display all lines of W. */
16280 while (it.current_y < it.last_visible_y)
16282 if (display_line (&it))
16283 last_text_row = it.glyph_row - 1;
16284 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16285 return 0;
16288 /* Don't let the cursor end in the scroll margins. */
16289 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16290 && !MINI_WINDOW_P (w))
16292 int this_scroll_margin;
16293 int window_total_lines
16294 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16296 if (scroll_margin > 0)
16298 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16299 this_scroll_margin *= frame_line_height;
16301 else
16302 this_scroll_margin = 0;
16304 if ((w->cursor.y >= 0 /* not vscrolled */
16305 && w->cursor.y < this_scroll_margin
16306 && CHARPOS (pos) > BEGV
16307 && IT_CHARPOS (it) < ZV)
16308 /* rms: considering make_cursor_line_fully_visible_p here
16309 seems to give wrong results. We don't want to recenter
16310 when the last line is partly visible, we want to allow
16311 that case to be handled in the usual way. */
16312 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16314 w->cursor.vpos = -1;
16315 clear_glyph_matrix (w->desired_matrix);
16316 return -1;
16320 /* If bottom moved off end of frame, change mode line percentage. */
16321 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16322 w->update_mode_line = 1;
16324 /* Set window_end_pos to the offset of the last character displayed
16325 on the window from the end of current_buffer. Set
16326 window_end_vpos to its row number. */
16327 if (last_text_row)
16329 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16330 adjust_window_ends (w, last_text_row, 0);
16331 eassert
16332 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16333 w->window_end_vpos)));
16335 else
16337 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16338 w->window_end_pos = Z - ZV;
16339 w->window_end_vpos = 0;
16342 /* But that is not valid info until redisplay finishes. */
16343 w->window_end_valid = 0;
16344 return 1;
16349 /************************************************************************
16350 Window redisplay reusing current matrix when buffer has not changed
16351 ************************************************************************/
16353 /* Try redisplay of window W showing an unchanged buffer with a
16354 different window start than the last time it was displayed by
16355 reusing its current matrix. Value is non-zero if successful.
16356 W->start is the new window start. */
16358 static int
16359 try_window_reusing_current_matrix (struct window *w)
16361 struct frame *f = XFRAME (w->frame);
16362 struct glyph_row *bottom_row;
16363 struct it it;
16364 struct run run;
16365 struct text_pos start, new_start;
16366 int nrows_scrolled, i;
16367 struct glyph_row *last_text_row;
16368 struct glyph_row *last_reused_text_row;
16369 struct glyph_row *start_row;
16370 int start_vpos, min_y, max_y;
16372 #ifdef GLYPH_DEBUG
16373 if (inhibit_try_window_reusing)
16374 return 0;
16375 #endif
16377 if (/* This function doesn't handle terminal frames. */
16378 !FRAME_WINDOW_P (f)
16379 /* Don't try to reuse the display if windows have been split
16380 or such. */
16381 || windows_or_buffers_changed
16382 || f->cursor_type_changed)
16383 return 0;
16385 /* Can't do this if region may have changed. */
16386 if (markpos_of_region () >= 0
16387 || w->region_showing
16388 || !NILP (Vshow_trailing_whitespace))
16389 return 0;
16391 /* If top-line visibility has changed, give up. */
16392 if (WINDOW_WANTS_HEADER_LINE_P (w)
16393 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16394 return 0;
16396 /* Give up if old or new display is scrolled vertically. We could
16397 make this function handle this, but right now it doesn't. */
16398 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16399 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16400 return 0;
16402 /* The variable new_start now holds the new window start. The old
16403 start `start' can be determined from the current matrix. */
16404 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16405 start = start_row->minpos;
16406 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16408 /* Clear the desired matrix for the display below. */
16409 clear_glyph_matrix (w->desired_matrix);
16411 if (CHARPOS (new_start) <= CHARPOS (start))
16413 /* Don't use this method if the display starts with an ellipsis
16414 displayed for invisible text. It's not easy to handle that case
16415 below, and it's certainly not worth the effort since this is
16416 not a frequent case. */
16417 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16418 return 0;
16420 IF_DEBUG (debug_method_add (w, "twu1"));
16422 /* Display up to a row that can be reused. The variable
16423 last_text_row is set to the last row displayed that displays
16424 text. Note that it.vpos == 0 if or if not there is a
16425 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16426 start_display (&it, w, new_start);
16427 w->cursor.vpos = -1;
16428 last_text_row = last_reused_text_row = NULL;
16430 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16432 /* If we have reached into the characters in the START row,
16433 that means the line boundaries have changed. So we
16434 can't start copying with the row START. Maybe it will
16435 work to start copying with the following row. */
16436 while (IT_CHARPOS (it) > CHARPOS (start))
16438 /* Advance to the next row as the "start". */
16439 start_row++;
16440 start = start_row->minpos;
16441 /* If there are no more rows to try, or just one, give up. */
16442 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16443 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16444 || CHARPOS (start) == ZV)
16446 clear_glyph_matrix (w->desired_matrix);
16447 return 0;
16450 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16452 /* If we have reached alignment, we can copy the rest of the
16453 rows. */
16454 if (IT_CHARPOS (it) == CHARPOS (start)
16455 /* Don't accept "alignment" inside a display vector,
16456 since start_row could have started in the middle of
16457 that same display vector (thus their character
16458 positions match), and we have no way of telling if
16459 that is the case. */
16460 && it.current.dpvec_index < 0)
16461 break;
16463 if (display_line (&it))
16464 last_text_row = it.glyph_row - 1;
16468 /* A value of current_y < last_visible_y means that we stopped
16469 at the previous window start, which in turn means that we
16470 have at least one reusable row. */
16471 if (it.current_y < it.last_visible_y)
16473 struct glyph_row *row;
16475 /* IT.vpos always starts from 0; it counts text lines. */
16476 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16478 /* Find PT if not already found in the lines displayed. */
16479 if (w->cursor.vpos < 0)
16481 int dy = it.current_y - start_row->y;
16483 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16484 row = row_containing_pos (w, PT, row, NULL, dy);
16485 if (row)
16486 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16487 dy, nrows_scrolled);
16488 else
16490 clear_glyph_matrix (w->desired_matrix);
16491 return 0;
16495 /* Scroll the display. Do it before the current matrix is
16496 changed. The problem here is that update has not yet
16497 run, i.e. part of the current matrix is not up to date.
16498 scroll_run_hook will clear the cursor, and use the
16499 current matrix to get the height of the row the cursor is
16500 in. */
16501 run.current_y = start_row->y;
16502 run.desired_y = it.current_y;
16503 run.height = it.last_visible_y - it.current_y;
16505 if (run.height > 0 && run.current_y != run.desired_y)
16507 update_begin (f);
16508 FRAME_RIF (f)->update_window_begin_hook (w);
16509 FRAME_RIF (f)->clear_window_mouse_face (w);
16510 FRAME_RIF (f)->scroll_run_hook (w, &run);
16511 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16512 update_end (f);
16515 /* Shift current matrix down by nrows_scrolled lines. */
16516 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16517 rotate_matrix (w->current_matrix,
16518 start_vpos,
16519 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16520 nrows_scrolled);
16522 /* Disable lines that must be updated. */
16523 for (i = 0; i < nrows_scrolled; ++i)
16524 (start_row + i)->enabled_p = 0;
16526 /* Re-compute Y positions. */
16527 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16528 max_y = it.last_visible_y;
16529 for (row = start_row + nrows_scrolled;
16530 row < bottom_row;
16531 ++row)
16533 row->y = it.current_y;
16534 row->visible_height = row->height;
16536 if (row->y < min_y)
16537 row->visible_height -= min_y - row->y;
16538 if (row->y + row->height > max_y)
16539 row->visible_height -= row->y + row->height - max_y;
16540 if (row->fringe_bitmap_periodic_p)
16541 row->redraw_fringe_bitmaps_p = 1;
16543 it.current_y += row->height;
16545 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16546 last_reused_text_row = row;
16547 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16548 break;
16551 /* Disable lines in the current matrix which are now
16552 below the window. */
16553 for (++row; row < bottom_row; ++row)
16554 row->enabled_p = row->mode_line_p = 0;
16557 /* Update window_end_pos etc.; last_reused_text_row is the last
16558 reused row from the current matrix containing text, if any.
16559 The value of last_text_row is the last displayed line
16560 containing text. */
16561 if (last_reused_text_row)
16562 adjust_window_ends (w, last_reused_text_row, 1);
16563 else if (last_text_row)
16564 adjust_window_ends (w, last_text_row, 0);
16565 else
16567 /* This window must be completely empty. */
16568 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16569 w->window_end_pos = Z - ZV;
16570 w->window_end_vpos = 0;
16572 w->window_end_valid = 0;
16574 /* Update hint: don't try scrolling again in update_window. */
16575 w->desired_matrix->no_scrolling_p = 1;
16577 #ifdef GLYPH_DEBUG
16578 debug_method_add (w, "try_window_reusing_current_matrix 1");
16579 #endif
16580 return 1;
16582 else if (CHARPOS (new_start) > CHARPOS (start))
16584 struct glyph_row *pt_row, *row;
16585 struct glyph_row *first_reusable_row;
16586 struct glyph_row *first_row_to_display;
16587 int dy;
16588 int yb = window_text_bottom_y (w);
16590 /* Find the row starting at new_start, if there is one. Don't
16591 reuse a partially visible line at the end. */
16592 first_reusable_row = start_row;
16593 while (first_reusable_row->enabled_p
16594 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16595 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16596 < CHARPOS (new_start)))
16597 ++first_reusable_row;
16599 /* Give up if there is no row to reuse. */
16600 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16601 || !first_reusable_row->enabled_p
16602 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16603 != CHARPOS (new_start)))
16604 return 0;
16606 /* We can reuse fully visible rows beginning with
16607 first_reusable_row to the end of the window. Set
16608 first_row_to_display to the first row that cannot be reused.
16609 Set pt_row to the row containing point, if there is any. */
16610 pt_row = NULL;
16611 for (first_row_to_display = first_reusable_row;
16612 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16613 ++first_row_to_display)
16615 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16616 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16617 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16618 && first_row_to_display->ends_at_zv_p
16619 && pt_row == NULL)))
16620 pt_row = first_row_to_display;
16623 /* Start displaying at the start of first_row_to_display. */
16624 eassert (first_row_to_display->y < yb);
16625 init_to_row_start (&it, w, first_row_to_display);
16627 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16628 - start_vpos);
16629 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16630 - nrows_scrolled);
16631 it.current_y = (first_row_to_display->y - first_reusable_row->y
16632 + WINDOW_HEADER_LINE_HEIGHT (w));
16634 /* Display lines beginning with first_row_to_display in the
16635 desired matrix. Set last_text_row to the last row displayed
16636 that displays text. */
16637 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16638 if (pt_row == NULL)
16639 w->cursor.vpos = -1;
16640 last_text_row = NULL;
16641 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16642 if (display_line (&it))
16643 last_text_row = it.glyph_row - 1;
16645 /* If point is in a reused row, adjust y and vpos of the cursor
16646 position. */
16647 if (pt_row)
16649 w->cursor.vpos -= nrows_scrolled;
16650 w->cursor.y -= first_reusable_row->y - start_row->y;
16653 /* Give up if point isn't in a row displayed or reused. (This
16654 also handles the case where w->cursor.vpos < nrows_scrolled
16655 after the calls to display_line, which can happen with scroll
16656 margins. See bug#1295.) */
16657 if (w->cursor.vpos < 0)
16659 clear_glyph_matrix (w->desired_matrix);
16660 return 0;
16663 /* Scroll the display. */
16664 run.current_y = first_reusable_row->y;
16665 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16666 run.height = it.last_visible_y - run.current_y;
16667 dy = run.current_y - run.desired_y;
16669 if (run.height)
16671 update_begin (f);
16672 FRAME_RIF (f)->update_window_begin_hook (w);
16673 FRAME_RIF (f)->clear_window_mouse_face (w);
16674 FRAME_RIF (f)->scroll_run_hook (w, &run);
16675 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16676 update_end (f);
16679 /* Adjust Y positions of reused rows. */
16680 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16681 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16682 max_y = it.last_visible_y;
16683 for (row = first_reusable_row; row < first_row_to_display; ++row)
16685 row->y -= dy;
16686 row->visible_height = row->height;
16687 if (row->y < min_y)
16688 row->visible_height -= min_y - row->y;
16689 if (row->y + row->height > max_y)
16690 row->visible_height -= row->y + row->height - max_y;
16691 if (row->fringe_bitmap_periodic_p)
16692 row->redraw_fringe_bitmaps_p = 1;
16695 /* Scroll the current matrix. */
16696 eassert (nrows_scrolled > 0);
16697 rotate_matrix (w->current_matrix,
16698 start_vpos,
16699 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16700 -nrows_scrolled);
16702 /* Disable rows not reused. */
16703 for (row -= nrows_scrolled; row < bottom_row; ++row)
16704 row->enabled_p = 0;
16706 /* Point may have moved to a different line, so we cannot assume that
16707 the previous cursor position is valid; locate the correct row. */
16708 if (pt_row)
16710 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16711 row < bottom_row
16712 && PT >= MATRIX_ROW_END_CHARPOS (row)
16713 && !row->ends_at_zv_p;
16714 row++)
16716 w->cursor.vpos++;
16717 w->cursor.y = row->y;
16719 if (row < bottom_row)
16721 /* Can't simply scan the row for point with
16722 bidi-reordered glyph rows. Let set_cursor_from_row
16723 figure out where to put the cursor, and if it fails,
16724 give up. */
16725 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16727 if (!set_cursor_from_row (w, row, w->current_matrix,
16728 0, 0, 0, 0))
16730 clear_glyph_matrix (w->desired_matrix);
16731 return 0;
16734 else
16736 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16737 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16739 for (; glyph < end
16740 && (!BUFFERP (glyph->object)
16741 || glyph->charpos < PT);
16742 glyph++)
16744 w->cursor.hpos++;
16745 w->cursor.x += glyph->pixel_width;
16751 /* Adjust window end. A null value of last_text_row means that
16752 the window end is in reused rows which in turn means that
16753 only its vpos can have changed. */
16754 if (last_text_row)
16755 adjust_window_ends (w, last_text_row, 0);
16756 else
16757 w->window_end_vpos -= nrows_scrolled;
16759 w->window_end_valid = 0;
16760 w->desired_matrix->no_scrolling_p = 1;
16762 #ifdef GLYPH_DEBUG
16763 debug_method_add (w, "try_window_reusing_current_matrix 2");
16764 #endif
16765 return 1;
16768 return 0;
16773 /************************************************************************
16774 Window redisplay reusing current matrix when buffer has changed
16775 ************************************************************************/
16777 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16778 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16779 ptrdiff_t *, ptrdiff_t *);
16780 static struct glyph_row *
16781 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16782 struct glyph_row *);
16785 /* Return the last row in MATRIX displaying text. If row START is
16786 non-null, start searching with that row. IT gives the dimensions
16787 of the display. Value is null if matrix is empty; otherwise it is
16788 a pointer to the row found. */
16790 static struct glyph_row *
16791 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16792 struct glyph_row *start)
16794 struct glyph_row *row, *row_found;
16796 /* Set row_found to the last row in IT->w's current matrix
16797 displaying text. The loop looks funny but think of partially
16798 visible lines. */
16799 row_found = NULL;
16800 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16801 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16803 eassert (row->enabled_p);
16804 row_found = row;
16805 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16806 break;
16807 ++row;
16810 return row_found;
16814 /* Return the last row in the current matrix of W that is not affected
16815 by changes at the start of current_buffer that occurred since W's
16816 current matrix was built. Value is null if no such row exists.
16818 BEG_UNCHANGED us the number of characters unchanged at the start of
16819 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16820 first changed character in current_buffer. Characters at positions <
16821 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16822 when the current matrix was built. */
16824 static struct glyph_row *
16825 find_last_unchanged_at_beg_row (struct window *w)
16827 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16828 struct glyph_row *row;
16829 struct glyph_row *row_found = NULL;
16830 int yb = window_text_bottom_y (w);
16832 /* Find the last row displaying unchanged text. */
16833 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16834 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16835 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16836 ++row)
16838 if (/* If row ends before first_changed_pos, it is unchanged,
16839 except in some case. */
16840 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16841 /* When row ends in ZV and we write at ZV it is not
16842 unchanged. */
16843 && !row->ends_at_zv_p
16844 /* When first_changed_pos is the end of a continued line,
16845 row is not unchanged because it may be no longer
16846 continued. */
16847 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16848 && (row->continued_p
16849 || row->exact_window_width_line_p))
16850 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16851 needs to be recomputed, so don't consider this row as
16852 unchanged. This happens when the last line was
16853 bidi-reordered and was killed immediately before this
16854 redisplay cycle. In that case, ROW->end stores the
16855 buffer position of the first visual-order character of
16856 the killed text, which is now beyond ZV. */
16857 && CHARPOS (row->end.pos) <= ZV)
16858 row_found = row;
16860 /* Stop if last visible row. */
16861 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16862 break;
16865 return row_found;
16869 /* Find the first glyph row in the current matrix of W that is not
16870 affected by changes at the end of current_buffer since the
16871 time W's current matrix was built.
16873 Return in *DELTA the number of chars by which buffer positions in
16874 unchanged text at the end of current_buffer must be adjusted.
16876 Return in *DELTA_BYTES the corresponding number of bytes.
16878 Value is null if no such row exists, i.e. all rows are affected by
16879 changes. */
16881 static struct glyph_row *
16882 find_first_unchanged_at_end_row (struct window *w,
16883 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16885 struct glyph_row *row;
16886 struct glyph_row *row_found = NULL;
16888 *delta = *delta_bytes = 0;
16890 /* Display must not have been paused, otherwise the current matrix
16891 is not up to date. */
16892 eassert (w->window_end_valid);
16894 /* A value of window_end_pos >= END_UNCHANGED means that the window
16895 end is in the range of changed text. If so, there is no
16896 unchanged row at the end of W's current matrix. */
16897 if (w->window_end_pos >= END_UNCHANGED)
16898 return NULL;
16900 /* Set row to the last row in W's current matrix displaying text. */
16901 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16903 /* If matrix is entirely empty, no unchanged row exists. */
16904 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16906 /* The value of row is the last glyph row in the matrix having a
16907 meaningful buffer position in it. The end position of row
16908 corresponds to window_end_pos. This allows us to translate
16909 buffer positions in the current matrix to current buffer
16910 positions for characters not in changed text. */
16911 ptrdiff_t Z_old =
16912 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16913 ptrdiff_t Z_BYTE_old =
16914 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16915 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16916 struct glyph_row *first_text_row
16917 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16919 *delta = Z - Z_old;
16920 *delta_bytes = Z_BYTE - Z_BYTE_old;
16922 /* Set last_unchanged_pos to the buffer position of the last
16923 character in the buffer that has not been changed. Z is the
16924 index + 1 of the last character in current_buffer, i.e. by
16925 subtracting END_UNCHANGED we get the index of the last
16926 unchanged character, and we have to add BEG to get its buffer
16927 position. */
16928 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16929 last_unchanged_pos_old = last_unchanged_pos - *delta;
16931 /* Search backward from ROW for a row displaying a line that
16932 starts at a minimum position >= last_unchanged_pos_old. */
16933 for (; row > first_text_row; --row)
16935 /* This used to abort, but it can happen.
16936 It is ok to just stop the search instead here. KFS. */
16937 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16938 break;
16940 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16941 row_found = row;
16945 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16947 return row_found;
16951 /* Make sure that glyph rows in the current matrix of window W
16952 reference the same glyph memory as corresponding rows in the
16953 frame's frame matrix. This function is called after scrolling W's
16954 current matrix on a terminal frame in try_window_id and
16955 try_window_reusing_current_matrix. */
16957 static void
16958 sync_frame_with_window_matrix_rows (struct window *w)
16960 struct frame *f = XFRAME (w->frame);
16961 struct glyph_row *window_row, *window_row_end, *frame_row;
16963 /* Preconditions: W must be a leaf window and full-width. Its frame
16964 must have a frame matrix. */
16965 eassert (BUFFERP (w->contents));
16966 eassert (WINDOW_FULL_WIDTH_P (w));
16967 eassert (!FRAME_WINDOW_P (f));
16969 /* If W is a full-width window, glyph pointers in W's current matrix
16970 have, by definition, to be the same as glyph pointers in the
16971 corresponding frame matrix. Note that frame matrices have no
16972 marginal areas (see build_frame_matrix). */
16973 window_row = w->current_matrix->rows;
16974 window_row_end = window_row + w->current_matrix->nrows;
16975 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16976 while (window_row < window_row_end)
16978 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16979 struct glyph *end = window_row->glyphs[LAST_AREA];
16981 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16982 frame_row->glyphs[TEXT_AREA] = start;
16983 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16984 frame_row->glyphs[LAST_AREA] = end;
16986 /* Disable frame rows whose corresponding window rows have
16987 been disabled in try_window_id. */
16988 if (!window_row->enabled_p)
16989 frame_row->enabled_p = 0;
16991 ++window_row, ++frame_row;
16996 /* Find the glyph row in window W containing CHARPOS. Consider all
16997 rows between START and END (not inclusive). END null means search
16998 all rows to the end of the display area of W. Value is the row
16999 containing CHARPOS or null. */
17001 struct glyph_row *
17002 row_containing_pos (struct window *w, ptrdiff_t charpos,
17003 struct glyph_row *start, struct glyph_row *end, int dy)
17005 struct glyph_row *row = start;
17006 struct glyph_row *best_row = NULL;
17007 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17008 int last_y;
17010 /* If we happen to start on a header-line, skip that. */
17011 if (row->mode_line_p)
17012 ++row;
17014 if ((end && row >= end) || !row->enabled_p)
17015 return NULL;
17017 last_y = window_text_bottom_y (w) - dy;
17019 while (1)
17021 /* Give up if we have gone too far. */
17022 if (end && row >= end)
17023 return NULL;
17024 /* This formerly returned if they were equal.
17025 I think that both quantities are of a "last plus one" type;
17026 if so, when they are equal, the row is within the screen. -- rms. */
17027 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17028 return NULL;
17030 /* If it is in this row, return this row. */
17031 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17032 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17033 /* The end position of a row equals the start
17034 position of the next row. If CHARPOS is there, we
17035 would rather consider it displayed in the next
17036 line, except when this line ends in ZV. */
17037 && !row_for_charpos_p (row, charpos)))
17038 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17040 struct glyph *g;
17042 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17043 || (!best_row && !row->continued_p))
17044 return row;
17045 /* In bidi-reordered rows, there could be several rows whose
17046 edges surround CHARPOS, all of these rows belonging to
17047 the same continued line. We need to find the row which
17048 fits CHARPOS the best. */
17049 for (g = row->glyphs[TEXT_AREA];
17050 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17051 g++)
17053 if (!STRINGP (g->object))
17055 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17057 mindif = eabs (g->charpos - charpos);
17058 best_row = row;
17059 /* Exact match always wins. */
17060 if (mindif == 0)
17061 return best_row;
17066 else if (best_row && !row->continued_p)
17067 return best_row;
17068 ++row;
17073 /* Try to redisplay window W by reusing its existing display. W's
17074 current matrix must be up to date when this function is called,
17075 i.e. window_end_valid must be nonzero.
17077 Value is
17079 1 if display has been updated
17080 0 if otherwise unsuccessful
17081 -1 if redisplay with same window start is known not to succeed
17083 The following steps are performed:
17085 1. Find the last row in the current matrix of W that is not
17086 affected by changes at the start of current_buffer. If no such row
17087 is found, give up.
17089 2. Find the first row in W's current matrix that is not affected by
17090 changes at the end of current_buffer. Maybe there is no such row.
17092 3. Display lines beginning with the row + 1 found in step 1 to the
17093 row found in step 2 or, if step 2 didn't find a row, to the end of
17094 the window.
17096 4. If cursor is not known to appear on the window, give up.
17098 5. If display stopped at the row found in step 2, scroll the
17099 display and current matrix as needed.
17101 6. Maybe display some lines at the end of W, if we must. This can
17102 happen under various circumstances, like a partially visible line
17103 becoming fully visible, or because newly displayed lines are displayed
17104 in smaller font sizes.
17106 7. Update W's window end information. */
17108 static int
17109 try_window_id (struct window *w)
17111 struct frame *f = XFRAME (w->frame);
17112 struct glyph_matrix *current_matrix = w->current_matrix;
17113 struct glyph_matrix *desired_matrix = w->desired_matrix;
17114 struct glyph_row *last_unchanged_at_beg_row;
17115 struct glyph_row *first_unchanged_at_end_row;
17116 struct glyph_row *row;
17117 struct glyph_row *bottom_row;
17118 int bottom_vpos;
17119 struct it it;
17120 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17121 int dvpos, dy;
17122 struct text_pos start_pos;
17123 struct run run;
17124 int first_unchanged_at_end_vpos = 0;
17125 struct glyph_row *last_text_row, *last_text_row_at_end;
17126 struct text_pos start;
17127 ptrdiff_t first_changed_charpos, last_changed_charpos;
17129 #ifdef GLYPH_DEBUG
17130 if (inhibit_try_window_id)
17131 return 0;
17132 #endif
17134 /* This is handy for debugging. */
17135 #if 0
17136 #define GIVE_UP(X) \
17137 do { \
17138 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17139 return 0; \
17140 } while (0)
17141 #else
17142 #define GIVE_UP(X) return 0
17143 #endif
17145 SET_TEXT_POS_FROM_MARKER (start, w->start);
17147 /* Don't use this for mini-windows because these can show
17148 messages and mini-buffers, and we don't handle that here. */
17149 if (MINI_WINDOW_P (w))
17150 GIVE_UP (1);
17152 /* This flag is used to prevent redisplay optimizations. */
17153 if (windows_or_buffers_changed || f->cursor_type_changed)
17154 GIVE_UP (2);
17156 /* Verify that narrowing has not changed.
17157 Also verify that we were not told to prevent redisplay optimizations.
17158 It would be nice to further
17159 reduce the number of cases where this prevents try_window_id. */
17160 if (current_buffer->clip_changed
17161 || current_buffer->prevent_redisplay_optimizations_p)
17162 GIVE_UP (3);
17164 /* Window must either use window-based redisplay or be full width. */
17165 if (!FRAME_WINDOW_P (f)
17166 && (!FRAME_LINE_INS_DEL_OK (f)
17167 || !WINDOW_FULL_WIDTH_P (w)))
17168 GIVE_UP (4);
17170 /* Give up if point is known NOT to appear in W. */
17171 if (PT < CHARPOS (start))
17172 GIVE_UP (5);
17174 /* Another way to prevent redisplay optimizations. */
17175 if (w->last_modified == 0)
17176 GIVE_UP (6);
17178 /* Verify that window is not hscrolled. */
17179 if (w->hscroll != 0)
17180 GIVE_UP (7);
17182 /* Verify that display wasn't paused. */
17183 if (!w->window_end_valid)
17184 GIVE_UP (8);
17186 /* Can't use this if highlighting a region because a cursor movement
17187 will do more than just set the cursor. */
17188 if (markpos_of_region () >= 0)
17189 GIVE_UP (9);
17191 /* Likewise if highlighting trailing whitespace. */
17192 if (!NILP (Vshow_trailing_whitespace))
17193 GIVE_UP (11);
17195 /* Likewise if showing a region. */
17196 if (w->region_showing)
17197 GIVE_UP (10);
17199 /* Can't use this if overlay arrow position and/or string have
17200 changed. */
17201 if (overlay_arrows_changed_p ())
17202 GIVE_UP (12);
17204 /* When word-wrap is on, adding a space to the first word of a
17205 wrapped line can change the wrap position, altering the line
17206 above it. It might be worthwhile to handle this more
17207 intelligently, but for now just redisplay from scratch. */
17208 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17209 GIVE_UP (21);
17211 /* Under bidi reordering, adding or deleting a character in the
17212 beginning of a paragraph, before the first strong directional
17213 character, can change the base direction of the paragraph (unless
17214 the buffer specifies a fixed paragraph direction), which will
17215 require to redisplay the whole paragraph. It might be worthwhile
17216 to find the paragraph limits and widen the range of redisplayed
17217 lines to that, but for now just give up this optimization and
17218 redisplay from scratch. */
17219 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17220 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17221 GIVE_UP (22);
17223 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17224 only if buffer has really changed. The reason is that the gap is
17225 initially at Z for freshly visited files. The code below would
17226 set end_unchanged to 0 in that case. */
17227 if (MODIFF > SAVE_MODIFF
17228 /* This seems to happen sometimes after saving a buffer. */
17229 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17231 if (GPT - BEG < BEG_UNCHANGED)
17232 BEG_UNCHANGED = GPT - BEG;
17233 if (Z - GPT < END_UNCHANGED)
17234 END_UNCHANGED = Z - GPT;
17237 /* The position of the first and last character that has been changed. */
17238 first_changed_charpos = BEG + BEG_UNCHANGED;
17239 last_changed_charpos = Z - END_UNCHANGED;
17241 /* If window starts after a line end, and the last change is in
17242 front of that newline, then changes don't affect the display.
17243 This case happens with stealth-fontification. Note that although
17244 the display is unchanged, glyph positions in the matrix have to
17245 be adjusted, of course. */
17246 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17247 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17248 && ((last_changed_charpos < CHARPOS (start)
17249 && CHARPOS (start) == BEGV)
17250 || (last_changed_charpos < CHARPOS (start) - 1
17251 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17253 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17254 struct glyph_row *r0;
17256 /* Compute how many chars/bytes have been added to or removed
17257 from the buffer. */
17258 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17259 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17260 Z_delta = Z - Z_old;
17261 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17263 /* Give up if PT is not in the window. Note that it already has
17264 been checked at the start of try_window_id that PT is not in
17265 front of the window start. */
17266 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17267 GIVE_UP (13);
17269 /* If window start is unchanged, we can reuse the whole matrix
17270 as is, after adjusting glyph positions. No need to compute
17271 the window end again, since its offset from Z hasn't changed. */
17272 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17273 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17274 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17275 /* PT must not be in a partially visible line. */
17276 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17277 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17279 /* Adjust positions in the glyph matrix. */
17280 if (Z_delta || Z_delta_bytes)
17282 struct glyph_row *r1
17283 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17284 increment_matrix_positions (w->current_matrix,
17285 MATRIX_ROW_VPOS (r0, current_matrix),
17286 MATRIX_ROW_VPOS (r1, current_matrix),
17287 Z_delta, Z_delta_bytes);
17290 /* Set the cursor. */
17291 row = row_containing_pos (w, PT, r0, NULL, 0);
17292 if (row)
17293 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17294 else
17295 emacs_abort ();
17296 return 1;
17300 /* Handle the case that changes are all below what is displayed in
17301 the window, and that PT is in the window. This shortcut cannot
17302 be taken if ZV is visible in the window, and text has been added
17303 there that is visible in the window. */
17304 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17305 /* ZV is not visible in the window, or there are no
17306 changes at ZV, actually. */
17307 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17308 || first_changed_charpos == last_changed_charpos))
17310 struct glyph_row *r0;
17312 /* Give up if PT is not in the window. Note that it already has
17313 been checked at the start of try_window_id that PT is not in
17314 front of the window start. */
17315 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17316 GIVE_UP (14);
17318 /* If window start is unchanged, we can reuse the whole matrix
17319 as is, without changing glyph positions since no text has
17320 been added/removed in front of the window end. */
17321 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17322 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17323 /* PT must not be in a partially visible line. */
17324 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17325 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17327 /* We have to compute the window end anew since text
17328 could have been added/removed after it. */
17329 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17330 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17332 /* Set the cursor. */
17333 row = row_containing_pos (w, PT, r0, NULL, 0);
17334 if (row)
17335 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17336 else
17337 emacs_abort ();
17338 return 2;
17342 /* Give up if window start is in the changed area.
17344 The condition used to read
17346 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17348 but why that was tested escapes me at the moment. */
17349 if (CHARPOS (start) >= first_changed_charpos
17350 && CHARPOS (start) <= last_changed_charpos)
17351 GIVE_UP (15);
17353 /* Check that window start agrees with the start of the first glyph
17354 row in its current matrix. Check this after we know the window
17355 start is not in changed text, otherwise positions would not be
17356 comparable. */
17357 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17358 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17359 GIVE_UP (16);
17361 /* Give up if the window ends in strings. Overlay strings
17362 at the end are difficult to handle, so don't try. */
17363 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17364 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17365 GIVE_UP (20);
17367 /* Compute the position at which we have to start displaying new
17368 lines. Some of the lines at the top of the window might be
17369 reusable because they are not displaying changed text. Find the
17370 last row in W's current matrix not affected by changes at the
17371 start of current_buffer. Value is null if changes start in the
17372 first line of window. */
17373 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17374 if (last_unchanged_at_beg_row)
17376 /* Avoid starting to display in the middle of a character, a TAB
17377 for instance. This is easier than to set up the iterator
17378 exactly, and it's not a frequent case, so the additional
17379 effort wouldn't really pay off. */
17380 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17381 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17382 && last_unchanged_at_beg_row > w->current_matrix->rows)
17383 --last_unchanged_at_beg_row;
17385 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17386 GIVE_UP (17);
17388 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17389 GIVE_UP (18);
17390 start_pos = it.current.pos;
17392 /* Start displaying new lines in the desired matrix at the same
17393 vpos we would use in the current matrix, i.e. below
17394 last_unchanged_at_beg_row. */
17395 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17396 current_matrix);
17397 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17398 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17400 eassert (it.hpos == 0 && it.current_x == 0);
17402 else
17404 /* There are no reusable lines at the start of the window.
17405 Start displaying in the first text line. */
17406 start_display (&it, w, start);
17407 it.vpos = it.first_vpos;
17408 start_pos = it.current.pos;
17411 /* Find the first row that is not affected by changes at the end of
17412 the buffer. Value will be null if there is no unchanged row, in
17413 which case we must redisplay to the end of the window. delta
17414 will be set to the value by which buffer positions beginning with
17415 first_unchanged_at_end_row have to be adjusted due to text
17416 changes. */
17417 first_unchanged_at_end_row
17418 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17419 IF_DEBUG (debug_delta = delta);
17420 IF_DEBUG (debug_delta_bytes = delta_bytes);
17422 /* Set stop_pos to the buffer position up to which we will have to
17423 display new lines. If first_unchanged_at_end_row != NULL, this
17424 is the buffer position of the start of the line displayed in that
17425 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17426 that we don't stop at a buffer position. */
17427 stop_pos = 0;
17428 if (first_unchanged_at_end_row)
17430 eassert (last_unchanged_at_beg_row == NULL
17431 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17433 /* If this is a continuation line, move forward to the next one
17434 that isn't. Changes in lines above affect this line.
17435 Caution: this may move first_unchanged_at_end_row to a row
17436 not displaying text. */
17437 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17438 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17439 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17440 < it.last_visible_y))
17441 ++first_unchanged_at_end_row;
17443 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17444 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17445 >= it.last_visible_y))
17446 first_unchanged_at_end_row = NULL;
17447 else
17449 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17450 + delta);
17451 first_unchanged_at_end_vpos
17452 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17453 eassert (stop_pos >= Z - END_UNCHANGED);
17456 else if (last_unchanged_at_beg_row == NULL)
17457 GIVE_UP (19);
17460 #ifdef GLYPH_DEBUG
17462 /* Either there is no unchanged row at the end, or the one we have
17463 now displays text. This is a necessary condition for the window
17464 end pos calculation at the end of this function. */
17465 eassert (first_unchanged_at_end_row == NULL
17466 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17468 debug_last_unchanged_at_beg_vpos
17469 = (last_unchanged_at_beg_row
17470 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17471 : -1);
17472 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17474 #endif /* GLYPH_DEBUG */
17477 /* Display new lines. Set last_text_row to the last new line
17478 displayed which has text on it, i.e. might end up as being the
17479 line where the window_end_vpos is. */
17480 w->cursor.vpos = -1;
17481 last_text_row = NULL;
17482 overlay_arrow_seen = 0;
17483 while (it.current_y < it.last_visible_y
17484 && !f->fonts_changed
17485 && (first_unchanged_at_end_row == NULL
17486 || IT_CHARPOS (it) < stop_pos))
17488 if (display_line (&it))
17489 last_text_row = it.glyph_row - 1;
17492 if (f->fonts_changed)
17493 return -1;
17496 /* Compute differences in buffer positions, y-positions etc. for
17497 lines reused at the bottom of the window. Compute what we can
17498 scroll. */
17499 if (first_unchanged_at_end_row
17500 /* No lines reused because we displayed everything up to the
17501 bottom of the window. */
17502 && it.current_y < it.last_visible_y)
17504 dvpos = (it.vpos
17505 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17506 current_matrix));
17507 dy = it.current_y - first_unchanged_at_end_row->y;
17508 run.current_y = first_unchanged_at_end_row->y;
17509 run.desired_y = run.current_y + dy;
17510 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17512 else
17514 delta = delta_bytes = dvpos = dy
17515 = run.current_y = run.desired_y = run.height = 0;
17516 first_unchanged_at_end_row = NULL;
17518 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17521 /* Find the cursor if not already found. We have to decide whether
17522 PT will appear on this window (it sometimes doesn't, but this is
17523 not a very frequent case.) This decision has to be made before
17524 the current matrix is altered. A value of cursor.vpos < 0 means
17525 that PT is either in one of the lines beginning at
17526 first_unchanged_at_end_row or below the window. Don't care for
17527 lines that might be displayed later at the window end; as
17528 mentioned, this is not a frequent case. */
17529 if (w->cursor.vpos < 0)
17531 /* Cursor in unchanged rows at the top? */
17532 if (PT < CHARPOS (start_pos)
17533 && last_unchanged_at_beg_row)
17535 row = row_containing_pos (w, PT,
17536 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17537 last_unchanged_at_beg_row + 1, 0);
17538 if (row)
17539 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17542 /* Start from first_unchanged_at_end_row looking for PT. */
17543 else if (first_unchanged_at_end_row)
17545 row = row_containing_pos (w, PT - delta,
17546 first_unchanged_at_end_row, NULL, 0);
17547 if (row)
17548 set_cursor_from_row (w, row, w->current_matrix, delta,
17549 delta_bytes, dy, dvpos);
17552 /* Give up if cursor was not found. */
17553 if (w->cursor.vpos < 0)
17555 clear_glyph_matrix (w->desired_matrix);
17556 return -1;
17560 /* Don't let the cursor end in the scroll margins. */
17562 int this_scroll_margin, cursor_height;
17563 int frame_line_height = default_line_pixel_height (w);
17564 int window_total_lines
17565 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17567 this_scroll_margin =
17568 max (0, min (scroll_margin, window_total_lines / 4));
17569 this_scroll_margin *= frame_line_height;
17570 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17572 if ((w->cursor.y < this_scroll_margin
17573 && CHARPOS (start) > BEGV)
17574 /* Old redisplay didn't take scroll margin into account at the bottom,
17575 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17576 || (w->cursor.y + (make_cursor_line_fully_visible_p
17577 ? cursor_height + this_scroll_margin
17578 : 1)) > it.last_visible_y)
17580 w->cursor.vpos = -1;
17581 clear_glyph_matrix (w->desired_matrix);
17582 return -1;
17586 /* Scroll the display. Do it before changing the current matrix so
17587 that xterm.c doesn't get confused about where the cursor glyph is
17588 found. */
17589 if (dy && run.height)
17591 update_begin (f);
17593 if (FRAME_WINDOW_P (f))
17595 FRAME_RIF (f)->update_window_begin_hook (w);
17596 FRAME_RIF (f)->clear_window_mouse_face (w);
17597 FRAME_RIF (f)->scroll_run_hook (w, &run);
17598 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17600 else
17602 /* Terminal frame. In this case, dvpos gives the number of
17603 lines to scroll by; dvpos < 0 means scroll up. */
17604 int from_vpos
17605 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17606 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17607 int end = (WINDOW_TOP_EDGE_LINE (w)
17608 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17609 + window_internal_height (w));
17611 #if defined (HAVE_GPM) || defined (MSDOS)
17612 x_clear_window_mouse_face (w);
17613 #endif
17614 /* Perform the operation on the screen. */
17615 if (dvpos > 0)
17617 /* Scroll last_unchanged_at_beg_row to the end of the
17618 window down dvpos lines. */
17619 set_terminal_window (f, end);
17621 /* On dumb terminals delete dvpos lines at the end
17622 before inserting dvpos empty lines. */
17623 if (!FRAME_SCROLL_REGION_OK (f))
17624 ins_del_lines (f, end - dvpos, -dvpos);
17626 /* Insert dvpos empty lines in front of
17627 last_unchanged_at_beg_row. */
17628 ins_del_lines (f, from, dvpos);
17630 else if (dvpos < 0)
17632 /* Scroll up last_unchanged_at_beg_vpos to the end of
17633 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17634 set_terminal_window (f, end);
17636 /* Delete dvpos lines in front of
17637 last_unchanged_at_beg_vpos. ins_del_lines will set
17638 the cursor to the given vpos and emit |dvpos| delete
17639 line sequences. */
17640 ins_del_lines (f, from + dvpos, dvpos);
17642 /* On a dumb terminal insert dvpos empty lines at the
17643 end. */
17644 if (!FRAME_SCROLL_REGION_OK (f))
17645 ins_del_lines (f, end + dvpos, -dvpos);
17648 set_terminal_window (f, 0);
17651 update_end (f);
17654 /* Shift reused rows of the current matrix to the right position.
17655 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17656 text. */
17657 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17658 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17659 if (dvpos < 0)
17661 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17662 bottom_vpos, dvpos);
17663 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17664 bottom_vpos);
17666 else if (dvpos > 0)
17668 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17669 bottom_vpos, dvpos);
17670 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17671 first_unchanged_at_end_vpos + dvpos);
17674 /* For frame-based redisplay, make sure that current frame and window
17675 matrix are in sync with respect to glyph memory. */
17676 if (!FRAME_WINDOW_P (f))
17677 sync_frame_with_window_matrix_rows (w);
17679 /* Adjust buffer positions in reused rows. */
17680 if (delta || delta_bytes)
17681 increment_matrix_positions (current_matrix,
17682 first_unchanged_at_end_vpos + dvpos,
17683 bottom_vpos, delta, delta_bytes);
17685 /* Adjust Y positions. */
17686 if (dy)
17687 shift_glyph_matrix (w, current_matrix,
17688 first_unchanged_at_end_vpos + dvpos,
17689 bottom_vpos, dy);
17691 if (first_unchanged_at_end_row)
17693 first_unchanged_at_end_row += dvpos;
17694 if (first_unchanged_at_end_row->y >= it.last_visible_y
17695 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17696 first_unchanged_at_end_row = NULL;
17699 /* If scrolling up, there may be some lines to display at the end of
17700 the window. */
17701 last_text_row_at_end = NULL;
17702 if (dy < 0)
17704 /* Scrolling up can leave for example a partially visible line
17705 at the end of the window to be redisplayed. */
17706 /* Set last_row to the glyph row in the current matrix where the
17707 window end line is found. It has been moved up or down in
17708 the matrix by dvpos. */
17709 int last_vpos = w->window_end_vpos + dvpos;
17710 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17712 /* If last_row is the window end line, it should display text. */
17713 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17715 /* If window end line was partially visible before, begin
17716 displaying at that line. Otherwise begin displaying with the
17717 line following it. */
17718 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17720 init_to_row_start (&it, w, last_row);
17721 it.vpos = last_vpos;
17722 it.current_y = last_row->y;
17724 else
17726 init_to_row_end (&it, w, last_row);
17727 it.vpos = 1 + last_vpos;
17728 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17729 ++last_row;
17732 /* We may start in a continuation line. If so, we have to
17733 get the right continuation_lines_width and current_x. */
17734 it.continuation_lines_width = last_row->continuation_lines_width;
17735 it.hpos = it.current_x = 0;
17737 /* Display the rest of the lines at the window end. */
17738 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17739 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17741 /* Is it always sure that the display agrees with lines in
17742 the current matrix? I don't think so, so we mark rows
17743 displayed invalid in the current matrix by setting their
17744 enabled_p flag to zero. */
17745 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17746 if (display_line (&it))
17747 last_text_row_at_end = it.glyph_row - 1;
17751 /* Update window_end_pos and window_end_vpos. */
17752 if (first_unchanged_at_end_row && !last_text_row_at_end)
17754 /* Window end line if one of the preserved rows from the current
17755 matrix. Set row to the last row displaying text in current
17756 matrix starting at first_unchanged_at_end_row, after
17757 scrolling. */
17758 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17759 row = find_last_row_displaying_text (w->current_matrix, &it,
17760 first_unchanged_at_end_row);
17761 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17762 adjust_window_ends (w, row, 1);
17763 eassert (w->window_end_bytepos >= 0);
17764 IF_DEBUG (debug_method_add (w, "A"));
17766 else if (last_text_row_at_end)
17768 adjust_window_ends (w, last_text_row_at_end, 0);
17769 eassert (w->window_end_bytepos >= 0);
17770 IF_DEBUG (debug_method_add (w, "B"));
17772 else if (last_text_row)
17774 /* We have displayed either to the end of the window or at the
17775 end of the window, i.e. the last row with text is to be found
17776 in the desired matrix. */
17777 adjust_window_ends (w, last_text_row, 0);
17778 eassert (w->window_end_bytepos >= 0);
17780 else if (first_unchanged_at_end_row == NULL
17781 && last_text_row == NULL
17782 && last_text_row_at_end == NULL)
17784 /* Displayed to end of window, but no line containing text was
17785 displayed. Lines were deleted at the end of the window. */
17786 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17787 int vpos = w->window_end_vpos;
17788 struct glyph_row *current_row = current_matrix->rows + vpos;
17789 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17791 for (row = NULL;
17792 row == NULL && vpos >= first_vpos;
17793 --vpos, --current_row, --desired_row)
17795 if (desired_row->enabled_p)
17797 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17798 row = desired_row;
17800 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17801 row = current_row;
17804 eassert (row != NULL);
17805 w->window_end_vpos = vpos + 1;
17806 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17807 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17808 eassert (w->window_end_bytepos >= 0);
17809 IF_DEBUG (debug_method_add (w, "C"));
17811 else
17812 emacs_abort ();
17814 IF_DEBUG (debug_end_pos = w->window_end_pos;
17815 debug_end_vpos = w->window_end_vpos);
17817 /* Record that display has not been completed. */
17818 w->window_end_valid = 0;
17819 w->desired_matrix->no_scrolling_p = 1;
17820 return 3;
17822 #undef GIVE_UP
17827 /***********************************************************************
17828 More debugging support
17829 ***********************************************************************/
17831 #ifdef GLYPH_DEBUG
17833 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17834 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17835 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17838 /* Dump the contents of glyph matrix MATRIX on stderr.
17840 GLYPHS 0 means don't show glyph contents.
17841 GLYPHS 1 means show glyphs in short form
17842 GLYPHS > 1 means show glyphs in long form. */
17844 void
17845 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17847 int i;
17848 for (i = 0; i < matrix->nrows; ++i)
17849 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17853 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17854 the glyph row and area where the glyph comes from. */
17856 void
17857 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17859 if (glyph->type == CHAR_GLYPH
17860 || glyph->type == GLYPHLESS_GLYPH)
17862 fprintf (stderr,
17863 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17864 glyph - row->glyphs[TEXT_AREA],
17865 (glyph->type == CHAR_GLYPH
17866 ? 'C'
17867 : 'G'),
17868 glyph->charpos,
17869 (BUFFERP (glyph->object)
17870 ? 'B'
17871 : (STRINGP (glyph->object)
17872 ? 'S'
17873 : (INTEGERP (glyph->object)
17874 ? '0'
17875 : '-'))),
17876 glyph->pixel_width,
17877 glyph->u.ch,
17878 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17879 ? glyph->u.ch
17880 : '.'),
17881 glyph->face_id,
17882 glyph->left_box_line_p,
17883 glyph->right_box_line_p);
17885 else if (glyph->type == STRETCH_GLYPH)
17887 fprintf (stderr,
17888 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17889 glyph - row->glyphs[TEXT_AREA],
17890 'S',
17891 glyph->charpos,
17892 (BUFFERP (glyph->object)
17893 ? 'B'
17894 : (STRINGP (glyph->object)
17895 ? 'S'
17896 : (INTEGERP (glyph->object)
17897 ? '0'
17898 : '-'))),
17899 glyph->pixel_width,
17901 ' ',
17902 glyph->face_id,
17903 glyph->left_box_line_p,
17904 glyph->right_box_line_p);
17906 else if (glyph->type == IMAGE_GLYPH)
17908 fprintf (stderr,
17909 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17910 glyph - row->glyphs[TEXT_AREA],
17911 'I',
17912 glyph->charpos,
17913 (BUFFERP (glyph->object)
17914 ? 'B'
17915 : (STRINGP (glyph->object)
17916 ? 'S'
17917 : (INTEGERP (glyph->object)
17918 ? '0'
17919 : '-'))),
17920 glyph->pixel_width,
17921 glyph->u.img_id,
17922 '.',
17923 glyph->face_id,
17924 glyph->left_box_line_p,
17925 glyph->right_box_line_p);
17927 else if (glyph->type == COMPOSITE_GLYPH)
17929 fprintf (stderr,
17930 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17931 glyph - row->glyphs[TEXT_AREA],
17932 '+',
17933 glyph->charpos,
17934 (BUFFERP (glyph->object)
17935 ? 'B'
17936 : (STRINGP (glyph->object)
17937 ? 'S'
17938 : (INTEGERP (glyph->object)
17939 ? '0'
17940 : '-'))),
17941 glyph->pixel_width,
17942 glyph->u.cmp.id);
17943 if (glyph->u.cmp.automatic)
17944 fprintf (stderr,
17945 "[%d-%d]",
17946 glyph->slice.cmp.from, glyph->slice.cmp.to);
17947 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17948 glyph->face_id,
17949 glyph->left_box_line_p,
17950 glyph->right_box_line_p);
17955 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17956 GLYPHS 0 means don't show glyph contents.
17957 GLYPHS 1 means show glyphs in short form
17958 GLYPHS > 1 means show glyphs in long form. */
17960 void
17961 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17963 if (glyphs != 1)
17965 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17966 fprintf (stderr, "==============================================================================\n");
17968 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17969 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17970 vpos,
17971 MATRIX_ROW_START_CHARPOS (row),
17972 MATRIX_ROW_END_CHARPOS (row),
17973 row->used[TEXT_AREA],
17974 row->contains_overlapping_glyphs_p,
17975 row->enabled_p,
17976 row->truncated_on_left_p,
17977 row->truncated_on_right_p,
17978 row->continued_p,
17979 MATRIX_ROW_CONTINUATION_LINE_P (row),
17980 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17981 row->ends_at_zv_p,
17982 row->fill_line_p,
17983 row->ends_in_middle_of_char_p,
17984 row->starts_in_middle_of_char_p,
17985 row->mouse_face_p,
17986 row->x,
17987 row->y,
17988 row->pixel_width,
17989 row->height,
17990 row->visible_height,
17991 row->ascent,
17992 row->phys_ascent);
17993 /* The next 3 lines should align to "Start" in the header. */
17994 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17995 row->end.overlay_string_index,
17996 row->continuation_lines_width);
17997 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17998 CHARPOS (row->start.string_pos),
17999 CHARPOS (row->end.string_pos));
18000 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18001 row->end.dpvec_index);
18004 if (glyphs > 1)
18006 int area;
18008 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18010 struct glyph *glyph = row->glyphs[area];
18011 struct glyph *glyph_end = glyph + row->used[area];
18013 /* Glyph for a line end in text. */
18014 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18015 ++glyph_end;
18017 if (glyph < glyph_end)
18018 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18020 for (; glyph < glyph_end; ++glyph)
18021 dump_glyph (row, glyph, area);
18024 else if (glyphs == 1)
18026 int area;
18028 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18030 char *s = alloca (row->used[area] + 4);
18031 int i;
18033 for (i = 0; i < row->used[area]; ++i)
18035 struct glyph *glyph = row->glyphs[area] + i;
18036 if (i == row->used[area] - 1
18037 && area == TEXT_AREA
18038 && INTEGERP (glyph->object)
18039 && glyph->type == CHAR_GLYPH
18040 && glyph->u.ch == ' ')
18042 strcpy (&s[i], "[\\n]");
18043 i += 4;
18045 else if (glyph->type == CHAR_GLYPH
18046 && glyph->u.ch < 0x80
18047 && glyph->u.ch >= ' ')
18048 s[i] = glyph->u.ch;
18049 else
18050 s[i] = '.';
18053 s[i] = '\0';
18054 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18060 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18061 Sdump_glyph_matrix, 0, 1, "p",
18062 doc: /* Dump the current matrix of the selected window to stderr.
18063 Shows contents of glyph row structures. With non-nil
18064 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18065 glyphs in short form, otherwise show glyphs in long form. */)
18066 (Lisp_Object glyphs)
18068 struct window *w = XWINDOW (selected_window);
18069 struct buffer *buffer = XBUFFER (w->contents);
18071 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18072 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18073 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18074 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18075 fprintf (stderr, "=============================================\n");
18076 dump_glyph_matrix (w->current_matrix,
18077 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18078 return Qnil;
18082 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18083 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18084 (void)
18086 struct frame *f = XFRAME (selected_frame);
18087 dump_glyph_matrix (f->current_matrix, 1);
18088 return Qnil;
18092 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18093 doc: /* Dump glyph row ROW to stderr.
18094 GLYPH 0 means don't dump glyphs.
18095 GLYPH 1 means dump glyphs in short form.
18096 GLYPH > 1 or omitted means dump glyphs in long form. */)
18097 (Lisp_Object row, Lisp_Object glyphs)
18099 struct glyph_matrix *matrix;
18100 EMACS_INT vpos;
18102 CHECK_NUMBER (row);
18103 matrix = XWINDOW (selected_window)->current_matrix;
18104 vpos = XINT (row);
18105 if (vpos >= 0 && vpos < matrix->nrows)
18106 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18107 vpos,
18108 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18109 return Qnil;
18113 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18114 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18115 GLYPH 0 means don't dump glyphs.
18116 GLYPH 1 means dump glyphs in short form.
18117 GLYPH > 1 or omitted means dump glyphs in long form. */)
18118 (Lisp_Object row, Lisp_Object glyphs)
18120 struct frame *sf = SELECTED_FRAME ();
18121 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18122 EMACS_INT vpos;
18124 CHECK_NUMBER (row);
18125 vpos = XINT (row);
18126 if (vpos >= 0 && vpos < m->nrows)
18127 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18128 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18129 return Qnil;
18133 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18134 doc: /* Toggle tracing of redisplay.
18135 With ARG, turn tracing on if and only if ARG is positive. */)
18136 (Lisp_Object arg)
18138 if (NILP (arg))
18139 trace_redisplay_p = !trace_redisplay_p;
18140 else
18142 arg = Fprefix_numeric_value (arg);
18143 trace_redisplay_p = XINT (arg) > 0;
18146 return Qnil;
18150 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18151 doc: /* Like `format', but print result to stderr.
18152 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18153 (ptrdiff_t nargs, Lisp_Object *args)
18155 Lisp_Object s = Fformat (nargs, args);
18156 fprintf (stderr, "%s", SDATA (s));
18157 return Qnil;
18160 #endif /* GLYPH_DEBUG */
18164 /***********************************************************************
18165 Building Desired Matrix Rows
18166 ***********************************************************************/
18168 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18169 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18171 static struct glyph_row *
18172 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18174 struct frame *f = XFRAME (WINDOW_FRAME (w));
18175 struct buffer *buffer = XBUFFER (w->contents);
18176 struct buffer *old = current_buffer;
18177 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18178 int arrow_len = SCHARS (overlay_arrow_string);
18179 const unsigned char *arrow_end = arrow_string + arrow_len;
18180 const unsigned char *p;
18181 struct it it;
18182 bool multibyte_p;
18183 int n_glyphs_before;
18185 set_buffer_temp (buffer);
18186 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18187 it.glyph_row->used[TEXT_AREA] = 0;
18188 SET_TEXT_POS (it.position, 0, 0);
18190 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18191 p = arrow_string;
18192 while (p < arrow_end)
18194 Lisp_Object face, ilisp;
18196 /* Get the next character. */
18197 if (multibyte_p)
18198 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18199 else
18201 it.c = it.char_to_display = *p, it.len = 1;
18202 if (! ASCII_CHAR_P (it.c))
18203 it.char_to_display = BYTE8_TO_CHAR (it.c);
18205 p += it.len;
18207 /* Get its face. */
18208 ilisp = make_number (p - arrow_string);
18209 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18210 it.face_id = compute_char_face (f, it.char_to_display, face);
18212 /* Compute its width, get its glyphs. */
18213 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18214 SET_TEXT_POS (it.position, -1, -1);
18215 PRODUCE_GLYPHS (&it);
18217 /* If this character doesn't fit any more in the line, we have
18218 to remove some glyphs. */
18219 if (it.current_x > it.last_visible_x)
18221 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18222 break;
18226 set_buffer_temp (old);
18227 return it.glyph_row;
18231 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18232 glyphs to insert is determined by produce_special_glyphs. */
18234 static void
18235 insert_left_trunc_glyphs (struct it *it)
18237 struct it truncate_it;
18238 struct glyph *from, *end, *to, *toend;
18240 eassert (!FRAME_WINDOW_P (it->f)
18241 || (!it->glyph_row->reversed_p
18242 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18243 || (it->glyph_row->reversed_p
18244 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18246 /* Get the truncation glyphs. */
18247 truncate_it = *it;
18248 truncate_it.current_x = 0;
18249 truncate_it.face_id = DEFAULT_FACE_ID;
18250 truncate_it.glyph_row = &scratch_glyph_row;
18251 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18252 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18253 truncate_it.object = make_number (0);
18254 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18256 /* Overwrite glyphs from IT with truncation glyphs. */
18257 if (!it->glyph_row->reversed_p)
18259 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18261 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18262 end = from + tused;
18263 to = it->glyph_row->glyphs[TEXT_AREA];
18264 toend = to + it->glyph_row->used[TEXT_AREA];
18265 if (FRAME_WINDOW_P (it->f))
18267 /* On GUI frames, when variable-size fonts are displayed,
18268 the truncation glyphs may need more pixels than the row's
18269 glyphs they overwrite. We overwrite more glyphs to free
18270 enough screen real estate, and enlarge the stretch glyph
18271 on the right (see display_line), if there is one, to
18272 preserve the screen position of the truncation glyphs on
18273 the right. */
18274 int w = 0;
18275 struct glyph *g = to;
18276 short used;
18278 /* The first glyph could be partially visible, in which case
18279 it->glyph_row->x will be negative. But we want the left
18280 truncation glyphs to be aligned at the left margin of the
18281 window, so we override the x coordinate at which the row
18282 will begin. */
18283 it->glyph_row->x = 0;
18284 while (g < toend && w < it->truncation_pixel_width)
18286 w += g->pixel_width;
18287 ++g;
18289 if (g - to - tused > 0)
18291 memmove (to + tused, g, (toend - g) * sizeof(*g));
18292 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18294 used = it->glyph_row->used[TEXT_AREA];
18295 if (it->glyph_row->truncated_on_right_p
18296 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18297 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18298 == STRETCH_GLYPH)
18300 int extra = w - it->truncation_pixel_width;
18302 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18306 while (from < end)
18307 *to++ = *from++;
18309 /* There may be padding glyphs left over. Overwrite them too. */
18310 if (!FRAME_WINDOW_P (it->f))
18312 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18314 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18315 while (from < end)
18316 *to++ = *from++;
18320 if (to > toend)
18321 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18323 else
18325 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18327 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18328 that back to front. */
18329 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18330 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18331 toend = it->glyph_row->glyphs[TEXT_AREA];
18332 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18333 if (FRAME_WINDOW_P (it->f))
18335 int w = 0;
18336 struct glyph *g = to;
18338 while (g >= toend && w < it->truncation_pixel_width)
18340 w += g->pixel_width;
18341 --g;
18343 if (to - g - tused > 0)
18344 to = g + tused;
18345 if (it->glyph_row->truncated_on_right_p
18346 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18347 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18349 int extra = w - it->truncation_pixel_width;
18351 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18355 while (from >= end && to >= toend)
18356 *to-- = *from--;
18357 if (!FRAME_WINDOW_P (it->f))
18359 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18361 from =
18362 truncate_it.glyph_row->glyphs[TEXT_AREA]
18363 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18364 while (from >= end && to >= toend)
18365 *to-- = *from--;
18368 if (from >= end)
18370 /* Need to free some room before prepending additional
18371 glyphs. */
18372 int move_by = from - end + 1;
18373 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18374 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18376 for ( ; g >= g0; g--)
18377 g[move_by] = *g;
18378 while (from >= end)
18379 *to-- = *from--;
18380 it->glyph_row->used[TEXT_AREA] += move_by;
18385 /* Compute the hash code for ROW. */
18386 unsigned
18387 row_hash (struct glyph_row *row)
18389 int area, k;
18390 unsigned hashval = 0;
18392 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18393 for (k = 0; k < row->used[area]; ++k)
18394 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18395 + row->glyphs[area][k].u.val
18396 + row->glyphs[area][k].face_id
18397 + row->glyphs[area][k].padding_p
18398 + (row->glyphs[area][k].type << 2));
18400 return hashval;
18403 /* Compute the pixel height and width of IT->glyph_row.
18405 Most of the time, ascent and height of a display line will be equal
18406 to the max_ascent and max_height values of the display iterator
18407 structure. This is not the case if
18409 1. We hit ZV without displaying anything. In this case, max_ascent
18410 and max_height will be zero.
18412 2. We have some glyphs that don't contribute to the line height.
18413 (The glyph row flag contributes_to_line_height_p is for future
18414 pixmap extensions).
18416 The first case is easily covered by using default values because in
18417 these cases, the line height does not really matter, except that it
18418 must not be zero. */
18420 static void
18421 compute_line_metrics (struct it *it)
18423 struct glyph_row *row = it->glyph_row;
18425 if (FRAME_WINDOW_P (it->f))
18427 int i, min_y, max_y;
18429 /* The line may consist of one space only, that was added to
18430 place the cursor on it. If so, the row's height hasn't been
18431 computed yet. */
18432 if (row->height == 0)
18434 if (it->max_ascent + it->max_descent == 0)
18435 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18436 row->ascent = it->max_ascent;
18437 row->height = it->max_ascent + it->max_descent;
18438 row->phys_ascent = it->max_phys_ascent;
18439 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18440 row->extra_line_spacing = it->max_extra_line_spacing;
18443 /* Compute the width of this line. */
18444 row->pixel_width = row->x;
18445 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18446 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18448 eassert (row->pixel_width >= 0);
18449 eassert (row->ascent >= 0 && row->height > 0);
18451 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18452 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18454 /* If first line's physical ascent is larger than its logical
18455 ascent, use the physical ascent, and make the row taller.
18456 This makes accented characters fully visible. */
18457 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18458 && row->phys_ascent > row->ascent)
18460 row->height += row->phys_ascent - row->ascent;
18461 row->ascent = row->phys_ascent;
18464 /* Compute how much of the line is visible. */
18465 row->visible_height = row->height;
18467 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18468 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18470 if (row->y < min_y)
18471 row->visible_height -= min_y - row->y;
18472 if (row->y + row->height > max_y)
18473 row->visible_height -= row->y + row->height - max_y;
18475 else
18477 row->pixel_width = row->used[TEXT_AREA];
18478 if (row->continued_p)
18479 row->pixel_width -= it->continuation_pixel_width;
18480 else if (row->truncated_on_right_p)
18481 row->pixel_width -= it->truncation_pixel_width;
18482 row->ascent = row->phys_ascent = 0;
18483 row->height = row->phys_height = row->visible_height = 1;
18484 row->extra_line_spacing = 0;
18487 /* Compute a hash code for this row. */
18488 row->hash = row_hash (row);
18490 it->max_ascent = it->max_descent = 0;
18491 it->max_phys_ascent = it->max_phys_descent = 0;
18495 /* Append one space to the glyph row of iterator IT if doing a
18496 window-based redisplay. The space has the same face as
18497 IT->face_id. Value is non-zero if a space was added.
18499 This function is called to make sure that there is always one glyph
18500 at the end of a glyph row that the cursor can be set on under
18501 window-systems. (If there weren't such a glyph we would not know
18502 how wide and tall a box cursor should be displayed).
18504 At the same time this space let's a nicely handle clearing to the
18505 end of the line if the row ends in italic text. */
18507 static int
18508 append_space_for_newline (struct it *it, int default_face_p)
18510 if (FRAME_WINDOW_P (it->f))
18512 int n = it->glyph_row->used[TEXT_AREA];
18514 if (it->glyph_row->glyphs[TEXT_AREA] + n
18515 < it->glyph_row->glyphs[1 + TEXT_AREA])
18517 /* Save some values that must not be changed.
18518 Must save IT->c and IT->len because otherwise
18519 ITERATOR_AT_END_P wouldn't work anymore after
18520 append_space_for_newline has been called. */
18521 enum display_element_type saved_what = it->what;
18522 int saved_c = it->c, saved_len = it->len;
18523 int saved_char_to_display = it->char_to_display;
18524 int saved_x = it->current_x;
18525 int saved_face_id = it->face_id;
18526 int saved_box_end = it->end_of_box_run_p;
18527 struct text_pos saved_pos;
18528 Lisp_Object saved_object;
18529 struct face *face;
18531 saved_object = it->object;
18532 saved_pos = it->position;
18534 it->what = IT_CHARACTER;
18535 memset (&it->position, 0, sizeof it->position);
18536 it->object = make_number (0);
18537 it->c = it->char_to_display = ' ';
18538 it->len = 1;
18540 /* If the default face was remapped, be sure to use the
18541 remapped face for the appended newline. */
18542 if (default_face_p)
18543 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18544 else if (it->face_before_selective_p)
18545 it->face_id = it->saved_face_id;
18546 face = FACE_FROM_ID (it->f, it->face_id);
18547 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18548 /* In R2L rows, we will prepend a stretch glyph that will
18549 have the end_of_box_run_p flag set for it, so there's no
18550 need for the appended newline glyph to have that flag
18551 set. */
18552 if (it->glyph_row->reversed_p
18553 /* But if the appended newline glyph goes all the way to
18554 the end of the row, there will be no stretch glyph,
18555 so leave the box flag set. */
18556 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18557 it->end_of_box_run_p = 0;
18559 PRODUCE_GLYPHS (it);
18561 it->override_ascent = -1;
18562 it->constrain_row_ascent_descent_p = 0;
18563 it->current_x = saved_x;
18564 it->object = saved_object;
18565 it->position = saved_pos;
18566 it->what = saved_what;
18567 it->face_id = saved_face_id;
18568 it->len = saved_len;
18569 it->c = saved_c;
18570 it->char_to_display = saved_char_to_display;
18571 it->end_of_box_run_p = saved_box_end;
18572 return 1;
18576 return 0;
18580 /* Extend the face of the last glyph in the text area of IT->glyph_row
18581 to the end of the display line. Called from display_line. If the
18582 glyph row is empty, add a space glyph to it so that we know the
18583 face to draw. Set the glyph row flag fill_line_p. If the glyph
18584 row is R2L, prepend a stretch glyph to cover the empty space to the
18585 left of the leftmost glyph. */
18587 static void
18588 extend_face_to_end_of_line (struct it *it)
18590 struct face *face, *default_face;
18591 struct frame *f = it->f;
18593 /* If line is already filled, do nothing. Non window-system frames
18594 get a grace of one more ``pixel'' because their characters are
18595 1-``pixel'' wide, so they hit the equality too early. This grace
18596 is needed only for R2L rows that are not continued, to produce
18597 one extra blank where we could display the cursor. */
18598 if (it->current_x >= it->last_visible_x
18599 + (!FRAME_WINDOW_P (f)
18600 && it->glyph_row->reversed_p
18601 && !it->glyph_row->continued_p))
18602 return;
18604 /* The default face, possibly remapped. */
18605 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18607 /* Face extension extends the background and box of IT->face_id
18608 to the end of the line. If the background equals the background
18609 of the frame, we don't have to do anything. */
18610 if (it->face_before_selective_p)
18611 face = FACE_FROM_ID (f, it->saved_face_id);
18612 else
18613 face = FACE_FROM_ID (f, it->face_id);
18615 if (FRAME_WINDOW_P (f)
18616 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18617 && face->box == FACE_NO_BOX
18618 && face->background == FRAME_BACKGROUND_PIXEL (f)
18619 && !face->stipple
18620 && !it->glyph_row->reversed_p)
18621 return;
18623 /* Set the glyph row flag indicating that the face of the last glyph
18624 in the text area has to be drawn to the end of the text area. */
18625 it->glyph_row->fill_line_p = 1;
18627 /* If current character of IT is not ASCII, make sure we have the
18628 ASCII face. This will be automatically undone the next time
18629 get_next_display_element returns a multibyte character. Note
18630 that the character will always be single byte in unibyte
18631 text. */
18632 if (!ASCII_CHAR_P (it->c))
18634 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18637 if (FRAME_WINDOW_P (f))
18639 /* If the row is empty, add a space with the current face of IT,
18640 so that we know which face to draw. */
18641 if (it->glyph_row->used[TEXT_AREA] == 0)
18643 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18644 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18645 it->glyph_row->used[TEXT_AREA] = 1;
18647 #ifdef HAVE_WINDOW_SYSTEM
18648 if (it->glyph_row->reversed_p)
18650 /* Prepend a stretch glyph to the row, such that the
18651 rightmost glyph will be drawn flushed all the way to the
18652 right margin of the window. The stretch glyph that will
18653 occupy the empty space, if any, to the left of the
18654 glyphs. */
18655 struct font *font = face->font ? face->font : FRAME_FONT (f);
18656 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18657 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18658 struct glyph *g;
18659 int row_width, stretch_ascent, stretch_width;
18660 struct text_pos saved_pos;
18661 int saved_face_id, saved_avoid_cursor, saved_box_start;
18663 for (row_width = 0, g = row_start; g < row_end; g++)
18664 row_width += g->pixel_width;
18665 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18666 if (stretch_width > 0)
18668 stretch_ascent =
18669 (((it->ascent + it->descent)
18670 * FONT_BASE (font)) / FONT_HEIGHT (font));
18671 saved_pos = it->position;
18672 memset (&it->position, 0, sizeof it->position);
18673 saved_avoid_cursor = it->avoid_cursor_p;
18674 it->avoid_cursor_p = 1;
18675 saved_face_id = it->face_id;
18676 saved_box_start = it->start_of_box_run_p;
18677 /* The last row's stretch glyph should get the default
18678 face, to avoid painting the rest of the window with
18679 the region face, if the region ends at ZV. */
18680 if (it->glyph_row->ends_at_zv_p)
18681 it->face_id = default_face->id;
18682 else
18683 it->face_id = face->id;
18684 it->start_of_box_run_p = 0;
18685 append_stretch_glyph (it, make_number (0), stretch_width,
18686 it->ascent + it->descent, stretch_ascent);
18687 it->position = saved_pos;
18688 it->avoid_cursor_p = saved_avoid_cursor;
18689 it->face_id = saved_face_id;
18690 it->start_of_box_run_p = saved_box_start;
18693 #endif /* HAVE_WINDOW_SYSTEM */
18695 else
18697 /* Save some values that must not be changed. */
18698 int saved_x = it->current_x;
18699 struct text_pos saved_pos;
18700 Lisp_Object saved_object;
18701 enum display_element_type saved_what = it->what;
18702 int saved_face_id = it->face_id;
18704 saved_object = it->object;
18705 saved_pos = it->position;
18707 it->what = IT_CHARACTER;
18708 memset (&it->position, 0, sizeof it->position);
18709 it->object = make_number (0);
18710 it->c = it->char_to_display = ' ';
18711 it->len = 1;
18712 /* The last row's blank glyphs should get the default face, to
18713 avoid painting the rest of the window with the region face,
18714 if the region ends at ZV. */
18715 if (it->glyph_row->ends_at_zv_p)
18716 it->face_id = default_face->id;
18717 else
18718 it->face_id = face->id;
18720 PRODUCE_GLYPHS (it);
18722 while (it->current_x <= it->last_visible_x)
18723 PRODUCE_GLYPHS (it);
18725 /* Don't count these blanks really. It would let us insert a left
18726 truncation glyph below and make us set the cursor on them, maybe. */
18727 it->current_x = saved_x;
18728 it->object = saved_object;
18729 it->position = saved_pos;
18730 it->what = saved_what;
18731 it->face_id = saved_face_id;
18736 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18737 trailing whitespace. */
18739 static int
18740 trailing_whitespace_p (ptrdiff_t charpos)
18742 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18743 int c = 0;
18745 while (bytepos < ZV_BYTE
18746 && (c = FETCH_CHAR (bytepos),
18747 c == ' ' || c == '\t'))
18748 ++bytepos;
18750 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18752 if (bytepos != PT_BYTE)
18753 return 1;
18755 return 0;
18759 /* Highlight trailing whitespace, if any, in ROW. */
18761 static void
18762 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18764 int used = row->used[TEXT_AREA];
18766 if (used)
18768 struct glyph *start = row->glyphs[TEXT_AREA];
18769 struct glyph *glyph = start + used - 1;
18771 if (row->reversed_p)
18773 /* Right-to-left rows need to be processed in the opposite
18774 direction, so swap the edge pointers. */
18775 glyph = start;
18776 start = row->glyphs[TEXT_AREA] + used - 1;
18779 /* Skip over glyphs inserted to display the cursor at the
18780 end of a line, for extending the face of the last glyph
18781 to the end of the line on terminals, and for truncation
18782 and continuation glyphs. */
18783 if (!row->reversed_p)
18785 while (glyph >= start
18786 && glyph->type == CHAR_GLYPH
18787 && INTEGERP (glyph->object))
18788 --glyph;
18790 else
18792 while (glyph <= start
18793 && glyph->type == CHAR_GLYPH
18794 && INTEGERP (glyph->object))
18795 ++glyph;
18798 /* If last glyph is a space or stretch, and it's trailing
18799 whitespace, set the face of all trailing whitespace glyphs in
18800 IT->glyph_row to `trailing-whitespace'. */
18801 if ((row->reversed_p ? glyph <= start : glyph >= start)
18802 && BUFFERP (glyph->object)
18803 && (glyph->type == STRETCH_GLYPH
18804 || (glyph->type == CHAR_GLYPH
18805 && glyph->u.ch == ' '))
18806 && trailing_whitespace_p (glyph->charpos))
18808 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18809 if (face_id < 0)
18810 return;
18812 if (!row->reversed_p)
18814 while (glyph >= start
18815 && BUFFERP (glyph->object)
18816 && (glyph->type == STRETCH_GLYPH
18817 || (glyph->type == CHAR_GLYPH
18818 && glyph->u.ch == ' ')))
18819 (glyph--)->face_id = face_id;
18821 else
18823 while (glyph <= start
18824 && BUFFERP (glyph->object)
18825 && (glyph->type == STRETCH_GLYPH
18826 || (glyph->type == CHAR_GLYPH
18827 && glyph->u.ch == ' ')))
18828 (glyph++)->face_id = face_id;
18835 /* Value is non-zero if glyph row ROW should be
18836 considered to hold the buffer position CHARPOS. */
18838 static int
18839 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18841 int result = 1;
18843 if (charpos == CHARPOS (row->end.pos)
18844 || charpos == MATRIX_ROW_END_CHARPOS (row))
18846 /* Suppose the row ends on a string.
18847 Unless the row is continued, that means it ends on a newline
18848 in the string. If it's anything other than a display string
18849 (e.g., a before-string from an overlay), we don't want the
18850 cursor there. (This heuristic seems to give the optimal
18851 behavior for the various types of multi-line strings.)
18852 One exception: if the string has `cursor' property on one of
18853 its characters, we _do_ want the cursor there. */
18854 if (CHARPOS (row->end.string_pos) >= 0)
18856 if (row->continued_p)
18857 result = 1;
18858 else
18860 /* Check for `display' property. */
18861 struct glyph *beg = row->glyphs[TEXT_AREA];
18862 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18863 struct glyph *glyph;
18865 result = 0;
18866 for (glyph = end; glyph >= beg; --glyph)
18867 if (STRINGP (glyph->object))
18869 Lisp_Object prop
18870 = Fget_char_property (make_number (charpos),
18871 Qdisplay, Qnil);
18872 result =
18873 (!NILP (prop)
18874 && display_prop_string_p (prop, glyph->object));
18875 /* If there's a `cursor' property on one of the
18876 string's characters, this row is a cursor row,
18877 even though this is not a display string. */
18878 if (!result)
18880 Lisp_Object s = glyph->object;
18882 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18884 ptrdiff_t gpos = glyph->charpos;
18886 if (!NILP (Fget_char_property (make_number (gpos),
18887 Qcursor, s)))
18889 result = 1;
18890 break;
18894 break;
18898 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18900 /* If the row ends in middle of a real character,
18901 and the line is continued, we want the cursor here.
18902 That's because CHARPOS (ROW->end.pos) would equal
18903 PT if PT is before the character. */
18904 if (!row->ends_in_ellipsis_p)
18905 result = row->continued_p;
18906 else
18907 /* If the row ends in an ellipsis, then
18908 CHARPOS (ROW->end.pos) will equal point after the
18909 invisible text. We want that position to be displayed
18910 after the ellipsis. */
18911 result = 0;
18913 /* If the row ends at ZV, display the cursor at the end of that
18914 row instead of at the start of the row below. */
18915 else if (row->ends_at_zv_p)
18916 result = 1;
18917 else
18918 result = 0;
18921 return result;
18924 /* Value is non-zero if glyph row ROW should be
18925 used to hold the cursor. */
18927 static int
18928 cursor_row_p (struct glyph_row *row)
18930 return row_for_charpos_p (row, PT);
18935 /* Push the property PROP so that it will be rendered at the current
18936 position in IT. Return 1 if PROP was successfully pushed, 0
18937 otherwise. Called from handle_line_prefix to handle the
18938 `line-prefix' and `wrap-prefix' properties. */
18940 static int
18941 push_prefix_prop (struct it *it, Lisp_Object prop)
18943 struct text_pos pos =
18944 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18946 eassert (it->method == GET_FROM_BUFFER
18947 || it->method == GET_FROM_DISPLAY_VECTOR
18948 || it->method == GET_FROM_STRING);
18950 /* We need to save the current buffer/string position, so it will be
18951 restored by pop_it, because iterate_out_of_display_property
18952 depends on that being set correctly, but some situations leave
18953 it->position not yet set when this function is called. */
18954 push_it (it, &pos);
18956 if (STRINGP (prop))
18958 if (SCHARS (prop) == 0)
18960 pop_it (it);
18961 return 0;
18964 it->string = prop;
18965 it->string_from_prefix_prop_p = 1;
18966 it->multibyte_p = STRING_MULTIBYTE (it->string);
18967 it->current.overlay_string_index = -1;
18968 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18969 it->end_charpos = it->string_nchars = SCHARS (it->string);
18970 it->method = GET_FROM_STRING;
18971 it->stop_charpos = 0;
18972 it->prev_stop = 0;
18973 it->base_level_stop = 0;
18975 /* Force paragraph direction to be that of the parent
18976 buffer/string. */
18977 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18978 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18979 else
18980 it->paragraph_embedding = L2R;
18982 /* Set up the bidi iterator for this display string. */
18983 if (it->bidi_p)
18985 it->bidi_it.string.lstring = it->string;
18986 it->bidi_it.string.s = NULL;
18987 it->bidi_it.string.schars = it->end_charpos;
18988 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18989 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18990 it->bidi_it.string.unibyte = !it->multibyte_p;
18991 it->bidi_it.w = it->w;
18992 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18995 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18997 it->method = GET_FROM_STRETCH;
18998 it->object = prop;
19000 #ifdef HAVE_WINDOW_SYSTEM
19001 else if (IMAGEP (prop))
19003 it->what = IT_IMAGE;
19004 it->image_id = lookup_image (it->f, prop);
19005 it->method = GET_FROM_IMAGE;
19007 #endif /* HAVE_WINDOW_SYSTEM */
19008 else
19010 pop_it (it); /* bogus display property, give up */
19011 return 0;
19014 return 1;
19017 /* Return the character-property PROP at the current position in IT. */
19019 static Lisp_Object
19020 get_it_property (struct it *it, Lisp_Object prop)
19022 Lisp_Object position, object = it->object;
19024 if (STRINGP (object))
19025 position = make_number (IT_STRING_CHARPOS (*it));
19026 else if (BUFFERP (object))
19028 position = make_number (IT_CHARPOS (*it));
19029 object = it->window;
19031 else
19032 return Qnil;
19034 return Fget_char_property (position, prop, object);
19037 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19039 static void
19040 handle_line_prefix (struct it *it)
19042 Lisp_Object prefix;
19044 if (it->continuation_lines_width > 0)
19046 prefix = get_it_property (it, Qwrap_prefix);
19047 if (NILP (prefix))
19048 prefix = Vwrap_prefix;
19050 else
19052 prefix = get_it_property (it, Qline_prefix);
19053 if (NILP (prefix))
19054 prefix = Vline_prefix;
19056 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19058 /* If the prefix is wider than the window, and we try to wrap
19059 it, it would acquire its own wrap prefix, and so on till the
19060 iterator stack overflows. So, don't wrap the prefix. */
19061 it->line_wrap = TRUNCATE;
19062 it->avoid_cursor_p = 1;
19068 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19069 only for R2L lines from display_line and display_string, when they
19070 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19071 the line/string needs to be continued on the next glyph row. */
19072 static void
19073 unproduce_glyphs (struct it *it, int n)
19075 struct glyph *glyph, *end;
19077 eassert (it->glyph_row);
19078 eassert (it->glyph_row->reversed_p);
19079 eassert (it->area == TEXT_AREA);
19080 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19082 if (n > it->glyph_row->used[TEXT_AREA])
19083 n = it->glyph_row->used[TEXT_AREA];
19084 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19085 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19086 for ( ; glyph < end; glyph++)
19087 glyph[-n] = *glyph;
19090 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19091 and ROW->maxpos. */
19092 static void
19093 find_row_edges (struct it *it, struct glyph_row *row,
19094 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19095 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19097 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19098 lines' rows is implemented for bidi-reordered rows. */
19100 /* ROW->minpos is the value of min_pos, the minimal buffer position
19101 we have in ROW, or ROW->start.pos if that is smaller. */
19102 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19103 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19104 else
19105 /* We didn't find buffer positions smaller than ROW->start, or
19106 didn't find _any_ valid buffer positions in any of the glyphs,
19107 so we must trust the iterator's computed positions. */
19108 row->minpos = row->start.pos;
19109 if (max_pos <= 0)
19111 max_pos = CHARPOS (it->current.pos);
19112 max_bpos = BYTEPOS (it->current.pos);
19115 /* Here are the various use-cases for ending the row, and the
19116 corresponding values for ROW->maxpos:
19118 Line ends in a newline from buffer eol_pos + 1
19119 Line is continued from buffer max_pos + 1
19120 Line is truncated on right it->current.pos
19121 Line ends in a newline from string max_pos + 1(*)
19122 (*) + 1 only when line ends in a forward scan
19123 Line is continued from string max_pos
19124 Line is continued from display vector max_pos
19125 Line is entirely from a string min_pos == max_pos
19126 Line is entirely from a display vector min_pos == max_pos
19127 Line that ends at ZV ZV
19129 If you discover other use-cases, please add them here as
19130 appropriate. */
19131 if (row->ends_at_zv_p)
19132 row->maxpos = it->current.pos;
19133 else if (row->used[TEXT_AREA])
19135 int seen_this_string = 0;
19136 struct glyph_row *r1 = row - 1;
19138 /* Did we see the same display string on the previous row? */
19139 if (STRINGP (it->object)
19140 /* this is not the first row */
19141 && row > it->w->desired_matrix->rows
19142 /* previous row is not the header line */
19143 && !r1->mode_line_p
19144 /* previous row also ends in a newline from a string */
19145 && r1->ends_in_newline_from_string_p)
19147 struct glyph *start, *end;
19149 /* Search for the last glyph of the previous row that came
19150 from buffer or string. Depending on whether the row is
19151 L2R or R2L, we need to process it front to back or the
19152 other way round. */
19153 if (!r1->reversed_p)
19155 start = r1->glyphs[TEXT_AREA];
19156 end = start + r1->used[TEXT_AREA];
19157 /* Glyphs inserted by redisplay have an integer (zero)
19158 as their object. */
19159 while (end > start
19160 && INTEGERP ((end - 1)->object)
19161 && (end - 1)->charpos <= 0)
19162 --end;
19163 if (end > start)
19165 if (EQ ((end - 1)->object, it->object))
19166 seen_this_string = 1;
19168 else
19169 /* If all the glyphs of the previous row were inserted
19170 by redisplay, it means the previous row was
19171 produced from a single newline, which is only
19172 possible if that newline came from the same string
19173 as the one which produced this ROW. */
19174 seen_this_string = 1;
19176 else
19178 end = r1->glyphs[TEXT_AREA] - 1;
19179 start = end + r1->used[TEXT_AREA];
19180 while (end < start
19181 && INTEGERP ((end + 1)->object)
19182 && (end + 1)->charpos <= 0)
19183 ++end;
19184 if (end < start)
19186 if (EQ ((end + 1)->object, it->object))
19187 seen_this_string = 1;
19189 else
19190 seen_this_string = 1;
19193 /* Take note of each display string that covers a newline only
19194 once, the first time we see it. This is for when a display
19195 string includes more than one newline in it. */
19196 if (row->ends_in_newline_from_string_p && !seen_this_string)
19198 /* If we were scanning the buffer forward when we displayed
19199 the string, we want to account for at least one buffer
19200 position that belongs to this row (position covered by
19201 the display string), so that cursor positioning will
19202 consider this row as a candidate when point is at the end
19203 of the visual line represented by this row. This is not
19204 required when scanning back, because max_pos will already
19205 have a much larger value. */
19206 if (CHARPOS (row->end.pos) > max_pos)
19207 INC_BOTH (max_pos, max_bpos);
19208 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19210 else if (CHARPOS (it->eol_pos) > 0)
19211 SET_TEXT_POS (row->maxpos,
19212 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19213 else if (row->continued_p)
19215 /* If max_pos is different from IT's current position, it
19216 means IT->method does not belong to the display element
19217 at max_pos. However, it also means that the display
19218 element at max_pos was displayed in its entirety on this
19219 line, which is equivalent to saying that the next line
19220 starts at the next buffer position. */
19221 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19222 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19223 else
19225 INC_BOTH (max_pos, max_bpos);
19226 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19229 else if (row->truncated_on_right_p)
19230 /* display_line already called reseat_at_next_visible_line_start,
19231 which puts the iterator at the beginning of the next line, in
19232 the logical order. */
19233 row->maxpos = it->current.pos;
19234 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19235 /* A line that is entirely from a string/image/stretch... */
19236 row->maxpos = row->minpos;
19237 else
19238 emacs_abort ();
19240 else
19241 row->maxpos = it->current.pos;
19244 /* Construct the glyph row IT->glyph_row in the desired matrix of
19245 IT->w from text at the current position of IT. See dispextern.h
19246 for an overview of struct it. Value is non-zero if
19247 IT->glyph_row displays text, as opposed to a line displaying ZV
19248 only. */
19250 static int
19251 display_line (struct it *it)
19253 struct glyph_row *row = it->glyph_row;
19254 Lisp_Object overlay_arrow_string;
19255 struct it wrap_it;
19256 void *wrap_data = NULL;
19257 int may_wrap = 0, wrap_x IF_LINT (= 0);
19258 int wrap_row_used = -1;
19259 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19260 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19261 int wrap_row_extra_line_spacing IF_LINT (= 0);
19262 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19263 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19264 int cvpos;
19265 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19266 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19268 /* We always start displaying at hpos zero even if hscrolled. */
19269 eassert (it->hpos == 0 && it->current_x == 0);
19271 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19272 >= it->w->desired_matrix->nrows)
19274 it->w->nrows_scale_factor++;
19275 it->f->fonts_changed = 1;
19276 return 0;
19279 /* Is IT->w showing the region? */
19280 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19282 /* Clear the result glyph row and enable it. */
19283 prepare_desired_row (row);
19285 row->y = it->current_y;
19286 row->start = it->start;
19287 row->continuation_lines_width = it->continuation_lines_width;
19288 row->displays_text_p = 1;
19289 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19290 it->starts_in_middle_of_char_p = 0;
19292 /* Arrange the overlays nicely for our purposes. Usually, we call
19293 display_line on only one line at a time, in which case this
19294 can't really hurt too much, or we call it on lines which appear
19295 one after another in the buffer, in which case all calls to
19296 recenter_overlay_lists but the first will be pretty cheap. */
19297 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19299 /* Move over display elements that are not visible because we are
19300 hscrolled. This may stop at an x-position < IT->first_visible_x
19301 if the first glyph is partially visible or if we hit a line end. */
19302 if (it->current_x < it->first_visible_x)
19304 enum move_it_result move_result;
19306 this_line_min_pos = row->start.pos;
19307 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19308 MOVE_TO_POS | MOVE_TO_X);
19309 /* If we are under a large hscroll, move_it_in_display_line_to
19310 could hit the end of the line without reaching
19311 it->first_visible_x. Pretend that we did reach it. This is
19312 especially important on a TTY, where we will call
19313 extend_face_to_end_of_line, which needs to know how many
19314 blank glyphs to produce. */
19315 if (it->current_x < it->first_visible_x
19316 && (move_result == MOVE_NEWLINE_OR_CR
19317 || move_result == MOVE_POS_MATCH_OR_ZV))
19318 it->current_x = it->first_visible_x;
19320 /* Record the smallest positions seen while we moved over
19321 display elements that are not visible. This is needed by
19322 redisplay_internal for optimizing the case where the cursor
19323 stays inside the same line. The rest of this function only
19324 considers positions that are actually displayed, so
19325 RECORD_MAX_MIN_POS will not otherwise record positions that
19326 are hscrolled to the left of the left edge of the window. */
19327 min_pos = CHARPOS (this_line_min_pos);
19328 min_bpos = BYTEPOS (this_line_min_pos);
19330 else
19332 /* We only do this when not calling `move_it_in_display_line_to'
19333 above, because move_it_in_display_line_to calls
19334 handle_line_prefix itself. */
19335 handle_line_prefix (it);
19338 /* Get the initial row height. This is either the height of the
19339 text hscrolled, if there is any, or zero. */
19340 row->ascent = it->max_ascent;
19341 row->height = it->max_ascent + it->max_descent;
19342 row->phys_ascent = it->max_phys_ascent;
19343 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19344 row->extra_line_spacing = it->max_extra_line_spacing;
19346 /* Utility macro to record max and min buffer positions seen until now. */
19347 #define RECORD_MAX_MIN_POS(IT) \
19348 do \
19350 int composition_p = !STRINGP ((IT)->string) \
19351 && ((IT)->what == IT_COMPOSITION); \
19352 ptrdiff_t current_pos = \
19353 composition_p ? (IT)->cmp_it.charpos \
19354 : IT_CHARPOS (*(IT)); \
19355 ptrdiff_t current_bpos = \
19356 composition_p ? CHAR_TO_BYTE (current_pos) \
19357 : IT_BYTEPOS (*(IT)); \
19358 if (current_pos < min_pos) \
19360 min_pos = current_pos; \
19361 min_bpos = current_bpos; \
19363 if (IT_CHARPOS (*it) > max_pos) \
19365 max_pos = IT_CHARPOS (*it); \
19366 max_bpos = IT_BYTEPOS (*it); \
19369 while (0)
19371 /* Loop generating characters. The loop is left with IT on the next
19372 character to display. */
19373 while (1)
19375 int n_glyphs_before, hpos_before, x_before;
19376 int x, nglyphs;
19377 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19379 /* Retrieve the next thing to display. Value is zero if end of
19380 buffer reached. */
19381 if (!get_next_display_element (it))
19383 /* Maybe add a space at the end of this line that is used to
19384 display the cursor there under X. Set the charpos of the
19385 first glyph of blank lines not corresponding to any text
19386 to -1. */
19387 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19388 row->exact_window_width_line_p = 1;
19389 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19390 || row->used[TEXT_AREA] == 0)
19392 row->glyphs[TEXT_AREA]->charpos = -1;
19393 row->displays_text_p = 0;
19395 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19396 && (!MINI_WINDOW_P (it->w)
19397 || (minibuf_level && EQ (it->window, minibuf_window))))
19398 row->indicate_empty_line_p = 1;
19401 it->continuation_lines_width = 0;
19402 row->ends_at_zv_p = 1;
19403 /* A row that displays right-to-left text must always have
19404 its last face extended all the way to the end of line,
19405 even if this row ends in ZV, because we still write to
19406 the screen left to right. We also need to extend the
19407 last face if the default face is remapped to some
19408 different face, otherwise the functions that clear
19409 portions of the screen will clear with the default face's
19410 background color. */
19411 if (row->reversed_p
19412 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19413 extend_face_to_end_of_line (it);
19414 break;
19417 /* Now, get the metrics of what we want to display. This also
19418 generates glyphs in `row' (which is IT->glyph_row). */
19419 n_glyphs_before = row->used[TEXT_AREA];
19420 x = it->current_x;
19422 /* Remember the line height so far in case the next element doesn't
19423 fit on the line. */
19424 if (it->line_wrap != TRUNCATE)
19426 ascent = it->max_ascent;
19427 descent = it->max_descent;
19428 phys_ascent = it->max_phys_ascent;
19429 phys_descent = it->max_phys_descent;
19431 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19433 if (IT_DISPLAYING_WHITESPACE (it))
19434 may_wrap = 1;
19435 else if (may_wrap)
19437 SAVE_IT (wrap_it, *it, wrap_data);
19438 wrap_x = x;
19439 wrap_row_used = row->used[TEXT_AREA];
19440 wrap_row_ascent = row->ascent;
19441 wrap_row_height = row->height;
19442 wrap_row_phys_ascent = row->phys_ascent;
19443 wrap_row_phys_height = row->phys_height;
19444 wrap_row_extra_line_spacing = row->extra_line_spacing;
19445 wrap_row_min_pos = min_pos;
19446 wrap_row_min_bpos = min_bpos;
19447 wrap_row_max_pos = max_pos;
19448 wrap_row_max_bpos = max_bpos;
19449 may_wrap = 0;
19454 PRODUCE_GLYPHS (it);
19456 /* If this display element was in marginal areas, continue with
19457 the next one. */
19458 if (it->area != TEXT_AREA)
19460 row->ascent = max (row->ascent, it->max_ascent);
19461 row->height = max (row->height, it->max_ascent + it->max_descent);
19462 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19463 row->phys_height = max (row->phys_height,
19464 it->max_phys_ascent + it->max_phys_descent);
19465 row->extra_line_spacing = max (row->extra_line_spacing,
19466 it->max_extra_line_spacing);
19467 set_iterator_to_next (it, 1);
19468 continue;
19471 /* Does the display element fit on the line? If we truncate
19472 lines, we should draw past the right edge of the window. If
19473 we don't truncate, we want to stop so that we can display the
19474 continuation glyph before the right margin. If lines are
19475 continued, there are two possible strategies for characters
19476 resulting in more than 1 glyph (e.g. tabs): Display as many
19477 glyphs as possible in this line and leave the rest for the
19478 continuation line, or display the whole element in the next
19479 line. Original redisplay did the former, so we do it also. */
19480 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19481 hpos_before = it->hpos;
19482 x_before = x;
19484 if (/* Not a newline. */
19485 nglyphs > 0
19486 /* Glyphs produced fit entirely in the line. */
19487 && it->current_x < it->last_visible_x)
19489 it->hpos += nglyphs;
19490 row->ascent = max (row->ascent, it->max_ascent);
19491 row->height = max (row->height, it->max_ascent + it->max_descent);
19492 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19493 row->phys_height = max (row->phys_height,
19494 it->max_phys_ascent + it->max_phys_descent);
19495 row->extra_line_spacing = max (row->extra_line_spacing,
19496 it->max_extra_line_spacing);
19497 if (it->current_x - it->pixel_width < it->first_visible_x)
19498 row->x = x - it->first_visible_x;
19499 /* Record the maximum and minimum buffer positions seen so
19500 far in glyphs that will be displayed by this row. */
19501 if (it->bidi_p)
19502 RECORD_MAX_MIN_POS (it);
19504 else
19506 int i, new_x;
19507 struct glyph *glyph;
19509 for (i = 0; i < nglyphs; ++i, x = new_x)
19511 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19512 new_x = x + glyph->pixel_width;
19514 if (/* Lines are continued. */
19515 it->line_wrap != TRUNCATE
19516 && (/* Glyph doesn't fit on the line. */
19517 new_x > it->last_visible_x
19518 /* Or it fits exactly on a window system frame. */
19519 || (new_x == it->last_visible_x
19520 && FRAME_WINDOW_P (it->f)
19521 && (row->reversed_p
19522 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19523 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19525 /* End of a continued line. */
19527 if (it->hpos == 0
19528 || (new_x == it->last_visible_x
19529 && FRAME_WINDOW_P (it->f)
19530 && (row->reversed_p
19531 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19532 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19534 /* Current glyph is the only one on the line or
19535 fits exactly on the line. We must continue
19536 the line because we can't draw the cursor
19537 after the glyph. */
19538 row->continued_p = 1;
19539 it->current_x = new_x;
19540 it->continuation_lines_width += new_x;
19541 ++it->hpos;
19542 if (i == nglyphs - 1)
19544 /* If line-wrap is on, check if a previous
19545 wrap point was found. */
19546 if (wrap_row_used > 0
19547 /* Even if there is a previous wrap
19548 point, continue the line here as
19549 usual, if (i) the previous character
19550 was a space or tab AND (ii) the
19551 current character is not. */
19552 && (!may_wrap
19553 || IT_DISPLAYING_WHITESPACE (it)))
19554 goto back_to_wrap;
19556 /* Record the maximum and minimum buffer
19557 positions seen so far in glyphs that will be
19558 displayed by this row. */
19559 if (it->bidi_p)
19560 RECORD_MAX_MIN_POS (it);
19561 set_iterator_to_next (it, 1);
19562 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19564 if (!get_next_display_element (it))
19566 row->exact_window_width_line_p = 1;
19567 it->continuation_lines_width = 0;
19568 row->continued_p = 0;
19569 row->ends_at_zv_p = 1;
19571 else if (ITERATOR_AT_END_OF_LINE_P (it))
19573 row->continued_p = 0;
19574 row->exact_window_width_line_p = 1;
19578 else if (it->bidi_p)
19579 RECORD_MAX_MIN_POS (it);
19581 else if (CHAR_GLYPH_PADDING_P (*glyph)
19582 && !FRAME_WINDOW_P (it->f))
19584 /* A padding glyph that doesn't fit on this line.
19585 This means the whole character doesn't fit
19586 on the line. */
19587 if (row->reversed_p)
19588 unproduce_glyphs (it, row->used[TEXT_AREA]
19589 - n_glyphs_before);
19590 row->used[TEXT_AREA] = n_glyphs_before;
19592 /* Fill the rest of the row with continuation
19593 glyphs like in 20.x. */
19594 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19595 < row->glyphs[1 + TEXT_AREA])
19596 produce_special_glyphs (it, IT_CONTINUATION);
19598 row->continued_p = 1;
19599 it->current_x = x_before;
19600 it->continuation_lines_width += x_before;
19602 /* Restore the height to what it was before the
19603 element not fitting on the line. */
19604 it->max_ascent = ascent;
19605 it->max_descent = descent;
19606 it->max_phys_ascent = phys_ascent;
19607 it->max_phys_descent = phys_descent;
19609 else if (wrap_row_used > 0)
19611 back_to_wrap:
19612 if (row->reversed_p)
19613 unproduce_glyphs (it,
19614 row->used[TEXT_AREA] - wrap_row_used);
19615 RESTORE_IT (it, &wrap_it, wrap_data);
19616 it->continuation_lines_width += wrap_x;
19617 row->used[TEXT_AREA] = wrap_row_used;
19618 row->ascent = wrap_row_ascent;
19619 row->height = wrap_row_height;
19620 row->phys_ascent = wrap_row_phys_ascent;
19621 row->phys_height = wrap_row_phys_height;
19622 row->extra_line_spacing = wrap_row_extra_line_spacing;
19623 min_pos = wrap_row_min_pos;
19624 min_bpos = wrap_row_min_bpos;
19625 max_pos = wrap_row_max_pos;
19626 max_bpos = wrap_row_max_bpos;
19627 row->continued_p = 1;
19628 row->ends_at_zv_p = 0;
19629 row->exact_window_width_line_p = 0;
19630 it->continuation_lines_width += x;
19632 /* Make sure that a non-default face is extended
19633 up to the right margin of the window. */
19634 extend_face_to_end_of_line (it);
19636 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19638 /* A TAB that extends past the right edge of the
19639 window. This produces a single glyph on
19640 window system frames. We leave the glyph in
19641 this row and let it fill the row, but don't
19642 consume the TAB. */
19643 if ((row->reversed_p
19644 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19645 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19646 produce_special_glyphs (it, IT_CONTINUATION);
19647 it->continuation_lines_width += it->last_visible_x;
19648 row->ends_in_middle_of_char_p = 1;
19649 row->continued_p = 1;
19650 glyph->pixel_width = it->last_visible_x - x;
19651 it->starts_in_middle_of_char_p = 1;
19653 else
19655 /* Something other than a TAB that draws past
19656 the right edge of the window. Restore
19657 positions to values before the element. */
19658 if (row->reversed_p)
19659 unproduce_glyphs (it, row->used[TEXT_AREA]
19660 - (n_glyphs_before + i));
19661 row->used[TEXT_AREA] = n_glyphs_before + i;
19663 /* Display continuation glyphs. */
19664 it->current_x = x_before;
19665 it->continuation_lines_width += x;
19666 if (!FRAME_WINDOW_P (it->f)
19667 || (row->reversed_p
19668 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19669 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19670 produce_special_glyphs (it, IT_CONTINUATION);
19671 row->continued_p = 1;
19673 extend_face_to_end_of_line (it);
19675 if (nglyphs > 1 && i > 0)
19677 row->ends_in_middle_of_char_p = 1;
19678 it->starts_in_middle_of_char_p = 1;
19681 /* Restore the height to what it was before the
19682 element not fitting on the line. */
19683 it->max_ascent = ascent;
19684 it->max_descent = descent;
19685 it->max_phys_ascent = phys_ascent;
19686 it->max_phys_descent = phys_descent;
19689 break;
19691 else if (new_x > it->first_visible_x)
19693 /* Increment number of glyphs actually displayed. */
19694 ++it->hpos;
19696 /* Record the maximum and minimum buffer positions
19697 seen so far in glyphs that will be displayed by
19698 this row. */
19699 if (it->bidi_p)
19700 RECORD_MAX_MIN_POS (it);
19702 if (x < it->first_visible_x)
19703 /* Glyph is partially visible, i.e. row starts at
19704 negative X position. */
19705 row->x = x - it->first_visible_x;
19707 else
19709 /* Glyph is completely off the left margin of the
19710 window. This should not happen because of the
19711 move_it_in_display_line at the start of this
19712 function, unless the text display area of the
19713 window is empty. */
19714 eassert (it->first_visible_x <= it->last_visible_x);
19717 /* Even if this display element produced no glyphs at all,
19718 we want to record its position. */
19719 if (it->bidi_p && nglyphs == 0)
19720 RECORD_MAX_MIN_POS (it);
19722 row->ascent = max (row->ascent, it->max_ascent);
19723 row->height = max (row->height, it->max_ascent + it->max_descent);
19724 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19725 row->phys_height = max (row->phys_height,
19726 it->max_phys_ascent + it->max_phys_descent);
19727 row->extra_line_spacing = max (row->extra_line_spacing,
19728 it->max_extra_line_spacing);
19730 /* End of this display line if row is continued. */
19731 if (row->continued_p || row->ends_at_zv_p)
19732 break;
19735 at_end_of_line:
19736 /* Is this a line end? If yes, we're also done, after making
19737 sure that a non-default face is extended up to the right
19738 margin of the window. */
19739 if (ITERATOR_AT_END_OF_LINE_P (it))
19741 int used_before = row->used[TEXT_AREA];
19743 row->ends_in_newline_from_string_p = STRINGP (it->object);
19745 /* Add a space at the end of the line that is used to
19746 display the cursor there. */
19747 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19748 append_space_for_newline (it, 0);
19750 /* Extend the face to the end of the line. */
19751 extend_face_to_end_of_line (it);
19753 /* Make sure we have the position. */
19754 if (used_before == 0)
19755 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19757 /* Record the position of the newline, for use in
19758 find_row_edges. */
19759 it->eol_pos = it->current.pos;
19761 /* Consume the line end. This skips over invisible lines. */
19762 set_iterator_to_next (it, 1);
19763 it->continuation_lines_width = 0;
19764 break;
19767 /* Proceed with next display element. Note that this skips
19768 over lines invisible because of selective display. */
19769 set_iterator_to_next (it, 1);
19771 /* If we truncate lines, we are done when the last displayed
19772 glyphs reach past the right margin of the window. */
19773 if (it->line_wrap == TRUNCATE
19774 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19775 ? (it->current_x >= it->last_visible_x)
19776 : (it->current_x > it->last_visible_x)))
19778 /* Maybe add truncation glyphs. */
19779 if (!FRAME_WINDOW_P (it->f)
19780 || (row->reversed_p
19781 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19782 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19784 int i, n;
19786 if (!row->reversed_p)
19788 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19789 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19790 break;
19792 else
19794 for (i = 0; i < row->used[TEXT_AREA]; i++)
19795 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19796 break;
19797 /* Remove any padding glyphs at the front of ROW, to
19798 make room for the truncation glyphs we will be
19799 adding below. The loop below always inserts at
19800 least one truncation glyph, so also remove the
19801 last glyph added to ROW. */
19802 unproduce_glyphs (it, i + 1);
19803 /* Adjust i for the loop below. */
19804 i = row->used[TEXT_AREA] - (i + 1);
19807 it->current_x = x_before;
19808 if (!FRAME_WINDOW_P (it->f))
19810 for (n = row->used[TEXT_AREA]; i < n; ++i)
19812 row->used[TEXT_AREA] = i;
19813 produce_special_glyphs (it, IT_TRUNCATION);
19816 else
19818 row->used[TEXT_AREA] = i;
19819 produce_special_glyphs (it, IT_TRUNCATION);
19822 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19824 /* Don't truncate if we can overflow newline into fringe. */
19825 if (!get_next_display_element (it))
19827 it->continuation_lines_width = 0;
19828 row->ends_at_zv_p = 1;
19829 row->exact_window_width_line_p = 1;
19830 break;
19832 if (ITERATOR_AT_END_OF_LINE_P (it))
19834 row->exact_window_width_line_p = 1;
19835 goto at_end_of_line;
19837 it->current_x = x_before;
19840 row->truncated_on_right_p = 1;
19841 it->continuation_lines_width = 0;
19842 reseat_at_next_visible_line_start (it, 0);
19843 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19844 it->hpos = hpos_before;
19845 break;
19849 if (wrap_data)
19850 bidi_unshelve_cache (wrap_data, 1);
19852 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19853 at the left window margin. */
19854 if (it->first_visible_x
19855 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19857 if (!FRAME_WINDOW_P (it->f)
19858 || (row->reversed_p
19859 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19860 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19861 insert_left_trunc_glyphs (it);
19862 row->truncated_on_left_p = 1;
19865 /* Remember the position at which this line ends.
19867 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19868 cannot be before the call to find_row_edges below, since that is
19869 where these positions are determined. */
19870 row->end = it->current;
19871 if (!it->bidi_p)
19873 row->minpos = row->start.pos;
19874 row->maxpos = row->end.pos;
19876 else
19878 /* ROW->minpos and ROW->maxpos must be the smallest and
19879 `1 + the largest' buffer positions in ROW. But if ROW was
19880 bidi-reordered, these two positions can be anywhere in the
19881 row, so we must determine them now. */
19882 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19885 /* If the start of this line is the overlay arrow-position, then
19886 mark this glyph row as the one containing the overlay arrow.
19887 This is clearly a mess with variable size fonts. It would be
19888 better to let it be displayed like cursors under X. */
19889 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19890 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19891 !NILP (overlay_arrow_string)))
19893 /* Overlay arrow in window redisplay is a fringe bitmap. */
19894 if (STRINGP (overlay_arrow_string))
19896 struct glyph_row *arrow_row
19897 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19898 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19899 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19900 struct glyph *p = row->glyphs[TEXT_AREA];
19901 struct glyph *p2, *end;
19903 /* Copy the arrow glyphs. */
19904 while (glyph < arrow_end)
19905 *p++ = *glyph++;
19907 /* Throw away padding glyphs. */
19908 p2 = p;
19909 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19910 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19911 ++p2;
19912 if (p2 > p)
19914 while (p2 < end)
19915 *p++ = *p2++;
19916 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19919 else
19921 eassert (INTEGERP (overlay_arrow_string));
19922 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19924 overlay_arrow_seen = 1;
19927 /* Highlight trailing whitespace. */
19928 if (!NILP (Vshow_trailing_whitespace))
19929 highlight_trailing_whitespace (it->f, it->glyph_row);
19931 /* Compute pixel dimensions of this line. */
19932 compute_line_metrics (it);
19934 /* Implementation note: No changes in the glyphs of ROW or in their
19935 faces can be done past this point, because compute_line_metrics
19936 computes ROW's hash value and stores it within the glyph_row
19937 structure. */
19939 /* Record whether this row ends inside an ellipsis. */
19940 row->ends_in_ellipsis_p
19941 = (it->method == GET_FROM_DISPLAY_VECTOR
19942 && it->ellipsis_p);
19944 /* Save fringe bitmaps in this row. */
19945 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19946 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19947 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19948 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19950 it->left_user_fringe_bitmap = 0;
19951 it->left_user_fringe_face_id = 0;
19952 it->right_user_fringe_bitmap = 0;
19953 it->right_user_fringe_face_id = 0;
19955 /* Maybe set the cursor. */
19956 cvpos = it->w->cursor.vpos;
19957 if ((cvpos < 0
19958 /* In bidi-reordered rows, keep checking for proper cursor
19959 position even if one has been found already, because buffer
19960 positions in such rows change non-linearly with ROW->VPOS,
19961 when a line is continued. One exception: when we are at ZV,
19962 display cursor on the first suitable glyph row, since all
19963 the empty rows after that also have their position set to ZV. */
19964 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19965 lines' rows is implemented for bidi-reordered rows. */
19966 || (it->bidi_p
19967 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19968 && PT >= MATRIX_ROW_START_CHARPOS (row)
19969 && PT <= MATRIX_ROW_END_CHARPOS (row)
19970 && cursor_row_p (row))
19971 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19973 /* Prepare for the next line. This line starts horizontally at (X
19974 HPOS) = (0 0). Vertical positions are incremented. As a
19975 convenience for the caller, IT->glyph_row is set to the next
19976 row to be used. */
19977 it->current_x = it->hpos = 0;
19978 it->current_y += row->height;
19979 SET_TEXT_POS (it->eol_pos, 0, 0);
19980 ++it->vpos;
19981 ++it->glyph_row;
19982 /* The next row should by default use the same value of the
19983 reversed_p flag as this one. set_iterator_to_next decides when
19984 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19985 the flag accordingly. */
19986 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19987 it->glyph_row->reversed_p = row->reversed_p;
19988 it->start = row->end;
19989 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19991 #undef RECORD_MAX_MIN_POS
19994 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19995 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19996 doc: /* Return paragraph direction at point in BUFFER.
19997 Value is either `left-to-right' or `right-to-left'.
19998 If BUFFER is omitted or nil, it defaults to the current buffer.
20000 Paragraph direction determines how the text in the paragraph is displayed.
20001 In left-to-right paragraphs, text begins at the left margin of the window
20002 and the reading direction is generally left to right. In right-to-left
20003 paragraphs, text begins at the right margin and is read from right to left.
20005 See also `bidi-paragraph-direction'. */)
20006 (Lisp_Object buffer)
20008 struct buffer *buf = current_buffer;
20009 struct buffer *old = buf;
20011 if (! NILP (buffer))
20013 CHECK_BUFFER (buffer);
20014 buf = XBUFFER (buffer);
20017 if (NILP (BVAR (buf, bidi_display_reordering))
20018 || NILP (BVAR (buf, enable_multibyte_characters))
20019 /* When we are loading loadup.el, the character property tables
20020 needed for bidi iteration are not yet available. */
20021 || !NILP (Vpurify_flag))
20022 return Qleft_to_right;
20023 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20024 return BVAR (buf, bidi_paragraph_direction);
20025 else
20027 /* Determine the direction from buffer text. We could try to
20028 use current_matrix if it is up to date, but this seems fast
20029 enough as it is. */
20030 struct bidi_it itb;
20031 ptrdiff_t pos = BUF_PT (buf);
20032 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20033 int c;
20034 void *itb_data = bidi_shelve_cache ();
20036 set_buffer_temp (buf);
20037 /* bidi_paragraph_init finds the base direction of the paragraph
20038 by searching forward from paragraph start. We need the base
20039 direction of the current or _previous_ paragraph, so we need
20040 to make sure we are within that paragraph. To that end, find
20041 the previous non-empty line. */
20042 if (pos >= ZV && pos > BEGV)
20043 DEC_BOTH (pos, bytepos);
20044 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20045 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20047 while ((c = FETCH_BYTE (bytepos)) == '\n'
20048 || c == ' ' || c == '\t' || c == '\f')
20050 if (bytepos <= BEGV_BYTE)
20051 break;
20052 bytepos--;
20053 pos--;
20055 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20056 bytepos--;
20058 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20059 itb.paragraph_dir = NEUTRAL_DIR;
20060 itb.string.s = NULL;
20061 itb.string.lstring = Qnil;
20062 itb.string.bufpos = 0;
20063 itb.string.unibyte = 0;
20064 /* We have no window to use here for ignoring window-specific
20065 overlays. Using NULL for window pointer will cause
20066 compute_display_string_pos to use the current buffer. */
20067 itb.w = NULL;
20068 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20069 bidi_unshelve_cache (itb_data, 0);
20070 set_buffer_temp (old);
20071 switch (itb.paragraph_dir)
20073 case L2R:
20074 return Qleft_to_right;
20075 break;
20076 case R2L:
20077 return Qright_to_left;
20078 break;
20079 default:
20080 emacs_abort ();
20085 DEFUN ("move-point-visually", Fmove_point_visually,
20086 Smove_point_visually, 1, 1, 0,
20087 doc: /* Move point in the visual order in the specified DIRECTION.
20088 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20089 left.
20091 Value is the new character position of point. */)
20092 (Lisp_Object direction)
20094 struct window *w = XWINDOW (selected_window);
20095 struct buffer *b = XBUFFER (w->contents);
20096 struct glyph_row *row;
20097 int dir;
20098 Lisp_Object paragraph_dir;
20100 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20101 (!(ROW)->continued_p \
20102 && INTEGERP ((GLYPH)->object) \
20103 && (GLYPH)->type == CHAR_GLYPH \
20104 && (GLYPH)->u.ch == ' ' \
20105 && (GLYPH)->charpos >= 0 \
20106 && !(GLYPH)->avoid_cursor_p)
20108 CHECK_NUMBER (direction);
20109 dir = XINT (direction);
20110 if (dir > 0)
20111 dir = 1;
20112 else
20113 dir = -1;
20115 /* If current matrix is up-to-date, we can use the information
20116 recorded in the glyphs, at least as long as the goal is on the
20117 screen. */
20118 if (w->window_end_valid
20119 && !windows_or_buffers_changed
20120 && b
20121 && !b->clip_changed
20122 && !b->prevent_redisplay_optimizations_p
20123 && !window_outdated (w)
20124 && w->cursor.vpos >= 0
20125 && w->cursor.vpos < w->current_matrix->nrows
20126 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20128 struct glyph *g = row->glyphs[TEXT_AREA];
20129 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20130 struct glyph *gpt = g + w->cursor.hpos;
20132 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20134 if (BUFFERP (g->object) && g->charpos != PT)
20136 SET_PT (g->charpos);
20137 w->cursor.vpos = -1;
20138 return make_number (PT);
20140 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20142 ptrdiff_t new_pos;
20144 if (BUFFERP (gpt->object))
20146 new_pos = PT;
20147 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20148 new_pos += (row->reversed_p ? -dir : dir);
20149 else
20150 new_pos -= (row->reversed_p ? -dir : dir);;
20152 else if (BUFFERP (g->object))
20153 new_pos = g->charpos;
20154 else
20155 break;
20156 SET_PT (new_pos);
20157 w->cursor.vpos = -1;
20158 return make_number (PT);
20160 else if (ROW_GLYPH_NEWLINE_P (row, g))
20162 /* Glyphs inserted at the end of a non-empty line for
20163 positioning the cursor have zero charpos, so we must
20164 deduce the value of point by other means. */
20165 if (g->charpos > 0)
20166 SET_PT (g->charpos);
20167 else if (row->ends_at_zv_p && PT != ZV)
20168 SET_PT (ZV);
20169 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20170 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20171 else
20172 break;
20173 w->cursor.vpos = -1;
20174 return make_number (PT);
20177 if (g == e || INTEGERP (g->object))
20179 if (row->truncated_on_left_p || row->truncated_on_right_p)
20180 goto simulate_display;
20181 if (!row->reversed_p)
20182 row += dir;
20183 else
20184 row -= dir;
20185 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20186 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20187 goto simulate_display;
20189 if (dir > 0)
20191 if (row->reversed_p && !row->continued_p)
20193 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20194 w->cursor.vpos = -1;
20195 return make_number (PT);
20197 g = row->glyphs[TEXT_AREA];
20198 e = g + row->used[TEXT_AREA];
20199 for ( ; g < e; g++)
20201 if (BUFFERP (g->object)
20202 /* Empty lines have only one glyph, which stands
20203 for the newline, and whose charpos is the
20204 buffer position of the newline. */
20205 || ROW_GLYPH_NEWLINE_P (row, g)
20206 /* When the buffer ends in a newline, the line at
20207 EOB also has one glyph, but its charpos is -1. */
20208 || (row->ends_at_zv_p
20209 && !row->reversed_p
20210 && INTEGERP (g->object)
20211 && g->type == CHAR_GLYPH
20212 && g->u.ch == ' '))
20214 if (g->charpos > 0)
20215 SET_PT (g->charpos);
20216 else if (!row->reversed_p
20217 && row->ends_at_zv_p
20218 && PT != ZV)
20219 SET_PT (ZV);
20220 else
20221 continue;
20222 w->cursor.vpos = -1;
20223 return make_number (PT);
20227 else
20229 if (!row->reversed_p && !row->continued_p)
20231 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20232 w->cursor.vpos = -1;
20233 return make_number (PT);
20235 e = row->glyphs[TEXT_AREA];
20236 g = e + row->used[TEXT_AREA] - 1;
20237 for ( ; g >= e; g--)
20239 if (BUFFERP (g->object)
20240 || (ROW_GLYPH_NEWLINE_P (row, g)
20241 && g->charpos > 0)
20242 /* Empty R2L lines on GUI frames have the buffer
20243 position of the newline stored in the stretch
20244 glyph. */
20245 || g->type == STRETCH_GLYPH
20246 || (row->ends_at_zv_p
20247 && row->reversed_p
20248 && INTEGERP (g->object)
20249 && g->type == CHAR_GLYPH
20250 && g->u.ch == ' '))
20252 if (g->charpos > 0)
20253 SET_PT (g->charpos);
20254 else if (row->reversed_p
20255 && row->ends_at_zv_p
20256 && PT != ZV)
20257 SET_PT (ZV);
20258 else
20259 continue;
20260 w->cursor.vpos = -1;
20261 return make_number (PT);
20268 simulate_display:
20270 /* If we wind up here, we failed to move by using the glyphs, so we
20271 need to simulate display instead. */
20273 if (b)
20274 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20275 else
20276 paragraph_dir = Qleft_to_right;
20277 if (EQ (paragraph_dir, Qright_to_left))
20278 dir = -dir;
20279 if (PT <= BEGV && dir < 0)
20280 xsignal0 (Qbeginning_of_buffer);
20281 else if (PT >= ZV && dir > 0)
20282 xsignal0 (Qend_of_buffer);
20283 else
20285 struct text_pos pt;
20286 struct it it;
20287 int pt_x, target_x, pixel_width, pt_vpos;
20288 bool at_eol_p;
20289 bool overshoot_expected = false;
20290 bool target_is_eol_p = false;
20292 /* Setup the arena. */
20293 SET_TEXT_POS (pt, PT, PT_BYTE);
20294 start_display (&it, w, pt);
20296 if (it.cmp_it.id < 0
20297 && it.method == GET_FROM_STRING
20298 && it.area == TEXT_AREA
20299 && it.string_from_display_prop_p
20300 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20301 overshoot_expected = true;
20303 /* Find the X coordinate of point. We start from the beginning
20304 of this or previous line to make sure we are before point in
20305 the logical order (since the move_it_* functions can only
20306 move forward). */
20307 reseat_at_previous_visible_line_start (&it);
20308 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20309 if (IT_CHARPOS (it) != PT)
20310 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20311 -1, -1, -1, MOVE_TO_POS);
20312 pt_x = it.current_x;
20313 pt_vpos = it.vpos;
20314 if (dir > 0 || overshoot_expected)
20316 struct glyph_row *row = it.glyph_row;
20318 /* When point is at beginning of line, we don't have
20319 information about the glyph there loaded into struct
20320 it. Calling get_next_display_element fixes that. */
20321 if (pt_x == 0)
20322 get_next_display_element (&it);
20323 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20324 it.glyph_row = NULL;
20325 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20326 it.glyph_row = row;
20327 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20328 it, lest it will become out of sync with it's buffer
20329 position. */
20330 it.current_x = pt_x;
20332 else
20333 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20334 pixel_width = it.pixel_width;
20335 if (overshoot_expected && at_eol_p)
20336 pixel_width = 0;
20337 else if (pixel_width <= 0)
20338 pixel_width = 1;
20340 /* If there's a display string at point, we are actually at the
20341 glyph to the left of point, so we need to correct the X
20342 coordinate. */
20343 if (overshoot_expected)
20344 pt_x += pixel_width;
20346 /* Compute target X coordinate, either to the left or to the
20347 right of point. On TTY frames, all characters have the same
20348 pixel width of 1, so we can use that. On GUI frames we don't
20349 have an easy way of getting at the pixel width of the
20350 character to the left of point, so we use a different method
20351 of getting to that place. */
20352 if (dir > 0)
20353 target_x = pt_x + pixel_width;
20354 else
20355 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20357 /* Target X coordinate could be one line above or below the line
20358 of point, in which case we need to adjust the target X
20359 coordinate. Also, if moving to the left, we need to begin at
20360 the left edge of the point's screen line. */
20361 if (dir < 0)
20363 if (pt_x > 0)
20365 start_display (&it, w, pt);
20366 reseat_at_previous_visible_line_start (&it);
20367 it.current_x = it.current_y = it.hpos = 0;
20368 if (pt_vpos != 0)
20369 move_it_by_lines (&it, pt_vpos);
20371 else
20373 move_it_by_lines (&it, -1);
20374 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20375 target_is_eol_p = true;
20378 else
20380 if (at_eol_p
20381 || (target_x >= it.last_visible_x
20382 && it.line_wrap != TRUNCATE))
20384 if (pt_x > 0)
20385 move_it_by_lines (&it, 0);
20386 move_it_by_lines (&it, 1);
20387 target_x = 0;
20391 /* Move to the target X coordinate. */
20392 #ifdef HAVE_WINDOW_SYSTEM
20393 /* On GUI frames, as we don't know the X coordinate of the
20394 character to the left of point, moving point to the left
20395 requires walking, one grapheme cluster at a time, until we
20396 find ourself at a place immediately to the left of the
20397 character at point. */
20398 if (FRAME_WINDOW_P (it.f) && dir < 0)
20400 struct text_pos new_pos = it.current.pos;
20401 enum move_it_result rc = MOVE_X_REACHED;
20403 while (it.current_x + it.pixel_width <= target_x
20404 && rc == MOVE_X_REACHED)
20406 int new_x = it.current_x + it.pixel_width;
20408 new_pos = it.current.pos;
20409 if (new_x == it.current_x)
20410 new_x++;
20411 rc = move_it_in_display_line_to (&it, ZV, new_x,
20412 MOVE_TO_POS | MOVE_TO_X);
20413 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20414 break;
20416 /* If we ended up on a composed character inside
20417 bidi-reordered text (e.g., Hebrew text with diacritics),
20418 the iterator gives us the buffer position of the last (in
20419 logical order) character of the composed grapheme cluster,
20420 which is not what we want. So we cheat: we compute the
20421 character position of the character that follows (in the
20422 logical order) the one where the above loop stopped. That
20423 character will appear on display to the left of point. */
20424 if (it.bidi_p
20425 && it.bidi_it.scan_dir == -1
20426 && new_pos.charpos - IT_CHARPOS (it) > 1)
20428 new_pos.charpos = IT_CHARPOS (it) + 1;
20429 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20431 it.current.pos = new_pos;
20433 else
20434 #endif
20435 if (it.current_x != target_x)
20436 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20438 /* When lines are truncated, the above loop will stop at the
20439 window edge. But we want to get to the end of line, even if
20440 it is beyond the window edge; automatic hscroll will then
20441 scroll the window to show point as appropriate. */
20442 if (target_is_eol_p && it.line_wrap == TRUNCATE
20443 && get_next_display_element (&it))
20445 struct text_pos new_pos = it.current.pos;
20447 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20449 set_iterator_to_next (&it, 0);
20450 if (it.method == GET_FROM_BUFFER)
20451 new_pos = it.current.pos;
20452 if (!get_next_display_element (&it))
20453 break;
20456 it.current.pos = new_pos;
20459 /* If we ended up in a display string that covers point, move to
20460 buffer position to the right in the visual order. */
20461 if (dir > 0)
20463 while (IT_CHARPOS (it) == PT)
20465 set_iterator_to_next (&it, 0);
20466 if (!get_next_display_element (&it))
20467 break;
20471 /* Move point to that position. */
20472 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20475 return make_number (PT);
20477 #undef ROW_GLYPH_NEWLINE_P
20481 /***********************************************************************
20482 Menu Bar
20483 ***********************************************************************/
20485 /* Redisplay the menu bar in the frame for window W.
20487 The menu bar of X frames that don't have X toolkit support is
20488 displayed in a special window W->frame->menu_bar_window.
20490 The menu bar of terminal frames is treated specially as far as
20491 glyph matrices are concerned. Menu bar lines are not part of
20492 windows, so the update is done directly on the frame matrix rows
20493 for the menu bar. */
20495 static void
20496 display_menu_bar (struct window *w)
20498 struct frame *f = XFRAME (WINDOW_FRAME (w));
20499 struct it it;
20500 Lisp_Object items;
20501 int i;
20503 /* Don't do all this for graphical frames. */
20504 #ifdef HAVE_NTGUI
20505 if (FRAME_W32_P (f))
20506 return;
20507 #endif
20508 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20509 if (FRAME_X_P (f))
20510 return;
20511 #endif
20513 #ifdef HAVE_NS
20514 if (FRAME_NS_P (f))
20515 return;
20516 #endif /* HAVE_NS */
20518 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20519 eassert (!FRAME_WINDOW_P (f));
20520 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20521 it.first_visible_x = 0;
20522 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20523 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20524 if (FRAME_WINDOW_P (f))
20526 /* Menu bar lines are displayed in the desired matrix of the
20527 dummy window menu_bar_window. */
20528 struct window *menu_w;
20529 menu_w = XWINDOW (f->menu_bar_window);
20530 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20531 MENU_FACE_ID);
20532 it.first_visible_x = 0;
20533 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20535 else
20536 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20538 /* This is a TTY frame, i.e. character hpos/vpos are used as
20539 pixel x/y. */
20540 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20541 MENU_FACE_ID);
20542 it.first_visible_x = 0;
20543 it.last_visible_x = FRAME_COLS (f);
20546 /* FIXME: This should be controlled by a user option. See the
20547 comments in redisplay_tool_bar and display_mode_line about
20548 this. */
20549 it.paragraph_embedding = L2R;
20551 /* Clear all rows of the menu bar. */
20552 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20554 struct glyph_row *row = it.glyph_row + i;
20555 clear_glyph_row (row);
20556 row->enabled_p = 1;
20557 row->full_width_p = 1;
20560 /* Display all items of the menu bar. */
20561 items = FRAME_MENU_BAR_ITEMS (it.f);
20562 for (i = 0; i < ASIZE (items); i += 4)
20564 Lisp_Object string;
20566 /* Stop at nil string. */
20567 string = AREF (items, i + 1);
20568 if (NILP (string))
20569 break;
20571 /* Remember where item was displayed. */
20572 ASET (items, i + 3, make_number (it.hpos));
20574 /* Display the item, pad with one space. */
20575 if (it.current_x < it.last_visible_x)
20576 display_string (NULL, string, Qnil, 0, 0, &it,
20577 SCHARS (string) + 1, 0, 0, -1);
20580 /* Fill out the line with spaces. */
20581 if (it.current_x < it.last_visible_x)
20582 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20584 /* Compute the total height of the lines. */
20585 compute_line_metrics (&it);
20590 /***********************************************************************
20591 Mode Line
20592 ***********************************************************************/
20594 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20595 FORCE is non-zero, redisplay mode lines unconditionally.
20596 Otherwise, redisplay only mode lines that are garbaged. Value is
20597 the number of windows whose mode lines were redisplayed. */
20599 static int
20600 redisplay_mode_lines (Lisp_Object window, int force)
20602 int nwindows = 0;
20604 while (!NILP (window))
20606 struct window *w = XWINDOW (window);
20608 if (WINDOWP (w->contents))
20609 nwindows += redisplay_mode_lines (w->contents, force);
20610 else if (force
20611 || FRAME_GARBAGED_P (XFRAME (w->frame))
20612 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20614 struct text_pos lpoint;
20615 struct buffer *old = current_buffer;
20617 /* Set the window's buffer for the mode line display. */
20618 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20619 set_buffer_internal_1 (XBUFFER (w->contents));
20621 /* Point refers normally to the selected window. For any
20622 other window, set up appropriate value. */
20623 if (!EQ (window, selected_window))
20625 struct text_pos pt;
20627 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20628 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20631 /* Display mode lines. */
20632 clear_glyph_matrix (w->desired_matrix);
20633 if (display_mode_lines (w))
20635 ++nwindows;
20636 w->must_be_updated_p = 1;
20639 /* Restore old settings. */
20640 set_buffer_internal_1 (old);
20641 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20644 window = w->next;
20647 return nwindows;
20651 /* Display the mode and/or header line of window W. Value is the
20652 sum number of mode lines and header lines displayed. */
20654 static int
20655 display_mode_lines (struct window *w)
20657 Lisp_Object old_selected_window = selected_window;
20658 Lisp_Object old_selected_frame = selected_frame;
20659 Lisp_Object new_frame = w->frame;
20660 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20661 int n = 0;
20663 selected_frame = new_frame;
20664 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20665 or window's point, then we'd need select_window_1 here as well. */
20666 XSETWINDOW (selected_window, w);
20667 XFRAME (new_frame)->selected_window = selected_window;
20669 /* These will be set while the mode line specs are processed. */
20670 line_number_displayed = 0;
20671 w->column_number_displayed = -1;
20673 if (WINDOW_WANTS_MODELINE_P (w))
20675 struct window *sel_w = XWINDOW (old_selected_window);
20677 /* Select mode line face based on the real selected window. */
20678 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20679 BVAR (current_buffer, mode_line_format));
20680 ++n;
20683 if (WINDOW_WANTS_HEADER_LINE_P (w))
20685 display_mode_line (w, HEADER_LINE_FACE_ID,
20686 BVAR (current_buffer, header_line_format));
20687 ++n;
20690 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20691 selected_frame = old_selected_frame;
20692 selected_window = old_selected_window;
20693 return n;
20697 /* Display mode or header line of window W. FACE_ID specifies which
20698 line to display; it is either MODE_LINE_FACE_ID or
20699 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20700 display. Value is the pixel height of the mode/header line
20701 displayed. */
20703 static int
20704 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20706 struct it it;
20707 struct face *face;
20708 ptrdiff_t count = SPECPDL_INDEX ();
20710 init_iterator (&it, w, -1, -1, NULL, face_id);
20711 /* Don't extend on a previously drawn mode-line.
20712 This may happen if called from pos_visible_p. */
20713 it.glyph_row->enabled_p = 0;
20714 prepare_desired_row (it.glyph_row);
20716 it.glyph_row->mode_line_p = 1;
20718 /* FIXME: This should be controlled by a user option. But
20719 supporting such an option is not trivial, since the mode line is
20720 made up of many separate strings. */
20721 it.paragraph_embedding = L2R;
20723 record_unwind_protect (unwind_format_mode_line,
20724 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20726 mode_line_target = MODE_LINE_DISPLAY;
20728 /* Temporarily make frame's keyboard the current kboard so that
20729 kboard-local variables in the mode_line_format will get the right
20730 values. */
20731 push_kboard (FRAME_KBOARD (it.f));
20732 record_unwind_save_match_data ();
20733 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20734 pop_kboard ();
20736 unbind_to (count, Qnil);
20738 /* Fill up with spaces. */
20739 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20741 compute_line_metrics (&it);
20742 it.glyph_row->full_width_p = 1;
20743 it.glyph_row->continued_p = 0;
20744 it.glyph_row->truncated_on_left_p = 0;
20745 it.glyph_row->truncated_on_right_p = 0;
20747 /* Make a 3D mode-line have a shadow at its right end. */
20748 face = FACE_FROM_ID (it.f, face_id);
20749 extend_face_to_end_of_line (&it);
20750 if (face->box != FACE_NO_BOX)
20752 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20753 + it.glyph_row->used[TEXT_AREA] - 1);
20754 last->right_box_line_p = 1;
20757 return it.glyph_row->height;
20760 /* Move element ELT in LIST to the front of LIST.
20761 Return the updated list. */
20763 static Lisp_Object
20764 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20766 register Lisp_Object tail, prev;
20767 register Lisp_Object tem;
20769 tail = list;
20770 prev = Qnil;
20771 while (CONSP (tail))
20773 tem = XCAR (tail);
20775 if (EQ (elt, tem))
20777 /* Splice out the link TAIL. */
20778 if (NILP (prev))
20779 list = XCDR (tail);
20780 else
20781 Fsetcdr (prev, XCDR (tail));
20783 /* Now make it the first. */
20784 Fsetcdr (tail, list);
20785 return tail;
20787 else
20788 prev = tail;
20789 tail = XCDR (tail);
20790 QUIT;
20793 /* Not found--return unchanged LIST. */
20794 return list;
20797 /* Contribute ELT to the mode line for window IT->w. How it
20798 translates into text depends on its data type.
20800 IT describes the display environment in which we display, as usual.
20802 DEPTH is the depth in recursion. It is used to prevent
20803 infinite recursion here.
20805 FIELD_WIDTH is the number of characters the display of ELT should
20806 occupy in the mode line, and PRECISION is the maximum number of
20807 characters to display from ELT's representation. See
20808 display_string for details.
20810 Returns the hpos of the end of the text generated by ELT.
20812 PROPS is a property list to add to any string we encounter.
20814 If RISKY is nonzero, remove (disregard) any properties in any string
20815 we encounter, and ignore :eval and :propertize.
20817 The global variable `mode_line_target' determines whether the
20818 output is passed to `store_mode_line_noprop',
20819 `store_mode_line_string', or `display_string'. */
20821 static int
20822 display_mode_element (struct it *it, int depth, int field_width, int precision,
20823 Lisp_Object elt, Lisp_Object props, int risky)
20825 int n = 0, field, prec;
20826 int literal = 0;
20828 tail_recurse:
20829 if (depth > 100)
20830 elt = build_string ("*too-deep*");
20832 depth++;
20834 switch (XTYPE (elt))
20836 case Lisp_String:
20838 /* A string: output it and check for %-constructs within it. */
20839 unsigned char c;
20840 ptrdiff_t offset = 0;
20842 if (SCHARS (elt) > 0
20843 && (!NILP (props) || risky))
20845 Lisp_Object oprops, aelt;
20846 oprops = Ftext_properties_at (make_number (0), elt);
20848 /* If the starting string's properties are not what
20849 we want, translate the string. Also, if the string
20850 is risky, do that anyway. */
20852 if (NILP (Fequal (props, oprops)) || risky)
20854 /* If the starting string has properties,
20855 merge the specified ones onto the existing ones. */
20856 if (! NILP (oprops) && !risky)
20858 Lisp_Object tem;
20860 oprops = Fcopy_sequence (oprops);
20861 tem = props;
20862 while (CONSP (tem))
20864 oprops = Fplist_put (oprops, XCAR (tem),
20865 XCAR (XCDR (tem)));
20866 tem = XCDR (XCDR (tem));
20868 props = oprops;
20871 aelt = Fassoc (elt, mode_line_proptrans_alist);
20872 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20874 /* AELT is what we want. Move it to the front
20875 without consing. */
20876 elt = XCAR (aelt);
20877 mode_line_proptrans_alist
20878 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20880 else
20882 Lisp_Object tem;
20884 /* If AELT has the wrong props, it is useless.
20885 so get rid of it. */
20886 if (! NILP (aelt))
20887 mode_line_proptrans_alist
20888 = Fdelq (aelt, mode_line_proptrans_alist);
20890 elt = Fcopy_sequence (elt);
20891 Fset_text_properties (make_number (0), Flength (elt),
20892 props, elt);
20893 /* Add this item to mode_line_proptrans_alist. */
20894 mode_line_proptrans_alist
20895 = Fcons (Fcons (elt, props),
20896 mode_line_proptrans_alist);
20897 /* Truncate mode_line_proptrans_alist
20898 to at most 50 elements. */
20899 tem = Fnthcdr (make_number (50),
20900 mode_line_proptrans_alist);
20901 if (! NILP (tem))
20902 XSETCDR (tem, Qnil);
20907 offset = 0;
20909 if (literal)
20911 prec = precision - n;
20912 switch (mode_line_target)
20914 case MODE_LINE_NOPROP:
20915 case MODE_LINE_TITLE:
20916 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20917 break;
20918 case MODE_LINE_STRING:
20919 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20920 break;
20921 case MODE_LINE_DISPLAY:
20922 n += display_string (NULL, elt, Qnil, 0, 0, it,
20923 0, prec, 0, STRING_MULTIBYTE (elt));
20924 break;
20927 break;
20930 /* Handle the non-literal case. */
20932 while ((precision <= 0 || n < precision)
20933 && SREF (elt, offset) != 0
20934 && (mode_line_target != MODE_LINE_DISPLAY
20935 || it->current_x < it->last_visible_x))
20937 ptrdiff_t last_offset = offset;
20939 /* Advance to end of string or next format specifier. */
20940 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20943 if (offset - 1 != last_offset)
20945 ptrdiff_t nchars, nbytes;
20947 /* Output to end of string or up to '%'. Field width
20948 is length of string. Don't output more than
20949 PRECISION allows us. */
20950 offset--;
20952 prec = c_string_width (SDATA (elt) + last_offset,
20953 offset - last_offset, precision - n,
20954 &nchars, &nbytes);
20956 switch (mode_line_target)
20958 case MODE_LINE_NOPROP:
20959 case MODE_LINE_TITLE:
20960 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20961 break;
20962 case MODE_LINE_STRING:
20964 ptrdiff_t bytepos = last_offset;
20965 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20966 ptrdiff_t endpos = (precision <= 0
20967 ? string_byte_to_char (elt, offset)
20968 : charpos + nchars);
20970 n += store_mode_line_string (NULL,
20971 Fsubstring (elt, make_number (charpos),
20972 make_number (endpos)),
20973 0, 0, 0, Qnil);
20975 break;
20976 case MODE_LINE_DISPLAY:
20978 ptrdiff_t bytepos = last_offset;
20979 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20981 if (precision <= 0)
20982 nchars = string_byte_to_char (elt, offset) - charpos;
20983 n += display_string (NULL, elt, Qnil, 0, charpos,
20984 it, 0, nchars, 0,
20985 STRING_MULTIBYTE (elt));
20987 break;
20990 else /* c == '%' */
20992 ptrdiff_t percent_position = offset;
20994 /* Get the specified minimum width. Zero means
20995 don't pad. */
20996 field = 0;
20997 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20998 field = field * 10 + c - '0';
21000 /* Don't pad beyond the total padding allowed. */
21001 if (field_width - n > 0 && field > field_width - n)
21002 field = field_width - n;
21004 /* Note that either PRECISION <= 0 or N < PRECISION. */
21005 prec = precision - n;
21007 if (c == 'M')
21008 n += display_mode_element (it, depth, field, prec,
21009 Vglobal_mode_string, props,
21010 risky);
21011 else if (c != 0)
21013 bool multibyte;
21014 ptrdiff_t bytepos, charpos;
21015 const char *spec;
21016 Lisp_Object string;
21018 bytepos = percent_position;
21019 charpos = (STRING_MULTIBYTE (elt)
21020 ? string_byte_to_char (elt, bytepos)
21021 : bytepos);
21022 spec = decode_mode_spec (it->w, c, field, &string);
21023 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21025 switch (mode_line_target)
21027 case MODE_LINE_NOPROP:
21028 case MODE_LINE_TITLE:
21029 n += store_mode_line_noprop (spec, field, prec);
21030 break;
21031 case MODE_LINE_STRING:
21033 Lisp_Object tem = build_string (spec);
21034 props = Ftext_properties_at (make_number (charpos), elt);
21035 /* Should only keep face property in props */
21036 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21038 break;
21039 case MODE_LINE_DISPLAY:
21041 int nglyphs_before, nwritten;
21043 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21044 nwritten = display_string (spec, string, elt,
21045 charpos, 0, it,
21046 field, prec, 0,
21047 multibyte);
21049 /* Assign to the glyphs written above the
21050 string where the `%x' came from, position
21051 of the `%'. */
21052 if (nwritten > 0)
21054 struct glyph *glyph
21055 = (it->glyph_row->glyphs[TEXT_AREA]
21056 + nglyphs_before);
21057 int i;
21059 for (i = 0; i < nwritten; ++i)
21061 glyph[i].object = elt;
21062 glyph[i].charpos = charpos;
21065 n += nwritten;
21068 break;
21071 else /* c == 0 */
21072 break;
21076 break;
21078 case Lisp_Symbol:
21079 /* A symbol: process the value of the symbol recursively
21080 as if it appeared here directly. Avoid error if symbol void.
21081 Special case: if value of symbol is a string, output the string
21082 literally. */
21084 register Lisp_Object tem;
21086 /* If the variable is not marked as risky to set
21087 then its contents are risky to use. */
21088 if (NILP (Fget (elt, Qrisky_local_variable)))
21089 risky = 1;
21091 tem = Fboundp (elt);
21092 if (!NILP (tem))
21094 tem = Fsymbol_value (elt);
21095 /* If value is a string, output that string literally:
21096 don't check for % within it. */
21097 if (STRINGP (tem))
21098 literal = 1;
21100 if (!EQ (tem, elt))
21102 /* Give up right away for nil or t. */
21103 elt = tem;
21104 goto tail_recurse;
21108 break;
21110 case Lisp_Cons:
21112 register Lisp_Object car, tem;
21114 /* A cons cell: five distinct cases.
21115 If first element is :eval or :propertize, do something special.
21116 If first element is a string or a cons, process all the elements
21117 and effectively concatenate them.
21118 If first element is a negative number, truncate displaying cdr to
21119 at most that many characters. If positive, pad (with spaces)
21120 to at least that many characters.
21121 If first element is a symbol, process the cadr or caddr recursively
21122 according to whether the symbol's value is non-nil or nil. */
21123 car = XCAR (elt);
21124 if (EQ (car, QCeval))
21126 /* An element of the form (:eval FORM) means evaluate FORM
21127 and use the result as mode line elements. */
21129 if (risky)
21130 break;
21132 if (CONSP (XCDR (elt)))
21134 Lisp_Object spec;
21135 spec = safe_eval (XCAR (XCDR (elt)));
21136 n += display_mode_element (it, depth, field_width - n,
21137 precision - n, spec, props,
21138 risky);
21141 else if (EQ (car, QCpropertize))
21143 /* An element of the form (:propertize ELT PROPS...)
21144 means display ELT but applying properties PROPS. */
21146 if (risky)
21147 break;
21149 if (CONSP (XCDR (elt)))
21150 n += display_mode_element (it, depth, field_width - n,
21151 precision - n, XCAR (XCDR (elt)),
21152 XCDR (XCDR (elt)), risky);
21154 else if (SYMBOLP (car))
21156 tem = Fboundp (car);
21157 elt = XCDR (elt);
21158 if (!CONSP (elt))
21159 goto invalid;
21160 /* elt is now the cdr, and we know it is a cons cell.
21161 Use its car if CAR has a non-nil value. */
21162 if (!NILP (tem))
21164 tem = Fsymbol_value (car);
21165 if (!NILP (tem))
21167 elt = XCAR (elt);
21168 goto tail_recurse;
21171 /* Symbol's value is nil (or symbol is unbound)
21172 Get the cddr of the original list
21173 and if possible find the caddr and use that. */
21174 elt = XCDR (elt);
21175 if (NILP (elt))
21176 break;
21177 else if (!CONSP (elt))
21178 goto invalid;
21179 elt = XCAR (elt);
21180 goto tail_recurse;
21182 else if (INTEGERP (car))
21184 register int lim = XINT (car);
21185 elt = XCDR (elt);
21186 if (lim < 0)
21188 /* Negative int means reduce maximum width. */
21189 if (precision <= 0)
21190 precision = -lim;
21191 else
21192 precision = min (precision, -lim);
21194 else if (lim > 0)
21196 /* Padding specified. Don't let it be more than
21197 current maximum. */
21198 if (precision > 0)
21199 lim = min (precision, lim);
21201 /* If that's more padding than already wanted, queue it.
21202 But don't reduce padding already specified even if
21203 that is beyond the current truncation point. */
21204 field_width = max (lim, field_width);
21206 goto tail_recurse;
21208 else if (STRINGP (car) || CONSP (car))
21210 Lisp_Object halftail = elt;
21211 int len = 0;
21213 while (CONSP (elt)
21214 && (precision <= 0 || n < precision))
21216 n += display_mode_element (it, depth,
21217 /* Do padding only after the last
21218 element in the list. */
21219 (! CONSP (XCDR (elt))
21220 ? field_width - n
21221 : 0),
21222 precision - n, XCAR (elt),
21223 props, risky);
21224 elt = XCDR (elt);
21225 len++;
21226 if ((len & 1) == 0)
21227 halftail = XCDR (halftail);
21228 /* Check for cycle. */
21229 if (EQ (halftail, elt))
21230 break;
21234 break;
21236 default:
21237 invalid:
21238 elt = build_string ("*invalid*");
21239 goto tail_recurse;
21242 /* Pad to FIELD_WIDTH. */
21243 if (field_width > 0 && n < field_width)
21245 switch (mode_line_target)
21247 case MODE_LINE_NOPROP:
21248 case MODE_LINE_TITLE:
21249 n += store_mode_line_noprop ("", field_width - n, 0);
21250 break;
21251 case MODE_LINE_STRING:
21252 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21253 break;
21254 case MODE_LINE_DISPLAY:
21255 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21256 0, 0, 0);
21257 break;
21261 return n;
21264 /* Store a mode-line string element in mode_line_string_list.
21266 If STRING is non-null, display that C string. Otherwise, the Lisp
21267 string LISP_STRING is displayed.
21269 FIELD_WIDTH is the minimum number of output glyphs to produce.
21270 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21271 with spaces. FIELD_WIDTH <= 0 means don't pad.
21273 PRECISION is the maximum number of characters to output from
21274 STRING. PRECISION <= 0 means don't truncate the string.
21276 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21277 properties to the string.
21279 PROPS are the properties to add to the string.
21280 The mode_line_string_face face property is always added to the string.
21283 static int
21284 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21285 int field_width, int precision, Lisp_Object props)
21287 ptrdiff_t len;
21288 int n = 0;
21290 if (string != NULL)
21292 len = strlen (string);
21293 if (precision > 0 && len > precision)
21294 len = precision;
21295 lisp_string = make_string (string, len);
21296 if (NILP (props))
21297 props = mode_line_string_face_prop;
21298 else if (!NILP (mode_line_string_face))
21300 Lisp_Object face = Fplist_get (props, Qface);
21301 props = Fcopy_sequence (props);
21302 if (NILP (face))
21303 face = mode_line_string_face;
21304 else
21305 face = list2 (face, mode_line_string_face);
21306 props = Fplist_put (props, Qface, face);
21308 Fadd_text_properties (make_number (0), make_number (len),
21309 props, lisp_string);
21311 else
21313 len = XFASTINT (Flength (lisp_string));
21314 if (precision > 0 && len > precision)
21316 len = precision;
21317 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21318 precision = -1;
21320 if (!NILP (mode_line_string_face))
21322 Lisp_Object face;
21323 if (NILP (props))
21324 props = Ftext_properties_at (make_number (0), lisp_string);
21325 face = Fplist_get (props, Qface);
21326 if (NILP (face))
21327 face = mode_line_string_face;
21328 else
21329 face = list2 (face, mode_line_string_face);
21330 props = list2 (Qface, face);
21331 if (copy_string)
21332 lisp_string = Fcopy_sequence (lisp_string);
21334 if (!NILP (props))
21335 Fadd_text_properties (make_number (0), make_number (len),
21336 props, lisp_string);
21339 if (len > 0)
21341 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21342 n += len;
21345 if (field_width > len)
21347 field_width -= len;
21348 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21349 if (!NILP (props))
21350 Fadd_text_properties (make_number (0), make_number (field_width),
21351 props, lisp_string);
21352 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21353 n += field_width;
21356 return n;
21360 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21361 1, 4, 0,
21362 doc: /* Format a string out of a mode line format specification.
21363 First arg FORMAT specifies the mode line format (see `mode-line-format'
21364 for details) to use.
21366 By default, the format is evaluated for the currently selected window.
21368 Optional second arg FACE specifies the face property to put on all
21369 characters for which no face is specified. The value nil means the
21370 default face. The value t means whatever face the window's mode line
21371 currently uses (either `mode-line' or `mode-line-inactive',
21372 depending on whether the window is the selected window or not).
21373 An integer value means the value string has no text
21374 properties.
21376 Optional third and fourth args WINDOW and BUFFER specify the window
21377 and buffer to use as the context for the formatting (defaults
21378 are the selected window and the WINDOW's buffer). */)
21379 (Lisp_Object format, Lisp_Object face,
21380 Lisp_Object window, Lisp_Object buffer)
21382 struct it it;
21383 int len;
21384 struct window *w;
21385 struct buffer *old_buffer = NULL;
21386 int face_id;
21387 int no_props = INTEGERP (face);
21388 ptrdiff_t count = SPECPDL_INDEX ();
21389 Lisp_Object str;
21390 int string_start = 0;
21392 w = decode_any_window (window);
21393 XSETWINDOW (window, w);
21395 if (NILP (buffer))
21396 buffer = w->contents;
21397 CHECK_BUFFER (buffer);
21399 /* Make formatting the modeline a non-op when noninteractive, otherwise
21400 there will be problems later caused by a partially initialized frame. */
21401 if (NILP (format) || noninteractive)
21402 return empty_unibyte_string;
21404 if (no_props)
21405 face = Qnil;
21407 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21408 : EQ (face, Qt) ? (EQ (window, selected_window)
21409 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21410 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21411 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21412 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21413 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21414 : DEFAULT_FACE_ID;
21416 old_buffer = current_buffer;
21418 /* Save things including mode_line_proptrans_alist,
21419 and set that to nil so that we don't alter the outer value. */
21420 record_unwind_protect (unwind_format_mode_line,
21421 format_mode_line_unwind_data
21422 (XFRAME (WINDOW_FRAME (w)),
21423 old_buffer, selected_window, 1));
21424 mode_line_proptrans_alist = Qnil;
21426 Fselect_window (window, Qt);
21427 set_buffer_internal_1 (XBUFFER (buffer));
21429 init_iterator (&it, w, -1, -1, NULL, face_id);
21431 if (no_props)
21433 mode_line_target = MODE_LINE_NOPROP;
21434 mode_line_string_face_prop = Qnil;
21435 mode_line_string_list = Qnil;
21436 string_start = MODE_LINE_NOPROP_LEN (0);
21438 else
21440 mode_line_target = MODE_LINE_STRING;
21441 mode_line_string_list = Qnil;
21442 mode_line_string_face = face;
21443 mode_line_string_face_prop
21444 = NILP (face) ? Qnil : list2 (Qface, face);
21447 push_kboard (FRAME_KBOARD (it.f));
21448 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21449 pop_kboard ();
21451 if (no_props)
21453 len = MODE_LINE_NOPROP_LEN (string_start);
21454 str = make_string (mode_line_noprop_buf + string_start, len);
21456 else
21458 mode_line_string_list = Fnreverse (mode_line_string_list);
21459 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21460 empty_unibyte_string);
21463 unbind_to (count, Qnil);
21464 return str;
21467 /* Write a null-terminated, right justified decimal representation of
21468 the positive integer D to BUF using a minimal field width WIDTH. */
21470 static void
21471 pint2str (register char *buf, register int width, register ptrdiff_t d)
21473 register char *p = buf;
21475 if (d <= 0)
21476 *p++ = '0';
21477 else
21479 while (d > 0)
21481 *p++ = d % 10 + '0';
21482 d /= 10;
21486 for (width -= (int) (p - buf); width > 0; --width)
21487 *p++ = ' ';
21488 *p-- = '\0';
21489 while (p > buf)
21491 d = *buf;
21492 *buf++ = *p;
21493 *p-- = d;
21497 /* Write a null-terminated, right justified decimal and "human
21498 readable" representation of the nonnegative integer D to BUF using
21499 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21501 static const char power_letter[] =
21503 0, /* no letter */
21504 'k', /* kilo */
21505 'M', /* mega */
21506 'G', /* giga */
21507 'T', /* tera */
21508 'P', /* peta */
21509 'E', /* exa */
21510 'Z', /* zetta */
21511 'Y' /* yotta */
21514 static void
21515 pint2hrstr (char *buf, int width, ptrdiff_t d)
21517 /* We aim to represent the nonnegative integer D as
21518 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21519 ptrdiff_t quotient = d;
21520 int remainder = 0;
21521 /* -1 means: do not use TENTHS. */
21522 int tenths = -1;
21523 int exponent = 0;
21525 /* Length of QUOTIENT.TENTHS as a string. */
21526 int length;
21528 char * psuffix;
21529 char * p;
21531 if (quotient >= 1000)
21533 /* Scale to the appropriate EXPONENT. */
21536 remainder = quotient % 1000;
21537 quotient /= 1000;
21538 exponent++;
21540 while (quotient >= 1000);
21542 /* Round to nearest and decide whether to use TENTHS or not. */
21543 if (quotient <= 9)
21545 tenths = remainder / 100;
21546 if (remainder % 100 >= 50)
21548 if (tenths < 9)
21549 tenths++;
21550 else
21552 quotient++;
21553 if (quotient == 10)
21554 tenths = -1;
21555 else
21556 tenths = 0;
21560 else
21561 if (remainder >= 500)
21563 if (quotient < 999)
21564 quotient++;
21565 else
21567 quotient = 1;
21568 exponent++;
21569 tenths = 0;
21574 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21575 if (tenths == -1 && quotient <= 99)
21576 if (quotient <= 9)
21577 length = 1;
21578 else
21579 length = 2;
21580 else
21581 length = 3;
21582 p = psuffix = buf + max (width, length);
21584 /* Print EXPONENT. */
21585 *psuffix++ = power_letter[exponent];
21586 *psuffix = '\0';
21588 /* Print TENTHS. */
21589 if (tenths >= 0)
21591 *--p = '0' + tenths;
21592 *--p = '.';
21595 /* Print QUOTIENT. */
21598 int digit = quotient % 10;
21599 *--p = '0' + digit;
21601 while ((quotient /= 10) != 0);
21603 /* Print leading spaces. */
21604 while (buf < p)
21605 *--p = ' ';
21608 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21609 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21610 type of CODING_SYSTEM. Return updated pointer into BUF. */
21612 static unsigned char invalid_eol_type[] = "(*invalid*)";
21614 static char *
21615 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21617 Lisp_Object val;
21618 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21619 const unsigned char *eol_str;
21620 int eol_str_len;
21621 /* The EOL conversion we are using. */
21622 Lisp_Object eoltype;
21624 val = CODING_SYSTEM_SPEC (coding_system);
21625 eoltype = Qnil;
21627 if (!VECTORP (val)) /* Not yet decided. */
21629 *buf++ = multibyte ? '-' : ' ';
21630 if (eol_flag)
21631 eoltype = eol_mnemonic_undecided;
21632 /* Don't mention EOL conversion if it isn't decided. */
21634 else
21636 Lisp_Object attrs;
21637 Lisp_Object eolvalue;
21639 attrs = AREF (val, 0);
21640 eolvalue = AREF (val, 2);
21642 *buf++ = multibyte
21643 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21644 : ' ';
21646 if (eol_flag)
21648 /* The EOL conversion that is normal on this system. */
21650 if (NILP (eolvalue)) /* Not yet decided. */
21651 eoltype = eol_mnemonic_undecided;
21652 else if (VECTORP (eolvalue)) /* Not yet decided. */
21653 eoltype = eol_mnemonic_undecided;
21654 else /* eolvalue is Qunix, Qdos, or Qmac. */
21655 eoltype = (EQ (eolvalue, Qunix)
21656 ? eol_mnemonic_unix
21657 : (EQ (eolvalue, Qdos) == 1
21658 ? eol_mnemonic_dos : eol_mnemonic_mac));
21662 if (eol_flag)
21664 /* Mention the EOL conversion if it is not the usual one. */
21665 if (STRINGP (eoltype))
21667 eol_str = SDATA (eoltype);
21668 eol_str_len = SBYTES (eoltype);
21670 else if (CHARACTERP (eoltype))
21672 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21673 int c = XFASTINT (eoltype);
21674 eol_str_len = CHAR_STRING (c, tmp);
21675 eol_str = tmp;
21677 else
21679 eol_str = invalid_eol_type;
21680 eol_str_len = sizeof (invalid_eol_type) - 1;
21682 memcpy (buf, eol_str, eol_str_len);
21683 buf += eol_str_len;
21686 return buf;
21689 /* Return a string for the output of a mode line %-spec for window W,
21690 generated by character C. FIELD_WIDTH > 0 means pad the string
21691 returned with spaces to that value. Return a Lisp string in
21692 *STRING if the resulting string is taken from that Lisp string.
21694 Note we operate on the current buffer for most purposes. */
21696 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21698 static const char *
21699 decode_mode_spec (struct window *w, register int c, int field_width,
21700 Lisp_Object *string)
21702 Lisp_Object obj;
21703 struct frame *f = XFRAME (WINDOW_FRAME (w));
21704 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21705 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21706 produce strings from numerical values, so limit preposterously
21707 large values of FIELD_WIDTH to avoid overrunning the buffer's
21708 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21709 bytes plus the terminating null. */
21710 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21711 struct buffer *b = current_buffer;
21713 obj = Qnil;
21714 *string = Qnil;
21716 switch (c)
21718 case '*':
21719 if (!NILP (BVAR (b, read_only)))
21720 return "%";
21721 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21722 return "*";
21723 return "-";
21725 case '+':
21726 /* This differs from %* only for a modified read-only buffer. */
21727 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21728 return "*";
21729 if (!NILP (BVAR (b, read_only)))
21730 return "%";
21731 return "-";
21733 case '&':
21734 /* This differs from %* in ignoring read-only-ness. */
21735 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21736 return "*";
21737 return "-";
21739 case '%':
21740 return "%";
21742 case '[':
21744 int i;
21745 char *p;
21747 if (command_loop_level > 5)
21748 return "[[[... ";
21749 p = decode_mode_spec_buf;
21750 for (i = 0; i < command_loop_level; i++)
21751 *p++ = '[';
21752 *p = 0;
21753 return decode_mode_spec_buf;
21756 case ']':
21758 int i;
21759 char *p;
21761 if (command_loop_level > 5)
21762 return " ...]]]";
21763 p = decode_mode_spec_buf;
21764 for (i = 0; i < command_loop_level; i++)
21765 *p++ = ']';
21766 *p = 0;
21767 return decode_mode_spec_buf;
21770 case '-':
21772 register int i;
21774 /* Let lots_of_dashes be a string of infinite length. */
21775 if (mode_line_target == MODE_LINE_NOPROP
21776 || mode_line_target == MODE_LINE_STRING)
21777 return "--";
21778 if (field_width <= 0
21779 || field_width > sizeof (lots_of_dashes))
21781 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21782 decode_mode_spec_buf[i] = '-';
21783 decode_mode_spec_buf[i] = '\0';
21784 return decode_mode_spec_buf;
21786 else
21787 return lots_of_dashes;
21790 case 'b':
21791 obj = BVAR (b, name);
21792 break;
21794 case 'c':
21795 /* %c and %l are ignored in `frame-title-format'.
21796 (In redisplay_internal, the frame title is drawn _before_ the
21797 windows are updated, so the stuff which depends on actual
21798 window contents (such as %l) may fail to render properly, or
21799 even crash emacs.) */
21800 if (mode_line_target == MODE_LINE_TITLE)
21801 return "";
21802 else
21804 ptrdiff_t col = current_column ();
21805 w->column_number_displayed = col;
21806 pint2str (decode_mode_spec_buf, width, col);
21807 return decode_mode_spec_buf;
21810 case 'e':
21811 #ifndef SYSTEM_MALLOC
21813 if (NILP (Vmemory_full))
21814 return "";
21815 else
21816 return "!MEM FULL! ";
21818 #else
21819 return "";
21820 #endif
21822 case 'F':
21823 /* %F displays the frame name. */
21824 if (!NILP (f->title))
21825 return SSDATA (f->title);
21826 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21827 return SSDATA (f->name);
21828 return "Emacs";
21830 case 'f':
21831 obj = BVAR (b, filename);
21832 break;
21834 case 'i':
21836 ptrdiff_t size = ZV - BEGV;
21837 pint2str (decode_mode_spec_buf, width, size);
21838 return decode_mode_spec_buf;
21841 case 'I':
21843 ptrdiff_t size = ZV - BEGV;
21844 pint2hrstr (decode_mode_spec_buf, width, size);
21845 return decode_mode_spec_buf;
21848 case 'l':
21850 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21851 ptrdiff_t topline, nlines, height;
21852 ptrdiff_t junk;
21854 /* %c and %l are ignored in `frame-title-format'. */
21855 if (mode_line_target == MODE_LINE_TITLE)
21856 return "";
21858 startpos = marker_position (w->start);
21859 startpos_byte = marker_byte_position (w->start);
21860 height = WINDOW_TOTAL_LINES (w);
21862 /* If we decided that this buffer isn't suitable for line numbers,
21863 don't forget that too fast. */
21864 if (w->base_line_pos == -1)
21865 goto no_value;
21867 /* If the buffer is very big, don't waste time. */
21868 if (INTEGERP (Vline_number_display_limit)
21869 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21871 w->base_line_pos = 0;
21872 w->base_line_number = 0;
21873 goto no_value;
21876 if (w->base_line_number > 0
21877 && w->base_line_pos > 0
21878 && w->base_line_pos <= startpos)
21880 line = w->base_line_number;
21881 linepos = w->base_line_pos;
21882 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21884 else
21886 line = 1;
21887 linepos = BUF_BEGV (b);
21888 linepos_byte = BUF_BEGV_BYTE (b);
21891 /* Count lines from base line to window start position. */
21892 nlines = display_count_lines (linepos_byte,
21893 startpos_byte,
21894 startpos, &junk);
21896 topline = nlines + line;
21898 /* Determine a new base line, if the old one is too close
21899 or too far away, or if we did not have one.
21900 "Too close" means it's plausible a scroll-down would
21901 go back past it. */
21902 if (startpos == BUF_BEGV (b))
21904 w->base_line_number = topline;
21905 w->base_line_pos = BUF_BEGV (b);
21907 else if (nlines < height + 25 || nlines > height * 3 + 50
21908 || linepos == BUF_BEGV (b))
21910 ptrdiff_t limit = BUF_BEGV (b);
21911 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21912 ptrdiff_t position;
21913 ptrdiff_t distance =
21914 (height * 2 + 30) * line_number_display_limit_width;
21916 if (startpos - distance > limit)
21918 limit = startpos - distance;
21919 limit_byte = CHAR_TO_BYTE (limit);
21922 nlines = display_count_lines (startpos_byte,
21923 limit_byte,
21924 - (height * 2 + 30),
21925 &position);
21926 /* If we couldn't find the lines we wanted within
21927 line_number_display_limit_width chars per line,
21928 give up on line numbers for this window. */
21929 if (position == limit_byte && limit == startpos - distance)
21931 w->base_line_pos = -1;
21932 w->base_line_number = 0;
21933 goto no_value;
21936 w->base_line_number = topline - nlines;
21937 w->base_line_pos = BYTE_TO_CHAR (position);
21940 /* Now count lines from the start pos to point. */
21941 nlines = display_count_lines (startpos_byte,
21942 PT_BYTE, PT, &junk);
21944 /* Record that we did display the line number. */
21945 line_number_displayed = 1;
21947 /* Make the string to show. */
21948 pint2str (decode_mode_spec_buf, width, topline + nlines);
21949 return decode_mode_spec_buf;
21950 no_value:
21952 char* p = decode_mode_spec_buf;
21953 int pad = width - 2;
21954 while (pad-- > 0)
21955 *p++ = ' ';
21956 *p++ = '?';
21957 *p++ = '?';
21958 *p = '\0';
21959 return decode_mode_spec_buf;
21962 break;
21964 case 'm':
21965 obj = BVAR (b, mode_name);
21966 break;
21968 case 'n':
21969 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21970 return " Narrow";
21971 break;
21973 case 'p':
21975 ptrdiff_t pos = marker_position (w->start);
21976 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21978 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
21980 if (pos <= BUF_BEGV (b))
21981 return "All";
21982 else
21983 return "Bottom";
21985 else if (pos <= BUF_BEGV (b))
21986 return "Top";
21987 else
21989 if (total > 1000000)
21990 /* Do it differently for a large value, to avoid overflow. */
21991 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21992 else
21993 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21994 /* We can't normally display a 3-digit number,
21995 so get us a 2-digit number that is close. */
21996 if (total == 100)
21997 total = 99;
21998 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21999 return decode_mode_spec_buf;
22003 /* Display percentage of size above the bottom of the screen. */
22004 case 'P':
22006 ptrdiff_t toppos = marker_position (w->start);
22007 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22008 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22010 if (botpos >= BUF_ZV (b))
22012 if (toppos <= BUF_BEGV (b))
22013 return "All";
22014 else
22015 return "Bottom";
22017 else
22019 if (total > 1000000)
22020 /* Do it differently for a large value, to avoid overflow. */
22021 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22022 else
22023 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22024 /* We can't normally display a 3-digit number,
22025 so get us a 2-digit number that is close. */
22026 if (total == 100)
22027 total = 99;
22028 if (toppos <= BUF_BEGV (b))
22029 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22030 else
22031 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22032 return decode_mode_spec_buf;
22036 case 's':
22037 /* status of process */
22038 obj = Fget_buffer_process (Fcurrent_buffer ());
22039 if (NILP (obj))
22040 return "no process";
22041 #ifndef MSDOS
22042 obj = Fsymbol_name (Fprocess_status (obj));
22043 #endif
22044 break;
22046 case '@':
22048 ptrdiff_t count = inhibit_garbage_collection ();
22049 Lisp_Object val = call1 (intern ("file-remote-p"),
22050 BVAR (current_buffer, directory));
22051 unbind_to (count, Qnil);
22053 if (NILP (val))
22054 return "-";
22055 else
22056 return "@";
22059 case 'z':
22060 /* coding-system (not including end-of-line format) */
22061 case 'Z':
22062 /* coding-system (including end-of-line type) */
22064 int eol_flag = (c == 'Z');
22065 char *p = decode_mode_spec_buf;
22067 if (! FRAME_WINDOW_P (f))
22069 /* No need to mention EOL here--the terminal never needs
22070 to do EOL conversion. */
22071 p = decode_mode_spec_coding (CODING_ID_NAME
22072 (FRAME_KEYBOARD_CODING (f)->id),
22073 p, 0);
22074 p = decode_mode_spec_coding (CODING_ID_NAME
22075 (FRAME_TERMINAL_CODING (f)->id),
22076 p, 0);
22078 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22079 p, eol_flag);
22081 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22082 #ifdef subprocesses
22083 obj = Fget_buffer_process (Fcurrent_buffer ());
22084 if (PROCESSP (obj))
22086 p = decode_mode_spec_coding
22087 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22088 p = decode_mode_spec_coding
22089 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22091 #endif /* subprocesses */
22092 #endif /* 0 */
22093 *p = 0;
22094 return decode_mode_spec_buf;
22098 if (STRINGP (obj))
22100 *string = obj;
22101 return SSDATA (obj);
22103 else
22104 return "";
22108 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22109 means count lines back from START_BYTE. But don't go beyond
22110 LIMIT_BYTE. Return the number of lines thus found (always
22111 nonnegative).
22113 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22114 either the position COUNT lines after/before START_BYTE, if we
22115 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22116 COUNT lines. */
22118 static ptrdiff_t
22119 display_count_lines (ptrdiff_t start_byte,
22120 ptrdiff_t limit_byte, ptrdiff_t count,
22121 ptrdiff_t *byte_pos_ptr)
22123 register unsigned char *cursor;
22124 unsigned char *base;
22126 register ptrdiff_t ceiling;
22127 register unsigned char *ceiling_addr;
22128 ptrdiff_t orig_count = count;
22130 /* If we are not in selective display mode,
22131 check only for newlines. */
22132 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22133 && !INTEGERP (BVAR (current_buffer, selective_display)));
22135 if (count > 0)
22137 while (start_byte < limit_byte)
22139 ceiling = BUFFER_CEILING_OF (start_byte);
22140 ceiling = min (limit_byte - 1, ceiling);
22141 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22142 base = (cursor = BYTE_POS_ADDR (start_byte));
22146 if (selective_display)
22148 while (*cursor != '\n' && *cursor != 015
22149 && ++cursor != ceiling_addr)
22150 continue;
22151 if (cursor == ceiling_addr)
22152 break;
22154 else
22156 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22157 if (! cursor)
22158 break;
22161 cursor++;
22163 if (--count == 0)
22165 start_byte += cursor - base;
22166 *byte_pos_ptr = start_byte;
22167 return orig_count;
22170 while (cursor < ceiling_addr);
22172 start_byte += ceiling_addr - base;
22175 else
22177 while (start_byte > limit_byte)
22179 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22180 ceiling = max (limit_byte, ceiling);
22181 ceiling_addr = BYTE_POS_ADDR (ceiling);
22182 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22183 while (1)
22185 if (selective_display)
22187 while (--cursor >= ceiling_addr
22188 && *cursor != '\n' && *cursor != 015)
22189 continue;
22190 if (cursor < ceiling_addr)
22191 break;
22193 else
22195 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22196 if (! cursor)
22197 break;
22200 if (++count == 0)
22202 start_byte += cursor - base + 1;
22203 *byte_pos_ptr = start_byte;
22204 /* When scanning backwards, we should
22205 not count the newline posterior to which we stop. */
22206 return - orig_count - 1;
22209 start_byte += ceiling_addr - base;
22213 *byte_pos_ptr = limit_byte;
22215 if (count < 0)
22216 return - orig_count + count;
22217 return orig_count - count;
22223 /***********************************************************************
22224 Displaying strings
22225 ***********************************************************************/
22227 /* Display a NUL-terminated string, starting with index START.
22229 If STRING is non-null, display that C string. Otherwise, the Lisp
22230 string LISP_STRING is displayed. There's a case that STRING is
22231 non-null and LISP_STRING is not nil. It means STRING is a string
22232 data of LISP_STRING. In that case, we display LISP_STRING while
22233 ignoring its text properties.
22235 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22236 FACE_STRING. Display STRING or LISP_STRING with the face at
22237 FACE_STRING_POS in FACE_STRING:
22239 Display the string in the environment given by IT, but use the
22240 standard display table, temporarily.
22242 FIELD_WIDTH is the minimum number of output glyphs to produce.
22243 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22244 with spaces. If STRING has more characters, more than FIELD_WIDTH
22245 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22247 PRECISION is the maximum number of characters to output from
22248 STRING. PRECISION < 0 means don't truncate the string.
22250 This is roughly equivalent to printf format specifiers:
22252 FIELD_WIDTH PRECISION PRINTF
22253 ----------------------------------------
22254 -1 -1 %s
22255 -1 10 %.10s
22256 10 -1 %10s
22257 20 10 %20.10s
22259 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22260 display them, and < 0 means obey the current buffer's value of
22261 enable_multibyte_characters.
22263 Value is the number of columns displayed. */
22265 static int
22266 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22267 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22268 int field_width, int precision, int max_x, int multibyte)
22270 int hpos_at_start = it->hpos;
22271 int saved_face_id = it->face_id;
22272 struct glyph_row *row = it->glyph_row;
22273 ptrdiff_t it_charpos;
22275 /* Initialize the iterator IT for iteration over STRING beginning
22276 with index START. */
22277 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22278 precision, field_width, multibyte);
22279 if (string && STRINGP (lisp_string))
22280 /* LISP_STRING is the one returned by decode_mode_spec. We should
22281 ignore its text properties. */
22282 it->stop_charpos = it->end_charpos;
22284 /* If displaying STRING, set up the face of the iterator from
22285 FACE_STRING, if that's given. */
22286 if (STRINGP (face_string))
22288 ptrdiff_t endptr;
22289 struct face *face;
22291 it->face_id
22292 = face_at_string_position (it->w, face_string, face_string_pos,
22293 0, it->region_beg_charpos,
22294 it->region_end_charpos,
22295 &endptr, it->base_face_id, 0);
22296 face = FACE_FROM_ID (it->f, it->face_id);
22297 it->face_box_p = face->box != FACE_NO_BOX;
22300 /* Set max_x to the maximum allowed X position. Don't let it go
22301 beyond the right edge of the window. */
22302 if (max_x <= 0)
22303 max_x = it->last_visible_x;
22304 else
22305 max_x = min (max_x, it->last_visible_x);
22307 /* Skip over display elements that are not visible. because IT->w is
22308 hscrolled. */
22309 if (it->current_x < it->first_visible_x)
22310 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22311 MOVE_TO_POS | MOVE_TO_X);
22313 row->ascent = it->max_ascent;
22314 row->height = it->max_ascent + it->max_descent;
22315 row->phys_ascent = it->max_phys_ascent;
22316 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22317 row->extra_line_spacing = it->max_extra_line_spacing;
22319 if (STRINGP (it->string))
22320 it_charpos = IT_STRING_CHARPOS (*it);
22321 else
22322 it_charpos = IT_CHARPOS (*it);
22324 /* This condition is for the case that we are called with current_x
22325 past last_visible_x. */
22326 while (it->current_x < max_x)
22328 int x_before, x, n_glyphs_before, i, nglyphs;
22330 /* Get the next display element. */
22331 if (!get_next_display_element (it))
22332 break;
22334 /* Produce glyphs. */
22335 x_before = it->current_x;
22336 n_glyphs_before = row->used[TEXT_AREA];
22337 PRODUCE_GLYPHS (it);
22339 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22340 i = 0;
22341 x = x_before;
22342 while (i < nglyphs)
22344 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22346 if (it->line_wrap != TRUNCATE
22347 && x + glyph->pixel_width > max_x)
22349 /* End of continued line or max_x reached. */
22350 if (CHAR_GLYPH_PADDING_P (*glyph))
22352 /* A wide character is unbreakable. */
22353 if (row->reversed_p)
22354 unproduce_glyphs (it, row->used[TEXT_AREA]
22355 - n_glyphs_before);
22356 row->used[TEXT_AREA] = n_glyphs_before;
22357 it->current_x = x_before;
22359 else
22361 if (row->reversed_p)
22362 unproduce_glyphs (it, row->used[TEXT_AREA]
22363 - (n_glyphs_before + i));
22364 row->used[TEXT_AREA] = n_glyphs_before + i;
22365 it->current_x = x;
22367 break;
22369 else if (x + glyph->pixel_width >= it->first_visible_x)
22371 /* Glyph is at least partially visible. */
22372 ++it->hpos;
22373 if (x < it->first_visible_x)
22374 row->x = x - it->first_visible_x;
22376 else
22378 /* Glyph is off the left margin of the display area.
22379 Should not happen. */
22380 emacs_abort ();
22383 row->ascent = max (row->ascent, it->max_ascent);
22384 row->height = max (row->height, it->max_ascent + it->max_descent);
22385 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22386 row->phys_height = max (row->phys_height,
22387 it->max_phys_ascent + it->max_phys_descent);
22388 row->extra_line_spacing = max (row->extra_line_spacing,
22389 it->max_extra_line_spacing);
22390 x += glyph->pixel_width;
22391 ++i;
22394 /* Stop if max_x reached. */
22395 if (i < nglyphs)
22396 break;
22398 /* Stop at line ends. */
22399 if (ITERATOR_AT_END_OF_LINE_P (it))
22401 it->continuation_lines_width = 0;
22402 break;
22405 set_iterator_to_next (it, 1);
22406 if (STRINGP (it->string))
22407 it_charpos = IT_STRING_CHARPOS (*it);
22408 else
22409 it_charpos = IT_CHARPOS (*it);
22411 /* Stop if truncating at the right edge. */
22412 if (it->line_wrap == TRUNCATE
22413 && it->current_x >= it->last_visible_x)
22415 /* Add truncation mark, but don't do it if the line is
22416 truncated at a padding space. */
22417 if (it_charpos < it->string_nchars)
22419 if (!FRAME_WINDOW_P (it->f))
22421 int ii, n;
22423 if (it->current_x > it->last_visible_x)
22425 if (!row->reversed_p)
22427 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22428 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22429 break;
22431 else
22433 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22434 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22435 break;
22436 unproduce_glyphs (it, ii + 1);
22437 ii = row->used[TEXT_AREA] - (ii + 1);
22439 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22441 row->used[TEXT_AREA] = ii;
22442 produce_special_glyphs (it, IT_TRUNCATION);
22445 produce_special_glyphs (it, IT_TRUNCATION);
22447 row->truncated_on_right_p = 1;
22449 break;
22453 /* Maybe insert a truncation at the left. */
22454 if (it->first_visible_x
22455 && it_charpos > 0)
22457 if (!FRAME_WINDOW_P (it->f)
22458 || (row->reversed_p
22459 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22460 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22461 insert_left_trunc_glyphs (it);
22462 row->truncated_on_left_p = 1;
22465 it->face_id = saved_face_id;
22467 /* Value is number of columns displayed. */
22468 return it->hpos - hpos_at_start;
22473 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22474 appears as an element of LIST or as the car of an element of LIST.
22475 If PROPVAL is a list, compare each element against LIST in that
22476 way, and return 1/2 if any element of PROPVAL is found in LIST.
22477 Otherwise return 0. This function cannot quit.
22478 The return value is 2 if the text is invisible but with an ellipsis
22479 and 1 if it's invisible and without an ellipsis. */
22482 invisible_p (register Lisp_Object propval, Lisp_Object list)
22484 register Lisp_Object tail, proptail;
22486 for (tail = list; CONSP (tail); tail = XCDR (tail))
22488 register Lisp_Object tem;
22489 tem = XCAR (tail);
22490 if (EQ (propval, tem))
22491 return 1;
22492 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22493 return NILP (XCDR (tem)) ? 1 : 2;
22496 if (CONSP (propval))
22498 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22500 Lisp_Object propelt;
22501 propelt = XCAR (proptail);
22502 for (tail = list; CONSP (tail); tail = XCDR (tail))
22504 register Lisp_Object tem;
22505 tem = XCAR (tail);
22506 if (EQ (propelt, tem))
22507 return 1;
22508 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22509 return NILP (XCDR (tem)) ? 1 : 2;
22514 return 0;
22517 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22518 doc: /* Non-nil if the property makes the text invisible.
22519 POS-OR-PROP can be a marker or number, in which case it is taken to be
22520 a position in the current buffer and the value of the `invisible' property
22521 is checked; or it can be some other value, which is then presumed to be the
22522 value of the `invisible' property of the text of interest.
22523 The non-nil value returned can be t for truly invisible text or something
22524 else if the text is replaced by an ellipsis. */)
22525 (Lisp_Object pos_or_prop)
22527 Lisp_Object prop
22528 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22529 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22530 : pos_or_prop);
22531 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22532 return (invis == 0 ? Qnil
22533 : invis == 1 ? Qt
22534 : make_number (invis));
22537 /* Calculate a width or height in pixels from a specification using
22538 the following elements:
22540 SPEC ::=
22541 NUM - a (fractional) multiple of the default font width/height
22542 (NUM) - specifies exactly NUM pixels
22543 UNIT - a fixed number of pixels, see below.
22544 ELEMENT - size of a display element in pixels, see below.
22545 (NUM . SPEC) - equals NUM * SPEC
22546 (+ SPEC SPEC ...) - add pixel values
22547 (- SPEC SPEC ...) - subtract pixel values
22548 (- SPEC) - negate pixel value
22550 NUM ::=
22551 INT or FLOAT - a number constant
22552 SYMBOL - use symbol's (buffer local) variable binding.
22554 UNIT ::=
22555 in - pixels per inch *)
22556 mm - pixels per 1/1000 meter *)
22557 cm - pixels per 1/100 meter *)
22558 width - width of current font in pixels.
22559 height - height of current font in pixels.
22561 *) using the ratio(s) defined in display-pixels-per-inch.
22563 ELEMENT ::=
22565 left-fringe - left fringe width in pixels
22566 right-fringe - right fringe width in pixels
22568 left-margin - left margin width in pixels
22569 right-margin - right margin width in pixels
22571 scroll-bar - scroll-bar area width in pixels
22573 Examples:
22575 Pixels corresponding to 5 inches:
22576 (5 . in)
22578 Total width of non-text areas on left side of window (if scroll-bar is on left):
22579 '(space :width (+ left-fringe left-margin scroll-bar))
22581 Align to first text column (in header line):
22582 '(space :align-to 0)
22584 Align to middle of text area minus half the width of variable `my-image'
22585 containing a loaded image:
22586 '(space :align-to (0.5 . (- text my-image)))
22588 Width of left margin minus width of 1 character in the default font:
22589 '(space :width (- left-margin 1))
22591 Width of left margin minus width of 2 characters in the current font:
22592 '(space :width (- left-margin (2 . width)))
22594 Center 1 character over left-margin (in header line):
22595 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22597 Different ways to express width of left fringe plus left margin minus one pixel:
22598 '(space :width (- (+ left-fringe left-margin) (1)))
22599 '(space :width (+ left-fringe left-margin (- (1))))
22600 '(space :width (+ left-fringe left-margin (-1)))
22604 static int
22605 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22606 struct font *font, int width_p, int *align_to)
22608 double pixels;
22610 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22611 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22613 if (NILP (prop))
22614 return OK_PIXELS (0);
22616 eassert (FRAME_LIVE_P (it->f));
22618 if (SYMBOLP (prop))
22620 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22622 char *unit = SSDATA (SYMBOL_NAME (prop));
22624 if (unit[0] == 'i' && unit[1] == 'n')
22625 pixels = 1.0;
22626 else if (unit[0] == 'm' && unit[1] == 'm')
22627 pixels = 25.4;
22628 else if (unit[0] == 'c' && unit[1] == 'm')
22629 pixels = 2.54;
22630 else
22631 pixels = 0;
22632 if (pixels > 0)
22634 double ppi = (width_p ? FRAME_RES_X (it->f)
22635 : FRAME_RES_Y (it->f));
22637 if (ppi > 0)
22638 return OK_PIXELS (ppi / pixels);
22639 return 0;
22643 #ifdef HAVE_WINDOW_SYSTEM
22644 if (EQ (prop, Qheight))
22645 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22646 if (EQ (prop, Qwidth))
22647 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22648 #else
22649 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22650 return OK_PIXELS (1);
22651 #endif
22653 if (EQ (prop, Qtext))
22654 return OK_PIXELS (width_p
22655 ? window_box_width (it->w, TEXT_AREA)
22656 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22658 if (align_to && *align_to < 0)
22660 *res = 0;
22661 if (EQ (prop, Qleft))
22662 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22663 if (EQ (prop, Qright))
22664 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22665 if (EQ (prop, Qcenter))
22666 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22667 + window_box_width (it->w, TEXT_AREA) / 2);
22668 if (EQ (prop, Qleft_fringe))
22669 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22670 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22671 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22672 if (EQ (prop, Qright_fringe))
22673 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22674 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22675 : window_box_right_offset (it->w, TEXT_AREA));
22676 if (EQ (prop, Qleft_margin))
22677 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22678 if (EQ (prop, Qright_margin))
22679 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22680 if (EQ (prop, Qscroll_bar))
22681 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22683 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22684 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22685 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22686 : 0)));
22688 else
22690 if (EQ (prop, Qleft_fringe))
22691 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22692 if (EQ (prop, Qright_fringe))
22693 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22694 if (EQ (prop, Qleft_margin))
22695 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22696 if (EQ (prop, Qright_margin))
22697 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22698 if (EQ (prop, Qscroll_bar))
22699 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22702 prop = buffer_local_value_1 (prop, it->w->contents);
22703 if (EQ (prop, Qunbound))
22704 prop = Qnil;
22707 if (INTEGERP (prop) || FLOATP (prop))
22709 int base_unit = (width_p
22710 ? FRAME_COLUMN_WIDTH (it->f)
22711 : FRAME_LINE_HEIGHT (it->f));
22712 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22715 if (CONSP (prop))
22717 Lisp_Object car = XCAR (prop);
22718 Lisp_Object cdr = XCDR (prop);
22720 if (SYMBOLP (car))
22722 #ifdef HAVE_WINDOW_SYSTEM
22723 if (FRAME_WINDOW_P (it->f)
22724 && valid_image_p (prop))
22726 ptrdiff_t id = lookup_image (it->f, prop);
22727 struct image *img = IMAGE_FROM_ID (it->f, id);
22729 return OK_PIXELS (width_p ? img->width : img->height);
22731 #endif
22732 if (EQ (car, Qplus) || EQ (car, Qminus))
22734 int first = 1;
22735 double px;
22737 pixels = 0;
22738 while (CONSP (cdr))
22740 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22741 font, width_p, align_to))
22742 return 0;
22743 if (first)
22744 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22745 else
22746 pixels += px;
22747 cdr = XCDR (cdr);
22749 if (EQ (car, Qminus))
22750 pixels = -pixels;
22751 return OK_PIXELS (pixels);
22754 car = buffer_local_value_1 (car, it->w->contents);
22755 if (EQ (car, Qunbound))
22756 car = Qnil;
22759 if (INTEGERP (car) || FLOATP (car))
22761 double fact;
22762 pixels = XFLOATINT (car);
22763 if (NILP (cdr))
22764 return OK_PIXELS (pixels);
22765 if (calc_pixel_width_or_height (&fact, it, cdr,
22766 font, width_p, align_to))
22767 return OK_PIXELS (pixels * fact);
22768 return 0;
22771 return 0;
22774 return 0;
22778 /***********************************************************************
22779 Glyph Display
22780 ***********************************************************************/
22782 #ifdef HAVE_WINDOW_SYSTEM
22784 #ifdef GLYPH_DEBUG
22786 void
22787 dump_glyph_string (struct glyph_string *s)
22789 fprintf (stderr, "glyph string\n");
22790 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22791 s->x, s->y, s->width, s->height);
22792 fprintf (stderr, " ybase = %d\n", s->ybase);
22793 fprintf (stderr, " hl = %d\n", s->hl);
22794 fprintf (stderr, " left overhang = %d, right = %d\n",
22795 s->left_overhang, s->right_overhang);
22796 fprintf (stderr, " nchars = %d\n", s->nchars);
22797 fprintf (stderr, " extends to end of line = %d\n",
22798 s->extends_to_end_of_line_p);
22799 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22800 fprintf (stderr, " bg width = %d\n", s->background_width);
22803 #endif /* GLYPH_DEBUG */
22805 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22806 of XChar2b structures for S; it can't be allocated in
22807 init_glyph_string because it must be allocated via `alloca'. W
22808 is the window on which S is drawn. ROW and AREA are the glyph row
22809 and area within the row from which S is constructed. START is the
22810 index of the first glyph structure covered by S. HL is a
22811 face-override for drawing S. */
22813 #ifdef HAVE_NTGUI
22814 #define OPTIONAL_HDC(hdc) HDC hdc,
22815 #define DECLARE_HDC(hdc) HDC hdc;
22816 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22817 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22818 #endif
22820 #ifndef OPTIONAL_HDC
22821 #define OPTIONAL_HDC(hdc)
22822 #define DECLARE_HDC(hdc)
22823 #define ALLOCATE_HDC(hdc, f)
22824 #define RELEASE_HDC(hdc, f)
22825 #endif
22827 static void
22828 init_glyph_string (struct glyph_string *s,
22829 OPTIONAL_HDC (hdc)
22830 XChar2b *char2b, struct window *w, struct glyph_row *row,
22831 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22833 memset (s, 0, sizeof *s);
22834 s->w = w;
22835 s->f = XFRAME (w->frame);
22836 #ifdef HAVE_NTGUI
22837 s->hdc = hdc;
22838 #endif
22839 s->display = FRAME_X_DISPLAY (s->f);
22840 s->window = FRAME_X_WINDOW (s->f);
22841 s->char2b = char2b;
22842 s->hl = hl;
22843 s->row = row;
22844 s->area = area;
22845 s->first_glyph = row->glyphs[area] + start;
22846 s->height = row->height;
22847 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22848 s->ybase = s->y + row->ascent;
22852 /* Append the list of glyph strings with head H and tail T to the list
22853 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22855 static void
22856 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22857 struct glyph_string *h, struct glyph_string *t)
22859 if (h)
22861 if (*head)
22862 (*tail)->next = h;
22863 else
22864 *head = h;
22865 h->prev = *tail;
22866 *tail = t;
22871 /* Prepend the list of glyph strings with head H and tail T to the
22872 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22873 result. */
22875 static void
22876 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22877 struct glyph_string *h, struct glyph_string *t)
22879 if (h)
22881 if (*head)
22882 (*head)->prev = t;
22883 else
22884 *tail = t;
22885 t->next = *head;
22886 *head = h;
22891 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22892 Set *HEAD and *TAIL to the resulting list. */
22894 static void
22895 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22896 struct glyph_string *s)
22898 s->next = s->prev = NULL;
22899 append_glyph_string_lists (head, tail, s, s);
22903 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22904 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22905 make sure that X resources for the face returned are allocated.
22906 Value is a pointer to a realized face that is ready for display if
22907 DISPLAY_P is non-zero. */
22909 static struct face *
22910 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22911 XChar2b *char2b, int display_p)
22913 struct face *face = FACE_FROM_ID (f, face_id);
22914 unsigned code = 0;
22916 if (face->font)
22918 code = face->font->driver->encode_char (face->font, c);
22920 if (code == FONT_INVALID_CODE)
22921 code = 0;
22923 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22925 /* Make sure X resources of the face are allocated. */
22926 #ifdef HAVE_X_WINDOWS
22927 if (display_p)
22928 #endif
22930 eassert (face != NULL);
22931 PREPARE_FACE_FOR_DISPLAY (f, face);
22934 return face;
22938 /* Get face and two-byte form of character glyph GLYPH on frame F.
22939 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22940 a pointer to a realized face that is ready for display. */
22942 static struct face *
22943 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22944 XChar2b *char2b, int *two_byte_p)
22946 struct face *face;
22947 unsigned code = 0;
22949 eassert (glyph->type == CHAR_GLYPH);
22950 face = FACE_FROM_ID (f, glyph->face_id);
22952 /* Make sure X resources of the face are allocated. */
22953 eassert (face != NULL);
22954 PREPARE_FACE_FOR_DISPLAY (f, face);
22956 if (two_byte_p)
22957 *two_byte_p = 0;
22959 if (face->font)
22961 if (CHAR_BYTE8_P (glyph->u.ch))
22962 code = CHAR_TO_BYTE8 (glyph->u.ch);
22963 else
22964 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22966 if (code == FONT_INVALID_CODE)
22967 code = 0;
22970 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22971 return face;
22975 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22976 Return 1 if FONT has a glyph for C, otherwise return 0. */
22978 static int
22979 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22981 unsigned code;
22983 if (CHAR_BYTE8_P (c))
22984 code = CHAR_TO_BYTE8 (c);
22985 else
22986 code = font->driver->encode_char (font, c);
22988 if (code == FONT_INVALID_CODE)
22989 return 0;
22990 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22991 return 1;
22995 /* Fill glyph string S with composition components specified by S->cmp.
22997 BASE_FACE is the base face of the composition.
22998 S->cmp_from is the index of the first component for S.
23000 OVERLAPS non-zero means S should draw the foreground only, and use
23001 its physical height for clipping. See also draw_glyphs.
23003 Value is the index of a component not in S. */
23005 static int
23006 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23007 int overlaps)
23009 int i;
23010 /* For all glyphs of this composition, starting at the offset
23011 S->cmp_from, until we reach the end of the definition or encounter a
23012 glyph that requires the different face, add it to S. */
23013 struct face *face;
23015 eassert (s);
23017 s->for_overlaps = overlaps;
23018 s->face = NULL;
23019 s->font = NULL;
23020 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23022 int c = COMPOSITION_GLYPH (s->cmp, i);
23024 /* TAB in a composition means display glyphs with padding space
23025 on the left or right. */
23026 if (c != '\t')
23028 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23029 -1, Qnil);
23031 face = get_char_face_and_encoding (s->f, c, face_id,
23032 s->char2b + i, 1);
23033 if (face)
23035 if (! s->face)
23037 s->face = face;
23038 s->font = s->face->font;
23040 else if (s->face != face)
23041 break;
23044 ++s->nchars;
23046 s->cmp_to = i;
23048 if (s->face == NULL)
23050 s->face = base_face->ascii_face;
23051 s->font = s->face->font;
23054 /* All glyph strings for the same composition has the same width,
23055 i.e. the width set for the first component of the composition. */
23056 s->width = s->first_glyph->pixel_width;
23058 /* If the specified font could not be loaded, use the frame's
23059 default font, but record the fact that we couldn't load it in
23060 the glyph string so that we can draw rectangles for the
23061 characters of the glyph string. */
23062 if (s->font == NULL)
23064 s->font_not_found_p = 1;
23065 s->font = FRAME_FONT (s->f);
23068 /* Adjust base line for subscript/superscript text. */
23069 s->ybase += s->first_glyph->voffset;
23071 /* This glyph string must always be drawn with 16-bit functions. */
23072 s->two_byte_p = 1;
23074 return s->cmp_to;
23077 static int
23078 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23079 int start, int end, int overlaps)
23081 struct glyph *glyph, *last;
23082 Lisp_Object lgstring;
23083 int i;
23085 s->for_overlaps = overlaps;
23086 glyph = s->row->glyphs[s->area] + start;
23087 last = s->row->glyphs[s->area] + end;
23088 s->cmp_id = glyph->u.cmp.id;
23089 s->cmp_from = glyph->slice.cmp.from;
23090 s->cmp_to = glyph->slice.cmp.to + 1;
23091 s->face = FACE_FROM_ID (s->f, face_id);
23092 lgstring = composition_gstring_from_id (s->cmp_id);
23093 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23094 glyph++;
23095 while (glyph < last
23096 && glyph->u.cmp.automatic
23097 && glyph->u.cmp.id == s->cmp_id
23098 && s->cmp_to == glyph->slice.cmp.from)
23099 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23101 for (i = s->cmp_from; i < s->cmp_to; i++)
23103 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23104 unsigned code = LGLYPH_CODE (lglyph);
23106 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23108 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23109 return glyph - s->row->glyphs[s->area];
23113 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23114 See the comment of fill_glyph_string for arguments.
23115 Value is the index of the first glyph not in S. */
23118 static int
23119 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23120 int start, int end, int overlaps)
23122 struct glyph *glyph, *last;
23123 int voffset;
23125 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23126 s->for_overlaps = overlaps;
23127 glyph = s->row->glyphs[s->area] + start;
23128 last = s->row->glyphs[s->area] + end;
23129 voffset = glyph->voffset;
23130 s->face = FACE_FROM_ID (s->f, face_id);
23131 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23132 s->nchars = 1;
23133 s->width = glyph->pixel_width;
23134 glyph++;
23135 while (glyph < last
23136 && glyph->type == GLYPHLESS_GLYPH
23137 && glyph->voffset == voffset
23138 && glyph->face_id == face_id)
23140 s->nchars++;
23141 s->width += glyph->pixel_width;
23142 glyph++;
23144 s->ybase += voffset;
23145 return glyph - s->row->glyphs[s->area];
23149 /* Fill glyph string S from a sequence of character glyphs.
23151 FACE_ID is the face id of the string. START is the index of the
23152 first glyph to consider, END is the index of the last + 1.
23153 OVERLAPS non-zero means S should draw the foreground only, and use
23154 its physical height for clipping. See also draw_glyphs.
23156 Value is the index of the first glyph not in S. */
23158 static int
23159 fill_glyph_string (struct glyph_string *s, int face_id,
23160 int start, int end, int overlaps)
23162 struct glyph *glyph, *last;
23163 int voffset;
23164 int glyph_not_available_p;
23166 eassert (s->f == XFRAME (s->w->frame));
23167 eassert (s->nchars == 0);
23168 eassert (start >= 0 && end > start);
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->padding_p = glyph->padding_p;
23175 glyph_not_available_p = glyph->glyph_not_available_p;
23177 while (glyph < last
23178 && glyph->type == CHAR_GLYPH
23179 && glyph->voffset == voffset
23180 /* Same face id implies same font, nowadays. */
23181 && glyph->face_id == face_id
23182 && glyph->glyph_not_available_p == glyph_not_available_p)
23184 int two_byte_p;
23186 s->face = get_glyph_face_and_encoding (s->f, glyph,
23187 s->char2b + s->nchars,
23188 &two_byte_p);
23189 s->two_byte_p = two_byte_p;
23190 ++s->nchars;
23191 eassert (s->nchars <= end - start);
23192 s->width += glyph->pixel_width;
23193 if (glyph++->padding_p != s->padding_p)
23194 break;
23197 s->font = s->face->font;
23199 /* If the specified font could not be loaded, use the frame's font,
23200 but record the fact that we couldn't load it in
23201 S->font_not_found_p so that we can draw rectangles for the
23202 characters of the glyph string. */
23203 if (s->font == NULL || glyph_not_available_p)
23205 s->font_not_found_p = 1;
23206 s->font = FRAME_FONT (s->f);
23209 /* Adjust base line for subscript/superscript text. */
23210 s->ybase += voffset;
23212 eassert (s->face && s->face->gc);
23213 return glyph - s->row->glyphs[s->area];
23217 /* Fill glyph string S from image glyph S->first_glyph. */
23219 static void
23220 fill_image_glyph_string (struct glyph_string *s)
23222 eassert (s->first_glyph->type == IMAGE_GLYPH);
23223 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23224 eassert (s->img);
23225 s->slice = s->first_glyph->slice.img;
23226 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23227 s->font = s->face->font;
23228 s->width = s->first_glyph->pixel_width;
23230 /* Adjust base line for subscript/superscript text. */
23231 s->ybase += s->first_glyph->voffset;
23235 /* Fill glyph string S from a sequence of stretch glyphs.
23237 START is the index of the first glyph to consider,
23238 END is the index of the last + 1.
23240 Value is the index of the first glyph not in S. */
23242 static int
23243 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23245 struct glyph *glyph, *last;
23246 int voffset, face_id;
23248 eassert (s->first_glyph->type == STRETCH_GLYPH);
23250 glyph = s->row->glyphs[s->area] + start;
23251 last = s->row->glyphs[s->area] + end;
23252 face_id = glyph->face_id;
23253 s->face = FACE_FROM_ID (s->f, face_id);
23254 s->font = s->face->font;
23255 s->width = glyph->pixel_width;
23256 s->nchars = 1;
23257 voffset = glyph->voffset;
23259 for (++glyph;
23260 (glyph < last
23261 && glyph->type == STRETCH_GLYPH
23262 && glyph->voffset == voffset
23263 && glyph->face_id == face_id);
23264 ++glyph)
23265 s->width += glyph->pixel_width;
23267 /* Adjust base line for subscript/superscript text. */
23268 s->ybase += voffset;
23270 /* The case that face->gc == 0 is handled when drawing the glyph
23271 string by calling PREPARE_FACE_FOR_DISPLAY. */
23272 eassert (s->face);
23273 return glyph - s->row->glyphs[s->area];
23276 static struct font_metrics *
23277 get_per_char_metric (struct font *font, XChar2b *char2b)
23279 static struct font_metrics metrics;
23280 unsigned code;
23282 if (! font)
23283 return NULL;
23284 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23285 if (code == FONT_INVALID_CODE)
23286 return NULL;
23287 font->driver->text_extents (font, &code, 1, &metrics);
23288 return &metrics;
23291 /* EXPORT for RIF:
23292 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23293 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23294 assumed to be zero. */
23296 void
23297 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23299 *left = *right = 0;
23301 if (glyph->type == CHAR_GLYPH)
23303 struct face *face;
23304 XChar2b char2b;
23305 struct font_metrics *pcm;
23307 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23308 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23310 if (pcm->rbearing > pcm->width)
23311 *right = pcm->rbearing - pcm->width;
23312 if (pcm->lbearing < 0)
23313 *left = -pcm->lbearing;
23316 else if (glyph->type == COMPOSITE_GLYPH)
23318 if (! glyph->u.cmp.automatic)
23320 struct composition *cmp = composition_table[glyph->u.cmp.id];
23322 if (cmp->rbearing > cmp->pixel_width)
23323 *right = cmp->rbearing - cmp->pixel_width;
23324 if (cmp->lbearing < 0)
23325 *left = - cmp->lbearing;
23327 else
23329 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23330 struct font_metrics metrics;
23332 composition_gstring_width (gstring, glyph->slice.cmp.from,
23333 glyph->slice.cmp.to + 1, &metrics);
23334 if (metrics.rbearing > metrics.width)
23335 *right = metrics.rbearing - metrics.width;
23336 if (metrics.lbearing < 0)
23337 *left = - metrics.lbearing;
23343 /* Return the index of the first glyph preceding glyph string S that
23344 is overwritten by S because of S's left overhang. Value is -1
23345 if no glyphs are overwritten. */
23347 static int
23348 left_overwritten (struct glyph_string *s)
23350 int k;
23352 if (s->left_overhang)
23354 int x = 0, i;
23355 struct glyph *glyphs = s->row->glyphs[s->area];
23356 int first = s->first_glyph - glyphs;
23358 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23359 x -= glyphs[i].pixel_width;
23361 k = i + 1;
23363 else
23364 k = -1;
23366 return k;
23370 /* Return the index of the first glyph preceding glyph string S that
23371 is overwriting S because of its right overhang. Value is -1 if no
23372 glyph in front of S overwrites S. */
23374 static int
23375 left_overwriting (struct glyph_string *s)
23377 int i, k, x;
23378 struct glyph *glyphs = s->row->glyphs[s->area];
23379 int first = s->first_glyph - glyphs;
23381 k = -1;
23382 x = 0;
23383 for (i = first - 1; i >= 0; --i)
23385 int left, right;
23386 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23387 if (x + right > 0)
23388 k = i;
23389 x -= glyphs[i].pixel_width;
23392 return k;
23396 /* Return the index of the last glyph following glyph string S that is
23397 overwritten by S because of S's right overhang. Value is -1 if
23398 no such glyph is found. */
23400 static int
23401 right_overwritten (struct glyph_string *s)
23403 int k = -1;
23405 if (s->right_overhang)
23407 int x = 0, i;
23408 struct glyph *glyphs = s->row->glyphs[s->area];
23409 int first = (s->first_glyph - glyphs
23410 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23411 int end = s->row->used[s->area];
23413 for (i = first; i < end && s->right_overhang > x; ++i)
23414 x += glyphs[i].pixel_width;
23416 k = i;
23419 return k;
23423 /* Return the index of the last glyph following glyph string S that
23424 overwrites S because of its left overhang. Value is negative
23425 if no such glyph is found. */
23427 static int
23428 right_overwriting (struct glyph_string *s)
23430 int i, k, x;
23431 int end = s->row->used[s->area];
23432 struct glyph *glyphs = s->row->glyphs[s->area];
23433 int first = (s->first_glyph - glyphs
23434 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23436 k = -1;
23437 x = 0;
23438 for (i = first; i < end; ++i)
23440 int left, right;
23441 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23442 if (x - left < 0)
23443 k = i;
23444 x += glyphs[i].pixel_width;
23447 return k;
23451 /* Set background width of glyph string S. START is the index of the
23452 first glyph following S. LAST_X is the right-most x-position + 1
23453 in the drawing area. */
23455 static void
23456 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23458 /* If the face of this glyph string has to be drawn to the end of
23459 the drawing area, set S->extends_to_end_of_line_p. */
23461 if (start == s->row->used[s->area]
23462 && s->area == TEXT_AREA
23463 && ((s->row->fill_line_p
23464 && (s->hl == DRAW_NORMAL_TEXT
23465 || s->hl == DRAW_IMAGE_RAISED
23466 || s->hl == DRAW_IMAGE_SUNKEN))
23467 || s->hl == DRAW_MOUSE_FACE))
23468 s->extends_to_end_of_line_p = 1;
23470 /* If S extends its face to the end of the line, set its
23471 background_width to the distance to the right edge of the drawing
23472 area. */
23473 if (s->extends_to_end_of_line_p)
23474 s->background_width = last_x - s->x + 1;
23475 else
23476 s->background_width = s->width;
23480 /* Compute overhangs and x-positions for glyph string S and its
23481 predecessors, or successors. X is the starting x-position for S.
23482 BACKWARD_P non-zero means process predecessors. */
23484 static void
23485 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23487 if (backward_p)
23489 while (s)
23491 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23492 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23493 x -= s->width;
23494 s->x = x;
23495 s = s->prev;
23498 else
23500 while (s)
23502 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23503 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23504 s->x = x;
23505 x += s->width;
23506 s = s->next;
23513 /* The following macros are only called from draw_glyphs below.
23514 They reference the following parameters of that function directly:
23515 `w', `row', `area', and `overlap_p'
23516 as well as the following local variables:
23517 `s', `f', and `hdc' (in W32) */
23519 #ifdef HAVE_NTGUI
23520 /* On W32, silently add local `hdc' variable to argument list of
23521 init_glyph_string. */
23522 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23523 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23524 #else
23525 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23526 init_glyph_string (s, char2b, w, row, area, start, hl)
23527 #endif
23529 /* Add a glyph string for a stretch glyph to the list of strings
23530 between HEAD and TAIL. START is the index of the stretch glyph in
23531 row area AREA of glyph row ROW. END is the index of the last glyph
23532 in that glyph row area. X is the current output position assigned
23533 to the new glyph string constructed. HL overrides that face of the
23534 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23535 is the right-most x-position of the drawing area. */
23537 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23538 and below -- keep them on one line. */
23539 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23540 do \
23542 s = alloca (sizeof *s); \
23543 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23544 START = fill_stretch_glyph_string (s, START, END); \
23545 append_glyph_string (&HEAD, &TAIL, s); \
23546 s->x = (X); \
23548 while (0)
23551 /* Add a glyph string for an image glyph to the list of strings
23552 between HEAD and TAIL. START is the index of the image glyph in
23553 row area AREA of glyph row ROW. END is the index of the last glyph
23554 in that glyph row area. X is the current output position assigned
23555 to the new glyph string constructed. HL overrides that face of the
23556 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23557 is the right-most x-position of the drawing area. */
23559 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23560 do \
23562 s = alloca (sizeof *s); \
23563 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23564 fill_image_glyph_string (s); \
23565 append_glyph_string (&HEAD, &TAIL, s); \
23566 ++START; \
23567 s->x = (X); \
23569 while (0)
23572 /* Add a glyph string for a sequence of character glyphs to the list
23573 of strings between HEAD and TAIL. START is the index of the first
23574 glyph in row area AREA of glyph row ROW that is part of the new
23575 glyph string. END is the index of the last glyph in that glyph row
23576 area. X is the current output position assigned to the new glyph
23577 string constructed. HL overrides that face of the glyph; e.g. it
23578 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23579 right-most x-position of the drawing area. */
23581 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23582 do \
23584 int face_id; \
23585 XChar2b *char2b; \
23587 face_id = (row)->glyphs[area][START].face_id; \
23589 s = alloca (sizeof *s); \
23590 char2b = alloca ((END - START) * sizeof *char2b); \
23591 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23592 append_glyph_string (&HEAD, &TAIL, s); \
23593 s->x = (X); \
23594 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23596 while (0)
23599 /* Add a glyph string for a composite sequence to the list of strings
23600 between HEAD and TAIL. START is the index of the first glyph in
23601 row area AREA of glyph row ROW that is part of the new glyph
23602 string. END is the index of the last glyph in that glyph row area.
23603 X is the current output position assigned to the new glyph string
23604 constructed. HL overrides that face of the glyph; e.g. it is
23605 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23606 x-position of the drawing area. */
23608 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23609 do { \
23610 int face_id = (row)->glyphs[area][START].face_id; \
23611 struct face *base_face = FACE_FROM_ID (f, face_id); \
23612 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23613 struct composition *cmp = composition_table[cmp_id]; \
23614 XChar2b *char2b; \
23615 struct glyph_string *first_s = NULL; \
23616 int n; \
23618 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23620 /* Make glyph_strings for each glyph sequence that is drawable by \
23621 the same face, and append them to HEAD/TAIL. */ \
23622 for (n = 0; n < cmp->glyph_len;) \
23624 s = alloca (sizeof *s); \
23625 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23626 append_glyph_string (&(HEAD), &(TAIL), s); \
23627 s->cmp = cmp; \
23628 s->cmp_from = n; \
23629 s->x = (X); \
23630 if (n == 0) \
23631 first_s = s; \
23632 n = fill_composite_glyph_string (s, base_face, overlaps); \
23635 ++START; \
23636 s = first_s; \
23637 } while (0)
23640 /* Add a glyph string for a glyph-string sequence to the list of strings
23641 between HEAD and TAIL. */
23643 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23644 do { \
23645 int face_id; \
23646 XChar2b *char2b; \
23647 Lisp_Object gstring; \
23649 face_id = (row)->glyphs[area][START].face_id; \
23650 gstring = (composition_gstring_from_id \
23651 ((row)->glyphs[area][START].u.cmp.id)); \
23652 s = alloca (sizeof *s); \
23653 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23654 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23655 append_glyph_string (&(HEAD), &(TAIL), s); \
23656 s->x = (X); \
23657 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23658 } while (0)
23661 /* Add a glyph string for a sequence of glyphless character's glyphs
23662 to the list of strings between HEAD and TAIL. The meanings of
23663 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23665 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23666 do \
23668 int face_id; \
23670 face_id = (row)->glyphs[area][START].face_id; \
23672 s = alloca (sizeof *s); \
23673 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23674 append_glyph_string (&HEAD, &TAIL, s); \
23675 s->x = (X); \
23676 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23677 overlaps); \
23679 while (0)
23682 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23683 of AREA of glyph row ROW on window W between indices START and END.
23684 HL overrides the face for drawing glyph strings, e.g. it is
23685 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23686 x-positions of the drawing area.
23688 This is an ugly monster macro construct because we must use alloca
23689 to allocate glyph strings (because draw_glyphs can be called
23690 asynchronously). */
23692 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23693 do \
23695 HEAD = TAIL = NULL; \
23696 while (START < END) \
23698 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23699 switch (first_glyph->type) \
23701 case CHAR_GLYPH: \
23702 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23703 HL, X, LAST_X); \
23704 break; \
23706 case COMPOSITE_GLYPH: \
23707 if (first_glyph->u.cmp.automatic) \
23708 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23709 HL, X, LAST_X); \
23710 else \
23711 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23712 HL, X, LAST_X); \
23713 break; \
23715 case STRETCH_GLYPH: \
23716 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23717 HL, X, LAST_X); \
23718 break; \
23720 case IMAGE_GLYPH: \
23721 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23722 HL, X, LAST_X); \
23723 break; \
23725 case GLYPHLESS_GLYPH: \
23726 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23727 HL, X, LAST_X); \
23728 break; \
23730 default: \
23731 emacs_abort (); \
23734 if (s) \
23736 set_glyph_string_background_width (s, START, LAST_X); \
23737 (X) += s->width; \
23740 } while (0)
23743 /* Draw glyphs between START and END in AREA of ROW on window W,
23744 starting at x-position X. X is relative to AREA in W. HL is a
23745 face-override with the following meaning:
23747 DRAW_NORMAL_TEXT draw normally
23748 DRAW_CURSOR draw in cursor face
23749 DRAW_MOUSE_FACE draw in mouse face.
23750 DRAW_INVERSE_VIDEO draw in mode line face
23751 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23752 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23754 If OVERLAPS is non-zero, draw only the foreground of characters and
23755 clip to the physical height of ROW. Non-zero value also defines
23756 the overlapping part to be drawn:
23758 OVERLAPS_PRED overlap with preceding rows
23759 OVERLAPS_SUCC overlap with succeeding rows
23760 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23761 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23763 Value is the x-position reached, relative to AREA of W. */
23765 static int
23766 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23767 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23768 enum draw_glyphs_face hl, int overlaps)
23770 struct glyph_string *head, *tail;
23771 struct glyph_string *s;
23772 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23773 int i, j, x_reached, last_x, area_left = 0;
23774 struct frame *f = XFRAME (WINDOW_FRAME (w));
23775 DECLARE_HDC (hdc);
23777 ALLOCATE_HDC (hdc, f);
23779 /* Let's rather be paranoid than getting a SEGV. */
23780 end = min (end, row->used[area]);
23781 start = clip_to_bounds (0, start, end);
23783 /* Translate X to frame coordinates. Set last_x to the right
23784 end of the drawing area. */
23785 if (row->full_width_p)
23787 /* X is relative to the left edge of W, without scroll bars
23788 or fringes. */
23789 area_left = WINDOW_LEFT_EDGE_X (w);
23790 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23792 else
23794 area_left = window_box_left (w, area);
23795 last_x = area_left + window_box_width (w, area);
23797 x += area_left;
23799 /* Build a doubly-linked list of glyph_string structures between
23800 head and tail from what we have to draw. Note that the macro
23801 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23802 the reason we use a separate variable `i'. */
23803 i = start;
23804 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23805 if (tail)
23806 x_reached = tail->x + tail->background_width;
23807 else
23808 x_reached = x;
23810 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23811 the row, redraw some glyphs in front or following the glyph
23812 strings built above. */
23813 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23815 struct glyph_string *h, *t;
23816 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23817 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23818 int check_mouse_face = 0;
23819 int dummy_x = 0;
23821 /* If mouse highlighting is on, we may need to draw adjacent
23822 glyphs using mouse-face highlighting. */
23823 if (area == TEXT_AREA && row->mouse_face_p
23824 && hlinfo->mouse_face_beg_row >= 0
23825 && hlinfo->mouse_face_end_row >= 0)
23827 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23829 if (row_vpos >= hlinfo->mouse_face_beg_row
23830 && row_vpos <= hlinfo->mouse_face_end_row)
23832 check_mouse_face = 1;
23833 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23834 ? hlinfo->mouse_face_beg_col : 0;
23835 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23836 ? hlinfo->mouse_face_end_col
23837 : row->used[TEXT_AREA];
23841 /* Compute overhangs for all glyph strings. */
23842 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23843 for (s = head; s; s = s->next)
23844 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23846 /* Prepend glyph strings for glyphs in front of the first glyph
23847 string that are overwritten because of the first glyph
23848 string's left overhang. The background of all strings
23849 prepended must be drawn because the first glyph string
23850 draws over it. */
23851 i = left_overwritten (head);
23852 if (i >= 0)
23854 enum draw_glyphs_face overlap_hl;
23856 /* If this row contains mouse highlighting, attempt to draw
23857 the overlapped glyphs with the correct highlight. This
23858 code fails if the overlap encompasses more than one glyph
23859 and mouse-highlight spans only some of these glyphs.
23860 However, making it work perfectly involves a lot more
23861 code, and I don't know if the pathological case occurs in
23862 practice, so we'll stick to this for now. --- cyd */
23863 if (check_mouse_face
23864 && mouse_beg_col < start && mouse_end_col > i)
23865 overlap_hl = DRAW_MOUSE_FACE;
23866 else
23867 overlap_hl = DRAW_NORMAL_TEXT;
23869 j = i;
23870 BUILD_GLYPH_STRINGS (j, start, h, t,
23871 overlap_hl, dummy_x, last_x);
23872 start = i;
23873 compute_overhangs_and_x (t, head->x, 1);
23874 prepend_glyph_string_lists (&head, &tail, h, t);
23875 clip_head = head;
23878 /* Prepend glyph strings for glyphs in front of the first glyph
23879 string that overwrite that glyph string because of their
23880 right overhang. For these strings, only the foreground must
23881 be drawn, because it draws over the glyph string at `head'.
23882 The background must not be drawn because this would overwrite
23883 right overhangs of preceding glyphs for which no glyph
23884 strings exist. */
23885 i = left_overwriting (head);
23886 if (i >= 0)
23888 enum draw_glyphs_face overlap_hl;
23890 if (check_mouse_face
23891 && mouse_beg_col < start && mouse_end_col > i)
23892 overlap_hl = DRAW_MOUSE_FACE;
23893 else
23894 overlap_hl = DRAW_NORMAL_TEXT;
23896 clip_head = head;
23897 BUILD_GLYPH_STRINGS (i, start, h, t,
23898 overlap_hl, dummy_x, last_x);
23899 for (s = h; s; s = s->next)
23900 s->background_filled_p = 1;
23901 compute_overhangs_and_x (t, head->x, 1);
23902 prepend_glyph_string_lists (&head, &tail, h, t);
23905 /* Append glyphs strings for glyphs following the last glyph
23906 string tail that are overwritten by tail. The background of
23907 these strings has to be drawn because tail's foreground draws
23908 over it. */
23909 i = right_overwritten (tail);
23910 if (i >= 0)
23912 enum draw_glyphs_face overlap_hl;
23914 if (check_mouse_face
23915 && mouse_beg_col < i && mouse_end_col > end)
23916 overlap_hl = DRAW_MOUSE_FACE;
23917 else
23918 overlap_hl = DRAW_NORMAL_TEXT;
23920 BUILD_GLYPH_STRINGS (end, i, h, t,
23921 overlap_hl, x, last_x);
23922 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23923 we don't have `end = i;' here. */
23924 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23925 append_glyph_string_lists (&head, &tail, h, t);
23926 clip_tail = tail;
23929 /* Append glyph strings for glyphs following the last glyph
23930 string tail that overwrite tail. The foreground of such
23931 glyphs has to be drawn because it writes into the background
23932 of tail. The background must not be drawn because it could
23933 paint over the foreground of following glyphs. */
23934 i = right_overwriting (tail);
23935 if (i >= 0)
23937 enum draw_glyphs_face overlap_hl;
23938 if (check_mouse_face
23939 && mouse_beg_col < i && mouse_end_col > end)
23940 overlap_hl = DRAW_MOUSE_FACE;
23941 else
23942 overlap_hl = DRAW_NORMAL_TEXT;
23944 clip_tail = tail;
23945 i++; /* We must include the Ith glyph. */
23946 BUILD_GLYPH_STRINGS (end, i, h, t,
23947 overlap_hl, x, last_x);
23948 for (s = h; s; s = s->next)
23949 s->background_filled_p = 1;
23950 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23951 append_glyph_string_lists (&head, &tail, h, t);
23953 if (clip_head || clip_tail)
23954 for (s = head; s; s = s->next)
23956 s->clip_head = clip_head;
23957 s->clip_tail = clip_tail;
23961 /* Draw all strings. */
23962 for (s = head; s; s = s->next)
23963 FRAME_RIF (f)->draw_glyph_string (s);
23965 #ifndef HAVE_NS
23966 /* When focus a sole frame and move horizontally, this sets on_p to 0
23967 causing a failure to erase prev cursor position. */
23968 if (area == TEXT_AREA
23969 && !row->full_width_p
23970 /* When drawing overlapping rows, only the glyph strings'
23971 foreground is drawn, which doesn't erase a cursor
23972 completely. */
23973 && !overlaps)
23975 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23976 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23977 : (tail ? tail->x + tail->background_width : x));
23978 x0 -= area_left;
23979 x1 -= area_left;
23981 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23982 row->y, MATRIX_ROW_BOTTOM_Y (row));
23984 #endif
23986 /* Value is the x-position up to which drawn, relative to AREA of W.
23987 This doesn't include parts drawn because of overhangs. */
23988 if (row->full_width_p)
23989 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23990 else
23991 x_reached -= area_left;
23993 RELEASE_HDC (hdc, f);
23995 return x_reached;
23998 /* Expand row matrix if too narrow. Don't expand if area
23999 is not present. */
24001 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24003 if (!it->f->fonts_changed \
24004 && (it->glyph_row->glyphs[area] \
24005 < it->glyph_row->glyphs[area + 1])) \
24007 it->w->ncols_scale_factor++; \
24008 it->f->fonts_changed = 1; \
24012 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24013 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24015 static void
24016 append_glyph (struct it *it)
24018 struct glyph *glyph;
24019 enum glyph_row_area area = it->area;
24021 eassert (it->glyph_row);
24022 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24024 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24025 if (glyph < it->glyph_row->glyphs[area + 1])
24027 /* If the glyph row is reversed, we need to prepend the glyph
24028 rather than append it. */
24029 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24031 struct glyph *g;
24033 /* Make room for the additional glyph. */
24034 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24035 g[1] = *g;
24036 glyph = it->glyph_row->glyphs[area];
24038 glyph->charpos = CHARPOS (it->position);
24039 glyph->object = it->object;
24040 if (it->pixel_width > 0)
24042 glyph->pixel_width = it->pixel_width;
24043 glyph->padding_p = 0;
24045 else
24047 /* Assure at least 1-pixel width. Otherwise, cursor can't
24048 be displayed correctly. */
24049 glyph->pixel_width = 1;
24050 glyph->padding_p = 1;
24052 glyph->ascent = it->ascent;
24053 glyph->descent = it->descent;
24054 glyph->voffset = it->voffset;
24055 glyph->type = CHAR_GLYPH;
24056 glyph->avoid_cursor_p = it->avoid_cursor_p;
24057 glyph->multibyte_p = it->multibyte_p;
24058 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24060 /* In R2L rows, the left and the right box edges need to be
24061 drawn in reverse direction. */
24062 glyph->right_box_line_p = it->start_of_box_run_p;
24063 glyph->left_box_line_p = it->end_of_box_run_p;
24065 else
24067 glyph->left_box_line_p = it->start_of_box_run_p;
24068 glyph->right_box_line_p = it->end_of_box_run_p;
24070 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24071 || it->phys_descent > it->descent);
24072 glyph->glyph_not_available_p = it->glyph_not_available_p;
24073 glyph->face_id = it->face_id;
24074 glyph->u.ch = it->char_to_display;
24075 glyph->slice.img = null_glyph_slice;
24076 glyph->font_type = FONT_TYPE_UNKNOWN;
24077 if (it->bidi_p)
24079 glyph->resolved_level = it->bidi_it.resolved_level;
24080 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24081 emacs_abort ();
24082 glyph->bidi_type = it->bidi_it.type;
24084 else
24086 glyph->resolved_level = 0;
24087 glyph->bidi_type = UNKNOWN_BT;
24089 ++it->glyph_row->used[area];
24091 else
24092 IT_EXPAND_MATRIX_WIDTH (it, area);
24095 /* Store one glyph for the composition IT->cmp_it.id in
24096 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24097 non-null. */
24099 static void
24100 append_composite_glyph (struct it *it)
24102 struct glyph *glyph;
24103 enum glyph_row_area area = it->area;
24105 eassert (it->glyph_row);
24107 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24108 if (glyph < it->glyph_row->glyphs[area + 1])
24110 /* If the glyph row is reversed, we need to prepend the glyph
24111 rather than append it. */
24112 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24114 struct glyph *g;
24116 /* Make room for the new glyph. */
24117 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24118 g[1] = *g;
24119 glyph = it->glyph_row->glyphs[it->area];
24121 glyph->charpos = it->cmp_it.charpos;
24122 glyph->object = it->object;
24123 glyph->pixel_width = it->pixel_width;
24124 glyph->ascent = it->ascent;
24125 glyph->descent = it->descent;
24126 glyph->voffset = it->voffset;
24127 glyph->type = COMPOSITE_GLYPH;
24128 if (it->cmp_it.ch < 0)
24130 glyph->u.cmp.automatic = 0;
24131 glyph->u.cmp.id = it->cmp_it.id;
24132 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24134 else
24136 glyph->u.cmp.automatic = 1;
24137 glyph->u.cmp.id = it->cmp_it.id;
24138 glyph->slice.cmp.from = it->cmp_it.from;
24139 glyph->slice.cmp.to = it->cmp_it.to - 1;
24141 glyph->avoid_cursor_p = it->avoid_cursor_p;
24142 glyph->multibyte_p = it->multibyte_p;
24143 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24145 /* In R2L rows, the left and the right box edges need to be
24146 drawn in reverse direction. */
24147 glyph->right_box_line_p = it->start_of_box_run_p;
24148 glyph->left_box_line_p = it->end_of_box_run_p;
24150 else
24152 glyph->left_box_line_p = it->start_of_box_run_p;
24153 glyph->right_box_line_p = it->end_of_box_run_p;
24155 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24156 || it->phys_descent > it->descent);
24157 glyph->padding_p = 0;
24158 glyph->glyph_not_available_p = 0;
24159 glyph->face_id = it->face_id;
24160 glyph->font_type = FONT_TYPE_UNKNOWN;
24161 if (it->bidi_p)
24163 glyph->resolved_level = it->bidi_it.resolved_level;
24164 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24165 emacs_abort ();
24166 glyph->bidi_type = it->bidi_it.type;
24168 ++it->glyph_row->used[area];
24170 else
24171 IT_EXPAND_MATRIX_WIDTH (it, area);
24175 /* Change IT->ascent and IT->height according to the setting of
24176 IT->voffset. */
24178 static void
24179 take_vertical_position_into_account (struct it *it)
24181 if (it->voffset)
24183 if (it->voffset < 0)
24184 /* Increase the ascent so that we can display the text higher
24185 in the line. */
24186 it->ascent -= it->voffset;
24187 else
24188 /* Increase the descent so that we can display the text lower
24189 in the line. */
24190 it->descent += it->voffset;
24195 /* Produce glyphs/get display metrics for the image IT is loaded with.
24196 See the description of struct display_iterator in dispextern.h for
24197 an overview of struct display_iterator. */
24199 static void
24200 produce_image_glyph (struct it *it)
24202 struct image *img;
24203 struct face *face;
24204 int glyph_ascent, crop;
24205 struct glyph_slice slice;
24207 eassert (it->what == IT_IMAGE);
24209 face = FACE_FROM_ID (it->f, it->face_id);
24210 eassert (face);
24211 /* Make sure X resources of the face is loaded. */
24212 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24214 if (it->image_id < 0)
24216 /* Fringe bitmap. */
24217 it->ascent = it->phys_ascent = 0;
24218 it->descent = it->phys_descent = 0;
24219 it->pixel_width = 0;
24220 it->nglyphs = 0;
24221 return;
24224 img = IMAGE_FROM_ID (it->f, it->image_id);
24225 eassert (img);
24226 /* Make sure X resources of the image is loaded. */
24227 prepare_image_for_display (it->f, img);
24229 slice.x = slice.y = 0;
24230 slice.width = img->width;
24231 slice.height = img->height;
24233 if (INTEGERP (it->slice.x))
24234 slice.x = XINT (it->slice.x);
24235 else if (FLOATP (it->slice.x))
24236 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24238 if (INTEGERP (it->slice.y))
24239 slice.y = XINT (it->slice.y);
24240 else if (FLOATP (it->slice.y))
24241 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24243 if (INTEGERP (it->slice.width))
24244 slice.width = XINT (it->slice.width);
24245 else if (FLOATP (it->slice.width))
24246 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24248 if (INTEGERP (it->slice.height))
24249 slice.height = XINT (it->slice.height);
24250 else if (FLOATP (it->slice.height))
24251 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24253 if (slice.x >= img->width)
24254 slice.x = img->width;
24255 if (slice.y >= img->height)
24256 slice.y = img->height;
24257 if (slice.x + slice.width >= img->width)
24258 slice.width = img->width - slice.x;
24259 if (slice.y + slice.height > img->height)
24260 slice.height = img->height - slice.y;
24262 if (slice.width == 0 || slice.height == 0)
24263 return;
24265 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24267 it->descent = slice.height - glyph_ascent;
24268 if (slice.y == 0)
24269 it->descent += img->vmargin;
24270 if (slice.y + slice.height == img->height)
24271 it->descent += img->vmargin;
24272 it->phys_descent = it->descent;
24274 it->pixel_width = slice.width;
24275 if (slice.x == 0)
24276 it->pixel_width += img->hmargin;
24277 if (slice.x + slice.width == img->width)
24278 it->pixel_width += img->hmargin;
24280 /* It's quite possible for images to have an ascent greater than
24281 their height, so don't get confused in that case. */
24282 if (it->descent < 0)
24283 it->descent = 0;
24285 it->nglyphs = 1;
24287 if (face->box != FACE_NO_BOX)
24289 if (face->box_line_width > 0)
24291 if (slice.y == 0)
24292 it->ascent += face->box_line_width;
24293 if (slice.y + slice.height == img->height)
24294 it->descent += face->box_line_width;
24297 if (it->start_of_box_run_p && slice.x == 0)
24298 it->pixel_width += eabs (face->box_line_width);
24299 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24300 it->pixel_width += eabs (face->box_line_width);
24303 take_vertical_position_into_account (it);
24305 /* Automatically crop wide image glyphs at right edge so we can
24306 draw the cursor on same display row. */
24307 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24308 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24310 it->pixel_width -= crop;
24311 slice.width -= crop;
24314 if (it->glyph_row)
24316 struct glyph *glyph;
24317 enum glyph_row_area area = it->area;
24319 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24320 if (glyph < it->glyph_row->glyphs[area + 1])
24322 glyph->charpos = CHARPOS (it->position);
24323 glyph->object = it->object;
24324 glyph->pixel_width = it->pixel_width;
24325 glyph->ascent = glyph_ascent;
24326 glyph->descent = it->descent;
24327 glyph->voffset = it->voffset;
24328 glyph->type = IMAGE_GLYPH;
24329 glyph->avoid_cursor_p = it->avoid_cursor_p;
24330 glyph->multibyte_p = it->multibyte_p;
24331 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24333 /* In R2L rows, the left and the right box edges need to be
24334 drawn in reverse direction. */
24335 glyph->right_box_line_p = it->start_of_box_run_p;
24336 glyph->left_box_line_p = it->end_of_box_run_p;
24338 else
24340 glyph->left_box_line_p = it->start_of_box_run_p;
24341 glyph->right_box_line_p = it->end_of_box_run_p;
24343 glyph->overlaps_vertically_p = 0;
24344 glyph->padding_p = 0;
24345 glyph->glyph_not_available_p = 0;
24346 glyph->face_id = it->face_id;
24347 glyph->u.img_id = img->id;
24348 glyph->slice.img = slice;
24349 glyph->font_type = FONT_TYPE_UNKNOWN;
24350 if (it->bidi_p)
24352 glyph->resolved_level = it->bidi_it.resolved_level;
24353 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24354 emacs_abort ();
24355 glyph->bidi_type = it->bidi_it.type;
24357 ++it->glyph_row->used[area];
24359 else
24360 IT_EXPAND_MATRIX_WIDTH (it, area);
24365 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24366 of the glyph, WIDTH and HEIGHT are the width and height of the
24367 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24369 static void
24370 append_stretch_glyph (struct it *it, Lisp_Object object,
24371 int width, int height, int ascent)
24373 struct glyph *glyph;
24374 enum glyph_row_area area = it->area;
24376 eassert (ascent >= 0 && ascent <= height);
24378 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24379 if (glyph < it->glyph_row->glyphs[area + 1])
24381 /* If the glyph row is reversed, we need to prepend the glyph
24382 rather than append it. */
24383 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24385 struct glyph *g;
24387 /* Make room for the additional glyph. */
24388 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24389 g[1] = *g;
24390 glyph = it->glyph_row->glyphs[area];
24392 glyph->charpos = CHARPOS (it->position);
24393 glyph->object = object;
24394 glyph->pixel_width = width;
24395 glyph->ascent = ascent;
24396 glyph->descent = height - ascent;
24397 glyph->voffset = it->voffset;
24398 glyph->type = STRETCH_GLYPH;
24399 glyph->avoid_cursor_p = it->avoid_cursor_p;
24400 glyph->multibyte_p = it->multibyte_p;
24401 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24403 /* In R2L rows, the left and the right box edges need to be
24404 drawn in reverse direction. */
24405 glyph->right_box_line_p = it->start_of_box_run_p;
24406 glyph->left_box_line_p = it->end_of_box_run_p;
24408 else
24410 glyph->left_box_line_p = it->start_of_box_run_p;
24411 glyph->right_box_line_p = it->end_of_box_run_p;
24413 glyph->overlaps_vertically_p = 0;
24414 glyph->padding_p = 0;
24415 glyph->glyph_not_available_p = 0;
24416 glyph->face_id = it->face_id;
24417 glyph->u.stretch.ascent = ascent;
24418 glyph->u.stretch.height = height;
24419 glyph->slice.img = null_glyph_slice;
24420 glyph->font_type = FONT_TYPE_UNKNOWN;
24421 if (it->bidi_p)
24423 glyph->resolved_level = it->bidi_it.resolved_level;
24424 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24425 emacs_abort ();
24426 glyph->bidi_type = it->bidi_it.type;
24428 else
24430 glyph->resolved_level = 0;
24431 glyph->bidi_type = UNKNOWN_BT;
24433 ++it->glyph_row->used[area];
24435 else
24436 IT_EXPAND_MATRIX_WIDTH (it, area);
24439 #endif /* HAVE_WINDOW_SYSTEM */
24441 /* Produce a stretch glyph for iterator IT. IT->object is the value
24442 of the glyph property displayed. The value must be a list
24443 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24444 being recognized:
24446 1. `:width WIDTH' specifies that the space should be WIDTH *
24447 canonical char width wide. WIDTH may be an integer or floating
24448 point number.
24450 2. `:relative-width FACTOR' specifies that the width of the stretch
24451 should be computed from the width of the first character having the
24452 `glyph' property, and should be FACTOR times that width.
24454 3. `:align-to HPOS' specifies that the space should be wide enough
24455 to reach HPOS, a value in canonical character units.
24457 Exactly one of the above pairs must be present.
24459 4. `:height HEIGHT' specifies that the height of the stretch produced
24460 should be HEIGHT, measured in canonical character units.
24462 5. `:relative-height FACTOR' specifies that the height of the
24463 stretch should be FACTOR times the height of the characters having
24464 the glyph property.
24466 Either none or exactly one of 4 or 5 must be present.
24468 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24469 of the stretch should be used for the ascent of the stretch.
24470 ASCENT must be in the range 0 <= ASCENT <= 100. */
24472 void
24473 produce_stretch_glyph (struct it *it)
24475 /* (space :width WIDTH :height HEIGHT ...) */
24476 Lisp_Object prop, plist;
24477 int width = 0, height = 0, align_to = -1;
24478 int zero_width_ok_p = 0;
24479 double tem;
24480 struct font *font = NULL;
24482 #ifdef HAVE_WINDOW_SYSTEM
24483 int ascent = 0;
24484 int zero_height_ok_p = 0;
24486 if (FRAME_WINDOW_P (it->f))
24488 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24489 font = face->font ? face->font : FRAME_FONT (it->f);
24490 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24492 #endif
24494 /* List should start with `space'. */
24495 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24496 plist = XCDR (it->object);
24498 /* Compute the width of the stretch. */
24499 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24500 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24502 /* Absolute width `:width WIDTH' specified and valid. */
24503 zero_width_ok_p = 1;
24504 width = (int)tem;
24506 #ifdef HAVE_WINDOW_SYSTEM
24507 else if (FRAME_WINDOW_P (it->f)
24508 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24510 /* Relative width `:relative-width FACTOR' specified and valid.
24511 Compute the width of the characters having the `glyph'
24512 property. */
24513 struct it it2;
24514 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24516 it2 = *it;
24517 if (it->multibyte_p)
24518 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24519 else
24521 it2.c = it2.char_to_display = *p, it2.len = 1;
24522 if (! ASCII_CHAR_P (it2.c))
24523 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24526 it2.glyph_row = NULL;
24527 it2.what = IT_CHARACTER;
24528 x_produce_glyphs (&it2);
24529 width = NUMVAL (prop) * it2.pixel_width;
24531 #endif /* HAVE_WINDOW_SYSTEM */
24532 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24533 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24535 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24536 align_to = (align_to < 0
24538 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24539 else if (align_to < 0)
24540 align_to = window_box_left_offset (it->w, TEXT_AREA);
24541 width = max (0, (int)tem + align_to - it->current_x);
24542 zero_width_ok_p = 1;
24544 else
24545 /* Nothing specified -> width defaults to canonical char width. */
24546 width = FRAME_COLUMN_WIDTH (it->f);
24548 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24549 width = 1;
24551 #ifdef HAVE_WINDOW_SYSTEM
24552 /* Compute height. */
24553 if (FRAME_WINDOW_P (it->f))
24555 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24556 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24558 height = (int)tem;
24559 zero_height_ok_p = 1;
24561 else if (prop = Fplist_get (plist, QCrelative_height),
24562 NUMVAL (prop) > 0)
24563 height = FONT_HEIGHT (font) * NUMVAL (prop);
24564 else
24565 height = FONT_HEIGHT (font);
24567 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24568 height = 1;
24570 /* Compute percentage of height used for ascent. If
24571 `:ascent ASCENT' is present and valid, use that. Otherwise,
24572 derive the ascent from the font in use. */
24573 if (prop = Fplist_get (plist, QCascent),
24574 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24575 ascent = height * NUMVAL (prop) / 100.0;
24576 else if (!NILP (prop)
24577 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24578 ascent = min (max (0, (int)tem), height);
24579 else
24580 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24582 else
24583 #endif /* HAVE_WINDOW_SYSTEM */
24584 height = 1;
24586 if (width > 0 && it->line_wrap != TRUNCATE
24587 && it->current_x + width > it->last_visible_x)
24589 width = it->last_visible_x - it->current_x;
24590 #ifdef HAVE_WINDOW_SYSTEM
24591 /* Subtract one more pixel from the stretch width, but only on
24592 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24593 width -= FRAME_WINDOW_P (it->f);
24594 #endif
24597 if (width > 0 && height > 0 && it->glyph_row)
24599 Lisp_Object o_object = it->object;
24600 Lisp_Object object = it->stack[it->sp - 1].string;
24601 int n = width;
24603 if (!STRINGP (object))
24604 object = it->w->contents;
24605 #ifdef HAVE_WINDOW_SYSTEM
24606 if (FRAME_WINDOW_P (it->f))
24607 append_stretch_glyph (it, object, width, height, ascent);
24608 else
24609 #endif
24611 it->object = object;
24612 it->char_to_display = ' ';
24613 it->pixel_width = it->len = 1;
24614 while (n--)
24615 tty_append_glyph (it);
24616 it->object = o_object;
24620 it->pixel_width = width;
24621 #ifdef HAVE_WINDOW_SYSTEM
24622 if (FRAME_WINDOW_P (it->f))
24624 it->ascent = it->phys_ascent = ascent;
24625 it->descent = it->phys_descent = height - it->ascent;
24626 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24627 take_vertical_position_into_account (it);
24629 else
24630 #endif
24631 it->nglyphs = width;
24634 /* Get information about special display element WHAT in an
24635 environment described by IT. WHAT is one of IT_TRUNCATION or
24636 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24637 non-null glyph_row member. This function ensures that fields like
24638 face_id, c, len of IT are left untouched. */
24640 static void
24641 produce_special_glyphs (struct it *it, enum display_element_type what)
24643 struct it temp_it;
24644 Lisp_Object gc;
24645 GLYPH glyph;
24647 temp_it = *it;
24648 temp_it.object = make_number (0);
24649 memset (&temp_it.current, 0, sizeof temp_it.current);
24651 if (what == IT_CONTINUATION)
24653 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24654 if (it->bidi_it.paragraph_dir == R2L)
24655 SET_GLYPH_FROM_CHAR (glyph, '/');
24656 else
24657 SET_GLYPH_FROM_CHAR (glyph, '\\');
24658 if (it->dp
24659 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24661 /* FIXME: Should we mirror GC for R2L lines? */
24662 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24663 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24666 else if (what == IT_TRUNCATION)
24668 /* Truncation glyph. */
24669 SET_GLYPH_FROM_CHAR (glyph, '$');
24670 if (it->dp
24671 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24673 /* FIXME: Should we mirror GC for R2L lines? */
24674 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24675 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24678 else
24679 emacs_abort ();
24681 #ifdef HAVE_WINDOW_SYSTEM
24682 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24683 is turned off, we precede the truncation/continuation glyphs by a
24684 stretch glyph whose width is computed such that these special
24685 glyphs are aligned at the window margin, even when very different
24686 fonts are used in different glyph rows. */
24687 if (FRAME_WINDOW_P (temp_it.f)
24688 /* init_iterator calls this with it->glyph_row == NULL, and it
24689 wants only the pixel width of the truncation/continuation
24690 glyphs. */
24691 && temp_it.glyph_row
24692 /* insert_left_trunc_glyphs calls us at the beginning of the
24693 row, and it has its own calculation of the stretch glyph
24694 width. */
24695 && temp_it.glyph_row->used[TEXT_AREA] > 0
24696 && (temp_it.glyph_row->reversed_p
24697 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24698 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24700 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24702 if (stretch_width > 0)
24704 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24705 struct font *font =
24706 face->font ? face->font : FRAME_FONT (temp_it.f);
24707 int stretch_ascent =
24708 (((temp_it.ascent + temp_it.descent)
24709 * FONT_BASE (font)) / FONT_HEIGHT (font));
24711 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24712 temp_it.ascent + temp_it.descent,
24713 stretch_ascent);
24716 #endif
24718 temp_it.dp = NULL;
24719 temp_it.what = IT_CHARACTER;
24720 temp_it.len = 1;
24721 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24722 temp_it.face_id = GLYPH_FACE (glyph);
24723 temp_it.len = CHAR_BYTES (temp_it.c);
24725 PRODUCE_GLYPHS (&temp_it);
24726 it->pixel_width = temp_it.pixel_width;
24727 it->nglyphs = temp_it.pixel_width;
24730 #ifdef HAVE_WINDOW_SYSTEM
24732 /* Calculate line-height and line-spacing properties.
24733 An integer value specifies explicit pixel value.
24734 A float value specifies relative value to current face height.
24735 A cons (float . face-name) specifies relative value to
24736 height of specified face font.
24738 Returns height in pixels, or nil. */
24741 static Lisp_Object
24742 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24743 int boff, int override)
24745 Lisp_Object face_name = Qnil;
24746 int ascent, descent, height;
24748 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24749 return val;
24751 if (CONSP (val))
24753 face_name = XCAR (val);
24754 val = XCDR (val);
24755 if (!NUMBERP (val))
24756 val = make_number (1);
24757 if (NILP (face_name))
24759 height = it->ascent + it->descent;
24760 goto scale;
24764 if (NILP (face_name))
24766 font = FRAME_FONT (it->f);
24767 boff = FRAME_BASELINE_OFFSET (it->f);
24769 else if (EQ (face_name, Qt))
24771 override = 0;
24773 else
24775 int face_id;
24776 struct face *face;
24778 face_id = lookup_named_face (it->f, face_name, 0);
24779 if (face_id < 0)
24780 return make_number (-1);
24782 face = FACE_FROM_ID (it->f, face_id);
24783 font = face->font;
24784 if (font == NULL)
24785 return make_number (-1);
24786 boff = font->baseline_offset;
24787 if (font->vertical_centering)
24788 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24791 ascent = FONT_BASE (font) + boff;
24792 descent = FONT_DESCENT (font) - boff;
24794 if (override)
24796 it->override_ascent = ascent;
24797 it->override_descent = descent;
24798 it->override_boff = boff;
24801 height = ascent + descent;
24803 scale:
24804 if (FLOATP (val))
24805 height = (int)(XFLOAT_DATA (val) * height);
24806 else if (INTEGERP (val))
24807 height *= XINT (val);
24809 return make_number (height);
24813 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24814 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24815 and only if this is for a character for which no font was found.
24817 If the display method (it->glyphless_method) is
24818 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24819 length of the acronym or the hexadecimal string, UPPER_XOFF and
24820 UPPER_YOFF are pixel offsets for the upper part of the string,
24821 LOWER_XOFF and LOWER_YOFF are for the lower part.
24823 For the other display methods, LEN through LOWER_YOFF are zero. */
24825 static void
24826 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24827 short upper_xoff, short upper_yoff,
24828 short lower_xoff, short lower_yoff)
24830 struct glyph *glyph;
24831 enum glyph_row_area area = it->area;
24833 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24834 if (glyph < it->glyph_row->glyphs[area + 1])
24836 /* If the glyph row is reversed, we need to prepend the glyph
24837 rather than append it. */
24838 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24840 struct glyph *g;
24842 /* Make room for the additional glyph. */
24843 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24844 g[1] = *g;
24845 glyph = it->glyph_row->glyphs[area];
24847 glyph->charpos = CHARPOS (it->position);
24848 glyph->object = it->object;
24849 glyph->pixel_width = it->pixel_width;
24850 glyph->ascent = it->ascent;
24851 glyph->descent = it->descent;
24852 glyph->voffset = it->voffset;
24853 glyph->type = GLYPHLESS_GLYPH;
24854 glyph->u.glyphless.method = it->glyphless_method;
24855 glyph->u.glyphless.for_no_font = for_no_font;
24856 glyph->u.glyphless.len = len;
24857 glyph->u.glyphless.ch = it->c;
24858 glyph->slice.glyphless.upper_xoff = upper_xoff;
24859 glyph->slice.glyphless.upper_yoff = upper_yoff;
24860 glyph->slice.glyphless.lower_xoff = lower_xoff;
24861 glyph->slice.glyphless.lower_yoff = lower_yoff;
24862 glyph->avoid_cursor_p = it->avoid_cursor_p;
24863 glyph->multibyte_p = it->multibyte_p;
24864 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24866 /* In R2L rows, the left and the right box edges need to be
24867 drawn in reverse direction. */
24868 glyph->right_box_line_p = it->start_of_box_run_p;
24869 glyph->left_box_line_p = it->end_of_box_run_p;
24871 else
24873 glyph->left_box_line_p = it->start_of_box_run_p;
24874 glyph->right_box_line_p = it->end_of_box_run_p;
24876 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24877 || it->phys_descent > it->descent);
24878 glyph->padding_p = 0;
24879 glyph->glyph_not_available_p = 0;
24880 glyph->face_id = face_id;
24881 glyph->font_type = FONT_TYPE_UNKNOWN;
24882 if (it->bidi_p)
24884 glyph->resolved_level = it->bidi_it.resolved_level;
24885 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24886 emacs_abort ();
24887 glyph->bidi_type = it->bidi_it.type;
24889 ++it->glyph_row->used[area];
24891 else
24892 IT_EXPAND_MATRIX_WIDTH (it, area);
24896 /* Produce a glyph for a glyphless character for iterator IT.
24897 IT->glyphless_method specifies which method to use for displaying
24898 the character. See the description of enum
24899 glyphless_display_method in dispextern.h for the detail.
24901 FOR_NO_FONT is nonzero if and only if this is for a character for
24902 which no font was found. ACRONYM, if non-nil, is an acronym string
24903 for the character. */
24905 static void
24906 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24908 int face_id;
24909 struct face *face;
24910 struct font *font;
24911 int base_width, base_height, width, height;
24912 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24913 int len;
24915 /* Get the metrics of the base font. We always refer to the current
24916 ASCII face. */
24917 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24918 font = face->font ? face->font : FRAME_FONT (it->f);
24919 it->ascent = FONT_BASE (font) + font->baseline_offset;
24920 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24921 base_height = it->ascent + it->descent;
24922 base_width = font->average_width;
24924 face_id = merge_glyphless_glyph_face (it);
24926 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24928 it->pixel_width = THIN_SPACE_WIDTH;
24929 len = 0;
24930 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24932 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24934 width = CHAR_WIDTH (it->c);
24935 if (width == 0)
24936 width = 1;
24937 else if (width > 4)
24938 width = 4;
24939 it->pixel_width = base_width * width;
24940 len = 0;
24941 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24943 else
24945 char buf[7];
24946 const char *str;
24947 unsigned int code[6];
24948 int upper_len;
24949 int ascent, descent;
24950 struct font_metrics metrics_upper, metrics_lower;
24952 face = FACE_FROM_ID (it->f, face_id);
24953 font = face->font ? face->font : FRAME_FONT (it->f);
24954 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24956 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24958 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24959 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24960 if (CONSP (acronym))
24961 acronym = XCAR (acronym);
24962 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24964 else
24966 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24967 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24968 str = buf;
24970 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24971 code[len] = font->driver->encode_char (font, str[len]);
24972 upper_len = (len + 1) / 2;
24973 font->driver->text_extents (font, code, upper_len,
24974 &metrics_upper);
24975 font->driver->text_extents (font, code + upper_len, len - upper_len,
24976 &metrics_lower);
24980 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24981 width = max (metrics_upper.width, metrics_lower.width) + 4;
24982 upper_xoff = upper_yoff = 2; /* the typical case */
24983 if (base_width >= width)
24985 /* Align the upper to the left, the lower to the right. */
24986 it->pixel_width = base_width;
24987 lower_xoff = base_width - 2 - metrics_lower.width;
24989 else
24991 /* Center the shorter one. */
24992 it->pixel_width = width;
24993 if (metrics_upper.width >= metrics_lower.width)
24994 lower_xoff = (width - metrics_lower.width) / 2;
24995 else
24997 /* FIXME: This code doesn't look right. It formerly was
24998 missing the "lower_xoff = 0;", which couldn't have
24999 been right since it left lower_xoff uninitialized. */
25000 lower_xoff = 0;
25001 upper_xoff = (width - metrics_upper.width) / 2;
25005 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25006 top, bottom, and between upper and lower strings. */
25007 height = (metrics_upper.ascent + metrics_upper.descent
25008 + metrics_lower.ascent + metrics_lower.descent) + 5;
25009 /* Center vertically.
25010 H:base_height, D:base_descent
25011 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25013 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25014 descent = D - H/2 + h/2;
25015 lower_yoff = descent - 2 - ld;
25016 upper_yoff = lower_yoff - la - 1 - ud; */
25017 ascent = - (it->descent - (base_height + height + 1) / 2);
25018 descent = it->descent - (base_height - height) / 2;
25019 lower_yoff = descent - 2 - metrics_lower.descent;
25020 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25021 - metrics_upper.descent);
25022 /* Don't make the height shorter than the base height. */
25023 if (height > base_height)
25025 it->ascent = ascent;
25026 it->descent = descent;
25030 it->phys_ascent = it->ascent;
25031 it->phys_descent = it->descent;
25032 if (it->glyph_row)
25033 append_glyphless_glyph (it, face_id, for_no_font, len,
25034 upper_xoff, upper_yoff,
25035 lower_xoff, lower_yoff);
25036 it->nglyphs = 1;
25037 take_vertical_position_into_account (it);
25041 /* RIF:
25042 Produce glyphs/get display metrics for the display element IT is
25043 loaded with. See the description of struct it in dispextern.h
25044 for an overview of struct it. */
25046 void
25047 x_produce_glyphs (struct it *it)
25049 int extra_line_spacing = it->extra_line_spacing;
25051 it->glyph_not_available_p = 0;
25053 if (it->what == IT_CHARACTER)
25055 XChar2b char2b;
25056 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25057 struct font *font = face->font;
25058 struct font_metrics *pcm = NULL;
25059 int boff; /* baseline offset */
25061 if (font == NULL)
25063 /* When no suitable font is found, display this character by
25064 the method specified in the first extra slot of
25065 Vglyphless_char_display. */
25066 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25068 eassert (it->what == IT_GLYPHLESS);
25069 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25070 goto done;
25073 boff = font->baseline_offset;
25074 if (font->vertical_centering)
25075 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25077 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25079 int stretched_p;
25081 it->nglyphs = 1;
25083 if (it->override_ascent >= 0)
25085 it->ascent = it->override_ascent;
25086 it->descent = it->override_descent;
25087 boff = it->override_boff;
25089 else
25091 it->ascent = FONT_BASE (font) + boff;
25092 it->descent = FONT_DESCENT (font) - boff;
25095 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25097 pcm = get_per_char_metric (font, &char2b);
25098 if (pcm->width == 0
25099 && pcm->rbearing == 0 && pcm->lbearing == 0)
25100 pcm = NULL;
25103 if (pcm)
25105 it->phys_ascent = pcm->ascent + boff;
25106 it->phys_descent = pcm->descent - boff;
25107 it->pixel_width = pcm->width;
25109 else
25111 it->glyph_not_available_p = 1;
25112 it->phys_ascent = it->ascent;
25113 it->phys_descent = it->descent;
25114 it->pixel_width = font->space_width;
25117 if (it->constrain_row_ascent_descent_p)
25119 if (it->descent > it->max_descent)
25121 it->ascent += it->descent - it->max_descent;
25122 it->descent = it->max_descent;
25124 if (it->ascent > it->max_ascent)
25126 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25127 it->ascent = it->max_ascent;
25129 it->phys_ascent = min (it->phys_ascent, it->ascent);
25130 it->phys_descent = min (it->phys_descent, it->descent);
25131 extra_line_spacing = 0;
25134 /* If this is a space inside a region of text with
25135 `space-width' property, change its width. */
25136 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25137 if (stretched_p)
25138 it->pixel_width *= XFLOATINT (it->space_width);
25140 /* If face has a box, add the box thickness to the character
25141 height. If character has a box line to the left and/or
25142 right, add the box line width to the character's width. */
25143 if (face->box != FACE_NO_BOX)
25145 int thick = face->box_line_width;
25147 if (thick > 0)
25149 it->ascent += thick;
25150 it->descent += thick;
25152 else
25153 thick = -thick;
25155 if (it->start_of_box_run_p)
25156 it->pixel_width += thick;
25157 if (it->end_of_box_run_p)
25158 it->pixel_width += thick;
25161 /* If face has an overline, add the height of the overline
25162 (1 pixel) and a 1 pixel margin to the character height. */
25163 if (face->overline_p)
25164 it->ascent += overline_margin;
25166 if (it->constrain_row_ascent_descent_p)
25168 if (it->ascent > it->max_ascent)
25169 it->ascent = it->max_ascent;
25170 if (it->descent > it->max_descent)
25171 it->descent = it->max_descent;
25174 take_vertical_position_into_account (it);
25176 /* If we have to actually produce glyphs, do it. */
25177 if (it->glyph_row)
25179 if (stretched_p)
25181 /* Translate a space with a `space-width' property
25182 into a stretch glyph. */
25183 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25184 / FONT_HEIGHT (font));
25185 append_stretch_glyph (it, it->object, it->pixel_width,
25186 it->ascent + it->descent, ascent);
25188 else
25189 append_glyph (it);
25191 /* If characters with lbearing or rbearing are displayed
25192 in this line, record that fact in a flag of the
25193 glyph row. This is used to optimize X output code. */
25194 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25195 it->glyph_row->contains_overlapping_glyphs_p = 1;
25197 if (! stretched_p && it->pixel_width == 0)
25198 /* We assure that all visible glyphs have at least 1-pixel
25199 width. */
25200 it->pixel_width = 1;
25202 else if (it->char_to_display == '\n')
25204 /* A newline has no width, but we need the height of the
25205 line. But if previous part of the line sets a height,
25206 don't increase that height */
25208 Lisp_Object height;
25209 Lisp_Object total_height = Qnil;
25211 it->override_ascent = -1;
25212 it->pixel_width = 0;
25213 it->nglyphs = 0;
25215 height = get_it_property (it, Qline_height);
25216 /* Split (line-height total-height) list */
25217 if (CONSP (height)
25218 && CONSP (XCDR (height))
25219 && NILP (XCDR (XCDR (height))))
25221 total_height = XCAR (XCDR (height));
25222 height = XCAR (height);
25224 height = calc_line_height_property (it, height, font, boff, 1);
25226 if (it->override_ascent >= 0)
25228 it->ascent = it->override_ascent;
25229 it->descent = it->override_descent;
25230 boff = it->override_boff;
25232 else
25234 it->ascent = FONT_BASE (font) + boff;
25235 it->descent = FONT_DESCENT (font) - boff;
25238 if (EQ (height, Qt))
25240 if (it->descent > it->max_descent)
25242 it->ascent += it->descent - it->max_descent;
25243 it->descent = it->max_descent;
25245 if (it->ascent > it->max_ascent)
25247 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25248 it->ascent = it->max_ascent;
25250 it->phys_ascent = min (it->phys_ascent, it->ascent);
25251 it->phys_descent = min (it->phys_descent, it->descent);
25252 it->constrain_row_ascent_descent_p = 1;
25253 extra_line_spacing = 0;
25255 else
25257 Lisp_Object spacing;
25259 it->phys_ascent = it->ascent;
25260 it->phys_descent = it->descent;
25262 if ((it->max_ascent > 0 || it->max_descent > 0)
25263 && face->box != FACE_NO_BOX
25264 && face->box_line_width > 0)
25266 it->ascent += face->box_line_width;
25267 it->descent += face->box_line_width;
25269 if (!NILP (height)
25270 && XINT (height) > it->ascent + it->descent)
25271 it->ascent = XINT (height) - it->descent;
25273 if (!NILP (total_height))
25274 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25275 else
25277 spacing = get_it_property (it, Qline_spacing);
25278 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25280 if (INTEGERP (spacing))
25282 extra_line_spacing = XINT (spacing);
25283 if (!NILP (total_height))
25284 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25288 else /* i.e. (it->char_to_display == '\t') */
25290 if (font->space_width > 0)
25292 int tab_width = it->tab_width * font->space_width;
25293 int x = it->current_x + it->continuation_lines_width;
25294 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25296 /* If the distance from the current position to the next tab
25297 stop is less than a space character width, use the
25298 tab stop after that. */
25299 if (next_tab_x - x < font->space_width)
25300 next_tab_x += tab_width;
25302 it->pixel_width = next_tab_x - x;
25303 it->nglyphs = 1;
25304 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25305 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25307 if (it->glyph_row)
25309 append_stretch_glyph (it, it->object, it->pixel_width,
25310 it->ascent + it->descent, it->ascent);
25313 else
25315 it->pixel_width = 0;
25316 it->nglyphs = 1;
25320 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25322 /* A static composition.
25324 Note: A composition is represented as one glyph in the
25325 glyph matrix. There are no padding glyphs.
25327 Important note: pixel_width, ascent, and descent are the
25328 values of what is drawn by draw_glyphs (i.e. the values of
25329 the overall glyphs composed). */
25330 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25331 int boff; /* baseline offset */
25332 struct composition *cmp = composition_table[it->cmp_it.id];
25333 int glyph_len = cmp->glyph_len;
25334 struct font *font = face->font;
25336 it->nglyphs = 1;
25338 /* If we have not yet calculated pixel size data of glyphs of
25339 the composition for the current face font, calculate them
25340 now. Theoretically, we have to check all fonts for the
25341 glyphs, but that requires much time and memory space. So,
25342 here we check only the font of the first glyph. This may
25343 lead to incorrect display, but it's very rare, and C-l
25344 (recenter-top-bottom) can correct the display anyway. */
25345 if (! cmp->font || cmp->font != font)
25347 /* Ascent and descent of the font of the first character
25348 of this composition (adjusted by baseline offset).
25349 Ascent and descent of overall glyphs should not be less
25350 than these, respectively. */
25351 int font_ascent, font_descent, font_height;
25352 /* Bounding box of the overall glyphs. */
25353 int leftmost, rightmost, lowest, highest;
25354 int lbearing, rbearing;
25355 int i, width, ascent, descent;
25356 int left_padded = 0, right_padded = 0;
25357 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25358 XChar2b char2b;
25359 struct font_metrics *pcm;
25360 int font_not_found_p;
25361 ptrdiff_t pos;
25363 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25364 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25365 break;
25366 if (glyph_len < cmp->glyph_len)
25367 right_padded = 1;
25368 for (i = 0; i < glyph_len; i++)
25370 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25371 break;
25372 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25374 if (i > 0)
25375 left_padded = 1;
25377 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25378 : IT_CHARPOS (*it));
25379 /* If no suitable font is found, use the default font. */
25380 font_not_found_p = font == NULL;
25381 if (font_not_found_p)
25383 face = face->ascii_face;
25384 font = face->font;
25386 boff = font->baseline_offset;
25387 if (font->vertical_centering)
25388 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25389 font_ascent = FONT_BASE (font) + boff;
25390 font_descent = FONT_DESCENT (font) - boff;
25391 font_height = FONT_HEIGHT (font);
25393 cmp->font = font;
25395 pcm = NULL;
25396 if (! font_not_found_p)
25398 get_char_face_and_encoding (it->f, c, it->face_id,
25399 &char2b, 0);
25400 pcm = get_per_char_metric (font, &char2b);
25403 /* Initialize the bounding box. */
25404 if (pcm)
25406 width = cmp->glyph_len > 0 ? pcm->width : 0;
25407 ascent = pcm->ascent;
25408 descent = pcm->descent;
25409 lbearing = pcm->lbearing;
25410 rbearing = pcm->rbearing;
25412 else
25414 width = cmp->glyph_len > 0 ? font->space_width : 0;
25415 ascent = FONT_BASE (font);
25416 descent = FONT_DESCENT (font);
25417 lbearing = 0;
25418 rbearing = width;
25421 rightmost = width;
25422 leftmost = 0;
25423 lowest = - descent + boff;
25424 highest = ascent + boff;
25426 if (! font_not_found_p
25427 && font->default_ascent
25428 && CHAR_TABLE_P (Vuse_default_ascent)
25429 && !NILP (Faref (Vuse_default_ascent,
25430 make_number (it->char_to_display))))
25431 highest = font->default_ascent + boff;
25433 /* Draw the first glyph at the normal position. It may be
25434 shifted to right later if some other glyphs are drawn
25435 at the left. */
25436 cmp->offsets[i * 2] = 0;
25437 cmp->offsets[i * 2 + 1] = boff;
25438 cmp->lbearing = lbearing;
25439 cmp->rbearing = rbearing;
25441 /* Set cmp->offsets for the remaining glyphs. */
25442 for (i++; i < glyph_len; i++)
25444 int left, right, btm, top;
25445 int ch = COMPOSITION_GLYPH (cmp, i);
25446 int face_id;
25447 struct face *this_face;
25449 if (ch == '\t')
25450 ch = ' ';
25451 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25452 this_face = FACE_FROM_ID (it->f, face_id);
25453 font = this_face->font;
25455 if (font == NULL)
25456 pcm = NULL;
25457 else
25459 get_char_face_and_encoding (it->f, ch, face_id,
25460 &char2b, 0);
25461 pcm = get_per_char_metric (font, &char2b);
25463 if (! pcm)
25464 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25465 else
25467 width = pcm->width;
25468 ascent = pcm->ascent;
25469 descent = pcm->descent;
25470 lbearing = pcm->lbearing;
25471 rbearing = pcm->rbearing;
25472 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25474 /* Relative composition with or without
25475 alternate chars. */
25476 left = (leftmost + rightmost - width) / 2;
25477 btm = - descent + boff;
25478 if (font->relative_compose
25479 && (! CHAR_TABLE_P (Vignore_relative_composition)
25480 || NILP (Faref (Vignore_relative_composition,
25481 make_number (ch)))))
25484 if (- descent >= font->relative_compose)
25485 /* One extra pixel between two glyphs. */
25486 btm = highest + 1;
25487 else if (ascent <= 0)
25488 /* One extra pixel between two glyphs. */
25489 btm = lowest - 1 - ascent - descent;
25492 else
25494 /* A composition rule is specified by an integer
25495 value that encodes global and new reference
25496 points (GREF and NREF). GREF and NREF are
25497 specified by numbers as below:
25499 0---1---2 -- ascent
25503 9--10--11 -- center
25505 ---3---4---5--- baseline
25507 6---7---8 -- descent
25509 int rule = COMPOSITION_RULE (cmp, i);
25510 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25512 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25513 grefx = gref % 3, nrefx = nref % 3;
25514 grefy = gref / 3, nrefy = nref / 3;
25515 if (xoff)
25516 xoff = font_height * (xoff - 128) / 256;
25517 if (yoff)
25518 yoff = font_height * (yoff - 128) / 256;
25520 left = (leftmost
25521 + grefx * (rightmost - leftmost) / 2
25522 - nrefx * width / 2
25523 + xoff);
25525 btm = ((grefy == 0 ? highest
25526 : grefy == 1 ? 0
25527 : grefy == 2 ? lowest
25528 : (highest + lowest) / 2)
25529 - (nrefy == 0 ? ascent + descent
25530 : nrefy == 1 ? descent - boff
25531 : nrefy == 2 ? 0
25532 : (ascent + descent) / 2)
25533 + yoff);
25536 cmp->offsets[i * 2] = left;
25537 cmp->offsets[i * 2 + 1] = btm + descent;
25539 /* Update the bounding box of the overall glyphs. */
25540 if (width > 0)
25542 right = left + width;
25543 if (left < leftmost)
25544 leftmost = left;
25545 if (right > rightmost)
25546 rightmost = right;
25548 top = btm + descent + ascent;
25549 if (top > highest)
25550 highest = top;
25551 if (btm < lowest)
25552 lowest = btm;
25554 if (cmp->lbearing > left + lbearing)
25555 cmp->lbearing = left + lbearing;
25556 if (cmp->rbearing < left + rbearing)
25557 cmp->rbearing = left + rbearing;
25561 /* If there are glyphs whose x-offsets are negative,
25562 shift all glyphs to the right and make all x-offsets
25563 non-negative. */
25564 if (leftmost < 0)
25566 for (i = 0; i < cmp->glyph_len; i++)
25567 cmp->offsets[i * 2] -= leftmost;
25568 rightmost -= leftmost;
25569 cmp->lbearing -= leftmost;
25570 cmp->rbearing -= leftmost;
25573 if (left_padded && cmp->lbearing < 0)
25575 for (i = 0; i < cmp->glyph_len; i++)
25576 cmp->offsets[i * 2] -= cmp->lbearing;
25577 rightmost -= cmp->lbearing;
25578 cmp->rbearing -= cmp->lbearing;
25579 cmp->lbearing = 0;
25581 if (right_padded && rightmost < cmp->rbearing)
25583 rightmost = cmp->rbearing;
25586 cmp->pixel_width = rightmost;
25587 cmp->ascent = highest;
25588 cmp->descent = - lowest;
25589 if (cmp->ascent < font_ascent)
25590 cmp->ascent = font_ascent;
25591 if (cmp->descent < font_descent)
25592 cmp->descent = font_descent;
25595 if (it->glyph_row
25596 && (cmp->lbearing < 0
25597 || cmp->rbearing > cmp->pixel_width))
25598 it->glyph_row->contains_overlapping_glyphs_p = 1;
25600 it->pixel_width = cmp->pixel_width;
25601 it->ascent = it->phys_ascent = cmp->ascent;
25602 it->descent = it->phys_descent = cmp->descent;
25603 if (face->box != FACE_NO_BOX)
25605 int thick = face->box_line_width;
25607 if (thick > 0)
25609 it->ascent += thick;
25610 it->descent += thick;
25612 else
25613 thick = - thick;
25615 if (it->start_of_box_run_p)
25616 it->pixel_width += thick;
25617 if (it->end_of_box_run_p)
25618 it->pixel_width += thick;
25621 /* If face has an overline, add the height of the overline
25622 (1 pixel) and a 1 pixel margin to the character height. */
25623 if (face->overline_p)
25624 it->ascent += overline_margin;
25626 take_vertical_position_into_account (it);
25627 if (it->ascent < 0)
25628 it->ascent = 0;
25629 if (it->descent < 0)
25630 it->descent = 0;
25632 if (it->glyph_row && cmp->glyph_len > 0)
25633 append_composite_glyph (it);
25635 else if (it->what == IT_COMPOSITION)
25637 /* A dynamic (automatic) composition. */
25638 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25639 Lisp_Object gstring;
25640 struct font_metrics metrics;
25642 it->nglyphs = 1;
25644 gstring = composition_gstring_from_id (it->cmp_it.id);
25645 it->pixel_width
25646 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25647 &metrics);
25648 if (it->glyph_row
25649 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25650 it->glyph_row->contains_overlapping_glyphs_p = 1;
25651 it->ascent = it->phys_ascent = metrics.ascent;
25652 it->descent = it->phys_descent = metrics.descent;
25653 if (face->box != FACE_NO_BOX)
25655 int thick = face->box_line_width;
25657 if (thick > 0)
25659 it->ascent += thick;
25660 it->descent += thick;
25662 else
25663 thick = - thick;
25665 if (it->start_of_box_run_p)
25666 it->pixel_width += thick;
25667 if (it->end_of_box_run_p)
25668 it->pixel_width += thick;
25670 /* If face has an overline, add the height of the overline
25671 (1 pixel) and a 1 pixel margin to the character height. */
25672 if (face->overline_p)
25673 it->ascent += overline_margin;
25674 take_vertical_position_into_account (it);
25675 if (it->ascent < 0)
25676 it->ascent = 0;
25677 if (it->descent < 0)
25678 it->descent = 0;
25680 if (it->glyph_row)
25681 append_composite_glyph (it);
25683 else if (it->what == IT_GLYPHLESS)
25684 produce_glyphless_glyph (it, 0, Qnil);
25685 else if (it->what == IT_IMAGE)
25686 produce_image_glyph (it);
25687 else if (it->what == IT_STRETCH)
25688 produce_stretch_glyph (it);
25690 done:
25691 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25692 because this isn't true for images with `:ascent 100'. */
25693 eassert (it->ascent >= 0 && it->descent >= 0);
25694 if (it->area == TEXT_AREA)
25695 it->current_x += it->pixel_width;
25697 if (extra_line_spacing > 0)
25699 it->descent += extra_line_spacing;
25700 if (extra_line_spacing > it->max_extra_line_spacing)
25701 it->max_extra_line_spacing = extra_line_spacing;
25704 it->max_ascent = max (it->max_ascent, it->ascent);
25705 it->max_descent = max (it->max_descent, it->descent);
25706 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25707 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25710 /* EXPORT for RIF:
25711 Output LEN glyphs starting at START at the nominal cursor position.
25712 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25713 being updated, and UPDATED_AREA is the area of that row being updated. */
25715 void
25716 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25717 struct glyph *start, enum glyph_row_area updated_area, int len)
25719 int x, hpos, chpos = w->phys_cursor.hpos;
25721 eassert (updated_row);
25722 /* When the window is hscrolled, cursor hpos can legitimately be out
25723 of bounds, but we draw the cursor at the corresponding window
25724 margin in that case. */
25725 if (!updated_row->reversed_p && chpos < 0)
25726 chpos = 0;
25727 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25728 chpos = updated_row->used[TEXT_AREA] - 1;
25730 block_input ();
25732 /* Write glyphs. */
25734 hpos = start - updated_row->glyphs[updated_area];
25735 x = draw_glyphs (w, w->output_cursor.x,
25736 updated_row, updated_area,
25737 hpos, hpos + len,
25738 DRAW_NORMAL_TEXT, 0);
25740 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25741 if (updated_area == TEXT_AREA
25742 && w->phys_cursor_on_p
25743 && w->phys_cursor.vpos == w->output_cursor.vpos
25744 && chpos >= hpos
25745 && chpos < hpos + len)
25746 w->phys_cursor_on_p = 0;
25748 unblock_input ();
25750 /* Advance the output cursor. */
25751 w->output_cursor.hpos += len;
25752 w->output_cursor.x = x;
25756 /* EXPORT for RIF:
25757 Insert LEN glyphs from START at the nominal cursor position. */
25759 void
25760 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25761 struct glyph *start, enum glyph_row_area updated_area, int len)
25763 struct frame *f;
25764 int line_height, shift_by_width, shifted_region_width;
25765 struct glyph_row *row;
25766 struct glyph *glyph;
25767 int frame_x, frame_y;
25768 ptrdiff_t hpos;
25770 eassert (updated_row);
25771 block_input ();
25772 f = XFRAME (WINDOW_FRAME (w));
25774 /* Get the height of the line we are in. */
25775 row = updated_row;
25776 line_height = row->height;
25778 /* Get the width of the glyphs to insert. */
25779 shift_by_width = 0;
25780 for (glyph = start; glyph < start + len; ++glyph)
25781 shift_by_width += glyph->pixel_width;
25783 /* Get the width of the region to shift right. */
25784 shifted_region_width = (window_box_width (w, updated_area)
25785 - w->output_cursor.x
25786 - shift_by_width);
25788 /* Shift right. */
25789 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25790 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25792 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25793 line_height, shift_by_width);
25795 /* Write the glyphs. */
25796 hpos = start - row->glyphs[updated_area];
25797 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25798 hpos, hpos + len,
25799 DRAW_NORMAL_TEXT, 0);
25801 /* Advance the output cursor. */
25802 w->output_cursor.hpos += len;
25803 w->output_cursor.x += shift_by_width;
25804 unblock_input ();
25808 /* EXPORT for RIF:
25809 Erase the current text line from the nominal cursor position
25810 (inclusive) to pixel column TO_X (exclusive). The idea is that
25811 everything from TO_X onward is already erased.
25813 TO_X is a pixel position relative to UPDATED_AREA of currently
25814 updated window W. TO_X == -1 means clear to the end of this area. */
25816 void
25817 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25818 enum glyph_row_area updated_area, int to_x)
25820 struct frame *f;
25821 int max_x, min_y, max_y;
25822 int from_x, from_y, to_y;
25824 eassert (updated_row);
25825 f = XFRAME (w->frame);
25827 if (updated_row->full_width_p)
25828 max_x = WINDOW_TOTAL_WIDTH (w);
25829 else
25830 max_x = window_box_width (w, updated_area);
25831 max_y = window_text_bottom_y (w);
25833 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25834 of window. For TO_X > 0, truncate to end of drawing area. */
25835 if (to_x == 0)
25836 return;
25837 else if (to_x < 0)
25838 to_x = max_x;
25839 else
25840 to_x = min (to_x, max_x);
25842 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25844 /* Notice if the cursor will be cleared by this operation. */
25845 if (!updated_row->full_width_p)
25846 notice_overwritten_cursor (w, updated_area,
25847 w->output_cursor.x, -1,
25848 updated_row->y,
25849 MATRIX_ROW_BOTTOM_Y (updated_row));
25851 from_x = w->output_cursor.x;
25853 /* Translate to frame coordinates. */
25854 if (updated_row->full_width_p)
25856 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25857 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25859 else
25861 int area_left = window_box_left (w, updated_area);
25862 from_x += area_left;
25863 to_x += area_left;
25866 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25867 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
25868 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25870 /* Prevent inadvertently clearing to end of the X window. */
25871 if (to_x > from_x && to_y > from_y)
25873 block_input ();
25874 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25875 to_x - from_x, to_y - from_y);
25876 unblock_input ();
25880 #endif /* HAVE_WINDOW_SYSTEM */
25884 /***********************************************************************
25885 Cursor types
25886 ***********************************************************************/
25888 /* Value is the internal representation of the specified cursor type
25889 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25890 of the bar cursor. */
25892 static enum text_cursor_kinds
25893 get_specified_cursor_type (Lisp_Object arg, int *width)
25895 enum text_cursor_kinds type;
25897 if (NILP (arg))
25898 return NO_CURSOR;
25900 if (EQ (arg, Qbox))
25901 return FILLED_BOX_CURSOR;
25903 if (EQ (arg, Qhollow))
25904 return HOLLOW_BOX_CURSOR;
25906 if (EQ (arg, Qbar))
25908 *width = 2;
25909 return BAR_CURSOR;
25912 if (CONSP (arg)
25913 && EQ (XCAR (arg), Qbar)
25914 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25916 *width = XINT (XCDR (arg));
25917 return BAR_CURSOR;
25920 if (EQ (arg, Qhbar))
25922 *width = 2;
25923 return HBAR_CURSOR;
25926 if (CONSP (arg)
25927 && EQ (XCAR (arg), Qhbar)
25928 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25930 *width = XINT (XCDR (arg));
25931 return HBAR_CURSOR;
25934 /* Treat anything unknown as "hollow box cursor".
25935 It was bad to signal an error; people have trouble fixing
25936 .Xdefaults with Emacs, when it has something bad in it. */
25937 type = HOLLOW_BOX_CURSOR;
25939 return type;
25942 /* Set the default cursor types for specified frame. */
25943 void
25944 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25946 int width = 1;
25947 Lisp_Object tem;
25949 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25950 FRAME_CURSOR_WIDTH (f) = width;
25952 /* By default, set up the blink-off state depending on the on-state. */
25954 tem = Fassoc (arg, Vblink_cursor_alist);
25955 if (!NILP (tem))
25957 FRAME_BLINK_OFF_CURSOR (f)
25958 = get_specified_cursor_type (XCDR (tem), &width);
25959 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25961 else
25962 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25964 /* Make sure the cursor gets redrawn. */
25965 f->cursor_type_changed = 1;
25969 #ifdef HAVE_WINDOW_SYSTEM
25971 /* Return the cursor we want to be displayed in window W. Return
25972 width of bar/hbar cursor through WIDTH arg. Return with
25973 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25974 (i.e. if the `system caret' should track this cursor).
25976 In a mini-buffer window, we want the cursor only to appear if we
25977 are reading input from this window. For the selected window, we
25978 want the cursor type given by the frame parameter or buffer local
25979 setting of cursor-type. If explicitly marked off, draw no cursor.
25980 In all other cases, we want a hollow box cursor. */
25982 static enum text_cursor_kinds
25983 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25984 int *active_cursor)
25986 struct frame *f = XFRAME (w->frame);
25987 struct buffer *b = XBUFFER (w->contents);
25988 int cursor_type = DEFAULT_CURSOR;
25989 Lisp_Object alt_cursor;
25990 int non_selected = 0;
25992 *active_cursor = 1;
25994 /* Echo area */
25995 if (cursor_in_echo_area
25996 && FRAME_HAS_MINIBUF_P (f)
25997 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25999 if (w == XWINDOW (echo_area_window))
26001 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26003 *width = FRAME_CURSOR_WIDTH (f);
26004 return FRAME_DESIRED_CURSOR (f);
26006 else
26007 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26010 *active_cursor = 0;
26011 non_selected = 1;
26014 /* Detect a nonselected window or nonselected frame. */
26015 else if (w != XWINDOW (f->selected_window)
26016 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26018 *active_cursor = 0;
26020 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26021 return NO_CURSOR;
26023 non_selected = 1;
26026 /* Never display a cursor in a window in which cursor-type is nil. */
26027 if (NILP (BVAR (b, cursor_type)))
26028 return NO_CURSOR;
26030 /* Get the normal cursor type for this window. */
26031 if (EQ (BVAR (b, cursor_type), Qt))
26033 cursor_type = FRAME_DESIRED_CURSOR (f);
26034 *width = FRAME_CURSOR_WIDTH (f);
26036 else
26037 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26039 /* Use cursor-in-non-selected-windows instead
26040 for non-selected window or frame. */
26041 if (non_selected)
26043 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26044 if (!EQ (Qt, alt_cursor))
26045 return get_specified_cursor_type (alt_cursor, width);
26046 /* t means modify the normal cursor type. */
26047 if (cursor_type == FILLED_BOX_CURSOR)
26048 cursor_type = HOLLOW_BOX_CURSOR;
26049 else if (cursor_type == BAR_CURSOR && *width > 1)
26050 --*width;
26051 return cursor_type;
26054 /* Use normal cursor if not blinked off. */
26055 if (!w->cursor_off_p)
26057 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26059 if (cursor_type == FILLED_BOX_CURSOR)
26061 /* Using a block cursor on large images can be very annoying.
26062 So use a hollow cursor for "large" images.
26063 If image is not transparent (no mask), also use hollow cursor. */
26064 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26065 if (img != NULL && IMAGEP (img->spec))
26067 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26068 where N = size of default frame font size.
26069 This should cover most of the "tiny" icons people may use. */
26070 if (!img->mask
26071 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26072 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26073 cursor_type = HOLLOW_BOX_CURSOR;
26076 else if (cursor_type != NO_CURSOR)
26078 /* Display current only supports BOX and HOLLOW cursors for images.
26079 So for now, unconditionally use a HOLLOW cursor when cursor is
26080 not a solid box cursor. */
26081 cursor_type = HOLLOW_BOX_CURSOR;
26084 return cursor_type;
26087 /* Cursor is blinked off, so determine how to "toggle" it. */
26089 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26090 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26091 return get_specified_cursor_type (XCDR (alt_cursor), width);
26093 /* Then see if frame has specified a specific blink off cursor type. */
26094 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26096 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26097 return FRAME_BLINK_OFF_CURSOR (f);
26100 #if 0
26101 /* Some people liked having a permanently visible blinking cursor,
26102 while others had very strong opinions against it. So it was
26103 decided to remove it. KFS 2003-09-03 */
26105 /* Finally perform built-in cursor blinking:
26106 filled box <-> hollow box
26107 wide [h]bar <-> narrow [h]bar
26108 narrow [h]bar <-> no cursor
26109 other type <-> no cursor */
26111 if (cursor_type == FILLED_BOX_CURSOR)
26112 return HOLLOW_BOX_CURSOR;
26114 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26116 *width = 1;
26117 return cursor_type;
26119 #endif
26121 return NO_CURSOR;
26125 /* Notice when the text cursor of window W has been completely
26126 overwritten by a drawing operation that outputs glyphs in AREA
26127 starting at X0 and ending at X1 in the line starting at Y0 and
26128 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26129 the rest of the line after X0 has been written. Y coordinates
26130 are window-relative. */
26132 static void
26133 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26134 int x0, int x1, int y0, int y1)
26136 int cx0, cx1, cy0, cy1;
26137 struct glyph_row *row;
26139 if (!w->phys_cursor_on_p)
26140 return;
26141 if (area != TEXT_AREA)
26142 return;
26144 if (w->phys_cursor.vpos < 0
26145 || w->phys_cursor.vpos >= w->current_matrix->nrows
26146 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26147 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26148 return;
26150 if (row->cursor_in_fringe_p)
26152 row->cursor_in_fringe_p = 0;
26153 draw_fringe_bitmap (w, row, row->reversed_p);
26154 w->phys_cursor_on_p = 0;
26155 return;
26158 cx0 = w->phys_cursor.x;
26159 cx1 = cx0 + w->phys_cursor_width;
26160 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26161 return;
26163 /* The cursor image will be completely removed from the
26164 screen if the output area intersects the cursor area in
26165 y-direction. When we draw in [y0 y1[, and some part of
26166 the cursor is at y < y0, that part must have been drawn
26167 before. When scrolling, the cursor is erased before
26168 actually scrolling, so we don't come here. When not
26169 scrolling, the rows above the old cursor row must have
26170 changed, and in this case these rows must have written
26171 over the cursor image.
26173 Likewise if part of the cursor is below y1, with the
26174 exception of the cursor being in the first blank row at
26175 the buffer and window end because update_text_area
26176 doesn't draw that row. (Except when it does, but
26177 that's handled in update_text_area.) */
26179 cy0 = w->phys_cursor.y;
26180 cy1 = cy0 + w->phys_cursor_height;
26181 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26182 return;
26184 w->phys_cursor_on_p = 0;
26187 #endif /* HAVE_WINDOW_SYSTEM */
26190 /************************************************************************
26191 Mouse Face
26192 ************************************************************************/
26194 #ifdef HAVE_WINDOW_SYSTEM
26196 /* EXPORT for RIF:
26197 Fix the display of area AREA of overlapping row ROW in window W
26198 with respect to the overlapping part OVERLAPS. */
26200 void
26201 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26202 enum glyph_row_area area, int overlaps)
26204 int i, x;
26206 block_input ();
26208 x = 0;
26209 for (i = 0; i < row->used[area];)
26211 if (row->glyphs[area][i].overlaps_vertically_p)
26213 int start = i, start_x = x;
26217 x += row->glyphs[area][i].pixel_width;
26218 ++i;
26220 while (i < row->used[area]
26221 && row->glyphs[area][i].overlaps_vertically_p);
26223 draw_glyphs (w, start_x, row, area,
26224 start, i,
26225 DRAW_NORMAL_TEXT, overlaps);
26227 else
26229 x += row->glyphs[area][i].pixel_width;
26230 ++i;
26234 unblock_input ();
26238 /* EXPORT:
26239 Draw the cursor glyph of window W in glyph row ROW. See the
26240 comment of draw_glyphs for the meaning of HL. */
26242 void
26243 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26244 enum draw_glyphs_face hl)
26246 /* If cursor hpos is out of bounds, don't draw garbage. This can
26247 happen in mini-buffer windows when switching between echo area
26248 glyphs and mini-buffer. */
26249 if ((row->reversed_p
26250 ? (w->phys_cursor.hpos >= 0)
26251 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26253 int on_p = w->phys_cursor_on_p;
26254 int x1;
26255 int hpos = w->phys_cursor.hpos;
26257 /* When the window is hscrolled, cursor hpos can legitimately be
26258 out of bounds, but we draw the cursor at the corresponding
26259 window margin in that case. */
26260 if (!row->reversed_p && hpos < 0)
26261 hpos = 0;
26262 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26263 hpos = row->used[TEXT_AREA] - 1;
26265 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26266 hl, 0);
26267 w->phys_cursor_on_p = on_p;
26269 if (hl == DRAW_CURSOR)
26270 w->phys_cursor_width = x1 - w->phys_cursor.x;
26271 /* When we erase the cursor, and ROW is overlapped by other
26272 rows, make sure that these overlapping parts of other rows
26273 are redrawn. */
26274 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26276 w->phys_cursor_width = x1 - w->phys_cursor.x;
26278 if (row > w->current_matrix->rows
26279 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26280 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26281 OVERLAPS_ERASED_CURSOR);
26283 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26284 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26285 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26286 OVERLAPS_ERASED_CURSOR);
26292 /* EXPORT:
26293 Erase the image of a cursor of window W from the screen. */
26295 void
26296 erase_phys_cursor (struct window *w)
26298 struct frame *f = XFRAME (w->frame);
26299 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26300 int hpos = w->phys_cursor.hpos;
26301 int vpos = w->phys_cursor.vpos;
26302 int mouse_face_here_p = 0;
26303 struct glyph_matrix *active_glyphs = w->current_matrix;
26304 struct glyph_row *cursor_row;
26305 struct glyph *cursor_glyph;
26306 enum draw_glyphs_face hl;
26308 /* No cursor displayed or row invalidated => nothing to do on the
26309 screen. */
26310 if (w->phys_cursor_type == NO_CURSOR)
26311 goto mark_cursor_off;
26313 /* VPOS >= active_glyphs->nrows means that window has been resized.
26314 Don't bother to erase the cursor. */
26315 if (vpos >= active_glyphs->nrows)
26316 goto mark_cursor_off;
26318 /* If row containing cursor is marked invalid, there is nothing we
26319 can do. */
26320 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26321 if (!cursor_row->enabled_p)
26322 goto mark_cursor_off;
26324 /* If line spacing is > 0, old cursor may only be partially visible in
26325 window after split-window. So adjust visible height. */
26326 cursor_row->visible_height = min (cursor_row->visible_height,
26327 window_text_bottom_y (w) - cursor_row->y);
26329 /* If row is completely invisible, don't attempt to delete a cursor which
26330 isn't there. This can happen if cursor is at top of a window, and
26331 we switch to a buffer with a header line in that window. */
26332 if (cursor_row->visible_height <= 0)
26333 goto mark_cursor_off;
26335 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26336 if (cursor_row->cursor_in_fringe_p)
26338 cursor_row->cursor_in_fringe_p = 0;
26339 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26340 goto mark_cursor_off;
26343 /* This can happen when the new row is shorter than the old one.
26344 In this case, either draw_glyphs or clear_end_of_line
26345 should have cleared the cursor. Note that we wouldn't be
26346 able to erase the cursor in this case because we don't have a
26347 cursor glyph at hand. */
26348 if ((cursor_row->reversed_p
26349 ? (w->phys_cursor.hpos < 0)
26350 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26351 goto mark_cursor_off;
26353 /* When the window is hscrolled, cursor hpos can legitimately be out
26354 of bounds, but we draw the cursor at the corresponding window
26355 margin in that case. */
26356 if (!cursor_row->reversed_p && hpos < 0)
26357 hpos = 0;
26358 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26359 hpos = cursor_row->used[TEXT_AREA] - 1;
26361 /* If the cursor is in the mouse face area, redisplay that when
26362 we clear the cursor. */
26363 if (! NILP (hlinfo->mouse_face_window)
26364 && coords_in_mouse_face_p (w, hpos, vpos)
26365 /* Don't redraw the cursor's spot in mouse face if it is at the
26366 end of a line (on a newline). The cursor appears there, but
26367 mouse highlighting does not. */
26368 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26369 mouse_face_here_p = 1;
26371 /* Maybe clear the display under the cursor. */
26372 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26374 int x, y, left_x;
26375 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26376 int width;
26378 cursor_glyph = get_phys_cursor_glyph (w);
26379 if (cursor_glyph == NULL)
26380 goto mark_cursor_off;
26382 width = cursor_glyph->pixel_width;
26383 left_x = window_box_left_offset (w, TEXT_AREA);
26384 x = w->phys_cursor.x;
26385 if (x < left_x)
26386 width -= left_x - x;
26387 width = min (width, window_box_width (w, TEXT_AREA) - x);
26388 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26389 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26391 if (width > 0)
26392 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26395 /* Erase the cursor by redrawing the character underneath it. */
26396 if (mouse_face_here_p)
26397 hl = DRAW_MOUSE_FACE;
26398 else
26399 hl = DRAW_NORMAL_TEXT;
26400 draw_phys_cursor_glyph (w, cursor_row, hl);
26402 mark_cursor_off:
26403 w->phys_cursor_on_p = 0;
26404 w->phys_cursor_type = NO_CURSOR;
26408 /* EXPORT:
26409 Display or clear cursor of window W. If ON is zero, clear the
26410 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26411 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26413 void
26414 display_and_set_cursor (struct window *w, bool on,
26415 int hpos, int vpos, int x, int y)
26417 struct frame *f = XFRAME (w->frame);
26418 int new_cursor_type;
26419 int new_cursor_width;
26420 int active_cursor;
26421 struct glyph_row *glyph_row;
26422 struct glyph *glyph;
26424 /* This is pointless on invisible frames, and dangerous on garbaged
26425 windows and frames; in the latter case, the frame or window may
26426 be in the midst of changing its size, and x and y may be off the
26427 window. */
26428 if (! FRAME_VISIBLE_P (f)
26429 || FRAME_GARBAGED_P (f)
26430 || vpos >= w->current_matrix->nrows
26431 || hpos >= w->current_matrix->matrix_w)
26432 return;
26434 /* If cursor is off and we want it off, return quickly. */
26435 if (!on && !w->phys_cursor_on_p)
26436 return;
26438 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26439 /* If cursor row is not enabled, we don't really know where to
26440 display the cursor. */
26441 if (!glyph_row->enabled_p)
26443 w->phys_cursor_on_p = 0;
26444 return;
26447 glyph = NULL;
26448 if (!glyph_row->exact_window_width_line_p
26449 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26450 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26452 eassert (input_blocked_p ());
26454 /* Set new_cursor_type to the cursor we want to be displayed. */
26455 new_cursor_type = get_window_cursor_type (w, glyph,
26456 &new_cursor_width, &active_cursor);
26458 /* If cursor is currently being shown and we don't want it to be or
26459 it is in the wrong place, or the cursor type is not what we want,
26460 erase it. */
26461 if (w->phys_cursor_on_p
26462 && (!on
26463 || w->phys_cursor.x != x
26464 || w->phys_cursor.y != y
26465 || new_cursor_type != w->phys_cursor_type
26466 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26467 && new_cursor_width != w->phys_cursor_width)))
26468 erase_phys_cursor (w);
26470 /* Don't check phys_cursor_on_p here because that flag is only set
26471 to zero in some cases where we know that the cursor has been
26472 completely erased, to avoid the extra work of erasing the cursor
26473 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26474 still not be visible, or it has only been partly erased. */
26475 if (on)
26477 w->phys_cursor_ascent = glyph_row->ascent;
26478 w->phys_cursor_height = glyph_row->height;
26480 /* Set phys_cursor_.* before x_draw_.* is called because some
26481 of them may need the information. */
26482 w->phys_cursor.x = x;
26483 w->phys_cursor.y = glyph_row->y;
26484 w->phys_cursor.hpos = hpos;
26485 w->phys_cursor.vpos = vpos;
26488 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26489 new_cursor_type, new_cursor_width,
26490 on, active_cursor);
26494 /* Switch the display of W's cursor on or off, according to the value
26495 of ON. */
26497 static void
26498 update_window_cursor (struct window *w, bool on)
26500 /* Don't update cursor in windows whose frame is in the process
26501 of being deleted. */
26502 if (w->current_matrix)
26504 int hpos = w->phys_cursor.hpos;
26505 int vpos = w->phys_cursor.vpos;
26506 struct glyph_row *row;
26508 if (vpos >= w->current_matrix->nrows
26509 || hpos >= w->current_matrix->matrix_w)
26510 return;
26512 row = MATRIX_ROW (w->current_matrix, vpos);
26514 /* When the window is hscrolled, cursor hpos can legitimately be
26515 out of bounds, but we draw the cursor at the corresponding
26516 window margin in that case. */
26517 if (!row->reversed_p && hpos < 0)
26518 hpos = 0;
26519 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26520 hpos = row->used[TEXT_AREA] - 1;
26522 block_input ();
26523 display_and_set_cursor (w, on, hpos, vpos,
26524 w->phys_cursor.x, w->phys_cursor.y);
26525 unblock_input ();
26530 /* Call update_window_cursor with parameter ON_P on all leaf windows
26531 in the window tree rooted at W. */
26533 static void
26534 update_cursor_in_window_tree (struct window *w, bool on_p)
26536 while (w)
26538 if (WINDOWP (w->contents))
26539 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26540 else
26541 update_window_cursor (w, on_p);
26543 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26548 /* EXPORT:
26549 Display the cursor on window W, or clear it, according to ON_P.
26550 Don't change the cursor's position. */
26552 void
26553 x_update_cursor (struct frame *f, bool on_p)
26555 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26559 /* EXPORT:
26560 Clear the cursor of window W to background color, and mark the
26561 cursor as not shown. This is used when the text where the cursor
26562 is about to be rewritten. */
26564 void
26565 x_clear_cursor (struct window *w)
26567 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26568 update_window_cursor (w, 0);
26571 #endif /* HAVE_WINDOW_SYSTEM */
26573 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26574 and MSDOS. */
26575 static void
26576 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26577 int start_hpos, int end_hpos,
26578 enum draw_glyphs_face draw)
26580 #ifdef HAVE_WINDOW_SYSTEM
26581 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26583 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26584 return;
26586 #endif
26587 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26588 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26589 #endif
26592 /* Display the active region described by mouse_face_* according to DRAW. */
26594 static void
26595 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26597 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26598 struct frame *f = XFRAME (WINDOW_FRAME (w));
26600 if (/* If window is in the process of being destroyed, don't bother
26601 to do anything. */
26602 w->current_matrix != NULL
26603 /* Don't update mouse highlight if hidden */
26604 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26605 /* Recognize when we are called to operate on rows that don't exist
26606 anymore. This can happen when a window is split. */
26607 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26609 int phys_cursor_on_p = w->phys_cursor_on_p;
26610 struct glyph_row *row, *first, *last;
26612 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26613 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26615 for (row = first; row <= last && row->enabled_p; ++row)
26617 int start_hpos, end_hpos, start_x;
26619 /* For all but the first row, the highlight starts at column 0. */
26620 if (row == first)
26622 /* R2L rows have BEG and END in reversed order, but the
26623 screen drawing geometry is always left to right. So
26624 we need to mirror the beginning and end of the
26625 highlighted area in R2L rows. */
26626 if (!row->reversed_p)
26628 start_hpos = hlinfo->mouse_face_beg_col;
26629 start_x = hlinfo->mouse_face_beg_x;
26631 else if (row == last)
26633 start_hpos = hlinfo->mouse_face_end_col;
26634 start_x = hlinfo->mouse_face_end_x;
26636 else
26638 start_hpos = 0;
26639 start_x = 0;
26642 else if (row->reversed_p && row == last)
26644 start_hpos = hlinfo->mouse_face_end_col;
26645 start_x = hlinfo->mouse_face_end_x;
26647 else
26649 start_hpos = 0;
26650 start_x = 0;
26653 if (row == last)
26655 if (!row->reversed_p)
26656 end_hpos = hlinfo->mouse_face_end_col;
26657 else if (row == first)
26658 end_hpos = hlinfo->mouse_face_beg_col;
26659 else
26661 end_hpos = row->used[TEXT_AREA];
26662 if (draw == DRAW_NORMAL_TEXT)
26663 row->fill_line_p = 1; /* Clear to end of line */
26666 else if (row->reversed_p && row == first)
26667 end_hpos = hlinfo->mouse_face_beg_col;
26668 else
26670 end_hpos = row->used[TEXT_AREA];
26671 if (draw == DRAW_NORMAL_TEXT)
26672 row->fill_line_p = 1; /* Clear to end of line */
26675 if (end_hpos > start_hpos)
26677 draw_row_with_mouse_face (w, start_x, row,
26678 start_hpos, end_hpos, draw);
26680 row->mouse_face_p
26681 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26685 #ifdef HAVE_WINDOW_SYSTEM
26686 /* When we've written over the cursor, arrange for it to
26687 be displayed again. */
26688 if (FRAME_WINDOW_P (f)
26689 && phys_cursor_on_p && !w->phys_cursor_on_p)
26691 int hpos = w->phys_cursor.hpos;
26693 /* When the window is hscrolled, cursor hpos can legitimately be
26694 out of bounds, but we draw the cursor at the corresponding
26695 window margin in that case. */
26696 if (!row->reversed_p && hpos < 0)
26697 hpos = 0;
26698 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26699 hpos = row->used[TEXT_AREA] - 1;
26701 block_input ();
26702 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26703 w->phys_cursor.x, w->phys_cursor.y);
26704 unblock_input ();
26706 #endif /* HAVE_WINDOW_SYSTEM */
26709 #ifdef HAVE_WINDOW_SYSTEM
26710 /* Change the mouse cursor. */
26711 if (FRAME_WINDOW_P (f))
26713 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
26714 if (draw == DRAW_NORMAL_TEXT
26715 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26716 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26717 else
26718 #endif
26719 if (draw == DRAW_MOUSE_FACE)
26720 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26721 else
26722 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26724 #endif /* HAVE_WINDOW_SYSTEM */
26727 /* EXPORT:
26728 Clear out the mouse-highlighted active region.
26729 Redraw it un-highlighted first. Value is non-zero if mouse
26730 face was actually drawn unhighlighted. */
26733 clear_mouse_face (Mouse_HLInfo *hlinfo)
26735 int cleared = 0;
26737 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26739 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26740 cleared = 1;
26743 reset_mouse_highlight (hlinfo);
26744 return cleared;
26747 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26748 within the mouse face on that window. */
26749 static int
26750 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26752 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26754 /* Quickly resolve the easy cases. */
26755 if (!(WINDOWP (hlinfo->mouse_face_window)
26756 && XWINDOW (hlinfo->mouse_face_window) == w))
26757 return 0;
26758 if (vpos < hlinfo->mouse_face_beg_row
26759 || vpos > hlinfo->mouse_face_end_row)
26760 return 0;
26761 if (vpos > hlinfo->mouse_face_beg_row
26762 && vpos < hlinfo->mouse_face_end_row)
26763 return 1;
26765 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26767 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26769 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26770 return 1;
26772 else if ((vpos == hlinfo->mouse_face_beg_row
26773 && hpos >= hlinfo->mouse_face_beg_col)
26774 || (vpos == hlinfo->mouse_face_end_row
26775 && hpos < hlinfo->mouse_face_end_col))
26776 return 1;
26778 else
26780 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26782 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26783 return 1;
26785 else if ((vpos == hlinfo->mouse_face_beg_row
26786 && hpos <= hlinfo->mouse_face_beg_col)
26787 || (vpos == hlinfo->mouse_face_end_row
26788 && hpos > hlinfo->mouse_face_end_col))
26789 return 1;
26791 return 0;
26795 /* EXPORT:
26796 Non-zero if physical cursor of window W is within mouse face. */
26799 cursor_in_mouse_face_p (struct window *w)
26801 int hpos = w->phys_cursor.hpos;
26802 int vpos = w->phys_cursor.vpos;
26803 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26805 /* When the window is hscrolled, cursor hpos can legitimately be out
26806 of bounds, but we draw the cursor at the corresponding window
26807 margin in that case. */
26808 if (!row->reversed_p && hpos < 0)
26809 hpos = 0;
26810 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26811 hpos = row->used[TEXT_AREA] - 1;
26813 return coords_in_mouse_face_p (w, hpos, vpos);
26818 /* Find the glyph rows START_ROW and END_ROW of window W that display
26819 characters between buffer positions START_CHARPOS and END_CHARPOS
26820 (excluding END_CHARPOS). DISP_STRING is a display string that
26821 covers these buffer positions. This is similar to
26822 row_containing_pos, but is more accurate when bidi reordering makes
26823 buffer positions change non-linearly with glyph rows. */
26824 static void
26825 rows_from_pos_range (struct window *w,
26826 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26827 Lisp_Object disp_string,
26828 struct glyph_row **start, struct glyph_row **end)
26830 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26831 int last_y = window_text_bottom_y (w);
26832 struct glyph_row *row;
26834 *start = NULL;
26835 *end = NULL;
26837 while (!first->enabled_p
26838 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26839 first++;
26841 /* Find the START row. */
26842 for (row = first;
26843 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26844 row++)
26846 /* A row can potentially be the START row if the range of the
26847 characters it displays intersects the range
26848 [START_CHARPOS..END_CHARPOS). */
26849 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26850 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26851 /* See the commentary in row_containing_pos, for the
26852 explanation of the complicated way to check whether
26853 some position is beyond the end of the characters
26854 displayed by a row. */
26855 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26856 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26857 && !row->ends_at_zv_p
26858 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26859 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26860 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26861 && !row->ends_at_zv_p
26862 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26864 /* Found a candidate row. Now make sure at least one of the
26865 glyphs it displays has a charpos from the range
26866 [START_CHARPOS..END_CHARPOS).
26868 This is not obvious because bidi reordering could make
26869 buffer positions of a row be 1,2,3,102,101,100, and if we
26870 want to highlight characters in [50..60), we don't want
26871 this row, even though [50..60) does intersect [1..103),
26872 the range of character positions given by the row's start
26873 and end positions. */
26874 struct glyph *g = row->glyphs[TEXT_AREA];
26875 struct glyph *e = g + row->used[TEXT_AREA];
26877 while (g < e)
26879 if (((BUFFERP (g->object) || INTEGERP (g->object))
26880 && start_charpos <= g->charpos && g->charpos < end_charpos)
26881 /* A glyph that comes from DISP_STRING is by
26882 definition to be highlighted. */
26883 || EQ (g->object, disp_string))
26884 *start = row;
26885 g++;
26887 if (*start)
26888 break;
26892 /* Find the END row. */
26893 if (!*start
26894 /* If the last row is partially visible, start looking for END
26895 from that row, instead of starting from FIRST. */
26896 && !(row->enabled_p
26897 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26898 row = first;
26899 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26901 struct glyph_row *next = row + 1;
26902 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26904 if (!next->enabled_p
26905 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26906 /* The first row >= START whose range of displayed characters
26907 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26908 is the row END + 1. */
26909 || (start_charpos < next_start
26910 && end_charpos < next_start)
26911 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26912 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26913 && !next->ends_at_zv_p
26914 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26915 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26916 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26917 && !next->ends_at_zv_p
26918 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26920 *end = row;
26921 break;
26923 else
26925 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26926 but none of the characters it displays are in the range, it is
26927 also END + 1. */
26928 struct glyph *g = next->glyphs[TEXT_AREA];
26929 struct glyph *s = g;
26930 struct glyph *e = g + next->used[TEXT_AREA];
26932 while (g < e)
26934 if (((BUFFERP (g->object) || INTEGERP (g->object))
26935 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26936 /* If the buffer position of the first glyph in
26937 the row is equal to END_CHARPOS, it means
26938 the last character to be highlighted is the
26939 newline of ROW, and we must consider NEXT as
26940 END, not END+1. */
26941 || (((!next->reversed_p && g == s)
26942 || (next->reversed_p && g == e - 1))
26943 && (g->charpos == end_charpos
26944 /* Special case for when NEXT is an
26945 empty line at ZV. */
26946 || (g->charpos == -1
26947 && !row->ends_at_zv_p
26948 && next_start == end_charpos)))))
26949 /* A glyph that comes from DISP_STRING is by
26950 definition to be highlighted. */
26951 || EQ (g->object, disp_string))
26952 break;
26953 g++;
26955 if (g == e)
26957 *end = row;
26958 break;
26960 /* The first row that ends at ZV must be the last to be
26961 highlighted. */
26962 else if (next->ends_at_zv_p)
26964 *end = next;
26965 break;
26971 /* This function sets the mouse_face_* elements of HLINFO, assuming
26972 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26973 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26974 for the overlay or run of text properties specifying the mouse
26975 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26976 before-string and after-string that must also be highlighted.
26977 DISP_STRING, if non-nil, is a display string that may cover some
26978 or all of the highlighted text. */
26980 static void
26981 mouse_face_from_buffer_pos (Lisp_Object window,
26982 Mouse_HLInfo *hlinfo,
26983 ptrdiff_t mouse_charpos,
26984 ptrdiff_t start_charpos,
26985 ptrdiff_t end_charpos,
26986 Lisp_Object before_string,
26987 Lisp_Object after_string,
26988 Lisp_Object disp_string)
26990 struct window *w = XWINDOW (window);
26991 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26992 struct glyph_row *r1, *r2;
26993 struct glyph *glyph, *end;
26994 ptrdiff_t ignore, pos;
26995 int x;
26997 eassert (NILP (disp_string) || STRINGP (disp_string));
26998 eassert (NILP (before_string) || STRINGP (before_string));
26999 eassert (NILP (after_string) || STRINGP (after_string));
27001 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27002 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27003 if (r1 == NULL)
27004 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27005 /* If the before-string or display-string contains newlines,
27006 rows_from_pos_range skips to its last row. Move back. */
27007 if (!NILP (before_string) || !NILP (disp_string))
27009 struct glyph_row *prev;
27010 while ((prev = r1 - 1, prev >= first)
27011 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27012 && prev->used[TEXT_AREA] > 0)
27014 struct glyph *beg = prev->glyphs[TEXT_AREA];
27015 glyph = beg + prev->used[TEXT_AREA];
27016 while (--glyph >= beg && INTEGERP (glyph->object));
27017 if (glyph < beg
27018 || !(EQ (glyph->object, before_string)
27019 || EQ (glyph->object, disp_string)))
27020 break;
27021 r1 = prev;
27024 if (r2 == NULL)
27026 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27027 hlinfo->mouse_face_past_end = 1;
27029 else if (!NILP (after_string))
27031 /* If the after-string has newlines, advance to its last row. */
27032 struct glyph_row *next;
27033 struct glyph_row *last
27034 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27036 for (next = r2 + 1;
27037 next <= last
27038 && next->used[TEXT_AREA] > 0
27039 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27040 ++next)
27041 r2 = next;
27043 /* The rest of the display engine assumes that mouse_face_beg_row is
27044 either above mouse_face_end_row or identical to it. But with
27045 bidi-reordered continued lines, the row for START_CHARPOS could
27046 be below the row for END_CHARPOS. If so, swap the rows and store
27047 them in correct order. */
27048 if (r1->y > r2->y)
27050 struct glyph_row *tem = r2;
27052 r2 = r1;
27053 r1 = tem;
27056 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27057 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27059 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27060 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27061 could be anywhere in the row and in any order. The strategy
27062 below is to find the leftmost and the rightmost glyph that
27063 belongs to either of these 3 strings, or whose position is
27064 between START_CHARPOS and END_CHARPOS, and highlight all the
27065 glyphs between those two. This may cover more than just the text
27066 between START_CHARPOS and END_CHARPOS if the range of characters
27067 strides the bidi level boundary, e.g. if the beginning is in R2L
27068 text while the end is in L2R text or vice versa. */
27069 if (!r1->reversed_p)
27071 /* This row is in a left to right paragraph. Scan it left to
27072 right. */
27073 glyph = r1->glyphs[TEXT_AREA];
27074 end = glyph + r1->used[TEXT_AREA];
27075 x = r1->x;
27077 /* Skip truncation glyphs at the start of the glyph row. */
27078 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27079 for (; glyph < end
27080 && INTEGERP (glyph->object)
27081 && glyph->charpos < 0;
27082 ++glyph)
27083 x += glyph->pixel_width;
27085 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27086 or DISP_STRING, and the first glyph from buffer whose
27087 position is between START_CHARPOS and END_CHARPOS. */
27088 for (; glyph < end
27089 && !INTEGERP (glyph->object)
27090 && !EQ (glyph->object, disp_string)
27091 && !(BUFFERP (glyph->object)
27092 && (glyph->charpos >= start_charpos
27093 && glyph->charpos < end_charpos));
27094 ++glyph)
27096 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27097 are present at buffer positions between START_CHARPOS and
27098 END_CHARPOS, or if they come from an overlay. */
27099 if (EQ (glyph->object, before_string))
27101 pos = string_buffer_position (before_string,
27102 start_charpos);
27103 /* If pos == 0, it means before_string came from an
27104 overlay, not from a buffer position. */
27105 if (!pos || (pos >= start_charpos && pos < end_charpos))
27106 break;
27108 else if (EQ (glyph->object, after_string))
27110 pos = string_buffer_position (after_string, end_charpos);
27111 if (!pos || (pos >= start_charpos && pos < end_charpos))
27112 break;
27114 x += glyph->pixel_width;
27116 hlinfo->mouse_face_beg_x = x;
27117 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27119 else
27121 /* This row is in a right to left paragraph. Scan it right to
27122 left. */
27123 struct glyph *g;
27125 end = r1->glyphs[TEXT_AREA] - 1;
27126 glyph = end + r1->used[TEXT_AREA];
27128 /* Skip truncation glyphs at the start of the glyph row. */
27129 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27130 for (; glyph > end
27131 && INTEGERP (glyph->object)
27132 && glyph->charpos < 0;
27133 --glyph)
27136 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27137 or DISP_STRING, and the first glyph from buffer whose
27138 position is between START_CHARPOS and END_CHARPOS. */
27139 for (; glyph > end
27140 && !INTEGERP (glyph->object)
27141 && !EQ (glyph->object, disp_string)
27142 && !(BUFFERP (glyph->object)
27143 && (glyph->charpos >= start_charpos
27144 && glyph->charpos < end_charpos));
27145 --glyph)
27147 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27148 are present at buffer positions between START_CHARPOS and
27149 END_CHARPOS, or if they come from an overlay. */
27150 if (EQ (glyph->object, before_string))
27152 pos = string_buffer_position (before_string, start_charpos);
27153 /* If pos == 0, it means before_string came from an
27154 overlay, not from a buffer position. */
27155 if (!pos || (pos >= start_charpos && pos < end_charpos))
27156 break;
27158 else if (EQ (glyph->object, after_string))
27160 pos = string_buffer_position (after_string, end_charpos);
27161 if (!pos || (pos >= start_charpos && pos < end_charpos))
27162 break;
27166 glyph++; /* first glyph to the right of the highlighted area */
27167 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27168 x += g->pixel_width;
27169 hlinfo->mouse_face_beg_x = x;
27170 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27173 /* If the highlight ends in a different row, compute GLYPH and END
27174 for the end row. Otherwise, reuse the values computed above for
27175 the row where the highlight begins. */
27176 if (r2 != r1)
27178 if (!r2->reversed_p)
27180 glyph = r2->glyphs[TEXT_AREA];
27181 end = glyph + r2->used[TEXT_AREA];
27182 x = r2->x;
27184 else
27186 end = r2->glyphs[TEXT_AREA] - 1;
27187 glyph = end + r2->used[TEXT_AREA];
27191 if (!r2->reversed_p)
27193 /* Skip truncation and continuation glyphs near the end of the
27194 row, and also blanks and stretch glyphs inserted by
27195 extend_face_to_end_of_line. */
27196 while (end > glyph
27197 && INTEGERP ((end - 1)->object))
27198 --end;
27199 /* Scan the rest of the glyph row from the end, looking for the
27200 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27201 DISP_STRING, or whose position is between START_CHARPOS
27202 and END_CHARPOS */
27203 for (--end;
27204 end > glyph
27205 && !INTEGERP (end->object)
27206 && !EQ (end->object, disp_string)
27207 && !(BUFFERP (end->object)
27208 && (end->charpos >= start_charpos
27209 && end->charpos < end_charpos));
27210 --end)
27212 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27213 are present at buffer positions between START_CHARPOS and
27214 END_CHARPOS, or if they come from an overlay. */
27215 if (EQ (end->object, before_string))
27217 pos = string_buffer_position (before_string, start_charpos);
27218 if (!pos || (pos >= start_charpos && pos < end_charpos))
27219 break;
27221 else if (EQ (end->object, after_string))
27223 pos = string_buffer_position (after_string, end_charpos);
27224 if (!pos || (pos >= start_charpos && pos < end_charpos))
27225 break;
27228 /* Find the X coordinate of the last glyph to be highlighted. */
27229 for (; glyph <= end; ++glyph)
27230 x += glyph->pixel_width;
27232 hlinfo->mouse_face_end_x = x;
27233 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27235 else
27237 /* Skip truncation and continuation glyphs near the end of the
27238 row, and also blanks and stretch glyphs inserted by
27239 extend_face_to_end_of_line. */
27240 x = r2->x;
27241 end++;
27242 while (end < glyph
27243 && INTEGERP (end->object))
27245 x += end->pixel_width;
27246 ++end;
27248 /* Scan the rest of the glyph row from the end, looking for the
27249 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27250 DISP_STRING, or whose position is between START_CHARPOS
27251 and END_CHARPOS */
27252 for ( ;
27253 end < glyph
27254 && !INTEGERP (end->object)
27255 && !EQ (end->object, disp_string)
27256 && !(BUFFERP (end->object)
27257 && (end->charpos >= start_charpos
27258 && end->charpos < end_charpos));
27259 ++end)
27261 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27262 are present at buffer positions between START_CHARPOS and
27263 END_CHARPOS, or if they come from an overlay. */
27264 if (EQ (end->object, before_string))
27266 pos = string_buffer_position (before_string, start_charpos);
27267 if (!pos || (pos >= start_charpos && pos < end_charpos))
27268 break;
27270 else if (EQ (end->object, after_string))
27272 pos = string_buffer_position (after_string, end_charpos);
27273 if (!pos || (pos >= start_charpos && pos < end_charpos))
27274 break;
27276 x += end->pixel_width;
27278 /* If we exited the above loop because we arrived at the last
27279 glyph of the row, and its buffer position is still not in
27280 range, it means the last character in range is the preceding
27281 newline. Bump the end column and x values to get past the
27282 last glyph. */
27283 if (end == glyph
27284 && BUFFERP (end->object)
27285 && (end->charpos < start_charpos
27286 || end->charpos >= end_charpos))
27288 x += end->pixel_width;
27289 ++end;
27291 hlinfo->mouse_face_end_x = x;
27292 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27295 hlinfo->mouse_face_window = window;
27296 hlinfo->mouse_face_face_id
27297 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27298 mouse_charpos + 1,
27299 !hlinfo->mouse_face_hidden, -1);
27300 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27303 /* The following function is not used anymore (replaced with
27304 mouse_face_from_string_pos), but I leave it here for the time
27305 being, in case someone would. */
27307 #if 0 /* not used */
27309 /* Find the position of the glyph for position POS in OBJECT in
27310 window W's current matrix, and return in *X, *Y the pixel
27311 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27313 RIGHT_P non-zero means return the position of the right edge of the
27314 glyph, RIGHT_P zero means return the left edge position.
27316 If no glyph for POS exists in the matrix, return the position of
27317 the glyph with the next smaller position that is in the matrix, if
27318 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27319 exists in the matrix, return the position of the glyph with the
27320 next larger position in OBJECT.
27322 Value is non-zero if a glyph was found. */
27324 static int
27325 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27326 int *hpos, int *vpos, int *x, int *y, int right_p)
27328 int yb = window_text_bottom_y (w);
27329 struct glyph_row *r;
27330 struct glyph *best_glyph = NULL;
27331 struct glyph_row *best_row = NULL;
27332 int best_x = 0;
27334 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27335 r->enabled_p && r->y < yb;
27336 ++r)
27338 struct glyph *g = r->glyphs[TEXT_AREA];
27339 struct glyph *e = g + r->used[TEXT_AREA];
27340 int gx;
27342 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27343 if (EQ (g->object, object))
27345 if (g->charpos == pos)
27347 best_glyph = g;
27348 best_x = gx;
27349 best_row = r;
27350 goto found;
27352 else if (best_glyph == NULL
27353 || ((eabs (g->charpos - pos)
27354 < eabs (best_glyph->charpos - pos))
27355 && (right_p
27356 ? g->charpos < pos
27357 : g->charpos > pos)))
27359 best_glyph = g;
27360 best_x = gx;
27361 best_row = r;
27366 found:
27368 if (best_glyph)
27370 *x = best_x;
27371 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27373 if (right_p)
27375 *x += best_glyph->pixel_width;
27376 ++*hpos;
27379 *y = best_row->y;
27380 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27383 return best_glyph != NULL;
27385 #endif /* not used */
27387 /* Find the positions of the first and the last glyphs in window W's
27388 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27389 (assumed to be a string), and return in HLINFO's mouse_face_*
27390 members the pixel and column/row coordinates of those glyphs. */
27392 static void
27393 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27394 Lisp_Object object,
27395 ptrdiff_t startpos, ptrdiff_t endpos)
27397 int yb = window_text_bottom_y (w);
27398 struct glyph_row *r;
27399 struct glyph *g, *e;
27400 int gx;
27401 int found = 0;
27403 /* Find the glyph row with at least one position in the range
27404 [STARTPOS..ENDPOS], and the first glyph in that row whose
27405 position belongs to that range. */
27406 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27407 r->enabled_p && r->y < yb;
27408 ++r)
27410 if (!r->reversed_p)
27412 g = r->glyphs[TEXT_AREA];
27413 e = g + r->used[TEXT_AREA];
27414 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27415 if (EQ (g->object, object)
27416 && startpos <= g->charpos && g->charpos <= endpos)
27418 hlinfo->mouse_face_beg_row
27419 = MATRIX_ROW_VPOS (r, w->current_matrix);
27420 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27421 hlinfo->mouse_face_beg_x = gx;
27422 found = 1;
27423 break;
27426 else
27428 struct glyph *g1;
27430 e = r->glyphs[TEXT_AREA];
27431 g = e + r->used[TEXT_AREA];
27432 for ( ; g > e; --g)
27433 if (EQ ((g-1)->object, object)
27434 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27436 hlinfo->mouse_face_beg_row
27437 = MATRIX_ROW_VPOS (r, w->current_matrix);
27438 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27439 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27440 gx += g1->pixel_width;
27441 hlinfo->mouse_face_beg_x = gx;
27442 found = 1;
27443 break;
27446 if (found)
27447 break;
27450 if (!found)
27451 return;
27453 /* Starting with the next row, look for the first row which does NOT
27454 include any glyphs whose positions are in the range. */
27455 for (++r; r->enabled_p && r->y < yb; ++r)
27457 g = r->glyphs[TEXT_AREA];
27458 e = g + r->used[TEXT_AREA];
27459 found = 0;
27460 for ( ; g < e; ++g)
27461 if (EQ (g->object, object)
27462 && startpos <= g->charpos && g->charpos <= endpos)
27464 found = 1;
27465 break;
27467 if (!found)
27468 break;
27471 /* The highlighted region ends on the previous row. */
27472 r--;
27474 /* Set the end row. */
27475 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27477 /* Compute and set the end column and the end column's horizontal
27478 pixel coordinate. */
27479 if (!r->reversed_p)
27481 g = r->glyphs[TEXT_AREA];
27482 e = g + r->used[TEXT_AREA];
27483 for ( ; e > g; --e)
27484 if (EQ ((e-1)->object, object)
27485 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27486 break;
27487 hlinfo->mouse_face_end_col = e - g;
27489 for (gx = r->x; g < e; ++g)
27490 gx += g->pixel_width;
27491 hlinfo->mouse_face_end_x = gx;
27493 else
27495 e = r->glyphs[TEXT_AREA];
27496 g = e + r->used[TEXT_AREA];
27497 for (gx = r->x ; e < g; ++e)
27499 if (EQ (e->object, object)
27500 && startpos <= e->charpos && e->charpos <= endpos)
27501 break;
27502 gx += e->pixel_width;
27504 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27505 hlinfo->mouse_face_end_x = gx;
27509 #ifdef HAVE_WINDOW_SYSTEM
27511 /* See if position X, Y is within a hot-spot of an image. */
27513 static int
27514 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27516 if (!CONSP (hot_spot))
27517 return 0;
27519 if (EQ (XCAR (hot_spot), Qrect))
27521 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27522 Lisp_Object rect = XCDR (hot_spot);
27523 Lisp_Object tem;
27524 if (!CONSP (rect))
27525 return 0;
27526 if (!CONSP (XCAR (rect)))
27527 return 0;
27528 if (!CONSP (XCDR (rect)))
27529 return 0;
27530 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27531 return 0;
27532 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27533 return 0;
27534 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27535 return 0;
27536 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27537 return 0;
27538 return 1;
27540 else if (EQ (XCAR (hot_spot), Qcircle))
27542 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27543 Lisp_Object circ = XCDR (hot_spot);
27544 Lisp_Object lr, lx0, ly0;
27545 if (CONSP (circ)
27546 && CONSP (XCAR (circ))
27547 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27548 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27549 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27551 double r = XFLOATINT (lr);
27552 double dx = XINT (lx0) - x;
27553 double dy = XINT (ly0) - y;
27554 return (dx * dx + dy * dy <= r * r);
27557 else if (EQ (XCAR (hot_spot), Qpoly))
27559 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27560 if (VECTORP (XCDR (hot_spot)))
27562 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27563 Lisp_Object *poly = v->contents;
27564 ptrdiff_t n = v->header.size;
27565 ptrdiff_t i;
27566 int inside = 0;
27567 Lisp_Object lx, ly;
27568 int x0, y0;
27570 /* Need an even number of coordinates, and at least 3 edges. */
27571 if (n < 6 || n & 1)
27572 return 0;
27574 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27575 If count is odd, we are inside polygon. Pixels on edges
27576 may or may not be included depending on actual geometry of the
27577 polygon. */
27578 if ((lx = poly[n-2], !INTEGERP (lx))
27579 || (ly = poly[n-1], !INTEGERP (lx)))
27580 return 0;
27581 x0 = XINT (lx), y0 = XINT (ly);
27582 for (i = 0; i < n; i += 2)
27584 int x1 = x0, y1 = y0;
27585 if ((lx = poly[i], !INTEGERP (lx))
27586 || (ly = poly[i+1], !INTEGERP (ly)))
27587 return 0;
27588 x0 = XINT (lx), y0 = XINT (ly);
27590 /* Does this segment cross the X line? */
27591 if (x0 >= x)
27593 if (x1 >= x)
27594 continue;
27596 else if (x1 < x)
27597 continue;
27598 if (y > y0 && y > y1)
27599 continue;
27600 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27601 inside = !inside;
27603 return inside;
27606 return 0;
27609 Lisp_Object
27610 find_hot_spot (Lisp_Object map, int x, int y)
27612 while (CONSP (map))
27614 if (CONSP (XCAR (map))
27615 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27616 return XCAR (map);
27617 map = XCDR (map);
27620 return Qnil;
27623 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27624 3, 3, 0,
27625 doc: /* Lookup in image map MAP coordinates X and Y.
27626 An image map is an alist where each element has the format (AREA ID PLIST).
27627 An AREA is specified as either a rectangle, a circle, or a polygon:
27628 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27629 pixel coordinates of the upper left and bottom right corners.
27630 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27631 and the radius of the circle; r may be a float or integer.
27632 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27633 vector describes one corner in the polygon.
27634 Returns the alist element for the first matching AREA in MAP. */)
27635 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27637 if (NILP (map))
27638 return Qnil;
27640 CHECK_NUMBER (x);
27641 CHECK_NUMBER (y);
27643 return find_hot_spot (map,
27644 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27645 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27649 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27650 static void
27651 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27653 /* Do not change cursor shape while dragging mouse. */
27654 if (!NILP (do_mouse_tracking))
27655 return;
27657 if (!NILP (pointer))
27659 if (EQ (pointer, Qarrow))
27660 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27661 else if (EQ (pointer, Qhand))
27662 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27663 else if (EQ (pointer, Qtext))
27664 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27665 else if (EQ (pointer, intern ("hdrag")))
27666 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27667 #ifdef HAVE_X_WINDOWS
27668 else if (EQ (pointer, intern ("vdrag")))
27669 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27670 #endif
27671 else if (EQ (pointer, intern ("hourglass")))
27672 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27673 else if (EQ (pointer, Qmodeline))
27674 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27675 else
27676 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27679 if (cursor != No_Cursor)
27680 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27683 #endif /* HAVE_WINDOW_SYSTEM */
27685 /* Take proper action when mouse has moved to the mode or header line
27686 or marginal area AREA of window W, x-position X and y-position Y.
27687 X is relative to the start of the text display area of W, so the
27688 width of bitmap areas and scroll bars must be subtracted to get a
27689 position relative to the start of the mode line. */
27691 static void
27692 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27693 enum window_part area)
27695 struct window *w = XWINDOW (window);
27696 struct frame *f = XFRAME (w->frame);
27697 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27698 #ifdef HAVE_WINDOW_SYSTEM
27699 Display_Info *dpyinfo;
27700 #endif
27701 Cursor cursor = No_Cursor;
27702 Lisp_Object pointer = Qnil;
27703 int dx, dy, width, height;
27704 ptrdiff_t charpos;
27705 Lisp_Object string, object = Qnil;
27706 Lisp_Object pos IF_LINT (= Qnil), help;
27708 Lisp_Object mouse_face;
27709 int original_x_pixel = x;
27710 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27711 struct glyph_row *row IF_LINT (= 0);
27713 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27715 int x0;
27716 struct glyph *end;
27718 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27719 returns them in row/column units! */
27720 string = mode_line_string (w, area, &x, &y, &charpos,
27721 &object, &dx, &dy, &width, &height);
27723 row = (area == ON_MODE_LINE
27724 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27725 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27727 /* Find the glyph under the mouse pointer. */
27728 if (row->mode_line_p && row->enabled_p)
27730 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27731 end = glyph + row->used[TEXT_AREA];
27733 for (x0 = original_x_pixel;
27734 glyph < end && x0 >= glyph->pixel_width;
27735 ++glyph)
27736 x0 -= glyph->pixel_width;
27738 if (glyph >= end)
27739 glyph = NULL;
27742 else
27744 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27745 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27746 returns them in row/column units! */
27747 string = marginal_area_string (w, area, &x, &y, &charpos,
27748 &object, &dx, &dy, &width, &height);
27751 help = Qnil;
27753 #ifdef HAVE_WINDOW_SYSTEM
27754 if (IMAGEP (object))
27756 Lisp_Object image_map, hotspot;
27757 if ((image_map = Fplist_get (XCDR (object), QCmap),
27758 !NILP (image_map))
27759 && (hotspot = find_hot_spot (image_map, dx, dy),
27760 CONSP (hotspot))
27761 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27763 Lisp_Object plist;
27765 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27766 If so, we could look for mouse-enter, mouse-leave
27767 properties in PLIST (and do something...). */
27768 hotspot = XCDR (hotspot);
27769 if (CONSP (hotspot)
27770 && (plist = XCAR (hotspot), CONSP (plist)))
27772 pointer = Fplist_get (plist, Qpointer);
27773 if (NILP (pointer))
27774 pointer = Qhand;
27775 help = Fplist_get (plist, Qhelp_echo);
27776 if (!NILP (help))
27778 help_echo_string = help;
27779 XSETWINDOW (help_echo_window, w);
27780 help_echo_object = w->contents;
27781 help_echo_pos = charpos;
27785 if (NILP (pointer))
27786 pointer = Fplist_get (XCDR (object), QCpointer);
27788 #endif /* HAVE_WINDOW_SYSTEM */
27790 if (STRINGP (string))
27791 pos = make_number (charpos);
27793 /* Set the help text and mouse pointer. If the mouse is on a part
27794 of the mode line without any text (e.g. past the right edge of
27795 the mode line text), use the default help text and pointer. */
27796 if (STRINGP (string) || area == ON_MODE_LINE)
27798 /* Arrange to display the help by setting the global variables
27799 help_echo_string, help_echo_object, and help_echo_pos. */
27800 if (NILP (help))
27802 if (STRINGP (string))
27803 help = Fget_text_property (pos, Qhelp_echo, string);
27805 if (!NILP (help))
27807 help_echo_string = help;
27808 XSETWINDOW (help_echo_window, w);
27809 help_echo_object = string;
27810 help_echo_pos = charpos;
27812 else if (area == ON_MODE_LINE)
27814 Lisp_Object default_help
27815 = buffer_local_value_1 (Qmode_line_default_help_echo,
27816 w->contents);
27818 if (STRINGP (default_help))
27820 help_echo_string = default_help;
27821 XSETWINDOW (help_echo_window, w);
27822 help_echo_object = Qnil;
27823 help_echo_pos = -1;
27828 #ifdef HAVE_WINDOW_SYSTEM
27829 /* Change the mouse pointer according to what is under it. */
27830 if (FRAME_WINDOW_P (f))
27832 dpyinfo = FRAME_DISPLAY_INFO (f);
27833 if (STRINGP (string))
27835 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27837 if (NILP (pointer))
27838 pointer = Fget_text_property (pos, Qpointer, string);
27840 /* Change the mouse pointer according to what is under X/Y. */
27841 if (NILP (pointer)
27842 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27844 Lisp_Object map;
27845 map = Fget_text_property (pos, Qlocal_map, string);
27846 if (!KEYMAPP (map))
27847 map = Fget_text_property (pos, Qkeymap, string);
27848 if (!KEYMAPP (map))
27849 cursor = dpyinfo->vertical_scroll_bar_cursor;
27852 else
27853 /* Default mode-line pointer. */
27854 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27856 #endif
27859 /* Change the mouse face according to what is under X/Y. */
27860 if (STRINGP (string))
27862 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27863 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27864 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27865 && glyph)
27867 Lisp_Object b, e;
27869 struct glyph * tmp_glyph;
27871 int gpos;
27872 int gseq_length;
27873 int total_pixel_width;
27874 ptrdiff_t begpos, endpos, ignore;
27876 int vpos, hpos;
27878 b = Fprevious_single_property_change (make_number (charpos + 1),
27879 Qmouse_face, string, Qnil);
27880 if (NILP (b))
27881 begpos = 0;
27882 else
27883 begpos = XINT (b);
27885 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27886 if (NILP (e))
27887 endpos = SCHARS (string);
27888 else
27889 endpos = XINT (e);
27891 /* Calculate the glyph position GPOS of GLYPH in the
27892 displayed string, relative to the beginning of the
27893 highlighted part of the string.
27895 Note: GPOS is different from CHARPOS. CHARPOS is the
27896 position of GLYPH in the internal string object. A mode
27897 line string format has structures which are converted to
27898 a flattened string by the Emacs Lisp interpreter. The
27899 internal string is an element of those structures. The
27900 displayed string is the flattened string. */
27901 tmp_glyph = row_start_glyph;
27902 while (tmp_glyph < glyph
27903 && (!(EQ (tmp_glyph->object, glyph->object)
27904 && begpos <= tmp_glyph->charpos
27905 && tmp_glyph->charpos < endpos)))
27906 tmp_glyph++;
27907 gpos = glyph - tmp_glyph;
27909 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27910 the highlighted part of the displayed string to which
27911 GLYPH belongs. Note: GSEQ_LENGTH is different from
27912 SCHARS (STRING), because the latter returns the length of
27913 the internal string. */
27914 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27915 tmp_glyph > glyph
27916 && (!(EQ (tmp_glyph->object, glyph->object)
27917 && begpos <= tmp_glyph->charpos
27918 && tmp_glyph->charpos < endpos));
27919 tmp_glyph--)
27921 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27923 /* Calculate the total pixel width of all the glyphs between
27924 the beginning of the highlighted area and GLYPH. */
27925 total_pixel_width = 0;
27926 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27927 total_pixel_width += tmp_glyph->pixel_width;
27929 /* Pre calculation of re-rendering position. Note: X is in
27930 column units here, after the call to mode_line_string or
27931 marginal_area_string. */
27932 hpos = x - gpos;
27933 vpos = (area == ON_MODE_LINE
27934 ? (w->current_matrix)->nrows - 1
27935 : 0);
27937 /* If GLYPH's position is included in the region that is
27938 already drawn in mouse face, we have nothing to do. */
27939 if ( EQ (window, hlinfo->mouse_face_window)
27940 && (!row->reversed_p
27941 ? (hlinfo->mouse_face_beg_col <= hpos
27942 && hpos < hlinfo->mouse_face_end_col)
27943 /* In R2L rows we swap BEG and END, see below. */
27944 : (hlinfo->mouse_face_end_col <= hpos
27945 && hpos < hlinfo->mouse_face_beg_col))
27946 && hlinfo->mouse_face_beg_row == vpos )
27947 return;
27949 if (clear_mouse_face (hlinfo))
27950 cursor = No_Cursor;
27952 if (!row->reversed_p)
27954 hlinfo->mouse_face_beg_col = hpos;
27955 hlinfo->mouse_face_beg_x = original_x_pixel
27956 - (total_pixel_width + dx);
27957 hlinfo->mouse_face_end_col = hpos + gseq_length;
27958 hlinfo->mouse_face_end_x = 0;
27960 else
27962 /* In R2L rows, show_mouse_face expects BEG and END
27963 coordinates to be swapped. */
27964 hlinfo->mouse_face_end_col = hpos;
27965 hlinfo->mouse_face_end_x = original_x_pixel
27966 - (total_pixel_width + dx);
27967 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27968 hlinfo->mouse_face_beg_x = 0;
27971 hlinfo->mouse_face_beg_row = vpos;
27972 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27973 hlinfo->mouse_face_past_end = 0;
27974 hlinfo->mouse_face_window = window;
27976 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27977 charpos,
27978 0, 0, 0,
27979 &ignore,
27980 glyph->face_id,
27982 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27984 if (NILP (pointer))
27985 pointer = Qhand;
27987 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27988 clear_mouse_face (hlinfo);
27990 #ifdef HAVE_WINDOW_SYSTEM
27991 if (FRAME_WINDOW_P (f))
27992 define_frame_cursor1 (f, cursor, pointer);
27993 #endif
27997 /* EXPORT:
27998 Take proper action when the mouse has moved to position X, Y on
27999 frame F with regards to highlighting portions of display that have
28000 mouse-face properties. Also de-highlight portions of display where
28001 the mouse was before, set the mouse pointer shape as appropriate
28002 for the mouse coordinates, and activate help echo (tooltips).
28003 X and Y can be negative or out of range. */
28005 void
28006 note_mouse_highlight (struct frame *f, int x, int y)
28008 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28009 enum window_part part = ON_NOTHING;
28010 Lisp_Object window;
28011 struct window *w;
28012 Cursor cursor = No_Cursor;
28013 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28014 struct buffer *b;
28016 /* When a menu is active, don't highlight because this looks odd. */
28017 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28018 if (popup_activated ())
28019 return;
28020 #endif
28022 if (!f->glyphs_initialized_p
28023 || f->pointer_invisible)
28024 return;
28026 hlinfo->mouse_face_mouse_x = x;
28027 hlinfo->mouse_face_mouse_y = y;
28028 hlinfo->mouse_face_mouse_frame = f;
28030 if (hlinfo->mouse_face_defer)
28031 return;
28033 /* Which window is that in? */
28034 window = window_from_coordinates (f, x, y, &part, 1);
28036 /* If displaying active text in another window, clear that. */
28037 if (! EQ (window, hlinfo->mouse_face_window)
28038 /* Also clear if we move out of text area in same window. */
28039 || (!NILP (hlinfo->mouse_face_window)
28040 && !NILP (window)
28041 && part != ON_TEXT
28042 && part != ON_MODE_LINE
28043 && part != ON_HEADER_LINE))
28044 clear_mouse_face (hlinfo);
28046 /* Not on a window -> return. */
28047 if (!WINDOWP (window))
28048 return;
28050 /* Reset help_echo_string. It will get recomputed below. */
28051 help_echo_string = Qnil;
28053 /* Convert to window-relative pixel coordinates. */
28054 w = XWINDOW (window);
28055 frame_to_window_pixel_xy (w, &x, &y);
28057 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28058 /* Handle tool-bar window differently since it doesn't display a
28059 buffer. */
28060 if (EQ (window, f->tool_bar_window))
28062 note_tool_bar_highlight (f, x, y);
28063 return;
28065 #endif
28067 /* Mouse is on the mode, header line or margin? */
28068 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28069 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28071 note_mode_line_or_margin_highlight (window, x, y, part);
28072 return;
28075 #ifdef HAVE_WINDOW_SYSTEM
28076 if (part == ON_VERTICAL_BORDER)
28078 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28079 help_echo_string = build_string ("drag-mouse-1: resize");
28081 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28082 || part == ON_SCROLL_BAR)
28083 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28084 else
28085 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28086 #endif
28088 /* Are we in a window whose display is up to date?
28089 And verify the buffer's text has not changed. */
28090 b = XBUFFER (w->contents);
28091 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28093 int hpos, vpos, dx, dy, area = LAST_AREA;
28094 ptrdiff_t pos;
28095 struct glyph *glyph;
28096 Lisp_Object object;
28097 Lisp_Object mouse_face = Qnil, position;
28098 Lisp_Object *overlay_vec = NULL;
28099 ptrdiff_t i, noverlays;
28100 struct buffer *obuf;
28101 ptrdiff_t obegv, ozv;
28102 int same_region;
28104 /* Find the glyph under X/Y. */
28105 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28107 #ifdef HAVE_WINDOW_SYSTEM
28108 /* Look for :pointer property on image. */
28109 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28111 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28112 if (img != NULL && IMAGEP (img->spec))
28114 Lisp_Object image_map, hotspot;
28115 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28116 !NILP (image_map))
28117 && (hotspot = find_hot_spot (image_map,
28118 glyph->slice.img.x + dx,
28119 glyph->slice.img.y + dy),
28120 CONSP (hotspot))
28121 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28123 Lisp_Object plist;
28125 /* Could check XCAR (hotspot) to see if we enter/leave
28126 this hot-spot.
28127 If so, we could look for mouse-enter, mouse-leave
28128 properties in PLIST (and do something...). */
28129 hotspot = XCDR (hotspot);
28130 if (CONSP (hotspot)
28131 && (plist = XCAR (hotspot), CONSP (plist)))
28133 pointer = Fplist_get (plist, Qpointer);
28134 if (NILP (pointer))
28135 pointer = Qhand;
28136 help_echo_string = Fplist_get (plist, Qhelp_echo);
28137 if (!NILP (help_echo_string))
28139 help_echo_window = window;
28140 help_echo_object = glyph->object;
28141 help_echo_pos = glyph->charpos;
28145 if (NILP (pointer))
28146 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28149 #endif /* HAVE_WINDOW_SYSTEM */
28151 /* Clear mouse face if X/Y not over text. */
28152 if (glyph == NULL
28153 || area != TEXT_AREA
28154 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28155 /* Glyph's OBJECT is an integer for glyphs inserted by the
28156 display engine for its internal purposes, like truncation
28157 and continuation glyphs and blanks beyond the end of
28158 line's text on text terminals. If we are over such a
28159 glyph, we are not over any text. */
28160 || INTEGERP (glyph->object)
28161 /* R2L rows have a stretch glyph at their front, which
28162 stands for no text, whereas L2R rows have no glyphs at
28163 all beyond the end of text. Treat such stretch glyphs
28164 like we do with NULL glyphs in L2R rows. */
28165 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28166 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28167 && glyph->type == STRETCH_GLYPH
28168 && glyph->avoid_cursor_p))
28170 if (clear_mouse_face (hlinfo))
28171 cursor = No_Cursor;
28172 #ifdef HAVE_WINDOW_SYSTEM
28173 if (FRAME_WINDOW_P (f) && NILP (pointer))
28175 if (area != TEXT_AREA)
28176 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28177 else
28178 pointer = Vvoid_text_area_pointer;
28180 #endif
28181 goto set_cursor;
28184 pos = glyph->charpos;
28185 object = glyph->object;
28186 if (!STRINGP (object) && !BUFFERP (object))
28187 goto set_cursor;
28189 /* If we get an out-of-range value, return now; avoid an error. */
28190 if (BUFFERP (object) && pos > BUF_Z (b))
28191 goto set_cursor;
28193 /* Make the window's buffer temporarily current for
28194 overlays_at and compute_char_face. */
28195 obuf = current_buffer;
28196 current_buffer = b;
28197 obegv = BEGV;
28198 ozv = ZV;
28199 BEGV = BEG;
28200 ZV = Z;
28202 /* Is this char mouse-active or does it have help-echo? */
28203 position = make_number (pos);
28205 if (BUFFERP (object))
28207 /* Put all the overlays we want in a vector in overlay_vec. */
28208 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28209 /* Sort overlays into increasing priority order. */
28210 noverlays = sort_overlays (overlay_vec, noverlays, w);
28212 else
28213 noverlays = 0;
28215 if (NILP (Vmouse_highlight))
28217 clear_mouse_face (hlinfo);
28218 goto check_help_echo;
28221 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28223 if (same_region)
28224 cursor = No_Cursor;
28226 /* Check mouse-face highlighting. */
28227 if (! same_region
28228 /* If there exists an overlay with mouse-face overlapping
28229 the one we are currently highlighting, we have to
28230 check if we enter the overlapping overlay, and then
28231 highlight only that. */
28232 || (OVERLAYP (hlinfo->mouse_face_overlay)
28233 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28235 /* Find the highest priority overlay with a mouse-face. */
28236 Lisp_Object overlay = Qnil;
28237 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28239 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28240 if (!NILP (mouse_face))
28241 overlay = overlay_vec[i];
28244 /* If we're highlighting the same overlay as before, there's
28245 no need to do that again. */
28246 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28247 goto check_help_echo;
28248 hlinfo->mouse_face_overlay = overlay;
28250 /* Clear the display of the old active region, if any. */
28251 if (clear_mouse_face (hlinfo))
28252 cursor = No_Cursor;
28254 /* If no overlay applies, get a text property. */
28255 if (NILP (overlay))
28256 mouse_face = Fget_text_property (position, Qmouse_face, object);
28258 /* Next, compute the bounds of the mouse highlighting and
28259 display it. */
28260 if (!NILP (mouse_face) && STRINGP (object))
28262 /* The mouse-highlighting comes from a display string
28263 with a mouse-face. */
28264 Lisp_Object s, e;
28265 ptrdiff_t ignore;
28267 s = Fprevious_single_property_change
28268 (make_number (pos + 1), Qmouse_face, object, Qnil);
28269 e = Fnext_single_property_change
28270 (position, Qmouse_face, object, Qnil);
28271 if (NILP (s))
28272 s = make_number (0);
28273 if (NILP (e))
28274 e = make_number (SCHARS (object) - 1);
28275 mouse_face_from_string_pos (w, hlinfo, object,
28276 XINT (s), XINT (e));
28277 hlinfo->mouse_face_past_end = 0;
28278 hlinfo->mouse_face_window = window;
28279 hlinfo->mouse_face_face_id
28280 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28281 glyph->face_id, 1);
28282 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28283 cursor = No_Cursor;
28285 else
28287 /* The mouse-highlighting, if any, comes from an overlay
28288 or text property in the buffer. */
28289 Lisp_Object buffer IF_LINT (= Qnil);
28290 Lisp_Object disp_string IF_LINT (= Qnil);
28292 if (STRINGP (object))
28294 /* If we are on a display string with no mouse-face,
28295 check if the text under it has one. */
28296 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28297 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28298 pos = string_buffer_position (object, start);
28299 if (pos > 0)
28301 mouse_face = get_char_property_and_overlay
28302 (make_number (pos), Qmouse_face, w->contents, &overlay);
28303 buffer = w->contents;
28304 disp_string = object;
28307 else
28309 buffer = object;
28310 disp_string = Qnil;
28313 if (!NILP (mouse_face))
28315 Lisp_Object before, after;
28316 Lisp_Object before_string, after_string;
28317 /* To correctly find the limits of mouse highlight
28318 in a bidi-reordered buffer, we must not use the
28319 optimization of limiting the search in
28320 previous-single-property-change and
28321 next-single-property-change, because
28322 rows_from_pos_range needs the real start and end
28323 positions to DTRT in this case. That's because
28324 the first row visible in a window does not
28325 necessarily display the character whose position
28326 is the smallest. */
28327 Lisp_Object lim1 =
28328 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28329 ? Fmarker_position (w->start)
28330 : Qnil;
28331 Lisp_Object lim2 =
28332 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28333 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28334 : Qnil;
28336 if (NILP (overlay))
28338 /* Handle the text property case. */
28339 before = Fprevious_single_property_change
28340 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28341 after = Fnext_single_property_change
28342 (make_number (pos), Qmouse_face, buffer, lim2);
28343 before_string = after_string = Qnil;
28345 else
28347 /* Handle the overlay case. */
28348 before = Foverlay_start (overlay);
28349 after = Foverlay_end (overlay);
28350 before_string = Foverlay_get (overlay, Qbefore_string);
28351 after_string = Foverlay_get (overlay, Qafter_string);
28353 if (!STRINGP (before_string)) before_string = Qnil;
28354 if (!STRINGP (after_string)) after_string = Qnil;
28357 mouse_face_from_buffer_pos (window, hlinfo, pos,
28358 NILP (before)
28360 : XFASTINT (before),
28361 NILP (after)
28362 ? BUF_Z (XBUFFER (buffer))
28363 : XFASTINT (after),
28364 before_string, after_string,
28365 disp_string);
28366 cursor = No_Cursor;
28371 check_help_echo:
28373 /* Look for a `help-echo' property. */
28374 if (NILP (help_echo_string)) {
28375 Lisp_Object help, overlay;
28377 /* Check overlays first. */
28378 help = overlay = Qnil;
28379 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28381 overlay = overlay_vec[i];
28382 help = Foverlay_get (overlay, Qhelp_echo);
28385 if (!NILP (help))
28387 help_echo_string = help;
28388 help_echo_window = window;
28389 help_echo_object = overlay;
28390 help_echo_pos = pos;
28392 else
28394 Lisp_Object obj = glyph->object;
28395 ptrdiff_t charpos = glyph->charpos;
28397 /* Try text properties. */
28398 if (STRINGP (obj)
28399 && charpos >= 0
28400 && charpos < SCHARS (obj))
28402 help = Fget_text_property (make_number (charpos),
28403 Qhelp_echo, obj);
28404 if (NILP (help))
28406 /* If the string itself doesn't specify a help-echo,
28407 see if the buffer text ``under'' it does. */
28408 struct glyph_row *r
28409 = MATRIX_ROW (w->current_matrix, vpos);
28410 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28411 ptrdiff_t p = string_buffer_position (obj, start);
28412 if (p > 0)
28414 help = Fget_char_property (make_number (p),
28415 Qhelp_echo, w->contents);
28416 if (!NILP (help))
28418 charpos = p;
28419 obj = w->contents;
28424 else if (BUFFERP (obj)
28425 && charpos >= BEGV
28426 && charpos < ZV)
28427 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28428 obj);
28430 if (!NILP (help))
28432 help_echo_string = help;
28433 help_echo_window = window;
28434 help_echo_object = obj;
28435 help_echo_pos = charpos;
28440 #ifdef HAVE_WINDOW_SYSTEM
28441 /* Look for a `pointer' property. */
28442 if (FRAME_WINDOW_P (f) && NILP (pointer))
28444 /* Check overlays first. */
28445 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28446 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28448 if (NILP (pointer))
28450 Lisp_Object obj = glyph->object;
28451 ptrdiff_t charpos = glyph->charpos;
28453 /* Try text properties. */
28454 if (STRINGP (obj)
28455 && charpos >= 0
28456 && charpos < SCHARS (obj))
28458 pointer = Fget_text_property (make_number (charpos),
28459 Qpointer, obj);
28460 if (NILP (pointer))
28462 /* If the string itself doesn't specify a pointer,
28463 see if the buffer text ``under'' it does. */
28464 struct glyph_row *r
28465 = MATRIX_ROW (w->current_matrix, vpos);
28466 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28467 ptrdiff_t p = string_buffer_position (obj, start);
28468 if (p > 0)
28469 pointer = Fget_char_property (make_number (p),
28470 Qpointer, w->contents);
28473 else if (BUFFERP (obj)
28474 && charpos >= BEGV
28475 && charpos < ZV)
28476 pointer = Fget_text_property (make_number (charpos),
28477 Qpointer, obj);
28480 #endif /* HAVE_WINDOW_SYSTEM */
28482 BEGV = obegv;
28483 ZV = ozv;
28484 current_buffer = obuf;
28487 set_cursor:
28489 #ifdef HAVE_WINDOW_SYSTEM
28490 if (FRAME_WINDOW_P (f))
28491 define_frame_cursor1 (f, cursor, pointer);
28492 #else
28493 /* This is here to prevent a compiler error, about "label at end of
28494 compound statement". */
28495 return;
28496 #endif
28500 /* EXPORT for RIF:
28501 Clear any mouse-face on window W. This function is part of the
28502 redisplay interface, and is called from try_window_id and similar
28503 functions to ensure the mouse-highlight is off. */
28505 void
28506 x_clear_window_mouse_face (struct window *w)
28508 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28509 Lisp_Object window;
28511 block_input ();
28512 XSETWINDOW (window, w);
28513 if (EQ (window, hlinfo->mouse_face_window))
28514 clear_mouse_face (hlinfo);
28515 unblock_input ();
28519 /* EXPORT:
28520 Just discard the mouse face information for frame F, if any.
28521 This is used when the size of F is changed. */
28523 void
28524 cancel_mouse_face (struct frame *f)
28526 Lisp_Object window;
28527 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28529 window = hlinfo->mouse_face_window;
28530 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28531 reset_mouse_highlight (hlinfo);
28536 /***********************************************************************
28537 Exposure Events
28538 ***********************************************************************/
28540 #ifdef HAVE_WINDOW_SYSTEM
28542 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28543 which intersects rectangle R. R is in window-relative coordinates. */
28545 static void
28546 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28547 enum glyph_row_area area)
28549 struct glyph *first = row->glyphs[area];
28550 struct glyph *end = row->glyphs[area] + row->used[area];
28551 struct glyph *last;
28552 int first_x, start_x, x;
28554 if (area == TEXT_AREA && row->fill_line_p)
28555 /* If row extends face to end of line write the whole line. */
28556 draw_glyphs (w, 0, row, area,
28557 0, row->used[area],
28558 DRAW_NORMAL_TEXT, 0);
28559 else
28561 /* Set START_X to the window-relative start position for drawing glyphs of
28562 AREA. The first glyph of the text area can be partially visible.
28563 The first glyphs of other areas cannot. */
28564 start_x = window_box_left_offset (w, area);
28565 x = start_x;
28566 if (area == TEXT_AREA)
28567 x += row->x;
28569 /* Find the first glyph that must be redrawn. */
28570 while (first < end
28571 && x + first->pixel_width < r->x)
28573 x += first->pixel_width;
28574 ++first;
28577 /* Find the last one. */
28578 last = first;
28579 first_x = x;
28580 while (last < end
28581 && x < r->x + r->width)
28583 x += last->pixel_width;
28584 ++last;
28587 /* Repaint. */
28588 if (last > first)
28589 draw_glyphs (w, first_x - start_x, row, area,
28590 first - row->glyphs[area], last - row->glyphs[area],
28591 DRAW_NORMAL_TEXT, 0);
28596 /* Redraw the parts of the glyph row ROW on window W intersecting
28597 rectangle R. R is in window-relative coordinates. Value is
28598 non-zero if mouse-face was overwritten. */
28600 static int
28601 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28603 eassert (row->enabled_p);
28605 if (row->mode_line_p || w->pseudo_window_p)
28606 draw_glyphs (w, 0, row, TEXT_AREA,
28607 0, row->used[TEXT_AREA],
28608 DRAW_NORMAL_TEXT, 0);
28609 else
28611 if (row->used[LEFT_MARGIN_AREA])
28612 expose_area (w, row, r, LEFT_MARGIN_AREA);
28613 if (row->used[TEXT_AREA])
28614 expose_area (w, row, r, TEXT_AREA);
28615 if (row->used[RIGHT_MARGIN_AREA])
28616 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28617 draw_row_fringe_bitmaps (w, row);
28620 return row->mouse_face_p;
28624 /* Redraw those parts of glyphs rows during expose event handling that
28625 overlap other rows. Redrawing of an exposed line writes over parts
28626 of lines overlapping that exposed line; this function fixes that.
28628 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28629 row in W's current matrix that is exposed and overlaps other rows.
28630 LAST_OVERLAPPING_ROW is the last such row. */
28632 static void
28633 expose_overlaps (struct window *w,
28634 struct glyph_row *first_overlapping_row,
28635 struct glyph_row *last_overlapping_row,
28636 XRectangle *r)
28638 struct glyph_row *row;
28640 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28641 if (row->overlapping_p)
28643 eassert (row->enabled_p && !row->mode_line_p);
28645 row->clip = r;
28646 if (row->used[LEFT_MARGIN_AREA])
28647 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28649 if (row->used[TEXT_AREA])
28650 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28652 if (row->used[RIGHT_MARGIN_AREA])
28653 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28654 row->clip = NULL;
28659 /* Return non-zero if W's cursor intersects rectangle R. */
28661 static int
28662 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28664 XRectangle cr, result;
28665 struct glyph *cursor_glyph;
28666 struct glyph_row *row;
28668 if (w->phys_cursor.vpos >= 0
28669 && w->phys_cursor.vpos < w->current_matrix->nrows
28670 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28671 row->enabled_p)
28672 && row->cursor_in_fringe_p)
28674 /* Cursor is in the fringe. */
28675 cr.x = window_box_right_offset (w,
28676 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28677 ? RIGHT_MARGIN_AREA
28678 : TEXT_AREA));
28679 cr.y = row->y;
28680 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28681 cr.height = row->height;
28682 return x_intersect_rectangles (&cr, r, &result);
28685 cursor_glyph = get_phys_cursor_glyph (w);
28686 if (cursor_glyph)
28688 /* r is relative to W's box, but w->phys_cursor.x is relative
28689 to left edge of W's TEXT area. Adjust it. */
28690 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28691 cr.y = w->phys_cursor.y;
28692 cr.width = cursor_glyph->pixel_width;
28693 cr.height = w->phys_cursor_height;
28694 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28695 I assume the effect is the same -- and this is portable. */
28696 return x_intersect_rectangles (&cr, r, &result);
28698 /* If we don't understand the format, pretend we're not in the hot-spot. */
28699 return 0;
28703 /* EXPORT:
28704 Draw a vertical window border to the right of window W if W doesn't
28705 have vertical scroll bars. */
28707 void
28708 x_draw_vertical_border (struct window *w)
28710 struct frame *f = XFRAME (WINDOW_FRAME (w));
28712 /* We could do better, if we knew what type of scroll-bar the adjacent
28713 windows (on either side) have... But we don't :-(
28714 However, I think this works ok. ++KFS 2003-04-25 */
28716 /* Redraw borders between horizontally adjacent windows. Don't
28717 do it for frames with vertical scroll bars because either the
28718 right scroll bar of a window, or the left scroll bar of its
28719 neighbor will suffice as a border. */
28720 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28721 return;
28723 /* Note: It is necessary to redraw both the left and the right
28724 borders, for when only this single window W is being
28725 redisplayed. */
28726 if (!WINDOW_RIGHTMOST_P (w)
28727 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28729 int x0, x1, y0, y1;
28731 window_box_edges (w, &x0, &y0, &x1, &y1);
28732 y1 -= 1;
28734 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28735 x1 -= 1;
28737 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28739 if (!WINDOW_LEFTMOST_P (w)
28740 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28742 int x0, x1, y0, y1;
28744 window_box_edges (w, &x0, &y0, &x1, &y1);
28745 y1 -= 1;
28747 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28748 x0 -= 1;
28750 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28755 /* Redraw the part of window W intersection rectangle FR. Pixel
28756 coordinates in FR are frame-relative. Call this function with
28757 input blocked. Value is non-zero if the exposure overwrites
28758 mouse-face. */
28760 static int
28761 expose_window (struct window *w, XRectangle *fr)
28763 struct frame *f = XFRAME (w->frame);
28764 XRectangle wr, r;
28765 int mouse_face_overwritten_p = 0;
28767 /* If window is not yet fully initialized, do nothing. This can
28768 happen when toolkit scroll bars are used and a window is split.
28769 Reconfiguring the scroll bar will generate an expose for a newly
28770 created window. */
28771 if (w->current_matrix == NULL)
28772 return 0;
28774 /* When we're currently updating the window, display and current
28775 matrix usually don't agree. Arrange for a thorough display
28776 later. */
28777 if (w->must_be_updated_p)
28779 SET_FRAME_GARBAGED (f);
28780 return 0;
28783 /* Frame-relative pixel rectangle of W. */
28784 wr.x = WINDOW_LEFT_EDGE_X (w);
28785 wr.y = WINDOW_TOP_EDGE_Y (w);
28786 wr.width = WINDOW_TOTAL_WIDTH (w);
28787 wr.height = WINDOW_TOTAL_HEIGHT (w);
28789 if (x_intersect_rectangles (fr, &wr, &r))
28791 int yb = window_text_bottom_y (w);
28792 struct glyph_row *row;
28793 int cursor_cleared_p, phys_cursor_on_p;
28794 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28796 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28797 r.x, r.y, r.width, r.height));
28799 /* Convert to window coordinates. */
28800 r.x -= WINDOW_LEFT_EDGE_X (w);
28801 r.y -= WINDOW_TOP_EDGE_Y (w);
28803 /* Turn off the cursor. */
28804 if (!w->pseudo_window_p
28805 && phys_cursor_in_rect_p (w, &r))
28807 x_clear_cursor (w);
28808 cursor_cleared_p = 1;
28810 else
28811 cursor_cleared_p = 0;
28813 /* If the row containing the cursor extends face to end of line,
28814 then expose_area might overwrite the cursor outside the
28815 rectangle and thus notice_overwritten_cursor might clear
28816 w->phys_cursor_on_p. We remember the original value and
28817 check later if it is changed. */
28818 phys_cursor_on_p = w->phys_cursor_on_p;
28820 /* Update lines intersecting rectangle R. */
28821 first_overlapping_row = last_overlapping_row = NULL;
28822 for (row = w->current_matrix->rows;
28823 row->enabled_p;
28824 ++row)
28826 int y0 = row->y;
28827 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28829 if ((y0 >= r.y && y0 < r.y + r.height)
28830 || (y1 > r.y && y1 < r.y + r.height)
28831 || (r.y >= y0 && r.y < y1)
28832 || (r.y + r.height > y0 && r.y + r.height < y1))
28834 /* A header line may be overlapping, but there is no need
28835 to fix overlapping areas for them. KFS 2005-02-12 */
28836 if (row->overlapping_p && !row->mode_line_p)
28838 if (first_overlapping_row == NULL)
28839 first_overlapping_row = row;
28840 last_overlapping_row = row;
28843 row->clip = fr;
28844 if (expose_line (w, row, &r))
28845 mouse_face_overwritten_p = 1;
28846 row->clip = NULL;
28848 else if (row->overlapping_p)
28850 /* We must redraw a row overlapping the exposed area. */
28851 if (y0 < r.y
28852 ? y0 + row->phys_height > r.y
28853 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28855 if (first_overlapping_row == NULL)
28856 first_overlapping_row = row;
28857 last_overlapping_row = row;
28861 if (y1 >= yb)
28862 break;
28865 /* Display the mode line if there is one. */
28866 if (WINDOW_WANTS_MODELINE_P (w)
28867 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28868 row->enabled_p)
28869 && row->y < r.y + r.height)
28871 if (expose_line (w, row, &r))
28872 mouse_face_overwritten_p = 1;
28875 if (!w->pseudo_window_p)
28877 /* Fix the display of overlapping rows. */
28878 if (first_overlapping_row)
28879 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28880 fr);
28882 /* Draw border between windows. */
28883 x_draw_vertical_border (w);
28885 /* Turn the cursor on again. */
28886 if (cursor_cleared_p
28887 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28888 update_window_cursor (w, 1);
28892 return mouse_face_overwritten_p;
28897 /* Redraw (parts) of all windows in the window tree rooted at W that
28898 intersect R. R contains frame pixel coordinates. Value is
28899 non-zero if the exposure overwrites mouse-face. */
28901 static int
28902 expose_window_tree (struct window *w, XRectangle *r)
28904 struct frame *f = XFRAME (w->frame);
28905 int mouse_face_overwritten_p = 0;
28907 while (w && !FRAME_GARBAGED_P (f))
28909 if (WINDOWP (w->contents))
28910 mouse_face_overwritten_p
28911 |= expose_window_tree (XWINDOW (w->contents), r);
28912 else
28913 mouse_face_overwritten_p |= expose_window (w, r);
28915 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28918 return mouse_face_overwritten_p;
28922 /* EXPORT:
28923 Redisplay an exposed area of frame F. X and Y are the upper-left
28924 corner of the exposed rectangle. W and H are width and height of
28925 the exposed area. All are pixel values. W or H zero means redraw
28926 the entire frame. */
28928 void
28929 expose_frame (struct frame *f, int x, int y, int w, int h)
28931 XRectangle r;
28932 int mouse_face_overwritten_p = 0;
28934 TRACE ((stderr, "expose_frame "));
28936 /* No need to redraw if frame will be redrawn soon. */
28937 if (FRAME_GARBAGED_P (f))
28939 TRACE ((stderr, " garbaged\n"));
28940 return;
28943 /* If basic faces haven't been realized yet, there is no point in
28944 trying to redraw anything. This can happen when we get an expose
28945 event while Emacs is starting, e.g. by moving another window. */
28946 if (FRAME_FACE_CACHE (f) == NULL
28947 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28949 TRACE ((stderr, " no faces\n"));
28950 return;
28953 if (w == 0 || h == 0)
28955 r.x = r.y = 0;
28956 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28957 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28959 else
28961 r.x = x;
28962 r.y = y;
28963 r.width = w;
28964 r.height = h;
28967 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28968 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28970 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28971 if (WINDOWP (f->tool_bar_window))
28972 mouse_face_overwritten_p
28973 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28974 #endif
28976 #ifdef HAVE_X_WINDOWS
28977 #ifndef MSDOS
28978 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28979 if (WINDOWP (f->menu_bar_window))
28980 mouse_face_overwritten_p
28981 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28982 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28983 #endif
28984 #endif
28986 /* Some window managers support a focus-follows-mouse style with
28987 delayed raising of frames. Imagine a partially obscured frame,
28988 and moving the mouse into partially obscured mouse-face on that
28989 frame. The visible part of the mouse-face will be highlighted,
28990 then the WM raises the obscured frame. With at least one WM, KDE
28991 2.1, Emacs is not getting any event for the raising of the frame
28992 (even tried with SubstructureRedirectMask), only Expose events.
28993 These expose events will draw text normally, i.e. not
28994 highlighted. Which means we must redo the highlight here.
28995 Subsume it under ``we love X''. --gerd 2001-08-15 */
28996 /* Included in Windows version because Windows most likely does not
28997 do the right thing if any third party tool offers
28998 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28999 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29001 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29002 if (f == hlinfo->mouse_face_mouse_frame)
29004 int mouse_x = hlinfo->mouse_face_mouse_x;
29005 int mouse_y = hlinfo->mouse_face_mouse_y;
29006 clear_mouse_face (hlinfo);
29007 note_mouse_highlight (f, mouse_x, mouse_y);
29013 /* EXPORT:
29014 Determine the intersection of two rectangles R1 and R2. Return
29015 the intersection in *RESULT. Value is non-zero if RESULT is not
29016 empty. */
29019 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29021 XRectangle *left, *right;
29022 XRectangle *upper, *lower;
29023 int intersection_p = 0;
29025 /* Rearrange so that R1 is the left-most rectangle. */
29026 if (r1->x < r2->x)
29027 left = r1, right = r2;
29028 else
29029 left = r2, right = r1;
29031 /* X0 of the intersection is right.x0, if this is inside R1,
29032 otherwise there is no intersection. */
29033 if (right->x <= left->x + left->width)
29035 result->x = right->x;
29037 /* The right end of the intersection is the minimum of
29038 the right ends of left and right. */
29039 result->width = (min (left->x + left->width, right->x + right->width)
29040 - result->x);
29042 /* Same game for Y. */
29043 if (r1->y < r2->y)
29044 upper = r1, lower = r2;
29045 else
29046 upper = r2, lower = r1;
29048 /* The upper end of the intersection is lower.y0, if this is inside
29049 of upper. Otherwise, there is no intersection. */
29050 if (lower->y <= upper->y + upper->height)
29052 result->y = lower->y;
29054 /* The lower end of the intersection is the minimum of the lower
29055 ends of upper and lower. */
29056 result->height = (min (lower->y + lower->height,
29057 upper->y + upper->height)
29058 - result->y);
29059 intersection_p = 1;
29063 return intersection_p;
29066 #endif /* HAVE_WINDOW_SYSTEM */
29069 /***********************************************************************
29070 Initialization
29071 ***********************************************************************/
29073 void
29074 syms_of_xdisp (void)
29076 Vwith_echo_area_save_vector = Qnil;
29077 staticpro (&Vwith_echo_area_save_vector);
29079 Vmessage_stack = Qnil;
29080 staticpro (&Vmessage_stack);
29082 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29083 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29085 message_dolog_marker1 = Fmake_marker ();
29086 staticpro (&message_dolog_marker1);
29087 message_dolog_marker2 = Fmake_marker ();
29088 staticpro (&message_dolog_marker2);
29089 message_dolog_marker3 = Fmake_marker ();
29090 staticpro (&message_dolog_marker3);
29092 #ifdef GLYPH_DEBUG
29093 defsubr (&Sdump_frame_glyph_matrix);
29094 defsubr (&Sdump_glyph_matrix);
29095 defsubr (&Sdump_glyph_row);
29096 defsubr (&Sdump_tool_bar_row);
29097 defsubr (&Strace_redisplay);
29098 defsubr (&Strace_to_stderr);
29099 #endif
29100 #ifdef HAVE_WINDOW_SYSTEM
29101 defsubr (&Stool_bar_lines_needed);
29102 defsubr (&Slookup_image_map);
29103 #endif
29104 defsubr (&Sline_pixel_height);
29105 defsubr (&Sformat_mode_line);
29106 defsubr (&Sinvisible_p);
29107 defsubr (&Scurrent_bidi_paragraph_direction);
29108 defsubr (&Smove_point_visually);
29110 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29111 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29112 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29113 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29114 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29115 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29116 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29117 DEFSYM (Qeval, "eval");
29118 DEFSYM (QCdata, ":data");
29119 DEFSYM (Qdisplay, "display");
29120 DEFSYM (Qspace_width, "space-width");
29121 DEFSYM (Qraise, "raise");
29122 DEFSYM (Qslice, "slice");
29123 DEFSYM (Qspace, "space");
29124 DEFSYM (Qmargin, "margin");
29125 DEFSYM (Qpointer, "pointer");
29126 DEFSYM (Qleft_margin, "left-margin");
29127 DEFSYM (Qright_margin, "right-margin");
29128 DEFSYM (Qcenter, "center");
29129 DEFSYM (Qline_height, "line-height");
29130 DEFSYM (QCalign_to, ":align-to");
29131 DEFSYM (QCrelative_width, ":relative-width");
29132 DEFSYM (QCrelative_height, ":relative-height");
29133 DEFSYM (QCeval, ":eval");
29134 DEFSYM (QCpropertize, ":propertize");
29135 DEFSYM (QCfile, ":file");
29136 DEFSYM (Qfontified, "fontified");
29137 DEFSYM (Qfontification_functions, "fontification-functions");
29138 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29139 DEFSYM (Qescape_glyph, "escape-glyph");
29140 DEFSYM (Qnobreak_space, "nobreak-space");
29141 DEFSYM (Qimage, "image");
29142 DEFSYM (Qtext, "text");
29143 DEFSYM (Qboth, "both");
29144 DEFSYM (Qboth_horiz, "both-horiz");
29145 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29146 DEFSYM (QCmap, ":map");
29147 DEFSYM (QCpointer, ":pointer");
29148 DEFSYM (Qrect, "rect");
29149 DEFSYM (Qcircle, "circle");
29150 DEFSYM (Qpoly, "poly");
29151 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29152 DEFSYM (Qgrow_only, "grow-only");
29153 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29154 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29155 DEFSYM (Qposition, "position");
29156 DEFSYM (Qbuffer_position, "buffer-position");
29157 DEFSYM (Qobject, "object");
29158 DEFSYM (Qbar, "bar");
29159 DEFSYM (Qhbar, "hbar");
29160 DEFSYM (Qbox, "box");
29161 DEFSYM (Qhollow, "hollow");
29162 DEFSYM (Qhand, "hand");
29163 DEFSYM (Qarrow, "arrow");
29164 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29166 list_of_error = list1 (list2 (intern_c_string ("error"),
29167 intern_c_string ("void-variable")));
29168 staticpro (&list_of_error);
29170 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29171 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29172 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29173 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29175 echo_buffer[0] = echo_buffer[1] = Qnil;
29176 staticpro (&echo_buffer[0]);
29177 staticpro (&echo_buffer[1]);
29179 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29180 staticpro (&echo_area_buffer[0]);
29181 staticpro (&echo_area_buffer[1]);
29183 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29184 staticpro (&Vmessages_buffer_name);
29186 mode_line_proptrans_alist = Qnil;
29187 staticpro (&mode_line_proptrans_alist);
29188 mode_line_string_list = Qnil;
29189 staticpro (&mode_line_string_list);
29190 mode_line_string_face = Qnil;
29191 staticpro (&mode_line_string_face);
29192 mode_line_string_face_prop = Qnil;
29193 staticpro (&mode_line_string_face_prop);
29194 Vmode_line_unwind_vector = Qnil;
29195 staticpro (&Vmode_line_unwind_vector);
29197 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29199 help_echo_string = Qnil;
29200 staticpro (&help_echo_string);
29201 help_echo_object = Qnil;
29202 staticpro (&help_echo_object);
29203 help_echo_window = Qnil;
29204 staticpro (&help_echo_window);
29205 previous_help_echo_string = Qnil;
29206 staticpro (&previous_help_echo_string);
29207 help_echo_pos = -1;
29209 DEFSYM (Qright_to_left, "right-to-left");
29210 DEFSYM (Qleft_to_right, "left-to-right");
29212 #ifdef HAVE_WINDOW_SYSTEM
29213 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29214 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29215 For example, if a block cursor is over a tab, it will be drawn as
29216 wide as that tab on the display. */);
29217 x_stretch_cursor_p = 0;
29218 #endif
29220 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29221 doc: /* Non-nil means highlight trailing whitespace.
29222 The face used for trailing whitespace is `trailing-whitespace'. */);
29223 Vshow_trailing_whitespace = Qnil;
29225 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29226 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29227 If the value is t, Emacs highlights non-ASCII chars which have the
29228 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29229 or `escape-glyph' face respectively.
29231 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29232 U+2011 (non-breaking hyphen) are affected.
29234 Any other non-nil value means to display these characters as a escape
29235 glyph followed by an ordinary space or hyphen.
29237 A value of nil means no special handling of these characters. */);
29238 Vnobreak_char_display = Qt;
29240 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29241 doc: /* The pointer shape to show in void text areas.
29242 A value of nil means to show the text pointer. Other options are `arrow',
29243 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29244 Vvoid_text_area_pointer = Qarrow;
29246 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29247 doc: /* Non-nil means don't actually do any redisplay.
29248 This is used for internal purposes. */);
29249 Vinhibit_redisplay = Qnil;
29251 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29252 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29253 Vglobal_mode_string = Qnil;
29255 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29256 doc: /* Marker for where to display an arrow on top of the buffer text.
29257 This must be the beginning of a line in order to work.
29258 See also `overlay-arrow-string'. */);
29259 Voverlay_arrow_position = Qnil;
29261 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29262 doc: /* String to display as an arrow in non-window frames.
29263 See also `overlay-arrow-position'. */);
29264 Voverlay_arrow_string = build_pure_c_string ("=>");
29266 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29267 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29268 The symbols on this list are examined during redisplay to determine
29269 where to display overlay arrows. */);
29270 Voverlay_arrow_variable_list
29271 = list1 (intern_c_string ("overlay-arrow-position"));
29273 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29274 doc: /* The number of lines to try scrolling a window by when point moves out.
29275 If that fails to bring point back on frame, point is centered instead.
29276 If this is zero, point is always centered after it moves off frame.
29277 If you want scrolling to always be a line at a time, you should set
29278 `scroll-conservatively' to a large value rather than set this to 1. */);
29280 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29281 doc: /* Scroll up to this many lines, to bring point back on screen.
29282 If point moves off-screen, redisplay will scroll by up to
29283 `scroll-conservatively' lines in order to bring point just barely
29284 onto the screen again. If that cannot be done, then redisplay
29285 recenters point as usual.
29287 If the value is greater than 100, redisplay will never recenter point,
29288 but will always scroll just enough text to bring point into view, even
29289 if you move far away.
29291 A value of zero means always recenter point if it moves off screen. */);
29292 scroll_conservatively = 0;
29294 DEFVAR_INT ("scroll-margin", scroll_margin,
29295 doc: /* Number of lines of margin at the top and bottom of a window.
29296 Recenter the window whenever point gets within this many lines
29297 of the top or bottom of the window. */);
29298 scroll_margin = 0;
29300 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29301 doc: /* Pixels per inch value for non-window system displays.
29302 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29303 Vdisplay_pixels_per_inch = make_float (72.0);
29305 #ifdef GLYPH_DEBUG
29306 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29307 #endif
29309 DEFVAR_LISP ("truncate-partial-width-windows",
29310 Vtruncate_partial_width_windows,
29311 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29312 For an integer value, truncate lines in each window narrower than the
29313 full frame width, provided the window width is less than that integer;
29314 otherwise, respect the value of `truncate-lines'.
29316 For any other non-nil value, truncate lines in all windows that do
29317 not span the full frame width.
29319 A value of nil means to respect the value of `truncate-lines'.
29321 If `word-wrap' is enabled, you might want to reduce this. */);
29322 Vtruncate_partial_width_windows = make_number (50);
29324 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29325 doc: /* Maximum buffer size for which line number should be displayed.
29326 If the buffer is bigger than this, the line number does not appear
29327 in the mode line. A value of nil means no limit. */);
29328 Vline_number_display_limit = Qnil;
29330 DEFVAR_INT ("line-number-display-limit-width",
29331 line_number_display_limit_width,
29332 doc: /* Maximum line width (in characters) for line number display.
29333 If the average length of the lines near point is bigger than this, then the
29334 line number may be omitted from the mode line. */);
29335 line_number_display_limit_width = 200;
29337 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29338 doc: /* Non-nil means highlight region even in nonselected windows. */);
29339 highlight_nonselected_windows = 0;
29341 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29342 doc: /* Non-nil if more than one frame is visible on this display.
29343 Minibuffer-only frames don't count, but iconified frames do.
29344 This variable is not guaranteed to be accurate except while processing
29345 `frame-title-format' and `icon-title-format'. */);
29347 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29348 doc: /* Template for displaying the title bar of visible frames.
29349 \(Assuming the window manager supports this feature.)
29351 This variable has the same structure as `mode-line-format', except that
29352 the %c and %l constructs are ignored. It is used only on frames for
29353 which no explicit name has been set \(see `modify-frame-parameters'). */);
29355 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29356 doc: /* Template for displaying the title bar of an iconified frame.
29357 \(Assuming the window manager supports this feature.)
29358 This variable has the same structure as `mode-line-format' (which see),
29359 and is used only on frames for which no explicit name has been set
29360 \(see `modify-frame-parameters'). */);
29361 Vicon_title_format
29362 = Vframe_title_format
29363 = listn (CONSTYPE_PURE, 3,
29364 intern_c_string ("multiple-frames"),
29365 build_pure_c_string ("%b"),
29366 listn (CONSTYPE_PURE, 4,
29367 empty_unibyte_string,
29368 intern_c_string ("invocation-name"),
29369 build_pure_c_string ("@"),
29370 intern_c_string ("system-name")));
29372 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29373 doc: /* Maximum number of lines to keep in the message log buffer.
29374 If nil, disable message logging. If t, log messages but don't truncate
29375 the buffer when it becomes large. */);
29376 Vmessage_log_max = make_number (1000);
29378 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29379 doc: /* Functions called before redisplay, if window sizes have changed.
29380 The value should be a list of functions that take one argument.
29381 Just before redisplay, for each frame, if any of its windows have changed
29382 size since the last redisplay, or have been split or deleted,
29383 all the functions in the list are called, with the frame as argument. */);
29384 Vwindow_size_change_functions = Qnil;
29386 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29387 doc: /* List of functions to call before redisplaying a window with scrolling.
29388 Each function is called with two arguments, the window and its new
29389 display-start position. Note that these functions are also called by
29390 `set-window-buffer'. Also note that the value of `window-end' is not
29391 valid when these functions are called.
29393 Warning: Do not use this feature to alter the way the window
29394 is scrolled. It is not designed for that, and such use probably won't
29395 work. */);
29396 Vwindow_scroll_functions = Qnil;
29398 DEFVAR_LISP ("window-text-change-functions",
29399 Vwindow_text_change_functions,
29400 doc: /* Functions to call in redisplay when text in the window might change. */);
29401 Vwindow_text_change_functions = Qnil;
29403 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29404 doc: /* Functions called when redisplay of a window reaches the end trigger.
29405 Each function is called with two arguments, the window and the end trigger value.
29406 See `set-window-redisplay-end-trigger'. */);
29407 Vredisplay_end_trigger_functions = Qnil;
29409 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29410 doc: /* Non-nil means autoselect window with mouse pointer.
29411 If nil, do not autoselect windows.
29412 A positive number means delay autoselection by that many seconds: a
29413 window is autoselected only after the mouse has remained in that
29414 window for the duration of the delay.
29415 A negative number has a similar effect, but causes windows to be
29416 autoselected only after the mouse has stopped moving. \(Because of
29417 the way Emacs compares mouse events, you will occasionally wait twice
29418 that time before the window gets selected.\)
29419 Any other value means to autoselect window instantaneously when the
29420 mouse pointer enters it.
29422 Autoselection selects the minibuffer only if it is active, and never
29423 unselects the minibuffer if it is active.
29425 When customizing this variable make sure that the actual value of
29426 `focus-follows-mouse' matches the behavior of your window manager. */);
29427 Vmouse_autoselect_window = Qnil;
29429 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29430 doc: /* Non-nil means automatically resize tool-bars.
29431 This dynamically changes the tool-bar's height to the minimum height
29432 that is needed to make all tool-bar items visible.
29433 If value is `grow-only', the tool-bar's height is only increased
29434 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29435 Vauto_resize_tool_bars = Qt;
29437 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29438 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29439 auto_raise_tool_bar_buttons_p = 1;
29441 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29442 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29443 make_cursor_line_fully_visible_p = 1;
29445 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29446 doc: /* Border below tool-bar in pixels.
29447 If an integer, use it as the height of the border.
29448 If it is one of `internal-border-width' or `border-width', use the
29449 value of the corresponding frame parameter.
29450 Otherwise, no border is added below the tool-bar. */);
29451 Vtool_bar_border = Qinternal_border_width;
29453 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29454 doc: /* Margin around tool-bar buttons in pixels.
29455 If an integer, use that for both horizontal and vertical margins.
29456 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29457 HORZ specifying the horizontal margin, and VERT specifying the
29458 vertical margin. */);
29459 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29461 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29462 doc: /* Relief thickness of tool-bar buttons. */);
29463 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29465 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29466 doc: /* Tool bar style to use.
29467 It can be one of
29468 image - show images only
29469 text - show text only
29470 both - show both, text below image
29471 both-horiz - show text to the right of the image
29472 text-image-horiz - show text to the left of the image
29473 any other - use system default or image if no system default.
29475 This variable only affects the GTK+ toolkit version of Emacs. */);
29476 Vtool_bar_style = Qnil;
29478 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29479 doc: /* Maximum number of characters a label can have to be shown.
29480 The tool bar style must also show labels for this to have any effect, see
29481 `tool-bar-style'. */);
29482 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29484 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29485 doc: /* List of functions to call to fontify regions of text.
29486 Each function is called with one argument POS. Functions must
29487 fontify a region starting at POS in the current buffer, and give
29488 fontified regions the property `fontified'. */);
29489 Vfontification_functions = Qnil;
29490 Fmake_variable_buffer_local (Qfontification_functions);
29492 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29493 unibyte_display_via_language_environment,
29494 doc: /* Non-nil means display unibyte text according to language environment.
29495 Specifically, this means that raw bytes in the range 160-255 decimal
29496 are displayed by converting them to the equivalent multibyte characters
29497 according to the current language environment. As a result, they are
29498 displayed according to the current fontset.
29500 Note that this variable affects only how these bytes are displayed,
29501 but does not change the fact they are interpreted as raw bytes. */);
29502 unibyte_display_via_language_environment = 0;
29504 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29505 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29506 If a float, it specifies a fraction of the mini-window frame's height.
29507 If an integer, it specifies a number of lines. */);
29508 Vmax_mini_window_height = make_float (0.25);
29510 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29511 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29512 A value of nil means don't automatically resize mini-windows.
29513 A value of t means resize them to fit the text displayed in them.
29514 A value of `grow-only', the default, means let mini-windows grow only;
29515 they return to their normal size when the minibuffer is closed, or the
29516 echo area becomes empty. */);
29517 Vresize_mini_windows = Qgrow_only;
29519 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29520 doc: /* Alist specifying how to blink the cursor off.
29521 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29522 `cursor-type' frame-parameter or variable equals ON-STATE,
29523 comparing using `equal', Emacs uses OFF-STATE to specify
29524 how to blink it off. ON-STATE and OFF-STATE are values for
29525 the `cursor-type' frame parameter.
29527 If a frame's ON-STATE has no entry in this list,
29528 the frame's other specifications determine how to blink the cursor off. */);
29529 Vblink_cursor_alist = Qnil;
29531 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29532 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29533 If non-nil, windows are automatically scrolled horizontally to make
29534 point visible. */);
29535 automatic_hscrolling_p = 1;
29536 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29538 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29539 doc: /* How many columns away from the window edge point is allowed to get
29540 before automatic hscrolling will horizontally scroll the window. */);
29541 hscroll_margin = 5;
29543 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29544 doc: /* How many columns to scroll the window when point gets too close to the edge.
29545 When point is less than `hscroll-margin' columns from the window
29546 edge, automatic hscrolling will scroll the window by the amount of columns
29547 determined by this variable. If its value is a positive integer, scroll that
29548 many columns. If it's a positive floating-point number, it specifies the
29549 fraction of the window's width to scroll. If it's nil or zero, point will be
29550 centered horizontally after the scroll. Any other value, including negative
29551 numbers, are treated as if the value were zero.
29553 Automatic hscrolling always moves point outside the scroll margin, so if
29554 point was more than scroll step columns inside the margin, the window will
29555 scroll more than the value given by the scroll step.
29557 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29558 and `scroll-right' overrides this variable's effect. */);
29559 Vhscroll_step = make_number (0);
29561 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29562 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29563 Bind this around calls to `message' to let it take effect. */);
29564 message_truncate_lines = 0;
29566 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29567 doc: /* Normal hook run to update the menu bar definitions.
29568 Redisplay runs this hook before it redisplays the menu bar.
29569 This is used to update submenus such as Buffers,
29570 whose contents depend on various data. */);
29571 Vmenu_bar_update_hook = Qnil;
29573 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29574 doc: /* Frame for which we are updating a menu.
29575 The enable predicate for a menu binding should check this variable. */);
29576 Vmenu_updating_frame = Qnil;
29578 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29579 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29580 inhibit_menubar_update = 0;
29582 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29583 doc: /* Prefix prepended to all continuation lines at display time.
29584 The value may be a string, an image, or a stretch-glyph; it is
29585 interpreted in the same way as the value of a `display' text property.
29587 This variable is overridden by any `wrap-prefix' text or overlay
29588 property.
29590 To add a prefix to non-continuation lines, use `line-prefix'. */);
29591 Vwrap_prefix = Qnil;
29592 DEFSYM (Qwrap_prefix, "wrap-prefix");
29593 Fmake_variable_buffer_local (Qwrap_prefix);
29595 DEFVAR_LISP ("line-prefix", Vline_prefix,
29596 doc: /* Prefix prepended to all non-continuation lines at display time.
29597 The value may be a string, an image, or a stretch-glyph; it is
29598 interpreted in the same way as the value of a `display' text property.
29600 This variable is overridden by any `line-prefix' text or overlay
29601 property.
29603 To add a prefix to continuation lines, use `wrap-prefix'. */);
29604 Vline_prefix = Qnil;
29605 DEFSYM (Qline_prefix, "line-prefix");
29606 Fmake_variable_buffer_local (Qline_prefix);
29608 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29609 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29610 inhibit_eval_during_redisplay = 0;
29612 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29613 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29614 inhibit_free_realized_faces = 0;
29616 #ifdef GLYPH_DEBUG
29617 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29618 doc: /* Inhibit try_window_id display optimization. */);
29619 inhibit_try_window_id = 0;
29621 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29622 doc: /* Inhibit try_window_reusing display optimization. */);
29623 inhibit_try_window_reusing = 0;
29625 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29626 doc: /* Inhibit try_cursor_movement display optimization. */);
29627 inhibit_try_cursor_movement = 0;
29628 #endif /* GLYPH_DEBUG */
29630 DEFVAR_INT ("overline-margin", overline_margin,
29631 doc: /* Space between overline and text, in pixels.
29632 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29633 margin to the character height. */);
29634 overline_margin = 2;
29636 DEFVAR_INT ("underline-minimum-offset",
29637 underline_minimum_offset,
29638 doc: /* Minimum distance between baseline and underline.
29639 This can improve legibility of underlined text at small font sizes,
29640 particularly when using variable `x-use-underline-position-properties'
29641 with fonts that specify an UNDERLINE_POSITION relatively close to the
29642 baseline. The default value is 1. */);
29643 underline_minimum_offset = 1;
29645 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29646 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29647 This feature only works when on a window system that can change
29648 cursor shapes. */);
29649 display_hourglass_p = 1;
29651 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29652 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29653 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29655 #ifdef HAVE_WINDOW_SYSTEM
29656 hourglass_atimer = NULL;
29657 hourglass_shown_p = 0;
29658 #endif /* HAVE_WINDOW_SYSTEM */
29660 DEFSYM (Qglyphless_char, "glyphless-char");
29661 DEFSYM (Qhex_code, "hex-code");
29662 DEFSYM (Qempty_box, "empty-box");
29663 DEFSYM (Qthin_space, "thin-space");
29664 DEFSYM (Qzero_width, "zero-width");
29666 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29667 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29669 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29670 doc: /* Char-table defining glyphless characters.
29671 Each element, if non-nil, should be one of the following:
29672 an ASCII acronym string: display this string in a box
29673 `hex-code': display the hexadecimal code of a character in a box
29674 `empty-box': display as an empty box
29675 `thin-space': display as 1-pixel width space
29676 `zero-width': don't display
29677 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29678 display method for graphical terminals and text terminals respectively.
29679 GRAPHICAL and TEXT should each have one of the values listed above.
29681 The char-table has one extra slot to control the display of a character for
29682 which no font is found. This slot only takes effect on graphical terminals.
29683 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29684 `thin-space'. The default is `empty-box'. */);
29685 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29686 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29687 Qempty_box);
29689 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29690 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29691 Vdebug_on_message = Qnil;
29695 /* Initialize this module when Emacs starts. */
29697 void
29698 init_xdisp (void)
29700 CHARPOS (this_line_start_pos) = 0;
29702 if (!noninteractive)
29704 struct window *m = XWINDOW (minibuf_window);
29705 Lisp_Object frame = m->frame;
29706 struct frame *f = XFRAME (frame);
29707 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29708 struct window *r = XWINDOW (root);
29709 int i;
29711 echo_area_window = minibuf_window;
29713 r->top_line = FRAME_TOP_MARGIN (f);
29714 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29715 r->total_cols = FRAME_COLS (f);
29717 m->top_line = FRAME_LINES (f) - 1;
29718 m->total_lines = 1;
29719 m->total_cols = FRAME_COLS (f);
29721 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29722 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29723 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29725 /* The default ellipsis glyphs `...'. */
29726 for (i = 0; i < 3; ++i)
29727 default_invis_vector[i] = make_number ('.');
29731 /* Allocate the buffer for frame titles.
29732 Also used for `format-mode-line'. */
29733 int size = 100;
29734 mode_line_noprop_buf = xmalloc (size);
29735 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29736 mode_line_noprop_ptr = mode_line_noprop_buf;
29737 mode_line_target = MODE_LINE_DISPLAY;
29740 help_echo_showing_p = 0;
29743 #ifdef HAVE_WINDOW_SYSTEM
29745 /* Platform-independent portion of hourglass implementation. */
29747 /* Cancel a currently active hourglass timer, and start a new one. */
29748 void
29749 start_hourglass (void)
29751 struct timespec delay;
29753 cancel_hourglass ();
29755 if (INTEGERP (Vhourglass_delay)
29756 && XINT (Vhourglass_delay) > 0)
29757 delay = make_timespec (min (XINT (Vhourglass_delay),
29758 TYPE_MAXIMUM (time_t)),
29760 else if (FLOATP (Vhourglass_delay)
29761 && XFLOAT_DATA (Vhourglass_delay) > 0)
29762 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29763 else
29764 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29766 #ifdef HAVE_NTGUI
29768 extern void w32_note_current_window (void);
29769 w32_note_current_window ();
29771 #endif /* HAVE_NTGUI */
29773 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29774 show_hourglass, NULL);
29778 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29779 shown. */
29780 void
29781 cancel_hourglass (void)
29783 if (hourglass_atimer)
29785 cancel_atimer (hourglass_atimer);
29786 hourglass_atimer = NULL;
29789 if (hourglass_shown_p)
29790 hide_hourglass ();
29793 #endif /* HAVE_WINDOW_SYSTEM */