Fix treatment of ld's nocombreloc option
[emacs.git] / src / xdisp.c
blobd5def0659364c1b7a02b35aab162f84d94bf643f
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
103 . try_window
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
116 Desired matrices.
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
161 Frame matrices.
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
182 Bidirectional display.
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
225 Bidirectional display and character compositions
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
252 Moving an iterator in bidirectional text
253 without producing glyphs
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302 #ifdef HAVE_WINDOW_SYSTEM
303 #include TERM_HEADER
304 #endif /* HAVE_WINDOW_SYSTEM */
306 #ifndef FRAME_X_OUTPUT
307 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
308 #endif
310 #define INFINITY 10000000
312 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
313 Lisp_Object Qwindow_scroll_functions;
314 static Lisp_Object Qwindow_text_change_functions;
315 static Lisp_Object Qredisplay_end_trigger_functions;
316 Lisp_Object Qinhibit_point_motion_hooks;
317 static Lisp_Object QCeval, QCpropertize;
318 Lisp_Object QCfile, QCdata;
319 static Lisp_Object Qfontified;
320 static Lisp_Object Qgrow_only;
321 static Lisp_Object Qinhibit_eval_during_redisplay;
322 static Lisp_Object Qbuffer_position, Qposition, Qobject;
323 static Lisp_Object Qright_to_left, Qleft_to_right;
325 /* Cursor shapes. */
326 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
328 /* Pointer shapes. */
329 static Lisp_Object Qarrow, Qhand;
330 Lisp_Object Qtext;
332 /* Holds the list (error). */
333 static Lisp_Object list_of_error;
335 static Lisp_Object Qfontification_functions;
337 static Lisp_Object Qwrap_prefix;
338 static Lisp_Object Qline_prefix;
339 static Lisp_Object Qredisplay_internal;
341 /* Non-nil means don't actually do any redisplay. */
343 Lisp_Object Qinhibit_redisplay;
345 /* Names of text properties relevant for redisplay. */
347 Lisp_Object Qdisplay;
349 Lisp_Object Qspace, QCalign_to;
350 static Lisp_Object QCrelative_width, QCrelative_height;
351 Lisp_Object Qleft_margin, Qright_margin;
352 static Lisp_Object Qspace_width, Qraise;
353 static Lisp_Object Qslice;
354 Lisp_Object Qcenter;
355 static Lisp_Object Qmargin, Qpointer;
356 static Lisp_Object Qline_height;
358 #ifdef HAVE_WINDOW_SYSTEM
360 /* Test if overflow newline into fringe. Called with iterator IT
361 at or past right window margin, and with IT->current_x set. */
363 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
364 (!NILP (Voverflow_newline_into_fringe) \
365 && FRAME_WINDOW_P ((IT)->f) \
366 && ((IT)->bidi_it.paragraph_dir == R2L \
367 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
368 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
369 && (IT)->current_x == (IT)->last_visible_x)
371 #else /* !HAVE_WINDOW_SYSTEM */
372 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
373 #endif /* HAVE_WINDOW_SYSTEM */
375 /* Test if the display element loaded in IT, or the underlying buffer
376 or string character, is a space or a TAB character. This is used
377 to determine where word wrapping can occur. */
379 #define IT_DISPLAYING_WHITESPACE(it) \
380 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
381 || ((STRINGP (it->string) \
382 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
383 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
384 || (it->s \
385 && (it->s[IT_BYTEPOS (*it)] == ' ' \
386 || it->s[IT_BYTEPOS (*it)] == '\t')) \
387 || (IT_BYTEPOS (*it) < ZV_BYTE \
388 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
389 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
391 /* Name of the face used to highlight trailing whitespace. */
393 static Lisp_Object Qtrailing_whitespace;
395 /* Name and number of the face used to highlight escape glyphs. */
397 static Lisp_Object Qescape_glyph;
399 /* Name and number of the face used to highlight non-breaking spaces. */
401 static Lisp_Object Qnobreak_space;
403 /* The symbol `image' which is the car of the lists used to represent
404 images in Lisp. Also a tool bar style. */
406 Lisp_Object Qimage;
408 /* The image map types. */
409 Lisp_Object QCmap;
410 static Lisp_Object QCpointer;
411 static Lisp_Object Qrect, Qcircle, Qpoly;
413 /* Tool bar styles */
414 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
416 /* Non-zero means print newline to stdout before next mini-buffer
417 message. */
419 int noninteractive_need_newline;
421 /* Non-zero means print newline to message log before next message. */
423 static int message_log_need_newline;
425 /* Three markers that message_dolog uses.
426 It could allocate them itself, but that causes trouble
427 in handling memory-full errors. */
428 static Lisp_Object message_dolog_marker1;
429 static Lisp_Object message_dolog_marker2;
430 static Lisp_Object message_dolog_marker3;
432 /* The buffer position of the first character appearing entirely or
433 partially on the line of the selected window which contains the
434 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
435 redisplay optimization in redisplay_internal. */
437 static struct text_pos this_line_start_pos;
439 /* Number of characters past the end of the line above, including the
440 terminating newline. */
442 static struct text_pos this_line_end_pos;
444 /* The vertical positions and the height of this line. */
446 static int this_line_vpos;
447 static int this_line_y;
448 static int this_line_pixel_height;
450 /* X position at which this display line starts. Usually zero;
451 negative if first character is partially visible. */
453 static int this_line_start_x;
455 /* The smallest character position seen by move_it_* functions as they
456 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
457 hscrolled lines, see display_line. */
459 static struct text_pos this_line_min_pos;
461 /* Buffer that this_line_.* variables are referring to. */
463 static struct buffer *this_line_buffer;
466 /* Values of those variables at last redisplay are stored as
467 properties on `overlay-arrow-position' symbol. However, if
468 Voverlay_arrow_position is a marker, last-arrow-position is its
469 numerical position. */
471 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
473 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
474 properties on a symbol in overlay-arrow-variable-list. */
476 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
478 Lisp_Object Qmenu_bar_update_hook;
480 /* Nonzero if an overlay arrow has been displayed in this window. */
482 static int overlay_arrow_seen;
484 /* Vector containing glyphs for an ellipsis `...'. */
486 static Lisp_Object default_invis_vector[3];
488 /* This is the window where the echo area message was displayed. It
489 is always a mini-buffer window, but it may not be the same window
490 currently active as a mini-buffer. */
492 Lisp_Object echo_area_window;
494 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
495 pushes the current message and the value of
496 message_enable_multibyte on the stack, the function restore_message
497 pops the stack and displays MESSAGE again. */
499 static Lisp_Object Vmessage_stack;
501 /* Nonzero means multibyte characters were enabled when the echo area
502 message was specified. */
504 static int message_enable_multibyte;
506 /* Nonzero if we should redraw the mode lines on the next redisplay. */
508 int update_mode_lines;
510 /* Nonzero if window sizes or contents have changed since last
511 redisplay that finished. */
513 int windows_or_buffers_changed;
515 /* Nonzero after display_mode_line if %l was used and it displayed a
516 line number. */
518 static int line_number_displayed;
520 /* The name of the *Messages* buffer, a string. */
522 static Lisp_Object Vmessages_buffer_name;
524 /* Current, index 0, and last displayed echo area message. Either
525 buffers from echo_buffers, or nil to indicate no message. */
527 Lisp_Object echo_area_buffer[2];
529 /* The buffers referenced from echo_area_buffer. */
531 static Lisp_Object echo_buffer[2];
533 /* A vector saved used in with_area_buffer to reduce consing. */
535 static Lisp_Object Vwith_echo_area_save_vector;
537 /* Non-zero means display_echo_area should display the last echo area
538 message again. Set by redisplay_preserve_echo_area. */
540 static int display_last_displayed_message_p;
542 /* Nonzero if echo area is being used by print; zero if being used by
543 message. */
545 static int message_buf_print;
547 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
549 static Lisp_Object Qinhibit_menubar_update;
550 static Lisp_Object Qmessage_truncate_lines;
552 /* Set to 1 in clear_message to make redisplay_internal aware
553 of an emptied echo area. */
555 static int message_cleared_p;
557 /* A scratch glyph row with contents used for generating truncation
558 glyphs. Also used in direct_output_for_insert. */
560 #define MAX_SCRATCH_GLYPHS 100
561 static struct glyph_row scratch_glyph_row;
562 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
564 /* Ascent and height of the last line processed by move_it_to. */
566 static int last_height;
568 /* Non-zero if there's a help-echo in the echo area. */
570 int help_echo_showing_p;
572 /* The maximum distance to look ahead for text properties. Values
573 that are too small let us call compute_char_face and similar
574 functions too often which is expensive. Values that are too large
575 let us call compute_char_face and alike too often because we
576 might not be interested in text properties that far away. */
578 #define TEXT_PROP_DISTANCE_LIMIT 100
580 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
581 iterator state and later restore it. This is needed because the
582 bidi iterator on bidi.c keeps a stacked cache of its states, which
583 is really a singleton. When we use scratch iterator objects to
584 move around the buffer, we can cause the bidi cache to be pushed or
585 popped, and therefore we need to restore the cache state when we
586 return to the original iterator. */
587 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
588 do { \
589 if (CACHE) \
590 bidi_unshelve_cache (CACHE, 1); \
591 ITCOPY = ITORIG; \
592 CACHE = bidi_shelve_cache (); \
593 } while (0)
595 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
596 do { \
597 if (pITORIG != pITCOPY) \
598 *(pITORIG) = *(pITCOPY); \
599 bidi_unshelve_cache (CACHE, 0); \
600 CACHE = NULL; \
601 } while (0)
603 #ifdef GLYPH_DEBUG
605 /* Non-zero means print traces of redisplay if compiled with
606 GLYPH_DEBUG defined. */
608 int trace_redisplay_p;
610 #endif /* GLYPH_DEBUG */
612 #ifdef DEBUG_TRACE_MOVE
613 /* Non-zero means trace with TRACE_MOVE to stderr. */
614 int trace_move;
616 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
617 #else
618 #define TRACE_MOVE(x) (void) 0
619 #endif
621 static Lisp_Object Qauto_hscroll_mode;
623 /* Buffer being redisplayed -- for redisplay_window_error. */
625 static struct buffer *displayed_buffer;
627 /* Value returned from text property handlers (see below). */
629 enum prop_handled
631 HANDLED_NORMALLY,
632 HANDLED_RECOMPUTE_PROPS,
633 HANDLED_OVERLAY_STRING_CONSUMED,
634 HANDLED_RETURN
637 /* A description of text properties that redisplay is interested
638 in. */
640 struct props
642 /* The name of the property. */
643 Lisp_Object *name;
645 /* A unique index for the property. */
646 enum prop_idx idx;
648 /* A handler function called to set up iterator IT from the property
649 at IT's current position. Value is used to steer handle_stop. */
650 enum prop_handled (*handler) (struct it *it);
653 static enum prop_handled handle_face_prop (struct it *);
654 static enum prop_handled handle_invisible_prop (struct it *);
655 static enum prop_handled handle_display_prop (struct it *);
656 static enum prop_handled handle_composition_prop (struct it *);
657 static enum prop_handled handle_overlay_change (struct it *);
658 static enum prop_handled handle_fontified_prop (struct it *);
660 /* Properties handled by iterators. */
662 static struct props it_props[] =
664 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
665 /* Handle `face' before `display' because some sub-properties of
666 `display' need to know the face. */
667 {&Qface, FACE_PROP_IDX, handle_face_prop},
668 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
669 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
670 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
671 {NULL, 0, NULL}
674 /* Value is the position described by X. If X is a marker, value is
675 the marker_position of X. Otherwise, value is X. */
677 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
679 /* Enumeration returned by some move_it_.* functions internally. */
681 enum move_it_result
683 /* Not used. Undefined value. */
684 MOVE_UNDEFINED,
686 /* Move ended at the requested buffer position or ZV. */
687 MOVE_POS_MATCH_OR_ZV,
689 /* Move ended at the requested X pixel position. */
690 MOVE_X_REACHED,
692 /* Move within a line ended at the end of a line that must be
693 continued. */
694 MOVE_LINE_CONTINUED,
696 /* Move within a line ended at the end of a line that would
697 be displayed truncated. */
698 MOVE_LINE_TRUNCATED,
700 /* Move within a line ended at a line end. */
701 MOVE_NEWLINE_OR_CR
704 /* This counter is used to clear the face cache every once in a while
705 in redisplay_internal. It is incremented for each redisplay.
706 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
707 cleared. */
709 #define CLEAR_FACE_CACHE_COUNT 500
710 static int clear_face_cache_count;
712 /* Similarly for the image cache. */
714 #ifdef HAVE_WINDOW_SYSTEM
715 #define CLEAR_IMAGE_CACHE_COUNT 101
716 static int clear_image_cache_count;
718 /* Null glyph slice */
719 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
720 #endif
722 /* True while redisplay_internal is in progress. */
724 bool redisplaying_p;
726 static Lisp_Object Qinhibit_free_realized_faces;
727 static Lisp_Object Qmode_line_default_help_echo;
729 /* If a string, XTread_socket generates an event to display that string.
730 (The display is done in read_char.) */
732 Lisp_Object help_echo_string;
733 Lisp_Object help_echo_window;
734 Lisp_Object help_echo_object;
735 ptrdiff_t help_echo_pos;
737 /* Temporary variable for XTread_socket. */
739 Lisp_Object previous_help_echo_string;
741 /* Platform-independent portion of hourglass implementation. */
743 #ifdef HAVE_WINDOW_SYSTEM
745 /* Non-zero means an hourglass cursor is currently shown. */
746 int hourglass_shown_p;
748 /* If non-null, an asynchronous timer that, when it expires, displays
749 an hourglass cursor on all frames. */
750 struct atimer *hourglass_atimer;
752 #endif /* HAVE_WINDOW_SYSTEM */
754 /* Name of the face used to display glyphless characters. */
755 Lisp_Object Qglyphless_char;
757 /* Symbol for the purpose of Vglyphless_char_display. */
758 static Lisp_Object Qglyphless_char_display;
760 /* Method symbols for Vglyphless_char_display. */
761 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
763 /* Default number of seconds to wait before displaying an hourglass
764 cursor. */
765 #define DEFAULT_HOURGLASS_DELAY 1
767 #ifdef HAVE_WINDOW_SYSTEM
769 /* Default pixel width of `thin-space' display method. */
770 #define THIN_SPACE_WIDTH 1
772 #endif /* HAVE_WINDOW_SYSTEM */
774 /* Function prototypes. */
776 static void setup_for_ellipsis (struct it *, int);
777 static void set_iterator_to_next (struct it *, int);
778 static void mark_window_display_accurate_1 (struct window *, int);
779 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
780 static int display_prop_string_p (Lisp_Object, Lisp_Object);
781 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
782 static int cursor_row_p (struct glyph_row *);
783 static int redisplay_mode_lines (Lisp_Object, int);
784 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
786 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
788 static void handle_line_prefix (struct it *);
790 static void pint2str (char *, int, ptrdiff_t);
791 static void pint2hrstr (char *, int, ptrdiff_t);
792 static struct text_pos run_window_scroll_functions (Lisp_Object,
793 struct text_pos);
794 static int text_outside_line_unchanged_p (struct window *,
795 ptrdiff_t, ptrdiff_t);
796 static void store_mode_line_noprop_char (char);
797 static int store_mode_line_noprop (const char *, int, int);
798 static void handle_stop (struct it *);
799 static void handle_stop_backwards (struct it *, ptrdiff_t);
800 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
801 static void ensure_echo_area_buffers (void);
802 static void unwind_with_echo_area_buffer (Lisp_Object);
803 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
804 static int with_echo_area_buffer (struct window *, int,
805 int (*) (ptrdiff_t, Lisp_Object),
806 ptrdiff_t, Lisp_Object);
807 static void clear_garbaged_frames (void);
808 static int current_message_1 (ptrdiff_t, Lisp_Object);
809 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
810 static void set_message (Lisp_Object);
811 static int set_message_1 (ptrdiff_t, Lisp_Object);
812 static int display_echo_area (struct window *);
813 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
814 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
815 static void unwind_redisplay (void);
816 static int string_char_and_length (const unsigned char *, int *);
817 static struct text_pos display_prop_end (struct it *, Lisp_Object,
818 struct text_pos);
819 static int compute_window_start_on_continuation_line (struct window *);
820 static void insert_left_trunc_glyphs (struct it *);
821 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
822 Lisp_Object);
823 static void extend_face_to_end_of_line (struct it *);
824 static int append_space_for_newline (struct it *, int);
825 static int cursor_row_fully_visible_p (struct window *, int, int);
826 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
827 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
828 static int trailing_whitespace_p (ptrdiff_t);
829 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
830 static void push_it (struct it *, struct text_pos *);
831 static void iterate_out_of_display_property (struct it *);
832 static void pop_it (struct it *);
833 static void sync_frame_with_window_matrix_rows (struct window *);
834 static void redisplay_internal (void);
835 static int echo_area_display (int);
836 static void redisplay_windows (Lisp_Object);
837 static void redisplay_window (Lisp_Object, int);
838 static Lisp_Object redisplay_window_error (Lisp_Object);
839 static Lisp_Object redisplay_window_0 (Lisp_Object);
840 static Lisp_Object redisplay_window_1 (Lisp_Object);
841 static int set_cursor_from_row (struct window *, struct glyph_row *,
842 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
843 int, int);
844 static int update_menu_bar (struct frame *, int, int);
845 static int try_window_reusing_current_matrix (struct window *);
846 static int try_window_id (struct window *);
847 static int display_line (struct it *);
848 static int display_mode_lines (struct window *);
849 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
850 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
851 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
852 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
853 static void display_menu_bar (struct window *);
854 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
855 ptrdiff_t *);
856 static int display_string (const char *, Lisp_Object, Lisp_Object,
857 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
858 static void compute_line_metrics (struct it *);
859 static void run_redisplay_end_trigger_hook (struct it *);
860 static int get_overlay_strings (struct it *, ptrdiff_t);
861 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
862 static void next_overlay_string (struct it *);
863 static void reseat (struct it *, struct text_pos, int);
864 static void reseat_1 (struct it *, struct text_pos, int);
865 static void back_to_previous_visible_line_start (struct it *);
866 static void reseat_at_next_visible_line_start (struct it *, int);
867 static int next_element_from_ellipsis (struct it *);
868 static int next_element_from_display_vector (struct it *);
869 static int next_element_from_string (struct it *);
870 static int next_element_from_c_string (struct it *);
871 static int next_element_from_buffer (struct it *);
872 static int next_element_from_composition (struct it *);
873 static int next_element_from_image (struct it *);
874 static int next_element_from_stretch (struct it *);
875 static void load_overlay_strings (struct it *, ptrdiff_t);
876 static int init_from_display_pos (struct it *, struct window *,
877 struct display_pos *);
878 static void reseat_to_string (struct it *, const char *,
879 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
880 static int get_next_display_element (struct it *);
881 static enum move_it_result
882 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
883 enum move_operation_enum);
884 static void get_visually_first_element (struct it *);
885 static void init_to_row_start (struct it *, struct window *,
886 struct glyph_row *);
887 static int init_to_row_end (struct it *, struct window *,
888 struct glyph_row *);
889 static void back_to_previous_line_start (struct it *);
890 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
891 static struct text_pos string_pos_nchars_ahead (struct text_pos,
892 Lisp_Object, ptrdiff_t);
893 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
894 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
895 static ptrdiff_t number_of_chars (const char *, bool);
896 static void compute_stop_pos (struct it *);
897 static void compute_string_pos (struct text_pos *, struct text_pos,
898 Lisp_Object);
899 static int face_before_or_after_it_pos (struct it *, int);
900 static ptrdiff_t next_overlay_change (ptrdiff_t);
901 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
902 Lisp_Object, struct text_pos *, ptrdiff_t, int);
903 static int handle_single_display_spec (struct it *, Lisp_Object,
904 Lisp_Object, Lisp_Object,
905 struct text_pos *, ptrdiff_t, int, int);
906 static int underlying_face_id (struct it *);
907 static int in_ellipses_for_invisible_text_p (struct display_pos *,
908 struct window *);
910 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
911 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
913 #ifdef HAVE_WINDOW_SYSTEM
915 static void x_consider_frame_title (Lisp_Object);
916 static int tool_bar_lines_needed (struct frame *, int *);
917 static void update_tool_bar (struct frame *, int);
918 static void build_desired_tool_bar_string (struct frame *f);
919 static int redisplay_tool_bar (struct frame *);
920 static void display_tool_bar_line (struct it *, int);
921 static void notice_overwritten_cursor (struct window *,
922 enum glyph_row_area,
923 int, int, int, int);
924 static void append_stretch_glyph (struct it *, Lisp_Object,
925 int, int, int);
928 #endif /* HAVE_WINDOW_SYSTEM */
930 static void produce_special_glyphs (struct it *, enum display_element_type);
931 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
932 static int coords_in_mouse_face_p (struct window *, int, int);
936 /***********************************************************************
937 Window display dimensions
938 ***********************************************************************/
940 /* Return the bottom boundary y-position for text lines in window W.
941 This is the first y position at which a line cannot start.
942 It is relative to the top of the window.
944 This is the height of W minus the height of a mode line, if any. */
947 window_text_bottom_y (struct window *w)
949 int height = WINDOW_TOTAL_HEIGHT (w);
951 if (WINDOW_WANTS_MODELINE_P (w))
952 height -= CURRENT_MODE_LINE_HEIGHT (w);
953 return height;
956 /* Return the pixel width of display area AREA of window W.
957 ANY_AREA means return the total width of W, not including
958 fringes to the left and right of the window. */
961 window_box_width (struct window *w, enum glyph_row_area area)
963 int cols = w->total_cols;
964 int pixels = 0;
966 if (!w->pseudo_window_p)
968 cols -= WINDOW_SCROLL_BAR_COLS (w);
970 if (area == TEXT_AREA)
972 cols -= max (0, w->left_margin_cols);
973 cols -= max (0, w->right_margin_cols);
974 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
976 else if (area == LEFT_MARGIN_AREA)
978 cols = max (0, w->left_margin_cols);
979 pixels = 0;
981 else if (area == RIGHT_MARGIN_AREA)
983 cols = max (0, w->right_margin_cols);
984 pixels = 0;
988 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
992 /* Return the pixel height of the display area of window W, not
993 including mode lines of W, if any. */
996 window_box_height (struct window *w)
998 struct frame *f = XFRAME (w->frame);
999 int height = WINDOW_TOTAL_HEIGHT (w);
1001 eassert (height >= 0);
1003 /* Note: the code below that determines the mode-line/header-line
1004 height is essentially the same as that contained in the macro
1005 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1006 the appropriate glyph row has its `mode_line_p' flag set,
1007 and if it doesn't, uses estimate_mode_line_height instead. */
1009 if (WINDOW_WANTS_MODELINE_P (w))
1011 struct glyph_row *ml_row
1012 = (w->current_matrix && w->current_matrix->rows
1013 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1014 : 0);
1015 if (ml_row && ml_row->mode_line_p)
1016 height -= ml_row->height;
1017 else
1018 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1021 if (WINDOW_WANTS_HEADER_LINE_P (w))
1023 struct glyph_row *hl_row
1024 = (w->current_matrix && w->current_matrix->rows
1025 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1026 : 0);
1027 if (hl_row && hl_row->mode_line_p)
1028 height -= hl_row->height;
1029 else
1030 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1033 /* With a very small font and a mode-line that's taller than
1034 default, we might end up with a negative height. */
1035 return max (0, height);
1038 /* Return the window-relative coordinate of the left edge of display
1039 area AREA of window W. ANY_AREA means return the left edge of the
1040 whole window, to the right of the left fringe of W. */
1043 window_box_left_offset (struct window *w, enum glyph_row_area area)
1045 int x;
1047 if (w->pseudo_window_p)
1048 return 0;
1050 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1052 if (area == TEXT_AREA)
1053 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1054 + window_box_width (w, LEFT_MARGIN_AREA));
1055 else if (area == RIGHT_MARGIN_AREA)
1056 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1057 + window_box_width (w, LEFT_MARGIN_AREA)
1058 + window_box_width (w, TEXT_AREA)
1059 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1061 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1062 else if (area == LEFT_MARGIN_AREA
1063 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1064 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1066 return x;
1070 /* Return the window-relative coordinate of the right edge of display
1071 area AREA of window W. ANY_AREA means return the right edge of the
1072 whole window, to the left of the right fringe of W. */
1075 window_box_right_offset (struct window *w, enum glyph_row_area area)
1077 return window_box_left_offset (w, area) + window_box_width (w, area);
1080 /* Return the frame-relative coordinate of the left edge of display
1081 area AREA of window W. ANY_AREA means return the left edge of the
1082 whole window, to the right of the left fringe of W. */
1085 window_box_left (struct window *w, enum glyph_row_area area)
1087 struct frame *f = XFRAME (w->frame);
1088 int x;
1090 if (w->pseudo_window_p)
1091 return FRAME_INTERNAL_BORDER_WIDTH (f);
1093 x = (WINDOW_LEFT_EDGE_X (w)
1094 + window_box_left_offset (w, area));
1096 return x;
1100 /* Return the frame-relative coordinate of the right edge of display
1101 area AREA of window W. ANY_AREA means return the right edge of the
1102 whole window, to the left of the right fringe of W. */
1105 window_box_right (struct window *w, enum glyph_row_area area)
1107 return window_box_left (w, area) + window_box_width (w, area);
1110 /* Get the bounding box of the display area AREA of window W, without
1111 mode lines, in frame-relative coordinates. ANY_AREA means the
1112 whole window, not including the left and right fringes of
1113 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1114 coordinates of the upper-left corner of the box. Return in
1115 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1117 void
1118 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1119 int *box_y, int *box_width, int *box_height)
1121 if (box_width)
1122 *box_width = window_box_width (w, area);
1123 if (box_height)
1124 *box_height = window_box_height (w);
1125 if (box_x)
1126 *box_x = window_box_left (w, area);
1127 if (box_y)
1129 *box_y = WINDOW_TOP_EDGE_Y (w);
1130 if (WINDOW_WANTS_HEADER_LINE_P (w))
1131 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1135 #ifdef HAVE_WINDOW_SYSTEM
1137 /* Get the bounding box of the display area AREA of window W, without
1138 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1139 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1140 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1141 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1142 box. */
1144 static void
1145 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1146 int *bottom_right_x, int *bottom_right_y)
1148 window_box (w, ANY_AREA, top_left_x, top_left_y,
1149 bottom_right_x, bottom_right_y);
1150 *bottom_right_x += *top_left_x;
1151 *bottom_right_y += *top_left_y;
1154 #endif /* HAVE_WINDOW_SYSTEM */
1156 /***********************************************************************
1157 Utilities
1158 ***********************************************************************/
1160 /* Return the bottom y-position of the line the iterator IT is in.
1161 This can modify IT's settings. */
1164 line_bottom_y (struct it *it)
1166 int line_height = it->max_ascent + it->max_descent;
1167 int line_top_y = it->current_y;
1169 if (line_height == 0)
1171 if (last_height)
1172 line_height = last_height;
1173 else if (IT_CHARPOS (*it) < ZV)
1175 move_it_by_lines (it, 1);
1176 line_height = (it->max_ascent || it->max_descent
1177 ? it->max_ascent + it->max_descent
1178 : last_height);
1180 else
1182 struct glyph_row *row = it->glyph_row;
1184 /* Use the default character height. */
1185 it->glyph_row = NULL;
1186 it->what = IT_CHARACTER;
1187 it->c = ' ';
1188 it->len = 1;
1189 PRODUCE_GLYPHS (it);
1190 line_height = it->ascent + it->descent;
1191 it->glyph_row = row;
1195 return line_top_y + line_height;
1198 DEFUN ("line-pixel-height", Fline_pixel_height,
1199 Sline_pixel_height, 0, 0, 0,
1200 doc: /* Return height in pixels of text line in the selected window.
1202 Value is the height in pixels of the line at point. */)
1203 (void)
1205 struct it it;
1206 struct text_pos pt;
1207 struct window *w = XWINDOW (selected_window);
1209 SET_TEXT_POS (pt, PT, PT_BYTE);
1210 start_display (&it, w, pt);
1211 it.vpos = it.current_y = 0;
1212 last_height = 0;
1213 return make_number (line_bottom_y (&it));
1216 /* Return the default pixel height of text lines in window W. The
1217 value is the canonical height of the W frame's default font, plus
1218 any extra space required by the line-spacing variable or frame
1219 parameter.
1221 Implementation note: this ignores any line-spacing text properties
1222 put on the newline characters. This is because those properties
1223 only affect the _screen_ line ending in the newline (i.e., in a
1224 continued line, only the last screen line will be affected), which
1225 means only a small number of lines in a buffer can ever use this
1226 feature. Since this function is used to compute the default pixel
1227 equivalent of text lines in a window, we can safely ignore those
1228 few lines. For the same reasons, we ignore the line-height
1229 properties. */
1231 default_line_pixel_height (struct window *w)
1233 struct frame *f = WINDOW_XFRAME (w);
1234 int height = FRAME_LINE_HEIGHT (f);
1236 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1238 struct buffer *b = XBUFFER (w->contents);
1239 Lisp_Object val = BVAR (b, extra_line_spacing);
1241 if (NILP (val))
1242 val = BVAR (&buffer_defaults, extra_line_spacing);
1243 if (!NILP (val))
1245 if (RANGED_INTEGERP (0, val, INT_MAX))
1246 height += XFASTINT (val);
1247 else if (FLOATP (val))
1249 int addon = XFLOAT_DATA (val) * height + 0.5;
1251 if (addon >= 0)
1252 height += addon;
1255 else
1256 height += f->extra_line_spacing;
1259 return height;
1262 /* Subroutine of pos_visible_p below. Extracts a display string, if
1263 any, from the display spec given as its argument. */
1264 static Lisp_Object
1265 string_from_display_spec (Lisp_Object spec)
1267 if (CONSP (spec))
1269 while (CONSP (spec))
1271 if (STRINGP (XCAR (spec)))
1272 return XCAR (spec);
1273 spec = XCDR (spec);
1276 else if (VECTORP (spec))
1278 ptrdiff_t i;
1280 for (i = 0; i < ASIZE (spec); i++)
1282 if (STRINGP (AREF (spec, i)))
1283 return AREF (spec, i);
1285 return Qnil;
1288 return spec;
1292 /* Limit insanely large values of W->hscroll on frame F to the largest
1293 value that will still prevent first_visible_x and last_visible_x of
1294 'struct it' from overflowing an int. */
1295 static int
1296 window_hscroll_limited (struct window *w, struct frame *f)
1298 ptrdiff_t window_hscroll = w->hscroll;
1299 int window_text_width = window_box_width (w, TEXT_AREA);
1300 int colwidth = FRAME_COLUMN_WIDTH (f);
1302 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1303 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1305 return window_hscroll;
1308 /* Return 1 if position CHARPOS is visible in window W.
1309 CHARPOS < 0 means return info about WINDOW_END position.
1310 If visible, set *X and *Y to pixel coordinates of top left corner.
1311 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1312 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1315 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1316 int *rtop, int *rbot, int *rowh, int *vpos)
1318 struct it it;
1319 void *itdata = bidi_shelve_cache ();
1320 struct text_pos top;
1321 int visible_p = 0;
1322 struct buffer *old_buffer = NULL;
1324 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1325 return visible_p;
1327 if (XBUFFER (w->contents) != current_buffer)
1329 old_buffer = current_buffer;
1330 set_buffer_internal_1 (XBUFFER (w->contents));
1333 SET_TEXT_POS_FROM_MARKER (top, w->start);
1334 /* Scrolling a minibuffer window via scroll bar when the echo area
1335 shows long text sometimes resets the minibuffer contents behind
1336 our backs. */
1337 if (CHARPOS (top) > ZV)
1338 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1340 /* Compute exact mode line heights. */
1341 if (WINDOW_WANTS_MODELINE_P (w))
1342 w->mode_line_height
1343 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1344 BVAR (current_buffer, mode_line_format));
1346 if (WINDOW_WANTS_HEADER_LINE_P (w))
1347 w->header_line_height
1348 = display_mode_line (w, HEADER_LINE_FACE_ID,
1349 BVAR (current_buffer, header_line_format));
1351 start_display (&it, w, top);
1352 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1353 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1355 if (charpos >= 0
1356 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1357 && IT_CHARPOS (it) >= charpos)
1358 /* When scanning backwards under bidi iteration, move_it_to
1359 stops at or _before_ CHARPOS, because it stops at or to
1360 the _right_ of the character at CHARPOS. */
1361 || (it.bidi_p && it.bidi_it.scan_dir == -1
1362 && IT_CHARPOS (it) <= charpos)))
1364 /* We have reached CHARPOS, or passed it. How the call to
1365 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1366 or covered by a display property, move_it_to stops at the end
1367 of the invisible text, to the right of CHARPOS. (ii) If
1368 CHARPOS is in a display vector, move_it_to stops on its last
1369 glyph. */
1370 int top_x = it.current_x;
1371 int top_y = it.current_y;
1372 /* Calling line_bottom_y may change it.method, it.position, etc. */
1373 enum it_method it_method = it.method;
1374 int bottom_y = (last_height = 0, line_bottom_y (&it));
1375 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1377 if (top_y < window_top_y)
1378 visible_p = bottom_y > window_top_y;
1379 else if (top_y < it.last_visible_y)
1380 visible_p = 1;
1381 if (bottom_y >= it.last_visible_y
1382 && it.bidi_p && it.bidi_it.scan_dir == -1
1383 && IT_CHARPOS (it) < charpos)
1385 /* When the last line of the window is scanned backwards
1386 under bidi iteration, we could be duped into thinking
1387 that we have passed CHARPOS, when in fact move_it_to
1388 simply stopped short of CHARPOS because it reached
1389 last_visible_y. To see if that's what happened, we call
1390 move_it_to again with a slightly larger vertical limit,
1391 and see if it actually moved vertically; if it did, we
1392 didn't really reach CHARPOS, which is beyond window end. */
1393 struct it save_it = it;
1394 /* Why 10? because we don't know how many canonical lines
1395 will the height of the next line(s) be. So we guess. */
1396 int ten_more_lines = 10 * default_line_pixel_height (w);
1398 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1399 MOVE_TO_POS | MOVE_TO_Y);
1400 if (it.current_y > top_y)
1401 visible_p = 0;
1403 it = save_it;
1405 if (visible_p)
1407 if (it_method == GET_FROM_DISPLAY_VECTOR)
1409 /* We stopped on the last glyph of a display vector.
1410 Try and recompute. Hack alert! */
1411 if (charpos < 2 || top.charpos >= charpos)
1412 top_x = it.glyph_row->x;
1413 else
1415 struct it it2, it2_prev;
1416 /* The idea is to get to the previous buffer
1417 position, consume the character there, and use
1418 the pixel coordinates we get after that. But if
1419 the previous buffer position is also displayed
1420 from a display vector, we need to consume all of
1421 the glyphs from that display vector. */
1422 start_display (&it2, w, top);
1423 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1424 /* If we didn't get to CHARPOS - 1, there's some
1425 replacing display property at that position, and
1426 we stopped after it. That is exactly the place
1427 whose coordinates we want. */
1428 if (IT_CHARPOS (it2) != charpos - 1)
1429 it2_prev = it2;
1430 else
1432 /* Iterate until we get out of the display
1433 vector that displays the character at
1434 CHARPOS - 1. */
1435 do {
1436 get_next_display_element (&it2);
1437 PRODUCE_GLYPHS (&it2);
1438 it2_prev = it2;
1439 set_iterator_to_next (&it2, 1);
1440 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1441 && IT_CHARPOS (it2) < charpos);
1443 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1444 || it2_prev.current_x > it2_prev.last_visible_x)
1445 top_x = it.glyph_row->x;
1446 else
1448 top_x = it2_prev.current_x;
1449 top_y = it2_prev.current_y;
1453 else if (IT_CHARPOS (it) != charpos)
1455 Lisp_Object cpos = make_number (charpos);
1456 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1457 Lisp_Object string = string_from_display_spec (spec);
1458 struct text_pos tpos;
1459 int replacing_spec_p;
1460 bool newline_in_string
1461 = (STRINGP (string)
1462 && memchr (SDATA (string), '\n', SBYTES (string)));
1464 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1465 replacing_spec_p
1466 = (!NILP (spec)
1467 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1468 charpos, FRAME_WINDOW_P (it.f)));
1469 /* The tricky code below is needed because there's a
1470 discrepancy between move_it_to and how we set cursor
1471 when PT is at the beginning of a portion of text
1472 covered by a display property or an overlay with a
1473 display property, or the display line ends in a
1474 newline from a display string. move_it_to will stop
1475 _after_ such display strings, whereas
1476 set_cursor_from_row conspires with cursor_row_p to
1477 place the cursor on the first glyph produced from the
1478 display string. */
1480 /* We have overshoot PT because it is covered by a
1481 display property that replaces the text it covers.
1482 If the string includes embedded newlines, we are also
1483 in the wrong display line. Backtrack to the correct
1484 line, where the display property begins. */
1485 if (replacing_spec_p)
1487 Lisp_Object startpos, endpos;
1488 EMACS_INT start, end;
1489 struct it it3;
1490 int it3_moved;
1492 /* Find the first and the last buffer positions
1493 covered by the display string. */
1494 endpos =
1495 Fnext_single_char_property_change (cpos, Qdisplay,
1496 Qnil, Qnil);
1497 startpos =
1498 Fprevious_single_char_property_change (endpos, Qdisplay,
1499 Qnil, Qnil);
1500 start = XFASTINT (startpos);
1501 end = XFASTINT (endpos);
1502 /* Move to the last buffer position before the
1503 display property. */
1504 start_display (&it3, w, top);
1505 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1506 /* Move forward one more line if the position before
1507 the display string is a newline or if it is the
1508 rightmost character on a line that is
1509 continued or word-wrapped. */
1510 if (it3.method == GET_FROM_BUFFER
1511 && (it3.c == '\n'
1512 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1513 move_it_by_lines (&it3, 1);
1514 else if (move_it_in_display_line_to (&it3, -1,
1515 it3.current_x
1516 + it3.pixel_width,
1517 MOVE_TO_X)
1518 == MOVE_LINE_CONTINUED)
1520 move_it_by_lines (&it3, 1);
1521 /* When we are under word-wrap, the #$@%!
1522 move_it_by_lines moves 2 lines, so we need to
1523 fix that up. */
1524 if (it3.line_wrap == WORD_WRAP)
1525 move_it_by_lines (&it3, -1);
1528 /* Record the vertical coordinate of the display
1529 line where we wound up. */
1530 top_y = it3.current_y;
1531 if (it3.bidi_p)
1533 /* When characters are reordered for display,
1534 the character displayed to the left of the
1535 display string could be _after_ the display
1536 property in the logical order. Use the
1537 smallest vertical position of these two. */
1538 start_display (&it3, w, top);
1539 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1540 if (it3.current_y < top_y)
1541 top_y = it3.current_y;
1543 /* Move from the top of the window to the beginning
1544 of the display line where the display string
1545 begins. */
1546 start_display (&it3, w, top);
1547 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1548 /* If it3_moved stays zero after the 'while' loop
1549 below, that means we already were at a newline
1550 before the loop (e.g., the display string begins
1551 with a newline), so we don't need to (and cannot)
1552 inspect the glyphs of it3.glyph_row, because
1553 PRODUCE_GLYPHS will not produce anything for a
1554 newline, and thus it3.glyph_row stays at its
1555 stale content it got at top of the window. */
1556 it3_moved = 0;
1557 /* Finally, advance the iterator until we hit the
1558 first display element whose character position is
1559 CHARPOS, or until the first newline from the
1560 display string, which signals the end of the
1561 display line. */
1562 while (get_next_display_element (&it3))
1564 PRODUCE_GLYPHS (&it3);
1565 if (IT_CHARPOS (it3) == charpos
1566 || ITERATOR_AT_END_OF_LINE_P (&it3))
1567 break;
1568 it3_moved = 1;
1569 set_iterator_to_next (&it3, 0);
1571 top_x = it3.current_x - it3.pixel_width;
1572 /* Normally, we would exit the above loop because we
1573 found the display element whose character
1574 position is CHARPOS. For the contingency that we
1575 didn't, and stopped at the first newline from the
1576 display string, move back over the glyphs
1577 produced from the string, until we find the
1578 rightmost glyph not from the string. */
1579 if (it3_moved
1580 && newline_in_string
1581 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1583 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1584 + it3.glyph_row->used[TEXT_AREA];
1586 while (EQ ((g - 1)->object, string))
1588 --g;
1589 top_x -= g->pixel_width;
1591 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1592 + it3.glyph_row->used[TEXT_AREA]);
1597 *x = top_x;
1598 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1599 *rtop = max (0, window_top_y - top_y);
1600 *rbot = max (0, bottom_y - it.last_visible_y);
1601 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1602 - max (top_y, window_top_y)));
1603 *vpos = it.vpos;
1606 else
1608 /* We were asked to provide info about WINDOW_END. */
1609 struct it it2;
1610 void *it2data = NULL;
1612 SAVE_IT (it2, it, it2data);
1613 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1614 move_it_by_lines (&it, 1);
1615 if (charpos < IT_CHARPOS (it)
1616 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1618 visible_p = 1;
1619 RESTORE_IT (&it2, &it2, it2data);
1620 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1621 *x = it2.current_x;
1622 *y = it2.current_y + it2.max_ascent - it2.ascent;
1623 *rtop = max (0, -it2.current_y);
1624 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1625 - it.last_visible_y));
1626 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1627 it.last_visible_y)
1628 - max (it2.current_y,
1629 WINDOW_HEADER_LINE_HEIGHT (w))));
1630 *vpos = it2.vpos;
1632 else
1633 bidi_unshelve_cache (it2data, 1);
1635 bidi_unshelve_cache (itdata, 0);
1637 if (old_buffer)
1638 set_buffer_internal_1 (old_buffer);
1640 if (visible_p && w->hscroll > 0)
1641 *x -=
1642 window_hscroll_limited (w, WINDOW_XFRAME (w))
1643 * WINDOW_FRAME_COLUMN_WIDTH (w);
1645 #if 0
1646 /* Debugging code. */
1647 if (visible_p)
1648 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1649 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1650 else
1651 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1652 #endif
1654 return visible_p;
1658 /* Return the next character from STR. Return in *LEN the length of
1659 the character. This is like STRING_CHAR_AND_LENGTH but never
1660 returns an invalid character. If we find one, we return a `?', but
1661 with the length of the invalid character. */
1663 static int
1664 string_char_and_length (const unsigned char *str, int *len)
1666 int c;
1668 c = STRING_CHAR_AND_LENGTH (str, *len);
1669 if (!CHAR_VALID_P (c))
1670 /* We may not change the length here because other places in Emacs
1671 don't use this function, i.e. they silently accept invalid
1672 characters. */
1673 c = '?';
1675 return c;
1680 /* Given a position POS containing a valid character and byte position
1681 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1683 static struct text_pos
1684 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1686 eassert (STRINGP (string) && nchars >= 0);
1688 if (STRING_MULTIBYTE (string))
1690 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1691 int len;
1693 while (nchars--)
1695 string_char_and_length (p, &len);
1696 p += len;
1697 CHARPOS (pos) += 1;
1698 BYTEPOS (pos) += len;
1701 else
1702 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1704 return pos;
1708 /* Value is the text position, i.e. character and byte position,
1709 for character position CHARPOS in STRING. */
1711 static struct text_pos
1712 string_pos (ptrdiff_t charpos, Lisp_Object string)
1714 struct text_pos pos;
1715 eassert (STRINGP (string));
1716 eassert (charpos >= 0);
1717 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1718 return pos;
1722 /* Value is a text position, i.e. character and byte position, for
1723 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1724 means recognize multibyte characters. */
1726 static struct text_pos
1727 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1729 struct text_pos pos;
1731 eassert (s != NULL);
1732 eassert (charpos >= 0);
1734 if (multibyte_p)
1736 int len;
1738 SET_TEXT_POS (pos, 0, 0);
1739 while (charpos--)
1741 string_char_and_length ((const unsigned char *) s, &len);
1742 s += len;
1743 CHARPOS (pos) += 1;
1744 BYTEPOS (pos) += len;
1747 else
1748 SET_TEXT_POS (pos, charpos, charpos);
1750 return pos;
1754 /* Value is the number of characters in C string S. MULTIBYTE_P
1755 non-zero means recognize multibyte characters. */
1757 static ptrdiff_t
1758 number_of_chars (const char *s, bool multibyte_p)
1760 ptrdiff_t nchars;
1762 if (multibyte_p)
1764 ptrdiff_t rest = strlen (s);
1765 int len;
1766 const unsigned char *p = (const unsigned char *) s;
1768 for (nchars = 0; rest > 0; ++nchars)
1770 string_char_and_length (p, &len);
1771 rest -= len, p += len;
1774 else
1775 nchars = strlen (s);
1777 return nchars;
1781 /* Compute byte position NEWPOS->bytepos corresponding to
1782 NEWPOS->charpos. POS is a known position in string STRING.
1783 NEWPOS->charpos must be >= POS.charpos. */
1785 static void
1786 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1788 eassert (STRINGP (string));
1789 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1791 if (STRING_MULTIBYTE (string))
1792 *newpos = string_pos_nchars_ahead (pos, string,
1793 CHARPOS (*newpos) - CHARPOS (pos));
1794 else
1795 BYTEPOS (*newpos) = CHARPOS (*newpos);
1798 /* EXPORT:
1799 Return an estimation of the pixel height of mode or header lines on
1800 frame F. FACE_ID specifies what line's height to estimate. */
1803 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1805 #ifdef HAVE_WINDOW_SYSTEM
1806 if (FRAME_WINDOW_P (f))
1808 int height = FONT_HEIGHT (FRAME_FONT (f));
1810 /* This function is called so early when Emacs starts that the face
1811 cache and mode line face are not yet initialized. */
1812 if (FRAME_FACE_CACHE (f))
1814 struct face *face = FACE_FROM_ID (f, face_id);
1815 if (face)
1817 if (face->font)
1818 height = FONT_HEIGHT (face->font);
1819 if (face->box_line_width > 0)
1820 height += 2 * face->box_line_width;
1824 return height;
1826 #endif
1828 return 1;
1831 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1832 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1833 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1834 not force the value into range. */
1836 void
1837 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1838 int *x, int *y, NativeRectangle *bounds, int noclip)
1841 #ifdef HAVE_WINDOW_SYSTEM
1842 if (FRAME_WINDOW_P (f))
1844 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1845 even for negative values. */
1846 if (pix_x < 0)
1847 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1848 if (pix_y < 0)
1849 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1851 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1852 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1854 if (bounds)
1855 STORE_NATIVE_RECT (*bounds,
1856 FRAME_COL_TO_PIXEL_X (f, pix_x),
1857 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1858 FRAME_COLUMN_WIDTH (f) - 1,
1859 FRAME_LINE_HEIGHT (f) - 1);
1861 if (!noclip)
1863 if (pix_x < 0)
1864 pix_x = 0;
1865 else if (pix_x > FRAME_TOTAL_COLS (f))
1866 pix_x = FRAME_TOTAL_COLS (f);
1868 if (pix_y < 0)
1869 pix_y = 0;
1870 else if (pix_y > FRAME_LINES (f))
1871 pix_y = FRAME_LINES (f);
1874 #endif
1876 *x = pix_x;
1877 *y = pix_y;
1881 /* Find the glyph under window-relative coordinates X/Y in window W.
1882 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1883 strings. Return in *HPOS and *VPOS the row and column number of
1884 the glyph found. Return in *AREA the glyph area containing X.
1885 Value is a pointer to the glyph found or null if X/Y is not on
1886 text, or we can't tell because W's current matrix is not up to
1887 date. */
1889 static
1890 struct glyph *
1891 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1892 int *dx, int *dy, int *area)
1894 struct glyph *glyph, *end;
1895 struct glyph_row *row = NULL;
1896 int x0, i;
1898 /* Find row containing Y. Give up if some row is not enabled. */
1899 for (i = 0; i < w->current_matrix->nrows; ++i)
1901 row = MATRIX_ROW (w->current_matrix, i);
1902 if (!row->enabled_p)
1903 return NULL;
1904 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1905 break;
1908 *vpos = i;
1909 *hpos = 0;
1911 /* Give up if Y is not in the window. */
1912 if (i == w->current_matrix->nrows)
1913 return NULL;
1915 /* Get the glyph area containing X. */
1916 if (w->pseudo_window_p)
1918 *area = TEXT_AREA;
1919 x0 = 0;
1921 else
1923 if (x < window_box_left_offset (w, TEXT_AREA))
1925 *area = LEFT_MARGIN_AREA;
1926 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1928 else if (x < window_box_right_offset (w, TEXT_AREA))
1930 *area = TEXT_AREA;
1931 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1933 else
1935 *area = RIGHT_MARGIN_AREA;
1936 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1940 /* Find glyph containing X. */
1941 glyph = row->glyphs[*area];
1942 end = glyph + row->used[*area];
1943 x -= x0;
1944 while (glyph < end && x >= glyph->pixel_width)
1946 x -= glyph->pixel_width;
1947 ++glyph;
1950 if (glyph == end)
1951 return NULL;
1953 if (dx)
1955 *dx = x;
1956 *dy = y - (row->y + row->ascent - glyph->ascent);
1959 *hpos = glyph - row->glyphs[*area];
1960 return glyph;
1963 /* Convert frame-relative x/y to coordinates relative to window W.
1964 Takes pseudo-windows into account. */
1966 static void
1967 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1969 if (w->pseudo_window_p)
1971 /* A pseudo-window is always full-width, and starts at the
1972 left edge of the frame, plus a frame border. */
1973 struct frame *f = XFRAME (w->frame);
1974 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1975 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1977 else
1979 *x -= WINDOW_LEFT_EDGE_X (w);
1980 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1984 #ifdef HAVE_WINDOW_SYSTEM
1986 /* EXPORT:
1987 Return in RECTS[] at most N clipping rectangles for glyph string S.
1988 Return the number of stored rectangles. */
1991 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1993 XRectangle r;
1995 if (n <= 0)
1996 return 0;
1998 if (s->row->full_width_p)
2000 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2001 r.x = WINDOW_LEFT_EDGE_X (s->w);
2002 r.width = WINDOW_TOTAL_WIDTH (s->w);
2004 /* Unless displaying a mode or menu bar line, which are always
2005 fully visible, clip to the visible part of the row. */
2006 if (s->w->pseudo_window_p)
2007 r.height = s->row->visible_height;
2008 else
2009 r.height = s->height;
2011 else
2013 /* This is a text line that may be partially visible. */
2014 r.x = window_box_left (s->w, s->area);
2015 r.width = window_box_width (s->w, s->area);
2016 r.height = s->row->visible_height;
2019 if (s->clip_head)
2020 if (r.x < s->clip_head->x)
2022 if (r.width >= s->clip_head->x - r.x)
2023 r.width -= s->clip_head->x - r.x;
2024 else
2025 r.width = 0;
2026 r.x = s->clip_head->x;
2028 if (s->clip_tail)
2029 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2031 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2032 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2033 else
2034 r.width = 0;
2037 /* If S draws overlapping rows, it's sufficient to use the top and
2038 bottom of the window for clipping because this glyph string
2039 intentionally draws over other lines. */
2040 if (s->for_overlaps)
2042 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2043 r.height = window_text_bottom_y (s->w) - r.y;
2045 /* Alas, the above simple strategy does not work for the
2046 environments with anti-aliased text: if the same text is
2047 drawn onto the same place multiple times, it gets thicker.
2048 If the overlap we are processing is for the erased cursor, we
2049 take the intersection with the rectangle of the cursor. */
2050 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2052 XRectangle rc, r_save = r;
2054 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2055 rc.y = s->w->phys_cursor.y;
2056 rc.width = s->w->phys_cursor_width;
2057 rc.height = s->w->phys_cursor_height;
2059 x_intersect_rectangles (&r_save, &rc, &r);
2062 else
2064 /* Don't use S->y for clipping because it doesn't take partially
2065 visible lines into account. For example, it can be negative for
2066 partially visible lines at the top of a window. */
2067 if (!s->row->full_width_p
2068 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2069 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2070 else
2071 r.y = max (0, s->row->y);
2074 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2076 /* If drawing the cursor, don't let glyph draw outside its
2077 advertised boundaries. Cleartype does this under some circumstances. */
2078 if (s->hl == DRAW_CURSOR)
2080 struct glyph *glyph = s->first_glyph;
2081 int height, max_y;
2083 if (s->x > r.x)
2085 r.width -= s->x - r.x;
2086 r.x = s->x;
2088 r.width = min (r.width, glyph->pixel_width);
2090 /* If r.y is below window bottom, ensure that we still see a cursor. */
2091 height = min (glyph->ascent + glyph->descent,
2092 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2093 max_y = window_text_bottom_y (s->w) - height;
2094 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2095 if (s->ybase - glyph->ascent > max_y)
2097 r.y = max_y;
2098 r.height = height;
2100 else
2102 /* Don't draw cursor glyph taller than our actual glyph. */
2103 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2104 if (height < r.height)
2106 max_y = r.y + r.height;
2107 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2108 r.height = min (max_y - r.y, height);
2113 if (s->row->clip)
2115 XRectangle r_save = r;
2117 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2118 r.width = 0;
2121 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2122 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2124 #ifdef CONVERT_FROM_XRECT
2125 CONVERT_FROM_XRECT (r, *rects);
2126 #else
2127 *rects = r;
2128 #endif
2129 return 1;
2131 else
2133 /* If we are processing overlapping and allowed to return
2134 multiple clipping rectangles, we exclude the row of the glyph
2135 string from the clipping rectangle. This is to avoid drawing
2136 the same text on the environment with anti-aliasing. */
2137 #ifdef CONVERT_FROM_XRECT
2138 XRectangle rs[2];
2139 #else
2140 XRectangle *rs = rects;
2141 #endif
2142 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2144 if (s->for_overlaps & OVERLAPS_PRED)
2146 rs[i] = r;
2147 if (r.y + r.height > row_y)
2149 if (r.y < row_y)
2150 rs[i].height = row_y - r.y;
2151 else
2152 rs[i].height = 0;
2154 i++;
2156 if (s->for_overlaps & OVERLAPS_SUCC)
2158 rs[i] = r;
2159 if (r.y < row_y + s->row->visible_height)
2161 if (r.y + r.height > row_y + s->row->visible_height)
2163 rs[i].y = row_y + s->row->visible_height;
2164 rs[i].height = r.y + r.height - rs[i].y;
2166 else
2167 rs[i].height = 0;
2169 i++;
2172 n = i;
2173 #ifdef CONVERT_FROM_XRECT
2174 for (i = 0; i < n; i++)
2175 CONVERT_FROM_XRECT (rs[i], rects[i]);
2176 #endif
2177 return n;
2181 /* EXPORT:
2182 Return in *NR the clipping rectangle for glyph string S. */
2184 void
2185 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2187 get_glyph_string_clip_rects (s, nr, 1);
2191 /* EXPORT:
2192 Return the position and height of the phys cursor in window W.
2193 Set w->phys_cursor_width to width of phys cursor.
2196 void
2197 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2198 struct glyph *glyph, int *xp, int *yp, int *heightp)
2200 struct frame *f = XFRAME (WINDOW_FRAME (w));
2201 int x, y, wd, h, h0, y0;
2203 /* Compute the width of the rectangle to draw. If on a stretch
2204 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2205 rectangle as wide as the glyph, but use a canonical character
2206 width instead. */
2207 wd = glyph->pixel_width - 1;
2208 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2209 wd++; /* Why? */
2210 #endif
2212 x = w->phys_cursor.x;
2213 if (x < 0)
2215 wd += x;
2216 x = 0;
2219 if (glyph->type == STRETCH_GLYPH
2220 && !x_stretch_cursor_p)
2221 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2222 w->phys_cursor_width = wd;
2224 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2226 /* If y is below window bottom, ensure that we still see a cursor. */
2227 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2229 h = max (h0, glyph->ascent + glyph->descent);
2230 h0 = min (h0, glyph->ascent + glyph->descent);
2232 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2233 if (y < y0)
2235 h = max (h - (y0 - y) + 1, h0);
2236 y = y0 - 1;
2238 else
2240 y0 = window_text_bottom_y (w) - h0;
2241 if (y > y0)
2243 h += y - y0;
2244 y = y0;
2248 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2249 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2250 *heightp = h;
2254 * Remember which glyph the mouse is over.
2257 void
2258 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2260 Lisp_Object window;
2261 struct window *w;
2262 struct glyph_row *r, *gr, *end_row;
2263 enum window_part part;
2264 enum glyph_row_area area;
2265 int x, y, width, height;
2267 /* Try to determine frame pixel position and size of the glyph under
2268 frame pixel coordinates X/Y on frame F. */
2270 if (!f->glyphs_initialized_p
2271 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2272 NILP (window)))
2274 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2275 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2276 goto virtual_glyph;
2279 w = XWINDOW (window);
2280 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2281 height = WINDOW_FRAME_LINE_HEIGHT (w);
2283 x = window_relative_x_coord (w, part, gx);
2284 y = gy - WINDOW_TOP_EDGE_Y (w);
2286 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2287 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2289 if (w->pseudo_window_p)
2291 area = TEXT_AREA;
2292 part = ON_MODE_LINE; /* Don't adjust margin. */
2293 goto text_glyph;
2296 switch (part)
2298 case ON_LEFT_MARGIN:
2299 area = LEFT_MARGIN_AREA;
2300 goto text_glyph;
2302 case ON_RIGHT_MARGIN:
2303 area = RIGHT_MARGIN_AREA;
2304 goto text_glyph;
2306 case ON_HEADER_LINE:
2307 case ON_MODE_LINE:
2308 gr = (part == ON_HEADER_LINE
2309 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2310 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2311 gy = gr->y;
2312 area = TEXT_AREA;
2313 goto text_glyph_row_found;
2315 case ON_TEXT:
2316 area = TEXT_AREA;
2318 text_glyph:
2319 gr = 0; gy = 0;
2320 for (; r <= end_row && r->enabled_p; ++r)
2321 if (r->y + r->height > y)
2323 gr = r; gy = r->y;
2324 break;
2327 text_glyph_row_found:
2328 if (gr && gy <= y)
2330 struct glyph *g = gr->glyphs[area];
2331 struct glyph *end = g + gr->used[area];
2333 height = gr->height;
2334 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2335 if (gx + g->pixel_width > x)
2336 break;
2338 if (g < end)
2340 if (g->type == IMAGE_GLYPH)
2342 /* Don't remember when mouse is over image, as
2343 image may have hot-spots. */
2344 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2345 return;
2347 width = g->pixel_width;
2349 else
2351 /* Use nominal char spacing at end of line. */
2352 x -= gx;
2353 gx += (x / width) * width;
2356 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2357 gx += window_box_left_offset (w, area);
2359 else
2361 /* Use nominal line height at end of window. */
2362 gx = (x / width) * width;
2363 y -= gy;
2364 gy += (y / height) * height;
2366 break;
2368 case ON_LEFT_FRINGE:
2369 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2370 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2371 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2372 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2373 goto row_glyph;
2375 case ON_RIGHT_FRINGE:
2376 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2377 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2378 : window_box_right_offset (w, TEXT_AREA));
2379 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2380 goto row_glyph;
2382 case ON_SCROLL_BAR:
2383 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2385 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2386 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2387 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2388 : 0)));
2389 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2391 row_glyph:
2392 gr = 0, gy = 0;
2393 for (; r <= end_row && r->enabled_p; ++r)
2394 if (r->y + r->height > y)
2396 gr = r; gy = r->y;
2397 break;
2400 if (gr && gy <= y)
2401 height = gr->height;
2402 else
2404 /* Use nominal line height at end of window. */
2405 y -= gy;
2406 gy += (y / height) * height;
2408 break;
2410 default:
2412 virtual_glyph:
2413 /* If there is no glyph under the mouse, then we divide the screen
2414 into a grid of the smallest glyph in the frame, and use that
2415 as our "glyph". */
2417 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2418 round down even for negative values. */
2419 if (gx < 0)
2420 gx -= width - 1;
2421 if (gy < 0)
2422 gy -= height - 1;
2424 gx = (gx / width) * width;
2425 gy = (gy / height) * height;
2427 goto store_rect;
2430 gx += WINDOW_LEFT_EDGE_X (w);
2431 gy += WINDOW_TOP_EDGE_Y (w);
2433 store_rect:
2434 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2436 /* Visible feedback for debugging. */
2437 #if 0
2438 #if HAVE_X_WINDOWS
2439 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2440 f->output_data.x->normal_gc,
2441 gx, gy, width, height);
2442 #endif
2443 #endif
2447 #endif /* HAVE_WINDOW_SYSTEM */
2449 static void
2450 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2452 eassert (w);
2453 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2454 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2455 w->window_end_vpos
2456 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2459 /***********************************************************************
2460 Lisp form evaluation
2461 ***********************************************************************/
2463 /* Error handler for safe_eval and safe_call. */
2465 static Lisp_Object
2466 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2468 add_to_log ("Error during redisplay: %S signaled %S",
2469 Flist (nargs, args), arg);
2470 return Qnil;
2473 /* Call function FUNC with the rest of NARGS - 1 arguments
2474 following. Return the result, or nil if something went
2475 wrong. Prevent redisplay during the evaluation. */
2477 Lisp_Object
2478 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2480 Lisp_Object val;
2482 if (inhibit_eval_during_redisplay)
2483 val = Qnil;
2484 else
2486 va_list ap;
2487 ptrdiff_t i;
2488 ptrdiff_t count = SPECPDL_INDEX ();
2489 struct gcpro gcpro1;
2490 Lisp_Object *args = alloca (nargs * word_size);
2492 args[0] = func;
2493 va_start (ap, func);
2494 for (i = 1; i < nargs; i++)
2495 args[i] = va_arg (ap, Lisp_Object);
2496 va_end (ap);
2498 GCPRO1 (args[0]);
2499 gcpro1.nvars = nargs;
2500 specbind (Qinhibit_redisplay, Qt);
2501 /* Use Qt to ensure debugger does not run,
2502 so there is no possibility of wanting to redisplay. */
2503 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2504 safe_eval_handler);
2505 UNGCPRO;
2506 val = unbind_to (count, val);
2509 return val;
2513 /* Call function FN with one argument ARG.
2514 Return the result, or nil if something went wrong. */
2516 Lisp_Object
2517 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2519 return safe_call (2, fn, arg);
2522 static Lisp_Object Qeval;
2524 Lisp_Object
2525 safe_eval (Lisp_Object sexpr)
2527 return safe_call1 (Qeval, sexpr);
2530 /* Call function FN with two arguments ARG1 and ARG2.
2531 Return the result, or nil if something went wrong. */
2533 Lisp_Object
2534 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2536 return safe_call (3, fn, arg1, arg2);
2541 /***********************************************************************
2542 Debugging
2543 ***********************************************************************/
2545 #if 0
2547 /* Define CHECK_IT to perform sanity checks on iterators.
2548 This is for debugging. It is too slow to do unconditionally. */
2550 static void
2551 check_it (struct it *it)
2553 if (it->method == GET_FROM_STRING)
2555 eassert (STRINGP (it->string));
2556 eassert (IT_STRING_CHARPOS (*it) >= 0);
2558 else
2560 eassert (IT_STRING_CHARPOS (*it) < 0);
2561 if (it->method == GET_FROM_BUFFER)
2563 /* Check that character and byte positions agree. */
2564 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2568 if (it->dpvec)
2569 eassert (it->current.dpvec_index >= 0);
2570 else
2571 eassert (it->current.dpvec_index < 0);
2574 #define CHECK_IT(IT) check_it ((IT))
2576 #else /* not 0 */
2578 #define CHECK_IT(IT) (void) 0
2580 #endif /* not 0 */
2583 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2585 /* Check that the window end of window W is what we expect it
2586 to be---the last row in the current matrix displaying text. */
2588 static void
2589 check_window_end (struct window *w)
2591 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2593 struct glyph_row *row;
2594 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2595 !row->enabled_p
2596 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2597 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2601 #define CHECK_WINDOW_END(W) check_window_end ((W))
2603 #else
2605 #define CHECK_WINDOW_END(W) (void) 0
2607 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2609 /* Return mark position if current buffer has the region of non-zero length,
2610 or -1 otherwise. */
2612 static ptrdiff_t
2613 markpos_of_region (void)
2615 if (!NILP (Vtransient_mark_mode)
2616 && !NILP (BVAR (current_buffer, mark_active))
2617 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2619 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2621 if (markpos != PT)
2622 return markpos;
2624 return -1;
2627 /***********************************************************************
2628 Iterator initialization
2629 ***********************************************************************/
2631 /* Initialize IT for displaying current_buffer in window W, starting
2632 at character position CHARPOS. CHARPOS < 0 means that no buffer
2633 position is specified which is useful when the iterator is assigned
2634 a position later. BYTEPOS is the byte position corresponding to
2635 CHARPOS.
2637 If ROW is not null, calls to produce_glyphs with IT as parameter
2638 will produce glyphs in that row.
2640 BASE_FACE_ID is the id of a base face to use. It must be one of
2641 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2642 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2643 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2645 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2646 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2647 will be initialized to use the corresponding mode line glyph row of
2648 the desired matrix of W. */
2650 void
2651 init_iterator (struct it *it, struct window *w,
2652 ptrdiff_t charpos, ptrdiff_t bytepos,
2653 struct glyph_row *row, enum face_id base_face_id)
2655 ptrdiff_t markpos;
2656 enum face_id remapped_base_face_id = base_face_id;
2658 /* Some precondition checks. */
2659 eassert (w != NULL && it != NULL);
2660 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2661 && charpos <= ZV));
2663 /* If face attributes have been changed since the last redisplay,
2664 free realized faces now because they depend on face definitions
2665 that might have changed. Don't free faces while there might be
2666 desired matrices pending which reference these faces. */
2667 if (face_change_count && !inhibit_free_realized_faces)
2669 face_change_count = 0;
2670 free_all_realized_faces (Qnil);
2673 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2674 if (! NILP (Vface_remapping_alist))
2675 remapped_base_face_id
2676 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2678 /* Use one of the mode line rows of W's desired matrix if
2679 appropriate. */
2680 if (row == NULL)
2682 if (base_face_id == MODE_LINE_FACE_ID
2683 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2684 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2685 else if (base_face_id == HEADER_LINE_FACE_ID)
2686 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2689 /* Clear IT. */
2690 memset (it, 0, sizeof *it);
2691 it->current.overlay_string_index = -1;
2692 it->current.dpvec_index = -1;
2693 it->base_face_id = remapped_base_face_id;
2694 it->string = Qnil;
2695 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2696 it->paragraph_embedding = L2R;
2697 it->bidi_it.string.lstring = Qnil;
2698 it->bidi_it.string.s = NULL;
2699 it->bidi_it.string.bufpos = 0;
2700 it->bidi_it.w = w;
2702 /* The window in which we iterate over current_buffer: */
2703 XSETWINDOW (it->window, w);
2704 it->w = w;
2705 it->f = XFRAME (w->frame);
2707 it->cmp_it.id = -1;
2709 /* Extra space between lines (on window systems only). */
2710 if (base_face_id == DEFAULT_FACE_ID
2711 && FRAME_WINDOW_P (it->f))
2713 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2714 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2715 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2716 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2717 * FRAME_LINE_HEIGHT (it->f));
2718 else if (it->f->extra_line_spacing > 0)
2719 it->extra_line_spacing = it->f->extra_line_spacing;
2720 it->max_extra_line_spacing = 0;
2723 /* If realized faces have been removed, e.g. because of face
2724 attribute changes of named faces, recompute them. When running
2725 in batch mode, the face cache of the initial frame is null. If
2726 we happen to get called, make a dummy face cache. */
2727 if (FRAME_FACE_CACHE (it->f) == NULL)
2728 init_frame_faces (it->f);
2729 if (FRAME_FACE_CACHE (it->f)->used == 0)
2730 recompute_basic_faces (it->f);
2732 /* Current value of the `slice', `space-width', and 'height' properties. */
2733 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2734 it->space_width = Qnil;
2735 it->font_height = Qnil;
2736 it->override_ascent = -1;
2738 /* Are control characters displayed as `^C'? */
2739 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2741 /* -1 means everything between a CR and the following line end
2742 is invisible. >0 means lines indented more than this value are
2743 invisible. */
2744 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2745 ? (clip_to_bounds
2746 (-1, XINT (BVAR (current_buffer, selective_display)),
2747 PTRDIFF_MAX))
2748 : (!NILP (BVAR (current_buffer, selective_display))
2749 ? -1 : 0));
2750 it->selective_display_ellipsis_p
2751 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2753 /* Display table to use. */
2754 it->dp = window_display_table (w);
2756 /* Are multibyte characters enabled in current_buffer? */
2757 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2759 /* If visible region is of non-zero length, set IT->region_beg_charpos
2760 and IT->region_end_charpos to the start and end of a visible region
2761 in window IT->w. Set both to -1 to indicate no region. */
2762 markpos = markpos_of_region ();
2763 if (markpos >= 0
2764 /* Maybe highlight only in selected window. */
2765 && (/* Either show region everywhere. */
2766 highlight_nonselected_windows
2767 /* Or show region in the selected window. */
2768 || w == XWINDOW (selected_window)
2769 /* Or show the region if we are in the mini-buffer and W is
2770 the window the mini-buffer refers to. */
2771 || (MINI_WINDOW_P (XWINDOW (selected_window))
2772 && WINDOWP (minibuf_selected_window)
2773 && w == XWINDOW (minibuf_selected_window))))
2775 it->region_beg_charpos = min (PT, markpos);
2776 it->region_end_charpos = max (PT, markpos);
2778 else
2779 it->region_beg_charpos = it->region_end_charpos = -1;
2781 /* Get the position at which the redisplay_end_trigger hook should
2782 be run, if it is to be run at all. */
2783 if (MARKERP (w->redisplay_end_trigger)
2784 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2785 it->redisplay_end_trigger_charpos
2786 = marker_position (w->redisplay_end_trigger);
2787 else if (INTEGERP (w->redisplay_end_trigger))
2788 it->redisplay_end_trigger_charpos =
2789 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2791 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2793 /* Are lines in the display truncated? */
2794 if (base_face_id != DEFAULT_FACE_ID
2795 || it->w->hscroll
2796 || (! WINDOW_FULL_WIDTH_P (it->w)
2797 && ((!NILP (Vtruncate_partial_width_windows)
2798 && !INTEGERP (Vtruncate_partial_width_windows))
2799 || (INTEGERP (Vtruncate_partial_width_windows)
2800 && (WINDOW_TOTAL_COLS (it->w)
2801 < XINT (Vtruncate_partial_width_windows))))))
2802 it->line_wrap = TRUNCATE;
2803 else if (NILP (BVAR (current_buffer, truncate_lines)))
2804 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2805 ? WINDOW_WRAP : WORD_WRAP;
2806 else
2807 it->line_wrap = TRUNCATE;
2809 /* Get dimensions of truncation and continuation glyphs. These are
2810 displayed as fringe bitmaps under X, but we need them for such
2811 frames when the fringes are turned off. But leave the dimensions
2812 zero for tooltip frames, as these glyphs look ugly there and also
2813 sabotage calculations of tooltip dimensions in x-show-tip. */
2814 #ifdef HAVE_WINDOW_SYSTEM
2815 if (!(FRAME_WINDOW_P (it->f)
2816 && FRAMEP (tip_frame)
2817 && it->f == XFRAME (tip_frame)))
2818 #endif
2820 if (it->line_wrap == TRUNCATE)
2822 /* We will need the truncation glyph. */
2823 eassert (it->glyph_row == NULL);
2824 produce_special_glyphs (it, IT_TRUNCATION);
2825 it->truncation_pixel_width = it->pixel_width;
2827 else
2829 /* We will need the continuation glyph. */
2830 eassert (it->glyph_row == NULL);
2831 produce_special_glyphs (it, IT_CONTINUATION);
2832 it->continuation_pixel_width = it->pixel_width;
2836 /* Reset these values to zero because the produce_special_glyphs
2837 above has changed them. */
2838 it->pixel_width = it->ascent = it->descent = 0;
2839 it->phys_ascent = it->phys_descent = 0;
2841 /* Set this after getting the dimensions of truncation and
2842 continuation glyphs, so that we don't produce glyphs when calling
2843 produce_special_glyphs, above. */
2844 it->glyph_row = row;
2845 it->area = TEXT_AREA;
2847 /* Forget any previous info about this row being reversed. */
2848 if (it->glyph_row)
2849 it->glyph_row->reversed_p = 0;
2851 /* Get the dimensions of the display area. The display area
2852 consists of the visible window area plus a horizontally scrolled
2853 part to the left of the window. All x-values are relative to the
2854 start of this total display area. */
2855 if (base_face_id != DEFAULT_FACE_ID)
2857 /* Mode lines, menu bar in terminal frames. */
2858 it->first_visible_x = 0;
2859 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2861 else
2863 it->first_visible_x =
2864 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2865 it->last_visible_x = (it->first_visible_x
2866 + window_box_width (w, TEXT_AREA));
2868 /* If we truncate lines, leave room for the truncation glyph(s) at
2869 the right margin. Otherwise, leave room for the continuation
2870 glyph(s). Done only if the window has no fringes. Since we
2871 don't know at this point whether there will be any R2L lines in
2872 the window, we reserve space for truncation/continuation glyphs
2873 even if only one of the fringes is absent. */
2874 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2875 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2877 if (it->line_wrap == TRUNCATE)
2878 it->last_visible_x -= it->truncation_pixel_width;
2879 else
2880 it->last_visible_x -= it->continuation_pixel_width;
2883 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2884 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2887 /* Leave room for a border glyph. */
2888 if (!FRAME_WINDOW_P (it->f)
2889 && !WINDOW_RIGHTMOST_P (it->w))
2890 it->last_visible_x -= 1;
2892 it->last_visible_y = window_text_bottom_y (w);
2894 /* For mode lines and alike, arrange for the first glyph having a
2895 left box line if the face specifies a box. */
2896 if (base_face_id != DEFAULT_FACE_ID)
2898 struct face *face;
2900 it->face_id = remapped_base_face_id;
2902 /* If we have a boxed mode line, make the first character appear
2903 with a left box line. */
2904 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2905 if (face->box != FACE_NO_BOX)
2906 it->start_of_box_run_p = 1;
2909 /* If a buffer position was specified, set the iterator there,
2910 getting overlays and face properties from that position. */
2911 if (charpos >= BUF_BEG (current_buffer))
2913 it->end_charpos = ZV;
2914 eassert (charpos == BYTE_TO_CHAR (bytepos));
2915 IT_CHARPOS (*it) = charpos;
2916 IT_BYTEPOS (*it) = bytepos;
2918 /* We will rely on `reseat' to set this up properly, via
2919 handle_face_prop. */
2920 it->face_id = it->base_face_id;
2922 it->start = it->current;
2923 /* Do we need to reorder bidirectional text? Not if this is a
2924 unibyte buffer: by definition, none of the single-byte
2925 characters are strong R2L, so no reordering is needed. And
2926 bidi.c doesn't support unibyte buffers anyway. Also, don't
2927 reorder while we are loading loadup.el, since the tables of
2928 character properties needed for reordering are not yet
2929 available. */
2930 it->bidi_p =
2931 NILP (Vpurify_flag)
2932 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2933 && it->multibyte_p;
2935 /* If we are to reorder bidirectional text, init the bidi
2936 iterator. */
2937 if (it->bidi_p)
2939 /* Note the paragraph direction that this buffer wants to
2940 use. */
2941 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2942 Qleft_to_right))
2943 it->paragraph_embedding = L2R;
2944 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2945 Qright_to_left))
2946 it->paragraph_embedding = R2L;
2947 else
2948 it->paragraph_embedding = NEUTRAL_DIR;
2949 bidi_unshelve_cache (NULL, 0);
2950 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2951 &it->bidi_it);
2954 /* Compute faces etc. */
2955 reseat (it, it->current.pos, 1);
2958 CHECK_IT (it);
2962 /* Initialize IT for the display of window W with window start POS. */
2964 void
2965 start_display (struct it *it, struct window *w, struct text_pos pos)
2967 struct glyph_row *row;
2968 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2970 row = w->desired_matrix->rows + first_vpos;
2971 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2972 it->first_vpos = first_vpos;
2974 /* Don't reseat to previous visible line start if current start
2975 position is in a string or image. */
2976 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2978 int start_at_line_beg_p;
2979 int first_y = it->current_y;
2981 /* If window start is not at a line start, skip forward to POS to
2982 get the correct continuation lines width. */
2983 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2984 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2985 if (!start_at_line_beg_p)
2987 int new_x;
2989 reseat_at_previous_visible_line_start (it);
2990 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2992 new_x = it->current_x + it->pixel_width;
2994 /* If lines are continued, this line may end in the middle
2995 of a multi-glyph character (e.g. a control character
2996 displayed as \003, or in the middle of an overlay
2997 string). In this case move_it_to above will not have
2998 taken us to the start of the continuation line but to the
2999 end of the continued line. */
3000 if (it->current_x > 0
3001 && it->line_wrap != TRUNCATE /* Lines are continued. */
3002 && (/* And glyph doesn't fit on the line. */
3003 new_x > it->last_visible_x
3004 /* Or it fits exactly and we're on a window
3005 system frame. */
3006 || (new_x == it->last_visible_x
3007 && FRAME_WINDOW_P (it->f)
3008 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3009 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3010 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3012 if ((it->current.dpvec_index >= 0
3013 || it->current.overlay_string_index >= 0)
3014 /* If we are on a newline from a display vector or
3015 overlay string, then we are already at the end of
3016 a screen line; no need to go to the next line in
3017 that case, as this line is not really continued.
3018 (If we do go to the next line, C-e will not DTRT.) */
3019 && it->c != '\n')
3021 set_iterator_to_next (it, 1);
3022 move_it_in_display_line_to (it, -1, -1, 0);
3025 it->continuation_lines_width += it->current_x;
3027 /* If the character at POS is displayed via a display
3028 vector, move_it_to above stops at the final glyph of
3029 IT->dpvec. To make the caller redisplay that character
3030 again (a.k.a. start at POS), we need to reset the
3031 dpvec_index to the beginning of IT->dpvec. */
3032 else if (it->current.dpvec_index >= 0)
3033 it->current.dpvec_index = 0;
3035 /* We're starting a new display line, not affected by the
3036 height of the continued line, so clear the appropriate
3037 fields in the iterator structure. */
3038 it->max_ascent = it->max_descent = 0;
3039 it->max_phys_ascent = it->max_phys_descent = 0;
3041 it->current_y = first_y;
3042 it->vpos = 0;
3043 it->current_x = it->hpos = 0;
3049 /* Return 1 if POS is a position in ellipses displayed for invisible
3050 text. W is the window we display, for text property lookup. */
3052 static int
3053 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3055 Lisp_Object prop, window;
3056 int ellipses_p = 0;
3057 ptrdiff_t charpos = CHARPOS (pos->pos);
3059 /* If POS specifies a position in a display vector, this might
3060 be for an ellipsis displayed for invisible text. We won't
3061 get the iterator set up for delivering that ellipsis unless
3062 we make sure that it gets aware of the invisible text. */
3063 if (pos->dpvec_index >= 0
3064 && pos->overlay_string_index < 0
3065 && CHARPOS (pos->string_pos) < 0
3066 && charpos > BEGV
3067 && (XSETWINDOW (window, w),
3068 prop = Fget_char_property (make_number (charpos),
3069 Qinvisible, window),
3070 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3072 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3073 window);
3074 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3077 return ellipses_p;
3081 /* Initialize IT for stepping through current_buffer in window W,
3082 starting at position POS that includes overlay string and display
3083 vector/ control character translation position information. Value
3084 is zero if there are overlay strings with newlines at POS. */
3086 static int
3087 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3089 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3090 int i, overlay_strings_with_newlines = 0;
3092 /* If POS specifies a position in a display vector, this might
3093 be for an ellipsis displayed for invisible text. We won't
3094 get the iterator set up for delivering that ellipsis unless
3095 we make sure that it gets aware of the invisible text. */
3096 if (in_ellipses_for_invisible_text_p (pos, w))
3098 --charpos;
3099 bytepos = 0;
3102 /* Keep in mind: the call to reseat in init_iterator skips invisible
3103 text, so we might end up at a position different from POS. This
3104 is only a problem when POS is a row start after a newline and an
3105 overlay starts there with an after-string, and the overlay has an
3106 invisible property. Since we don't skip invisible text in
3107 display_line and elsewhere immediately after consuming the
3108 newline before the row start, such a POS will not be in a string,
3109 but the call to init_iterator below will move us to the
3110 after-string. */
3111 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3113 /* This only scans the current chunk -- it should scan all chunks.
3114 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3115 to 16 in 22.1 to make this a lesser problem. */
3116 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3118 const char *s = SSDATA (it->overlay_strings[i]);
3119 const char *e = s + SBYTES (it->overlay_strings[i]);
3121 while (s < e && *s != '\n')
3122 ++s;
3124 if (s < e)
3126 overlay_strings_with_newlines = 1;
3127 break;
3131 /* If position is within an overlay string, set up IT to the right
3132 overlay string. */
3133 if (pos->overlay_string_index >= 0)
3135 int relative_index;
3137 /* If the first overlay string happens to have a `display'
3138 property for an image, the iterator will be set up for that
3139 image, and we have to undo that setup first before we can
3140 correct the overlay string index. */
3141 if (it->method == GET_FROM_IMAGE)
3142 pop_it (it);
3144 /* We already have the first chunk of overlay strings in
3145 IT->overlay_strings. Load more until the one for
3146 pos->overlay_string_index is in IT->overlay_strings. */
3147 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3149 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3150 it->current.overlay_string_index = 0;
3151 while (n--)
3153 load_overlay_strings (it, 0);
3154 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3158 it->current.overlay_string_index = pos->overlay_string_index;
3159 relative_index = (it->current.overlay_string_index
3160 % OVERLAY_STRING_CHUNK_SIZE);
3161 it->string = it->overlay_strings[relative_index];
3162 eassert (STRINGP (it->string));
3163 it->current.string_pos = pos->string_pos;
3164 it->method = GET_FROM_STRING;
3165 it->end_charpos = SCHARS (it->string);
3166 /* Set up the bidi iterator for this overlay string. */
3167 if (it->bidi_p)
3169 it->bidi_it.string.lstring = it->string;
3170 it->bidi_it.string.s = NULL;
3171 it->bidi_it.string.schars = SCHARS (it->string);
3172 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3173 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3174 it->bidi_it.string.unibyte = !it->multibyte_p;
3175 it->bidi_it.w = it->w;
3176 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3177 FRAME_WINDOW_P (it->f), &it->bidi_it);
3179 /* Synchronize the state of the bidi iterator with
3180 pos->string_pos. For any string position other than
3181 zero, this will be done automagically when we resume
3182 iteration over the string and get_visually_first_element
3183 is called. But if string_pos is zero, and the string is
3184 to be reordered for display, we need to resync manually,
3185 since it could be that the iteration state recorded in
3186 pos ended at string_pos of 0 moving backwards in string. */
3187 if (CHARPOS (pos->string_pos) == 0)
3189 get_visually_first_element (it);
3190 if (IT_STRING_CHARPOS (*it) != 0)
3191 do {
3192 /* Paranoia. */
3193 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3194 bidi_move_to_visually_next (&it->bidi_it);
3195 } while (it->bidi_it.charpos != 0);
3197 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3198 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3202 if (CHARPOS (pos->string_pos) >= 0)
3204 /* Recorded position is not in an overlay string, but in another
3205 string. This can only be a string from a `display' property.
3206 IT should already be filled with that string. */
3207 it->current.string_pos = pos->string_pos;
3208 eassert (STRINGP (it->string));
3209 if (it->bidi_p)
3210 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3211 FRAME_WINDOW_P (it->f), &it->bidi_it);
3214 /* Restore position in display vector translations, control
3215 character translations or ellipses. */
3216 if (pos->dpvec_index >= 0)
3218 if (it->dpvec == NULL)
3219 get_next_display_element (it);
3220 eassert (it->dpvec && it->current.dpvec_index == 0);
3221 it->current.dpvec_index = pos->dpvec_index;
3224 CHECK_IT (it);
3225 return !overlay_strings_with_newlines;
3229 /* Initialize IT for stepping through current_buffer in window W
3230 starting at ROW->start. */
3232 static void
3233 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3235 init_from_display_pos (it, w, &row->start);
3236 it->start = row->start;
3237 it->continuation_lines_width = row->continuation_lines_width;
3238 CHECK_IT (it);
3242 /* Initialize IT for stepping through current_buffer in window W
3243 starting in the line following ROW, i.e. starting at ROW->end.
3244 Value is zero if there are overlay strings with newlines at ROW's
3245 end position. */
3247 static int
3248 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3250 int success = 0;
3252 if (init_from_display_pos (it, w, &row->end))
3254 if (row->continued_p)
3255 it->continuation_lines_width
3256 = row->continuation_lines_width + row->pixel_width;
3257 CHECK_IT (it);
3258 success = 1;
3261 return success;
3267 /***********************************************************************
3268 Text properties
3269 ***********************************************************************/
3271 /* Called when IT reaches IT->stop_charpos. Handle text property and
3272 overlay changes. Set IT->stop_charpos to the next position where
3273 to stop. */
3275 static void
3276 handle_stop (struct it *it)
3278 enum prop_handled handled;
3279 int handle_overlay_change_p;
3280 struct props *p;
3282 it->dpvec = NULL;
3283 it->current.dpvec_index = -1;
3284 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3285 it->ignore_overlay_strings_at_pos_p = 0;
3286 it->ellipsis_p = 0;
3288 /* Use face of preceding text for ellipsis (if invisible) */
3289 if (it->selective_display_ellipsis_p)
3290 it->saved_face_id = it->face_id;
3294 handled = HANDLED_NORMALLY;
3296 /* Call text property handlers. */
3297 for (p = it_props; p->handler; ++p)
3299 handled = p->handler (it);
3301 if (handled == HANDLED_RECOMPUTE_PROPS)
3302 break;
3303 else if (handled == HANDLED_RETURN)
3305 /* We still want to show before and after strings from
3306 overlays even if the actual buffer text is replaced. */
3307 if (!handle_overlay_change_p
3308 || it->sp > 1
3309 /* Don't call get_overlay_strings_1 if we already
3310 have overlay strings loaded, because doing so
3311 will load them again and push the iterator state
3312 onto the stack one more time, which is not
3313 expected by the rest of the code that processes
3314 overlay strings. */
3315 || (it->current.overlay_string_index < 0
3316 ? !get_overlay_strings_1 (it, 0, 0)
3317 : 0))
3319 if (it->ellipsis_p)
3320 setup_for_ellipsis (it, 0);
3321 /* When handling a display spec, we might load an
3322 empty string. In that case, discard it here. We
3323 used to discard it in handle_single_display_spec,
3324 but that causes get_overlay_strings_1, above, to
3325 ignore overlay strings that we must check. */
3326 if (STRINGP (it->string) && !SCHARS (it->string))
3327 pop_it (it);
3328 return;
3330 else if (STRINGP (it->string) && !SCHARS (it->string))
3331 pop_it (it);
3332 else
3334 it->ignore_overlay_strings_at_pos_p = 1;
3335 it->string_from_display_prop_p = 0;
3336 it->from_disp_prop_p = 0;
3337 handle_overlay_change_p = 0;
3339 handled = HANDLED_RECOMPUTE_PROPS;
3340 break;
3342 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3343 handle_overlay_change_p = 0;
3346 if (handled != HANDLED_RECOMPUTE_PROPS)
3348 /* Don't check for overlay strings below when set to deliver
3349 characters from a display vector. */
3350 if (it->method == GET_FROM_DISPLAY_VECTOR)
3351 handle_overlay_change_p = 0;
3353 /* Handle overlay changes.
3354 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3355 if it finds overlays. */
3356 if (handle_overlay_change_p)
3357 handled = handle_overlay_change (it);
3360 if (it->ellipsis_p)
3362 setup_for_ellipsis (it, 0);
3363 break;
3366 while (handled == HANDLED_RECOMPUTE_PROPS);
3368 /* Determine where to stop next. */
3369 if (handled == HANDLED_NORMALLY)
3370 compute_stop_pos (it);
3374 /* Compute IT->stop_charpos from text property and overlay change
3375 information for IT's current position. */
3377 static void
3378 compute_stop_pos (struct it *it)
3380 register INTERVAL iv, next_iv;
3381 Lisp_Object object, limit, position;
3382 ptrdiff_t charpos, bytepos;
3384 if (STRINGP (it->string))
3386 /* Strings are usually short, so don't limit the search for
3387 properties. */
3388 it->stop_charpos = it->end_charpos;
3389 object = it->string;
3390 limit = Qnil;
3391 charpos = IT_STRING_CHARPOS (*it);
3392 bytepos = IT_STRING_BYTEPOS (*it);
3394 else
3396 ptrdiff_t pos;
3398 /* If end_charpos is out of range for some reason, such as a
3399 misbehaving display function, rationalize it (Bug#5984). */
3400 if (it->end_charpos > ZV)
3401 it->end_charpos = ZV;
3402 it->stop_charpos = it->end_charpos;
3404 /* If next overlay change is in front of the current stop pos
3405 (which is IT->end_charpos), stop there. Note: value of
3406 next_overlay_change is point-max if no overlay change
3407 follows. */
3408 charpos = IT_CHARPOS (*it);
3409 bytepos = IT_BYTEPOS (*it);
3410 pos = next_overlay_change (charpos);
3411 if (pos < it->stop_charpos)
3412 it->stop_charpos = pos;
3414 /* If showing the region, we have to stop at the region
3415 start or end because the face might change there. */
3416 if (it->region_beg_charpos > 0)
3418 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3419 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3420 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3421 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3424 /* Set up variables for computing the stop position from text
3425 property changes. */
3426 XSETBUFFER (object, current_buffer);
3427 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3430 /* Get the interval containing IT's position. Value is a null
3431 interval if there isn't such an interval. */
3432 position = make_number (charpos);
3433 iv = validate_interval_range (object, &position, &position, 0);
3434 if (iv)
3436 Lisp_Object values_here[LAST_PROP_IDX];
3437 struct props *p;
3439 /* Get properties here. */
3440 for (p = it_props; p->handler; ++p)
3441 values_here[p->idx] = textget (iv->plist, *p->name);
3443 /* Look for an interval following iv that has different
3444 properties. */
3445 for (next_iv = next_interval (iv);
3446 (next_iv
3447 && (NILP (limit)
3448 || XFASTINT (limit) > next_iv->position));
3449 next_iv = next_interval (next_iv))
3451 for (p = it_props; p->handler; ++p)
3453 Lisp_Object new_value;
3455 new_value = textget (next_iv->plist, *p->name);
3456 if (!EQ (values_here[p->idx], new_value))
3457 break;
3460 if (p->handler)
3461 break;
3464 if (next_iv)
3466 if (INTEGERP (limit)
3467 && next_iv->position >= XFASTINT (limit))
3468 /* No text property change up to limit. */
3469 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3470 else
3471 /* Text properties change in next_iv. */
3472 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3476 if (it->cmp_it.id < 0)
3478 ptrdiff_t stoppos = it->end_charpos;
3480 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3481 stoppos = -1;
3482 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3483 stoppos, it->string);
3486 eassert (STRINGP (it->string)
3487 || (it->stop_charpos >= BEGV
3488 && it->stop_charpos >= IT_CHARPOS (*it)));
3492 /* Return the position of the next overlay change after POS in
3493 current_buffer. Value is point-max if no overlay change
3494 follows. This is like `next-overlay-change' but doesn't use
3495 xmalloc. */
3497 static ptrdiff_t
3498 next_overlay_change (ptrdiff_t pos)
3500 ptrdiff_t i, noverlays;
3501 ptrdiff_t endpos;
3502 Lisp_Object *overlays;
3504 /* Get all overlays at the given position. */
3505 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3507 /* If any of these overlays ends before endpos,
3508 use its ending point instead. */
3509 for (i = 0; i < noverlays; ++i)
3511 Lisp_Object oend;
3512 ptrdiff_t oendpos;
3514 oend = OVERLAY_END (overlays[i]);
3515 oendpos = OVERLAY_POSITION (oend);
3516 endpos = min (endpos, oendpos);
3519 return endpos;
3522 /* How many characters forward to search for a display property or
3523 display string. Searching too far forward makes the bidi display
3524 sluggish, especially in small windows. */
3525 #define MAX_DISP_SCAN 250
3527 /* Return the character position of a display string at or after
3528 position specified by POSITION. If no display string exists at or
3529 after POSITION, return ZV. A display string is either an overlay
3530 with `display' property whose value is a string, or a `display'
3531 text property whose value is a string. STRING is data about the
3532 string to iterate; if STRING->lstring is nil, we are iterating a
3533 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3534 on a GUI frame. DISP_PROP is set to zero if we searched
3535 MAX_DISP_SCAN characters forward without finding any display
3536 strings, non-zero otherwise. It is set to 2 if the display string
3537 uses any kind of `(space ...)' spec that will produce a stretch of
3538 white space in the text area. */
3539 ptrdiff_t
3540 compute_display_string_pos (struct text_pos *position,
3541 struct bidi_string_data *string,
3542 struct window *w,
3543 int frame_window_p, int *disp_prop)
3545 /* OBJECT = nil means current buffer. */
3546 Lisp_Object object, object1;
3547 Lisp_Object pos, spec, limpos;
3548 int string_p = (string && (STRINGP (string->lstring) || string->s));
3549 ptrdiff_t eob = string_p ? string->schars : ZV;
3550 ptrdiff_t begb = string_p ? 0 : BEGV;
3551 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3552 ptrdiff_t lim =
3553 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3554 struct text_pos tpos;
3555 int rv = 0;
3557 if (string && STRINGP (string->lstring))
3558 object1 = object = string->lstring;
3559 else if (w && !string_p)
3561 XSETWINDOW (object, w);
3562 object1 = Qnil;
3564 else
3565 object1 = object = Qnil;
3567 *disp_prop = 1;
3569 if (charpos >= eob
3570 /* We don't support display properties whose values are strings
3571 that have display string properties. */
3572 || string->from_disp_str
3573 /* C strings cannot have display properties. */
3574 || (string->s && !STRINGP (object)))
3576 *disp_prop = 0;
3577 return eob;
3580 /* If the character at CHARPOS is where the display string begins,
3581 return CHARPOS. */
3582 pos = make_number (charpos);
3583 if (STRINGP (object))
3584 bufpos = string->bufpos;
3585 else
3586 bufpos = charpos;
3587 tpos = *position;
3588 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3589 && (charpos <= begb
3590 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3591 object),
3592 spec))
3593 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3594 frame_window_p)))
3596 if (rv == 2)
3597 *disp_prop = 2;
3598 return charpos;
3601 /* Look forward for the first character with a `display' property
3602 that will replace the underlying text when displayed. */
3603 limpos = make_number (lim);
3604 do {
3605 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3606 CHARPOS (tpos) = XFASTINT (pos);
3607 if (CHARPOS (tpos) >= lim)
3609 *disp_prop = 0;
3610 break;
3612 if (STRINGP (object))
3613 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3614 else
3615 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3616 spec = Fget_char_property (pos, Qdisplay, object);
3617 if (!STRINGP (object))
3618 bufpos = CHARPOS (tpos);
3619 } while (NILP (spec)
3620 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3621 bufpos, frame_window_p)));
3622 if (rv == 2)
3623 *disp_prop = 2;
3625 return CHARPOS (tpos);
3628 /* Return the character position of the end of the display string that
3629 started at CHARPOS. If there's no display string at CHARPOS,
3630 return -1. A display string is either an overlay with `display'
3631 property whose value is a string or a `display' text property whose
3632 value is a string. */
3633 ptrdiff_t
3634 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3636 /* OBJECT = nil means current buffer. */
3637 Lisp_Object object =
3638 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3639 Lisp_Object pos = make_number (charpos);
3640 ptrdiff_t eob =
3641 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3643 if (charpos >= eob || (string->s && !STRINGP (object)))
3644 return eob;
3646 /* It could happen that the display property or overlay was removed
3647 since we found it in compute_display_string_pos above. One way
3648 this can happen is if JIT font-lock was called (through
3649 handle_fontified_prop), and jit-lock-functions remove text
3650 properties or overlays from the portion of buffer that includes
3651 CHARPOS. Muse mode is known to do that, for example. In this
3652 case, we return -1 to the caller, to signal that no display
3653 string is actually present at CHARPOS. See bidi_fetch_char for
3654 how this is handled.
3656 An alternative would be to never look for display properties past
3657 it->stop_charpos. But neither compute_display_string_pos nor
3658 bidi_fetch_char that calls it know or care where the next
3659 stop_charpos is. */
3660 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3661 return -1;
3663 /* Look forward for the first character where the `display' property
3664 changes. */
3665 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3667 return XFASTINT (pos);
3672 /***********************************************************************
3673 Fontification
3674 ***********************************************************************/
3676 /* Handle changes in the `fontified' property of the current buffer by
3677 calling hook functions from Qfontification_functions to fontify
3678 regions of text. */
3680 static enum prop_handled
3681 handle_fontified_prop (struct it *it)
3683 Lisp_Object prop, pos;
3684 enum prop_handled handled = HANDLED_NORMALLY;
3686 if (!NILP (Vmemory_full))
3687 return handled;
3689 /* Get the value of the `fontified' property at IT's current buffer
3690 position. (The `fontified' property doesn't have a special
3691 meaning in strings.) If the value is nil, call functions from
3692 Qfontification_functions. */
3693 if (!STRINGP (it->string)
3694 && it->s == NULL
3695 && !NILP (Vfontification_functions)
3696 && !NILP (Vrun_hooks)
3697 && (pos = make_number (IT_CHARPOS (*it)),
3698 prop = Fget_char_property (pos, Qfontified, Qnil),
3699 /* Ignore the special cased nil value always present at EOB since
3700 no amount of fontifying will be able to change it. */
3701 NILP (prop) && IT_CHARPOS (*it) < Z))
3703 ptrdiff_t count = SPECPDL_INDEX ();
3704 Lisp_Object val;
3705 struct buffer *obuf = current_buffer;
3706 int begv = BEGV, zv = ZV;
3707 int old_clip_changed = current_buffer->clip_changed;
3709 val = Vfontification_functions;
3710 specbind (Qfontification_functions, Qnil);
3712 eassert (it->end_charpos == ZV);
3714 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3715 safe_call1 (val, pos);
3716 else
3718 Lisp_Object fns, fn;
3719 struct gcpro gcpro1, gcpro2;
3721 fns = Qnil;
3722 GCPRO2 (val, fns);
3724 for (; CONSP (val); val = XCDR (val))
3726 fn = XCAR (val);
3728 if (EQ (fn, Qt))
3730 /* A value of t indicates this hook has a local
3731 binding; it means to run the global binding too.
3732 In a global value, t should not occur. If it
3733 does, we must ignore it to avoid an endless
3734 loop. */
3735 for (fns = Fdefault_value (Qfontification_functions);
3736 CONSP (fns);
3737 fns = XCDR (fns))
3739 fn = XCAR (fns);
3740 if (!EQ (fn, Qt))
3741 safe_call1 (fn, pos);
3744 else
3745 safe_call1 (fn, pos);
3748 UNGCPRO;
3751 unbind_to (count, Qnil);
3753 /* Fontification functions routinely call `save-restriction'.
3754 Normally, this tags clip_changed, which can confuse redisplay
3755 (see discussion in Bug#6671). Since we don't perform any
3756 special handling of fontification changes in the case where
3757 `save-restriction' isn't called, there's no point doing so in
3758 this case either. So, if the buffer's restrictions are
3759 actually left unchanged, reset clip_changed. */
3760 if (obuf == current_buffer)
3762 if (begv == BEGV && zv == ZV)
3763 current_buffer->clip_changed = old_clip_changed;
3765 /* There isn't much we can reasonably do to protect against
3766 misbehaving fontification, but here's a fig leaf. */
3767 else if (BUFFER_LIVE_P (obuf))
3768 set_buffer_internal_1 (obuf);
3770 /* The fontification code may have added/removed text.
3771 It could do even a lot worse, but let's at least protect against
3772 the most obvious case where only the text past `pos' gets changed',
3773 as is/was done in grep.el where some escapes sequences are turned
3774 into face properties (bug#7876). */
3775 it->end_charpos = ZV;
3777 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3778 something. This avoids an endless loop if they failed to
3779 fontify the text for which reason ever. */
3780 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3781 handled = HANDLED_RECOMPUTE_PROPS;
3784 return handled;
3789 /***********************************************************************
3790 Faces
3791 ***********************************************************************/
3793 /* Set up iterator IT from face properties at its current position.
3794 Called from handle_stop. */
3796 static enum prop_handled
3797 handle_face_prop (struct it *it)
3799 int new_face_id;
3800 ptrdiff_t next_stop;
3802 if (!STRINGP (it->string))
3804 new_face_id
3805 = face_at_buffer_position (it->w,
3806 IT_CHARPOS (*it),
3807 it->region_beg_charpos,
3808 it->region_end_charpos,
3809 &next_stop,
3810 (IT_CHARPOS (*it)
3811 + TEXT_PROP_DISTANCE_LIMIT),
3812 0, it->base_face_id);
3814 /* Is this a start of a run of characters with box face?
3815 Caveat: this can be called for a freshly initialized
3816 iterator; face_id is -1 in this case. We know that the new
3817 face will not change until limit, i.e. if the new face has a
3818 box, all characters up to limit will have one. But, as
3819 usual, we don't know whether limit is really the end. */
3820 if (new_face_id != it->face_id)
3822 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3823 /* If it->face_id is -1, old_face below will be NULL, see
3824 the definition of FACE_FROM_ID. This will happen if this
3825 is the initial call that gets the face. */
3826 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3828 /* If the value of face_id of the iterator is -1, we have to
3829 look in front of IT's position and see whether there is a
3830 face there that's different from new_face_id. */
3831 if (!old_face && IT_CHARPOS (*it) > BEG)
3833 int prev_face_id = face_before_it_pos (it);
3835 old_face = FACE_FROM_ID (it->f, prev_face_id);
3838 /* If the new face has a box, but the old face does not,
3839 this is the start of a run of characters with box face,
3840 i.e. this character has a shadow on the left side. */
3841 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3842 && (old_face == NULL || !old_face->box));
3843 it->face_box_p = new_face->box != FACE_NO_BOX;
3846 else
3848 int base_face_id;
3849 ptrdiff_t bufpos;
3850 int i;
3851 Lisp_Object from_overlay
3852 = (it->current.overlay_string_index >= 0
3853 ? it->string_overlays[it->current.overlay_string_index
3854 % OVERLAY_STRING_CHUNK_SIZE]
3855 : Qnil);
3857 /* See if we got to this string directly or indirectly from
3858 an overlay property. That includes the before-string or
3859 after-string of an overlay, strings in display properties
3860 provided by an overlay, their text properties, etc.
3862 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3863 if (! NILP (from_overlay))
3864 for (i = it->sp - 1; i >= 0; i--)
3866 if (it->stack[i].current.overlay_string_index >= 0)
3867 from_overlay
3868 = it->string_overlays[it->stack[i].current.overlay_string_index
3869 % OVERLAY_STRING_CHUNK_SIZE];
3870 else if (! NILP (it->stack[i].from_overlay))
3871 from_overlay = it->stack[i].from_overlay;
3873 if (!NILP (from_overlay))
3874 break;
3877 if (! NILP (from_overlay))
3879 bufpos = IT_CHARPOS (*it);
3880 /* For a string from an overlay, the base face depends
3881 only on text properties and ignores overlays. */
3882 base_face_id
3883 = face_for_overlay_string (it->w,
3884 IT_CHARPOS (*it),
3885 it->region_beg_charpos,
3886 it->region_end_charpos,
3887 &next_stop,
3888 (IT_CHARPOS (*it)
3889 + TEXT_PROP_DISTANCE_LIMIT),
3891 from_overlay);
3893 else
3895 bufpos = 0;
3897 /* For strings from a `display' property, use the face at
3898 IT's current buffer position as the base face to merge
3899 with, so that overlay strings appear in the same face as
3900 surrounding text, unless they specify their own faces.
3901 For strings from wrap-prefix and line-prefix properties,
3902 use the default face, possibly remapped via
3903 Vface_remapping_alist. */
3904 base_face_id = it->string_from_prefix_prop_p
3905 ? (!NILP (Vface_remapping_alist)
3906 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3907 : DEFAULT_FACE_ID)
3908 : underlying_face_id (it);
3911 new_face_id = face_at_string_position (it->w,
3912 it->string,
3913 IT_STRING_CHARPOS (*it),
3914 bufpos,
3915 it->region_beg_charpos,
3916 it->region_end_charpos,
3917 &next_stop,
3918 base_face_id, 0);
3920 /* Is this a start of a run of characters with box? Caveat:
3921 this can be called for a freshly allocated iterator; face_id
3922 is -1 is this case. We know that the new face will not
3923 change until the next check pos, i.e. if the new face has a
3924 box, all characters up to that position will have a
3925 box. But, as usual, we don't know whether that position
3926 is really the end. */
3927 if (new_face_id != it->face_id)
3929 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3930 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3932 /* If new face has a box but old face hasn't, this is the
3933 start of a run of characters with box, i.e. it has a
3934 shadow on the left side. */
3935 it->start_of_box_run_p
3936 = new_face->box && (old_face == NULL || !old_face->box);
3937 it->face_box_p = new_face->box != FACE_NO_BOX;
3941 it->face_id = new_face_id;
3942 return HANDLED_NORMALLY;
3946 /* Return the ID of the face ``underlying'' IT's current position,
3947 which is in a string. If the iterator is associated with a
3948 buffer, return the face at IT's current buffer position.
3949 Otherwise, use the iterator's base_face_id. */
3951 static int
3952 underlying_face_id (struct it *it)
3954 int face_id = it->base_face_id, i;
3956 eassert (STRINGP (it->string));
3958 for (i = it->sp - 1; i >= 0; --i)
3959 if (NILP (it->stack[i].string))
3960 face_id = it->stack[i].face_id;
3962 return face_id;
3966 /* Compute the face one character before or after the current position
3967 of IT, in the visual order. BEFORE_P non-zero means get the face
3968 in front (to the left in L2R paragraphs, to the right in R2L
3969 paragraphs) of IT's screen position. Value is the ID of the face. */
3971 static int
3972 face_before_or_after_it_pos (struct it *it, int before_p)
3974 int face_id, limit;
3975 ptrdiff_t next_check_charpos;
3976 struct it it_copy;
3977 void *it_copy_data = NULL;
3979 eassert (it->s == NULL);
3981 if (STRINGP (it->string))
3983 ptrdiff_t bufpos, charpos;
3984 int base_face_id;
3986 /* No face change past the end of the string (for the case
3987 we are padding with spaces). No face change before the
3988 string start. */
3989 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3990 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3991 return it->face_id;
3993 if (!it->bidi_p)
3995 /* Set charpos to the position before or after IT's current
3996 position, in the logical order, which in the non-bidi
3997 case is the same as the visual order. */
3998 if (before_p)
3999 charpos = IT_STRING_CHARPOS (*it) - 1;
4000 else if (it->what == IT_COMPOSITION)
4001 /* For composition, we must check the character after the
4002 composition. */
4003 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4004 else
4005 charpos = IT_STRING_CHARPOS (*it) + 1;
4007 else
4009 if (before_p)
4011 /* With bidi iteration, the character before the current
4012 in the visual order cannot be found by simple
4013 iteration, because "reverse" reordering is not
4014 supported. Instead, we need to use the move_it_*
4015 family of functions. */
4016 /* Ignore face changes before the first visible
4017 character on this display line. */
4018 if (it->current_x <= it->first_visible_x)
4019 return it->face_id;
4020 SAVE_IT (it_copy, *it, it_copy_data);
4021 /* Implementation note: Since move_it_in_display_line
4022 works in the iterator geometry, and thinks the first
4023 character is always the leftmost, even in R2L lines,
4024 we don't need to distinguish between the R2L and L2R
4025 cases here. */
4026 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4027 it_copy.current_x - 1, MOVE_TO_X);
4028 charpos = IT_STRING_CHARPOS (it_copy);
4029 RESTORE_IT (it, it, it_copy_data);
4031 else
4033 /* Set charpos to the string position of the character
4034 that comes after IT's current position in the visual
4035 order. */
4036 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4038 it_copy = *it;
4039 while (n--)
4040 bidi_move_to_visually_next (&it_copy.bidi_it);
4042 charpos = it_copy.bidi_it.charpos;
4045 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4047 if (it->current.overlay_string_index >= 0)
4048 bufpos = IT_CHARPOS (*it);
4049 else
4050 bufpos = 0;
4052 base_face_id = underlying_face_id (it);
4054 /* Get the face for ASCII, or unibyte. */
4055 face_id = face_at_string_position (it->w,
4056 it->string,
4057 charpos,
4058 bufpos,
4059 it->region_beg_charpos,
4060 it->region_end_charpos,
4061 &next_check_charpos,
4062 base_face_id, 0);
4064 /* Correct the face for charsets different from ASCII. Do it
4065 for the multibyte case only. The face returned above is
4066 suitable for unibyte text if IT->string is unibyte. */
4067 if (STRING_MULTIBYTE (it->string))
4069 struct text_pos pos1 = string_pos (charpos, it->string);
4070 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4071 int c, len;
4072 struct face *face = FACE_FROM_ID (it->f, face_id);
4074 c = string_char_and_length (p, &len);
4075 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4078 else
4080 struct text_pos pos;
4082 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4083 || (IT_CHARPOS (*it) <= BEGV && before_p))
4084 return it->face_id;
4086 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4087 pos = it->current.pos;
4089 if (!it->bidi_p)
4091 if (before_p)
4092 DEC_TEXT_POS (pos, it->multibyte_p);
4093 else
4095 if (it->what == IT_COMPOSITION)
4097 /* For composition, we must check the position after
4098 the composition. */
4099 pos.charpos += it->cmp_it.nchars;
4100 pos.bytepos += it->len;
4102 else
4103 INC_TEXT_POS (pos, it->multibyte_p);
4106 else
4108 if (before_p)
4110 /* With bidi iteration, the character before the current
4111 in the visual order cannot be found by simple
4112 iteration, because "reverse" reordering is not
4113 supported. Instead, we need to use the move_it_*
4114 family of functions. */
4115 /* Ignore face changes before the first visible
4116 character on this display line. */
4117 if (it->current_x <= it->first_visible_x)
4118 return it->face_id;
4119 SAVE_IT (it_copy, *it, it_copy_data);
4120 /* Implementation note: Since move_it_in_display_line
4121 works in the iterator geometry, and thinks the first
4122 character is always the leftmost, even in R2L lines,
4123 we don't need to distinguish between the R2L and L2R
4124 cases here. */
4125 move_it_in_display_line (&it_copy, ZV,
4126 it_copy.current_x - 1, MOVE_TO_X);
4127 pos = it_copy.current.pos;
4128 RESTORE_IT (it, it, it_copy_data);
4130 else
4132 /* Set charpos to the buffer position of the character
4133 that comes after IT's current position in the visual
4134 order. */
4135 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4137 it_copy = *it;
4138 while (n--)
4139 bidi_move_to_visually_next (&it_copy.bidi_it);
4141 SET_TEXT_POS (pos,
4142 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4145 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4147 /* Determine face for CHARSET_ASCII, or unibyte. */
4148 face_id = face_at_buffer_position (it->w,
4149 CHARPOS (pos),
4150 it->region_beg_charpos,
4151 it->region_end_charpos,
4152 &next_check_charpos,
4153 limit, 0, -1);
4155 /* Correct the face for charsets different from ASCII. Do it
4156 for the multibyte case only. The face returned above is
4157 suitable for unibyte text if current_buffer is unibyte. */
4158 if (it->multibyte_p)
4160 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4161 struct face *face = FACE_FROM_ID (it->f, face_id);
4162 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4166 return face_id;
4171 /***********************************************************************
4172 Invisible text
4173 ***********************************************************************/
4175 /* Set up iterator IT from invisible properties at its current
4176 position. Called from handle_stop. */
4178 static enum prop_handled
4179 handle_invisible_prop (struct it *it)
4181 enum prop_handled handled = HANDLED_NORMALLY;
4182 int invis_p;
4183 Lisp_Object prop;
4185 if (STRINGP (it->string))
4187 Lisp_Object end_charpos, limit, charpos;
4189 /* Get the value of the invisible text property at the
4190 current position. Value will be nil if there is no such
4191 property. */
4192 charpos = make_number (IT_STRING_CHARPOS (*it));
4193 prop = Fget_text_property (charpos, Qinvisible, it->string);
4194 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4196 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4198 /* Record whether we have to display an ellipsis for the
4199 invisible text. */
4200 int display_ellipsis_p = (invis_p == 2);
4201 ptrdiff_t len, endpos;
4203 handled = HANDLED_RECOMPUTE_PROPS;
4205 /* Get the position at which the next visible text can be
4206 found in IT->string, if any. */
4207 endpos = len = SCHARS (it->string);
4208 XSETINT (limit, len);
4211 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4212 it->string, limit);
4213 if (INTEGERP (end_charpos))
4215 endpos = XFASTINT (end_charpos);
4216 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4217 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4218 if (invis_p == 2)
4219 display_ellipsis_p = 1;
4222 while (invis_p && endpos < len);
4224 if (display_ellipsis_p)
4225 it->ellipsis_p = 1;
4227 if (endpos < len)
4229 /* Text at END_CHARPOS is visible. Move IT there. */
4230 struct text_pos old;
4231 ptrdiff_t oldpos;
4233 old = it->current.string_pos;
4234 oldpos = CHARPOS (old);
4235 if (it->bidi_p)
4237 if (it->bidi_it.first_elt
4238 && it->bidi_it.charpos < SCHARS (it->string))
4239 bidi_paragraph_init (it->paragraph_embedding,
4240 &it->bidi_it, 1);
4241 /* Bidi-iterate out of the invisible text. */
4244 bidi_move_to_visually_next (&it->bidi_it);
4246 while (oldpos <= it->bidi_it.charpos
4247 && it->bidi_it.charpos < endpos);
4249 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4250 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4251 if (IT_CHARPOS (*it) >= endpos)
4252 it->prev_stop = endpos;
4254 else
4256 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4257 compute_string_pos (&it->current.string_pos, old, it->string);
4260 else
4262 /* The rest of the string is invisible. If this is an
4263 overlay string, proceed with the next overlay string
4264 or whatever comes and return a character from there. */
4265 if (it->current.overlay_string_index >= 0
4266 && !display_ellipsis_p)
4268 next_overlay_string (it);
4269 /* Don't check for overlay strings when we just
4270 finished processing them. */
4271 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4273 else
4275 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4276 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4281 else
4283 ptrdiff_t newpos, next_stop, start_charpos, tem;
4284 Lisp_Object pos, overlay;
4286 /* First of all, is there invisible text at this position? */
4287 tem = start_charpos = IT_CHARPOS (*it);
4288 pos = make_number (tem);
4289 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4290 &overlay);
4291 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4293 /* If we are on invisible text, skip over it. */
4294 if (invis_p && start_charpos < it->end_charpos)
4296 /* Record whether we have to display an ellipsis for the
4297 invisible text. */
4298 int display_ellipsis_p = invis_p == 2;
4300 handled = HANDLED_RECOMPUTE_PROPS;
4302 /* Loop skipping over invisible text. The loop is left at
4303 ZV or with IT on the first char being visible again. */
4306 /* Try to skip some invisible text. Return value is the
4307 position reached which can be equal to where we start
4308 if there is nothing invisible there. This skips both
4309 over invisible text properties and overlays with
4310 invisible property. */
4311 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4313 /* If we skipped nothing at all we weren't at invisible
4314 text in the first place. If everything to the end of
4315 the buffer was skipped, end the loop. */
4316 if (newpos == tem || newpos >= ZV)
4317 invis_p = 0;
4318 else
4320 /* We skipped some characters but not necessarily
4321 all there are. Check if we ended up on visible
4322 text. Fget_char_property returns the property of
4323 the char before the given position, i.e. if we
4324 get invis_p = 0, this means that the char at
4325 newpos is visible. */
4326 pos = make_number (newpos);
4327 prop = Fget_char_property (pos, Qinvisible, it->window);
4328 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4331 /* If we ended up on invisible text, proceed to
4332 skip starting with next_stop. */
4333 if (invis_p)
4334 tem = next_stop;
4336 /* If there are adjacent invisible texts, don't lose the
4337 second one's ellipsis. */
4338 if (invis_p == 2)
4339 display_ellipsis_p = 1;
4341 while (invis_p);
4343 /* The position newpos is now either ZV or on visible text. */
4344 if (it->bidi_p)
4346 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4347 int on_newline =
4348 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4349 int after_newline =
4350 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4352 /* If the invisible text ends on a newline or on a
4353 character after a newline, we can avoid the costly,
4354 character by character, bidi iteration to NEWPOS, and
4355 instead simply reseat the iterator there. That's
4356 because all bidi reordering information is tossed at
4357 the newline. This is a big win for modes that hide
4358 complete lines, like Outline, Org, etc. */
4359 if (on_newline || after_newline)
4361 struct text_pos tpos;
4362 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4364 SET_TEXT_POS (tpos, newpos, bpos);
4365 reseat_1 (it, tpos, 0);
4366 /* If we reseat on a newline/ZV, we need to prep the
4367 bidi iterator for advancing to the next character
4368 after the newline/EOB, keeping the current paragraph
4369 direction (so that PRODUCE_GLYPHS does TRT wrt
4370 prepending/appending glyphs to a glyph row). */
4371 if (on_newline)
4373 it->bidi_it.first_elt = 0;
4374 it->bidi_it.paragraph_dir = pdir;
4375 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4376 it->bidi_it.nchars = 1;
4377 it->bidi_it.ch_len = 1;
4380 else /* Must use the slow method. */
4382 /* With bidi iteration, the region of invisible text
4383 could start and/or end in the middle of a
4384 non-base embedding level. Therefore, we need to
4385 skip invisible text using the bidi iterator,
4386 starting at IT's current position, until we find
4387 ourselves outside of the invisible text.
4388 Skipping invisible text _after_ bidi iteration
4389 avoids affecting the visual order of the
4390 displayed text when invisible properties are
4391 added or removed. */
4392 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4394 /* If we were `reseat'ed to a new paragraph,
4395 determine the paragraph base direction. We
4396 need to do it now because
4397 next_element_from_buffer may not have a
4398 chance to do it, if we are going to skip any
4399 text at the beginning, which resets the
4400 FIRST_ELT flag. */
4401 bidi_paragraph_init (it->paragraph_embedding,
4402 &it->bidi_it, 1);
4406 bidi_move_to_visually_next (&it->bidi_it);
4408 while (it->stop_charpos <= it->bidi_it.charpos
4409 && it->bidi_it.charpos < newpos);
4410 IT_CHARPOS (*it) = it->bidi_it.charpos;
4411 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4412 /* If we overstepped NEWPOS, record its position in
4413 the iterator, so that we skip invisible text if
4414 later the bidi iteration lands us in the
4415 invisible region again. */
4416 if (IT_CHARPOS (*it) >= newpos)
4417 it->prev_stop = newpos;
4420 else
4422 IT_CHARPOS (*it) = newpos;
4423 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4426 /* If there are before-strings at the start of invisible
4427 text, and the text is invisible because of a text
4428 property, arrange to show before-strings because 20.x did
4429 it that way. (If the text is invisible because of an
4430 overlay property instead of a text property, this is
4431 already handled in the overlay code.) */
4432 if (NILP (overlay)
4433 && get_overlay_strings (it, it->stop_charpos))
4435 handled = HANDLED_RECOMPUTE_PROPS;
4436 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4438 else if (display_ellipsis_p)
4440 /* Make sure that the glyphs of the ellipsis will get
4441 correct `charpos' values. If we would not update
4442 it->position here, the glyphs would belong to the
4443 last visible character _before_ the invisible
4444 text, which confuses `set_cursor_from_row'.
4446 We use the last invisible position instead of the
4447 first because this way the cursor is always drawn on
4448 the first "." of the ellipsis, whenever PT is inside
4449 the invisible text. Otherwise the cursor would be
4450 placed _after_ the ellipsis when the point is after the
4451 first invisible character. */
4452 if (!STRINGP (it->object))
4454 it->position.charpos = newpos - 1;
4455 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4457 it->ellipsis_p = 1;
4458 /* Let the ellipsis display before
4459 considering any properties of the following char.
4460 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4461 handled = HANDLED_RETURN;
4466 return handled;
4470 /* Make iterator IT return `...' next.
4471 Replaces LEN characters from buffer. */
4473 static void
4474 setup_for_ellipsis (struct it *it, int len)
4476 /* Use the display table definition for `...'. Invalid glyphs
4477 will be handled by the method returning elements from dpvec. */
4478 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4480 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4481 it->dpvec = v->contents;
4482 it->dpend = v->contents + v->header.size;
4484 else
4486 /* Default `...'. */
4487 it->dpvec = default_invis_vector;
4488 it->dpend = default_invis_vector + 3;
4491 it->dpvec_char_len = len;
4492 it->current.dpvec_index = 0;
4493 it->dpvec_face_id = -1;
4495 /* Remember the current face id in case glyphs specify faces.
4496 IT's face is restored in set_iterator_to_next.
4497 saved_face_id was set to preceding char's face in handle_stop. */
4498 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4499 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4501 it->method = GET_FROM_DISPLAY_VECTOR;
4502 it->ellipsis_p = 1;
4507 /***********************************************************************
4508 'display' property
4509 ***********************************************************************/
4511 /* Set up iterator IT from `display' property at its current position.
4512 Called from handle_stop.
4513 We return HANDLED_RETURN if some part of the display property
4514 overrides the display of the buffer text itself.
4515 Otherwise we return HANDLED_NORMALLY. */
4517 static enum prop_handled
4518 handle_display_prop (struct it *it)
4520 Lisp_Object propval, object, overlay;
4521 struct text_pos *position;
4522 ptrdiff_t bufpos;
4523 /* Nonzero if some property replaces the display of the text itself. */
4524 int display_replaced_p = 0;
4526 if (STRINGP (it->string))
4528 object = it->string;
4529 position = &it->current.string_pos;
4530 bufpos = CHARPOS (it->current.pos);
4532 else
4534 XSETWINDOW (object, it->w);
4535 position = &it->current.pos;
4536 bufpos = CHARPOS (*position);
4539 /* Reset those iterator values set from display property values. */
4540 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4541 it->space_width = Qnil;
4542 it->font_height = Qnil;
4543 it->voffset = 0;
4545 /* We don't support recursive `display' properties, i.e. string
4546 values that have a string `display' property, that have a string
4547 `display' property etc. */
4548 if (!it->string_from_display_prop_p)
4549 it->area = TEXT_AREA;
4551 propval = get_char_property_and_overlay (make_number (position->charpos),
4552 Qdisplay, object, &overlay);
4553 if (NILP (propval))
4554 return HANDLED_NORMALLY;
4555 /* Now OVERLAY is the overlay that gave us this property, or nil
4556 if it was a text property. */
4558 if (!STRINGP (it->string))
4559 object = it->w->contents;
4561 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4562 position, bufpos,
4563 FRAME_WINDOW_P (it->f));
4565 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4568 /* Subroutine of handle_display_prop. Returns non-zero if the display
4569 specification in SPEC is a replacing specification, i.e. it would
4570 replace the text covered by `display' property with something else,
4571 such as an image or a display string. If SPEC includes any kind or
4572 `(space ...) specification, the value is 2; this is used by
4573 compute_display_string_pos, which see.
4575 See handle_single_display_spec for documentation of arguments.
4576 frame_window_p is non-zero if the window being redisplayed is on a
4577 GUI frame; this argument is used only if IT is NULL, see below.
4579 IT can be NULL, if this is called by the bidi reordering code
4580 through compute_display_string_pos, which see. In that case, this
4581 function only examines SPEC, but does not otherwise "handle" it, in
4582 the sense that it doesn't set up members of IT from the display
4583 spec. */
4584 static int
4585 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4586 Lisp_Object overlay, struct text_pos *position,
4587 ptrdiff_t bufpos, int frame_window_p)
4589 int replacing_p = 0;
4590 int rv;
4592 if (CONSP (spec)
4593 /* Simple specifications. */
4594 && !EQ (XCAR (spec), Qimage)
4595 && !EQ (XCAR (spec), Qspace)
4596 && !EQ (XCAR (spec), Qwhen)
4597 && !EQ (XCAR (spec), Qslice)
4598 && !EQ (XCAR (spec), Qspace_width)
4599 && !EQ (XCAR (spec), Qheight)
4600 && !EQ (XCAR (spec), Qraise)
4601 /* Marginal area specifications. */
4602 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4603 && !EQ (XCAR (spec), Qleft_fringe)
4604 && !EQ (XCAR (spec), Qright_fringe)
4605 && !NILP (XCAR (spec)))
4607 for (; CONSP (spec); spec = XCDR (spec))
4609 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4610 overlay, position, bufpos,
4611 replacing_p, frame_window_p)))
4613 replacing_p = rv;
4614 /* If some text in a string is replaced, `position' no
4615 longer points to the position of `object'. */
4616 if (!it || STRINGP (object))
4617 break;
4621 else if (VECTORP (spec))
4623 ptrdiff_t i;
4624 for (i = 0; i < ASIZE (spec); ++i)
4625 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4626 overlay, position, bufpos,
4627 replacing_p, frame_window_p)))
4629 replacing_p = rv;
4630 /* If some text in a string is replaced, `position' no
4631 longer points to the position of `object'. */
4632 if (!it || STRINGP (object))
4633 break;
4636 else
4638 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4639 position, bufpos, 0,
4640 frame_window_p)))
4641 replacing_p = rv;
4644 return replacing_p;
4647 /* Value is the position of the end of the `display' property starting
4648 at START_POS in OBJECT. */
4650 static struct text_pos
4651 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4653 Lisp_Object end;
4654 struct text_pos end_pos;
4656 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4657 Qdisplay, object, Qnil);
4658 CHARPOS (end_pos) = XFASTINT (end);
4659 if (STRINGP (object))
4660 compute_string_pos (&end_pos, start_pos, it->string);
4661 else
4662 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4664 return end_pos;
4668 /* Set up IT from a single `display' property specification SPEC. OBJECT
4669 is the object in which the `display' property was found. *POSITION
4670 is the position in OBJECT at which the `display' property was found.
4671 BUFPOS is the buffer position of OBJECT (different from POSITION if
4672 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4673 previously saw a display specification which already replaced text
4674 display with something else, for example an image; we ignore such
4675 properties after the first one has been processed.
4677 OVERLAY is the overlay this `display' property came from,
4678 or nil if it was a text property.
4680 If SPEC is a `space' or `image' specification, and in some other
4681 cases too, set *POSITION to the position where the `display'
4682 property ends.
4684 If IT is NULL, only examine the property specification in SPEC, but
4685 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4686 is intended to be displayed in a window on a GUI frame.
4688 Value is non-zero if something was found which replaces the display
4689 of buffer or string text. */
4691 static int
4692 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4693 Lisp_Object overlay, struct text_pos *position,
4694 ptrdiff_t bufpos, int display_replaced_p,
4695 int frame_window_p)
4697 Lisp_Object form;
4698 Lisp_Object location, value;
4699 struct text_pos start_pos = *position;
4700 int valid_p;
4702 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4703 If the result is non-nil, use VALUE instead of SPEC. */
4704 form = Qt;
4705 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4707 spec = XCDR (spec);
4708 if (!CONSP (spec))
4709 return 0;
4710 form = XCAR (spec);
4711 spec = XCDR (spec);
4714 if (!NILP (form) && !EQ (form, Qt))
4716 ptrdiff_t count = SPECPDL_INDEX ();
4717 struct gcpro gcpro1;
4719 /* Bind `object' to the object having the `display' property, a
4720 buffer or string. Bind `position' to the position in the
4721 object where the property was found, and `buffer-position'
4722 to the current position in the buffer. */
4724 if (NILP (object))
4725 XSETBUFFER (object, current_buffer);
4726 specbind (Qobject, object);
4727 specbind (Qposition, make_number (CHARPOS (*position)));
4728 specbind (Qbuffer_position, make_number (bufpos));
4729 GCPRO1 (form);
4730 form = safe_eval (form);
4731 UNGCPRO;
4732 unbind_to (count, Qnil);
4735 if (NILP (form))
4736 return 0;
4738 /* Handle `(height HEIGHT)' specifications. */
4739 if (CONSP (spec)
4740 && EQ (XCAR (spec), Qheight)
4741 && CONSP (XCDR (spec)))
4743 if (it)
4745 if (!FRAME_WINDOW_P (it->f))
4746 return 0;
4748 it->font_height = XCAR (XCDR (spec));
4749 if (!NILP (it->font_height))
4751 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4752 int new_height = -1;
4754 if (CONSP (it->font_height)
4755 && (EQ (XCAR (it->font_height), Qplus)
4756 || EQ (XCAR (it->font_height), Qminus))
4757 && CONSP (XCDR (it->font_height))
4758 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4760 /* `(+ N)' or `(- N)' where N is an integer. */
4761 int steps = XINT (XCAR (XCDR (it->font_height)));
4762 if (EQ (XCAR (it->font_height), Qplus))
4763 steps = - steps;
4764 it->face_id = smaller_face (it->f, it->face_id, steps);
4766 else if (FUNCTIONP (it->font_height))
4768 /* Call function with current height as argument.
4769 Value is the new height. */
4770 Lisp_Object height;
4771 height = safe_call1 (it->font_height,
4772 face->lface[LFACE_HEIGHT_INDEX]);
4773 if (NUMBERP (height))
4774 new_height = XFLOATINT (height);
4776 else if (NUMBERP (it->font_height))
4778 /* Value is a multiple of the canonical char height. */
4779 struct face *f;
4781 f = FACE_FROM_ID (it->f,
4782 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4783 new_height = (XFLOATINT (it->font_height)
4784 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4786 else
4788 /* Evaluate IT->font_height with `height' bound to the
4789 current specified height to get the new height. */
4790 ptrdiff_t count = SPECPDL_INDEX ();
4792 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4793 value = safe_eval (it->font_height);
4794 unbind_to (count, Qnil);
4796 if (NUMBERP (value))
4797 new_height = XFLOATINT (value);
4800 if (new_height > 0)
4801 it->face_id = face_with_height (it->f, it->face_id, new_height);
4805 return 0;
4808 /* Handle `(space-width WIDTH)'. */
4809 if (CONSP (spec)
4810 && EQ (XCAR (spec), Qspace_width)
4811 && CONSP (XCDR (spec)))
4813 if (it)
4815 if (!FRAME_WINDOW_P (it->f))
4816 return 0;
4818 value = XCAR (XCDR (spec));
4819 if (NUMBERP (value) && XFLOATINT (value) > 0)
4820 it->space_width = value;
4823 return 0;
4826 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4827 if (CONSP (spec)
4828 && EQ (XCAR (spec), Qslice))
4830 Lisp_Object tem;
4832 if (it)
4834 if (!FRAME_WINDOW_P (it->f))
4835 return 0;
4837 if (tem = XCDR (spec), CONSP (tem))
4839 it->slice.x = XCAR (tem);
4840 if (tem = XCDR (tem), CONSP (tem))
4842 it->slice.y = XCAR (tem);
4843 if (tem = XCDR (tem), CONSP (tem))
4845 it->slice.width = XCAR (tem);
4846 if (tem = XCDR (tem), CONSP (tem))
4847 it->slice.height = XCAR (tem);
4853 return 0;
4856 /* Handle `(raise FACTOR)'. */
4857 if (CONSP (spec)
4858 && EQ (XCAR (spec), Qraise)
4859 && CONSP (XCDR (spec)))
4861 if (it)
4863 if (!FRAME_WINDOW_P (it->f))
4864 return 0;
4866 #ifdef HAVE_WINDOW_SYSTEM
4867 value = XCAR (XCDR (spec));
4868 if (NUMBERP (value))
4870 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4871 it->voffset = - (XFLOATINT (value)
4872 * (FONT_HEIGHT (face->font)));
4874 #endif /* HAVE_WINDOW_SYSTEM */
4877 return 0;
4880 /* Don't handle the other kinds of display specifications
4881 inside a string that we got from a `display' property. */
4882 if (it && it->string_from_display_prop_p)
4883 return 0;
4885 /* Characters having this form of property are not displayed, so
4886 we have to find the end of the property. */
4887 if (it)
4889 start_pos = *position;
4890 *position = display_prop_end (it, object, start_pos);
4892 value = Qnil;
4894 /* Stop the scan at that end position--we assume that all
4895 text properties change there. */
4896 if (it)
4897 it->stop_charpos = position->charpos;
4899 /* Handle `(left-fringe BITMAP [FACE])'
4900 and `(right-fringe BITMAP [FACE])'. */
4901 if (CONSP (spec)
4902 && (EQ (XCAR (spec), Qleft_fringe)
4903 || EQ (XCAR (spec), Qright_fringe))
4904 && CONSP (XCDR (spec)))
4906 int fringe_bitmap;
4908 if (it)
4910 if (!FRAME_WINDOW_P (it->f))
4911 /* If we return here, POSITION has been advanced
4912 across the text with this property. */
4914 /* Synchronize the bidi iterator with POSITION. This is
4915 needed because we are not going to push the iterator
4916 on behalf of this display property, so there will be
4917 no pop_it call to do this synchronization for us. */
4918 if (it->bidi_p)
4920 it->position = *position;
4921 iterate_out_of_display_property (it);
4922 *position = it->position;
4924 return 1;
4927 else if (!frame_window_p)
4928 return 1;
4930 #ifdef HAVE_WINDOW_SYSTEM
4931 value = XCAR (XCDR (spec));
4932 if (!SYMBOLP (value)
4933 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4934 /* If we return here, POSITION has been advanced
4935 across the text with this property. */
4937 if (it && it->bidi_p)
4939 it->position = *position;
4940 iterate_out_of_display_property (it);
4941 *position = it->position;
4943 return 1;
4946 if (it)
4948 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4950 if (CONSP (XCDR (XCDR (spec))))
4952 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4953 int face_id2 = lookup_derived_face (it->f, face_name,
4954 FRINGE_FACE_ID, 0);
4955 if (face_id2 >= 0)
4956 face_id = face_id2;
4959 /* Save current settings of IT so that we can restore them
4960 when we are finished with the glyph property value. */
4961 push_it (it, position);
4963 it->area = TEXT_AREA;
4964 it->what = IT_IMAGE;
4965 it->image_id = -1; /* no image */
4966 it->position = start_pos;
4967 it->object = NILP (object) ? it->w->contents : object;
4968 it->method = GET_FROM_IMAGE;
4969 it->from_overlay = Qnil;
4970 it->face_id = face_id;
4971 it->from_disp_prop_p = 1;
4973 /* Say that we haven't consumed the characters with
4974 `display' property yet. The call to pop_it in
4975 set_iterator_to_next will clean this up. */
4976 *position = start_pos;
4978 if (EQ (XCAR (spec), Qleft_fringe))
4980 it->left_user_fringe_bitmap = fringe_bitmap;
4981 it->left_user_fringe_face_id = face_id;
4983 else
4985 it->right_user_fringe_bitmap = fringe_bitmap;
4986 it->right_user_fringe_face_id = face_id;
4989 #endif /* HAVE_WINDOW_SYSTEM */
4990 return 1;
4993 /* Prepare to handle `((margin left-margin) ...)',
4994 `((margin right-margin) ...)' and `((margin nil) ...)'
4995 prefixes for display specifications. */
4996 location = Qunbound;
4997 if (CONSP (spec) && CONSP (XCAR (spec)))
4999 Lisp_Object tem;
5001 value = XCDR (spec);
5002 if (CONSP (value))
5003 value = XCAR (value);
5005 tem = XCAR (spec);
5006 if (EQ (XCAR (tem), Qmargin)
5007 && (tem = XCDR (tem),
5008 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5009 (NILP (tem)
5010 || EQ (tem, Qleft_margin)
5011 || EQ (tem, Qright_margin))))
5012 location = tem;
5015 if (EQ (location, Qunbound))
5017 location = Qnil;
5018 value = spec;
5021 /* After this point, VALUE is the property after any
5022 margin prefix has been stripped. It must be a string,
5023 an image specification, or `(space ...)'.
5025 LOCATION specifies where to display: `left-margin',
5026 `right-margin' or nil. */
5028 valid_p = (STRINGP (value)
5029 #ifdef HAVE_WINDOW_SYSTEM
5030 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5031 && valid_image_p (value))
5032 #endif /* not HAVE_WINDOW_SYSTEM */
5033 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5035 if (valid_p && !display_replaced_p)
5037 int retval = 1;
5039 if (!it)
5041 /* Callers need to know whether the display spec is any kind
5042 of `(space ...)' spec that is about to affect text-area
5043 display. */
5044 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5045 retval = 2;
5046 return retval;
5049 /* Save current settings of IT so that we can restore them
5050 when we are finished with the glyph property value. */
5051 push_it (it, position);
5052 it->from_overlay = overlay;
5053 it->from_disp_prop_p = 1;
5055 if (NILP (location))
5056 it->area = TEXT_AREA;
5057 else if (EQ (location, Qleft_margin))
5058 it->area = LEFT_MARGIN_AREA;
5059 else
5060 it->area = RIGHT_MARGIN_AREA;
5062 if (STRINGP (value))
5064 it->string = value;
5065 it->multibyte_p = STRING_MULTIBYTE (it->string);
5066 it->current.overlay_string_index = -1;
5067 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5068 it->end_charpos = it->string_nchars = SCHARS (it->string);
5069 it->method = GET_FROM_STRING;
5070 it->stop_charpos = 0;
5071 it->prev_stop = 0;
5072 it->base_level_stop = 0;
5073 it->string_from_display_prop_p = 1;
5074 /* Say that we haven't consumed the characters with
5075 `display' property yet. The call to pop_it in
5076 set_iterator_to_next will clean this up. */
5077 if (BUFFERP (object))
5078 *position = start_pos;
5080 /* Force paragraph direction to be that of the parent
5081 object. If the parent object's paragraph direction is
5082 not yet determined, default to L2R. */
5083 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5084 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5085 else
5086 it->paragraph_embedding = L2R;
5088 /* Set up the bidi iterator for this display string. */
5089 if (it->bidi_p)
5091 it->bidi_it.string.lstring = it->string;
5092 it->bidi_it.string.s = NULL;
5093 it->bidi_it.string.schars = it->end_charpos;
5094 it->bidi_it.string.bufpos = bufpos;
5095 it->bidi_it.string.from_disp_str = 1;
5096 it->bidi_it.string.unibyte = !it->multibyte_p;
5097 it->bidi_it.w = it->w;
5098 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5101 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5103 it->method = GET_FROM_STRETCH;
5104 it->object = value;
5105 *position = it->position = start_pos;
5106 retval = 1 + (it->area == TEXT_AREA);
5108 #ifdef HAVE_WINDOW_SYSTEM
5109 else
5111 it->what = IT_IMAGE;
5112 it->image_id = lookup_image (it->f, value);
5113 it->position = start_pos;
5114 it->object = NILP (object) ? it->w->contents : object;
5115 it->method = GET_FROM_IMAGE;
5117 /* Say that we haven't consumed the characters with
5118 `display' property yet. The call to pop_it in
5119 set_iterator_to_next will clean this up. */
5120 *position = start_pos;
5122 #endif /* HAVE_WINDOW_SYSTEM */
5124 return retval;
5127 /* Invalid property or property not supported. Restore
5128 POSITION to what it was before. */
5129 *position = start_pos;
5130 return 0;
5133 /* Check if PROP is a display property value whose text should be
5134 treated as intangible. OVERLAY is the overlay from which PROP
5135 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5136 specify the buffer position covered by PROP. */
5139 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5140 ptrdiff_t charpos, ptrdiff_t bytepos)
5142 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5143 struct text_pos position;
5145 SET_TEXT_POS (position, charpos, bytepos);
5146 return handle_display_spec (NULL, prop, Qnil, overlay,
5147 &position, charpos, frame_window_p);
5151 /* Return 1 if PROP is a display sub-property value containing STRING.
5153 Implementation note: this and the following function are really
5154 special cases of handle_display_spec and
5155 handle_single_display_spec, and should ideally use the same code.
5156 Until they do, these two pairs must be consistent and must be
5157 modified in sync. */
5159 static int
5160 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5162 if (EQ (string, prop))
5163 return 1;
5165 /* Skip over `when FORM'. */
5166 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5168 prop = XCDR (prop);
5169 if (!CONSP (prop))
5170 return 0;
5171 /* Actually, the condition following `when' should be eval'ed,
5172 like handle_single_display_spec does, and we should return
5173 zero if it evaluates to nil. However, this function is
5174 called only when the buffer was already displayed and some
5175 glyph in the glyph matrix was found to come from a display
5176 string. Therefore, the condition was already evaluated, and
5177 the result was non-nil, otherwise the display string wouldn't
5178 have been displayed and we would have never been called for
5179 this property. Thus, we can skip the evaluation and assume
5180 its result is non-nil. */
5181 prop = XCDR (prop);
5184 if (CONSP (prop))
5185 /* Skip over `margin LOCATION'. */
5186 if (EQ (XCAR (prop), Qmargin))
5188 prop = XCDR (prop);
5189 if (!CONSP (prop))
5190 return 0;
5192 prop = XCDR (prop);
5193 if (!CONSP (prop))
5194 return 0;
5197 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5201 /* Return 1 if STRING appears in the `display' property PROP. */
5203 static int
5204 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5206 if (CONSP (prop)
5207 && !EQ (XCAR (prop), Qwhen)
5208 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5210 /* A list of sub-properties. */
5211 while (CONSP (prop))
5213 if (single_display_spec_string_p (XCAR (prop), string))
5214 return 1;
5215 prop = XCDR (prop);
5218 else if (VECTORP (prop))
5220 /* A vector of sub-properties. */
5221 ptrdiff_t i;
5222 for (i = 0; i < ASIZE (prop); ++i)
5223 if (single_display_spec_string_p (AREF (prop, i), string))
5224 return 1;
5226 else
5227 return single_display_spec_string_p (prop, string);
5229 return 0;
5232 /* Look for STRING in overlays and text properties in the current
5233 buffer, between character positions FROM and TO (excluding TO).
5234 BACK_P non-zero means look back (in this case, TO is supposed to be
5235 less than FROM).
5236 Value is the first character position where STRING was found, or
5237 zero if it wasn't found before hitting TO.
5239 This function may only use code that doesn't eval because it is
5240 called asynchronously from note_mouse_highlight. */
5242 static ptrdiff_t
5243 string_buffer_position_lim (Lisp_Object string,
5244 ptrdiff_t from, ptrdiff_t to, int back_p)
5246 Lisp_Object limit, prop, pos;
5247 int found = 0;
5249 pos = make_number (max (from, BEGV));
5251 if (!back_p) /* looking forward */
5253 limit = make_number (min (to, ZV));
5254 while (!found && !EQ (pos, limit))
5256 prop = Fget_char_property (pos, Qdisplay, Qnil);
5257 if (!NILP (prop) && display_prop_string_p (prop, string))
5258 found = 1;
5259 else
5260 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5261 limit);
5264 else /* looking back */
5266 limit = make_number (max (to, BEGV));
5267 while (!found && !EQ (pos, limit))
5269 prop = Fget_char_property (pos, Qdisplay, Qnil);
5270 if (!NILP (prop) && display_prop_string_p (prop, string))
5271 found = 1;
5272 else
5273 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5274 limit);
5278 return found ? XINT (pos) : 0;
5281 /* Determine which buffer position in current buffer STRING comes from.
5282 AROUND_CHARPOS is an approximate position where it could come from.
5283 Value is the buffer position or 0 if it couldn't be determined.
5285 This function is necessary because we don't record buffer positions
5286 in glyphs generated from strings (to keep struct glyph small).
5287 This function may only use code that doesn't eval because it is
5288 called asynchronously from note_mouse_highlight. */
5290 static ptrdiff_t
5291 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5293 const int MAX_DISTANCE = 1000;
5294 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5295 around_charpos + MAX_DISTANCE,
5298 if (!found)
5299 found = string_buffer_position_lim (string, around_charpos,
5300 around_charpos - MAX_DISTANCE, 1);
5301 return found;
5306 /***********************************************************************
5307 `composition' property
5308 ***********************************************************************/
5310 /* Set up iterator IT from `composition' property at its current
5311 position. Called from handle_stop. */
5313 static enum prop_handled
5314 handle_composition_prop (struct it *it)
5316 Lisp_Object prop, string;
5317 ptrdiff_t pos, pos_byte, start, end;
5319 if (STRINGP (it->string))
5321 unsigned char *s;
5323 pos = IT_STRING_CHARPOS (*it);
5324 pos_byte = IT_STRING_BYTEPOS (*it);
5325 string = it->string;
5326 s = SDATA (string) + pos_byte;
5327 it->c = STRING_CHAR (s);
5329 else
5331 pos = IT_CHARPOS (*it);
5332 pos_byte = IT_BYTEPOS (*it);
5333 string = Qnil;
5334 it->c = FETCH_CHAR (pos_byte);
5337 /* If there's a valid composition and point is not inside of the
5338 composition (in the case that the composition is from the current
5339 buffer), draw a glyph composed from the composition components. */
5340 if (find_composition (pos, -1, &start, &end, &prop, string)
5341 && composition_valid_p (start, end, prop)
5342 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5344 if (start < pos)
5345 /* As we can't handle this situation (perhaps font-lock added
5346 a new composition), we just return here hoping that next
5347 redisplay will detect this composition much earlier. */
5348 return HANDLED_NORMALLY;
5349 if (start != pos)
5351 if (STRINGP (it->string))
5352 pos_byte = string_char_to_byte (it->string, start);
5353 else
5354 pos_byte = CHAR_TO_BYTE (start);
5356 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5357 prop, string);
5359 if (it->cmp_it.id >= 0)
5361 it->cmp_it.ch = -1;
5362 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5363 it->cmp_it.nglyphs = -1;
5367 return HANDLED_NORMALLY;
5372 /***********************************************************************
5373 Overlay strings
5374 ***********************************************************************/
5376 /* The following structure is used to record overlay strings for
5377 later sorting in load_overlay_strings. */
5379 struct overlay_entry
5381 Lisp_Object overlay;
5382 Lisp_Object string;
5383 EMACS_INT priority;
5384 int after_string_p;
5388 /* Set up iterator IT from overlay strings at its current position.
5389 Called from handle_stop. */
5391 static enum prop_handled
5392 handle_overlay_change (struct it *it)
5394 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5395 return HANDLED_RECOMPUTE_PROPS;
5396 else
5397 return HANDLED_NORMALLY;
5401 /* Set up the next overlay string for delivery by IT, if there is an
5402 overlay string to deliver. Called by set_iterator_to_next when the
5403 end of the current overlay string is reached. If there are more
5404 overlay strings to display, IT->string and
5405 IT->current.overlay_string_index are set appropriately here.
5406 Otherwise IT->string is set to nil. */
5408 static void
5409 next_overlay_string (struct it *it)
5411 ++it->current.overlay_string_index;
5412 if (it->current.overlay_string_index == it->n_overlay_strings)
5414 /* No more overlay strings. Restore IT's settings to what
5415 they were before overlay strings were processed, and
5416 continue to deliver from current_buffer. */
5418 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5419 pop_it (it);
5420 eassert (it->sp > 0
5421 || (NILP (it->string)
5422 && it->method == GET_FROM_BUFFER
5423 && it->stop_charpos >= BEGV
5424 && it->stop_charpos <= it->end_charpos));
5425 it->current.overlay_string_index = -1;
5426 it->n_overlay_strings = 0;
5427 it->overlay_strings_charpos = -1;
5428 /* If there's an empty display string on the stack, pop the
5429 stack, to resync the bidi iterator with IT's position. Such
5430 empty strings are pushed onto the stack in
5431 get_overlay_strings_1. */
5432 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5433 pop_it (it);
5435 /* If we're at the end of the buffer, record that we have
5436 processed the overlay strings there already, so that
5437 next_element_from_buffer doesn't try it again. */
5438 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5439 it->overlay_strings_at_end_processed_p = 1;
5441 else
5443 /* There are more overlay strings to process. If
5444 IT->current.overlay_string_index has advanced to a position
5445 where we must load IT->overlay_strings with more strings, do
5446 it. We must load at the IT->overlay_strings_charpos where
5447 IT->n_overlay_strings was originally computed; when invisible
5448 text is present, this might not be IT_CHARPOS (Bug#7016). */
5449 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5451 if (it->current.overlay_string_index && i == 0)
5452 load_overlay_strings (it, it->overlay_strings_charpos);
5454 /* Initialize IT to deliver display elements from the overlay
5455 string. */
5456 it->string = it->overlay_strings[i];
5457 it->multibyte_p = STRING_MULTIBYTE (it->string);
5458 SET_TEXT_POS (it->current.string_pos, 0, 0);
5459 it->method = GET_FROM_STRING;
5460 it->stop_charpos = 0;
5461 it->end_charpos = SCHARS (it->string);
5462 if (it->cmp_it.stop_pos >= 0)
5463 it->cmp_it.stop_pos = 0;
5464 it->prev_stop = 0;
5465 it->base_level_stop = 0;
5467 /* Set up the bidi iterator for this overlay string. */
5468 if (it->bidi_p)
5470 it->bidi_it.string.lstring = it->string;
5471 it->bidi_it.string.s = NULL;
5472 it->bidi_it.string.schars = SCHARS (it->string);
5473 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5474 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5475 it->bidi_it.string.unibyte = !it->multibyte_p;
5476 it->bidi_it.w = it->w;
5477 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5481 CHECK_IT (it);
5485 /* Compare two overlay_entry structures E1 and E2. Used as a
5486 comparison function for qsort in load_overlay_strings. Overlay
5487 strings for the same position are sorted so that
5489 1. All after-strings come in front of before-strings, except
5490 when they come from the same overlay.
5492 2. Within after-strings, strings are sorted so that overlay strings
5493 from overlays with higher priorities come first.
5495 2. Within before-strings, strings are sorted so that overlay
5496 strings from overlays with higher priorities come last.
5498 Value is analogous to strcmp. */
5501 static int
5502 compare_overlay_entries (const void *e1, const void *e2)
5504 struct overlay_entry const *entry1 = e1;
5505 struct overlay_entry const *entry2 = e2;
5506 int result;
5508 if (entry1->after_string_p != entry2->after_string_p)
5510 /* Let after-strings appear in front of before-strings if
5511 they come from different overlays. */
5512 if (EQ (entry1->overlay, entry2->overlay))
5513 result = entry1->after_string_p ? 1 : -1;
5514 else
5515 result = entry1->after_string_p ? -1 : 1;
5517 else if (entry1->priority != entry2->priority)
5519 if (entry1->after_string_p)
5520 /* After-strings sorted in order of decreasing priority. */
5521 result = entry2->priority < entry1->priority ? -1 : 1;
5522 else
5523 /* Before-strings sorted in order of increasing priority. */
5524 result = entry1->priority < entry2->priority ? -1 : 1;
5526 else
5527 result = 0;
5529 return result;
5533 /* Load the vector IT->overlay_strings with overlay strings from IT's
5534 current buffer position, or from CHARPOS if that is > 0. Set
5535 IT->n_overlays to the total number of overlay strings found.
5537 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5538 a time. On entry into load_overlay_strings,
5539 IT->current.overlay_string_index gives the number of overlay
5540 strings that have already been loaded by previous calls to this
5541 function.
5543 IT->add_overlay_start contains an additional overlay start
5544 position to consider for taking overlay strings from, if non-zero.
5545 This position comes into play when the overlay has an `invisible'
5546 property, and both before and after-strings. When we've skipped to
5547 the end of the overlay, because of its `invisible' property, we
5548 nevertheless want its before-string to appear.
5549 IT->add_overlay_start will contain the overlay start position
5550 in this case.
5552 Overlay strings are sorted so that after-string strings come in
5553 front of before-string strings. Within before and after-strings,
5554 strings are sorted by overlay priority. See also function
5555 compare_overlay_entries. */
5557 static void
5558 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5560 Lisp_Object overlay, window, str, invisible;
5561 struct Lisp_Overlay *ov;
5562 ptrdiff_t start, end;
5563 ptrdiff_t size = 20;
5564 ptrdiff_t n = 0, i, j;
5565 int invis_p;
5566 struct overlay_entry *entries = alloca (size * sizeof *entries);
5567 USE_SAFE_ALLOCA;
5569 if (charpos <= 0)
5570 charpos = IT_CHARPOS (*it);
5572 /* Append the overlay string STRING of overlay OVERLAY to vector
5573 `entries' which has size `size' and currently contains `n'
5574 elements. AFTER_P non-zero means STRING is an after-string of
5575 OVERLAY. */
5576 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5577 do \
5579 Lisp_Object priority; \
5581 if (n == size) \
5583 struct overlay_entry *old = entries; \
5584 SAFE_NALLOCA (entries, 2, size); \
5585 memcpy (entries, old, size * sizeof *entries); \
5586 size *= 2; \
5589 entries[n].string = (STRING); \
5590 entries[n].overlay = (OVERLAY); \
5591 priority = Foverlay_get ((OVERLAY), Qpriority); \
5592 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5593 entries[n].after_string_p = (AFTER_P); \
5594 ++n; \
5596 while (0)
5598 /* Process overlay before the overlay center. */
5599 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5601 XSETMISC (overlay, ov);
5602 eassert (OVERLAYP (overlay));
5603 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5604 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5606 if (end < charpos)
5607 break;
5609 /* Skip this overlay if it doesn't start or end at IT's current
5610 position. */
5611 if (end != charpos && start != charpos)
5612 continue;
5614 /* Skip this overlay if it doesn't apply to IT->w. */
5615 window = Foverlay_get (overlay, Qwindow);
5616 if (WINDOWP (window) && XWINDOW (window) != it->w)
5617 continue;
5619 /* If the text ``under'' the overlay is invisible, both before-
5620 and after-strings from this overlay are visible; start and
5621 end position are indistinguishable. */
5622 invisible = Foverlay_get (overlay, Qinvisible);
5623 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5625 /* If overlay has a non-empty before-string, record it. */
5626 if ((start == charpos || (end == charpos && invis_p))
5627 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5628 && SCHARS (str))
5629 RECORD_OVERLAY_STRING (overlay, str, 0);
5631 /* If overlay has a non-empty after-string, record it. */
5632 if ((end == charpos || (start == charpos && invis_p))
5633 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5634 && SCHARS (str))
5635 RECORD_OVERLAY_STRING (overlay, str, 1);
5638 /* Process overlays after the overlay center. */
5639 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5641 XSETMISC (overlay, ov);
5642 eassert (OVERLAYP (overlay));
5643 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5644 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5646 if (start > charpos)
5647 break;
5649 /* Skip this overlay if it doesn't start or end at IT's current
5650 position. */
5651 if (end != charpos && start != charpos)
5652 continue;
5654 /* Skip this overlay if it doesn't apply to IT->w. */
5655 window = Foverlay_get (overlay, Qwindow);
5656 if (WINDOWP (window) && XWINDOW (window) != it->w)
5657 continue;
5659 /* If the text ``under'' the overlay is invisible, it has a zero
5660 dimension, and both before- and after-strings apply. */
5661 invisible = Foverlay_get (overlay, Qinvisible);
5662 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5664 /* If overlay has a non-empty before-string, record it. */
5665 if ((start == charpos || (end == charpos && invis_p))
5666 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5667 && SCHARS (str))
5668 RECORD_OVERLAY_STRING (overlay, str, 0);
5670 /* If overlay has a non-empty after-string, record it. */
5671 if ((end == charpos || (start == charpos && invis_p))
5672 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5673 && SCHARS (str))
5674 RECORD_OVERLAY_STRING (overlay, str, 1);
5677 #undef RECORD_OVERLAY_STRING
5679 /* Sort entries. */
5680 if (n > 1)
5681 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5683 /* Record number of overlay strings, and where we computed it. */
5684 it->n_overlay_strings = n;
5685 it->overlay_strings_charpos = charpos;
5687 /* IT->current.overlay_string_index is the number of overlay strings
5688 that have already been consumed by IT. Copy some of the
5689 remaining overlay strings to IT->overlay_strings. */
5690 i = 0;
5691 j = it->current.overlay_string_index;
5692 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5694 it->overlay_strings[i] = entries[j].string;
5695 it->string_overlays[i++] = entries[j++].overlay;
5698 CHECK_IT (it);
5699 SAFE_FREE ();
5703 /* Get the first chunk of overlay strings at IT's current buffer
5704 position, or at CHARPOS if that is > 0. Value is non-zero if at
5705 least one overlay string was found. */
5707 static int
5708 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5710 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5711 process. This fills IT->overlay_strings with strings, and sets
5712 IT->n_overlay_strings to the total number of strings to process.
5713 IT->pos.overlay_string_index has to be set temporarily to zero
5714 because load_overlay_strings needs this; it must be set to -1
5715 when no overlay strings are found because a zero value would
5716 indicate a position in the first overlay string. */
5717 it->current.overlay_string_index = 0;
5718 load_overlay_strings (it, charpos);
5720 /* If we found overlay strings, set up IT to deliver display
5721 elements from the first one. Otherwise set up IT to deliver
5722 from current_buffer. */
5723 if (it->n_overlay_strings)
5725 /* Make sure we know settings in current_buffer, so that we can
5726 restore meaningful values when we're done with the overlay
5727 strings. */
5728 if (compute_stop_p)
5729 compute_stop_pos (it);
5730 eassert (it->face_id >= 0);
5732 /* Save IT's settings. They are restored after all overlay
5733 strings have been processed. */
5734 eassert (!compute_stop_p || it->sp == 0);
5736 /* When called from handle_stop, there might be an empty display
5737 string loaded. In that case, don't bother saving it. But
5738 don't use this optimization with the bidi iterator, since we
5739 need the corresponding pop_it call to resync the bidi
5740 iterator's position with IT's position, after we are done
5741 with the overlay strings. (The corresponding call to pop_it
5742 in case of an empty display string is in
5743 next_overlay_string.) */
5744 if (!(!it->bidi_p
5745 && STRINGP (it->string) && !SCHARS (it->string)))
5746 push_it (it, NULL);
5748 /* Set up IT to deliver display elements from the first overlay
5749 string. */
5750 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5751 it->string = it->overlay_strings[0];
5752 it->from_overlay = Qnil;
5753 it->stop_charpos = 0;
5754 eassert (STRINGP (it->string));
5755 it->end_charpos = SCHARS (it->string);
5756 it->prev_stop = 0;
5757 it->base_level_stop = 0;
5758 it->multibyte_p = STRING_MULTIBYTE (it->string);
5759 it->method = GET_FROM_STRING;
5760 it->from_disp_prop_p = 0;
5762 /* Force paragraph direction to be that of the parent
5763 buffer. */
5764 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5765 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5766 else
5767 it->paragraph_embedding = L2R;
5769 /* Set up the bidi iterator for this overlay string. */
5770 if (it->bidi_p)
5772 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5774 it->bidi_it.string.lstring = it->string;
5775 it->bidi_it.string.s = NULL;
5776 it->bidi_it.string.schars = SCHARS (it->string);
5777 it->bidi_it.string.bufpos = pos;
5778 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5779 it->bidi_it.string.unibyte = !it->multibyte_p;
5780 it->bidi_it.w = it->w;
5781 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5783 return 1;
5786 it->current.overlay_string_index = -1;
5787 return 0;
5790 static int
5791 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5793 it->string = Qnil;
5794 it->method = GET_FROM_BUFFER;
5796 (void) get_overlay_strings_1 (it, charpos, 1);
5798 CHECK_IT (it);
5800 /* Value is non-zero if we found at least one overlay string. */
5801 return STRINGP (it->string);
5806 /***********************************************************************
5807 Saving and restoring state
5808 ***********************************************************************/
5810 /* Save current settings of IT on IT->stack. Called, for example,
5811 before setting up IT for an overlay string, to be able to restore
5812 IT's settings to what they were after the overlay string has been
5813 processed. If POSITION is non-NULL, it is the position to save on
5814 the stack instead of IT->position. */
5816 static void
5817 push_it (struct it *it, struct text_pos *position)
5819 struct iterator_stack_entry *p;
5821 eassert (it->sp < IT_STACK_SIZE);
5822 p = it->stack + it->sp;
5824 p->stop_charpos = it->stop_charpos;
5825 p->prev_stop = it->prev_stop;
5826 p->base_level_stop = it->base_level_stop;
5827 p->cmp_it = it->cmp_it;
5828 eassert (it->face_id >= 0);
5829 p->face_id = it->face_id;
5830 p->string = it->string;
5831 p->method = it->method;
5832 p->from_overlay = it->from_overlay;
5833 switch (p->method)
5835 case GET_FROM_IMAGE:
5836 p->u.image.object = it->object;
5837 p->u.image.image_id = it->image_id;
5838 p->u.image.slice = it->slice;
5839 break;
5840 case GET_FROM_STRETCH:
5841 p->u.stretch.object = it->object;
5842 break;
5844 p->position = position ? *position : it->position;
5845 p->current = it->current;
5846 p->end_charpos = it->end_charpos;
5847 p->string_nchars = it->string_nchars;
5848 p->area = it->area;
5849 p->multibyte_p = it->multibyte_p;
5850 p->avoid_cursor_p = it->avoid_cursor_p;
5851 p->space_width = it->space_width;
5852 p->font_height = it->font_height;
5853 p->voffset = it->voffset;
5854 p->string_from_display_prop_p = it->string_from_display_prop_p;
5855 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5856 p->display_ellipsis_p = 0;
5857 p->line_wrap = it->line_wrap;
5858 p->bidi_p = it->bidi_p;
5859 p->paragraph_embedding = it->paragraph_embedding;
5860 p->from_disp_prop_p = it->from_disp_prop_p;
5861 ++it->sp;
5863 /* Save the state of the bidi iterator as well. */
5864 if (it->bidi_p)
5865 bidi_push_it (&it->bidi_it);
5868 static void
5869 iterate_out_of_display_property (struct it *it)
5871 int buffer_p = !STRINGP (it->string);
5872 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5873 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5875 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5877 /* Maybe initialize paragraph direction. If we are at the beginning
5878 of a new paragraph, next_element_from_buffer may not have a
5879 chance to do that. */
5880 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5881 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5882 /* prev_stop can be zero, so check against BEGV as well. */
5883 while (it->bidi_it.charpos >= bob
5884 && it->prev_stop <= it->bidi_it.charpos
5885 && it->bidi_it.charpos < CHARPOS (it->position)
5886 && it->bidi_it.charpos < eob)
5887 bidi_move_to_visually_next (&it->bidi_it);
5888 /* Record the stop_pos we just crossed, for when we cross it
5889 back, maybe. */
5890 if (it->bidi_it.charpos > CHARPOS (it->position))
5891 it->prev_stop = CHARPOS (it->position);
5892 /* If we ended up not where pop_it put us, resync IT's
5893 positional members with the bidi iterator. */
5894 if (it->bidi_it.charpos != CHARPOS (it->position))
5895 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5896 if (buffer_p)
5897 it->current.pos = it->position;
5898 else
5899 it->current.string_pos = it->position;
5902 /* Restore IT's settings from IT->stack. Called, for example, when no
5903 more overlay strings must be processed, and we return to delivering
5904 display elements from a buffer, or when the end of a string from a
5905 `display' property is reached and we return to delivering display
5906 elements from an overlay string, or from a buffer. */
5908 static void
5909 pop_it (struct it *it)
5911 struct iterator_stack_entry *p;
5912 int from_display_prop = it->from_disp_prop_p;
5914 eassert (it->sp > 0);
5915 --it->sp;
5916 p = it->stack + it->sp;
5917 it->stop_charpos = p->stop_charpos;
5918 it->prev_stop = p->prev_stop;
5919 it->base_level_stop = p->base_level_stop;
5920 it->cmp_it = p->cmp_it;
5921 it->face_id = p->face_id;
5922 it->current = p->current;
5923 it->position = p->position;
5924 it->string = p->string;
5925 it->from_overlay = p->from_overlay;
5926 if (NILP (it->string))
5927 SET_TEXT_POS (it->current.string_pos, -1, -1);
5928 it->method = p->method;
5929 switch (it->method)
5931 case GET_FROM_IMAGE:
5932 it->image_id = p->u.image.image_id;
5933 it->object = p->u.image.object;
5934 it->slice = p->u.image.slice;
5935 break;
5936 case GET_FROM_STRETCH:
5937 it->object = p->u.stretch.object;
5938 break;
5939 case GET_FROM_BUFFER:
5940 it->object = it->w->contents;
5941 break;
5942 case GET_FROM_STRING:
5943 it->object = it->string;
5944 break;
5945 case GET_FROM_DISPLAY_VECTOR:
5946 if (it->s)
5947 it->method = GET_FROM_C_STRING;
5948 else if (STRINGP (it->string))
5949 it->method = GET_FROM_STRING;
5950 else
5952 it->method = GET_FROM_BUFFER;
5953 it->object = it->w->contents;
5956 it->end_charpos = p->end_charpos;
5957 it->string_nchars = p->string_nchars;
5958 it->area = p->area;
5959 it->multibyte_p = p->multibyte_p;
5960 it->avoid_cursor_p = p->avoid_cursor_p;
5961 it->space_width = p->space_width;
5962 it->font_height = p->font_height;
5963 it->voffset = p->voffset;
5964 it->string_from_display_prop_p = p->string_from_display_prop_p;
5965 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5966 it->line_wrap = p->line_wrap;
5967 it->bidi_p = p->bidi_p;
5968 it->paragraph_embedding = p->paragraph_embedding;
5969 it->from_disp_prop_p = p->from_disp_prop_p;
5970 if (it->bidi_p)
5972 bidi_pop_it (&it->bidi_it);
5973 /* Bidi-iterate until we get out of the portion of text, if any,
5974 covered by a `display' text property or by an overlay with
5975 `display' property. (We cannot just jump there, because the
5976 internal coherency of the bidi iterator state can not be
5977 preserved across such jumps.) We also must determine the
5978 paragraph base direction if the overlay we just processed is
5979 at the beginning of a new paragraph. */
5980 if (from_display_prop
5981 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5982 iterate_out_of_display_property (it);
5984 eassert ((BUFFERP (it->object)
5985 && IT_CHARPOS (*it) == it->bidi_it.charpos
5986 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5987 || (STRINGP (it->object)
5988 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5989 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5990 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5996 /***********************************************************************
5997 Moving over lines
5998 ***********************************************************************/
6000 /* Set IT's current position to the previous line start. */
6002 static void
6003 back_to_previous_line_start (struct it *it)
6005 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6007 DEC_BOTH (cp, bp);
6008 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6012 /* Move IT to the next line start.
6014 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6015 we skipped over part of the text (as opposed to moving the iterator
6016 continuously over the text). Otherwise, don't change the value
6017 of *SKIPPED_P.
6019 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6020 iterator on the newline, if it was found.
6022 Newlines may come from buffer text, overlay strings, or strings
6023 displayed via the `display' property. That's the reason we can't
6024 simply use find_newline_no_quit.
6026 Note that this function may not skip over invisible text that is so
6027 because of text properties and immediately follows a newline. If
6028 it would, function reseat_at_next_visible_line_start, when called
6029 from set_iterator_to_next, would effectively make invisible
6030 characters following a newline part of the wrong glyph row, which
6031 leads to wrong cursor motion. */
6033 static int
6034 forward_to_next_line_start (struct it *it, int *skipped_p,
6035 struct bidi_it *bidi_it_prev)
6037 ptrdiff_t old_selective;
6038 int newline_found_p, n;
6039 const int MAX_NEWLINE_DISTANCE = 500;
6041 /* If already on a newline, just consume it to avoid unintended
6042 skipping over invisible text below. */
6043 if (it->what == IT_CHARACTER
6044 && it->c == '\n'
6045 && CHARPOS (it->position) == IT_CHARPOS (*it))
6047 if (it->bidi_p && bidi_it_prev)
6048 *bidi_it_prev = it->bidi_it;
6049 set_iterator_to_next (it, 0);
6050 it->c = 0;
6051 return 1;
6054 /* Don't handle selective display in the following. It's (a)
6055 unnecessary because it's done by the caller, and (b) leads to an
6056 infinite recursion because next_element_from_ellipsis indirectly
6057 calls this function. */
6058 old_selective = it->selective;
6059 it->selective = 0;
6061 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6062 from buffer text. */
6063 for (n = newline_found_p = 0;
6064 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6065 n += STRINGP (it->string) ? 0 : 1)
6067 if (!get_next_display_element (it))
6068 return 0;
6069 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6070 if (newline_found_p && it->bidi_p && bidi_it_prev)
6071 *bidi_it_prev = it->bidi_it;
6072 set_iterator_to_next (it, 0);
6075 /* If we didn't find a newline near enough, see if we can use a
6076 short-cut. */
6077 if (!newline_found_p)
6079 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6080 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6081 1, &bytepos);
6082 Lisp_Object pos;
6084 eassert (!STRINGP (it->string));
6086 /* If there isn't any `display' property in sight, and no
6087 overlays, we can just use the position of the newline in
6088 buffer text. */
6089 if (it->stop_charpos >= limit
6090 || ((pos = Fnext_single_property_change (make_number (start),
6091 Qdisplay, Qnil,
6092 make_number (limit)),
6093 NILP (pos))
6094 && next_overlay_change (start) == ZV))
6096 if (!it->bidi_p)
6098 IT_CHARPOS (*it) = limit;
6099 IT_BYTEPOS (*it) = bytepos;
6101 else
6103 struct bidi_it bprev;
6105 /* Help bidi.c avoid expensive searches for display
6106 properties and overlays, by telling it that there are
6107 none up to `limit'. */
6108 if (it->bidi_it.disp_pos < limit)
6110 it->bidi_it.disp_pos = limit;
6111 it->bidi_it.disp_prop = 0;
6113 do {
6114 bprev = it->bidi_it;
6115 bidi_move_to_visually_next (&it->bidi_it);
6116 } while (it->bidi_it.charpos != limit);
6117 IT_CHARPOS (*it) = limit;
6118 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6119 if (bidi_it_prev)
6120 *bidi_it_prev = bprev;
6122 *skipped_p = newline_found_p = 1;
6124 else
6126 while (get_next_display_element (it)
6127 && !newline_found_p)
6129 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6130 if (newline_found_p && it->bidi_p && bidi_it_prev)
6131 *bidi_it_prev = it->bidi_it;
6132 set_iterator_to_next (it, 0);
6137 it->selective = old_selective;
6138 return newline_found_p;
6142 /* Set IT's current position to the previous visible line start. Skip
6143 invisible text that is so either due to text properties or due to
6144 selective display. Caution: this does not change IT->current_x and
6145 IT->hpos. */
6147 static void
6148 back_to_previous_visible_line_start (struct it *it)
6150 while (IT_CHARPOS (*it) > BEGV)
6152 back_to_previous_line_start (it);
6154 if (IT_CHARPOS (*it) <= BEGV)
6155 break;
6157 /* If selective > 0, then lines indented more than its value are
6158 invisible. */
6159 if (it->selective > 0
6160 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6161 it->selective))
6162 continue;
6164 /* Check the newline before point for invisibility. */
6166 Lisp_Object prop;
6167 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6168 Qinvisible, it->window);
6169 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6170 continue;
6173 if (IT_CHARPOS (*it) <= BEGV)
6174 break;
6177 struct it it2;
6178 void *it2data = NULL;
6179 ptrdiff_t pos;
6180 ptrdiff_t beg, end;
6181 Lisp_Object val, overlay;
6183 SAVE_IT (it2, *it, it2data);
6185 /* If newline is part of a composition, continue from start of composition */
6186 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6187 && beg < IT_CHARPOS (*it))
6188 goto replaced;
6190 /* If newline is replaced by a display property, find start of overlay
6191 or interval and continue search from that point. */
6192 pos = --IT_CHARPOS (it2);
6193 --IT_BYTEPOS (it2);
6194 it2.sp = 0;
6195 bidi_unshelve_cache (NULL, 0);
6196 it2.string_from_display_prop_p = 0;
6197 it2.from_disp_prop_p = 0;
6198 if (handle_display_prop (&it2) == HANDLED_RETURN
6199 && !NILP (val = get_char_property_and_overlay
6200 (make_number (pos), Qdisplay, Qnil, &overlay))
6201 && (OVERLAYP (overlay)
6202 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6203 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6205 RESTORE_IT (it, it, it2data);
6206 goto replaced;
6209 /* Newline is not replaced by anything -- so we are done. */
6210 RESTORE_IT (it, it, it2data);
6211 break;
6213 replaced:
6214 if (beg < BEGV)
6215 beg = BEGV;
6216 IT_CHARPOS (*it) = beg;
6217 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6221 it->continuation_lines_width = 0;
6223 eassert (IT_CHARPOS (*it) >= BEGV);
6224 eassert (IT_CHARPOS (*it) == BEGV
6225 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6226 CHECK_IT (it);
6230 /* Reseat iterator IT at the previous visible line start. Skip
6231 invisible text that is so either due to text properties or due to
6232 selective display. At the end, update IT's overlay information,
6233 face information etc. */
6235 void
6236 reseat_at_previous_visible_line_start (struct it *it)
6238 back_to_previous_visible_line_start (it);
6239 reseat (it, it->current.pos, 1);
6240 CHECK_IT (it);
6244 /* Reseat iterator IT on the next visible line start in the current
6245 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6246 preceding the line start. Skip over invisible text that is so
6247 because of selective display. Compute faces, overlays etc at the
6248 new position. Note that this function does not skip over text that
6249 is invisible because of text properties. */
6251 static void
6252 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6254 int newline_found_p, skipped_p = 0;
6255 struct bidi_it bidi_it_prev;
6257 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6259 /* Skip over lines that are invisible because they are indented
6260 more than the value of IT->selective. */
6261 if (it->selective > 0)
6262 while (IT_CHARPOS (*it) < ZV
6263 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6264 it->selective))
6266 eassert (IT_BYTEPOS (*it) == BEGV
6267 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6268 newline_found_p =
6269 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6272 /* Position on the newline if that's what's requested. */
6273 if (on_newline_p && newline_found_p)
6275 if (STRINGP (it->string))
6277 if (IT_STRING_CHARPOS (*it) > 0)
6279 if (!it->bidi_p)
6281 --IT_STRING_CHARPOS (*it);
6282 --IT_STRING_BYTEPOS (*it);
6284 else
6286 /* We need to restore the bidi iterator to the state
6287 it had on the newline, and resync the IT's
6288 position with that. */
6289 it->bidi_it = bidi_it_prev;
6290 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6291 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6295 else if (IT_CHARPOS (*it) > BEGV)
6297 if (!it->bidi_p)
6299 --IT_CHARPOS (*it);
6300 --IT_BYTEPOS (*it);
6302 else
6304 /* We need to restore the bidi iterator to the state it
6305 had on the newline and resync IT with that. */
6306 it->bidi_it = bidi_it_prev;
6307 IT_CHARPOS (*it) = it->bidi_it.charpos;
6308 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6310 reseat (it, it->current.pos, 0);
6313 else if (skipped_p)
6314 reseat (it, it->current.pos, 0);
6316 CHECK_IT (it);
6321 /***********************************************************************
6322 Changing an iterator's position
6323 ***********************************************************************/
6325 /* Change IT's current position to POS in current_buffer. If FORCE_P
6326 is non-zero, always check for text properties at the new position.
6327 Otherwise, text properties are only looked up if POS >=
6328 IT->check_charpos of a property. */
6330 static void
6331 reseat (struct it *it, struct text_pos pos, int force_p)
6333 ptrdiff_t original_pos = IT_CHARPOS (*it);
6335 reseat_1 (it, pos, 0);
6337 /* Determine where to check text properties. Avoid doing it
6338 where possible because text property lookup is very expensive. */
6339 if (force_p
6340 || CHARPOS (pos) > it->stop_charpos
6341 || CHARPOS (pos) < original_pos)
6343 if (it->bidi_p)
6345 /* For bidi iteration, we need to prime prev_stop and
6346 base_level_stop with our best estimations. */
6347 /* Implementation note: Of course, POS is not necessarily a
6348 stop position, so assigning prev_pos to it is a lie; we
6349 should have called compute_stop_backwards. However, if
6350 the current buffer does not include any R2L characters,
6351 that call would be a waste of cycles, because the
6352 iterator will never move back, and thus never cross this
6353 "fake" stop position. So we delay that backward search
6354 until the time we really need it, in next_element_from_buffer. */
6355 if (CHARPOS (pos) != it->prev_stop)
6356 it->prev_stop = CHARPOS (pos);
6357 if (CHARPOS (pos) < it->base_level_stop)
6358 it->base_level_stop = 0; /* meaning it's unknown */
6359 handle_stop (it);
6361 else
6363 handle_stop (it);
6364 it->prev_stop = it->base_level_stop = 0;
6369 CHECK_IT (it);
6373 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6374 IT->stop_pos to POS, also. */
6376 static void
6377 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6379 /* Don't call this function when scanning a C string. */
6380 eassert (it->s == NULL);
6382 /* POS must be a reasonable value. */
6383 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6385 it->current.pos = it->position = pos;
6386 it->end_charpos = ZV;
6387 it->dpvec = NULL;
6388 it->current.dpvec_index = -1;
6389 it->current.overlay_string_index = -1;
6390 IT_STRING_CHARPOS (*it) = -1;
6391 IT_STRING_BYTEPOS (*it) = -1;
6392 it->string = Qnil;
6393 it->method = GET_FROM_BUFFER;
6394 it->object = it->w->contents;
6395 it->area = TEXT_AREA;
6396 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6397 it->sp = 0;
6398 it->string_from_display_prop_p = 0;
6399 it->string_from_prefix_prop_p = 0;
6401 it->from_disp_prop_p = 0;
6402 it->face_before_selective_p = 0;
6403 if (it->bidi_p)
6405 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6406 &it->bidi_it);
6407 bidi_unshelve_cache (NULL, 0);
6408 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6409 it->bidi_it.string.s = NULL;
6410 it->bidi_it.string.lstring = Qnil;
6411 it->bidi_it.string.bufpos = 0;
6412 it->bidi_it.string.unibyte = 0;
6413 it->bidi_it.w = it->w;
6416 if (set_stop_p)
6418 it->stop_charpos = CHARPOS (pos);
6419 it->base_level_stop = CHARPOS (pos);
6421 /* This make the information stored in it->cmp_it invalidate. */
6422 it->cmp_it.id = -1;
6426 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6427 If S is non-null, it is a C string to iterate over. Otherwise,
6428 STRING gives a Lisp string to iterate over.
6430 If PRECISION > 0, don't return more then PRECISION number of
6431 characters from the string.
6433 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6434 characters have been returned. FIELD_WIDTH < 0 means an infinite
6435 field width.
6437 MULTIBYTE = 0 means disable processing of multibyte characters,
6438 MULTIBYTE > 0 means enable it,
6439 MULTIBYTE < 0 means use IT->multibyte_p.
6441 IT must be initialized via a prior call to init_iterator before
6442 calling this function. */
6444 static void
6445 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6446 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6447 int multibyte)
6449 /* No region in strings. */
6450 it->region_beg_charpos = it->region_end_charpos = -1;
6452 /* No text property checks performed by default, but see below. */
6453 it->stop_charpos = -1;
6455 /* Set iterator position and end position. */
6456 memset (&it->current, 0, sizeof it->current);
6457 it->current.overlay_string_index = -1;
6458 it->current.dpvec_index = -1;
6459 eassert (charpos >= 0);
6461 /* If STRING is specified, use its multibyteness, otherwise use the
6462 setting of MULTIBYTE, if specified. */
6463 if (multibyte >= 0)
6464 it->multibyte_p = multibyte > 0;
6466 /* Bidirectional reordering of strings is controlled by the default
6467 value of bidi-display-reordering. Don't try to reorder while
6468 loading loadup.el, as the necessary character property tables are
6469 not yet available. */
6470 it->bidi_p =
6471 NILP (Vpurify_flag)
6472 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6474 if (s == NULL)
6476 eassert (STRINGP (string));
6477 it->string = string;
6478 it->s = NULL;
6479 it->end_charpos = it->string_nchars = SCHARS (string);
6480 it->method = GET_FROM_STRING;
6481 it->current.string_pos = string_pos (charpos, string);
6483 if (it->bidi_p)
6485 it->bidi_it.string.lstring = string;
6486 it->bidi_it.string.s = NULL;
6487 it->bidi_it.string.schars = it->end_charpos;
6488 it->bidi_it.string.bufpos = 0;
6489 it->bidi_it.string.from_disp_str = 0;
6490 it->bidi_it.string.unibyte = !it->multibyte_p;
6491 it->bidi_it.w = it->w;
6492 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6493 FRAME_WINDOW_P (it->f), &it->bidi_it);
6496 else
6498 it->s = (const unsigned char *) s;
6499 it->string = Qnil;
6501 /* Note that we use IT->current.pos, not it->current.string_pos,
6502 for displaying C strings. */
6503 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6504 if (it->multibyte_p)
6506 it->current.pos = c_string_pos (charpos, s, 1);
6507 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6509 else
6511 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6512 it->end_charpos = it->string_nchars = strlen (s);
6515 if (it->bidi_p)
6517 it->bidi_it.string.lstring = Qnil;
6518 it->bidi_it.string.s = (const unsigned char *) s;
6519 it->bidi_it.string.schars = it->end_charpos;
6520 it->bidi_it.string.bufpos = 0;
6521 it->bidi_it.string.from_disp_str = 0;
6522 it->bidi_it.string.unibyte = !it->multibyte_p;
6523 it->bidi_it.w = it->w;
6524 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6525 &it->bidi_it);
6527 it->method = GET_FROM_C_STRING;
6530 /* PRECISION > 0 means don't return more than PRECISION characters
6531 from the string. */
6532 if (precision > 0 && it->end_charpos - charpos > precision)
6534 it->end_charpos = it->string_nchars = charpos + precision;
6535 if (it->bidi_p)
6536 it->bidi_it.string.schars = it->end_charpos;
6539 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6540 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6541 FIELD_WIDTH < 0 means infinite field width. This is useful for
6542 padding with `-' at the end of a mode line. */
6543 if (field_width < 0)
6544 field_width = INFINITY;
6545 /* Implementation note: We deliberately don't enlarge
6546 it->bidi_it.string.schars here to fit it->end_charpos, because
6547 the bidi iterator cannot produce characters out of thin air. */
6548 if (field_width > it->end_charpos - charpos)
6549 it->end_charpos = charpos + field_width;
6551 /* Use the standard display table for displaying strings. */
6552 if (DISP_TABLE_P (Vstandard_display_table))
6553 it->dp = XCHAR_TABLE (Vstandard_display_table);
6555 it->stop_charpos = charpos;
6556 it->prev_stop = charpos;
6557 it->base_level_stop = 0;
6558 if (it->bidi_p)
6560 it->bidi_it.first_elt = 1;
6561 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6562 it->bidi_it.disp_pos = -1;
6564 if (s == NULL && it->multibyte_p)
6566 ptrdiff_t endpos = SCHARS (it->string);
6567 if (endpos > it->end_charpos)
6568 endpos = it->end_charpos;
6569 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6570 it->string);
6572 CHECK_IT (it);
6577 /***********************************************************************
6578 Iteration
6579 ***********************************************************************/
6581 /* Map enum it_method value to corresponding next_element_from_* function. */
6583 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6585 next_element_from_buffer,
6586 next_element_from_display_vector,
6587 next_element_from_string,
6588 next_element_from_c_string,
6589 next_element_from_image,
6590 next_element_from_stretch
6593 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6596 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6597 (possibly with the following characters). */
6599 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6600 ((IT)->cmp_it.id >= 0 \
6601 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6602 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6603 END_CHARPOS, (IT)->w, \
6604 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6605 (IT)->string)))
6608 /* Lookup the char-table Vglyphless_char_display for character C (-1
6609 if we want information for no-font case), and return the display
6610 method symbol. By side-effect, update it->what and
6611 it->glyphless_method. This function is called from
6612 get_next_display_element for each character element, and from
6613 x_produce_glyphs when no suitable font was found. */
6615 Lisp_Object
6616 lookup_glyphless_char_display (int c, struct it *it)
6618 Lisp_Object glyphless_method = Qnil;
6620 if (CHAR_TABLE_P (Vglyphless_char_display)
6621 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6623 if (c >= 0)
6625 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6626 if (CONSP (glyphless_method))
6627 glyphless_method = FRAME_WINDOW_P (it->f)
6628 ? XCAR (glyphless_method)
6629 : XCDR (glyphless_method);
6631 else
6632 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6635 retry:
6636 if (NILP (glyphless_method))
6638 if (c >= 0)
6639 /* The default is to display the character by a proper font. */
6640 return Qnil;
6641 /* The default for the no-font case is to display an empty box. */
6642 glyphless_method = Qempty_box;
6644 if (EQ (glyphless_method, Qzero_width))
6646 if (c >= 0)
6647 return glyphless_method;
6648 /* This method can't be used for the no-font case. */
6649 glyphless_method = Qempty_box;
6651 if (EQ (glyphless_method, Qthin_space))
6652 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6653 else if (EQ (glyphless_method, Qempty_box))
6654 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6655 else if (EQ (glyphless_method, Qhex_code))
6656 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6657 else if (STRINGP (glyphless_method))
6658 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6659 else
6661 /* Invalid value. We use the default method. */
6662 glyphless_method = Qnil;
6663 goto retry;
6665 it->what = IT_GLYPHLESS;
6666 return glyphless_method;
6669 /* Merge escape glyph face and cache the result. */
6671 static struct frame *last_escape_glyph_frame = NULL;
6672 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6673 static int last_escape_glyph_merged_face_id = 0;
6675 static int
6676 merge_escape_glyph_face (struct it *it)
6678 int face_id;
6680 if (it->f == last_escape_glyph_frame
6681 && it->face_id == last_escape_glyph_face_id)
6682 face_id = last_escape_glyph_merged_face_id;
6683 else
6685 /* Merge the `escape-glyph' face into the current face. */
6686 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6687 last_escape_glyph_frame = it->f;
6688 last_escape_glyph_face_id = it->face_id;
6689 last_escape_glyph_merged_face_id = face_id;
6691 return face_id;
6694 /* Likewise for glyphless glyph face. */
6696 static struct frame *last_glyphless_glyph_frame = NULL;
6697 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6698 static int last_glyphless_glyph_merged_face_id = 0;
6701 merge_glyphless_glyph_face (struct it *it)
6703 int face_id;
6705 if (it->f == last_glyphless_glyph_frame
6706 && it->face_id == last_glyphless_glyph_face_id)
6707 face_id = last_glyphless_glyph_merged_face_id;
6708 else
6710 /* Merge the `glyphless-char' face into the current face. */
6711 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6712 last_glyphless_glyph_frame = it->f;
6713 last_glyphless_glyph_face_id = it->face_id;
6714 last_glyphless_glyph_merged_face_id = face_id;
6716 return face_id;
6719 /* Load IT's display element fields with information about the next
6720 display element from the current position of IT. Value is zero if
6721 end of buffer (or C string) is reached. */
6723 static int
6724 get_next_display_element (struct it *it)
6726 /* Non-zero means that we found a display element. Zero means that
6727 we hit the end of what we iterate over. Performance note: the
6728 function pointer `method' used here turns out to be faster than
6729 using a sequence of if-statements. */
6730 int success_p;
6732 get_next:
6733 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6735 if (it->what == IT_CHARACTER)
6737 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6738 and only if (a) the resolved directionality of that character
6739 is R..." */
6740 /* FIXME: Do we need an exception for characters from display
6741 tables? */
6742 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6743 it->c = bidi_mirror_char (it->c);
6744 /* Map via display table or translate control characters.
6745 IT->c, IT->len etc. have been set to the next character by
6746 the function call above. If we have a display table, and it
6747 contains an entry for IT->c, translate it. Don't do this if
6748 IT->c itself comes from a display table, otherwise we could
6749 end up in an infinite recursion. (An alternative could be to
6750 count the recursion depth of this function and signal an
6751 error when a certain maximum depth is reached.) Is it worth
6752 it? */
6753 if (success_p && it->dpvec == NULL)
6755 Lisp_Object dv;
6756 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6757 int nonascii_space_p = 0;
6758 int nonascii_hyphen_p = 0;
6759 int c = it->c; /* This is the character to display. */
6761 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6763 eassert (SINGLE_BYTE_CHAR_P (c));
6764 if (unibyte_display_via_language_environment)
6766 c = DECODE_CHAR (unibyte, c);
6767 if (c < 0)
6768 c = BYTE8_TO_CHAR (it->c);
6770 else
6771 c = BYTE8_TO_CHAR (it->c);
6774 if (it->dp
6775 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6776 VECTORP (dv)))
6778 struct Lisp_Vector *v = XVECTOR (dv);
6780 /* Return the first character from the display table
6781 entry, if not empty. If empty, don't display the
6782 current character. */
6783 if (v->header.size)
6785 it->dpvec_char_len = it->len;
6786 it->dpvec = v->contents;
6787 it->dpend = v->contents + v->header.size;
6788 it->current.dpvec_index = 0;
6789 it->dpvec_face_id = -1;
6790 it->saved_face_id = it->face_id;
6791 it->method = GET_FROM_DISPLAY_VECTOR;
6792 it->ellipsis_p = 0;
6794 else
6796 set_iterator_to_next (it, 0);
6798 goto get_next;
6801 if (! NILP (lookup_glyphless_char_display (c, it)))
6803 if (it->what == IT_GLYPHLESS)
6804 goto done;
6805 /* Don't display this character. */
6806 set_iterator_to_next (it, 0);
6807 goto get_next;
6810 /* If `nobreak-char-display' is non-nil, we display
6811 non-ASCII spaces and hyphens specially. */
6812 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6814 if (c == 0xA0)
6815 nonascii_space_p = 1;
6816 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6817 nonascii_hyphen_p = 1;
6820 /* Translate control characters into `\003' or `^C' form.
6821 Control characters coming from a display table entry are
6822 currently not translated because we use IT->dpvec to hold
6823 the translation. This could easily be changed but I
6824 don't believe that it is worth doing.
6826 The characters handled by `nobreak-char-display' must be
6827 translated too.
6829 Non-printable characters and raw-byte characters are also
6830 translated to octal form. */
6831 if (((c < ' ' || c == 127) /* ASCII control chars */
6832 ? (it->area != TEXT_AREA
6833 /* In mode line, treat \n, \t like other crl chars. */
6834 || (c != '\t'
6835 && it->glyph_row
6836 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6837 || (c != '\n' && c != '\t'))
6838 : (nonascii_space_p
6839 || nonascii_hyphen_p
6840 || CHAR_BYTE8_P (c)
6841 || ! CHAR_PRINTABLE_P (c))))
6843 /* C is a control character, non-ASCII space/hyphen,
6844 raw-byte, or a non-printable character which must be
6845 displayed either as '\003' or as `^C' where the '\\'
6846 and '^' can be defined in the display table. Fill
6847 IT->ctl_chars with glyphs for what we have to
6848 display. Then, set IT->dpvec to these glyphs. */
6849 Lisp_Object gc;
6850 int ctl_len;
6851 int face_id;
6852 int lface_id = 0;
6853 int escape_glyph;
6855 /* Handle control characters with ^. */
6857 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6859 int g;
6861 g = '^'; /* default glyph for Control */
6862 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6863 if (it->dp
6864 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6866 g = GLYPH_CODE_CHAR (gc);
6867 lface_id = GLYPH_CODE_FACE (gc);
6870 face_id = (lface_id
6871 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6872 : merge_escape_glyph_face (it));
6874 XSETINT (it->ctl_chars[0], g);
6875 XSETINT (it->ctl_chars[1], c ^ 0100);
6876 ctl_len = 2;
6877 goto display_control;
6880 /* Handle non-ascii space in the mode where it only gets
6881 highlighting. */
6883 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6885 /* Merge `nobreak-space' into the current face. */
6886 face_id = merge_faces (it->f, Qnobreak_space, 0,
6887 it->face_id);
6888 XSETINT (it->ctl_chars[0], ' ');
6889 ctl_len = 1;
6890 goto display_control;
6893 /* Handle sequences that start with the "escape glyph". */
6895 /* the default escape glyph is \. */
6896 escape_glyph = '\\';
6898 if (it->dp
6899 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6901 escape_glyph = GLYPH_CODE_CHAR (gc);
6902 lface_id = GLYPH_CODE_FACE (gc);
6905 face_id = (lface_id
6906 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6907 : merge_escape_glyph_face (it));
6909 /* Draw non-ASCII hyphen with just highlighting: */
6911 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6913 XSETINT (it->ctl_chars[0], '-');
6914 ctl_len = 1;
6915 goto display_control;
6918 /* Draw non-ASCII space/hyphen with escape glyph: */
6920 if (nonascii_space_p || nonascii_hyphen_p)
6922 XSETINT (it->ctl_chars[0], escape_glyph);
6923 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6924 ctl_len = 2;
6925 goto display_control;
6929 char str[10];
6930 int len, i;
6932 if (CHAR_BYTE8_P (c))
6933 /* Display \200 instead of \17777600. */
6934 c = CHAR_TO_BYTE8 (c);
6935 len = sprintf (str, "%03o", c);
6937 XSETINT (it->ctl_chars[0], escape_glyph);
6938 for (i = 0; i < len; i++)
6939 XSETINT (it->ctl_chars[i + 1], str[i]);
6940 ctl_len = len + 1;
6943 display_control:
6944 /* Set up IT->dpvec and return first character from it. */
6945 it->dpvec_char_len = it->len;
6946 it->dpvec = it->ctl_chars;
6947 it->dpend = it->dpvec + ctl_len;
6948 it->current.dpvec_index = 0;
6949 it->dpvec_face_id = face_id;
6950 it->saved_face_id = it->face_id;
6951 it->method = GET_FROM_DISPLAY_VECTOR;
6952 it->ellipsis_p = 0;
6953 goto get_next;
6955 it->char_to_display = c;
6957 else if (success_p)
6959 it->char_to_display = it->c;
6963 /* Adjust face id for a multibyte character. There are no multibyte
6964 character in unibyte text. */
6965 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6966 && it->multibyte_p
6967 && success_p
6968 && FRAME_WINDOW_P (it->f))
6970 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6972 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6974 /* Automatic composition with glyph-string. */
6975 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6977 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6979 else
6981 ptrdiff_t pos = (it->s ? -1
6982 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6983 : IT_CHARPOS (*it));
6984 int c;
6986 if (it->what == IT_CHARACTER)
6987 c = it->char_to_display;
6988 else
6990 struct composition *cmp = composition_table[it->cmp_it.id];
6991 int i;
6993 c = ' ';
6994 for (i = 0; i < cmp->glyph_len; i++)
6995 /* TAB in a composition means display glyphs with
6996 padding space on the left or right. */
6997 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6998 break;
7000 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7004 done:
7005 /* Is this character the last one of a run of characters with
7006 box? If yes, set IT->end_of_box_run_p to 1. */
7007 if (it->face_box_p
7008 && it->s == NULL)
7010 if (it->method == GET_FROM_STRING && it->sp)
7012 int face_id = underlying_face_id (it);
7013 struct face *face = FACE_FROM_ID (it->f, face_id);
7015 if (face)
7017 if (face->box == FACE_NO_BOX)
7019 /* If the box comes from face properties in a
7020 display string, check faces in that string. */
7021 int string_face_id = face_after_it_pos (it);
7022 it->end_of_box_run_p
7023 = (FACE_FROM_ID (it->f, string_face_id)->box
7024 == FACE_NO_BOX);
7026 /* Otherwise, the box comes from the underlying face.
7027 If this is the last string character displayed, check
7028 the next buffer location. */
7029 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7030 && (it->current.overlay_string_index
7031 == it->n_overlay_strings - 1))
7033 ptrdiff_t ignore;
7034 int next_face_id;
7035 struct text_pos pos = it->current.pos;
7036 INC_TEXT_POS (pos, it->multibyte_p);
7038 next_face_id = face_at_buffer_position
7039 (it->w, CHARPOS (pos), it->region_beg_charpos,
7040 it->region_end_charpos, &ignore,
7041 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7042 -1);
7043 it->end_of_box_run_p
7044 = (FACE_FROM_ID (it->f, next_face_id)->box
7045 == FACE_NO_BOX);
7049 /* next_element_from_display_vector sets this flag according to
7050 faces of the display vector glyphs, see there. */
7051 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7053 int face_id = face_after_it_pos (it);
7054 it->end_of_box_run_p
7055 = (face_id != it->face_id
7056 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7059 /* If we reached the end of the object we've been iterating (e.g., a
7060 display string or an overlay string), and there's something on
7061 IT->stack, proceed with what's on the stack. It doesn't make
7062 sense to return zero if there's unprocessed stuff on the stack,
7063 because otherwise that stuff will never be displayed. */
7064 if (!success_p && it->sp > 0)
7066 set_iterator_to_next (it, 0);
7067 success_p = get_next_display_element (it);
7070 /* Value is 0 if end of buffer or string reached. */
7071 return success_p;
7075 /* Move IT to the next display element.
7077 RESEAT_P non-zero means if called on a newline in buffer text,
7078 skip to the next visible line start.
7080 Functions get_next_display_element and set_iterator_to_next are
7081 separate because I find this arrangement easier to handle than a
7082 get_next_display_element function that also increments IT's
7083 position. The way it is we can first look at an iterator's current
7084 display element, decide whether it fits on a line, and if it does,
7085 increment the iterator position. The other way around we probably
7086 would either need a flag indicating whether the iterator has to be
7087 incremented the next time, or we would have to implement a
7088 decrement position function which would not be easy to write. */
7090 void
7091 set_iterator_to_next (struct it *it, int reseat_p)
7093 /* Reset flags indicating start and end of a sequence of characters
7094 with box. Reset them at the start of this function because
7095 moving the iterator to a new position might set them. */
7096 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7098 switch (it->method)
7100 case GET_FROM_BUFFER:
7101 /* The current display element of IT is a character from
7102 current_buffer. Advance in the buffer, and maybe skip over
7103 invisible lines that are so because of selective display. */
7104 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7105 reseat_at_next_visible_line_start (it, 0);
7106 else if (it->cmp_it.id >= 0)
7108 /* We are currently getting glyphs from a composition. */
7109 int i;
7111 if (! it->bidi_p)
7113 IT_CHARPOS (*it) += it->cmp_it.nchars;
7114 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7115 if (it->cmp_it.to < it->cmp_it.nglyphs)
7117 it->cmp_it.from = it->cmp_it.to;
7119 else
7121 it->cmp_it.id = -1;
7122 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7123 IT_BYTEPOS (*it),
7124 it->end_charpos, Qnil);
7127 else if (! it->cmp_it.reversed_p)
7129 /* Composition created while scanning forward. */
7130 /* Update IT's char/byte positions to point to the first
7131 character of the next grapheme cluster, or to the
7132 character visually after the current composition. */
7133 for (i = 0; i < it->cmp_it.nchars; i++)
7134 bidi_move_to_visually_next (&it->bidi_it);
7135 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7136 IT_CHARPOS (*it) = it->bidi_it.charpos;
7138 if (it->cmp_it.to < it->cmp_it.nglyphs)
7140 /* Proceed to the next grapheme cluster. */
7141 it->cmp_it.from = it->cmp_it.to;
7143 else
7145 /* No more grapheme clusters in this composition.
7146 Find the next stop position. */
7147 ptrdiff_t stop = it->end_charpos;
7148 if (it->bidi_it.scan_dir < 0)
7149 /* Now we are scanning backward and don't know
7150 where to stop. */
7151 stop = -1;
7152 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7153 IT_BYTEPOS (*it), stop, Qnil);
7156 else
7158 /* Composition created while scanning backward. */
7159 /* Update IT's char/byte positions to point to the last
7160 character of the previous grapheme cluster, or the
7161 character visually after the current composition. */
7162 for (i = 0; i < it->cmp_it.nchars; i++)
7163 bidi_move_to_visually_next (&it->bidi_it);
7164 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7165 IT_CHARPOS (*it) = it->bidi_it.charpos;
7166 if (it->cmp_it.from > 0)
7168 /* Proceed to the previous grapheme cluster. */
7169 it->cmp_it.to = it->cmp_it.from;
7171 else
7173 /* No more grapheme clusters in this composition.
7174 Find the next stop position. */
7175 ptrdiff_t stop = it->end_charpos;
7176 if (it->bidi_it.scan_dir < 0)
7177 /* Now we are scanning backward and don't know
7178 where to stop. */
7179 stop = -1;
7180 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7181 IT_BYTEPOS (*it), stop, Qnil);
7185 else
7187 eassert (it->len != 0);
7189 if (!it->bidi_p)
7191 IT_BYTEPOS (*it) += it->len;
7192 IT_CHARPOS (*it) += 1;
7194 else
7196 int prev_scan_dir = it->bidi_it.scan_dir;
7197 /* If this is a new paragraph, determine its base
7198 direction (a.k.a. its base embedding level). */
7199 if (it->bidi_it.new_paragraph)
7200 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7201 bidi_move_to_visually_next (&it->bidi_it);
7202 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7203 IT_CHARPOS (*it) = it->bidi_it.charpos;
7204 if (prev_scan_dir != it->bidi_it.scan_dir)
7206 /* As the scan direction was changed, we must
7207 re-compute the stop position for composition. */
7208 ptrdiff_t stop = it->end_charpos;
7209 if (it->bidi_it.scan_dir < 0)
7210 stop = -1;
7211 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7212 IT_BYTEPOS (*it), stop, Qnil);
7215 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7217 break;
7219 case GET_FROM_C_STRING:
7220 /* Current display element of IT is from a C string. */
7221 if (!it->bidi_p
7222 /* If the string position is beyond string's end, it means
7223 next_element_from_c_string is padding the string with
7224 blanks, in which case we bypass the bidi iterator,
7225 because it cannot deal with such virtual characters. */
7226 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7228 IT_BYTEPOS (*it) += it->len;
7229 IT_CHARPOS (*it) += 1;
7231 else
7233 bidi_move_to_visually_next (&it->bidi_it);
7234 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7235 IT_CHARPOS (*it) = it->bidi_it.charpos;
7237 break;
7239 case GET_FROM_DISPLAY_VECTOR:
7240 /* Current display element of IT is from a display table entry.
7241 Advance in the display table definition. Reset it to null if
7242 end reached, and continue with characters from buffers/
7243 strings. */
7244 ++it->current.dpvec_index;
7246 /* Restore face of the iterator to what they were before the
7247 display vector entry (these entries may contain faces). */
7248 it->face_id = it->saved_face_id;
7250 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7252 int recheck_faces = it->ellipsis_p;
7254 if (it->s)
7255 it->method = GET_FROM_C_STRING;
7256 else if (STRINGP (it->string))
7257 it->method = GET_FROM_STRING;
7258 else
7260 it->method = GET_FROM_BUFFER;
7261 it->object = it->w->contents;
7264 it->dpvec = NULL;
7265 it->current.dpvec_index = -1;
7267 /* Skip over characters which were displayed via IT->dpvec. */
7268 if (it->dpvec_char_len < 0)
7269 reseat_at_next_visible_line_start (it, 1);
7270 else if (it->dpvec_char_len > 0)
7272 if (it->method == GET_FROM_STRING
7273 && it->current.overlay_string_index >= 0
7274 && it->n_overlay_strings > 0)
7275 it->ignore_overlay_strings_at_pos_p = 1;
7276 it->len = it->dpvec_char_len;
7277 set_iterator_to_next (it, reseat_p);
7280 /* Maybe recheck faces after display vector */
7281 if (recheck_faces)
7282 it->stop_charpos = IT_CHARPOS (*it);
7284 break;
7286 case GET_FROM_STRING:
7287 /* Current display element is a character from a Lisp string. */
7288 eassert (it->s == NULL && STRINGP (it->string));
7289 /* Don't advance past string end. These conditions are true
7290 when set_iterator_to_next is called at the end of
7291 get_next_display_element, in which case the Lisp string is
7292 already exhausted, and all we want is pop the iterator
7293 stack. */
7294 if (it->current.overlay_string_index >= 0)
7296 /* This is an overlay string, so there's no padding with
7297 spaces, and the number of characters in the string is
7298 where the string ends. */
7299 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7300 goto consider_string_end;
7302 else
7304 /* Not an overlay string. There could be padding, so test
7305 against it->end_charpos . */
7306 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7307 goto consider_string_end;
7309 if (it->cmp_it.id >= 0)
7311 int i;
7313 if (! it->bidi_p)
7315 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7316 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7317 if (it->cmp_it.to < it->cmp_it.nglyphs)
7318 it->cmp_it.from = it->cmp_it.to;
7319 else
7321 it->cmp_it.id = -1;
7322 composition_compute_stop_pos (&it->cmp_it,
7323 IT_STRING_CHARPOS (*it),
7324 IT_STRING_BYTEPOS (*it),
7325 it->end_charpos, it->string);
7328 else if (! it->cmp_it.reversed_p)
7330 for (i = 0; i < it->cmp_it.nchars; i++)
7331 bidi_move_to_visually_next (&it->bidi_it);
7332 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7333 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7335 if (it->cmp_it.to < it->cmp_it.nglyphs)
7336 it->cmp_it.from = it->cmp_it.to;
7337 else
7339 ptrdiff_t stop = it->end_charpos;
7340 if (it->bidi_it.scan_dir < 0)
7341 stop = -1;
7342 composition_compute_stop_pos (&it->cmp_it,
7343 IT_STRING_CHARPOS (*it),
7344 IT_STRING_BYTEPOS (*it), stop,
7345 it->string);
7348 else
7350 for (i = 0; i < it->cmp_it.nchars; i++)
7351 bidi_move_to_visually_next (&it->bidi_it);
7352 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7353 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7354 if (it->cmp_it.from > 0)
7355 it->cmp_it.to = it->cmp_it.from;
7356 else
7358 ptrdiff_t stop = it->end_charpos;
7359 if (it->bidi_it.scan_dir < 0)
7360 stop = -1;
7361 composition_compute_stop_pos (&it->cmp_it,
7362 IT_STRING_CHARPOS (*it),
7363 IT_STRING_BYTEPOS (*it), stop,
7364 it->string);
7368 else
7370 if (!it->bidi_p
7371 /* If the string position is beyond string's end, it
7372 means next_element_from_string is padding the string
7373 with blanks, in which case we bypass the bidi
7374 iterator, because it cannot deal with such virtual
7375 characters. */
7376 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7378 IT_STRING_BYTEPOS (*it) += it->len;
7379 IT_STRING_CHARPOS (*it) += 1;
7381 else
7383 int prev_scan_dir = it->bidi_it.scan_dir;
7385 bidi_move_to_visually_next (&it->bidi_it);
7386 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7387 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7388 if (prev_scan_dir != it->bidi_it.scan_dir)
7390 ptrdiff_t stop = it->end_charpos;
7392 if (it->bidi_it.scan_dir < 0)
7393 stop = -1;
7394 composition_compute_stop_pos (&it->cmp_it,
7395 IT_STRING_CHARPOS (*it),
7396 IT_STRING_BYTEPOS (*it), stop,
7397 it->string);
7402 consider_string_end:
7404 if (it->current.overlay_string_index >= 0)
7406 /* IT->string is an overlay string. Advance to the
7407 next, if there is one. */
7408 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7410 it->ellipsis_p = 0;
7411 next_overlay_string (it);
7412 if (it->ellipsis_p)
7413 setup_for_ellipsis (it, 0);
7416 else
7418 /* IT->string is not an overlay string. If we reached
7419 its end, and there is something on IT->stack, proceed
7420 with what is on the stack. This can be either another
7421 string, this time an overlay string, or a buffer. */
7422 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7423 && it->sp > 0)
7425 pop_it (it);
7426 if (it->method == GET_FROM_STRING)
7427 goto consider_string_end;
7430 break;
7432 case GET_FROM_IMAGE:
7433 case GET_FROM_STRETCH:
7434 /* The position etc with which we have to proceed are on
7435 the stack. The position may be at the end of a string,
7436 if the `display' property takes up the whole string. */
7437 eassert (it->sp > 0);
7438 pop_it (it);
7439 if (it->method == GET_FROM_STRING)
7440 goto consider_string_end;
7441 break;
7443 default:
7444 /* There are no other methods defined, so this should be a bug. */
7445 emacs_abort ();
7448 eassert (it->method != GET_FROM_STRING
7449 || (STRINGP (it->string)
7450 && IT_STRING_CHARPOS (*it) >= 0));
7453 /* Load IT's display element fields with information about the next
7454 display element which comes from a display table entry or from the
7455 result of translating a control character to one of the forms `^C'
7456 or `\003'.
7458 IT->dpvec holds the glyphs to return as characters.
7459 IT->saved_face_id holds the face id before the display vector--it
7460 is restored into IT->face_id in set_iterator_to_next. */
7462 static int
7463 next_element_from_display_vector (struct it *it)
7465 Lisp_Object gc;
7466 int prev_face_id = it->face_id;
7467 int next_face_id;
7469 /* Precondition. */
7470 eassert (it->dpvec && it->current.dpvec_index >= 0);
7472 it->face_id = it->saved_face_id;
7474 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7475 That seemed totally bogus - so I changed it... */
7476 gc = it->dpvec[it->current.dpvec_index];
7478 if (GLYPH_CODE_P (gc))
7480 struct face *this_face, *prev_face, *next_face;
7482 it->c = GLYPH_CODE_CHAR (gc);
7483 it->len = CHAR_BYTES (it->c);
7485 /* The entry may contain a face id to use. Such a face id is
7486 the id of a Lisp face, not a realized face. A face id of
7487 zero means no face is specified. */
7488 if (it->dpvec_face_id >= 0)
7489 it->face_id = it->dpvec_face_id;
7490 else
7492 int lface_id = GLYPH_CODE_FACE (gc);
7493 if (lface_id > 0)
7494 it->face_id = merge_faces (it->f, Qt, lface_id,
7495 it->saved_face_id);
7498 /* Glyphs in the display vector could have the box face, so we
7499 need to set the related flags in the iterator, as
7500 appropriate. */
7501 this_face = FACE_FROM_ID (it->f, it->face_id);
7502 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7504 /* Is this character the first character of a box-face run? */
7505 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7506 && (!prev_face
7507 || prev_face->box == FACE_NO_BOX));
7509 /* For the last character of the box-face run, we need to look
7510 either at the next glyph from the display vector, or at the
7511 face we saw before the display vector. */
7512 next_face_id = it->saved_face_id;
7513 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7515 if (it->dpvec_face_id >= 0)
7516 next_face_id = it->dpvec_face_id;
7517 else
7519 int lface_id =
7520 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7522 if (lface_id > 0)
7523 next_face_id = merge_faces (it->f, Qt, lface_id,
7524 it->saved_face_id);
7527 next_face = FACE_FROM_ID (it->f, next_face_id);
7528 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7529 && (!next_face
7530 || next_face->box == FACE_NO_BOX));
7531 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7533 else
7534 /* Display table entry is invalid. Return a space. */
7535 it->c = ' ', it->len = 1;
7537 /* Don't change position and object of the iterator here. They are
7538 still the values of the character that had this display table
7539 entry or was translated, and that's what we want. */
7540 it->what = IT_CHARACTER;
7541 return 1;
7544 /* Get the first element of string/buffer in the visual order, after
7545 being reseated to a new position in a string or a buffer. */
7546 static void
7547 get_visually_first_element (struct it *it)
7549 int string_p = STRINGP (it->string) || it->s;
7550 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7551 ptrdiff_t bob = (string_p ? 0 : BEGV);
7553 if (STRINGP (it->string))
7555 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7556 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7558 else
7560 it->bidi_it.charpos = IT_CHARPOS (*it);
7561 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7564 if (it->bidi_it.charpos == eob)
7566 /* Nothing to do, but reset the FIRST_ELT flag, like
7567 bidi_paragraph_init does, because we are not going to
7568 call it. */
7569 it->bidi_it.first_elt = 0;
7571 else if (it->bidi_it.charpos == bob
7572 || (!string_p
7573 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7574 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7576 /* If we are at the beginning of a line/string, we can produce
7577 the next element right away. */
7578 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7579 bidi_move_to_visually_next (&it->bidi_it);
7581 else
7583 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7585 /* We need to prime the bidi iterator starting at the line's or
7586 string's beginning, before we will be able to produce the
7587 next element. */
7588 if (string_p)
7589 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7590 else
7591 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7592 IT_BYTEPOS (*it), -1,
7593 &it->bidi_it.bytepos);
7594 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7597 /* Now return to buffer/string position where we were asked
7598 to get the next display element, and produce that. */
7599 bidi_move_to_visually_next (&it->bidi_it);
7601 while (it->bidi_it.bytepos != orig_bytepos
7602 && it->bidi_it.charpos < eob);
7605 /* Adjust IT's position information to where we ended up. */
7606 if (STRINGP (it->string))
7608 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7609 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7611 else
7613 IT_CHARPOS (*it) = it->bidi_it.charpos;
7614 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7617 if (STRINGP (it->string) || !it->s)
7619 ptrdiff_t stop, charpos, bytepos;
7621 if (STRINGP (it->string))
7623 eassert (!it->s);
7624 stop = SCHARS (it->string);
7625 if (stop > it->end_charpos)
7626 stop = it->end_charpos;
7627 charpos = IT_STRING_CHARPOS (*it);
7628 bytepos = IT_STRING_BYTEPOS (*it);
7630 else
7632 stop = it->end_charpos;
7633 charpos = IT_CHARPOS (*it);
7634 bytepos = IT_BYTEPOS (*it);
7636 if (it->bidi_it.scan_dir < 0)
7637 stop = -1;
7638 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7639 it->string);
7643 /* Load IT with the next display element from Lisp string IT->string.
7644 IT->current.string_pos is the current position within the string.
7645 If IT->current.overlay_string_index >= 0, the Lisp string is an
7646 overlay string. */
7648 static int
7649 next_element_from_string (struct it *it)
7651 struct text_pos position;
7653 eassert (STRINGP (it->string));
7654 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7655 eassert (IT_STRING_CHARPOS (*it) >= 0);
7656 position = it->current.string_pos;
7658 /* With bidi reordering, the character to display might not be the
7659 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7660 that we were reseat()ed to a new string, whose paragraph
7661 direction is not known. */
7662 if (it->bidi_p && it->bidi_it.first_elt)
7664 get_visually_first_element (it);
7665 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7668 /* Time to check for invisible text? */
7669 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7671 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7673 if (!(!it->bidi_p
7674 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7675 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7677 /* With bidi non-linear iteration, we could find
7678 ourselves far beyond the last computed stop_charpos,
7679 with several other stop positions in between that we
7680 missed. Scan them all now, in buffer's logical
7681 order, until we find and handle the last stop_charpos
7682 that precedes our current position. */
7683 handle_stop_backwards (it, it->stop_charpos);
7684 return GET_NEXT_DISPLAY_ELEMENT (it);
7686 else
7688 if (it->bidi_p)
7690 /* Take note of the stop position we just moved
7691 across, for when we will move back across it. */
7692 it->prev_stop = it->stop_charpos;
7693 /* If we are at base paragraph embedding level, take
7694 note of the last stop position seen at this
7695 level. */
7696 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7697 it->base_level_stop = it->stop_charpos;
7699 handle_stop (it);
7701 /* Since a handler may have changed IT->method, we must
7702 recurse here. */
7703 return GET_NEXT_DISPLAY_ELEMENT (it);
7706 else if (it->bidi_p
7707 /* If we are before prev_stop, we may have overstepped
7708 on our way backwards a stop_pos, and if so, we need
7709 to handle that stop_pos. */
7710 && IT_STRING_CHARPOS (*it) < it->prev_stop
7711 /* We can sometimes back up for reasons that have nothing
7712 to do with bidi reordering. E.g., compositions. The
7713 code below is only needed when we are above the base
7714 embedding level, so test for that explicitly. */
7715 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7717 /* If we lost track of base_level_stop, we have no better
7718 place for handle_stop_backwards to start from than string
7719 beginning. This happens, e.g., when we were reseated to
7720 the previous screenful of text by vertical-motion. */
7721 if (it->base_level_stop <= 0
7722 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7723 it->base_level_stop = 0;
7724 handle_stop_backwards (it, it->base_level_stop);
7725 return GET_NEXT_DISPLAY_ELEMENT (it);
7729 if (it->current.overlay_string_index >= 0)
7731 /* Get the next character from an overlay string. In overlay
7732 strings, there is no field width or padding with spaces to
7733 do. */
7734 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7736 it->what = IT_EOB;
7737 return 0;
7739 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7740 IT_STRING_BYTEPOS (*it),
7741 it->bidi_it.scan_dir < 0
7742 ? -1
7743 : SCHARS (it->string))
7744 && next_element_from_composition (it))
7746 return 1;
7748 else if (STRING_MULTIBYTE (it->string))
7750 const unsigned char *s = (SDATA (it->string)
7751 + IT_STRING_BYTEPOS (*it));
7752 it->c = string_char_and_length (s, &it->len);
7754 else
7756 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7757 it->len = 1;
7760 else
7762 /* Get the next character from a Lisp string that is not an
7763 overlay string. Such strings come from the mode line, for
7764 example. We may have to pad with spaces, or truncate the
7765 string. See also next_element_from_c_string. */
7766 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7768 it->what = IT_EOB;
7769 return 0;
7771 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7773 /* Pad with spaces. */
7774 it->c = ' ', it->len = 1;
7775 CHARPOS (position) = BYTEPOS (position) = -1;
7777 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7778 IT_STRING_BYTEPOS (*it),
7779 it->bidi_it.scan_dir < 0
7780 ? -1
7781 : it->string_nchars)
7782 && next_element_from_composition (it))
7784 return 1;
7786 else if (STRING_MULTIBYTE (it->string))
7788 const unsigned char *s = (SDATA (it->string)
7789 + IT_STRING_BYTEPOS (*it));
7790 it->c = string_char_and_length (s, &it->len);
7792 else
7794 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7795 it->len = 1;
7799 /* Record what we have and where it came from. */
7800 it->what = IT_CHARACTER;
7801 it->object = it->string;
7802 it->position = position;
7803 return 1;
7807 /* Load IT with next display element from C string IT->s.
7808 IT->string_nchars is the maximum number of characters to return
7809 from the string. IT->end_charpos may be greater than
7810 IT->string_nchars when this function is called, in which case we
7811 may have to return padding spaces. Value is zero if end of string
7812 reached, including padding spaces. */
7814 static int
7815 next_element_from_c_string (struct it *it)
7817 int success_p = 1;
7819 eassert (it->s);
7820 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7821 it->what = IT_CHARACTER;
7822 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7823 it->object = Qnil;
7825 /* With bidi reordering, the character to display might not be the
7826 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7827 we were reseated to a new string, whose paragraph direction is
7828 not known. */
7829 if (it->bidi_p && it->bidi_it.first_elt)
7830 get_visually_first_element (it);
7832 /* IT's position can be greater than IT->string_nchars in case a
7833 field width or precision has been specified when the iterator was
7834 initialized. */
7835 if (IT_CHARPOS (*it) >= it->end_charpos)
7837 /* End of the game. */
7838 it->what = IT_EOB;
7839 success_p = 0;
7841 else if (IT_CHARPOS (*it) >= it->string_nchars)
7843 /* Pad with spaces. */
7844 it->c = ' ', it->len = 1;
7845 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7847 else if (it->multibyte_p)
7848 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7849 else
7850 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7852 return success_p;
7856 /* Set up IT to return characters from an ellipsis, if appropriate.
7857 The definition of the ellipsis glyphs may come from a display table
7858 entry. This function fills IT with the first glyph from the
7859 ellipsis if an ellipsis is to be displayed. */
7861 static int
7862 next_element_from_ellipsis (struct it *it)
7864 if (it->selective_display_ellipsis_p)
7865 setup_for_ellipsis (it, it->len);
7866 else
7868 /* The face at the current position may be different from the
7869 face we find after the invisible text. Remember what it
7870 was in IT->saved_face_id, and signal that it's there by
7871 setting face_before_selective_p. */
7872 it->saved_face_id = it->face_id;
7873 it->method = GET_FROM_BUFFER;
7874 it->object = it->w->contents;
7875 reseat_at_next_visible_line_start (it, 1);
7876 it->face_before_selective_p = 1;
7879 return GET_NEXT_DISPLAY_ELEMENT (it);
7883 /* Deliver an image display element. The iterator IT is already
7884 filled with image information (done in handle_display_prop). Value
7885 is always 1. */
7888 static int
7889 next_element_from_image (struct it *it)
7891 it->what = IT_IMAGE;
7892 it->ignore_overlay_strings_at_pos_p = 0;
7893 return 1;
7897 /* Fill iterator IT with next display element from a stretch glyph
7898 property. IT->object is the value of the text property. Value is
7899 always 1. */
7901 static int
7902 next_element_from_stretch (struct it *it)
7904 it->what = IT_STRETCH;
7905 return 1;
7908 /* Scan backwards from IT's current position until we find a stop
7909 position, or until BEGV. This is called when we find ourself
7910 before both the last known prev_stop and base_level_stop while
7911 reordering bidirectional text. */
7913 static void
7914 compute_stop_pos_backwards (struct it *it)
7916 const int SCAN_BACK_LIMIT = 1000;
7917 struct text_pos pos;
7918 struct display_pos save_current = it->current;
7919 struct text_pos save_position = it->position;
7920 ptrdiff_t charpos = IT_CHARPOS (*it);
7921 ptrdiff_t where_we_are = charpos;
7922 ptrdiff_t save_stop_pos = it->stop_charpos;
7923 ptrdiff_t save_end_pos = it->end_charpos;
7925 eassert (NILP (it->string) && !it->s);
7926 eassert (it->bidi_p);
7927 it->bidi_p = 0;
7930 it->end_charpos = min (charpos + 1, ZV);
7931 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7932 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7933 reseat_1 (it, pos, 0);
7934 compute_stop_pos (it);
7935 /* We must advance forward, right? */
7936 if (it->stop_charpos <= charpos)
7937 emacs_abort ();
7939 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7941 if (it->stop_charpos <= where_we_are)
7942 it->prev_stop = it->stop_charpos;
7943 else
7944 it->prev_stop = BEGV;
7945 it->bidi_p = 1;
7946 it->current = save_current;
7947 it->position = save_position;
7948 it->stop_charpos = save_stop_pos;
7949 it->end_charpos = save_end_pos;
7952 /* Scan forward from CHARPOS in the current buffer/string, until we
7953 find a stop position > current IT's position. Then handle the stop
7954 position before that. This is called when we bump into a stop
7955 position while reordering bidirectional text. CHARPOS should be
7956 the last previously processed stop_pos (or BEGV/0, if none were
7957 processed yet) whose position is less that IT's current
7958 position. */
7960 static void
7961 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7963 int bufp = !STRINGP (it->string);
7964 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7965 struct display_pos save_current = it->current;
7966 struct text_pos save_position = it->position;
7967 struct text_pos pos1;
7968 ptrdiff_t next_stop;
7970 /* Scan in strict logical order. */
7971 eassert (it->bidi_p);
7972 it->bidi_p = 0;
7975 it->prev_stop = charpos;
7976 if (bufp)
7978 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7979 reseat_1 (it, pos1, 0);
7981 else
7982 it->current.string_pos = string_pos (charpos, it->string);
7983 compute_stop_pos (it);
7984 /* We must advance forward, right? */
7985 if (it->stop_charpos <= it->prev_stop)
7986 emacs_abort ();
7987 charpos = it->stop_charpos;
7989 while (charpos <= where_we_are);
7991 it->bidi_p = 1;
7992 it->current = save_current;
7993 it->position = save_position;
7994 next_stop = it->stop_charpos;
7995 it->stop_charpos = it->prev_stop;
7996 handle_stop (it);
7997 it->stop_charpos = next_stop;
8000 /* Load IT with the next display element from current_buffer. Value
8001 is zero if end of buffer reached. IT->stop_charpos is the next
8002 position at which to stop and check for text properties or buffer
8003 end. */
8005 static int
8006 next_element_from_buffer (struct it *it)
8008 int success_p = 1;
8010 eassert (IT_CHARPOS (*it) >= BEGV);
8011 eassert (NILP (it->string) && !it->s);
8012 eassert (!it->bidi_p
8013 || (EQ (it->bidi_it.string.lstring, Qnil)
8014 && it->bidi_it.string.s == NULL));
8016 /* With bidi reordering, the character to display might not be the
8017 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8018 we were reseat()ed to a new buffer position, which is potentially
8019 a different paragraph. */
8020 if (it->bidi_p && it->bidi_it.first_elt)
8022 get_visually_first_element (it);
8023 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8026 if (IT_CHARPOS (*it) >= it->stop_charpos)
8028 if (IT_CHARPOS (*it) >= it->end_charpos)
8030 int overlay_strings_follow_p;
8032 /* End of the game, except when overlay strings follow that
8033 haven't been returned yet. */
8034 if (it->overlay_strings_at_end_processed_p)
8035 overlay_strings_follow_p = 0;
8036 else
8038 it->overlay_strings_at_end_processed_p = 1;
8039 overlay_strings_follow_p = get_overlay_strings (it, 0);
8042 if (overlay_strings_follow_p)
8043 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8044 else
8046 it->what = IT_EOB;
8047 it->position = it->current.pos;
8048 success_p = 0;
8051 else if (!(!it->bidi_p
8052 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8053 || IT_CHARPOS (*it) == it->stop_charpos))
8055 /* With bidi non-linear iteration, we could find ourselves
8056 far beyond the last computed stop_charpos, with several
8057 other stop positions in between that we missed. Scan
8058 them all now, in buffer's logical order, until we find
8059 and handle the last stop_charpos that precedes our
8060 current position. */
8061 handle_stop_backwards (it, it->stop_charpos);
8062 return GET_NEXT_DISPLAY_ELEMENT (it);
8064 else
8066 if (it->bidi_p)
8068 /* Take note of the stop position we just moved across,
8069 for when we will move back across it. */
8070 it->prev_stop = it->stop_charpos;
8071 /* If we are at base paragraph embedding level, take
8072 note of the last stop position seen at this
8073 level. */
8074 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8075 it->base_level_stop = it->stop_charpos;
8077 handle_stop (it);
8078 return GET_NEXT_DISPLAY_ELEMENT (it);
8081 else if (it->bidi_p
8082 /* If we are before prev_stop, we may have overstepped on
8083 our way backwards a stop_pos, and if so, we need to
8084 handle that stop_pos. */
8085 && IT_CHARPOS (*it) < it->prev_stop
8086 /* We can sometimes back up for reasons that have nothing
8087 to do with bidi reordering. E.g., compositions. The
8088 code below is only needed when we are above the base
8089 embedding level, so test for that explicitly. */
8090 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8092 if (it->base_level_stop <= 0
8093 || IT_CHARPOS (*it) < it->base_level_stop)
8095 /* If we lost track of base_level_stop, we need to find
8096 prev_stop by looking backwards. This happens, e.g., when
8097 we were reseated to the previous screenful of text by
8098 vertical-motion. */
8099 it->base_level_stop = BEGV;
8100 compute_stop_pos_backwards (it);
8101 handle_stop_backwards (it, it->prev_stop);
8103 else
8104 handle_stop_backwards (it, it->base_level_stop);
8105 return GET_NEXT_DISPLAY_ELEMENT (it);
8107 else
8109 /* No face changes, overlays etc. in sight, so just return a
8110 character from current_buffer. */
8111 unsigned char *p;
8112 ptrdiff_t stop;
8114 /* Maybe run the redisplay end trigger hook. Performance note:
8115 This doesn't seem to cost measurable time. */
8116 if (it->redisplay_end_trigger_charpos
8117 && it->glyph_row
8118 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8119 run_redisplay_end_trigger_hook (it);
8121 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8122 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8123 stop)
8124 && next_element_from_composition (it))
8126 return 1;
8129 /* Get the next character, maybe multibyte. */
8130 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8131 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8132 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8133 else
8134 it->c = *p, it->len = 1;
8136 /* Record what we have and where it came from. */
8137 it->what = IT_CHARACTER;
8138 it->object = it->w->contents;
8139 it->position = it->current.pos;
8141 /* Normally we return the character found above, except when we
8142 really want to return an ellipsis for selective display. */
8143 if (it->selective)
8145 if (it->c == '\n')
8147 /* A value of selective > 0 means hide lines indented more
8148 than that number of columns. */
8149 if (it->selective > 0
8150 && IT_CHARPOS (*it) + 1 < ZV
8151 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8152 IT_BYTEPOS (*it) + 1,
8153 it->selective))
8155 success_p = next_element_from_ellipsis (it);
8156 it->dpvec_char_len = -1;
8159 else if (it->c == '\r' && it->selective == -1)
8161 /* A value of selective == -1 means that everything from the
8162 CR to the end of the line is invisible, with maybe an
8163 ellipsis displayed for it. */
8164 success_p = next_element_from_ellipsis (it);
8165 it->dpvec_char_len = -1;
8170 /* Value is zero if end of buffer reached. */
8171 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8172 return success_p;
8176 /* Run the redisplay end trigger hook for IT. */
8178 static void
8179 run_redisplay_end_trigger_hook (struct it *it)
8181 Lisp_Object args[3];
8183 /* IT->glyph_row should be non-null, i.e. we should be actually
8184 displaying something, or otherwise we should not run the hook. */
8185 eassert (it->glyph_row);
8187 /* Set up hook arguments. */
8188 args[0] = Qredisplay_end_trigger_functions;
8189 args[1] = it->window;
8190 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8191 it->redisplay_end_trigger_charpos = 0;
8193 /* Since we are *trying* to run these functions, don't try to run
8194 them again, even if they get an error. */
8195 wset_redisplay_end_trigger (it->w, Qnil);
8196 Frun_hook_with_args (3, args);
8198 /* Notice if it changed the face of the character we are on. */
8199 handle_face_prop (it);
8203 /* Deliver a composition display element. Unlike the other
8204 next_element_from_XXX, this function is not registered in the array
8205 get_next_element[]. It is called from next_element_from_buffer and
8206 next_element_from_string when necessary. */
8208 static int
8209 next_element_from_composition (struct it *it)
8211 it->what = IT_COMPOSITION;
8212 it->len = it->cmp_it.nbytes;
8213 if (STRINGP (it->string))
8215 if (it->c < 0)
8217 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8218 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8219 return 0;
8221 it->position = it->current.string_pos;
8222 it->object = it->string;
8223 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8224 IT_STRING_BYTEPOS (*it), it->string);
8226 else
8228 if (it->c < 0)
8230 IT_CHARPOS (*it) += it->cmp_it.nchars;
8231 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8232 if (it->bidi_p)
8234 if (it->bidi_it.new_paragraph)
8235 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8236 /* Resync the bidi iterator with IT's new position.
8237 FIXME: this doesn't support bidirectional text. */
8238 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8239 bidi_move_to_visually_next (&it->bidi_it);
8241 return 0;
8243 it->position = it->current.pos;
8244 it->object = it->w->contents;
8245 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8246 IT_BYTEPOS (*it), Qnil);
8248 return 1;
8253 /***********************************************************************
8254 Moving an iterator without producing glyphs
8255 ***********************************************************************/
8257 /* Check if iterator is at a position corresponding to a valid buffer
8258 position after some move_it_ call. */
8260 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8261 ((it)->method == GET_FROM_STRING \
8262 ? IT_STRING_CHARPOS (*it) == 0 \
8263 : 1)
8266 /* Move iterator IT to a specified buffer or X position within one
8267 line on the display without producing glyphs.
8269 OP should be a bit mask including some or all of these bits:
8270 MOVE_TO_X: Stop upon reaching x-position TO_X.
8271 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8272 Regardless of OP's value, stop upon reaching the end of the display line.
8274 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8275 This means, in particular, that TO_X includes window's horizontal
8276 scroll amount.
8278 The return value has several possible values that
8279 say what condition caused the scan to stop:
8281 MOVE_POS_MATCH_OR_ZV
8282 - when TO_POS or ZV was reached.
8284 MOVE_X_REACHED
8285 -when TO_X was reached before TO_POS or ZV were reached.
8287 MOVE_LINE_CONTINUED
8288 - when we reached the end of the display area and the line must
8289 be continued.
8291 MOVE_LINE_TRUNCATED
8292 - when we reached the end of the display area and the line is
8293 truncated.
8295 MOVE_NEWLINE_OR_CR
8296 - when we stopped at a line end, i.e. a newline or a CR and selective
8297 display is on. */
8299 static enum move_it_result
8300 move_it_in_display_line_to (struct it *it,
8301 ptrdiff_t to_charpos, int to_x,
8302 enum move_operation_enum op)
8304 enum move_it_result result = MOVE_UNDEFINED;
8305 struct glyph_row *saved_glyph_row;
8306 struct it wrap_it, atpos_it, atx_it, ppos_it;
8307 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8308 void *ppos_data = NULL;
8309 int may_wrap = 0;
8310 enum it_method prev_method = it->method;
8311 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8312 int saw_smaller_pos = prev_pos < to_charpos;
8314 /* Don't produce glyphs in produce_glyphs. */
8315 saved_glyph_row = it->glyph_row;
8316 it->glyph_row = NULL;
8318 /* Use wrap_it to save a copy of IT wherever a word wrap could
8319 occur. Use atpos_it to save a copy of IT at the desired buffer
8320 position, if found, so that we can scan ahead and check if the
8321 word later overshoots the window edge. Use atx_it similarly, for
8322 pixel positions. */
8323 wrap_it.sp = -1;
8324 atpos_it.sp = -1;
8325 atx_it.sp = -1;
8327 /* Use ppos_it under bidi reordering to save a copy of IT for the
8328 position > CHARPOS that is the closest to CHARPOS. We restore
8329 that position in IT when we have scanned the entire display line
8330 without finding a match for CHARPOS and all the character
8331 positions are greater than CHARPOS. */
8332 if (it->bidi_p)
8334 SAVE_IT (ppos_it, *it, ppos_data);
8335 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8336 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8337 SAVE_IT (ppos_it, *it, ppos_data);
8340 #define BUFFER_POS_REACHED_P() \
8341 ((op & MOVE_TO_POS) != 0 \
8342 && BUFFERP (it->object) \
8343 && (IT_CHARPOS (*it) == to_charpos \
8344 || ((!it->bidi_p \
8345 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8346 && IT_CHARPOS (*it) > to_charpos) \
8347 || (it->what == IT_COMPOSITION \
8348 && ((IT_CHARPOS (*it) > to_charpos \
8349 && to_charpos >= it->cmp_it.charpos) \
8350 || (IT_CHARPOS (*it) < to_charpos \
8351 && to_charpos <= it->cmp_it.charpos)))) \
8352 && (it->method == GET_FROM_BUFFER \
8353 || (it->method == GET_FROM_DISPLAY_VECTOR \
8354 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8356 /* If there's a line-/wrap-prefix, handle it. */
8357 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8358 && it->current_y < it->last_visible_y)
8359 handle_line_prefix (it);
8361 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8362 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8364 while (1)
8366 int x, i, ascent = 0, descent = 0;
8368 /* Utility macro to reset an iterator with x, ascent, and descent. */
8369 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8370 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8371 (IT)->max_descent = descent)
8373 /* Stop if we move beyond TO_CHARPOS (after an image or a
8374 display string or stretch glyph). */
8375 if ((op & MOVE_TO_POS) != 0
8376 && BUFFERP (it->object)
8377 && it->method == GET_FROM_BUFFER
8378 && (((!it->bidi_p
8379 /* When the iterator is at base embedding level, we
8380 are guaranteed that characters are delivered for
8381 display in strictly increasing order of their
8382 buffer positions. */
8383 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8384 && IT_CHARPOS (*it) > to_charpos)
8385 || (it->bidi_p
8386 && (prev_method == GET_FROM_IMAGE
8387 || prev_method == GET_FROM_STRETCH
8388 || prev_method == GET_FROM_STRING)
8389 /* Passed TO_CHARPOS from left to right. */
8390 && ((prev_pos < to_charpos
8391 && IT_CHARPOS (*it) > to_charpos)
8392 /* Passed TO_CHARPOS from right to left. */
8393 || (prev_pos > to_charpos
8394 && IT_CHARPOS (*it) < to_charpos)))))
8396 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8398 result = MOVE_POS_MATCH_OR_ZV;
8399 break;
8401 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8402 /* If wrap_it is valid, the current position might be in a
8403 word that is wrapped. So, save the iterator in
8404 atpos_it and continue to see if wrapping happens. */
8405 SAVE_IT (atpos_it, *it, atpos_data);
8408 /* Stop when ZV reached.
8409 We used to stop here when TO_CHARPOS reached as well, but that is
8410 too soon if this glyph does not fit on this line. So we handle it
8411 explicitly below. */
8412 if (!get_next_display_element (it))
8414 result = MOVE_POS_MATCH_OR_ZV;
8415 break;
8418 if (it->line_wrap == TRUNCATE)
8420 if (BUFFER_POS_REACHED_P ())
8422 result = MOVE_POS_MATCH_OR_ZV;
8423 break;
8426 else
8428 if (it->line_wrap == WORD_WRAP)
8430 if (IT_DISPLAYING_WHITESPACE (it))
8431 may_wrap = 1;
8432 else if (may_wrap)
8434 /* We have reached a glyph that follows one or more
8435 whitespace characters. If the position is
8436 already found, we are done. */
8437 if (atpos_it.sp >= 0)
8439 RESTORE_IT (it, &atpos_it, atpos_data);
8440 result = MOVE_POS_MATCH_OR_ZV;
8441 goto done;
8443 if (atx_it.sp >= 0)
8445 RESTORE_IT (it, &atx_it, atx_data);
8446 result = MOVE_X_REACHED;
8447 goto done;
8449 /* Otherwise, we can wrap here. */
8450 SAVE_IT (wrap_it, *it, wrap_data);
8451 may_wrap = 0;
8456 /* Remember the line height for the current line, in case
8457 the next element doesn't fit on the line. */
8458 ascent = it->max_ascent;
8459 descent = it->max_descent;
8461 /* The call to produce_glyphs will get the metrics of the
8462 display element IT is loaded with. Record the x-position
8463 before this display element, in case it doesn't fit on the
8464 line. */
8465 x = it->current_x;
8467 PRODUCE_GLYPHS (it);
8469 if (it->area != TEXT_AREA)
8471 prev_method = it->method;
8472 if (it->method == GET_FROM_BUFFER)
8473 prev_pos = IT_CHARPOS (*it);
8474 set_iterator_to_next (it, 1);
8475 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8476 SET_TEXT_POS (this_line_min_pos,
8477 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8478 if (it->bidi_p
8479 && (op & MOVE_TO_POS)
8480 && IT_CHARPOS (*it) > to_charpos
8481 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8482 SAVE_IT (ppos_it, *it, ppos_data);
8483 continue;
8486 /* The number of glyphs we get back in IT->nglyphs will normally
8487 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8488 character on a terminal frame, or (iii) a line end. For the
8489 second case, IT->nglyphs - 1 padding glyphs will be present.
8490 (On X frames, there is only one glyph produced for a
8491 composite character.)
8493 The behavior implemented below means, for continuation lines,
8494 that as many spaces of a TAB as fit on the current line are
8495 displayed there. For terminal frames, as many glyphs of a
8496 multi-glyph character are displayed in the current line, too.
8497 This is what the old redisplay code did, and we keep it that
8498 way. Under X, the whole shape of a complex character must
8499 fit on the line or it will be completely displayed in the
8500 next line.
8502 Note that both for tabs and padding glyphs, all glyphs have
8503 the same width. */
8504 if (it->nglyphs)
8506 /* More than one glyph or glyph doesn't fit on line. All
8507 glyphs have the same width. */
8508 int single_glyph_width = it->pixel_width / it->nglyphs;
8509 int new_x;
8510 int x_before_this_char = x;
8511 int hpos_before_this_char = it->hpos;
8513 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8515 new_x = x + single_glyph_width;
8517 /* We want to leave anything reaching TO_X to the caller. */
8518 if ((op & MOVE_TO_X) && new_x > to_x)
8520 if (BUFFER_POS_REACHED_P ())
8522 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8523 goto buffer_pos_reached;
8524 if (atpos_it.sp < 0)
8526 SAVE_IT (atpos_it, *it, atpos_data);
8527 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8530 else
8532 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8534 it->current_x = x;
8535 result = MOVE_X_REACHED;
8536 break;
8538 if (atx_it.sp < 0)
8540 SAVE_IT (atx_it, *it, atx_data);
8541 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8546 if (/* Lines are continued. */
8547 it->line_wrap != TRUNCATE
8548 && (/* And glyph doesn't fit on the line. */
8549 new_x > it->last_visible_x
8550 /* Or it fits exactly and we're on a window
8551 system frame. */
8552 || (new_x == it->last_visible_x
8553 && FRAME_WINDOW_P (it->f)
8554 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8555 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8556 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8558 if (/* IT->hpos == 0 means the very first glyph
8559 doesn't fit on the line, e.g. a wide image. */
8560 it->hpos == 0
8561 || (new_x == it->last_visible_x
8562 && FRAME_WINDOW_P (it->f)))
8564 ++it->hpos;
8565 it->current_x = new_x;
8567 /* The character's last glyph just barely fits
8568 in this row. */
8569 if (i == it->nglyphs - 1)
8571 /* If this is the destination position,
8572 return a position *before* it in this row,
8573 now that we know it fits in this row. */
8574 if (BUFFER_POS_REACHED_P ())
8576 if (it->line_wrap != WORD_WRAP
8577 || wrap_it.sp < 0)
8579 it->hpos = hpos_before_this_char;
8580 it->current_x = x_before_this_char;
8581 result = MOVE_POS_MATCH_OR_ZV;
8582 break;
8584 if (it->line_wrap == WORD_WRAP
8585 && atpos_it.sp < 0)
8587 SAVE_IT (atpos_it, *it, atpos_data);
8588 atpos_it.current_x = x_before_this_char;
8589 atpos_it.hpos = hpos_before_this_char;
8593 prev_method = it->method;
8594 if (it->method == GET_FROM_BUFFER)
8595 prev_pos = IT_CHARPOS (*it);
8596 set_iterator_to_next (it, 1);
8597 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8598 SET_TEXT_POS (this_line_min_pos,
8599 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8600 /* On graphical terminals, newlines may
8601 "overflow" into the fringe if
8602 overflow-newline-into-fringe is non-nil.
8603 On text terminals, and on graphical
8604 terminals with no right margin, newlines
8605 may overflow into the last glyph on the
8606 display line.*/
8607 if (!FRAME_WINDOW_P (it->f)
8608 || ((it->bidi_p
8609 && it->bidi_it.paragraph_dir == R2L)
8610 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8611 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8612 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8614 if (!get_next_display_element (it))
8616 result = MOVE_POS_MATCH_OR_ZV;
8617 break;
8619 if (BUFFER_POS_REACHED_P ())
8621 if (ITERATOR_AT_END_OF_LINE_P (it))
8622 result = MOVE_POS_MATCH_OR_ZV;
8623 else
8624 result = MOVE_LINE_CONTINUED;
8625 break;
8627 if (ITERATOR_AT_END_OF_LINE_P (it)
8628 && (it->line_wrap != WORD_WRAP
8629 || wrap_it.sp < 0))
8631 result = MOVE_NEWLINE_OR_CR;
8632 break;
8637 else
8638 IT_RESET_X_ASCENT_DESCENT (it);
8640 if (wrap_it.sp >= 0)
8642 RESTORE_IT (it, &wrap_it, wrap_data);
8643 atpos_it.sp = -1;
8644 atx_it.sp = -1;
8647 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8648 IT_CHARPOS (*it)));
8649 result = MOVE_LINE_CONTINUED;
8650 break;
8653 if (BUFFER_POS_REACHED_P ())
8655 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8656 goto buffer_pos_reached;
8657 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8659 SAVE_IT (atpos_it, *it, atpos_data);
8660 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8664 if (new_x > it->first_visible_x)
8666 /* Glyph is visible. Increment number of glyphs that
8667 would be displayed. */
8668 ++it->hpos;
8672 if (result != MOVE_UNDEFINED)
8673 break;
8675 else if (BUFFER_POS_REACHED_P ())
8677 buffer_pos_reached:
8678 IT_RESET_X_ASCENT_DESCENT (it);
8679 result = MOVE_POS_MATCH_OR_ZV;
8680 break;
8682 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8684 /* Stop when TO_X specified and reached. This check is
8685 necessary here because of lines consisting of a line end,
8686 only. The line end will not produce any glyphs and we
8687 would never get MOVE_X_REACHED. */
8688 eassert (it->nglyphs == 0);
8689 result = MOVE_X_REACHED;
8690 break;
8693 /* Is this a line end? If yes, we're done. */
8694 if (ITERATOR_AT_END_OF_LINE_P (it))
8696 /* If we are past TO_CHARPOS, but never saw any character
8697 positions smaller than TO_CHARPOS, return
8698 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8699 did. */
8700 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8702 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8704 if (IT_CHARPOS (ppos_it) < ZV)
8706 RESTORE_IT (it, &ppos_it, ppos_data);
8707 result = MOVE_POS_MATCH_OR_ZV;
8709 else
8710 goto buffer_pos_reached;
8712 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8713 && IT_CHARPOS (*it) > to_charpos)
8714 goto buffer_pos_reached;
8715 else
8716 result = MOVE_NEWLINE_OR_CR;
8718 else
8719 result = MOVE_NEWLINE_OR_CR;
8720 break;
8723 prev_method = it->method;
8724 if (it->method == GET_FROM_BUFFER)
8725 prev_pos = IT_CHARPOS (*it);
8726 /* The current display element has been consumed. Advance
8727 to the next. */
8728 set_iterator_to_next (it, 1);
8729 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8730 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8731 if (IT_CHARPOS (*it) < to_charpos)
8732 saw_smaller_pos = 1;
8733 if (it->bidi_p
8734 && (op & MOVE_TO_POS)
8735 && IT_CHARPOS (*it) >= to_charpos
8736 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8737 SAVE_IT (ppos_it, *it, ppos_data);
8739 /* Stop if lines are truncated and IT's current x-position is
8740 past the right edge of the window now. */
8741 if (it->line_wrap == TRUNCATE
8742 && it->current_x >= it->last_visible_x)
8744 if (!FRAME_WINDOW_P (it->f)
8745 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8746 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8747 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8748 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8750 int at_eob_p = 0;
8752 if ((at_eob_p = !get_next_display_element (it))
8753 || BUFFER_POS_REACHED_P ()
8754 /* If we are past TO_CHARPOS, but never saw any
8755 character positions smaller than TO_CHARPOS,
8756 return MOVE_POS_MATCH_OR_ZV, like the
8757 unidirectional display did. */
8758 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8759 && !saw_smaller_pos
8760 && IT_CHARPOS (*it) > to_charpos))
8762 if (it->bidi_p
8763 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8764 RESTORE_IT (it, &ppos_it, ppos_data);
8765 result = MOVE_POS_MATCH_OR_ZV;
8766 break;
8768 if (ITERATOR_AT_END_OF_LINE_P (it))
8770 result = MOVE_NEWLINE_OR_CR;
8771 break;
8774 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8775 && !saw_smaller_pos
8776 && IT_CHARPOS (*it) > to_charpos)
8778 if (IT_CHARPOS (ppos_it) < ZV)
8779 RESTORE_IT (it, &ppos_it, ppos_data);
8780 result = MOVE_POS_MATCH_OR_ZV;
8781 break;
8783 result = MOVE_LINE_TRUNCATED;
8784 break;
8786 #undef IT_RESET_X_ASCENT_DESCENT
8789 #undef BUFFER_POS_REACHED_P
8791 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8792 restore the saved iterator. */
8793 if (atpos_it.sp >= 0)
8794 RESTORE_IT (it, &atpos_it, atpos_data);
8795 else if (atx_it.sp >= 0)
8796 RESTORE_IT (it, &atx_it, atx_data);
8798 done:
8800 if (atpos_data)
8801 bidi_unshelve_cache (atpos_data, 1);
8802 if (atx_data)
8803 bidi_unshelve_cache (atx_data, 1);
8804 if (wrap_data)
8805 bidi_unshelve_cache (wrap_data, 1);
8806 if (ppos_data)
8807 bidi_unshelve_cache (ppos_data, 1);
8809 /* Restore the iterator settings altered at the beginning of this
8810 function. */
8811 it->glyph_row = saved_glyph_row;
8812 return result;
8815 /* For external use. */
8816 void
8817 move_it_in_display_line (struct it *it,
8818 ptrdiff_t to_charpos, int to_x,
8819 enum move_operation_enum op)
8821 if (it->line_wrap == WORD_WRAP
8822 && (op & MOVE_TO_X))
8824 struct it save_it;
8825 void *save_data = NULL;
8826 int skip;
8828 SAVE_IT (save_it, *it, save_data);
8829 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8830 /* When word-wrap is on, TO_X may lie past the end
8831 of a wrapped line. Then it->current is the
8832 character on the next line, so backtrack to the
8833 space before the wrap point. */
8834 if (skip == MOVE_LINE_CONTINUED)
8836 int prev_x = max (it->current_x - 1, 0);
8837 RESTORE_IT (it, &save_it, save_data);
8838 move_it_in_display_line_to
8839 (it, -1, prev_x, MOVE_TO_X);
8841 else
8842 bidi_unshelve_cache (save_data, 1);
8844 else
8845 move_it_in_display_line_to (it, to_charpos, to_x, op);
8849 /* Move IT forward until it satisfies one or more of the criteria in
8850 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8852 OP is a bit-mask that specifies where to stop, and in particular,
8853 which of those four position arguments makes a difference. See the
8854 description of enum move_operation_enum.
8856 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8857 screen line, this function will set IT to the next position that is
8858 displayed to the right of TO_CHARPOS on the screen. */
8860 void
8861 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8863 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8864 int line_height, line_start_x = 0, reached = 0;
8865 void *backup_data = NULL;
8867 for (;;)
8869 if (op & MOVE_TO_VPOS)
8871 /* If no TO_CHARPOS and no TO_X specified, stop at the
8872 start of the line TO_VPOS. */
8873 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8875 if (it->vpos == to_vpos)
8877 reached = 1;
8878 break;
8880 else
8881 skip = move_it_in_display_line_to (it, -1, -1, 0);
8883 else
8885 /* TO_VPOS >= 0 means stop at TO_X in the line at
8886 TO_VPOS, or at TO_POS, whichever comes first. */
8887 if (it->vpos == to_vpos)
8889 reached = 2;
8890 break;
8893 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8895 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8897 reached = 3;
8898 break;
8900 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8902 /* We have reached TO_X but not in the line we want. */
8903 skip = move_it_in_display_line_to (it, to_charpos,
8904 -1, MOVE_TO_POS);
8905 if (skip == MOVE_POS_MATCH_OR_ZV)
8907 reached = 4;
8908 break;
8913 else if (op & MOVE_TO_Y)
8915 struct it it_backup;
8917 if (it->line_wrap == WORD_WRAP)
8918 SAVE_IT (it_backup, *it, backup_data);
8920 /* TO_Y specified means stop at TO_X in the line containing
8921 TO_Y---or at TO_CHARPOS if this is reached first. The
8922 problem is that we can't really tell whether the line
8923 contains TO_Y before we have completely scanned it, and
8924 this may skip past TO_X. What we do is to first scan to
8925 TO_X.
8927 If TO_X is not specified, use a TO_X of zero. The reason
8928 is to make the outcome of this function more predictable.
8929 If we didn't use TO_X == 0, we would stop at the end of
8930 the line which is probably not what a caller would expect
8931 to happen. */
8932 skip = move_it_in_display_line_to
8933 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8934 (MOVE_TO_X | (op & MOVE_TO_POS)));
8936 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8937 if (skip == MOVE_POS_MATCH_OR_ZV)
8938 reached = 5;
8939 else if (skip == MOVE_X_REACHED)
8941 /* If TO_X was reached, we want to know whether TO_Y is
8942 in the line. We know this is the case if the already
8943 scanned glyphs make the line tall enough. Otherwise,
8944 we must check by scanning the rest of the line. */
8945 line_height = it->max_ascent + it->max_descent;
8946 if (to_y >= it->current_y
8947 && to_y < it->current_y + line_height)
8949 reached = 6;
8950 break;
8952 SAVE_IT (it_backup, *it, backup_data);
8953 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8954 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8955 op & MOVE_TO_POS);
8956 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8957 line_height = it->max_ascent + it->max_descent;
8958 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8960 if (to_y >= it->current_y
8961 && to_y < it->current_y + line_height)
8963 /* If TO_Y is in this line and TO_X was reached
8964 above, we scanned too far. We have to restore
8965 IT's settings to the ones before skipping. But
8966 keep the more accurate values of max_ascent and
8967 max_descent we've found while skipping the rest
8968 of the line, for the sake of callers, such as
8969 pos_visible_p, that need to know the line
8970 height. */
8971 int max_ascent = it->max_ascent;
8972 int max_descent = it->max_descent;
8974 RESTORE_IT (it, &it_backup, backup_data);
8975 it->max_ascent = max_ascent;
8976 it->max_descent = max_descent;
8977 reached = 6;
8979 else
8981 skip = skip2;
8982 if (skip == MOVE_POS_MATCH_OR_ZV)
8983 reached = 7;
8986 else
8988 /* Check whether TO_Y is in this line. */
8989 line_height = it->max_ascent + it->max_descent;
8990 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8992 if (to_y >= it->current_y
8993 && to_y < it->current_y + line_height)
8995 /* When word-wrap is on, TO_X may lie past the end
8996 of a wrapped line. Then it->current is the
8997 character on the next line, so backtrack to the
8998 space before the wrap point. */
8999 if (skip == MOVE_LINE_CONTINUED
9000 && it->line_wrap == WORD_WRAP)
9002 int prev_x = max (it->current_x - 1, 0);
9003 RESTORE_IT (it, &it_backup, backup_data);
9004 skip = move_it_in_display_line_to
9005 (it, -1, prev_x, MOVE_TO_X);
9007 reached = 6;
9011 if (reached)
9012 break;
9014 else if (BUFFERP (it->object)
9015 && (it->method == GET_FROM_BUFFER
9016 || it->method == GET_FROM_STRETCH)
9017 && IT_CHARPOS (*it) >= to_charpos
9018 /* Under bidi iteration, a call to set_iterator_to_next
9019 can scan far beyond to_charpos if the initial
9020 portion of the next line needs to be reordered. In
9021 that case, give move_it_in_display_line_to another
9022 chance below. */
9023 && !(it->bidi_p
9024 && it->bidi_it.scan_dir == -1))
9025 skip = MOVE_POS_MATCH_OR_ZV;
9026 else
9027 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9029 switch (skip)
9031 case MOVE_POS_MATCH_OR_ZV:
9032 reached = 8;
9033 goto out;
9035 case MOVE_NEWLINE_OR_CR:
9036 set_iterator_to_next (it, 1);
9037 it->continuation_lines_width = 0;
9038 break;
9040 case MOVE_LINE_TRUNCATED:
9041 it->continuation_lines_width = 0;
9042 reseat_at_next_visible_line_start (it, 0);
9043 if ((op & MOVE_TO_POS) != 0
9044 && IT_CHARPOS (*it) > to_charpos)
9046 reached = 9;
9047 goto out;
9049 break;
9051 case MOVE_LINE_CONTINUED:
9052 /* For continued lines ending in a tab, some of the glyphs
9053 associated with the tab are displayed on the current
9054 line. Since it->current_x does not include these glyphs,
9055 we use it->last_visible_x instead. */
9056 if (it->c == '\t')
9058 it->continuation_lines_width += it->last_visible_x;
9059 /* When moving by vpos, ensure that the iterator really
9060 advances to the next line (bug#847, bug#969). Fixme:
9061 do we need to do this in other circumstances? */
9062 if (it->current_x != it->last_visible_x
9063 && (op & MOVE_TO_VPOS)
9064 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9066 line_start_x = it->current_x + it->pixel_width
9067 - it->last_visible_x;
9068 set_iterator_to_next (it, 0);
9071 else
9072 it->continuation_lines_width += it->current_x;
9073 break;
9075 default:
9076 emacs_abort ();
9079 /* Reset/increment for the next run. */
9080 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9081 it->current_x = line_start_x;
9082 line_start_x = 0;
9083 it->hpos = 0;
9084 it->current_y += it->max_ascent + it->max_descent;
9085 ++it->vpos;
9086 last_height = it->max_ascent + it->max_descent;
9087 it->max_ascent = it->max_descent = 0;
9090 out:
9092 /* On text terminals, we may stop at the end of a line in the middle
9093 of a multi-character glyph. If the glyph itself is continued,
9094 i.e. it is actually displayed on the next line, don't treat this
9095 stopping point as valid; move to the next line instead (unless
9096 that brings us offscreen). */
9097 if (!FRAME_WINDOW_P (it->f)
9098 && op & MOVE_TO_POS
9099 && IT_CHARPOS (*it) == to_charpos
9100 && it->what == IT_CHARACTER
9101 && it->nglyphs > 1
9102 && it->line_wrap == WINDOW_WRAP
9103 && it->current_x == it->last_visible_x - 1
9104 && it->c != '\n'
9105 && it->c != '\t'
9106 && it->vpos < it->w->window_end_vpos)
9108 it->continuation_lines_width += it->current_x;
9109 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9110 it->current_y += it->max_ascent + it->max_descent;
9111 ++it->vpos;
9112 last_height = it->max_ascent + it->max_descent;
9115 if (backup_data)
9116 bidi_unshelve_cache (backup_data, 1);
9118 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9122 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9124 If DY > 0, move IT backward at least that many pixels. DY = 0
9125 means move IT backward to the preceding line start or BEGV. This
9126 function may move over more than DY pixels if IT->current_y - DY
9127 ends up in the middle of a line; in this case IT->current_y will be
9128 set to the top of the line moved to. */
9130 void
9131 move_it_vertically_backward (struct it *it, int dy)
9133 int nlines, h;
9134 struct it it2, it3;
9135 void *it2data = NULL, *it3data = NULL;
9136 ptrdiff_t start_pos;
9137 int nchars_per_row
9138 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9139 ptrdiff_t pos_limit;
9141 move_further_back:
9142 eassert (dy >= 0);
9144 start_pos = IT_CHARPOS (*it);
9146 /* Estimate how many newlines we must move back. */
9147 nlines = max (1, dy / default_line_pixel_height (it->w));
9148 if (it->line_wrap == TRUNCATE)
9149 pos_limit = BEGV;
9150 else
9151 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9153 /* Set the iterator's position that many lines back. But don't go
9154 back more than NLINES full screen lines -- this wins a day with
9155 buffers which have very long lines. */
9156 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9157 back_to_previous_visible_line_start (it);
9159 /* Reseat the iterator here. When moving backward, we don't want
9160 reseat to skip forward over invisible text, set up the iterator
9161 to deliver from overlay strings at the new position etc. So,
9162 use reseat_1 here. */
9163 reseat_1 (it, it->current.pos, 1);
9165 /* We are now surely at a line start. */
9166 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9167 reordering is in effect. */
9168 it->continuation_lines_width = 0;
9170 /* Move forward and see what y-distance we moved. First move to the
9171 start of the next line so that we get its height. We need this
9172 height to be able to tell whether we reached the specified
9173 y-distance. */
9174 SAVE_IT (it2, *it, it2data);
9175 it2.max_ascent = it2.max_descent = 0;
9178 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9179 MOVE_TO_POS | MOVE_TO_VPOS);
9181 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9182 /* If we are in a display string which starts at START_POS,
9183 and that display string includes a newline, and we are
9184 right after that newline (i.e. at the beginning of a
9185 display line), exit the loop, because otherwise we will
9186 infloop, since move_it_to will see that it is already at
9187 START_POS and will not move. */
9188 || (it2.method == GET_FROM_STRING
9189 && IT_CHARPOS (it2) == start_pos
9190 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9191 eassert (IT_CHARPOS (*it) >= BEGV);
9192 SAVE_IT (it3, it2, it3data);
9194 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9195 eassert (IT_CHARPOS (*it) >= BEGV);
9196 /* H is the actual vertical distance from the position in *IT
9197 and the starting position. */
9198 h = it2.current_y - it->current_y;
9199 /* NLINES is the distance in number of lines. */
9200 nlines = it2.vpos - it->vpos;
9202 /* Correct IT's y and vpos position
9203 so that they are relative to the starting point. */
9204 it->vpos -= nlines;
9205 it->current_y -= h;
9207 if (dy == 0)
9209 /* DY == 0 means move to the start of the screen line. The
9210 value of nlines is > 0 if continuation lines were involved,
9211 or if the original IT position was at start of a line. */
9212 RESTORE_IT (it, it, it2data);
9213 if (nlines > 0)
9214 move_it_by_lines (it, nlines);
9215 /* The above code moves us to some position NLINES down,
9216 usually to its first glyph (leftmost in an L2R line), but
9217 that's not necessarily the start of the line, under bidi
9218 reordering. We want to get to the character position
9219 that is immediately after the newline of the previous
9220 line. */
9221 if (it->bidi_p
9222 && !it->continuation_lines_width
9223 && !STRINGP (it->string)
9224 && IT_CHARPOS (*it) > BEGV
9225 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9227 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9229 DEC_BOTH (cp, bp);
9230 cp = find_newline_no_quit (cp, bp, -1, NULL);
9231 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9233 bidi_unshelve_cache (it3data, 1);
9235 else
9237 /* The y-position we try to reach, relative to *IT.
9238 Note that H has been subtracted in front of the if-statement. */
9239 int target_y = it->current_y + h - dy;
9240 int y0 = it3.current_y;
9241 int y1;
9242 int line_height;
9244 RESTORE_IT (&it3, &it3, it3data);
9245 y1 = line_bottom_y (&it3);
9246 line_height = y1 - y0;
9247 RESTORE_IT (it, it, it2data);
9248 /* If we did not reach target_y, try to move further backward if
9249 we can. If we moved too far backward, try to move forward. */
9250 if (target_y < it->current_y
9251 /* This is heuristic. In a window that's 3 lines high, with
9252 a line height of 13 pixels each, recentering with point
9253 on the bottom line will try to move -39/2 = 19 pixels
9254 backward. Try to avoid moving into the first line. */
9255 && (it->current_y - target_y
9256 > min (window_box_height (it->w), line_height * 2 / 3))
9257 && IT_CHARPOS (*it) > BEGV)
9259 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9260 target_y - it->current_y));
9261 dy = it->current_y - target_y;
9262 goto move_further_back;
9264 else if (target_y >= it->current_y + line_height
9265 && IT_CHARPOS (*it) < ZV)
9267 /* Should move forward by at least one line, maybe more.
9269 Note: Calling move_it_by_lines can be expensive on
9270 terminal frames, where compute_motion is used (via
9271 vmotion) to do the job, when there are very long lines
9272 and truncate-lines is nil. That's the reason for
9273 treating terminal frames specially here. */
9275 if (!FRAME_WINDOW_P (it->f))
9276 move_it_vertically (it, target_y - (it->current_y + line_height));
9277 else
9281 move_it_by_lines (it, 1);
9283 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9290 /* Move IT by a specified amount of pixel lines DY. DY negative means
9291 move backwards. DY = 0 means move to start of screen line. At the
9292 end, IT will be on the start of a screen line. */
9294 void
9295 move_it_vertically (struct it *it, int dy)
9297 if (dy <= 0)
9298 move_it_vertically_backward (it, -dy);
9299 else
9301 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9302 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9303 MOVE_TO_POS | MOVE_TO_Y);
9304 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9306 /* If buffer ends in ZV without a newline, move to the start of
9307 the line to satisfy the post-condition. */
9308 if (IT_CHARPOS (*it) == ZV
9309 && ZV > BEGV
9310 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9311 move_it_by_lines (it, 0);
9316 /* Move iterator IT past the end of the text line it is in. */
9318 void
9319 move_it_past_eol (struct it *it)
9321 enum move_it_result rc;
9323 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9324 if (rc == MOVE_NEWLINE_OR_CR)
9325 set_iterator_to_next (it, 0);
9329 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9330 negative means move up. DVPOS == 0 means move to the start of the
9331 screen line.
9333 Optimization idea: If we would know that IT->f doesn't use
9334 a face with proportional font, we could be faster for
9335 truncate-lines nil. */
9337 void
9338 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9341 /* The commented-out optimization uses vmotion on terminals. This
9342 gives bad results, because elements like it->what, on which
9343 callers such as pos_visible_p rely, aren't updated. */
9344 /* struct position pos;
9345 if (!FRAME_WINDOW_P (it->f))
9347 struct text_pos textpos;
9349 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9350 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9351 reseat (it, textpos, 1);
9352 it->vpos += pos.vpos;
9353 it->current_y += pos.vpos;
9355 else */
9357 if (dvpos == 0)
9359 /* DVPOS == 0 means move to the start of the screen line. */
9360 move_it_vertically_backward (it, 0);
9361 /* Let next call to line_bottom_y calculate real line height */
9362 last_height = 0;
9364 else if (dvpos > 0)
9366 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9367 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9369 /* Only move to the next buffer position if we ended up in a
9370 string from display property, not in an overlay string
9371 (before-string or after-string). That is because the
9372 latter don't conceal the underlying buffer position, so
9373 we can ask to move the iterator to the exact position we
9374 are interested in. Note that, even if we are already at
9375 IT_CHARPOS (*it), the call below is not a no-op, as it
9376 will detect that we are at the end of the string, pop the
9377 iterator, and compute it->current_x and it->hpos
9378 correctly. */
9379 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9380 -1, -1, -1, MOVE_TO_POS);
9383 else
9385 struct it it2;
9386 void *it2data = NULL;
9387 ptrdiff_t start_charpos, i;
9388 int nchars_per_row
9389 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9390 ptrdiff_t pos_limit;
9392 /* Start at the beginning of the screen line containing IT's
9393 position. This may actually move vertically backwards,
9394 in case of overlays, so adjust dvpos accordingly. */
9395 dvpos += it->vpos;
9396 move_it_vertically_backward (it, 0);
9397 dvpos -= it->vpos;
9399 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9400 screen lines, and reseat the iterator there. */
9401 start_charpos = IT_CHARPOS (*it);
9402 if (it->line_wrap == TRUNCATE)
9403 pos_limit = BEGV;
9404 else
9405 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9406 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9407 back_to_previous_visible_line_start (it);
9408 reseat (it, it->current.pos, 1);
9410 /* Move further back if we end up in a string or an image. */
9411 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9413 /* First try to move to start of display line. */
9414 dvpos += it->vpos;
9415 move_it_vertically_backward (it, 0);
9416 dvpos -= it->vpos;
9417 if (IT_POS_VALID_AFTER_MOVE_P (it))
9418 break;
9419 /* If start of line is still in string or image,
9420 move further back. */
9421 back_to_previous_visible_line_start (it);
9422 reseat (it, it->current.pos, 1);
9423 dvpos--;
9426 it->current_x = it->hpos = 0;
9428 /* Above call may have moved too far if continuation lines
9429 are involved. Scan forward and see if it did. */
9430 SAVE_IT (it2, *it, it2data);
9431 it2.vpos = it2.current_y = 0;
9432 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9433 it->vpos -= it2.vpos;
9434 it->current_y -= it2.current_y;
9435 it->current_x = it->hpos = 0;
9437 /* If we moved too far back, move IT some lines forward. */
9438 if (it2.vpos > -dvpos)
9440 int delta = it2.vpos + dvpos;
9442 RESTORE_IT (&it2, &it2, it2data);
9443 SAVE_IT (it2, *it, it2data);
9444 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9445 /* Move back again if we got too far ahead. */
9446 if (IT_CHARPOS (*it) >= start_charpos)
9447 RESTORE_IT (it, &it2, it2data);
9448 else
9449 bidi_unshelve_cache (it2data, 1);
9451 else
9452 RESTORE_IT (it, it, it2data);
9456 /* Return 1 if IT points into the middle of a display vector. */
9459 in_display_vector_p (struct it *it)
9461 return (it->method == GET_FROM_DISPLAY_VECTOR
9462 && it->current.dpvec_index > 0
9463 && it->dpvec + it->current.dpvec_index != it->dpend);
9467 /***********************************************************************
9468 Messages
9469 ***********************************************************************/
9472 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9473 to *Messages*. */
9475 void
9476 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9478 Lisp_Object args[3];
9479 Lisp_Object msg, fmt;
9480 char *buffer;
9481 ptrdiff_t len;
9482 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9483 USE_SAFE_ALLOCA;
9485 fmt = msg = Qnil;
9486 GCPRO4 (fmt, msg, arg1, arg2);
9488 args[0] = fmt = build_string (format);
9489 args[1] = arg1;
9490 args[2] = arg2;
9491 msg = Fformat (3, args);
9493 len = SBYTES (msg) + 1;
9494 buffer = SAFE_ALLOCA (len);
9495 memcpy (buffer, SDATA (msg), len);
9497 message_dolog (buffer, len - 1, 1, 0);
9498 SAFE_FREE ();
9500 UNGCPRO;
9504 /* Output a newline in the *Messages* buffer if "needs" one. */
9506 void
9507 message_log_maybe_newline (void)
9509 if (message_log_need_newline)
9510 message_dolog ("", 0, 1, 0);
9514 /* Add a string M of length NBYTES to the message log, optionally
9515 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9516 true, means interpret the contents of M as multibyte. This
9517 function calls low-level routines in order to bypass text property
9518 hooks, etc. which might not be safe to run.
9520 This may GC (insert may run before/after change hooks),
9521 so the buffer M must NOT point to a Lisp string. */
9523 void
9524 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9526 const unsigned char *msg = (const unsigned char *) m;
9528 if (!NILP (Vmemory_full))
9529 return;
9531 if (!NILP (Vmessage_log_max))
9533 struct buffer *oldbuf;
9534 Lisp_Object oldpoint, oldbegv, oldzv;
9535 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9536 ptrdiff_t point_at_end = 0;
9537 ptrdiff_t zv_at_end = 0;
9538 Lisp_Object old_deactivate_mark;
9539 bool shown;
9540 struct gcpro gcpro1;
9542 old_deactivate_mark = Vdeactivate_mark;
9543 oldbuf = current_buffer;
9544 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9545 bset_undo_list (current_buffer, Qt);
9547 oldpoint = message_dolog_marker1;
9548 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9549 oldbegv = message_dolog_marker2;
9550 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9551 oldzv = message_dolog_marker3;
9552 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9553 GCPRO1 (old_deactivate_mark);
9555 if (PT == Z)
9556 point_at_end = 1;
9557 if (ZV == Z)
9558 zv_at_end = 1;
9560 BEGV = BEG;
9561 BEGV_BYTE = BEG_BYTE;
9562 ZV = Z;
9563 ZV_BYTE = Z_BYTE;
9564 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9566 /* Insert the string--maybe converting multibyte to single byte
9567 or vice versa, so that all the text fits the buffer. */
9568 if (multibyte
9569 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9571 ptrdiff_t i;
9572 int c, char_bytes;
9573 char work[1];
9575 /* Convert a multibyte string to single-byte
9576 for the *Message* buffer. */
9577 for (i = 0; i < nbytes; i += char_bytes)
9579 c = string_char_and_length (msg + i, &char_bytes);
9580 work[0] = (ASCII_CHAR_P (c)
9582 : multibyte_char_to_unibyte (c));
9583 insert_1_both (work, 1, 1, 1, 0, 0);
9586 else if (! multibyte
9587 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9589 ptrdiff_t i;
9590 int c, char_bytes;
9591 unsigned char str[MAX_MULTIBYTE_LENGTH];
9592 /* Convert a single-byte string to multibyte
9593 for the *Message* buffer. */
9594 for (i = 0; i < nbytes; i++)
9596 c = msg[i];
9597 MAKE_CHAR_MULTIBYTE (c);
9598 char_bytes = CHAR_STRING (c, str);
9599 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9602 else if (nbytes)
9603 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9605 if (nlflag)
9607 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9608 printmax_t dups;
9610 insert_1_both ("\n", 1, 1, 1, 0, 0);
9612 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9613 this_bol = PT;
9614 this_bol_byte = PT_BYTE;
9616 /* See if this line duplicates the previous one.
9617 If so, combine duplicates. */
9618 if (this_bol > BEG)
9620 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9621 prev_bol = PT;
9622 prev_bol_byte = PT_BYTE;
9624 dups = message_log_check_duplicate (prev_bol_byte,
9625 this_bol_byte);
9626 if (dups)
9628 del_range_both (prev_bol, prev_bol_byte,
9629 this_bol, this_bol_byte, 0);
9630 if (dups > 1)
9632 char dupstr[sizeof " [ times]"
9633 + INT_STRLEN_BOUND (printmax_t)];
9635 /* If you change this format, don't forget to also
9636 change message_log_check_duplicate. */
9637 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9638 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9639 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9644 /* If we have more than the desired maximum number of lines
9645 in the *Messages* buffer now, delete the oldest ones.
9646 This is safe because we don't have undo in this buffer. */
9648 if (NATNUMP (Vmessage_log_max))
9650 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9651 -XFASTINT (Vmessage_log_max) - 1, 0);
9652 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9655 BEGV = marker_position (oldbegv);
9656 BEGV_BYTE = marker_byte_position (oldbegv);
9658 if (zv_at_end)
9660 ZV = Z;
9661 ZV_BYTE = Z_BYTE;
9663 else
9665 ZV = marker_position (oldzv);
9666 ZV_BYTE = marker_byte_position (oldzv);
9669 if (point_at_end)
9670 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9671 else
9672 /* We can't do Fgoto_char (oldpoint) because it will run some
9673 Lisp code. */
9674 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9675 marker_byte_position (oldpoint));
9677 UNGCPRO;
9678 unchain_marker (XMARKER (oldpoint));
9679 unchain_marker (XMARKER (oldbegv));
9680 unchain_marker (XMARKER (oldzv));
9682 shown = buffer_window_count (current_buffer) > 0;
9683 set_buffer_internal (oldbuf);
9684 /* We called insert_1_both above with its 5th argument (PREPARE)
9685 zero, which prevents insert_1_both from calling
9686 prepare_to_modify_buffer, which in turns prevents us from
9687 incrementing windows_or_buffers_changed even if *Messages* is
9688 shown in some window. So we must manually incrementing
9689 windows_or_buffers_changed here to make up for that. */
9690 if (shown)
9691 windows_or_buffers_changed++;
9692 else
9693 windows_or_buffers_changed = old_windows_or_buffers_changed;
9694 message_log_need_newline = !nlflag;
9695 Vdeactivate_mark = old_deactivate_mark;
9700 /* We are at the end of the buffer after just having inserted a newline.
9701 (Note: We depend on the fact we won't be crossing the gap.)
9702 Check to see if the most recent message looks a lot like the previous one.
9703 Return 0 if different, 1 if the new one should just replace it, or a
9704 value N > 1 if we should also append " [N times]". */
9706 static intmax_t
9707 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9709 ptrdiff_t i;
9710 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9711 int seen_dots = 0;
9712 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9713 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9715 for (i = 0; i < len; i++)
9717 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9718 seen_dots = 1;
9719 if (p1[i] != p2[i])
9720 return seen_dots;
9722 p1 += len;
9723 if (*p1 == '\n')
9724 return 2;
9725 if (*p1++ == ' ' && *p1++ == '[')
9727 char *pend;
9728 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9729 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9730 return n + 1;
9732 return 0;
9736 /* Display an echo area message M with a specified length of NBYTES
9737 bytes. The string may include null characters. If M is not a
9738 string, clear out any existing message, and let the mini-buffer
9739 text show through.
9741 This function cancels echoing. */
9743 void
9744 message3 (Lisp_Object m)
9746 struct gcpro gcpro1;
9748 GCPRO1 (m);
9749 clear_message (1,1);
9750 cancel_echoing ();
9752 /* First flush out any partial line written with print. */
9753 message_log_maybe_newline ();
9754 if (STRINGP (m))
9756 ptrdiff_t nbytes = SBYTES (m);
9757 bool multibyte = STRING_MULTIBYTE (m);
9758 USE_SAFE_ALLOCA;
9759 char *buffer = SAFE_ALLOCA (nbytes);
9760 memcpy (buffer, SDATA (m), nbytes);
9761 message_dolog (buffer, nbytes, 1, multibyte);
9762 SAFE_FREE ();
9764 message3_nolog (m);
9766 UNGCPRO;
9770 /* The non-logging version of message3.
9771 This does not cancel echoing, because it is used for echoing.
9772 Perhaps we need to make a separate function for echoing
9773 and make this cancel echoing. */
9775 void
9776 message3_nolog (Lisp_Object m)
9778 struct frame *sf = SELECTED_FRAME ();
9780 if (FRAME_INITIAL_P (sf))
9782 if (noninteractive_need_newline)
9783 putc ('\n', stderr);
9784 noninteractive_need_newline = 0;
9785 if (STRINGP (m))
9786 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9787 if (cursor_in_echo_area == 0)
9788 fprintf (stderr, "\n");
9789 fflush (stderr);
9791 /* Error messages get reported properly by cmd_error, so this must be just an
9792 informative message; if the frame hasn't really been initialized yet, just
9793 toss it. */
9794 else if (INTERACTIVE && sf->glyphs_initialized_p)
9796 /* Get the frame containing the mini-buffer
9797 that the selected frame is using. */
9798 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9799 Lisp_Object frame = XWINDOW (mini_window)->frame;
9800 struct frame *f = XFRAME (frame);
9802 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9803 Fmake_frame_visible (frame);
9805 if (STRINGP (m) && SCHARS (m) > 0)
9807 set_message (m);
9808 if (minibuffer_auto_raise)
9809 Fraise_frame (frame);
9810 /* Assume we are not echoing.
9811 (If we are, echo_now will override this.) */
9812 echo_message_buffer = Qnil;
9814 else
9815 clear_message (1, 1);
9817 do_pending_window_change (0);
9818 echo_area_display (1);
9819 do_pending_window_change (0);
9820 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9821 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9826 /* Display a null-terminated echo area message M. If M is 0, clear
9827 out any existing message, and let the mini-buffer text show through.
9829 The buffer M must continue to exist until after the echo area gets
9830 cleared or some other message gets displayed there. Do not pass
9831 text that is stored in a Lisp string. Do not pass text in a buffer
9832 that was alloca'd. */
9834 void
9835 message1 (const char *m)
9837 message3 (m ? build_unibyte_string (m) : Qnil);
9841 /* The non-logging counterpart of message1. */
9843 void
9844 message1_nolog (const char *m)
9846 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9849 /* Display a message M which contains a single %s
9850 which gets replaced with STRING. */
9852 void
9853 message_with_string (const char *m, Lisp_Object string, int log)
9855 CHECK_STRING (string);
9857 if (noninteractive)
9859 if (m)
9861 if (noninteractive_need_newline)
9862 putc ('\n', stderr);
9863 noninteractive_need_newline = 0;
9864 fprintf (stderr, m, SDATA (string));
9865 if (!cursor_in_echo_area)
9866 fprintf (stderr, "\n");
9867 fflush (stderr);
9870 else if (INTERACTIVE)
9872 /* The frame whose minibuffer we're going to display the message on.
9873 It may be larger than the selected frame, so we need
9874 to use its buffer, not the selected frame's buffer. */
9875 Lisp_Object mini_window;
9876 struct frame *f, *sf = SELECTED_FRAME ();
9878 /* Get the frame containing the minibuffer
9879 that the selected frame is using. */
9880 mini_window = FRAME_MINIBUF_WINDOW (sf);
9881 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9883 /* Error messages get reported properly by cmd_error, so this must be
9884 just an informative message; if the frame hasn't really been
9885 initialized yet, just toss it. */
9886 if (f->glyphs_initialized_p)
9888 Lisp_Object args[2], msg;
9889 struct gcpro gcpro1, gcpro2;
9891 args[0] = build_string (m);
9892 args[1] = msg = string;
9893 GCPRO2 (args[0], msg);
9894 gcpro1.nvars = 2;
9896 msg = Fformat (2, args);
9898 if (log)
9899 message3 (msg);
9900 else
9901 message3_nolog (msg);
9903 UNGCPRO;
9905 /* Print should start at the beginning of the message
9906 buffer next time. */
9907 message_buf_print = 0;
9913 /* Dump an informative message to the minibuf. If M is 0, clear out
9914 any existing message, and let the mini-buffer text show through. */
9916 static void
9917 vmessage (const char *m, va_list ap)
9919 if (noninteractive)
9921 if (m)
9923 if (noninteractive_need_newline)
9924 putc ('\n', stderr);
9925 noninteractive_need_newline = 0;
9926 vfprintf (stderr, m, ap);
9927 if (cursor_in_echo_area == 0)
9928 fprintf (stderr, "\n");
9929 fflush (stderr);
9932 else if (INTERACTIVE)
9934 /* The frame whose mini-buffer we're going to display the message
9935 on. It may be larger than the selected frame, so we need to
9936 use its buffer, not the selected frame's buffer. */
9937 Lisp_Object mini_window;
9938 struct frame *f, *sf = SELECTED_FRAME ();
9940 /* Get the frame containing the mini-buffer
9941 that the selected frame is using. */
9942 mini_window = FRAME_MINIBUF_WINDOW (sf);
9943 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9945 /* Error messages get reported properly by cmd_error, so this must be
9946 just an informative message; if the frame hasn't really been
9947 initialized yet, just toss it. */
9948 if (f->glyphs_initialized_p)
9950 if (m)
9952 ptrdiff_t len;
9953 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9954 char *message_buf = alloca (maxsize + 1);
9956 len = doprnt (message_buf, maxsize, m, 0, ap);
9958 message3 (make_string (message_buf, len));
9960 else
9961 message1 (0);
9963 /* Print should start at the beginning of the message
9964 buffer next time. */
9965 message_buf_print = 0;
9970 void
9971 message (const char *m, ...)
9973 va_list ap;
9974 va_start (ap, m);
9975 vmessage (m, ap);
9976 va_end (ap);
9980 #if 0
9981 /* The non-logging version of message. */
9983 void
9984 message_nolog (const char *m, ...)
9986 Lisp_Object old_log_max;
9987 va_list ap;
9988 va_start (ap, m);
9989 old_log_max = Vmessage_log_max;
9990 Vmessage_log_max = Qnil;
9991 vmessage (m, ap);
9992 Vmessage_log_max = old_log_max;
9993 va_end (ap);
9995 #endif
9998 /* Display the current message in the current mini-buffer. This is
9999 only called from error handlers in process.c, and is not time
10000 critical. */
10002 void
10003 update_echo_area (void)
10005 if (!NILP (echo_area_buffer[0]))
10007 Lisp_Object string;
10008 string = Fcurrent_message ();
10009 message3 (string);
10014 /* Make sure echo area buffers in `echo_buffers' are live.
10015 If they aren't, make new ones. */
10017 static void
10018 ensure_echo_area_buffers (void)
10020 int i;
10022 for (i = 0; i < 2; ++i)
10023 if (!BUFFERP (echo_buffer[i])
10024 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10026 char name[30];
10027 Lisp_Object old_buffer;
10028 int j;
10030 old_buffer = echo_buffer[i];
10031 echo_buffer[i] = Fget_buffer_create
10032 (make_formatted_string (name, " *Echo Area %d*", i));
10033 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10034 /* to force word wrap in echo area -
10035 it was decided to postpone this*/
10036 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10038 for (j = 0; j < 2; ++j)
10039 if (EQ (old_buffer, echo_area_buffer[j]))
10040 echo_area_buffer[j] = echo_buffer[i];
10045 /* Call FN with args A1..A2 with either the current or last displayed
10046 echo_area_buffer as current buffer.
10048 WHICH zero means use the current message buffer
10049 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10050 from echo_buffer[] and clear it.
10052 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10053 suitable buffer from echo_buffer[] and clear it.
10055 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10056 that the current message becomes the last displayed one, make
10057 choose a suitable buffer for echo_area_buffer[0], and clear it.
10059 Value is what FN returns. */
10061 static int
10062 with_echo_area_buffer (struct window *w, int which,
10063 int (*fn) (ptrdiff_t, Lisp_Object),
10064 ptrdiff_t a1, Lisp_Object a2)
10066 Lisp_Object buffer;
10067 int this_one, the_other, clear_buffer_p, rc;
10068 ptrdiff_t count = SPECPDL_INDEX ();
10070 /* If buffers aren't live, make new ones. */
10071 ensure_echo_area_buffers ();
10073 clear_buffer_p = 0;
10075 if (which == 0)
10076 this_one = 0, the_other = 1;
10077 else if (which > 0)
10078 this_one = 1, the_other = 0;
10079 else
10081 this_one = 0, the_other = 1;
10082 clear_buffer_p = 1;
10084 /* We need a fresh one in case the current echo buffer equals
10085 the one containing the last displayed echo area message. */
10086 if (!NILP (echo_area_buffer[this_one])
10087 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10088 echo_area_buffer[this_one] = Qnil;
10091 /* Choose a suitable buffer from echo_buffer[] is we don't
10092 have one. */
10093 if (NILP (echo_area_buffer[this_one]))
10095 echo_area_buffer[this_one]
10096 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10097 ? echo_buffer[the_other]
10098 : echo_buffer[this_one]);
10099 clear_buffer_p = 1;
10102 buffer = echo_area_buffer[this_one];
10104 /* Don't get confused by reusing the buffer used for echoing
10105 for a different purpose. */
10106 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10107 cancel_echoing ();
10109 record_unwind_protect (unwind_with_echo_area_buffer,
10110 with_echo_area_buffer_unwind_data (w));
10112 /* Make the echo area buffer current. Note that for display
10113 purposes, it is not necessary that the displayed window's buffer
10114 == current_buffer, except for text property lookup. So, let's
10115 only set that buffer temporarily here without doing a full
10116 Fset_window_buffer. We must also change w->pointm, though,
10117 because otherwise an assertions in unshow_buffer fails, and Emacs
10118 aborts. */
10119 set_buffer_internal_1 (XBUFFER (buffer));
10120 if (w)
10122 wset_buffer (w, buffer);
10123 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10126 bset_undo_list (current_buffer, Qt);
10127 bset_read_only (current_buffer, Qnil);
10128 specbind (Qinhibit_read_only, Qt);
10129 specbind (Qinhibit_modification_hooks, Qt);
10131 if (clear_buffer_p && Z > BEG)
10132 del_range (BEG, Z);
10134 eassert (BEGV >= BEG);
10135 eassert (ZV <= Z && ZV >= BEGV);
10137 rc = fn (a1, a2);
10139 eassert (BEGV >= BEG);
10140 eassert (ZV <= Z && ZV >= BEGV);
10142 unbind_to (count, Qnil);
10143 return rc;
10147 /* Save state that should be preserved around the call to the function
10148 FN called in with_echo_area_buffer. */
10150 static Lisp_Object
10151 with_echo_area_buffer_unwind_data (struct window *w)
10153 int i = 0;
10154 Lisp_Object vector, tmp;
10156 /* Reduce consing by keeping one vector in
10157 Vwith_echo_area_save_vector. */
10158 vector = Vwith_echo_area_save_vector;
10159 Vwith_echo_area_save_vector = Qnil;
10161 if (NILP (vector))
10162 vector = Fmake_vector (make_number (9), Qnil);
10164 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10165 ASET (vector, i, Vdeactivate_mark); ++i;
10166 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10168 if (w)
10170 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10171 ASET (vector, i, w->contents); ++i;
10172 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10173 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10174 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10175 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10177 else
10179 int end = i + 6;
10180 for (; i < end; ++i)
10181 ASET (vector, i, Qnil);
10184 eassert (i == ASIZE (vector));
10185 return vector;
10189 /* Restore global state from VECTOR which was created by
10190 with_echo_area_buffer_unwind_data. */
10192 static void
10193 unwind_with_echo_area_buffer (Lisp_Object vector)
10195 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10196 Vdeactivate_mark = AREF (vector, 1);
10197 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10199 if (WINDOWP (AREF (vector, 3)))
10201 struct window *w;
10202 Lisp_Object buffer;
10204 w = XWINDOW (AREF (vector, 3));
10205 buffer = AREF (vector, 4);
10207 wset_buffer (w, buffer);
10208 set_marker_both (w->pointm, buffer,
10209 XFASTINT (AREF (vector, 5)),
10210 XFASTINT (AREF (vector, 6)));
10211 set_marker_both (w->start, buffer,
10212 XFASTINT (AREF (vector, 7)),
10213 XFASTINT (AREF (vector, 8)));
10216 Vwith_echo_area_save_vector = vector;
10220 /* Set up the echo area for use by print functions. MULTIBYTE_P
10221 non-zero means we will print multibyte. */
10223 void
10224 setup_echo_area_for_printing (int multibyte_p)
10226 /* If we can't find an echo area any more, exit. */
10227 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10228 Fkill_emacs (Qnil);
10230 ensure_echo_area_buffers ();
10232 if (!message_buf_print)
10234 /* A message has been output since the last time we printed.
10235 Choose a fresh echo area buffer. */
10236 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10237 echo_area_buffer[0] = echo_buffer[1];
10238 else
10239 echo_area_buffer[0] = echo_buffer[0];
10241 /* Switch to that buffer and clear it. */
10242 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10243 bset_truncate_lines (current_buffer, Qnil);
10245 if (Z > BEG)
10247 ptrdiff_t count = SPECPDL_INDEX ();
10248 specbind (Qinhibit_read_only, Qt);
10249 /* Note that undo recording is always disabled. */
10250 del_range (BEG, Z);
10251 unbind_to (count, Qnil);
10253 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10255 /* Set up the buffer for the multibyteness we need. */
10256 if (multibyte_p
10257 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10258 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10260 /* Raise the frame containing the echo area. */
10261 if (minibuffer_auto_raise)
10263 struct frame *sf = SELECTED_FRAME ();
10264 Lisp_Object mini_window;
10265 mini_window = FRAME_MINIBUF_WINDOW (sf);
10266 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10269 message_log_maybe_newline ();
10270 message_buf_print = 1;
10272 else
10274 if (NILP (echo_area_buffer[0]))
10276 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10277 echo_area_buffer[0] = echo_buffer[1];
10278 else
10279 echo_area_buffer[0] = echo_buffer[0];
10282 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10284 /* Someone switched buffers between print requests. */
10285 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10286 bset_truncate_lines (current_buffer, Qnil);
10292 /* Display an echo area message in window W. Value is non-zero if W's
10293 height is changed. If display_last_displayed_message_p is
10294 non-zero, display the message that was last displayed, otherwise
10295 display the current message. */
10297 static int
10298 display_echo_area (struct window *w)
10300 int i, no_message_p, window_height_changed_p;
10302 /* Temporarily disable garbage collections while displaying the echo
10303 area. This is done because a GC can print a message itself.
10304 That message would modify the echo area buffer's contents while a
10305 redisplay of the buffer is going on, and seriously confuse
10306 redisplay. */
10307 ptrdiff_t count = inhibit_garbage_collection ();
10309 /* If there is no message, we must call display_echo_area_1
10310 nevertheless because it resizes the window. But we will have to
10311 reset the echo_area_buffer in question to nil at the end because
10312 with_echo_area_buffer will sets it to an empty buffer. */
10313 i = display_last_displayed_message_p ? 1 : 0;
10314 no_message_p = NILP (echo_area_buffer[i]);
10316 window_height_changed_p
10317 = with_echo_area_buffer (w, display_last_displayed_message_p,
10318 display_echo_area_1,
10319 (intptr_t) w, Qnil);
10321 if (no_message_p)
10322 echo_area_buffer[i] = Qnil;
10324 unbind_to (count, Qnil);
10325 return window_height_changed_p;
10329 /* Helper for display_echo_area. Display the current buffer which
10330 contains the current echo area message in window W, a mini-window,
10331 a pointer to which is passed in A1. A2..A4 are currently not used.
10332 Change the height of W so that all of the message is displayed.
10333 Value is non-zero if height of W was changed. */
10335 static int
10336 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10338 intptr_t i1 = a1;
10339 struct window *w = (struct window *) i1;
10340 Lisp_Object window;
10341 struct text_pos start;
10342 int window_height_changed_p = 0;
10344 /* Do this before displaying, so that we have a large enough glyph
10345 matrix for the display. If we can't get enough space for the
10346 whole text, display the last N lines. That works by setting w->start. */
10347 window_height_changed_p = resize_mini_window (w, 0);
10349 /* Use the starting position chosen by resize_mini_window. */
10350 SET_TEXT_POS_FROM_MARKER (start, w->start);
10352 /* Display. */
10353 clear_glyph_matrix (w->desired_matrix);
10354 XSETWINDOW (window, w);
10355 try_window (window, start, 0);
10357 return window_height_changed_p;
10361 /* Resize the echo area window to exactly the size needed for the
10362 currently displayed message, if there is one. If a mini-buffer
10363 is active, don't shrink it. */
10365 void
10366 resize_echo_area_exactly (void)
10368 if (BUFFERP (echo_area_buffer[0])
10369 && WINDOWP (echo_area_window))
10371 struct window *w = XWINDOW (echo_area_window);
10372 int resized_p;
10373 Lisp_Object resize_exactly;
10375 if (minibuf_level == 0)
10376 resize_exactly = Qt;
10377 else
10378 resize_exactly = Qnil;
10380 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10381 (intptr_t) w, resize_exactly);
10382 if (resized_p)
10384 ++windows_or_buffers_changed;
10385 ++update_mode_lines;
10386 redisplay_internal ();
10392 /* Callback function for with_echo_area_buffer, when used from
10393 resize_echo_area_exactly. A1 contains a pointer to the window to
10394 resize, EXACTLY non-nil means resize the mini-window exactly to the
10395 size of the text displayed. A3 and A4 are not used. Value is what
10396 resize_mini_window returns. */
10398 static int
10399 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10401 intptr_t i1 = a1;
10402 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10406 /* Resize mini-window W to fit the size of its contents. EXACT_P
10407 means size the window exactly to the size needed. Otherwise, it's
10408 only enlarged until W's buffer is empty.
10410 Set W->start to the right place to begin display. If the whole
10411 contents fit, start at the beginning. Otherwise, start so as
10412 to make the end of the contents appear. This is particularly
10413 important for y-or-n-p, but seems desirable generally.
10415 Value is non-zero if the window height has been changed. */
10418 resize_mini_window (struct window *w, int exact_p)
10420 struct frame *f = XFRAME (w->frame);
10421 int window_height_changed_p = 0;
10423 eassert (MINI_WINDOW_P (w));
10425 /* By default, start display at the beginning. */
10426 set_marker_both (w->start, w->contents,
10427 BUF_BEGV (XBUFFER (w->contents)),
10428 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10430 /* Don't resize windows while redisplaying a window; it would
10431 confuse redisplay functions when the size of the window they are
10432 displaying changes from under them. Such a resizing can happen,
10433 for instance, when which-func prints a long message while
10434 we are running fontification-functions. We're running these
10435 functions with safe_call which binds inhibit-redisplay to t. */
10436 if (!NILP (Vinhibit_redisplay))
10437 return 0;
10439 /* Nil means don't try to resize. */
10440 if (NILP (Vresize_mini_windows)
10441 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10442 return 0;
10444 if (!FRAME_MINIBUF_ONLY_P (f))
10446 struct it it;
10447 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10448 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10449 int height;
10450 EMACS_INT max_height;
10451 int unit = FRAME_LINE_HEIGHT (f);
10452 struct text_pos start;
10453 struct buffer *old_current_buffer = NULL;
10455 if (current_buffer != XBUFFER (w->contents))
10457 old_current_buffer = current_buffer;
10458 set_buffer_internal (XBUFFER (w->contents));
10461 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10463 /* Compute the max. number of lines specified by the user. */
10464 if (FLOATP (Vmax_mini_window_height))
10465 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10466 else if (INTEGERP (Vmax_mini_window_height))
10467 max_height = XINT (Vmax_mini_window_height);
10468 else
10469 max_height = total_height / 4;
10471 /* Correct that max. height if it's bogus. */
10472 max_height = clip_to_bounds (1, max_height, total_height);
10474 /* Find out the height of the text in the window. */
10475 if (it.line_wrap == TRUNCATE)
10476 height = 1;
10477 else
10479 last_height = 0;
10480 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10481 if (it.max_ascent == 0 && it.max_descent == 0)
10482 height = it.current_y + last_height;
10483 else
10484 height = it.current_y + it.max_ascent + it.max_descent;
10485 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10486 height = (height + unit - 1) / unit;
10489 /* Compute a suitable window start. */
10490 if (height > max_height)
10492 height = max_height;
10493 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10494 move_it_vertically_backward (&it, (height - 1) * unit);
10495 start = it.current.pos;
10497 else
10498 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10499 SET_MARKER_FROM_TEXT_POS (w->start, start);
10501 if (EQ (Vresize_mini_windows, Qgrow_only))
10503 /* Let it grow only, until we display an empty message, in which
10504 case the window shrinks again. */
10505 if (height > WINDOW_TOTAL_LINES (w))
10507 int old_height = WINDOW_TOTAL_LINES (w);
10509 FRAME_WINDOWS_FROZEN (f) = 1;
10510 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10511 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10513 else if (height < WINDOW_TOTAL_LINES (w)
10514 && (exact_p || BEGV == ZV))
10516 int old_height = WINDOW_TOTAL_LINES (w);
10518 FRAME_WINDOWS_FROZEN (f) = 0;
10519 shrink_mini_window (w);
10520 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10523 else
10525 /* Always resize to exact size needed. */
10526 if (height > WINDOW_TOTAL_LINES (w))
10528 int old_height = WINDOW_TOTAL_LINES (w);
10530 FRAME_WINDOWS_FROZEN (f) = 1;
10531 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10532 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10534 else if (height < WINDOW_TOTAL_LINES (w))
10536 int old_height = WINDOW_TOTAL_LINES (w);
10538 FRAME_WINDOWS_FROZEN (f) = 0;
10539 shrink_mini_window (w);
10541 if (height)
10543 FRAME_WINDOWS_FROZEN (f) = 1;
10544 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10547 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10551 if (old_current_buffer)
10552 set_buffer_internal (old_current_buffer);
10555 return window_height_changed_p;
10559 /* Value is the current message, a string, or nil if there is no
10560 current message. */
10562 Lisp_Object
10563 current_message (void)
10565 Lisp_Object msg;
10567 if (!BUFFERP (echo_area_buffer[0]))
10568 msg = Qnil;
10569 else
10571 with_echo_area_buffer (0, 0, current_message_1,
10572 (intptr_t) &msg, Qnil);
10573 if (NILP (msg))
10574 echo_area_buffer[0] = Qnil;
10577 return msg;
10581 static int
10582 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10584 intptr_t i1 = a1;
10585 Lisp_Object *msg = (Lisp_Object *) i1;
10587 if (Z > BEG)
10588 *msg = make_buffer_string (BEG, Z, 1);
10589 else
10590 *msg = Qnil;
10591 return 0;
10595 /* Push the current message on Vmessage_stack for later restoration
10596 by restore_message. Value is non-zero if the current message isn't
10597 empty. This is a relatively infrequent operation, so it's not
10598 worth optimizing. */
10600 bool
10601 push_message (void)
10603 Lisp_Object msg = current_message ();
10604 Vmessage_stack = Fcons (msg, Vmessage_stack);
10605 return STRINGP (msg);
10609 /* Restore message display from the top of Vmessage_stack. */
10611 void
10612 restore_message (void)
10614 eassert (CONSP (Vmessage_stack));
10615 message3_nolog (XCAR (Vmessage_stack));
10619 /* Handler for unwind-protect calling pop_message. */
10621 void
10622 pop_message_unwind (void)
10624 /* Pop the top-most entry off Vmessage_stack. */
10625 eassert (CONSP (Vmessage_stack));
10626 Vmessage_stack = XCDR (Vmessage_stack);
10630 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10631 exits. If the stack is not empty, we have a missing pop_message
10632 somewhere. */
10634 void
10635 check_message_stack (void)
10637 if (!NILP (Vmessage_stack))
10638 emacs_abort ();
10642 /* Truncate to NCHARS what will be displayed in the echo area the next
10643 time we display it---but don't redisplay it now. */
10645 void
10646 truncate_echo_area (ptrdiff_t nchars)
10648 if (nchars == 0)
10649 echo_area_buffer[0] = Qnil;
10650 else if (!noninteractive
10651 && INTERACTIVE
10652 && !NILP (echo_area_buffer[0]))
10654 struct frame *sf = SELECTED_FRAME ();
10655 /* Error messages get reported properly by cmd_error, so this must be
10656 just an informative message; if the frame hasn't really been
10657 initialized yet, just toss it. */
10658 if (sf->glyphs_initialized_p)
10659 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10664 /* Helper function for truncate_echo_area. Truncate the current
10665 message to at most NCHARS characters. */
10667 static int
10668 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10670 if (BEG + nchars < Z)
10671 del_range (BEG + nchars, Z);
10672 if (Z == BEG)
10673 echo_area_buffer[0] = Qnil;
10674 return 0;
10677 /* Set the current message to STRING. */
10679 static void
10680 set_message (Lisp_Object string)
10682 eassert (STRINGP (string));
10684 message_enable_multibyte = STRING_MULTIBYTE (string);
10686 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10687 message_buf_print = 0;
10688 help_echo_showing_p = 0;
10690 if (STRINGP (Vdebug_on_message)
10691 && STRINGP (string)
10692 && fast_string_match (Vdebug_on_message, string) >= 0)
10693 call_debugger (list2 (Qerror, string));
10697 /* Helper function for set_message. First argument is ignored and second
10698 argument has the same meaning as for set_message.
10699 This function is called with the echo area buffer being current. */
10701 static int
10702 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10704 eassert (STRINGP (string));
10706 /* Change multibyteness of the echo buffer appropriately. */
10707 if (message_enable_multibyte
10708 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10709 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10711 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10712 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10713 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10715 /* Insert new message at BEG. */
10716 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10718 /* This function takes care of single/multibyte conversion.
10719 We just have to ensure that the echo area buffer has the right
10720 setting of enable_multibyte_characters. */
10721 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10723 return 0;
10727 /* Clear messages. CURRENT_P non-zero means clear the current
10728 message. LAST_DISPLAYED_P non-zero means clear the message
10729 last displayed. */
10731 void
10732 clear_message (int current_p, int last_displayed_p)
10734 if (current_p)
10736 echo_area_buffer[0] = Qnil;
10737 message_cleared_p = 1;
10740 if (last_displayed_p)
10741 echo_area_buffer[1] = Qnil;
10743 message_buf_print = 0;
10746 /* Clear garbaged frames.
10748 This function is used where the old redisplay called
10749 redraw_garbaged_frames which in turn called redraw_frame which in
10750 turn called clear_frame. The call to clear_frame was a source of
10751 flickering. I believe a clear_frame is not necessary. It should
10752 suffice in the new redisplay to invalidate all current matrices,
10753 and ensure a complete redisplay of all windows. */
10755 static void
10756 clear_garbaged_frames (void)
10758 if (frame_garbaged)
10760 Lisp_Object tail, frame;
10761 int changed_count = 0;
10763 FOR_EACH_FRAME (tail, frame)
10765 struct frame *f = XFRAME (frame);
10767 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10769 if (f->resized_p)
10771 redraw_frame (f);
10772 f->force_flush_display_p = 1;
10774 clear_current_matrices (f);
10775 changed_count++;
10776 f->garbaged = 0;
10777 f->resized_p = 0;
10781 frame_garbaged = 0;
10782 if (changed_count)
10783 ++windows_or_buffers_changed;
10788 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10789 is non-zero update selected_frame. Value is non-zero if the
10790 mini-windows height has been changed. */
10792 static int
10793 echo_area_display (int update_frame_p)
10795 Lisp_Object mini_window;
10796 struct window *w;
10797 struct frame *f;
10798 int window_height_changed_p = 0;
10799 struct frame *sf = SELECTED_FRAME ();
10801 mini_window = FRAME_MINIBUF_WINDOW (sf);
10802 w = XWINDOW (mini_window);
10803 f = XFRAME (WINDOW_FRAME (w));
10805 /* Don't display if frame is invisible or not yet initialized. */
10806 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10807 return 0;
10809 #ifdef HAVE_WINDOW_SYSTEM
10810 /* When Emacs starts, selected_frame may be the initial terminal
10811 frame. If we let this through, a message would be displayed on
10812 the terminal. */
10813 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10814 return 0;
10815 #endif /* HAVE_WINDOW_SYSTEM */
10817 /* Redraw garbaged frames. */
10818 clear_garbaged_frames ();
10820 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10822 echo_area_window = mini_window;
10823 window_height_changed_p = display_echo_area (w);
10824 w->must_be_updated_p = 1;
10826 /* Update the display, unless called from redisplay_internal.
10827 Also don't update the screen during redisplay itself. The
10828 update will happen at the end of redisplay, and an update
10829 here could cause confusion. */
10830 if (update_frame_p && !redisplaying_p)
10832 int n = 0;
10834 /* If the display update has been interrupted by pending
10835 input, update mode lines in the frame. Due to the
10836 pending input, it might have been that redisplay hasn't
10837 been called, so that mode lines above the echo area are
10838 garbaged. This looks odd, so we prevent it here. */
10839 if (!display_completed)
10840 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10842 if (window_height_changed_p
10843 /* Don't do this if Emacs is shutting down. Redisplay
10844 needs to run hooks. */
10845 && !NILP (Vrun_hooks))
10847 /* Must update other windows. Likewise as in other
10848 cases, don't let this update be interrupted by
10849 pending input. */
10850 ptrdiff_t count = SPECPDL_INDEX ();
10851 specbind (Qredisplay_dont_pause, Qt);
10852 windows_or_buffers_changed = 1;
10853 redisplay_internal ();
10854 unbind_to (count, Qnil);
10856 else if (FRAME_WINDOW_P (f) && n == 0)
10858 /* Window configuration is the same as before.
10859 Can do with a display update of the echo area,
10860 unless we displayed some mode lines. */
10861 update_single_window (w, 1);
10862 FRAME_RIF (f)->flush_display (f);
10864 else
10865 update_frame (f, 1, 1);
10867 /* If cursor is in the echo area, make sure that the next
10868 redisplay displays the minibuffer, so that the cursor will
10869 be replaced with what the minibuffer wants. */
10870 if (cursor_in_echo_area)
10871 ++windows_or_buffers_changed;
10874 else if (!EQ (mini_window, selected_window))
10875 windows_or_buffers_changed++;
10877 /* Last displayed message is now the current message. */
10878 echo_area_buffer[1] = echo_area_buffer[0];
10879 /* Inform read_char that we're not echoing. */
10880 echo_message_buffer = Qnil;
10882 /* Prevent redisplay optimization in redisplay_internal by resetting
10883 this_line_start_pos. This is done because the mini-buffer now
10884 displays the message instead of its buffer text. */
10885 if (EQ (mini_window, selected_window))
10886 CHARPOS (this_line_start_pos) = 0;
10888 return window_height_changed_p;
10891 /* Nonzero if the current window's buffer is shown in more than one
10892 window and was modified since last redisplay. */
10894 static int
10895 buffer_shared_and_changed (void)
10897 return (buffer_window_count (current_buffer) > 1
10898 && UNCHANGED_MODIFIED < MODIFF);
10901 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10902 is enabled and mark of W's buffer was changed since last W's update. */
10904 static int
10905 window_buffer_changed (struct window *w)
10907 struct buffer *b = XBUFFER (w->contents);
10909 eassert (BUFFER_LIVE_P (b));
10911 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10912 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10913 != (w->region_showing != 0)));
10916 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10918 static int
10919 mode_line_update_needed (struct window *w)
10921 return (w->column_number_displayed != -1
10922 && !(PT == w->last_point && !window_outdated (w))
10923 && (w->column_number_displayed != current_column ()));
10926 /* Nonzero if window start of W is frozen and may not be changed during
10927 redisplay. */
10929 static bool
10930 window_frozen_p (struct window *w)
10932 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10934 Lisp_Object window;
10936 XSETWINDOW (window, w);
10937 if (MINI_WINDOW_P (w))
10938 return 0;
10939 else if (EQ (window, selected_window))
10940 return 0;
10941 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10942 && EQ (window, Vminibuf_scroll_window))
10943 /* This special window can't be frozen too. */
10944 return 0;
10945 else
10946 return 1;
10948 return 0;
10951 /***********************************************************************
10952 Mode Lines and Frame Titles
10953 ***********************************************************************/
10955 /* A buffer for constructing non-propertized mode-line strings and
10956 frame titles in it; allocated from the heap in init_xdisp and
10957 resized as needed in store_mode_line_noprop_char. */
10959 static char *mode_line_noprop_buf;
10961 /* The buffer's end, and a current output position in it. */
10963 static char *mode_line_noprop_buf_end;
10964 static char *mode_line_noprop_ptr;
10966 #define MODE_LINE_NOPROP_LEN(start) \
10967 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10969 static enum {
10970 MODE_LINE_DISPLAY = 0,
10971 MODE_LINE_TITLE,
10972 MODE_LINE_NOPROP,
10973 MODE_LINE_STRING
10974 } mode_line_target;
10976 /* Alist that caches the results of :propertize.
10977 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10978 static Lisp_Object mode_line_proptrans_alist;
10980 /* List of strings making up the mode-line. */
10981 static Lisp_Object mode_line_string_list;
10983 /* Base face property when building propertized mode line string. */
10984 static Lisp_Object mode_line_string_face;
10985 static Lisp_Object mode_line_string_face_prop;
10988 /* Unwind data for mode line strings */
10990 static Lisp_Object Vmode_line_unwind_vector;
10992 static Lisp_Object
10993 format_mode_line_unwind_data (struct frame *target_frame,
10994 struct buffer *obuf,
10995 Lisp_Object owin,
10996 int save_proptrans)
10998 Lisp_Object vector, tmp;
11000 /* Reduce consing by keeping one vector in
11001 Vwith_echo_area_save_vector. */
11002 vector = Vmode_line_unwind_vector;
11003 Vmode_line_unwind_vector = Qnil;
11005 if (NILP (vector))
11006 vector = Fmake_vector (make_number (10), Qnil);
11008 ASET (vector, 0, make_number (mode_line_target));
11009 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11010 ASET (vector, 2, mode_line_string_list);
11011 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11012 ASET (vector, 4, mode_line_string_face);
11013 ASET (vector, 5, mode_line_string_face_prop);
11015 if (obuf)
11016 XSETBUFFER (tmp, obuf);
11017 else
11018 tmp = Qnil;
11019 ASET (vector, 6, tmp);
11020 ASET (vector, 7, owin);
11021 if (target_frame)
11023 /* Similarly to `with-selected-window', if the operation selects
11024 a window on another frame, we must restore that frame's
11025 selected window, and (for a tty) the top-frame. */
11026 ASET (vector, 8, target_frame->selected_window);
11027 if (FRAME_TERMCAP_P (target_frame))
11028 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11031 return vector;
11034 static void
11035 unwind_format_mode_line (Lisp_Object vector)
11037 Lisp_Object old_window = AREF (vector, 7);
11038 Lisp_Object target_frame_window = AREF (vector, 8);
11039 Lisp_Object old_top_frame = AREF (vector, 9);
11041 mode_line_target = XINT (AREF (vector, 0));
11042 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11043 mode_line_string_list = AREF (vector, 2);
11044 if (! EQ (AREF (vector, 3), Qt))
11045 mode_line_proptrans_alist = AREF (vector, 3);
11046 mode_line_string_face = AREF (vector, 4);
11047 mode_line_string_face_prop = AREF (vector, 5);
11049 /* Select window before buffer, since it may change the buffer. */
11050 if (!NILP (old_window))
11052 /* If the operation that we are unwinding had selected a window
11053 on a different frame, reset its frame-selected-window. For a
11054 text terminal, reset its top-frame if necessary. */
11055 if (!NILP (target_frame_window))
11057 Lisp_Object frame
11058 = WINDOW_FRAME (XWINDOW (target_frame_window));
11060 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11061 Fselect_window (target_frame_window, Qt);
11063 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11064 Fselect_frame (old_top_frame, Qt);
11067 Fselect_window (old_window, Qt);
11070 if (!NILP (AREF (vector, 6)))
11072 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11073 ASET (vector, 6, Qnil);
11076 Vmode_line_unwind_vector = vector;
11080 /* Store a single character C for the frame title in mode_line_noprop_buf.
11081 Re-allocate mode_line_noprop_buf if necessary. */
11083 static void
11084 store_mode_line_noprop_char (char c)
11086 /* If output position has reached the end of the allocated buffer,
11087 increase the buffer's size. */
11088 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11090 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11091 ptrdiff_t size = len;
11092 mode_line_noprop_buf =
11093 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11094 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11095 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11098 *mode_line_noprop_ptr++ = c;
11102 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11103 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11104 characters that yield more columns than PRECISION; PRECISION <= 0
11105 means copy the whole string. Pad with spaces until FIELD_WIDTH
11106 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11107 pad. Called from display_mode_element when it is used to build a
11108 frame title. */
11110 static int
11111 store_mode_line_noprop (const char *string, int field_width, int precision)
11113 const unsigned char *str = (const unsigned char *) string;
11114 int n = 0;
11115 ptrdiff_t dummy, nbytes;
11117 /* Copy at most PRECISION chars from STR. */
11118 nbytes = strlen (string);
11119 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11120 while (nbytes--)
11121 store_mode_line_noprop_char (*str++);
11123 /* Fill up with spaces until FIELD_WIDTH reached. */
11124 while (field_width > 0
11125 && n < field_width)
11127 store_mode_line_noprop_char (' ');
11128 ++n;
11131 return n;
11134 /***********************************************************************
11135 Frame Titles
11136 ***********************************************************************/
11138 #ifdef HAVE_WINDOW_SYSTEM
11140 /* Set the title of FRAME, if it has changed. The title format is
11141 Vicon_title_format if FRAME is iconified, otherwise it is
11142 frame_title_format. */
11144 static void
11145 x_consider_frame_title (Lisp_Object frame)
11147 struct frame *f = XFRAME (frame);
11149 if (FRAME_WINDOW_P (f)
11150 || FRAME_MINIBUF_ONLY_P (f)
11151 || f->explicit_name)
11153 /* Do we have more than one visible frame on this X display? */
11154 Lisp_Object tail, other_frame, fmt;
11155 ptrdiff_t title_start;
11156 char *title;
11157 ptrdiff_t len;
11158 struct it it;
11159 ptrdiff_t count = SPECPDL_INDEX ();
11161 FOR_EACH_FRAME (tail, other_frame)
11163 struct frame *tf = XFRAME (other_frame);
11165 if (tf != f
11166 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11167 && !FRAME_MINIBUF_ONLY_P (tf)
11168 && !EQ (other_frame, tip_frame)
11169 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11170 break;
11173 /* Set global variable indicating that multiple frames exist. */
11174 multiple_frames = CONSP (tail);
11176 /* Switch to the buffer of selected window of the frame. Set up
11177 mode_line_target so that display_mode_element will output into
11178 mode_line_noprop_buf; then display the title. */
11179 record_unwind_protect (unwind_format_mode_line,
11180 format_mode_line_unwind_data
11181 (f, current_buffer, selected_window, 0));
11183 Fselect_window (f->selected_window, Qt);
11184 set_buffer_internal_1
11185 (XBUFFER (XWINDOW (f->selected_window)->contents));
11186 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11188 mode_line_target = MODE_LINE_TITLE;
11189 title_start = MODE_LINE_NOPROP_LEN (0);
11190 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11191 NULL, DEFAULT_FACE_ID);
11192 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11193 len = MODE_LINE_NOPROP_LEN (title_start);
11194 title = mode_line_noprop_buf + title_start;
11195 unbind_to (count, Qnil);
11197 /* Set the title only if it's changed. This avoids consing in
11198 the common case where it hasn't. (If it turns out that we've
11199 already wasted too much time by walking through the list with
11200 display_mode_element, then we might need to optimize at a
11201 higher level than this.) */
11202 if (! STRINGP (f->name)
11203 || SBYTES (f->name) != len
11204 || memcmp (title, SDATA (f->name), len) != 0)
11205 x_implicitly_set_name (f, make_string (title, len), Qnil);
11209 #endif /* not HAVE_WINDOW_SYSTEM */
11212 /***********************************************************************
11213 Menu Bars
11214 ***********************************************************************/
11217 /* Prepare for redisplay by updating menu-bar item lists when
11218 appropriate. This can call eval. */
11220 void
11221 prepare_menu_bars (void)
11223 int all_windows;
11224 struct gcpro gcpro1, gcpro2;
11225 struct frame *f;
11226 Lisp_Object tooltip_frame;
11228 #ifdef HAVE_WINDOW_SYSTEM
11229 tooltip_frame = tip_frame;
11230 #else
11231 tooltip_frame = Qnil;
11232 #endif
11234 /* Update all frame titles based on their buffer names, etc. We do
11235 this before the menu bars so that the buffer-menu will show the
11236 up-to-date frame titles. */
11237 #ifdef HAVE_WINDOW_SYSTEM
11238 if (windows_or_buffers_changed || update_mode_lines)
11240 Lisp_Object tail, frame;
11242 FOR_EACH_FRAME (tail, frame)
11244 f = XFRAME (frame);
11245 if (!EQ (frame, tooltip_frame)
11246 && (FRAME_ICONIFIED_P (f)
11247 || FRAME_VISIBLE_P (f) == 1
11248 /* Exclude TTY frames that are obscured because they
11249 are not the top frame on their console. This is
11250 because x_consider_frame_title actually switches
11251 to the frame, which for TTY frames means it is
11252 marked as garbaged, and will be completely
11253 redrawn on the next redisplay cycle. This causes
11254 TTY frames to be completely redrawn, when there
11255 are more than one of them, even though nothing
11256 should be changed on display. */
11257 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11258 x_consider_frame_title (frame);
11261 #endif /* HAVE_WINDOW_SYSTEM */
11263 /* Update the menu bar item lists, if appropriate. This has to be
11264 done before any actual redisplay or generation of display lines. */
11265 all_windows = (update_mode_lines
11266 || buffer_shared_and_changed ()
11267 || windows_or_buffers_changed);
11268 if (all_windows)
11270 Lisp_Object tail, frame;
11271 ptrdiff_t count = SPECPDL_INDEX ();
11272 /* 1 means that update_menu_bar has run its hooks
11273 so any further calls to update_menu_bar shouldn't do so again. */
11274 int menu_bar_hooks_run = 0;
11276 record_unwind_save_match_data ();
11278 FOR_EACH_FRAME (tail, frame)
11280 f = XFRAME (frame);
11282 /* Ignore tooltip frame. */
11283 if (EQ (frame, tooltip_frame))
11284 continue;
11286 /* If a window on this frame changed size, report that to
11287 the user and clear the size-change flag. */
11288 if (FRAME_WINDOW_SIZES_CHANGED (f))
11290 Lisp_Object functions;
11292 /* Clear flag first in case we get an error below. */
11293 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11294 functions = Vwindow_size_change_functions;
11295 GCPRO2 (tail, functions);
11297 while (CONSP (functions))
11299 if (!EQ (XCAR (functions), Qt))
11300 call1 (XCAR (functions), frame);
11301 functions = XCDR (functions);
11303 UNGCPRO;
11306 GCPRO1 (tail);
11307 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11308 #ifdef HAVE_WINDOW_SYSTEM
11309 update_tool_bar (f, 0);
11310 #endif
11311 #ifdef HAVE_NS
11312 if (windows_or_buffers_changed
11313 && FRAME_NS_P (f))
11314 ns_set_doc_edited
11315 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11316 #endif
11317 UNGCPRO;
11320 unbind_to (count, Qnil);
11322 else
11324 struct frame *sf = SELECTED_FRAME ();
11325 update_menu_bar (sf, 1, 0);
11326 #ifdef HAVE_WINDOW_SYSTEM
11327 update_tool_bar (sf, 1);
11328 #endif
11333 /* Update the menu bar item list for frame F. This has to be done
11334 before we start to fill in any display lines, because it can call
11335 eval.
11337 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11339 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11340 already ran the menu bar hooks for this redisplay, so there
11341 is no need to run them again. The return value is the
11342 updated value of this flag, to pass to the next call. */
11344 static int
11345 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11347 Lisp_Object window;
11348 register struct window *w;
11350 /* If called recursively during a menu update, do nothing. This can
11351 happen when, for instance, an activate-menubar-hook causes a
11352 redisplay. */
11353 if (inhibit_menubar_update)
11354 return hooks_run;
11356 window = FRAME_SELECTED_WINDOW (f);
11357 w = XWINDOW (window);
11359 if (FRAME_WINDOW_P (f)
11361 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11362 || defined (HAVE_NS) || defined (USE_GTK)
11363 FRAME_EXTERNAL_MENU_BAR (f)
11364 #else
11365 FRAME_MENU_BAR_LINES (f) > 0
11366 #endif
11367 : FRAME_MENU_BAR_LINES (f) > 0)
11369 /* If the user has switched buffers or windows, we need to
11370 recompute to reflect the new bindings. But we'll
11371 recompute when update_mode_lines is set too; that means
11372 that people can use force-mode-line-update to request
11373 that the menu bar be recomputed. The adverse effect on
11374 the rest of the redisplay algorithm is about the same as
11375 windows_or_buffers_changed anyway. */
11376 if (windows_or_buffers_changed
11377 /* This used to test w->update_mode_line, but we believe
11378 there is no need to recompute the menu in that case. */
11379 || update_mode_lines
11380 || window_buffer_changed (w))
11382 struct buffer *prev = current_buffer;
11383 ptrdiff_t count = SPECPDL_INDEX ();
11385 specbind (Qinhibit_menubar_update, Qt);
11387 set_buffer_internal_1 (XBUFFER (w->contents));
11388 if (save_match_data)
11389 record_unwind_save_match_data ();
11390 if (NILP (Voverriding_local_map_menu_flag))
11392 specbind (Qoverriding_terminal_local_map, Qnil);
11393 specbind (Qoverriding_local_map, Qnil);
11396 if (!hooks_run)
11398 /* Run the Lucid hook. */
11399 safe_run_hooks (Qactivate_menubar_hook);
11401 /* If it has changed current-menubar from previous value,
11402 really recompute the menu-bar from the value. */
11403 if (! NILP (Vlucid_menu_bar_dirty_flag))
11404 call0 (Qrecompute_lucid_menubar);
11406 safe_run_hooks (Qmenu_bar_update_hook);
11408 hooks_run = 1;
11411 XSETFRAME (Vmenu_updating_frame, f);
11412 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11414 /* Redisplay the menu bar in case we changed it. */
11415 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11416 || defined (HAVE_NS) || defined (USE_GTK)
11417 if (FRAME_WINDOW_P (f))
11419 #if defined (HAVE_NS)
11420 /* All frames on Mac OS share the same menubar. So only
11421 the selected frame should be allowed to set it. */
11422 if (f == SELECTED_FRAME ())
11423 #endif
11424 set_frame_menubar (f, 0, 0);
11426 else
11427 /* On a terminal screen, the menu bar is an ordinary screen
11428 line, and this makes it get updated. */
11429 w->update_mode_line = 1;
11430 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11431 /* In the non-toolkit version, the menu bar is an ordinary screen
11432 line, and this makes it get updated. */
11433 w->update_mode_line = 1;
11434 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11436 unbind_to (count, Qnil);
11437 set_buffer_internal_1 (prev);
11441 return hooks_run;
11444 /***********************************************************************
11445 Tool-bars
11446 ***********************************************************************/
11448 #ifdef HAVE_WINDOW_SYSTEM
11450 /* Where the mouse was last time we reported a mouse event. */
11452 struct frame *last_mouse_frame;
11454 /* Tool-bar item index of the item on which a mouse button was pressed
11455 or -1. */
11457 int last_tool_bar_item;
11459 /* Select `frame' temporarily without running all the code in
11460 do_switch_frame.
11461 FIXME: Maybe do_switch_frame should be trimmed down similarly
11462 when `norecord' is set. */
11463 static void
11464 fast_set_selected_frame (Lisp_Object frame)
11466 if (!EQ (selected_frame, frame))
11468 selected_frame = frame;
11469 selected_window = XFRAME (frame)->selected_window;
11473 /* Update the tool-bar item list for frame F. This has to be done
11474 before we start to fill in any display lines. Called from
11475 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11476 and restore it here. */
11478 static void
11479 update_tool_bar (struct frame *f, int save_match_data)
11481 #if defined (USE_GTK) || defined (HAVE_NS)
11482 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11483 #else
11484 int do_update = WINDOWP (f->tool_bar_window)
11485 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11486 #endif
11488 if (do_update)
11490 Lisp_Object window;
11491 struct window *w;
11493 window = FRAME_SELECTED_WINDOW (f);
11494 w = XWINDOW (window);
11496 /* If the user has switched buffers or windows, we need to
11497 recompute to reflect the new bindings. But we'll
11498 recompute when update_mode_lines is set too; that means
11499 that people can use force-mode-line-update to request
11500 that the menu bar be recomputed. The adverse effect on
11501 the rest of the redisplay algorithm is about the same as
11502 windows_or_buffers_changed anyway. */
11503 if (windows_or_buffers_changed
11504 || w->update_mode_line
11505 || update_mode_lines
11506 || window_buffer_changed (w))
11508 struct buffer *prev = current_buffer;
11509 ptrdiff_t count = SPECPDL_INDEX ();
11510 Lisp_Object frame, new_tool_bar;
11511 int new_n_tool_bar;
11512 struct gcpro gcpro1;
11514 /* Set current_buffer to the buffer of the selected
11515 window of the frame, so that we get the right local
11516 keymaps. */
11517 set_buffer_internal_1 (XBUFFER (w->contents));
11519 /* Save match data, if we must. */
11520 if (save_match_data)
11521 record_unwind_save_match_data ();
11523 /* Make sure that we don't accidentally use bogus keymaps. */
11524 if (NILP (Voverriding_local_map_menu_flag))
11526 specbind (Qoverriding_terminal_local_map, Qnil);
11527 specbind (Qoverriding_local_map, Qnil);
11530 GCPRO1 (new_tool_bar);
11532 /* We must temporarily set the selected frame to this frame
11533 before calling tool_bar_items, because the calculation of
11534 the tool-bar keymap uses the selected frame (see
11535 `tool-bar-make-keymap' in tool-bar.el). */
11536 eassert (EQ (selected_window,
11537 /* Since we only explicitly preserve selected_frame,
11538 check that selected_window would be redundant. */
11539 XFRAME (selected_frame)->selected_window));
11540 record_unwind_protect (fast_set_selected_frame, selected_frame);
11541 XSETFRAME (frame, f);
11542 fast_set_selected_frame (frame);
11544 /* Build desired tool-bar items from keymaps. */
11545 new_tool_bar
11546 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11547 &new_n_tool_bar);
11549 /* Redisplay the tool-bar if we changed it. */
11550 if (new_n_tool_bar != f->n_tool_bar_items
11551 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11553 /* Redisplay that happens asynchronously due to an expose event
11554 may access f->tool_bar_items. Make sure we update both
11555 variables within BLOCK_INPUT so no such event interrupts. */
11556 block_input ();
11557 fset_tool_bar_items (f, new_tool_bar);
11558 f->n_tool_bar_items = new_n_tool_bar;
11559 w->update_mode_line = 1;
11560 unblock_input ();
11563 UNGCPRO;
11565 unbind_to (count, Qnil);
11566 set_buffer_internal_1 (prev);
11572 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11573 F's desired tool-bar contents. F->tool_bar_items must have
11574 been set up previously by calling prepare_menu_bars. */
11576 static void
11577 build_desired_tool_bar_string (struct frame *f)
11579 int i, size, size_needed;
11580 struct gcpro gcpro1, gcpro2, gcpro3;
11581 Lisp_Object image, plist, props;
11583 image = plist = props = Qnil;
11584 GCPRO3 (image, plist, props);
11586 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11587 Otherwise, make a new string. */
11589 /* The size of the string we might be able to reuse. */
11590 size = (STRINGP (f->desired_tool_bar_string)
11591 ? SCHARS (f->desired_tool_bar_string)
11592 : 0);
11594 /* We need one space in the string for each image. */
11595 size_needed = f->n_tool_bar_items;
11597 /* Reuse f->desired_tool_bar_string, if possible. */
11598 if (size < size_needed || NILP (f->desired_tool_bar_string))
11599 fset_desired_tool_bar_string
11600 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11601 else
11603 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11604 Fremove_text_properties (make_number (0), make_number (size),
11605 props, f->desired_tool_bar_string);
11608 /* Put a `display' property on the string for the images to display,
11609 put a `menu_item' property on tool-bar items with a value that
11610 is the index of the item in F's tool-bar item vector. */
11611 for (i = 0; i < f->n_tool_bar_items; ++i)
11613 #define PROP(IDX) \
11614 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11616 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11617 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11618 int hmargin, vmargin, relief, idx, end;
11620 /* If image is a vector, choose the image according to the
11621 button state. */
11622 image = PROP (TOOL_BAR_ITEM_IMAGES);
11623 if (VECTORP (image))
11625 if (enabled_p)
11626 idx = (selected_p
11627 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11628 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11629 else
11630 idx = (selected_p
11631 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11632 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11634 eassert (ASIZE (image) >= idx);
11635 image = AREF (image, idx);
11637 else
11638 idx = -1;
11640 /* Ignore invalid image specifications. */
11641 if (!valid_image_p (image))
11642 continue;
11644 /* Display the tool-bar button pressed, or depressed. */
11645 plist = Fcopy_sequence (XCDR (image));
11647 /* Compute margin and relief to draw. */
11648 relief = (tool_bar_button_relief >= 0
11649 ? tool_bar_button_relief
11650 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11651 hmargin = vmargin = relief;
11653 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11654 INT_MAX - max (hmargin, vmargin)))
11656 hmargin += XFASTINT (Vtool_bar_button_margin);
11657 vmargin += XFASTINT (Vtool_bar_button_margin);
11659 else if (CONSP (Vtool_bar_button_margin))
11661 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11662 INT_MAX - hmargin))
11663 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11665 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11666 INT_MAX - vmargin))
11667 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11670 if (auto_raise_tool_bar_buttons_p)
11672 /* Add a `:relief' property to the image spec if the item is
11673 selected. */
11674 if (selected_p)
11676 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11677 hmargin -= relief;
11678 vmargin -= relief;
11681 else
11683 /* If image is selected, display it pressed, i.e. with a
11684 negative relief. If it's not selected, display it with a
11685 raised relief. */
11686 plist = Fplist_put (plist, QCrelief,
11687 (selected_p
11688 ? make_number (-relief)
11689 : make_number (relief)));
11690 hmargin -= relief;
11691 vmargin -= relief;
11694 /* Put a margin around the image. */
11695 if (hmargin || vmargin)
11697 if (hmargin == vmargin)
11698 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11699 else
11700 plist = Fplist_put (plist, QCmargin,
11701 Fcons (make_number (hmargin),
11702 make_number (vmargin)));
11705 /* If button is not enabled, and we don't have special images
11706 for the disabled state, make the image appear disabled by
11707 applying an appropriate algorithm to it. */
11708 if (!enabled_p && idx < 0)
11709 plist = Fplist_put (plist, QCconversion, Qdisabled);
11711 /* Put a `display' text property on the string for the image to
11712 display. Put a `menu-item' property on the string that gives
11713 the start of this item's properties in the tool-bar items
11714 vector. */
11715 image = Fcons (Qimage, plist);
11716 props = list4 (Qdisplay, image,
11717 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11719 /* Let the last image hide all remaining spaces in the tool bar
11720 string. The string can be longer than needed when we reuse a
11721 previous string. */
11722 if (i + 1 == f->n_tool_bar_items)
11723 end = SCHARS (f->desired_tool_bar_string);
11724 else
11725 end = i + 1;
11726 Fadd_text_properties (make_number (i), make_number (end),
11727 props, f->desired_tool_bar_string);
11728 #undef PROP
11731 UNGCPRO;
11735 /* Display one line of the tool-bar of frame IT->f.
11737 HEIGHT specifies the desired height of the tool-bar line.
11738 If the actual height of the glyph row is less than HEIGHT, the
11739 row's height is increased to HEIGHT, and the icons are centered
11740 vertically in the new height.
11742 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11743 count a final empty row in case the tool-bar width exactly matches
11744 the window width.
11747 static void
11748 display_tool_bar_line (struct it *it, int height)
11750 struct glyph_row *row = it->glyph_row;
11751 int max_x = it->last_visible_x;
11752 struct glyph *last;
11754 prepare_desired_row (row);
11755 row->y = it->current_y;
11757 /* Note that this isn't made use of if the face hasn't a box,
11758 so there's no need to check the face here. */
11759 it->start_of_box_run_p = 1;
11761 while (it->current_x < max_x)
11763 int x, n_glyphs_before, i, nglyphs;
11764 struct it it_before;
11766 /* Get the next display element. */
11767 if (!get_next_display_element (it))
11769 /* Don't count empty row if we are counting needed tool-bar lines. */
11770 if (height < 0 && !it->hpos)
11771 return;
11772 break;
11775 /* Produce glyphs. */
11776 n_glyphs_before = row->used[TEXT_AREA];
11777 it_before = *it;
11779 PRODUCE_GLYPHS (it);
11781 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11782 i = 0;
11783 x = it_before.current_x;
11784 while (i < nglyphs)
11786 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11788 if (x + glyph->pixel_width > max_x)
11790 /* Glyph doesn't fit on line. Backtrack. */
11791 row->used[TEXT_AREA] = n_glyphs_before;
11792 *it = it_before;
11793 /* If this is the only glyph on this line, it will never fit on the
11794 tool-bar, so skip it. But ensure there is at least one glyph,
11795 so we don't accidentally disable the tool-bar. */
11796 if (n_glyphs_before == 0
11797 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11798 break;
11799 goto out;
11802 ++it->hpos;
11803 x += glyph->pixel_width;
11804 ++i;
11807 /* Stop at line end. */
11808 if (ITERATOR_AT_END_OF_LINE_P (it))
11809 break;
11811 set_iterator_to_next (it, 1);
11814 out:;
11816 row->displays_text_p = row->used[TEXT_AREA] != 0;
11818 /* Use default face for the border below the tool bar.
11820 FIXME: When auto-resize-tool-bars is grow-only, there is
11821 no additional border below the possibly empty tool-bar lines.
11822 So to make the extra empty lines look "normal", we have to
11823 use the tool-bar face for the border too. */
11824 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11825 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11826 it->face_id = DEFAULT_FACE_ID;
11828 extend_face_to_end_of_line (it);
11829 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11830 last->right_box_line_p = 1;
11831 if (last == row->glyphs[TEXT_AREA])
11832 last->left_box_line_p = 1;
11834 /* Make line the desired height and center it vertically. */
11835 if ((height -= it->max_ascent + it->max_descent) > 0)
11837 /* Don't add more than one line height. */
11838 height %= FRAME_LINE_HEIGHT (it->f);
11839 it->max_ascent += height / 2;
11840 it->max_descent += (height + 1) / 2;
11843 compute_line_metrics (it);
11845 /* If line is empty, make it occupy the rest of the tool-bar. */
11846 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11848 row->height = row->phys_height = it->last_visible_y - row->y;
11849 row->visible_height = row->height;
11850 row->ascent = row->phys_ascent = 0;
11851 row->extra_line_spacing = 0;
11854 row->full_width_p = 1;
11855 row->continued_p = 0;
11856 row->truncated_on_left_p = 0;
11857 row->truncated_on_right_p = 0;
11859 it->current_x = it->hpos = 0;
11860 it->current_y += row->height;
11861 ++it->vpos;
11862 ++it->glyph_row;
11866 /* Max tool-bar height. */
11868 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11869 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11871 /* Value is the number of screen lines needed to make all tool-bar
11872 items of frame F visible. The number of actual rows needed is
11873 returned in *N_ROWS if non-NULL. */
11875 static int
11876 tool_bar_lines_needed (struct frame *f, int *n_rows)
11878 struct window *w = XWINDOW (f->tool_bar_window);
11879 struct it it;
11880 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11881 the desired matrix, so use (unused) mode-line row as temporary row to
11882 avoid destroying the first tool-bar row. */
11883 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11885 /* Initialize an iterator for iteration over
11886 F->desired_tool_bar_string in the tool-bar window of frame F. */
11887 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11888 it.first_visible_x = 0;
11889 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11890 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11891 it.paragraph_embedding = L2R;
11893 while (!ITERATOR_AT_END_P (&it))
11895 clear_glyph_row (temp_row);
11896 it.glyph_row = temp_row;
11897 display_tool_bar_line (&it, -1);
11899 clear_glyph_row (temp_row);
11901 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11902 if (n_rows)
11903 *n_rows = it.vpos > 0 ? it.vpos : -1;
11905 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11909 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11910 0, 1, 0,
11911 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11912 If FRAME is nil or omitted, use the selected frame. */)
11913 (Lisp_Object frame)
11915 struct frame *f = decode_any_frame (frame);
11916 struct window *w;
11917 int nlines = 0;
11919 if (WINDOWP (f->tool_bar_window)
11920 && (w = XWINDOW (f->tool_bar_window),
11921 WINDOW_TOTAL_LINES (w) > 0))
11923 update_tool_bar (f, 1);
11924 if (f->n_tool_bar_items)
11926 build_desired_tool_bar_string (f);
11927 nlines = tool_bar_lines_needed (f, NULL);
11931 return make_number (nlines);
11935 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11936 height should be changed. */
11938 static int
11939 redisplay_tool_bar (struct frame *f)
11941 struct window *w;
11942 struct it it;
11943 struct glyph_row *row;
11945 #if defined (USE_GTK) || defined (HAVE_NS)
11946 if (FRAME_EXTERNAL_TOOL_BAR (f))
11947 update_frame_tool_bar (f);
11948 return 0;
11949 #endif
11951 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11952 do anything. This means you must start with tool-bar-lines
11953 non-zero to get the auto-sizing effect. Or in other words, you
11954 can turn off tool-bars by specifying tool-bar-lines zero. */
11955 if (!WINDOWP (f->tool_bar_window)
11956 || (w = XWINDOW (f->tool_bar_window),
11957 WINDOW_TOTAL_LINES (w) == 0))
11958 return 0;
11960 /* Set up an iterator for the tool-bar window. */
11961 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11962 it.first_visible_x = 0;
11963 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11964 row = it.glyph_row;
11966 /* Build a string that represents the contents of the tool-bar. */
11967 build_desired_tool_bar_string (f);
11968 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11969 /* FIXME: This should be controlled by a user option. But it
11970 doesn't make sense to have an R2L tool bar if the menu bar cannot
11971 be drawn also R2L, and making the menu bar R2L is tricky due
11972 toolkit-specific code that implements it. If an R2L tool bar is
11973 ever supported, display_tool_bar_line should also be augmented to
11974 call unproduce_glyphs like display_line and display_string
11975 do. */
11976 it.paragraph_embedding = L2R;
11978 if (f->n_tool_bar_rows == 0)
11980 int nlines;
11982 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11983 nlines != WINDOW_TOTAL_LINES (w)))
11985 Lisp_Object frame;
11986 int old_height = WINDOW_TOTAL_LINES (w);
11988 XSETFRAME (frame, f);
11989 Fmodify_frame_parameters (frame,
11990 list1 (Fcons (Qtool_bar_lines,
11991 make_number (nlines))));
11992 if (WINDOW_TOTAL_LINES (w) != old_height)
11994 clear_glyph_matrix (w->desired_matrix);
11995 f->fonts_changed = 1;
11996 return 1;
12001 /* Display as many lines as needed to display all tool-bar items. */
12003 if (f->n_tool_bar_rows > 0)
12005 int border, rows, height, extra;
12007 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12008 border = XINT (Vtool_bar_border);
12009 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12010 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12011 else if (EQ (Vtool_bar_border, Qborder_width))
12012 border = f->border_width;
12013 else
12014 border = 0;
12015 if (border < 0)
12016 border = 0;
12018 rows = f->n_tool_bar_rows;
12019 height = max (1, (it.last_visible_y - border) / rows);
12020 extra = it.last_visible_y - border - height * rows;
12022 while (it.current_y < it.last_visible_y)
12024 int h = 0;
12025 if (extra > 0 && rows-- > 0)
12027 h = (extra + rows - 1) / rows;
12028 extra -= h;
12030 display_tool_bar_line (&it, height + h);
12033 else
12035 while (it.current_y < it.last_visible_y)
12036 display_tool_bar_line (&it, 0);
12039 /* It doesn't make much sense to try scrolling in the tool-bar
12040 window, so don't do it. */
12041 w->desired_matrix->no_scrolling_p = 1;
12042 w->must_be_updated_p = 1;
12044 if (!NILP (Vauto_resize_tool_bars))
12046 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12047 int change_height_p = 0;
12049 /* If we couldn't display everything, change the tool-bar's
12050 height if there is room for more. */
12051 if (IT_STRING_CHARPOS (it) < it.end_charpos
12052 && it.current_y < max_tool_bar_height)
12053 change_height_p = 1;
12055 row = it.glyph_row - 1;
12057 /* If there are blank lines at the end, except for a partially
12058 visible blank line at the end that is smaller than
12059 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12060 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12061 && row->height >= FRAME_LINE_HEIGHT (f))
12062 change_height_p = 1;
12064 /* If row displays tool-bar items, but is partially visible,
12065 change the tool-bar's height. */
12066 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12067 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12068 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12069 change_height_p = 1;
12071 /* Resize windows as needed by changing the `tool-bar-lines'
12072 frame parameter. */
12073 if (change_height_p)
12075 Lisp_Object frame;
12076 int old_height = WINDOW_TOTAL_LINES (w);
12077 int nrows;
12078 int nlines = tool_bar_lines_needed (f, &nrows);
12080 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12081 && !f->minimize_tool_bar_window_p)
12082 ? (nlines > old_height)
12083 : (nlines != old_height));
12084 f->minimize_tool_bar_window_p = 0;
12086 if (change_height_p)
12088 XSETFRAME (frame, f);
12089 Fmodify_frame_parameters (frame,
12090 list1 (Fcons (Qtool_bar_lines,
12091 make_number (nlines))));
12092 if (WINDOW_TOTAL_LINES (w) != old_height)
12094 clear_glyph_matrix (w->desired_matrix);
12095 f->n_tool_bar_rows = nrows;
12096 f->fonts_changed = 1;
12097 return 1;
12103 f->minimize_tool_bar_window_p = 0;
12104 return 0;
12108 /* Get information about the tool-bar item which is displayed in GLYPH
12109 on frame F. Return in *PROP_IDX the index where tool-bar item
12110 properties start in F->tool_bar_items. Value is zero if
12111 GLYPH doesn't display a tool-bar item. */
12113 static int
12114 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12116 Lisp_Object prop;
12117 int success_p;
12118 int charpos;
12120 /* This function can be called asynchronously, which means we must
12121 exclude any possibility that Fget_text_property signals an
12122 error. */
12123 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12124 charpos = max (0, charpos);
12126 /* Get the text property `menu-item' at pos. The value of that
12127 property is the start index of this item's properties in
12128 F->tool_bar_items. */
12129 prop = Fget_text_property (make_number (charpos),
12130 Qmenu_item, f->current_tool_bar_string);
12131 if (INTEGERP (prop))
12133 *prop_idx = XINT (prop);
12134 success_p = 1;
12136 else
12137 success_p = 0;
12139 return success_p;
12143 /* Get information about the tool-bar item at position X/Y on frame F.
12144 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12145 the current matrix of the tool-bar window of F, or NULL if not
12146 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12147 item in F->tool_bar_items. Value is
12149 -1 if X/Y is not on a tool-bar item
12150 0 if X/Y is on the same item that was highlighted before.
12151 1 otherwise. */
12153 static int
12154 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12155 int *hpos, int *vpos, int *prop_idx)
12157 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12158 struct window *w = XWINDOW (f->tool_bar_window);
12159 int area;
12161 /* Find the glyph under X/Y. */
12162 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12163 if (*glyph == NULL)
12164 return -1;
12166 /* Get the start of this tool-bar item's properties in
12167 f->tool_bar_items. */
12168 if (!tool_bar_item_info (f, *glyph, prop_idx))
12169 return -1;
12171 /* Is mouse on the highlighted item? */
12172 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12173 && *vpos >= hlinfo->mouse_face_beg_row
12174 && *vpos <= hlinfo->mouse_face_end_row
12175 && (*vpos > hlinfo->mouse_face_beg_row
12176 || *hpos >= hlinfo->mouse_face_beg_col)
12177 && (*vpos < hlinfo->mouse_face_end_row
12178 || *hpos < hlinfo->mouse_face_end_col
12179 || hlinfo->mouse_face_past_end))
12180 return 0;
12182 return 1;
12186 /* EXPORT:
12187 Handle mouse button event on the tool-bar of frame F, at
12188 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12189 0 for button release. MODIFIERS is event modifiers for button
12190 release. */
12192 void
12193 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12194 int modifiers)
12196 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12197 struct window *w = XWINDOW (f->tool_bar_window);
12198 int hpos, vpos, prop_idx;
12199 struct glyph *glyph;
12200 Lisp_Object enabled_p;
12201 int ts;
12203 /* If not on the highlighted tool-bar item, and mouse-highlight is
12204 non-nil, return. This is so we generate the tool-bar button
12205 click only when the mouse button is released on the same item as
12206 where it was pressed. However, when mouse-highlight is disabled,
12207 generate the click when the button is released regardless of the
12208 highlight, since tool-bar items are not highlighted in that
12209 case. */
12210 frame_to_window_pixel_xy (w, &x, &y);
12211 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12212 if (ts == -1
12213 || (ts != 0 && !NILP (Vmouse_highlight)))
12214 return;
12216 /* When mouse-highlight is off, generate the click for the item
12217 where the button was pressed, disregarding where it was
12218 released. */
12219 if (NILP (Vmouse_highlight) && !down_p)
12220 prop_idx = last_tool_bar_item;
12222 /* If item is disabled, do nothing. */
12223 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12224 if (NILP (enabled_p))
12225 return;
12227 if (down_p)
12229 /* Show item in pressed state. */
12230 if (!NILP (Vmouse_highlight))
12231 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12232 last_tool_bar_item = prop_idx;
12234 else
12236 Lisp_Object key, frame;
12237 struct input_event event;
12238 EVENT_INIT (event);
12240 /* Show item in released state. */
12241 if (!NILP (Vmouse_highlight))
12242 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12244 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12246 XSETFRAME (frame, f);
12247 event.kind = TOOL_BAR_EVENT;
12248 event.frame_or_window = frame;
12249 event.arg = frame;
12250 kbd_buffer_store_event (&event);
12252 event.kind = TOOL_BAR_EVENT;
12253 event.frame_or_window = frame;
12254 event.arg = key;
12255 event.modifiers = modifiers;
12256 kbd_buffer_store_event (&event);
12257 last_tool_bar_item = -1;
12262 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12263 tool-bar window-relative coordinates X/Y. Called from
12264 note_mouse_highlight. */
12266 static void
12267 note_tool_bar_highlight (struct frame *f, int x, int y)
12269 Lisp_Object window = f->tool_bar_window;
12270 struct window *w = XWINDOW (window);
12271 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12272 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12273 int hpos, vpos;
12274 struct glyph *glyph;
12275 struct glyph_row *row;
12276 int i;
12277 Lisp_Object enabled_p;
12278 int prop_idx;
12279 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12280 int mouse_down_p, rc;
12282 /* Function note_mouse_highlight is called with negative X/Y
12283 values when mouse moves outside of the frame. */
12284 if (x <= 0 || y <= 0)
12286 clear_mouse_face (hlinfo);
12287 return;
12290 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12291 if (rc < 0)
12293 /* Not on tool-bar item. */
12294 clear_mouse_face (hlinfo);
12295 return;
12297 else if (rc == 0)
12298 /* On same tool-bar item as before. */
12299 goto set_help_echo;
12301 clear_mouse_face (hlinfo);
12303 /* Mouse is down, but on different tool-bar item? */
12304 mouse_down_p = (dpyinfo->grabbed
12305 && f == last_mouse_frame
12306 && FRAME_LIVE_P (f));
12307 if (mouse_down_p
12308 && last_tool_bar_item != prop_idx)
12309 return;
12311 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12313 /* If tool-bar item is not enabled, don't highlight it. */
12314 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12315 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12317 /* Compute the x-position of the glyph. In front and past the
12318 image is a space. We include this in the highlighted area. */
12319 row = MATRIX_ROW (w->current_matrix, vpos);
12320 for (i = x = 0; i < hpos; ++i)
12321 x += row->glyphs[TEXT_AREA][i].pixel_width;
12323 /* Record this as the current active region. */
12324 hlinfo->mouse_face_beg_col = hpos;
12325 hlinfo->mouse_face_beg_row = vpos;
12326 hlinfo->mouse_face_beg_x = x;
12327 hlinfo->mouse_face_past_end = 0;
12329 hlinfo->mouse_face_end_col = hpos + 1;
12330 hlinfo->mouse_face_end_row = vpos;
12331 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12332 hlinfo->mouse_face_window = window;
12333 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12335 /* Display it as active. */
12336 show_mouse_face (hlinfo, draw);
12339 set_help_echo:
12341 /* Set help_echo_string to a help string to display for this tool-bar item.
12342 XTread_socket does the rest. */
12343 help_echo_object = help_echo_window = Qnil;
12344 help_echo_pos = -1;
12345 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12346 if (NILP (help_echo_string))
12347 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12350 #endif /* HAVE_WINDOW_SYSTEM */
12354 /************************************************************************
12355 Horizontal scrolling
12356 ************************************************************************/
12358 static int hscroll_window_tree (Lisp_Object);
12359 static int hscroll_windows (Lisp_Object);
12361 /* For all leaf windows in the window tree rooted at WINDOW, set their
12362 hscroll value so that PT is (i) visible in the window, and (ii) so
12363 that it is not within a certain margin at the window's left and
12364 right border. Value is non-zero if any window's hscroll has been
12365 changed. */
12367 static int
12368 hscroll_window_tree (Lisp_Object window)
12370 int hscrolled_p = 0;
12371 int hscroll_relative_p = FLOATP (Vhscroll_step);
12372 int hscroll_step_abs = 0;
12373 double hscroll_step_rel = 0;
12375 if (hscroll_relative_p)
12377 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12378 if (hscroll_step_rel < 0)
12380 hscroll_relative_p = 0;
12381 hscroll_step_abs = 0;
12384 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12386 hscroll_step_abs = XINT (Vhscroll_step);
12387 if (hscroll_step_abs < 0)
12388 hscroll_step_abs = 0;
12390 else
12391 hscroll_step_abs = 0;
12393 while (WINDOWP (window))
12395 struct window *w = XWINDOW (window);
12397 if (WINDOWP (w->contents))
12398 hscrolled_p |= hscroll_window_tree (w->contents);
12399 else if (w->cursor.vpos >= 0)
12401 int h_margin;
12402 int text_area_width;
12403 struct glyph_row *current_cursor_row
12404 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12405 struct glyph_row *desired_cursor_row
12406 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12407 struct glyph_row *cursor_row
12408 = (desired_cursor_row->enabled_p
12409 ? desired_cursor_row
12410 : current_cursor_row);
12411 int row_r2l_p = cursor_row->reversed_p;
12413 text_area_width = window_box_width (w, TEXT_AREA);
12415 /* Scroll when cursor is inside this scroll margin. */
12416 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12418 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12419 /* For left-to-right rows, hscroll when cursor is either
12420 (i) inside the right hscroll margin, or (ii) if it is
12421 inside the left margin and the window is already
12422 hscrolled. */
12423 && ((!row_r2l_p
12424 && ((w->hscroll
12425 && w->cursor.x <= h_margin)
12426 || (cursor_row->enabled_p
12427 && cursor_row->truncated_on_right_p
12428 && (w->cursor.x >= text_area_width - h_margin))))
12429 /* For right-to-left rows, the logic is similar,
12430 except that rules for scrolling to left and right
12431 are reversed. E.g., if cursor.x <= h_margin, we
12432 need to hscroll "to the right" unconditionally,
12433 and that will scroll the screen to the left so as
12434 to reveal the next portion of the row. */
12435 || (row_r2l_p
12436 && ((cursor_row->enabled_p
12437 /* FIXME: It is confusing to set the
12438 truncated_on_right_p flag when R2L rows
12439 are actually truncated on the left. */
12440 && cursor_row->truncated_on_right_p
12441 && w->cursor.x <= h_margin)
12442 || (w->hscroll
12443 && (w->cursor.x >= text_area_width - h_margin))))))
12445 struct it it;
12446 ptrdiff_t hscroll;
12447 struct buffer *saved_current_buffer;
12448 ptrdiff_t pt;
12449 int wanted_x;
12451 /* Find point in a display of infinite width. */
12452 saved_current_buffer = current_buffer;
12453 current_buffer = XBUFFER (w->contents);
12455 if (w == XWINDOW (selected_window))
12456 pt = PT;
12457 else
12458 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12460 /* Move iterator to pt starting at cursor_row->start in
12461 a line with infinite width. */
12462 init_to_row_start (&it, w, cursor_row);
12463 it.last_visible_x = INFINITY;
12464 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12465 current_buffer = saved_current_buffer;
12467 /* Position cursor in window. */
12468 if (!hscroll_relative_p && hscroll_step_abs == 0)
12469 hscroll = max (0, (it.current_x
12470 - (ITERATOR_AT_END_OF_LINE_P (&it)
12471 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12472 : (text_area_width / 2))))
12473 / FRAME_COLUMN_WIDTH (it.f);
12474 else if ((!row_r2l_p
12475 && w->cursor.x >= text_area_width - h_margin)
12476 || (row_r2l_p && w->cursor.x <= h_margin))
12478 if (hscroll_relative_p)
12479 wanted_x = text_area_width * (1 - hscroll_step_rel)
12480 - h_margin;
12481 else
12482 wanted_x = text_area_width
12483 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12484 - h_margin;
12485 hscroll
12486 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12488 else
12490 if (hscroll_relative_p)
12491 wanted_x = text_area_width * hscroll_step_rel
12492 + h_margin;
12493 else
12494 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12495 + h_margin;
12496 hscroll
12497 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12499 hscroll = max (hscroll, w->min_hscroll);
12501 /* Don't prevent redisplay optimizations if hscroll
12502 hasn't changed, as it will unnecessarily slow down
12503 redisplay. */
12504 if (w->hscroll != hscroll)
12506 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12507 w->hscroll = hscroll;
12508 hscrolled_p = 1;
12513 window = w->next;
12516 /* Value is non-zero if hscroll of any leaf window has been changed. */
12517 return hscrolled_p;
12521 /* Set hscroll so that cursor is visible and not inside horizontal
12522 scroll margins for all windows in the tree rooted at WINDOW. See
12523 also hscroll_window_tree above. Value is non-zero if any window's
12524 hscroll has been changed. If it has, desired matrices on the frame
12525 of WINDOW are cleared. */
12527 static int
12528 hscroll_windows (Lisp_Object window)
12530 int hscrolled_p = hscroll_window_tree (window);
12531 if (hscrolled_p)
12532 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12533 return hscrolled_p;
12538 /************************************************************************
12539 Redisplay
12540 ************************************************************************/
12542 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12543 to a non-zero value. This is sometimes handy to have in a debugger
12544 session. */
12546 #ifdef GLYPH_DEBUG
12548 /* First and last unchanged row for try_window_id. */
12550 static int debug_first_unchanged_at_end_vpos;
12551 static int debug_last_unchanged_at_beg_vpos;
12553 /* Delta vpos and y. */
12555 static int debug_dvpos, debug_dy;
12557 /* Delta in characters and bytes for try_window_id. */
12559 static ptrdiff_t debug_delta, debug_delta_bytes;
12561 /* Values of window_end_pos and window_end_vpos at the end of
12562 try_window_id. */
12564 static ptrdiff_t debug_end_vpos;
12566 /* Append a string to W->desired_matrix->method. FMT is a printf
12567 format string. If trace_redisplay_p is non-zero also printf the
12568 resulting string to stderr. */
12570 static void debug_method_add (struct window *, char const *, ...)
12571 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12573 static void
12574 debug_method_add (struct window *w, char const *fmt, ...)
12576 void *ptr = w;
12577 char *method = w->desired_matrix->method;
12578 int len = strlen (method);
12579 int size = sizeof w->desired_matrix->method;
12580 int remaining = size - len - 1;
12581 va_list ap;
12583 if (len && remaining)
12585 method[len] = '|';
12586 --remaining, ++len;
12589 va_start (ap, fmt);
12590 vsnprintf (method + len, remaining + 1, fmt, ap);
12591 va_end (ap);
12593 if (trace_redisplay_p)
12594 fprintf (stderr, "%p (%s): %s\n",
12595 ptr,
12596 ((BUFFERP (w->contents)
12597 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12598 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12599 : "no buffer"),
12600 method + len);
12603 #endif /* GLYPH_DEBUG */
12606 /* Value is non-zero if all changes in window W, which displays
12607 current_buffer, are in the text between START and END. START is a
12608 buffer position, END is given as a distance from Z. Used in
12609 redisplay_internal for display optimization. */
12611 static int
12612 text_outside_line_unchanged_p (struct window *w,
12613 ptrdiff_t start, ptrdiff_t end)
12615 int unchanged_p = 1;
12617 /* If text or overlays have changed, see where. */
12618 if (window_outdated (w))
12620 /* Gap in the line? */
12621 if (GPT < start || Z - GPT < end)
12622 unchanged_p = 0;
12624 /* Changes start in front of the line, or end after it? */
12625 if (unchanged_p
12626 && (BEG_UNCHANGED < start - 1
12627 || END_UNCHANGED < end))
12628 unchanged_p = 0;
12630 /* If selective display, can't optimize if changes start at the
12631 beginning of the line. */
12632 if (unchanged_p
12633 && INTEGERP (BVAR (current_buffer, selective_display))
12634 && XINT (BVAR (current_buffer, selective_display)) > 0
12635 && (BEG_UNCHANGED < start || GPT <= start))
12636 unchanged_p = 0;
12638 /* If there are overlays at the start or end of the line, these
12639 may have overlay strings with newlines in them. A change at
12640 START, for instance, may actually concern the display of such
12641 overlay strings as well, and they are displayed on different
12642 lines. So, quickly rule out this case. (For the future, it
12643 might be desirable to implement something more telling than
12644 just BEG/END_UNCHANGED.) */
12645 if (unchanged_p)
12647 if (BEG + BEG_UNCHANGED == start
12648 && overlay_touches_p (start))
12649 unchanged_p = 0;
12650 if (END_UNCHANGED == end
12651 && overlay_touches_p (Z - end))
12652 unchanged_p = 0;
12655 /* Under bidi reordering, adding or deleting a character in the
12656 beginning of a paragraph, before the first strong directional
12657 character, can change the base direction of the paragraph (unless
12658 the buffer specifies a fixed paragraph direction), which will
12659 require to redisplay the whole paragraph. It might be worthwhile
12660 to find the paragraph limits and widen the range of redisplayed
12661 lines to that, but for now just give up this optimization. */
12662 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12663 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12664 unchanged_p = 0;
12667 return unchanged_p;
12671 /* Do a frame update, taking possible shortcuts into account. This is
12672 the main external entry point for redisplay.
12674 If the last redisplay displayed an echo area message and that message
12675 is no longer requested, we clear the echo area or bring back the
12676 mini-buffer if that is in use. */
12678 void
12679 redisplay (void)
12681 redisplay_internal ();
12685 static Lisp_Object
12686 overlay_arrow_string_or_property (Lisp_Object var)
12688 Lisp_Object val;
12690 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12691 return val;
12693 return Voverlay_arrow_string;
12696 /* Return 1 if there are any overlay-arrows in current_buffer. */
12697 static int
12698 overlay_arrow_in_current_buffer_p (void)
12700 Lisp_Object vlist;
12702 for (vlist = Voverlay_arrow_variable_list;
12703 CONSP (vlist);
12704 vlist = XCDR (vlist))
12706 Lisp_Object var = XCAR (vlist);
12707 Lisp_Object val;
12709 if (!SYMBOLP (var))
12710 continue;
12711 val = find_symbol_value (var);
12712 if (MARKERP (val)
12713 && current_buffer == XMARKER (val)->buffer)
12714 return 1;
12716 return 0;
12720 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12721 has changed. */
12723 static int
12724 overlay_arrows_changed_p (void)
12726 Lisp_Object vlist;
12728 for (vlist = Voverlay_arrow_variable_list;
12729 CONSP (vlist);
12730 vlist = XCDR (vlist))
12732 Lisp_Object var = XCAR (vlist);
12733 Lisp_Object val, pstr;
12735 if (!SYMBOLP (var))
12736 continue;
12737 val = find_symbol_value (var);
12738 if (!MARKERP (val))
12739 continue;
12740 if (! EQ (COERCE_MARKER (val),
12741 Fget (var, Qlast_arrow_position))
12742 || ! (pstr = overlay_arrow_string_or_property (var),
12743 EQ (pstr, Fget (var, Qlast_arrow_string))))
12744 return 1;
12746 return 0;
12749 /* Mark overlay arrows to be updated on next redisplay. */
12751 static void
12752 update_overlay_arrows (int up_to_date)
12754 Lisp_Object vlist;
12756 for (vlist = Voverlay_arrow_variable_list;
12757 CONSP (vlist);
12758 vlist = XCDR (vlist))
12760 Lisp_Object var = XCAR (vlist);
12762 if (!SYMBOLP (var))
12763 continue;
12765 if (up_to_date > 0)
12767 Lisp_Object val = find_symbol_value (var);
12768 Fput (var, Qlast_arrow_position,
12769 COERCE_MARKER (val));
12770 Fput (var, Qlast_arrow_string,
12771 overlay_arrow_string_or_property (var));
12773 else if (up_to_date < 0
12774 || !NILP (Fget (var, Qlast_arrow_position)))
12776 Fput (var, Qlast_arrow_position, Qt);
12777 Fput (var, Qlast_arrow_string, Qt);
12783 /* Return overlay arrow string to display at row.
12784 Return integer (bitmap number) for arrow bitmap in left fringe.
12785 Return nil if no overlay arrow. */
12787 static Lisp_Object
12788 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12790 Lisp_Object vlist;
12792 for (vlist = Voverlay_arrow_variable_list;
12793 CONSP (vlist);
12794 vlist = XCDR (vlist))
12796 Lisp_Object var = XCAR (vlist);
12797 Lisp_Object val;
12799 if (!SYMBOLP (var))
12800 continue;
12802 val = find_symbol_value (var);
12804 if (MARKERP (val)
12805 && current_buffer == XMARKER (val)->buffer
12806 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12808 if (FRAME_WINDOW_P (it->f)
12809 /* FIXME: if ROW->reversed_p is set, this should test
12810 the right fringe, not the left one. */
12811 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12813 #ifdef HAVE_WINDOW_SYSTEM
12814 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12816 int fringe_bitmap;
12817 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12818 return make_number (fringe_bitmap);
12820 #endif
12821 return make_number (-1); /* Use default arrow bitmap. */
12823 return overlay_arrow_string_or_property (var);
12827 return Qnil;
12830 /* Return 1 if point moved out of or into a composition. Otherwise
12831 return 0. PREV_BUF and PREV_PT are the last point buffer and
12832 position. BUF and PT are the current point buffer and position. */
12834 static int
12835 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12836 struct buffer *buf, ptrdiff_t pt)
12838 ptrdiff_t start, end;
12839 Lisp_Object prop;
12840 Lisp_Object buffer;
12842 XSETBUFFER (buffer, buf);
12843 /* Check a composition at the last point if point moved within the
12844 same buffer. */
12845 if (prev_buf == buf)
12847 if (prev_pt == pt)
12848 /* Point didn't move. */
12849 return 0;
12851 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12852 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12853 && composition_valid_p (start, end, prop)
12854 && start < prev_pt && end > prev_pt)
12855 /* The last point was within the composition. Return 1 iff
12856 point moved out of the composition. */
12857 return (pt <= start || pt >= end);
12860 /* Check a composition at the current point. */
12861 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12862 && find_composition (pt, -1, &start, &end, &prop, buffer)
12863 && composition_valid_p (start, end, prop)
12864 && start < pt && end > pt);
12867 /* Reconsider the clip changes of buffer which is displayed in W. */
12869 static void
12870 reconsider_clip_changes (struct window *w)
12872 struct buffer *b = XBUFFER (w->contents);
12874 if (b->clip_changed
12875 && w->window_end_valid
12876 && w->current_matrix->buffer == b
12877 && w->current_matrix->zv == BUF_ZV (b)
12878 && w->current_matrix->begv == BUF_BEGV (b))
12879 b->clip_changed = 0;
12881 /* If display wasn't paused, and W is not a tool bar window, see if
12882 point has been moved into or out of a composition. In that case,
12883 we set b->clip_changed to 1 to force updating the screen. If
12884 b->clip_changed has already been set to 1, we can skip this
12885 check. */
12886 if (!b->clip_changed && w->window_end_valid)
12888 ptrdiff_t pt = (w == XWINDOW (selected_window)
12889 ? PT : marker_position (w->pointm));
12891 if ((w->current_matrix->buffer != b || pt != w->last_point)
12892 && check_point_in_composition (w->current_matrix->buffer,
12893 w->last_point, b, pt))
12894 b->clip_changed = 1;
12898 #define STOP_POLLING \
12899 do { if (! polling_stopped_here) stop_polling (); \
12900 polling_stopped_here = 1; } while (0)
12902 #define RESUME_POLLING \
12903 do { if (polling_stopped_here) start_polling (); \
12904 polling_stopped_here = 0; } while (0)
12907 /* Perhaps in the future avoid recentering windows if it
12908 is not necessary; currently that causes some problems. */
12910 static void
12911 redisplay_internal (void)
12913 struct window *w = XWINDOW (selected_window);
12914 struct window *sw;
12915 struct frame *fr;
12916 int pending;
12917 bool must_finish = 0, match_p;
12918 struct text_pos tlbufpos, tlendpos;
12919 int number_of_visible_frames;
12920 ptrdiff_t count;
12921 struct frame *sf;
12922 int polling_stopped_here = 0;
12923 Lisp_Object tail, frame;
12925 /* Non-zero means redisplay has to consider all windows on all
12926 frames. Zero means, only selected_window is considered. */
12927 int consider_all_windows_p;
12929 /* Non-zero means redisplay has to redisplay the miniwindow. */
12930 int update_miniwindow_p = 0;
12932 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12934 /* No redisplay if running in batch mode or frame is not yet fully
12935 initialized, or redisplay is explicitly turned off by setting
12936 Vinhibit_redisplay. */
12937 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12938 || !NILP (Vinhibit_redisplay))
12939 return;
12941 /* Don't examine these until after testing Vinhibit_redisplay.
12942 When Emacs is shutting down, perhaps because its connection to
12943 X has dropped, we should not look at them at all. */
12944 fr = XFRAME (w->frame);
12945 sf = SELECTED_FRAME ();
12947 if (!fr->glyphs_initialized_p)
12948 return;
12950 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12951 if (popup_activated ())
12952 return;
12953 #endif
12955 /* I don't think this happens but let's be paranoid. */
12956 if (redisplaying_p)
12957 return;
12959 /* Record a function that clears redisplaying_p
12960 when we leave this function. */
12961 count = SPECPDL_INDEX ();
12962 record_unwind_protect_void (unwind_redisplay);
12963 redisplaying_p = 1;
12964 specbind (Qinhibit_free_realized_faces, Qnil);
12966 /* Record this function, so it appears on the profiler's backtraces. */
12967 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12969 FOR_EACH_FRAME (tail, frame)
12970 XFRAME (frame)->already_hscrolled_p = 0;
12972 retry:
12973 /* Remember the currently selected window. */
12974 sw = w;
12976 pending = 0;
12977 last_escape_glyph_frame = NULL;
12978 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12979 last_glyphless_glyph_frame = NULL;
12980 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12982 /* If face_change_count is non-zero, init_iterator will free all
12983 realized faces, which includes the faces referenced from current
12984 matrices. So, we can't reuse current matrices in this case. */
12985 if (face_change_count)
12986 ++windows_or_buffers_changed;
12988 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12989 && FRAME_TTY (sf)->previous_frame != sf)
12991 /* Since frames on a single ASCII terminal share the same
12992 display area, displaying a different frame means redisplay
12993 the whole thing. */
12994 windows_or_buffers_changed++;
12995 SET_FRAME_GARBAGED (sf);
12996 #ifndef DOS_NT
12997 set_tty_color_mode (FRAME_TTY (sf), sf);
12998 #endif
12999 FRAME_TTY (sf)->previous_frame = sf;
13002 /* Set the visible flags for all frames. Do this before checking for
13003 resized or garbaged frames; they want to know if their frames are
13004 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13005 number_of_visible_frames = 0;
13007 FOR_EACH_FRAME (tail, frame)
13009 struct frame *f = XFRAME (frame);
13011 if (FRAME_VISIBLE_P (f))
13013 ++number_of_visible_frames;
13014 /* Adjust matrices for visible frames only. */
13015 if (f->fonts_changed)
13017 adjust_frame_glyphs (f);
13018 f->fonts_changed = 0;
13020 /* If cursor type has been changed on the frame
13021 other than selected, consider all frames. */
13022 if (f != sf && f->cursor_type_changed)
13023 update_mode_lines++;
13025 clear_desired_matrices (f);
13028 /* Notice any pending interrupt request to change frame size. */
13029 do_pending_window_change (1);
13031 /* do_pending_window_change could change the selected_window due to
13032 frame resizing which makes the selected window too small. */
13033 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13034 sw = w;
13036 /* Clear frames marked as garbaged. */
13037 clear_garbaged_frames ();
13039 /* Build menubar and tool-bar items. */
13040 if (NILP (Vmemory_full))
13041 prepare_menu_bars ();
13043 if (windows_or_buffers_changed)
13044 update_mode_lines++;
13046 reconsider_clip_changes (w);
13048 /* In most cases selected window displays current buffer. */
13049 match_p = XBUFFER (w->contents) == current_buffer;
13050 if (match_p)
13052 ptrdiff_t count1;
13054 /* Detect case that we need to write or remove a star in the mode line. */
13055 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13057 w->update_mode_line = 1;
13058 if (buffer_shared_and_changed ())
13059 update_mode_lines++;
13062 /* Avoid invocation of point motion hooks by `current_column' below. */
13063 count1 = SPECPDL_INDEX ();
13064 specbind (Qinhibit_point_motion_hooks, Qt);
13066 if (mode_line_update_needed (w))
13067 w->update_mode_line = 1;
13069 unbind_to (count1, Qnil);
13072 consider_all_windows_p = (update_mode_lines
13073 || buffer_shared_and_changed ());
13075 /* If specs for an arrow have changed, do thorough redisplay
13076 to ensure we remove any arrow that should no longer exist. */
13077 if (overlay_arrows_changed_p ())
13078 consider_all_windows_p = windows_or_buffers_changed = 1;
13080 /* Normally the message* functions will have already displayed and
13081 updated the echo area, but the frame may have been trashed, or
13082 the update may have been preempted, so display the echo area
13083 again here. Checking message_cleared_p captures the case that
13084 the echo area should be cleared. */
13085 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13086 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13087 || (message_cleared_p
13088 && minibuf_level == 0
13089 /* If the mini-window is currently selected, this means the
13090 echo-area doesn't show through. */
13091 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13093 int window_height_changed_p = echo_area_display (0);
13095 if (message_cleared_p)
13096 update_miniwindow_p = 1;
13098 must_finish = 1;
13100 /* If we don't display the current message, don't clear the
13101 message_cleared_p flag, because, if we did, we wouldn't clear
13102 the echo area in the next redisplay which doesn't preserve
13103 the echo area. */
13104 if (!display_last_displayed_message_p)
13105 message_cleared_p = 0;
13107 if (window_height_changed_p)
13109 consider_all_windows_p = 1;
13110 ++update_mode_lines;
13111 ++windows_or_buffers_changed;
13113 /* If window configuration was changed, frames may have been
13114 marked garbaged. Clear them or we will experience
13115 surprises wrt scrolling. */
13116 clear_garbaged_frames ();
13119 else if (EQ (selected_window, minibuf_window)
13120 && (current_buffer->clip_changed || window_outdated (w))
13121 && resize_mini_window (w, 0))
13123 /* Resized active mini-window to fit the size of what it is
13124 showing if its contents might have changed. */
13125 must_finish = 1;
13126 /* FIXME: this causes all frames to be updated, which seems unnecessary
13127 since only the current frame needs to be considered. This function
13128 needs to be rewritten with two variables, consider_all_windows and
13129 consider_all_frames. */
13130 consider_all_windows_p = 1;
13131 ++windows_or_buffers_changed;
13132 ++update_mode_lines;
13134 /* If window configuration was changed, frames may have been
13135 marked garbaged. Clear them or we will experience
13136 surprises wrt scrolling. */
13137 clear_garbaged_frames ();
13140 /* If showing the region, and mark has changed, we must redisplay
13141 the whole window. The assignment to this_line_start_pos prevents
13142 the optimization directly below this if-statement. */
13143 if (((!NILP (Vtransient_mark_mode)
13144 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13145 != (w->region_showing > 0))
13146 || (w->region_showing
13147 && w->region_showing
13148 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13149 CHARPOS (this_line_start_pos) = 0;
13151 /* Optimize the case that only the line containing the cursor in the
13152 selected window has changed. Variables starting with this_ are
13153 set in display_line and record information about the line
13154 containing the cursor. */
13155 tlbufpos = this_line_start_pos;
13156 tlendpos = this_line_end_pos;
13157 if (!consider_all_windows_p
13158 && CHARPOS (tlbufpos) > 0
13159 && !w->update_mode_line
13160 && !current_buffer->clip_changed
13161 && !current_buffer->prevent_redisplay_optimizations_p
13162 && FRAME_VISIBLE_P (XFRAME (w->frame))
13163 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13164 && !XFRAME (w->frame)->cursor_type_changed
13165 /* Make sure recorded data applies to current buffer, etc. */
13166 && this_line_buffer == current_buffer
13167 && match_p
13168 && !w->force_start
13169 && !w->optional_new_start
13170 /* Point must be on the line that we have info recorded about. */
13171 && PT >= CHARPOS (tlbufpos)
13172 && PT <= Z - CHARPOS (tlendpos)
13173 /* All text outside that line, including its final newline,
13174 must be unchanged. */
13175 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13176 CHARPOS (tlendpos)))
13178 if (CHARPOS (tlbufpos) > BEGV
13179 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13180 && (CHARPOS (tlbufpos) == ZV
13181 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13182 /* Former continuation line has disappeared by becoming empty. */
13183 goto cancel;
13184 else if (window_outdated (w) || MINI_WINDOW_P (w))
13186 /* We have to handle the case of continuation around a
13187 wide-column character (see the comment in indent.c around
13188 line 1340).
13190 For instance, in the following case:
13192 -------- Insert --------
13193 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13194 J_I_ ==> J_I_ `^^' are cursors.
13195 ^^ ^^
13196 -------- --------
13198 As we have to redraw the line above, we cannot use this
13199 optimization. */
13201 struct it it;
13202 int line_height_before = this_line_pixel_height;
13204 /* Note that start_display will handle the case that the
13205 line starting at tlbufpos is a continuation line. */
13206 start_display (&it, w, tlbufpos);
13208 /* Implementation note: It this still necessary? */
13209 if (it.current_x != this_line_start_x)
13210 goto cancel;
13212 TRACE ((stderr, "trying display optimization 1\n"));
13213 w->cursor.vpos = -1;
13214 overlay_arrow_seen = 0;
13215 it.vpos = this_line_vpos;
13216 it.current_y = this_line_y;
13217 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13218 display_line (&it);
13220 /* If line contains point, is not continued,
13221 and ends at same distance from eob as before, we win. */
13222 if (w->cursor.vpos >= 0
13223 /* Line is not continued, otherwise this_line_start_pos
13224 would have been set to 0 in display_line. */
13225 && CHARPOS (this_line_start_pos)
13226 /* Line ends as before. */
13227 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13228 /* Line has same height as before. Otherwise other lines
13229 would have to be shifted up or down. */
13230 && this_line_pixel_height == line_height_before)
13232 /* If this is not the window's last line, we must adjust
13233 the charstarts of the lines below. */
13234 if (it.current_y < it.last_visible_y)
13236 struct glyph_row *row
13237 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13238 ptrdiff_t delta, delta_bytes;
13240 /* We used to distinguish between two cases here,
13241 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13242 when the line ends in a newline or the end of the
13243 buffer's accessible portion. But both cases did
13244 the same, so they were collapsed. */
13245 delta = (Z
13246 - CHARPOS (tlendpos)
13247 - MATRIX_ROW_START_CHARPOS (row));
13248 delta_bytes = (Z_BYTE
13249 - BYTEPOS (tlendpos)
13250 - MATRIX_ROW_START_BYTEPOS (row));
13252 increment_matrix_positions (w->current_matrix,
13253 this_line_vpos + 1,
13254 w->current_matrix->nrows,
13255 delta, delta_bytes);
13258 /* If this row displays text now but previously didn't,
13259 or vice versa, w->window_end_vpos may have to be
13260 adjusted. */
13261 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13263 if (w->window_end_vpos < this_line_vpos)
13264 w->window_end_vpos = this_line_vpos;
13266 else if (w->window_end_vpos == this_line_vpos
13267 && this_line_vpos > 0)
13268 w->window_end_vpos = this_line_vpos - 1;
13269 w->window_end_valid = 0;
13271 /* Update hint: No need to try to scroll in update_window. */
13272 w->desired_matrix->no_scrolling_p = 1;
13274 #ifdef GLYPH_DEBUG
13275 *w->desired_matrix->method = 0;
13276 debug_method_add (w, "optimization 1");
13277 #endif
13278 #ifdef HAVE_WINDOW_SYSTEM
13279 update_window_fringes (w, 0);
13280 #endif
13281 goto update;
13283 else
13284 goto cancel;
13286 else if (/* Cursor position hasn't changed. */
13287 PT == w->last_point
13288 /* Make sure the cursor was last displayed
13289 in this window. Otherwise we have to reposition it. */
13290 && 0 <= w->cursor.vpos
13291 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13293 if (!must_finish)
13295 do_pending_window_change (1);
13296 /* If selected_window changed, redisplay again. */
13297 if (WINDOWP (selected_window)
13298 && (w = XWINDOW (selected_window)) != sw)
13299 goto retry;
13301 /* We used to always goto end_of_redisplay here, but this
13302 isn't enough if we have a blinking cursor. */
13303 if (w->cursor_off_p == w->last_cursor_off_p)
13304 goto end_of_redisplay;
13306 goto update;
13308 /* If highlighting the region, or if the cursor is in the echo area,
13309 then we can't just move the cursor. */
13310 else if (! (!NILP (Vtransient_mark_mode)
13311 && !NILP (BVAR (current_buffer, mark_active)))
13312 && (EQ (selected_window,
13313 BVAR (current_buffer, last_selected_window))
13314 || highlight_nonselected_windows)
13315 && !w->region_showing
13316 && NILP (Vshow_trailing_whitespace)
13317 && !cursor_in_echo_area)
13319 struct it it;
13320 struct glyph_row *row;
13322 /* Skip from tlbufpos to PT and see where it is. Note that
13323 PT may be in invisible text. If so, we will end at the
13324 next visible position. */
13325 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13326 NULL, DEFAULT_FACE_ID);
13327 it.current_x = this_line_start_x;
13328 it.current_y = this_line_y;
13329 it.vpos = this_line_vpos;
13331 /* The call to move_it_to stops in front of PT, but
13332 moves over before-strings. */
13333 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13335 if (it.vpos == this_line_vpos
13336 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13337 row->enabled_p))
13339 eassert (this_line_vpos == it.vpos);
13340 eassert (this_line_y == it.current_y);
13341 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13342 #ifdef GLYPH_DEBUG
13343 *w->desired_matrix->method = 0;
13344 debug_method_add (w, "optimization 3");
13345 #endif
13346 goto update;
13348 else
13349 goto cancel;
13352 cancel:
13353 /* Text changed drastically or point moved off of line. */
13354 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13357 CHARPOS (this_line_start_pos) = 0;
13358 consider_all_windows_p |= buffer_shared_and_changed ();
13359 ++clear_face_cache_count;
13360 #ifdef HAVE_WINDOW_SYSTEM
13361 ++clear_image_cache_count;
13362 #endif
13364 /* Build desired matrices, and update the display. If
13365 consider_all_windows_p is non-zero, do it for all windows on all
13366 frames. Otherwise do it for selected_window, only. */
13368 if (consider_all_windows_p)
13370 FOR_EACH_FRAME (tail, frame)
13371 XFRAME (frame)->updated_p = 0;
13373 FOR_EACH_FRAME (tail, frame)
13375 struct frame *f = XFRAME (frame);
13377 /* We don't have to do anything for unselected terminal
13378 frames. */
13379 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13380 && !EQ (FRAME_TTY (f)->top_frame, frame))
13381 continue;
13383 retry_frame:
13385 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13387 /* Mark all the scroll bars to be removed; we'll redeem
13388 the ones we want when we redisplay their windows. */
13389 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13390 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13392 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13393 redisplay_windows (FRAME_ROOT_WINDOW (f));
13395 /* The X error handler may have deleted that frame. */
13396 if (!FRAME_LIVE_P (f))
13397 continue;
13399 /* Any scroll bars which redisplay_windows should have
13400 nuked should now go away. */
13401 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13402 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13404 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13406 /* If fonts changed on visible frame, display again. */
13407 if (f->fonts_changed)
13409 adjust_frame_glyphs (f);
13410 f->fonts_changed = 0;
13411 goto retry_frame;
13414 /* See if we have to hscroll. */
13415 if (!f->already_hscrolled_p)
13417 f->already_hscrolled_p = 1;
13418 if (hscroll_windows (f->root_window))
13419 goto retry_frame;
13422 /* Prevent various kinds of signals during display
13423 update. stdio is not robust about handling
13424 signals, which can cause an apparent I/O
13425 error. */
13426 if (interrupt_input)
13427 unrequest_sigio ();
13428 STOP_POLLING;
13430 /* Update the display. */
13431 set_window_update_flags (XWINDOW (f->root_window), 1);
13432 pending |= update_frame (f, 0, 0);
13433 f->cursor_type_changed = 0;
13434 f->updated_p = 1;
13439 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13441 if (!pending)
13443 /* Do the mark_window_display_accurate after all windows have
13444 been redisplayed because this call resets flags in buffers
13445 which are needed for proper redisplay. */
13446 FOR_EACH_FRAME (tail, frame)
13448 struct frame *f = XFRAME (frame);
13449 if (f->updated_p)
13451 mark_window_display_accurate (f->root_window, 1);
13452 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13453 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13458 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13460 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13461 struct frame *mini_frame;
13463 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13464 /* Use list_of_error, not Qerror, so that
13465 we catch only errors and don't run the debugger. */
13466 internal_condition_case_1 (redisplay_window_1, selected_window,
13467 list_of_error,
13468 redisplay_window_error);
13469 if (update_miniwindow_p)
13470 internal_condition_case_1 (redisplay_window_1, mini_window,
13471 list_of_error,
13472 redisplay_window_error);
13474 /* Compare desired and current matrices, perform output. */
13476 update:
13477 /* If fonts changed, display again. */
13478 if (sf->fonts_changed)
13479 goto retry;
13481 /* Prevent various kinds of signals during display update.
13482 stdio is not robust about handling signals,
13483 which can cause an apparent I/O error. */
13484 if (interrupt_input)
13485 unrequest_sigio ();
13486 STOP_POLLING;
13488 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13490 if (hscroll_windows (selected_window))
13491 goto retry;
13493 XWINDOW (selected_window)->must_be_updated_p = 1;
13494 pending = update_frame (sf, 0, 0);
13495 sf->cursor_type_changed = 0;
13498 /* We may have called echo_area_display at the top of this
13499 function. If the echo area is on another frame, that may
13500 have put text on a frame other than the selected one, so the
13501 above call to update_frame would not have caught it. Catch
13502 it here. */
13503 mini_window = FRAME_MINIBUF_WINDOW (sf);
13504 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13506 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13508 XWINDOW (mini_window)->must_be_updated_p = 1;
13509 pending |= update_frame (mini_frame, 0, 0);
13510 mini_frame->cursor_type_changed = 0;
13511 if (!pending && hscroll_windows (mini_window))
13512 goto retry;
13516 /* If display was paused because of pending input, make sure we do a
13517 thorough update the next time. */
13518 if (pending)
13520 /* Prevent the optimization at the beginning of
13521 redisplay_internal that tries a single-line update of the
13522 line containing the cursor in the selected window. */
13523 CHARPOS (this_line_start_pos) = 0;
13525 /* Let the overlay arrow be updated the next time. */
13526 update_overlay_arrows (0);
13528 /* If we pause after scrolling, some rows in the current
13529 matrices of some windows are not valid. */
13530 if (!WINDOW_FULL_WIDTH_P (w)
13531 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13532 update_mode_lines = 1;
13534 else
13536 if (!consider_all_windows_p)
13538 /* This has already been done above if
13539 consider_all_windows_p is set. */
13540 mark_window_display_accurate_1 (w, 1);
13542 /* Say overlay arrows are up to date. */
13543 update_overlay_arrows (1);
13545 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13546 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13549 update_mode_lines = 0;
13550 windows_or_buffers_changed = 0;
13553 /* Start SIGIO interrupts coming again. Having them off during the
13554 code above makes it less likely one will discard output, but not
13555 impossible, since there might be stuff in the system buffer here.
13556 But it is much hairier to try to do anything about that. */
13557 if (interrupt_input)
13558 request_sigio ();
13559 RESUME_POLLING;
13561 /* If a frame has become visible which was not before, redisplay
13562 again, so that we display it. Expose events for such a frame
13563 (which it gets when becoming visible) don't call the parts of
13564 redisplay constructing glyphs, so simply exposing a frame won't
13565 display anything in this case. So, we have to display these
13566 frames here explicitly. */
13567 if (!pending)
13569 int new_count = 0;
13571 FOR_EACH_FRAME (tail, frame)
13573 int this_is_visible = 0;
13575 if (XFRAME (frame)->visible)
13576 this_is_visible = 1;
13578 if (this_is_visible)
13579 new_count++;
13582 if (new_count != number_of_visible_frames)
13583 windows_or_buffers_changed++;
13586 /* Change frame size now if a change is pending. */
13587 do_pending_window_change (1);
13589 /* If we just did a pending size change, or have additional
13590 visible frames, or selected_window changed, redisplay again. */
13591 if ((windows_or_buffers_changed && !pending)
13592 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13593 goto retry;
13595 /* Clear the face and image caches.
13597 We used to do this only if consider_all_windows_p. But the cache
13598 needs to be cleared if a timer creates images in the current
13599 buffer (e.g. the test case in Bug#6230). */
13601 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13603 clear_face_cache (0);
13604 clear_face_cache_count = 0;
13607 #ifdef HAVE_WINDOW_SYSTEM
13608 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13610 clear_image_caches (Qnil);
13611 clear_image_cache_count = 0;
13613 #endif /* HAVE_WINDOW_SYSTEM */
13615 end_of_redisplay:
13616 unbind_to (count, Qnil);
13617 RESUME_POLLING;
13621 /* Redisplay, but leave alone any recent echo area message unless
13622 another message has been requested in its place.
13624 This is useful in situations where you need to redisplay but no
13625 user action has occurred, making it inappropriate for the message
13626 area to be cleared. See tracking_off and
13627 wait_reading_process_output for examples of these situations.
13629 FROM_WHERE is an integer saying from where this function was
13630 called. This is useful for debugging. */
13632 void
13633 redisplay_preserve_echo_area (int from_where)
13635 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13637 if (!NILP (echo_area_buffer[1]))
13639 /* We have a previously displayed message, but no current
13640 message. Redisplay the previous message. */
13641 display_last_displayed_message_p = 1;
13642 redisplay_internal ();
13643 display_last_displayed_message_p = 0;
13645 else
13646 redisplay_internal ();
13648 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13649 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13650 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13654 /* Function registered with record_unwind_protect in redisplay_internal. */
13656 static void
13657 unwind_redisplay (void)
13659 redisplaying_p = 0;
13663 /* Mark the display of leaf window W as accurate or inaccurate.
13664 If ACCURATE_P is non-zero mark display of W as accurate. If
13665 ACCURATE_P is zero, arrange for W to be redisplayed the next
13666 time redisplay_internal is called. */
13668 static void
13669 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13671 struct buffer *b = XBUFFER (w->contents);
13673 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13674 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13675 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13677 if (accurate_p)
13679 b->clip_changed = 0;
13680 b->prevent_redisplay_optimizations_p = 0;
13682 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13683 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13684 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13685 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13687 w->current_matrix->buffer = b;
13688 w->current_matrix->begv = BUF_BEGV (b);
13689 w->current_matrix->zv = BUF_ZV (b);
13691 w->last_cursor_vpos = w->cursor.vpos;
13692 w->last_cursor_off_p = w->cursor_off_p;
13694 if (w == XWINDOW (selected_window))
13695 w->last_point = BUF_PT (b);
13696 else
13697 w->last_point = marker_position (w->pointm);
13699 w->window_end_valid = 1;
13700 w->update_mode_line = 0;
13705 /* Mark the display of windows in the window tree rooted at WINDOW as
13706 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13707 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13708 be redisplayed the next time redisplay_internal is called. */
13710 void
13711 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13713 struct window *w;
13715 for (; !NILP (window); window = w->next)
13717 w = XWINDOW (window);
13718 if (WINDOWP (w->contents))
13719 mark_window_display_accurate (w->contents, accurate_p);
13720 else
13721 mark_window_display_accurate_1 (w, accurate_p);
13724 if (accurate_p)
13725 update_overlay_arrows (1);
13726 else
13727 /* Force a thorough redisplay the next time by setting
13728 last_arrow_position and last_arrow_string to t, which is
13729 unequal to any useful value of Voverlay_arrow_... */
13730 update_overlay_arrows (-1);
13734 /* Return value in display table DP (Lisp_Char_Table *) for character
13735 C. Since a display table doesn't have any parent, we don't have to
13736 follow parent. Do not call this function directly but use the
13737 macro DISP_CHAR_VECTOR. */
13739 Lisp_Object
13740 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13742 Lisp_Object val;
13744 if (ASCII_CHAR_P (c))
13746 val = dp->ascii;
13747 if (SUB_CHAR_TABLE_P (val))
13748 val = XSUB_CHAR_TABLE (val)->contents[c];
13750 else
13752 Lisp_Object table;
13754 XSETCHAR_TABLE (table, dp);
13755 val = char_table_ref (table, c);
13757 if (NILP (val))
13758 val = dp->defalt;
13759 return val;
13764 /***********************************************************************
13765 Window Redisplay
13766 ***********************************************************************/
13768 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13770 static void
13771 redisplay_windows (Lisp_Object window)
13773 while (!NILP (window))
13775 struct window *w = XWINDOW (window);
13777 if (WINDOWP (w->contents))
13778 redisplay_windows (w->contents);
13779 else if (BUFFERP (w->contents))
13781 displayed_buffer = XBUFFER (w->contents);
13782 /* Use list_of_error, not Qerror, so that
13783 we catch only errors and don't run the debugger. */
13784 internal_condition_case_1 (redisplay_window_0, window,
13785 list_of_error,
13786 redisplay_window_error);
13789 window = w->next;
13793 static Lisp_Object
13794 redisplay_window_error (Lisp_Object ignore)
13796 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13797 return Qnil;
13800 static Lisp_Object
13801 redisplay_window_0 (Lisp_Object window)
13803 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13804 redisplay_window (window, 0);
13805 return Qnil;
13808 static Lisp_Object
13809 redisplay_window_1 (Lisp_Object window)
13811 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13812 redisplay_window (window, 1);
13813 return Qnil;
13817 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13818 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13819 which positions recorded in ROW differ from current buffer
13820 positions.
13822 Return 0 if cursor is not on this row, 1 otherwise. */
13824 static int
13825 set_cursor_from_row (struct window *w, struct glyph_row *row,
13826 struct glyph_matrix *matrix,
13827 ptrdiff_t delta, ptrdiff_t delta_bytes,
13828 int dy, int dvpos)
13830 struct glyph *glyph = row->glyphs[TEXT_AREA];
13831 struct glyph *end = glyph + row->used[TEXT_AREA];
13832 struct glyph *cursor = NULL;
13833 /* The last known character position in row. */
13834 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13835 int x = row->x;
13836 ptrdiff_t pt_old = PT - delta;
13837 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13838 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13839 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13840 /* A glyph beyond the edge of TEXT_AREA which we should never
13841 touch. */
13842 struct glyph *glyphs_end = end;
13843 /* Non-zero means we've found a match for cursor position, but that
13844 glyph has the avoid_cursor_p flag set. */
13845 int match_with_avoid_cursor = 0;
13846 /* Non-zero means we've seen at least one glyph that came from a
13847 display string. */
13848 int string_seen = 0;
13849 /* Largest and smallest buffer positions seen so far during scan of
13850 glyph row. */
13851 ptrdiff_t bpos_max = pos_before;
13852 ptrdiff_t bpos_min = pos_after;
13853 /* Last buffer position covered by an overlay string with an integer
13854 `cursor' property. */
13855 ptrdiff_t bpos_covered = 0;
13856 /* Non-zero means the display string on which to display the cursor
13857 comes from a text property, not from an overlay. */
13858 int string_from_text_prop = 0;
13860 /* Don't even try doing anything if called for a mode-line or
13861 header-line row, since the rest of the code isn't prepared to
13862 deal with such calamities. */
13863 eassert (!row->mode_line_p);
13864 if (row->mode_line_p)
13865 return 0;
13867 /* Skip over glyphs not having an object at the start and the end of
13868 the row. These are special glyphs like truncation marks on
13869 terminal frames. */
13870 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13872 if (!row->reversed_p)
13874 while (glyph < end
13875 && INTEGERP (glyph->object)
13876 && glyph->charpos < 0)
13878 x += glyph->pixel_width;
13879 ++glyph;
13881 while (end > glyph
13882 && INTEGERP ((end - 1)->object)
13883 /* CHARPOS is zero for blanks and stretch glyphs
13884 inserted by extend_face_to_end_of_line. */
13885 && (end - 1)->charpos <= 0)
13886 --end;
13887 glyph_before = glyph - 1;
13888 glyph_after = end;
13890 else
13892 struct glyph *g;
13894 /* If the glyph row is reversed, we need to process it from back
13895 to front, so swap the edge pointers. */
13896 glyphs_end = end = glyph - 1;
13897 glyph += row->used[TEXT_AREA] - 1;
13899 while (glyph > end + 1
13900 && INTEGERP (glyph->object)
13901 && glyph->charpos < 0)
13903 --glyph;
13904 x -= glyph->pixel_width;
13906 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13907 --glyph;
13908 /* By default, in reversed rows we put the cursor on the
13909 rightmost (first in the reading order) glyph. */
13910 for (g = end + 1; g < glyph; g++)
13911 x += g->pixel_width;
13912 while (end < glyph
13913 && INTEGERP ((end + 1)->object)
13914 && (end + 1)->charpos <= 0)
13915 ++end;
13916 glyph_before = glyph + 1;
13917 glyph_after = end;
13920 else if (row->reversed_p)
13922 /* In R2L rows that don't display text, put the cursor on the
13923 rightmost glyph. Case in point: an empty last line that is
13924 part of an R2L paragraph. */
13925 cursor = end - 1;
13926 /* Avoid placing the cursor on the last glyph of the row, where
13927 on terminal frames we hold the vertical border between
13928 adjacent windows. */
13929 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13930 && !WINDOW_RIGHTMOST_P (w)
13931 && cursor == row->glyphs[LAST_AREA] - 1)
13932 cursor--;
13933 x = -1; /* will be computed below, at label compute_x */
13936 /* Step 1: Try to find the glyph whose character position
13937 corresponds to point. If that's not possible, find 2 glyphs
13938 whose character positions are the closest to point, one before
13939 point, the other after it. */
13940 if (!row->reversed_p)
13941 while (/* not marched to end of glyph row */
13942 glyph < end
13943 /* glyph was not inserted by redisplay for internal purposes */
13944 && !INTEGERP (glyph->object))
13946 if (BUFFERP (glyph->object))
13948 ptrdiff_t dpos = glyph->charpos - pt_old;
13950 if (glyph->charpos > bpos_max)
13951 bpos_max = glyph->charpos;
13952 if (glyph->charpos < bpos_min)
13953 bpos_min = glyph->charpos;
13954 if (!glyph->avoid_cursor_p)
13956 /* If we hit point, we've found the glyph on which to
13957 display the cursor. */
13958 if (dpos == 0)
13960 match_with_avoid_cursor = 0;
13961 break;
13963 /* See if we've found a better approximation to
13964 POS_BEFORE or to POS_AFTER. */
13965 if (0 > dpos && dpos > pos_before - pt_old)
13967 pos_before = glyph->charpos;
13968 glyph_before = glyph;
13970 else if (0 < dpos && dpos < pos_after - pt_old)
13972 pos_after = glyph->charpos;
13973 glyph_after = glyph;
13976 else if (dpos == 0)
13977 match_with_avoid_cursor = 1;
13979 else if (STRINGP (glyph->object))
13981 Lisp_Object chprop;
13982 ptrdiff_t glyph_pos = glyph->charpos;
13984 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13985 glyph->object);
13986 if (!NILP (chprop))
13988 /* If the string came from a `display' text property,
13989 look up the buffer position of that property and
13990 use that position to update bpos_max, as if we
13991 actually saw such a position in one of the row's
13992 glyphs. This helps with supporting integer values
13993 of `cursor' property on the display string in
13994 situations where most or all of the row's buffer
13995 text is completely covered by display properties,
13996 so that no glyph with valid buffer positions is
13997 ever seen in the row. */
13998 ptrdiff_t prop_pos =
13999 string_buffer_position_lim (glyph->object, pos_before,
14000 pos_after, 0);
14002 if (prop_pos >= pos_before)
14003 bpos_max = prop_pos - 1;
14005 if (INTEGERP (chprop))
14007 bpos_covered = bpos_max + XINT (chprop);
14008 /* If the `cursor' property covers buffer positions up
14009 to and including point, we should display cursor on
14010 this glyph. Note that, if a `cursor' property on one
14011 of the string's characters has an integer value, we
14012 will break out of the loop below _before_ we get to
14013 the position match above. IOW, integer values of
14014 the `cursor' property override the "exact match for
14015 point" strategy of positioning the cursor. */
14016 /* Implementation note: bpos_max == pt_old when, e.g.,
14017 we are in an empty line, where bpos_max is set to
14018 MATRIX_ROW_START_CHARPOS, see above. */
14019 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14021 cursor = glyph;
14022 break;
14026 string_seen = 1;
14028 x += glyph->pixel_width;
14029 ++glyph;
14031 else if (glyph > end) /* row is reversed */
14032 while (!INTEGERP (glyph->object))
14034 if (BUFFERP (glyph->object))
14036 ptrdiff_t dpos = glyph->charpos - pt_old;
14038 if (glyph->charpos > bpos_max)
14039 bpos_max = glyph->charpos;
14040 if (glyph->charpos < bpos_min)
14041 bpos_min = glyph->charpos;
14042 if (!glyph->avoid_cursor_p)
14044 if (dpos == 0)
14046 match_with_avoid_cursor = 0;
14047 break;
14049 if (0 > dpos && dpos > pos_before - pt_old)
14051 pos_before = glyph->charpos;
14052 glyph_before = glyph;
14054 else if (0 < dpos && dpos < pos_after - pt_old)
14056 pos_after = glyph->charpos;
14057 glyph_after = glyph;
14060 else if (dpos == 0)
14061 match_with_avoid_cursor = 1;
14063 else if (STRINGP (glyph->object))
14065 Lisp_Object chprop;
14066 ptrdiff_t glyph_pos = glyph->charpos;
14068 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14069 glyph->object);
14070 if (!NILP (chprop))
14072 ptrdiff_t prop_pos =
14073 string_buffer_position_lim (glyph->object, pos_before,
14074 pos_after, 0);
14076 if (prop_pos >= pos_before)
14077 bpos_max = prop_pos - 1;
14079 if (INTEGERP (chprop))
14081 bpos_covered = bpos_max + XINT (chprop);
14082 /* If the `cursor' property covers buffer positions up
14083 to and including point, we should display cursor on
14084 this glyph. */
14085 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14087 cursor = glyph;
14088 break;
14091 string_seen = 1;
14093 --glyph;
14094 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14096 x--; /* can't use any pixel_width */
14097 break;
14099 x -= glyph->pixel_width;
14102 /* Step 2: If we didn't find an exact match for point, we need to
14103 look for a proper place to put the cursor among glyphs between
14104 GLYPH_BEFORE and GLYPH_AFTER. */
14105 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14106 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14107 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14109 /* An empty line has a single glyph whose OBJECT is zero and
14110 whose CHARPOS is the position of a newline on that line.
14111 Note that on a TTY, there are more glyphs after that, which
14112 were produced by extend_face_to_end_of_line, but their
14113 CHARPOS is zero or negative. */
14114 int empty_line_p =
14115 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14116 && INTEGERP (glyph->object) && glyph->charpos > 0
14117 /* On a TTY, continued and truncated rows also have a glyph at
14118 their end whose OBJECT is zero and whose CHARPOS is
14119 positive (the continuation and truncation glyphs), but such
14120 rows are obviously not "empty". */
14121 && !(row->continued_p || row->truncated_on_right_p);
14123 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14125 ptrdiff_t ellipsis_pos;
14127 /* Scan back over the ellipsis glyphs. */
14128 if (!row->reversed_p)
14130 ellipsis_pos = (glyph - 1)->charpos;
14131 while (glyph > row->glyphs[TEXT_AREA]
14132 && (glyph - 1)->charpos == ellipsis_pos)
14133 glyph--, x -= glyph->pixel_width;
14134 /* That loop always goes one position too far, including
14135 the glyph before the ellipsis. So scan forward over
14136 that one. */
14137 x += glyph->pixel_width;
14138 glyph++;
14140 else /* row is reversed */
14142 ellipsis_pos = (glyph + 1)->charpos;
14143 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14144 && (glyph + 1)->charpos == ellipsis_pos)
14145 glyph++, x += glyph->pixel_width;
14146 x -= glyph->pixel_width;
14147 glyph--;
14150 else if (match_with_avoid_cursor)
14152 cursor = glyph_after;
14153 x = -1;
14155 else if (string_seen)
14157 int incr = row->reversed_p ? -1 : +1;
14159 /* Need to find the glyph that came out of a string which is
14160 present at point. That glyph is somewhere between
14161 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14162 positioned between POS_BEFORE and POS_AFTER in the
14163 buffer. */
14164 struct glyph *start, *stop;
14165 ptrdiff_t pos = pos_before;
14167 x = -1;
14169 /* If the row ends in a newline from a display string,
14170 reordering could have moved the glyphs belonging to the
14171 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14172 in this case we extend the search to the last glyph in
14173 the row that was not inserted by redisplay. */
14174 if (row->ends_in_newline_from_string_p)
14176 glyph_after = end;
14177 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14180 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14181 correspond to POS_BEFORE and POS_AFTER, respectively. We
14182 need START and STOP in the order that corresponds to the
14183 row's direction as given by its reversed_p flag. If the
14184 directionality of characters between POS_BEFORE and
14185 POS_AFTER is the opposite of the row's base direction,
14186 these characters will have been reordered for display,
14187 and we need to reverse START and STOP. */
14188 if (!row->reversed_p)
14190 start = min (glyph_before, glyph_after);
14191 stop = max (glyph_before, glyph_after);
14193 else
14195 start = max (glyph_before, glyph_after);
14196 stop = min (glyph_before, glyph_after);
14198 for (glyph = start + incr;
14199 row->reversed_p ? glyph > stop : glyph < stop; )
14202 /* Any glyphs that come from the buffer are here because
14203 of bidi reordering. Skip them, and only pay
14204 attention to glyphs that came from some string. */
14205 if (STRINGP (glyph->object))
14207 Lisp_Object str;
14208 ptrdiff_t tem;
14209 /* If the display property covers the newline, we
14210 need to search for it one position farther. */
14211 ptrdiff_t lim = pos_after
14212 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14214 string_from_text_prop = 0;
14215 str = glyph->object;
14216 tem = string_buffer_position_lim (str, pos, lim, 0);
14217 if (tem == 0 /* from overlay */
14218 || pos <= tem)
14220 /* If the string from which this glyph came is
14221 found in the buffer at point, or at position
14222 that is closer to point than pos_after, then
14223 we've found the glyph we've been looking for.
14224 If it comes from an overlay (tem == 0), and
14225 it has the `cursor' property on one of its
14226 glyphs, record that glyph as a candidate for
14227 displaying the cursor. (As in the
14228 unidirectional version, we will display the
14229 cursor on the last candidate we find.) */
14230 if (tem == 0
14231 || tem == pt_old
14232 || (tem - pt_old > 0 && tem < pos_after))
14234 /* The glyphs from this string could have
14235 been reordered. Find the one with the
14236 smallest string position. Or there could
14237 be a character in the string with the
14238 `cursor' property, which means display
14239 cursor on that character's glyph. */
14240 ptrdiff_t strpos = glyph->charpos;
14242 if (tem)
14244 cursor = glyph;
14245 string_from_text_prop = 1;
14247 for ( ;
14248 (row->reversed_p ? glyph > stop : glyph < stop)
14249 && EQ (glyph->object, str);
14250 glyph += incr)
14252 Lisp_Object cprop;
14253 ptrdiff_t gpos = glyph->charpos;
14255 cprop = Fget_char_property (make_number (gpos),
14256 Qcursor,
14257 glyph->object);
14258 if (!NILP (cprop))
14260 cursor = glyph;
14261 break;
14263 if (tem && glyph->charpos < strpos)
14265 strpos = glyph->charpos;
14266 cursor = glyph;
14270 if (tem == pt_old
14271 || (tem - pt_old > 0 && tem < pos_after))
14272 goto compute_x;
14274 if (tem)
14275 pos = tem + 1; /* don't find previous instances */
14277 /* This string is not what we want; skip all of the
14278 glyphs that came from it. */
14279 while ((row->reversed_p ? glyph > stop : glyph < stop)
14280 && EQ (glyph->object, str))
14281 glyph += incr;
14283 else
14284 glyph += incr;
14287 /* If we reached the end of the line, and END was from a string,
14288 the cursor is not on this line. */
14289 if (cursor == NULL
14290 && (row->reversed_p ? glyph <= end : glyph >= end)
14291 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14292 && STRINGP (end->object)
14293 && row->continued_p)
14294 return 0;
14296 /* A truncated row may not include PT among its character positions.
14297 Setting the cursor inside the scroll margin will trigger
14298 recalculation of hscroll in hscroll_window_tree. But if a
14299 display string covers point, defer to the string-handling
14300 code below to figure this out. */
14301 else if (row->truncated_on_left_p && pt_old < bpos_min)
14303 cursor = glyph_before;
14304 x = -1;
14306 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14307 /* Zero-width characters produce no glyphs. */
14308 || (!empty_line_p
14309 && (row->reversed_p
14310 ? glyph_after > glyphs_end
14311 : glyph_after < glyphs_end)))
14313 cursor = glyph_after;
14314 x = -1;
14318 compute_x:
14319 if (cursor != NULL)
14320 glyph = cursor;
14321 else if (glyph == glyphs_end
14322 && pos_before == pos_after
14323 && STRINGP ((row->reversed_p
14324 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14325 : row->glyphs[TEXT_AREA])->object))
14327 /* If all the glyphs of this row came from strings, put the
14328 cursor on the first glyph of the row. This avoids having the
14329 cursor outside of the text area in this very rare and hard
14330 use case. */
14331 glyph =
14332 row->reversed_p
14333 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14334 : row->glyphs[TEXT_AREA];
14336 if (x < 0)
14338 struct glyph *g;
14340 /* Need to compute x that corresponds to GLYPH. */
14341 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14343 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14344 emacs_abort ();
14345 x += g->pixel_width;
14349 /* ROW could be part of a continued line, which, under bidi
14350 reordering, might have other rows whose start and end charpos
14351 occlude point. Only set w->cursor if we found a better
14352 approximation to the cursor position than we have from previously
14353 examined candidate rows belonging to the same continued line. */
14354 if (/* we already have a candidate row */
14355 w->cursor.vpos >= 0
14356 /* that candidate is not the row we are processing */
14357 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14358 /* Make sure cursor.vpos specifies a row whose start and end
14359 charpos occlude point, and it is valid candidate for being a
14360 cursor-row. This is because some callers of this function
14361 leave cursor.vpos at the row where the cursor was displayed
14362 during the last redisplay cycle. */
14363 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14364 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14365 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14367 struct glyph *g1 =
14368 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14370 /* Don't consider glyphs that are outside TEXT_AREA. */
14371 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14372 return 0;
14373 /* Keep the candidate whose buffer position is the closest to
14374 point or has the `cursor' property. */
14375 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14376 w->cursor.hpos >= 0
14377 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14378 && ((BUFFERP (g1->object)
14379 && (g1->charpos == pt_old /* an exact match always wins */
14380 || (BUFFERP (glyph->object)
14381 && eabs (g1->charpos - pt_old)
14382 < eabs (glyph->charpos - pt_old))))
14383 /* previous candidate is a glyph from a string that has
14384 a non-nil `cursor' property */
14385 || (STRINGP (g1->object)
14386 && (!NILP (Fget_char_property (make_number (g1->charpos),
14387 Qcursor, g1->object))
14388 /* previous candidate is from the same display
14389 string as this one, and the display string
14390 came from a text property */
14391 || (EQ (g1->object, glyph->object)
14392 && string_from_text_prop)
14393 /* this candidate is from newline and its
14394 position is not an exact match */
14395 || (INTEGERP (glyph->object)
14396 && glyph->charpos != pt_old)))))
14397 return 0;
14398 /* If this candidate gives an exact match, use that. */
14399 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14400 /* If this candidate is a glyph created for the
14401 terminating newline of a line, and point is on that
14402 newline, it wins because it's an exact match. */
14403 || (!row->continued_p
14404 && INTEGERP (glyph->object)
14405 && glyph->charpos == 0
14406 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14407 /* Otherwise, keep the candidate that comes from a row
14408 spanning less buffer positions. This may win when one or
14409 both candidate positions are on glyphs that came from
14410 display strings, for which we cannot compare buffer
14411 positions. */
14412 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14413 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14414 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14415 return 0;
14417 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14418 w->cursor.x = x;
14419 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14420 w->cursor.y = row->y + dy;
14422 if (w == XWINDOW (selected_window))
14424 if (!row->continued_p
14425 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14426 && row->x == 0)
14428 this_line_buffer = XBUFFER (w->contents);
14430 CHARPOS (this_line_start_pos)
14431 = MATRIX_ROW_START_CHARPOS (row) + delta;
14432 BYTEPOS (this_line_start_pos)
14433 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14435 CHARPOS (this_line_end_pos)
14436 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14437 BYTEPOS (this_line_end_pos)
14438 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14440 this_line_y = w->cursor.y;
14441 this_line_pixel_height = row->height;
14442 this_line_vpos = w->cursor.vpos;
14443 this_line_start_x = row->x;
14445 else
14446 CHARPOS (this_line_start_pos) = 0;
14449 return 1;
14453 /* Run window scroll functions, if any, for WINDOW with new window
14454 start STARTP. Sets the window start of WINDOW to that position.
14456 We assume that the window's buffer is really current. */
14458 static struct text_pos
14459 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14461 struct window *w = XWINDOW (window);
14462 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14464 eassert (current_buffer == XBUFFER (w->contents));
14466 if (!NILP (Vwindow_scroll_functions))
14468 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14469 make_number (CHARPOS (startp)));
14470 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14471 /* In case the hook functions switch buffers. */
14472 set_buffer_internal (XBUFFER (w->contents));
14475 return startp;
14479 /* Make sure the line containing the cursor is fully visible.
14480 A value of 1 means there is nothing to be done.
14481 (Either the line is fully visible, or it cannot be made so,
14482 or we cannot tell.)
14484 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14485 is higher than window.
14487 A value of 0 means the caller should do scrolling
14488 as if point had gone off the screen. */
14490 static int
14491 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14493 struct glyph_matrix *matrix;
14494 struct glyph_row *row;
14495 int window_height;
14497 if (!make_cursor_line_fully_visible_p)
14498 return 1;
14500 /* It's not always possible to find the cursor, e.g, when a window
14501 is full of overlay strings. Don't do anything in that case. */
14502 if (w->cursor.vpos < 0)
14503 return 1;
14505 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14506 row = MATRIX_ROW (matrix, w->cursor.vpos);
14508 /* If the cursor row is not partially visible, there's nothing to do. */
14509 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14510 return 1;
14512 /* If the row the cursor is in is taller than the window's height,
14513 it's not clear what to do, so do nothing. */
14514 window_height = window_box_height (w);
14515 if (row->height >= window_height)
14517 if (!force_p || MINI_WINDOW_P (w)
14518 || w->vscroll || w->cursor.vpos == 0)
14519 return 1;
14521 return 0;
14525 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14526 non-zero means only WINDOW is redisplayed in redisplay_internal.
14527 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14528 in redisplay_window to bring a partially visible line into view in
14529 the case that only the cursor has moved.
14531 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14532 last screen line's vertical height extends past the end of the screen.
14534 Value is
14536 1 if scrolling succeeded
14538 0 if scrolling didn't find point.
14540 -1 if new fonts have been loaded so that we must interrupt
14541 redisplay, adjust glyph matrices, and try again. */
14543 enum
14545 SCROLLING_SUCCESS,
14546 SCROLLING_FAILED,
14547 SCROLLING_NEED_LARGER_MATRICES
14550 /* If scroll-conservatively is more than this, never recenter.
14552 If you change this, don't forget to update the doc string of
14553 `scroll-conservatively' and the Emacs manual. */
14554 #define SCROLL_LIMIT 100
14556 static int
14557 try_scrolling (Lisp_Object window, int just_this_one_p,
14558 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14559 int temp_scroll_step, int last_line_misfit)
14561 struct window *w = XWINDOW (window);
14562 struct frame *f = XFRAME (w->frame);
14563 struct text_pos pos, startp;
14564 struct it it;
14565 int this_scroll_margin, scroll_max, rc, height;
14566 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14567 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14568 Lisp_Object aggressive;
14569 /* We will never try scrolling more than this number of lines. */
14570 int scroll_limit = SCROLL_LIMIT;
14571 int frame_line_height = default_line_pixel_height (w);
14572 int window_total_lines
14573 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14575 #ifdef GLYPH_DEBUG
14576 debug_method_add (w, "try_scrolling");
14577 #endif
14579 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14581 /* Compute scroll margin height in pixels. We scroll when point is
14582 within this distance from the top or bottom of the window. */
14583 if (scroll_margin > 0)
14584 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14585 * frame_line_height;
14586 else
14587 this_scroll_margin = 0;
14589 /* Force arg_scroll_conservatively to have a reasonable value, to
14590 avoid scrolling too far away with slow move_it_* functions. Note
14591 that the user can supply scroll-conservatively equal to
14592 `most-positive-fixnum', which can be larger than INT_MAX. */
14593 if (arg_scroll_conservatively > scroll_limit)
14595 arg_scroll_conservatively = scroll_limit + 1;
14596 scroll_max = scroll_limit * frame_line_height;
14598 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14599 /* Compute how much we should try to scroll maximally to bring
14600 point into view. */
14601 scroll_max = (max (scroll_step,
14602 max (arg_scroll_conservatively, temp_scroll_step))
14603 * frame_line_height);
14604 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14605 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14606 /* We're trying to scroll because of aggressive scrolling but no
14607 scroll_step is set. Choose an arbitrary one. */
14608 scroll_max = 10 * frame_line_height;
14609 else
14610 scroll_max = 0;
14612 too_near_end:
14614 /* Decide whether to scroll down. */
14615 if (PT > CHARPOS (startp))
14617 int scroll_margin_y;
14619 /* Compute the pixel ypos of the scroll margin, then move IT to
14620 either that ypos or PT, whichever comes first. */
14621 start_display (&it, w, startp);
14622 scroll_margin_y = it.last_visible_y - this_scroll_margin
14623 - frame_line_height * extra_scroll_margin_lines;
14624 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14625 (MOVE_TO_POS | MOVE_TO_Y));
14627 if (PT > CHARPOS (it.current.pos))
14629 int y0 = line_bottom_y (&it);
14630 /* Compute how many pixels below window bottom to stop searching
14631 for PT. This avoids costly search for PT that is far away if
14632 the user limited scrolling by a small number of lines, but
14633 always finds PT if scroll_conservatively is set to a large
14634 number, such as most-positive-fixnum. */
14635 int slack = max (scroll_max, 10 * frame_line_height);
14636 int y_to_move = it.last_visible_y + slack;
14638 /* Compute the distance from the scroll margin to PT or to
14639 the scroll limit, whichever comes first. This should
14640 include the height of the cursor line, to make that line
14641 fully visible. */
14642 move_it_to (&it, PT, -1, y_to_move,
14643 -1, MOVE_TO_POS | MOVE_TO_Y);
14644 dy = line_bottom_y (&it) - y0;
14646 if (dy > scroll_max)
14647 return SCROLLING_FAILED;
14649 if (dy > 0)
14650 scroll_down_p = 1;
14654 if (scroll_down_p)
14656 /* Point is in or below the bottom scroll margin, so move the
14657 window start down. If scrolling conservatively, move it just
14658 enough down to make point visible. If scroll_step is set,
14659 move it down by scroll_step. */
14660 if (arg_scroll_conservatively)
14661 amount_to_scroll
14662 = min (max (dy, frame_line_height),
14663 frame_line_height * arg_scroll_conservatively);
14664 else if (scroll_step || temp_scroll_step)
14665 amount_to_scroll = scroll_max;
14666 else
14668 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14669 height = WINDOW_BOX_TEXT_HEIGHT (w);
14670 if (NUMBERP (aggressive))
14672 double float_amount = XFLOATINT (aggressive) * height;
14673 int aggressive_scroll = float_amount;
14674 if (aggressive_scroll == 0 && float_amount > 0)
14675 aggressive_scroll = 1;
14676 /* Don't let point enter the scroll margin near top of
14677 the window. This could happen if the value of
14678 scroll_up_aggressively is too large and there are
14679 non-zero margins, because scroll_up_aggressively
14680 means put point that fraction of window height
14681 _from_the_bottom_margin_. */
14682 if (aggressive_scroll + 2*this_scroll_margin > height)
14683 aggressive_scroll = height - 2*this_scroll_margin;
14684 amount_to_scroll = dy + aggressive_scroll;
14688 if (amount_to_scroll <= 0)
14689 return SCROLLING_FAILED;
14691 start_display (&it, w, startp);
14692 if (arg_scroll_conservatively <= scroll_limit)
14693 move_it_vertically (&it, amount_to_scroll);
14694 else
14696 /* Extra precision for users who set scroll-conservatively
14697 to a large number: make sure the amount we scroll
14698 the window start is never less than amount_to_scroll,
14699 which was computed as distance from window bottom to
14700 point. This matters when lines at window top and lines
14701 below window bottom have different height. */
14702 struct it it1;
14703 void *it1data = NULL;
14704 /* We use a temporary it1 because line_bottom_y can modify
14705 its argument, if it moves one line down; see there. */
14706 int start_y;
14708 SAVE_IT (it1, it, it1data);
14709 start_y = line_bottom_y (&it1);
14710 do {
14711 RESTORE_IT (&it, &it, it1data);
14712 move_it_by_lines (&it, 1);
14713 SAVE_IT (it1, it, it1data);
14714 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14717 /* If STARTP is unchanged, move it down another screen line. */
14718 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14719 move_it_by_lines (&it, 1);
14720 startp = it.current.pos;
14722 else
14724 struct text_pos scroll_margin_pos = startp;
14725 int y_offset = 0;
14727 /* See if point is inside the scroll margin at the top of the
14728 window. */
14729 if (this_scroll_margin)
14731 int y_start;
14733 start_display (&it, w, startp);
14734 y_start = it.current_y;
14735 move_it_vertically (&it, this_scroll_margin);
14736 scroll_margin_pos = it.current.pos;
14737 /* If we didn't move enough before hitting ZV, request
14738 additional amount of scroll, to move point out of the
14739 scroll margin. */
14740 if (IT_CHARPOS (it) == ZV
14741 && it.current_y - y_start < this_scroll_margin)
14742 y_offset = this_scroll_margin - (it.current_y - y_start);
14745 if (PT < CHARPOS (scroll_margin_pos))
14747 /* Point is in the scroll margin at the top of the window or
14748 above what is displayed in the window. */
14749 int y0, y_to_move;
14751 /* Compute the vertical distance from PT to the scroll
14752 margin position. Move as far as scroll_max allows, or
14753 one screenful, or 10 screen lines, whichever is largest.
14754 Give up if distance is greater than scroll_max or if we
14755 didn't reach the scroll margin position. */
14756 SET_TEXT_POS (pos, PT, PT_BYTE);
14757 start_display (&it, w, pos);
14758 y0 = it.current_y;
14759 y_to_move = max (it.last_visible_y,
14760 max (scroll_max, 10 * frame_line_height));
14761 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14762 y_to_move, -1,
14763 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14764 dy = it.current_y - y0;
14765 if (dy > scroll_max
14766 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14767 return SCROLLING_FAILED;
14769 /* Additional scroll for when ZV was too close to point. */
14770 dy += y_offset;
14772 /* Compute new window start. */
14773 start_display (&it, w, startp);
14775 if (arg_scroll_conservatively)
14776 amount_to_scroll = max (dy, frame_line_height *
14777 max (scroll_step, temp_scroll_step));
14778 else if (scroll_step || temp_scroll_step)
14779 amount_to_scroll = scroll_max;
14780 else
14782 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14783 height = WINDOW_BOX_TEXT_HEIGHT (w);
14784 if (NUMBERP (aggressive))
14786 double float_amount = XFLOATINT (aggressive) * height;
14787 int aggressive_scroll = float_amount;
14788 if (aggressive_scroll == 0 && float_amount > 0)
14789 aggressive_scroll = 1;
14790 /* Don't let point enter the scroll margin near
14791 bottom of the window, if the value of
14792 scroll_down_aggressively happens to be too
14793 large. */
14794 if (aggressive_scroll + 2*this_scroll_margin > height)
14795 aggressive_scroll = height - 2*this_scroll_margin;
14796 amount_to_scroll = dy + aggressive_scroll;
14800 if (amount_to_scroll <= 0)
14801 return SCROLLING_FAILED;
14803 move_it_vertically_backward (&it, amount_to_scroll);
14804 startp = it.current.pos;
14808 /* Run window scroll functions. */
14809 startp = run_window_scroll_functions (window, startp);
14811 /* Display the window. Give up if new fonts are loaded, or if point
14812 doesn't appear. */
14813 if (!try_window (window, startp, 0))
14814 rc = SCROLLING_NEED_LARGER_MATRICES;
14815 else if (w->cursor.vpos < 0)
14817 clear_glyph_matrix (w->desired_matrix);
14818 rc = SCROLLING_FAILED;
14820 else
14822 /* Maybe forget recorded base line for line number display. */
14823 if (!just_this_one_p
14824 || current_buffer->clip_changed
14825 || BEG_UNCHANGED < CHARPOS (startp))
14826 w->base_line_number = 0;
14828 /* If cursor ends up on a partially visible line,
14829 treat that as being off the bottom of the screen. */
14830 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14831 /* It's possible that the cursor is on the first line of the
14832 buffer, which is partially obscured due to a vscroll
14833 (Bug#7537). In that case, avoid looping forever . */
14834 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14836 clear_glyph_matrix (w->desired_matrix);
14837 ++extra_scroll_margin_lines;
14838 goto too_near_end;
14840 rc = SCROLLING_SUCCESS;
14843 return rc;
14847 /* Compute a suitable window start for window W if display of W starts
14848 on a continuation line. Value is non-zero if a new window start
14849 was computed.
14851 The new window start will be computed, based on W's width, starting
14852 from the start of the continued line. It is the start of the
14853 screen line with the minimum distance from the old start W->start. */
14855 static int
14856 compute_window_start_on_continuation_line (struct window *w)
14858 struct text_pos pos, start_pos;
14859 int window_start_changed_p = 0;
14861 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14863 /* If window start is on a continuation line... Window start may be
14864 < BEGV in case there's invisible text at the start of the
14865 buffer (M-x rmail, for example). */
14866 if (CHARPOS (start_pos) > BEGV
14867 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14869 struct it it;
14870 struct glyph_row *row;
14872 /* Handle the case that the window start is out of range. */
14873 if (CHARPOS (start_pos) < BEGV)
14874 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14875 else if (CHARPOS (start_pos) > ZV)
14876 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14878 /* Find the start of the continued line. This should be fast
14879 because find_newline is fast (newline cache). */
14880 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14881 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14882 row, DEFAULT_FACE_ID);
14883 reseat_at_previous_visible_line_start (&it);
14885 /* If the line start is "too far" away from the window start,
14886 say it takes too much time to compute a new window start. */
14887 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14888 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14890 int min_distance, distance;
14892 /* Move forward by display lines to find the new window
14893 start. If window width was enlarged, the new start can
14894 be expected to be > the old start. If window width was
14895 decreased, the new window start will be < the old start.
14896 So, we're looking for the display line start with the
14897 minimum distance from the old window start. */
14898 pos = it.current.pos;
14899 min_distance = INFINITY;
14900 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14901 distance < min_distance)
14903 min_distance = distance;
14904 pos = it.current.pos;
14905 if (it.line_wrap == WORD_WRAP)
14907 /* Under WORD_WRAP, move_it_by_lines is likely to
14908 overshoot and stop not at the first, but the
14909 second character from the left margin. So in
14910 that case, we need a more tight control on the X
14911 coordinate of the iterator than move_it_by_lines
14912 promises in its contract. The method is to first
14913 go to the last (rightmost) visible character of a
14914 line, then move to the leftmost character on the
14915 next line in a separate call. */
14916 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14917 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14918 move_it_to (&it, ZV, 0,
14919 it.current_y + it.max_ascent + it.max_descent, -1,
14920 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14922 else
14923 move_it_by_lines (&it, 1);
14926 /* Set the window start there. */
14927 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14928 window_start_changed_p = 1;
14932 return window_start_changed_p;
14936 /* Try cursor movement in case text has not changed in window WINDOW,
14937 with window start STARTP. Value is
14939 CURSOR_MOVEMENT_SUCCESS if successful
14941 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14943 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14944 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14945 we want to scroll as if scroll-step were set to 1. See the code.
14947 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14948 which case we have to abort this redisplay, and adjust matrices
14949 first. */
14951 enum
14953 CURSOR_MOVEMENT_SUCCESS,
14954 CURSOR_MOVEMENT_CANNOT_BE_USED,
14955 CURSOR_MOVEMENT_MUST_SCROLL,
14956 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14959 static int
14960 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14962 struct window *w = XWINDOW (window);
14963 struct frame *f = XFRAME (w->frame);
14964 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14966 #ifdef GLYPH_DEBUG
14967 if (inhibit_try_cursor_movement)
14968 return rc;
14969 #endif
14971 /* Previously, there was a check for Lisp integer in the
14972 if-statement below. Now, this field is converted to
14973 ptrdiff_t, thus zero means invalid position in a buffer. */
14974 eassert (w->last_point > 0);
14975 /* Likewise there was a check whether window_end_vpos is nil or larger
14976 than the window. Now window_end_vpos is int and so never nil, but
14977 let's leave eassert to check whether it fits in the window. */
14978 eassert (w->window_end_vpos < w->current_matrix->nrows);
14980 /* Handle case where text has not changed, only point, and it has
14981 not moved off the frame. */
14982 if (/* Point may be in this window. */
14983 PT >= CHARPOS (startp)
14984 /* Selective display hasn't changed. */
14985 && !current_buffer->clip_changed
14986 /* Function force-mode-line-update is used to force a thorough
14987 redisplay. It sets either windows_or_buffers_changed or
14988 update_mode_lines. So don't take a shortcut here for these
14989 cases. */
14990 && !update_mode_lines
14991 && !windows_or_buffers_changed
14992 && !f->cursor_type_changed
14993 /* Can't use this case if highlighting a region. When a
14994 region exists, cursor movement has to do more than just
14995 set the cursor. */
14996 && markpos_of_region () < 0
14997 && !w->region_showing
14998 && NILP (Vshow_trailing_whitespace)
14999 /* This code is not used for mini-buffer for the sake of the case
15000 of redisplaying to replace an echo area message; since in
15001 that case the mini-buffer contents per se are usually
15002 unchanged. This code is of no real use in the mini-buffer
15003 since the handling of this_line_start_pos, etc., in redisplay
15004 handles the same cases. */
15005 && !EQ (window, minibuf_window)
15006 && (FRAME_WINDOW_P (f)
15007 || !overlay_arrow_in_current_buffer_p ()))
15009 int this_scroll_margin, top_scroll_margin;
15010 struct glyph_row *row = NULL;
15011 int frame_line_height = default_line_pixel_height (w);
15012 int window_total_lines
15013 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15015 #ifdef GLYPH_DEBUG
15016 debug_method_add (w, "cursor movement");
15017 #endif
15019 /* Scroll if point within this distance from the top or bottom
15020 of the window. This is a pixel value. */
15021 if (scroll_margin > 0)
15023 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15024 this_scroll_margin *= frame_line_height;
15026 else
15027 this_scroll_margin = 0;
15029 top_scroll_margin = this_scroll_margin;
15030 if (WINDOW_WANTS_HEADER_LINE_P (w))
15031 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15033 /* Start with the row the cursor was displayed during the last
15034 not paused redisplay. Give up if that row is not valid. */
15035 if (w->last_cursor_vpos < 0
15036 || w->last_cursor_vpos >= w->current_matrix->nrows)
15037 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15038 else
15040 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15041 if (row->mode_line_p)
15042 ++row;
15043 if (!row->enabled_p)
15044 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15047 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15049 int scroll_p = 0, must_scroll = 0;
15050 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15052 if (PT > w->last_point)
15054 /* Point has moved forward. */
15055 while (MATRIX_ROW_END_CHARPOS (row) < PT
15056 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15058 eassert (row->enabled_p);
15059 ++row;
15062 /* If the end position of a row equals the start
15063 position of the next row, and PT is at that position,
15064 we would rather display cursor in the next line. */
15065 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15066 && MATRIX_ROW_END_CHARPOS (row) == PT
15067 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15068 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15069 && !cursor_row_p (row))
15070 ++row;
15072 /* If within the scroll margin, scroll. Note that
15073 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15074 the next line would be drawn, and that
15075 this_scroll_margin can be zero. */
15076 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15077 || PT > MATRIX_ROW_END_CHARPOS (row)
15078 /* Line is completely visible last line in window
15079 and PT is to be set in the next line. */
15080 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15081 && PT == MATRIX_ROW_END_CHARPOS (row)
15082 && !row->ends_at_zv_p
15083 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15084 scroll_p = 1;
15086 else if (PT < w->last_point)
15088 /* Cursor has to be moved backward. Note that PT >=
15089 CHARPOS (startp) because of the outer if-statement. */
15090 while (!row->mode_line_p
15091 && (MATRIX_ROW_START_CHARPOS (row) > PT
15092 || (MATRIX_ROW_START_CHARPOS (row) == PT
15093 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15094 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15095 row > w->current_matrix->rows
15096 && (row-1)->ends_in_newline_from_string_p))))
15097 && (row->y > top_scroll_margin
15098 || CHARPOS (startp) == BEGV))
15100 eassert (row->enabled_p);
15101 --row;
15104 /* Consider the following case: Window starts at BEGV,
15105 there is invisible, intangible text at BEGV, so that
15106 display starts at some point START > BEGV. It can
15107 happen that we are called with PT somewhere between
15108 BEGV and START. Try to handle that case. */
15109 if (row < w->current_matrix->rows
15110 || row->mode_line_p)
15112 row = w->current_matrix->rows;
15113 if (row->mode_line_p)
15114 ++row;
15117 /* Due to newlines in overlay strings, we may have to
15118 skip forward over overlay strings. */
15119 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15120 && MATRIX_ROW_END_CHARPOS (row) == PT
15121 && !cursor_row_p (row))
15122 ++row;
15124 /* If within the scroll margin, scroll. */
15125 if (row->y < top_scroll_margin
15126 && CHARPOS (startp) != BEGV)
15127 scroll_p = 1;
15129 else
15131 /* Cursor did not move. So don't scroll even if cursor line
15132 is partially visible, as it was so before. */
15133 rc = CURSOR_MOVEMENT_SUCCESS;
15136 if (PT < MATRIX_ROW_START_CHARPOS (row)
15137 || PT > MATRIX_ROW_END_CHARPOS (row))
15139 /* if PT is not in the glyph row, give up. */
15140 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15141 must_scroll = 1;
15143 else if (rc != CURSOR_MOVEMENT_SUCCESS
15144 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15146 struct glyph_row *row1;
15148 /* If rows are bidi-reordered and point moved, back up
15149 until we find a row that does not belong to a
15150 continuation line. This is because we must consider
15151 all rows of a continued line as candidates for the
15152 new cursor positioning, since row start and end
15153 positions change non-linearly with vertical position
15154 in such rows. */
15155 /* FIXME: Revisit this when glyph ``spilling'' in
15156 continuation lines' rows is implemented for
15157 bidi-reordered rows. */
15158 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15159 MATRIX_ROW_CONTINUATION_LINE_P (row);
15160 --row)
15162 /* If we hit the beginning of the displayed portion
15163 without finding the first row of a continued
15164 line, give up. */
15165 if (row <= row1)
15167 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15168 break;
15170 eassert (row->enabled_p);
15173 if (must_scroll)
15175 else if (rc != CURSOR_MOVEMENT_SUCCESS
15176 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15177 /* Make sure this isn't a header line by any chance, since
15178 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15179 && !row->mode_line_p
15180 && make_cursor_line_fully_visible_p)
15182 if (PT == MATRIX_ROW_END_CHARPOS (row)
15183 && !row->ends_at_zv_p
15184 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15185 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15186 else if (row->height > window_box_height (w))
15188 /* If we end up in a partially visible line, let's
15189 make it fully visible, except when it's taller
15190 than the window, in which case we can't do much
15191 about it. */
15192 *scroll_step = 1;
15193 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15195 else
15197 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15198 if (!cursor_row_fully_visible_p (w, 0, 1))
15199 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15200 else
15201 rc = CURSOR_MOVEMENT_SUCCESS;
15204 else if (scroll_p)
15205 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15206 else if (rc != CURSOR_MOVEMENT_SUCCESS
15207 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15209 /* With bidi-reordered rows, there could be more than
15210 one candidate row whose start and end positions
15211 occlude point. We need to let set_cursor_from_row
15212 find the best candidate. */
15213 /* FIXME: Revisit this when glyph ``spilling'' in
15214 continuation lines' rows is implemented for
15215 bidi-reordered rows. */
15216 int rv = 0;
15220 int at_zv_p = 0, exact_match_p = 0;
15222 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15223 && PT <= MATRIX_ROW_END_CHARPOS (row)
15224 && cursor_row_p (row))
15225 rv |= set_cursor_from_row (w, row, w->current_matrix,
15226 0, 0, 0, 0);
15227 /* As soon as we've found the exact match for point,
15228 or the first suitable row whose ends_at_zv_p flag
15229 is set, we are done. */
15230 at_zv_p =
15231 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15232 if (rv && !at_zv_p
15233 && w->cursor.hpos >= 0
15234 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15235 w->cursor.vpos))
15237 struct glyph_row *candidate =
15238 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15239 struct glyph *g =
15240 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15241 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15243 exact_match_p =
15244 (BUFFERP (g->object) && g->charpos == PT)
15245 || (INTEGERP (g->object)
15246 && (g->charpos == PT
15247 || (g->charpos == 0 && endpos - 1 == PT)));
15249 if (rv && (at_zv_p || exact_match_p))
15251 rc = CURSOR_MOVEMENT_SUCCESS;
15252 break;
15254 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15255 break;
15256 ++row;
15258 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15259 || row->continued_p)
15260 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15261 || (MATRIX_ROW_START_CHARPOS (row) == PT
15262 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15263 /* If we didn't find any candidate rows, or exited the
15264 loop before all the candidates were examined, signal
15265 to the caller that this method failed. */
15266 if (rc != CURSOR_MOVEMENT_SUCCESS
15267 && !(rv
15268 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15269 && !row->continued_p))
15270 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15271 else if (rv)
15272 rc = CURSOR_MOVEMENT_SUCCESS;
15274 else
15278 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15280 rc = CURSOR_MOVEMENT_SUCCESS;
15281 break;
15283 ++row;
15285 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15286 && MATRIX_ROW_START_CHARPOS (row) == PT
15287 && cursor_row_p (row));
15292 return rc;
15295 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15296 static
15297 #endif
15298 void
15299 set_vertical_scroll_bar (struct window *w)
15301 ptrdiff_t start, end, whole;
15303 /* Calculate the start and end positions for the current window.
15304 At some point, it would be nice to choose between scrollbars
15305 which reflect the whole buffer size, with special markers
15306 indicating narrowing, and scrollbars which reflect only the
15307 visible region.
15309 Note that mini-buffers sometimes aren't displaying any text. */
15310 if (!MINI_WINDOW_P (w)
15311 || (w == XWINDOW (minibuf_window)
15312 && NILP (echo_area_buffer[0])))
15314 struct buffer *buf = XBUFFER (w->contents);
15315 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15316 start = marker_position (w->start) - BUF_BEGV (buf);
15317 /* I don't think this is guaranteed to be right. For the
15318 moment, we'll pretend it is. */
15319 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15321 if (end < start)
15322 end = start;
15323 if (whole < (end - start))
15324 whole = end - start;
15326 else
15327 start = end = whole = 0;
15329 /* Indicate what this scroll bar ought to be displaying now. */
15330 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15331 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15332 (w, end - start, whole, start);
15336 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15337 selected_window is redisplayed.
15339 We can return without actually redisplaying the window if fonts has been
15340 changed on window's frame. In that case, redisplay_internal will retry. */
15342 static void
15343 redisplay_window (Lisp_Object window, int just_this_one_p)
15345 struct window *w = XWINDOW (window);
15346 struct frame *f = XFRAME (w->frame);
15347 struct buffer *buffer = XBUFFER (w->contents);
15348 struct buffer *old = current_buffer;
15349 struct text_pos lpoint, opoint, startp;
15350 int update_mode_line;
15351 int tem;
15352 struct it it;
15353 /* Record it now because it's overwritten. */
15354 int current_matrix_up_to_date_p = 0;
15355 int used_current_matrix_p = 0;
15356 /* This is less strict than current_matrix_up_to_date_p.
15357 It indicates that the buffer contents and narrowing are unchanged. */
15358 int buffer_unchanged_p = 0;
15359 int temp_scroll_step = 0;
15360 ptrdiff_t count = SPECPDL_INDEX ();
15361 int rc;
15362 int centering_position = -1;
15363 int last_line_misfit = 0;
15364 ptrdiff_t beg_unchanged, end_unchanged;
15365 int frame_line_height;
15367 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15368 opoint = lpoint;
15370 #ifdef GLYPH_DEBUG
15371 *w->desired_matrix->method = 0;
15372 #endif
15374 /* Make sure that both W's markers are valid. */
15375 eassert (XMARKER (w->start)->buffer == buffer);
15376 eassert (XMARKER (w->pointm)->buffer == buffer);
15378 restart:
15379 reconsider_clip_changes (w);
15380 frame_line_height = default_line_pixel_height (w);
15382 /* Has the mode line to be updated? */
15383 update_mode_line = (w->update_mode_line
15384 || update_mode_lines
15385 || buffer->clip_changed
15386 || buffer->prevent_redisplay_optimizations_p);
15388 if (MINI_WINDOW_P (w))
15390 if (w == XWINDOW (echo_area_window)
15391 && !NILP (echo_area_buffer[0]))
15393 if (update_mode_line)
15394 /* We may have to update a tty frame's menu bar or a
15395 tool-bar. Example `M-x C-h C-h C-g'. */
15396 goto finish_menu_bars;
15397 else
15398 /* We've already displayed the echo area glyphs in this window. */
15399 goto finish_scroll_bars;
15401 else if ((w != XWINDOW (minibuf_window)
15402 || minibuf_level == 0)
15403 /* When buffer is nonempty, redisplay window normally. */
15404 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15405 /* Quail displays non-mini buffers in minibuffer window.
15406 In that case, redisplay the window normally. */
15407 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15409 /* W is a mini-buffer window, but it's not active, so clear
15410 it. */
15411 int yb = window_text_bottom_y (w);
15412 struct glyph_row *row;
15413 int y;
15415 for (y = 0, row = w->desired_matrix->rows;
15416 y < yb;
15417 y += row->height, ++row)
15418 blank_row (w, row, y);
15419 goto finish_scroll_bars;
15422 clear_glyph_matrix (w->desired_matrix);
15425 /* Otherwise set up data on this window; select its buffer and point
15426 value. */
15427 /* Really select the buffer, for the sake of buffer-local
15428 variables. */
15429 set_buffer_internal_1 (XBUFFER (w->contents));
15431 current_matrix_up_to_date_p
15432 = (w->window_end_valid
15433 && !current_buffer->clip_changed
15434 && !current_buffer->prevent_redisplay_optimizations_p
15435 && !window_outdated (w));
15437 /* Run the window-bottom-change-functions
15438 if it is possible that the text on the screen has changed
15439 (either due to modification of the text, or any other reason). */
15440 if (!current_matrix_up_to_date_p
15441 && !NILP (Vwindow_text_change_functions))
15443 safe_run_hooks (Qwindow_text_change_functions);
15444 goto restart;
15447 beg_unchanged = BEG_UNCHANGED;
15448 end_unchanged = END_UNCHANGED;
15450 SET_TEXT_POS (opoint, PT, PT_BYTE);
15452 specbind (Qinhibit_point_motion_hooks, Qt);
15454 buffer_unchanged_p
15455 = (w->window_end_valid
15456 && !current_buffer->clip_changed
15457 && !window_outdated (w));
15459 /* When windows_or_buffers_changed is non-zero, we can't rely
15460 on the window end being valid, so set it to zero there. */
15461 if (windows_or_buffers_changed)
15463 /* If window starts on a continuation line, maybe adjust the
15464 window start in case the window's width changed. */
15465 if (XMARKER (w->start)->buffer == current_buffer)
15466 compute_window_start_on_continuation_line (w);
15468 w->window_end_valid = 0;
15469 /* If so, we also can't rely on current matrix
15470 and should not fool try_cursor_movement below. */
15471 current_matrix_up_to_date_p = 0;
15474 /* Some sanity checks. */
15475 CHECK_WINDOW_END (w);
15476 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15477 emacs_abort ();
15478 if (BYTEPOS (opoint) < CHARPOS (opoint))
15479 emacs_abort ();
15481 if (mode_line_update_needed (w))
15482 update_mode_line = 1;
15484 /* Point refers normally to the selected window. For any other
15485 window, set up appropriate value. */
15486 if (!EQ (window, selected_window))
15488 ptrdiff_t new_pt = marker_position (w->pointm);
15489 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15490 if (new_pt < BEGV)
15492 new_pt = BEGV;
15493 new_pt_byte = BEGV_BYTE;
15494 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15496 else if (new_pt > (ZV - 1))
15498 new_pt = ZV;
15499 new_pt_byte = ZV_BYTE;
15500 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15503 /* We don't use SET_PT so that the point-motion hooks don't run. */
15504 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15507 /* If any of the character widths specified in the display table
15508 have changed, invalidate the width run cache. It's true that
15509 this may be a bit late to catch such changes, but the rest of
15510 redisplay goes (non-fatally) haywire when the display table is
15511 changed, so why should we worry about doing any better? */
15512 if (current_buffer->width_run_cache)
15514 struct Lisp_Char_Table *disptab = buffer_display_table ();
15516 if (! disptab_matches_widthtab
15517 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15519 invalidate_region_cache (current_buffer,
15520 current_buffer->width_run_cache,
15521 BEG, Z);
15522 recompute_width_table (current_buffer, disptab);
15526 /* If window-start is screwed up, choose a new one. */
15527 if (XMARKER (w->start)->buffer != current_buffer)
15528 goto recenter;
15530 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15532 /* If someone specified a new starting point but did not insist,
15533 check whether it can be used. */
15534 if (w->optional_new_start
15535 && CHARPOS (startp) >= BEGV
15536 && CHARPOS (startp) <= ZV)
15538 w->optional_new_start = 0;
15539 start_display (&it, w, startp);
15540 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15541 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15542 if (IT_CHARPOS (it) == PT)
15543 w->force_start = 1;
15544 /* IT may overshoot PT if text at PT is invisible. */
15545 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15546 w->force_start = 1;
15549 force_start:
15551 /* Handle case where place to start displaying has been specified,
15552 unless the specified location is outside the accessible range. */
15553 if (w->force_start || window_frozen_p (w))
15555 /* We set this later on if we have to adjust point. */
15556 int new_vpos = -1;
15558 w->force_start = 0;
15559 w->vscroll = 0;
15560 w->window_end_valid = 0;
15562 /* Forget any recorded base line for line number display. */
15563 if (!buffer_unchanged_p)
15564 w->base_line_number = 0;
15566 /* Redisplay the mode line. Select the buffer properly for that.
15567 Also, run the hook window-scroll-functions
15568 because we have scrolled. */
15569 /* Note, we do this after clearing force_start because
15570 if there's an error, it is better to forget about force_start
15571 than to get into an infinite loop calling the hook functions
15572 and having them get more errors. */
15573 if (!update_mode_line
15574 || ! NILP (Vwindow_scroll_functions))
15576 update_mode_line = 1;
15577 w->update_mode_line = 1;
15578 startp = run_window_scroll_functions (window, startp);
15581 if (CHARPOS (startp) < BEGV)
15582 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15583 else if (CHARPOS (startp) > ZV)
15584 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15586 /* Redisplay, then check if cursor has been set during the
15587 redisplay. Give up if new fonts were loaded. */
15588 /* We used to issue a CHECK_MARGINS argument to try_window here,
15589 but this causes scrolling to fail when point begins inside
15590 the scroll margin (bug#148) -- cyd */
15591 if (!try_window (window, startp, 0))
15593 w->force_start = 1;
15594 clear_glyph_matrix (w->desired_matrix);
15595 goto need_larger_matrices;
15598 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15600 /* If point does not appear, try to move point so it does
15601 appear. The desired matrix has been built above, so we
15602 can use it here. */
15603 new_vpos = window_box_height (w) / 2;
15606 if (!cursor_row_fully_visible_p (w, 0, 0))
15608 /* Point does appear, but on a line partly visible at end of window.
15609 Move it back to a fully-visible line. */
15610 new_vpos = window_box_height (w);
15612 else if (w->cursor.vpos >=0)
15614 /* Some people insist on not letting point enter the scroll
15615 margin, even though this part handles windows that didn't
15616 scroll at all. */
15617 int window_total_lines
15618 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15619 int margin = min (scroll_margin, window_total_lines / 4);
15620 int pixel_margin = margin * frame_line_height;
15621 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15623 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15624 below, which finds the row to move point to, advances by
15625 the Y coordinate of the _next_ row, see the definition of
15626 MATRIX_ROW_BOTTOM_Y. */
15627 if (w->cursor.vpos < margin + header_line)
15629 w->cursor.vpos = -1;
15630 clear_glyph_matrix (w->desired_matrix);
15631 goto try_to_scroll;
15633 else
15635 int window_height = window_box_height (w);
15637 if (header_line)
15638 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15639 if (w->cursor.y >= window_height - pixel_margin)
15641 w->cursor.vpos = -1;
15642 clear_glyph_matrix (w->desired_matrix);
15643 goto try_to_scroll;
15648 /* If we need to move point for either of the above reasons,
15649 now actually do it. */
15650 if (new_vpos >= 0)
15652 struct glyph_row *row;
15654 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15655 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15656 ++row;
15658 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15659 MATRIX_ROW_START_BYTEPOS (row));
15661 if (w != XWINDOW (selected_window))
15662 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15663 else if (current_buffer == old)
15664 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15666 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15668 /* If we are highlighting the region, then we just changed
15669 the region, so redisplay to show it. */
15670 if (markpos_of_region () >= 0)
15672 clear_glyph_matrix (w->desired_matrix);
15673 if (!try_window (window, startp, 0))
15674 goto need_larger_matrices;
15678 #ifdef GLYPH_DEBUG
15679 debug_method_add (w, "forced window start");
15680 #endif
15681 goto done;
15684 /* Handle case where text has not changed, only point, and it has
15685 not moved off the frame, and we are not retrying after hscroll.
15686 (current_matrix_up_to_date_p is nonzero when retrying.) */
15687 if (current_matrix_up_to_date_p
15688 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15689 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15691 switch (rc)
15693 case CURSOR_MOVEMENT_SUCCESS:
15694 used_current_matrix_p = 1;
15695 goto done;
15697 case CURSOR_MOVEMENT_MUST_SCROLL:
15698 goto try_to_scroll;
15700 default:
15701 emacs_abort ();
15704 /* If current starting point was originally the beginning of a line
15705 but no longer is, find a new starting point. */
15706 else if (w->start_at_line_beg
15707 && !(CHARPOS (startp) <= BEGV
15708 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15710 #ifdef GLYPH_DEBUG
15711 debug_method_add (w, "recenter 1");
15712 #endif
15713 goto recenter;
15716 /* Try scrolling with try_window_id. Value is > 0 if update has
15717 been done, it is -1 if we know that the same window start will
15718 not work. It is 0 if unsuccessful for some other reason. */
15719 else if ((tem = try_window_id (w)) != 0)
15721 #ifdef GLYPH_DEBUG
15722 debug_method_add (w, "try_window_id %d", tem);
15723 #endif
15725 if (f->fonts_changed)
15726 goto need_larger_matrices;
15727 if (tem > 0)
15728 goto done;
15730 /* Otherwise try_window_id has returned -1 which means that we
15731 don't want the alternative below this comment to execute. */
15733 else if (CHARPOS (startp) >= BEGV
15734 && CHARPOS (startp) <= ZV
15735 && PT >= CHARPOS (startp)
15736 && (CHARPOS (startp) < ZV
15737 /* Avoid starting at end of buffer. */
15738 || CHARPOS (startp) == BEGV
15739 || !window_outdated (w)))
15741 int d1, d2, d3, d4, d5, d6;
15743 /* If first window line is a continuation line, and window start
15744 is inside the modified region, but the first change is before
15745 current window start, we must select a new window start.
15747 However, if this is the result of a down-mouse event (e.g. by
15748 extending the mouse-drag-overlay), we don't want to select a
15749 new window start, since that would change the position under
15750 the mouse, resulting in an unwanted mouse-movement rather
15751 than a simple mouse-click. */
15752 if (!w->start_at_line_beg
15753 && NILP (do_mouse_tracking)
15754 && CHARPOS (startp) > BEGV
15755 && CHARPOS (startp) > BEG + beg_unchanged
15756 && CHARPOS (startp) <= Z - end_unchanged
15757 /* Even if w->start_at_line_beg is nil, a new window may
15758 start at a line_beg, since that's how set_buffer_window
15759 sets it. So, we need to check the return value of
15760 compute_window_start_on_continuation_line. (See also
15761 bug#197). */
15762 && XMARKER (w->start)->buffer == current_buffer
15763 && compute_window_start_on_continuation_line (w)
15764 /* It doesn't make sense to force the window start like we
15765 do at label force_start if it is already known that point
15766 will not be visible in the resulting window, because
15767 doing so will move point from its correct position
15768 instead of scrolling the window to bring point into view.
15769 See bug#9324. */
15770 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15772 w->force_start = 1;
15773 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15774 goto force_start;
15777 #ifdef GLYPH_DEBUG
15778 debug_method_add (w, "same window start");
15779 #endif
15781 /* Try to redisplay starting at same place as before.
15782 If point has not moved off frame, accept the results. */
15783 if (!current_matrix_up_to_date_p
15784 /* Don't use try_window_reusing_current_matrix in this case
15785 because a window scroll function can have changed the
15786 buffer. */
15787 || !NILP (Vwindow_scroll_functions)
15788 || MINI_WINDOW_P (w)
15789 || !(used_current_matrix_p
15790 = try_window_reusing_current_matrix (w)))
15792 IF_DEBUG (debug_method_add (w, "1"));
15793 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15794 /* -1 means we need to scroll.
15795 0 means we need new matrices, but fonts_changed
15796 is set in that case, so we will detect it below. */
15797 goto try_to_scroll;
15800 if (f->fonts_changed)
15801 goto need_larger_matrices;
15803 if (w->cursor.vpos >= 0)
15805 if (!just_this_one_p
15806 || current_buffer->clip_changed
15807 || BEG_UNCHANGED < CHARPOS (startp))
15808 /* Forget any recorded base line for line number display. */
15809 w->base_line_number = 0;
15811 if (!cursor_row_fully_visible_p (w, 1, 0))
15813 clear_glyph_matrix (w->desired_matrix);
15814 last_line_misfit = 1;
15816 /* Drop through and scroll. */
15817 else
15818 goto done;
15820 else
15821 clear_glyph_matrix (w->desired_matrix);
15824 try_to_scroll:
15826 /* Redisplay the mode line. Select the buffer properly for that. */
15827 if (!update_mode_line)
15829 update_mode_line = 1;
15830 w->update_mode_line = 1;
15833 /* Try to scroll by specified few lines. */
15834 if ((scroll_conservatively
15835 || emacs_scroll_step
15836 || temp_scroll_step
15837 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15838 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15839 && CHARPOS (startp) >= BEGV
15840 && CHARPOS (startp) <= ZV)
15842 /* The function returns -1 if new fonts were loaded, 1 if
15843 successful, 0 if not successful. */
15844 int ss = try_scrolling (window, just_this_one_p,
15845 scroll_conservatively,
15846 emacs_scroll_step,
15847 temp_scroll_step, last_line_misfit);
15848 switch (ss)
15850 case SCROLLING_SUCCESS:
15851 goto done;
15853 case SCROLLING_NEED_LARGER_MATRICES:
15854 goto need_larger_matrices;
15856 case SCROLLING_FAILED:
15857 break;
15859 default:
15860 emacs_abort ();
15864 /* Finally, just choose a place to start which positions point
15865 according to user preferences. */
15867 recenter:
15869 #ifdef GLYPH_DEBUG
15870 debug_method_add (w, "recenter");
15871 #endif
15873 /* Forget any previously recorded base line for line number display. */
15874 if (!buffer_unchanged_p)
15875 w->base_line_number = 0;
15877 /* Determine the window start relative to point. */
15878 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15879 it.current_y = it.last_visible_y;
15880 if (centering_position < 0)
15882 int window_total_lines
15883 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15884 int margin =
15885 scroll_margin > 0
15886 ? min (scroll_margin, window_total_lines / 4)
15887 : 0;
15888 ptrdiff_t margin_pos = CHARPOS (startp);
15889 Lisp_Object aggressive;
15890 int scrolling_up;
15892 /* If there is a scroll margin at the top of the window, find
15893 its character position. */
15894 if (margin
15895 /* Cannot call start_display if startp is not in the
15896 accessible region of the buffer. This can happen when we
15897 have just switched to a different buffer and/or changed
15898 its restriction. In that case, startp is initialized to
15899 the character position 1 (BEGV) because we did not yet
15900 have chance to display the buffer even once. */
15901 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15903 struct it it1;
15904 void *it1data = NULL;
15906 SAVE_IT (it1, it, it1data);
15907 start_display (&it1, w, startp);
15908 move_it_vertically (&it1, margin * frame_line_height);
15909 margin_pos = IT_CHARPOS (it1);
15910 RESTORE_IT (&it, &it, it1data);
15912 scrolling_up = PT > margin_pos;
15913 aggressive =
15914 scrolling_up
15915 ? BVAR (current_buffer, scroll_up_aggressively)
15916 : BVAR (current_buffer, scroll_down_aggressively);
15918 if (!MINI_WINDOW_P (w)
15919 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15921 int pt_offset = 0;
15923 /* Setting scroll-conservatively overrides
15924 scroll-*-aggressively. */
15925 if (!scroll_conservatively && NUMBERP (aggressive))
15927 double float_amount = XFLOATINT (aggressive);
15929 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15930 if (pt_offset == 0 && float_amount > 0)
15931 pt_offset = 1;
15932 if (pt_offset && margin > 0)
15933 margin -= 1;
15935 /* Compute how much to move the window start backward from
15936 point so that point will be displayed where the user
15937 wants it. */
15938 if (scrolling_up)
15940 centering_position = it.last_visible_y;
15941 if (pt_offset)
15942 centering_position -= pt_offset;
15943 centering_position -=
15944 frame_line_height * (1 + margin + (last_line_misfit != 0))
15945 + WINDOW_HEADER_LINE_HEIGHT (w);
15946 /* Don't let point enter the scroll margin near top of
15947 the window. */
15948 if (centering_position < margin * frame_line_height)
15949 centering_position = margin * frame_line_height;
15951 else
15952 centering_position = margin * frame_line_height + pt_offset;
15954 else
15955 /* Set the window start half the height of the window backward
15956 from point. */
15957 centering_position = window_box_height (w) / 2;
15959 move_it_vertically_backward (&it, centering_position);
15961 eassert (IT_CHARPOS (it) >= BEGV);
15963 /* The function move_it_vertically_backward may move over more
15964 than the specified y-distance. If it->w is small, e.g. a
15965 mini-buffer window, we may end up in front of the window's
15966 display area. Start displaying at the start of the line
15967 containing PT in this case. */
15968 if (it.current_y <= 0)
15970 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15971 move_it_vertically_backward (&it, 0);
15972 it.current_y = 0;
15975 it.current_x = it.hpos = 0;
15977 /* Set the window start position here explicitly, to avoid an
15978 infinite loop in case the functions in window-scroll-functions
15979 get errors. */
15980 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15982 /* Run scroll hooks. */
15983 startp = run_window_scroll_functions (window, it.current.pos);
15985 /* Redisplay the window. */
15986 if (!current_matrix_up_to_date_p
15987 || windows_or_buffers_changed
15988 || f->cursor_type_changed
15989 /* Don't use try_window_reusing_current_matrix in this case
15990 because it can have changed the buffer. */
15991 || !NILP (Vwindow_scroll_functions)
15992 || !just_this_one_p
15993 || MINI_WINDOW_P (w)
15994 || !(used_current_matrix_p
15995 = try_window_reusing_current_matrix (w)))
15996 try_window (window, startp, 0);
15998 /* If new fonts have been loaded (due to fontsets), give up. We
15999 have to start a new redisplay since we need to re-adjust glyph
16000 matrices. */
16001 if (f->fonts_changed)
16002 goto need_larger_matrices;
16004 /* If cursor did not appear assume that the middle of the window is
16005 in the first line of the window. Do it again with the next line.
16006 (Imagine a window of height 100, displaying two lines of height
16007 60. Moving back 50 from it->last_visible_y will end in the first
16008 line.) */
16009 if (w->cursor.vpos < 0)
16011 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16013 clear_glyph_matrix (w->desired_matrix);
16014 move_it_by_lines (&it, 1);
16015 try_window (window, it.current.pos, 0);
16017 else if (PT < IT_CHARPOS (it))
16019 clear_glyph_matrix (w->desired_matrix);
16020 move_it_by_lines (&it, -1);
16021 try_window (window, it.current.pos, 0);
16023 else
16025 /* Not much we can do about it. */
16029 /* Consider the following case: Window starts at BEGV, there is
16030 invisible, intangible text at BEGV, so that display starts at
16031 some point START > BEGV. It can happen that we are called with
16032 PT somewhere between BEGV and START. Try to handle that case. */
16033 if (w->cursor.vpos < 0)
16035 struct glyph_row *row = w->current_matrix->rows;
16036 if (row->mode_line_p)
16037 ++row;
16038 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16041 if (!cursor_row_fully_visible_p (w, 0, 0))
16043 /* If vscroll is enabled, disable it and try again. */
16044 if (w->vscroll)
16046 w->vscroll = 0;
16047 clear_glyph_matrix (w->desired_matrix);
16048 goto recenter;
16051 /* Users who set scroll-conservatively to a large number want
16052 point just above/below the scroll margin. If we ended up
16053 with point's row partially visible, move the window start to
16054 make that row fully visible and out of the margin. */
16055 if (scroll_conservatively > SCROLL_LIMIT)
16057 int window_total_lines
16058 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16059 int margin =
16060 scroll_margin > 0
16061 ? min (scroll_margin, window_total_lines / 4)
16062 : 0;
16063 int move_down = w->cursor.vpos >= window_total_lines / 2;
16065 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16066 clear_glyph_matrix (w->desired_matrix);
16067 if (1 == try_window (window, it.current.pos,
16068 TRY_WINDOW_CHECK_MARGINS))
16069 goto done;
16072 /* If centering point failed to make the whole line visible,
16073 put point at the top instead. That has to make the whole line
16074 visible, if it can be done. */
16075 if (centering_position == 0)
16076 goto done;
16078 clear_glyph_matrix (w->desired_matrix);
16079 centering_position = 0;
16080 goto recenter;
16083 done:
16085 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16086 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16087 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16089 /* Display the mode line, if we must. */
16090 if ((update_mode_line
16091 /* If window not full width, must redo its mode line
16092 if (a) the window to its side is being redone and
16093 (b) we do a frame-based redisplay. This is a consequence
16094 of how inverted lines are drawn in frame-based redisplay. */
16095 || (!just_this_one_p
16096 && !FRAME_WINDOW_P (f)
16097 && !WINDOW_FULL_WIDTH_P (w))
16098 /* Line number to display. */
16099 || w->base_line_pos > 0
16100 /* Column number is displayed and different from the one displayed. */
16101 || (w->column_number_displayed != -1
16102 && (w->column_number_displayed != current_column ())))
16103 /* This means that the window has a mode line. */
16104 && (WINDOW_WANTS_MODELINE_P (w)
16105 || WINDOW_WANTS_HEADER_LINE_P (w)))
16107 display_mode_lines (w);
16109 /* If mode line height has changed, arrange for a thorough
16110 immediate redisplay using the correct mode line height. */
16111 if (WINDOW_WANTS_MODELINE_P (w)
16112 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16114 f->fonts_changed = 1;
16115 w->mode_line_height = -1;
16116 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16117 = DESIRED_MODE_LINE_HEIGHT (w);
16120 /* If header line height has changed, arrange for a thorough
16121 immediate redisplay using the correct header line height. */
16122 if (WINDOW_WANTS_HEADER_LINE_P (w)
16123 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16125 f->fonts_changed = 1;
16126 w->header_line_height = -1;
16127 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16128 = DESIRED_HEADER_LINE_HEIGHT (w);
16131 if (f->fonts_changed)
16132 goto need_larger_matrices;
16135 if (!line_number_displayed && w->base_line_pos != -1)
16137 w->base_line_pos = 0;
16138 w->base_line_number = 0;
16141 finish_menu_bars:
16143 /* When we reach a frame's selected window, redo the frame's menu bar. */
16144 if (update_mode_line
16145 && EQ (FRAME_SELECTED_WINDOW (f), window))
16147 int redisplay_menu_p = 0;
16149 if (FRAME_WINDOW_P (f))
16151 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16152 || defined (HAVE_NS) || defined (USE_GTK)
16153 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16154 #else
16155 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16156 #endif
16158 else
16159 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16161 if (redisplay_menu_p)
16162 display_menu_bar (w);
16164 #ifdef HAVE_WINDOW_SYSTEM
16165 if (FRAME_WINDOW_P (f))
16167 #if defined (USE_GTK) || defined (HAVE_NS)
16168 if (FRAME_EXTERNAL_TOOL_BAR (f))
16169 redisplay_tool_bar (f);
16170 #else
16171 if (WINDOWP (f->tool_bar_window)
16172 && (FRAME_TOOL_BAR_LINES (f) > 0
16173 || !NILP (Vauto_resize_tool_bars))
16174 && redisplay_tool_bar (f))
16175 ignore_mouse_drag_p = 1;
16176 #endif
16178 #endif
16181 #ifdef HAVE_WINDOW_SYSTEM
16182 if (FRAME_WINDOW_P (f)
16183 && update_window_fringes (w, (just_this_one_p
16184 || (!used_current_matrix_p && !overlay_arrow_seen)
16185 || w->pseudo_window_p)))
16187 update_begin (f);
16188 block_input ();
16189 if (draw_window_fringes (w, 1))
16190 x_draw_vertical_border (w);
16191 unblock_input ();
16192 update_end (f);
16194 #endif /* HAVE_WINDOW_SYSTEM */
16196 /* We go to this label, with fonts_changed set, if it is
16197 necessary to try again using larger glyph matrices.
16198 We have to redeem the scroll bar even in this case,
16199 because the loop in redisplay_internal expects that. */
16200 need_larger_matrices:
16202 finish_scroll_bars:
16204 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16206 /* Set the thumb's position and size. */
16207 set_vertical_scroll_bar (w);
16209 /* Note that we actually used the scroll bar attached to this
16210 window, so it shouldn't be deleted at the end of redisplay. */
16211 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16212 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16215 /* Restore current_buffer and value of point in it. The window
16216 update may have changed the buffer, so first make sure `opoint'
16217 is still valid (Bug#6177). */
16218 if (CHARPOS (opoint) < BEGV)
16219 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16220 else if (CHARPOS (opoint) > ZV)
16221 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16222 else
16223 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16225 set_buffer_internal_1 (old);
16226 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16227 shorter. This can be caused by log truncation in *Messages*. */
16228 if (CHARPOS (lpoint) <= ZV)
16229 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16231 unbind_to (count, Qnil);
16235 /* Build the complete desired matrix of WINDOW with a window start
16236 buffer position POS.
16238 Value is 1 if successful. It is zero if fonts were loaded during
16239 redisplay which makes re-adjusting glyph matrices necessary, and -1
16240 if point would appear in the scroll margins.
16241 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16242 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16243 set in FLAGS.) */
16246 try_window (Lisp_Object window, struct text_pos pos, int flags)
16248 struct window *w = XWINDOW (window);
16249 struct it it;
16250 struct glyph_row *last_text_row = NULL;
16251 struct frame *f = XFRAME (w->frame);
16252 int frame_line_height = default_line_pixel_height (w);
16254 /* Make POS the new window start. */
16255 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16257 /* Mark cursor position as unknown. No overlay arrow seen. */
16258 w->cursor.vpos = -1;
16259 overlay_arrow_seen = 0;
16261 /* Initialize iterator and info to start at POS. */
16262 start_display (&it, w, pos);
16264 /* Display all lines of W. */
16265 while (it.current_y < it.last_visible_y)
16267 if (display_line (&it))
16268 last_text_row = it.glyph_row - 1;
16269 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16270 return 0;
16273 /* Don't let the cursor end in the scroll margins. */
16274 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16275 && !MINI_WINDOW_P (w))
16277 int this_scroll_margin;
16278 int window_total_lines
16279 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16281 if (scroll_margin > 0)
16283 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16284 this_scroll_margin *= frame_line_height;
16286 else
16287 this_scroll_margin = 0;
16289 if ((w->cursor.y >= 0 /* not vscrolled */
16290 && w->cursor.y < this_scroll_margin
16291 && CHARPOS (pos) > BEGV
16292 && IT_CHARPOS (it) < ZV)
16293 /* rms: considering make_cursor_line_fully_visible_p here
16294 seems to give wrong results. We don't want to recenter
16295 when the last line is partly visible, we want to allow
16296 that case to be handled in the usual way. */
16297 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16299 w->cursor.vpos = -1;
16300 clear_glyph_matrix (w->desired_matrix);
16301 return -1;
16305 /* If bottom moved off end of frame, change mode line percentage. */
16306 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16307 w->update_mode_line = 1;
16309 /* Set window_end_pos to the offset of the last character displayed
16310 on the window from the end of current_buffer. Set
16311 window_end_vpos to its row number. */
16312 if (last_text_row)
16314 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16315 adjust_window_ends (w, last_text_row, 0);
16316 eassert
16317 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16318 w->window_end_vpos)));
16320 else
16322 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16323 w->window_end_pos = Z - ZV;
16324 w->window_end_vpos = 0;
16327 /* But that is not valid info until redisplay finishes. */
16328 w->window_end_valid = 0;
16329 return 1;
16334 /************************************************************************
16335 Window redisplay reusing current matrix when buffer has not changed
16336 ************************************************************************/
16338 /* Try redisplay of window W showing an unchanged buffer with a
16339 different window start than the last time it was displayed by
16340 reusing its current matrix. Value is non-zero if successful.
16341 W->start is the new window start. */
16343 static int
16344 try_window_reusing_current_matrix (struct window *w)
16346 struct frame *f = XFRAME (w->frame);
16347 struct glyph_row *bottom_row;
16348 struct it it;
16349 struct run run;
16350 struct text_pos start, new_start;
16351 int nrows_scrolled, i;
16352 struct glyph_row *last_text_row;
16353 struct glyph_row *last_reused_text_row;
16354 struct glyph_row *start_row;
16355 int start_vpos, min_y, max_y;
16357 #ifdef GLYPH_DEBUG
16358 if (inhibit_try_window_reusing)
16359 return 0;
16360 #endif
16362 if (/* This function doesn't handle terminal frames. */
16363 !FRAME_WINDOW_P (f)
16364 /* Don't try to reuse the display if windows have been split
16365 or such. */
16366 || windows_or_buffers_changed
16367 || f->cursor_type_changed)
16368 return 0;
16370 /* Can't do this if region may have changed. */
16371 if (markpos_of_region () >= 0
16372 || w->region_showing
16373 || !NILP (Vshow_trailing_whitespace))
16374 return 0;
16376 /* If top-line visibility has changed, give up. */
16377 if (WINDOW_WANTS_HEADER_LINE_P (w)
16378 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16379 return 0;
16381 /* Give up if old or new display is scrolled vertically. We could
16382 make this function handle this, but right now it doesn't. */
16383 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16384 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16385 return 0;
16387 /* The variable new_start now holds the new window start. The old
16388 start `start' can be determined from the current matrix. */
16389 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16390 start = start_row->minpos;
16391 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16393 /* Clear the desired matrix for the display below. */
16394 clear_glyph_matrix (w->desired_matrix);
16396 if (CHARPOS (new_start) <= CHARPOS (start))
16398 /* Don't use this method if the display starts with an ellipsis
16399 displayed for invisible text. It's not easy to handle that case
16400 below, and it's certainly not worth the effort since this is
16401 not a frequent case. */
16402 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16403 return 0;
16405 IF_DEBUG (debug_method_add (w, "twu1"));
16407 /* Display up to a row that can be reused. The variable
16408 last_text_row is set to the last row displayed that displays
16409 text. Note that it.vpos == 0 if or if not there is a
16410 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16411 start_display (&it, w, new_start);
16412 w->cursor.vpos = -1;
16413 last_text_row = last_reused_text_row = NULL;
16415 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16417 /* If we have reached into the characters in the START row,
16418 that means the line boundaries have changed. So we
16419 can't start copying with the row START. Maybe it will
16420 work to start copying with the following row. */
16421 while (IT_CHARPOS (it) > CHARPOS (start))
16423 /* Advance to the next row as the "start". */
16424 start_row++;
16425 start = start_row->minpos;
16426 /* If there are no more rows to try, or just one, give up. */
16427 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16428 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16429 || CHARPOS (start) == ZV)
16431 clear_glyph_matrix (w->desired_matrix);
16432 return 0;
16435 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16437 /* If we have reached alignment, we can copy the rest of the
16438 rows. */
16439 if (IT_CHARPOS (it) == CHARPOS (start)
16440 /* Don't accept "alignment" inside a display vector,
16441 since start_row could have started in the middle of
16442 that same display vector (thus their character
16443 positions match), and we have no way of telling if
16444 that is the case. */
16445 && it.current.dpvec_index < 0)
16446 break;
16448 if (display_line (&it))
16449 last_text_row = it.glyph_row - 1;
16453 /* A value of current_y < last_visible_y means that we stopped
16454 at the previous window start, which in turn means that we
16455 have at least one reusable row. */
16456 if (it.current_y < it.last_visible_y)
16458 struct glyph_row *row;
16460 /* IT.vpos always starts from 0; it counts text lines. */
16461 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16463 /* Find PT if not already found in the lines displayed. */
16464 if (w->cursor.vpos < 0)
16466 int dy = it.current_y - start_row->y;
16468 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16469 row = row_containing_pos (w, PT, row, NULL, dy);
16470 if (row)
16471 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16472 dy, nrows_scrolled);
16473 else
16475 clear_glyph_matrix (w->desired_matrix);
16476 return 0;
16480 /* Scroll the display. Do it before the current matrix is
16481 changed. The problem here is that update has not yet
16482 run, i.e. part of the current matrix is not up to date.
16483 scroll_run_hook will clear the cursor, and use the
16484 current matrix to get the height of the row the cursor is
16485 in. */
16486 run.current_y = start_row->y;
16487 run.desired_y = it.current_y;
16488 run.height = it.last_visible_y - it.current_y;
16490 if (run.height > 0 && run.current_y != run.desired_y)
16492 update_begin (f);
16493 FRAME_RIF (f)->update_window_begin_hook (w);
16494 FRAME_RIF (f)->clear_window_mouse_face (w);
16495 FRAME_RIF (f)->scroll_run_hook (w, &run);
16496 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16497 update_end (f);
16500 /* Shift current matrix down by nrows_scrolled lines. */
16501 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16502 rotate_matrix (w->current_matrix,
16503 start_vpos,
16504 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16505 nrows_scrolled);
16507 /* Disable lines that must be updated. */
16508 for (i = 0; i < nrows_scrolled; ++i)
16509 (start_row + i)->enabled_p = 0;
16511 /* Re-compute Y positions. */
16512 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16513 max_y = it.last_visible_y;
16514 for (row = start_row + nrows_scrolled;
16515 row < bottom_row;
16516 ++row)
16518 row->y = it.current_y;
16519 row->visible_height = row->height;
16521 if (row->y < min_y)
16522 row->visible_height -= min_y - row->y;
16523 if (row->y + row->height > max_y)
16524 row->visible_height -= row->y + row->height - max_y;
16525 if (row->fringe_bitmap_periodic_p)
16526 row->redraw_fringe_bitmaps_p = 1;
16528 it.current_y += row->height;
16530 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16531 last_reused_text_row = row;
16532 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16533 break;
16536 /* Disable lines in the current matrix which are now
16537 below the window. */
16538 for (++row; row < bottom_row; ++row)
16539 row->enabled_p = row->mode_line_p = 0;
16542 /* Update window_end_pos etc.; last_reused_text_row is the last
16543 reused row from the current matrix containing text, if any.
16544 The value of last_text_row is the last displayed line
16545 containing text. */
16546 if (last_reused_text_row)
16547 adjust_window_ends (w, last_reused_text_row, 1);
16548 else if (last_text_row)
16549 adjust_window_ends (w, last_text_row, 0);
16550 else
16552 /* This window must be completely empty. */
16553 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16554 w->window_end_pos = Z - ZV;
16555 w->window_end_vpos = 0;
16557 w->window_end_valid = 0;
16559 /* Update hint: don't try scrolling again in update_window. */
16560 w->desired_matrix->no_scrolling_p = 1;
16562 #ifdef GLYPH_DEBUG
16563 debug_method_add (w, "try_window_reusing_current_matrix 1");
16564 #endif
16565 return 1;
16567 else if (CHARPOS (new_start) > CHARPOS (start))
16569 struct glyph_row *pt_row, *row;
16570 struct glyph_row *first_reusable_row;
16571 struct glyph_row *first_row_to_display;
16572 int dy;
16573 int yb = window_text_bottom_y (w);
16575 /* Find the row starting at new_start, if there is one. Don't
16576 reuse a partially visible line at the end. */
16577 first_reusable_row = start_row;
16578 while (first_reusable_row->enabled_p
16579 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16580 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16581 < CHARPOS (new_start)))
16582 ++first_reusable_row;
16584 /* Give up if there is no row to reuse. */
16585 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16586 || !first_reusable_row->enabled_p
16587 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16588 != CHARPOS (new_start)))
16589 return 0;
16591 /* We can reuse fully visible rows beginning with
16592 first_reusable_row to the end of the window. Set
16593 first_row_to_display to the first row that cannot be reused.
16594 Set pt_row to the row containing point, if there is any. */
16595 pt_row = NULL;
16596 for (first_row_to_display = first_reusable_row;
16597 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16598 ++first_row_to_display)
16600 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16601 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16602 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16603 && first_row_to_display->ends_at_zv_p
16604 && pt_row == NULL)))
16605 pt_row = first_row_to_display;
16608 /* Start displaying at the start of first_row_to_display. */
16609 eassert (first_row_to_display->y < yb);
16610 init_to_row_start (&it, w, first_row_to_display);
16612 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16613 - start_vpos);
16614 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16615 - nrows_scrolled);
16616 it.current_y = (first_row_to_display->y - first_reusable_row->y
16617 + WINDOW_HEADER_LINE_HEIGHT (w));
16619 /* Display lines beginning with first_row_to_display in the
16620 desired matrix. Set last_text_row to the last row displayed
16621 that displays text. */
16622 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16623 if (pt_row == NULL)
16624 w->cursor.vpos = -1;
16625 last_text_row = NULL;
16626 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16627 if (display_line (&it))
16628 last_text_row = it.glyph_row - 1;
16630 /* If point is in a reused row, adjust y and vpos of the cursor
16631 position. */
16632 if (pt_row)
16634 w->cursor.vpos -= nrows_scrolled;
16635 w->cursor.y -= first_reusable_row->y - start_row->y;
16638 /* Give up if point isn't in a row displayed or reused. (This
16639 also handles the case where w->cursor.vpos < nrows_scrolled
16640 after the calls to display_line, which can happen with scroll
16641 margins. See bug#1295.) */
16642 if (w->cursor.vpos < 0)
16644 clear_glyph_matrix (w->desired_matrix);
16645 return 0;
16648 /* Scroll the display. */
16649 run.current_y = first_reusable_row->y;
16650 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16651 run.height = it.last_visible_y - run.current_y;
16652 dy = run.current_y - run.desired_y;
16654 if (run.height)
16656 update_begin (f);
16657 FRAME_RIF (f)->update_window_begin_hook (w);
16658 FRAME_RIF (f)->clear_window_mouse_face (w);
16659 FRAME_RIF (f)->scroll_run_hook (w, &run);
16660 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16661 update_end (f);
16664 /* Adjust Y positions of reused rows. */
16665 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16666 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16667 max_y = it.last_visible_y;
16668 for (row = first_reusable_row; row < first_row_to_display; ++row)
16670 row->y -= dy;
16671 row->visible_height = row->height;
16672 if (row->y < min_y)
16673 row->visible_height -= min_y - row->y;
16674 if (row->y + row->height > max_y)
16675 row->visible_height -= row->y + row->height - max_y;
16676 if (row->fringe_bitmap_periodic_p)
16677 row->redraw_fringe_bitmaps_p = 1;
16680 /* Scroll the current matrix. */
16681 eassert (nrows_scrolled > 0);
16682 rotate_matrix (w->current_matrix,
16683 start_vpos,
16684 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16685 -nrows_scrolled);
16687 /* Disable rows not reused. */
16688 for (row -= nrows_scrolled; row < bottom_row; ++row)
16689 row->enabled_p = 0;
16691 /* Point may have moved to a different line, so we cannot assume that
16692 the previous cursor position is valid; locate the correct row. */
16693 if (pt_row)
16695 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16696 row < bottom_row
16697 && PT >= MATRIX_ROW_END_CHARPOS (row)
16698 && !row->ends_at_zv_p;
16699 row++)
16701 w->cursor.vpos++;
16702 w->cursor.y = row->y;
16704 if (row < bottom_row)
16706 /* Can't simply scan the row for point with
16707 bidi-reordered glyph rows. Let set_cursor_from_row
16708 figure out where to put the cursor, and if it fails,
16709 give up. */
16710 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16712 if (!set_cursor_from_row (w, row, w->current_matrix,
16713 0, 0, 0, 0))
16715 clear_glyph_matrix (w->desired_matrix);
16716 return 0;
16719 else
16721 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16722 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16724 for (; glyph < end
16725 && (!BUFFERP (glyph->object)
16726 || glyph->charpos < PT);
16727 glyph++)
16729 w->cursor.hpos++;
16730 w->cursor.x += glyph->pixel_width;
16736 /* Adjust window end. A null value of last_text_row means that
16737 the window end is in reused rows which in turn means that
16738 only its vpos can have changed. */
16739 if (last_text_row)
16740 adjust_window_ends (w, last_text_row, 0);
16741 else
16742 w->window_end_vpos -= nrows_scrolled;
16744 w->window_end_valid = 0;
16745 w->desired_matrix->no_scrolling_p = 1;
16747 #ifdef GLYPH_DEBUG
16748 debug_method_add (w, "try_window_reusing_current_matrix 2");
16749 #endif
16750 return 1;
16753 return 0;
16758 /************************************************************************
16759 Window redisplay reusing current matrix when buffer has changed
16760 ************************************************************************/
16762 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16763 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16764 ptrdiff_t *, ptrdiff_t *);
16765 static struct glyph_row *
16766 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16767 struct glyph_row *);
16770 /* Return the last row in MATRIX displaying text. If row START is
16771 non-null, start searching with that row. IT gives the dimensions
16772 of the display. Value is null if matrix is empty; otherwise it is
16773 a pointer to the row found. */
16775 static struct glyph_row *
16776 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16777 struct glyph_row *start)
16779 struct glyph_row *row, *row_found;
16781 /* Set row_found to the last row in IT->w's current matrix
16782 displaying text. The loop looks funny but think of partially
16783 visible lines. */
16784 row_found = NULL;
16785 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16786 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16788 eassert (row->enabled_p);
16789 row_found = row;
16790 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16791 break;
16792 ++row;
16795 return row_found;
16799 /* Return the last row in the current matrix of W that is not affected
16800 by changes at the start of current_buffer that occurred since W's
16801 current matrix was built. Value is null if no such row exists.
16803 BEG_UNCHANGED us the number of characters unchanged at the start of
16804 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16805 first changed character in current_buffer. Characters at positions <
16806 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16807 when the current matrix was built. */
16809 static struct glyph_row *
16810 find_last_unchanged_at_beg_row (struct window *w)
16812 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16813 struct glyph_row *row;
16814 struct glyph_row *row_found = NULL;
16815 int yb = window_text_bottom_y (w);
16817 /* Find the last row displaying unchanged text. */
16818 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16819 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16820 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16821 ++row)
16823 if (/* If row ends before first_changed_pos, it is unchanged,
16824 except in some case. */
16825 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16826 /* When row ends in ZV and we write at ZV it is not
16827 unchanged. */
16828 && !row->ends_at_zv_p
16829 /* When first_changed_pos is the end of a continued line,
16830 row is not unchanged because it may be no longer
16831 continued. */
16832 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16833 && (row->continued_p
16834 || row->exact_window_width_line_p))
16835 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16836 needs to be recomputed, so don't consider this row as
16837 unchanged. This happens when the last line was
16838 bidi-reordered and was killed immediately before this
16839 redisplay cycle. In that case, ROW->end stores the
16840 buffer position of the first visual-order character of
16841 the killed text, which is now beyond ZV. */
16842 && CHARPOS (row->end.pos) <= ZV)
16843 row_found = row;
16845 /* Stop if last visible row. */
16846 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16847 break;
16850 return row_found;
16854 /* Find the first glyph row in the current matrix of W that is not
16855 affected by changes at the end of current_buffer since the
16856 time W's current matrix was built.
16858 Return in *DELTA the number of chars by which buffer positions in
16859 unchanged text at the end of current_buffer must be adjusted.
16861 Return in *DELTA_BYTES the corresponding number of bytes.
16863 Value is null if no such row exists, i.e. all rows are affected by
16864 changes. */
16866 static struct glyph_row *
16867 find_first_unchanged_at_end_row (struct window *w,
16868 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16870 struct glyph_row *row;
16871 struct glyph_row *row_found = NULL;
16873 *delta = *delta_bytes = 0;
16875 /* Display must not have been paused, otherwise the current matrix
16876 is not up to date. */
16877 eassert (w->window_end_valid);
16879 /* A value of window_end_pos >= END_UNCHANGED means that the window
16880 end is in the range of changed text. If so, there is no
16881 unchanged row at the end of W's current matrix. */
16882 if (w->window_end_pos >= END_UNCHANGED)
16883 return NULL;
16885 /* Set row to the last row in W's current matrix displaying text. */
16886 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16888 /* If matrix is entirely empty, no unchanged row exists. */
16889 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16891 /* The value of row is the last glyph row in the matrix having a
16892 meaningful buffer position in it. The end position of row
16893 corresponds to window_end_pos. This allows us to translate
16894 buffer positions in the current matrix to current buffer
16895 positions for characters not in changed text. */
16896 ptrdiff_t Z_old =
16897 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16898 ptrdiff_t Z_BYTE_old =
16899 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16900 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16901 struct glyph_row *first_text_row
16902 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16904 *delta = Z - Z_old;
16905 *delta_bytes = Z_BYTE - Z_BYTE_old;
16907 /* Set last_unchanged_pos to the buffer position of the last
16908 character in the buffer that has not been changed. Z is the
16909 index + 1 of the last character in current_buffer, i.e. by
16910 subtracting END_UNCHANGED we get the index of the last
16911 unchanged character, and we have to add BEG to get its buffer
16912 position. */
16913 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16914 last_unchanged_pos_old = last_unchanged_pos - *delta;
16916 /* Search backward from ROW for a row displaying a line that
16917 starts at a minimum position >= last_unchanged_pos_old. */
16918 for (; row > first_text_row; --row)
16920 /* This used to abort, but it can happen.
16921 It is ok to just stop the search instead here. KFS. */
16922 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16923 break;
16925 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16926 row_found = row;
16930 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16932 return row_found;
16936 /* Make sure that glyph rows in the current matrix of window W
16937 reference the same glyph memory as corresponding rows in the
16938 frame's frame matrix. This function is called after scrolling W's
16939 current matrix on a terminal frame in try_window_id and
16940 try_window_reusing_current_matrix. */
16942 static void
16943 sync_frame_with_window_matrix_rows (struct window *w)
16945 struct frame *f = XFRAME (w->frame);
16946 struct glyph_row *window_row, *window_row_end, *frame_row;
16948 /* Preconditions: W must be a leaf window and full-width. Its frame
16949 must have a frame matrix. */
16950 eassert (BUFFERP (w->contents));
16951 eassert (WINDOW_FULL_WIDTH_P (w));
16952 eassert (!FRAME_WINDOW_P (f));
16954 /* If W is a full-width window, glyph pointers in W's current matrix
16955 have, by definition, to be the same as glyph pointers in the
16956 corresponding frame matrix. Note that frame matrices have no
16957 marginal areas (see build_frame_matrix). */
16958 window_row = w->current_matrix->rows;
16959 window_row_end = window_row + w->current_matrix->nrows;
16960 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16961 while (window_row < window_row_end)
16963 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16964 struct glyph *end = window_row->glyphs[LAST_AREA];
16966 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16967 frame_row->glyphs[TEXT_AREA] = start;
16968 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16969 frame_row->glyphs[LAST_AREA] = end;
16971 /* Disable frame rows whose corresponding window rows have
16972 been disabled in try_window_id. */
16973 if (!window_row->enabled_p)
16974 frame_row->enabled_p = 0;
16976 ++window_row, ++frame_row;
16981 /* Find the glyph row in window W containing CHARPOS. Consider all
16982 rows between START and END (not inclusive). END null means search
16983 all rows to the end of the display area of W. Value is the row
16984 containing CHARPOS or null. */
16986 struct glyph_row *
16987 row_containing_pos (struct window *w, ptrdiff_t charpos,
16988 struct glyph_row *start, struct glyph_row *end, int dy)
16990 struct glyph_row *row = start;
16991 struct glyph_row *best_row = NULL;
16992 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16993 int last_y;
16995 /* If we happen to start on a header-line, skip that. */
16996 if (row->mode_line_p)
16997 ++row;
16999 if ((end && row >= end) || !row->enabled_p)
17000 return NULL;
17002 last_y = window_text_bottom_y (w) - dy;
17004 while (1)
17006 /* Give up if we have gone too far. */
17007 if (end && row >= end)
17008 return NULL;
17009 /* This formerly returned if they were equal.
17010 I think that both quantities are of a "last plus one" type;
17011 if so, when they are equal, the row is within the screen. -- rms. */
17012 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17013 return NULL;
17015 /* If it is in this row, return this row. */
17016 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17017 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17018 /* The end position of a row equals the start
17019 position of the next row. If CHARPOS is there, we
17020 would rather consider it displayed in the next
17021 line, except when this line ends in ZV. */
17022 && !row_for_charpos_p (row, charpos)))
17023 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17025 struct glyph *g;
17027 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17028 || (!best_row && !row->continued_p))
17029 return row;
17030 /* In bidi-reordered rows, there could be several rows whose
17031 edges surround CHARPOS, all of these rows belonging to
17032 the same continued line. We need to find the row which
17033 fits CHARPOS the best. */
17034 for (g = row->glyphs[TEXT_AREA];
17035 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17036 g++)
17038 if (!STRINGP (g->object))
17040 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17042 mindif = eabs (g->charpos - charpos);
17043 best_row = row;
17044 /* Exact match always wins. */
17045 if (mindif == 0)
17046 return best_row;
17051 else if (best_row && !row->continued_p)
17052 return best_row;
17053 ++row;
17058 /* Try to redisplay window W by reusing its existing display. W's
17059 current matrix must be up to date when this function is called,
17060 i.e. window_end_valid must be nonzero.
17062 Value is
17064 1 if display has been updated
17065 0 if otherwise unsuccessful
17066 -1 if redisplay with same window start is known not to succeed
17068 The following steps are performed:
17070 1. Find the last row in the current matrix of W that is not
17071 affected by changes at the start of current_buffer. If no such row
17072 is found, give up.
17074 2. Find the first row in W's current matrix that is not affected by
17075 changes at the end of current_buffer. Maybe there is no such row.
17077 3. Display lines beginning with the row + 1 found in step 1 to the
17078 row found in step 2 or, if step 2 didn't find a row, to the end of
17079 the window.
17081 4. If cursor is not known to appear on the window, give up.
17083 5. If display stopped at the row found in step 2, scroll the
17084 display and current matrix as needed.
17086 6. Maybe display some lines at the end of W, if we must. This can
17087 happen under various circumstances, like a partially visible line
17088 becoming fully visible, or because newly displayed lines are displayed
17089 in smaller font sizes.
17091 7. Update W's window end information. */
17093 static int
17094 try_window_id (struct window *w)
17096 struct frame *f = XFRAME (w->frame);
17097 struct glyph_matrix *current_matrix = w->current_matrix;
17098 struct glyph_matrix *desired_matrix = w->desired_matrix;
17099 struct glyph_row *last_unchanged_at_beg_row;
17100 struct glyph_row *first_unchanged_at_end_row;
17101 struct glyph_row *row;
17102 struct glyph_row *bottom_row;
17103 int bottom_vpos;
17104 struct it it;
17105 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17106 int dvpos, dy;
17107 struct text_pos start_pos;
17108 struct run run;
17109 int first_unchanged_at_end_vpos = 0;
17110 struct glyph_row *last_text_row, *last_text_row_at_end;
17111 struct text_pos start;
17112 ptrdiff_t first_changed_charpos, last_changed_charpos;
17114 #ifdef GLYPH_DEBUG
17115 if (inhibit_try_window_id)
17116 return 0;
17117 #endif
17119 /* This is handy for debugging. */
17120 #if 0
17121 #define GIVE_UP(X) \
17122 do { \
17123 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17124 return 0; \
17125 } while (0)
17126 #else
17127 #define GIVE_UP(X) return 0
17128 #endif
17130 SET_TEXT_POS_FROM_MARKER (start, w->start);
17132 /* Don't use this for mini-windows because these can show
17133 messages and mini-buffers, and we don't handle that here. */
17134 if (MINI_WINDOW_P (w))
17135 GIVE_UP (1);
17137 /* This flag is used to prevent redisplay optimizations. */
17138 if (windows_or_buffers_changed || f->cursor_type_changed)
17139 GIVE_UP (2);
17141 /* Verify that narrowing has not changed.
17142 Also verify that we were not told to prevent redisplay optimizations.
17143 It would be nice to further
17144 reduce the number of cases where this prevents try_window_id. */
17145 if (current_buffer->clip_changed
17146 || current_buffer->prevent_redisplay_optimizations_p)
17147 GIVE_UP (3);
17149 /* Window must either use window-based redisplay or be full width. */
17150 if (!FRAME_WINDOW_P (f)
17151 && (!FRAME_LINE_INS_DEL_OK (f)
17152 || !WINDOW_FULL_WIDTH_P (w)))
17153 GIVE_UP (4);
17155 /* Give up if point is known NOT to appear in W. */
17156 if (PT < CHARPOS (start))
17157 GIVE_UP (5);
17159 /* Another way to prevent redisplay optimizations. */
17160 if (w->last_modified == 0)
17161 GIVE_UP (6);
17163 /* Verify that window is not hscrolled. */
17164 if (w->hscroll != 0)
17165 GIVE_UP (7);
17167 /* Verify that display wasn't paused. */
17168 if (!w->window_end_valid)
17169 GIVE_UP (8);
17171 /* Can't use this if highlighting a region because a cursor movement
17172 will do more than just set the cursor. */
17173 if (markpos_of_region () >= 0)
17174 GIVE_UP (9);
17176 /* Likewise if highlighting trailing whitespace. */
17177 if (!NILP (Vshow_trailing_whitespace))
17178 GIVE_UP (11);
17180 /* Likewise if showing a region. */
17181 if (w->region_showing)
17182 GIVE_UP (10);
17184 /* Can't use this if overlay arrow position and/or string have
17185 changed. */
17186 if (overlay_arrows_changed_p ())
17187 GIVE_UP (12);
17189 /* When word-wrap is on, adding a space to the first word of a
17190 wrapped line can change the wrap position, altering the line
17191 above it. It might be worthwhile to handle this more
17192 intelligently, but for now just redisplay from scratch. */
17193 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17194 GIVE_UP (21);
17196 /* Under bidi reordering, adding or deleting a character in the
17197 beginning of a paragraph, before the first strong directional
17198 character, can change the base direction of the paragraph (unless
17199 the buffer specifies a fixed paragraph direction), which will
17200 require to redisplay the whole paragraph. It might be worthwhile
17201 to find the paragraph limits and widen the range of redisplayed
17202 lines to that, but for now just give up this optimization and
17203 redisplay from scratch. */
17204 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17205 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17206 GIVE_UP (22);
17208 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17209 only if buffer has really changed. The reason is that the gap is
17210 initially at Z for freshly visited files. The code below would
17211 set end_unchanged to 0 in that case. */
17212 if (MODIFF > SAVE_MODIFF
17213 /* This seems to happen sometimes after saving a buffer. */
17214 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17216 if (GPT - BEG < BEG_UNCHANGED)
17217 BEG_UNCHANGED = GPT - BEG;
17218 if (Z - GPT < END_UNCHANGED)
17219 END_UNCHANGED = Z - GPT;
17222 /* The position of the first and last character that has been changed. */
17223 first_changed_charpos = BEG + BEG_UNCHANGED;
17224 last_changed_charpos = Z - END_UNCHANGED;
17226 /* If window starts after a line end, and the last change is in
17227 front of that newline, then changes don't affect the display.
17228 This case happens with stealth-fontification. Note that although
17229 the display is unchanged, glyph positions in the matrix have to
17230 be adjusted, of course. */
17231 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17232 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17233 && ((last_changed_charpos < CHARPOS (start)
17234 && CHARPOS (start) == BEGV)
17235 || (last_changed_charpos < CHARPOS (start) - 1
17236 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17238 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17239 struct glyph_row *r0;
17241 /* Compute how many chars/bytes have been added to or removed
17242 from the buffer. */
17243 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17244 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17245 Z_delta = Z - Z_old;
17246 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17248 /* Give up if PT is not in the window. Note that it already has
17249 been checked at the start of try_window_id that PT is not in
17250 front of the window start. */
17251 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17252 GIVE_UP (13);
17254 /* If window start is unchanged, we can reuse the whole matrix
17255 as is, after adjusting glyph positions. No need to compute
17256 the window end again, since its offset from Z hasn't changed. */
17257 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17258 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17259 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17260 /* PT must not be in a partially visible line. */
17261 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17262 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17264 /* Adjust positions in the glyph matrix. */
17265 if (Z_delta || Z_delta_bytes)
17267 struct glyph_row *r1
17268 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17269 increment_matrix_positions (w->current_matrix,
17270 MATRIX_ROW_VPOS (r0, current_matrix),
17271 MATRIX_ROW_VPOS (r1, current_matrix),
17272 Z_delta, Z_delta_bytes);
17275 /* Set the cursor. */
17276 row = row_containing_pos (w, PT, r0, NULL, 0);
17277 if (row)
17278 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17279 else
17280 emacs_abort ();
17281 return 1;
17285 /* Handle the case that changes are all below what is displayed in
17286 the window, and that PT is in the window. This shortcut cannot
17287 be taken if ZV is visible in the window, and text has been added
17288 there that is visible in the window. */
17289 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17290 /* ZV is not visible in the window, or there are no
17291 changes at ZV, actually. */
17292 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17293 || first_changed_charpos == last_changed_charpos))
17295 struct glyph_row *r0;
17297 /* Give up if PT is not in the window. Note that it already has
17298 been checked at the start of try_window_id that PT is not in
17299 front of the window start. */
17300 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17301 GIVE_UP (14);
17303 /* If window start is unchanged, we can reuse the whole matrix
17304 as is, without changing glyph positions since no text has
17305 been added/removed in front of the window end. */
17306 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17307 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17308 /* PT must not be in a partially visible line. */
17309 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17310 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17312 /* We have to compute the window end anew since text
17313 could have been added/removed after it. */
17314 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17315 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17317 /* Set the cursor. */
17318 row = row_containing_pos (w, PT, r0, NULL, 0);
17319 if (row)
17320 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17321 else
17322 emacs_abort ();
17323 return 2;
17327 /* Give up if window start is in the changed area.
17329 The condition used to read
17331 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17333 but why that was tested escapes me at the moment. */
17334 if (CHARPOS (start) >= first_changed_charpos
17335 && CHARPOS (start) <= last_changed_charpos)
17336 GIVE_UP (15);
17338 /* Check that window start agrees with the start of the first glyph
17339 row in its current matrix. Check this after we know the window
17340 start is not in changed text, otherwise positions would not be
17341 comparable. */
17342 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17343 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17344 GIVE_UP (16);
17346 /* Give up if the window ends in strings. Overlay strings
17347 at the end are difficult to handle, so don't try. */
17348 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17349 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17350 GIVE_UP (20);
17352 /* Compute the position at which we have to start displaying new
17353 lines. Some of the lines at the top of the window might be
17354 reusable because they are not displaying changed text. Find the
17355 last row in W's current matrix not affected by changes at the
17356 start of current_buffer. Value is null if changes start in the
17357 first line of window. */
17358 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17359 if (last_unchanged_at_beg_row)
17361 /* Avoid starting to display in the middle of a character, a TAB
17362 for instance. This is easier than to set up the iterator
17363 exactly, and it's not a frequent case, so the additional
17364 effort wouldn't really pay off. */
17365 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17366 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17367 && last_unchanged_at_beg_row > w->current_matrix->rows)
17368 --last_unchanged_at_beg_row;
17370 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17371 GIVE_UP (17);
17373 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17374 GIVE_UP (18);
17375 start_pos = it.current.pos;
17377 /* Start displaying new lines in the desired matrix at the same
17378 vpos we would use in the current matrix, i.e. below
17379 last_unchanged_at_beg_row. */
17380 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17381 current_matrix);
17382 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17383 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17385 eassert (it.hpos == 0 && it.current_x == 0);
17387 else
17389 /* There are no reusable lines at the start of the window.
17390 Start displaying in the first text line. */
17391 start_display (&it, w, start);
17392 it.vpos = it.first_vpos;
17393 start_pos = it.current.pos;
17396 /* Find the first row that is not affected by changes at the end of
17397 the buffer. Value will be null if there is no unchanged row, in
17398 which case we must redisplay to the end of the window. delta
17399 will be set to the value by which buffer positions beginning with
17400 first_unchanged_at_end_row have to be adjusted due to text
17401 changes. */
17402 first_unchanged_at_end_row
17403 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17404 IF_DEBUG (debug_delta = delta);
17405 IF_DEBUG (debug_delta_bytes = delta_bytes);
17407 /* Set stop_pos to the buffer position up to which we will have to
17408 display new lines. If first_unchanged_at_end_row != NULL, this
17409 is the buffer position of the start of the line displayed in that
17410 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17411 that we don't stop at a buffer position. */
17412 stop_pos = 0;
17413 if (first_unchanged_at_end_row)
17415 eassert (last_unchanged_at_beg_row == NULL
17416 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17418 /* If this is a continuation line, move forward to the next one
17419 that isn't. Changes in lines above affect this line.
17420 Caution: this may move first_unchanged_at_end_row to a row
17421 not displaying text. */
17422 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17423 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17424 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17425 < it.last_visible_y))
17426 ++first_unchanged_at_end_row;
17428 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17429 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17430 >= it.last_visible_y))
17431 first_unchanged_at_end_row = NULL;
17432 else
17434 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17435 + delta);
17436 first_unchanged_at_end_vpos
17437 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17438 eassert (stop_pos >= Z - END_UNCHANGED);
17441 else if (last_unchanged_at_beg_row == NULL)
17442 GIVE_UP (19);
17445 #ifdef GLYPH_DEBUG
17447 /* Either there is no unchanged row at the end, or the one we have
17448 now displays text. This is a necessary condition for the window
17449 end pos calculation at the end of this function. */
17450 eassert (first_unchanged_at_end_row == NULL
17451 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17453 debug_last_unchanged_at_beg_vpos
17454 = (last_unchanged_at_beg_row
17455 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17456 : -1);
17457 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17459 #endif /* GLYPH_DEBUG */
17462 /* Display new lines. Set last_text_row to the last new line
17463 displayed which has text on it, i.e. might end up as being the
17464 line where the window_end_vpos is. */
17465 w->cursor.vpos = -1;
17466 last_text_row = NULL;
17467 overlay_arrow_seen = 0;
17468 while (it.current_y < it.last_visible_y
17469 && !f->fonts_changed
17470 && (first_unchanged_at_end_row == NULL
17471 || IT_CHARPOS (it) < stop_pos))
17473 if (display_line (&it))
17474 last_text_row = it.glyph_row - 1;
17477 if (f->fonts_changed)
17478 return -1;
17481 /* Compute differences in buffer positions, y-positions etc. for
17482 lines reused at the bottom of the window. Compute what we can
17483 scroll. */
17484 if (first_unchanged_at_end_row
17485 /* No lines reused because we displayed everything up to the
17486 bottom of the window. */
17487 && it.current_y < it.last_visible_y)
17489 dvpos = (it.vpos
17490 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17491 current_matrix));
17492 dy = it.current_y - first_unchanged_at_end_row->y;
17493 run.current_y = first_unchanged_at_end_row->y;
17494 run.desired_y = run.current_y + dy;
17495 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17497 else
17499 delta = delta_bytes = dvpos = dy
17500 = run.current_y = run.desired_y = run.height = 0;
17501 first_unchanged_at_end_row = NULL;
17503 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17506 /* Find the cursor if not already found. We have to decide whether
17507 PT will appear on this window (it sometimes doesn't, but this is
17508 not a very frequent case.) This decision has to be made before
17509 the current matrix is altered. A value of cursor.vpos < 0 means
17510 that PT is either in one of the lines beginning at
17511 first_unchanged_at_end_row or below the window. Don't care for
17512 lines that might be displayed later at the window end; as
17513 mentioned, this is not a frequent case. */
17514 if (w->cursor.vpos < 0)
17516 /* Cursor in unchanged rows at the top? */
17517 if (PT < CHARPOS (start_pos)
17518 && last_unchanged_at_beg_row)
17520 row = row_containing_pos (w, PT,
17521 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17522 last_unchanged_at_beg_row + 1, 0);
17523 if (row)
17524 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17527 /* Start from first_unchanged_at_end_row looking for PT. */
17528 else if (first_unchanged_at_end_row)
17530 row = row_containing_pos (w, PT - delta,
17531 first_unchanged_at_end_row, NULL, 0);
17532 if (row)
17533 set_cursor_from_row (w, row, w->current_matrix, delta,
17534 delta_bytes, dy, dvpos);
17537 /* Give up if cursor was not found. */
17538 if (w->cursor.vpos < 0)
17540 clear_glyph_matrix (w->desired_matrix);
17541 return -1;
17545 /* Don't let the cursor end in the scroll margins. */
17547 int this_scroll_margin, cursor_height;
17548 int frame_line_height = default_line_pixel_height (w);
17549 int window_total_lines
17550 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17552 this_scroll_margin =
17553 max (0, min (scroll_margin, window_total_lines / 4));
17554 this_scroll_margin *= frame_line_height;
17555 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17557 if ((w->cursor.y < this_scroll_margin
17558 && CHARPOS (start) > BEGV)
17559 /* Old redisplay didn't take scroll margin into account at the bottom,
17560 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17561 || (w->cursor.y + (make_cursor_line_fully_visible_p
17562 ? cursor_height + this_scroll_margin
17563 : 1)) > it.last_visible_y)
17565 w->cursor.vpos = -1;
17566 clear_glyph_matrix (w->desired_matrix);
17567 return -1;
17571 /* Scroll the display. Do it before changing the current matrix so
17572 that xterm.c doesn't get confused about where the cursor glyph is
17573 found. */
17574 if (dy && run.height)
17576 update_begin (f);
17578 if (FRAME_WINDOW_P (f))
17580 FRAME_RIF (f)->update_window_begin_hook (w);
17581 FRAME_RIF (f)->clear_window_mouse_face (w);
17582 FRAME_RIF (f)->scroll_run_hook (w, &run);
17583 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17585 else
17587 /* Terminal frame. In this case, dvpos gives the number of
17588 lines to scroll by; dvpos < 0 means scroll up. */
17589 int from_vpos
17590 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17591 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17592 int end = (WINDOW_TOP_EDGE_LINE (w)
17593 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17594 + window_internal_height (w));
17596 #if defined (HAVE_GPM) || defined (MSDOS)
17597 x_clear_window_mouse_face (w);
17598 #endif
17599 /* Perform the operation on the screen. */
17600 if (dvpos > 0)
17602 /* Scroll last_unchanged_at_beg_row to the end of the
17603 window down dvpos lines. */
17604 set_terminal_window (f, end);
17606 /* On dumb terminals delete dvpos lines at the end
17607 before inserting dvpos empty lines. */
17608 if (!FRAME_SCROLL_REGION_OK (f))
17609 ins_del_lines (f, end - dvpos, -dvpos);
17611 /* Insert dvpos empty lines in front of
17612 last_unchanged_at_beg_row. */
17613 ins_del_lines (f, from, dvpos);
17615 else if (dvpos < 0)
17617 /* Scroll up last_unchanged_at_beg_vpos to the end of
17618 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17619 set_terminal_window (f, end);
17621 /* Delete dvpos lines in front of
17622 last_unchanged_at_beg_vpos. ins_del_lines will set
17623 the cursor to the given vpos and emit |dvpos| delete
17624 line sequences. */
17625 ins_del_lines (f, from + dvpos, dvpos);
17627 /* On a dumb terminal insert dvpos empty lines at the
17628 end. */
17629 if (!FRAME_SCROLL_REGION_OK (f))
17630 ins_del_lines (f, end + dvpos, -dvpos);
17633 set_terminal_window (f, 0);
17636 update_end (f);
17639 /* Shift reused rows of the current matrix to the right position.
17640 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17641 text. */
17642 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17643 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17644 if (dvpos < 0)
17646 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17647 bottom_vpos, dvpos);
17648 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17649 bottom_vpos);
17651 else if (dvpos > 0)
17653 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17654 bottom_vpos, dvpos);
17655 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17656 first_unchanged_at_end_vpos + dvpos);
17659 /* For frame-based redisplay, make sure that current frame and window
17660 matrix are in sync with respect to glyph memory. */
17661 if (!FRAME_WINDOW_P (f))
17662 sync_frame_with_window_matrix_rows (w);
17664 /* Adjust buffer positions in reused rows. */
17665 if (delta || delta_bytes)
17666 increment_matrix_positions (current_matrix,
17667 first_unchanged_at_end_vpos + dvpos,
17668 bottom_vpos, delta, delta_bytes);
17670 /* Adjust Y positions. */
17671 if (dy)
17672 shift_glyph_matrix (w, current_matrix,
17673 first_unchanged_at_end_vpos + dvpos,
17674 bottom_vpos, dy);
17676 if (first_unchanged_at_end_row)
17678 first_unchanged_at_end_row += dvpos;
17679 if (first_unchanged_at_end_row->y >= it.last_visible_y
17680 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17681 first_unchanged_at_end_row = NULL;
17684 /* If scrolling up, there may be some lines to display at the end of
17685 the window. */
17686 last_text_row_at_end = NULL;
17687 if (dy < 0)
17689 /* Scrolling up can leave for example a partially visible line
17690 at the end of the window to be redisplayed. */
17691 /* Set last_row to the glyph row in the current matrix where the
17692 window end line is found. It has been moved up or down in
17693 the matrix by dvpos. */
17694 int last_vpos = w->window_end_vpos + dvpos;
17695 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17697 /* If last_row is the window end line, it should display text. */
17698 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17700 /* If window end line was partially visible before, begin
17701 displaying at that line. Otherwise begin displaying with the
17702 line following it. */
17703 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17705 init_to_row_start (&it, w, last_row);
17706 it.vpos = last_vpos;
17707 it.current_y = last_row->y;
17709 else
17711 init_to_row_end (&it, w, last_row);
17712 it.vpos = 1 + last_vpos;
17713 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17714 ++last_row;
17717 /* We may start in a continuation line. If so, we have to
17718 get the right continuation_lines_width and current_x. */
17719 it.continuation_lines_width = last_row->continuation_lines_width;
17720 it.hpos = it.current_x = 0;
17722 /* Display the rest of the lines at the window end. */
17723 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17724 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17726 /* Is it always sure that the display agrees with lines in
17727 the current matrix? I don't think so, so we mark rows
17728 displayed invalid in the current matrix by setting their
17729 enabled_p flag to zero. */
17730 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17731 if (display_line (&it))
17732 last_text_row_at_end = it.glyph_row - 1;
17736 /* Update window_end_pos and window_end_vpos. */
17737 if (first_unchanged_at_end_row && !last_text_row_at_end)
17739 /* Window end line if one of the preserved rows from the current
17740 matrix. Set row to the last row displaying text in current
17741 matrix starting at first_unchanged_at_end_row, after
17742 scrolling. */
17743 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17744 row = find_last_row_displaying_text (w->current_matrix, &it,
17745 first_unchanged_at_end_row);
17746 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17747 adjust_window_ends (w, row, 1);
17748 eassert (w->window_end_bytepos >= 0);
17749 IF_DEBUG (debug_method_add (w, "A"));
17751 else if (last_text_row_at_end)
17753 adjust_window_ends (w, last_text_row_at_end, 0);
17754 eassert (w->window_end_bytepos >= 0);
17755 IF_DEBUG (debug_method_add (w, "B"));
17757 else if (last_text_row)
17759 /* We have displayed either to the end of the window or at the
17760 end of the window, i.e. the last row with text is to be found
17761 in the desired matrix. */
17762 adjust_window_ends (w, last_text_row, 0);
17763 eassert (w->window_end_bytepos >= 0);
17765 else if (first_unchanged_at_end_row == NULL
17766 && last_text_row == NULL
17767 && last_text_row_at_end == NULL)
17769 /* Displayed to end of window, but no line containing text was
17770 displayed. Lines were deleted at the end of the window. */
17771 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17772 int vpos = w->window_end_vpos;
17773 struct glyph_row *current_row = current_matrix->rows + vpos;
17774 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17776 for (row = NULL;
17777 row == NULL && vpos >= first_vpos;
17778 --vpos, --current_row, --desired_row)
17780 if (desired_row->enabled_p)
17782 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17783 row = desired_row;
17785 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17786 row = current_row;
17789 eassert (row != NULL);
17790 w->window_end_vpos = vpos + 1;
17791 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17792 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17793 eassert (w->window_end_bytepos >= 0);
17794 IF_DEBUG (debug_method_add (w, "C"));
17796 else
17797 emacs_abort ();
17799 IF_DEBUG (debug_end_pos = w->window_end_pos;
17800 debug_end_vpos = w->window_end_vpos);
17802 /* Record that display has not been completed. */
17803 w->window_end_valid = 0;
17804 w->desired_matrix->no_scrolling_p = 1;
17805 return 3;
17807 #undef GIVE_UP
17812 /***********************************************************************
17813 More debugging support
17814 ***********************************************************************/
17816 #ifdef GLYPH_DEBUG
17818 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17819 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17820 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17823 /* Dump the contents of glyph matrix MATRIX on stderr.
17825 GLYPHS 0 means don't show glyph contents.
17826 GLYPHS 1 means show glyphs in short form
17827 GLYPHS > 1 means show glyphs in long form. */
17829 void
17830 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17832 int i;
17833 for (i = 0; i < matrix->nrows; ++i)
17834 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17838 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17839 the glyph row and area where the glyph comes from. */
17841 void
17842 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17844 if (glyph->type == CHAR_GLYPH
17845 || glyph->type == GLYPHLESS_GLYPH)
17847 fprintf (stderr,
17848 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17849 glyph - row->glyphs[TEXT_AREA],
17850 (glyph->type == CHAR_GLYPH
17851 ? 'C'
17852 : 'G'),
17853 glyph->charpos,
17854 (BUFFERP (glyph->object)
17855 ? 'B'
17856 : (STRINGP (glyph->object)
17857 ? 'S'
17858 : (INTEGERP (glyph->object)
17859 ? '0'
17860 : '-'))),
17861 glyph->pixel_width,
17862 glyph->u.ch,
17863 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17864 ? glyph->u.ch
17865 : '.'),
17866 glyph->face_id,
17867 glyph->left_box_line_p,
17868 glyph->right_box_line_p);
17870 else if (glyph->type == STRETCH_GLYPH)
17872 fprintf (stderr,
17873 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17874 glyph - row->glyphs[TEXT_AREA],
17875 'S',
17876 glyph->charpos,
17877 (BUFFERP (glyph->object)
17878 ? 'B'
17879 : (STRINGP (glyph->object)
17880 ? 'S'
17881 : (INTEGERP (glyph->object)
17882 ? '0'
17883 : '-'))),
17884 glyph->pixel_width,
17886 ' ',
17887 glyph->face_id,
17888 glyph->left_box_line_p,
17889 glyph->right_box_line_p);
17891 else if (glyph->type == IMAGE_GLYPH)
17893 fprintf (stderr,
17894 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17895 glyph - row->glyphs[TEXT_AREA],
17896 'I',
17897 glyph->charpos,
17898 (BUFFERP (glyph->object)
17899 ? 'B'
17900 : (STRINGP (glyph->object)
17901 ? 'S'
17902 : (INTEGERP (glyph->object)
17903 ? '0'
17904 : '-'))),
17905 glyph->pixel_width,
17906 glyph->u.img_id,
17907 '.',
17908 glyph->face_id,
17909 glyph->left_box_line_p,
17910 glyph->right_box_line_p);
17912 else if (glyph->type == COMPOSITE_GLYPH)
17914 fprintf (stderr,
17915 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17916 glyph - row->glyphs[TEXT_AREA],
17917 '+',
17918 glyph->charpos,
17919 (BUFFERP (glyph->object)
17920 ? 'B'
17921 : (STRINGP (glyph->object)
17922 ? 'S'
17923 : (INTEGERP (glyph->object)
17924 ? '0'
17925 : '-'))),
17926 glyph->pixel_width,
17927 glyph->u.cmp.id);
17928 if (glyph->u.cmp.automatic)
17929 fprintf (stderr,
17930 "[%d-%d]",
17931 glyph->slice.cmp.from, glyph->slice.cmp.to);
17932 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17933 glyph->face_id,
17934 glyph->left_box_line_p,
17935 glyph->right_box_line_p);
17940 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17941 GLYPHS 0 means don't show glyph contents.
17942 GLYPHS 1 means show glyphs in short form
17943 GLYPHS > 1 means show glyphs in long form. */
17945 void
17946 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17948 if (glyphs != 1)
17950 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17951 fprintf (stderr, "==============================================================================\n");
17953 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17954 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17955 vpos,
17956 MATRIX_ROW_START_CHARPOS (row),
17957 MATRIX_ROW_END_CHARPOS (row),
17958 row->used[TEXT_AREA],
17959 row->contains_overlapping_glyphs_p,
17960 row->enabled_p,
17961 row->truncated_on_left_p,
17962 row->truncated_on_right_p,
17963 row->continued_p,
17964 MATRIX_ROW_CONTINUATION_LINE_P (row),
17965 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17966 row->ends_at_zv_p,
17967 row->fill_line_p,
17968 row->ends_in_middle_of_char_p,
17969 row->starts_in_middle_of_char_p,
17970 row->mouse_face_p,
17971 row->x,
17972 row->y,
17973 row->pixel_width,
17974 row->height,
17975 row->visible_height,
17976 row->ascent,
17977 row->phys_ascent);
17978 /* The next 3 lines should align to "Start" in the header. */
17979 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17980 row->end.overlay_string_index,
17981 row->continuation_lines_width);
17982 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17983 CHARPOS (row->start.string_pos),
17984 CHARPOS (row->end.string_pos));
17985 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17986 row->end.dpvec_index);
17989 if (glyphs > 1)
17991 int area;
17993 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17995 struct glyph *glyph = row->glyphs[area];
17996 struct glyph *glyph_end = glyph + row->used[area];
17998 /* Glyph for a line end in text. */
17999 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18000 ++glyph_end;
18002 if (glyph < glyph_end)
18003 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18005 for (; glyph < glyph_end; ++glyph)
18006 dump_glyph (row, glyph, area);
18009 else if (glyphs == 1)
18011 int area;
18013 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18015 char *s = alloca (row->used[area] + 4);
18016 int i;
18018 for (i = 0; i < row->used[area]; ++i)
18020 struct glyph *glyph = row->glyphs[area] + i;
18021 if (i == row->used[area] - 1
18022 && area == TEXT_AREA
18023 && INTEGERP (glyph->object)
18024 && glyph->type == CHAR_GLYPH
18025 && glyph->u.ch == ' ')
18027 strcpy (&s[i], "[\\n]");
18028 i += 4;
18030 else if (glyph->type == CHAR_GLYPH
18031 && glyph->u.ch < 0x80
18032 && glyph->u.ch >= ' ')
18033 s[i] = glyph->u.ch;
18034 else
18035 s[i] = '.';
18038 s[i] = '\0';
18039 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18045 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18046 Sdump_glyph_matrix, 0, 1, "p",
18047 doc: /* Dump the current matrix of the selected window to stderr.
18048 Shows contents of glyph row structures. With non-nil
18049 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18050 glyphs in short form, otherwise show glyphs in long form. */)
18051 (Lisp_Object glyphs)
18053 struct window *w = XWINDOW (selected_window);
18054 struct buffer *buffer = XBUFFER (w->contents);
18056 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18057 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18058 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18059 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18060 fprintf (stderr, "=============================================\n");
18061 dump_glyph_matrix (w->current_matrix,
18062 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18063 return Qnil;
18067 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18068 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18069 (void)
18071 struct frame *f = XFRAME (selected_frame);
18072 dump_glyph_matrix (f->current_matrix, 1);
18073 return Qnil;
18077 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18078 doc: /* Dump glyph row ROW to stderr.
18079 GLYPH 0 means don't dump glyphs.
18080 GLYPH 1 means dump glyphs in short form.
18081 GLYPH > 1 or omitted means dump glyphs in long form. */)
18082 (Lisp_Object row, Lisp_Object glyphs)
18084 struct glyph_matrix *matrix;
18085 EMACS_INT vpos;
18087 CHECK_NUMBER (row);
18088 matrix = XWINDOW (selected_window)->current_matrix;
18089 vpos = XINT (row);
18090 if (vpos >= 0 && vpos < matrix->nrows)
18091 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18092 vpos,
18093 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18094 return Qnil;
18098 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18099 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18100 GLYPH 0 means don't dump glyphs.
18101 GLYPH 1 means dump glyphs in short form.
18102 GLYPH > 1 or omitted means dump glyphs in long form. */)
18103 (Lisp_Object row, Lisp_Object glyphs)
18105 struct frame *sf = SELECTED_FRAME ();
18106 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18107 EMACS_INT vpos;
18109 CHECK_NUMBER (row);
18110 vpos = XINT (row);
18111 if (vpos >= 0 && vpos < m->nrows)
18112 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18113 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18114 return Qnil;
18118 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18119 doc: /* Toggle tracing of redisplay.
18120 With ARG, turn tracing on if and only if ARG is positive. */)
18121 (Lisp_Object arg)
18123 if (NILP (arg))
18124 trace_redisplay_p = !trace_redisplay_p;
18125 else
18127 arg = Fprefix_numeric_value (arg);
18128 trace_redisplay_p = XINT (arg) > 0;
18131 return Qnil;
18135 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18136 doc: /* Like `format', but print result to stderr.
18137 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18138 (ptrdiff_t nargs, Lisp_Object *args)
18140 Lisp_Object s = Fformat (nargs, args);
18141 fprintf (stderr, "%s", SDATA (s));
18142 return Qnil;
18145 #endif /* GLYPH_DEBUG */
18149 /***********************************************************************
18150 Building Desired Matrix Rows
18151 ***********************************************************************/
18153 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18154 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18156 static struct glyph_row *
18157 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18159 struct frame *f = XFRAME (WINDOW_FRAME (w));
18160 struct buffer *buffer = XBUFFER (w->contents);
18161 struct buffer *old = current_buffer;
18162 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18163 int arrow_len = SCHARS (overlay_arrow_string);
18164 const unsigned char *arrow_end = arrow_string + arrow_len;
18165 const unsigned char *p;
18166 struct it it;
18167 bool multibyte_p;
18168 int n_glyphs_before;
18170 set_buffer_temp (buffer);
18171 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18172 it.glyph_row->used[TEXT_AREA] = 0;
18173 SET_TEXT_POS (it.position, 0, 0);
18175 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18176 p = arrow_string;
18177 while (p < arrow_end)
18179 Lisp_Object face, ilisp;
18181 /* Get the next character. */
18182 if (multibyte_p)
18183 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18184 else
18186 it.c = it.char_to_display = *p, it.len = 1;
18187 if (! ASCII_CHAR_P (it.c))
18188 it.char_to_display = BYTE8_TO_CHAR (it.c);
18190 p += it.len;
18192 /* Get its face. */
18193 ilisp = make_number (p - arrow_string);
18194 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18195 it.face_id = compute_char_face (f, it.char_to_display, face);
18197 /* Compute its width, get its glyphs. */
18198 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18199 SET_TEXT_POS (it.position, -1, -1);
18200 PRODUCE_GLYPHS (&it);
18202 /* If this character doesn't fit any more in the line, we have
18203 to remove some glyphs. */
18204 if (it.current_x > it.last_visible_x)
18206 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18207 break;
18211 set_buffer_temp (old);
18212 return it.glyph_row;
18216 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18217 glyphs to insert is determined by produce_special_glyphs. */
18219 static void
18220 insert_left_trunc_glyphs (struct it *it)
18222 struct it truncate_it;
18223 struct glyph *from, *end, *to, *toend;
18225 eassert (!FRAME_WINDOW_P (it->f)
18226 || (!it->glyph_row->reversed_p
18227 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18228 || (it->glyph_row->reversed_p
18229 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18231 /* Get the truncation glyphs. */
18232 truncate_it = *it;
18233 truncate_it.current_x = 0;
18234 truncate_it.face_id = DEFAULT_FACE_ID;
18235 truncate_it.glyph_row = &scratch_glyph_row;
18236 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18237 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18238 truncate_it.object = make_number (0);
18239 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18241 /* Overwrite glyphs from IT with truncation glyphs. */
18242 if (!it->glyph_row->reversed_p)
18244 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18246 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18247 end = from + tused;
18248 to = it->glyph_row->glyphs[TEXT_AREA];
18249 toend = to + it->glyph_row->used[TEXT_AREA];
18250 if (FRAME_WINDOW_P (it->f))
18252 /* On GUI frames, when variable-size fonts are displayed,
18253 the truncation glyphs may need more pixels than the row's
18254 glyphs they overwrite. We overwrite more glyphs to free
18255 enough screen real estate, and enlarge the stretch glyph
18256 on the right (see display_line), if there is one, to
18257 preserve the screen position of the truncation glyphs on
18258 the right. */
18259 int w = 0;
18260 struct glyph *g = to;
18261 short used;
18263 /* The first glyph could be partially visible, in which case
18264 it->glyph_row->x will be negative. But we want the left
18265 truncation glyphs to be aligned at the left margin of the
18266 window, so we override the x coordinate at which the row
18267 will begin. */
18268 it->glyph_row->x = 0;
18269 while (g < toend && w < it->truncation_pixel_width)
18271 w += g->pixel_width;
18272 ++g;
18274 if (g - to - tused > 0)
18276 memmove (to + tused, g, (toend - g) * sizeof(*g));
18277 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18279 used = it->glyph_row->used[TEXT_AREA];
18280 if (it->glyph_row->truncated_on_right_p
18281 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18282 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18283 == STRETCH_GLYPH)
18285 int extra = w - it->truncation_pixel_width;
18287 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18291 while (from < end)
18292 *to++ = *from++;
18294 /* There may be padding glyphs left over. Overwrite them too. */
18295 if (!FRAME_WINDOW_P (it->f))
18297 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18299 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18300 while (from < end)
18301 *to++ = *from++;
18305 if (to > toend)
18306 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18308 else
18310 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18312 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18313 that back to front. */
18314 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18315 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18316 toend = it->glyph_row->glyphs[TEXT_AREA];
18317 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18318 if (FRAME_WINDOW_P (it->f))
18320 int w = 0;
18321 struct glyph *g = to;
18323 while (g >= toend && w < it->truncation_pixel_width)
18325 w += g->pixel_width;
18326 --g;
18328 if (to - g - tused > 0)
18329 to = g + tused;
18330 if (it->glyph_row->truncated_on_right_p
18331 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18332 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18334 int extra = w - it->truncation_pixel_width;
18336 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18340 while (from >= end && to >= toend)
18341 *to-- = *from--;
18342 if (!FRAME_WINDOW_P (it->f))
18344 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18346 from =
18347 truncate_it.glyph_row->glyphs[TEXT_AREA]
18348 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18349 while (from >= end && to >= toend)
18350 *to-- = *from--;
18353 if (from >= end)
18355 /* Need to free some room before prepending additional
18356 glyphs. */
18357 int move_by = from - end + 1;
18358 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18359 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18361 for ( ; g >= g0; g--)
18362 g[move_by] = *g;
18363 while (from >= end)
18364 *to-- = *from--;
18365 it->glyph_row->used[TEXT_AREA] += move_by;
18370 /* Compute the hash code for ROW. */
18371 unsigned
18372 row_hash (struct glyph_row *row)
18374 int area, k;
18375 unsigned hashval = 0;
18377 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18378 for (k = 0; k < row->used[area]; ++k)
18379 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18380 + row->glyphs[area][k].u.val
18381 + row->glyphs[area][k].face_id
18382 + row->glyphs[area][k].padding_p
18383 + (row->glyphs[area][k].type << 2));
18385 return hashval;
18388 /* Compute the pixel height and width of IT->glyph_row.
18390 Most of the time, ascent and height of a display line will be equal
18391 to the max_ascent and max_height values of the display iterator
18392 structure. This is not the case if
18394 1. We hit ZV without displaying anything. In this case, max_ascent
18395 and max_height will be zero.
18397 2. We have some glyphs that don't contribute to the line height.
18398 (The glyph row flag contributes_to_line_height_p is for future
18399 pixmap extensions).
18401 The first case is easily covered by using default values because in
18402 these cases, the line height does not really matter, except that it
18403 must not be zero. */
18405 static void
18406 compute_line_metrics (struct it *it)
18408 struct glyph_row *row = it->glyph_row;
18410 if (FRAME_WINDOW_P (it->f))
18412 int i, min_y, max_y;
18414 /* The line may consist of one space only, that was added to
18415 place the cursor on it. If so, the row's height hasn't been
18416 computed yet. */
18417 if (row->height == 0)
18419 if (it->max_ascent + it->max_descent == 0)
18420 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18421 row->ascent = it->max_ascent;
18422 row->height = it->max_ascent + it->max_descent;
18423 row->phys_ascent = it->max_phys_ascent;
18424 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18425 row->extra_line_spacing = it->max_extra_line_spacing;
18428 /* Compute the width of this line. */
18429 row->pixel_width = row->x;
18430 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18431 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18433 eassert (row->pixel_width >= 0);
18434 eassert (row->ascent >= 0 && row->height > 0);
18436 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18437 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18439 /* If first line's physical ascent is larger than its logical
18440 ascent, use the physical ascent, and make the row taller.
18441 This makes accented characters fully visible. */
18442 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18443 && row->phys_ascent > row->ascent)
18445 row->height += row->phys_ascent - row->ascent;
18446 row->ascent = row->phys_ascent;
18449 /* Compute how much of the line is visible. */
18450 row->visible_height = row->height;
18452 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18453 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18455 if (row->y < min_y)
18456 row->visible_height -= min_y - row->y;
18457 if (row->y + row->height > max_y)
18458 row->visible_height -= row->y + row->height - max_y;
18460 else
18462 row->pixel_width = row->used[TEXT_AREA];
18463 if (row->continued_p)
18464 row->pixel_width -= it->continuation_pixel_width;
18465 else if (row->truncated_on_right_p)
18466 row->pixel_width -= it->truncation_pixel_width;
18467 row->ascent = row->phys_ascent = 0;
18468 row->height = row->phys_height = row->visible_height = 1;
18469 row->extra_line_spacing = 0;
18472 /* Compute a hash code for this row. */
18473 row->hash = row_hash (row);
18475 it->max_ascent = it->max_descent = 0;
18476 it->max_phys_ascent = it->max_phys_descent = 0;
18480 /* Append one space to the glyph row of iterator IT if doing a
18481 window-based redisplay. The space has the same face as
18482 IT->face_id. Value is non-zero if a space was added.
18484 This function is called to make sure that there is always one glyph
18485 at the end of a glyph row that the cursor can be set on under
18486 window-systems. (If there weren't such a glyph we would not know
18487 how wide and tall a box cursor should be displayed).
18489 At the same time this space let's a nicely handle clearing to the
18490 end of the line if the row ends in italic text. */
18492 static int
18493 append_space_for_newline (struct it *it, int default_face_p)
18495 if (FRAME_WINDOW_P (it->f))
18497 int n = it->glyph_row->used[TEXT_AREA];
18499 if (it->glyph_row->glyphs[TEXT_AREA] + n
18500 < it->glyph_row->glyphs[1 + TEXT_AREA])
18502 /* Save some values that must not be changed.
18503 Must save IT->c and IT->len because otherwise
18504 ITERATOR_AT_END_P wouldn't work anymore after
18505 append_space_for_newline has been called. */
18506 enum display_element_type saved_what = it->what;
18507 int saved_c = it->c, saved_len = it->len;
18508 int saved_char_to_display = it->char_to_display;
18509 int saved_x = it->current_x;
18510 int saved_face_id = it->face_id;
18511 int saved_box_end = it->end_of_box_run_p;
18512 struct text_pos saved_pos;
18513 Lisp_Object saved_object;
18514 struct face *face;
18516 saved_object = it->object;
18517 saved_pos = it->position;
18519 it->what = IT_CHARACTER;
18520 memset (&it->position, 0, sizeof it->position);
18521 it->object = make_number (0);
18522 it->c = it->char_to_display = ' ';
18523 it->len = 1;
18525 /* If the default face was remapped, be sure to use the
18526 remapped face for the appended newline. */
18527 if (default_face_p)
18528 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18529 else if (it->face_before_selective_p)
18530 it->face_id = it->saved_face_id;
18531 face = FACE_FROM_ID (it->f, it->face_id);
18532 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18533 /* In R2L rows, we will prepend a stretch glyph that will
18534 have the end_of_box_run_p flag set for it, so there's no
18535 need for the appended newline glyph to have that flag
18536 set. */
18537 if (it->glyph_row->reversed_p
18538 /* But if the appended newline glyph goes all the way to
18539 the end of the row, there will be no stretch glyph,
18540 so leave the box flag set. */
18541 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18542 it->end_of_box_run_p = 0;
18544 PRODUCE_GLYPHS (it);
18546 it->override_ascent = -1;
18547 it->constrain_row_ascent_descent_p = 0;
18548 it->current_x = saved_x;
18549 it->object = saved_object;
18550 it->position = saved_pos;
18551 it->what = saved_what;
18552 it->face_id = saved_face_id;
18553 it->len = saved_len;
18554 it->c = saved_c;
18555 it->char_to_display = saved_char_to_display;
18556 it->end_of_box_run_p = saved_box_end;
18557 return 1;
18561 return 0;
18565 /* Extend the face of the last glyph in the text area of IT->glyph_row
18566 to the end of the display line. Called from display_line. If the
18567 glyph row is empty, add a space glyph to it so that we know the
18568 face to draw. Set the glyph row flag fill_line_p. If the glyph
18569 row is R2L, prepend a stretch glyph to cover the empty space to the
18570 left of the leftmost glyph. */
18572 static void
18573 extend_face_to_end_of_line (struct it *it)
18575 struct face *face, *default_face;
18576 struct frame *f = it->f;
18578 /* If line is already filled, do nothing. Non window-system frames
18579 get a grace of one more ``pixel'' because their characters are
18580 1-``pixel'' wide, so they hit the equality too early. This grace
18581 is needed only for R2L rows that are not continued, to produce
18582 one extra blank where we could display the cursor. */
18583 if (it->current_x >= it->last_visible_x
18584 + (!FRAME_WINDOW_P (f)
18585 && it->glyph_row->reversed_p
18586 && !it->glyph_row->continued_p))
18587 return;
18589 /* The default face, possibly remapped. */
18590 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18592 /* Face extension extends the background and box of IT->face_id
18593 to the end of the line. If the background equals the background
18594 of the frame, we don't have to do anything. */
18595 if (it->face_before_selective_p)
18596 face = FACE_FROM_ID (f, it->saved_face_id);
18597 else
18598 face = FACE_FROM_ID (f, it->face_id);
18600 if (FRAME_WINDOW_P (f)
18601 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18602 && face->box == FACE_NO_BOX
18603 && face->background == FRAME_BACKGROUND_PIXEL (f)
18604 && !face->stipple
18605 && !it->glyph_row->reversed_p)
18606 return;
18608 /* Set the glyph row flag indicating that the face of the last glyph
18609 in the text area has to be drawn to the end of the text area. */
18610 it->glyph_row->fill_line_p = 1;
18612 /* If current character of IT is not ASCII, make sure we have the
18613 ASCII face. This will be automatically undone the next time
18614 get_next_display_element returns a multibyte character. Note
18615 that the character will always be single byte in unibyte
18616 text. */
18617 if (!ASCII_CHAR_P (it->c))
18619 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18622 if (FRAME_WINDOW_P (f))
18624 /* If the row is empty, add a space with the current face of IT,
18625 so that we know which face to draw. */
18626 if (it->glyph_row->used[TEXT_AREA] == 0)
18628 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18629 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18630 it->glyph_row->used[TEXT_AREA] = 1;
18632 #ifdef HAVE_WINDOW_SYSTEM
18633 if (it->glyph_row->reversed_p)
18635 /* Prepend a stretch glyph to the row, such that the
18636 rightmost glyph will be drawn flushed all the way to the
18637 right margin of the window. The stretch glyph that will
18638 occupy the empty space, if any, to the left of the
18639 glyphs. */
18640 struct font *font = face->font ? face->font : FRAME_FONT (f);
18641 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18642 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18643 struct glyph *g;
18644 int row_width, stretch_ascent, stretch_width;
18645 struct text_pos saved_pos;
18646 int saved_face_id, saved_avoid_cursor, saved_box_start;
18648 for (row_width = 0, g = row_start; g < row_end; g++)
18649 row_width += g->pixel_width;
18650 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18651 if (stretch_width > 0)
18653 stretch_ascent =
18654 (((it->ascent + it->descent)
18655 * FONT_BASE (font)) / FONT_HEIGHT (font));
18656 saved_pos = it->position;
18657 memset (&it->position, 0, sizeof it->position);
18658 saved_avoid_cursor = it->avoid_cursor_p;
18659 it->avoid_cursor_p = 1;
18660 saved_face_id = it->face_id;
18661 saved_box_start = it->start_of_box_run_p;
18662 /* The last row's stretch glyph should get the default
18663 face, to avoid painting the rest of the window with
18664 the region face, if the region ends at ZV. */
18665 if (it->glyph_row->ends_at_zv_p)
18666 it->face_id = default_face->id;
18667 else
18668 it->face_id = face->id;
18669 it->start_of_box_run_p = 0;
18670 append_stretch_glyph (it, make_number (0), stretch_width,
18671 it->ascent + it->descent, stretch_ascent);
18672 it->position = saved_pos;
18673 it->avoid_cursor_p = saved_avoid_cursor;
18674 it->face_id = saved_face_id;
18675 it->start_of_box_run_p = saved_box_start;
18678 #endif /* HAVE_WINDOW_SYSTEM */
18680 else
18682 /* Save some values that must not be changed. */
18683 int saved_x = it->current_x;
18684 struct text_pos saved_pos;
18685 Lisp_Object saved_object;
18686 enum display_element_type saved_what = it->what;
18687 int saved_face_id = it->face_id;
18689 saved_object = it->object;
18690 saved_pos = it->position;
18692 it->what = IT_CHARACTER;
18693 memset (&it->position, 0, sizeof it->position);
18694 it->object = make_number (0);
18695 it->c = it->char_to_display = ' ';
18696 it->len = 1;
18697 /* The last row's blank glyphs should get the default face, to
18698 avoid painting the rest of the window with the region face,
18699 if the region ends at ZV. */
18700 if (it->glyph_row->ends_at_zv_p)
18701 it->face_id = default_face->id;
18702 else
18703 it->face_id = face->id;
18705 PRODUCE_GLYPHS (it);
18707 while (it->current_x <= it->last_visible_x)
18708 PRODUCE_GLYPHS (it);
18710 /* Don't count these blanks really. It would let us insert a left
18711 truncation glyph below and make us set the cursor on them, maybe. */
18712 it->current_x = saved_x;
18713 it->object = saved_object;
18714 it->position = saved_pos;
18715 it->what = saved_what;
18716 it->face_id = saved_face_id;
18721 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18722 trailing whitespace. */
18724 static int
18725 trailing_whitespace_p (ptrdiff_t charpos)
18727 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18728 int c = 0;
18730 while (bytepos < ZV_BYTE
18731 && (c = FETCH_CHAR (bytepos),
18732 c == ' ' || c == '\t'))
18733 ++bytepos;
18735 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18737 if (bytepos != PT_BYTE)
18738 return 1;
18740 return 0;
18744 /* Highlight trailing whitespace, if any, in ROW. */
18746 static void
18747 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18749 int used = row->used[TEXT_AREA];
18751 if (used)
18753 struct glyph *start = row->glyphs[TEXT_AREA];
18754 struct glyph *glyph = start + used - 1;
18756 if (row->reversed_p)
18758 /* Right-to-left rows need to be processed in the opposite
18759 direction, so swap the edge pointers. */
18760 glyph = start;
18761 start = row->glyphs[TEXT_AREA] + used - 1;
18764 /* Skip over glyphs inserted to display the cursor at the
18765 end of a line, for extending the face of the last glyph
18766 to the end of the line on terminals, and for truncation
18767 and continuation glyphs. */
18768 if (!row->reversed_p)
18770 while (glyph >= start
18771 && glyph->type == CHAR_GLYPH
18772 && INTEGERP (glyph->object))
18773 --glyph;
18775 else
18777 while (glyph <= start
18778 && glyph->type == CHAR_GLYPH
18779 && INTEGERP (glyph->object))
18780 ++glyph;
18783 /* If last glyph is a space or stretch, and it's trailing
18784 whitespace, set the face of all trailing whitespace glyphs in
18785 IT->glyph_row to `trailing-whitespace'. */
18786 if ((row->reversed_p ? glyph <= start : glyph >= start)
18787 && BUFFERP (glyph->object)
18788 && (glyph->type == STRETCH_GLYPH
18789 || (glyph->type == CHAR_GLYPH
18790 && glyph->u.ch == ' '))
18791 && trailing_whitespace_p (glyph->charpos))
18793 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18794 if (face_id < 0)
18795 return;
18797 if (!row->reversed_p)
18799 while (glyph >= start
18800 && BUFFERP (glyph->object)
18801 && (glyph->type == STRETCH_GLYPH
18802 || (glyph->type == CHAR_GLYPH
18803 && glyph->u.ch == ' ')))
18804 (glyph--)->face_id = face_id;
18806 else
18808 while (glyph <= start
18809 && BUFFERP (glyph->object)
18810 && (glyph->type == STRETCH_GLYPH
18811 || (glyph->type == CHAR_GLYPH
18812 && glyph->u.ch == ' ')))
18813 (glyph++)->face_id = face_id;
18820 /* Value is non-zero if glyph row ROW should be
18821 considered to hold the buffer position CHARPOS. */
18823 static int
18824 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18826 int result = 1;
18828 if (charpos == CHARPOS (row->end.pos)
18829 || charpos == MATRIX_ROW_END_CHARPOS (row))
18831 /* Suppose the row ends on a string.
18832 Unless the row is continued, that means it ends on a newline
18833 in the string. If it's anything other than a display string
18834 (e.g., a before-string from an overlay), we don't want the
18835 cursor there. (This heuristic seems to give the optimal
18836 behavior for the various types of multi-line strings.)
18837 One exception: if the string has `cursor' property on one of
18838 its characters, we _do_ want the cursor there. */
18839 if (CHARPOS (row->end.string_pos) >= 0)
18841 if (row->continued_p)
18842 result = 1;
18843 else
18845 /* Check for `display' property. */
18846 struct glyph *beg = row->glyphs[TEXT_AREA];
18847 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18848 struct glyph *glyph;
18850 result = 0;
18851 for (glyph = end; glyph >= beg; --glyph)
18852 if (STRINGP (glyph->object))
18854 Lisp_Object prop
18855 = Fget_char_property (make_number (charpos),
18856 Qdisplay, Qnil);
18857 result =
18858 (!NILP (prop)
18859 && display_prop_string_p (prop, glyph->object));
18860 /* If there's a `cursor' property on one of the
18861 string's characters, this row is a cursor row,
18862 even though this is not a display string. */
18863 if (!result)
18865 Lisp_Object s = glyph->object;
18867 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18869 ptrdiff_t gpos = glyph->charpos;
18871 if (!NILP (Fget_char_property (make_number (gpos),
18872 Qcursor, s)))
18874 result = 1;
18875 break;
18879 break;
18883 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18885 /* If the row ends in middle of a real character,
18886 and the line is continued, we want the cursor here.
18887 That's because CHARPOS (ROW->end.pos) would equal
18888 PT if PT is before the character. */
18889 if (!row->ends_in_ellipsis_p)
18890 result = row->continued_p;
18891 else
18892 /* If the row ends in an ellipsis, then
18893 CHARPOS (ROW->end.pos) will equal point after the
18894 invisible text. We want that position to be displayed
18895 after the ellipsis. */
18896 result = 0;
18898 /* If the row ends at ZV, display the cursor at the end of that
18899 row instead of at the start of the row below. */
18900 else if (row->ends_at_zv_p)
18901 result = 1;
18902 else
18903 result = 0;
18906 return result;
18909 /* Value is non-zero if glyph row ROW should be
18910 used to hold the cursor. */
18912 static int
18913 cursor_row_p (struct glyph_row *row)
18915 return row_for_charpos_p (row, PT);
18920 /* Push the property PROP so that it will be rendered at the current
18921 position in IT. Return 1 if PROP was successfully pushed, 0
18922 otherwise. Called from handle_line_prefix to handle the
18923 `line-prefix' and `wrap-prefix' properties. */
18925 static int
18926 push_prefix_prop (struct it *it, Lisp_Object prop)
18928 struct text_pos pos =
18929 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18931 eassert (it->method == GET_FROM_BUFFER
18932 || it->method == GET_FROM_DISPLAY_VECTOR
18933 || it->method == GET_FROM_STRING);
18935 /* We need to save the current buffer/string position, so it will be
18936 restored by pop_it, because iterate_out_of_display_property
18937 depends on that being set correctly, but some situations leave
18938 it->position not yet set when this function is called. */
18939 push_it (it, &pos);
18941 if (STRINGP (prop))
18943 if (SCHARS (prop) == 0)
18945 pop_it (it);
18946 return 0;
18949 it->string = prop;
18950 it->string_from_prefix_prop_p = 1;
18951 it->multibyte_p = STRING_MULTIBYTE (it->string);
18952 it->current.overlay_string_index = -1;
18953 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18954 it->end_charpos = it->string_nchars = SCHARS (it->string);
18955 it->method = GET_FROM_STRING;
18956 it->stop_charpos = 0;
18957 it->prev_stop = 0;
18958 it->base_level_stop = 0;
18960 /* Force paragraph direction to be that of the parent
18961 buffer/string. */
18962 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18963 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18964 else
18965 it->paragraph_embedding = L2R;
18967 /* Set up the bidi iterator for this display string. */
18968 if (it->bidi_p)
18970 it->bidi_it.string.lstring = it->string;
18971 it->bidi_it.string.s = NULL;
18972 it->bidi_it.string.schars = it->end_charpos;
18973 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18974 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18975 it->bidi_it.string.unibyte = !it->multibyte_p;
18976 it->bidi_it.w = it->w;
18977 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18980 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18982 it->method = GET_FROM_STRETCH;
18983 it->object = prop;
18985 #ifdef HAVE_WINDOW_SYSTEM
18986 else if (IMAGEP (prop))
18988 it->what = IT_IMAGE;
18989 it->image_id = lookup_image (it->f, prop);
18990 it->method = GET_FROM_IMAGE;
18992 #endif /* HAVE_WINDOW_SYSTEM */
18993 else
18995 pop_it (it); /* bogus display property, give up */
18996 return 0;
18999 return 1;
19002 /* Return the character-property PROP at the current position in IT. */
19004 static Lisp_Object
19005 get_it_property (struct it *it, Lisp_Object prop)
19007 Lisp_Object position, object = it->object;
19009 if (STRINGP (object))
19010 position = make_number (IT_STRING_CHARPOS (*it));
19011 else if (BUFFERP (object))
19013 position = make_number (IT_CHARPOS (*it));
19014 object = it->window;
19016 else
19017 return Qnil;
19019 return Fget_char_property (position, prop, object);
19022 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19024 static void
19025 handle_line_prefix (struct it *it)
19027 Lisp_Object prefix;
19029 if (it->continuation_lines_width > 0)
19031 prefix = get_it_property (it, Qwrap_prefix);
19032 if (NILP (prefix))
19033 prefix = Vwrap_prefix;
19035 else
19037 prefix = get_it_property (it, Qline_prefix);
19038 if (NILP (prefix))
19039 prefix = Vline_prefix;
19041 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19043 /* If the prefix is wider than the window, and we try to wrap
19044 it, it would acquire its own wrap prefix, and so on till the
19045 iterator stack overflows. So, don't wrap the prefix. */
19046 it->line_wrap = TRUNCATE;
19047 it->avoid_cursor_p = 1;
19053 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19054 only for R2L lines from display_line and display_string, when they
19055 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19056 the line/string needs to be continued on the next glyph row. */
19057 static void
19058 unproduce_glyphs (struct it *it, int n)
19060 struct glyph *glyph, *end;
19062 eassert (it->glyph_row);
19063 eassert (it->glyph_row->reversed_p);
19064 eassert (it->area == TEXT_AREA);
19065 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19067 if (n > it->glyph_row->used[TEXT_AREA])
19068 n = it->glyph_row->used[TEXT_AREA];
19069 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19070 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19071 for ( ; glyph < end; glyph++)
19072 glyph[-n] = *glyph;
19075 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19076 and ROW->maxpos. */
19077 static void
19078 find_row_edges (struct it *it, struct glyph_row *row,
19079 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19080 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19082 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19083 lines' rows is implemented for bidi-reordered rows. */
19085 /* ROW->minpos is the value of min_pos, the minimal buffer position
19086 we have in ROW, or ROW->start.pos if that is smaller. */
19087 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19088 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19089 else
19090 /* We didn't find buffer positions smaller than ROW->start, or
19091 didn't find _any_ valid buffer positions in any of the glyphs,
19092 so we must trust the iterator's computed positions. */
19093 row->minpos = row->start.pos;
19094 if (max_pos <= 0)
19096 max_pos = CHARPOS (it->current.pos);
19097 max_bpos = BYTEPOS (it->current.pos);
19100 /* Here are the various use-cases for ending the row, and the
19101 corresponding values for ROW->maxpos:
19103 Line ends in a newline from buffer eol_pos + 1
19104 Line is continued from buffer max_pos + 1
19105 Line is truncated on right it->current.pos
19106 Line ends in a newline from string max_pos + 1(*)
19107 (*) + 1 only when line ends in a forward scan
19108 Line is continued from string max_pos
19109 Line is continued from display vector max_pos
19110 Line is entirely from a string min_pos == max_pos
19111 Line is entirely from a display vector min_pos == max_pos
19112 Line that ends at ZV ZV
19114 If you discover other use-cases, please add them here as
19115 appropriate. */
19116 if (row->ends_at_zv_p)
19117 row->maxpos = it->current.pos;
19118 else if (row->used[TEXT_AREA])
19120 int seen_this_string = 0;
19121 struct glyph_row *r1 = row - 1;
19123 /* Did we see the same display string on the previous row? */
19124 if (STRINGP (it->object)
19125 /* this is not the first row */
19126 && row > it->w->desired_matrix->rows
19127 /* previous row is not the header line */
19128 && !r1->mode_line_p
19129 /* previous row also ends in a newline from a string */
19130 && r1->ends_in_newline_from_string_p)
19132 struct glyph *start, *end;
19134 /* Search for the last glyph of the previous row that came
19135 from buffer or string. Depending on whether the row is
19136 L2R or R2L, we need to process it front to back or the
19137 other way round. */
19138 if (!r1->reversed_p)
19140 start = r1->glyphs[TEXT_AREA];
19141 end = start + r1->used[TEXT_AREA];
19142 /* Glyphs inserted by redisplay have an integer (zero)
19143 as their object. */
19144 while (end > start
19145 && INTEGERP ((end - 1)->object)
19146 && (end - 1)->charpos <= 0)
19147 --end;
19148 if (end > start)
19150 if (EQ ((end - 1)->object, it->object))
19151 seen_this_string = 1;
19153 else
19154 /* If all the glyphs of the previous row were inserted
19155 by redisplay, it means the previous row was
19156 produced from a single newline, which is only
19157 possible if that newline came from the same string
19158 as the one which produced this ROW. */
19159 seen_this_string = 1;
19161 else
19163 end = r1->glyphs[TEXT_AREA] - 1;
19164 start = end + r1->used[TEXT_AREA];
19165 while (end < start
19166 && INTEGERP ((end + 1)->object)
19167 && (end + 1)->charpos <= 0)
19168 ++end;
19169 if (end < start)
19171 if (EQ ((end + 1)->object, it->object))
19172 seen_this_string = 1;
19174 else
19175 seen_this_string = 1;
19178 /* Take note of each display string that covers a newline only
19179 once, the first time we see it. This is for when a display
19180 string includes more than one newline in it. */
19181 if (row->ends_in_newline_from_string_p && !seen_this_string)
19183 /* If we were scanning the buffer forward when we displayed
19184 the string, we want to account for at least one buffer
19185 position that belongs to this row (position covered by
19186 the display string), so that cursor positioning will
19187 consider this row as a candidate when point is at the end
19188 of the visual line represented by this row. This is not
19189 required when scanning back, because max_pos will already
19190 have a much larger value. */
19191 if (CHARPOS (row->end.pos) > max_pos)
19192 INC_BOTH (max_pos, max_bpos);
19193 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19195 else if (CHARPOS (it->eol_pos) > 0)
19196 SET_TEXT_POS (row->maxpos,
19197 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19198 else if (row->continued_p)
19200 /* If max_pos is different from IT's current position, it
19201 means IT->method does not belong to the display element
19202 at max_pos. However, it also means that the display
19203 element at max_pos was displayed in its entirety on this
19204 line, which is equivalent to saying that the next line
19205 starts at the next buffer position. */
19206 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19207 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19208 else
19210 INC_BOTH (max_pos, max_bpos);
19211 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19214 else if (row->truncated_on_right_p)
19215 /* display_line already called reseat_at_next_visible_line_start,
19216 which puts the iterator at the beginning of the next line, in
19217 the logical order. */
19218 row->maxpos = it->current.pos;
19219 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19220 /* A line that is entirely from a string/image/stretch... */
19221 row->maxpos = row->minpos;
19222 else
19223 emacs_abort ();
19225 else
19226 row->maxpos = it->current.pos;
19229 /* Construct the glyph row IT->glyph_row in the desired matrix of
19230 IT->w from text at the current position of IT. See dispextern.h
19231 for an overview of struct it. Value is non-zero if
19232 IT->glyph_row displays text, as opposed to a line displaying ZV
19233 only. */
19235 static int
19236 display_line (struct it *it)
19238 struct glyph_row *row = it->glyph_row;
19239 Lisp_Object overlay_arrow_string;
19240 struct it wrap_it;
19241 void *wrap_data = NULL;
19242 int may_wrap = 0, wrap_x IF_LINT (= 0);
19243 int wrap_row_used = -1;
19244 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19245 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19246 int wrap_row_extra_line_spacing IF_LINT (= 0);
19247 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19248 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19249 int cvpos;
19250 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19251 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19253 /* We always start displaying at hpos zero even if hscrolled. */
19254 eassert (it->hpos == 0 && it->current_x == 0);
19256 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19257 >= it->w->desired_matrix->nrows)
19259 it->w->nrows_scale_factor++;
19260 it->f->fonts_changed = 1;
19261 return 0;
19264 /* Is IT->w showing the region? */
19265 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19267 /* Clear the result glyph row and enable it. */
19268 prepare_desired_row (row);
19270 row->y = it->current_y;
19271 row->start = it->start;
19272 row->continuation_lines_width = it->continuation_lines_width;
19273 row->displays_text_p = 1;
19274 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19275 it->starts_in_middle_of_char_p = 0;
19277 /* Arrange the overlays nicely for our purposes. Usually, we call
19278 display_line on only one line at a time, in which case this
19279 can't really hurt too much, or we call it on lines which appear
19280 one after another in the buffer, in which case all calls to
19281 recenter_overlay_lists but the first will be pretty cheap. */
19282 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19284 /* Move over display elements that are not visible because we are
19285 hscrolled. This may stop at an x-position < IT->first_visible_x
19286 if the first glyph is partially visible or if we hit a line end. */
19287 if (it->current_x < it->first_visible_x)
19289 enum move_it_result move_result;
19291 this_line_min_pos = row->start.pos;
19292 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19293 MOVE_TO_POS | MOVE_TO_X);
19294 /* If we are under a large hscroll, move_it_in_display_line_to
19295 could hit the end of the line without reaching
19296 it->first_visible_x. Pretend that we did reach it. This is
19297 especially important on a TTY, where we will call
19298 extend_face_to_end_of_line, which needs to know how many
19299 blank glyphs to produce. */
19300 if (it->current_x < it->first_visible_x
19301 && (move_result == MOVE_NEWLINE_OR_CR
19302 || move_result == MOVE_POS_MATCH_OR_ZV))
19303 it->current_x = it->first_visible_x;
19305 /* Record the smallest positions seen while we moved over
19306 display elements that are not visible. This is needed by
19307 redisplay_internal for optimizing the case where the cursor
19308 stays inside the same line. The rest of this function only
19309 considers positions that are actually displayed, so
19310 RECORD_MAX_MIN_POS will not otherwise record positions that
19311 are hscrolled to the left of the left edge of the window. */
19312 min_pos = CHARPOS (this_line_min_pos);
19313 min_bpos = BYTEPOS (this_line_min_pos);
19315 else
19317 /* We only do this when not calling `move_it_in_display_line_to'
19318 above, because move_it_in_display_line_to calls
19319 handle_line_prefix itself. */
19320 handle_line_prefix (it);
19323 /* Get the initial row height. This is either the height of the
19324 text hscrolled, if there is any, or zero. */
19325 row->ascent = it->max_ascent;
19326 row->height = it->max_ascent + it->max_descent;
19327 row->phys_ascent = it->max_phys_ascent;
19328 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19329 row->extra_line_spacing = it->max_extra_line_spacing;
19331 /* Utility macro to record max and min buffer positions seen until now. */
19332 #define RECORD_MAX_MIN_POS(IT) \
19333 do \
19335 int composition_p = !STRINGP ((IT)->string) \
19336 && ((IT)->what == IT_COMPOSITION); \
19337 ptrdiff_t current_pos = \
19338 composition_p ? (IT)->cmp_it.charpos \
19339 : IT_CHARPOS (*(IT)); \
19340 ptrdiff_t current_bpos = \
19341 composition_p ? CHAR_TO_BYTE (current_pos) \
19342 : IT_BYTEPOS (*(IT)); \
19343 if (current_pos < min_pos) \
19345 min_pos = current_pos; \
19346 min_bpos = current_bpos; \
19348 if (IT_CHARPOS (*it) > max_pos) \
19350 max_pos = IT_CHARPOS (*it); \
19351 max_bpos = IT_BYTEPOS (*it); \
19354 while (0)
19356 /* Loop generating characters. The loop is left with IT on the next
19357 character to display. */
19358 while (1)
19360 int n_glyphs_before, hpos_before, x_before;
19361 int x, nglyphs;
19362 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19364 /* Retrieve the next thing to display. Value is zero if end of
19365 buffer reached. */
19366 if (!get_next_display_element (it))
19368 /* Maybe add a space at the end of this line that is used to
19369 display the cursor there under X. Set the charpos of the
19370 first glyph of blank lines not corresponding to any text
19371 to -1. */
19372 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19373 row->exact_window_width_line_p = 1;
19374 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19375 || row->used[TEXT_AREA] == 0)
19377 row->glyphs[TEXT_AREA]->charpos = -1;
19378 row->displays_text_p = 0;
19380 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19381 && (!MINI_WINDOW_P (it->w)
19382 || (minibuf_level && EQ (it->window, minibuf_window))))
19383 row->indicate_empty_line_p = 1;
19386 it->continuation_lines_width = 0;
19387 row->ends_at_zv_p = 1;
19388 /* A row that displays right-to-left text must always have
19389 its last face extended all the way to the end of line,
19390 even if this row ends in ZV, because we still write to
19391 the screen left to right. We also need to extend the
19392 last face if the default face is remapped to some
19393 different face, otherwise the functions that clear
19394 portions of the screen will clear with the default face's
19395 background color. */
19396 if (row->reversed_p
19397 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19398 extend_face_to_end_of_line (it);
19399 break;
19402 /* Now, get the metrics of what we want to display. This also
19403 generates glyphs in `row' (which is IT->glyph_row). */
19404 n_glyphs_before = row->used[TEXT_AREA];
19405 x = it->current_x;
19407 /* Remember the line height so far in case the next element doesn't
19408 fit on the line. */
19409 if (it->line_wrap != TRUNCATE)
19411 ascent = it->max_ascent;
19412 descent = it->max_descent;
19413 phys_ascent = it->max_phys_ascent;
19414 phys_descent = it->max_phys_descent;
19416 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19418 if (IT_DISPLAYING_WHITESPACE (it))
19419 may_wrap = 1;
19420 else if (may_wrap)
19422 SAVE_IT (wrap_it, *it, wrap_data);
19423 wrap_x = x;
19424 wrap_row_used = row->used[TEXT_AREA];
19425 wrap_row_ascent = row->ascent;
19426 wrap_row_height = row->height;
19427 wrap_row_phys_ascent = row->phys_ascent;
19428 wrap_row_phys_height = row->phys_height;
19429 wrap_row_extra_line_spacing = row->extra_line_spacing;
19430 wrap_row_min_pos = min_pos;
19431 wrap_row_min_bpos = min_bpos;
19432 wrap_row_max_pos = max_pos;
19433 wrap_row_max_bpos = max_bpos;
19434 may_wrap = 0;
19439 PRODUCE_GLYPHS (it);
19441 /* If this display element was in marginal areas, continue with
19442 the next one. */
19443 if (it->area != TEXT_AREA)
19445 row->ascent = max (row->ascent, it->max_ascent);
19446 row->height = max (row->height, it->max_ascent + it->max_descent);
19447 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19448 row->phys_height = max (row->phys_height,
19449 it->max_phys_ascent + it->max_phys_descent);
19450 row->extra_line_spacing = max (row->extra_line_spacing,
19451 it->max_extra_line_spacing);
19452 set_iterator_to_next (it, 1);
19453 continue;
19456 /* Does the display element fit on the line? If we truncate
19457 lines, we should draw past the right edge of the window. If
19458 we don't truncate, we want to stop so that we can display the
19459 continuation glyph before the right margin. If lines are
19460 continued, there are two possible strategies for characters
19461 resulting in more than 1 glyph (e.g. tabs): Display as many
19462 glyphs as possible in this line and leave the rest for the
19463 continuation line, or display the whole element in the next
19464 line. Original redisplay did the former, so we do it also. */
19465 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19466 hpos_before = it->hpos;
19467 x_before = x;
19469 if (/* Not a newline. */
19470 nglyphs > 0
19471 /* Glyphs produced fit entirely in the line. */
19472 && it->current_x < it->last_visible_x)
19474 it->hpos += nglyphs;
19475 row->ascent = max (row->ascent, it->max_ascent);
19476 row->height = max (row->height, it->max_ascent + it->max_descent);
19477 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19478 row->phys_height = max (row->phys_height,
19479 it->max_phys_ascent + it->max_phys_descent);
19480 row->extra_line_spacing = max (row->extra_line_spacing,
19481 it->max_extra_line_spacing);
19482 if (it->current_x - it->pixel_width < it->first_visible_x)
19483 row->x = x - it->first_visible_x;
19484 /* Record the maximum and minimum buffer positions seen so
19485 far in glyphs that will be displayed by this row. */
19486 if (it->bidi_p)
19487 RECORD_MAX_MIN_POS (it);
19489 else
19491 int i, new_x;
19492 struct glyph *glyph;
19494 for (i = 0; i < nglyphs; ++i, x = new_x)
19496 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19497 new_x = x + glyph->pixel_width;
19499 if (/* Lines are continued. */
19500 it->line_wrap != TRUNCATE
19501 && (/* Glyph doesn't fit on the line. */
19502 new_x > it->last_visible_x
19503 /* Or it fits exactly on a window system frame. */
19504 || (new_x == it->last_visible_x
19505 && FRAME_WINDOW_P (it->f)
19506 && (row->reversed_p
19507 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19508 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19510 /* End of a continued line. */
19512 if (it->hpos == 0
19513 || (new_x == it->last_visible_x
19514 && FRAME_WINDOW_P (it->f)
19515 && (row->reversed_p
19516 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19517 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19519 /* Current glyph is the only one on the line or
19520 fits exactly on the line. We must continue
19521 the line because we can't draw the cursor
19522 after the glyph. */
19523 row->continued_p = 1;
19524 it->current_x = new_x;
19525 it->continuation_lines_width += new_x;
19526 ++it->hpos;
19527 if (i == nglyphs - 1)
19529 /* If line-wrap is on, check if a previous
19530 wrap point was found. */
19531 if (wrap_row_used > 0
19532 /* Even if there is a previous wrap
19533 point, continue the line here as
19534 usual, if (i) the previous character
19535 was a space or tab AND (ii) the
19536 current character is not. */
19537 && (!may_wrap
19538 || IT_DISPLAYING_WHITESPACE (it)))
19539 goto back_to_wrap;
19541 /* Record the maximum and minimum buffer
19542 positions seen so far in glyphs that will be
19543 displayed by this row. */
19544 if (it->bidi_p)
19545 RECORD_MAX_MIN_POS (it);
19546 set_iterator_to_next (it, 1);
19547 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19549 if (!get_next_display_element (it))
19551 row->exact_window_width_line_p = 1;
19552 it->continuation_lines_width = 0;
19553 row->continued_p = 0;
19554 row->ends_at_zv_p = 1;
19556 else if (ITERATOR_AT_END_OF_LINE_P (it))
19558 row->continued_p = 0;
19559 row->exact_window_width_line_p = 1;
19563 else if (it->bidi_p)
19564 RECORD_MAX_MIN_POS (it);
19566 else if (CHAR_GLYPH_PADDING_P (*glyph)
19567 && !FRAME_WINDOW_P (it->f))
19569 /* A padding glyph that doesn't fit on this line.
19570 This means the whole character doesn't fit
19571 on the line. */
19572 if (row->reversed_p)
19573 unproduce_glyphs (it, row->used[TEXT_AREA]
19574 - n_glyphs_before);
19575 row->used[TEXT_AREA] = n_glyphs_before;
19577 /* Fill the rest of the row with continuation
19578 glyphs like in 20.x. */
19579 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19580 < row->glyphs[1 + TEXT_AREA])
19581 produce_special_glyphs (it, IT_CONTINUATION);
19583 row->continued_p = 1;
19584 it->current_x = x_before;
19585 it->continuation_lines_width += x_before;
19587 /* Restore the height to what it was before the
19588 element not fitting on the line. */
19589 it->max_ascent = ascent;
19590 it->max_descent = descent;
19591 it->max_phys_ascent = phys_ascent;
19592 it->max_phys_descent = phys_descent;
19594 else if (wrap_row_used > 0)
19596 back_to_wrap:
19597 if (row->reversed_p)
19598 unproduce_glyphs (it,
19599 row->used[TEXT_AREA] - wrap_row_used);
19600 RESTORE_IT (it, &wrap_it, wrap_data);
19601 it->continuation_lines_width += wrap_x;
19602 row->used[TEXT_AREA] = wrap_row_used;
19603 row->ascent = wrap_row_ascent;
19604 row->height = wrap_row_height;
19605 row->phys_ascent = wrap_row_phys_ascent;
19606 row->phys_height = wrap_row_phys_height;
19607 row->extra_line_spacing = wrap_row_extra_line_spacing;
19608 min_pos = wrap_row_min_pos;
19609 min_bpos = wrap_row_min_bpos;
19610 max_pos = wrap_row_max_pos;
19611 max_bpos = wrap_row_max_bpos;
19612 row->continued_p = 1;
19613 row->ends_at_zv_p = 0;
19614 row->exact_window_width_line_p = 0;
19615 it->continuation_lines_width += x;
19617 /* Make sure that a non-default face is extended
19618 up to the right margin of the window. */
19619 extend_face_to_end_of_line (it);
19621 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19623 /* A TAB that extends past the right edge of the
19624 window. This produces a single glyph on
19625 window system frames. We leave the glyph in
19626 this row and let it fill the row, but don't
19627 consume the TAB. */
19628 if ((row->reversed_p
19629 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19630 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19631 produce_special_glyphs (it, IT_CONTINUATION);
19632 it->continuation_lines_width += it->last_visible_x;
19633 row->ends_in_middle_of_char_p = 1;
19634 row->continued_p = 1;
19635 glyph->pixel_width = it->last_visible_x - x;
19636 it->starts_in_middle_of_char_p = 1;
19638 else
19640 /* Something other than a TAB that draws past
19641 the right edge of the window. Restore
19642 positions to values before the element. */
19643 if (row->reversed_p)
19644 unproduce_glyphs (it, row->used[TEXT_AREA]
19645 - (n_glyphs_before + i));
19646 row->used[TEXT_AREA] = n_glyphs_before + i;
19648 /* Display continuation glyphs. */
19649 it->current_x = x_before;
19650 it->continuation_lines_width += x;
19651 if (!FRAME_WINDOW_P (it->f)
19652 || (row->reversed_p
19653 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19654 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19655 produce_special_glyphs (it, IT_CONTINUATION);
19656 row->continued_p = 1;
19658 extend_face_to_end_of_line (it);
19660 if (nglyphs > 1 && i > 0)
19662 row->ends_in_middle_of_char_p = 1;
19663 it->starts_in_middle_of_char_p = 1;
19666 /* Restore the height to what it was before the
19667 element not fitting on the line. */
19668 it->max_ascent = ascent;
19669 it->max_descent = descent;
19670 it->max_phys_ascent = phys_ascent;
19671 it->max_phys_descent = phys_descent;
19674 break;
19676 else if (new_x > it->first_visible_x)
19678 /* Increment number of glyphs actually displayed. */
19679 ++it->hpos;
19681 /* Record the maximum and minimum buffer positions
19682 seen so far in glyphs that will be displayed by
19683 this row. */
19684 if (it->bidi_p)
19685 RECORD_MAX_MIN_POS (it);
19687 if (x < it->first_visible_x)
19688 /* Glyph is partially visible, i.e. row starts at
19689 negative X position. */
19690 row->x = x - it->first_visible_x;
19692 else
19694 /* Glyph is completely off the left margin of the
19695 window. This should not happen because of the
19696 move_it_in_display_line at the start of this
19697 function, unless the text display area of the
19698 window is empty. */
19699 eassert (it->first_visible_x <= it->last_visible_x);
19702 /* Even if this display element produced no glyphs at all,
19703 we want to record its position. */
19704 if (it->bidi_p && nglyphs == 0)
19705 RECORD_MAX_MIN_POS (it);
19707 row->ascent = max (row->ascent, it->max_ascent);
19708 row->height = max (row->height, it->max_ascent + it->max_descent);
19709 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19710 row->phys_height = max (row->phys_height,
19711 it->max_phys_ascent + it->max_phys_descent);
19712 row->extra_line_spacing = max (row->extra_line_spacing,
19713 it->max_extra_line_spacing);
19715 /* End of this display line if row is continued. */
19716 if (row->continued_p || row->ends_at_zv_p)
19717 break;
19720 at_end_of_line:
19721 /* Is this a line end? If yes, we're also done, after making
19722 sure that a non-default face is extended up to the right
19723 margin of the window. */
19724 if (ITERATOR_AT_END_OF_LINE_P (it))
19726 int used_before = row->used[TEXT_AREA];
19728 row->ends_in_newline_from_string_p = STRINGP (it->object);
19730 /* Add a space at the end of the line that is used to
19731 display the cursor there. */
19732 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19733 append_space_for_newline (it, 0);
19735 /* Extend the face to the end of the line. */
19736 extend_face_to_end_of_line (it);
19738 /* Make sure we have the position. */
19739 if (used_before == 0)
19740 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19742 /* Record the position of the newline, for use in
19743 find_row_edges. */
19744 it->eol_pos = it->current.pos;
19746 /* Consume the line end. This skips over invisible lines. */
19747 set_iterator_to_next (it, 1);
19748 it->continuation_lines_width = 0;
19749 break;
19752 /* Proceed with next display element. Note that this skips
19753 over lines invisible because of selective display. */
19754 set_iterator_to_next (it, 1);
19756 /* If we truncate lines, we are done when the last displayed
19757 glyphs reach past the right margin of the window. */
19758 if (it->line_wrap == TRUNCATE
19759 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19760 ? (it->current_x >= it->last_visible_x)
19761 : (it->current_x > it->last_visible_x)))
19763 /* Maybe add truncation glyphs. */
19764 if (!FRAME_WINDOW_P (it->f)
19765 || (row->reversed_p
19766 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19767 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19769 int i, n;
19771 if (!row->reversed_p)
19773 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19774 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19775 break;
19777 else
19779 for (i = 0; i < row->used[TEXT_AREA]; i++)
19780 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19781 break;
19782 /* Remove any padding glyphs at the front of ROW, to
19783 make room for the truncation glyphs we will be
19784 adding below. The loop below always inserts at
19785 least one truncation glyph, so also remove the
19786 last glyph added to ROW. */
19787 unproduce_glyphs (it, i + 1);
19788 /* Adjust i for the loop below. */
19789 i = row->used[TEXT_AREA] - (i + 1);
19792 it->current_x = x_before;
19793 if (!FRAME_WINDOW_P (it->f))
19795 for (n = row->used[TEXT_AREA]; i < n; ++i)
19797 row->used[TEXT_AREA] = i;
19798 produce_special_glyphs (it, IT_TRUNCATION);
19801 else
19803 row->used[TEXT_AREA] = i;
19804 produce_special_glyphs (it, IT_TRUNCATION);
19807 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19809 /* Don't truncate if we can overflow newline into fringe. */
19810 if (!get_next_display_element (it))
19812 it->continuation_lines_width = 0;
19813 row->ends_at_zv_p = 1;
19814 row->exact_window_width_line_p = 1;
19815 break;
19817 if (ITERATOR_AT_END_OF_LINE_P (it))
19819 row->exact_window_width_line_p = 1;
19820 goto at_end_of_line;
19822 it->current_x = x_before;
19825 row->truncated_on_right_p = 1;
19826 it->continuation_lines_width = 0;
19827 reseat_at_next_visible_line_start (it, 0);
19828 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19829 it->hpos = hpos_before;
19830 break;
19834 if (wrap_data)
19835 bidi_unshelve_cache (wrap_data, 1);
19837 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19838 at the left window margin. */
19839 if (it->first_visible_x
19840 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19842 if (!FRAME_WINDOW_P (it->f)
19843 || (row->reversed_p
19844 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19845 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19846 insert_left_trunc_glyphs (it);
19847 row->truncated_on_left_p = 1;
19850 /* Remember the position at which this line ends.
19852 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19853 cannot be before the call to find_row_edges below, since that is
19854 where these positions are determined. */
19855 row->end = it->current;
19856 if (!it->bidi_p)
19858 row->minpos = row->start.pos;
19859 row->maxpos = row->end.pos;
19861 else
19863 /* ROW->minpos and ROW->maxpos must be the smallest and
19864 `1 + the largest' buffer positions in ROW. But if ROW was
19865 bidi-reordered, these two positions can be anywhere in the
19866 row, so we must determine them now. */
19867 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19870 /* If the start of this line is the overlay arrow-position, then
19871 mark this glyph row as the one containing the overlay arrow.
19872 This is clearly a mess with variable size fonts. It would be
19873 better to let it be displayed like cursors under X. */
19874 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19875 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19876 !NILP (overlay_arrow_string)))
19878 /* Overlay arrow in window redisplay is a fringe bitmap. */
19879 if (STRINGP (overlay_arrow_string))
19881 struct glyph_row *arrow_row
19882 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19883 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19884 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19885 struct glyph *p = row->glyphs[TEXT_AREA];
19886 struct glyph *p2, *end;
19888 /* Copy the arrow glyphs. */
19889 while (glyph < arrow_end)
19890 *p++ = *glyph++;
19892 /* Throw away padding glyphs. */
19893 p2 = p;
19894 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19895 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19896 ++p2;
19897 if (p2 > p)
19899 while (p2 < end)
19900 *p++ = *p2++;
19901 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19904 else
19906 eassert (INTEGERP (overlay_arrow_string));
19907 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19909 overlay_arrow_seen = 1;
19912 /* Highlight trailing whitespace. */
19913 if (!NILP (Vshow_trailing_whitespace))
19914 highlight_trailing_whitespace (it->f, it->glyph_row);
19916 /* Compute pixel dimensions of this line. */
19917 compute_line_metrics (it);
19919 /* Implementation note: No changes in the glyphs of ROW or in their
19920 faces can be done past this point, because compute_line_metrics
19921 computes ROW's hash value and stores it within the glyph_row
19922 structure. */
19924 /* Record whether this row ends inside an ellipsis. */
19925 row->ends_in_ellipsis_p
19926 = (it->method == GET_FROM_DISPLAY_VECTOR
19927 && it->ellipsis_p);
19929 /* Save fringe bitmaps in this row. */
19930 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19931 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19932 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19933 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19935 it->left_user_fringe_bitmap = 0;
19936 it->left_user_fringe_face_id = 0;
19937 it->right_user_fringe_bitmap = 0;
19938 it->right_user_fringe_face_id = 0;
19940 /* Maybe set the cursor. */
19941 cvpos = it->w->cursor.vpos;
19942 if ((cvpos < 0
19943 /* In bidi-reordered rows, keep checking for proper cursor
19944 position even if one has been found already, because buffer
19945 positions in such rows change non-linearly with ROW->VPOS,
19946 when a line is continued. One exception: when we are at ZV,
19947 display cursor on the first suitable glyph row, since all
19948 the empty rows after that also have their position set to ZV. */
19949 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19950 lines' rows is implemented for bidi-reordered rows. */
19951 || (it->bidi_p
19952 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19953 && PT >= MATRIX_ROW_START_CHARPOS (row)
19954 && PT <= MATRIX_ROW_END_CHARPOS (row)
19955 && cursor_row_p (row))
19956 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19958 /* Prepare for the next line. This line starts horizontally at (X
19959 HPOS) = (0 0). Vertical positions are incremented. As a
19960 convenience for the caller, IT->glyph_row is set to the next
19961 row to be used. */
19962 it->current_x = it->hpos = 0;
19963 it->current_y += row->height;
19964 SET_TEXT_POS (it->eol_pos, 0, 0);
19965 ++it->vpos;
19966 ++it->glyph_row;
19967 /* The next row should by default use the same value of the
19968 reversed_p flag as this one. set_iterator_to_next decides when
19969 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19970 the flag accordingly. */
19971 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19972 it->glyph_row->reversed_p = row->reversed_p;
19973 it->start = row->end;
19974 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19976 #undef RECORD_MAX_MIN_POS
19979 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19980 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19981 doc: /* Return paragraph direction at point in BUFFER.
19982 Value is either `left-to-right' or `right-to-left'.
19983 If BUFFER is omitted or nil, it defaults to the current buffer.
19985 Paragraph direction determines how the text in the paragraph is displayed.
19986 In left-to-right paragraphs, text begins at the left margin of the window
19987 and the reading direction is generally left to right. In right-to-left
19988 paragraphs, text begins at the right margin and is read from right to left.
19990 See also `bidi-paragraph-direction'. */)
19991 (Lisp_Object buffer)
19993 struct buffer *buf = current_buffer;
19994 struct buffer *old = buf;
19996 if (! NILP (buffer))
19998 CHECK_BUFFER (buffer);
19999 buf = XBUFFER (buffer);
20002 if (NILP (BVAR (buf, bidi_display_reordering))
20003 || NILP (BVAR (buf, enable_multibyte_characters))
20004 /* When we are loading loadup.el, the character property tables
20005 needed for bidi iteration are not yet available. */
20006 || !NILP (Vpurify_flag))
20007 return Qleft_to_right;
20008 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20009 return BVAR (buf, bidi_paragraph_direction);
20010 else
20012 /* Determine the direction from buffer text. We could try to
20013 use current_matrix if it is up to date, but this seems fast
20014 enough as it is. */
20015 struct bidi_it itb;
20016 ptrdiff_t pos = BUF_PT (buf);
20017 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20018 int c;
20019 void *itb_data = bidi_shelve_cache ();
20021 set_buffer_temp (buf);
20022 /* bidi_paragraph_init finds the base direction of the paragraph
20023 by searching forward from paragraph start. We need the base
20024 direction of the current or _previous_ paragraph, so we need
20025 to make sure we are within that paragraph. To that end, find
20026 the previous non-empty line. */
20027 if (pos >= ZV && pos > BEGV)
20028 DEC_BOTH (pos, bytepos);
20029 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20030 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20032 while ((c = FETCH_BYTE (bytepos)) == '\n'
20033 || c == ' ' || c == '\t' || c == '\f')
20035 if (bytepos <= BEGV_BYTE)
20036 break;
20037 bytepos--;
20038 pos--;
20040 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20041 bytepos--;
20043 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20044 itb.paragraph_dir = NEUTRAL_DIR;
20045 itb.string.s = NULL;
20046 itb.string.lstring = Qnil;
20047 itb.string.bufpos = 0;
20048 itb.string.unibyte = 0;
20049 /* We have no window to use here for ignoring window-specific
20050 overlays. Using NULL for window pointer will cause
20051 compute_display_string_pos to use the current buffer. */
20052 itb.w = NULL;
20053 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20054 bidi_unshelve_cache (itb_data, 0);
20055 set_buffer_temp (old);
20056 switch (itb.paragraph_dir)
20058 case L2R:
20059 return Qleft_to_right;
20060 break;
20061 case R2L:
20062 return Qright_to_left;
20063 break;
20064 default:
20065 emacs_abort ();
20070 DEFUN ("move-point-visually", Fmove_point_visually,
20071 Smove_point_visually, 1, 1, 0,
20072 doc: /* Move point in the visual order in the specified DIRECTION.
20073 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20074 left.
20076 Value is the new character position of point. */)
20077 (Lisp_Object direction)
20079 struct window *w = XWINDOW (selected_window);
20080 struct buffer *b = XBUFFER (w->contents);
20081 struct glyph_row *row;
20082 int dir;
20083 Lisp_Object paragraph_dir;
20085 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20086 (!(ROW)->continued_p \
20087 && INTEGERP ((GLYPH)->object) \
20088 && (GLYPH)->type == CHAR_GLYPH \
20089 && (GLYPH)->u.ch == ' ' \
20090 && (GLYPH)->charpos >= 0 \
20091 && !(GLYPH)->avoid_cursor_p)
20093 CHECK_NUMBER (direction);
20094 dir = XINT (direction);
20095 if (dir > 0)
20096 dir = 1;
20097 else
20098 dir = -1;
20100 /* If current matrix is up-to-date, we can use the information
20101 recorded in the glyphs, at least as long as the goal is on the
20102 screen. */
20103 if (w->window_end_valid
20104 && !windows_or_buffers_changed
20105 && b
20106 && !b->clip_changed
20107 && !b->prevent_redisplay_optimizations_p
20108 && !window_outdated (w)
20109 && w->cursor.vpos >= 0
20110 && w->cursor.vpos < w->current_matrix->nrows
20111 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20113 struct glyph *g = row->glyphs[TEXT_AREA];
20114 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20115 struct glyph *gpt = g + w->cursor.hpos;
20117 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20119 if (BUFFERP (g->object) && g->charpos != PT)
20121 SET_PT (g->charpos);
20122 w->cursor.vpos = -1;
20123 return make_number (PT);
20125 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20127 ptrdiff_t new_pos;
20129 if (BUFFERP (gpt->object))
20131 new_pos = PT;
20132 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20133 new_pos += (row->reversed_p ? -dir : dir);
20134 else
20135 new_pos -= (row->reversed_p ? -dir : dir);;
20137 else if (BUFFERP (g->object))
20138 new_pos = g->charpos;
20139 else
20140 break;
20141 SET_PT (new_pos);
20142 w->cursor.vpos = -1;
20143 return make_number (PT);
20145 else if (ROW_GLYPH_NEWLINE_P (row, g))
20147 /* Glyphs inserted at the end of a non-empty line for
20148 positioning the cursor have zero charpos, so we must
20149 deduce the value of point by other means. */
20150 if (g->charpos > 0)
20151 SET_PT (g->charpos);
20152 else if (row->ends_at_zv_p && PT != ZV)
20153 SET_PT (ZV);
20154 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20155 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20156 else
20157 break;
20158 w->cursor.vpos = -1;
20159 return make_number (PT);
20162 if (g == e || INTEGERP (g->object))
20164 if (row->truncated_on_left_p || row->truncated_on_right_p)
20165 goto simulate_display;
20166 if (!row->reversed_p)
20167 row += dir;
20168 else
20169 row -= dir;
20170 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20171 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20172 goto simulate_display;
20174 if (dir > 0)
20176 if (row->reversed_p && !row->continued_p)
20178 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20179 w->cursor.vpos = -1;
20180 return make_number (PT);
20182 g = row->glyphs[TEXT_AREA];
20183 e = g + row->used[TEXT_AREA];
20184 for ( ; g < e; g++)
20186 if (BUFFERP (g->object)
20187 /* Empty lines have only one glyph, which stands
20188 for the newline, and whose charpos is the
20189 buffer position of the newline. */
20190 || ROW_GLYPH_NEWLINE_P (row, g)
20191 /* When the buffer ends in a newline, the line at
20192 EOB also has one glyph, but its charpos is -1. */
20193 || (row->ends_at_zv_p
20194 && !row->reversed_p
20195 && INTEGERP (g->object)
20196 && g->type == CHAR_GLYPH
20197 && g->u.ch == ' '))
20199 if (g->charpos > 0)
20200 SET_PT (g->charpos);
20201 else if (!row->reversed_p
20202 && row->ends_at_zv_p
20203 && PT != ZV)
20204 SET_PT (ZV);
20205 else
20206 continue;
20207 w->cursor.vpos = -1;
20208 return make_number (PT);
20212 else
20214 if (!row->reversed_p && !row->continued_p)
20216 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20217 w->cursor.vpos = -1;
20218 return make_number (PT);
20220 e = row->glyphs[TEXT_AREA];
20221 g = e + row->used[TEXT_AREA] - 1;
20222 for ( ; g >= e; g--)
20224 if (BUFFERP (g->object)
20225 || (ROW_GLYPH_NEWLINE_P (row, g)
20226 && g->charpos > 0)
20227 /* Empty R2L lines on GUI frames have the buffer
20228 position of the newline stored in the stretch
20229 glyph. */
20230 || g->type == STRETCH_GLYPH
20231 || (row->ends_at_zv_p
20232 && row->reversed_p
20233 && INTEGERP (g->object)
20234 && g->type == CHAR_GLYPH
20235 && g->u.ch == ' '))
20237 if (g->charpos > 0)
20238 SET_PT (g->charpos);
20239 else if (row->reversed_p
20240 && row->ends_at_zv_p
20241 && PT != ZV)
20242 SET_PT (ZV);
20243 else
20244 continue;
20245 w->cursor.vpos = -1;
20246 return make_number (PT);
20253 simulate_display:
20255 /* If we wind up here, we failed to move by using the glyphs, so we
20256 need to simulate display instead. */
20258 if (b)
20259 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20260 else
20261 paragraph_dir = Qleft_to_right;
20262 if (EQ (paragraph_dir, Qright_to_left))
20263 dir = -dir;
20264 if (PT <= BEGV && dir < 0)
20265 xsignal0 (Qbeginning_of_buffer);
20266 else if (PT >= ZV && dir > 0)
20267 xsignal0 (Qend_of_buffer);
20268 else
20270 struct text_pos pt;
20271 struct it it;
20272 int pt_x, target_x, pixel_width, pt_vpos;
20273 bool at_eol_p;
20274 bool overshoot_expected = false;
20275 bool target_is_eol_p = false;
20277 /* Setup the arena. */
20278 SET_TEXT_POS (pt, PT, PT_BYTE);
20279 start_display (&it, w, pt);
20281 if (it.cmp_it.id < 0
20282 && it.method == GET_FROM_STRING
20283 && it.area == TEXT_AREA
20284 && it.string_from_display_prop_p
20285 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20286 overshoot_expected = true;
20288 /* Find the X coordinate of point. We start from the beginning
20289 of this or previous line to make sure we are before point in
20290 the logical order (since the move_it_* functions can only
20291 move forward). */
20292 reseat_at_previous_visible_line_start (&it);
20293 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20294 if (IT_CHARPOS (it) != PT)
20295 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20296 -1, -1, -1, MOVE_TO_POS);
20297 pt_x = it.current_x;
20298 pt_vpos = it.vpos;
20299 if (dir > 0 || overshoot_expected)
20301 struct glyph_row *row = it.glyph_row;
20303 /* When point is at beginning of line, we don't have
20304 information about the glyph there loaded into struct
20305 it. Calling get_next_display_element fixes that. */
20306 if (pt_x == 0)
20307 get_next_display_element (&it);
20308 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20309 it.glyph_row = NULL;
20310 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20311 it.glyph_row = row;
20312 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20313 it, lest it will become out of sync with it's buffer
20314 position. */
20315 it.current_x = pt_x;
20317 else
20318 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20319 pixel_width = it.pixel_width;
20320 if (overshoot_expected && at_eol_p)
20321 pixel_width = 0;
20322 else if (pixel_width <= 0)
20323 pixel_width = 1;
20325 /* If there's a display string at point, we are actually at the
20326 glyph to the left of point, so we need to correct the X
20327 coordinate. */
20328 if (overshoot_expected)
20329 pt_x += pixel_width;
20331 /* Compute target X coordinate, either to the left or to the
20332 right of point. On TTY frames, all characters have the same
20333 pixel width of 1, so we can use that. On GUI frames we don't
20334 have an easy way of getting at the pixel width of the
20335 character to the left of point, so we use a different method
20336 of getting to that place. */
20337 if (dir > 0)
20338 target_x = pt_x + pixel_width;
20339 else
20340 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20342 /* Target X coordinate could be one line above or below the line
20343 of point, in which case we need to adjust the target X
20344 coordinate. Also, if moving to the left, we need to begin at
20345 the left edge of the point's screen line. */
20346 if (dir < 0)
20348 if (pt_x > 0)
20350 start_display (&it, w, pt);
20351 reseat_at_previous_visible_line_start (&it);
20352 it.current_x = it.current_y = it.hpos = 0;
20353 if (pt_vpos != 0)
20354 move_it_by_lines (&it, pt_vpos);
20356 else
20358 move_it_by_lines (&it, -1);
20359 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20360 target_is_eol_p = true;
20363 else
20365 if (at_eol_p
20366 || (target_x >= it.last_visible_x
20367 && it.line_wrap != TRUNCATE))
20369 if (pt_x > 0)
20370 move_it_by_lines (&it, 0);
20371 move_it_by_lines (&it, 1);
20372 target_x = 0;
20376 /* Move to the target X coordinate. */
20377 #ifdef HAVE_WINDOW_SYSTEM
20378 /* On GUI frames, as we don't know the X coordinate of the
20379 character to the left of point, moving point to the left
20380 requires walking, one grapheme cluster at a time, until we
20381 find ourself at a place immediately to the left of the
20382 character at point. */
20383 if (FRAME_WINDOW_P (it.f) && dir < 0)
20385 struct text_pos new_pos = it.current.pos;
20386 enum move_it_result rc = MOVE_X_REACHED;
20388 while (it.current_x + it.pixel_width <= target_x
20389 && rc == MOVE_X_REACHED)
20391 int new_x = it.current_x + it.pixel_width;
20393 new_pos = it.current.pos;
20394 if (new_x == it.current_x)
20395 new_x++;
20396 rc = move_it_in_display_line_to (&it, ZV, new_x,
20397 MOVE_TO_POS | MOVE_TO_X);
20398 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20399 break;
20401 /* If we ended up on a composed character inside
20402 bidi-reordered text (e.g., Hebrew text with diacritics),
20403 the iterator gives us the buffer position of the last (in
20404 logical order) character of the composed grapheme cluster,
20405 which is not what we want. So we cheat: we compute the
20406 character position of the character that follows (in the
20407 logical order) the one where the above loop stopped. That
20408 character will appear on display to the left of point. */
20409 if (it.bidi_p
20410 && it.bidi_it.scan_dir == -1
20411 && new_pos.charpos - IT_CHARPOS (it) > 1)
20413 new_pos.charpos = IT_CHARPOS (it) + 1;
20414 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20416 it.current.pos = new_pos;
20418 else
20419 #endif
20420 if (it.current_x != target_x)
20421 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20423 /* When lines are truncated, the above loop will stop at the
20424 window edge. But we want to get to the end of line, even if
20425 it is beyond the window edge; automatic hscroll will then
20426 scroll the window to show point as appropriate. */
20427 if (target_is_eol_p && it.line_wrap == TRUNCATE
20428 && get_next_display_element (&it))
20430 struct text_pos new_pos = it.current.pos;
20432 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20434 set_iterator_to_next (&it, 0);
20435 if (it.method == GET_FROM_BUFFER)
20436 new_pos = it.current.pos;
20437 if (!get_next_display_element (&it))
20438 break;
20441 it.current.pos = new_pos;
20444 /* If we ended up in a display string that covers point, move to
20445 buffer position to the right in the visual order. */
20446 if (dir > 0)
20448 while (IT_CHARPOS (it) == PT)
20450 set_iterator_to_next (&it, 0);
20451 if (!get_next_display_element (&it))
20452 break;
20456 /* Move point to that position. */
20457 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20460 return make_number (PT);
20462 #undef ROW_GLYPH_NEWLINE_P
20466 /***********************************************************************
20467 Menu Bar
20468 ***********************************************************************/
20470 /* Redisplay the menu bar in the frame for window W.
20472 The menu bar of X frames that don't have X toolkit support is
20473 displayed in a special window W->frame->menu_bar_window.
20475 The menu bar of terminal frames is treated specially as far as
20476 glyph matrices are concerned. Menu bar lines are not part of
20477 windows, so the update is done directly on the frame matrix rows
20478 for the menu bar. */
20480 static void
20481 display_menu_bar (struct window *w)
20483 struct frame *f = XFRAME (WINDOW_FRAME (w));
20484 struct it it;
20485 Lisp_Object items;
20486 int i;
20488 /* Don't do all this for graphical frames. */
20489 #ifdef HAVE_NTGUI
20490 if (FRAME_W32_P (f))
20491 return;
20492 #endif
20493 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20494 if (FRAME_X_P (f))
20495 return;
20496 #endif
20498 #ifdef HAVE_NS
20499 if (FRAME_NS_P (f))
20500 return;
20501 #endif /* HAVE_NS */
20503 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20504 eassert (!FRAME_WINDOW_P (f));
20505 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20506 it.first_visible_x = 0;
20507 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20508 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20509 if (FRAME_WINDOW_P (f))
20511 /* Menu bar lines are displayed in the desired matrix of the
20512 dummy window menu_bar_window. */
20513 struct window *menu_w;
20514 menu_w = XWINDOW (f->menu_bar_window);
20515 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20516 MENU_FACE_ID);
20517 it.first_visible_x = 0;
20518 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20520 else
20521 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20523 /* This is a TTY frame, i.e. character hpos/vpos are used as
20524 pixel x/y. */
20525 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20526 MENU_FACE_ID);
20527 it.first_visible_x = 0;
20528 it.last_visible_x = FRAME_COLS (f);
20531 /* FIXME: This should be controlled by a user option. See the
20532 comments in redisplay_tool_bar and display_mode_line about
20533 this. */
20534 it.paragraph_embedding = L2R;
20536 /* Clear all rows of the menu bar. */
20537 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20539 struct glyph_row *row = it.glyph_row + i;
20540 clear_glyph_row (row);
20541 row->enabled_p = 1;
20542 row->full_width_p = 1;
20545 /* Display all items of the menu bar. */
20546 items = FRAME_MENU_BAR_ITEMS (it.f);
20547 for (i = 0; i < ASIZE (items); i += 4)
20549 Lisp_Object string;
20551 /* Stop at nil string. */
20552 string = AREF (items, i + 1);
20553 if (NILP (string))
20554 break;
20556 /* Remember where item was displayed. */
20557 ASET (items, i + 3, make_number (it.hpos));
20559 /* Display the item, pad with one space. */
20560 if (it.current_x < it.last_visible_x)
20561 display_string (NULL, string, Qnil, 0, 0, &it,
20562 SCHARS (string) + 1, 0, 0, -1);
20565 /* Fill out the line with spaces. */
20566 if (it.current_x < it.last_visible_x)
20567 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20569 /* Compute the total height of the lines. */
20570 compute_line_metrics (&it);
20575 /***********************************************************************
20576 Mode Line
20577 ***********************************************************************/
20579 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20580 FORCE is non-zero, redisplay mode lines unconditionally.
20581 Otherwise, redisplay only mode lines that are garbaged. Value is
20582 the number of windows whose mode lines were redisplayed. */
20584 static int
20585 redisplay_mode_lines (Lisp_Object window, int force)
20587 int nwindows = 0;
20589 while (!NILP (window))
20591 struct window *w = XWINDOW (window);
20593 if (WINDOWP (w->contents))
20594 nwindows += redisplay_mode_lines (w->contents, force);
20595 else if (force
20596 || FRAME_GARBAGED_P (XFRAME (w->frame))
20597 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20599 struct text_pos lpoint;
20600 struct buffer *old = current_buffer;
20602 /* Set the window's buffer for the mode line display. */
20603 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20604 set_buffer_internal_1 (XBUFFER (w->contents));
20606 /* Point refers normally to the selected window. For any
20607 other window, set up appropriate value. */
20608 if (!EQ (window, selected_window))
20610 struct text_pos pt;
20612 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20613 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20616 /* Display mode lines. */
20617 clear_glyph_matrix (w->desired_matrix);
20618 if (display_mode_lines (w))
20620 ++nwindows;
20621 w->must_be_updated_p = 1;
20624 /* Restore old settings. */
20625 set_buffer_internal_1 (old);
20626 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20629 window = w->next;
20632 return nwindows;
20636 /* Display the mode and/or header line of window W. Value is the
20637 sum number of mode lines and header lines displayed. */
20639 static int
20640 display_mode_lines (struct window *w)
20642 Lisp_Object old_selected_window = selected_window;
20643 Lisp_Object old_selected_frame = selected_frame;
20644 Lisp_Object new_frame = w->frame;
20645 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20646 int n = 0;
20648 selected_frame = new_frame;
20649 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20650 or window's point, then we'd need select_window_1 here as well. */
20651 XSETWINDOW (selected_window, w);
20652 XFRAME (new_frame)->selected_window = selected_window;
20654 /* These will be set while the mode line specs are processed. */
20655 line_number_displayed = 0;
20656 w->column_number_displayed = -1;
20658 if (WINDOW_WANTS_MODELINE_P (w))
20660 struct window *sel_w = XWINDOW (old_selected_window);
20662 /* Select mode line face based on the real selected window. */
20663 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20664 BVAR (current_buffer, mode_line_format));
20665 ++n;
20668 if (WINDOW_WANTS_HEADER_LINE_P (w))
20670 display_mode_line (w, HEADER_LINE_FACE_ID,
20671 BVAR (current_buffer, header_line_format));
20672 ++n;
20675 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20676 selected_frame = old_selected_frame;
20677 selected_window = old_selected_window;
20678 return n;
20682 /* Display mode or header line of window W. FACE_ID specifies which
20683 line to display; it is either MODE_LINE_FACE_ID or
20684 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20685 display. Value is the pixel height of the mode/header line
20686 displayed. */
20688 static int
20689 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20691 struct it it;
20692 struct face *face;
20693 ptrdiff_t count = SPECPDL_INDEX ();
20695 init_iterator (&it, w, -1, -1, NULL, face_id);
20696 /* Don't extend on a previously drawn mode-line.
20697 This may happen if called from pos_visible_p. */
20698 it.glyph_row->enabled_p = 0;
20699 prepare_desired_row (it.glyph_row);
20701 it.glyph_row->mode_line_p = 1;
20703 /* FIXME: This should be controlled by a user option. But
20704 supporting such an option is not trivial, since the mode line is
20705 made up of many separate strings. */
20706 it.paragraph_embedding = L2R;
20708 record_unwind_protect (unwind_format_mode_line,
20709 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20711 mode_line_target = MODE_LINE_DISPLAY;
20713 /* Temporarily make frame's keyboard the current kboard so that
20714 kboard-local variables in the mode_line_format will get the right
20715 values. */
20716 push_kboard (FRAME_KBOARD (it.f));
20717 record_unwind_save_match_data ();
20718 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20719 pop_kboard ();
20721 unbind_to (count, Qnil);
20723 /* Fill up with spaces. */
20724 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20726 compute_line_metrics (&it);
20727 it.glyph_row->full_width_p = 1;
20728 it.glyph_row->continued_p = 0;
20729 it.glyph_row->truncated_on_left_p = 0;
20730 it.glyph_row->truncated_on_right_p = 0;
20732 /* Make a 3D mode-line have a shadow at its right end. */
20733 face = FACE_FROM_ID (it.f, face_id);
20734 extend_face_to_end_of_line (&it);
20735 if (face->box != FACE_NO_BOX)
20737 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20738 + it.glyph_row->used[TEXT_AREA] - 1);
20739 last->right_box_line_p = 1;
20742 return it.glyph_row->height;
20745 /* Move element ELT in LIST to the front of LIST.
20746 Return the updated list. */
20748 static Lisp_Object
20749 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20751 register Lisp_Object tail, prev;
20752 register Lisp_Object tem;
20754 tail = list;
20755 prev = Qnil;
20756 while (CONSP (tail))
20758 tem = XCAR (tail);
20760 if (EQ (elt, tem))
20762 /* Splice out the link TAIL. */
20763 if (NILP (prev))
20764 list = XCDR (tail);
20765 else
20766 Fsetcdr (prev, XCDR (tail));
20768 /* Now make it the first. */
20769 Fsetcdr (tail, list);
20770 return tail;
20772 else
20773 prev = tail;
20774 tail = XCDR (tail);
20775 QUIT;
20778 /* Not found--return unchanged LIST. */
20779 return list;
20782 /* Contribute ELT to the mode line for window IT->w. How it
20783 translates into text depends on its data type.
20785 IT describes the display environment in which we display, as usual.
20787 DEPTH is the depth in recursion. It is used to prevent
20788 infinite recursion here.
20790 FIELD_WIDTH is the number of characters the display of ELT should
20791 occupy in the mode line, and PRECISION is the maximum number of
20792 characters to display from ELT's representation. See
20793 display_string for details.
20795 Returns the hpos of the end of the text generated by ELT.
20797 PROPS is a property list to add to any string we encounter.
20799 If RISKY is nonzero, remove (disregard) any properties in any string
20800 we encounter, and ignore :eval and :propertize.
20802 The global variable `mode_line_target' determines whether the
20803 output is passed to `store_mode_line_noprop',
20804 `store_mode_line_string', or `display_string'. */
20806 static int
20807 display_mode_element (struct it *it, int depth, int field_width, int precision,
20808 Lisp_Object elt, Lisp_Object props, int risky)
20810 int n = 0, field, prec;
20811 int literal = 0;
20813 tail_recurse:
20814 if (depth > 100)
20815 elt = build_string ("*too-deep*");
20817 depth++;
20819 switch (XTYPE (elt))
20821 case Lisp_String:
20823 /* A string: output it and check for %-constructs within it. */
20824 unsigned char c;
20825 ptrdiff_t offset = 0;
20827 if (SCHARS (elt) > 0
20828 && (!NILP (props) || risky))
20830 Lisp_Object oprops, aelt;
20831 oprops = Ftext_properties_at (make_number (0), elt);
20833 /* If the starting string's properties are not what
20834 we want, translate the string. Also, if the string
20835 is risky, do that anyway. */
20837 if (NILP (Fequal (props, oprops)) || risky)
20839 /* If the starting string has properties,
20840 merge the specified ones onto the existing ones. */
20841 if (! NILP (oprops) && !risky)
20843 Lisp_Object tem;
20845 oprops = Fcopy_sequence (oprops);
20846 tem = props;
20847 while (CONSP (tem))
20849 oprops = Fplist_put (oprops, XCAR (tem),
20850 XCAR (XCDR (tem)));
20851 tem = XCDR (XCDR (tem));
20853 props = oprops;
20856 aelt = Fassoc (elt, mode_line_proptrans_alist);
20857 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20859 /* AELT is what we want. Move it to the front
20860 without consing. */
20861 elt = XCAR (aelt);
20862 mode_line_proptrans_alist
20863 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20865 else
20867 Lisp_Object tem;
20869 /* If AELT has the wrong props, it is useless.
20870 so get rid of it. */
20871 if (! NILP (aelt))
20872 mode_line_proptrans_alist
20873 = Fdelq (aelt, mode_line_proptrans_alist);
20875 elt = Fcopy_sequence (elt);
20876 Fset_text_properties (make_number (0), Flength (elt),
20877 props, elt);
20878 /* Add this item to mode_line_proptrans_alist. */
20879 mode_line_proptrans_alist
20880 = Fcons (Fcons (elt, props),
20881 mode_line_proptrans_alist);
20882 /* Truncate mode_line_proptrans_alist
20883 to at most 50 elements. */
20884 tem = Fnthcdr (make_number (50),
20885 mode_line_proptrans_alist);
20886 if (! NILP (tem))
20887 XSETCDR (tem, Qnil);
20892 offset = 0;
20894 if (literal)
20896 prec = precision - n;
20897 switch (mode_line_target)
20899 case MODE_LINE_NOPROP:
20900 case MODE_LINE_TITLE:
20901 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20902 break;
20903 case MODE_LINE_STRING:
20904 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20905 break;
20906 case MODE_LINE_DISPLAY:
20907 n += display_string (NULL, elt, Qnil, 0, 0, it,
20908 0, prec, 0, STRING_MULTIBYTE (elt));
20909 break;
20912 break;
20915 /* Handle the non-literal case. */
20917 while ((precision <= 0 || n < precision)
20918 && SREF (elt, offset) != 0
20919 && (mode_line_target != MODE_LINE_DISPLAY
20920 || it->current_x < it->last_visible_x))
20922 ptrdiff_t last_offset = offset;
20924 /* Advance to end of string or next format specifier. */
20925 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20928 if (offset - 1 != last_offset)
20930 ptrdiff_t nchars, nbytes;
20932 /* Output to end of string or up to '%'. Field width
20933 is length of string. Don't output more than
20934 PRECISION allows us. */
20935 offset--;
20937 prec = c_string_width (SDATA (elt) + last_offset,
20938 offset - last_offset, precision - n,
20939 &nchars, &nbytes);
20941 switch (mode_line_target)
20943 case MODE_LINE_NOPROP:
20944 case MODE_LINE_TITLE:
20945 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20946 break;
20947 case MODE_LINE_STRING:
20949 ptrdiff_t bytepos = last_offset;
20950 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20951 ptrdiff_t endpos = (precision <= 0
20952 ? string_byte_to_char (elt, offset)
20953 : charpos + nchars);
20955 n += store_mode_line_string (NULL,
20956 Fsubstring (elt, make_number (charpos),
20957 make_number (endpos)),
20958 0, 0, 0, Qnil);
20960 break;
20961 case MODE_LINE_DISPLAY:
20963 ptrdiff_t bytepos = last_offset;
20964 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20966 if (precision <= 0)
20967 nchars = string_byte_to_char (elt, offset) - charpos;
20968 n += display_string (NULL, elt, Qnil, 0, charpos,
20969 it, 0, nchars, 0,
20970 STRING_MULTIBYTE (elt));
20972 break;
20975 else /* c == '%' */
20977 ptrdiff_t percent_position = offset;
20979 /* Get the specified minimum width. Zero means
20980 don't pad. */
20981 field = 0;
20982 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20983 field = field * 10 + c - '0';
20985 /* Don't pad beyond the total padding allowed. */
20986 if (field_width - n > 0 && field > field_width - n)
20987 field = field_width - n;
20989 /* Note that either PRECISION <= 0 or N < PRECISION. */
20990 prec = precision - n;
20992 if (c == 'M')
20993 n += display_mode_element (it, depth, field, prec,
20994 Vglobal_mode_string, props,
20995 risky);
20996 else if (c != 0)
20998 bool multibyte;
20999 ptrdiff_t bytepos, charpos;
21000 const char *spec;
21001 Lisp_Object string;
21003 bytepos = percent_position;
21004 charpos = (STRING_MULTIBYTE (elt)
21005 ? string_byte_to_char (elt, bytepos)
21006 : bytepos);
21007 spec = decode_mode_spec (it->w, c, field, &string);
21008 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21010 switch (mode_line_target)
21012 case MODE_LINE_NOPROP:
21013 case MODE_LINE_TITLE:
21014 n += store_mode_line_noprop (spec, field, prec);
21015 break;
21016 case MODE_LINE_STRING:
21018 Lisp_Object tem = build_string (spec);
21019 props = Ftext_properties_at (make_number (charpos), elt);
21020 /* Should only keep face property in props */
21021 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21023 break;
21024 case MODE_LINE_DISPLAY:
21026 int nglyphs_before, nwritten;
21028 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21029 nwritten = display_string (spec, string, elt,
21030 charpos, 0, it,
21031 field, prec, 0,
21032 multibyte);
21034 /* Assign to the glyphs written above the
21035 string where the `%x' came from, position
21036 of the `%'. */
21037 if (nwritten > 0)
21039 struct glyph *glyph
21040 = (it->glyph_row->glyphs[TEXT_AREA]
21041 + nglyphs_before);
21042 int i;
21044 for (i = 0; i < nwritten; ++i)
21046 glyph[i].object = elt;
21047 glyph[i].charpos = charpos;
21050 n += nwritten;
21053 break;
21056 else /* c == 0 */
21057 break;
21061 break;
21063 case Lisp_Symbol:
21064 /* A symbol: process the value of the symbol recursively
21065 as if it appeared here directly. Avoid error if symbol void.
21066 Special case: if value of symbol is a string, output the string
21067 literally. */
21069 register Lisp_Object tem;
21071 /* If the variable is not marked as risky to set
21072 then its contents are risky to use. */
21073 if (NILP (Fget (elt, Qrisky_local_variable)))
21074 risky = 1;
21076 tem = Fboundp (elt);
21077 if (!NILP (tem))
21079 tem = Fsymbol_value (elt);
21080 /* If value is a string, output that string literally:
21081 don't check for % within it. */
21082 if (STRINGP (tem))
21083 literal = 1;
21085 if (!EQ (tem, elt))
21087 /* Give up right away for nil or t. */
21088 elt = tem;
21089 goto tail_recurse;
21093 break;
21095 case Lisp_Cons:
21097 register Lisp_Object car, tem;
21099 /* A cons cell: five distinct cases.
21100 If first element is :eval or :propertize, do something special.
21101 If first element is a string or a cons, process all the elements
21102 and effectively concatenate them.
21103 If first element is a negative number, truncate displaying cdr to
21104 at most that many characters. If positive, pad (with spaces)
21105 to at least that many characters.
21106 If first element is a symbol, process the cadr or caddr recursively
21107 according to whether the symbol's value is non-nil or nil. */
21108 car = XCAR (elt);
21109 if (EQ (car, QCeval))
21111 /* An element of the form (:eval FORM) means evaluate FORM
21112 and use the result as mode line elements. */
21114 if (risky)
21115 break;
21117 if (CONSP (XCDR (elt)))
21119 Lisp_Object spec;
21120 spec = safe_eval (XCAR (XCDR (elt)));
21121 n += display_mode_element (it, depth, field_width - n,
21122 precision - n, spec, props,
21123 risky);
21126 else if (EQ (car, QCpropertize))
21128 /* An element of the form (:propertize ELT PROPS...)
21129 means display ELT but applying properties PROPS. */
21131 if (risky)
21132 break;
21134 if (CONSP (XCDR (elt)))
21135 n += display_mode_element (it, depth, field_width - n,
21136 precision - n, XCAR (XCDR (elt)),
21137 XCDR (XCDR (elt)), risky);
21139 else if (SYMBOLP (car))
21141 tem = Fboundp (car);
21142 elt = XCDR (elt);
21143 if (!CONSP (elt))
21144 goto invalid;
21145 /* elt is now the cdr, and we know it is a cons cell.
21146 Use its car if CAR has a non-nil value. */
21147 if (!NILP (tem))
21149 tem = Fsymbol_value (car);
21150 if (!NILP (tem))
21152 elt = XCAR (elt);
21153 goto tail_recurse;
21156 /* Symbol's value is nil (or symbol is unbound)
21157 Get the cddr of the original list
21158 and if possible find the caddr and use that. */
21159 elt = XCDR (elt);
21160 if (NILP (elt))
21161 break;
21162 else if (!CONSP (elt))
21163 goto invalid;
21164 elt = XCAR (elt);
21165 goto tail_recurse;
21167 else if (INTEGERP (car))
21169 register int lim = XINT (car);
21170 elt = XCDR (elt);
21171 if (lim < 0)
21173 /* Negative int means reduce maximum width. */
21174 if (precision <= 0)
21175 precision = -lim;
21176 else
21177 precision = min (precision, -lim);
21179 else if (lim > 0)
21181 /* Padding specified. Don't let it be more than
21182 current maximum. */
21183 if (precision > 0)
21184 lim = min (precision, lim);
21186 /* If that's more padding than already wanted, queue it.
21187 But don't reduce padding already specified even if
21188 that is beyond the current truncation point. */
21189 field_width = max (lim, field_width);
21191 goto tail_recurse;
21193 else if (STRINGP (car) || CONSP (car))
21195 Lisp_Object halftail = elt;
21196 int len = 0;
21198 while (CONSP (elt)
21199 && (precision <= 0 || n < precision))
21201 n += display_mode_element (it, depth,
21202 /* Do padding only after the last
21203 element in the list. */
21204 (! CONSP (XCDR (elt))
21205 ? field_width - n
21206 : 0),
21207 precision - n, XCAR (elt),
21208 props, risky);
21209 elt = XCDR (elt);
21210 len++;
21211 if ((len & 1) == 0)
21212 halftail = XCDR (halftail);
21213 /* Check for cycle. */
21214 if (EQ (halftail, elt))
21215 break;
21219 break;
21221 default:
21222 invalid:
21223 elt = build_string ("*invalid*");
21224 goto tail_recurse;
21227 /* Pad to FIELD_WIDTH. */
21228 if (field_width > 0 && n < field_width)
21230 switch (mode_line_target)
21232 case MODE_LINE_NOPROP:
21233 case MODE_LINE_TITLE:
21234 n += store_mode_line_noprop ("", field_width - n, 0);
21235 break;
21236 case MODE_LINE_STRING:
21237 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21238 break;
21239 case MODE_LINE_DISPLAY:
21240 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21241 0, 0, 0);
21242 break;
21246 return n;
21249 /* Store a mode-line string element in mode_line_string_list.
21251 If STRING is non-null, display that C string. Otherwise, the Lisp
21252 string LISP_STRING is displayed.
21254 FIELD_WIDTH is the minimum number of output glyphs to produce.
21255 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21256 with spaces. FIELD_WIDTH <= 0 means don't pad.
21258 PRECISION is the maximum number of characters to output from
21259 STRING. PRECISION <= 0 means don't truncate the string.
21261 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21262 properties to the string.
21264 PROPS are the properties to add to the string.
21265 The mode_line_string_face face property is always added to the string.
21268 static int
21269 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21270 int field_width, int precision, Lisp_Object props)
21272 ptrdiff_t len;
21273 int n = 0;
21275 if (string != NULL)
21277 len = strlen (string);
21278 if (precision > 0 && len > precision)
21279 len = precision;
21280 lisp_string = make_string (string, len);
21281 if (NILP (props))
21282 props = mode_line_string_face_prop;
21283 else if (!NILP (mode_line_string_face))
21285 Lisp_Object face = Fplist_get (props, Qface);
21286 props = Fcopy_sequence (props);
21287 if (NILP (face))
21288 face = mode_line_string_face;
21289 else
21290 face = list2 (face, mode_line_string_face);
21291 props = Fplist_put (props, Qface, face);
21293 Fadd_text_properties (make_number (0), make_number (len),
21294 props, lisp_string);
21296 else
21298 len = XFASTINT (Flength (lisp_string));
21299 if (precision > 0 && len > precision)
21301 len = precision;
21302 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21303 precision = -1;
21305 if (!NILP (mode_line_string_face))
21307 Lisp_Object face;
21308 if (NILP (props))
21309 props = Ftext_properties_at (make_number (0), lisp_string);
21310 face = Fplist_get (props, Qface);
21311 if (NILP (face))
21312 face = mode_line_string_face;
21313 else
21314 face = list2 (face, mode_line_string_face);
21315 props = list2 (Qface, face);
21316 if (copy_string)
21317 lisp_string = Fcopy_sequence (lisp_string);
21319 if (!NILP (props))
21320 Fadd_text_properties (make_number (0), make_number (len),
21321 props, lisp_string);
21324 if (len > 0)
21326 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21327 n += len;
21330 if (field_width > len)
21332 field_width -= len;
21333 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21334 if (!NILP (props))
21335 Fadd_text_properties (make_number (0), make_number (field_width),
21336 props, lisp_string);
21337 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21338 n += field_width;
21341 return n;
21345 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21346 1, 4, 0,
21347 doc: /* Format a string out of a mode line format specification.
21348 First arg FORMAT specifies the mode line format (see `mode-line-format'
21349 for details) to use.
21351 By default, the format is evaluated for the currently selected window.
21353 Optional second arg FACE specifies the face property to put on all
21354 characters for which no face is specified. The value nil means the
21355 default face. The value t means whatever face the window's mode line
21356 currently uses (either `mode-line' or `mode-line-inactive',
21357 depending on whether the window is the selected window or not).
21358 An integer value means the value string has no text
21359 properties.
21361 Optional third and fourth args WINDOW and BUFFER specify the window
21362 and buffer to use as the context for the formatting (defaults
21363 are the selected window and the WINDOW's buffer). */)
21364 (Lisp_Object format, Lisp_Object face,
21365 Lisp_Object window, Lisp_Object buffer)
21367 struct it it;
21368 int len;
21369 struct window *w;
21370 struct buffer *old_buffer = NULL;
21371 int face_id;
21372 int no_props = INTEGERP (face);
21373 ptrdiff_t count = SPECPDL_INDEX ();
21374 Lisp_Object str;
21375 int string_start = 0;
21377 w = decode_any_window (window);
21378 XSETWINDOW (window, w);
21380 if (NILP (buffer))
21381 buffer = w->contents;
21382 CHECK_BUFFER (buffer);
21384 /* Make formatting the modeline a non-op when noninteractive, otherwise
21385 there will be problems later caused by a partially initialized frame. */
21386 if (NILP (format) || noninteractive)
21387 return empty_unibyte_string;
21389 if (no_props)
21390 face = Qnil;
21392 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21393 : EQ (face, Qt) ? (EQ (window, selected_window)
21394 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21395 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21396 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21397 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21398 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21399 : DEFAULT_FACE_ID;
21401 old_buffer = current_buffer;
21403 /* Save things including mode_line_proptrans_alist,
21404 and set that to nil so that we don't alter the outer value. */
21405 record_unwind_protect (unwind_format_mode_line,
21406 format_mode_line_unwind_data
21407 (XFRAME (WINDOW_FRAME (w)),
21408 old_buffer, selected_window, 1));
21409 mode_line_proptrans_alist = Qnil;
21411 Fselect_window (window, Qt);
21412 set_buffer_internal_1 (XBUFFER (buffer));
21414 init_iterator (&it, w, -1, -1, NULL, face_id);
21416 if (no_props)
21418 mode_line_target = MODE_LINE_NOPROP;
21419 mode_line_string_face_prop = Qnil;
21420 mode_line_string_list = Qnil;
21421 string_start = MODE_LINE_NOPROP_LEN (0);
21423 else
21425 mode_line_target = MODE_LINE_STRING;
21426 mode_line_string_list = Qnil;
21427 mode_line_string_face = face;
21428 mode_line_string_face_prop
21429 = NILP (face) ? Qnil : list2 (Qface, face);
21432 push_kboard (FRAME_KBOARD (it.f));
21433 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21434 pop_kboard ();
21436 if (no_props)
21438 len = MODE_LINE_NOPROP_LEN (string_start);
21439 str = make_string (mode_line_noprop_buf + string_start, len);
21441 else
21443 mode_line_string_list = Fnreverse (mode_line_string_list);
21444 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21445 empty_unibyte_string);
21448 unbind_to (count, Qnil);
21449 return str;
21452 /* Write a null-terminated, right justified decimal representation of
21453 the positive integer D to BUF using a minimal field width WIDTH. */
21455 static void
21456 pint2str (register char *buf, register int width, register ptrdiff_t d)
21458 register char *p = buf;
21460 if (d <= 0)
21461 *p++ = '0';
21462 else
21464 while (d > 0)
21466 *p++ = d % 10 + '0';
21467 d /= 10;
21471 for (width -= (int) (p - buf); width > 0; --width)
21472 *p++ = ' ';
21473 *p-- = '\0';
21474 while (p > buf)
21476 d = *buf;
21477 *buf++ = *p;
21478 *p-- = d;
21482 /* Write a null-terminated, right justified decimal and "human
21483 readable" representation of the nonnegative integer D to BUF using
21484 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21486 static const char power_letter[] =
21488 0, /* no letter */
21489 'k', /* kilo */
21490 'M', /* mega */
21491 'G', /* giga */
21492 'T', /* tera */
21493 'P', /* peta */
21494 'E', /* exa */
21495 'Z', /* zetta */
21496 'Y' /* yotta */
21499 static void
21500 pint2hrstr (char *buf, int width, ptrdiff_t d)
21502 /* We aim to represent the nonnegative integer D as
21503 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21504 ptrdiff_t quotient = d;
21505 int remainder = 0;
21506 /* -1 means: do not use TENTHS. */
21507 int tenths = -1;
21508 int exponent = 0;
21510 /* Length of QUOTIENT.TENTHS as a string. */
21511 int length;
21513 char * psuffix;
21514 char * p;
21516 if (quotient >= 1000)
21518 /* Scale to the appropriate EXPONENT. */
21521 remainder = quotient % 1000;
21522 quotient /= 1000;
21523 exponent++;
21525 while (quotient >= 1000);
21527 /* Round to nearest and decide whether to use TENTHS or not. */
21528 if (quotient <= 9)
21530 tenths = remainder / 100;
21531 if (remainder % 100 >= 50)
21533 if (tenths < 9)
21534 tenths++;
21535 else
21537 quotient++;
21538 if (quotient == 10)
21539 tenths = -1;
21540 else
21541 tenths = 0;
21545 else
21546 if (remainder >= 500)
21548 if (quotient < 999)
21549 quotient++;
21550 else
21552 quotient = 1;
21553 exponent++;
21554 tenths = 0;
21559 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21560 if (tenths == -1 && quotient <= 99)
21561 if (quotient <= 9)
21562 length = 1;
21563 else
21564 length = 2;
21565 else
21566 length = 3;
21567 p = psuffix = buf + max (width, length);
21569 /* Print EXPONENT. */
21570 *psuffix++ = power_letter[exponent];
21571 *psuffix = '\0';
21573 /* Print TENTHS. */
21574 if (tenths >= 0)
21576 *--p = '0' + tenths;
21577 *--p = '.';
21580 /* Print QUOTIENT. */
21583 int digit = quotient % 10;
21584 *--p = '0' + digit;
21586 while ((quotient /= 10) != 0);
21588 /* Print leading spaces. */
21589 while (buf < p)
21590 *--p = ' ';
21593 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21594 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21595 type of CODING_SYSTEM. Return updated pointer into BUF. */
21597 static unsigned char invalid_eol_type[] = "(*invalid*)";
21599 static char *
21600 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21602 Lisp_Object val;
21603 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21604 const unsigned char *eol_str;
21605 int eol_str_len;
21606 /* The EOL conversion we are using. */
21607 Lisp_Object eoltype;
21609 val = CODING_SYSTEM_SPEC (coding_system);
21610 eoltype = Qnil;
21612 if (!VECTORP (val)) /* Not yet decided. */
21614 *buf++ = multibyte ? '-' : ' ';
21615 if (eol_flag)
21616 eoltype = eol_mnemonic_undecided;
21617 /* Don't mention EOL conversion if it isn't decided. */
21619 else
21621 Lisp_Object attrs;
21622 Lisp_Object eolvalue;
21624 attrs = AREF (val, 0);
21625 eolvalue = AREF (val, 2);
21627 *buf++ = multibyte
21628 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21629 : ' ';
21631 if (eol_flag)
21633 /* The EOL conversion that is normal on this system. */
21635 if (NILP (eolvalue)) /* Not yet decided. */
21636 eoltype = eol_mnemonic_undecided;
21637 else if (VECTORP (eolvalue)) /* Not yet decided. */
21638 eoltype = eol_mnemonic_undecided;
21639 else /* eolvalue is Qunix, Qdos, or Qmac. */
21640 eoltype = (EQ (eolvalue, Qunix)
21641 ? eol_mnemonic_unix
21642 : (EQ (eolvalue, Qdos) == 1
21643 ? eol_mnemonic_dos : eol_mnemonic_mac));
21647 if (eol_flag)
21649 /* Mention the EOL conversion if it is not the usual one. */
21650 if (STRINGP (eoltype))
21652 eol_str = SDATA (eoltype);
21653 eol_str_len = SBYTES (eoltype);
21655 else if (CHARACTERP (eoltype))
21657 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21658 int c = XFASTINT (eoltype);
21659 eol_str_len = CHAR_STRING (c, tmp);
21660 eol_str = tmp;
21662 else
21664 eol_str = invalid_eol_type;
21665 eol_str_len = sizeof (invalid_eol_type) - 1;
21667 memcpy (buf, eol_str, eol_str_len);
21668 buf += eol_str_len;
21671 return buf;
21674 /* Return a string for the output of a mode line %-spec for window W,
21675 generated by character C. FIELD_WIDTH > 0 means pad the string
21676 returned with spaces to that value. Return a Lisp string in
21677 *STRING if the resulting string is taken from that Lisp string.
21679 Note we operate on the current buffer for most purposes. */
21681 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21683 static const char *
21684 decode_mode_spec (struct window *w, register int c, int field_width,
21685 Lisp_Object *string)
21687 Lisp_Object obj;
21688 struct frame *f = XFRAME (WINDOW_FRAME (w));
21689 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21690 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21691 produce strings from numerical values, so limit preposterously
21692 large values of FIELD_WIDTH to avoid overrunning the buffer's
21693 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21694 bytes plus the terminating null. */
21695 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21696 struct buffer *b = current_buffer;
21698 obj = Qnil;
21699 *string = Qnil;
21701 switch (c)
21703 case '*':
21704 if (!NILP (BVAR (b, read_only)))
21705 return "%";
21706 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21707 return "*";
21708 return "-";
21710 case '+':
21711 /* This differs from %* only for a modified read-only buffer. */
21712 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21713 return "*";
21714 if (!NILP (BVAR (b, read_only)))
21715 return "%";
21716 return "-";
21718 case '&':
21719 /* This differs from %* in ignoring read-only-ness. */
21720 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21721 return "*";
21722 return "-";
21724 case '%':
21725 return "%";
21727 case '[':
21729 int i;
21730 char *p;
21732 if (command_loop_level > 5)
21733 return "[[[... ";
21734 p = decode_mode_spec_buf;
21735 for (i = 0; i < command_loop_level; i++)
21736 *p++ = '[';
21737 *p = 0;
21738 return decode_mode_spec_buf;
21741 case ']':
21743 int i;
21744 char *p;
21746 if (command_loop_level > 5)
21747 return " ...]]]";
21748 p = decode_mode_spec_buf;
21749 for (i = 0; i < command_loop_level; i++)
21750 *p++ = ']';
21751 *p = 0;
21752 return decode_mode_spec_buf;
21755 case '-':
21757 register int i;
21759 /* Let lots_of_dashes be a string of infinite length. */
21760 if (mode_line_target == MODE_LINE_NOPROP
21761 || mode_line_target == MODE_LINE_STRING)
21762 return "--";
21763 if (field_width <= 0
21764 || field_width > sizeof (lots_of_dashes))
21766 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21767 decode_mode_spec_buf[i] = '-';
21768 decode_mode_spec_buf[i] = '\0';
21769 return decode_mode_spec_buf;
21771 else
21772 return lots_of_dashes;
21775 case 'b':
21776 obj = BVAR (b, name);
21777 break;
21779 case 'c':
21780 /* %c and %l are ignored in `frame-title-format'.
21781 (In redisplay_internal, the frame title is drawn _before_ the
21782 windows are updated, so the stuff which depends on actual
21783 window contents (such as %l) may fail to render properly, or
21784 even crash emacs.) */
21785 if (mode_line_target == MODE_LINE_TITLE)
21786 return "";
21787 else
21789 ptrdiff_t col = current_column ();
21790 w->column_number_displayed = col;
21791 pint2str (decode_mode_spec_buf, width, col);
21792 return decode_mode_spec_buf;
21795 case 'e':
21796 #ifndef SYSTEM_MALLOC
21798 if (NILP (Vmemory_full))
21799 return "";
21800 else
21801 return "!MEM FULL! ";
21803 #else
21804 return "";
21805 #endif
21807 case 'F':
21808 /* %F displays the frame name. */
21809 if (!NILP (f->title))
21810 return SSDATA (f->title);
21811 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21812 return SSDATA (f->name);
21813 return "Emacs";
21815 case 'f':
21816 obj = BVAR (b, filename);
21817 break;
21819 case 'i':
21821 ptrdiff_t size = ZV - BEGV;
21822 pint2str (decode_mode_spec_buf, width, size);
21823 return decode_mode_spec_buf;
21826 case 'I':
21828 ptrdiff_t size = ZV - BEGV;
21829 pint2hrstr (decode_mode_spec_buf, width, size);
21830 return decode_mode_spec_buf;
21833 case 'l':
21835 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21836 ptrdiff_t topline, nlines, height;
21837 ptrdiff_t junk;
21839 /* %c and %l are ignored in `frame-title-format'. */
21840 if (mode_line_target == MODE_LINE_TITLE)
21841 return "";
21843 startpos = marker_position (w->start);
21844 startpos_byte = marker_byte_position (w->start);
21845 height = WINDOW_TOTAL_LINES (w);
21847 /* If we decided that this buffer isn't suitable for line numbers,
21848 don't forget that too fast. */
21849 if (w->base_line_pos == -1)
21850 goto no_value;
21852 /* If the buffer is very big, don't waste time. */
21853 if (INTEGERP (Vline_number_display_limit)
21854 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21856 w->base_line_pos = 0;
21857 w->base_line_number = 0;
21858 goto no_value;
21861 if (w->base_line_number > 0
21862 && w->base_line_pos > 0
21863 && w->base_line_pos <= startpos)
21865 line = w->base_line_number;
21866 linepos = w->base_line_pos;
21867 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21869 else
21871 line = 1;
21872 linepos = BUF_BEGV (b);
21873 linepos_byte = BUF_BEGV_BYTE (b);
21876 /* Count lines from base line to window start position. */
21877 nlines = display_count_lines (linepos_byte,
21878 startpos_byte,
21879 startpos, &junk);
21881 topline = nlines + line;
21883 /* Determine a new base line, if the old one is too close
21884 or too far away, or if we did not have one.
21885 "Too close" means it's plausible a scroll-down would
21886 go back past it. */
21887 if (startpos == BUF_BEGV (b))
21889 w->base_line_number = topline;
21890 w->base_line_pos = BUF_BEGV (b);
21892 else if (nlines < height + 25 || nlines > height * 3 + 50
21893 || linepos == BUF_BEGV (b))
21895 ptrdiff_t limit = BUF_BEGV (b);
21896 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21897 ptrdiff_t position;
21898 ptrdiff_t distance =
21899 (height * 2 + 30) * line_number_display_limit_width;
21901 if (startpos - distance > limit)
21903 limit = startpos - distance;
21904 limit_byte = CHAR_TO_BYTE (limit);
21907 nlines = display_count_lines (startpos_byte,
21908 limit_byte,
21909 - (height * 2 + 30),
21910 &position);
21911 /* If we couldn't find the lines we wanted within
21912 line_number_display_limit_width chars per line,
21913 give up on line numbers for this window. */
21914 if (position == limit_byte && limit == startpos - distance)
21916 w->base_line_pos = -1;
21917 w->base_line_number = 0;
21918 goto no_value;
21921 w->base_line_number = topline - nlines;
21922 w->base_line_pos = BYTE_TO_CHAR (position);
21925 /* Now count lines from the start pos to point. */
21926 nlines = display_count_lines (startpos_byte,
21927 PT_BYTE, PT, &junk);
21929 /* Record that we did display the line number. */
21930 line_number_displayed = 1;
21932 /* Make the string to show. */
21933 pint2str (decode_mode_spec_buf, width, topline + nlines);
21934 return decode_mode_spec_buf;
21935 no_value:
21937 char* p = decode_mode_spec_buf;
21938 int pad = width - 2;
21939 while (pad-- > 0)
21940 *p++ = ' ';
21941 *p++ = '?';
21942 *p++ = '?';
21943 *p = '\0';
21944 return decode_mode_spec_buf;
21947 break;
21949 case 'm':
21950 obj = BVAR (b, mode_name);
21951 break;
21953 case 'n':
21954 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21955 return " Narrow";
21956 break;
21958 case 'p':
21960 ptrdiff_t pos = marker_position (w->start);
21961 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21963 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
21965 if (pos <= BUF_BEGV (b))
21966 return "All";
21967 else
21968 return "Bottom";
21970 else if (pos <= BUF_BEGV (b))
21971 return "Top";
21972 else
21974 if (total > 1000000)
21975 /* Do it differently for a large value, to avoid overflow. */
21976 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21977 else
21978 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21979 /* We can't normally display a 3-digit number,
21980 so get us a 2-digit number that is close. */
21981 if (total == 100)
21982 total = 99;
21983 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21984 return decode_mode_spec_buf;
21988 /* Display percentage of size above the bottom of the screen. */
21989 case 'P':
21991 ptrdiff_t toppos = marker_position (w->start);
21992 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
21993 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21995 if (botpos >= BUF_ZV (b))
21997 if (toppos <= BUF_BEGV (b))
21998 return "All";
21999 else
22000 return "Bottom";
22002 else
22004 if (total > 1000000)
22005 /* Do it differently for a large value, to avoid overflow. */
22006 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22007 else
22008 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22009 /* We can't normally display a 3-digit number,
22010 so get us a 2-digit number that is close. */
22011 if (total == 100)
22012 total = 99;
22013 if (toppos <= BUF_BEGV (b))
22014 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22015 else
22016 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22017 return decode_mode_spec_buf;
22021 case 's':
22022 /* status of process */
22023 obj = Fget_buffer_process (Fcurrent_buffer ());
22024 if (NILP (obj))
22025 return "no process";
22026 #ifndef MSDOS
22027 obj = Fsymbol_name (Fprocess_status (obj));
22028 #endif
22029 break;
22031 case '@':
22033 ptrdiff_t count = inhibit_garbage_collection ();
22034 Lisp_Object val = call1 (intern ("file-remote-p"),
22035 BVAR (current_buffer, directory));
22036 unbind_to (count, Qnil);
22038 if (NILP (val))
22039 return "-";
22040 else
22041 return "@";
22044 case 'z':
22045 /* coding-system (not including end-of-line format) */
22046 case 'Z':
22047 /* coding-system (including end-of-line type) */
22049 int eol_flag = (c == 'Z');
22050 char *p = decode_mode_spec_buf;
22052 if (! FRAME_WINDOW_P (f))
22054 /* No need to mention EOL here--the terminal never needs
22055 to do EOL conversion. */
22056 p = decode_mode_spec_coding (CODING_ID_NAME
22057 (FRAME_KEYBOARD_CODING (f)->id),
22058 p, 0);
22059 p = decode_mode_spec_coding (CODING_ID_NAME
22060 (FRAME_TERMINAL_CODING (f)->id),
22061 p, 0);
22063 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22064 p, eol_flag);
22066 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22067 #ifdef subprocesses
22068 obj = Fget_buffer_process (Fcurrent_buffer ());
22069 if (PROCESSP (obj))
22071 p = decode_mode_spec_coding
22072 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22073 p = decode_mode_spec_coding
22074 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22076 #endif /* subprocesses */
22077 #endif /* 0 */
22078 *p = 0;
22079 return decode_mode_spec_buf;
22083 if (STRINGP (obj))
22085 *string = obj;
22086 return SSDATA (obj);
22088 else
22089 return "";
22093 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22094 means count lines back from START_BYTE. But don't go beyond
22095 LIMIT_BYTE. Return the number of lines thus found (always
22096 nonnegative).
22098 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22099 either the position COUNT lines after/before START_BYTE, if we
22100 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22101 COUNT lines. */
22103 static ptrdiff_t
22104 display_count_lines (ptrdiff_t start_byte,
22105 ptrdiff_t limit_byte, ptrdiff_t count,
22106 ptrdiff_t *byte_pos_ptr)
22108 register unsigned char *cursor;
22109 unsigned char *base;
22111 register ptrdiff_t ceiling;
22112 register unsigned char *ceiling_addr;
22113 ptrdiff_t orig_count = count;
22115 /* If we are not in selective display mode,
22116 check only for newlines. */
22117 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22118 && !INTEGERP (BVAR (current_buffer, selective_display)));
22120 if (count > 0)
22122 while (start_byte < limit_byte)
22124 ceiling = BUFFER_CEILING_OF (start_byte);
22125 ceiling = min (limit_byte - 1, ceiling);
22126 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22127 base = (cursor = BYTE_POS_ADDR (start_byte));
22131 if (selective_display)
22133 while (*cursor != '\n' && *cursor != 015
22134 && ++cursor != ceiling_addr)
22135 continue;
22136 if (cursor == ceiling_addr)
22137 break;
22139 else
22141 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22142 if (! cursor)
22143 break;
22146 cursor++;
22148 if (--count == 0)
22150 start_byte += cursor - base;
22151 *byte_pos_ptr = start_byte;
22152 return orig_count;
22155 while (cursor < ceiling_addr);
22157 start_byte += ceiling_addr - base;
22160 else
22162 while (start_byte > limit_byte)
22164 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22165 ceiling = max (limit_byte, ceiling);
22166 ceiling_addr = BYTE_POS_ADDR (ceiling);
22167 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22168 while (1)
22170 if (selective_display)
22172 while (--cursor >= ceiling_addr
22173 && *cursor != '\n' && *cursor != 015)
22174 continue;
22175 if (cursor < ceiling_addr)
22176 break;
22178 else
22180 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22181 if (! cursor)
22182 break;
22185 if (++count == 0)
22187 start_byte += cursor - base + 1;
22188 *byte_pos_ptr = start_byte;
22189 /* When scanning backwards, we should
22190 not count the newline posterior to which we stop. */
22191 return - orig_count - 1;
22194 start_byte += ceiling_addr - base;
22198 *byte_pos_ptr = limit_byte;
22200 if (count < 0)
22201 return - orig_count + count;
22202 return orig_count - count;
22208 /***********************************************************************
22209 Displaying strings
22210 ***********************************************************************/
22212 /* Display a NUL-terminated string, starting with index START.
22214 If STRING is non-null, display that C string. Otherwise, the Lisp
22215 string LISP_STRING is displayed. There's a case that STRING is
22216 non-null and LISP_STRING is not nil. It means STRING is a string
22217 data of LISP_STRING. In that case, we display LISP_STRING while
22218 ignoring its text properties.
22220 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22221 FACE_STRING. Display STRING or LISP_STRING with the face at
22222 FACE_STRING_POS in FACE_STRING:
22224 Display the string in the environment given by IT, but use the
22225 standard display table, temporarily.
22227 FIELD_WIDTH is the minimum number of output glyphs to produce.
22228 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22229 with spaces. If STRING has more characters, more than FIELD_WIDTH
22230 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22232 PRECISION is the maximum number of characters to output from
22233 STRING. PRECISION < 0 means don't truncate the string.
22235 This is roughly equivalent to printf format specifiers:
22237 FIELD_WIDTH PRECISION PRINTF
22238 ----------------------------------------
22239 -1 -1 %s
22240 -1 10 %.10s
22241 10 -1 %10s
22242 20 10 %20.10s
22244 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22245 display them, and < 0 means obey the current buffer's value of
22246 enable_multibyte_characters.
22248 Value is the number of columns displayed. */
22250 static int
22251 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22252 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22253 int field_width, int precision, int max_x, int multibyte)
22255 int hpos_at_start = it->hpos;
22256 int saved_face_id = it->face_id;
22257 struct glyph_row *row = it->glyph_row;
22258 ptrdiff_t it_charpos;
22260 /* Initialize the iterator IT for iteration over STRING beginning
22261 with index START. */
22262 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22263 precision, field_width, multibyte);
22264 if (string && STRINGP (lisp_string))
22265 /* LISP_STRING is the one returned by decode_mode_spec. We should
22266 ignore its text properties. */
22267 it->stop_charpos = it->end_charpos;
22269 /* If displaying STRING, set up the face of the iterator from
22270 FACE_STRING, if that's given. */
22271 if (STRINGP (face_string))
22273 ptrdiff_t endptr;
22274 struct face *face;
22276 it->face_id
22277 = face_at_string_position (it->w, face_string, face_string_pos,
22278 0, it->region_beg_charpos,
22279 it->region_end_charpos,
22280 &endptr, it->base_face_id, 0);
22281 face = FACE_FROM_ID (it->f, it->face_id);
22282 it->face_box_p = face->box != FACE_NO_BOX;
22285 /* Set max_x to the maximum allowed X position. Don't let it go
22286 beyond the right edge of the window. */
22287 if (max_x <= 0)
22288 max_x = it->last_visible_x;
22289 else
22290 max_x = min (max_x, it->last_visible_x);
22292 /* Skip over display elements that are not visible. because IT->w is
22293 hscrolled. */
22294 if (it->current_x < it->first_visible_x)
22295 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22296 MOVE_TO_POS | MOVE_TO_X);
22298 row->ascent = it->max_ascent;
22299 row->height = it->max_ascent + it->max_descent;
22300 row->phys_ascent = it->max_phys_ascent;
22301 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22302 row->extra_line_spacing = it->max_extra_line_spacing;
22304 if (STRINGP (it->string))
22305 it_charpos = IT_STRING_CHARPOS (*it);
22306 else
22307 it_charpos = IT_CHARPOS (*it);
22309 /* This condition is for the case that we are called with current_x
22310 past last_visible_x. */
22311 while (it->current_x < max_x)
22313 int x_before, x, n_glyphs_before, i, nglyphs;
22315 /* Get the next display element. */
22316 if (!get_next_display_element (it))
22317 break;
22319 /* Produce glyphs. */
22320 x_before = it->current_x;
22321 n_glyphs_before = row->used[TEXT_AREA];
22322 PRODUCE_GLYPHS (it);
22324 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22325 i = 0;
22326 x = x_before;
22327 while (i < nglyphs)
22329 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22331 if (it->line_wrap != TRUNCATE
22332 && x + glyph->pixel_width > max_x)
22334 /* End of continued line or max_x reached. */
22335 if (CHAR_GLYPH_PADDING_P (*glyph))
22337 /* A wide character is unbreakable. */
22338 if (row->reversed_p)
22339 unproduce_glyphs (it, row->used[TEXT_AREA]
22340 - n_glyphs_before);
22341 row->used[TEXT_AREA] = n_glyphs_before;
22342 it->current_x = x_before;
22344 else
22346 if (row->reversed_p)
22347 unproduce_glyphs (it, row->used[TEXT_AREA]
22348 - (n_glyphs_before + i));
22349 row->used[TEXT_AREA] = n_glyphs_before + i;
22350 it->current_x = x;
22352 break;
22354 else if (x + glyph->pixel_width >= it->first_visible_x)
22356 /* Glyph is at least partially visible. */
22357 ++it->hpos;
22358 if (x < it->first_visible_x)
22359 row->x = x - it->first_visible_x;
22361 else
22363 /* Glyph is off the left margin of the display area.
22364 Should not happen. */
22365 emacs_abort ();
22368 row->ascent = max (row->ascent, it->max_ascent);
22369 row->height = max (row->height, it->max_ascent + it->max_descent);
22370 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22371 row->phys_height = max (row->phys_height,
22372 it->max_phys_ascent + it->max_phys_descent);
22373 row->extra_line_spacing = max (row->extra_line_spacing,
22374 it->max_extra_line_spacing);
22375 x += glyph->pixel_width;
22376 ++i;
22379 /* Stop if max_x reached. */
22380 if (i < nglyphs)
22381 break;
22383 /* Stop at line ends. */
22384 if (ITERATOR_AT_END_OF_LINE_P (it))
22386 it->continuation_lines_width = 0;
22387 break;
22390 set_iterator_to_next (it, 1);
22391 if (STRINGP (it->string))
22392 it_charpos = IT_STRING_CHARPOS (*it);
22393 else
22394 it_charpos = IT_CHARPOS (*it);
22396 /* Stop if truncating at the right edge. */
22397 if (it->line_wrap == TRUNCATE
22398 && it->current_x >= it->last_visible_x)
22400 /* Add truncation mark, but don't do it if the line is
22401 truncated at a padding space. */
22402 if (it_charpos < it->string_nchars)
22404 if (!FRAME_WINDOW_P (it->f))
22406 int ii, n;
22408 if (it->current_x > it->last_visible_x)
22410 if (!row->reversed_p)
22412 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22413 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22414 break;
22416 else
22418 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22419 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22420 break;
22421 unproduce_glyphs (it, ii + 1);
22422 ii = row->used[TEXT_AREA] - (ii + 1);
22424 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22426 row->used[TEXT_AREA] = ii;
22427 produce_special_glyphs (it, IT_TRUNCATION);
22430 produce_special_glyphs (it, IT_TRUNCATION);
22432 row->truncated_on_right_p = 1;
22434 break;
22438 /* Maybe insert a truncation at the left. */
22439 if (it->first_visible_x
22440 && it_charpos > 0)
22442 if (!FRAME_WINDOW_P (it->f)
22443 || (row->reversed_p
22444 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22445 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22446 insert_left_trunc_glyphs (it);
22447 row->truncated_on_left_p = 1;
22450 it->face_id = saved_face_id;
22452 /* Value is number of columns displayed. */
22453 return it->hpos - hpos_at_start;
22458 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22459 appears as an element of LIST or as the car of an element of LIST.
22460 If PROPVAL is a list, compare each element against LIST in that
22461 way, and return 1/2 if any element of PROPVAL is found in LIST.
22462 Otherwise return 0. This function cannot quit.
22463 The return value is 2 if the text is invisible but with an ellipsis
22464 and 1 if it's invisible and without an ellipsis. */
22467 invisible_p (register Lisp_Object propval, Lisp_Object list)
22469 register Lisp_Object tail, proptail;
22471 for (tail = list; CONSP (tail); tail = XCDR (tail))
22473 register Lisp_Object tem;
22474 tem = XCAR (tail);
22475 if (EQ (propval, tem))
22476 return 1;
22477 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22478 return NILP (XCDR (tem)) ? 1 : 2;
22481 if (CONSP (propval))
22483 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22485 Lisp_Object propelt;
22486 propelt = XCAR (proptail);
22487 for (tail = list; CONSP (tail); tail = XCDR (tail))
22489 register Lisp_Object tem;
22490 tem = XCAR (tail);
22491 if (EQ (propelt, tem))
22492 return 1;
22493 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22494 return NILP (XCDR (tem)) ? 1 : 2;
22499 return 0;
22502 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22503 doc: /* Non-nil if the property makes the text invisible.
22504 POS-OR-PROP can be a marker or number, in which case it is taken to be
22505 a position in the current buffer and the value of the `invisible' property
22506 is checked; or it can be some other value, which is then presumed to be the
22507 value of the `invisible' property of the text of interest.
22508 The non-nil value returned can be t for truly invisible text or something
22509 else if the text is replaced by an ellipsis. */)
22510 (Lisp_Object pos_or_prop)
22512 Lisp_Object prop
22513 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22514 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22515 : pos_or_prop);
22516 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22517 return (invis == 0 ? Qnil
22518 : invis == 1 ? Qt
22519 : make_number (invis));
22522 /* Calculate a width or height in pixels from a specification using
22523 the following elements:
22525 SPEC ::=
22526 NUM - a (fractional) multiple of the default font width/height
22527 (NUM) - specifies exactly NUM pixels
22528 UNIT - a fixed number of pixels, see below.
22529 ELEMENT - size of a display element in pixels, see below.
22530 (NUM . SPEC) - equals NUM * SPEC
22531 (+ SPEC SPEC ...) - add pixel values
22532 (- SPEC SPEC ...) - subtract pixel values
22533 (- SPEC) - negate pixel value
22535 NUM ::=
22536 INT or FLOAT - a number constant
22537 SYMBOL - use symbol's (buffer local) variable binding.
22539 UNIT ::=
22540 in - pixels per inch *)
22541 mm - pixels per 1/1000 meter *)
22542 cm - pixels per 1/100 meter *)
22543 width - width of current font in pixels.
22544 height - height of current font in pixels.
22546 *) using the ratio(s) defined in display-pixels-per-inch.
22548 ELEMENT ::=
22550 left-fringe - left fringe width in pixels
22551 right-fringe - right fringe width in pixels
22553 left-margin - left margin width in pixels
22554 right-margin - right margin width in pixels
22556 scroll-bar - scroll-bar area width in pixels
22558 Examples:
22560 Pixels corresponding to 5 inches:
22561 (5 . in)
22563 Total width of non-text areas on left side of window (if scroll-bar is on left):
22564 '(space :width (+ left-fringe left-margin scroll-bar))
22566 Align to first text column (in header line):
22567 '(space :align-to 0)
22569 Align to middle of text area minus half the width of variable `my-image'
22570 containing a loaded image:
22571 '(space :align-to (0.5 . (- text my-image)))
22573 Width of left margin minus width of 1 character in the default font:
22574 '(space :width (- left-margin 1))
22576 Width of left margin minus width of 2 characters in the current font:
22577 '(space :width (- left-margin (2 . width)))
22579 Center 1 character over left-margin (in header line):
22580 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22582 Different ways to express width of left fringe plus left margin minus one pixel:
22583 '(space :width (- (+ left-fringe left-margin) (1)))
22584 '(space :width (+ left-fringe left-margin (- (1))))
22585 '(space :width (+ left-fringe left-margin (-1)))
22589 static int
22590 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22591 struct font *font, int width_p, int *align_to)
22593 double pixels;
22595 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22596 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22598 if (NILP (prop))
22599 return OK_PIXELS (0);
22601 eassert (FRAME_LIVE_P (it->f));
22603 if (SYMBOLP (prop))
22605 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22607 char *unit = SSDATA (SYMBOL_NAME (prop));
22609 if (unit[0] == 'i' && unit[1] == 'n')
22610 pixels = 1.0;
22611 else if (unit[0] == 'm' && unit[1] == 'm')
22612 pixels = 25.4;
22613 else if (unit[0] == 'c' && unit[1] == 'm')
22614 pixels = 2.54;
22615 else
22616 pixels = 0;
22617 if (pixels > 0)
22619 double ppi = (width_p ? FRAME_RES_X (it->f)
22620 : FRAME_RES_Y (it->f));
22622 if (ppi > 0)
22623 return OK_PIXELS (ppi / pixels);
22624 return 0;
22628 #ifdef HAVE_WINDOW_SYSTEM
22629 if (EQ (prop, Qheight))
22630 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22631 if (EQ (prop, Qwidth))
22632 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22633 #else
22634 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22635 return OK_PIXELS (1);
22636 #endif
22638 if (EQ (prop, Qtext))
22639 return OK_PIXELS (width_p
22640 ? window_box_width (it->w, TEXT_AREA)
22641 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22643 if (align_to && *align_to < 0)
22645 *res = 0;
22646 if (EQ (prop, Qleft))
22647 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22648 if (EQ (prop, Qright))
22649 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22650 if (EQ (prop, Qcenter))
22651 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22652 + window_box_width (it->w, TEXT_AREA) / 2);
22653 if (EQ (prop, Qleft_fringe))
22654 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22655 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22656 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22657 if (EQ (prop, Qright_fringe))
22658 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22659 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22660 : window_box_right_offset (it->w, TEXT_AREA));
22661 if (EQ (prop, Qleft_margin))
22662 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22663 if (EQ (prop, Qright_margin))
22664 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22665 if (EQ (prop, Qscroll_bar))
22666 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22668 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22669 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22670 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22671 : 0)));
22673 else
22675 if (EQ (prop, Qleft_fringe))
22676 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22677 if (EQ (prop, Qright_fringe))
22678 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22679 if (EQ (prop, Qleft_margin))
22680 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22681 if (EQ (prop, Qright_margin))
22682 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22683 if (EQ (prop, Qscroll_bar))
22684 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22687 prop = buffer_local_value_1 (prop, it->w->contents);
22688 if (EQ (prop, Qunbound))
22689 prop = Qnil;
22692 if (INTEGERP (prop) || FLOATP (prop))
22694 int base_unit = (width_p
22695 ? FRAME_COLUMN_WIDTH (it->f)
22696 : FRAME_LINE_HEIGHT (it->f));
22697 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22700 if (CONSP (prop))
22702 Lisp_Object car = XCAR (prop);
22703 Lisp_Object cdr = XCDR (prop);
22705 if (SYMBOLP (car))
22707 #ifdef HAVE_WINDOW_SYSTEM
22708 if (FRAME_WINDOW_P (it->f)
22709 && valid_image_p (prop))
22711 ptrdiff_t id = lookup_image (it->f, prop);
22712 struct image *img = IMAGE_FROM_ID (it->f, id);
22714 return OK_PIXELS (width_p ? img->width : img->height);
22716 #endif
22717 if (EQ (car, Qplus) || EQ (car, Qminus))
22719 int first = 1;
22720 double px;
22722 pixels = 0;
22723 while (CONSP (cdr))
22725 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22726 font, width_p, align_to))
22727 return 0;
22728 if (first)
22729 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22730 else
22731 pixels += px;
22732 cdr = XCDR (cdr);
22734 if (EQ (car, Qminus))
22735 pixels = -pixels;
22736 return OK_PIXELS (pixels);
22739 car = buffer_local_value_1 (car, it->w->contents);
22740 if (EQ (car, Qunbound))
22741 car = Qnil;
22744 if (INTEGERP (car) || FLOATP (car))
22746 double fact;
22747 pixels = XFLOATINT (car);
22748 if (NILP (cdr))
22749 return OK_PIXELS (pixels);
22750 if (calc_pixel_width_or_height (&fact, it, cdr,
22751 font, width_p, align_to))
22752 return OK_PIXELS (pixels * fact);
22753 return 0;
22756 return 0;
22759 return 0;
22763 /***********************************************************************
22764 Glyph Display
22765 ***********************************************************************/
22767 #ifdef HAVE_WINDOW_SYSTEM
22769 #ifdef GLYPH_DEBUG
22771 void
22772 dump_glyph_string (struct glyph_string *s)
22774 fprintf (stderr, "glyph string\n");
22775 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22776 s->x, s->y, s->width, s->height);
22777 fprintf (stderr, " ybase = %d\n", s->ybase);
22778 fprintf (stderr, " hl = %d\n", s->hl);
22779 fprintf (stderr, " left overhang = %d, right = %d\n",
22780 s->left_overhang, s->right_overhang);
22781 fprintf (stderr, " nchars = %d\n", s->nchars);
22782 fprintf (stderr, " extends to end of line = %d\n",
22783 s->extends_to_end_of_line_p);
22784 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22785 fprintf (stderr, " bg width = %d\n", s->background_width);
22788 #endif /* GLYPH_DEBUG */
22790 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22791 of XChar2b structures for S; it can't be allocated in
22792 init_glyph_string because it must be allocated via `alloca'. W
22793 is the window on which S is drawn. ROW and AREA are the glyph row
22794 and area within the row from which S is constructed. START is the
22795 index of the first glyph structure covered by S. HL is a
22796 face-override for drawing S. */
22798 #ifdef HAVE_NTGUI
22799 #define OPTIONAL_HDC(hdc) HDC hdc,
22800 #define DECLARE_HDC(hdc) HDC hdc;
22801 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22802 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22803 #endif
22805 #ifndef OPTIONAL_HDC
22806 #define OPTIONAL_HDC(hdc)
22807 #define DECLARE_HDC(hdc)
22808 #define ALLOCATE_HDC(hdc, f)
22809 #define RELEASE_HDC(hdc, f)
22810 #endif
22812 static void
22813 init_glyph_string (struct glyph_string *s,
22814 OPTIONAL_HDC (hdc)
22815 XChar2b *char2b, struct window *w, struct glyph_row *row,
22816 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22818 memset (s, 0, sizeof *s);
22819 s->w = w;
22820 s->f = XFRAME (w->frame);
22821 #ifdef HAVE_NTGUI
22822 s->hdc = hdc;
22823 #endif
22824 s->display = FRAME_X_DISPLAY (s->f);
22825 s->window = FRAME_X_WINDOW (s->f);
22826 s->char2b = char2b;
22827 s->hl = hl;
22828 s->row = row;
22829 s->area = area;
22830 s->first_glyph = row->glyphs[area] + start;
22831 s->height = row->height;
22832 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22833 s->ybase = s->y + row->ascent;
22837 /* Append the list of glyph strings with head H and tail T to the list
22838 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22840 static void
22841 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22842 struct glyph_string *h, struct glyph_string *t)
22844 if (h)
22846 if (*head)
22847 (*tail)->next = h;
22848 else
22849 *head = h;
22850 h->prev = *tail;
22851 *tail = t;
22856 /* Prepend the list of glyph strings with head H and tail T to the
22857 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22858 result. */
22860 static void
22861 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22862 struct glyph_string *h, struct glyph_string *t)
22864 if (h)
22866 if (*head)
22867 (*head)->prev = t;
22868 else
22869 *tail = t;
22870 t->next = *head;
22871 *head = h;
22876 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22877 Set *HEAD and *TAIL to the resulting list. */
22879 static void
22880 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22881 struct glyph_string *s)
22883 s->next = s->prev = NULL;
22884 append_glyph_string_lists (head, tail, s, s);
22888 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22889 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22890 make sure that X resources for the face returned are allocated.
22891 Value is a pointer to a realized face that is ready for display if
22892 DISPLAY_P is non-zero. */
22894 static struct face *
22895 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22896 XChar2b *char2b, int display_p)
22898 struct face *face = FACE_FROM_ID (f, face_id);
22899 unsigned code = 0;
22901 if (face->font)
22903 code = face->font->driver->encode_char (face->font, c);
22905 if (code == FONT_INVALID_CODE)
22906 code = 0;
22908 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22910 /* Make sure X resources of the face are allocated. */
22911 #ifdef HAVE_X_WINDOWS
22912 if (display_p)
22913 #endif
22915 eassert (face != NULL);
22916 PREPARE_FACE_FOR_DISPLAY (f, face);
22919 return face;
22923 /* Get face and two-byte form of character glyph GLYPH on frame F.
22924 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22925 a pointer to a realized face that is ready for display. */
22927 static struct face *
22928 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22929 XChar2b *char2b, int *two_byte_p)
22931 struct face *face;
22932 unsigned code = 0;
22934 eassert (glyph->type == CHAR_GLYPH);
22935 face = FACE_FROM_ID (f, glyph->face_id);
22937 /* Make sure X resources of the face are allocated. */
22938 eassert (face != NULL);
22939 PREPARE_FACE_FOR_DISPLAY (f, face);
22941 if (two_byte_p)
22942 *two_byte_p = 0;
22944 if (face->font)
22946 if (CHAR_BYTE8_P (glyph->u.ch))
22947 code = CHAR_TO_BYTE8 (glyph->u.ch);
22948 else
22949 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22951 if (code == FONT_INVALID_CODE)
22952 code = 0;
22955 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22956 return face;
22960 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22961 Return 1 if FONT has a glyph for C, otherwise return 0. */
22963 static int
22964 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22966 unsigned code;
22968 if (CHAR_BYTE8_P (c))
22969 code = CHAR_TO_BYTE8 (c);
22970 else
22971 code = font->driver->encode_char (font, c);
22973 if (code == FONT_INVALID_CODE)
22974 return 0;
22975 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22976 return 1;
22980 /* Fill glyph string S with composition components specified by S->cmp.
22982 BASE_FACE is the base face of the composition.
22983 S->cmp_from is the index of the first component for S.
22985 OVERLAPS non-zero means S should draw the foreground only, and use
22986 its physical height for clipping. See also draw_glyphs.
22988 Value is the index of a component not in S. */
22990 static int
22991 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22992 int overlaps)
22994 int i;
22995 /* For all glyphs of this composition, starting at the offset
22996 S->cmp_from, until we reach the end of the definition or encounter a
22997 glyph that requires the different face, add it to S. */
22998 struct face *face;
23000 eassert (s);
23002 s->for_overlaps = overlaps;
23003 s->face = NULL;
23004 s->font = NULL;
23005 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23007 int c = COMPOSITION_GLYPH (s->cmp, i);
23009 /* TAB in a composition means display glyphs with padding space
23010 on the left or right. */
23011 if (c != '\t')
23013 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23014 -1, Qnil);
23016 face = get_char_face_and_encoding (s->f, c, face_id,
23017 s->char2b + i, 1);
23018 if (face)
23020 if (! s->face)
23022 s->face = face;
23023 s->font = s->face->font;
23025 else if (s->face != face)
23026 break;
23029 ++s->nchars;
23031 s->cmp_to = i;
23033 if (s->face == NULL)
23035 s->face = base_face->ascii_face;
23036 s->font = s->face->font;
23039 /* All glyph strings for the same composition has the same width,
23040 i.e. the width set for the first component of the composition. */
23041 s->width = s->first_glyph->pixel_width;
23043 /* If the specified font could not be loaded, use the frame's
23044 default font, but record the fact that we couldn't load it in
23045 the glyph string so that we can draw rectangles for the
23046 characters of the glyph string. */
23047 if (s->font == NULL)
23049 s->font_not_found_p = 1;
23050 s->font = FRAME_FONT (s->f);
23053 /* Adjust base line for subscript/superscript text. */
23054 s->ybase += s->first_glyph->voffset;
23056 /* This glyph string must always be drawn with 16-bit functions. */
23057 s->two_byte_p = 1;
23059 return s->cmp_to;
23062 static int
23063 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23064 int start, int end, int overlaps)
23066 struct glyph *glyph, *last;
23067 Lisp_Object lgstring;
23068 int i;
23070 s->for_overlaps = overlaps;
23071 glyph = s->row->glyphs[s->area] + start;
23072 last = s->row->glyphs[s->area] + end;
23073 s->cmp_id = glyph->u.cmp.id;
23074 s->cmp_from = glyph->slice.cmp.from;
23075 s->cmp_to = glyph->slice.cmp.to + 1;
23076 s->face = FACE_FROM_ID (s->f, face_id);
23077 lgstring = composition_gstring_from_id (s->cmp_id);
23078 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23079 glyph++;
23080 while (glyph < last
23081 && glyph->u.cmp.automatic
23082 && glyph->u.cmp.id == s->cmp_id
23083 && s->cmp_to == glyph->slice.cmp.from)
23084 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23086 for (i = s->cmp_from; i < s->cmp_to; i++)
23088 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23089 unsigned code = LGLYPH_CODE (lglyph);
23091 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23093 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23094 return glyph - s->row->glyphs[s->area];
23098 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23099 See the comment of fill_glyph_string for arguments.
23100 Value is the index of the first glyph not in S. */
23103 static int
23104 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23105 int start, int end, int overlaps)
23107 struct glyph *glyph, *last;
23108 int voffset;
23110 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23111 s->for_overlaps = overlaps;
23112 glyph = s->row->glyphs[s->area] + start;
23113 last = s->row->glyphs[s->area] + end;
23114 voffset = glyph->voffset;
23115 s->face = FACE_FROM_ID (s->f, face_id);
23116 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23117 s->nchars = 1;
23118 s->width = glyph->pixel_width;
23119 glyph++;
23120 while (glyph < last
23121 && glyph->type == GLYPHLESS_GLYPH
23122 && glyph->voffset == voffset
23123 && glyph->face_id == face_id)
23125 s->nchars++;
23126 s->width += glyph->pixel_width;
23127 glyph++;
23129 s->ybase += voffset;
23130 return glyph - s->row->glyphs[s->area];
23134 /* Fill glyph string S from a sequence of character glyphs.
23136 FACE_ID is the face id of the string. START is the index of the
23137 first glyph to consider, END is the index of the last + 1.
23138 OVERLAPS non-zero means S should draw the foreground only, and use
23139 its physical height for clipping. See also draw_glyphs.
23141 Value is the index of the first glyph not in S. */
23143 static int
23144 fill_glyph_string (struct glyph_string *s, int face_id,
23145 int start, int end, int overlaps)
23147 struct glyph *glyph, *last;
23148 int voffset;
23149 int glyph_not_available_p;
23151 eassert (s->f == XFRAME (s->w->frame));
23152 eassert (s->nchars == 0);
23153 eassert (start >= 0 && end > start);
23155 s->for_overlaps = overlaps;
23156 glyph = s->row->glyphs[s->area] + start;
23157 last = s->row->glyphs[s->area] + end;
23158 voffset = glyph->voffset;
23159 s->padding_p = glyph->padding_p;
23160 glyph_not_available_p = glyph->glyph_not_available_p;
23162 while (glyph < last
23163 && glyph->type == CHAR_GLYPH
23164 && glyph->voffset == voffset
23165 /* Same face id implies same font, nowadays. */
23166 && glyph->face_id == face_id
23167 && glyph->glyph_not_available_p == glyph_not_available_p)
23169 int two_byte_p;
23171 s->face = get_glyph_face_and_encoding (s->f, glyph,
23172 s->char2b + s->nchars,
23173 &two_byte_p);
23174 s->two_byte_p = two_byte_p;
23175 ++s->nchars;
23176 eassert (s->nchars <= end - start);
23177 s->width += glyph->pixel_width;
23178 if (glyph++->padding_p != s->padding_p)
23179 break;
23182 s->font = s->face->font;
23184 /* If the specified font could not be loaded, use the frame's font,
23185 but record the fact that we couldn't load it in
23186 S->font_not_found_p so that we can draw rectangles for the
23187 characters of the glyph string. */
23188 if (s->font == NULL || glyph_not_available_p)
23190 s->font_not_found_p = 1;
23191 s->font = FRAME_FONT (s->f);
23194 /* Adjust base line for subscript/superscript text. */
23195 s->ybase += voffset;
23197 eassert (s->face && s->face->gc);
23198 return glyph - s->row->glyphs[s->area];
23202 /* Fill glyph string S from image glyph S->first_glyph. */
23204 static void
23205 fill_image_glyph_string (struct glyph_string *s)
23207 eassert (s->first_glyph->type == IMAGE_GLYPH);
23208 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23209 eassert (s->img);
23210 s->slice = s->first_glyph->slice.img;
23211 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23212 s->font = s->face->font;
23213 s->width = s->first_glyph->pixel_width;
23215 /* Adjust base line for subscript/superscript text. */
23216 s->ybase += s->first_glyph->voffset;
23220 /* Fill glyph string S from a sequence of stretch glyphs.
23222 START is the index of the first glyph to consider,
23223 END is the index of the last + 1.
23225 Value is the index of the first glyph not in S. */
23227 static int
23228 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23230 struct glyph *glyph, *last;
23231 int voffset, face_id;
23233 eassert (s->first_glyph->type == STRETCH_GLYPH);
23235 glyph = s->row->glyphs[s->area] + start;
23236 last = s->row->glyphs[s->area] + end;
23237 face_id = glyph->face_id;
23238 s->face = FACE_FROM_ID (s->f, face_id);
23239 s->font = s->face->font;
23240 s->width = glyph->pixel_width;
23241 s->nchars = 1;
23242 voffset = glyph->voffset;
23244 for (++glyph;
23245 (glyph < last
23246 && glyph->type == STRETCH_GLYPH
23247 && glyph->voffset == voffset
23248 && glyph->face_id == face_id);
23249 ++glyph)
23250 s->width += glyph->pixel_width;
23252 /* Adjust base line for subscript/superscript text. */
23253 s->ybase += voffset;
23255 /* The case that face->gc == 0 is handled when drawing the glyph
23256 string by calling PREPARE_FACE_FOR_DISPLAY. */
23257 eassert (s->face);
23258 return glyph - s->row->glyphs[s->area];
23261 static struct font_metrics *
23262 get_per_char_metric (struct font *font, XChar2b *char2b)
23264 static struct font_metrics metrics;
23265 unsigned code;
23267 if (! font)
23268 return NULL;
23269 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23270 if (code == FONT_INVALID_CODE)
23271 return NULL;
23272 font->driver->text_extents (font, &code, 1, &metrics);
23273 return &metrics;
23276 /* EXPORT for RIF:
23277 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23278 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23279 assumed to be zero. */
23281 void
23282 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23284 *left = *right = 0;
23286 if (glyph->type == CHAR_GLYPH)
23288 struct face *face;
23289 XChar2b char2b;
23290 struct font_metrics *pcm;
23292 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23293 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23295 if (pcm->rbearing > pcm->width)
23296 *right = pcm->rbearing - pcm->width;
23297 if (pcm->lbearing < 0)
23298 *left = -pcm->lbearing;
23301 else if (glyph->type == COMPOSITE_GLYPH)
23303 if (! glyph->u.cmp.automatic)
23305 struct composition *cmp = composition_table[glyph->u.cmp.id];
23307 if (cmp->rbearing > cmp->pixel_width)
23308 *right = cmp->rbearing - cmp->pixel_width;
23309 if (cmp->lbearing < 0)
23310 *left = - cmp->lbearing;
23312 else
23314 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23315 struct font_metrics metrics;
23317 composition_gstring_width (gstring, glyph->slice.cmp.from,
23318 glyph->slice.cmp.to + 1, &metrics);
23319 if (metrics.rbearing > metrics.width)
23320 *right = metrics.rbearing - metrics.width;
23321 if (metrics.lbearing < 0)
23322 *left = - metrics.lbearing;
23328 /* Return the index of the first glyph preceding glyph string S that
23329 is overwritten by S because of S's left overhang. Value is -1
23330 if no glyphs are overwritten. */
23332 static int
23333 left_overwritten (struct glyph_string *s)
23335 int k;
23337 if (s->left_overhang)
23339 int x = 0, i;
23340 struct glyph *glyphs = s->row->glyphs[s->area];
23341 int first = s->first_glyph - glyphs;
23343 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23344 x -= glyphs[i].pixel_width;
23346 k = i + 1;
23348 else
23349 k = -1;
23351 return k;
23355 /* Return the index of the first glyph preceding glyph string S that
23356 is overwriting S because of its right overhang. Value is -1 if no
23357 glyph in front of S overwrites S. */
23359 static int
23360 left_overwriting (struct glyph_string *s)
23362 int i, k, x;
23363 struct glyph *glyphs = s->row->glyphs[s->area];
23364 int first = s->first_glyph - glyphs;
23366 k = -1;
23367 x = 0;
23368 for (i = first - 1; i >= 0; --i)
23370 int left, right;
23371 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23372 if (x + right > 0)
23373 k = i;
23374 x -= glyphs[i].pixel_width;
23377 return k;
23381 /* Return the index of the last glyph following glyph string S that is
23382 overwritten by S because of S's right overhang. Value is -1 if
23383 no such glyph is found. */
23385 static int
23386 right_overwritten (struct glyph_string *s)
23388 int k = -1;
23390 if (s->right_overhang)
23392 int x = 0, i;
23393 struct glyph *glyphs = s->row->glyphs[s->area];
23394 int first = (s->first_glyph - glyphs
23395 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23396 int end = s->row->used[s->area];
23398 for (i = first; i < end && s->right_overhang > x; ++i)
23399 x += glyphs[i].pixel_width;
23401 k = i;
23404 return k;
23408 /* Return the index of the last glyph following glyph string S that
23409 overwrites S because of its left overhang. Value is negative
23410 if no such glyph is found. */
23412 static int
23413 right_overwriting (struct glyph_string *s)
23415 int i, k, x;
23416 int end = s->row->used[s->area];
23417 struct glyph *glyphs = s->row->glyphs[s->area];
23418 int first = (s->first_glyph - glyphs
23419 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23421 k = -1;
23422 x = 0;
23423 for (i = first; i < end; ++i)
23425 int left, right;
23426 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23427 if (x - left < 0)
23428 k = i;
23429 x += glyphs[i].pixel_width;
23432 return k;
23436 /* Set background width of glyph string S. START is the index of the
23437 first glyph following S. LAST_X is the right-most x-position + 1
23438 in the drawing area. */
23440 static void
23441 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23443 /* If the face of this glyph string has to be drawn to the end of
23444 the drawing area, set S->extends_to_end_of_line_p. */
23446 if (start == s->row->used[s->area]
23447 && s->area == TEXT_AREA
23448 && ((s->row->fill_line_p
23449 && (s->hl == DRAW_NORMAL_TEXT
23450 || s->hl == DRAW_IMAGE_RAISED
23451 || s->hl == DRAW_IMAGE_SUNKEN))
23452 || s->hl == DRAW_MOUSE_FACE))
23453 s->extends_to_end_of_line_p = 1;
23455 /* If S extends its face to the end of the line, set its
23456 background_width to the distance to the right edge of the drawing
23457 area. */
23458 if (s->extends_to_end_of_line_p)
23459 s->background_width = last_x - s->x + 1;
23460 else
23461 s->background_width = s->width;
23465 /* Compute overhangs and x-positions for glyph string S and its
23466 predecessors, or successors. X is the starting x-position for S.
23467 BACKWARD_P non-zero means process predecessors. */
23469 static void
23470 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23472 if (backward_p)
23474 while (s)
23476 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23477 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23478 x -= s->width;
23479 s->x = x;
23480 s = s->prev;
23483 else
23485 while (s)
23487 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23488 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23489 s->x = x;
23490 x += s->width;
23491 s = s->next;
23498 /* The following macros are only called from draw_glyphs below.
23499 They reference the following parameters of that function directly:
23500 `w', `row', `area', and `overlap_p'
23501 as well as the following local variables:
23502 `s', `f', and `hdc' (in W32) */
23504 #ifdef HAVE_NTGUI
23505 /* On W32, silently add local `hdc' variable to argument list of
23506 init_glyph_string. */
23507 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23508 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23509 #else
23510 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23511 init_glyph_string (s, char2b, w, row, area, start, hl)
23512 #endif
23514 /* Add a glyph string for a stretch glyph to the list of strings
23515 between HEAD and TAIL. START is the index of the stretch glyph in
23516 row area AREA of glyph row ROW. END is the index of the last glyph
23517 in that glyph row area. X is the current output position assigned
23518 to the new glyph string constructed. HL overrides that face of the
23519 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23520 is the right-most x-position of the drawing area. */
23522 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23523 and below -- keep them on one line. */
23524 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23525 do \
23527 s = alloca (sizeof *s); \
23528 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23529 START = fill_stretch_glyph_string (s, START, END); \
23530 append_glyph_string (&HEAD, &TAIL, s); \
23531 s->x = (X); \
23533 while (0)
23536 /* Add a glyph string for an image glyph to the list of strings
23537 between HEAD and TAIL. START is the index of the image glyph in
23538 row area AREA of glyph row ROW. END is the index of the last glyph
23539 in that glyph row area. X is the current output position assigned
23540 to the new glyph string constructed. HL overrides that face of the
23541 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23542 is the right-most x-position of the drawing area. */
23544 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23545 do \
23547 s = alloca (sizeof *s); \
23548 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23549 fill_image_glyph_string (s); \
23550 append_glyph_string (&HEAD, &TAIL, s); \
23551 ++START; \
23552 s->x = (X); \
23554 while (0)
23557 /* Add a glyph string for a sequence of character glyphs to the list
23558 of strings between HEAD and TAIL. START is the index of the first
23559 glyph in row area AREA of glyph row ROW that is part of the new
23560 glyph string. END is the index of the last glyph in that glyph row
23561 area. X is the current output position assigned to the new glyph
23562 string constructed. HL overrides that face of the glyph; e.g. it
23563 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23564 right-most x-position of the drawing area. */
23566 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23567 do \
23569 int face_id; \
23570 XChar2b *char2b; \
23572 face_id = (row)->glyphs[area][START].face_id; \
23574 s = alloca (sizeof *s); \
23575 char2b = alloca ((END - START) * sizeof *char2b); \
23576 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23577 append_glyph_string (&HEAD, &TAIL, s); \
23578 s->x = (X); \
23579 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23581 while (0)
23584 /* Add a glyph string for a composite sequence to the list of strings
23585 between HEAD and TAIL. START is the index of the first glyph in
23586 row area AREA of glyph row ROW that is part of the new glyph
23587 string. END is the index of the last glyph in that glyph row area.
23588 X is the current output position assigned to the new glyph string
23589 constructed. HL overrides that face of the glyph; e.g. it is
23590 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23591 x-position of the drawing area. */
23593 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23594 do { \
23595 int face_id = (row)->glyphs[area][START].face_id; \
23596 struct face *base_face = FACE_FROM_ID (f, face_id); \
23597 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23598 struct composition *cmp = composition_table[cmp_id]; \
23599 XChar2b *char2b; \
23600 struct glyph_string *first_s = NULL; \
23601 int n; \
23603 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23605 /* Make glyph_strings for each glyph sequence that is drawable by \
23606 the same face, and append them to HEAD/TAIL. */ \
23607 for (n = 0; n < cmp->glyph_len;) \
23609 s = alloca (sizeof *s); \
23610 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23611 append_glyph_string (&(HEAD), &(TAIL), s); \
23612 s->cmp = cmp; \
23613 s->cmp_from = n; \
23614 s->x = (X); \
23615 if (n == 0) \
23616 first_s = s; \
23617 n = fill_composite_glyph_string (s, base_face, overlaps); \
23620 ++START; \
23621 s = first_s; \
23622 } while (0)
23625 /* Add a glyph string for a glyph-string sequence to the list of strings
23626 between HEAD and TAIL. */
23628 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23629 do { \
23630 int face_id; \
23631 XChar2b *char2b; \
23632 Lisp_Object gstring; \
23634 face_id = (row)->glyphs[area][START].face_id; \
23635 gstring = (composition_gstring_from_id \
23636 ((row)->glyphs[area][START].u.cmp.id)); \
23637 s = alloca (sizeof *s); \
23638 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23639 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23640 append_glyph_string (&(HEAD), &(TAIL), s); \
23641 s->x = (X); \
23642 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23643 } while (0)
23646 /* Add a glyph string for a sequence of glyphless character's glyphs
23647 to the list of strings between HEAD and TAIL. The meanings of
23648 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23650 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23651 do \
23653 int face_id; \
23655 face_id = (row)->glyphs[area][START].face_id; \
23657 s = alloca (sizeof *s); \
23658 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23659 append_glyph_string (&HEAD, &TAIL, s); \
23660 s->x = (X); \
23661 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23662 overlaps); \
23664 while (0)
23667 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23668 of AREA of glyph row ROW on window W between indices START and END.
23669 HL overrides the face for drawing glyph strings, e.g. it is
23670 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23671 x-positions of the drawing area.
23673 This is an ugly monster macro construct because we must use alloca
23674 to allocate glyph strings (because draw_glyphs can be called
23675 asynchronously). */
23677 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23678 do \
23680 HEAD = TAIL = NULL; \
23681 while (START < END) \
23683 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23684 switch (first_glyph->type) \
23686 case CHAR_GLYPH: \
23687 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23688 HL, X, LAST_X); \
23689 break; \
23691 case COMPOSITE_GLYPH: \
23692 if (first_glyph->u.cmp.automatic) \
23693 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23694 HL, X, LAST_X); \
23695 else \
23696 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23697 HL, X, LAST_X); \
23698 break; \
23700 case STRETCH_GLYPH: \
23701 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23702 HL, X, LAST_X); \
23703 break; \
23705 case IMAGE_GLYPH: \
23706 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23707 HL, X, LAST_X); \
23708 break; \
23710 case GLYPHLESS_GLYPH: \
23711 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23712 HL, X, LAST_X); \
23713 break; \
23715 default: \
23716 emacs_abort (); \
23719 if (s) \
23721 set_glyph_string_background_width (s, START, LAST_X); \
23722 (X) += s->width; \
23725 } while (0)
23728 /* Draw glyphs between START and END in AREA of ROW on window W,
23729 starting at x-position X. X is relative to AREA in W. HL is a
23730 face-override with the following meaning:
23732 DRAW_NORMAL_TEXT draw normally
23733 DRAW_CURSOR draw in cursor face
23734 DRAW_MOUSE_FACE draw in mouse face.
23735 DRAW_INVERSE_VIDEO draw in mode line face
23736 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23737 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23739 If OVERLAPS is non-zero, draw only the foreground of characters and
23740 clip to the physical height of ROW. Non-zero value also defines
23741 the overlapping part to be drawn:
23743 OVERLAPS_PRED overlap with preceding rows
23744 OVERLAPS_SUCC overlap with succeeding rows
23745 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23746 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23748 Value is the x-position reached, relative to AREA of W. */
23750 static int
23751 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23752 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23753 enum draw_glyphs_face hl, int overlaps)
23755 struct glyph_string *head, *tail;
23756 struct glyph_string *s;
23757 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23758 int i, j, x_reached, last_x, area_left = 0;
23759 struct frame *f = XFRAME (WINDOW_FRAME (w));
23760 DECLARE_HDC (hdc);
23762 ALLOCATE_HDC (hdc, f);
23764 /* Let's rather be paranoid than getting a SEGV. */
23765 end = min (end, row->used[area]);
23766 start = clip_to_bounds (0, start, end);
23768 /* Translate X to frame coordinates. Set last_x to the right
23769 end of the drawing area. */
23770 if (row->full_width_p)
23772 /* X is relative to the left edge of W, without scroll bars
23773 or fringes. */
23774 area_left = WINDOW_LEFT_EDGE_X (w);
23775 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23777 else
23779 area_left = window_box_left (w, area);
23780 last_x = area_left + window_box_width (w, area);
23782 x += area_left;
23784 /* Build a doubly-linked list of glyph_string structures between
23785 head and tail from what we have to draw. Note that the macro
23786 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23787 the reason we use a separate variable `i'. */
23788 i = start;
23789 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23790 if (tail)
23791 x_reached = tail->x + tail->background_width;
23792 else
23793 x_reached = x;
23795 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23796 the row, redraw some glyphs in front or following the glyph
23797 strings built above. */
23798 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23800 struct glyph_string *h, *t;
23801 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23802 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23803 int check_mouse_face = 0;
23804 int dummy_x = 0;
23806 /* If mouse highlighting is on, we may need to draw adjacent
23807 glyphs using mouse-face highlighting. */
23808 if (area == TEXT_AREA && row->mouse_face_p
23809 && hlinfo->mouse_face_beg_row >= 0
23810 && hlinfo->mouse_face_end_row >= 0)
23812 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23814 if (row_vpos >= hlinfo->mouse_face_beg_row
23815 && row_vpos <= hlinfo->mouse_face_end_row)
23817 check_mouse_face = 1;
23818 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23819 ? hlinfo->mouse_face_beg_col : 0;
23820 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23821 ? hlinfo->mouse_face_end_col
23822 : row->used[TEXT_AREA];
23826 /* Compute overhangs for all glyph strings. */
23827 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23828 for (s = head; s; s = s->next)
23829 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23831 /* Prepend glyph strings for glyphs in front of the first glyph
23832 string that are overwritten because of the first glyph
23833 string's left overhang. The background of all strings
23834 prepended must be drawn because the first glyph string
23835 draws over it. */
23836 i = left_overwritten (head);
23837 if (i >= 0)
23839 enum draw_glyphs_face overlap_hl;
23841 /* If this row contains mouse highlighting, attempt to draw
23842 the overlapped glyphs with the correct highlight. This
23843 code fails if the overlap encompasses more than one glyph
23844 and mouse-highlight spans only some of these glyphs.
23845 However, making it work perfectly involves a lot more
23846 code, and I don't know if the pathological case occurs in
23847 practice, so we'll stick to this for now. --- cyd */
23848 if (check_mouse_face
23849 && mouse_beg_col < start && mouse_end_col > i)
23850 overlap_hl = DRAW_MOUSE_FACE;
23851 else
23852 overlap_hl = DRAW_NORMAL_TEXT;
23854 j = i;
23855 BUILD_GLYPH_STRINGS (j, start, h, t,
23856 overlap_hl, dummy_x, last_x);
23857 start = i;
23858 compute_overhangs_and_x (t, head->x, 1);
23859 prepend_glyph_string_lists (&head, &tail, h, t);
23860 clip_head = head;
23863 /* Prepend glyph strings for glyphs in front of the first glyph
23864 string that overwrite that glyph string because of their
23865 right overhang. For these strings, only the foreground must
23866 be drawn, because it draws over the glyph string at `head'.
23867 The background must not be drawn because this would overwrite
23868 right overhangs of preceding glyphs for which no glyph
23869 strings exist. */
23870 i = left_overwriting (head);
23871 if (i >= 0)
23873 enum draw_glyphs_face overlap_hl;
23875 if (check_mouse_face
23876 && mouse_beg_col < start && mouse_end_col > i)
23877 overlap_hl = DRAW_MOUSE_FACE;
23878 else
23879 overlap_hl = DRAW_NORMAL_TEXT;
23881 clip_head = head;
23882 BUILD_GLYPH_STRINGS (i, start, h, t,
23883 overlap_hl, dummy_x, last_x);
23884 for (s = h; s; s = s->next)
23885 s->background_filled_p = 1;
23886 compute_overhangs_and_x (t, head->x, 1);
23887 prepend_glyph_string_lists (&head, &tail, h, t);
23890 /* Append glyphs strings for glyphs following the last glyph
23891 string tail that are overwritten by tail. The background of
23892 these strings has to be drawn because tail's foreground draws
23893 over it. */
23894 i = right_overwritten (tail);
23895 if (i >= 0)
23897 enum draw_glyphs_face overlap_hl;
23899 if (check_mouse_face
23900 && mouse_beg_col < i && mouse_end_col > end)
23901 overlap_hl = DRAW_MOUSE_FACE;
23902 else
23903 overlap_hl = DRAW_NORMAL_TEXT;
23905 BUILD_GLYPH_STRINGS (end, i, h, t,
23906 overlap_hl, x, last_x);
23907 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23908 we don't have `end = i;' here. */
23909 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23910 append_glyph_string_lists (&head, &tail, h, t);
23911 clip_tail = tail;
23914 /* Append glyph strings for glyphs following the last glyph
23915 string tail that overwrite tail. The foreground of such
23916 glyphs has to be drawn because it writes into the background
23917 of tail. The background must not be drawn because it could
23918 paint over the foreground of following glyphs. */
23919 i = right_overwriting (tail);
23920 if (i >= 0)
23922 enum draw_glyphs_face overlap_hl;
23923 if (check_mouse_face
23924 && mouse_beg_col < i && mouse_end_col > end)
23925 overlap_hl = DRAW_MOUSE_FACE;
23926 else
23927 overlap_hl = DRAW_NORMAL_TEXT;
23929 clip_tail = tail;
23930 i++; /* We must include the Ith glyph. */
23931 BUILD_GLYPH_STRINGS (end, i, h, t,
23932 overlap_hl, x, last_x);
23933 for (s = h; s; s = s->next)
23934 s->background_filled_p = 1;
23935 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23936 append_glyph_string_lists (&head, &tail, h, t);
23938 if (clip_head || clip_tail)
23939 for (s = head; s; s = s->next)
23941 s->clip_head = clip_head;
23942 s->clip_tail = clip_tail;
23946 /* Draw all strings. */
23947 for (s = head; s; s = s->next)
23948 FRAME_RIF (f)->draw_glyph_string (s);
23950 #ifndef HAVE_NS
23951 /* When focus a sole frame and move horizontally, this sets on_p to 0
23952 causing a failure to erase prev cursor position. */
23953 if (area == TEXT_AREA
23954 && !row->full_width_p
23955 /* When drawing overlapping rows, only the glyph strings'
23956 foreground is drawn, which doesn't erase a cursor
23957 completely. */
23958 && !overlaps)
23960 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23961 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23962 : (tail ? tail->x + tail->background_width : x));
23963 x0 -= area_left;
23964 x1 -= area_left;
23966 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23967 row->y, MATRIX_ROW_BOTTOM_Y (row));
23969 #endif
23971 /* Value is the x-position up to which drawn, relative to AREA of W.
23972 This doesn't include parts drawn because of overhangs. */
23973 if (row->full_width_p)
23974 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23975 else
23976 x_reached -= area_left;
23978 RELEASE_HDC (hdc, f);
23980 return x_reached;
23983 /* Expand row matrix if too narrow. Don't expand if area
23984 is not present. */
23986 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23988 if (!it->f->fonts_changed \
23989 && (it->glyph_row->glyphs[area] \
23990 < it->glyph_row->glyphs[area + 1])) \
23992 it->w->ncols_scale_factor++; \
23993 it->f->fonts_changed = 1; \
23997 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23998 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24000 static void
24001 append_glyph (struct it *it)
24003 struct glyph *glyph;
24004 enum glyph_row_area area = it->area;
24006 eassert (it->glyph_row);
24007 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24009 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24010 if (glyph < it->glyph_row->glyphs[area + 1])
24012 /* If the glyph row is reversed, we need to prepend the glyph
24013 rather than append it. */
24014 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24016 struct glyph *g;
24018 /* Make room for the additional glyph. */
24019 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24020 g[1] = *g;
24021 glyph = it->glyph_row->glyphs[area];
24023 glyph->charpos = CHARPOS (it->position);
24024 glyph->object = it->object;
24025 if (it->pixel_width > 0)
24027 glyph->pixel_width = it->pixel_width;
24028 glyph->padding_p = 0;
24030 else
24032 /* Assure at least 1-pixel width. Otherwise, cursor can't
24033 be displayed correctly. */
24034 glyph->pixel_width = 1;
24035 glyph->padding_p = 1;
24037 glyph->ascent = it->ascent;
24038 glyph->descent = it->descent;
24039 glyph->voffset = it->voffset;
24040 glyph->type = CHAR_GLYPH;
24041 glyph->avoid_cursor_p = it->avoid_cursor_p;
24042 glyph->multibyte_p = it->multibyte_p;
24043 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24045 /* In R2L rows, the left and the right box edges need to be
24046 drawn in reverse direction. */
24047 glyph->right_box_line_p = it->start_of_box_run_p;
24048 glyph->left_box_line_p = it->end_of_box_run_p;
24050 else
24052 glyph->left_box_line_p = it->start_of_box_run_p;
24053 glyph->right_box_line_p = it->end_of_box_run_p;
24055 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24056 || it->phys_descent > it->descent);
24057 glyph->glyph_not_available_p = it->glyph_not_available_p;
24058 glyph->face_id = it->face_id;
24059 glyph->u.ch = it->char_to_display;
24060 glyph->slice.img = null_glyph_slice;
24061 glyph->font_type = FONT_TYPE_UNKNOWN;
24062 if (it->bidi_p)
24064 glyph->resolved_level = it->bidi_it.resolved_level;
24065 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24066 emacs_abort ();
24067 glyph->bidi_type = it->bidi_it.type;
24069 else
24071 glyph->resolved_level = 0;
24072 glyph->bidi_type = UNKNOWN_BT;
24074 ++it->glyph_row->used[area];
24076 else
24077 IT_EXPAND_MATRIX_WIDTH (it, area);
24080 /* Store one glyph for the composition IT->cmp_it.id in
24081 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24082 non-null. */
24084 static void
24085 append_composite_glyph (struct it *it)
24087 struct glyph *glyph;
24088 enum glyph_row_area area = it->area;
24090 eassert (it->glyph_row);
24092 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24093 if (glyph < it->glyph_row->glyphs[area + 1])
24095 /* If the glyph row is reversed, we need to prepend the glyph
24096 rather than append it. */
24097 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24099 struct glyph *g;
24101 /* Make room for the new glyph. */
24102 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24103 g[1] = *g;
24104 glyph = it->glyph_row->glyphs[it->area];
24106 glyph->charpos = it->cmp_it.charpos;
24107 glyph->object = it->object;
24108 glyph->pixel_width = it->pixel_width;
24109 glyph->ascent = it->ascent;
24110 glyph->descent = it->descent;
24111 glyph->voffset = it->voffset;
24112 glyph->type = COMPOSITE_GLYPH;
24113 if (it->cmp_it.ch < 0)
24115 glyph->u.cmp.automatic = 0;
24116 glyph->u.cmp.id = it->cmp_it.id;
24117 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24119 else
24121 glyph->u.cmp.automatic = 1;
24122 glyph->u.cmp.id = it->cmp_it.id;
24123 glyph->slice.cmp.from = it->cmp_it.from;
24124 glyph->slice.cmp.to = it->cmp_it.to - 1;
24126 glyph->avoid_cursor_p = it->avoid_cursor_p;
24127 glyph->multibyte_p = it->multibyte_p;
24128 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24130 /* In R2L rows, the left and the right box edges need to be
24131 drawn in reverse direction. */
24132 glyph->right_box_line_p = it->start_of_box_run_p;
24133 glyph->left_box_line_p = it->end_of_box_run_p;
24135 else
24137 glyph->left_box_line_p = it->start_of_box_run_p;
24138 glyph->right_box_line_p = it->end_of_box_run_p;
24140 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24141 || it->phys_descent > it->descent);
24142 glyph->padding_p = 0;
24143 glyph->glyph_not_available_p = 0;
24144 glyph->face_id = it->face_id;
24145 glyph->font_type = FONT_TYPE_UNKNOWN;
24146 if (it->bidi_p)
24148 glyph->resolved_level = it->bidi_it.resolved_level;
24149 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24150 emacs_abort ();
24151 glyph->bidi_type = it->bidi_it.type;
24153 ++it->glyph_row->used[area];
24155 else
24156 IT_EXPAND_MATRIX_WIDTH (it, area);
24160 /* Change IT->ascent and IT->height according to the setting of
24161 IT->voffset. */
24163 static void
24164 take_vertical_position_into_account (struct it *it)
24166 if (it->voffset)
24168 if (it->voffset < 0)
24169 /* Increase the ascent so that we can display the text higher
24170 in the line. */
24171 it->ascent -= it->voffset;
24172 else
24173 /* Increase the descent so that we can display the text lower
24174 in the line. */
24175 it->descent += it->voffset;
24180 /* Produce glyphs/get display metrics for the image IT is loaded with.
24181 See the description of struct display_iterator in dispextern.h for
24182 an overview of struct display_iterator. */
24184 static void
24185 produce_image_glyph (struct it *it)
24187 struct image *img;
24188 struct face *face;
24189 int glyph_ascent, crop;
24190 struct glyph_slice slice;
24192 eassert (it->what == IT_IMAGE);
24194 face = FACE_FROM_ID (it->f, it->face_id);
24195 eassert (face);
24196 /* Make sure X resources of the face is loaded. */
24197 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24199 if (it->image_id < 0)
24201 /* Fringe bitmap. */
24202 it->ascent = it->phys_ascent = 0;
24203 it->descent = it->phys_descent = 0;
24204 it->pixel_width = 0;
24205 it->nglyphs = 0;
24206 return;
24209 img = IMAGE_FROM_ID (it->f, it->image_id);
24210 eassert (img);
24211 /* Make sure X resources of the image is loaded. */
24212 prepare_image_for_display (it->f, img);
24214 slice.x = slice.y = 0;
24215 slice.width = img->width;
24216 slice.height = img->height;
24218 if (INTEGERP (it->slice.x))
24219 slice.x = XINT (it->slice.x);
24220 else if (FLOATP (it->slice.x))
24221 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24223 if (INTEGERP (it->slice.y))
24224 slice.y = XINT (it->slice.y);
24225 else if (FLOATP (it->slice.y))
24226 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24228 if (INTEGERP (it->slice.width))
24229 slice.width = XINT (it->slice.width);
24230 else if (FLOATP (it->slice.width))
24231 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24233 if (INTEGERP (it->slice.height))
24234 slice.height = XINT (it->slice.height);
24235 else if (FLOATP (it->slice.height))
24236 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24238 if (slice.x >= img->width)
24239 slice.x = img->width;
24240 if (slice.y >= img->height)
24241 slice.y = img->height;
24242 if (slice.x + slice.width >= img->width)
24243 slice.width = img->width - slice.x;
24244 if (slice.y + slice.height > img->height)
24245 slice.height = img->height - slice.y;
24247 if (slice.width == 0 || slice.height == 0)
24248 return;
24250 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24252 it->descent = slice.height - glyph_ascent;
24253 if (slice.y == 0)
24254 it->descent += img->vmargin;
24255 if (slice.y + slice.height == img->height)
24256 it->descent += img->vmargin;
24257 it->phys_descent = it->descent;
24259 it->pixel_width = slice.width;
24260 if (slice.x == 0)
24261 it->pixel_width += img->hmargin;
24262 if (slice.x + slice.width == img->width)
24263 it->pixel_width += img->hmargin;
24265 /* It's quite possible for images to have an ascent greater than
24266 their height, so don't get confused in that case. */
24267 if (it->descent < 0)
24268 it->descent = 0;
24270 it->nglyphs = 1;
24272 if (face->box != FACE_NO_BOX)
24274 if (face->box_line_width > 0)
24276 if (slice.y == 0)
24277 it->ascent += face->box_line_width;
24278 if (slice.y + slice.height == img->height)
24279 it->descent += face->box_line_width;
24282 if (it->start_of_box_run_p && slice.x == 0)
24283 it->pixel_width += eabs (face->box_line_width);
24284 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24285 it->pixel_width += eabs (face->box_line_width);
24288 take_vertical_position_into_account (it);
24290 /* Automatically crop wide image glyphs at right edge so we can
24291 draw the cursor on same display row. */
24292 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24293 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24295 it->pixel_width -= crop;
24296 slice.width -= crop;
24299 if (it->glyph_row)
24301 struct glyph *glyph;
24302 enum glyph_row_area area = it->area;
24304 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24305 if (glyph < it->glyph_row->glyphs[area + 1])
24307 glyph->charpos = CHARPOS (it->position);
24308 glyph->object = it->object;
24309 glyph->pixel_width = it->pixel_width;
24310 glyph->ascent = glyph_ascent;
24311 glyph->descent = it->descent;
24312 glyph->voffset = it->voffset;
24313 glyph->type = IMAGE_GLYPH;
24314 glyph->avoid_cursor_p = it->avoid_cursor_p;
24315 glyph->multibyte_p = it->multibyte_p;
24316 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24318 /* In R2L rows, the left and the right box edges need to be
24319 drawn in reverse direction. */
24320 glyph->right_box_line_p = it->start_of_box_run_p;
24321 glyph->left_box_line_p = it->end_of_box_run_p;
24323 else
24325 glyph->left_box_line_p = it->start_of_box_run_p;
24326 glyph->right_box_line_p = it->end_of_box_run_p;
24328 glyph->overlaps_vertically_p = 0;
24329 glyph->padding_p = 0;
24330 glyph->glyph_not_available_p = 0;
24331 glyph->face_id = it->face_id;
24332 glyph->u.img_id = img->id;
24333 glyph->slice.img = slice;
24334 glyph->font_type = FONT_TYPE_UNKNOWN;
24335 if (it->bidi_p)
24337 glyph->resolved_level = it->bidi_it.resolved_level;
24338 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24339 emacs_abort ();
24340 glyph->bidi_type = it->bidi_it.type;
24342 ++it->glyph_row->used[area];
24344 else
24345 IT_EXPAND_MATRIX_WIDTH (it, area);
24350 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24351 of the glyph, WIDTH and HEIGHT are the width and height of the
24352 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24354 static void
24355 append_stretch_glyph (struct it *it, Lisp_Object object,
24356 int width, int height, int ascent)
24358 struct glyph *glyph;
24359 enum glyph_row_area area = it->area;
24361 eassert (ascent >= 0 && ascent <= height);
24363 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24364 if (glyph < it->glyph_row->glyphs[area + 1])
24366 /* If the glyph row is reversed, we need to prepend the glyph
24367 rather than append it. */
24368 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24370 struct glyph *g;
24372 /* Make room for the additional glyph. */
24373 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24374 g[1] = *g;
24375 glyph = it->glyph_row->glyphs[area];
24377 glyph->charpos = CHARPOS (it->position);
24378 glyph->object = object;
24379 glyph->pixel_width = width;
24380 glyph->ascent = ascent;
24381 glyph->descent = height - ascent;
24382 glyph->voffset = it->voffset;
24383 glyph->type = STRETCH_GLYPH;
24384 glyph->avoid_cursor_p = it->avoid_cursor_p;
24385 glyph->multibyte_p = it->multibyte_p;
24386 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24388 /* In R2L rows, the left and the right box edges need to be
24389 drawn in reverse direction. */
24390 glyph->right_box_line_p = it->start_of_box_run_p;
24391 glyph->left_box_line_p = it->end_of_box_run_p;
24393 else
24395 glyph->left_box_line_p = it->start_of_box_run_p;
24396 glyph->right_box_line_p = it->end_of_box_run_p;
24398 glyph->overlaps_vertically_p = 0;
24399 glyph->padding_p = 0;
24400 glyph->glyph_not_available_p = 0;
24401 glyph->face_id = it->face_id;
24402 glyph->u.stretch.ascent = ascent;
24403 glyph->u.stretch.height = height;
24404 glyph->slice.img = null_glyph_slice;
24405 glyph->font_type = FONT_TYPE_UNKNOWN;
24406 if (it->bidi_p)
24408 glyph->resolved_level = it->bidi_it.resolved_level;
24409 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24410 emacs_abort ();
24411 glyph->bidi_type = it->bidi_it.type;
24413 else
24415 glyph->resolved_level = 0;
24416 glyph->bidi_type = UNKNOWN_BT;
24418 ++it->glyph_row->used[area];
24420 else
24421 IT_EXPAND_MATRIX_WIDTH (it, area);
24424 #endif /* HAVE_WINDOW_SYSTEM */
24426 /* Produce a stretch glyph for iterator IT. IT->object is the value
24427 of the glyph property displayed. The value must be a list
24428 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24429 being recognized:
24431 1. `:width WIDTH' specifies that the space should be WIDTH *
24432 canonical char width wide. WIDTH may be an integer or floating
24433 point number.
24435 2. `:relative-width FACTOR' specifies that the width of the stretch
24436 should be computed from the width of the first character having the
24437 `glyph' property, and should be FACTOR times that width.
24439 3. `:align-to HPOS' specifies that the space should be wide enough
24440 to reach HPOS, a value in canonical character units.
24442 Exactly one of the above pairs must be present.
24444 4. `:height HEIGHT' specifies that the height of the stretch produced
24445 should be HEIGHT, measured in canonical character units.
24447 5. `:relative-height FACTOR' specifies that the height of the
24448 stretch should be FACTOR times the height of the characters having
24449 the glyph property.
24451 Either none or exactly one of 4 or 5 must be present.
24453 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24454 of the stretch should be used for the ascent of the stretch.
24455 ASCENT must be in the range 0 <= ASCENT <= 100. */
24457 void
24458 produce_stretch_glyph (struct it *it)
24460 /* (space :width WIDTH :height HEIGHT ...) */
24461 Lisp_Object prop, plist;
24462 int width = 0, height = 0, align_to = -1;
24463 int zero_width_ok_p = 0;
24464 double tem;
24465 struct font *font = NULL;
24467 #ifdef HAVE_WINDOW_SYSTEM
24468 int ascent = 0;
24469 int zero_height_ok_p = 0;
24471 if (FRAME_WINDOW_P (it->f))
24473 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24474 font = face->font ? face->font : FRAME_FONT (it->f);
24475 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24477 #endif
24479 /* List should start with `space'. */
24480 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24481 plist = XCDR (it->object);
24483 /* Compute the width of the stretch. */
24484 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24485 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24487 /* Absolute width `:width WIDTH' specified and valid. */
24488 zero_width_ok_p = 1;
24489 width = (int)tem;
24491 #ifdef HAVE_WINDOW_SYSTEM
24492 else if (FRAME_WINDOW_P (it->f)
24493 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24495 /* Relative width `:relative-width FACTOR' specified and valid.
24496 Compute the width of the characters having the `glyph'
24497 property. */
24498 struct it it2;
24499 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24501 it2 = *it;
24502 if (it->multibyte_p)
24503 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24504 else
24506 it2.c = it2.char_to_display = *p, it2.len = 1;
24507 if (! ASCII_CHAR_P (it2.c))
24508 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24511 it2.glyph_row = NULL;
24512 it2.what = IT_CHARACTER;
24513 x_produce_glyphs (&it2);
24514 width = NUMVAL (prop) * it2.pixel_width;
24516 #endif /* HAVE_WINDOW_SYSTEM */
24517 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24518 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24520 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24521 align_to = (align_to < 0
24523 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24524 else if (align_to < 0)
24525 align_to = window_box_left_offset (it->w, TEXT_AREA);
24526 width = max (0, (int)tem + align_to - it->current_x);
24527 zero_width_ok_p = 1;
24529 else
24530 /* Nothing specified -> width defaults to canonical char width. */
24531 width = FRAME_COLUMN_WIDTH (it->f);
24533 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24534 width = 1;
24536 #ifdef HAVE_WINDOW_SYSTEM
24537 /* Compute height. */
24538 if (FRAME_WINDOW_P (it->f))
24540 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24541 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24543 height = (int)tem;
24544 zero_height_ok_p = 1;
24546 else if (prop = Fplist_get (plist, QCrelative_height),
24547 NUMVAL (prop) > 0)
24548 height = FONT_HEIGHT (font) * NUMVAL (prop);
24549 else
24550 height = FONT_HEIGHT (font);
24552 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24553 height = 1;
24555 /* Compute percentage of height used for ascent. If
24556 `:ascent ASCENT' is present and valid, use that. Otherwise,
24557 derive the ascent from the font in use. */
24558 if (prop = Fplist_get (plist, QCascent),
24559 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24560 ascent = height * NUMVAL (prop) / 100.0;
24561 else if (!NILP (prop)
24562 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24563 ascent = min (max (0, (int)tem), height);
24564 else
24565 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24567 else
24568 #endif /* HAVE_WINDOW_SYSTEM */
24569 height = 1;
24571 if (width > 0 && it->line_wrap != TRUNCATE
24572 && it->current_x + width > it->last_visible_x)
24574 width = it->last_visible_x - it->current_x;
24575 #ifdef HAVE_WINDOW_SYSTEM
24576 /* Subtract one more pixel from the stretch width, but only on
24577 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24578 width -= FRAME_WINDOW_P (it->f);
24579 #endif
24582 if (width > 0 && height > 0 && it->glyph_row)
24584 Lisp_Object o_object = it->object;
24585 Lisp_Object object = it->stack[it->sp - 1].string;
24586 int n = width;
24588 if (!STRINGP (object))
24589 object = it->w->contents;
24590 #ifdef HAVE_WINDOW_SYSTEM
24591 if (FRAME_WINDOW_P (it->f))
24592 append_stretch_glyph (it, object, width, height, ascent);
24593 else
24594 #endif
24596 it->object = object;
24597 it->char_to_display = ' ';
24598 it->pixel_width = it->len = 1;
24599 while (n--)
24600 tty_append_glyph (it);
24601 it->object = o_object;
24605 it->pixel_width = width;
24606 #ifdef HAVE_WINDOW_SYSTEM
24607 if (FRAME_WINDOW_P (it->f))
24609 it->ascent = it->phys_ascent = ascent;
24610 it->descent = it->phys_descent = height - it->ascent;
24611 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24612 take_vertical_position_into_account (it);
24614 else
24615 #endif
24616 it->nglyphs = width;
24619 /* Get information about special display element WHAT in an
24620 environment described by IT. WHAT is one of IT_TRUNCATION or
24621 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24622 non-null glyph_row member. This function ensures that fields like
24623 face_id, c, len of IT are left untouched. */
24625 static void
24626 produce_special_glyphs (struct it *it, enum display_element_type what)
24628 struct it temp_it;
24629 Lisp_Object gc;
24630 GLYPH glyph;
24632 temp_it = *it;
24633 temp_it.object = make_number (0);
24634 memset (&temp_it.current, 0, sizeof temp_it.current);
24636 if (what == IT_CONTINUATION)
24638 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24639 if (it->bidi_it.paragraph_dir == R2L)
24640 SET_GLYPH_FROM_CHAR (glyph, '/');
24641 else
24642 SET_GLYPH_FROM_CHAR (glyph, '\\');
24643 if (it->dp
24644 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24646 /* FIXME: Should we mirror GC for R2L lines? */
24647 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24648 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24651 else if (what == IT_TRUNCATION)
24653 /* Truncation glyph. */
24654 SET_GLYPH_FROM_CHAR (glyph, '$');
24655 if (it->dp
24656 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24658 /* FIXME: Should we mirror GC for R2L lines? */
24659 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24660 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24663 else
24664 emacs_abort ();
24666 #ifdef HAVE_WINDOW_SYSTEM
24667 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24668 is turned off, we precede the truncation/continuation glyphs by a
24669 stretch glyph whose width is computed such that these special
24670 glyphs are aligned at the window margin, even when very different
24671 fonts are used in different glyph rows. */
24672 if (FRAME_WINDOW_P (temp_it.f)
24673 /* init_iterator calls this with it->glyph_row == NULL, and it
24674 wants only the pixel width of the truncation/continuation
24675 glyphs. */
24676 && temp_it.glyph_row
24677 /* insert_left_trunc_glyphs calls us at the beginning of the
24678 row, and it has its own calculation of the stretch glyph
24679 width. */
24680 && temp_it.glyph_row->used[TEXT_AREA] > 0
24681 && (temp_it.glyph_row->reversed_p
24682 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24683 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24685 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24687 if (stretch_width > 0)
24689 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24690 struct font *font =
24691 face->font ? face->font : FRAME_FONT (temp_it.f);
24692 int stretch_ascent =
24693 (((temp_it.ascent + temp_it.descent)
24694 * FONT_BASE (font)) / FONT_HEIGHT (font));
24696 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24697 temp_it.ascent + temp_it.descent,
24698 stretch_ascent);
24701 #endif
24703 temp_it.dp = NULL;
24704 temp_it.what = IT_CHARACTER;
24705 temp_it.len = 1;
24706 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24707 temp_it.face_id = GLYPH_FACE (glyph);
24708 temp_it.len = CHAR_BYTES (temp_it.c);
24710 PRODUCE_GLYPHS (&temp_it);
24711 it->pixel_width = temp_it.pixel_width;
24712 it->nglyphs = temp_it.pixel_width;
24715 #ifdef HAVE_WINDOW_SYSTEM
24717 /* Calculate line-height and line-spacing properties.
24718 An integer value specifies explicit pixel value.
24719 A float value specifies relative value to current face height.
24720 A cons (float . face-name) specifies relative value to
24721 height of specified face font.
24723 Returns height in pixels, or nil. */
24726 static Lisp_Object
24727 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24728 int boff, int override)
24730 Lisp_Object face_name = Qnil;
24731 int ascent, descent, height;
24733 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24734 return val;
24736 if (CONSP (val))
24738 face_name = XCAR (val);
24739 val = XCDR (val);
24740 if (!NUMBERP (val))
24741 val = make_number (1);
24742 if (NILP (face_name))
24744 height = it->ascent + it->descent;
24745 goto scale;
24749 if (NILP (face_name))
24751 font = FRAME_FONT (it->f);
24752 boff = FRAME_BASELINE_OFFSET (it->f);
24754 else if (EQ (face_name, Qt))
24756 override = 0;
24758 else
24760 int face_id;
24761 struct face *face;
24763 face_id = lookup_named_face (it->f, face_name, 0);
24764 if (face_id < 0)
24765 return make_number (-1);
24767 face = FACE_FROM_ID (it->f, face_id);
24768 font = face->font;
24769 if (font == NULL)
24770 return make_number (-1);
24771 boff = font->baseline_offset;
24772 if (font->vertical_centering)
24773 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24776 ascent = FONT_BASE (font) + boff;
24777 descent = FONT_DESCENT (font) - boff;
24779 if (override)
24781 it->override_ascent = ascent;
24782 it->override_descent = descent;
24783 it->override_boff = boff;
24786 height = ascent + descent;
24788 scale:
24789 if (FLOATP (val))
24790 height = (int)(XFLOAT_DATA (val) * height);
24791 else if (INTEGERP (val))
24792 height *= XINT (val);
24794 return make_number (height);
24798 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24799 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24800 and only if this is for a character for which no font was found.
24802 If the display method (it->glyphless_method) is
24803 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24804 length of the acronym or the hexadecimal string, UPPER_XOFF and
24805 UPPER_YOFF are pixel offsets for the upper part of the string,
24806 LOWER_XOFF and LOWER_YOFF are for the lower part.
24808 For the other display methods, LEN through LOWER_YOFF are zero. */
24810 static void
24811 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24812 short upper_xoff, short upper_yoff,
24813 short lower_xoff, short lower_yoff)
24815 struct glyph *glyph;
24816 enum glyph_row_area area = it->area;
24818 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24819 if (glyph < it->glyph_row->glyphs[area + 1])
24821 /* If the glyph row is reversed, we need to prepend the glyph
24822 rather than append it. */
24823 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24825 struct glyph *g;
24827 /* Make room for the additional glyph. */
24828 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24829 g[1] = *g;
24830 glyph = it->glyph_row->glyphs[area];
24832 glyph->charpos = CHARPOS (it->position);
24833 glyph->object = it->object;
24834 glyph->pixel_width = it->pixel_width;
24835 glyph->ascent = it->ascent;
24836 glyph->descent = it->descent;
24837 glyph->voffset = it->voffset;
24838 glyph->type = GLYPHLESS_GLYPH;
24839 glyph->u.glyphless.method = it->glyphless_method;
24840 glyph->u.glyphless.for_no_font = for_no_font;
24841 glyph->u.glyphless.len = len;
24842 glyph->u.glyphless.ch = it->c;
24843 glyph->slice.glyphless.upper_xoff = upper_xoff;
24844 glyph->slice.glyphless.upper_yoff = upper_yoff;
24845 glyph->slice.glyphless.lower_xoff = lower_xoff;
24846 glyph->slice.glyphless.lower_yoff = lower_yoff;
24847 glyph->avoid_cursor_p = it->avoid_cursor_p;
24848 glyph->multibyte_p = it->multibyte_p;
24849 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24851 /* In R2L rows, the left and the right box edges need to be
24852 drawn in reverse direction. */
24853 glyph->right_box_line_p = it->start_of_box_run_p;
24854 glyph->left_box_line_p = it->end_of_box_run_p;
24856 else
24858 glyph->left_box_line_p = it->start_of_box_run_p;
24859 glyph->right_box_line_p = it->end_of_box_run_p;
24861 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24862 || it->phys_descent > it->descent);
24863 glyph->padding_p = 0;
24864 glyph->glyph_not_available_p = 0;
24865 glyph->face_id = face_id;
24866 glyph->font_type = FONT_TYPE_UNKNOWN;
24867 if (it->bidi_p)
24869 glyph->resolved_level = it->bidi_it.resolved_level;
24870 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24871 emacs_abort ();
24872 glyph->bidi_type = it->bidi_it.type;
24874 ++it->glyph_row->used[area];
24876 else
24877 IT_EXPAND_MATRIX_WIDTH (it, area);
24881 /* Produce a glyph for a glyphless character for iterator IT.
24882 IT->glyphless_method specifies which method to use for displaying
24883 the character. See the description of enum
24884 glyphless_display_method in dispextern.h for the detail.
24886 FOR_NO_FONT is nonzero if and only if this is for a character for
24887 which no font was found. ACRONYM, if non-nil, is an acronym string
24888 for the character. */
24890 static void
24891 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24893 int face_id;
24894 struct face *face;
24895 struct font *font;
24896 int base_width, base_height, width, height;
24897 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24898 int len;
24900 /* Get the metrics of the base font. We always refer to the current
24901 ASCII face. */
24902 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24903 font = face->font ? face->font : FRAME_FONT (it->f);
24904 it->ascent = FONT_BASE (font) + font->baseline_offset;
24905 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24906 base_height = it->ascent + it->descent;
24907 base_width = font->average_width;
24909 face_id = merge_glyphless_glyph_face (it);
24911 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24913 it->pixel_width = THIN_SPACE_WIDTH;
24914 len = 0;
24915 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24917 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24919 width = CHAR_WIDTH (it->c);
24920 if (width == 0)
24921 width = 1;
24922 else if (width > 4)
24923 width = 4;
24924 it->pixel_width = base_width * width;
24925 len = 0;
24926 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24928 else
24930 char buf[7];
24931 const char *str;
24932 unsigned int code[6];
24933 int upper_len;
24934 int ascent, descent;
24935 struct font_metrics metrics_upper, metrics_lower;
24937 face = FACE_FROM_ID (it->f, face_id);
24938 font = face->font ? face->font : FRAME_FONT (it->f);
24939 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24941 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24943 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24944 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24945 if (CONSP (acronym))
24946 acronym = XCAR (acronym);
24947 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24949 else
24951 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24952 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24953 str = buf;
24955 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24956 code[len] = font->driver->encode_char (font, str[len]);
24957 upper_len = (len + 1) / 2;
24958 font->driver->text_extents (font, code, upper_len,
24959 &metrics_upper);
24960 font->driver->text_extents (font, code + upper_len, len - upper_len,
24961 &metrics_lower);
24965 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24966 width = max (metrics_upper.width, metrics_lower.width) + 4;
24967 upper_xoff = upper_yoff = 2; /* the typical case */
24968 if (base_width >= width)
24970 /* Align the upper to the left, the lower to the right. */
24971 it->pixel_width = base_width;
24972 lower_xoff = base_width - 2 - metrics_lower.width;
24974 else
24976 /* Center the shorter one. */
24977 it->pixel_width = width;
24978 if (metrics_upper.width >= metrics_lower.width)
24979 lower_xoff = (width - metrics_lower.width) / 2;
24980 else
24982 /* FIXME: This code doesn't look right. It formerly was
24983 missing the "lower_xoff = 0;", which couldn't have
24984 been right since it left lower_xoff uninitialized. */
24985 lower_xoff = 0;
24986 upper_xoff = (width - metrics_upper.width) / 2;
24990 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24991 top, bottom, and between upper and lower strings. */
24992 height = (metrics_upper.ascent + metrics_upper.descent
24993 + metrics_lower.ascent + metrics_lower.descent) + 5;
24994 /* Center vertically.
24995 H:base_height, D:base_descent
24996 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24998 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24999 descent = D - H/2 + h/2;
25000 lower_yoff = descent - 2 - ld;
25001 upper_yoff = lower_yoff - la - 1 - ud; */
25002 ascent = - (it->descent - (base_height + height + 1) / 2);
25003 descent = it->descent - (base_height - height) / 2;
25004 lower_yoff = descent - 2 - metrics_lower.descent;
25005 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25006 - metrics_upper.descent);
25007 /* Don't make the height shorter than the base height. */
25008 if (height > base_height)
25010 it->ascent = ascent;
25011 it->descent = descent;
25015 it->phys_ascent = it->ascent;
25016 it->phys_descent = it->descent;
25017 if (it->glyph_row)
25018 append_glyphless_glyph (it, face_id, for_no_font, len,
25019 upper_xoff, upper_yoff,
25020 lower_xoff, lower_yoff);
25021 it->nglyphs = 1;
25022 take_vertical_position_into_account (it);
25026 /* RIF:
25027 Produce glyphs/get display metrics for the display element IT is
25028 loaded with. See the description of struct it in dispextern.h
25029 for an overview of struct it. */
25031 void
25032 x_produce_glyphs (struct it *it)
25034 int extra_line_spacing = it->extra_line_spacing;
25036 it->glyph_not_available_p = 0;
25038 if (it->what == IT_CHARACTER)
25040 XChar2b char2b;
25041 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25042 struct font *font = face->font;
25043 struct font_metrics *pcm = NULL;
25044 int boff; /* baseline offset */
25046 if (font == NULL)
25048 /* When no suitable font is found, display this character by
25049 the method specified in the first extra slot of
25050 Vglyphless_char_display. */
25051 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25053 eassert (it->what == IT_GLYPHLESS);
25054 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25055 goto done;
25058 boff = font->baseline_offset;
25059 if (font->vertical_centering)
25060 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25062 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25064 int stretched_p;
25066 it->nglyphs = 1;
25068 if (it->override_ascent >= 0)
25070 it->ascent = it->override_ascent;
25071 it->descent = it->override_descent;
25072 boff = it->override_boff;
25074 else
25076 it->ascent = FONT_BASE (font) + boff;
25077 it->descent = FONT_DESCENT (font) - boff;
25080 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25082 pcm = get_per_char_metric (font, &char2b);
25083 if (pcm->width == 0
25084 && pcm->rbearing == 0 && pcm->lbearing == 0)
25085 pcm = NULL;
25088 if (pcm)
25090 it->phys_ascent = pcm->ascent + boff;
25091 it->phys_descent = pcm->descent - boff;
25092 it->pixel_width = pcm->width;
25094 else
25096 it->glyph_not_available_p = 1;
25097 it->phys_ascent = it->ascent;
25098 it->phys_descent = it->descent;
25099 it->pixel_width = font->space_width;
25102 if (it->constrain_row_ascent_descent_p)
25104 if (it->descent > it->max_descent)
25106 it->ascent += it->descent - it->max_descent;
25107 it->descent = it->max_descent;
25109 if (it->ascent > it->max_ascent)
25111 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25112 it->ascent = it->max_ascent;
25114 it->phys_ascent = min (it->phys_ascent, it->ascent);
25115 it->phys_descent = min (it->phys_descent, it->descent);
25116 extra_line_spacing = 0;
25119 /* If this is a space inside a region of text with
25120 `space-width' property, change its width. */
25121 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25122 if (stretched_p)
25123 it->pixel_width *= XFLOATINT (it->space_width);
25125 /* If face has a box, add the box thickness to the character
25126 height. If character has a box line to the left and/or
25127 right, add the box line width to the character's width. */
25128 if (face->box != FACE_NO_BOX)
25130 int thick = face->box_line_width;
25132 if (thick > 0)
25134 it->ascent += thick;
25135 it->descent += thick;
25137 else
25138 thick = -thick;
25140 if (it->start_of_box_run_p)
25141 it->pixel_width += thick;
25142 if (it->end_of_box_run_p)
25143 it->pixel_width += thick;
25146 /* If face has an overline, add the height of the overline
25147 (1 pixel) and a 1 pixel margin to the character height. */
25148 if (face->overline_p)
25149 it->ascent += overline_margin;
25151 if (it->constrain_row_ascent_descent_p)
25153 if (it->ascent > it->max_ascent)
25154 it->ascent = it->max_ascent;
25155 if (it->descent > it->max_descent)
25156 it->descent = it->max_descent;
25159 take_vertical_position_into_account (it);
25161 /* If we have to actually produce glyphs, do it. */
25162 if (it->glyph_row)
25164 if (stretched_p)
25166 /* Translate a space with a `space-width' property
25167 into a stretch glyph. */
25168 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25169 / FONT_HEIGHT (font));
25170 append_stretch_glyph (it, it->object, it->pixel_width,
25171 it->ascent + it->descent, ascent);
25173 else
25174 append_glyph (it);
25176 /* If characters with lbearing or rbearing are displayed
25177 in this line, record that fact in a flag of the
25178 glyph row. This is used to optimize X output code. */
25179 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25180 it->glyph_row->contains_overlapping_glyphs_p = 1;
25182 if (! stretched_p && it->pixel_width == 0)
25183 /* We assure that all visible glyphs have at least 1-pixel
25184 width. */
25185 it->pixel_width = 1;
25187 else if (it->char_to_display == '\n')
25189 /* A newline has no width, but we need the height of the
25190 line. But if previous part of the line sets a height,
25191 don't increase that height */
25193 Lisp_Object height;
25194 Lisp_Object total_height = Qnil;
25196 it->override_ascent = -1;
25197 it->pixel_width = 0;
25198 it->nglyphs = 0;
25200 height = get_it_property (it, Qline_height);
25201 /* Split (line-height total-height) list */
25202 if (CONSP (height)
25203 && CONSP (XCDR (height))
25204 && NILP (XCDR (XCDR (height))))
25206 total_height = XCAR (XCDR (height));
25207 height = XCAR (height);
25209 height = calc_line_height_property (it, height, font, boff, 1);
25211 if (it->override_ascent >= 0)
25213 it->ascent = it->override_ascent;
25214 it->descent = it->override_descent;
25215 boff = it->override_boff;
25217 else
25219 it->ascent = FONT_BASE (font) + boff;
25220 it->descent = FONT_DESCENT (font) - boff;
25223 if (EQ (height, Qt))
25225 if (it->descent > it->max_descent)
25227 it->ascent += it->descent - it->max_descent;
25228 it->descent = it->max_descent;
25230 if (it->ascent > it->max_ascent)
25232 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25233 it->ascent = it->max_ascent;
25235 it->phys_ascent = min (it->phys_ascent, it->ascent);
25236 it->phys_descent = min (it->phys_descent, it->descent);
25237 it->constrain_row_ascent_descent_p = 1;
25238 extra_line_spacing = 0;
25240 else
25242 Lisp_Object spacing;
25244 it->phys_ascent = it->ascent;
25245 it->phys_descent = it->descent;
25247 if ((it->max_ascent > 0 || it->max_descent > 0)
25248 && face->box != FACE_NO_BOX
25249 && face->box_line_width > 0)
25251 it->ascent += face->box_line_width;
25252 it->descent += face->box_line_width;
25254 if (!NILP (height)
25255 && XINT (height) > it->ascent + it->descent)
25256 it->ascent = XINT (height) - it->descent;
25258 if (!NILP (total_height))
25259 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25260 else
25262 spacing = get_it_property (it, Qline_spacing);
25263 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25265 if (INTEGERP (spacing))
25267 extra_line_spacing = XINT (spacing);
25268 if (!NILP (total_height))
25269 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25273 else /* i.e. (it->char_to_display == '\t') */
25275 if (font->space_width > 0)
25277 int tab_width = it->tab_width * font->space_width;
25278 int x = it->current_x + it->continuation_lines_width;
25279 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25281 /* If the distance from the current position to the next tab
25282 stop is less than a space character width, use the
25283 tab stop after that. */
25284 if (next_tab_x - x < font->space_width)
25285 next_tab_x += tab_width;
25287 it->pixel_width = next_tab_x - x;
25288 it->nglyphs = 1;
25289 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25290 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25292 if (it->glyph_row)
25294 append_stretch_glyph (it, it->object, it->pixel_width,
25295 it->ascent + it->descent, it->ascent);
25298 else
25300 it->pixel_width = 0;
25301 it->nglyphs = 1;
25305 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25307 /* A static composition.
25309 Note: A composition is represented as one glyph in the
25310 glyph matrix. There are no padding glyphs.
25312 Important note: pixel_width, ascent, and descent are the
25313 values of what is drawn by draw_glyphs (i.e. the values of
25314 the overall glyphs composed). */
25315 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25316 int boff; /* baseline offset */
25317 struct composition *cmp = composition_table[it->cmp_it.id];
25318 int glyph_len = cmp->glyph_len;
25319 struct font *font = face->font;
25321 it->nglyphs = 1;
25323 /* If we have not yet calculated pixel size data of glyphs of
25324 the composition for the current face font, calculate them
25325 now. Theoretically, we have to check all fonts for the
25326 glyphs, but that requires much time and memory space. So,
25327 here we check only the font of the first glyph. This may
25328 lead to incorrect display, but it's very rare, and C-l
25329 (recenter-top-bottom) can correct the display anyway. */
25330 if (! cmp->font || cmp->font != font)
25332 /* Ascent and descent of the font of the first character
25333 of this composition (adjusted by baseline offset).
25334 Ascent and descent of overall glyphs should not be less
25335 than these, respectively. */
25336 int font_ascent, font_descent, font_height;
25337 /* Bounding box of the overall glyphs. */
25338 int leftmost, rightmost, lowest, highest;
25339 int lbearing, rbearing;
25340 int i, width, ascent, descent;
25341 int left_padded = 0, right_padded = 0;
25342 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25343 XChar2b char2b;
25344 struct font_metrics *pcm;
25345 int font_not_found_p;
25346 ptrdiff_t pos;
25348 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25349 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25350 break;
25351 if (glyph_len < cmp->glyph_len)
25352 right_padded = 1;
25353 for (i = 0; i < glyph_len; i++)
25355 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25356 break;
25357 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25359 if (i > 0)
25360 left_padded = 1;
25362 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25363 : IT_CHARPOS (*it));
25364 /* If no suitable font is found, use the default font. */
25365 font_not_found_p = font == NULL;
25366 if (font_not_found_p)
25368 face = face->ascii_face;
25369 font = face->font;
25371 boff = font->baseline_offset;
25372 if (font->vertical_centering)
25373 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25374 font_ascent = FONT_BASE (font) + boff;
25375 font_descent = FONT_DESCENT (font) - boff;
25376 font_height = FONT_HEIGHT (font);
25378 cmp->font = font;
25380 pcm = NULL;
25381 if (! font_not_found_p)
25383 get_char_face_and_encoding (it->f, c, it->face_id,
25384 &char2b, 0);
25385 pcm = get_per_char_metric (font, &char2b);
25388 /* Initialize the bounding box. */
25389 if (pcm)
25391 width = cmp->glyph_len > 0 ? pcm->width : 0;
25392 ascent = pcm->ascent;
25393 descent = pcm->descent;
25394 lbearing = pcm->lbearing;
25395 rbearing = pcm->rbearing;
25397 else
25399 width = cmp->glyph_len > 0 ? font->space_width : 0;
25400 ascent = FONT_BASE (font);
25401 descent = FONT_DESCENT (font);
25402 lbearing = 0;
25403 rbearing = width;
25406 rightmost = width;
25407 leftmost = 0;
25408 lowest = - descent + boff;
25409 highest = ascent + boff;
25411 if (! font_not_found_p
25412 && font->default_ascent
25413 && CHAR_TABLE_P (Vuse_default_ascent)
25414 && !NILP (Faref (Vuse_default_ascent,
25415 make_number (it->char_to_display))))
25416 highest = font->default_ascent + boff;
25418 /* Draw the first glyph at the normal position. It may be
25419 shifted to right later if some other glyphs are drawn
25420 at the left. */
25421 cmp->offsets[i * 2] = 0;
25422 cmp->offsets[i * 2 + 1] = boff;
25423 cmp->lbearing = lbearing;
25424 cmp->rbearing = rbearing;
25426 /* Set cmp->offsets for the remaining glyphs. */
25427 for (i++; i < glyph_len; i++)
25429 int left, right, btm, top;
25430 int ch = COMPOSITION_GLYPH (cmp, i);
25431 int face_id;
25432 struct face *this_face;
25434 if (ch == '\t')
25435 ch = ' ';
25436 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25437 this_face = FACE_FROM_ID (it->f, face_id);
25438 font = this_face->font;
25440 if (font == NULL)
25441 pcm = NULL;
25442 else
25444 get_char_face_and_encoding (it->f, ch, face_id,
25445 &char2b, 0);
25446 pcm = get_per_char_metric (font, &char2b);
25448 if (! pcm)
25449 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25450 else
25452 width = pcm->width;
25453 ascent = pcm->ascent;
25454 descent = pcm->descent;
25455 lbearing = pcm->lbearing;
25456 rbearing = pcm->rbearing;
25457 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25459 /* Relative composition with or without
25460 alternate chars. */
25461 left = (leftmost + rightmost - width) / 2;
25462 btm = - descent + boff;
25463 if (font->relative_compose
25464 && (! CHAR_TABLE_P (Vignore_relative_composition)
25465 || NILP (Faref (Vignore_relative_composition,
25466 make_number (ch)))))
25469 if (- descent >= font->relative_compose)
25470 /* One extra pixel between two glyphs. */
25471 btm = highest + 1;
25472 else if (ascent <= 0)
25473 /* One extra pixel between two glyphs. */
25474 btm = lowest - 1 - ascent - descent;
25477 else
25479 /* A composition rule is specified by an integer
25480 value that encodes global and new reference
25481 points (GREF and NREF). GREF and NREF are
25482 specified by numbers as below:
25484 0---1---2 -- ascent
25488 9--10--11 -- center
25490 ---3---4---5--- baseline
25492 6---7---8 -- descent
25494 int rule = COMPOSITION_RULE (cmp, i);
25495 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25497 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25498 grefx = gref % 3, nrefx = nref % 3;
25499 grefy = gref / 3, nrefy = nref / 3;
25500 if (xoff)
25501 xoff = font_height * (xoff - 128) / 256;
25502 if (yoff)
25503 yoff = font_height * (yoff - 128) / 256;
25505 left = (leftmost
25506 + grefx * (rightmost - leftmost) / 2
25507 - nrefx * width / 2
25508 + xoff);
25510 btm = ((grefy == 0 ? highest
25511 : grefy == 1 ? 0
25512 : grefy == 2 ? lowest
25513 : (highest + lowest) / 2)
25514 - (nrefy == 0 ? ascent + descent
25515 : nrefy == 1 ? descent - boff
25516 : nrefy == 2 ? 0
25517 : (ascent + descent) / 2)
25518 + yoff);
25521 cmp->offsets[i * 2] = left;
25522 cmp->offsets[i * 2 + 1] = btm + descent;
25524 /* Update the bounding box of the overall glyphs. */
25525 if (width > 0)
25527 right = left + width;
25528 if (left < leftmost)
25529 leftmost = left;
25530 if (right > rightmost)
25531 rightmost = right;
25533 top = btm + descent + ascent;
25534 if (top > highest)
25535 highest = top;
25536 if (btm < lowest)
25537 lowest = btm;
25539 if (cmp->lbearing > left + lbearing)
25540 cmp->lbearing = left + lbearing;
25541 if (cmp->rbearing < left + rbearing)
25542 cmp->rbearing = left + rbearing;
25546 /* If there are glyphs whose x-offsets are negative,
25547 shift all glyphs to the right and make all x-offsets
25548 non-negative. */
25549 if (leftmost < 0)
25551 for (i = 0; i < cmp->glyph_len; i++)
25552 cmp->offsets[i * 2] -= leftmost;
25553 rightmost -= leftmost;
25554 cmp->lbearing -= leftmost;
25555 cmp->rbearing -= leftmost;
25558 if (left_padded && cmp->lbearing < 0)
25560 for (i = 0; i < cmp->glyph_len; i++)
25561 cmp->offsets[i * 2] -= cmp->lbearing;
25562 rightmost -= cmp->lbearing;
25563 cmp->rbearing -= cmp->lbearing;
25564 cmp->lbearing = 0;
25566 if (right_padded && rightmost < cmp->rbearing)
25568 rightmost = cmp->rbearing;
25571 cmp->pixel_width = rightmost;
25572 cmp->ascent = highest;
25573 cmp->descent = - lowest;
25574 if (cmp->ascent < font_ascent)
25575 cmp->ascent = font_ascent;
25576 if (cmp->descent < font_descent)
25577 cmp->descent = font_descent;
25580 if (it->glyph_row
25581 && (cmp->lbearing < 0
25582 || cmp->rbearing > cmp->pixel_width))
25583 it->glyph_row->contains_overlapping_glyphs_p = 1;
25585 it->pixel_width = cmp->pixel_width;
25586 it->ascent = it->phys_ascent = cmp->ascent;
25587 it->descent = it->phys_descent = cmp->descent;
25588 if (face->box != FACE_NO_BOX)
25590 int thick = face->box_line_width;
25592 if (thick > 0)
25594 it->ascent += thick;
25595 it->descent += thick;
25597 else
25598 thick = - thick;
25600 if (it->start_of_box_run_p)
25601 it->pixel_width += thick;
25602 if (it->end_of_box_run_p)
25603 it->pixel_width += thick;
25606 /* If face has an overline, add the height of the overline
25607 (1 pixel) and a 1 pixel margin to the character height. */
25608 if (face->overline_p)
25609 it->ascent += overline_margin;
25611 take_vertical_position_into_account (it);
25612 if (it->ascent < 0)
25613 it->ascent = 0;
25614 if (it->descent < 0)
25615 it->descent = 0;
25617 if (it->glyph_row && cmp->glyph_len > 0)
25618 append_composite_glyph (it);
25620 else if (it->what == IT_COMPOSITION)
25622 /* A dynamic (automatic) composition. */
25623 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25624 Lisp_Object gstring;
25625 struct font_metrics metrics;
25627 it->nglyphs = 1;
25629 gstring = composition_gstring_from_id (it->cmp_it.id);
25630 it->pixel_width
25631 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25632 &metrics);
25633 if (it->glyph_row
25634 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25635 it->glyph_row->contains_overlapping_glyphs_p = 1;
25636 it->ascent = it->phys_ascent = metrics.ascent;
25637 it->descent = it->phys_descent = metrics.descent;
25638 if (face->box != FACE_NO_BOX)
25640 int thick = face->box_line_width;
25642 if (thick > 0)
25644 it->ascent += thick;
25645 it->descent += thick;
25647 else
25648 thick = - thick;
25650 if (it->start_of_box_run_p)
25651 it->pixel_width += thick;
25652 if (it->end_of_box_run_p)
25653 it->pixel_width += thick;
25655 /* If face has an overline, add the height of the overline
25656 (1 pixel) and a 1 pixel margin to the character height. */
25657 if (face->overline_p)
25658 it->ascent += overline_margin;
25659 take_vertical_position_into_account (it);
25660 if (it->ascent < 0)
25661 it->ascent = 0;
25662 if (it->descent < 0)
25663 it->descent = 0;
25665 if (it->glyph_row)
25666 append_composite_glyph (it);
25668 else if (it->what == IT_GLYPHLESS)
25669 produce_glyphless_glyph (it, 0, Qnil);
25670 else if (it->what == IT_IMAGE)
25671 produce_image_glyph (it);
25672 else if (it->what == IT_STRETCH)
25673 produce_stretch_glyph (it);
25675 done:
25676 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25677 because this isn't true for images with `:ascent 100'. */
25678 eassert (it->ascent >= 0 && it->descent >= 0);
25679 if (it->area == TEXT_AREA)
25680 it->current_x += it->pixel_width;
25682 if (extra_line_spacing > 0)
25684 it->descent += extra_line_spacing;
25685 if (extra_line_spacing > it->max_extra_line_spacing)
25686 it->max_extra_line_spacing = extra_line_spacing;
25689 it->max_ascent = max (it->max_ascent, it->ascent);
25690 it->max_descent = max (it->max_descent, it->descent);
25691 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25692 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25695 /* EXPORT for RIF:
25696 Output LEN glyphs starting at START at the nominal cursor position.
25697 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25698 being updated, and UPDATED_AREA is the area of that row being updated. */
25700 void
25701 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25702 struct glyph *start, enum glyph_row_area updated_area, int len)
25704 int x, hpos, chpos = w->phys_cursor.hpos;
25706 eassert (updated_row);
25707 /* When the window is hscrolled, cursor hpos can legitimately be out
25708 of bounds, but we draw the cursor at the corresponding window
25709 margin in that case. */
25710 if (!updated_row->reversed_p && chpos < 0)
25711 chpos = 0;
25712 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25713 chpos = updated_row->used[TEXT_AREA] - 1;
25715 block_input ();
25717 /* Write glyphs. */
25719 hpos = start - updated_row->glyphs[updated_area];
25720 x = draw_glyphs (w, w->output_cursor.x,
25721 updated_row, updated_area,
25722 hpos, hpos + len,
25723 DRAW_NORMAL_TEXT, 0);
25725 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25726 if (updated_area == TEXT_AREA
25727 && w->phys_cursor_on_p
25728 && w->phys_cursor.vpos == w->output_cursor.vpos
25729 && chpos >= hpos
25730 && chpos < hpos + len)
25731 w->phys_cursor_on_p = 0;
25733 unblock_input ();
25735 /* Advance the output cursor. */
25736 w->output_cursor.hpos += len;
25737 w->output_cursor.x = x;
25741 /* EXPORT for RIF:
25742 Insert LEN glyphs from START at the nominal cursor position. */
25744 void
25745 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25746 struct glyph *start, enum glyph_row_area updated_area, int len)
25748 struct frame *f;
25749 int line_height, shift_by_width, shifted_region_width;
25750 struct glyph_row *row;
25751 struct glyph *glyph;
25752 int frame_x, frame_y;
25753 ptrdiff_t hpos;
25755 eassert (updated_row);
25756 block_input ();
25757 f = XFRAME (WINDOW_FRAME (w));
25759 /* Get the height of the line we are in. */
25760 row = updated_row;
25761 line_height = row->height;
25763 /* Get the width of the glyphs to insert. */
25764 shift_by_width = 0;
25765 for (glyph = start; glyph < start + len; ++glyph)
25766 shift_by_width += glyph->pixel_width;
25768 /* Get the width of the region to shift right. */
25769 shifted_region_width = (window_box_width (w, updated_area)
25770 - w->output_cursor.x
25771 - shift_by_width);
25773 /* Shift right. */
25774 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25775 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25777 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25778 line_height, shift_by_width);
25780 /* Write the glyphs. */
25781 hpos = start - row->glyphs[updated_area];
25782 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25783 hpos, hpos + len,
25784 DRAW_NORMAL_TEXT, 0);
25786 /* Advance the output cursor. */
25787 w->output_cursor.hpos += len;
25788 w->output_cursor.x += shift_by_width;
25789 unblock_input ();
25793 /* EXPORT for RIF:
25794 Erase the current text line from the nominal cursor position
25795 (inclusive) to pixel column TO_X (exclusive). The idea is that
25796 everything from TO_X onward is already erased.
25798 TO_X is a pixel position relative to UPDATED_AREA of currently
25799 updated window W. TO_X == -1 means clear to the end of this area. */
25801 void
25802 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25803 enum glyph_row_area updated_area, int to_x)
25805 struct frame *f;
25806 int max_x, min_y, max_y;
25807 int from_x, from_y, to_y;
25809 eassert (updated_row);
25810 f = XFRAME (w->frame);
25812 if (updated_row->full_width_p)
25813 max_x = WINDOW_TOTAL_WIDTH (w);
25814 else
25815 max_x = window_box_width (w, updated_area);
25816 max_y = window_text_bottom_y (w);
25818 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25819 of window. For TO_X > 0, truncate to end of drawing area. */
25820 if (to_x == 0)
25821 return;
25822 else if (to_x < 0)
25823 to_x = max_x;
25824 else
25825 to_x = min (to_x, max_x);
25827 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25829 /* Notice if the cursor will be cleared by this operation. */
25830 if (!updated_row->full_width_p)
25831 notice_overwritten_cursor (w, updated_area,
25832 w->output_cursor.x, -1,
25833 updated_row->y,
25834 MATRIX_ROW_BOTTOM_Y (updated_row));
25836 from_x = w->output_cursor.x;
25838 /* Translate to frame coordinates. */
25839 if (updated_row->full_width_p)
25841 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25842 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25844 else
25846 int area_left = window_box_left (w, updated_area);
25847 from_x += area_left;
25848 to_x += area_left;
25851 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25852 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
25853 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25855 /* Prevent inadvertently clearing to end of the X window. */
25856 if (to_x > from_x && to_y > from_y)
25858 block_input ();
25859 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25860 to_x - from_x, to_y - from_y);
25861 unblock_input ();
25865 #endif /* HAVE_WINDOW_SYSTEM */
25869 /***********************************************************************
25870 Cursor types
25871 ***********************************************************************/
25873 /* Value is the internal representation of the specified cursor type
25874 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25875 of the bar cursor. */
25877 static enum text_cursor_kinds
25878 get_specified_cursor_type (Lisp_Object arg, int *width)
25880 enum text_cursor_kinds type;
25882 if (NILP (arg))
25883 return NO_CURSOR;
25885 if (EQ (arg, Qbox))
25886 return FILLED_BOX_CURSOR;
25888 if (EQ (arg, Qhollow))
25889 return HOLLOW_BOX_CURSOR;
25891 if (EQ (arg, Qbar))
25893 *width = 2;
25894 return BAR_CURSOR;
25897 if (CONSP (arg)
25898 && EQ (XCAR (arg), Qbar)
25899 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25901 *width = XINT (XCDR (arg));
25902 return BAR_CURSOR;
25905 if (EQ (arg, Qhbar))
25907 *width = 2;
25908 return HBAR_CURSOR;
25911 if (CONSP (arg)
25912 && EQ (XCAR (arg), Qhbar)
25913 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25915 *width = XINT (XCDR (arg));
25916 return HBAR_CURSOR;
25919 /* Treat anything unknown as "hollow box cursor".
25920 It was bad to signal an error; people have trouble fixing
25921 .Xdefaults with Emacs, when it has something bad in it. */
25922 type = HOLLOW_BOX_CURSOR;
25924 return type;
25927 /* Set the default cursor types for specified frame. */
25928 void
25929 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25931 int width = 1;
25932 Lisp_Object tem;
25934 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25935 FRAME_CURSOR_WIDTH (f) = width;
25937 /* By default, set up the blink-off state depending on the on-state. */
25939 tem = Fassoc (arg, Vblink_cursor_alist);
25940 if (!NILP (tem))
25942 FRAME_BLINK_OFF_CURSOR (f)
25943 = get_specified_cursor_type (XCDR (tem), &width);
25944 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25946 else
25947 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25949 /* Make sure the cursor gets redrawn. */
25950 f->cursor_type_changed = 1;
25954 #ifdef HAVE_WINDOW_SYSTEM
25956 /* Return the cursor we want to be displayed in window W. Return
25957 width of bar/hbar cursor through WIDTH arg. Return with
25958 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25959 (i.e. if the `system caret' should track this cursor).
25961 In a mini-buffer window, we want the cursor only to appear if we
25962 are reading input from this window. For the selected window, we
25963 want the cursor type given by the frame parameter or buffer local
25964 setting of cursor-type. If explicitly marked off, draw no cursor.
25965 In all other cases, we want a hollow box cursor. */
25967 static enum text_cursor_kinds
25968 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25969 int *active_cursor)
25971 struct frame *f = XFRAME (w->frame);
25972 struct buffer *b = XBUFFER (w->contents);
25973 int cursor_type = DEFAULT_CURSOR;
25974 Lisp_Object alt_cursor;
25975 int non_selected = 0;
25977 *active_cursor = 1;
25979 /* Echo area */
25980 if (cursor_in_echo_area
25981 && FRAME_HAS_MINIBUF_P (f)
25982 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25984 if (w == XWINDOW (echo_area_window))
25986 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25988 *width = FRAME_CURSOR_WIDTH (f);
25989 return FRAME_DESIRED_CURSOR (f);
25991 else
25992 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25995 *active_cursor = 0;
25996 non_selected = 1;
25999 /* Detect a nonselected window or nonselected frame. */
26000 else if (w != XWINDOW (f->selected_window)
26001 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26003 *active_cursor = 0;
26005 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26006 return NO_CURSOR;
26008 non_selected = 1;
26011 /* Never display a cursor in a window in which cursor-type is nil. */
26012 if (NILP (BVAR (b, cursor_type)))
26013 return NO_CURSOR;
26015 /* Get the normal cursor type for this window. */
26016 if (EQ (BVAR (b, cursor_type), Qt))
26018 cursor_type = FRAME_DESIRED_CURSOR (f);
26019 *width = FRAME_CURSOR_WIDTH (f);
26021 else
26022 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26024 /* Use cursor-in-non-selected-windows instead
26025 for non-selected window or frame. */
26026 if (non_selected)
26028 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26029 if (!EQ (Qt, alt_cursor))
26030 return get_specified_cursor_type (alt_cursor, width);
26031 /* t means modify the normal cursor type. */
26032 if (cursor_type == FILLED_BOX_CURSOR)
26033 cursor_type = HOLLOW_BOX_CURSOR;
26034 else if (cursor_type == BAR_CURSOR && *width > 1)
26035 --*width;
26036 return cursor_type;
26039 /* Use normal cursor if not blinked off. */
26040 if (!w->cursor_off_p)
26042 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26044 if (cursor_type == FILLED_BOX_CURSOR)
26046 /* Using a block cursor on large images can be very annoying.
26047 So use a hollow cursor for "large" images.
26048 If image is not transparent (no mask), also use hollow cursor. */
26049 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26050 if (img != NULL && IMAGEP (img->spec))
26052 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26053 where N = size of default frame font size.
26054 This should cover most of the "tiny" icons people may use. */
26055 if (!img->mask
26056 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26057 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26058 cursor_type = HOLLOW_BOX_CURSOR;
26061 else if (cursor_type != NO_CURSOR)
26063 /* Display current only supports BOX and HOLLOW cursors for images.
26064 So for now, unconditionally use a HOLLOW cursor when cursor is
26065 not a solid box cursor. */
26066 cursor_type = HOLLOW_BOX_CURSOR;
26069 return cursor_type;
26072 /* Cursor is blinked off, so determine how to "toggle" it. */
26074 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26075 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26076 return get_specified_cursor_type (XCDR (alt_cursor), width);
26078 /* Then see if frame has specified a specific blink off cursor type. */
26079 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26081 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26082 return FRAME_BLINK_OFF_CURSOR (f);
26085 #if 0
26086 /* Some people liked having a permanently visible blinking cursor,
26087 while others had very strong opinions against it. So it was
26088 decided to remove it. KFS 2003-09-03 */
26090 /* Finally perform built-in cursor blinking:
26091 filled box <-> hollow box
26092 wide [h]bar <-> narrow [h]bar
26093 narrow [h]bar <-> no cursor
26094 other type <-> no cursor */
26096 if (cursor_type == FILLED_BOX_CURSOR)
26097 return HOLLOW_BOX_CURSOR;
26099 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26101 *width = 1;
26102 return cursor_type;
26104 #endif
26106 return NO_CURSOR;
26110 /* Notice when the text cursor of window W has been completely
26111 overwritten by a drawing operation that outputs glyphs in AREA
26112 starting at X0 and ending at X1 in the line starting at Y0 and
26113 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26114 the rest of the line after X0 has been written. Y coordinates
26115 are window-relative. */
26117 static void
26118 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26119 int x0, int x1, int y0, int y1)
26121 int cx0, cx1, cy0, cy1;
26122 struct glyph_row *row;
26124 if (!w->phys_cursor_on_p)
26125 return;
26126 if (area != TEXT_AREA)
26127 return;
26129 if (w->phys_cursor.vpos < 0
26130 || w->phys_cursor.vpos >= w->current_matrix->nrows
26131 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26132 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26133 return;
26135 if (row->cursor_in_fringe_p)
26137 row->cursor_in_fringe_p = 0;
26138 draw_fringe_bitmap (w, row, row->reversed_p);
26139 w->phys_cursor_on_p = 0;
26140 return;
26143 cx0 = w->phys_cursor.x;
26144 cx1 = cx0 + w->phys_cursor_width;
26145 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26146 return;
26148 /* The cursor image will be completely removed from the
26149 screen if the output area intersects the cursor area in
26150 y-direction. When we draw in [y0 y1[, and some part of
26151 the cursor is at y < y0, that part must have been drawn
26152 before. When scrolling, the cursor is erased before
26153 actually scrolling, so we don't come here. When not
26154 scrolling, the rows above the old cursor row must have
26155 changed, and in this case these rows must have written
26156 over the cursor image.
26158 Likewise if part of the cursor is below y1, with the
26159 exception of the cursor being in the first blank row at
26160 the buffer and window end because update_text_area
26161 doesn't draw that row. (Except when it does, but
26162 that's handled in update_text_area.) */
26164 cy0 = w->phys_cursor.y;
26165 cy1 = cy0 + w->phys_cursor_height;
26166 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26167 return;
26169 w->phys_cursor_on_p = 0;
26172 #endif /* HAVE_WINDOW_SYSTEM */
26175 /************************************************************************
26176 Mouse Face
26177 ************************************************************************/
26179 #ifdef HAVE_WINDOW_SYSTEM
26181 /* EXPORT for RIF:
26182 Fix the display of area AREA of overlapping row ROW in window W
26183 with respect to the overlapping part OVERLAPS. */
26185 void
26186 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26187 enum glyph_row_area area, int overlaps)
26189 int i, x;
26191 block_input ();
26193 x = 0;
26194 for (i = 0; i < row->used[area];)
26196 if (row->glyphs[area][i].overlaps_vertically_p)
26198 int start = i, start_x = x;
26202 x += row->glyphs[area][i].pixel_width;
26203 ++i;
26205 while (i < row->used[area]
26206 && row->glyphs[area][i].overlaps_vertically_p);
26208 draw_glyphs (w, start_x, row, area,
26209 start, i,
26210 DRAW_NORMAL_TEXT, overlaps);
26212 else
26214 x += row->glyphs[area][i].pixel_width;
26215 ++i;
26219 unblock_input ();
26223 /* EXPORT:
26224 Draw the cursor glyph of window W in glyph row ROW. See the
26225 comment of draw_glyphs for the meaning of HL. */
26227 void
26228 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26229 enum draw_glyphs_face hl)
26231 /* If cursor hpos is out of bounds, don't draw garbage. This can
26232 happen in mini-buffer windows when switching between echo area
26233 glyphs and mini-buffer. */
26234 if ((row->reversed_p
26235 ? (w->phys_cursor.hpos >= 0)
26236 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26238 int on_p = w->phys_cursor_on_p;
26239 int x1;
26240 int hpos = w->phys_cursor.hpos;
26242 /* When the window is hscrolled, cursor hpos can legitimately be
26243 out of bounds, but we draw the cursor at the corresponding
26244 window margin in that case. */
26245 if (!row->reversed_p && hpos < 0)
26246 hpos = 0;
26247 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26248 hpos = row->used[TEXT_AREA] - 1;
26250 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26251 hl, 0);
26252 w->phys_cursor_on_p = on_p;
26254 if (hl == DRAW_CURSOR)
26255 w->phys_cursor_width = x1 - w->phys_cursor.x;
26256 /* When we erase the cursor, and ROW is overlapped by other
26257 rows, make sure that these overlapping parts of other rows
26258 are redrawn. */
26259 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26261 w->phys_cursor_width = x1 - w->phys_cursor.x;
26263 if (row > w->current_matrix->rows
26264 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26265 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26266 OVERLAPS_ERASED_CURSOR);
26268 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26269 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26270 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26271 OVERLAPS_ERASED_CURSOR);
26277 /* EXPORT:
26278 Erase the image of a cursor of window W from the screen. */
26280 void
26281 erase_phys_cursor (struct window *w)
26283 struct frame *f = XFRAME (w->frame);
26284 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26285 int hpos = w->phys_cursor.hpos;
26286 int vpos = w->phys_cursor.vpos;
26287 int mouse_face_here_p = 0;
26288 struct glyph_matrix *active_glyphs = w->current_matrix;
26289 struct glyph_row *cursor_row;
26290 struct glyph *cursor_glyph;
26291 enum draw_glyphs_face hl;
26293 /* No cursor displayed or row invalidated => nothing to do on the
26294 screen. */
26295 if (w->phys_cursor_type == NO_CURSOR)
26296 goto mark_cursor_off;
26298 /* VPOS >= active_glyphs->nrows means that window has been resized.
26299 Don't bother to erase the cursor. */
26300 if (vpos >= active_glyphs->nrows)
26301 goto mark_cursor_off;
26303 /* If row containing cursor is marked invalid, there is nothing we
26304 can do. */
26305 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26306 if (!cursor_row->enabled_p)
26307 goto mark_cursor_off;
26309 /* If line spacing is > 0, old cursor may only be partially visible in
26310 window after split-window. So adjust visible height. */
26311 cursor_row->visible_height = min (cursor_row->visible_height,
26312 window_text_bottom_y (w) - cursor_row->y);
26314 /* If row is completely invisible, don't attempt to delete a cursor which
26315 isn't there. This can happen if cursor is at top of a window, and
26316 we switch to a buffer with a header line in that window. */
26317 if (cursor_row->visible_height <= 0)
26318 goto mark_cursor_off;
26320 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26321 if (cursor_row->cursor_in_fringe_p)
26323 cursor_row->cursor_in_fringe_p = 0;
26324 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26325 goto mark_cursor_off;
26328 /* This can happen when the new row is shorter than the old one.
26329 In this case, either draw_glyphs or clear_end_of_line
26330 should have cleared the cursor. Note that we wouldn't be
26331 able to erase the cursor in this case because we don't have a
26332 cursor glyph at hand. */
26333 if ((cursor_row->reversed_p
26334 ? (w->phys_cursor.hpos < 0)
26335 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26336 goto mark_cursor_off;
26338 /* When the window is hscrolled, cursor hpos can legitimately be out
26339 of bounds, but we draw the cursor at the corresponding window
26340 margin in that case. */
26341 if (!cursor_row->reversed_p && hpos < 0)
26342 hpos = 0;
26343 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26344 hpos = cursor_row->used[TEXT_AREA] - 1;
26346 /* If the cursor is in the mouse face area, redisplay that when
26347 we clear the cursor. */
26348 if (! NILP (hlinfo->mouse_face_window)
26349 && coords_in_mouse_face_p (w, hpos, vpos)
26350 /* Don't redraw the cursor's spot in mouse face if it is at the
26351 end of a line (on a newline). The cursor appears there, but
26352 mouse highlighting does not. */
26353 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26354 mouse_face_here_p = 1;
26356 /* Maybe clear the display under the cursor. */
26357 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26359 int x, y, left_x;
26360 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26361 int width;
26363 cursor_glyph = get_phys_cursor_glyph (w);
26364 if (cursor_glyph == NULL)
26365 goto mark_cursor_off;
26367 width = cursor_glyph->pixel_width;
26368 left_x = window_box_left_offset (w, TEXT_AREA);
26369 x = w->phys_cursor.x;
26370 if (x < left_x)
26371 width -= left_x - x;
26372 width = min (width, window_box_width (w, TEXT_AREA) - x);
26373 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26374 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26376 if (width > 0)
26377 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26380 /* Erase the cursor by redrawing the character underneath it. */
26381 if (mouse_face_here_p)
26382 hl = DRAW_MOUSE_FACE;
26383 else
26384 hl = DRAW_NORMAL_TEXT;
26385 draw_phys_cursor_glyph (w, cursor_row, hl);
26387 mark_cursor_off:
26388 w->phys_cursor_on_p = 0;
26389 w->phys_cursor_type = NO_CURSOR;
26393 /* EXPORT:
26394 Display or clear cursor of window W. If ON is zero, clear the
26395 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26396 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26398 void
26399 display_and_set_cursor (struct window *w, bool on,
26400 int hpos, int vpos, int x, int y)
26402 struct frame *f = XFRAME (w->frame);
26403 int new_cursor_type;
26404 int new_cursor_width;
26405 int active_cursor;
26406 struct glyph_row *glyph_row;
26407 struct glyph *glyph;
26409 /* This is pointless on invisible frames, and dangerous on garbaged
26410 windows and frames; in the latter case, the frame or window may
26411 be in the midst of changing its size, and x and y may be off the
26412 window. */
26413 if (! FRAME_VISIBLE_P (f)
26414 || FRAME_GARBAGED_P (f)
26415 || vpos >= w->current_matrix->nrows
26416 || hpos >= w->current_matrix->matrix_w)
26417 return;
26419 /* If cursor is off and we want it off, return quickly. */
26420 if (!on && !w->phys_cursor_on_p)
26421 return;
26423 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26424 /* If cursor row is not enabled, we don't really know where to
26425 display the cursor. */
26426 if (!glyph_row->enabled_p)
26428 w->phys_cursor_on_p = 0;
26429 return;
26432 glyph = NULL;
26433 if (!glyph_row->exact_window_width_line_p
26434 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26435 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26437 eassert (input_blocked_p ());
26439 /* Set new_cursor_type to the cursor we want to be displayed. */
26440 new_cursor_type = get_window_cursor_type (w, glyph,
26441 &new_cursor_width, &active_cursor);
26443 /* If cursor is currently being shown and we don't want it to be or
26444 it is in the wrong place, or the cursor type is not what we want,
26445 erase it. */
26446 if (w->phys_cursor_on_p
26447 && (!on
26448 || w->phys_cursor.x != x
26449 || w->phys_cursor.y != y
26450 || new_cursor_type != w->phys_cursor_type
26451 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26452 && new_cursor_width != w->phys_cursor_width)))
26453 erase_phys_cursor (w);
26455 /* Don't check phys_cursor_on_p here because that flag is only set
26456 to zero in some cases where we know that the cursor has been
26457 completely erased, to avoid the extra work of erasing the cursor
26458 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26459 still not be visible, or it has only been partly erased. */
26460 if (on)
26462 w->phys_cursor_ascent = glyph_row->ascent;
26463 w->phys_cursor_height = glyph_row->height;
26465 /* Set phys_cursor_.* before x_draw_.* is called because some
26466 of them may need the information. */
26467 w->phys_cursor.x = x;
26468 w->phys_cursor.y = glyph_row->y;
26469 w->phys_cursor.hpos = hpos;
26470 w->phys_cursor.vpos = vpos;
26473 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26474 new_cursor_type, new_cursor_width,
26475 on, active_cursor);
26479 /* Switch the display of W's cursor on or off, according to the value
26480 of ON. */
26482 static void
26483 update_window_cursor (struct window *w, bool on)
26485 /* Don't update cursor in windows whose frame is in the process
26486 of being deleted. */
26487 if (w->current_matrix)
26489 int hpos = w->phys_cursor.hpos;
26490 int vpos = w->phys_cursor.vpos;
26491 struct glyph_row *row;
26493 if (vpos >= w->current_matrix->nrows
26494 || hpos >= w->current_matrix->matrix_w)
26495 return;
26497 row = MATRIX_ROW (w->current_matrix, vpos);
26499 /* When the window is hscrolled, cursor hpos can legitimately be
26500 out of bounds, but we draw the cursor at the corresponding
26501 window margin in that case. */
26502 if (!row->reversed_p && hpos < 0)
26503 hpos = 0;
26504 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26505 hpos = row->used[TEXT_AREA] - 1;
26507 block_input ();
26508 display_and_set_cursor (w, on, hpos, vpos,
26509 w->phys_cursor.x, w->phys_cursor.y);
26510 unblock_input ();
26515 /* Call update_window_cursor with parameter ON_P on all leaf windows
26516 in the window tree rooted at W. */
26518 static void
26519 update_cursor_in_window_tree (struct window *w, bool on_p)
26521 while (w)
26523 if (WINDOWP (w->contents))
26524 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26525 else
26526 update_window_cursor (w, on_p);
26528 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26533 /* EXPORT:
26534 Display the cursor on window W, or clear it, according to ON_P.
26535 Don't change the cursor's position. */
26537 void
26538 x_update_cursor (struct frame *f, bool on_p)
26540 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26544 /* EXPORT:
26545 Clear the cursor of window W to background color, and mark the
26546 cursor as not shown. This is used when the text where the cursor
26547 is about to be rewritten. */
26549 void
26550 x_clear_cursor (struct window *w)
26552 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26553 update_window_cursor (w, 0);
26556 #endif /* HAVE_WINDOW_SYSTEM */
26558 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26559 and MSDOS. */
26560 static void
26561 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26562 int start_hpos, int end_hpos,
26563 enum draw_glyphs_face draw)
26565 #ifdef HAVE_WINDOW_SYSTEM
26566 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26568 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26569 return;
26571 #endif
26572 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26573 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26574 #endif
26577 /* Display the active region described by mouse_face_* according to DRAW. */
26579 static void
26580 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26582 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26583 struct frame *f = XFRAME (WINDOW_FRAME (w));
26585 if (/* If window is in the process of being destroyed, don't bother
26586 to do anything. */
26587 w->current_matrix != NULL
26588 /* Don't update mouse highlight if hidden */
26589 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26590 /* Recognize when we are called to operate on rows that don't exist
26591 anymore. This can happen when a window is split. */
26592 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26594 int phys_cursor_on_p = w->phys_cursor_on_p;
26595 struct glyph_row *row, *first, *last;
26597 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26598 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26600 for (row = first; row <= last && row->enabled_p; ++row)
26602 int start_hpos, end_hpos, start_x;
26604 /* For all but the first row, the highlight starts at column 0. */
26605 if (row == first)
26607 /* R2L rows have BEG and END in reversed order, but the
26608 screen drawing geometry is always left to right. So
26609 we need to mirror the beginning and end of the
26610 highlighted area in R2L rows. */
26611 if (!row->reversed_p)
26613 start_hpos = hlinfo->mouse_face_beg_col;
26614 start_x = hlinfo->mouse_face_beg_x;
26616 else if (row == last)
26618 start_hpos = hlinfo->mouse_face_end_col;
26619 start_x = hlinfo->mouse_face_end_x;
26621 else
26623 start_hpos = 0;
26624 start_x = 0;
26627 else if (row->reversed_p && row == last)
26629 start_hpos = hlinfo->mouse_face_end_col;
26630 start_x = hlinfo->mouse_face_end_x;
26632 else
26634 start_hpos = 0;
26635 start_x = 0;
26638 if (row == last)
26640 if (!row->reversed_p)
26641 end_hpos = hlinfo->mouse_face_end_col;
26642 else if (row == first)
26643 end_hpos = hlinfo->mouse_face_beg_col;
26644 else
26646 end_hpos = row->used[TEXT_AREA];
26647 if (draw == DRAW_NORMAL_TEXT)
26648 row->fill_line_p = 1; /* Clear to end of line */
26651 else if (row->reversed_p && row == first)
26652 end_hpos = hlinfo->mouse_face_beg_col;
26653 else
26655 end_hpos = row->used[TEXT_AREA];
26656 if (draw == DRAW_NORMAL_TEXT)
26657 row->fill_line_p = 1; /* Clear to end of line */
26660 if (end_hpos > start_hpos)
26662 draw_row_with_mouse_face (w, start_x, row,
26663 start_hpos, end_hpos, draw);
26665 row->mouse_face_p
26666 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26670 #ifdef HAVE_WINDOW_SYSTEM
26671 /* When we've written over the cursor, arrange for it to
26672 be displayed again. */
26673 if (FRAME_WINDOW_P (f)
26674 && phys_cursor_on_p && !w->phys_cursor_on_p)
26676 int hpos = w->phys_cursor.hpos;
26678 /* When the window is hscrolled, cursor hpos can legitimately be
26679 out of bounds, but we draw the cursor at the corresponding
26680 window margin in that case. */
26681 if (!row->reversed_p && hpos < 0)
26682 hpos = 0;
26683 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26684 hpos = row->used[TEXT_AREA] - 1;
26686 block_input ();
26687 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26688 w->phys_cursor.x, w->phys_cursor.y);
26689 unblock_input ();
26691 #endif /* HAVE_WINDOW_SYSTEM */
26694 #ifdef HAVE_WINDOW_SYSTEM
26695 /* Change the mouse cursor. */
26696 if (FRAME_WINDOW_P (f))
26698 if (draw == DRAW_NORMAL_TEXT
26699 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26700 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26701 else if (draw == DRAW_MOUSE_FACE)
26702 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26703 else
26704 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26706 #endif /* HAVE_WINDOW_SYSTEM */
26709 /* EXPORT:
26710 Clear out the mouse-highlighted active region.
26711 Redraw it un-highlighted first. Value is non-zero if mouse
26712 face was actually drawn unhighlighted. */
26715 clear_mouse_face (Mouse_HLInfo *hlinfo)
26717 int cleared = 0;
26719 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26721 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26722 cleared = 1;
26725 reset_mouse_highlight (hlinfo);
26726 return cleared;
26729 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26730 within the mouse face on that window. */
26731 static int
26732 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26734 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26736 /* Quickly resolve the easy cases. */
26737 if (!(WINDOWP (hlinfo->mouse_face_window)
26738 && XWINDOW (hlinfo->mouse_face_window) == w))
26739 return 0;
26740 if (vpos < hlinfo->mouse_face_beg_row
26741 || vpos > hlinfo->mouse_face_end_row)
26742 return 0;
26743 if (vpos > hlinfo->mouse_face_beg_row
26744 && vpos < hlinfo->mouse_face_end_row)
26745 return 1;
26747 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26749 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26751 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26752 return 1;
26754 else if ((vpos == hlinfo->mouse_face_beg_row
26755 && hpos >= hlinfo->mouse_face_beg_col)
26756 || (vpos == hlinfo->mouse_face_end_row
26757 && hpos < hlinfo->mouse_face_end_col))
26758 return 1;
26760 else
26762 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26764 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26765 return 1;
26767 else if ((vpos == hlinfo->mouse_face_beg_row
26768 && hpos <= hlinfo->mouse_face_beg_col)
26769 || (vpos == hlinfo->mouse_face_end_row
26770 && hpos > hlinfo->mouse_face_end_col))
26771 return 1;
26773 return 0;
26777 /* EXPORT:
26778 Non-zero if physical cursor of window W is within mouse face. */
26781 cursor_in_mouse_face_p (struct window *w)
26783 int hpos = w->phys_cursor.hpos;
26784 int vpos = w->phys_cursor.vpos;
26785 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26787 /* When the window is hscrolled, cursor hpos can legitimately be out
26788 of bounds, but we draw the cursor at the corresponding window
26789 margin in that case. */
26790 if (!row->reversed_p && hpos < 0)
26791 hpos = 0;
26792 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26793 hpos = row->used[TEXT_AREA] - 1;
26795 return coords_in_mouse_face_p (w, hpos, vpos);
26800 /* Find the glyph rows START_ROW and END_ROW of window W that display
26801 characters between buffer positions START_CHARPOS and END_CHARPOS
26802 (excluding END_CHARPOS). DISP_STRING is a display string that
26803 covers these buffer positions. This is similar to
26804 row_containing_pos, but is more accurate when bidi reordering makes
26805 buffer positions change non-linearly with glyph rows. */
26806 static void
26807 rows_from_pos_range (struct window *w,
26808 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26809 Lisp_Object disp_string,
26810 struct glyph_row **start, struct glyph_row **end)
26812 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26813 int last_y = window_text_bottom_y (w);
26814 struct glyph_row *row;
26816 *start = NULL;
26817 *end = NULL;
26819 while (!first->enabled_p
26820 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26821 first++;
26823 /* Find the START row. */
26824 for (row = first;
26825 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26826 row++)
26828 /* A row can potentially be the START row if the range of the
26829 characters it displays intersects the range
26830 [START_CHARPOS..END_CHARPOS). */
26831 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26832 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26833 /* See the commentary in row_containing_pos, for the
26834 explanation of the complicated way to check whether
26835 some position is beyond the end of the characters
26836 displayed by a row. */
26837 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26838 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26839 && !row->ends_at_zv_p
26840 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26841 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26842 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26843 && !row->ends_at_zv_p
26844 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26846 /* Found a candidate row. Now make sure at least one of the
26847 glyphs it displays has a charpos from the range
26848 [START_CHARPOS..END_CHARPOS).
26850 This is not obvious because bidi reordering could make
26851 buffer positions of a row be 1,2,3,102,101,100, and if we
26852 want to highlight characters in [50..60), we don't want
26853 this row, even though [50..60) does intersect [1..103),
26854 the range of character positions given by the row's start
26855 and end positions. */
26856 struct glyph *g = row->glyphs[TEXT_AREA];
26857 struct glyph *e = g + row->used[TEXT_AREA];
26859 while (g < e)
26861 if (((BUFFERP (g->object) || INTEGERP (g->object))
26862 && start_charpos <= g->charpos && g->charpos < end_charpos)
26863 /* A glyph that comes from DISP_STRING is by
26864 definition to be highlighted. */
26865 || EQ (g->object, disp_string))
26866 *start = row;
26867 g++;
26869 if (*start)
26870 break;
26874 /* Find the END row. */
26875 if (!*start
26876 /* If the last row is partially visible, start looking for END
26877 from that row, instead of starting from FIRST. */
26878 && !(row->enabled_p
26879 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26880 row = first;
26881 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26883 struct glyph_row *next = row + 1;
26884 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26886 if (!next->enabled_p
26887 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26888 /* The first row >= START whose range of displayed characters
26889 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26890 is the row END + 1. */
26891 || (start_charpos < next_start
26892 && end_charpos < next_start)
26893 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26894 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26895 && !next->ends_at_zv_p
26896 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26897 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26898 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26899 && !next->ends_at_zv_p
26900 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26902 *end = row;
26903 break;
26905 else
26907 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26908 but none of the characters it displays are in the range, it is
26909 also END + 1. */
26910 struct glyph *g = next->glyphs[TEXT_AREA];
26911 struct glyph *s = g;
26912 struct glyph *e = g + next->used[TEXT_AREA];
26914 while (g < e)
26916 if (((BUFFERP (g->object) || INTEGERP (g->object))
26917 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26918 /* If the buffer position of the first glyph in
26919 the row is equal to END_CHARPOS, it means
26920 the last character to be highlighted is the
26921 newline of ROW, and we must consider NEXT as
26922 END, not END+1. */
26923 || (((!next->reversed_p && g == s)
26924 || (next->reversed_p && g == e - 1))
26925 && (g->charpos == end_charpos
26926 /* Special case for when NEXT is an
26927 empty line at ZV. */
26928 || (g->charpos == -1
26929 && !row->ends_at_zv_p
26930 && next_start == end_charpos)))))
26931 /* A glyph that comes from DISP_STRING is by
26932 definition to be highlighted. */
26933 || EQ (g->object, disp_string))
26934 break;
26935 g++;
26937 if (g == e)
26939 *end = row;
26940 break;
26942 /* The first row that ends at ZV must be the last to be
26943 highlighted. */
26944 else if (next->ends_at_zv_p)
26946 *end = next;
26947 break;
26953 /* This function sets the mouse_face_* elements of HLINFO, assuming
26954 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26955 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26956 for the overlay or run of text properties specifying the mouse
26957 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26958 before-string and after-string that must also be highlighted.
26959 DISP_STRING, if non-nil, is a display string that may cover some
26960 or all of the highlighted text. */
26962 static void
26963 mouse_face_from_buffer_pos (Lisp_Object window,
26964 Mouse_HLInfo *hlinfo,
26965 ptrdiff_t mouse_charpos,
26966 ptrdiff_t start_charpos,
26967 ptrdiff_t end_charpos,
26968 Lisp_Object before_string,
26969 Lisp_Object after_string,
26970 Lisp_Object disp_string)
26972 struct window *w = XWINDOW (window);
26973 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26974 struct glyph_row *r1, *r2;
26975 struct glyph *glyph, *end;
26976 ptrdiff_t ignore, pos;
26977 int x;
26979 eassert (NILP (disp_string) || STRINGP (disp_string));
26980 eassert (NILP (before_string) || STRINGP (before_string));
26981 eassert (NILP (after_string) || STRINGP (after_string));
26983 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26984 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26985 if (r1 == NULL)
26986 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
26987 /* If the before-string or display-string contains newlines,
26988 rows_from_pos_range skips to its last row. Move back. */
26989 if (!NILP (before_string) || !NILP (disp_string))
26991 struct glyph_row *prev;
26992 while ((prev = r1 - 1, prev >= first)
26993 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26994 && prev->used[TEXT_AREA] > 0)
26996 struct glyph *beg = prev->glyphs[TEXT_AREA];
26997 glyph = beg + prev->used[TEXT_AREA];
26998 while (--glyph >= beg && INTEGERP (glyph->object));
26999 if (glyph < beg
27000 || !(EQ (glyph->object, before_string)
27001 || EQ (glyph->object, disp_string)))
27002 break;
27003 r1 = prev;
27006 if (r2 == NULL)
27008 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27009 hlinfo->mouse_face_past_end = 1;
27011 else if (!NILP (after_string))
27013 /* If the after-string has newlines, advance to its last row. */
27014 struct glyph_row *next;
27015 struct glyph_row *last
27016 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27018 for (next = r2 + 1;
27019 next <= last
27020 && next->used[TEXT_AREA] > 0
27021 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27022 ++next)
27023 r2 = next;
27025 /* The rest of the display engine assumes that mouse_face_beg_row is
27026 either above mouse_face_end_row or identical to it. But with
27027 bidi-reordered continued lines, the row for START_CHARPOS could
27028 be below the row for END_CHARPOS. If so, swap the rows and store
27029 them in correct order. */
27030 if (r1->y > r2->y)
27032 struct glyph_row *tem = r2;
27034 r2 = r1;
27035 r1 = tem;
27038 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27039 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27041 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27042 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27043 could be anywhere in the row and in any order. The strategy
27044 below is to find the leftmost and the rightmost glyph that
27045 belongs to either of these 3 strings, or whose position is
27046 between START_CHARPOS and END_CHARPOS, and highlight all the
27047 glyphs between those two. This may cover more than just the text
27048 between START_CHARPOS and END_CHARPOS if the range of characters
27049 strides the bidi level boundary, e.g. if the beginning is in R2L
27050 text while the end is in L2R text or vice versa. */
27051 if (!r1->reversed_p)
27053 /* This row is in a left to right paragraph. Scan it left to
27054 right. */
27055 glyph = r1->glyphs[TEXT_AREA];
27056 end = glyph + r1->used[TEXT_AREA];
27057 x = r1->x;
27059 /* Skip truncation glyphs at the start of the glyph row. */
27060 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27061 for (; glyph < end
27062 && INTEGERP (glyph->object)
27063 && glyph->charpos < 0;
27064 ++glyph)
27065 x += glyph->pixel_width;
27067 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27068 or DISP_STRING, and the first glyph from buffer whose
27069 position is between START_CHARPOS and END_CHARPOS. */
27070 for (; glyph < end
27071 && !INTEGERP (glyph->object)
27072 && !EQ (glyph->object, disp_string)
27073 && !(BUFFERP (glyph->object)
27074 && (glyph->charpos >= start_charpos
27075 && glyph->charpos < end_charpos));
27076 ++glyph)
27078 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27079 are present at buffer positions between START_CHARPOS and
27080 END_CHARPOS, or if they come from an overlay. */
27081 if (EQ (glyph->object, before_string))
27083 pos = string_buffer_position (before_string,
27084 start_charpos);
27085 /* If pos == 0, it means before_string came from an
27086 overlay, not from a buffer position. */
27087 if (!pos || (pos >= start_charpos && pos < end_charpos))
27088 break;
27090 else if (EQ (glyph->object, after_string))
27092 pos = string_buffer_position (after_string, end_charpos);
27093 if (!pos || (pos >= start_charpos && pos < end_charpos))
27094 break;
27096 x += glyph->pixel_width;
27098 hlinfo->mouse_face_beg_x = x;
27099 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27101 else
27103 /* This row is in a right to left paragraph. Scan it right to
27104 left. */
27105 struct glyph *g;
27107 end = r1->glyphs[TEXT_AREA] - 1;
27108 glyph = end + r1->used[TEXT_AREA];
27110 /* Skip truncation glyphs at the start of the glyph row. */
27111 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27112 for (; glyph > end
27113 && INTEGERP (glyph->object)
27114 && glyph->charpos < 0;
27115 --glyph)
27118 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27119 or DISP_STRING, and the first glyph from buffer whose
27120 position is between START_CHARPOS and END_CHARPOS. */
27121 for (; glyph > end
27122 && !INTEGERP (glyph->object)
27123 && !EQ (glyph->object, disp_string)
27124 && !(BUFFERP (glyph->object)
27125 && (glyph->charpos >= start_charpos
27126 && glyph->charpos < end_charpos));
27127 --glyph)
27129 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27130 are present at buffer positions between START_CHARPOS and
27131 END_CHARPOS, or if they come from an overlay. */
27132 if (EQ (glyph->object, before_string))
27134 pos = string_buffer_position (before_string, start_charpos);
27135 /* If pos == 0, it means before_string came from an
27136 overlay, not from a buffer position. */
27137 if (!pos || (pos >= start_charpos && pos < end_charpos))
27138 break;
27140 else if (EQ (glyph->object, after_string))
27142 pos = string_buffer_position (after_string, end_charpos);
27143 if (!pos || (pos >= start_charpos && pos < end_charpos))
27144 break;
27148 glyph++; /* first glyph to the right of the highlighted area */
27149 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27150 x += g->pixel_width;
27151 hlinfo->mouse_face_beg_x = x;
27152 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27155 /* If the highlight ends in a different row, compute GLYPH and END
27156 for the end row. Otherwise, reuse the values computed above for
27157 the row where the highlight begins. */
27158 if (r2 != r1)
27160 if (!r2->reversed_p)
27162 glyph = r2->glyphs[TEXT_AREA];
27163 end = glyph + r2->used[TEXT_AREA];
27164 x = r2->x;
27166 else
27168 end = r2->glyphs[TEXT_AREA] - 1;
27169 glyph = end + r2->used[TEXT_AREA];
27173 if (!r2->reversed_p)
27175 /* Skip truncation and continuation glyphs near the end of the
27176 row, and also blanks and stretch glyphs inserted by
27177 extend_face_to_end_of_line. */
27178 while (end > glyph
27179 && INTEGERP ((end - 1)->object))
27180 --end;
27181 /* Scan the rest of the glyph row from the end, looking for the
27182 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27183 DISP_STRING, or whose position is between START_CHARPOS
27184 and END_CHARPOS */
27185 for (--end;
27186 end > glyph
27187 && !INTEGERP (end->object)
27188 && !EQ (end->object, disp_string)
27189 && !(BUFFERP (end->object)
27190 && (end->charpos >= start_charpos
27191 && end->charpos < end_charpos));
27192 --end)
27194 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27195 are present at buffer positions between START_CHARPOS and
27196 END_CHARPOS, or if they come from an overlay. */
27197 if (EQ (end->object, before_string))
27199 pos = string_buffer_position (before_string, start_charpos);
27200 if (!pos || (pos >= start_charpos && pos < end_charpos))
27201 break;
27203 else if (EQ (end->object, after_string))
27205 pos = string_buffer_position (after_string, end_charpos);
27206 if (!pos || (pos >= start_charpos && pos < end_charpos))
27207 break;
27210 /* Find the X coordinate of the last glyph to be highlighted. */
27211 for (; glyph <= end; ++glyph)
27212 x += glyph->pixel_width;
27214 hlinfo->mouse_face_end_x = x;
27215 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27217 else
27219 /* Skip truncation and continuation glyphs near the end of the
27220 row, and also blanks and stretch glyphs inserted by
27221 extend_face_to_end_of_line. */
27222 x = r2->x;
27223 end++;
27224 while (end < glyph
27225 && INTEGERP (end->object))
27227 x += end->pixel_width;
27228 ++end;
27230 /* Scan the rest of the glyph row from the end, looking for the
27231 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27232 DISP_STRING, or whose position is between START_CHARPOS
27233 and END_CHARPOS */
27234 for ( ;
27235 end < glyph
27236 && !INTEGERP (end->object)
27237 && !EQ (end->object, disp_string)
27238 && !(BUFFERP (end->object)
27239 && (end->charpos >= start_charpos
27240 && end->charpos < end_charpos));
27241 ++end)
27243 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27244 are present at buffer positions between START_CHARPOS and
27245 END_CHARPOS, or if they come from an overlay. */
27246 if (EQ (end->object, before_string))
27248 pos = string_buffer_position (before_string, start_charpos);
27249 if (!pos || (pos >= start_charpos && pos < end_charpos))
27250 break;
27252 else if (EQ (end->object, after_string))
27254 pos = string_buffer_position (after_string, end_charpos);
27255 if (!pos || (pos >= start_charpos && pos < end_charpos))
27256 break;
27258 x += end->pixel_width;
27260 /* If we exited the above loop because we arrived at the last
27261 glyph of the row, and its buffer position is still not in
27262 range, it means the last character in range is the preceding
27263 newline. Bump the end column and x values to get past the
27264 last glyph. */
27265 if (end == glyph
27266 && BUFFERP (end->object)
27267 && (end->charpos < start_charpos
27268 || end->charpos >= end_charpos))
27270 x += end->pixel_width;
27271 ++end;
27273 hlinfo->mouse_face_end_x = x;
27274 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27277 hlinfo->mouse_face_window = window;
27278 hlinfo->mouse_face_face_id
27279 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27280 mouse_charpos + 1,
27281 !hlinfo->mouse_face_hidden, -1);
27282 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27285 /* The following function is not used anymore (replaced with
27286 mouse_face_from_string_pos), but I leave it here for the time
27287 being, in case someone would. */
27289 #if 0 /* not used */
27291 /* Find the position of the glyph for position POS in OBJECT in
27292 window W's current matrix, and return in *X, *Y the pixel
27293 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27295 RIGHT_P non-zero means return the position of the right edge of the
27296 glyph, RIGHT_P zero means return the left edge position.
27298 If no glyph for POS exists in the matrix, return the position of
27299 the glyph with the next smaller position that is in the matrix, if
27300 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27301 exists in the matrix, return the position of the glyph with the
27302 next larger position in OBJECT.
27304 Value is non-zero if a glyph was found. */
27306 static int
27307 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27308 int *hpos, int *vpos, int *x, int *y, int right_p)
27310 int yb = window_text_bottom_y (w);
27311 struct glyph_row *r;
27312 struct glyph *best_glyph = NULL;
27313 struct glyph_row *best_row = NULL;
27314 int best_x = 0;
27316 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27317 r->enabled_p && r->y < yb;
27318 ++r)
27320 struct glyph *g = r->glyphs[TEXT_AREA];
27321 struct glyph *e = g + r->used[TEXT_AREA];
27322 int gx;
27324 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27325 if (EQ (g->object, object))
27327 if (g->charpos == pos)
27329 best_glyph = g;
27330 best_x = gx;
27331 best_row = r;
27332 goto found;
27334 else if (best_glyph == NULL
27335 || ((eabs (g->charpos - pos)
27336 < eabs (best_glyph->charpos - pos))
27337 && (right_p
27338 ? g->charpos < pos
27339 : g->charpos > pos)))
27341 best_glyph = g;
27342 best_x = gx;
27343 best_row = r;
27348 found:
27350 if (best_glyph)
27352 *x = best_x;
27353 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27355 if (right_p)
27357 *x += best_glyph->pixel_width;
27358 ++*hpos;
27361 *y = best_row->y;
27362 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27365 return best_glyph != NULL;
27367 #endif /* not used */
27369 /* Find the positions of the first and the last glyphs in window W's
27370 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27371 (assumed to be a string), and return in HLINFO's mouse_face_*
27372 members the pixel and column/row coordinates of those glyphs. */
27374 static void
27375 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27376 Lisp_Object object,
27377 ptrdiff_t startpos, ptrdiff_t endpos)
27379 int yb = window_text_bottom_y (w);
27380 struct glyph_row *r;
27381 struct glyph *g, *e;
27382 int gx;
27383 int found = 0;
27385 /* Find the glyph row with at least one position in the range
27386 [STARTPOS..ENDPOS], and the first glyph in that row whose
27387 position belongs to that range. */
27388 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27389 r->enabled_p && r->y < yb;
27390 ++r)
27392 if (!r->reversed_p)
27394 g = r->glyphs[TEXT_AREA];
27395 e = g + r->used[TEXT_AREA];
27396 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27397 if (EQ (g->object, object)
27398 && startpos <= g->charpos && g->charpos <= endpos)
27400 hlinfo->mouse_face_beg_row
27401 = MATRIX_ROW_VPOS (r, w->current_matrix);
27402 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27403 hlinfo->mouse_face_beg_x = gx;
27404 found = 1;
27405 break;
27408 else
27410 struct glyph *g1;
27412 e = r->glyphs[TEXT_AREA];
27413 g = e + r->used[TEXT_AREA];
27414 for ( ; g > e; --g)
27415 if (EQ ((g-1)->object, object)
27416 && startpos <= (g-1)->charpos && (g-1)->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 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27422 gx += g1->pixel_width;
27423 hlinfo->mouse_face_beg_x = gx;
27424 found = 1;
27425 break;
27428 if (found)
27429 break;
27432 if (!found)
27433 return;
27435 /* Starting with the next row, look for the first row which does NOT
27436 include any glyphs whose positions are in the range. */
27437 for (++r; r->enabled_p && r->y < yb; ++r)
27439 g = r->glyphs[TEXT_AREA];
27440 e = g + r->used[TEXT_AREA];
27441 found = 0;
27442 for ( ; g < e; ++g)
27443 if (EQ (g->object, object)
27444 && startpos <= g->charpos && g->charpos <= endpos)
27446 found = 1;
27447 break;
27449 if (!found)
27450 break;
27453 /* The highlighted region ends on the previous row. */
27454 r--;
27456 /* Set the end row. */
27457 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27459 /* Compute and set the end column and the end column's horizontal
27460 pixel coordinate. */
27461 if (!r->reversed_p)
27463 g = r->glyphs[TEXT_AREA];
27464 e = g + r->used[TEXT_AREA];
27465 for ( ; e > g; --e)
27466 if (EQ ((e-1)->object, object)
27467 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27468 break;
27469 hlinfo->mouse_face_end_col = e - g;
27471 for (gx = r->x; g < e; ++g)
27472 gx += g->pixel_width;
27473 hlinfo->mouse_face_end_x = gx;
27475 else
27477 e = r->glyphs[TEXT_AREA];
27478 g = e + r->used[TEXT_AREA];
27479 for (gx = r->x ; e < g; ++e)
27481 if (EQ (e->object, object)
27482 && startpos <= e->charpos && e->charpos <= endpos)
27483 break;
27484 gx += e->pixel_width;
27486 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27487 hlinfo->mouse_face_end_x = gx;
27491 #ifdef HAVE_WINDOW_SYSTEM
27493 /* See if position X, Y is within a hot-spot of an image. */
27495 static int
27496 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27498 if (!CONSP (hot_spot))
27499 return 0;
27501 if (EQ (XCAR (hot_spot), Qrect))
27503 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27504 Lisp_Object rect = XCDR (hot_spot);
27505 Lisp_Object tem;
27506 if (!CONSP (rect))
27507 return 0;
27508 if (!CONSP (XCAR (rect)))
27509 return 0;
27510 if (!CONSP (XCDR (rect)))
27511 return 0;
27512 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27513 return 0;
27514 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27515 return 0;
27516 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27517 return 0;
27518 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27519 return 0;
27520 return 1;
27522 else if (EQ (XCAR (hot_spot), Qcircle))
27524 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27525 Lisp_Object circ = XCDR (hot_spot);
27526 Lisp_Object lr, lx0, ly0;
27527 if (CONSP (circ)
27528 && CONSP (XCAR (circ))
27529 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27530 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27531 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27533 double r = XFLOATINT (lr);
27534 double dx = XINT (lx0) - x;
27535 double dy = XINT (ly0) - y;
27536 return (dx * dx + dy * dy <= r * r);
27539 else if (EQ (XCAR (hot_spot), Qpoly))
27541 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27542 if (VECTORP (XCDR (hot_spot)))
27544 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27545 Lisp_Object *poly = v->contents;
27546 ptrdiff_t n = v->header.size;
27547 ptrdiff_t i;
27548 int inside = 0;
27549 Lisp_Object lx, ly;
27550 int x0, y0;
27552 /* Need an even number of coordinates, and at least 3 edges. */
27553 if (n < 6 || n & 1)
27554 return 0;
27556 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27557 If count is odd, we are inside polygon. Pixels on edges
27558 may or may not be included depending on actual geometry of the
27559 polygon. */
27560 if ((lx = poly[n-2], !INTEGERP (lx))
27561 || (ly = poly[n-1], !INTEGERP (lx)))
27562 return 0;
27563 x0 = XINT (lx), y0 = XINT (ly);
27564 for (i = 0; i < n; i += 2)
27566 int x1 = x0, y1 = y0;
27567 if ((lx = poly[i], !INTEGERP (lx))
27568 || (ly = poly[i+1], !INTEGERP (ly)))
27569 return 0;
27570 x0 = XINT (lx), y0 = XINT (ly);
27572 /* Does this segment cross the X line? */
27573 if (x0 >= x)
27575 if (x1 >= x)
27576 continue;
27578 else if (x1 < x)
27579 continue;
27580 if (y > y0 && y > y1)
27581 continue;
27582 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27583 inside = !inside;
27585 return inside;
27588 return 0;
27591 Lisp_Object
27592 find_hot_spot (Lisp_Object map, int x, int y)
27594 while (CONSP (map))
27596 if (CONSP (XCAR (map))
27597 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27598 return XCAR (map);
27599 map = XCDR (map);
27602 return Qnil;
27605 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27606 3, 3, 0,
27607 doc: /* Lookup in image map MAP coordinates X and Y.
27608 An image map is an alist where each element has the format (AREA ID PLIST).
27609 An AREA is specified as either a rectangle, a circle, or a polygon:
27610 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27611 pixel coordinates of the upper left and bottom right corners.
27612 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27613 and the radius of the circle; r may be a float or integer.
27614 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27615 vector describes one corner in the polygon.
27616 Returns the alist element for the first matching AREA in MAP. */)
27617 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27619 if (NILP (map))
27620 return Qnil;
27622 CHECK_NUMBER (x);
27623 CHECK_NUMBER (y);
27625 return find_hot_spot (map,
27626 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27627 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27631 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27632 static void
27633 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27635 /* Do not change cursor shape while dragging mouse. */
27636 if (!NILP (do_mouse_tracking))
27637 return;
27639 if (!NILP (pointer))
27641 if (EQ (pointer, Qarrow))
27642 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27643 else if (EQ (pointer, Qhand))
27644 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27645 else if (EQ (pointer, Qtext))
27646 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27647 else if (EQ (pointer, intern ("hdrag")))
27648 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27649 #ifdef HAVE_X_WINDOWS
27650 else if (EQ (pointer, intern ("vdrag")))
27651 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27652 #endif
27653 else if (EQ (pointer, intern ("hourglass")))
27654 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27655 else if (EQ (pointer, Qmodeline))
27656 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27657 else
27658 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27661 if (cursor != No_Cursor)
27662 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27665 #endif /* HAVE_WINDOW_SYSTEM */
27667 /* Take proper action when mouse has moved to the mode or header line
27668 or marginal area AREA of window W, x-position X and y-position Y.
27669 X is relative to the start of the text display area of W, so the
27670 width of bitmap areas and scroll bars must be subtracted to get a
27671 position relative to the start of the mode line. */
27673 static void
27674 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27675 enum window_part area)
27677 struct window *w = XWINDOW (window);
27678 struct frame *f = XFRAME (w->frame);
27679 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27680 #ifdef HAVE_WINDOW_SYSTEM
27681 Display_Info *dpyinfo;
27682 #endif
27683 Cursor cursor = No_Cursor;
27684 Lisp_Object pointer = Qnil;
27685 int dx, dy, width, height;
27686 ptrdiff_t charpos;
27687 Lisp_Object string, object = Qnil;
27688 Lisp_Object pos IF_LINT (= Qnil), help;
27690 Lisp_Object mouse_face;
27691 int original_x_pixel = x;
27692 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27693 struct glyph_row *row IF_LINT (= 0);
27695 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27697 int x0;
27698 struct glyph *end;
27700 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27701 returns them in row/column units! */
27702 string = mode_line_string (w, area, &x, &y, &charpos,
27703 &object, &dx, &dy, &width, &height);
27705 row = (area == ON_MODE_LINE
27706 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27707 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27709 /* Find the glyph under the mouse pointer. */
27710 if (row->mode_line_p && row->enabled_p)
27712 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27713 end = glyph + row->used[TEXT_AREA];
27715 for (x0 = original_x_pixel;
27716 glyph < end && x0 >= glyph->pixel_width;
27717 ++glyph)
27718 x0 -= glyph->pixel_width;
27720 if (glyph >= end)
27721 glyph = NULL;
27724 else
27726 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27727 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27728 returns them in row/column units! */
27729 string = marginal_area_string (w, area, &x, &y, &charpos,
27730 &object, &dx, &dy, &width, &height);
27733 help = Qnil;
27735 #ifdef HAVE_WINDOW_SYSTEM
27736 if (IMAGEP (object))
27738 Lisp_Object image_map, hotspot;
27739 if ((image_map = Fplist_get (XCDR (object), QCmap),
27740 !NILP (image_map))
27741 && (hotspot = find_hot_spot (image_map, dx, dy),
27742 CONSP (hotspot))
27743 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27745 Lisp_Object plist;
27747 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27748 If so, we could look for mouse-enter, mouse-leave
27749 properties in PLIST (and do something...). */
27750 hotspot = XCDR (hotspot);
27751 if (CONSP (hotspot)
27752 && (plist = XCAR (hotspot), CONSP (plist)))
27754 pointer = Fplist_get (plist, Qpointer);
27755 if (NILP (pointer))
27756 pointer = Qhand;
27757 help = Fplist_get (plist, Qhelp_echo);
27758 if (!NILP (help))
27760 help_echo_string = help;
27761 XSETWINDOW (help_echo_window, w);
27762 help_echo_object = w->contents;
27763 help_echo_pos = charpos;
27767 if (NILP (pointer))
27768 pointer = Fplist_get (XCDR (object), QCpointer);
27770 #endif /* HAVE_WINDOW_SYSTEM */
27772 if (STRINGP (string))
27773 pos = make_number (charpos);
27775 /* Set the help text and mouse pointer. If the mouse is on a part
27776 of the mode line without any text (e.g. past the right edge of
27777 the mode line text), use the default help text and pointer. */
27778 if (STRINGP (string) || area == ON_MODE_LINE)
27780 /* Arrange to display the help by setting the global variables
27781 help_echo_string, help_echo_object, and help_echo_pos. */
27782 if (NILP (help))
27784 if (STRINGP (string))
27785 help = Fget_text_property (pos, Qhelp_echo, string);
27787 if (!NILP (help))
27789 help_echo_string = help;
27790 XSETWINDOW (help_echo_window, w);
27791 help_echo_object = string;
27792 help_echo_pos = charpos;
27794 else if (area == ON_MODE_LINE)
27796 Lisp_Object default_help
27797 = buffer_local_value_1 (Qmode_line_default_help_echo,
27798 w->contents);
27800 if (STRINGP (default_help))
27802 help_echo_string = default_help;
27803 XSETWINDOW (help_echo_window, w);
27804 help_echo_object = Qnil;
27805 help_echo_pos = -1;
27810 #ifdef HAVE_WINDOW_SYSTEM
27811 /* Change the mouse pointer according to what is under it. */
27812 if (FRAME_WINDOW_P (f))
27814 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27815 if (STRINGP (string))
27817 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27819 if (NILP (pointer))
27820 pointer = Fget_text_property (pos, Qpointer, string);
27822 /* Change the mouse pointer according to what is under X/Y. */
27823 if (NILP (pointer)
27824 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27826 Lisp_Object map;
27827 map = Fget_text_property (pos, Qlocal_map, string);
27828 if (!KEYMAPP (map))
27829 map = Fget_text_property (pos, Qkeymap, string);
27830 if (!KEYMAPP (map))
27831 cursor = dpyinfo->vertical_scroll_bar_cursor;
27834 else
27835 /* Default mode-line pointer. */
27836 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27838 #endif
27841 /* Change the mouse face according to what is under X/Y. */
27842 if (STRINGP (string))
27844 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27845 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27846 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27847 && glyph)
27849 Lisp_Object b, e;
27851 struct glyph * tmp_glyph;
27853 int gpos;
27854 int gseq_length;
27855 int total_pixel_width;
27856 ptrdiff_t begpos, endpos, ignore;
27858 int vpos, hpos;
27860 b = Fprevious_single_property_change (make_number (charpos + 1),
27861 Qmouse_face, string, Qnil);
27862 if (NILP (b))
27863 begpos = 0;
27864 else
27865 begpos = XINT (b);
27867 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27868 if (NILP (e))
27869 endpos = SCHARS (string);
27870 else
27871 endpos = XINT (e);
27873 /* Calculate the glyph position GPOS of GLYPH in the
27874 displayed string, relative to the beginning of the
27875 highlighted part of the string.
27877 Note: GPOS is different from CHARPOS. CHARPOS is the
27878 position of GLYPH in the internal string object. A mode
27879 line string format has structures which are converted to
27880 a flattened string by the Emacs Lisp interpreter. The
27881 internal string is an element of those structures. The
27882 displayed string is the flattened string. */
27883 tmp_glyph = row_start_glyph;
27884 while (tmp_glyph < glyph
27885 && (!(EQ (tmp_glyph->object, glyph->object)
27886 && begpos <= tmp_glyph->charpos
27887 && tmp_glyph->charpos < endpos)))
27888 tmp_glyph++;
27889 gpos = glyph - tmp_glyph;
27891 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27892 the highlighted part of the displayed string to which
27893 GLYPH belongs. Note: GSEQ_LENGTH is different from
27894 SCHARS (STRING), because the latter returns the length of
27895 the internal string. */
27896 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27897 tmp_glyph > glyph
27898 && (!(EQ (tmp_glyph->object, glyph->object)
27899 && begpos <= tmp_glyph->charpos
27900 && tmp_glyph->charpos < endpos));
27901 tmp_glyph--)
27903 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27905 /* Calculate the total pixel width of all the glyphs between
27906 the beginning of the highlighted area and GLYPH. */
27907 total_pixel_width = 0;
27908 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27909 total_pixel_width += tmp_glyph->pixel_width;
27911 /* Pre calculation of re-rendering position. Note: X is in
27912 column units here, after the call to mode_line_string or
27913 marginal_area_string. */
27914 hpos = x - gpos;
27915 vpos = (area == ON_MODE_LINE
27916 ? (w->current_matrix)->nrows - 1
27917 : 0);
27919 /* If GLYPH's position is included in the region that is
27920 already drawn in mouse face, we have nothing to do. */
27921 if ( EQ (window, hlinfo->mouse_face_window)
27922 && (!row->reversed_p
27923 ? (hlinfo->mouse_face_beg_col <= hpos
27924 && hpos < hlinfo->mouse_face_end_col)
27925 /* In R2L rows we swap BEG and END, see below. */
27926 : (hlinfo->mouse_face_end_col <= hpos
27927 && hpos < hlinfo->mouse_face_beg_col))
27928 && hlinfo->mouse_face_beg_row == vpos )
27929 return;
27931 if (clear_mouse_face (hlinfo))
27932 cursor = No_Cursor;
27934 if (!row->reversed_p)
27936 hlinfo->mouse_face_beg_col = hpos;
27937 hlinfo->mouse_face_beg_x = original_x_pixel
27938 - (total_pixel_width + dx);
27939 hlinfo->mouse_face_end_col = hpos + gseq_length;
27940 hlinfo->mouse_face_end_x = 0;
27942 else
27944 /* In R2L rows, show_mouse_face expects BEG and END
27945 coordinates to be swapped. */
27946 hlinfo->mouse_face_end_col = hpos;
27947 hlinfo->mouse_face_end_x = original_x_pixel
27948 - (total_pixel_width + dx);
27949 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27950 hlinfo->mouse_face_beg_x = 0;
27953 hlinfo->mouse_face_beg_row = vpos;
27954 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27955 hlinfo->mouse_face_past_end = 0;
27956 hlinfo->mouse_face_window = window;
27958 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27959 charpos,
27960 0, 0, 0,
27961 &ignore,
27962 glyph->face_id,
27964 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27966 if (NILP (pointer))
27967 pointer = Qhand;
27969 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27970 clear_mouse_face (hlinfo);
27972 #ifdef HAVE_WINDOW_SYSTEM
27973 if (FRAME_WINDOW_P (f))
27974 define_frame_cursor1 (f, cursor, pointer);
27975 #endif
27979 /* EXPORT:
27980 Take proper action when the mouse has moved to position X, Y on
27981 frame F with regards to highlighting portions of display that have
27982 mouse-face properties. Also de-highlight portions of display where
27983 the mouse was before, set the mouse pointer shape as appropriate
27984 for the mouse coordinates, and activate help echo (tooltips).
27985 X and Y can be negative or out of range. */
27987 void
27988 note_mouse_highlight (struct frame *f, int x, int y)
27990 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27991 enum window_part part = ON_NOTHING;
27992 Lisp_Object window;
27993 struct window *w;
27994 Cursor cursor = No_Cursor;
27995 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27996 struct buffer *b;
27998 /* When a menu is active, don't highlight because this looks odd. */
27999 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28000 if (popup_activated ())
28001 return;
28002 #endif
28004 if (!f->glyphs_initialized_p
28005 || f->pointer_invisible)
28006 return;
28008 hlinfo->mouse_face_mouse_x = x;
28009 hlinfo->mouse_face_mouse_y = y;
28010 hlinfo->mouse_face_mouse_frame = f;
28012 if (hlinfo->mouse_face_defer)
28013 return;
28015 /* Which window is that in? */
28016 window = window_from_coordinates (f, x, y, &part, 1);
28018 /* If displaying active text in another window, clear that. */
28019 if (! EQ (window, hlinfo->mouse_face_window)
28020 /* Also clear if we move out of text area in same window. */
28021 || (!NILP (hlinfo->mouse_face_window)
28022 && !NILP (window)
28023 && part != ON_TEXT
28024 && part != ON_MODE_LINE
28025 && part != ON_HEADER_LINE))
28026 clear_mouse_face (hlinfo);
28028 /* Not on a window -> return. */
28029 if (!WINDOWP (window))
28030 return;
28032 /* Reset help_echo_string. It will get recomputed below. */
28033 help_echo_string = Qnil;
28035 /* Convert to window-relative pixel coordinates. */
28036 w = XWINDOW (window);
28037 frame_to_window_pixel_xy (w, &x, &y);
28039 #ifdef HAVE_WINDOW_SYSTEM
28040 /* Handle tool-bar window differently since it doesn't display a
28041 buffer. */
28042 if (EQ (window, f->tool_bar_window))
28044 note_tool_bar_highlight (f, x, y);
28045 return;
28047 #endif
28049 /* Mouse is on the mode, header line or margin? */
28050 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28051 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28053 note_mode_line_or_margin_highlight (window, x, y, part);
28054 return;
28057 #ifdef HAVE_WINDOW_SYSTEM
28058 if (part == ON_VERTICAL_BORDER)
28060 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28061 help_echo_string = build_string ("drag-mouse-1: resize");
28063 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28064 || part == ON_SCROLL_BAR)
28065 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28066 else
28067 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28068 #endif
28070 /* Are we in a window whose display is up to date?
28071 And verify the buffer's text has not changed. */
28072 b = XBUFFER (w->contents);
28073 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28075 int hpos, vpos, dx, dy, area = LAST_AREA;
28076 ptrdiff_t pos;
28077 struct glyph *glyph;
28078 Lisp_Object object;
28079 Lisp_Object mouse_face = Qnil, position;
28080 Lisp_Object *overlay_vec = NULL;
28081 ptrdiff_t i, noverlays;
28082 struct buffer *obuf;
28083 ptrdiff_t obegv, ozv;
28084 int same_region;
28086 /* Find the glyph under X/Y. */
28087 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28089 #ifdef HAVE_WINDOW_SYSTEM
28090 /* Look for :pointer property on image. */
28091 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28093 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28094 if (img != NULL && IMAGEP (img->spec))
28096 Lisp_Object image_map, hotspot;
28097 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28098 !NILP (image_map))
28099 && (hotspot = find_hot_spot (image_map,
28100 glyph->slice.img.x + dx,
28101 glyph->slice.img.y + dy),
28102 CONSP (hotspot))
28103 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28105 Lisp_Object plist;
28107 /* Could check XCAR (hotspot) to see if we enter/leave
28108 this hot-spot.
28109 If so, we could look for mouse-enter, mouse-leave
28110 properties in PLIST (and do something...). */
28111 hotspot = XCDR (hotspot);
28112 if (CONSP (hotspot)
28113 && (plist = XCAR (hotspot), CONSP (plist)))
28115 pointer = Fplist_get (plist, Qpointer);
28116 if (NILP (pointer))
28117 pointer = Qhand;
28118 help_echo_string = Fplist_get (plist, Qhelp_echo);
28119 if (!NILP (help_echo_string))
28121 help_echo_window = window;
28122 help_echo_object = glyph->object;
28123 help_echo_pos = glyph->charpos;
28127 if (NILP (pointer))
28128 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28131 #endif /* HAVE_WINDOW_SYSTEM */
28133 /* Clear mouse face if X/Y not over text. */
28134 if (glyph == NULL
28135 || area != TEXT_AREA
28136 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28137 /* Glyph's OBJECT is an integer for glyphs inserted by the
28138 display engine for its internal purposes, like truncation
28139 and continuation glyphs and blanks beyond the end of
28140 line's text on text terminals. If we are over such a
28141 glyph, we are not over any text. */
28142 || INTEGERP (glyph->object)
28143 /* R2L rows have a stretch glyph at their front, which
28144 stands for no text, whereas L2R rows have no glyphs at
28145 all beyond the end of text. Treat such stretch glyphs
28146 like we do with NULL glyphs in L2R rows. */
28147 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28148 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28149 && glyph->type == STRETCH_GLYPH
28150 && glyph->avoid_cursor_p))
28152 if (clear_mouse_face (hlinfo))
28153 cursor = No_Cursor;
28154 #ifdef HAVE_WINDOW_SYSTEM
28155 if (FRAME_WINDOW_P (f) && NILP (pointer))
28157 if (area != TEXT_AREA)
28158 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28159 else
28160 pointer = Vvoid_text_area_pointer;
28162 #endif
28163 goto set_cursor;
28166 pos = glyph->charpos;
28167 object = glyph->object;
28168 if (!STRINGP (object) && !BUFFERP (object))
28169 goto set_cursor;
28171 /* If we get an out-of-range value, return now; avoid an error. */
28172 if (BUFFERP (object) && pos > BUF_Z (b))
28173 goto set_cursor;
28175 /* Make the window's buffer temporarily current for
28176 overlays_at and compute_char_face. */
28177 obuf = current_buffer;
28178 current_buffer = b;
28179 obegv = BEGV;
28180 ozv = ZV;
28181 BEGV = BEG;
28182 ZV = Z;
28184 /* Is this char mouse-active or does it have help-echo? */
28185 position = make_number (pos);
28187 if (BUFFERP (object))
28189 /* Put all the overlays we want in a vector in overlay_vec. */
28190 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28191 /* Sort overlays into increasing priority order. */
28192 noverlays = sort_overlays (overlay_vec, noverlays, w);
28194 else
28195 noverlays = 0;
28197 if (NILP (Vmouse_highlight))
28199 clear_mouse_face (hlinfo);
28200 goto check_help_echo;
28203 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28205 if (same_region)
28206 cursor = No_Cursor;
28208 /* Check mouse-face highlighting. */
28209 if (! same_region
28210 /* If there exists an overlay with mouse-face overlapping
28211 the one we are currently highlighting, we have to
28212 check if we enter the overlapping overlay, and then
28213 highlight only that. */
28214 || (OVERLAYP (hlinfo->mouse_face_overlay)
28215 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28217 /* Find the highest priority overlay with a mouse-face. */
28218 Lisp_Object overlay = Qnil;
28219 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28221 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28222 if (!NILP (mouse_face))
28223 overlay = overlay_vec[i];
28226 /* If we're highlighting the same overlay as before, there's
28227 no need to do that again. */
28228 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28229 goto check_help_echo;
28230 hlinfo->mouse_face_overlay = overlay;
28232 /* Clear the display of the old active region, if any. */
28233 if (clear_mouse_face (hlinfo))
28234 cursor = No_Cursor;
28236 /* If no overlay applies, get a text property. */
28237 if (NILP (overlay))
28238 mouse_face = Fget_text_property (position, Qmouse_face, object);
28240 /* Next, compute the bounds of the mouse highlighting and
28241 display it. */
28242 if (!NILP (mouse_face) && STRINGP (object))
28244 /* The mouse-highlighting comes from a display string
28245 with a mouse-face. */
28246 Lisp_Object s, e;
28247 ptrdiff_t ignore;
28249 s = Fprevious_single_property_change
28250 (make_number (pos + 1), Qmouse_face, object, Qnil);
28251 e = Fnext_single_property_change
28252 (position, Qmouse_face, object, Qnil);
28253 if (NILP (s))
28254 s = make_number (0);
28255 if (NILP (e))
28256 e = make_number (SCHARS (object) - 1);
28257 mouse_face_from_string_pos (w, hlinfo, object,
28258 XINT (s), XINT (e));
28259 hlinfo->mouse_face_past_end = 0;
28260 hlinfo->mouse_face_window = window;
28261 hlinfo->mouse_face_face_id
28262 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28263 glyph->face_id, 1);
28264 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28265 cursor = No_Cursor;
28267 else
28269 /* The mouse-highlighting, if any, comes from an overlay
28270 or text property in the buffer. */
28271 Lisp_Object buffer IF_LINT (= Qnil);
28272 Lisp_Object disp_string IF_LINT (= Qnil);
28274 if (STRINGP (object))
28276 /* If we are on a display string with no mouse-face,
28277 check if the text under it has one. */
28278 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28279 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28280 pos = string_buffer_position (object, start);
28281 if (pos > 0)
28283 mouse_face = get_char_property_and_overlay
28284 (make_number (pos), Qmouse_face, w->contents, &overlay);
28285 buffer = w->contents;
28286 disp_string = object;
28289 else
28291 buffer = object;
28292 disp_string = Qnil;
28295 if (!NILP (mouse_face))
28297 Lisp_Object before, after;
28298 Lisp_Object before_string, after_string;
28299 /* To correctly find the limits of mouse highlight
28300 in a bidi-reordered buffer, we must not use the
28301 optimization of limiting the search in
28302 previous-single-property-change and
28303 next-single-property-change, because
28304 rows_from_pos_range needs the real start and end
28305 positions to DTRT in this case. That's because
28306 the first row visible in a window does not
28307 necessarily display the character whose position
28308 is the smallest. */
28309 Lisp_Object lim1 =
28310 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28311 ? Fmarker_position (w->start)
28312 : Qnil;
28313 Lisp_Object lim2 =
28314 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28315 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28316 : Qnil;
28318 if (NILP (overlay))
28320 /* Handle the text property case. */
28321 before = Fprevious_single_property_change
28322 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28323 after = Fnext_single_property_change
28324 (make_number (pos), Qmouse_face, buffer, lim2);
28325 before_string = after_string = Qnil;
28327 else
28329 /* Handle the overlay case. */
28330 before = Foverlay_start (overlay);
28331 after = Foverlay_end (overlay);
28332 before_string = Foverlay_get (overlay, Qbefore_string);
28333 after_string = Foverlay_get (overlay, Qafter_string);
28335 if (!STRINGP (before_string)) before_string = Qnil;
28336 if (!STRINGP (after_string)) after_string = Qnil;
28339 mouse_face_from_buffer_pos (window, hlinfo, pos,
28340 NILP (before)
28342 : XFASTINT (before),
28343 NILP (after)
28344 ? BUF_Z (XBUFFER (buffer))
28345 : XFASTINT (after),
28346 before_string, after_string,
28347 disp_string);
28348 cursor = No_Cursor;
28353 check_help_echo:
28355 /* Look for a `help-echo' property. */
28356 if (NILP (help_echo_string)) {
28357 Lisp_Object help, overlay;
28359 /* Check overlays first. */
28360 help = overlay = Qnil;
28361 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28363 overlay = overlay_vec[i];
28364 help = Foverlay_get (overlay, Qhelp_echo);
28367 if (!NILP (help))
28369 help_echo_string = help;
28370 help_echo_window = window;
28371 help_echo_object = overlay;
28372 help_echo_pos = pos;
28374 else
28376 Lisp_Object obj = glyph->object;
28377 ptrdiff_t charpos = glyph->charpos;
28379 /* Try text properties. */
28380 if (STRINGP (obj)
28381 && charpos >= 0
28382 && charpos < SCHARS (obj))
28384 help = Fget_text_property (make_number (charpos),
28385 Qhelp_echo, obj);
28386 if (NILP (help))
28388 /* If the string itself doesn't specify a help-echo,
28389 see if the buffer text ``under'' it does. */
28390 struct glyph_row *r
28391 = MATRIX_ROW (w->current_matrix, vpos);
28392 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28393 ptrdiff_t p = string_buffer_position (obj, start);
28394 if (p > 0)
28396 help = Fget_char_property (make_number (p),
28397 Qhelp_echo, w->contents);
28398 if (!NILP (help))
28400 charpos = p;
28401 obj = w->contents;
28406 else if (BUFFERP (obj)
28407 && charpos >= BEGV
28408 && charpos < ZV)
28409 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28410 obj);
28412 if (!NILP (help))
28414 help_echo_string = help;
28415 help_echo_window = window;
28416 help_echo_object = obj;
28417 help_echo_pos = charpos;
28422 #ifdef HAVE_WINDOW_SYSTEM
28423 /* Look for a `pointer' property. */
28424 if (FRAME_WINDOW_P (f) && NILP (pointer))
28426 /* Check overlays first. */
28427 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28428 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28430 if (NILP (pointer))
28432 Lisp_Object obj = glyph->object;
28433 ptrdiff_t charpos = glyph->charpos;
28435 /* Try text properties. */
28436 if (STRINGP (obj)
28437 && charpos >= 0
28438 && charpos < SCHARS (obj))
28440 pointer = Fget_text_property (make_number (charpos),
28441 Qpointer, obj);
28442 if (NILP (pointer))
28444 /* If the string itself doesn't specify a pointer,
28445 see if the buffer text ``under'' it does. */
28446 struct glyph_row *r
28447 = MATRIX_ROW (w->current_matrix, vpos);
28448 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28449 ptrdiff_t p = string_buffer_position (obj, start);
28450 if (p > 0)
28451 pointer = Fget_char_property (make_number (p),
28452 Qpointer, w->contents);
28455 else if (BUFFERP (obj)
28456 && charpos >= BEGV
28457 && charpos < ZV)
28458 pointer = Fget_text_property (make_number (charpos),
28459 Qpointer, obj);
28462 #endif /* HAVE_WINDOW_SYSTEM */
28464 BEGV = obegv;
28465 ZV = ozv;
28466 current_buffer = obuf;
28469 set_cursor:
28471 #ifdef HAVE_WINDOW_SYSTEM
28472 if (FRAME_WINDOW_P (f))
28473 define_frame_cursor1 (f, cursor, pointer);
28474 #else
28475 /* This is here to prevent a compiler error, about "label at end of
28476 compound statement". */
28477 return;
28478 #endif
28482 /* EXPORT for RIF:
28483 Clear any mouse-face on window W. This function is part of the
28484 redisplay interface, and is called from try_window_id and similar
28485 functions to ensure the mouse-highlight is off. */
28487 void
28488 x_clear_window_mouse_face (struct window *w)
28490 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28491 Lisp_Object window;
28493 block_input ();
28494 XSETWINDOW (window, w);
28495 if (EQ (window, hlinfo->mouse_face_window))
28496 clear_mouse_face (hlinfo);
28497 unblock_input ();
28501 /* EXPORT:
28502 Just discard the mouse face information for frame F, if any.
28503 This is used when the size of F is changed. */
28505 void
28506 cancel_mouse_face (struct frame *f)
28508 Lisp_Object window;
28509 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28511 window = hlinfo->mouse_face_window;
28512 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28513 reset_mouse_highlight (hlinfo);
28518 /***********************************************************************
28519 Exposure Events
28520 ***********************************************************************/
28522 #ifdef HAVE_WINDOW_SYSTEM
28524 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28525 which intersects rectangle R. R is in window-relative coordinates. */
28527 static void
28528 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28529 enum glyph_row_area area)
28531 struct glyph *first = row->glyphs[area];
28532 struct glyph *end = row->glyphs[area] + row->used[area];
28533 struct glyph *last;
28534 int first_x, start_x, x;
28536 if (area == TEXT_AREA && row->fill_line_p)
28537 /* If row extends face to end of line write the whole line. */
28538 draw_glyphs (w, 0, row, area,
28539 0, row->used[area],
28540 DRAW_NORMAL_TEXT, 0);
28541 else
28543 /* Set START_X to the window-relative start position for drawing glyphs of
28544 AREA. The first glyph of the text area can be partially visible.
28545 The first glyphs of other areas cannot. */
28546 start_x = window_box_left_offset (w, area);
28547 x = start_x;
28548 if (area == TEXT_AREA)
28549 x += row->x;
28551 /* Find the first glyph that must be redrawn. */
28552 while (first < end
28553 && x + first->pixel_width < r->x)
28555 x += first->pixel_width;
28556 ++first;
28559 /* Find the last one. */
28560 last = first;
28561 first_x = x;
28562 while (last < end
28563 && x < r->x + r->width)
28565 x += last->pixel_width;
28566 ++last;
28569 /* Repaint. */
28570 if (last > first)
28571 draw_glyphs (w, first_x - start_x, row, area,
28572 first - row->glyphs[area], last - row->glyphs[area],
28573 DRAW_NORMAL_TEXT, 0);
28578 /* Redraw the parts of the glyph row ROW on window W intersecting
28579 rectangle R. R is in window-relative coordinates. Value is
28580 non-zero if mouse-face was overwritten. */
28582 static int
28583 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28585 eassert (row->enabled_p);
28587 if (row->mode_line_p || w->pseudo_window_p)
28588 draw_glyphs (w, 0, row, TEXT_AREA,
28589 0, row->used[TEXT_AREA],
28590 DRAW_NORMAL_TEXT, 0);
28591 else
28593 if (row->used[LEFT_MARGIN_AREA])
28594 expose_area (w, row, r, LEFT_MARGIN_AREA);
28595 if (row->used[TEXT_AREA])
28596 expose_area (w, row, r, TEXT_AREA);
28597 if (row->used[RIGHT_MARGIN_AREA])
28598 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28599 draw_row_fringe_bitmaps (w, row);
28602 return row->mouse_face_p;
28606 /* Redraw those parts of glyphs rows during expose event handling that
28607 overlap other rows. Redrawing of an exposed line writes over parts
28608 of lines overlapping that exposed line; this function fixes that.
28610 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28611 row in W's current matrix that is exposed and overlaps other rows.
28612 LAST_OVERLAPPING_ROW is the last such row. */
28614 static void
28615 expose_overlaps (struct window *w,
28616 struct glyph_row *first_overlapping_row,
28617 struct glyph_row *last_overlapping_row,
28618 XRectangle *r)
28620 struct glyph_row *row;
28622 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28623 if (row->overlapping_p)
28625 eassert (row->enabled_p && !row->mode_line_p);
28627 row->clip = r;
28628 if (row->used[LEFT_MARGIN_AREA])
28629 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28631 if (row->used[TEXT_AREA])
28632 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28634 if (row->used[RIGHT_MARGIN_AREA])
28635 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28636 row->clip = NULL;
28641 /* Return non-zero if W's cursor intersects rectangle R. */
28643 static int
28644 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28646 XRectangle cr, result;
28647 struct glyph *cursor_glyph;
28648 struct glyph_row *row;
28650 if (w->phys_cursor.vpos >= 0
28651 && w->phys_cursor.vpos < w->current_matrix->nrows
28652 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28653 row->enabled_p)
28654 && row->cursor_in_fringe_p)
28656 /* Cursor is in the fringe. */
28657 cr.x = window_box_right_offset (w,
28658 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28659 ? RIGHT_MARGIN_AREA
28660 : TEXT_AREA));
28661 cr.y = row->y;
28662 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28663 cr.height = row->height;
28664 return x_intersect_rectangles (&cr, r, &result);
28667 cursor_glyph = get_phys_cursor_glyph (w);
28668 if (cursor_glyph)
28670 /* r is relative to W's box, but w->phys_cursor.x is relative
28671 to left edge of W's TEXT area. Adjust it. */
28672 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28673 cr.y = w->phys_cursor.y;
28674 cr.width = cursor_glyph->pixel_width;
28675 cr.height = w->phys_cursor_height;
28676 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28677 I assume the effect is the same -- and this is portable. */
28678 return x_intersect_rectangles (&cr, r, &result);
28680 /* If we don't understand the format, pretend we're not in the hot-spot. */
28681 return 0;
28685 /* EXPORT:
28686 Draw a vertical window border to the right of window W if W doesn't
28687 have vertical scroll bars. */
28689 void
28690 x_draw_vertical_border (struct window *w)
28692 struct frame *f = XFRAME (WINDOW_FRAME (w));
28694 /* We could do better, if we knew what type of scroll-bar the adjacent
28695 windows (on either side) have... But we don't :-(
28696 However, I think this works ok. ++KFS 2003-04-25 */
28698 /* Redraw borders between horizontally adjacent windows. Don't
28699 do it for frames with vertical scroll bars because either the
28700 right scroll bar of a window, or the left scroll bar of its
28701 neighbor will suffice as a border. */
28702 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28703 return;
28705 /* Note: It is necessary to redraw both the left and the right
28706 borders, for when only this single window W is being
28707 redisplayed. */
28708 if (!WINDOW_RIGHTMOST_P (w)
28709 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28711 int x0, x1, y0, y1;
28713 window_box_edges (w, &x0, &y0, &x1, &y1);
28714 y1 -= 1;
28716 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28717 x1 -= 1;
28719 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28721 if (!WINDOW_LEFTMOST_P (w)
28722 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28724 int x0, x1, y0, y1;
28726 window_box_edges (w, &x0, &y0, &x1, &y1);
28727 y1 -= 1;
28729 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28730 x0 -= 1;
28732 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28737 /* Redraw the part of window W intersection rectangle FR. Pixel
28738 coordinates in FR are frame-relative. Call this function with
28739 input blocked. Value is non-zero if the exposure overwrites
28740 mouse-face. */
28742 static int
28743 expose_window (struct window *w, XRectangle *fr)
28745 struct frame *f = XFRAME (w->frame);
28746 XRectangle wr, r;
28747 int mouse_face_overwritten_p = 0;
28749 /* If window is not yet fully initialized, do nothing. This can
28750 happen when toolkit scroll bars are used and a window is split.
28751 Reconfiguring the scroll bar will generate an expose for a newly
28752 created window. */
28753 if (w->current_matrix == NULL)
28754 return 0;
28756 /* When we're currently updating the window, display and current
28757 matrix usually don't agree. Arrange for a thorough display
28758 later. */
28759 if (w->must_be_updated_p)
28761 SET_FRAME_GARBAGED (f);
28762 return 0;
28765 /* Frame-relative pixel rectangle of W. */
28766 wr.x = WINDOW_LEFT_EDGE_X (w);
28767 wr.y = WINDOW_TOP_EDGE_Y (w);
28768 wr.width = WINDOW_TOTAL_WIDTH (w);
28769 wr.height = WINDOW_TOTAL_HEIGHT (w);
28771 if (x_intersect_rectangles (fr, &wr, &r))
28773 int yb = window_text_bottom_y (w);
28774 struct glyph_row *row;
28775 int cursor_cleared_p, phys_cursor_on_p;
28776 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28778 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28779 r.x, r.y, r.width, r.height));
28781 /* Convert to window coordinates. */
28782 r.x -= WINDOW_LEFT_EDGE_X (w);
28783 r.y -= WINDOW_TOP_EDGE_Y (w);
28785 /* Turn off the cursor. */
28786 if (!w->pseudo_window_p
28787 && phys_cursor_in_rect_p (w, &r))
28789 x_clear_cursor (w);
28790 cursor_cleared_p = 1;
28792 else
28793 cursor_cleared_p = 0;
28795 /* If the row containing the cursor extends face to end of line,
28796 then expose_area might overwrite the cursor outside the
28797 rectangle and thus notice_overwritten_cursor might clear
28798 w->phys_cursor_on_p. We remember the original value and
28799 check later if it is changed. */
28800 phys_cursor_on_p = w->phys_cursor_on_p;
28802 /* Update lines intersecting rectangle R. */
28803 first_overlapping_row = last_overlapping_row = NULL;
28804 for (row = w->current_matrix->rows;
28805 row->enabled_p;
28806 ++row)
28808 int y0 = row->y;
28809 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28811 if ((y0 >= r.y && y0 < r.y + r.height)
28812 || (y1 > r.y && y1 < r.y + r.height)
28813 || (r.y >= y0 && r.y < y1)
28814 || (r.y + r.height > y0 && r.y + r.height < y1))
28816 /* A header line may be overlapping, but there is no need
28817 to fix overlapping areas for them. KFS 2005-02-12 */
28818 if (row->overlapping_p && !row->mode_line_p)
28820 if (first_overlapping_row == NULL)
28821 first_overlapping_row = row;
28822 last_overlapping_row = row;
28825 row->clip = fr;
28826 if (expose_line (w, row, &r))
28827 mouse_face_overwritten_p = 1;
28828 row->clip = NULL;
28830 else if (row->overlapping_p)
28832 /* We must redraw a row overlapping the exposed area. */
28833 if (y0 < r.y
28834 ? y0 + row->phys_height > r.y
28835 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28837 if (first_overlapping_row == NULL)
28838 first_overlapping_row = row;
28839 last_overlapping_row = row;
28843 if (y1 >= yb)
28844 break;
28847 /* Display the mode line if there is one. */
28848 if (WINDOW_WANTS_MODELINE_P (w)
28849 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28850 row->enabled_p)
28851 && row->y < r.y + r.height)
28853 if (expose_line (w, row, &r))
28854 mouse_face_overwritten_p = 1;
28857 if (!w->pseudo_window_p)
28859 /* Fix the display of overlapping rows. */
28860 if (first_overlapping_row)
28861 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28862 fr);
28864 /* Draw border between windows. */
28865 x_draw_vertical_border (w);
28867 /* Turn the cursor on again. */
28868 if (cursor_cleared_p
28869 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28870 update_window_cursor (w, 1);
28874 return mouse_face_overwritten_p;
28879 /* Redraw (parts) of all windows in the window tree rooted at W that
28880 intersect R. R contains frame pixel coordinates. Value is
28881 non-zero if the exposure overwrites mouse-face. */
28883 static int
28884 expose_window_tree (struct window *w, XRectangle *r)
28886 struct frame *f = XFRAME (w->frame);
28887 int mouse_face_overwritten_p = 0;
28889 while (w && !FRAME_GARBAGED_P (f))
28891 if (WINDOWP (w->contents))
28892 mouse_face_overwritten_p
28893 |= expose_window_tree (XWINDOW (w->contents), r);
28894 else
28895 mouse_face_overwritten_p |= expose_window (w, r);
28897 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28900 return mouse_face_overwritten_p;
28904 /* EXPORT:
28905 Redisplay an exposed area of frame F. X and Y are the upper-left
28906 corner of the exposed rectangle. W and H are width and height of
28907 the exposed area. All are pixel values. W or H zero means redraw
28908 the entire frame. */
28910 void
28911 expose_frame (struct frame *f, int x, int y, int w, int h)
28913 XRectangle r;
28914 int mouse_face_overwritten_p = 0;
28916 TRACE ((stderr, "expose_frame "));
28918 /* No need to redraw if frame will be redrawn soon. */
28919 if (FRAME_GARBAGED_P (f))
28921 TRACE ((stderr, " garbaged\n"));
28922 return;
28925 /* If basic faces haven't been realized yet, there is no point in
28926 trying to redraw anything. This can happen when we get an expose
28927 event while Emacs is starting, e.g. by moving another window. */
28928 if (FRAME_FACE_CACHE (f) == NULL
28929 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28931 TRACE ((stderr, " no faces\n"));
28932 return;
28935 if (w == 0 || h == 0)
28937 r.x = r.y = 0;
28938 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28939 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28941 else
28943 r.x = x;
28944 r.y = y;
28945 r.width = w;
28946 r.height = h;
28949 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28950 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28952 if (WINDOWP (f->tool_bar_window))
28953 mouse_face_overwritten_p
28954 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28956 #ifdef HAVE_X_WINDOWS
28957 #ifndef MSDOS
28958 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28959 if (WINDOWP (f->menu_bar_window))
28960 mouse_face_overwritten_p
28961 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28962 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28963 #endif
28964 #endif
28966 /* Some window managers support a focus-follows-mouse style with
28967 delayed raising of frames. Imagine a partially obscured frame,
28968 and moving the mouse into partially obscured mouse-face on that
28969 frame. The visible part of the mouse-face will be highlighted,
28970 then the WM raises the obscured frame. With at least one WM, KDE
28971 2.1, Emacs is not getting any event for the raising of the frame
28972 (even tried with SubstructureRedirectMask), only Expose events.
28973 These expose events will draw text normally, i.e. not
28974 highlighted. Which means we must redo the highlight here.
28975 Subsume it under ``we love X''. --gerd 2001-08-15 */
28976 /* Included in Windows version because Windows most likely does not
28977 do the right thing if any third party tool offers
28978 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28979 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28981 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28982 if (f == hlinfo->mouse_face_mouse_frame)
28984 int mouse_x = hlinfo->mouse_face_mouse_x;
28985 int mouse_y = hlinfo->mouse_face_mouse_y;
28986 clear_mouse_face (hlinfo);
28987 note_mouse_highlight (f, mouse_x, mouse_y);
28993 /* EXPORT:
28994 Determine the intersection of two rectangles R1 and R2. Return
28995 the intersection in *RESULT. Value is non-zero if RESULT is not
28996 empty. */
28999 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29001 XRectangle *left, *right;
29002 XRectangle *upper, *lower;
29003 int intersection_p = 0;
29005 /* Rearrange so that R1 is the left-most rectangle. */
29006 if (r1->x < r2->x)
29007 left = r1, right = r2;
29008 else
29009 left = r2, right = r1;
29011 /* X0 of the intersection is right.x0, if this is inside R1,
29012 otherwise there is no intersection. */
29013 if (right->x <= left->x + left->width)
29015 result->x = right->x;
29017 /* The right end of the intersection is the minimum of
29018 the right ends of left and right. */
29019 result->width = (min (left->x + left->width, right->x + right->width)
29020 - result->x);
29022 /* Same game for Y. */
29023 if (r1->y < r2->y)
29024 upper = r1, lower = r2;
29025 else
29026 upper = r2, lower = r1;
29028 /* The upper end of the intersection is lower.y0, if this is inside
29029 of upper. Otherwise, there is no intersection. */
29030 if (lower->y <= upper->y + upper->height)
29032 result->y = lower->y;
29034 /* The lower end of the intersection is the minimum of the lower
29035 ends of upper and lower. */
29036 result->height = (min (lower->y + lower->height,
29037 upper->y + upper->height)
29038 - result->y);
29039 intersection_p = 1;
29043 return intersection_p;
29046 #endif /* HAVE_WINDOW_SYSTEM */
29049 /***********************************************************************
29050 Initialization
29051 ***********************************************************************/
29053 void
29054 syms_of_xdisp (void)
29056 Vwith_echo_area_save_vector = Qnil;
29057 staticpro (&Vwith_echo_area_save_vector);
29059 Vmessage_stack = Qnil;
29060 staticpro (&Vmessage_stack);
29062 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29063 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29065 message_dolog_marker1 = Fmake_marker ();
29066 staticpro (&message_dolog_marker1);
29067 message_dolog_marker2 = Fmake_marker ();
29068 staticpro (&message_dolog_marker2);
29069 message_dolog_marker3 = Fmake_marker ();
29070 staticpro (&message_dolog_marker3);
29072 #ifdef GLYPH_DEBUG
29073 defsubr (&Sdump_frame_glyph_matrix);
29074 defsubr (&Sdump_glyph_matrix);
29075 defsubr (&Sdump_glyph_row);
29076 defsubr (&Sdump_tool_bar_row);
29077 defsubr (&Strace_redisplay);
29078 defsubr (&Strace_to_stderr);
29079 #endif
29080 #ifdef HAVE_WINDOW_SYSTEM
29081 defsubr (&Stool_bar_lines_needed);
29082 defsubr (&Slookup_image_map);
29083 #endif
29084 defsubr (&Sline_pixel_height);
29085 defsubr (&Sformat_mode_line);
29086 defsubr (&Sinvisible_p);
29087 defsubr (&Scurrent_bidi_paragraph_direction);
29088 defsubr (&Smove_point_visually);
29090 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29091 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29092 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29093 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29094 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29095 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29096 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29097 DEFSYM (Qeval, "eval");
29098 DEFSYM (QCdata, ":data");
29099 DEFSYM (Qdisplay, "display");
29100 DEFSYM (Qspace_width, "space-width");
29101 DEFSYM (Qraise, "raise");
29102 DEFSYM (Qslice, "slice");
29103 DEFSYM (Qspace, "space");
29104 DEFSYM (Qmargin, "margin");
29105 DEFSYM (Qpointer, "pointer");
29106 DEFSYM (Qleft_margin, "left-margin");
29107 DEFSYM (Qright_margin, "right-margin");
29108 DEFSYM (Qcenter, "center");
29109 DEFSYM (Qline_height, "line-height");
29110 DEFSYM (QCalign_to, ":align-to");
29111 DEFSYM (QCrelative_width, ":relative-width");
29112 DEFSYM (QCrelative_height, ":relative-height");
29113 DEFSYM (QCeval, ":eval");
29114 DEFSYM (QCpropertize, ":propertize");
29115 DEFSYM (QCfile, ":file");
29116 DEFSYM (Qfontified, "fontified");
29117 DEFSYM (Qfontification_functions, "fontification-functions");
29118 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29119 DEFSYM (Qescape_glyph, "escape-glyph");
29120 DEFSYM (Qnobreak_space, "nobreak-space");
29121 DEFSYM (Qimage, "image");
29122 DEFSYM (Qtext, "text");
29123 DEFSYM (Qboth, "both");
29124 DEFSYM (Qboth_horiz, "both-horiz");
29125 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29126 DEFSYM (QCmap, ":map");
29127 DEFSYM (QCpointer, ":pointer");
29128 DEFSYM (Qrect, "rect");
29129 DEFSYM (Qcircle, "circle");
29130 DEFSYM (Qpoly, "poly");
29131 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29132 DEFSYM (Qgrow_only, "grow-only");
29133 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29134 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29135 DEFSYM (Qposition, "position");
29136 DEFSYM (Qbuffer_position, "buffer-position");
29137 DEFSYM (Qobject, "object");
29138 DEFSYM (Qbar, "bar");
29139 DEFSYM (Qhbar, "hbar");
29140 DEFSYM (Qbox, "box");
29141 DEFSYM (Qhollow, "hollow");
29142 DEFSYM (Qhand, "hand");
29143 DEFSYM (Qarrow, "arrow");
29144 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29146 list_of_error = list1 (list2 (intern_c_string ("error"),
29147 intern_c_string ("void-variable")));
29148 staticpro (&list_of_error);
29150 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29151 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29152 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29153 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29155 echo_buffer[0] = echo_buffer[1] = Qnil;
29156 staticpro (&echo_buffer[0]);
29157 staticpro (&echo_buffer[1]);
29159 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29160 staticpro (&echo_area_buffer[0]);
29161 staticpro (&echo_area_buffer[1]);
29163 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29164 staticpro (&Vmessages_buffer_name);
29166 mode_line_proptrans_alist = Qnil;
29167 staticpro (&mode_line_proptrans_alist);
29168 mode_line_string_list = Qnil;
29169 staticpro (&mode_line_string_list);
29170 mode_line_string_face = Qnil;
29171 staticpro (&mode_line_string_face);
29172 mode_line_string_face_prop = Qnil;
29173 staticpro (&mode_line_string_face_prop);
29174 Vmode_line_unwind_vector = Qnil;
29175 staticpro (&Vmode_line_unwind_vector);
29177 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29179 help_echo_string = Qnil;
29180 staticpro (&help_echo_string);
29181 help_echo_object = Qnil;
29182 staticpro (&help_echo_object);
29183 help_echo_window = Qnil;
29184 staticpro (&help_echo_window);
29185 previous_help_echo_string = Qnil;
29186 staticpro (&previous_help_echo_string);
29187 help_echo_pos = -1;
29189 DEFSYM (Qright_to_left, "right-to-left");
29190 DEFSYM (Qleft_to_right, "left-to-right");
29192 #ifdef HAVE_WINDOW_SYSTEM
29193 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29194 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29195 For example, if a block cursor is over a tab, it will be drawn as
29196 wide as that tab on the display. */);
29197 x_stretch_cursor_p = 0;
29198 #endif
29200 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29201 doc: /* Non-nil means highlight trailing whitespace.
29202 The face used for trailing whitespace is `trailing-whitespace'. */);
29203 Vshow_trailing_whitespace = Qnil;
29205 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29206 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29207 If the value is t, Emacs highlights non-ASCII chars which have the
29208 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29209 or `escape-glyph' face respectively.
29211 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29212 U+2011 (non-breaking hyphen) are affected.
29214 Any other non-nil value means to display these characters as a escape
29215 glyph followed by an ordinary space or hyphen.
29217 A value of nil means no special handling of these characters. */);
29218 Vnobreak_char_display = Qt;
29220 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29221 doc: /* The pointer shape to show in void text areas.
29222 A value of nil means to show the text pointer. Other options are `arrow',
29223 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29224 Vvoid_text_area_pointer = Qarrow;
29226 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29227 doc: /* Non-nil means don't actually do any redisplay.
29228 This is used for internal purposes. */);
29229 Vinhibit_redisplay = Qnil;
29231 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29232 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29233 Vglobal_mode_string = Qnil;
29235 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29236 doc: /* Marker for where to display an arrow on top of the buffer text.
29237 This must be the beginning of a line in order to work.
29238 See also `overlay-arrow-string'. */);
29239 Voverlay_arrow_position = Qnil;
29241 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29242 doc: /* String to display as an arrow in non-window frames.
29243 See also `overlay-arrow-position'. */);
29244 Voverlay_arrow_string = build_pure_c_string ("=>");
29246 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29247 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29248 The symbols on this list are examined during redisplay to determine
29249 where to display overlay arrows. */);
29250 Voverlay_arrow_variable_list
29251 = list1 (intern_c_string ("overlay-arrow-position"));
29253 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29254 doc: /* The number of lines to try scrolling a window by when point moves out.
29255 If that fails to bring point back on frame, point is centered instead.
29256 If this is zero, point is always centered after it moves off frame.
29257 If you want scrolling to always be a line at a time, you should set
29258 `scroll-conservatively' to a large value rather than set this to 1. */);
29260 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29261 doc: /* Scroll up to this many lines, to bring point back on screen.
29262 If point moves off-screen, redisplay will scroll by up to
29263 `scroll-conservatively' lines in order to bring point just barely
29264 onto the screen again. If that cannot be done, then redisplay
29265 recenters point as usual.
29267 If the value is greater than 100, redisplay will never recenter point,
29268 but will always scroll just enough text to bring point into view, even
29269 if you move far away.
29271 A value of zero means always recenter point if it moves off screen. */);
29272 scroll_conservatively = 0;
29274 DEFVAR_INT ("scroll-margin", scroll_margin,
29275 doc: /* Number of lines of margin at the top and bottom of a window.
29276 Recenter the window whenever point gets within this many lines
29277 of the top or bottom of the window. */);
29278 scroll_margin = 0;
29280 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29281 doc: /* Pixels per inch value for non-window system displays.
29282 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29283 Vdisplay_pixels_per_inch = make_float (72.0);
29285 #ifdef GLYPH_DEBUG
29286 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29287 #endif
29289 DEFVAR_LISP ("truncate-partial-width-windows",
29290 Vtruncate_partial_width_windows,
29291 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29292 For an integer value, truncate lines in each window narrower than the
29293 full frame width, provided the window width is less than that integer;
29294 otherwise, respect the value of `truncate-lines'.
29296 For any other non-nil value, truncate lines in all windows that do
29297 not span the full frame width.
29299 A value of nil means to respect the value of `truncate-lines'.
29301 If `word-wrap' is enabled, you might want to reduce this. */);
29302 Vtruncate_partial_width_windows = make_number (50);
29304 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29305 doc: /* Maximum buffer size for which line number should be displayed.
29306 If the buffer is bigger than this, the line number does not appear
29307 in the mode line. A value of nil means no limit. */);
29308 Vline_number_display_limit = Qnil;
29310 DEFVAR_INT ("line-number-display-limit-width",
29311 line_number_display_limit_width,
29312 doc: /* Maximum line width (in characters) for line number display.
29313 If the average length of the lines near point is bigger than this, then the
29314 line number may be omitted from the mode line. */);
29315 line_number_display_limit_width = 200;
29317 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29318 doc: /* Non-nil means highlight region even in nonselected windows. */);
29319 highlight_nonselected_windows = 0;
29321 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29322 doc: /* Non-nil if more than one frame is visible on this display.
29323 Minibuffer-only frames don't count, but iconified frames do.
29324 This variable is not guaranteed to be accurate except while processing
29325 `frame-title-format' and `icon-title-format'. */);
29327 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29328 doc: /* Template for displaying the title bar of visible frames.
29329 \(Assuming the window manager supports this feature.)
29331 This variable has the same structure as `mode-line-format', except that
29332 the %c and %l constructs are ignored. It is used only on frames for
29333 which no explicit name has been set \(see `modify-frame-parameters'). */);
29335 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29336 doc: /* Template for displaying the title bar of an iconified frame.
29337 \(Assuming the window manager supports this feature.)
29338 This variable has the same structure as `mode-line-format' (which see),
29339 and is used only on frames for which no explicit name has been set
29340 \(see `modify-frame-parameters'). */);
29341 Vicon_title_format
29342 = Vframe_title_format
29343 = listn (CONSTYPE_PURE, 3,
29344 intern_c_string ("multiple-frames"),
29345 build_pure_c_string ("%b"),
29346 listn (CONSTYPE_PURE, 4,
29347 empty_unibyte_string,
29348 intern_c_string ("invocation-name"),
29349 build_pure_c_string ("@"),
29350 intern_c_string ("system-name")));
29352 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29353 doc: /* Maximum number of lines to keep in the message log buffer.
29354 If nil, disable message logging. If t, log messages but don't truncate
29355 the buffer when it becomes large. */);
29356 Vmessage_log_max = make_number (1000);
29358 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29359 doc: /* Functions called before redisplay, if window sizes have changed.
29360 The value should be a list of functions that take one argument.
29361 Just before redisplay, for each frame, if any of its windows have changed
29362 size since the last redisplay, or have been split or deleted,
29363 all the functions in the list are called, with the frame as argument. */);
29364 Vwindow_size_change_functions = Qnil;
29366 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29367 doc: /* List of functions to call before redisplaying a window with scrolling.
29368 Each function is called with two arguments, the window and its new
29369 display-start position. Note that these functions are also called by
29370 `set-window-buffer'. Also note that the value of `window-end' is not
29371 valid when these functions are called.
29373 Warning: Do not use this feature to alter the way the window
29374 is scrolled. It is not designed for that, and such use probably won't
29375 work. */);
29376 Vwindow_scroll_functions = Qnil;
29378 DEFVAR_LISP ("window-text-change-functions",
29379 Vwindow_text_change_functions,
29380 doc: /* Functions to call in redisplay when text in the window might change. */);
29381 Vwindow_text_change_functions = Qnil;
29383 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29384 doc: /* Functions called when redisplay of a window reaches the end trigger.
29385 Each function is called with two arguments, the window and the end trigger value.
29386 See `set-window-redisplay-end-trigger'. */);
29387 Vredisplay_end_trigger_functions = Qnil;
29389 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29390 doc: /* Non-nil means autoselect window with mouse pointer.
29391 If nil, do not autoselect windows.
29392 A positive number means delay autoselection by that many seconds: a
29393 window is autoselected only after the mouse has remained in that
29394 window for the duration of the delay.
29395 A negative number has a similar effect, but causes windows to be
29396 autoselected only after the mouse has stopped moving. \(Because of
29397 the way Emacs compares mouse events, you will occasionally wait twice
29398 that time before the window gets selected.\)
29399 Any other value means to autoselect window instantaneously when the
29400 mouse pointer enters it.
29402 Autoselection selects the minibuffer only if it is active, and never
29403 unselects the minibuffer if it is active.
29405 When customizing this variable make sure that the actual value of
29406 `focus-follows-mouse' matches the behavior of your window manager. */);
29407 Vmouse_autoselect_window = Qnil;
29409 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29410 doc: /* Non-nil means automatically resize tool-bars.
29411 This dynamically changes the tool-bar's height to the minimum height
29412 that is needed to make all tool-bar items visible.
29413 If value is `grow-only', the tool-bar's height is only increased
29414 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29415 Vauto_resize_tool_bars = Qt;
29417 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29418 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29419 auto_raise_tool_bar_buttons_p = 1;
29421 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29422 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29423 make_cursor_line_fully_visible_p = 1;
29425 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29426 doc: /* Border below tool-bar in pixels.
29427 If an integer, use it as the height of the border.
29428 If it is one of `internal-border-width' or `border-width', use the
29429 value of the corresponding frame parameter.
29430 Otherwise, no border is added below the tool-bar. */);
29431 Vtool_bar_border = Qinternal_border_width;
29433 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29434 doc: /* Margin around tool-bar buttons in pixels.
29435 If an integer, use that for both horizontal and vertical margins.
29436 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29437 HORZ specifying the horizontal margin, and VERT specifying the
29438 vertical margin. */);
29439 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29441 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29442 doc: /* Relief thickness of tool-bar buttons. */);
29443 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29445 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29446 doc: /* Tool bar style to use.
29447 It can be one of
29448 image - show images only
29449 text - show text only
29450 both - show both, text below image
29451 both-horiz - show text to the right of the image
29452 text-image-horiz - show text to the left of the image
29453 any other - use system default or image if no system default.
29455 This variable only affects the GTK+ toolkit version of Emacs. */);
29456 Vtool_bar_style = Qnil;
29458 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29459 doc: /* Maximum number of characters a label can have to be shown.
29460 The tool bar style must also show labels for this to have any effect, see
29461 `tool-bar-style'. */);
29462 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29464 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29465 doc: /* List of functions to call to fontify regions of text.
29466 Each function is called with one argument POS. Functions must
29467 fontify a region starting at POS in the current buffer, and give
29468 fontified regions the property `fontified'. */);
29469 Vfontification_functions = Qnil;
29470 Fmake_variable_buffer_local (Qfontification_functions);
29472 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29473 unibyte_display_via_language_environment,
29474 doc: /* Non-nil means display unibyte text according to language environment.
29475 Specifically, this means that raw bytes in the range 160-255 decimal
29476 are displayed by converting them to the equivalent multibyte characters
29477 according to the current language environment. As a result, they are
29478 displayed according to the current fontset.
29480 Note that this variable affects only how these bytes are displayed,
29481 but does not change the fact they are interpreted as raw bytes. */);
29482 unibyte_display_via_language_environment = 0;
29484 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29485 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29486 If a float, it specifies a fraction of the mini-window frame's height.
29487 If an integer, it specifies a number of lines. */);
29488 Vmax_mini_window_height = make_float (0.25);
29490 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29491 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29492 A value of nil means don't automatically resize mini-windows.
29493 A value of t means resize them to fit the text displayed in them.
29494 A value of `grow-only', the default, means let mini-windows grow only;
29495 they return to their normal size when the minibuffer is closed, or the
29496 echo area becomes empty. */);
29497 Vresize_mini_windows = Qgrow_only;
29499 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29500 doc: /* Alist specifying how to blink the cursor off.
29501 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29502 `cursor-type' frame-parameter or variable equals ON-STATE,
29503 comparing using `equal', Emacs uses OFF-STATE to specify
29504 how to blink it off. ON-STATE and OFF-STATE are values for
29505 the `cursor-type' frame parameter.
29507 If a frame's ON-STATE has no entry in this list,
29508 the frame's other specifications determine how to blink the cursor off. */);
29509 Vblink_cursor_alist = Qnil;
29511 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29512 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29513 If non-nil, windows are automatically scrolled horizontally to make
29514 point visible. */);
29515 automatic_hscrolling_p = 1;
29516 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29518 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29519 doc: /* How many columns away from the window edge point is allowed to get
29520 before automatic hscrolling will horizontally scroll the window. */);
29521 hscroll_margin = 5;
29523 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29524 doc: /* How many columns to scroll the window when point gets too close to the edge.
29525 When point is less than `hscroll-margin' columns from the window
29526 edge, automatic hscrolling will scroll the window by the amount of columns
29527 determined by this variable. If its value is a positive integer, scroll that
29528 many columns. If it's a positive floating-point number, it specifies the
29529 fraction of the window's width to scroll. If it's nil or zero, point will be
29530 centered horizontally after the scroll. Any other value, including negative
29531 numbers, are treated as if the value were zero.
29533 Automatic hscrolling always moves point outside the scroll margin, so if
29534 point was more than scroll step columns inside the margin, the window will
29535 scroll more than the value given by the scroll step.
29537 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29538 and `scroll-right' overrides this variable's effect. */);
29539 Vhscroll_step = make_number (0);
29541 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29542 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29543 Bind this around calls to `message' to let it take effect. */);
29544 message_truncate_lines = 0;
29546 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29547 doc: /* Normal hook run to update the menu bar definitions.
29548 Redisplay runs this hook before it redisplays the menu bar.
29549 This is used to update submenus such as Buffers,
29550 whose contents depend on various data. */);
29551 Vmenu_bar_update_hook = Qnil;
29553 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29554 doc: /* Frame for which we are updating a menu.
29555 The enable predicate for a menu binding should check this variable. */);
29556 Vmenu_updating_frame = Qnil;
29558 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29559 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29560 inhibit_menubar_update = 0;
29562 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29563 doc: /* Prefix prepended to all continuation lines at display time.
29564 The value may be a string, an image, or a stretch-glyph; it is
29565 interpreted in the same way as the value of a `display' text property.
29567 This variable is overridden by any `wrap-prefix' text or overlay
29568 property.
29570 To add a prefix to non-continuation lines, use `line-prefix'. */);
29571 Vwrap_prefix = Qnil;
29572 DEFSYM (Qwrap_prefix, "wrap-prefix");
29573 Fmake_variable_buffer_local (Qwrap_prefix);
29575 DEFVAR_LISP ("line-prefix", Vline_prefix,
29576 doc: /* Prefix prepended to all non-continuation lines at display time.
29577 The value may be a string, an image, or a stretch-glyph; it is
29578 interpreted in the same way as the value of a `display' text property.
29580 This variable is overridden by any `line-prefix' text or overlay
29581 property.
29583 To add a prefix to continuation lines, use `wrap-prefix'. */);
29584 Vline_prefix = Qnil;
29585 DEFSYM (Qline_prefix, "line-prefix");
29586 Fmake_variable_buffer_local (Qline_prefix);
29588 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29589 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29590 inhibit_eval_during_redisplay = 0;
29592 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29593 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29594 inhibit_free_realized_faces = 0;
29596 #ifdef GLYPH_DEBUG
29597 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29598 doc: /* Inhibit try_window_id display optimization. */);
29599 inhibit_try_window_id = 0;
29601 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29602 doc: /* Inhibit try_window_reusing display optimization. */);
29603 inhibit_try_window_reusing = 0;
29605 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29606 doc: /* Inhibit try_cursor_movement display optimization. */);
29607 inhibit_try_cursor_movement = 0;
29608 #endif /* GLYPH_DEBUG */
29610 DEFVAR_INT ("overline-margin", overline_margin,
29611 doc: /* Space between overline and text, in pixels.
29612 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29613 margin to the character height. */);
29614 overline_margin = 2;
29616 DEFVAR_INT ("underline-minimum-offset",
29617 underline_minimum_offset,
29618 doc: /* Minimum distance between baseline and underline.
29619 This can improve legibility of underlined text at small font sizes,
29620 particularly when using variable `x-use-underline-position-properties'
29621 with fonts that specify an UNDERLINE_POSITION relatively close to the
29622 baseline. The default value is 1. */);
29623 underline_minimum_offset = 1;
29625 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29626 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29627 This feature only works when on a window system that can change
29628 cursor shapes. */);
29629 display_hourglass_p = 1;
29631 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29632 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29633 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29635 #ifdef HAVE_WINDOW_SYSTEM
29636 hourglass_atimer = NULL;
29637 hourglass_shown_p = 0;
29638 #endif /* HAVE_WINDOW_SYSTEM */
29640 DEFSYM (Qglyphless_char, "glyphless-char");
29641 DEFSYM (Qhex_code, "hex-code");
29642 DEFSYM (Qempty_box, "empty-box");
29643 DEFSYM (Qthin_space, "thin-space");
29644 DEFSYM (Qzero_width, "zero-width");
29646 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29647 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29649 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29650 doc: /* Char-table defining glyphless characters.
29651 Each element, if non-nil, should be one of the following:
29652 an ASCII acronym string: display this string in a box
29653 `hex-code': display the hexadecimal code of a character in a box
29654 `empty-box': display as an empty box
29655 `thin-space': display as 1-pixel width space
29656 `zero-width': don't display
29657 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29658 display method for graphical terminals and text terminals respectively.
29659 GRAPHICAL and TEXT should each have one of the values listed above.
29661 The char-table has one extra slot to control the display of a character for
29662 which no font is found. This slot only takes effect on graphical terminals.
29663 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29664 `thin-space'. The default is `empty-box'. */);
29665 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29666 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29667 Qempty_box);
29669 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29670 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29671 Vdebug_on_message = Qnil;
29675 /* Initialize this module when Emacs starts. */
29677 void
29678 init_xdisp (void)
29680 CHARPOS (this_line_start_pos) = 0;
29682 if (!noninteractive)
29684 struct window *m = XWINDOW (minibuf_window);
29685 Lisp_Object frame = m->frame;
29686 struct frame *f = XFRAME (frame);
29687 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29688 struct window *r = XWINDOW (root);
29689 int i;
29691 echo_area_window = minibuf_window;
29693 r->top_line = FRAME_TOP_MARGIN (f);
29694 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29695 r->total_cols = FRAME_COLS (f);
29697 m->top_line = FRAME_LINES (f) - 1;
29698 m->total_lines = 1;
29699 m->total_cols = FRAME_COLS (f);
29701 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29702 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29703 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29705 /* The default ellipsis glyphs `...'. */
29706 for (i = 0; i < 3; ++i)
29707 default_invis_vector[i] = make_number ('.');
29711 /* Allocate the buffer for frame titles.
29712 Also used for `format-mode-line'. */
29713 int size = 100;
29714 mode_line_noprop_buf = xmalloc (size);
29715 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29716 mode_line_noprop_ptr = mode_line_noprop_buf;
29717 mode_line_target = MODE_LINE_DISPLAY;
29720 help_echo_showing_p = 0;
29723 #ifdef HAVE_WINDOW_SYSTEM
29725 /* Platform-independent portion of hourglass implementation. */
29727 /* Cancel a currently active hourglass timer, and start a new one. */
29728 void
29729 start_hourglass (void)
29731 struct timespec delay;
29733 cancel_hourglass ();
29735 if (INTEGERP (Vhourglass_delay)
29736 && XINT (Vhourglass_delay) > 0)
29737 delay = make_timespec (min (XINT (Vhourglass_delay),
29738 TYPE_MAXIMUM (time_t)),
29740 else if (FLOATP (Vhourglass_delay)
29741 && XFLOAT_DATA (Vhourglass_delay) > 0)
29742 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29743 else
29744 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29746 #ifdef HAVE_NTGUI
29748 extern void w32_note_current_window (void);
29749 w32_note_current_window ();
29751 #endif /* HAVE_NTGUI */
29753 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29754 show_hourglass, NULL);
29758 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29759 shown. */
29760 void
29761 cancel_hourglass (void)
29763 if (hourglass_atimer)
29765 cancel_atimer (hourglass_atimer);
29766 hourglass_atimer = NULL;
29769 if (hourglass_shown_p)
29770 hide_hourglass ();
29773 #endif /* HAVE_WINDOW_SYSTEM */