* calendar/todo-mode.el: Add handling of file deletion, both by
[emacs.git] / src / xdisp.c
blobb61b976a40102462b19c7e87b8a9ea6b6dd3072e
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
103 . try_window
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
116 Desired matrices.
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
161 Frame matrices.
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
182 Bidirectional display.
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
225 Bidirectional display and character compositions
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
252 Moving an iterator in bidirectional text
253 without producing glyphs
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
303 #ifdef HAVE_X_WINDOWS
304 #include "xterm.h"
305 #endif
306 #ifdef HAVE_NTGUI
307 #include "w32term.h"
308 #endif
309 #ifdef HAVE_NS
310 #include "nsterm.h"
311 #endif
312 #ifdef USE_GTK
313 #include "gtkutil.h"
314 #endif
316 #include "font.h"
318 #ifndef FRAME_X_OUTPUT
319 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
320 #endif
322 #define INFINITY 10000000
324 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
325 Lisp_Object Qwindow_scroll_functions;
326 static Lisp_Object Qwindow_text_change_functions;
327 static Lisp_Object Qredisplay_end_trigger_functions;
328 Lisp_Object Qinhibit_point_motion_hooks;
329 static Lisp_Object QCeval, QCpropertize;
330 Lisp_Object QCfile, QCdata;
331 static Lisp_Object Qfontified;
332 static Lisp_Object Qgrow_only;
333 static Lisp_Object Qinhibit_eval_during_redisplay;
334 static Lisp_Object Qbuffer_position, Qposition, Qobject;
335 static Lisp_Object Qright_to_left, Qleft_to_right;
337 /* Cursor shapes. */
338 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
340 /* Pointer shapes. */
341 static Lisp_Object Qarrow, Qhand;
342 Lisp_Object Qtext;
344 /* Holds the list (error). */
345 static Lisp_Object list_of_error;
347 static Lisp_Object Qfontification_functions;
349 static Lisp_Object Qwrap_prefix;
350 static Lisp_Object Qline_prefix;
351 static Lisp_Object Qredisplay_internal;
353 /* Non-nil means don't actually do any redisplay. */
355 Lisp_Object Qinhibit_redisplay;
357 /* Names of text properties relevant for redisplay. */
359 Lisp_Object Qdisplay;
361 Lisp_Object Qspace, QCalign_to;
362 static Lisp_Object QCrelative_width, QCrelative_height;
363 Lisp_Object Qleft_margin, Qright_margin;
364 static Lisp_Object Qspace_width, Qraise;
365 static Lisp_Object Qslice;
366 Lisp_Object Qcenter;
367 static Lisp_Object Qmargin, Qpointer;
368 static Lisp_Object Qline_height;
370 #ifdef HAVE_WINDOW_SYSTEM
372 /* Test if overflow newline into fringe. Called with iterator IT
373 at or past right window margin, and with IT->current_x set. */
375 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
376 (!NILP (Voverflow_newline_into_fringe) \
377 && FRAME_WINDOW_P ((IT)->f) \
378 && ((IT)->bidi_it.paragraph_dir == R2L \
379 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
380 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
381 && (IT)->current_x == (IT)->last_visible_x)
383 #else /* !HAVE_WINDOW_SYSTEM */
384 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
385 #endif /* HAVE_WINDOW_SYSTEM */
387 /* Test if the display element loaded in IT, or the underlying buffer
388 or string character, is a space or a TAB character. This is used
389 to determine where word wrapping can occur. */
391 #define IT_DISPLAYING_WHITESPACE(it) \
392 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
393 || ((STRINGP (it->string) \
394 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
395 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
396 || (it->s \
397 && (it->s[IT_BYTEPOS (*it)] == ' ' \
398 || it->s[IT_BYTEPOS (*it)] == '\t')) \
399 || (IT_BYTEPOS (*it) < ZV_BYTE \
400 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
401 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
403 /* Name of the face used to highlight trailing whitespace. */
405 static Lisp_Object Qtrailing_whitespace;
407 /* Name and number of the face used to highlight escape glyphs. */
409 static Lisp_Object Qescape_glyph;
411 /* Name and number of the face used to highlight non-breaking spaces. */
413 static Lisp_Object Qnobreak_space;
415 /* The symbol `image' which is the car of the lists used to represent
416 images in Lisp. Also a tool bar style. */
418 Lisp_Object Qimage;
420 /* The image map types. */
421 Lisp_Object QCmap;
422 static Lisp_Object QCpointer;
423 static Lisp_Object Qrect, Qcircle, Qpoly;
425 /* Tool bar styles */
426 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
428 /* Non-zero means print newline to stdout before next mini-buffer
429 message. */
431 int noninteractive_need_newline;
433 /* Non-zero means print newline to message log before next message. */
435 static int message_log_need_newline;
437 /* Three markers that message_dolog uses.
438 It could allocate them itself, but that causes trouble
439 in handling memory-full errors. */
440 static Lisp_Object message_dolog_marker1;
441 static Lisp_Object message_dolog_marker2;
442 static Lisp_Object message_dolog_marker3;
444 /* The buffer position of the first character appearing entirely or
445 partially on the line of the selected window which contains the
446 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
447 redisplay optimization in redisplay_internal. */
449 static struct text_pos this_line_start_pos;
451 /* Number of characters past the end of the line above, including the
452 terminating newline. */
454 static struct text_pos this_line_end_pos;
456 /* The vertical positions and the height of this line. */
458 static int this_line_vpos;
459 static int this_line_y;
460 static int this_line_pixel_height;
462 /* X position at which this display line starts. Usually zero;
463 negative if first character is partially visible. */
465 static int this_line_start_x;
467 /* The smallest character position seen by move_it_* functions as they
468 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
469 hscrolled lines, see display_line. */
471 static struct text_pos this_line_min_pos;
473 /* Buffer that this_line_.* variables are referring to. */
475 static struct buffer *this_line_buffer;
478 /* Values of those variables at last redisplay are stored as
479 properties on `overlay-arrow-position' symbol. However, if
480 Voverlay_arrow_position is a marker, last-arrow-position is its
481 numerical position. */
483 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
485 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
486 properties on a symbol in overlay-arrow-variable-list. */
488 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
490 Lisp_Object Qmenu_bar_update_hook;
492 /* Nonzero if an overlay arrow has been displayed in this window. */
494 static int overlay_arrow_seen;
496 /* Vector containing glyphs for an ellipsis `...'. */
498 static Lisp_Object default_invis_vector[3];
500 /* This is the window where the echo area message was displayed. It
501 is always a mini-buffer window, but it may not be the same window
502 currently active as a mini-buffer. */
504 Lisp_Object echo_area_window;
506 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
507 pushes the current message and the value of
508 message_enable_multibyte on the stack, the function restore_message
509 pops the stack and displays MESSAGE again. */
511 static Lisp_Object Vmessage_stack;
513 /* Nonzero means multibyte characters were enabled when the echo area
514 message was specified. */
516 static int message_enable_multibyte;
518 /* Nonzero if we should redraw the mode lines on the next redisplay. */
520 int update_mode_lines;
522 /* Nonzero if window sizes or contents have changed since last
523 redisplay that finished. */
525 int windows_or_buffers_changed;
527 /* Nonzero means a frame's cursor type has been changed. */
529 int cursor_type_changed;
531 /* Nonzero after display_mode_line if %l was used and it displayed a
532 line number. */
534 static int line_number_displayed;
536 /* The name of the *Messages* buffer, a string. */
538 static Lisp_Object Vmessages_buffer_name;
540 /* Current, index 0, and last displayed echo area message. Either
541 buffers from echo_buffers, or nil to indicate no message. */
543 Lisp_Object echo_area_buffer[2];
545 /* The buffers referenced from echo_area_buffer. */
547 static Lisp_Object echo_buffer[2];
549 /* A vector saved used in with_area_buffer to reduce consing. */
551 static Lisp_Object Vwith_echo_area_save_vector;
553 /* Non-zero means display_echo_area should display the last echo area
554 message again. Set by redisplay_preserve_echo_area. */
556 static int display_last_displayed_message_p;
558 /* Nonzero if echo area is being used by print; zero if being used by
559 message. */
561 static int message_buf_print;
563 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
565 static Lisp_Object Qinhibit_menubar_update;
566 static Lisp_Object Qmessage_truncate_lines;
568 /* Set to 1 in clear_message to make redisplay_internal aware
569 of an emptied echo area. */
571 static int message_cleared_p;
573 /* A scratch glyph row with contents used for generating truncation
574 glyphs. Also used in direct_output_for_insert. */
576 #define MAX_SCRATCH_GLYPHS 100
577 static struct glyph_row scratch_glyph_row;
578 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
580 /* Ascent and height of the last line processed by move_it_to. */
582 static int last_height;
584 /* Non-zero if there's a help-echo in the echo area. */
586 int help_echo_showing_p;
588 /* If >= 0, computed, exact values of mode-line and header-line height
589 to use in the macros CURRENT_MODE_LINE_HEIGHT and
590 CURRENT_HEADER_LINE_HEIGHT. */
592 int current_mode_line_height, current_header_line_height;
594 /* The maximum distance to look ahead for text properties. Values
595 that are too small let us call compute_char_face and similar
596 functions too often which is expensive. Values that are too large
597 let us call compute_char_face and alike too often because we
598 might not be interested in text properties that far away. */
600 #define TEXT_PROP_DISTANCE_LIMIT 100
602 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
603 iterator state and later restore it. This is needed because the
604 bidi iterator on bidi.c keeps a stacked cache of its states, which
605 is really a singleton. When we use scratch iterator objects to
606 move around the buffer, we can cause the bidi cache to be pushed or
607 popped, and therefore we need to restore the cache state when we
608 return to the original iterator. */
609 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
610 do { \
611 if (CACHE) \
612 bidi_unshelve_cache (CACHE, 1); \
613 ITCOPY = ITORIG; \
614 CACHE = bidi_shelve_cache (); \
615 } while (0)
617 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
618 do { \
619 if (pITORIG != pITCOPY) \
620 *(pITORIG) = *(pITCOPY); \
621 bidi_unshelve_cache (CACHE, 0); \
622 CACHE = NULL; \
623 } while (0)
625 #ifdef GLYPH_DEBUG
627 /* Non-zero means print traces of redisplay if compiled with
628 GLYPH_DEBUG defined. */
630 int trace_redisplay_p;
632 #endif /* GLYPH_DEBUG */
634 #ifdef DEBUG_TRACE_MOVE
635 /* Non-zero means trace with TRACE_MOVE to stderr. */
636 int trace_move;
638 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
639 #else
640 #define TRACE_MOVE(x) (void) 0
641 #endif
643 static Lisp_Object Qauto_hscroll_mode;
645 /* Buffer being redisplayed -- for redisplay_window_error. */
647 static struct buffer *displayed_buffer;
649 /* Value returned from text property handlers (see below). */
651 enum prop_handled
653 HANDLED_NORMALLY,
654 HANDLED_RECOMPUTE_PROPS,
655 HANDLED_OVERLAY_STRING_CONSUMED,
656 HANDLED_RETURN
659 /* A description of text properties that redisplay is interested
660 in. */
662 struct props
664 /* The name of the property. */
665 Lisp_Object *name;
667 /* A unique index for the property. */
668 enum prop_idx idx;
670 /* A handler function called to set up iterator IT from the property
671 at IT's current position. Value is used to steer handle_stop. */
672 enum prop_handled (*handler) (struct it *it);
675 static enum prop_handled handle_face_prop (struct it *);
676 static enum prop_handled handle_invisible_prop (struct it *);
677 static enum prop_handled handle_display_prop (struct it *);
678 static enum prop_handled handle_composition_prop (struct it *);
679 static enum prop_handled handle_overlay_change (struct it *);
680 static enum prop_handled handle_fontified_prop (struct it *);
682 /* Properties handled by iterators. */
684 static struct props it_props[] =
686 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
687 /* Handle `face' before `display' because some sub-properties of
688 `display' need to know the face. */
689 {&Qface, FACE_PROP_IDX, handle_face_prop},
690 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
691 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
692 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
693 {NULL, 0, NULL}
696 /* Value is the position described by X. If X is a marker, value is
697 the marker_position of X. Otherwise, value is X. */
699 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
701 /* Enumeration returned by some move_it_.* functions internally. */
703 enum move_it_result
705 /* Not used. Undefined value. */
706 MOVE_UNDEFINED,
708 /* Move ended at the requested buffer position or ZV. */
709 MOVE_POS_MATCH_OR_ZV,
711 /* Move ended at the requested X pixel position. */
712 MOVE_X_REACHED,
714 /* Move within a line ended at the end of a line that must be
715 continued. */
716 MOVE_LINE_CONTINUED,
718 /* Move within a line ended at the end of a line that would
719 be displayed truncated. */
720 MOVE_LINE_TRUNCATED,
722 /* Move within a line ended at a line end. */
723 MOVE_NEWLINE_OR_CR
726 /* This counter is used to clear the face cache every once in a while
727 in redisplay_internal. It is incremented for each redisplay.
728 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
729 cleared. */
731 #define CLEAR_FACE_CACHE_COUNT 500
732 static int clear_face_cache_count;
734 /* Similarly for the image cache. */
736 #ifdef HAVE_WINDOW_SYSTEM
737 #define CLEAR_IMAGE_CACHE_COUNT 101
738 static int clear_image_cache_count;
740 /* Null glyph slice */
741 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
742 #endif
744 /* True while redisplay_internal is in progress. */
746 bool redisplaying_p;
748 static Lisp_Object Qinhibit_free_realized_faces;
749 static Lisp_Object Qmode_line_default_help_echo;
751 /* If a string, XTread_socket generates an event to display that string.
752 (The display is done in read_char.) */
754 Lisp_Object help_echo_string;
755 Lisp_Object help_echo_window;
756 Lisp_Object help_echo_object;
757 ptrdiff_t help_echo_pos;
759 /* Temporary variable for XTread_socket. */
761 Lisp_Object previous_help_echo_string;
763 /* Platform-independent portion of hourglass implementation. */
765 /* Non-zero means an hourglass cursor is currently shown. */
766 int hourglass_shown_p;
768 /* If non-null, an asynchronous timer that, when it expires, displays
769 an hourglass cursor on all frames. */
770 struct atimer *hourglass_atimer;
772 /* Name of the face used to display glyphless characters. */
773 Lisp_Object Qglyphless_char;
775 /* Symbol for the purpose of Vglyphless_char_display. */
776 static Lisp_Object Qglyphless_char_display;
778 /* Method symbols for Vglyphless_char_display. */
779 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
781 /* Default pixel width of `thin-space' display method. */
782 #define THIN_SPACE_WIDTH 1
784 /* Default number of seconds to wait before displaying an hourglass
785 cursor. */
786 #define DEFAULT_HOURGLASS_DELAY 1
789 /* Function prototypes. */
791 static void setup_for_ellipsis (struct it *, int);
792 static void set_iterator_to_next (struct it *, int);
793 static void mark_window_display_accurate_1 (struct window *, int);
794 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
795 static int display_prop_string_p (Lisp_Object, Lisp_Object);
796 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
797 static int cursor_row_p (struct glyph_row *);
798 static int redisplay_mode_lines (Lisp_Object, int);
799 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
801 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
803 static void handle_line_prefix (struct it *);
805 static void pint2str (char *, int, ptrdiff_t);
806 static void pint2hrstr (char *, int, ptrdiff_t);
807 static struct text_pos run_window_scroll_functions (Lisp_Object,
808 struct text_pos);
809 static void reconsider_clip_changes (struct window *, struct buffer *);
810 static int text_outside_line_unchanged_p (struct window *,
811 ptrdiff_t, ptrdiff_t);
812 static void store_mode_line_noprop_char (char);
813 static int store_mode_line_noprop (const char *, int, int);
814 static void handle_stop (struct it *);
815 static void handle_stop_backwards (struct it *, ptrdiff_t);
816 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
817 static void ensure_echo_area_buffers (void);
818 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
819 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
820 static int with_echo_area_buffer (struct window *, int,
821 int (*) (ptrdiff_t, Lisp_Object),
822 ptrdiff_t, Lisp_Object);
823 static void clear_garbaged_frames (void);
824 static int current_message_1 (ptrdiff_t, Lisp_Object);
825 static void pop_message (void);
826 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
827 static void set_message (Lisp_Object);
828 static int set_message_1 (ptrdiff_t, Lisp_Object);
829 static int display_echo_area (struct window *);
830 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
831 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
832 static Lisp_Object unwind_redisplay (Lisp_Object);
833 static int string_char_and_length (const unsigned char *, int *);
834 static struct text_pos display_prop_end (struct it *, Lisp_Object,
835 struct text_pos);
836 static int compute_window_start_on_continuation_line (struct window *);
837 static void insert_left_trunc_glyphs (struct it *);
838 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
839 Lisp_Object);
840 static void extend_face_to_end_of_line (struct it *);
841 static int append_space_for_newline (struct it *, int);
842 static int cursor_row_fully_visible_p (struct window *, int, int);
843 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
844 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
845 static int trailing_whitespace_p (ptrdiff_t);
846 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
847 static void push_it (struct it *, struct text_pos *);
848 static void iterate_out_of_display_property (struct it *);
849 static void pop_it (struct it *);
850 static void sync_frame_with_window_matrix_rows (struct window *);
851 static void redisplay_internal (void);
852 static int echo_area_display (int);
853 static void redisplay_windows (Lisp_Object);
854 static void redisplay_window (Lisp_Object, int);
855 static Lisp_Object redisplay_window_error (Lisp_Object);
856 static Lisp_Object redisplay_window_0 (Lisp_Object);
857 static Lisp_Object redisplay_window_1 (Lisp_Object);
858 static int set_cursor_from_row (struct window *, struct glyph_row *,
859 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
860 int, int);
861 static int update_menu_bar (struct frame *, int, int);
862 static int try_window_reusing_current_matrix (struct window *);
863 static int try_window_id (struct window *);
864 static int display_line (struct it *);
865 static int display_mode_lines (struct window *);
866 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
867 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
868 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
869 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
870 static void display_menu_bar (struct window *);
871 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
872 ptrdiff_t *);
873 static int display_string (const char *, Lisp_Object, Lisp_Object,
874 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
875 static void compute_line_metrics (struct it *);
876 static void run_redisplay_end_trigger_hook (struct it *);
877 static int get_overlay_strings (struct it *, ptrdiff_t);
878 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
879 static void next_overlay_string (struct it *);
880 static void reseat (struct it *, struct text_pos, int);
881 static void reseat_1 (struct it *, struct text_pos, int);
882 static void back_to_previous_visible_line_start (struct it *);
883 static void reseat_at_next_visible_line_start (struct it *, int);
884 static int next_element_from_ellipsis (struct it *);
885 static int next_element_from_display_vector (struct it *);
886 static int next_element_from_string (struct it *);
887 static int next_element_from_c_string (struct it *);
888 static int next_element_from_buffer (struct it *);
889 static int next_element_from_composition (struct it *);
890 static int next_element_from_image (struct it *);
891 static int next_element_from_stretch (struct it *);
892 static void load_overlay_strings (struct it *, ptrdiff_t);
893 static int init_from_display_pos (struct it *, struct window *,
894 struct display_pos *);
895 static void reseat_to_string (struct it *, const char *,
896 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
897 static int get_next_display_element (struct it *);
898 static enum move_it_result
899 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
900 enum move_operation_enum);
901 static void get_visually_first_element (struct it *);
902 static void init_to_row_start (struct it *, struct window *,
903 struct glyph_row *);
904 static int init_to_row_end (struct it *, struct window *,
905 struct glyph_row *);
906 static void back_to_previous_line_start (struct it *);
907 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
908 static struct text_pos string_pos_nchars_ahead (struct text_pos,
909 Lisp_Object, ptrdiff_t);
910 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
911 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
912 static ptrdiff_t number_of_chars (const char *, bool);
913 static void compute_stop_pos (struct it *);
914 static void compute_string_pos (struct text_pos *, struct text_pos,
915 Lisp_Object);
916 static int face_before_or_after_it_pos (struct it *, int);
917 static ptrdiff_t next_overlay_change (ptrdiff_t);
918 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
919 Lisp_Object, struct text_pos *, ptrdiff_t, int);
920 static int handle_single_display_spec (struct it *, Lisp_Object,
921 Lisp_Object, Lisp_Object,
922 struct text_pos *, ptrdiff_t, int, int);
923 static int underlying_face_id (struct it *);
924 static int in_ellipses_for_invisible_text_p (struct display_pos *,
925 struct window *);
927 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
928 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
930 #ifdef HAVE_WINDOW_SYSTEM
932 static void x_consider_frame_title (Lisp_Object);
933 static int tool_bar_lines_needed (struct frame *, int *);
934 static void update_tool_bar (struct frame *, int);
935 static void build_desired_tool_bar_string (struct frame *f);
936 static int redisplay_tool_bar (struct frame *);
937 static void display_tool_bar_line (struct it *, int);
938 static void notice_overwritten_cursor (struct window *,
939 enum glyph_row_area,
940 int, int, int, int);
941 static void append_stretch_glyph (struct it *, Lisp_Object,
942 int, int, int);
945 #endif /* HAVE_WINDOW_SYSTEM */
947 static void produce_special_glyphs (struct it *, enum display_element_type);
948 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
949 static int coords_in_mouse_face_p (struct window *, int, int);
953 /***********************************************************************
954 Window display dimensions
955 ***********************************************************************/
957 /* Return the bottom boundary y-position for text lines in window W.
958 This is the first y position at which a line cannot start.
959 It is relative to the top of the window.
961 This is the height of W minus the height of a mode line, if any. */
964 window_text_bottom_y (struct window *w)
966 int height = WINDOW_TOTAL_HEIGHT (w);
968 if (WINDOW_WANTS_MODELINE_P (w))
969 height -= CURRENT_MODE_LINE_HEIGHT (w);
970 return height;
973 /* Return the pixel width of display area AREA of window W. AREA < 0
974 means return the total width of W, not including fringes to
975 the left and right of the window. */
978 window_box_width (struct window *w, int area)
980 int cols = w->total_cols;
981 int pixels = 0;
983 if (!w->pseudo_window_p)
985 cols -= WINDOW_SCROLL_BAR_COLS (w);
987 if (area == TEXT_AREA)
989 if (INTEGERP (w->left_margin_cols))
990 cols -= XFASTINT (w->left_margin_cols);
991 if (INTEGERP (w->right_margin_cols))
992 cols -= XFASTINT (w->right_margin_cols);
993 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
995 else if (area == LEFT_MARGIN_AREA)
997 cols = (INTEGERP (w->left_margin_cols)
998 ? XFASTINT (w->left_margin_cols) : 0);
999 pixels = 0;
1001 else if (area == RIGHT_MARGIN_AREA)
1003 cols = (INTEGERP (w->right_margin_cols)
1004 ? XFASTINT (w->right_margin_cols) : 0);
1005 pixels = 0;
1009 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1013 /* Return the pixel height of the display area of window W, not
1014 including mode lines of W, if any. */
1017 window_box_height (struct window *w)
1019 struct frame *f = XFRAME (w->frame);
1020 int height = WINDOW_TOTAL_HEIGHT (w);
1022 eassert (height >= 0);
1024 /* Note: the code below that determines the mode-line/header-line
1025 height is essentially the same as that contained in the macro
1026 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1027 the appropriate glyph row has its `mode_line_p' flag set,
1028 and if it doesn't, uses estimate_mode_line_height instead. */
1030 if (WINDOW_WANTS_MODELINE_P (w))
1032 struct glyph_row *ml_row
1033 = (w->current_matrix && w->current_matrix->rows
1034 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1035 : 0);
1036 if (ml_row && ml_row->mode_line_p)
1037 height -= ml_row->height;
1038 else
1039 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1042 if (WINDOW_WANTS_HEADER_LINE_P (w))
1044 struct glyph_row *hl_row
1045 = (w->current_matrix && w->current_matrix->rows
1046 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1047 : 0);
1048 if (hl_row && hl_row->mode_line_p)
1049 height -= hl_row->height;
1050 else
1051 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1054 /* With a very small font and a mode-line that's taller than
1055 default, we might end up with a negative height. */
1056 return max (0, height);
1059 /* Return the window-relative coordinate of the left edge of display
1060 area AREA of window W. AREA < 0 means return the left edge of the
1061 whole window, to the right of the left fringe of W. */
1064 window_box_left_offset (struct window *w, int area)
1066 int x;
1068 if (w->pseudo_window_p)
1069 return 0;
1071 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1073 if (area == TEXT_AREA)
1074 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1075 + window_box_width (w, LEFT_MARGIN_AREA));
1076 else if (area == RIGHT_MARGIN_AREA)
1077 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1078 + window_box_width (w, LEFT_MARGIN_AREA)
1079 + window_box_width (w, TEXT_AREA)
1080 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1082 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1083 else if (area == LEFT_MARGIN_AREA
1084 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1085 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1087 return x;
1091 /* Return the window-relative coordinate of the right edge of display
1092 area AREA of window W. AREA < 0 means return the right edge of the
1093 whole window, to the left of the right fringe of W. */
1096 window_box_right_offset (struct window *w, int area)
1098 return window_box_left_offset (w, area) + window_box_width (w, area);
1101 /* Return the frame-relative coordinate of the left edge of display
1102 area AREA of window W. AREA < 0 means return the left edge of the
1103 whole window, to the right of the left fringe of W. */
1106 window_box_left (struct window *w, int area)
1108 struct frame *f = XFRAME (w->frame);
1109 int x;
1111 if (w->pseudo_window_p)
1112 return FRAME_INTERNAL_BORDER_WIDTH (f);
1114 x = (WINDOW_LEFT_EDGE_X (w)
1115 + window_box_left_offset (w, area));
1117 return x;
1121 /* Return the frame-relative coordinate of the right edge of display
1122 area AREA of window W. AREA < 0 means return the right edge of the
1123 whole window, to the left of the right fringe of W. */
1126 window_box_right (struct window *w, int area)
1128 return window_box_left (w, area) + window_box_width (w, area);
1131 /* Get the bounding box of the display area AREA of window W, without
1132 mode lines, in frame-relative coordinates. AREA < 0 means the
1133 whole window, not including the left and right fringes of
1134 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1135 coordinates of the upper-left corner of the box. Return in
1136 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1138 void
1139 window_box (struct window *w, int area, int *box_x, int *box_y,
1140 int *box_width, int *box_height)
1142 if (box_width)
1143 *box_width = window_box_width (w, area);
1144 if (box_height)
1145 *box_height = window_box_height (w);
1146 if (box_x)
1147 *box_x = window_box_left (w, area);
1148 if (box_y)
1150 *box_y = WINDOW_TOP_EDGE_Y (w);
1151 if (WINDOW_WANTS_HEADER_LINE_P (w))
1152 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1157 /* Get the bounding box of the display area AREA of window W, without
1158 mode lines. AREA < 0 means the whole window, not including the
1159 left and right fringe of the window. Return in *TOP_LEFT_X
1160 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1161 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1162 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1163 box. */
1165 static void
1166 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1167 int *bottom_right_x, int *bottom_right_y)
1169 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1170 bottom_right_y);
1171 *bottom_right_x += *top_left_x;
1172 *bottom_right_y += *top_left_y;
1177 /***********************************************************************
1178 Utilities
1179 ***********************************************************************/
1181 /* Return the bottom y-position of the line the iterator IT is in.
1182 This can modify IT's settings. */
1185 line_bottom_y (struct it *it)
1187 int line_height = it->max_ascent + it->max_descent;
1188 int line_top_y = it->current_y;
1190 if (line_height == 0)
1192 if (last_height)
1193 line_height = last_height;
1194 else if (IT_CHARPOS (*it) < ZV)
1196 move_it_by_lines (it, 1);
1197 line_height = (it->max_ascent || it->max_descent
1198 ? it->max_ascent + it->max_descent
1199 : last_height);
1201 else
1203 struct glyph_row *row = it->glyph_row;
1205 /* Use the default character height. */
1206 it->glyph_row = NULL;
1207 it->what = IT_CHARACTER;
1208 it->c = ' ';
1209 it->len = 1;
1210 PRODUCE_GLYPHS (it);
1211 line_height = it->ascent + it->descent;
1212 it->glyph_row = row;
1216 return line_top_y + line_height;
1219 DEFUN ("line-pixel-height", Fline_pixel_height,
1220 Sline_pixel_height, 0, 0, 0,
1221 doc: /* Return height in pixels of text line in the selected window.
1223 Value is the height in pixels of the line at point. */)
1224 (void)
1226 struct it it;
1227 struct text_pos pt;
1228 struct window *w = XWINDOW (selected_window);
1230 SET_TEXT_POS (pt, PT, PT_BYTE);
1231 start_display (&it, w, pt);
1232 it.vpos = it.current_y = 0;
1233 last_height = 0;
1234 return make_number (line_bottom_y (&it));
1237 /* Subroutine of pos_visible_p below. Extracts a display string, if
1238 any, from the display spec given as its argument. */
1239 static Lisp_Object
1240 string_from_display_spec (Lisp_Object spec)
1242 if (CONSP (spec))
1244 while (CONSP (spec))
1246 if (STRINGP (XCAR (spec)))
1247 return XCAR (spec);
1248 spec = XCDR (spec);
1251 else if (VECTORP (spec))
1253 ptrdiff_t i;
1255 for (i = 0; i < ASIZE (spec); i++)
1257 if (STRINGP (AREF (spec, i)))
1258 return AREF (spec, i);
1260 return Qnil;
1263 return spec;
1267 /* Limit insanely large values of W->hscroll on frame F to the largest
1268 value that will still prevent first_visible_x and last_visible_x of
1269 'struct it' from overflowing an int. */
1270 static int
1271 window_hscroll_limited (struct window *w, struct frame *f)
1273 ptrdiff_t window_hscroll = w->hscroll;
1274 int window_text_width = window_box_width (w, TEXT_AREA);
1275 int colwidth = FRAME_COLUMN_WIDTH (f);
1277 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1278 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1280 return window_hscroll;
1283 /* Return 1 if position CHARPOS is visible in window W.
1284 CHARPOS < 0 means return info about WINDOW_END position.
1285 If visible, set *X and *Y to pixel coordinates of top left corner.
1286 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1287 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1290 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1291 int *rtop, int *rbot, int *rowh, int *vpos)
1293 struct it it;
1294 void *itdata = bidi_shelve_cache ();
1295 struct text_pos top;
1296 int visible_p = 0;
1297 struct buffer *old_buffer = NULL;
1299 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1300 return visible_p;
1302 if (XBUFFER (w->contents) != current_buffer)
1304 old_buffer = current_buffer;
1305 set_buffer_internal_1 (XBUFFER (w->contents));
1308 SET_TEXT_POS_FROM_MARKER (top, w->start);
1309 /* Scrolling a minibuffer window via scroll bar when the echo area
1310 shows long text sometimes resets the minibuffer contents behind
1311 our backs. */
1312 if (CHARPOS (top) > ZV)
1313 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1315 /* Compute exact mode line heights. */
1316 if (WINDOW_WANTS_MODELINE_P (w))
1317 current_mode_line_height
1318 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1319 BVAR (current_buffer, mode_line_format));
1321 if (WINDOW_WANTS_HEADER_LINE_P (w))
1322 current_header_line_height
1323 = display_mode_line (w, HEADER_LINE_FACE_ID,
1324 BVAR (current_buffer, header_line_format));
1326 start_display (&it, w, top);
1327 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1328 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1330 if (charpos >= 0
1331 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1332 && IT_CHARPOS (it) >= charpos)
1333 /* When scanning backwards under bidi iteration, move_it_to
1334 stops at or _before_ CHARPOS, because it stops at or to
1335 the _right_ of the character at CHARPOS. */
1336 || (it.bidi_p && it.bidi_it.scan_dir == -1
1337 && IT_CHARPOS (it) <= charpos)))
1339 /* We have reached CHARPOS, or passed it. How the call to
1340 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1341 or covered by a display property, move_it_to stops at the end
1342 of the invisible text, to the right of CHARPOS. (ii) If
1343 CHARPOS is in a display vector, move_it_to stops on its last
1344 glyph. */
1345 int top_x = it.current_x;
1346 int top_y = it.current_y;
1347 /* Calling line_bottom_y may change it.method, it.position, etc. */
1348 enum it_method it_method = it.method;
1349 int bottom_y = (last_height = 0, line_bottom_y (&it));
1350 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1352 if (top_y < window_top_y)
1353 visible_p = bottom_y > window_top_y;
1354 else if (top_y < it.last_visible_y)
1355 visible_p = 1;
1356 if (bottom_y >= it.last_visible_y
1357 && it.bidi_p && it.bidi_it.scan_dir == -1
1358 && IT_CHARPOS (it) < charpos)
1360 /* When the last line of the window is scanned backwards
1361 under bidi iteration, we could be duped into thinking
1362 that we have passed CHARPOS, when in fact move_it_to
1363 simply stopped short of CHARPOS because it reached
1364 last_visible_y. To see if that's what happened, we call
1365 move_it_to again with a slightly larger vertical limit,
1366 and see if it actually moved vertically; if it did, we
1367 didn't really reach CHARPOS, which is beyond window end. */
1368 struct it save_it = it;
1369 /* Why 10? because we don't know how many canonical lines
1370 will the height of the next line(s) be. So we guess. */
1371 int ten_more_lines =
1372 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1374 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1375 MOVE_TO_POS | MOVE_TO_Y);
1376 if (it.current_y > top_y)
1377 visible_p = 0;
1379 it = save_it;
1381 if (visible_p)
1383 if (it_method == GET_FROM_DISPLAY_VECTOR)
1385 /* We stopped on the last glyph of a display vector.
1386 Try and recompute. Hack alert! */
1387 if (charpos < 2 || top.charpos >= charpos)
1388 top_x = it.glyph_row->x;
1389 else
1391 struct it it2, it2_prev;
1392 /* The idea is to get to the previous buffer
1393 position, consume the character there, and use
1394 the pixel coordinates we get after that. But if
1395 the previous buffer position is also displayed
1396 from a display vector, we need to consume all of
1397 the glyphs from that display vector. */
1398 start_display (&it2, w, top);
1399 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1400 /* If we didn't get to CHARPOS - 1, there's some
1401 replacing display property at that position, and
1402 we stopped after it. That is exactly the place
1403 whose coordinates we want. */
1404 if (IT_CHARPOS (it2) != charpos - 1)
1405 it2_prev = it2;
1406 else
1408 /* Iterate until we get out of the display
1409 vector that displays the character at
1410 CHARPOS - 1. */
1411 do {
1412 get_next_display_element (&it2);
1413 PRODUCE_GLYPHS (&it2);
1414 it2_prev = it2;
1415 set_iterator_to_next (&it2, 1);
1416 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1417 && IT_CHARPOS (it2) < charpos);
1419 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1420 || it2_prev.current_x > it2_prev.last_visible_x)
1421 top_x = it.glyph_row->x;
1422 else
1424 top_x = it2_prev.current_x;
1425 top_y = it2_prev.current_y;
1429 else if (IT_CHARPOS (it) != charpos)
1431 Lisp_Object cpos = make_number (charpos);
1432 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1433 Lisp_Object string = string_from_display_spec (spec);
1434 struct text_pos tpos;
1435 int replacing_spec_p;
1436 bool newline_in_string
1437 = (STRINGP (string)
1438 && memchr (SDATA (string), '\n', SBYTES (string)));
1440 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1441 replacing_spec_p
1442 = (!NILP (spec)
1443 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1444 charpos, FRAME_WINDOW_P (it.f)));
1445 /* The tricky code below is needed because there's a
1446 discrepancy between move_it_to and how we set cursor
1447 when PT is at the beginning of a portion of text
1448 covered by a display property or an overlay with a
1449 display property, or the display line ends in a
1450 newline from a display string. move_it_to will stop
1451 _after_ such display strings, whereas
1452 set_cursor_from_row conspires with cursor_row_p to
1453 place the cursor on the first glyph produced from the
1454 display string. */
1456 /* We have overshoot PT because it is covered by a
1457 display property that replaces the text it covers.
1458 If the string includes embedded newlines, we are also
1459 in the wrong display line. Backtrack to the correct
1460 line, where the display property begins. */
1461 if (replacing_spec_p)
1463 Lisp_Object startpos, endpos;
1464 EMACS_INT start, end;
1465 struct it it3;
1466 int it3_moved;
1468 /* Find the first and the last buffer positions
1469 covered by the display string. */
1470 endpos =
1471 Fnext_single_char_property_change (cpos, Qdisplay,
1472 Qnil, Qnil);
1473 startpos =
1474 Fprevious_single_char_property_change (endpos, Qdisplay,
1475 Qnil, Qnil);
1476 start = XFASTINT (startpos);
1477 end = XFASTINT (endpos);
1478 /* Move to the last buffer position before the
1479 display property. */
1480 start_display (&it3, w, top);
1481 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1482 /* Move forward one more line if the position before
1483 the display string is a newline or if it is the
1484 rightmost character on a line that is
1485 continued or word-wrapped. */
1486 if (it3.method == GET_FROM_BUFFER
1487 && (it3.c == '\n'
1488 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1489 move_it_by_lines (&it3, 1);
1490 else if (move_it_in_display_line_to (&it3, -1,
1491 it3.current_x
1492 + it3.pixel_width,
1493 MOVE_TO_X)
1494 == MOVE_LINE_CONTINUED)
1496 move_it_by_lines (&it3, 1);
1497 /* When we are under word-wrap, the #$@%!
1498 move_it_by_lines moves 2 lines, so we need to
1499 fix that up. */
1500 if (it3.line_wrap == WORD_WRAP)
1501 move_it_by_lines (&it3, -1);
1504 /* Record the vertical coordinate of the display
1505 line where we wound up. */
1506 top_y = it3.current_y;
1507 if (it3.bidi_p)
1509 /* When characters are reordered for display,
1510 the character displayed to the left of the
1511 display string could be _after_ the display
1512 property in the logical order. Use the
1513 smallest vertical position of these two. */
1514 start_display (&it3, w, top);
1515 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1516 if (it3.current_y < top_y)
1517 top_y = it3.current_y;
1519 /* Move from the top of the window to the beginning
1520 of the display line where the display string
1521 begins. */
1522 start_display (&it3, w, top);
1523 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1524 /* If it3_moved stays zero after the 'while' loop
1525 below, that means we already were at a newline
1526 before the loop (e.g., the display string begins
1527 with a newline), so we don't need to (and cannot)
1528 inspect the glyphs of it3.glyph_row, because
1529 PRODUCE_GLYPHS will not produce anything for a
1530 newline, and thus it3.glyph_row stays at its
1531 stale content it got at top of the window. */
1532 it3_moved = 0;
1533 /* Finally, advance the iterator until we hit the
1534 first display element whose character position is
1535 CHARPOS, or until the first newline from the
1536 display string, which signals the end of the
1537 display line. */
1538 while (get_next_display_element (&it3))
1540 PRODUCE_GLYPHS (&it3);
1541 if (IT_CHARPOS (it3) == charpos
1542 || ITERATOR_AT_END_OF_LINE_P (&it3))
1543 break;
1544 it3_moved = 1;
1545 set_iterator_to_next (&it3, 0);
1547 top_x = it3.current_x - it3.pixel_width;
1548 /* Normally, we would exit the above loop because we
1549 found the display element whose character
1550 position is CHARPOS. For the contingency that we
1551 didn't, and stopped at the first newline from the
1552 display string, move back over the glyphs
1553 produced from the string, until we find the
1554 rightmost glyph not from the string. */
1555 if (it3_moved
1556 && newline_in_string
1557 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1559 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1560 + it3.glyph_row->used[TEXT_AREA];
1562 while (EQ ((g - 1)->object, string))
1564 --g;
1565 top_x -= g->pixel_width;
1567 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1568 + it3.glyph_row->used[TEXT_AREA]);
1573 *x = top_x;
1574 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1575 *rtop = max (0, window_top_y - top_y);
1576 *rbot = max (0, bottom_y - it.last_visible_y);
1577 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1578 - max (top_y, window_top_y)));
1579 *vpos = it.vpos;
1582 else
1584 /* We were asked to provide info about WINDOW_END. */
1585 struct it it2;
1586 void *it2data = NULL;
1588 SAVE_IT (it2, it, it2data);
1589 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1590 move_it_by_lines (&it, 1);
1591 if (charpos < IT_CHARPOS (it)
1592 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1594 visible_p = 1;
1595 RESTORE_IT (&it2, &it2, it2data);
1596 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1597 *x = it2.current_x;
1598 *y = it2.current_y + it2.max_ascent - it2.ascent;
1599 *rtop = max (0, -it2.current_y);
1600 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1601 - it.last_visible_y));
1602 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1603 it.last_visible_y)
1604 - max (it2.current_y,
1605 WINDOW_HEADER_LINE_HEIGHT (w))));
1606 *vpos = it2.vpos;
1608 else
1609 bidi_unshelve_cache (it2data, 1);
1611 bidi_unshelve_cache (itdata, 0);
1613 if (old_buffer)
1614 set_buffer_internal_1 (old_buffer);
1616 current_header_line_height = current_mode_line_height = -1;
1618 if (visible_p && w->hscroll > 0)
1619 *x -=
1620 window_hscroll_limited (w, WINDOW_XFRAME (w))
1621 * WINDOW_FRAME_COLUMN_WIDTH (w);
1623 #if 0
1624 /* Debugging code. */
1625 if (visible_p)
1626 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1627 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1628 else
1629 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1630 #endif
1632 return visible_p;
1636 /* Return the next character from STR. Return in *LEN the length of
1637 the character. This is like STRING_CHAR_AND_LENGTH but never
1638 returns an invalid character. If we find one, we return a `?', but
1639 with the length of the invalid character. */
1641 static int
1642 string_char_and_length (const unsigned char *str, int *len)
1644 int c;
1646 c = STRING_CHAR_AND_LENGTH (str, *len);
1647 if (!CHAR_VALID_P (c))
1648 /* We may not change the length here because other places in Emacs
1649 don't use this function, i.e. they silently accept invalid
1650 characters. */
1651 c = '?';
1653 return c;
1658 /* Given a position POS containing a valid character and byte position
1659 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1661 static struct text_pos
1662 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1664 eassert (STRINGP (string) && nchars >= 0);
1666 if (STRING_MULTIBYTE (string))
1668 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1669 int len;
1671 while (nchars--)
1673 string_char_and_length (p, &len);
1674 p += len;
1675 CHARPOS (pos) += 1;
1676 BYTEPOS (pos) += len;
1679 else
1680 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1682 return pos;
1686 /* Value is the text position, i.e. character and byte position,
1687 for character position CHARPOS in STRING. */
1689 static struct text_pos
1690 string_pos (ptrdiff_t charpos, Lisp_Object string)
1692 struct text_pos pos;
1693 eassert (STRINGP (string));
1694 eassert (charpos >= 0);
1695 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1696 return pos;
1700 /* Value is a text position, i.e. character and byte position, for
1701 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1702 means recognize multibyte characters. */
1704 static struct text_pos
1705 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1707 struct text_pos pos;
1709 eassert (s != NULL);
1710 eassert (charpos >= 0);
1712 if (multibyte_p)
1714 int len;
1716 SET_TEXT_POS (pos, 0, 0);
1717 while (charpos--)
1719 string_char_and_length ((const unsigned char *) s, &len);
1720 s += len;
1721 CHARPOS (pos) += 1;
1722 BYTEPOS (pos) += len;
1725 else
1726 SET_TEXT_POS (pos, charpos, charpos);
1728 return pos;
1732 /* Value is the number of characters in C string S. MULTIBYTE_P
1733 non-zero means recognize multibyte characters. */
1735 static ptrdiff_t
1736 number_of_chars (const char *s, bool multibyte_p)
1738 ptrdiff_t nchars;
1740 if (multibyte_p)
1742 ptrdiff_t rest = strlen (s);
1743 int len;
1744 const unsigned char *p = (const unsigned char *) s;
1746 for (nchars = 0; rest > 0; ++nchars)
1748 string_char_and_length (p, &len);
1749 rest -= len, p += len;
1752 else
1753 nchars = strlen (s);
1755 return nchars;
1759 /* Compute byte position NEWPOS->bytepos corresponding to
1760 NEWPOS->charpos. POS is a known position in string STRING.
1761 NEWPOS->charpos must be >= POS.charpos. */
1763 static void
1764 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1766 eassert (STRINGP (string));
1767 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1769 if (STRING_MULTIBYTE (string))
1770 *newpos = string_pos_nchars_ahead (pos, string,
1771 CHARPOS (*newpos) - CHARPOS (pos));
1772 else
1773 BYTEPOS (*newpos) = CHARPOS (*newpos);
1776 /* EXPORT:
1777 Return an estimation of the pixel height of mode or header lines on
1778 frame F. FACE_ID specifies what line's height to estimate. */
1781 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1783 #ifdef HAVE_WINDOW_SYSTEM
1784 if (FRAME_WINDOW_P (f))
1786 int height = FONT_HEIGHT (FRAME_FONT (f));
1788 /* This function is called so early when Emacs starts that the face
1789 cache and mode line face are not yet initialized. */
1790 if (FRAME_FACE_CACHE (f))
1792 struct face *face = FACE_FROM_ID (f, face_id);
1793 if (face)
1795 if (face->font)
1796 height = FONT_HEIGHT (face->font);
1797 if (face->box_line_width > 0)
1798 height += 2 * face->box_line_width;
1802 return height;
1804 #endif
1806 return 1;
1809 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1810 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1811 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1812 not force the value into range. */
1814 void
1815 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1816 int *x, int *y, NativeRectangle *bounds, int noclip)
1819 #ifdef HAVE_WINDOW_SYSTEM
1820 if (FRAME_WINDOW_P (f))
1822 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1823 even for negative values. */
1824 if (pix_x < 0)
1825 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1826 if (pix_y < 0)
1827 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1829 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1830 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1832 if (bounds)
1833 STORE_NATIVE_RECT (*bounds,
1834 FRAME_COL_TO_PIXEL_X (f, pix_x),
1835 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1836 FRAME_COLUMN_WIDTH (f) - 1,
1837 FRAME_LINE_HEIGHT (f) - 1);
1839 if (!noclip)
1841 if (pix_x < 0)
1842 pix_x = 0;
1843 else if (pix_x > FRAME_TOTAL_COLS (f))
1844 pix_x = FRAME_TOTAL_COLS (f);
1846 if (pix_y < 0)
1847 pix_y = 0;
1848 else if (pix_y > FRAME_LINES (f))
1849 pix_y = FRAME_LINES (f);
1852 #endif
1854 *x = pix_x;
1855 *y = pix_y;
1859 /* Find the glyph under window-relative coordinates X/Y in window W.
1860 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1861 strings. Return in *HPOS and *VPOS the row and column number of
1862 the glyph found. Return in *AREA the glyph area containing X.
1863 Value is a pointer to the glyph found or null if X/Y is not on
1864 text, or we can't tell because W's current matrix is not up to
1865 date. */
1867 static
1868 struct glyph *
1869 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1870 int *dx, int *dy, int *area)
1872 struct glyph *glyph, *end;
1873 struct glyph_row *row = NULL;
1874 int x0, i;
1876 /* Find row containing Y. Give up if some row is not enabled. */
1877 for (i = 0; i < w->current_matrix->nrows; ++i)
1879 row = MATRIX_ROW (w->current_matrix, i);
1880 if (!row->enabled_p)
1881 return NULL;
1882 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1883 break;
1886 *vpos = i;
1887 *hpos = 0;
1889 /* Give up if Y is not in the window. */
1890 if (i == w->current_matrix->nrows)
1891 return NULL;
1893 /* Get the glyph area containing X. */
1894 if (w->pseudo_window_p)
1896 *area = TEXT_AREA;
1897 x0 = 0;
1899 else
1901 if (x < window_box_left_offset (w, TEXT_AREA))
1903 *area = LEFT_MARGIN_AREA;
1904 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1906 else if (x < window_box_right_offset (w, TEXT_AREA))
1908 *area = TEXT_AREA;
1909 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1911 else
1913 *area = RIGHT_MARGIN_AREA;
1914 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1918 /* Find glyph containing X. */
1919 glyph = row->glyphs[*area];
1920 end = glyph + row->used[*area];
1921 x -= x0;
1922 while (glyph < end && x >= glyph->pixel_width)
1924 x -= glyph->pixel_width;
1925 ++glyph;
1928 if (glyph == end)
1929 return NULL;
1931 if (dx)
1933 *dx = x;
1934 *dy = y - (row->y + row->ascent - glyph->ascent);
1937 *hpos = glyph - row->glyphs[*area];
1938 return glyph;
1941 /* Convert frame-relative x/y to coordinates relative to window W.
1942 Takes pseudo-windows into account. */
1944 static void
1945 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1947 if (w->pseudo_window_p)
1949 /* A pseudo-window is always full-width, and starts at the
1950 left edge of the frame, plus a frame border. */
1951 struct frame *f = XFRAME (w->frame);
1952 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1953 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1955 else
1957 *x -= WINDOW_LEFT_EDGE_X (w);
1958 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1962 #ifdef HAVE_WINDOW_SYSTEM
1964 /* EXPORT:
1965 Return in RECTS[] at most N clipping rectangles for glyph string S.
1966 Return the number of stored rectangles. */
1969 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1971 XRectangle r;
1973 if (n <= 0)
1974 return 0;
1976 if (s->row->full_width_p)
1978 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1979 r.x = WINDOW_LEFT_EDGE_X (s->w);
1980 r.width = WINDOW_TOTAL_WIDTH (s->w);
1982 /* Unless displaying a mode or menu bar line, which are always
1983 fully visible, clip to the visible part of the row. */
1984 if (s->w->pseudo_window_p)
1985 r.height = s->row->visible_height;
1986 else
1987 r.height = s->height;
1989 else
1991 /* This is a text line that may be partially visible. */
1992 r.x = window_box_left (s->w, s->area);
1993 r.width = window_box_width (s->w, s->area);
1994 r.height = s->row->visible_height;
1997 if (s->clip_head)
1998 if (r.x < s->clip_head->x)
2000 if (r.width >= s->clip_head->x - r.x)
2001 r.width -= s->clip_head->x - r.x;
2002 else
2003 r.width = 0;
2004 r.x = s->clip_head->x;
2006 if (s->clip_tail)
2007 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2009 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2010 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2011 else
2012 r.width = 0;
2015 /* If S draws overlapping rows, it's sufficient to use the top and
2016 bottom of the window for clipping because this glyph string
2017 intentionally draws over other lines. */
2018 if (s->for_overlaps)
2020 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2021 r.height = window_text_bottom_y (s->w) - r.y;
2023 /* Alas, the above simple strategy does not work for the
2024 environments with anti-aliased text: if the same text is
2025 drawn onto the same place multiple times, it gets thicker.
2026 If the overlap we are processing is for the erased cursor, we
2027 take the intersection with the rectangle of the cursor. */
2028 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2030 XRectangle rc, r_save = r;
2032 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2033 rc.y = s->w->phys_cursor.y;
2034 rc.width = s->w->phys_cursor_width;
2035 rc.height = s->w->phys_cursor_height;
2037 x_intersect_rectangles (&r_save, &rc, &r);
2040 else
2042 /* Don't use S->y for clipping because it doesn't take partially
2043 visible lines into account. For example, it can be negative for
2044 partially visible lines at the top of a window. */
2045 if (!s->row->full_width_p
2046 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2047 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2048 else
2049 r.y = max (0, s->row->y);
2052 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2054 /* If drawing the cursor, don't let glyph draw outside its
2055 advertised boundaries. Cleartype does this under some circumstances. */
2056 if (s->hl == DRAW_CURSOR)
2058 struct glyph *glyph = s->first_glyph;
2059 int height, max_y;
2061 if (s->x > r.x)
2063 r.width -= s->x - r.x;
2064 r.x = s->x;
2066 r.width = min (r.width, glyph->pixel_width);
2068 /* If r.y is below window bottom, ensure that we still see a cursor. */
2069 height = min (glyph->ascent + glyph->descent,
2070 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2071 max_y = window_text_bottom_y (s->w) - height;
2072 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2073 if (s->ybase - glyph->ascent > max_y)
2075 r.y = max_y;
2076 r.height = height;
2078 else
2080 /* Don't draw cursor glyph taller than our actual glyph. */
2081 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2082 if (height < r.height)
2084 max_y = r.y + r.height;
2085 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2086 r.height = min (max_y - r.y, height);
2091 if (s->row->clip)
2093 XRectangle r_save = r;
2095 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2096 r.width = 0;
2099 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2100 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2102 #ifdef CONVERT_FROM_XRECT
2103 CONVERT_FROM_XRECT (r, *rects);
2104 #else
2105 *rects = r;
2106 #endif
2107 return 1;
2109 else
2111 /* If we are processing overlapping and allowed to return
2112 multiple clipping rectangles, we exclude the row of the glyph
2113 string from the clipping rectangle. This is to avoid drawing
2114 the same text on the environment with anti-aliasing. */
2115 #ifdef CONVERT_FROM_XRECT
2116 XRectangle rs[2];
2117 #else
2118 XRectangle *rs = rects;
2119 #endif
2120 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2122 if (s->for_overlaps & OVERLAPS_PRED)
2124 rs[i] = r;
2125 if (r.y + r.height > row_y)
2127 if (r.y < row_y)
2128 rs[i].height = row_y - r.y;
2129 else
2130 rs[i].height = 0;
2132 i++;
2134 if (s->for_overlaps & OVERLAPS_SUCC)
2136 rs[i] = r;
2137 if (r.y < row_y + s->row->visible_height)
2139 if (r.y + r.height > row_y + s->row->visible_height)
2141 rs[i].y = row_y + s->row->visible_height;
2142 rs[i].height = r.y + r.height - rs[i].y;
2144 else
2145 rs[i].height = 0;
2147 i++;
2150 n = i;
2151 #ifdef CONVERT_FROM_XRECT
2152 for (i = 0; i < n; i++)
2153 CONVERT_FROM_XRECT (rs[i], rects[i]);
2154 #endif
2155 return n;
2159 /* EXPORT:
2160 Return in *NR the clipping rectangle for glyph string S. */
2162 void
2163 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2165 get_glyph_string_clip_rects (s, nr, 1);
2169 /* EXPORT:
2170 Return the position and height of the phys cursor in window W.
2171 Set w->phys_cursor_width to width of phys cursor.
2174 void
2175 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2176 struct glyph *glyph, int *xp, int *yp, int *heightp)
2178 struct frame *f = XFRAME (WINDOW_FRAME (w));
2179 int x, y, wd, h, h0, y0;
2181 /* Compute the width of the rectangle to draw. If on a stretch
2182 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2183 rectangle as wide as the glyph, but use a canonical character
2184 width instead. */
2185 wd = glyph->pixel_width - 1;
2186 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2187 wd++; /* Why? */
2188 #endif
2190 x = w->phys_cursor.x;
2191 if (x < 0)
2193 wd += x;
2194 x = 0;
2197 if (glyph->type == STRETCH_GLYPH
2198 && !x_stretch_cursor_p)
2199 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2200 w->phys_cursor_width = wd;
2202 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2204 /* If y is below window bottom, ensure that we still see a cursor. */
2205 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2207 h = max (h0, glyph->ascent + glyph->descent);
2208 h0 = min (h0, glyph->ascent + glyph->descent);
2210 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2211 if (y < y0)
2213 h = max (h - (y0 - y) + 1, h0);
2214 y = y0 - 1;
2216 else
2218 y0 = window_text_bottom_y (w) - h0;
2219 if (y > y0)
2221 h += y - y0;
2222 y = y0;
2226 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2227 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2228 *heightp = h;
2232 * Remember which glyph the mouse is over.
2235 void
2236 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2238 Lisp_Object window;
2239 struct window *w;
2240 struct glyph_row *r, *gr, *end_row;
2241 enum window_part part;
2242 enum glyph_row_area area;
2243 int x, y, width, height;
2245 /* Try to determine frame pixel position and size of the glyph under
2246 frame pixel coordinates X/Y on frame F. */
2248 if (!f->glyphs_initialized_p
2249 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2250 NILP (window)))
2252 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2253 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2254 goto virtual_glyph;
2257 w = XWINDOW (window);
2258 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2259 height = WINDOW_FRAME_LINE_HEIGHT (w);
2261 x = window_relative_x_coord (w, part, gx);
2262 y = gy - WINDOW_TOP_EDGE_Y (w);
2264 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2265 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2267 if (w->pseudo_window_p)
2269 area = TEXT_AREA;
2270 part = ON_MODE_LINE; /* Don't adjust margin. */
2271 goto text_glyph;
2274 switch (part)
2276 case ON_LEFT_MARGIN:
2277 area = LEFT_MARGIN_AREA;
2278 goto text_glyph;
2280 case ON_RIGHT_MARGIN:
2281 area = RIGHT_MARGIN_AREA;
2282 goto text_glyph;
2284 case ON_HEADER_LINE:
2285 case ON_MODE_LINE:
2286 gr = (part == ON_HEADER_LINE
2287 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2288 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2289 gy = gr->y;
2290 area = TEXT_AREA;
2291 goto text_glyph_row_found;
2293 case ON_TEXT:
2294 area = TEXT_AREA;
2296 text_glyph:
2297 gr = 0; gy = 0;
2298 for (; r <= end_row && r->enabled_p; ++r)
2299 if (r->y + r->height > y)
2301 gr = r; gy = r->y;
2302 break;
2305 text_glyph_row_found:
2306 if (gr && gy <= y)
2308 struct glyph *g = gr->glyphs[area];
2309 struct glyph *end = g + gr->used[area];
2311 height = gr->height;
2312 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2313 if (gx + g->pixel_width > x)
2314 break;
2316 if (g < end)
2318 if (g->type == IMAGE_GLYPH)
2320 /* Don't remember when mouse is over image, as
2321 image may have hot-spots. */
2322 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2323 return;
2325 width = g->pixel_width;
2327 else
2329 /* Use nominal char spacing at end of line. */
2330 x -= gx;
2331 gx += (x / width) * width;
2334 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2335 gx += window_box_left_offset (w, area);
2337 else
2339 /* Use nominal line height at end of window. */
2340 gx = (x / width) * width;
2341 y -= gy;
2342 gy += (y / height) * height;
2344 break;
2346 case ON_LEFT_FRINGE:
2347 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2348 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2349 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2350 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2351 goto row_glyph;
2353 case ON_RIGHT_FRINGE:
2354 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2355 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2356 : window_box_right_offset (w, TEXT_AREA));
2357 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2358 goto row_glyph;
2360 case ON_SCROLL_BAR:
2361 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2363 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2364 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2365 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2366 : 0)));
2367 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2369 row_glyph:
2370 gr = 0, gy = 0;
2371 for (; r <= end_row && r->enabled_p; ++r)
2372 if (r->y + r->height > y)
2374 gr = r; gy = r->y;
2375 break;
2378 if (gr && gy <= y)
2379 height = gr->height;
2380 else
2382 /* Use nominal line height at end of window. */
2383 y -= gy;
2384 gy += (y / height) * height;
2386 break;
2388 default:
2390 virtual_glyph:
2391 /* If there is no glyph under the mouse, then we divide the screen
2392 into a grid of the smallest glyph in the frame, and use that
2393 as our "glyph". */
2395 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2396 round down even for negative values. */
2397 if (gx < 0)
2398 gx -= width - 1;
2399 if (gy < 0)
2400 gy -= height - 1;
2402 gx = (gx / width) * width;
2403 gy = (gy / height) * height;
2405 goto store_rect;
2408 gx += WINDOW_LEFT_EDGE_X (w);
2409 gy += WINDOW_TOP_EDGE_Y (w);
2411 store_rect:
2412 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2414 /* Visible feedback for debugging. */
2415 #if 0
2416 #if HAVE_X_WINDOWS
2417 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2418 f->output_data.x->normal_gc,
2419 gx, gy, width, height);
2420 #endif
2421 #endif
2425 #endif /* HAVE_WINDOW_SYSTEM */
2428 /***********************************************************************
2429 Lisp form evaluation
2430 ***********************************************************************/
2432 /* Error handler for safe_eval and safe_call. */
2434 static Lisp_Object
2435 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2437 add_to_log ("Error during redisplay: %S signaled %S",
2438 Flist (nargs, args), arg);
2439 return Qnil;
2442 /* Call function FUNC with the rest of NARGS - 1 arguments
2443 following. Return the result, or nil if something went
2444 wrong. Prevent redisplay during the evaluation. */
2446 Lisp_Object
2447 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2449 Lisp_Object val;
2451 if (inhibit_eval_during_redisplay)
2452 val = Qnil;
2453 else
2455 va_list ap;
2456 ptrdiff_t i;
2457 ptrdiff_t count = SPECPDL_INDEX ();
2458 struct gcpro gcpro1;
2459 Lisp_Object *args = alloca (nargs * word_size);
2461 args[0] = func;
2462 va_start (ap, func);
2463 for (i = 1; i < nargs; i++)
2464 args[i] = va_arg (ap, Lisp_Object);
2465 va_end (ap);
2467 GCPRO1 (args[0]);
2468 gcpro1.nvars = nargs;
2469 specbind (Qinhibit_redisplay, Qt);
2470 /* Use Qt to ensure debugger does not run,
2471 so there is no possibility of wanting to redisplay. */
2472 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2473 safe_eval_handler);
2474 UNGCPRO;
2475 val = unbind_to (count, val);
2478 return val;
2482 /* Call function FN with one argument ARG.
2483 Return the result, or nil if something went wrong. */
2485 Lisp_Object
2486 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2488 return safe_call (2, fn, arg);
2491 static Lisp_Object Qeval;
2493 Lisp_Object
2494 safe_eval (Lisp_Object sexpr)
2496 return safe_call1 (Qeval, sexpr);
2499 /* Call function FN with two arguments ARG1 and ARG2.
2500 Return the result, or nil if something went wrong. */
2502 Lisp_Object
2503 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2505 return safe_call (3, fn, arg1, arg2);
2510 /***********************************************************************
2511 Debugging
2512 ***********************************************************************/
2514 #if 0
2516 /* Define CHECK_IT to perform sanity checks on iterators.
2517 This is for debugging. It is too slow to do unconditionally. */
2519 static void
2520 check_it (struct it *it)
2522 if (it->method == GET_FROM_STRING)
2524 eassert (STRINGP (it->string));
2525 eassert (IT_STRING_CHARPOS (*it) >= 0);
2527 else
2529 eassert (IT_STRING_CHARPOS (*it) < 0);
2530 if (it->method == GET_FROM_BUFFER)
2532 /* Check that character and byte positions agree. */
2533 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2537 if (it->dpvec)
2538 eassert (it->current.dpvec_index >= 0);
2539 else
2540 eassert (it->current.dpvec_index < 0);
2543 #define CHECK_IT(IT) check_it ((IT))
2545 #else /* not 0 */
2547 #define CHECK_IT(IT) (void) 0
2549 #endif /* not 0 */
2552 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2554 /* Check that the window end of window W is what we expect it
2555 to be---the last row in the current matrix displaying text. */
2557 static void
2558 check_window_end (struct window *w)
2560 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2562 struct glyph_row *row;
2563 eassert ((row = MATRIX_ROW (w->current_matrix,
2564 XFASTINT (w->window_end_vpos)),
2565 !row->enabled_p
2566 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2567 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2571 #define CHECK_WINDOW_END(W) check_window_end ((W))
2573 #else
2575 #define CHECK_WINDOW_END(W) (void) 0
2577 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2579 /* Return mark position if current buffer has the region of non-zero length,
2580 or -1 otherwise. */
2582 static ptrdiff_t
2583 markpos_of_region (void)
2585 if (!NILP (Vtransient_mark_mode)
2586 && !NILP (BVAR (current_buffer, mark_active))
2587 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2589 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2591 if (markpos != PT)
2592 return markpos;
2594 return -1;
2597 /***********************************************************************
2598 Iterator initialization
2599 ***********************************************************************/
2601 /* Initialize IT for displaying current_buffer in window W, starting
2602 at character position CHARPOS. CHARPOS < 0 means that no buffer
2603 position is specified which is useful when the iterator is assigned
2604 a position later. BYTEPOS is the byte position corresponding to
2605 CHARPOS.
2607 If ROW is not null, calls to produce_glyphs with IT as parameter
2608 will produce glyphs in that row.
2610 BASE_FACE_ID is the id of a base face to use. It must be one of
2611 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2612 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2613 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2615 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2616 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2617 will be initialized to use the corresponding mode line glyph row of
2618 the desired matrix of W. */
2620 void
2621 init_iterator (struct it *it, struct window *w,
2622 ptrdiff_t charpos, ptrdiff_t bytepos,
2623 struct glyph_row *row, enum face_id base_face_id)
2625 ptrdiff_t markpos;
2626 enum face_id remapped_base_face_id = base_face_id;
2628 /* Some precondition checks. */
2629 eassert (w != NULL && it != NULL);
2630 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2631 && charpos <= ZV));
2633 /* If face attributes have been changed since the last redisplay,
2634 free realized faces now because they depend on face definitions
2635 that might have changed. Don't free faces while there might be
2636 desired matrices pending which reference these faces. */
2637 if (face_change_count && !inhibit_free_realized_faces)
2639 face_change_count = 0;
2640 free_all_realized_faces (Qnil);
2643 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2644 if (! NILP (Vface_remapping_alist))
2645 remapped_base_face_id
2646 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2648 /* Use one of the mode line rows of W's desired matrix if
2649 appropriate. */
2650 if (row == NULL)
2652 if (base_face_id == MODE_LINE_FACE_ID
2653 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2654 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2655 else if (base_face_id == HEADER_LINE_FACE_ID)
2656 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2659 /* Clear IT. */
2660 memset (it, 0, sizeof *it);
2661 it->current.overlay_string_index = -1;
2662 it->current.dpvec_index = -1;
2663 it->base_face_id = remapped_base_face_id;
2664 it->string = Qnil;
2665 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2666 it->paragraph_embedding = L2R;
2667 it->bidi_it.string.lstring = Qnil;
2668 it->bidi_it.string.s = NULL;
2669 it->bidi_it.string.bufpos = 0;
2670 it->bidi_it.w = w;
2672 /* The window in which we iterate over current_buffer: */
2673 XSETWINDOW (it->window, w);
2674 it->w = w;
2675 it->f = XFRAME (w->frame);
2677 it->cmp_it.id = -1;
2679 /* Extra space between lines (on window systems only). */
2680 if (base_face_id == DEFAULT_FACE_ID
2681 && FRAME_WINDOW_P (it->f))
2683 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2684 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2685 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2686 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2687 * FRAME_LINE_HEIGHT (it->f));
2688 else if (it->f->extra_line_spacing > 0)
2689 it->extra_line_spacing = it->f->extra_line_spacing;
2690 it->max_extra_line_spacing = 0;
2693 /* If realized faces have been removed, e.g. because of face
2694 attribute changes of named faces, recompute them. When running
2695 in batch mode, the face cache of the initial frame is null. If
2696 we happen to get called, make a dummy face cache. */
2697 if (FRAME_FACE_CACHE (it->f) == NULL)
2698 init_frame_faces (it->f);
2699 if (FRAME_FACE_CACHE (it->f)->used == 0)
2700 recompute_basic_faces (it->f);
2702 /* Current value of the `slice', `space-width', and 'height' properties. */
2703 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2704 it->space_width = Qnil;
2705 it->font_height = Qnil;
2706 it->override_ascent = -1;
2708 /* Are control characters displayed as `^C'? */
2709 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2711 /* -1 means everything between a CR and the following line end
2712 is invisible. >0 means lines indented more than this value are
2713 invisible. */
2714 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2715 ? (clip_to_bounds
2716 (-1, XINT (BVAR (current_buffer, selective_display)),
2717 PTRDIFF_MAX))
2718 : (!NILP (BVAR (current_buffer, selective_display))
2719 ? -1 : 0));
2720 it->selective_display_ellipsis_p
2721 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2723 /* Display table to use. */
2724 it->dp = window_display_table (w);
2726 /* Are multibyte characters enabled in current_buffer? */
2727 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2729 /* If visible region is of non-zero length, set IT->region_beg_charpos
2730 and IT->region_end_charpos to the start and end of a visible region
2731 in window IT->w. Set both to -1 to indicate no region. */
2732 markpos = markpos_of_region ();
2733 if (markpos >= 0
2734 /* Maybe highlight only in selected window. */
2735 && (/* Either show region everywhere. */
2736 highlight_nonselected_windows
2737 /* Or show region in the selected window. */
2738 || w == XWINDOW (selected_window)
2739 /* Or show the region if we are in the mini-buffer and W is
2740 the window the mini-buffer refers to. */
2741 || (MINI_WINDOW_P (XWINDOW (selected_window))
2742 && WINDOWP (minibuf_selected_window)
2743 && w == XWINDOW (minibuf_selected_window))))
2745 it->region_beg_charpos = min (PT, markpos);
2746 it->region_end_charpos = max (PT, markpos);
2748 else
2749 it->region_beg_charpos = it->region_end_charpos = -1;
2751 /* Get the position at which the redisplay_end_trigger hook should
2752 be run, if it is to be run at all. */
2753 if (MARKERP (w->redisplay_end_trigger)
2754 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2755 it->redisplay_end_trigger_charpos
2756 = marker_position (w->redisplay_end_trigger);
2757 else if (INTEGERP (w->redisplay_end_trigger))
2758 it->redisplay_end_trigger_charpos =
2759 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2761 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2763 /* Are lines in the display truncated? */
2764 if (base_face_id != DEFAULT_FACE_ID
2765 || it->w->hscroll
2766 || (! WINDOW_FULL_WIDTH_P (it->w)
2767 && ((!NILP (Vtruncate_partial_width_windows)
2768 && !INTEGERP (Vtruncate_partial_width_windows))
2769 || (INTEGERP (Vtruncate_partial_width_windows)
2770 && (WINDOW_TOTAL_COLS (it->w)
2771 < XINT (Vtruncate_partial_width_windows))))))
2772 it->line_wrap = TRUNCATE;
2773 else if (NILP (BVAR (current_buffer, truncate_lines)))
2774 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2775 ? WINDOW_WRAP : WORD_WRAP;
2776 else
2777 it->line_wrap = TRUNCATE;
2779 /* Get dimensions of truncation and continuation glyphs. These are
2780 displayed as fringe bitmaps under X, but we need them for such
2781 frames when the fringes are turned off. But leave the dimensions
2782 zero for tooltip frames, as these glyphs look ugly there and also
2783 sabotage calculations of tooltip dimensions in x-show-tip. */
2784 #ifdef HAVE_WINDOW_SYSTEM
2785 if (!(FRAME_WINDOW_P (it->f)
2786 && FRAMEP (tip_frame)
2787 && it->f == XFRAME (tip_frame)))
2788 #endif
2790 if (it->line_wrap == TRUNCATE)
2792 /* We will need the truncation glyph. */
2793 eassert (it->glyph_row == NULL);
2794 produce_special_glyphs (it, IT_TRUNCATION);
2795 it->truncation_pixel_width = it->pixel_width;
2797 else
2799 /* We will need the continuation glyph. */
2800 eassert (it->glyph_row == NULL);
2801 produce_special_glyphs (it, IT_CONTINUATION);
2802 it->continuation_pixel_width = it->pixel_width;
2806 /* Reset these values to zero because the produce_special_glyphs
2807 above has changed them. */
2808 it->pixel_width = it->ascent = it->descent = 0;
2809 it->phys_ascent = it->phys_descent = 0;
2811 /* Set this after getting the dimensions of truncation and
2812 continuation glyphs, so that we don't produce glyphs when calling
2813 produce_special_glyphs, above. */
2814 it->glyph_row = row;
2815 it->area = TEXT_AREA;
2817 /* Forget any previous info about this row being reversed. */
2818 if (it->glyph_row)
2819 it->glyph_row->reversed_p = 0;
2821 /* Get the dimensions of the display area. The display area
2822 consists of the visible window area plus a horizontally scrolled
2823 part to the left of the window. All x-values are relative to the
2824 start of this total display area. */
2825 if (base_face_id != DEFAULT_FACE_ID)
2827 /* Mode lines, menu bar in terminal frames. */
2828 it->first_visible_x = 0;
2829 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2831 else
2833 it->first_visible_x =
2834 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2835 it->last_visible_x = (it->first_visible_x
2836 + window_box_width (w, TEXT_AREA));
2838 /* If we truncate lines, leave room for the truncation glyph(s) at
2839 the right margin. Otherwise, leave room for the continuation
2840 glyph(s). Done only if the window has no fringes. Since we
2841 don't know at this point whether there will be any R2L lines in
2842 the window, we reserve space for truncation/continuation glyphs
2843 even if only one of the fringes is absent. */
2844 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2845 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2847 if (it->line_wrap == TRUNCATE)
2848 it->last_visible_x -= it->truncation_pixel_width;
2849 else
2850 it->last_visible_x -= it->continuation_pixel_width;
2853 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2854 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2857 /* Leave room for a border glyph. */
2858 if (!FRAME_WINDOW_P (it->f)
2859 && !WINDOW_RIGHTMOST_P (it->w))
2860 it->last_visible_x -= 1;
2862 it->last_visible_y = window_text_bottom_y (w);
2864 /* For mode lines and alike, arrange for the first glyph having a
2865 left box line if the face specifies a box. */
2866 if (base_face_id != DEFAULT_FACE_ID)
2868 struct face *face;
2870 it->face_id = remapped_base_face_id;
2872 /* If we have a boxed mode line, make the first character appear
2873 with a left box line. */
2874 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2875 if (face->box != FACE_NO_BOX)
2876 it->start_of_box_run_p = 1;
2879 /* If a buffer position was specified, set the iterator there,
2880 getting overlays and face properties from that position. */
2881 if (charpos >= BUF_BEG (current_buffer))
2883 it->end_charpos = ZV;
2884 eassert (charpos == BYTE_TO_CHAR (bytepos));
2885 IT_CHARPOS (*it) = charpos;
2886 IT_BYTEPOS (*it) = bytepos;
2888 /* We will rely on `reseat' to set this up properly, via
2889 handle_face_prop. */
2890 it->face_id = it->base_face_id;
2892 it->start = it->current;
2893 /* Do we need to reorder bidirectional text? Not if this is a
2894 unibyte buffer: by definition, none of the single-byte
2895 characters are strong R2L, so no reordering is needed. And
2896 bidi.c doesn't support unibyte buffers anyway. Also, don't
2897 reorder while we are loading loadup.el, since the tables of
2898 character properties needed for reordering are not yet
2899 available. */
2900 it->bidi_p =
2901 NILP (Vpurify_flag)
2902 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2903 && it->multibyte_p;
2905 /* If we are to reorder bidirectional text, init the bidi
2906 iterator. */
2907 if (it->bidi_p)
2909 /* Note the paragraph direction that this buffer wants to
2910 use. */
2911 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2912 Qleft_to_right))
2913 it->paragraph_embedding = L2R;
2914 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2915 Qright_to_left))
2916 it->paragraph_embedding = R2L;
2917 else
2918 it->paragraph_embedding = NEUTRAL_DIR;
2919 bidi_unshelve_cache (NULL, 0);
2920 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2921 &it->bidi_it);
2924 /* Compute faces etc. */
2925 reseat (it, it->current.pos, 1);
2928 CHECK_IT (it);
2932 /* Initialize IT for the display of window W with window start POS. */
2934 void
2935 start_display (struct it *it, struct window *w, struct text_pos pos)
2937 struct glyph_row *row;
2938 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2940 row = w->desired_matrix->rows + first_vpos;
2941 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2942 it->first_vpos = first_vpos;
2944 /* Don't reseat to previous visible line start if current start
2945 position is in a string or image. */
2946 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2948 int start_at_line_beg_p;
2949 int first_y = it->current_y;
2951 /* If window start is not at a line start, skip forward to POS to
2952 get the correct continuation lines width. */
2953 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2954 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2955 if (!start_at_line_beg_p)
2957 int new_x;
2959 reseat_at_previous_visible_line_start (it);
2960 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2962 new_x = it->current_x + it->pixel_width;
2964 /* If lines are continued, this line may end in the middle
2965 of a multi-glyph character (e.g. a control character
2966 displayed as \003, or in the middle of an overlay
2967 string). In this case move_it_to above will not have
2968 taken us to the start of the continuation line but to the
2969 end of the continued line. */
2970 if (it->current_x > 0
2971 && it->line_wrap != TRUNCATE /* Lines are continued. */
2972 && (/* And glyph doesn't fit on the line. */
2973 new_x > it->last_visible_x
2974 /* Or it fits exactly and we're on a window
2975 system frame. */
2976 || (new_x == it->last_visible_x
2977 && FRAME_WINDOW_P (it->f)
2978 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2979 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2980 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2982 if ((it->current.dpvec_index >= 0
2983 || it->current.overlay_string_index >= 0)
2984 /* If we are on a newline from a display vector or
2985 overlay string, then we are already at the end of
2986 a screen line; no need to go to the next line in
2987 that case, as this line is not really continued.
2988 (If we do go to the next line, C-e will not DTRT.) */
2989 && it->c != '\n')
2991 set_iterator_to_next (it, 1);
2992 move_it_in_display_line_to (it, -1, -1, 0);
2995 it->continuation_lines_width += it->current_x;
2997 /* If the character at POS is displayed via a display
2998 vector, move_it_to above stops at the final glyph of
2999 IT->dpvec. To make the caller redisplay that character
3000 again (a.k.a. start at POS), we need to reset the
3001 dpvec_index to the beginning of IT->dpvec. */
3002 else if (it->current.dpvec_index >= 0)
3003 it->current.dpvec_index = 0;
3005 /* We're starting a new display line, not affected by the
3006 height of the continued line, so clear the appropriate
3007 fields in the iterator structure. */
3008 it->max_ascent = it->max_descent = 0;
3009 it->max_phys_ascent = it->max_phys_descent = 0;
3011 it->current_y = first_y;
3012 it->vpos = 0;
3013 it->current_x = it->hpos = 0;
3019 /* Return 1 if POS is a position in ellipses displayed for invisible
3020 text. W is the window we display, for text property lookup. */
3022 static int
3023 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3025 Lisp_Object prop, window;
3026 int ellipses_p = 0;
3027 ptrdiff_t charpos = CHARPOS (pos->pos);
3029 /* If POS specifies a position in a display vector, this might
3030 be for an ellipsis displayed for invisible text. We won't
3031 get the iterator set up for delivering that ellipsis unless
3032 we make sure that it gets aware of the invisible text. */
3033 if (pos->dpvec_index >= 0
3034 && pos->overlay_string_index < 0
3035 && CHARPOS (pos->string_pos) < 0
3036 && charpos > BEGV
3037 && (XSETWINDOW (window, w),
3038 prop = Fget_char_property (make_number (charpos),
3039 Qinvisible, window),
3040 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3042 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3043 window);
3044 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3047 return ellipses_p;
3051 /* Initialize IT for stepping through current_buffer in window W,
3052 starting at position POS that includes overlay string and display
3053 vector/ control character translation position information. Value
3054 is zero if there are overlay strings with newlines at POS. */
3056 static int
3057 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3059 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3060 int i, overlay_strings_with_newlines = 0;
3062 /* If POS specifies a position in a display vector, this might
3063 be for an ellipsis displayed for invisible text. We won't
3064 get the iterator set up for delivering that ellipsis unless
3065 we make sure that it gets aware of the invisible text. */
3066 if (in_ellipses_for_invisible_text_p (pos, w))
3068 --charpos;
3069 bytepos = 0;
3072 /* Keep in mind: the call to reseat in init_iterator skips invisible
3073 text, so we might end up at a position different from POS. This
3074 is only a problem when POS is a row start after a newline and an
3075 overlay starts there with an after-string, and the overlay has an
3076 invisible property. Since we don't skip invisible text in
3077 display_line and elsewhere immediately after consuming the
3078 newline before the row start, such a POS will not be in a string,
3079 but the call to init_iterator below will move us to the
3080 after-string. */
3081 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3083 /* This only scans the current chunk -- it should scan all chunks.
3084 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3085 to 16 in 22.1 to make this a lesser problem. */
3086 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3088 const char *s = SSDATA (it->overlay_strings[i]);
3089 const char *e = s + SBYTES (it->overlay_strings[i]);
3091 while (s < e && *s != '\n')
3092 ++s;
3094 if (s < e)
3096 overlay_strings_with_newlines = 1;
3097 break;
3101 /* If position is within an overlay string, set up IT to the right
3102 overlay string. */
3103 if (pos->overlay_string_index >= 0)
3105 int relative_index;
3107 /* If the first overlay string happens to have a `display'
3108 property for an image, the iterator will be set up for that
3109 image, and we have to undo that setup first before we can
3110 correct the overlay string index. */
3111 if (it->method == GET_FROM_IMAGE)
3112 pop_it (it);
3114 /* We already have the first chunk of overlay strings in
3115 IT->overlay_strings. Load more until the one for
3116 pos->overlay_string_index is in IT->overlay_strings. */
3117 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3119 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3120 it->current.overlay_string_index = 0;
3121 while (n--)
3123 load_overlay_strings (it, 0);
3124 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3128 it->current.overlay_string_index = pos->overlay_string_index;
3129 relative_index = (it->current.overlay_string_index
3130 % OVERLAY_STRING_CHUNK_SIZE);
3131 it->string = it->overlay_strings[relative_index];
3132 eassert (STRINGP (it->string));
3133 it->current.string_pos = pos->string_pos;
3134 it->method = GET_FROM_STRING;
3135 it->end_charpos = SCHARS (it->string);
3136 /* Set up the bidi iterator for this overlay string. */
3137 if (it->bidi_p)
3139 it->bidi_it.string.lstring = it->string;
3140 it->bidi_it.string.s = NULL;
3141 it->bidi_it.string.schars = SCHARS (it->string);
3142 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3143 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3144 it->bidi_it.string.unibyte = !it->multibyte_p;
3145 it->bidi_it.w = it->w;
3146 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3147 FRAME_WINDOW_P (it->f), &it->bidi_it);
3149 /* Synchronize the state of the bidi iterator with
3150 pos->string_pos. For any string position other than
3151 zero, this will be done automagically when we resume
3152 iteration over the string and get_visually_first_element
3153 is called. But if string_pos is zero, and the string is
3154 to be reordered for display, we need to resync manually,
3155 since it could be that the iteration state recorded in
3156 pos ended at string_pos of 0 moving backwards in string. */
3157 if (CHARPOS (pos->string_pos) == 0)
3159 get_visually_first_element (it);
3160 if (IT_STRING_CHARPOS (*it) != 0)
3161 do {
3162 /* Paranoia. */
3163 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3164 bidi_move_to_visually_next (&it->bidi_it);
3165 } while (it->bidi_it.charpos != 0);
3167 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3168 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3172 if (CHARPOS (pos->string_pos) >= 0)
3174 /* Recorded position is not in an overlay string, but in another
3175 string. This can only be a string from a `display' property.
3176 IT should already be filled with that string. */
3177 it->current.string_pos = pos->string_pos;
3178 eassert (STRINGP (it->string));
3179 if (it->bidi_p)
3180 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3181 FRAME_WINDOW_P (it->f), &it->bidi_it);
3184 /* Restore position in display vector translations, control
3185 character translations or ellipses. */
3186 if (pos->dpvec_index >= 0)
3188 if (it->dpvec == NULL)
3189 get_next_display_element (it);
3190 eassert (it->dpvec && it->current.dpvec_index == 0);
3191 it->current.dpvec_index = pos->dpvec_index;
3194 CHECK_IT (it);
3195 return !overlay_strings_with_newlines;
3199 /* Initialize IT for stepping through current_buffer in window W
3200 starting at ROW->start. */
3202 static void
3203 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3205 init_from_display_pos (it, w, &row->start);
3206 it->start = row->start;
3207 it->continuation_lines_width = row->continuation_lines_width;
3208 CHECK_IT (it);
3212 /* Initialize IT for stepping through current_buffer in window W
3213 starting in the line following ROW, i.e. starting at ROW->end.
3214 Value is zero if there are overlay strings with newlines at ROW's
3215 end position. */
3217 static int
3218 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3220 int success = 0;
3222 if (init_from_display_pos (it, w, &row->end))
3224 if (row->continued_p)
3225 it->continuation_lines_width
3226 = row->continuation_lines_width + row->pixel_width;
3227 CHECK_IT (it);
3228 success = 1;
3231 return success;
3237 /***********************************************************************
3238 Text properties
3239 ***********************************************************************/
3241 /* Called when IT reaches IT->stop_charpos. Handle text property and
3242 overlay changes. Set IT->stop_charpos to the next position where
3243 to stop. */
3245 static void
3246 handle_stop (struct it *it)
3248 enum prop_handled handled;
3249 int handle_overlay_change_p;
3250 struct props *p;
3252 it->dpvec = NULL;
3253 it->current.dpvec_index = -1;
3254 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3255 it->ignore_overlay_strings_at_pos_p = 0;
3256 it->ellipsis_p = 0;
3258 /* Use face of preceding text for ellipsis (if invisible) */
3259 if (it->selective_display_ellipsis_p)
3260 it->saved_face_id = it->face_id;
3264 handled = HANDLED_NORMALLY;
3266 /* Call text property handlers. */
3267 for (p = it_props; p->handler; ++p)
3269 handled = p->handler (it);
3271 if (handled == HANDLED_RECOMPUTE_PROPS)
3272 break;
3273 else if (handled == HANDLED_RETURN)
3275 /* We still want to show before and after strings from
3276 overlays even if the actual buffer text is replaced. */
3277 if (!handle_overlay_change_p
3278 || it->sp > 1
3279 /* Don't call get_overlay_strings_1 if we already
3280 have overlay strings loaded, because doing so
3281 will load them again and push the iterator state
3282 onto the stack one more time, which is not
3283 expected by the rest of the code that processes
3284 overlay strings. */
3285 || (it->current.overlay_string_index < 0
3286 ? !get_overlay_strings_1 (it, 0, 0)
3287 : 0))
3289 if (it->ellipsis_p)
3290 setup_for_ellipsis (it, 0);
3291 /* When handling a display spec, we might load an
3292 empty string. In that case, discard it here. We
3293 used to discard it in handle_single_display_spec,
3294 but that causes get_overlay_strings_1, above, to
3295 ignore overlay strings that we must check. */
3296 if (STRINGP (it->string) && !SCHARS (it->string))
3297 pop_it (it);
3298 return;
3300 else if (STRINGP (it->string) && !SCHARS (it->string))
3301 pop_it (it);
3302 else
3304 it->ignore_overlay_strings_at_pos_p = 1;
3305 it->string_from_display_prop_p = 0;
3306 it->from_disp_prop_p = 0;
3307 handle_overlay_change_p = 0;
3309 handled = HANDLED_RECOMPUTE_PROPS;
3310 break;
3312 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3313 handle_overlay_change_p = 0;
3316 if (handled != HANDLED_RECOMPUTE_PROPS)
3318 /* Don't check for overlay strings below when set to deliver
3319 characters from a display vector. */
3320 if (it->method == GET_FROM_DISPLAY_VECTOR)
3321 handle_overlay_change_p = 0;
3323 /* Handle overlay changes.
3324 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3325 if it finds overlays. */
3326 if (handle_overlay_change_p)
3327 handled = handle_overlay_change (it);
3330 if (it->ellipsis_p)
3332 setup_for_ellipsis (it, 0);
3333 break;
3336 while (handled == HANDLED_RECOMPUTE_PROPS);
3338 /* Determine where to stop next. */
3339 if (handled == HANDLED_NORMALLY)
3340 compute_stop_pos (it);
3344 /* Compute IT->stop_charpos from text property and overlay change
3345 information for IT's current position. */
3347 static void
3348 compute_stop_pos (struct it *it)
3350 register INTERVAL iv, next_iv;
3351 Lisp_Object object, limit, position;
3352 ptrdiff_t charpos, bytepos;
3354 if (STRINGP (it->string))
3356 /* Strings are usually short, so don't limit the search for
3357 properties. */
3358 it->stop_charpos = it->end_charpos;
3359 object = it->string;
3360 limit = Qnil;
3361 charpos = IT_STRING_CHARPOS (*it);
3362 bytepos = IT_STRING_BYTEPOS (*it);
3364 else
3366 ptrdiff_t pos;
3368 /* If end_charpos is out of range for some reason, such as a
3369 misbehaving display function, rationalize it (Bug#5984). */
3370 if (it->end_charpos > ZV)
3371 it->end_charpos = ZV;
3372 it->stop_charpos = it->end_charpos;
3374 /* If next overlay change is in front of the current stop pos
3375 (which is IT->end_charpos), stop there. Note: value of
3376 next_overlay_change is point-max if no overlay change
3377 follows. */
3378 charpos = IT_CHARPOS (*it);
3379 bytepos = IT_BYTEPOS (*it);
3380 pos = next_overlay_change (charpos);
3381 if (pos < it->stop_charpos)
3382 it->stop_charpos = pos;
3384 /* If showing the region, we have to stop at the region
3385 start or end because the face might change there. */
3386 if (it->region_beg_charpos > 0)
3388 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3389 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3390 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3391 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3394 /* Set up variables for computing the stop position from text
3395 property changes. */
3396 XSETBUFFER (object, current_buffer);
3397 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3400 /* Get the interval containing IT's position. Value is a null
3401 interval if there isn't such an interval. */
3402 position = make_number (charpos);
3403 iv = validate_interval_range (object, &position, &position, 0);
3404 if (iv)
3406 Lisp_Object values_here[LAST_PROP_IDX];
3407 struct props *p;
3409 /* Get properties here. */
3410 for (p = it_props; p->handler; ++p)
3411 values_here[p->idx] = textget (iv->plist, *p->name);
3413 /* Look for an interval following iv that has different
3414 properties. */
3415 for (next_iv = next_interval (iv);
3416 (next_iv
3417 && (NILP (limit)
3418 || XFASTINT (limit) > next_iv->position));
3419 next_iv = next_interval (next_iv))
3421 for (p = it_props; p->handler; ++p)
3423 Lisp_Object new_value;
3425 new_value = textget (next_iv->plist, *p->name);
3426 if (!EQ (values_here[p->idx], new_value))
3427 break;
3430 if (p->handler)
3431 break;
3434 if (next_iv)
3436 if (INTEGERP (limit)
3437 && next_iv->position >= XFASTINT (limit))
3438 /* No text property change up to limit. */
3439 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3440 else
3441 /* Text properties change in next_iv. */
3442 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3446 if (it->cmp_it.id < 0)
3448 ptrdiff_t stoppos = it->end_charpos;
3450 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3451 stoppos = -1;
3452 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3453 stoppos, it->string);
3456 eassert (STRINGP (it->string)
3457 || (it->stop_charpos >= BEGV
3458 && it->stop_charpos >= IT_CHARPOS (*it)));
3462 /* Return the position of the next overlay change after POS in
3463 current_buffer. Value is point-max if no overlay change
3464 follows. This is like `next-overlay-change' but doesn't use
3465 xmalloc. */
3467 static ptrdiff_t
3468 next_overlay_change (ptrdiff_t pos)
3470 ptrdiff_t i, noverlays;
3471 ptrdiff_t endpos;
3472 Lisp_Object *overlays;
3474 /* Get all overlays at the given position. */
3475 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3477 /* If any of these overlays ends before endpos,
3478 use its ending point instead. */
3479 for (i = 0; i < noverlays; ++i)
3481 Lisp_Object oend;
3482 ptrdiff_t oendpos;
3484 oend = OVERLAY_END (overlays[i]);
3485 oendpos = OVERLAY_POSITION (oend);
3486 endpos = min (endpos, oendpos);
3489 return endpos;
3492 /* How many characters forward to search for a display property or
3493 display string. Searching too far forward makes the bidi display
3494 sluggish, especially in small windows. */
3495 #define MAX_DISP_SCAN 250
3497 /* Return the character position of a display string at or after
3498 position specified by POSITION. If no display string exists at or
3499 after POSITION, return ZV. A display string is either an overlay
3500 with `display' property whose value is a string, or a `display'
3501 text property whose value is a string. STRING is data about the
3502 string to iterate; if STRING->lstring is nil, we are iterating a
3503 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3504 on a GUI frame. DISP_PROP is set to zero if we searched
3505 MAX_DISP_SCAN characters forward without finding any display
3506 strings, non-zero otherwise. It is set to 2 if the display string
3507 uses any kind of `(space ...)' spec that will produce a stretch of
3508 white space in the text area. */
3509 ptrdiff_t
3510 compute_display_string_pos (struct text_pos *position,
3511 struct bidi_string_data *string,
3512 struct window *w,
3513 int frame_window_p, int *disp_prop)
3515 /* OBJECT = nil means current buffer. */
3516 Lisp_Object object, object1;
3517 Lisp_Object pos, spec, limpos;
3518 int string_p = (string && (STRINGP (string->lstring) || string->s));
3519 ptrdiff_t eob = string_p ? string->schars : ZV;
3520 ptrdiff_t begb = string_p ? 0 : BEGV;
3521 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3522 ptrdiff_t lim =
3523 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3524 struct text_pos tpos;
3525 int rv = 0;
3527 if (string && STRINGP (string->lstring))
3528 object1 = object = string->lstring;
3529 else if (w && !string_p)
3531 XSETWINDOW (object, w);
3532 object1 = Qnil;
3534 else
3535 object1 = object = Qnil;
3537 *disp_prop = 1;
3539 if (charpos >= eob
3540 /* We don't support display properties whose values are strings
3541 that have display string properties. */
3542 || string->from_disp_str
3543 /* C strings cannot have display properties. */
3544 || (string->s && !STRINGP (object)))
3546 *disp_prop = 0;
3547 return eob;
3550 /* If the character at CHARPOS is where the display string begins,
3551 return CHARPOS. */
3552 pos = make_number (charpos);
3553 if (STRINGP (object))
3554 bufpos = string->bufpos;
3555 else
3556 bufpos = charpos;
3557 tpos = *position;
3558 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3559 && (charpos <= begb
3560 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3561 object),
3562 spec))
3563 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3564 frame_window_p)))
3566 if (rv == 2)
3567 *disp_prop = 2;
3568 return charpos;
3571 /* Look forward for the first character with a `display' property
3572 that will replace the underlying text when displayed. */
3573 limpos = make_number (lim);
3574 do {
3575 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3576 CHARPOS (tpos) = XFASTINT (pos);
3577 if (CHARPOS (tpos) >= lim)
3579 *disp_prop = 0;
3580 break;
3582 if (STRINGP (object))
3583 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3584 else
3585 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3586 spec = Fget_char_property (pos, Qdisplay, object);
3587 if (!STRINGP (object))
3588 bufpos = CHARPOS (tpos);
3589 } while (NILP (spec)
3590 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3591 bufpos, frame_window_p)));
3592 if (rv == 2)
3593 *disp_prop = 2;
3595 return CHARPOS (tpos);
3598 /* Return the character position of the end of the display string that
3599 started at CHARPOS. If there's no display string at CHARPOS,
3600 return -1. A display string is either an overlay with `display'
3601 property whose value is a string or a `display' text property whose
3602 value is a string. */
3603 ptrdiff_t
3604 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3606 /* OBJECT = nil means current buffer. */
3607 Lisp_Object object =
3608 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3609 Lisp_Object pos = make_number (charpos);
3610 ptrdiff_t eob =
3611 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3613 if (charpos >= eob || (string->s && !STRINGP (object)))
3614 return eob;
3616 /* It could happen that the display property or overlay was removed
3617 since we found it in compute_display_string_pos above. One way
3618 this can happen is if JIT font-lock was called (through
3619 handle_fontified_prop), and jit-lock-functions remove text
3620 properties or overlays from the portion of buffer that includes
3621 CHARPOS. Muse mode is known to do that, for example. In this
3622 case, we return -1 to the caller, to signal that no display
3623 string is actually present at CHARPOS. See bidi_fetch_char for
3624 how this is handled.
3626 An alternative would be to never look for display properties past
3627 it->stop_charpos. But neither compute_display_string_pos nor
3628 bidi_fetch_char that calls it know or care where the next
3629 stop_charpos is. */
3630 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3631 return -1;
3633 /* Look forward for the first character where the `display' property
3634 changes. */
3635 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3637 return XFASTINT (pos);
3642 /***********************************************************************
3643 Fontification
3644 ***********************************************************************/
3646 /* Handle changes in the `fontified' property of the current buffer by
3647 calling hook functions from Qfontification_functions to fontify
3648 regions of text. */
3650 static enum prop_handled
3651 handle_fontified_prop (struct it *it)
3653 Lisp_Object prop, pos;
3654 enum prop_handled handled = HANDLED_NORMALLY;
3656 if (!NILP (Vmemory_full))
3657 return handled;
3659 /* Get the value of the `fontified' property at IT's current buffer
3660 position. (The `fontified' property doesn't have a special
3661 meaning in strings.) If the value is nil, call functions from
3662 Qfontification_functions. */
3663 if (!STRINGP (it->string)
3664 && it->s == NULL
3665 && !NILP (Vfontification_functions)
3666 && !NILP (Vrun_hooks)
3667 && (pos = make_number (IT_CHARPOS (*it)),
3668 prop = Fget_char_property (pos, Qfontified, Qnil),
3669 /* Ignore the special cased nil value always present at EOB since
3670 no amount of fontifying will be able to change it. */
3671 NILP (prop) && IT_CHARPOS (*it) < Z))
3673 ptrdiff_t count = SPECPDL_INDEX ();
3674 Lisp_Object val;
3675 struct buffer *obuf = current_buffer;
3676 int begv = BEGV, zv = ZV;
3677 int old_clip_changed = current_buffer->clip_changed;
3679 val = Vfontification_functions;
3680 specbind (Qfontification_functions, Qnil);
3682 eassert (it->end_charpos == ZV);
3684 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3685 safe_call1 (val, pos);
3686 else
3688 Lisp_Object fns, fn;
3689 struct gcpro gcpro1, gcpro2;
3691 fns = Qnil;
3692 GCPRO2 (val, fns);
3694 for (; CONSP (val); val = XCDR (val))
3696 fn = XCAR (val);
3698 if (EQ (fn, Qt))
3700 /* A value of t indicates this hook has a local
3701 binding; it means to run the global binding too.
3702 In a global value, t should not occur. If it
3703 does, we must ignore it to avoid an endless
3704 loop. */
3705 for (fns = Fdefault_value (Qfontification_functions);
3706 CONSP (fns);
3707 fns = XCDR (fns))
3709 fn = XCAR (fns);
3710 if (!EQ (fn, Qt))
3711 safe_call1 (fn, pos);
3714 else
3715 safe_call1 (fn, pos);
3718 UNGCPRO;
3721 unbind_to (count, Qnil);
3723 /* Fontification functions routinely call `save-restriction'.
3724 Normally, this tags clip_changed, which can confuse redisplay
3725 (see discussion in Bug#6671). Since we don't perform any
3726 special handling of fontification changes in the case where
3727 `save-restriction' isn't called, there's no point doing so in
3728 this case either. So, if the buffer's restrictions are
3729 actually left unchanged, reset clip_changed. */
3730 if (obuf == current_buffer)
3732 if (begv == BEGV && zv == ZV)
3733 current_buffer->clip_changed = old_clip_changed;
3735 /* There isn't much we can reasonably do to protect against
3736 misbehaving fontification, but here's a fig leaf. */
3737 else if (BUFFER_LIVE_P (obuf))
3738 set_buffer_internal_1 (obuf);
3740 /* The fontification code may have added/removed text.
3741 It could do even a lot worse, but let's at least protect against
3742 the most obvious case where only the text past `pos' gets changed',
3743 as is/was done in grep.el where some escapes sequences are turned
3744 into face properties (bug#7876). */
3745 it->end_charpos = ZV;
3747 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3748 something. This avoids an endless loop if they failed to
3749 fontify the text for which reason ever. */
3750 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3751 handled = HANDLED_RECOMPUTE_PROPS;
3754 return handled;
3759 /***********************************************************************
3760 Faces
3761 ***********************************************************************/
3763 /* Set up iterator IT from face properties at its current position.
3764 Called from handle_stop. */
3766 static enum prop_handled
3767 handle_face_prop (struct it *it)
3769 int new_face_id;
3770 ptrdiff_t next_stop;
3772 if (!STRINGP (it->string))
3774 new_face_id
3775 = face_at_buffer_position (it->w,
3776 IT_CHARPOS (*it),
3777 it->region_beg_charpos,
3778 it->region_end_charpos,
3779 &next_stop,
3780 (IT_CHARPOS (*it)
3781 + TEXT_PROP_DISTANCE_LIMIT),
3782 0, it->base_face_id);
3784 /* Is this a start of a run of characters with box face?
3785 Caveat: this can be called for a freshly initialized
3786 iterator; face_id is -1 in this case. We know that the new
3787 face will not change until limit, i.e. if the new face has a
3788 box, all characters up to limit will have one. But, as
3789 usual, we don't know whether limit is really the end. */
3790 if (new_face_id != it->face_id)
3792 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3793 /* If it->face_id is -1, old_face below will be NULL, see
3794 the definition of FACE_FROM_ID. This will happen if this
3795 is the initial call that gets the face. */
3796 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3798 /* If the value of face_id of the iterator is -1, we have to
3799 look in front of IT's position and see whether there is a
3800 face there that's different from new_face_id. */
3801 if (!old_face && IT_CHARPOS (*it) > BEG)
3803 int prev_face_id = face_before_it_pos (it);
3805 old_face = FACE_FROM_ID (it->f, prev_face_id);
3808 /* If the new face has a box, but the old face does not,
3809 this is the start of a run of characters with box face,
3810 i.e. this character has a shadow on the left side. */
3811 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3812 && (old_face == NULL || !old_face->box));
3813 it->face_box_p = new_face->box != FACE_NO_BOX;
3816 else
3818 int base_face_id;
3819 ptrdiff_t bufpos;
3820 int i;
3821 Lisp_Object from_overlay
3822 = (it->current.overlay_string_index >= 0
3823 ? it->string_overlays[it->current.overlay_string_index
3824 % OVERLAY_STRING_CHUNK_SIZE]
3825 : Qnil);
3827 /* See if we got to this string directly or indirectly from
3828 an overlay property. That includes the before-string or
3829 after-string of an overlay, strings in display properties
3830 provided by an overlay, their text properties, etc.
3832 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3833 if (! NILP (from_overlay))
3834 for (i = it->sp - 1; i >= 0; i--)
3836 if (it->stack[i].current.overlay_string_index >= 0)
3837 from_overlay
3838 = it->string_overlays[it->stack[i].current.overlay_string_index
3839 % OVERLAY_STRING_CHUNK_SIZE];
3840 else if (! NILP (it->stack[i].from_overlay))
3841 from_overlay = it->stack[i].from_overlay;
3843 if (!NILP (from_overlay))
3844 break;
3847 if (! NILP (from_overlay))
3849 bufpos = IT_CHARPOS (*it);
3850 /* For a string from an overlay, the base face depends
3851 only on text properties and ignores overlays. */
3852 base_face_id
3853 = face_for_overlay_string (it->w,
3854 IT_CHARPOS (*it),
3855 it->region_beg_charpos,
3856 it->region_end_charpos,
3857 &next_stop,
3858 (IT_CHARPOS (*it)
3859 + TEXT_PROP_DISTANCE_LIMIT),
3861 from_overlay);
3863 else
3865 bufpos = 0;
3867 /* For strings from a `display' property, use the face at
3868 IT's current buffer position as the base face to merge
3869 with, so that overlay strings appear in the same face as
3870 surrounding text, unless they specify their own
3871 faces. */
3872 base_face_id = it->string_from_prefix_prop_p
3873 ? DEFAULT_FACE_ID
3874 : underlying_face_id (it);
3877 new_face_id = face_at_string_position (it->w,
3878 it->string,
3879 IT_STRING_CHARPOS (*it),
3880 bufpos,
3881 it->region_beg_charpos,
3882 it->region_end_charpos,
3883 &next_stop,
3884 base_face_id, 0);
3886 /* Is this a start of a run of characters with box? Caveat:
3887 this can be called for a freshly allocated iterator; face_id
3888 is -1 is this case. We know that the new face will not
3889 change until the next check pos, i.e. if the new face has a
3890 box, all characters up to that position will have a
3891 box. But, as usual, we don't know whether that position
3892 is really the end. */
3893 if (new_face_id != it->face_id)
3895 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3896 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3898 /* If new face has a box but old face hasn't, this is the
3899 start of a run of characters with box, i.e. it has a
3900 shadow on the left side. */
3901 it->start_of_box_run_p
3902 = new_face->box && (old_face == NULL || !old_face->box);
3903 it->face_box_p = new_face->box != FACE_NO_BOX;
3907 it->face_id = new_face_id;
3908 return HANDLED_NORMALLY;
3912 /* Return the ID of the face ``underlying'' IT's current position,
3913 which is in a string. If the iterator is associated with a
3914 buffer, return the face at IT's current buffer position.
3915 Otherwise, use the iterator's base_face_id. */
3917 static int
3918 underlying_face_id (struct it *it)
3920 int face_id = it->base_face_id, i;
3922 eassert (STRINGP (it->string));
3924 for (i = it->sp - 1; i >= 0; --i)
3925 if (NILP (it->stack[i].string))
3926 face_id = it->stack[i].face_id;
3928 return face_id;
3932 /* Compute the face one character before or after the current position
3933 of IT, in the visual order. BEFORE_P non-zero means get the face
3934 in front (to the left in L2R paragraphs, to the right in R2L
3935 paragraphs) of IT's screen position. Value is the ID of the face. */
3937 static int
3938 face_before_or_after_it_pos (struct it *it, int before_p)
3940 int face_id, limit;
3941 ptrdiff_t next_check_charpos;
3942 struct it it_copy;
3943 void *it_copy_data = NULL;
3945 eassert (it->s == NULL);
3947 if (STRINGP (it->string))
3949 ptrdiff_t bufpos, charpos;
3950 int base_face_id;
3952 /* No face change past the end of the string (for the case
3953 we are padding with spaces). No face change before the
3954 string start. */
3955 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3956 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3957 return it->face_id;
3959 if (!it->bidi_p)
3961 /* Set charpos to the position before or after IT's current
3962 position, in the logical order, which in the non-bidi
3963 case is the same as the visual order. */
3964 if (before_p)
3965 charpos = IT_STRING_CHARPOS (*it) - 1;
3966 else if (it->what == IT_COMPOSITION)
3967 /* For composition, we must check the character after the
3968 composition. */
3969 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3970 else
3971 charpos = IT_STRING_CHARPOS (*it) + 1;
3973 else
3975 if (before_p)
3977 /* With bidi iteration, the character before the current
3978 in the visual order cannot be found by simple
3979 iteration, because "reverse" reordering is not
3980 supported. Instead, we need to use the move_it_*
3981 family of functions. */
3982 /* Ignore face changes before the first visible
3983 character on this display line. */
3984 if (it->current_x <= it->first_visible_x)
3985 return it->face_id;
3986 SAVE_IT (it_copy, *it, it_copy_data);
3987 /* Implementation note: Since move_it_in_display_line
3988 works in the iterator geometry, and thinks the first
3989 character is always the leftmost, even in R2L lines,
3990 we don't need to distinguish between the R2L and L2R
3991 cases here. */
3992 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3993 it_copy.current_x - 1, MOVE_TO_X);
3994 charpos = IT_STRING_CHARPOS (it_copy);
3995 RESTORE_IT (it, it, it_copy_data);
3997 else
3999 /* Set charpos to the string position of the character
4000 that comes after IT's current position in the visual
4001 order. */
4002 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4004 it_copy = *it;
4005 while (n--)
4006 bidi_move_to_visually_next (&it_copy.bidi_it);
4008 charpos = it_copy.bidi_it.charpos;
4011 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4013 if (it->current.overlay_string_index >= 0)
4014 bufpos = IT_CHARPOS (*it);
4015 else
4016 bufpos = 0;
4018 base_face_id = underlying_face_id (it);
4020 /* Get the face for ASCII, or unibyte. */
4021 face_id = face_at_string_position (it->w,
4022 it->string,
4023 charpos,
4024 bufpos,
4025 it->region_beg_charpos,
4026 it->region_end_charpos,
4027 &next_check_charpos,
4028 base_face_id, 0);
4030 /* Correct the face for charsets different from ASCII. Do it
4031 for the multibyte case only. The face returned above is
4032 suitable for unibyte text if IT->string is unibyte. */
4033 if (STRING_MULTIBYTE (it->string))
4035 struct text_pos pos1 = string_pos (charpos, it->string);
4036 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4037 int c, len;
4038 struct face *face = FACE_FROM_ID (it->f, face_id);
4040 c = string_char_and_length (p, &len);
4041 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4044 else
4046 struct text_pos pos;
4048 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4049 || (IT_CHARPOS (*it) <= BEGV && before_p))
4050 return it->face_id;
4052 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4053 pos = it->current.pos;
4055 if (!it->bidi_p)
4057 if (before_p)
4058 DEC_TEXT_POS (pos, it->multibyte_p);
4059 else
4061 if (it->what == IT_COMPOSITION)
4063 /* For composition, we must check the position after
4064 the composition. */
4065 pos.charpos += it->cmp_it.nchars;
4066 pos.bytepos += it->len;
4068 else
4069 INC_TEXT_POS (pos, it->multibyte_p);
4072 else
4074 if (before_p)
4076 /* With bidi iteration, the character before the current
4077 in the visual order cannot be found by simple
4078 iteration, because "reverse" reordering is not
4079 supported. Instead, we need to use the move_it_*
4080 family of functions. */
4081 /* Ignore face changes before the first visible
4082 character on this display line. */
4083 if (it->current_x <= it->first_visible_x)
4084 return it->face_id;
4085 SAVE_IT (it_copy, *it, it_copy_data);
4086 /* Implementation note: Since move_it_in_display_line
4087 works in the iterator geometry, and thinks the first
4088 character is always the leftmost, even in R2L lines,
4089 we don't need to distinguish between the R2L and L2R
4090 cases here. */
4091 move_it_in_display_line (&it_copy, ZV,
4092 it_copy.current_x - 1, MOVE_TO_X);
4093 pos = it_copy.current.pos;
4094 RESTORE_IT (it, it, it_copy_data);
4096 else
4098 /* Set charpos to the buffer position of the character
4099 that comes after IT's current position in the visual
4100 order. */
4101 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4103 it_copy = *it;
4104 while (n--)
4105 bidi_move_to_visually_next (&it_copy.bidi_it);
4107 SET_TEXT_POS (pos,
4108 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4111 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4113 /* Determine face for CHARSET_ASCII, or unibyte. */
4114 face_id = face_at_buffer_position (it->w,
4115 CHARPOS (pos),
4116 it->region_beg_charpos,
4117 it->region_end_charpos,
4118 &next_check_charpos,
4119 limit, 0, -1);
4121 /* Correct the face for charsets different from ASCII. Do it
4122 for the multibyte case only. The face returned above is
4123 suitable for unibyte text if current_buffer is unibyte. */
4124 if (it->multibyte_p)
4126 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4127 struct face *face = FACE_FROM_ID (it->f, face_id);
4128 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4132 return face_id;
4137 /***********************************************************************
4138 Invisible text
4139 ***********************************************************************/
4141 /* Set up iterator IT from invisible properties at its current
4142 position. Called from handle_stop. */
4144 static enum prop_handled
4145 handle_invisible_prop (struct it *it)
4147 enum prop_handled handled = HANDLED_NORMALLY;
4148 int invis_p;
4149 Lisp_Object prop;
4151 if (STRINGP (it->string))
4153 Lisp_Object end_charpos, limit, charpos;
4155 /* Get the value of the invisible text property at the
4156 current position. Value will be nil if there is no such
4157 property. */
4158 charpos = make_number (IT_STRING_CHARPOS (*it));
4159 prop = Fget_text_property (charpos, Qinvisible, it->string);
4160 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4162 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4164 /* Record whether we have to display an ellipsis for the
4165 invisible text. */
4166 int display_ellipsis_p = (invis_p == 2);
4167 ptrdiff_t len, endpos;
4169 handled = HANDLED_RECOMPUTE_PROPS;
4171 /* Get the position at which the next visible text can be
4172 found in IT->string, if any. */
4173 endpos = len = SCHARS (it->string);
4174 XSETINT (limit, len);
4177 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4178 it->string, limit);
4179 if (INTEGERP (end_charpos))
4181 endpos = XFASTINT (end_charpos);
4182 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4183 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4184 if (invis_p == 2)
4185 display_ellipsis_p = 1;
4188 while (invis_p && endpos < len);
4190 if (display_ellipsis_p)
4191 it->ellipsis_p = 1;
4193 if (endpos < len)
4195 /* Text at END_CHARPOS is visible. Move IT there. */
4196 struct text_pos old;
4197 ptrdiff_t oldpos;
4199 old = it->current.string_pos;
4200 oldpos = CHARPOS (old);
4201 if (it->bidi_p)
4203 if (it->bidi_it.first_elt
4204 && it->bidi_it.charpos < SCHARS (it->string))
4205 bidi_paragraph_init (it->paragraph_embedding,
4206 &it->bidi_it, 1);
4207 /* Bidi-iterate out of the invisible text. */
4210 bidi_move_to_visually_next (&it->bidi_it);
4212 while (oldpos <= it->bidi_it.charpos
4213 && it->bidi_it.charpos < endpos);
4215 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4216 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4217 if (IT_CHARPOS (*it) >= endpos)
4218 it->prev_stop = endpos;
4220 else
4222 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4223 compute_string_pos (&it->current.string_pos, old, it->string);
4226 else
4228 /* The rest of the string is invisible. If this is an
4229 overlay string, proceed with the next overlay string
4230 or whatever comes and return a character from there. */
4231 if (it->current.overlay_string_index >= 0
4232 && !display_ellipsis_p)
4234 next_overlay_string (it);
4235 /* Don't check for overlay strings when we just
4236 finished processing them. */
4237 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4239 else
4241 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4242 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4247 else
4249 ptrdiff_t newpos, next_stop, start_charpos, tem;
4250 Lisp_Object pos, overlay;
4252 /* First of all, is there invisible text at this position? */
4253 tem = start_charpos = IT_CHARPOS (*it);
4254 pos = make_number (tem);
4255 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4256 &overlay);
4257 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4259 /* If we are on invisible text, skip over it. */
4260 if (invis_p && start_charpos < it->end_charpos)
4262 /* Record whether we have to display an ellipsis for the
4263 invisible text. */
4264 int display_ellipsis_p = invis_p == 2;
4266 handled = HANDLED_RECOMPUTE_PROPS;
4268 /* Loop skipping over invisible text. The loop is left at
4269 ZV or with IT on the first char being visible again. */
4272 /* Try to skip some invisible text. Return value is the
4273 position reached which can be equal to where we start
4274 if there is nothing invisible there. This skips both
4275 over invisible text properties and overlays with
4276 invisible property. */
4277 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4279 /* If we skipped nothing at all we weren't at invisible
4280 text in the first place. If everything to the end of
4281 the buffer was skipped, end the loop. */
4282 if (newpos == tem || newpos >= ZV)
4283 invis_p = 0;
4284 else
4286 /* We skipped some characters but not necessarily
4287 all there are. Check if we ended up on visible
4288 text. Fget_char_property returns the property of
4289 the char before the given position, i.e. if we
4290 get invis_p = 0, this means that the char at
4291 newpos is visible. */
4292 pos = make_number (newpos);
4293 prop = Fget_char_property (pos, Qinvisible, it->window);
4294 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4297 /* If we ended up on invisible text, proceed to
4298 skip starting with next_stop. */
4299 if (invis_p)
4300 tem = next_stop;
4302 /* If there are adjacent invisible texts, don't lose the
4303 second one's ellipsis. */
4304 if (invis_p == 2)
4305 display_ellipsis_p = 1;
4307 while (invis_p);
4309 /* The position newpos is now either ZV or on visible text. */
4310 if (it->bidi_p)
4312 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4313 int on_newline =
4314 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4315 int after_newline =
4316 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4318 /* If the invisible text ends on a newline or on a
4319 character after a newline, we can avoid the costly,
4320 character by character, bidi iteration to NEWPOS, and
4321 instead simply reseat the iterator there. That's
4322 because all bidi reordering information is tossed at
4323 the newline. This is a big win for modes that hide
4324 complete lines, like Outline, Org, etc. */
4325 if (on_newline || after_newline)
4327 struct text_pos tpos;
4328 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4330 SET_TEXT_POS (tpos, newpos, bpos);
4331 reseat_1 (it, tpos, 0);
4332 /* If we reseat on a newline/ZV, we need to prep the
4333 bidi iterator for advancing to the next character
4334 after the newline/EOB, keeping the current paragraph
4335 direction (so that PRODUCE_GLYPHS does TRT wrt
4336 prepending/appending glyphs to a glyph row). */
4337 if (on_newline)
4339 it->bidi_it.first_elt = 0;
4340 it->bidi_it.paragraph_dir = pdir;
4341 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4342 it->bidi_it.nchars = 1;
4343 it->bidi_it.ch_len = 1;
4346 else /* Must use the slow method. */
4348 /* With bidi iteration, the region of invisible text
4349 could start and/or end in the middle of a
4350 non-base embedding level. Therefore, we need to
4351 skip invisible text using the bidi iterator,
4352 starting at IT's current position, until we find
4353 ourselves outside of the invisible text.
4354 Skipping invisible text _after_ bidi iteration
4355 avoids affecting the visual order of the
4356 displayed text when invisible properties are
4357 added or removed. */
4358 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4360 /* If we were `reseat'ed to a new paragraph,
4361 determine the paragraph base direction. We
4362 need to do it now because
4363 next_element_from_buffer may not have a
4364 chance to do it, if we are going to skip any
4365 text at the beginning, which resets the
4366 FIRST_ELT flag. */
4367 bidi_paragraph_init (it->paragraph_embedding,
4368 &it->bidi_it, 1);
4372 bidi_move_to_visually_next (&it->bidi_it);
4374 while (it->stop_charpos <= it->bidi_it.charpos
4375 && it->bidi_it.charpos < newpos);
4376 IT_CHARPOS (*it) = it->bidi_it.charpos;
4377 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4378 /* If we overstepped NEWPOS, record its position in
4379 the iterator, so that we skip invisible text if
4380 later the bidi iteration lands us in the
4381 invisible region again. */
4382 if (IT_CHARPOS (*it) >= newpos)
4383 it->prev_stop = newpos;
4386 else
4388 IT_CHARPOS (*it) = newpos;
4389 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4392 /* If there are before-strings at the start of invisible
4393 text, and the text is invisible because of a text
4394 property, arrange to show before-strings because 20.x did
4395 it that way. (If the text is invisible because of an
4396 overlay property instead of a text property, this is
4397 already handled in the overlay code.) */
4398 if (NILP (overlay)
4399 && get_overlay_strings (it, it->stop_charpos))
4401 handled = HANDLED_RECOMPUTE_PROPS;
4402 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4404 else if (display_ellipsis_p)
4406 /* Make sure that the glyphs of the ellipsis will get
4407 correct `charpos' values. If we would not update
4408 it->position here, the glyphs would belong to the
4409 last visible character _before_ the invisible
4410 text, which confuses `set_cursor_from_row'.
4412 We use the last invisible position instead of the
4413 first because this way the cursor is always drawn on
4414 the first "." of the ellipsis, whenever PT is inside
4415 the invisible text. Otherwise the cursor would be
4416 placed _after_ the ellipsis when the point is after the
4417 first invisible character. */
4418 if (!STRINGP (it->object))
4420 it->position.charpos = newpos - 1;
4421 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4423 it->ellipsis_p = 1;
4424 /* Let the ellipsis display before
4425 considering any properties of the following char.
4426 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4427 handled = HANDLED_RETURN;
4432 return handled;
4436 /* Make iterator IT return `...' next.
4437 Replaces LEN characters from buffer. */
4439 static void
4440 setup_for_ellipsis (struct it *it, int len)
4442 /* Use the display table definition for `...'. Invalid glyphs
4443 will be handled by the method returning elements from dpvec. */
4444 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4446 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4447 it->dpvec = v->contents;
4448 it->dpend = v->contents + v->header.size;
4450 else
4452 /* Default `...'. */
4453 it->dpvec = default_invis_vector;
4454 it->dpend = default_invis_vector + 3;
4457 it->dpvec_char_len = len;
4458 it->current.dpvec_index = 0;
4459 it->dpvec_face_id = -1;
4461 /* Remember the current face id in case glyphs specify faces.
4462 IT's face is restored in set_iterator_to_next.
4463 saved_face_id was set to preceding char's face in handle_stop. */
4464 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4465 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4467 it->method = GET_FROM_DISPLAY_VECTOR;
4468 it->ellipsis_p = 1;
4473 /***********************************************************************
4474 'display' property
4475 ***********************************************************************/
4477 /* Set up iterator IT from `display' property at its current position.
4478 Called from handle_stop.
4479 We return HANDLED_RETURN if some part of the display property
4480 overrides the display of the buffer text itself.
4481 Otherwise we return HANDLED_NORMALLY. */
4483 static enum prop_handled
4484 handle_display_prop (struct it *it)
4486 Lisp_Object propval, object, overlay;
4487 struct text_pos *position;
4488 ptrdiff_t bufpos;
4489 /* Nonzero if some property replaces the display of the text itself. */
4490 int display_replaced_p = 0;
4492 if (STRINGP (it->string))
4494 object = it->string;
4495 position = &it->current.string_pos;
4496 bufpos = CHARPOS (it->current.pos);
4498 else
4500 XSETWINDOW (object, it->w);
4501 position = &it->current.pos;
4502 bufpos = CHARPOS (*position);
4505 /* Reset those iterator values set from display property values. */
4506 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4507 it->space_width = Qnil;
4508 it->font_height = Qnil;
4509 it->voffset = 0;
4511 /* We don't support recursive `display' properties, i.e. string
4512 values that have a string `display' property, that have a string
4513 `display' property etc. */
4514 if (!it->string_from_display_prop_p)
4515 it->area = TEXT_AREA;
4517 propval = get_char_property_and_overlay (make_number (position->charpos),
4518 Qdisplay, object, &overlay);
4519 if (NILP (propval))
4520 return HANDLED_NORMALLY;
4521 /* Now OVERLAY is the overlay that gave us this property, or nil
4522 if it was a text property. */
4524 if (!STRINGP (it->string))
4525 object = it->w->contents;
4527 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4528 position, bufpos,
4529 FRAME_WINDOW_P (it->f));
4531 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4534 /* Subroutine of handle_display_prop. Returns non-zero if the display
4535 specification in SPEC is a replacing specification, i.e. it would
4536 replace the text covered by `display' property with something else,
4537 such as an image or a display string. If SPEC includes any kind or
4538 `(space ...) specification, the value is 2; this is used by
4539 compute_display_string_pos, which see.
4541 See handle_single_display_spec for documentation of arguments.
4542 frame_window_p is non-zero if the window being redisplayed is on a
4543 GUI frame; this argument is used only if IT is NULL, see below.
4545 IT can be NULL, if this is called by the bidi reordering code
4546 through compute_display_string_pos, which see. In that case, this
4547 function only examines SPEC, but does not otherwise "handle" it, in
4548 the sense that it doesn't set up members of IT from the display
4549 spec. */
4550 static int
4551 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4552 Lisp_Object overlay, struct text_pos *position,
4553 ptrdiff_t bufpos, int frame_window_p)
4555 int replacing_p = 0;
4556 int rv;
4558 if (CONSP (spec)
4559 /* Simple specifications. */
4560 && !EQ (XCAR (spec), Qimage)
4561 && !EQ (XCAR (spec), Qspace)
4562 && !EQ (XCAR (spec), Qwhen)
4563 && !EQ (XCAR (spec), Qslice)
4564 && !EQ (XCAR (spec), Qspace_width)
4565 && !EQ (XCAR (spec), Qheight)
4566 && !EQ (XCAR (spec), Qraise)
4567 /* Marginal area specifications. */
4568 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4569 && !EQ (XCAR (spec), Qleft_fringe)
4570 && !EQ (XCAR (spec), Qright_fringe)
4571 && !NILP (XCAR (spec)))
4573 for (; CONSP (spec); spec = XCDR (spec))
4575 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4576 overlay, position, bufpos,
4577 replacing_p, frame_window_p)))
4579 replacing_p = rv;
4580 /* If some text in a string is replaced, `position' no
4581 longer points to the position of `object'. */
4582 if (!it || STRINGP (object))
4583 break;
4587 else if (VECTORP (spec))
4589 ptrdiff_t i;
4590 for (i = 0; i < ASIZE (spec); ++i)
4591 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4592 overlay, position, bufpos,
4593 replacing_p, frame_window_p)))
4595 replacing_p = rv;
4596 /* If some text in a string is replaced, `position' no
4597 longer points to the position of `object'. */
4598 if (!it || STRINGP (object))
4599 break;
4602 else
4604 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4605 position, bufpos, 0,
4606 frame_window_p)))
4607 replacing_p = rv;
4610 return replacing_p;
4613 /* Value is the position of the end of the `display' property starting
4614 at START_POS in OBJECT. */
4616 static struct text_pos
4617 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4619 Lisp_Object end;
4620 struct text_pos end_pos;
4622 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4623 Qdisplay, object, Qnil);
4624 CHARPOS (end_pos) = XFASTINT (end);
4625 if (STRINGP (object))
4626 compute_string_pos (&end_pos, start_pos, it->string);
4627 else
4628 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4630 return end_pos;
4634 /* Set up IT from a single `display' property specification SPEC. OBJECT
4635 is the object in which the `display' property was found. *POSITION
4636 is the position in OBJECT at which the `display' property was found.
4637 BUFPOS is the buffer position of OBJECT (different from POSITION if
4638 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4639 previously saw a display specification which already replaced text
4640 display with something else, for example an image; we ignore such
4641 properties after the first one has been processed.
4643 OVERLAY is the overlay this `display' property came from,
4644 or nil if it was a text property.
4646 If SPEC is a `space' or `image' specification, and in some other
4647 cases too, set *POSITION to the position where the `display'
4648 property ends.
4650 If IT is NULL, only examine the property specification in SPEC, but
4651 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4652 is intended to be displayed in a window on a GUI frame.
4654 Value is non-zero if something was found which replaces the display
4655 of buffer or string text. */
4657 static int
4658 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4659 Lisp_Object overlay, struct text_pos *position,
4660 ptrdiff_t bufpos, int display_replaced_p,
4661 int frame_window_p)
4663 Lisp_Object form;
4664 Lisp_Object location, value;
4665 struct text_pos start_pos = *position;
4666 int valid_p;
4668 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4669 If the result is non-nil, use VALUE instead of SPEC. */
4670 form = Qt;
4671 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4673 spec = XCDR (spec);
4674 if (!CONSP (spec))
4675 return 0;
4676 form = XCAR (spec);
4677 spec = XCDR (spec);
4680 if (!NILP (form) && !EQ (form, Qt))
4682 ptrdiff_t count = SPECPDL_INDEX ();
4683 struct gcpro gcpro1;
4685 /* Bind `object' to the object having the `display' property, a
4686 buffer or string. Bind `position' to the position in the
4687 object where the property was found, and `buffer-position'
4688 to the current position in the buffer. */
4690 if (NILP (object))
4691 XSETBUFFER (object, current_buffer);
4692 specbind (Qobject, object);
4693 specbind (Qposition, make_number (CHARPOS (*position)));
4694 specbind (Qbuffer_position, make_number (bufpos));
4695 GCPRO1 (form);
4696 form = safe_eval (form);
4697 UNGCPRO;
4698 unbind_to (count, Qnil);
4701 if (NILP (form))
4702 return 0;
4704 /* Handle `(height HEIGHT)' specifications. */
4705 if (CONSP (spec)
4706 && EQ (XCAR (spec), Qheight)
4707 && CONSP (XCDR (spec)))
4709 if (it)
4711 if (!FRAME_WINDOW_P (it->f))
4712 return 0;
4714 it->font_height = XCAR (XCDR (spec));
4715 if (!NILP (it->font_height))
4717 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4718 int new_height = -1;
4720 if (CONSP (it->font_height)
4721 && (EQ (XCAR (it->font_height), Qplus)
4722 || EQ (XCAR (it->font_height), Qminus))
4723 && CONSP (XCDR (it->font_height))
4724 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4726 /* `(+ N)' or `(- N)' where N is an integer. */
4727 int steps = XINT (XCAR (XCDR (it->font_height)));
4728 if (EQ (XCAR (it->font_height), Qplus))
4729 steps = - steps;
4730 it->face_id = smaller_face (it->f, it->face_id, steps);
4732 else if (FUNCTIONP (it->font_height))
4734 /* Call function with current height as argument.
4735 Value is the new height. */
4736 Lisp_Object height;
4737 height = safe_call1 (it->font_height,
4738 face->lface[LFACE_HEIGHT_INDEX]);
4739 if (NUMBERP (height))
4740 new_height = XFLOATINT (height);
4742 else if (NUMBERP (it->font_height))
4744 /* Value is a multiple of the canonical char height. */
4745 struct face *f;
4747 f = FACE_FROM_ID (it->f,
4748 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4749 new_height = (XFLOATINT (it->font_height)
4750 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4752 else
4754 /* Evaluate IT->font_height with `height' bound to the
4755 current specified height to get the new height. */
4756 ptrdiff_t count = SPECPDL_INDEX ();
4758 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4759 value = safe_eval (it->font_height);
4760 unbind_to (count, Qnil);
4762 if (NUMBERP (value))
4763 new_height = XFLOATINT (value);
4766 if (new_height > 0)
4767 it->face_id = face_with_height (it->f, it->face_id, new_height);
4771 return 0;
4774 /* Handle `(space-width WIDTH)'. */
4775 if (CONSP (spec)
4776 && EQ (XCAR (spec), Qspace_width)
4777 && CONSP (XCDR (spec)))
4779 if (it)
4781 if (!FRAME_WINDOW_P (it->f))
4782 return 0;
4784 value = XCAR (XCDR (spec));
4785 if (NUMBERP (value) && XFLOATINT (value) > 0)
4786 it->space_width = value;
4789 return 0;
4792 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4793 if (CONSP (spec)
4794 && EQ (XCAR (spec), Qslice))
4796 Lisp_Object tem;
4798 if (it)
4800 if (!FRAME_WINDOW_P (it->f))
4801 return 0;
4803 if (tem = XCDR (spec), CONSP (tem))
4805 it->slice.x = XCAR (tem);
4806 if (tem = XCDR (tem), CONSP (tem))
4808 it->slice.y = XCAR (tem);
4809 if (tem = XCDR (tem), CONSP (tem))
4811 it->slice.width = XCAR (tem);
4812 if (tem = XCDR (tem), CONSP (tem))
4813 it->slice.height = XCAR (tem);
4819 return 0;
4822 /* Handle `(raise FACTOR)'. */
4823 if (CONSP (spec)
4824 && EQ (XCAR (spec), Qraise)
4825 && CONSP (XCDR (spec)))
4827 if (it)
4829 if (!FRAME_WINDOW_P (it->f))
4830 return 0;
4832 #ifdef HAVE_WINDOW_SYSTEM
4833 value = XCAR (XCDR (spec));
4834 if (NUMBERP (value))
4836 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4837 it->voffset = - (XFLOATINT (value)
4838 * (FONT_HEIGHT (face->font)));
4840 #endif /* HAVE_WINDOW_SYSTEM */
4843 return 0;
4846 /* Don't handle the other kinds of display specifications
4847 inside a string that we got from a `display' property. */
4848 if (it && it->string_from_display_prop_p)
4849 return 0;
4851 /* Characters having this form of property are not displayed, so
4852 we have to find the end of the property. */
4853 if (it)
4855 start_pos = *position;
4856 *position = display_prop_end (it, object, start_pos);
4858 value = Qnil;
4860 /* Stop the scan at that end position--we assume that all
4861 text properties change there. */
4862 if (it)
4863 it->stop_charpos = position->charpos;
4865 /* Handle `(left-fringe BITMAP [FACE])'
4866 and `(right-fringe BITMAP [FACE])'. */
4867 if (CONSP (spec)
4868 && (EQ (XCAR (spec), Qleft_fringe)
4869 || EQ (XCAR (spec), Qright_fringe))
4870 && CONSP (XCDR (spec)))
4872 int fringe_bitmap;
4874 if (it)
4876 if (!FRAME_WINDOW_P (it->f))
4877 /* If we return here, POSITION has been advanced
4878 across the text with this property. */
4880 /* Synchronize the bidi iterator with POSITION. This is
4881 needed because we are not going to push the iterator
4882 on behalf of this display property, so there will be
4883 no pop_it call to do this synchronization for us. */
4884 if (it->bidi_p)
4886 it->position = *position;
4887 iterate_out_of_display_property (it);
4888 *position = it->position;
4890 return 1;
4893 else if (!frame_window_p)
4894 return 1;
4896 #ifdef HAVE_WINDOW_SYSTEM
4897 value = XCAR (XCDR (spec));
4898 if (!SYMBOLP (value)
4899 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4900 /* If we return here, POSITION has been advanced
4901 across the text with this property. */
4903 if (it && it->bidi_p)
4905 it->position = *position;
4906 iterate_out_of_display_property (it);
4907 *position = it->position;
4909 return 1;
4912 if (it)
4914 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4916 if (CONSP (XCDR (XCDR (spec))))
4918 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4919 int face_id2 = lookup_derived_face (it->f, face_name,
4920 FRINGE_FACE_ID, 0);
4921 if (face_id2 >= 0)
4922 face_id = face_id2;
4925 /* Save current settings of IT so that we can restore them
4926 when we are finished with the glyph property value. */
4927 push_it (it, position);
4929 it->area = TEXT_AREA;
4930 it->what = IT_IMAGE;
4931 it->image_id = -1; /* no image */
4932 it->position = start_pos;
4933 it->object = NILP (object) ? it->w->contents : object;
4934 it->method = GET_FROM_IMAGE;
4935 it->from_overlay = Qnil;
4936 it->face_id = face_id;
4937 it->from_disp_prop_p = 1;
4939 /* Say that we haven't consumed the characters with
4940 `display' property yet. The call to pop_it in
4941 set_iterator_to_next will clean this up. */
4942 *position = start_pos;
4944 if (EQ (XCAR (spec), Qleft_fringe))
4946 it->left_user_fringe_bitmap = fringe_bitmap;
4947 it->left_user_fringe_face_id = face_id;
4949 else
4951 it->right_user_fringe_bitmap = fringe_bitmap;
4952 it->right_user_fringe_face_id = face_id;
4955 #endif /* HAVE_WINDOW_SYSTEM */
4956 return 1;
4959 /* Prepare to handle `((margin left-margin) ...)',
4960 `((margin right-margin) ...)' and `((margin nil) ...)'
4961 prefixes for display specifications. */
4962 location = Qunbound;
4963 if (CONSP (spec) && CONSP (XCAR (spec)))
4965 Lisp_Object tem;
4967 value = XCDR (spec);
4968 if (CONSP (value))
4969 value = XCAR (value);
4971 tem = XCAR (spec);
4972 if (EQ (XCAR (tem), Qmargin)
4973 && (tem = XCDR (tem),
4974 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4975 (NILP (tem)
4976 || EQ (tem, Qleft_margin)
4977 || EQ (tem, Qright_margin))))
4978 location = tem;
4981 if (EQ (location, Qunbound))
4983 location = Qnil;
4984 value = spec;
4987 /* After this point, VALUE is the property after any
4988 margin prefix has been stripped. It must be a string,
4989 an image specification, or `(space ...)'.
4991 LOCATION specifies where to display: `left-margin',
4992 `right-margin' or nil. */
4994 valid_p = (STRINGP (value)
4995 #ifdef HAVE_WINDOW_SYSTEM
4996 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4997 && valid_image_p (value))
4998 #endif /* not HAVE_WINDOW_SYSTEM */
4999 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5001 if (valid_p && !display_replaced_p)
5003 int retval = 1;
5005 if (!it)
5007 /* Callers need to know whether the display spec is any kind
5008 of `(space ...)' spec that is about to affect text-area
5009 display. */
5010 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5011 retval = 2;
5012 return retval;
5015 /* Save current settings of IT so that we can restore them
5016 when we are finished with the glyph property value. */
5017 push_it (it, position);
5018 it->from_overlay = overlay;
5019 it->from_disp_prop_p = 1;
5021 if (NILP (location))
5022 it->area = TEXT_AREA;
5023 else if (EQ (location, Qleft_margin))
5024 it->area = LEFT_MARGIN_AREA;
5025 else
5026 it->area = RIGHT_MARGIN_AREA;
5028 if (STRINGP (value))
5030 it->string = value;
5031 it->multibyte_p = STRING_MULTIBYTE (it->string);
5032 it->current.overlay_string_index = -1;
5033 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5034 it->end_charpos = it->string_nchars = SCHARS (it->string);
5035 it->method = GET_FROM_STRING;
5036 it->stop_charpos = 0;
5037 it->prev_stop = 0;
5038 it->base_level_stop = 0;
5039 it->string_from_display_prop_p = 1;
5040 /* Say that we haven't consumed the characters with
5041 `display' property yet. The call to pop_it in
5042 set_iterator_to_next will clean this up. */
5043 if (BUFFERP (object))
5044 *position = start_pos;
5046 /* Force paragraph direction to be that of the parent
5047 object. If the parent object's paragraph direction is
5048 not yet determined, default to L2R. */
5049 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5050 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5051 else
5052 it->paragraph_embedding = L2R;
5054 /* Set up the bidi iterator for this display string. */
5055 if (it->bidi_p)
5057 it->bidi_it.string.lstring = it->string;
5058 it->bidi_it.string.s = NULL;
5059 it->bidi_it.string.schars = it->end_charpos;
5060 it->bidi_it.string.bufpos = bufpos;
5061 it->bidi_it.string.from_disp_str = 1;
5062 it->bidi_it.string.unibyte = !it->multibyte_p;
5063 it->bidi_it.w = it->w;
5064 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5067 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5069 it->method = GET_FROM_STRETCH;
5070 it->object = value;
5071 *position = it->position = start_pos;
5072 retval = 1 + (it->area == TEXT_AREA);
5074 #ifdef HAVE_WINDOW_SYSTEM
5075 else
5077 it->what = IT_IMAGE;
5078 it->image_id = lookup_image (it->f, value);
5079 it->position = start_pos;
5080 it->object = NILP (object) ? it->w->contents : object;
5081 it->method = GET_FROM_IMAGE;
5083 /* Say that we haven't consumed the characters with
5084 `display' property yet. The call to pop_it in
5085 set_iterator_to_next will clean this up. */
5086 *position = start_pos;
5088 #endif /* HAVE_WINDOW_SYSTEM */
5090 return retval;
5093 /* Invalid property or property not supported. Restore
5094 POSITION to what it was before. */
5095 *position = start_pos;
5096 return 0;
5099 /* Check if PROP is a display property value whose text should be
5100 treated as intangible. OVERLAY is the overlay from which PROP
5101 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5102 specify the buffer position covered by PROP. */
5105 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5106 ptrdiff_t charpos, ptrdiff_t bytepos)
5108 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5109 struct text_pos position;
5111 SET_TEXT_POS (position, charpos, bytepos);
5112 return handle_display_spec (NULL, prop, Qnil, overlay,
5113 &position, charpos, frame_window_p);
5117 /* Return 1 if PROP is a display sub-property value containing STRING.
5119 Implementation note: this and the following function are really
5120 special cases of handle_display_spec and
5121 handle_single_display_spec, and should ideally use the same code.
5122 Until they do, these two pairs must be consistent and must be
5123 modified in sync. */
5125 static int
5126 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5128 if (EQ (string, prop))
5129 return 1;
5131 /* Skip over `when FORM'. */
5132 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5134 prop = XCDR (prop);
5135 if (!CONSP (prop))
5136 return 0;
5137 /* Actually, the condition following `when' should be eval'ed,
5138 like handle_single_display_spec does, and we should return
5139 zero if it evaluates to nil. However, this function is
5140 called only when the buffer was already displayed and some
5141 glyph in the glyph matrix was found to come from a display
5142 string. Therefore, the condition was already evaluated, and
5143 the result was non-nil, otherwise the display string wouldn't
5144 have been displayed and we would have never been called for
5145 this property. Thus, we can skip the evaluation and assume
5146 its result is non-nil. */
5147 prop = XCDR (prop);
5150 if (CONSP (prop))
5151 /* Skip over `margin LOCATION'. */
5152 if (EQ (XCAR (prop), Qmargin))
5154 prop = XCDR (prop);
5155 if (!CONSP (prop))
5156 return 0;
5158 prop = XCDR (prop);
5159 if (!CONSP (prop))
5160 return 0;
5163 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5167 /* Return 1 if STRING appears in the `display' property PROP. */
5169 static int
5170 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5172 if (CONSP (prop)
5173 && !EQ (XCAR (prop), Qwhen)
5174 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5176 /* A list of sub-properties. */
5177 while (CONSP (prop))
5179 if (single_display_spec_string_p (XCAR (prop), string))
5180 return 1;
5181 prop = XCDR (prop);
5184 else if (VECTORP (prop))
5186 /* A vector of sub-properties. */
5187 ptrdiff_t i;
5188 for (i = 0; i < ASIZE (prop); ++i)
5189 if (single_display_spec_string_p (AREF (prop, i), string))
5190 return 1;
5192 else
5193 return single_display_spec_string_p (prop, string);
5195 return 0;
5198 /* Look for STRING in overlays and text properties in the current
5199 buffer, between character positions FROM and TO (excluding TO).
5200 BACK_P non-zero means look back (in this case, TO is supposed to be
5201 less than FROM).
5202 Value is the first character position where STRING was found, or
5203 zero if it wasn't found before hitting TO.
5205 This function may only use code that doesn't eval because it is
5206 called asynchronously from note_mouse_highlight. */
5208 static ptrdiff_t
5209 string_buffer_position_lim (Lisp_Object string,
5210 ptrdiff_t from, ptrdiff_t to, int back_p)
5212 Lisp_Object limit, prop, pos;
5213 int found = 0;
5215 pos = make_number (max (from, BEGV));
5217 if (!back_p) /* looking forward */
5219 limit = make_number (min (to, ZV));
5220 while (!found && !EQ (pos, limit))
5222 prop = Fget_char_property (pos, Qdisplay, Qnil);
5223 if (!NILP (prop) && display_prop_string_p (prop, string))
5224 found = 1;
5225 else
5226 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5227 limit);
5230 else /* looking back */
5232 limit = make_number (max (to, BEGV));
5233 while (!found && !EQ (pos, limit))
5235 prop = Fget_char_property (pos, Qdisplay, Qnil);
5236 if (!NILP (prop) && display_prop_string_p (prop, string))
5237 found = 1;
5238 else
5239 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5240 limit);
5244 return found ? XINT (pos) : 0;
5247 /* Determine which buffer position in current buffer STRING comes from.
5248 AROUND_CHARPOS is an approximate position where it could come from.
5249 Value is the buffer position or 0 if it couldn't be determined.
5251 This function is necessary because we don't record buffer positions
5252 in glyphs generated from strings (to keep struct glyph small).
5253 This function may only use code that doesn't eval because it is
5254 called asynchronously from note_mouse_highlight. */
5256 static ptrdiff_t
5257 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5259 const int MAX_DISTANCE = 1000;
5260 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5261 around_charpos + MAX_DISTANCE,
5264 if (!found)
5265 found = string_buffer_position_lim (string, around_charpos,
5266 around_charpos - MAX_DISTANCE, 1);
5267 return found;
5272 /***********************************************************************
5273 `composition' property
5274 ***********************************************************************/
5276 /* Set up iterator IT from `composition' property at its current
5277 position. Called from handle_stop. */
5279 static enum prop_handled
5280 handle_composition_prop (struct it *it)
5282 Lisp_Object prop, string;
5283 ptrdiff_t pos, pos_byte, start, end;
5285 if (STRINGP (it->string))
5287 unsigned char *s;
5289 pos = IT_STRING_CHARPOS (*it);
5290 pos_byte = IT_STRING_BYTEPOS (*it);
5291 string = it->string;
5292 s = SDATA (string) + pos_byte;
5293 it->c = STRING_CHAR (s);
5295 else
5297 pos = IT_CHARPOS (*it);
5298 pos_byte = IT_BYTEPOS (*it);
5299 string = Qnil;
5300 it->c = FETCH_CHAR (pos_byte);
5303 /* If there's a valid composition and point is not inside of the
5304 composition (in the case that the composition is from the current
5305 buffer), draw a glyph composed from the composition components. */
5306 if (find_composition (pos, -1, &start, &end, &prop, string)
5307 && COMPOSITION_VALID_P (start, end, prop)
5308 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5310 if (start < pos)
5311 /* As we can't handle this situation (perhaps font-lock added
5312 a new composition), we just return here hoping that next
5313 redisplay will detect this composition much earlier. */
5314 return HANDLED_NORMALLY;
5315 if (start != pos)
5317 if (STRINGP (it->string))
5318 pos_byte = string_char_to_byte (it->string, start);
5319 else
5320 pos_byte = CHAR_TO_BYTE (start);
5322 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5323 prop, string);
5325 if (it->cmp_it.id >= 0)
5327 it->cmp_it.ch = -1;
5328 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5329 it->cmp_it.nglyphs = -1;
5333 return HANDLED_NORMALLY;
5338 /***********************************************************************
5339 Overlay strings
5340 ***********************************************************************/
5342 /* The following structure is used to record overlay strings for
5343 later sorting in load_overlay_strings. */
5345 struct overlay_entry
5347 Lisp_Object overlay;
5348 Lisp_Object string;
5349 EMACS_INT priority;
5350 int after_string_p;
5354 /* Set up iterator IT from overlay strings at its current position.
5355 Called from handle_stop. */
5357 static enum prop_handled
5358 handle_overlay_change (struct it *it)
5360 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5361 return HANDLED_RECOMPUTE_PROPS;
5362 else
5363 return HANDLED_NORMALLY;
5367 /* Set up the next overlay string for delivery by IT, if there is an
5368 overlay string to deliver. Called by set_iterator_to_next when the
5369 end of the current overlay string is reached. If there are more
5370 overlay strings to display, IT->string and
5371 IT->current.overlay_string_index are set appropriately here.
5372 Otherwise IT->string is set to nil. */
5374 static void
5375 next_overlay_string (struct it *it)
5377 ++it->current.overlay_string_index;
5378 if (it->current.overlay_string_index == it->n_overlay_strings)
5380 /* No more overlay strings. Restore IT's settings to what
5381 they were before overlay strings were processed, and
5382 continue to deliver from current_buffer. */
5384 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5385 pop_it (it);
5386 eassert (it->sp > 0
5387 || (NILP (it->string)
5388 && it->method == GET_FROM_BUFFER
5389 && it->stop_charpos >= BEGV
5390 && it->stop_charpos <= it->end_charpos));
5391 it->current.overlay_string_index = -1;
5392 it->n_overlay_strings = 0;
5393 it->overlay_strings_charpos = -1;
5394 /* If there's an empty display string on the stack, pop the
5395 stack, to resync the bidi iterator with IT's position. Such
5396 empty strings are pushed onto the stack in
5397 get_overlay_strings_1. */
5398 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5399 pop_it (it);
5401 /* If we're at the end of the buffer, record that we have
5402 processed the overlay strings there already, so that
5403 next_element_from_buffer doesn't try it again. */
5404 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5405 it->overlay_strings_at_end_processed_p = 1;
5407 else
5409 /* There are more overlay strings to process. If
5410 IT->current.overlay_string_index has advanced to a position
5411 where we must load IT->overlay_strings with more strings, do
5412 it. We must load at the IT->overlay_strings_charpos where
5413 IT->n_overlay_strings was originally computed; when invisible
5414 text is present, this might not be IT_CHARPOS (Bug#7016). */
5415 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5417 if (it->current.overlay_string_index && i == 0)
5418 load_overlay_strings (it, it->overlay_strings_charpos);
5420 /* Initialize IT to deliver display elements from the overlay
5421 string. */
5422 it->string = it->overlay_strings[i];
5423 it->multibyte_p = STRING_MULTIBYTE (it->string);
5424 SET_TEXT_POS (it->current.string_pos, 0, 0);
5425 it->method = GET_FROM_STRING;
5426 it->stop_charpos = 0;
5427 it->end_charpos = SCHARS (it->string);
5428 if (it->cmp_it.stop_pos >= 0)
5429 it->cmp_it.stop_pos = 0;
5430 it->prev_stop = 0;
5431 it->base_level_stop = 0;
5433 /* Set up the bidi iterator for this overlay string. */
5434 if (it->bidi_p)
5436 it->bidi_it.string.lstring = it->string;
5437 it->bidi_it.string.s = NULL;
5438 it->bidi_it.string.schars = SCHARS (it->string);
5439 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5440 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5441 it->bidi_it.string.unibyte = !it->multibyte_p;
5442 it->bidi_it.w = it->w;
5443 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5447 CHECK_IT (it);
5451 /* Compare two overlay_entry structures E1 and E2. Used as a
5452 comparison function for qsort in load_overlay_strings. Overlay
5453 strings for the same position are sorted so that
5455 1. All after-strings come in front of before-strings, except
5456 when they come from the same overlay.
5458 2. Within after-strings, strings are sorted so that overlay strings
5459 from overlays with higher priorities come first.
5461 2. Within before-strings, strings are sorted so that overlay
5462 strings from overlays with higher priorities come last.
5464 Value is analogous to strcmp. */
5467 static int
5468 compare_overlay_entries (const void *e1, const void *e2)
5470 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5471 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5472 int result;
5474 if (entry1->after_string_p != entry2->after_string_p)
5476 /* Let after-strings appear in front of before-strings if
5477 they come from different overlays. */
5478 if (EQ (entry1->overlay, entry2->overlay))
5479 result = entry1->after_string_p ? 1 : -1;
5480 else
5481 result = entry1->after_string_p ? -1 : 1;
5483 else if (entry1->priority != entry2->priority)
5485 if (entry1->after_string_p)
5486 /* After-strings sorted in order of decreasing priority. */
5487 result = entry2->priority < entry1->priority ? -1 : 1;
5488 else
5489 /* Before-strings sorted in order of increasing priority. */
5490 result = entry1->priority < entry2->priority ? -1 : 1;
5492 else
5493 result = 0;
5495 return result;
5499 /* Load the vector IT->overlay_strings with overlay strings from IT's
5500 current buffer position, or from CHARPOS if that is > 0. Set
5501 IT->n_overlays to the total number of overlay strings found.
5503 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5504 a time. On entry into load_overlay_strings,
5505 IT->current.overlay_string_index gives the number of overlay
5506 strings that have already been loaded by previous calls to this
5507 function.
5509 IT->add_overlay_start contains an additional overlay start
5510 position to consider for taking overlay strings from, if non-zero.
5511 This position comes into play when the overlay has an `invisible'
5512 property, and both before and after-strings. When we've skipped to
5513 the end of the overlay, because of its `invisible' property, we
5514 nevertheless want its before-string to appear.
5515 IT->add_overlay_start will contain the overlay start position
5516 in this case.
5518 Overlay strings are sorted so that after-string strings come in
5519 front of before-string strings. Within before and after-strings,
5520 strings are sorted by overlay priority. See also function
5521 compare_overlay_entries. */
5523 static void
5524 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5526 Lisp_Object overlay, window, str, invisible;
5527 struct Lisp_Overlay *ov;
5528 ptrdiff_t start, end;
5529 ptrdiff_t size = 20;
5530 ptrdiff_t n = 0, i, j;
5531 int invis_p;
5532 struct overlay_entry *entries = alloca (size * sizeof *entries);
5533 USE_SAFE_ALLOCA;
5535 if (charpos <= 0)
5536 charpos = IT_CHARPOS (*it);
5538 /* Append the overlay string STRING of overlay OVERLAY to vector
5539 `entries' which has size `size' and currently contains `n'
5540 elements. AFTER_P non-zero means STRING is an after-string of
5541 OVERLAY. */
5542 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5543 do \
5545 Lisp_Object priority; \
5547 if (n == size) \
5549 struct overlay_entry *old = entries; \
5550 SAFE_NALLOCA (entries, 2, size); \
5551 memcpy (entries, old, size * sizeof *entries); \
5552 size *= 2; \
5555 entries[n].string = (STRING); \
5556 entries[n].overlay = (OVERLAY); \
5557 priority = Foverlay_get ((OVERLAY), Qpriority); \
5558 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5559 entries[n].after_string_p = (AFTER_P); \
5560 ++n; \
5562 while (0)
5564 /* Process overlay before the overlay center. */
5565 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5567 XSETMISC (overlay, ov);
5568 eassert (OVERLAYP (overlay));
5569 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5570 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5572 if (end < charpos)
5573 break;
5575 /* Skip this overlay if it doesn't start or end at IT's current
5576 position. */
5577 if (end != charpos && start != charpos)
5578 continue;
5580 /* Skip this overlay if it doesn't apply to IT->w. */
5581 window = Foverlay_get (overlay, Qwindow);
5582 if (WINDOWP (window) && XWINDOW (window) != it->w)
5583 continue;
5585 /* If the text ``under'' the overlay is invisible, both before-
5586 and after-strings from this overlay are visible; start and
5587 end position are indistinguishable. */
5588 invisible = Foverlay_get (overlay, Qinvisible);
5589 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5591 /* If overlay has a non-empty before-string, record it. */
5592 if ((start == charpos || (end == charpos && invis_p))
5593 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5594 && SCHARS (str))
5595 RECORD_OVERLAY_STRING (overlay, str, 0);
5597 /* If overlay has a non-empty after-string, record it. */
5598 if ((end == charpos || (start == charpos && invis_p))
5599 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5600 && SCHARS (str))
5601 RECORD_OVERLAY_STRING (overlay, str, 1);
5604 /* Process overlays after the overlay center. */
5605 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5607 XSETMISC (overlay, ov);
5608 eassert (OVERLAYP (overlay));
5609 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5610 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5612 if (start > charpos)
5613 break;
5615 /* Skip this overlay if it doesn't start or end at IT's current
5616 position. */
5617 if (end != charpos && start != charpos)
5618 continue;
5620 /* Skip this overlay if it doesn't apply to IT->w. */
5621 window = Foverlay_get (overlay, Qwindow);
5622 if (WINDOWP (window) && XWINDOW (window) != it->w)
5623 continue;
5625 /* If the text ``under'' the overlay is invisible, it has a zero
5626 dimension, and both before- and after-strings apply. */
5627 invisible = Foverlay_get (overlay, Qinvisible);
5628 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5630 /* If overlay has a non-empty before-string, record it. */
5631 if ((start == charpos || (end == charpos && invis_p))
5632 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5633 && SCHARS (str))
5634 RECORD_OVERLAY_STRING (overlay, str, 0);
5636 /* If overlay has a non-empty after-string, record it. */
5637 if ((end == charpos || (start == charpos && invis_p))
5638 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5639 && SCHARS (str))
5640 RECORD_OVERLAY_STRING (overlay, str, 1);
5643 #undef RECORD_OVERLAY_STRING
5645 /* Sort entries. */
5646 if (n > 1)
5647 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5649 /* Record number of overlay strings, and where we computed it. */
5650 it->n_overlay_strings = n;
5651 it->overlay_strings_charpos = charpos;
5653 /* IT->current.overlay_string_index is the number of overlay strings
5654 that have already been consumed by IT. Copy some of the
5655 remaining overlay strings to IT->overlay_strings. */
5656 i = 0;
5657 j = it->current.overlay_string_index;
5658 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5660 it->overlay_strings[i] = entries[j].string;
5661 it->string_overlays[i++] = entries[j++].overlay;
5664 CHECK_IT (it);
5665 SAFE_FREE ();
5669 /* Get the first chunk of overlay strings at IT's current buffer
5670 position, or at CHARPOS if that is > 0. Value is non-zero if at
5671 least one overlay string was found. */
5673 static int
5674 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5676 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5677 process. This fills IT->overlay_strings with strings, and sets
5678 IT->n_overlay_strings to the total number of strings to process.
5679 IT->pos.overlay_string_index has to be set temporarily to zero
5680 because load_overlay_strings needs this; it must be set to -1
5681 when no overlay strings are found because a zero value would
5682 indicate a position in the first overlay string. */
5683 it->current.overlay_string_index = 0;
5684 load_overlay_strings (it, charpos);
5686 /* If we found overlay strings, set up IT to deliver display
5687 elements from the first one. Otherwise set up IT to deliver
5688 from current_buffer. */
5689 if (it->n_overlay_strings)
5691 /* Make sure we know settings in current_buffer, so that we can
5692 restore meaningful values when we're done with the overlay
5693 strings. */
5694 if (compute_stop_p)
5695 compute_stop_pos (it);
5696 eassert (it->face_id >= 0);
5698 /* Save IT's settings. They are restored after all overlay
5699 strings have been processed. */
5700 eassert (!compute_stop_p || it->sp == 0);
5702 /* When called from handle_stop, there might be an empty display
5703 string loaded. In that case, don't bother saving it. But
5704 don't use this optimization with the bidi iterator, since we
5705 need the corresponding pop_it call to resync the bidi
5706 iterator's position with IT's position, after we are done
5707 with the overlay strings. (The corresponding call to pop_it
5708 in case of an empty display string is in
5709 next_overlay_string.) */
5710 if (!(!it->bidi_p
5711 && STRINGP (it->string) && !SCHARS (it->string)))
5712 push_it (it, NULL);
5714 /* Set up IT to deliver display elements from the first overlay
5715 string. */
5716 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5717 it->string = it->overlay_strings[0];
5718 it->from_overlay = Qnil;
5719 it->stop_charpos = 0;
5720 eassert (STRINGP (it->string));
5721 it->end_charpos = SCHARS (it->string);
5722 it->prev_stop = 0;
5723 it->base_level_stop = 0;
5724 it->multibyte_p = STRING_MULTIBYTE (it->string);
5725 it->method = GET_FROM_STRING;
5726 it->from_disp_prop_p = 0;
5728 /* Force paragraph direction to be that of the parent
5729 buffer. */
5730 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5731 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5732 else
5733 it->paragraph_embedding = L2R;
5735 /* Set up the bidi iterator for this overlay string. */
5736 if (it->bidi_p)
5738 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5740 it->bidi_it.string.lstring = it->string;
5741 it->bidi_it.string.s = NULL;
5742 it->bidi_it.string.schars = SCHARS (it->string);
5743 it->bidi_it.string.bufpos = pos;
5744 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5745 it->bidi_it.string.unibyte = !it->multibyte_p;
5746 it->bidi_it.w = it->w;
5747 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5749 return 1;
5752 it->current.overlay_string_index = -1;
5753 return 0;
5756 static int
5757 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5759 it->string = Qnil;
5760 it->method = GET_FROM_BUFFER;
5762 (void) get_overlay_strings_1 (it, charpos, 1);
5764 CHECK_IT (it);
5766 /* Value is non-zero if we found at least one overlay string. */
5767 return STRINGP (it->string);
5772 /***********************************************************************
5773 Saving and restoring state
5774 ***********************************************************************/
5776 /* Save current settings of IT on IT->stack. Called, for example,
5777 before setting up IT for an overlay string, to be able to restore
5778 IT's settings to what they were after the overlay string has been
5779 processed. If POSITION is non-NULL, it is the position to save on
5780 the stack instead of IT->position. */
5782 static void
5783 push_it (struct it *it, struct text_pos *position)
5785 struct iterator_stack_entry *p;
5787 eassert (it->sp < IT_STACK_SIZE);
5788 p = it->stack + it->sp;
5790 p->stop_charpos = it->stop_charpos;
5791 p->prev_stop = it->prev_stop;
5792 p->base_level_stop = it->base_level_stop;
5793 p->cmp_it = it->cmp_it;
5794 eassert (it->face_id >= 0);
5795 p->face_id = it->face_id;
5796 p->string = it->string;
5797 p->method = it->method;
5798 p->from_overlay = it->from_overlay;
5799 switch (p->method)
5801 case GET_FROM_IMAGE:
5802 p->u.image.object = it->object;
5803 p->u.image.image_id = it->image_id;
5804 p->u.image.slice = it->slice;
5805 break;
5806 case GET_FROM_STRETCH:
5807 p->u.stretch.object = it->object;
5808 break;
5810 p->position = position ? *position : it->position;
5811 p->current = it->current;
5812 p->end_charpos = it->end_charpos;
5813 p->string_nchars = it->string_nchars;
5814 p->area = it->area;
5815 p->multibyte_p = it->multibyte_p;
5816 p->avoid_cursor_p = it->avoid_cursor_p;
5817 p->space_width = it->space_width;
5818 p->font_height = it->font_height;
5819 p->voffset = it->voffset;
5820 p->string_from_display_prop_p = it->string_from_display_prop_p;
5821 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5822 p->display_ellipsis_p = 0;
5823 p->line_wrap = it->line_wrap;
5824 p->bidi_p = it->bidi_p;
5825 p->paragraph_embedding = it->paragraph_embedding;
5826 p->from_disp_prop_p = it->from_disp_prop_p;
5827 ++it->sp;
5829 /* Save the state of the bidi iterator as well. */
5830 if (it->bidi_p)
5831 bidi_push_it (&it->bidi_it);
5834 static void
5835 iterate_out_of_display_property (struct it *it)
5837 int buffer_p = !STRINGP (it->string);
5838 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5839 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5841 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5843 /* Maybe initialize paragraph direction. If we are at the beginning
5844 of a new paragraph, next_element_from_buffer may not have a
5845 chance to do that. */
5846 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5847 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5848 /* prev_stop can be zero, so check against BEGV as well. */
5849 while (it->bidi_it.charpos >= bob
5850 && it->prev_stop <= it->bidi_it.charpos
5851 && it->bidi_it.charpos < CHARPOS (it->position)
5852 && it->bidi_it.charpos < eob)
5853 bidi_move_to_visually_next (&it->bidi_it);
5854 /* Record the stop_pos we just crossed, for when we cross it
5855 back, maybe. */
5856 if (it->bidi_it.charpos > CHARPOS (it->position))
5857 it->prev_stop = CHARPOS (it->position);
5858 /* If we ended up not where pop_it put us, resync IT's
5859 positional members with the bidi iterator. */
5860 if (it->bidi_it.charpos != CHARPOS (it->position))
5861 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5862 if (buffer_p)
5863 it->current.pos = it->position;
5864 else
5865 it->current.string_pos = it->position;
5868 /* Restore IT's settings from IT->stack. Called, for example, when no
5869 more overlay strings must be processed, and we return to delivering
5870 display elements from a buffer, or when the end of a string from a
5871 `display' property is reached and we return to delivering display
5872 elements from an overlay string, or from a buffer. */
5874 static void
5875 pop_it (struct it *it)
5877 struct iterator_stack_entry *p;
5878 int from_display_prop = it->from_disp_prop_p;
5880 eassert (it->sp > 0);
5881 --it->sp;
5882 p = it->stack + it->sp;
5883 it->stop_charpos = p->stop_charpos;
5884 it->prev_stop = p->prev_stop;
5885 it->base_level_stop = p->base_level_stop;
5886 it->cmp_it = p->cmp_it;
5887 it->face_id = p->face_id;
5888 it->current = p->current;
5889 it->position = p->position;
5890 it->string = p->string;
5891 it->from_overlay = p->from_overlay;
5892 if (NILP (it->string))
5893 SET_TEXT_POS (it->current.string_pos, -1, -1);
5894 it->method = p->method;
5895 switch (it->method)
5897 case GET_FROM_IMAGE:
5898 it->image_id = p->u.image.image_id;
5899 it->object = p->u.image.object;
5900 it->slice = p->u.image.slice;
5901 break;
5902 case GET_FROM_STRETCH:
5903 it->object = p->u.stretch.object;
5904 break;
5905 case GET_FROM_BUFFER:
5906 it->object = it->w->contents;
5907 break;
5908 case GET_FROM_STRING:
5909 it->object = it->string;
5910 break;
5911 case GET_FROM_DISPLAY_VECTOR:
5912 if (it->s)
5913 it->method = GET_FROM_C_STRING;
5914 else if (STRINGP (it->string))
5915 it->method = GET_FROM_STRING;
5916 else
5918 it->method = GET_FROM_BUFFER;
5919 it->object = it->w->contents;
5922 it->end_charpos = p->end_charpos;
5923 it->string_nchars = p->string_nchars;
5924 it->area = p->area;
5925 it->multibyte_p = p->multibyte_p;
5926 it->avoid_cursor_p = p->avoid_cursor_p;
5927 it->space_width = p->space_width;
5928 it->font_height = p->font_height;
5929 it->voffset = p->voffset;
5930 it->string_from_display_prop_p = p->string_from_display_prop_p;
5931 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5932 it->line_wrap = p->line_wrap;
5933 it->bidi_p = p->bidi_p;
5934 it->paragraph_embedding = p->paragraph_embedding;
5935 it->from_disp_prop_p = p->from_disp_prop_p;
5936 if (it->bidi_p)
5938 bidi_pop_it (&it->bidi_it);
5939 /* Bidi-iterate until we get out of the portion of text, if any,
5940 covered by a `display' text property or by an overlay with
5941 `display' property. (We cannot just jump there, because the
5942 internal coherency of the bidi iterator state can not be
5943 preserved across such jumps.) We also must determine the
5944 paragraph base direction if the overlay we just processed is
5945 at the beginning of a new paragraph. */
5946 if (from_display_prop
5947 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5948 iterate_out_of_display_property (it);
5950 eassert ((BUFFERP (it->object)
5951 && IT_CHARPOS (*it) == it->bidi_it.charpos
5952 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5953 || (STRINGP (it->object)
5954 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5955 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5956 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5962 /***********************************************************************
5963 Moving over lines
5964 ***********************************************************************/
5966 /* Set IT's current position to the previous line start. */
5968 static void
5969 back_to_previous_line_start (struct it *it)
5971 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
5973 DEC_BOTH (cp, bp);
5974 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
5978 /* Move IT to the next line start.
5980 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5981 we skipped over part of the text (as opposed to moving the iterator
5982 continuously over the text). Otherwise, don't change the value
5983 of *SKIPPED_P.
5985 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5986 iterator on the newline, if it was found.
5988 Newlines may come from buffer text, overlay strings, or strings
5989 displayed via the `display' property. That's the reason we can't
5990 simply use find_newline_no_quit.
5992 Note that this function may not skip over invisible text that is so
5993 because of text properties and immediately follows a newline. If
5994 it would, function reseat_at_next_visible_line_start, when called
5995 from set_iterator_to_next, would effectively make invisible
5996 characters following a newline part of the wrong glyph row, which
5997 leads to wrong cursor motion. */
5999 static int
6000 forward_to_next_line_start (struct it *it, int *skipped_p,
6001 struct bidi_it *bidi_it_prev)
6003 ptrdiff_t old_selective;
6004 int newline_found_p, n;
6005 const int MAX_NEWLINE_DISTANCE = 500;
6007 /* If already on a newline, just consume it to avoid unintended
6008 skipping over invisible text below. */
6009 if (it->what == IT_CHARACTER
6010 && it->c == '\n'
6011 && CHARPOS (it->position) == IT_CHARPOS (*it))
6013 if (it->bidi_p && bidi_it_prev)
6014 *bidi_it_prev = it->bidi_it;
6015 set_iterator_to_next (it, 0);
6016 it->c = 0;
6017 return 1;
6020 /* Don't handle selective display in the following. It's (a)
6021 unnecessary because it's done by the caller, and (b) leads to an
6022 infinite recursion because next_element_from_ellipsis indirectly
6023 calls this function. */
6024 old_selective = it->selective;
6025 it->selective = 0;
6027 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6028 from buffer text. */
6029 for (n = newline_found_p = 0;
6030 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6031 n += STRINGP (it->string) ? 0 : 1)
6033 if (!get_next_display_element (it))
6034 return 0;
6035 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6036 if (newline_found_p && it->bidi_p && bidi_it_prev)
6037 *bidi_it_prev = it->bidi_it;
6038 set_iterator_to_next (it, 0);
6041 /* If we didn't find a newline near enough, see if we can use a
6042 short-cut. */
6043 if (!newline_found_p)
6045 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6046 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6047 1, &bytepos);
6048 Lisp_Object pos;
6050 eassert (!STRINGP (it->string));
6052 /* If there isn't any `display' property in sight, and no
6053 overlays, we can just use the position of the newline in
6054 buffer text. */
6055 if (it->stop_charpos >= limit
6056 || ((pos = Fnext_single_property_change (make_number (start),
6057 Qdisplay, Qnil,
6058 make_number (limit)),
6059 NILP (pos))
6060 && next_overlay_change (start) == ZV))
6062 if (!it->bidi_p)
6064 IT_CHARPOS (*it) = limit;
6065 IT_BYTEPOS (*it) = bytepos;
6067 else
6069 struct bidi_it bprev;
6071 /* Help bidi.c avoid expensive searches for display
6072 properties and overlays, by telling it that there are
6073 none up to `limit'. */
6074 if (it->bidi_it.disp_pos < limit)
6076 it->bidi_it.disp_pos = limit;
6077 it->bidi_it.disp_prop = 0;
6079 do {
6080 bprev = it->bidi_it;
6081 bidi_move_to_visually_next (&it->bidi_it);
6082 } while (it->bidi_it.charpos != limit);
6083 IT_CHARPOS (*it) = limit;
6084 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6085 if (bidi_it_prev)
6086 *bidi_it_prev = bprev;
6088 *skipped_p = newline_found_p = 1;
6090 else
6092 while (get_next_display_element (it)
6093 && !newline_found_p)
6095 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6096 if (newline_found_p && it->bidi_p && bidi_it_prev)
6097 *bidi_it_prev = it->bidi_it;
6098 set_iterator_to_next (it, 0);
6103 it->selective = old_selective;
6104 return newline_found_p;
6108 /* Set IT's current position to the previous visible line start. Skip
6109 invisible text that is so either due to text properties or due to
6110 selective display. Caution: this does not change IT->current_x and
6111 IT->hpos. */
6113 static void
6114 back_to_previous_visible_line_start (struct it *it)
6116 while (IT_CHARPOS (*it) > BEGV)
6118 back_to_previous_line_start (it);
6120 if (IT_CHARPOS (*it) <= BEGV)
6121 break;
6123 /* If selective > 0, then lines indented more than its value are
6124 invisible. */
6125 if (it->selective > 0
6126 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6127 it->selective))
6128 continue;
6130 /* Check the newline before point for invisibility. */
6132 Lisp_Object prop;
6133 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6134 Qinvisible, it->window);
6135 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6136 continue;
6139 if (IT_CHARPOS (*it) <= BEGV)
6140 break;
6143 struct it it2;
6144 void *it2data = NULL;
6145 ptrdiff_t pos;
6146 ptrdiff_t beg, end;
6147 Lisp_Object val, overlay;
6149 SAVE_IT (it2, *it, it2data);
6151 /* If newline is part of a composition, continue from start of composition */
6152 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6153 && beg < IT_CHARPOS (*it))
6154 goto replaced;
6156 /* If newline is replaced by a display property, find start of overlay
6157 or interval and continue search from that point. */
6158 pos = --IT_CHARPOS (it2);
6159 --IT_BYTEPOS (it2);
6160 it2.sp = 0;
6161 bidi_unshelve_cache (NULL, 0);
6162 it2.string_from_display_prop_p = 0;
6163 it2.from_disp_prop_p = 0;
6164 if (handle_display_prop (&it2) == HANDLED_RETURN
6165 && !NILP (val = get_char_property_and_overlay
6166 (make_number (pos), Qdisplay, Qnil, &overlay))
6167 && (OVERLAYP (overlay)
6168 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6169 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6171 RESTORE_IT (it, it, it2data);
6172 goto replaced;
6175 /* Newline is not replaced by anything -- so we are done. */
6176 RESTORE_IT (it, it, it2data);
6177 break;
6179 replaced:
6180 if (beg < BEGV)
6181 beg = BEGV;
6182 IT_CHARPOS (*it) = beg;
6183 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6187 it->continuation_lines_width = 0;
6189 eassert (IT_CHARPOS (*it) >= BEGV);
6190 eassert (IT_CHARPOS (*it) == BEGV
6191 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6192 CHECK_IT (it);
6196 /* Reseat iterator IT at the previous visible line start. Skip
6197 invisible text that is so either due to text properties or due to
6198 selective display. At the end, update IT's overlay information,
6199 face information etc. */
6201 void
6202 reseat_at_previous_visible_line_start (struct it *it)
6204 back_to_previous_visible_line_start (it);
6205 reseat (it, it->current.pos, 1);
6206 CHECK_IT (it);
6210 /* Reseat iterator IT on the next visible line start in the current
6211 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6212 preceding the line start. Skip over invisible text that is so
6213 because of selective display. Compute faces, overlays etc at the
6214 new position. Note that this function does not skip over text that
6215 is invisible because of text properties. */
6217 static void
6218 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6220 int newline_found_p, skipped_p = 0;
6221 struct bidi_it bidi_it_prev;
6223 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6225 /* Skip over lines that are invisible because they are indented
6226 more than the value of IT->selective. */
6227 if (it->selective > 0)
6228 while (IT_CHARPOS (*it) < ZV
6229 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6230 it->selective))
6232 eassert (IT_BYTEPOS (*it) == BEGV
6233 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6234 newline_found_p =
6235 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6238 /* Position on the newline if that's what's requested. */
6239 if (on_newline_p && newline_found_p)
6241 if (STRINGP (it->string))
6243 if (IT_STRING_CHARPOS (*it) > 0)
6245 if (!it->bidi_p)
6247 --IT_STRING_CHARPOS (*it);
6248 --IT_STRING_BYTEPOS (*it);
6250 else
6252 /* We need to restore the bidi iterator to the state
6253 it had on the newline, and resync the IT's
6254 position with that. */
6255 it->bidi_it = bidi_it_prev;
6256 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6257 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6261 else if (IT_CHARPOS (*it) > BEGV)
6263 if (!it->bidi_p)
6265 --IT_CHARPOS (*it);
6266 --IT_BYTEPOS (*it);
6268 else
6270 /* We need to restore the bidi iterator to the state it
6271 had on the newline and resync IT with that. */
6272 it->bidi_it = bidi_it_prev;
6273 IT_CHARPOS (*it) = it->bidi_it.charpos;
6274 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6276 reseat (it, it->current.pos, 0);
6279 else if (skipped_p)
6280 reseat (it, it->current.pos, 0);
6282 CHECK_IT (it);
6287 /***********************************************************************
6288 Changing an iterator's position
6289 ***********************************************************************/
6291 /* Change IT's current position to POS in current_buffer. If FORCE_P
6292 is non-zero, always check for text properties at the new position.
6293 Otherwise, text properties are only looked up if POS >=
6294 IT->check_charpos of a property. */
6296 static void
6297 reseat (struct it *it, struct text_pos pos, int force_p)
6299 ptrdiff_t original_pos = IT_CHARPOS (*it);
6301 reseat_1 (it, pos, 0);
6303 /* Determine where to check text properties. Avoid doing it
6304 where possible because text property lookup is very expensive. */
6305 if (force_p
6306 || CHARPOS (pos) > it->stop_charpos
6307 || CHARPOS (pos) < original_pos)
6309 if (it->bidi_p)
6311 /* For bidi iteration, we need to prime prev_stop and
6312 base_level_stop with our best estimations. */
6313 /* Implementation note: Of course, POS is not necessarily a
6314 stop position, so assigning prev_pos to it is a lie; we
6315 should have called compute_stop_backwards. However, if
6316 the current buffer does not include any R2L characters,
6317 that call would be a waste of cycles, because the
6318 iterator will never move back, and thus never cross this
6319 "fake" stop position. So we delay that backward search
6320 until the time we really need it, in next_element_from_buffer. */
6321 if (CHARPOS (pos) != it->prev_stop)
6322 it->prev_stop = CHARPOS (pos);
6323 if (CHARPOS (pos) < it->base_level_stop)
6324 it->base_level_stop = 0; /* meaning it's unknown */
6325 handle_stop (it);
6327 else
6329 handle_stop (it);
6330 it->prev_stop = it->base_level_stop = 0;
6335 CHECK_IT (it);
6339 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6340 IT->stop_pos to POS, also. */
6342 static void
6343 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6345 /* Don't call this function when scanning a C string. */
6346 eassert (it->s == NULL);
6348 /* POS must be a reasonable value. */
6349 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6351 it->current.pos = it->position = pos;
6352 it->end_charpos = ZV;
6353 it->dpvec = NULL;
6354 it->current.dpvec_index = -1;
6355 it->current.overlay_string_index = -1;
6356 IT_STRING_CHARPOS (*it) = -1;
6357 IT_STRING_BYTEPOS (*it) = -1;
6358 it->string = Qnil;
6359 it->method = GET_FROM_BUFFER;
6360 it->object = it->w->contents;
6361 it->area = TEXT_AREA;
6362 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6363 it->sp = 0;
6364 it->string_from_display_prop_p = 0;
6365 it->string_from_prefix_prop_p = 0;
6367 it->from_disp_prop_p = 0;
6368 it->face_before_selective_p = 0;
6369 if (it->bidi_p)
6371 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6372 &it->bidi_it);
6373 bidi_unshelve_cache (NULL, 0);
6374 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6375 it->bidi_it.string.s = NULL;
6376 it->bidi_it.string.lstring = Qnil;
6377 it->bidi_it.string.bufpos = 0;
6378 it->bidi_it.string.unibyte = 0;
6379 it->bidi_it.w = it->w;
6382 if (set_stop_p)
6384 it->stop_charpos = CHARPOS (pos);
6385 it->base_level_stop = CHARPOS (pos);
6387 /* This make the information stored in it->cmp_it invalidate. */
6388 it->cmp_it.id = -1;
6392 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6393 If S is non-null, it is a C string to iterate over. Otherwise,
6394 STRING gives a Lisp string to iterate over.
6396 If PRECISION > 0, don't return more then PRECISION number of
6397 characters from the string.
6399 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6400 characters have been returned. FIELD_WIDTH < 0 means an infinite
6401 field width.
6403 MULTIBYTE = 0 means disable processing of multibyte characters,
6404 MULTIBYTE > 0 means enable it,
6405 MULTIBYTE < 0 means use IT->multibyte_p.
6407 IT must be initialized via a prior call to init_iterator before
6408 calling this function. */
6410 static void
6411 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6412 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6413 int multibyte)
6415 /* No region in strings. */
6416 it->region_beg_charpos = it->region_end_charpos = -1;
6418 /* No text property checks performed by default, but see below. */
6419 it->stop_charpos = -1;
6421 /* Set iterator position and end position. */
6422 memset (&it->current, 0, sizeof it->current);
6423 it->current.overlay_string_index = -1;
6424 it->current.dpvec_index = -1;
6425 eassert (charpos >= 0);
6427 /* If STRING is specified, use its multibyteness, otherwise use the
6428 setting of MULTIBYTE, if specified. */
6429 if (multibyte >= 0)
6430 it->multibyte_p = multibyte > 0;
6432 /* Bidirectional reordering of strings is controlled by the default
6433 value of bidi-display-reordering. Don't try to reorder while
6434 loading loadup.el, as the necessary character property tables are
6435 not yet available. */
6436 it->bidi_p =
6437 NILP (Vpurify_flag)
6438 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6440 if (s == NULL)
6442 eassert (STRINGP (string));
6443 it->string = string;
6444 it->s = NULL;
6445 it->end_charpos = it->string_nchars = SCHARS (string);
6446 it->method = GET_FROM_STRING;
6447 it->current.string_pos = string_pos (charpos, string);
6449 if (it->bidi_p)
6451 it->bidi_it.string.lstring = string;
6452 it->bidi_it.string.s = NULL;
6453 it->bidi_it.string.schars = it->end_charpos;
6454 it->bidi_it.string.bufpos = 0;
6455 it->bidi_it.string.from_disp_str = 0;
6456 it->bidi_it.string.unibyte = !it->multibyte_p;
6457 it->bidi_it.w = it->w;
6458 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6459 FRAME_WINDOW_P (it->f), &it->bidi_it);
6462 else
6464 it->s = (const unsigned char *) s;
6465 it->string = Qnil;
6467 /* Note that we use IT->current.pos, not it->current.string_pos,
6468 for displaying C strings. */
6469 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6470 if (it->multibyte_p)
6472 it->current.pos = c_string_pos (charpos, s, 1);
6473 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6475 else
6477 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6478 it->end_charpos = it->string_nchars = strlen (s);
6481 if (it->bidi_p)
6483 it->bidi_it.string.lstring = Qnil;
6484 it->bidi_it.string.s = (const unsigned char *) s;
6485 it->bidi_it.string.schars = it->end_charpos;
6486 it->bidi_it.string.bufpos = 0;
6487 it->bidi_it.string.from_disp_str = 0;
6488 it->bidi_it.string.unibyte = !it->multibyte_p;
6489 it->bidi_it.w = it->w;
6490 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6491 &it->bidi_it);
6493 it->method = GET_FROM_C_STRING;
6496 /* PRECISION > 0 means don't return more than PRECISION characters
6497 from the string. */
6498 if (precision > 0 && it->end_charpos - charpos > precision)
6500 it->end_charpos = it->string_nchars = charpos + precision;
6501 if (it->bidi_p)
6502 it->bidi_it.string.schars = it->end_charpos;
6505 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6506 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6507 FIELD_WIDTH < 0 means infinite field width. This is useful for
6508 padding with `-' at the end of a mode line. */
6509 if (field_width < 0)
6510 field_width = INFINITY;
6511 /* Implementation note: We deliberately don't enlarge
6512 it->bidi_it.string.schars here to fit it->end_charpos, because
6513 the bidi iterator cannot produce characters out of thin air. */
6514 if (field_width > it->end_charpos - charpos)
6515 it->end_charpos = charpos + field_width;
6517 /* Use the standard display table for displaying strings. */
6518 if (DISP_TABLE_P (Vstandard_display_table))
6519 it->dp = XCHAR_TABLE (Vstandard_display_table);
6521 it->stop_charpos = charpos;
6522 it->prev_stop = charpos;
6523 it->base_level_stop = 0;
6524 if (it->bidi_p)
6526 it->bidi_it.first_elt = 1;
6527 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6528 it->bidi_it.disp_pos = -1;
6530 if (s == NULL && it->multibyte_p)
6532 ptrdiff_t endpos = SCHARS (it->string);
6533 if (endpos > it->end_charpos)
6534 endpos = it->end_charpos;
6535 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6536 it->string);
6538 CHECK_IT (it);
6543 /***********************************************************************
6544 Iteration
6545 ***********************************************************************/
6547 /* Map enum it_method value to corresponding next_element_from_* function. */
6549 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6551 next_element_from_buffer,
6552 next_element_from_display_vector,
6553 next_element_from_string,
6554 next_element_from_c_string,
6555 next_element_from_image,
6556 next_element_from_stretch
6559 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6562 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6563 (possibly with the following characters). */
6565 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6566 ((IT)->cmp_it.id >= 0 \
6567 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6568 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6569 END_CHARPOS, (IT)->w, \
6570 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6571 (IT)->string)))
6574 /* Lookup the char-table Vglyphless_char_display for character C (-1
6575 if we want information for no-font case), and return the display
6576 method symbol. By side-effect, update it->what and
6577 it->glyphless_method. This function is called from
6578 get_next_display_element for each character element, and from
6579 x_produce_glyphs when no suitable font was found. */
6581 Lisp_Object
6582 lookup_glyphless_char_display (int c, struct it *it)
6584 Lisp_Object glyphless_method = Qnil;
6586 if (CHAR_TABLE_P (Vglyphless_char_display)
6587 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6589 if (c >= 0)
6591 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6592 if (CONSP (glyphless_method))
6593 glyphless_method = FRAME_WINDOW_P (it->f)
6594 ? XCAR (glyphless_method)
6595 : XCDR (glyphless_method);
6597 else
6598 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6601 retry:
6602 if (NILP (glyphless_method))
6604 if (c >= 0)
6605 /* The default is to display the character by a proper font. */
6606 return Qnil;
6607 /* The default for the no-font case is to display an empty box. */
6608 glyphless_method = Qempty_box;
6610 if (EQ (glyphless_method, Qzero_width))
6612 if (c >= 0)
6613 return glyphless_method;
6614 /* This method can't be used for the no-font case. */
6615 glyphless_method = Qempty_box;
6617 if (EQ (glyphless_method, Qthin_space))
6618 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6619 else if (EQ (glyphless_method, Qempty_box))
6620 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6621 else if (EQ (glyphless_method, Qhex_code))
6622 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6623 else if (STRINGP (glyphless_method))
6624 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6625 else
6627 /* Invalid value. We use the default method. */
6628 glyphless_method = Qnil;
6629 goto retry;
6631 it->what = IT_GLYPHLESS;
6632 return glyphless_method;
6635 /* Load IT's display element fields with information about the next
6636 display element from the current position of IT. Value is zero if
6637 end of buffer (or C string) is reached. */
6639 static struct frame *last_escape_glyph_frame = NULL;
6640 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6641 static int last_escape_glyph_merged_face_id = 0;
6643 struct frame *last_glyphless_glyph_frame = NULL;
6644 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6645 int last_glyphless_glyph_merged_face_id = 0;
6647 static int
6648 get_next_display_element (struct it *it)
6650 /* Non-zero means that we found a display element. Zero means that
6651 we hit the end of what we iterate over. Performance note: the
6652 function pointer `method' used here turns out to be faster than
6653 using a sequence of if-statements. */
6654 int success_p;
6656 get_next:
6657 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6659 if (it->what == IT_CHARACTER)
6661 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6662 and only if (a) the resolved directionality of that character
6663 is R..." */
6664 /* FIXME: Do we need an exception for characters from display
6665 tables? */
6666 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6667 it->c = bidi_mirror_char (it->c);
6668 /* Map via display table or translate control characters.
6669 IT->c, IT->len etc. have been set to the next character by
6670 the function call above. If we have a display table, and it
6671 contains an entry for IT->c, translate it. Don't do this if
6672 IT->c itself comes from a display table, otherwise we could
6673 end up in an infinite recursion. (An alternative could be to
6674 count the recursion depth of this function and signal an
6675 error when a certain maximum depth is reached.) Is it worth
6676 it? */
6677 if (success_p && it->dpvec == NULL)
6679 Lisp_Object dv;
6680 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6681 int nonascii_space_p = 0;
6682 int nonascii_hyphen_p = 0;
6683 int c = it->c; /* This is the character to display. */
6685 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6687 eassert (SINGLE_BYTE_CHAR_P (c));
6688 if (unibyte_display_via_language_environment)
6690 c = DECODE_CHAR (unibyte, c);
6691 if (c < 0)
6692 c = BYTE8_TO_CHAR (it->c);
6694 else
6695 c = BYTE8_TO_CHAR (it->c);
6698 if (it->dp
6699 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6700 VECTORP (dv)))
6702 struct Lisp_Vector *v = XVECTOR (dv);
6704 /* Return the first character from the display table
6705 entry, if not empty. If empty, don't display the
6706 current character. */
6707 if (v->header.size)
6709 it->dpvec_char_len = it->len;
6710 it->dpvec = v->contents;
6711 it->dpend = v->contents + v->header.size;
6712 it->current.dpvec_index = 0;
6713 it->dpvec_face_id = -1;
6714 it->saved_face_id = it->face_id;
6715 it->method = GET_FROM_DISPLAY_VECTOR;
6716 it->ellipsis_p = 0;
6718 else
6720 set_iterator_to_next (it, 0);
6722 goto get_next;
6725 if (! NILP (lookup_glyphless_char_display (c, it)))
6727 if (it->what == IT_GLYPHLESS)
6728 goto done;
6729 /* Don't display this character. */
6730 set_iterator_to_next (it, 0);
6731 goto get_next;
6734 /* If `nobreak-char-display' is non-nil, we display
6735 non-ASCII spaces and hyphens specially. */
6736 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6738 if (c == 0xA0)
6739 nonascii_space_p = 1;
6740 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6741 nonascii_hyphen_p = 1;
6744 /* Translate control characters into `\003' or `^C' form.
6745 Control characters coming from a display table entry are
6746 currently not translated because we use IT->dpvec to hold
6747 the translation. This could easily be changed but I
6748 don't believe that it is worth doing.
6750 The characters handled by `nobreak-char-display' must be
6751 translated too.
6753 Non-printable characters and raw-byte characters are also
6754 translated to octal form. */
6755 if (((c < ' ' || c == 127) /* ASCII control chars */
6756 ? (it->area != TEXT_AREA
6757 /* In mode line, treat \n, \t like other crl chars. */
6758 || (c != '\t'
6759 && it->glyph_row
6760 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6761 || (c != '\n' && c != '\t'))
6762 : (nonascii_space_p
6763 || nonascii_hyphen_p
6764 || CHAR_BYTE8_P (c)
6765 || ! CHAR_PRINTABLE_P (c))))
6767 /* C is a control character, non-ASCII space/hyphen,
6768 raw-byte, or a non-printable character which must be
6769 displayed either as '\003' or as `^C' where the '\\'
6770 and '^' can be defined in the display table. Fill
6771 IT->ctl_chars with glyphs for what we have to
6772 display. Then, set IT->dpvec to these glyphs. */
6773 Lisp_Object gc;
6774 int ctl_len;
6775 int face_id;
6776 int lface_id = 0;
6777 int escape_glyph;
6779 /* Handle control characters with ^. */
6781 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6783 int g;
6785 g = '^'; /* default glyph for Control */
6786 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6787 if (it->dp
6788 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6790 g = GLYPH_CODE_CHAR (gc);
6791 lface_id = GLYPH_CODE_FACE (gc);
6793 if (lface_id)
6795 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6797 else if (it->f == last_escape_glyph_frame
6798 && it->face_id == last_escape_glyph_face_id)
6800 face_id = last_escape_glyph_merged_face_id;
6802 else
6804 /* Merge the escape-glyph face into the current face. */
6805 face_id = merge_faces (it->f, Qescape_glyph, 0,
6806 it->face_id);
6807 last_escape_glyph_frame = it->f;
6808 last_escape_glyph_face_id = it->face_id;
6809 last_escape_glyph_merged_face_id = face_id;
6812 XSETINT (it->ctl_chars[0], g);
6813 XSETINT (it->ctl_chars[1], c ^ 0100);
6814 ctl_len = 2;
6815 goto display_control;
6818 /* Handle non-ascii space in the mode where it only gets
6819 highlighting. */
6821 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6823 /* Merge `nobreak-space' into the current face. */
6824 face_id = merge_faces (it->f, Qnobreak_space, 0,
6825 it->face_id);
6826 XSETINT (it->ctl_chars[0], ' ');
6827 ctl_len = 1;
6828 goto display_control;
6831 /* Handle sequences that start with the "escape glyph". */
6833 /* the default escape glyph is \. */
6834 escape_glyph = '\\';
6836 if (it->dp
6837 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6839 escape_glyph = GLYPH_CODE_CHAR (gc);
6840 lface_id = GLYPH_CODE_FACE (gc);
6842 if (lface_id)
6844 /* The display table specified a face.
6845 Merge it into face_id and also into escape_glyph. */
6846 face_id = merge_faces (it->f, Qt, lface_id,
6847 it->face_id);
6849 else if (it->f == last_escape_glyph_frame
6850 && it->face_id == last_escape_glyph_face_id)
6852 face_id = last_escape_glyph_merged_face_id;
6854 else
6856 /* Merge the escape-glyph face into the current face. */
6857 face_id = merge_faces (it->f, Qescape_glyph, 0,
6858 it->face_id);
6859 last_escape_glyph_frame = it->f;
6860 last_escape_glyph_face_id = it->face_id;
6861 last_escape_glyph_merged_face_id = face_id;
6864 /* Draw non-ASCII hyphen with just highlighting: */
6866 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6868 XSETINT (it->ctl_chars[0], '-');
6869 ctl_len = 1;
6870 goto display_control;
6873 /* Draw non-ASCII space/hyphen with escape glyph: */
6875 if (nonascii_space_p || nonascii_hyphen_p)
6877 XSETINT (it->ctl_chars[0], escape_glyph);
6878 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6879 ctl_len = 2;
6880 goto display_control;
6884 char str[10];
6885 int len, i;
6887 if (CHAR_BYTE8_P (c))
6888 /* Display \200 instead of \17777600. */
6889 c = CHAR_TO_BYTE8 (c);
6890 len = sprintf (str, "%03o", c);
6892 XSETINT (it->ctl_chars[0], escape_glyph);
6893 for (i = 0; i < len; i++)
6894 XSETINT (it->ctl_chars[i + 1], str[i]);
6895 ctl_len = len + 1;
6898 display_control:
6899 /* Set up IT->dpvec and return first character from it. */
6900 it->dpvec_char_len = it->len;
6901 it->dpvec = it->ctl_chars;
6902 it->dpend = it->dpvec + ctl_len;
6903 it->current.dpvec_index = 0;
6904 it->dpvec_face_id = face_id;
6905 it->saved_face_id = it->face_id;
6906 it->method = GET_FROM_DISPLAY_VECTOR;
6907 it->ellipsis_p = 0;
6908 goto get_next;
6910 it->char_to_display = c;
6912 else if (success_p)
6914 it->char_to_display = it->c;
6918 /* Adjust face id for a multibyte character. There are no multibyte
6919 character in unibyte text. */
6920 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6921 && it->multibyte_p
6922 && success_p
6923 && FRAME_WINDOW_P (it->f))
6925 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6927 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6929 /* Automatic composition with glyph-string. */
6930 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6932 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6934 else
6936 ptrdiff_t pos = (it->s ? -1
6937 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6938 : IT_CHARPOS (*it));
6939 int c;
6941 if (it->what == IT_CHARACTER)
6942 c = it->char_to_display;
6943 else
6945 struct composition *cmp = composition_table[it->cmp_it.id];
6946 int i;
6948 c = ' ';
6949 for (i = 0; i < cmp->glyph_len; i++)
6950 /* TAB in a composition means display glyphs with
6951 padding space on the left or right. */
6952 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6953 break;
6955 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6959 done:
6960 /* Is this character the last one of a run of characters with
6961 box? If yes, set IT->end_of_box_run_p to 1. */
6962 if (it->face_box_p
6963 && it->s == NULL)
6965 if (it->method == GET_FROM_STRING && it->sp)
6967 int face_id = underlying_face_id (it);
6968 struct face *face = FACE_FROM_ID (it->f, face_id);
6970 if (face)
6972 if (face->box == FACE_NO_BOX)
6974 /* If the box comes from face properties in a
6975 display string, check faces in that string. */
6976 int string_face_id = face_after_it_pos (it);
6977 it->end_of_box_run_p
6978 = (FACE_FROM_ID (it->f, string_face_id)->box
6979 == FACE_NO_BOX);
6981 /* Otherwise, the box comes from the underlying face.
6982 If this is the last string character displayed, check
6983 the next buffer location. */
6984 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6985 && (it->current.overlay_string_index
6986 == it->n_overlay_strings - 1))
6988 ptrdiff_t ignore;
6989 int next_face_id;
6990 struct text_pos pos = it->current.pos;
6991 INC_TEXT_POS (pos, it->multibyte_p);
6993 next_face_id = face_at_buffer_position
6994 (it->w, CHARPOS (pos), it->region_beg_charpos,
6995 it->region_end_charpos, &ignore,
6996 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6997 -1);
6998 it->end_of_box_run_p
6999 = (FACE_FROM_ID (it->f, next_face_id)->box
7000 == FACE_NO_BOX);
7004 else
7006 int face_id = face_after_it_pos (it);
7007 it->end_of_box_run_p
7008 = (face_id != it->face_id
7009 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7012 /* If we reached the end of the object we've been iterating (e.g., a
7013 display string or an overlay string), and there's something on
7014 IT->stack, proceed with what's on the stack. It doesn't make
7015 sense to return zero if there's unprocessed stuff on the stack,
7016 because otherwise that stuff will never be displayed. */
7017 if (!success_p && it->sp > 0)
7019 set_iterator_to_next (it, 0);
7020 success_p = get_next_display_element (it);
7023 /* Value is 0 if end of buffer or string reached. */
7024 return success_p;
7028 /* Move IT to the next display element.
7030 RESEAT_P non-zero means if called on a newline in buffer text,
7031 skip to the next visible line start.
7033 Functions get_next_display_element and set_iterator_to_next are
7034 separate because I find this arrangement easier to handle than a
7035 get_next_display_element function that also increments IT's
7036 position. The way it is we can first look at an iterator's current
7037 display element, decide whether it fits on a line, and if it does,
7038 increment the iterator position. The other way around we probably
7039 would either need a flag indicating whether the iterator has to be
7040 incremented the next time, or we would have to implement a
7041 decrement position function which would not be easy to write. */
7043 void
7044 set_iterator_to_next (struct it *it, int reseat_p)
7046 /* Reset flags indicating start and end of a sequence of characters
7047 with box. Reset them at the start of this function because
7048 moving the iterator to a new position might set them. */
7049 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7051 switch (it->method)
7053 case GET_FROM_BUFFER:
7054 /* The current display element of IT is a character from
7055 current_buffer. Advance in the buffer, and maybe skip over
7056 invisible lines that are so because of selective display. */
7057 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7058 reseat_at_next_visible_line_start (it, 0);
7059 else if (it->cmp_it.id >= 0)
7061 /* We are currently getting glyphs from a composition. */
7062 int i;
7064 if (! it->bidi_p)
7066 IT_CHARPOS (*it) += it->cmp_it.nchars;
7067 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7068 if (it->cmp_it.to < it->cmp_it.nglyphs)
7070 it->cmp_it.from = it->cmp_it.to;
7072 else
7074 it->cmp_it.id = -1;
7075 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7076 IT_BYTEPOS (*it),
7077 it->end_charpos, Qnil);
7080 else if (! it->cmp_it.reversed_p)
7082 /* Composition created while scanning forward. */
7083 /* Update IT's char/byte positions to point to the first
7084 character of the next grapheme cluster, or to the
7085 character visually after the current composition. */
7086 for (i = 0; i < it->cmp_it.nchars; i++)
7087 bidi_move_to_visually_next (&it->bidi_it);
7088 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7089 IT_CHARPOS (*it) = it->bidi_it.charpos;
7091 if (it->cmp_it.to < it->cmp_it.nglyphs)
7093 /* Proceed to the next grapheme cluster. */
7094 it->cmp_it.from = it->cmp_it.to;
7096 else
7098 /* No more grapheme clusters in this composition.
7099 Find the next stop position. */
7100 ptrdiff_t stop = it->end_charpos;
7101 if (it->bidi_it.scan_dir < 0)
7102 /* Now we are scanning backward and don't know
7103 where to stop. */
7104 stop = -1;
7105 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7106 IT_BYTEPOS (*it), stop, Qnil);
7109 else
7111 /* Composition created while scanning backward. */
7112 /* Update IT's char/byte positions to point to the last
7113 character of the previous grapheme cluster, or the
7114 character visually after the current composition. */
7115 for (i = 0; i < it->cmp_it.nchars; i++)
7116 bidi_move_to_visually_next (&it->bidi_it);
7117 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7118 IT_CHARPOS (*it) = it->bidi_it.charpos;
7119 if (it->cmp_it.from > 0)
7121 /* Proceed to the previous grapheme cluster. */
7122 it->cmp_it.to = it->cmp_it.from;
7124 else
7126 /* No more grapheme clusters in this composition.
7127 Find the next stop position. */
7128 ptrdiff_t stop = it->end_charpos;
7129 if (it->bidi_it.scan_dir < 0)
7130 /* Now we are scanning backward and don't know
7131 where to stop. */
7132 stop = -1;
7133 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7134 IT_BYTEPOS (*it), stop, Qnil);
7138 else
7140 eassert (it->len != 0);
7142 if (!it->bidi_p)
7144 IT_BYTEPOS (*it) += it->len;
7145 IT_CHARPOS (*it) += 1;
7147 else
7149 int prev_scan_dir = it->bidi_it.scan_dir;
7150 /* If this is a new paragraph, determine its base
7151 direction (a.k.a. its base embedding level). */
7152 if (it->bidi_it.new_paragraph)
7153 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7154 bidi_move_to_visually_next (&it->bidi_it);
7155 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7156 IT_CHARPOS (*it) = it->bidi_it.charpos;
7157 if (prev_scan_dir != it->bidi_it.scan_dir)
7159 /* As the scan direction was changed, we must
7160 re-compute the stop position for composition. */
7161 ptrdiff_t stop = it->end_charpos;
7162 if (it->bidi_it.scan_dir < 0)
7163 stop = -1;
7164 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7165 IT_BYTEPOS (*it), stop, Qnil);
7168 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7170 break;
7172 case GET_FROM_C_STRING:
7173 /* Current display element of IT is from a C string. */
7174 if (!it->bidi_p
7175 /* If the string position is beyond string's end, it means
7176 next_element_from_c_string is padding the string with
7177 blanks, in which case we bypass the bidi iterator,
7178 because it cannot deal with such virtual characters. */
7179 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7181 IT_BYTEPOS (*it) += it->len;
7182 IT_CHARPOS (*it) += 1;
7184 else
7186 bidi_move_to_visually_next (&it->bidi_it);
7187 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7188 IT_CHARPOS (*it) = it->bidi_it.charpos;
7190 break;
7192 case GET_FROM_DISPLAY_VECTOR:
7193 /* Current display element of IT is from a display table entry.
7194 Advance in the display table definition. Reset it to null if
7195 end reached, and continue with characters from buffers/
7196 strings. */
7197 ++it->current.dpvec_index;
7199 /* Restore face of the iterator to what they were before the
7200 display vector entry (these entries may contain faces). */
7201 it->face_id = it->saved_face_id;
7203 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7205 int recheck_faces = it->ellipsis_p;
7207 if (it->s)
7208 it->method = GET_FROM_C_STRING;
7209 else if (STRINGP (it->string))
7210 it->method = GET_FROM_STRING;
7211 else
7213 it->method = GET_FROM_BUFFER;
7214 it->object = it->w->contents;
7217 it->dpvec = NULL;
7218 it->current.dpvec_index = -1;
7220 /* Skip over characters which were displayed via IT->dpvec. */
7221 if (it->dpvec_char_len < 0)
7222 reseat_at_next_visible_line_start (it, 1);
7223 else if (it->dpvec_char_len > 0)
7225 if (it->method == GET_FROM_STRING
7226 && it->current.overlay_string_index >= 0
7227 && it->n_overlay_strings > 0)
7228 it->ignore_overlay_strings_at_pos_p = 1;
7229 it->len = it->dpvec_char_len;
7230 set_iterator_to_next (it, reseat_p);
7233 /* Maybe recheck faces after display vector */
7234 if (recheck_faces)
7235 it->stop_charpos = IT_CHARPOS (*it);
7237 break;
7239 case GET_FROM_STRING:
7240 /* Current display element is a character from a Lisp string. */
7241 eassert (it->s == NULL && STRINGP (it->string));
7242 /* Don't advance past string end. These conditions are true
7243 when set_iterator_to_next is called at the end of
7244 get_next_display_element, in which case the Lisp string is
7245 already exhausted, and all we want is pop the iterator
7246 stack. */
7247 if (it->current.overlay_string_index >= 0)
7249 /* This is an overlay string, so there's no padding with
7250 spaces, and the number of characters in the string is
7251 where the string ends. */
7252 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7253 goto consider_string_end;
7255 else
7257 /* Not an overlay string. There could be padding, so test
7258 against it->end_charpos . */
7259 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7260 goto consider_string_end;
7262 if (it->cmp_it.id >= 0)
7264 int i;
7266 if (! it->bidi_p)
7268 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7269 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7270 if (it->cmp_it.to < it->cmp_it.nglyphs)
7271 it->cmp_it.from = it->cmp_it.to;
7272 else
7274 it->cmp_it.id = -1;
7275 composition_compute_stop_pos (&it->cmp_it,
7276 IT_STRING_CHARPOS (*it),
7277 IT_STRING_BYTEPOS (*it),
7278 it->end_charpos, it->string);
7281 else if (! it->cmp_it.reversed_p)
7283 for (i = 0; i < it->cmp_it.nchars; i++)
7284 bidi_move_to_visually_next (&it->bidi_it);
7285 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7286 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7288 if (it->cmp_it.to < it->cmp_it.nglyphs)
7289 it->cmp_it.from = it->cmp_it.to;
7290 else
7292 ptrdiff_t stop = it->end_charpos;
7293 if (it->bidi_it.scan_dir < 0)
7294 stop = -1;
7295 composition_compute_stop_pos (&it->cmp_it,
7296 IT_STRING_CHARPOS (*it),
7297 IT_STRING_BYTEPOS (*it), stop,
7298 it->string);
7301 else
7303 for (i = 0; i < it->cmp_it.nchars; i++)
7304 bidi_move_to_visually_next (&it->bidi_it);
7305 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7306 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7307 if (it->cmp_it.from > 0)
7308 it->cmp_it.to = it->cmp_it.from;
7309 else
7311 ptrdiff_t stop = it->end_charpos;
7312 if (it->bidi_it.scan_dir < 0)
7313 stop = -1;
7314 composition_compute_stop_pos (&it->cmp_it,
7315 IT_STRING_CHARPOS (*it),
7316 IT_STRING_BYTEPOS (*it), stop,
7317 it->string);
7321 else
7323 if (!it->bidi_p
7324 /* If the string position is beyond string's end, it
7325 means next_element_from_string is padding the string
7326 with blanks, in which case we bypass the bidi
7327 iterator, because it cannot deal with such virtual
7328 characters. */
7329 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7331 IT_STRING_BYTEPOS (*it) += it->len;
7332 IT_STRING_CHARPOS (*it) += 1;
7334 else
7336 int prev_scan_dir = it->bidi_it.scan_dir;
7338 bidi_move_to_visually_next (&it->bidi_it);
7339 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7340 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7341 if (prev_scan_dir != it->bidi_it.scan_dir)
7343 ptrdiff_t stop = it->end_charpos;
7345 if (it->bidi_it.scan_dir < 0)
7346 stop = -1;
7347 composition_compute_stop_pos (&it->cmp_it,
7348 IT_STRING_CHARPOS (*it),
7349 IT_STRING_BYTEPOS (*it), stop,
7350 it->string);
7355 consider_string_end:
7357 if (it->current.overlay_string_index >= 0)
7359 /* IT->string is an overlay string. Advance to the
7360 next, if there is one. */
7361 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7363 it->ellipsis_p = 0;
7364 next_overlay_string (it);
7365 if (it->ellipsis_p)
7366 setup_for_ellipsis (it, 0);
7369 else
7371 /* IT->string is not an overlay string. If we reached
7372 its end, and there is something on IT->stack, proceed
7373 with what is on the stack. This can be either another
7374 string, this time an overlay string, or a buffer. */
7375 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7376 && it->sp > 0)
7378 pop_it (it);
7379 if (it->method == GET_FROM_STRING)
7380 goto consider_string_end;
7383 break;
7385 case GET_FROM_IMAGE:
7386 case GET_FROM_STRETCH:
7387 /* The position etc with which we have to proceed are on
7388 the stack. The position may be at the end of a string,
7389 if the `display' property takes up the whole string. */
7390 eassert (it->sp > 0);
7391 pop_it (it);
7392 if (it->method == GET_FROM_STRING)
7393 goto consider_string_end;
7394 break;
7396 default:
7397 /* There are no other methods defined, so this should be a bug. */
7398 emacs_abort ();
7401 eassert (it->method != GET_FROM_STRING
7402 || (STRINGP (it->string)
7403 && IT_STRING_CHARPOS (*it) >= 0));
7406 /* Load IT's display element fields with information about the next
7407 display element which comes from a display table entry or from the
7408 result of translating a control character to one of the forms `^C'
7409 or `\003'.
7411 IT->dpvec holds the glyphs to return as characters.
7412 IT->saved_face_id holds the face id before the display vector--it
7413 is restored into IT->face_id in set_iterator_to_next. */
7415 static int
7416 next_element_from_display_vector (struct it *it)
7418 Lisp_Object gc;
7420 /* Precondition. */
7421 eassert (it->dpvec && it->current.dpvec_index >= 0);
7423 it->face_id = it->saved_face_id;
7425 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7426 That seemed totally bogus - so I changed it... */
7427 gc = it->dpvec[it->current.dpvec_index];
7429 if (GLYPH_CODE_P (gc))
7431 it->c = GLYPH_CODE_CHAR (gc);
7432 it->len = CHAR_BYTES (it->c);
7434 /* The entry may contain a face id to use. Such a face id is
7435 the id of a Lisp face, not a realized face. A face id of
7436 zero means no face is specified. */
7437 if (it->dpvec_face_id >= 0)
7438 it->face_id = it->dpvec_face_id;
7439 else
7441 int lface_id = GLYPH_CODE_FACE (gc);
7442 if (lface_id > 0)
7443 it->face_id = merge_faces (it->f, Qt, lface_id,
7444 it->saved_face_id);
7447 else
7448 /* Display table entry is invalid. Return a space. */
7449 it->c = ' ', it->len = 1;
7451 /* Don't change position and object of the iterator here. They are
7452 still the values of the character that had this display table
7453 entry or was translated, and that's what we want. */
7454 it->what = IT_CHARACTER;
7455 return 1;
7458 /* Get the first element of string/buffer in the visual order, after
7459 being reseated to a new position in a string or a buffer. */
7460 static void
7461 get_visually_first_element (struct it *it)
7463 int string_p = STRINGP (it->string) || it->s;
7464 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7465 ptrdiff_t bob = (string_p ? 0 : BEGV);
7467 if (STRINGP (it->string))
7469 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7470 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7472 else
7474 it->bidi_it.charpos = IT_CHARPOS (*it);
7475 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7478 if (it->bidi_it.charpos == eob)
7480 /* Nothing to do, but reset the FIRST_ELT flag, like
7481 bidi_paragraph_init does, because we are not going to
7482 call it. */
7483 it->bidi_it.first_elt = 0;
7485 else if (it->bidi_it.charpos == bob
7486 || (!string_p
7487 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7488 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7490 /* If we are at the beginning of a line/string, we can produce
7491 the next element right away. */
7492 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7493 bidi_move_to_visually_next (&it->bidi_it);
7495 else
7497 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7499 /* We need to prime the bidi iterator starting at the line's or
7500 string's beginning, before we will be able to produce the
7501 next element. */
7502 if (string_p)
7503 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7504 else
7505 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7506 IT_BYTEPOS (*it), -1,
7507 &it->bidi_it.bytepos);
7508 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7511 /* Now return to buffer/string position where we were asked
7512 to get the next display element, and produce that. */
7513 bidi_move_to_visually_next (&it->bidi_it);
7515 while (it->bidi_it.bytepos != orig_bytepos
7516 && it->bidi_it.charpos < eob);
7519 /* Adjust IT's position information to where we ended up. */
7520 if (STRINGP (it->string))
7522 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7523 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7525 else
7527 IT_CHARPOS (*it) = it->bidi_it.charpos;
7528 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7531 if (STRINGP (it->string) || !it->s)
7533 ptrdiff_t stop, charpos, bytepos;
7535 if (STRINGP (it->string))
7537 eassert (!it->s);
7538 stop = SCHARS (it->string);
7539 if (stop > it->end_charpos)
7540 stop = it->end_charpos;
7541 charpos = IT_STRING_CHARPOS (*it);
7542 bytepos = IT_STRING_BYTEPOS (*it);
7544 else
7546 stop = it->end_charpos;
7547 charpos = IT_CHARPOS (*it);
7548 bytepos = IT_BYTEPOS (*it);
7550 if (it->bidi_it.scan_dir < 0)
7551 stop = -1;
7552 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7553 it->string);
7557 /* Load IT with the next display element from Lisp string IT->string.
7558 IT->current.string_pos is the current position within the string.
7559 If IT->current.overlay_string_index >= 0, the Lisp string is an
7560 overlay string. */
7562 static int
7563 next_element_from_string (struct it *it)
7565 struct text_pos position;
7567 eassert (STRINGP (it->string));
7568 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7569 eassert (IT_STRING_CHARPOS (*it) >= 0);
7570 position = it->current.string_pos;
7572 /* With bidi reordering, the character to display might not be the
7573 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7574 that we were reseat()ed to a new string, whose paragraph
7575 direction is not known. */
7576 if (it->bidi_p && it->bidi_it.first_elt)
7578 get_visually_first_element (it);
7579 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7582 /* Time to check for invisible text? */
7583 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7585 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7587 if (!(!it->bidi_p
7588 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7589 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7591 /* With bidi non-linear iteration, we could find
7592 ourselves far beyond the last computed stop_charpos,
7593 with several other stop positions in between that we
7594 missed. Scan them all now, in buffer's logical
7595 order, until we find and handle the last stop_charpos
7596 that precedes our current position. */
7597 handle_stop_backwards (it, it->stop_charpos);
7598 return GET_NEXT_DISPLAY_ELEMENT (it);
7600 else
7602 if (it->bidi_p)
7604 /* Take note of the stop position we just moved
7605 across, for when we will move back across it. */
7606 it->prev_stop = it->stop_charpos;
7607 /* If we are at base paragraph embedding level, take
7608 note of the last stop position seen at this
7609 level. */
7610 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7611 it->base_level_stop = it->stop_charpos;
7613 handle_stop (it);
7615 /* Since a handler may have changed IT->method, we must
7616 recurse here. */
7617 return GET_NEXT_DISPLAY_ELEMENT (it);
7620 else if (it->bidi_p
7621 /* If we are before prev_stop, we may have overstepped
7622 on our way backwards a stop_pos, and if so, we need
7623 to handle that stop_pos. */
7624 && IT_STRING_CHARPOS (*it) < it->prev_stop
7625 /* We can sometimes back up for reasons that have nothing
7626 to do with bidi reordering. E.g., compositions. The
7627 code below is only needed when we are above the base
7628 embedding level, so test for that explicitly. */
7629 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7631 /* If we lost track of base_level_stop, we have no better
7632 place for handle_stop_backwards to start from than string
7633 beginning. This happens, e.g., when we were reseated to
7634 the previous screenful of text by vertical-motion. */
7635 if (it->base_level_stop <= 0
7636 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7637 it->base_level_stop = 0;
7638 handle_stop_backwards (it, it->base_level_stop);
7639 return GET_NEXT_DISPLAY_ELEMENT (it);
7643 if (it->current.overlay_string_index >= 0)
7645 /* Get the next character from an overlay string. In overlay
7646 strings, there is no field width or padding with spaces to
7647 do. */
7648 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7650 it->what = IT_EOB;
7651 return 0;
7653 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7654 IT_STRING_BYTEPOS (*it),
7655 it->bidi_it.scan_dir < 0
7656 ? -1
7657 : SCHARS (it->string))
7658 && next_element_from_composition (it))
7660 return 1;
7662 else if (STRING_MULTIBYTE (it->string))
7664 const unsigned char *s = (SDATA (it->string)
7665 + IT_STRING_BYTEPOS (*it));
7666 it->c = string_char_and_length (s, &it->len);
7668 else
7670 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7671 it->len = 1;
7674 else
7676 /* Get the next character from a Lisp string that is not an
7677 overlay string. Such strings come from the mode line, for
7678 example. We may have to pad with spaces, or truncate the
7679 string. See also next_element_from_c_string. */
7680 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7682 it->what = IT_EOB;
7683 return 0;
7685 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7687 /* Pad with spaces. */
7688 it->c = ' ', it->len = 1;
7689 CHARPOS (position) = BYTEPOS (position) = -1;
7691 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7692 IT_STRING_BYTEPOS (*it),
7693 it->bidi_it.scan_dir < 0
7694 ? -1
7695 : it->string_nchars)
7696 && next_element_from_composition (it))
7698 return 1;
7700 else if (STRING_MULTIBYTE (it->string))
7702 const unsigned char *s = (SDATA (it->string)
7703 + IT_STRING_BYTEPOS (*it));
7704 it->c = string_char_and_length (s, &it->len);
7706 else
7708 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7709 it->len = 1;
7713 /* Record what we have and where it came from. */
7714 it->what = IT_CHARACTER;
7715 it->object = it->string;
7716 it->position = position;
7717 return 1;
7721 /* Load IT with next display element from C string IT->s.
7722 IT->string_nchars is the maximum number of characters to return
7723 from the string. IT->end_charpos may be greater than
7724 IT->string_nchars when this function is called, in which case we
7725 may have to return padding spaces. Value is zero if end of string
7726 reached, including padding spaces. */
7728 static int
7729 next_element_from_c_string (struct it *it)
7731 int success_p = 1;
7733 eassert (it->s);
7734 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7735 it->what = IT_CHARACTER;
7736 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7737 it->object = Qnil;
7739 /* With bidi reordering, the character to display might not be the
7740 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7741 we were reseated to a new string, whose paragraph direction is
7742 not known. */
7743 if (it->bidi_p && it->bidi_it.first_elt)
7744 get_visually_first_element (it);
7746 /* IT's position can be greater than IT->string_nchars in case a
7747 field width or precision has been specified when the iterator was
7748 initialized. */
7749 if (IT_CHARPOS (*it) >= it->end_charpos)
7751 /* End of the game. */
7752 it->what = IT_EOB;
7753 success_p = 0;
7755 else if (IT_CHARPOS (*it) >= it->string_nchars)
7757 /* Pad with spaces. */
7758 it->c = ' ', it->len = 1;
7759 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7761 else if (it->multibyte_p)
7762 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7763 else
7764 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7766 return success_p;
7770 /* Set up IT to return characters from an ellipsis, if appropriate.
7771 The definition of the ellipsis glyphs may come from a display table
7772 entry. This function fills IT with the first glyph from the
7773 ellipsis if an ellipsis is to be displayed. */
7775 static int
7776 next_element_from_ellipsis (struct it *it)
7778 if (it->selective_display_ellipsis_p)
7779 setup_for_ellipsis (it, it->len);
7780 else
7782 /* The face at the current position may be different from the
7783 face we find after the invisible text. Remember what it
7784 was in IT->saved_face_id, and signal that it's there by
7785 setting face_before_selective_p. */
7786 it->saved_face_id = it->face_id;
7787 it->method = GET_FROM_BUFFER;
7788 it->object = it->w->contents;
7789 reseat_at_next_visible_line_start (it, 1);
7790 it->face_before_selective_p = 1;
7793 return GET_NEXT_DISPLAY_ELEMENT (it);
7797 /* Deliver an image display element. The iterator IT is already
7798 filled with image information (done in handle_display_prop). Value
7799 is always 1. */
7802 static int
7803 next_element_from_image (struct it *it)
7805 it->what = IT_IMAGE;
7806 it->ignore_overlay_strings_at_pos_p = 0;
7807 return 1;
7811 /* Fill iterator IT with next display element from a stretch glyph
7812 property. IT->object is the value of the text property. Value is
7813 always 1. */
7815 static int
7816 next_element_from_stretch (struct it *it)
7818 it->what = IT_STRETCH;
7819 return 1;
7822 /* Scan backwards from IT's current position until we find a stop
7823 position, or until BEGV. This is called when we find ourself
7824 before both the last known prev_stop and base_level_stop while
7825 reordering bidirectional text. */
7827 static void
7828 compute_stop_pos_backwards (struct it *it)
7830 const int SCAN_BACK_LIMIT = 1000;
7831 struct text_pos pos;
7832 struct display_pos save_current = it->current;
7833 struct text_pos save_position = it->position;
7834 ptrdiff_t charpos = IT_CHARPOS (*it);
7835 ptrdiff_t where_we_are = charpos;
7836 ptrdiff_t save_stop_pos = it->stop_charpos;
7837 ptrdiff_t save_end_pos = it->end_charpos;
7839 eassert (NILP (it->string) && !it->s);
7840 eassert (it->bidi_p);
7841 it->bidi_p = 0;
7844 it->end_charpos = min (charpos + 1, ZV);
7845 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7846 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7847 reseat_1 (it, pos, 0);
7848 compute_stop_pos (it);
7849 /* We must advance forward, right? */
7850 if (it->stop_charpos <= charpos)
7851 emacs_abort ();
7853 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7855 if (it->stop_charpos <= where_we_are)
7856 it->prev_stop = it->stop_charpos;
7857 else
7858 it->prev_stop = BEGV;
7859 it->bidi_p = 1;
7860 it->current = save_current;
7861 it->position = save_position;
7862 it->stop_charpos = save_stop_pos;
7863 it->end_charpos = save_end_pos;
7866 /* Scan forward from CHARPOS in the current buffer/string, until we
7867 find a stop position > current IT's position. Then handle the stop
7868 position before that. This is called when we bump into a stop
7869 position while reordering bidirectional text. CHARPOS should be
7870 the last previously processed stop_pos (or BEGV/0, if none were
7871 processed yet) whose position is less that IT's current
7872 position. */
7874 static void
7875 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7877 int bufp = !STRINGP (it->string);
7878 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7879 struct display_pos save_current = it->current;
7880 struct text_pos save_position = it->position;
7881 struct text_pos pos1;
7882 ptrdiff_t next_stop;
7884 /* Scan in strict logical order. */
7885 eassert (it->bidi_p);
7886 it->bidi_p = 0;
7889 it->prev_stop = charpos;
7890 if (bufp)
7892 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7893 reseat_1 (it, pos1, 0);
7895 else
7896 it->current.string_pos = string_pos (charpos, it->string);
7897 compute_stop_pos (it);
7898 /* We must advance forward, right? */
7899 if (it->stop_charpos <= it->prev_stop)
7900 emacs_abort ();
7901 charpos = it->stop_charpos;
7903 while (charpos <= where_we_are);
7905 it->bidi_p = 1;
7906 it->current = save_current;
7907 it->position = save_position;
7908 next_stop = it->stop_charpos;
7909 it->stop_charpos = it->prev_stop;
7910 handle_stop (it);
7911 it->stop_charpos = next_stop;
7914 /* Load IT with the next display element from current_buffer. Value
7915 is zero if end of buffer reached. IT->stop_charpos is the next
7916 position at which to stop and check for text properties or buffer
7917 end. */
7919 static int
7920 next_element_from_buffer (struct it *it)
7922 int success_p = 1;
7924 eassert (IT_CHARPOS (*it) >= BEGV);
7925 eassert (NILP (it->string) && !it->s);
7926 eassert (!it->bidi_p
7927 || (EQ (it->bidi_it.string.lstring, Qnil)
7928 && it->bidi_it.string.s == NULL));
7930 /* With bidi reordering, the character to display might not be the
7931 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7932 we were reseat()ed to a new buffer position, which is potentially
7933 a different paragraph. */
7934 if (it->bidi_p && it->bidi_it.first_elt)
7936 get_visually_first_element (it);
7937 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7940 if (IT_CHARPOS (*it) >= it->stop_charpos)
7942 if (IT_CHARPOS (*it) >= it->end_charpos)
7944 int overlay_strings_follow_p;
7946 /* End of the game, except when overlay strings follow that
7947 haven't been returned yet. */
7948 if (it->overlay_strings_at_end_processed_p)
7949 overlay_strings_follow_p = 0;
7950 else
7952 it->overlay_strings_at_end_processed_p = 1;
7953 overlay_strings_follow_p = get_overlay_strings (it, 0);
7956 if (overlay_strings_follow_p)
7957 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7958 else
7960 it->what = IT_EOB;
7961 it->position = it->current.pos;
7962 success_p = 0;
7965 else if (!(!it->bidi_p
7966 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7967 || IT_CHARPOS (*it) == it->stop_charpos))
7969 /* With bidi non-linear iteration, we could find ourselves
7970 far beyond the last computed stop_charpos, with several
7971 other stop positions in between that we missed. Scan
7972 them all now, in buffer's logical order, until we find
7973 and handle the last stop_charpos that precedes our
7974 current position. */
7975 handle_stop_backwards (it, it->stop_charpos);
7976 return GET_NEXT_DISPLAY_ELEMENT (it);
7978 else
7980 if (it->bidi_p)
7982 /* Take note of the stop position we just moved across,
7983 for when we will move back across it. */
7984 it->prev_stop = it->stop_charpos;
7985 /* If we are at base paragraph embedding level, take
7986 note of the last stop position seen at this
7987 level. */
7988 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7989 it->base_level_stop = it->stop_charpos;
7991 handle_stop (it);
7992 return GET_NEXT_DISPLAY_ELEMENT (it);
7995 else if (it->bidi_p
7996 /* If we are before prev_stop, we may have overstepped on
7997 our way backwards a stop_pos, and if so, we need to
7998 handle that stop_pos. */
7999 && IT_CHARPOS (*it) < it->prev_stop
8000 /* We can sometimes back up for reasons that have nothing
8001 to do with bidi reordering. E.g., compositions. The
8002 code below is only needed when we are above the base
8003 embedding level, so test for that explicitly. */
8004 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8006 if (it->base_level_stop <= 0
8007 || IT_CHARPOS (*it) < it->base_level_stop)
8009 /* If we lost track of base_level_stop, we need to find
8010 prev_stop by looking backwards. This happens, e.g., when
8011 we were reseated to the previous screenful of text by
8012 vertical-motion. */
8013 it->base_level_stop = BEGV;
8014 compute_stop_pos_backwards (it);
8015 handle_stop_backwards (it, it->prev_stop);
8017 else
8018 handle_stop_backwards (it, it->base_level_stop);
8019 return GET_NEXT_DISPLAY_ELEMENT (it);
8021 else
8023 /* No face changes, overlays etc. in sight, so just return a
8024 character from current_buffer. */
8025 unsigned char *p;
8026 ptrdiff_t stop;
8028 /* Maybe run the redisplay end trigger hook. Performance note:
8029 This doesn't seem to cost measurable time. */
8030 if (it->redisplay_end_trigger_charpos
8031 && it->glyph_row
8032 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8033 run_redisplay_end_trigger_hook (it);
8035 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8036 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8037 stop)
8038 && next_element_from_composition (it))
8040 return 1;
8043 /* Get the next character, maybe multibyte. */
8044 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8045 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8046 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8047 else
8048 it->c = *p, it->len = 1;
8050 /* Record what we have and where it came from. */
8051 it->what = IT_CHARACTER;
8052 it->object = it->w->contents;
8053 it->position = it->current.pos;
8055 /* Normally we return the character found above, except when we
8056 really want to return an ellipsis for selective display. */
8057 if (it->selective)
8059 if (it->c == '\n')
8061 /* A value of selective > 0 means hide lines indented more
8062 than that number of columns. */
8063 if (it->selective > 0
8064 && IT_CHARPOS (*it) + 1 < ZV
8065 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8066 IT_BYTEPOS (*it) + 1,
8067 it->selective))
8069 success_p = next_element_from_ellipsis (it);
8070 it->dpvec_char_len = -1;
8073 else if (it->c == '\r' && it->selective == -1)
8075 /* A value of selective == -1 means that everything from the
8076 CR to the end of the line is invisible, with maybe an
8077 ellipsis displayed for it. */
8078 success_p = next_element_from_ellipsis (it);
8079 it->dpvec_char_len = -1;
8084 /* Value is zero if end of buffer reached. */
8085 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8086 return success_p;
8090 /* Run the redisplay end trigger hook for IT. */
8092 static void
8093 run_redisplay_end_trigger_hook (struct it *it)
8095 Lisp_Object args[3];
8097 /* IT->glyph_row should be non-null, i.e. we should be actually
8098 displaying something, or otherwise we should not run the hook. */
8099 eassert (it->glyph_row);
8101 /* Set up hook arguments. */
8102 args[0] = Qredisplay_end_trigger_functions;
8103 args[1] = it->window;
8104 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8105 it->redisplay_end_trigger_charpos = 0;
8107 /* Since we are *trying* to run these functions, don't try to run
8108 them again, even if they get an error. */
8109 wset_redisplay_end_trigger (it->w, Qnil);
8110 Frun_hook_with_args (3, args);
8112 /* Notice if it changed the face of the character we are on. */
8113 handle_face_prop (it);
8117 /* Deliver a composition display element. Unlike the other
8118 next_element_from_XXX, this function is not registered in the array
8119 get_next_element[]. It is called from next_element_from_buffer and
8120 next_element_from_string when necessary. */
8122 static int
8123 next_element_from_composition (struct it *it)
8125 it->what = IT_COMPOSITION;
8126 it->len = it->cmp_it.nbytes;
8127 if (STRINGP (it->string))
8129 if (it->c < 0)
8131 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8132 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8133 return 0;
8135 it->position = it->current.string_pos;
8136 it->object = it->string;
8137 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8138 IT_STRING_BYTEPOS (*it), it->string);
8140 else
8142 if (it->c < 0)
8144 IT_CHARPOS (*it) += it->cmp_it.nchars;
8145 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8146 if (it->bidi_p)
8148 if (it->bidi_it.new_paragraph)
8149 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8150 /* Resync the bidi iterator with IT's new position.
8151 FIXME: this doesn't support bidirectional text. */
8152 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8153 bidi_move_to_visually_next (&it->bidi_it);
8155 return 0;
8157 it->position = it->current.pos;
8158 it->object = it->w->contents;
8159 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8160 IT_BYTEPOS (*it), Qnil);
8162 return 1;
8167 /***********************************************************************
8168 Moving an iterator without producing glyphs
8169 ***********************************************************************/
8171 /* Check if iterator is at a position corresponding to a valid buffer
8172 position after some move_it_ call. */
8174 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8175 ((it)->method == GET_FROM_STRING \
8176 ? IT_STRING_CHARPOS (*it) == 0 \
8177 : 1)
8180 /* Move iterator IT to a specified buffer or X position within one
8181 line on the display without producing glyphs.
8183 OP should be a bit mask including some or all of these bits:
8184 MOVE_TO_X: Stop upon reaching x-position TO_X.
8185 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8186 Regardless of OP's value, stop upon reaching the end of the display line.
8188 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8189 This means, in particular, that TO_X includes window's horizontal
8190 scroll amount.
8192 The return value has several possible values that
8193 say what condition caused the scan to stop:
8195 MOVE_POS_MATCH_OR_ZV
8196 - when TO_POS or ZV was reached.
8198 MOVE_X_REACHED
8199 -when TO_X was reached before TO_POS or ZV were reached.
8201 MOVE_LINE_CONTINUED
8202 - when we reached the end of the display area and the line must
8203 be continued.
8205 MOVE_LINE_TRUNCATED
8206 - when we reached the end of the display area and the line is
8207 truncated.
8209 MOVE_NEWLINE_OR_CR
8210 - when we stopped at a line end, i.e. a newline or a CR and selective
8211 display is on. */
8213 static enum move_it_result
8214 move_it_in_display_line_to (struct it *it,
8215 ptrdiff_t to_charpos, int to_x,
8216 enum move_operation_enum op)
8218 enum move_it_result result = MOVE_UNDEFINED;
8219 struct glyph_row *saved_glyph_row;
8220 struct it wrap_it, atpos_it, atx_it, ppos_it;
8221 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8222 void *ppos_data = NULL;
8223 int may_wrap = 0;
8224 enum it_method prev_method = it->method;
8225 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8226 int saw_smaller_pos = prev_pos < to_charpos;
8228 /* Don't produce glyphs in produce_glyphs. */
8229 saved_glyph_row = it->glyph_row;
8230 it->glyph_row = NULL;
8232 /* Use wrap_it to save a copy of IT wherever a word wrap could
8233 occur. Use atpos_it to save a copy of IT at the desired buffer
8234 position, if found, so that we can scan ahead and check if the
8235 word later overshoots the window edge. Use atx_it similarly, for
8236 pixel positions. */
8237 wrap_it.sp = -1;
8238 atpos_it.sp = -1;
8239 atx_it.sp = -1;
8241 /* Use ppos_it under bidi reordering to save a copy of IT for the
8242 position > CHARPOS that is the closest to CHARPOS. We restore
8243 that position in IT when we have scanned the entire display line
8244 without finding a match for CHARPOS and all the character
8245 positions are greater than CHARPOS. */
8246 if (it->bidi_p)
8248 SAVE_IT (ppos_it, *it, ppos_data);
8249 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8250 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8251 SAVE_IT (ppos_it, *it, ppos_data);
8254 #define BUFFER_POS_REACHED_P() \
8255 ((op & MOVE_TO_POS) != 0 \
8256 && BUFFERP (it->object) \
8257 && (IT_CHARPOS (*it) == to_charpos \
8258 || ((!it->bidi_p \
8259 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8260 && IT_CHARPOS (*it) > to_charpos) \
8261 || (it->what == IT_COMPOSITION \
8262 && ((IT_CHARPOS (*it) > to_charpos \
8263 && to_charpos >= it->cmp_it.charpos) \
8264 || (IT_CHARPOS (*it) < to_charpos \
8265 && to_charpos <= it->cmp_it.charpos)))) \
8266 && (it->method == GET_FROM_BUFFER \
8267 || (it->method == GET_FROM_DISPLAY_VECTOR \
8268 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8270 /* If there's a line-/wrap-prefix, handle it. */
8271 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8272 && it->current_y < it->last_visible_y)
8273 handle_line_prefix (it);
8275 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8276 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8278 while (1)
8280 int x, i, ascent = 0, descent = 0;
8282 /* Utility macro to reset an iterator with x, ascent, and descent. */
8283 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8284 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8285 (IT)->max_descent = descent)
8287 /* Stop if we move beyond TO_CHARPOS (after an image or a
8288 display string or stretch glyph). */
8289 if ((op & MOVE_TO_POS) != 0
8290 && BUFFERP (it->object)
8291 && it->method == GET_FROM_BUFFER
8292 && (((!it->bidi_p
8293 /* When the iterator is at base embedding level, we
8294 are guaranteed that characters are delivered for
8295 display in strictly increasing order of their
8296 buffer positions. */
8297 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8298 && IT_CHARPOS (*it) > to_charpos)
8299 || (it->bidi_p
8300 && (prev_method == GET_FROM_IMAGE
8301 || prev_method == GET_FROM_STRETCH
8302 || prev_method == GET_FROM_STRING)
8303 /* Passed TO_CHARPOS from left to right. */
8304 && ((prev_pos < to_charpos
8305 && IT_CHARPOS (*it) > to_charpos)
8306 /* Passed TO_CHARPOS from right to left. */
8307 || (prev_pos > to_charpos
8308 && IT_CHARPOS (*it) < to_charpos)))))
8310 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8312 result = MOVE_POS_MATCH_OR_ZV;
8313 break;
8315 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8316 /* If wrap_it is valid, the current position might be in a
8317 word that is wrapped. So, save the iterator in
8318 atpos_it and continue to see if wrapping happens. */
8319 SAVE_IT (atpos_it, *it, atpos_data);
8322 /* Stop when ZV reached.
8323 We used to stop here when TO_CHARPOS reached as well, but that is
8324 too soon if this glyph does not fit on this line. So we handle it
8325 explicitly below. */
8326 if (!get_next_display_element (it))
8328 result = MOVE_POS_MATCH_OR_ZV;
8329 break;
8332 if (it->line_wrap == TRUNCATE)
8334 if (BUFFER_POS_REACHED_P ())
8336 result = MOVE_POS_MATCH_OR_ZV;
8337 break;
8340 else
8342 if (it->line_wrap == WORD_WRAP)
8344 if (IT_DISPLAYING_WHITESPACE (it))
8345 may_wrap = 1;
8346 else if (may_wrap)
8348 /* We have reached a glyph that follows one or more
8349 whitespace characters. If the position is
8350 already found, we are done. */
8351 if (atpos_it.sp >= 0)
8353 RESTORE_IT (it, &atpos_it, atpos_data);
8354 result = MOVE_POS_MATCH_OR_ZV;
8355 goto done;
8357 if (atx_it.sp >= 0)
8359 RESTORE_IT (it, &atx_it, atx_data);
8360 result = MOVE_X_REACHED;
8361 goto done;
8363 /* Otherwise, we can wrap here. */
8364 SAVE_IT (wrap_it, *it, wrap_data);
8365 may_wrap = 0;
8370 /* Remember the line height for the current line, in case
8371 the next element doesn't fit on the line. */
8372 ascent = it->max_ascent;
8373 descent = it->max_descent;
8375 /* The call to produce_glyphs will get the metrics of the
8376 display element IT is loaded with. Record the x-position
8377 before this display element, in case it doesn't fit on the
8378 line. */
8379 x = it->current_x;
8381 PRODUCE_GLYPHS (it);
8383 if (it->area != TEXT_AREA)
8385 prev_method = it->method;
8386 if (it->method == GET_FROM_BUFFER)
8387 prev_pos = IT_CHARPOS (*it);
8388 set_iterator_to_next (it, 1);
8389 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8390 SET_TEXT_POS (this_line_min_pos,
8391 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8392 if (it->bidi_p
8393 && (op & MOVE_TO_POS)
8394 && IT_CHARPOS (*it) > to_charpos
8395 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8396 SAVE_IT (ppos_it, *it, ppos_data);
8397 continue;
8400 /* The number of glyphs we get back in IT->nglyphs will normally
8401 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8402 character on a terminal frame, or (iii) a line end. For the
8403 second case, IT->nglyphs - 1 padding glyphs will be present.
8404 (On X frames, there is only one glyph produced for a
8405 composite character.)
8407 The behavior implemented below means, for continuation lines,
8408 that as many spaces of a TAB as fit on the current line are
8409 displayed there. For terminal frames, as many glyphs of a
8410 multi-glyph character are displayed in the current line, too.
8411 This is what the old redisplay code did, and we keep it that
8412 way. Under X, the whole shape of a complex character must
8413 fit on the line or it will be completely displayed in the
8414 next line.
8416 Note that both for tabs and padding glyphs, all glyphs have
8417 the same width. */
8418 if (it->nglyphs)
8420 /* More than one glyph or glyph doesn't fit on line. All
8421 glyphs have the same width. */
8422 int single_glyph_width = it->pixel_width / it->nglyphs;
8423 int new_x;
8424 int x_before_this_char = x;
8425 int hpos_before_this_char = it->hpos;
8427 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8429 new_x = x + single_glyph_width;
8431 /* We want to leave anything reaching TO_X to the caller. */
8432 if ((op & MOVE_TO_X) && new_x > to_x)
8434 if (BUFFER_POS_REACHED_P ())
8436 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8437 goto buffer_pos_reached;
8438 if (atpos_it.sp < 0)
8440 SAVE_IT (atpos_it, *it, atpos_data);
8441 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8444 else
8446 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8448 it->current_x = x;
8449 result = MOVE_X_REACHED;
8450 break;
8452 if (atx_it.sp < 0)
8454 SAVE_IT (atx_it, *it, atx_data);
8455 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8460 if (/* Lines are continued. */
8461 it->line_wrap != TRUNCATE
8462 && (/* And glyph doesn't fit on the line. */
8463 new_x > it->last_visible_x
8464 /* Or it fits exactly and we're on a window
8465 system frame. */
8466 || (new_x == it->last_visible_x
8467 && FRAME_WINDOW_P (it->f)
8468 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8469 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8470 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8472 if (/* IT->hpos == 0 means the very first glyph
8473 doesn't fit on the line, e.g. a wide image. */
8474 it->hpos == 0
8475 || (new_x == it->last_visible_x
8476 && FRAME_WINDOW_P (it->f)))
8478 ++it->hpos;
8479 it->current_x = new_x;
8481 /* The character's last glyph just barely fits
8482 in this row. */
8483 if (i == it->nglyphs - 1)
8485 /* If this is the destination position,
8486 return a position *before* it in this row,
8487 now that we know it fits in this row. */
8488 if (BUFFER_POS_REACHED_P ())
8490 if (it->line_wrap != WORD_WRAP
8491 || wrap_it.sp < 0)
8493 it->hpos = hpos_before_this_char;
8494 it->current_x = x_before_this_char;
8495 result = MOVE_POS_MATCH_OR_ZV;
8496 break;
8498 if (it->line_wrap == WORD_WRAP
8499 && atpos_it.sp < 0)
8501 SAVE_IT (atpos_it, *it, atpos_data);
8502 atpos_it.current_x = x_before_this_char;
8503 atpos_it.hpos = hpos_before_this_char;
8507 prev_method = it->method;
8508 if (it->method == GET_FROM_BUFFER)
8509 prev_pos = IT_CHARPOS (*it);
8510 set_iterator_to_next (it, 1);
8511 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8512 SET_TEXT_POS (this_line_min_pos,
8513 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8514 /* On graphical terminals, newlines may
8515 "overflow" into the fringe if
8516 overflow-newline-into-fringe is non-nil.
8517 On text terminals, and on graphical
8518 terminals with no right margin, newlines
8519 may overflow into the last glyph on the
8520 display line.*/
8521 if (!FRAME_WINDOW_P (it->f)
8522 || ((it->bidi_p
8523 && it->bidi_it.paragraph_dir == R2L)
8524 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8525 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8526 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8528 if (!get_next_display_element (it))
8530 result = MOVE_POS_MATCH_OR_ZV;
8531 break;
8533 if (BUFFER_POS_REACHED_P ())
8535 if (ITERATOR_AT_END_OF_LINE_P (it))
8536 result = MOVE_POS_MATCH_OR_ZV;
8537 else
8538 result = MOVE_LINE_CONTINUED;
8539 break;
8541 if (ITERATOR_AT_END_OF_LINE_P (it)
8542 && (it->line_wrap != WORD_WRAP
8543 || wrap_it.sp < 0))
8545 result = MOVE_NEWLINE_OR_CR;
8546 break;
8551 else
8552 IT_RESET_X_ASCENT_DESCENT (it);
8554 if (wrap_it.sp >= 0)
8556 RESTORE_IT (it, &wrap_it, wrap_data);
8557 atpos_it.sp = -1;
8558 atx_it.sp = -1;
8561 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8562 IT_CHARPOS (*it)));
8563 result = MOVE_LINE_CONTINUED;
8564 break;
8567 if (BUFFER_POS_REACHED_P ())
8569 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8570 goto buffer_pos_reached;
8571 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8573 SAVE_IT (atpos_it, *it, atpos_data);
8574 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8578 if (new_x > it->first_visible_x)
8580 /* Glyph is visible. Increment number of glyphs that
8581 would be displayed. */
8582 ++it->hpos;
8586 if (result != MOVE_UNDEFINED)
8587 break;
8589 else if (BUFFER_POS_REACHED_P ())
8591 buffer_pos_reached:
8592 IT_RESET_X_ASCENT_DESCENT (it);
8593 result = MOVE_POS_MATCH_OR_ZV;
8594 break;
8596 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8598 /* Stop when TO_X specified and reached. This check is
8599 necessary here because of lines consisting of a line end,
8600 only. The line end will not produce any glyphs and we
8601 would never get MOVE_X_REACHED. */
8602 eassert (it->nglyphs == 0);
8603 result = MOVE_X_REACHED;
8604 break;
8607 /* Is this a line end? If yes, we're done. */
8608 if (ITERATOR_AT_END_OF_LINE_P (it))
8610 /* If we are past TO_CHARPOS, but never saw any character
8611 positions smaller than TO_CHARPOS, return
8612 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8613 did. */
8614 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8616 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8618 if (IT_CHARPOS (ppos_it) < ZV)
8620 RESTORE_IT (it, &ppos_it, ppos_data);
8621 result = MOVE_POS_MATCH_OR_ZV;
8623 else
8624 goto buffer_pos_reached;
8626 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8627 && IT_CHARPOS (*it) > to_charpos)
8628 goto buffer_pos_reached;
8629 else
8630 result = MOVE_NEWLINE_OR_CR;
8632 else
8633 result = MOVE_NEWLINE_OR_CR;
8634 break;
8637 prev_method = it->method;
8638 if (it->method == GET_FROM_BUFFER)
8639 prev_pos = IT_CHARPOS (*it);
8640 /* The current display element has been consumed. Advance
8641 to the next. */
8642 set_iterator_to_next (it, 1);
8643 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8644 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8645 if (IT_CHARPOS (*it) < to_charpos)
8646 saw_smaller_pos = 1;
8647 if (it->bidi_p
8648 && (op & MOVE_TO_POS)
8649 && IT_CHARPOS (*it) >= to_charpos
8650 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8651 SAVE_IT (ppos_it, *it, ppos_data);
8653 /* Stop if lines are truncated and IT's current x-position is
8654 past the right edge of the window now. */
8655 if (it->line_wrap == TRUNCATE
8656 && it->current_x >= it->last_visible_x)
8658 if (!FRAME_WINDOW_P (it->f)
8659 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8660 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8661 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8662 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8664 int at_eob_p = 0;
8666 if ((at_eob_p = !get_next_display_element (it))
8667 || BUFFER_POS_REACHED_P ()
8668 /* If we are past TO_CHARPOS, but never saw any
8669 character positions smaller than TO_CHARPOS,
8670 return MOVE_POS_MATCH_OR_ZV, like the
8671 unidirectional display did. */
8672 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8673 && !saw_smaller_pos
8674 && IT_CHARPOS (*it) > to_charpos))
8676 if (it->bidi_p
8677 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8678 RESTORE_IT (it, &ppos_it, ppos_data);
8679 result = MOVE_POS_MATCH_OR_ZV;
8680 break;
8682 if (ITERATOR_AT_END_OF_LINE_P (it))
8684 result = MOVE_NEWLINE_OR_CR;
8685 break;
8688 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8689 && !saw_smaller_pos
8690 && IT_CHARPOS (*it) > to_charpos)
8692 if (IT_CHARPOS (ppos_it) < ZV)
8693 RESTORE_IT (it, &ppos_it, ppos_data);
8694 result = MOVE_POS_MATCH_OR_ZV;
8695 break;
8697 result = MOVE_LINE_TRUNCATED;
8698 break;
8700 #undef IT_RESET_X_ASCENT_DESCENT
8703 #undef BUFFER_POS_REACHED_P
8705 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8706 restore the saved iterator. */
8707 if (atpos_it.sp >= 0)
8708 RESTORE_IT (it, &atpos_it, atpos_data);
8709 else if (atx_it.sp >= 0)
8710 RESTORE_IT (it, &atx_it, atx_data);
8712 done:
8714 if (atpos_data)
8715 bidi_unshelve_cache (atpos_data, 1);
8716 if (atx_data)
8717 bidi_unshelve_cache (atx_data, 1);
8718 if (wrap_data)
8719 bidi_unshelve_cache (wrap_data, 1);
8720 if (ppos_data)
8721 bidi_unshelve_cache (ppos_data, 1);
8723 /* Restore the iterator settings altered at the beginning of this
8724 function. */
8725 it->glyph_row = saved_glyph_row;
8726 return result;
8729 /* For external use. */
8730 void
8731 move_it_in_display_line (struct it *it,
8732 ptrdiff_t to_charpos, int to_x,
8733 enum move_operation_enum op)
8735 if (it->line_wrap == WORD_WRAP
8736 && (op & MOVE_TO_X))
8738 struct it save_it;
8739 void *save_data = NULL;
8740 int skip;
8742 SAVE_IT (save_it, *it, save_data);
8743 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8744 /* When word-wrap is on, TO_X may lie past the end
8745 of a wrapped line. Then it->current is the
8746 character on the next line, so backtrack to the
8747 space before the wrap point. */
8748 if (skip == MOVE_LINE_CONTINUED)
8750 int prev_x = max (it->current_x - 1, 0);
8751 RESTORE_IT (it, &save_it, save_data);
8752 move_it_in_display_line_to
8753 (it, -1, prev_x, MOVE_TO_X);
8755 else
8756 bidi_unshelve_cache (save_data, 1);
8758 else
8759 move_it_in_display_line_to (it, to_charpos, to_x, op);
8763 /* Move IT forward until it satisfies one or more of the criteria in
8764 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8766 OP is a bit-mask that specifies where to stop, and in particular,
8767 which of those four position arguments makes a difference. See the
8768 description of enum move_operation_enum.
8770 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8771 screen line, this function will set IT to the next position that is
8772 displayed to the right of TO_CHARPOS on the screen. */
8774 void
8775 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8777 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8778 int line_height, line_start_x = 0, reached = 0;
8779 void *backup_data = NULL;
8781 for (;;)
8783 if (op & MOVE_TO_VPOS)
8785 /* If no TO_CHARPOS and no TO_X specified, stop at the
8786 start of the line TO_VPOS. */
8787 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8789 if (it->vpos == to_vpos)
8791 reached = 1;
8792 break;
8794 else
8795 skip = move_it_in_display_line_to (it, -1, -1, 0);
8797 else
8799 /* TO_VPOS >= 0 means stop at TO_X in the line at
8800 TO_VPOS, or at TO_POS, whichever comes first. */
8801 if (it->vpos == to_vpos)
8803 reached = 2;
8804 break;
8807 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8809 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8811 reached = 3;
8812 break;
8814 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8816 /* We have reached TO_X but not in the line we want. */
8817 skip = move_it_in_display_line_to (it, to_charpos,
8818 -1, MOVE_TO_POS);
8819 if (skip == MOVE_POS_MATCH_OR_ZV)
8821 reached = 4;
8822 break;
8827 else if (op & MOVE_TO_Y)
8829 struct it it_backup;
8831 if (it->line_wrap == WORD_WRAP)
8832 SAVE_IT (it_backup, *it, backup_data);
8834 /* TO_Y specified means stop at TO_X in the line containing
8835 TO_Y---or at TO_CHARPOS if this is reached first. The
8836 problem is that we can't really tell whether the line
8837 contains TO_Y before we have completely scanned it, and
8838 this may skip past TO_X. What we do is to first scan to
8839 TO_X.
8841 If TO_X is not specified, use a TO_X of zero. The reason
8842 is to make the outcome of this function more predictable.
8843 If we didn't use TO_X == 0, we would stop at the end of
8844 the line which is probably not what a caller would expect
8845 to happen. */
8846 skip = move_it_in_display_line_to
8847 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8848 (MOVE_TO_X | (op & MOVE_TO_POS)));
8850 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8851 if (skip == MOVE_POS_MATCH_OR_ZV)
8852 reached = 5;
8853 else if (skip == MOVE_X_REACHED)
8855 /* If TO_X was reached, we want to know whether TO_Y is
8856 in the line. We know this is the case if the already
8857 scanned glyphs make the line tall enough. Otherwise,
8858 we must check by scanning the rest of the line. */
8859 line_height = it->max_ascent + it->max_descent;
8860 if (to_y >= it->current_y
8861 && to_y < it->current_y + line_height)
8863 reached = 6;
8864 break;
8866 SAVE_IT (it_backup, *it, backup_data);
8867 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8868 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8869 op & MOVE_TO_POS);
8870 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8871 line_height = it->max_ascent + it->max_descent;
8872 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8874 if (to_y >= it->current_y
8875 && to_y < it->current_y + line_height)
8877 /* If TO_Y is in this line and TO_X was reached
8878 above, we scanned too far. We have to restore
8879 IT's settings to the ones before skipping. But
8880 keep the more accurate values of max_ascent and
8881 max_descent we've found while skipping the rest
8882 of the line, for the sake of callers, such as
8883 pos_visible_p, that need to know the line
8884 height. */
8885 int max_ascent = it->max_ascent;
8886 int max_descent = it->max_descent;
8888 RESTORE_IT (it, &it_backup, backup_data);
8889 it->max_ascent = max_ascent;
8890 it->max_descent = max_descent;
8891 reached = 6;
8893 else
8895 skip = skip2;
8896 if (skip == MOVE_POS_MATCH_OR_ZV)
8897 reached = 7;
8900 else
8902 /* Check whether TO_Y is in this line. */
8903 line_height = it->max_ascent + it->max_descent;
8904 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8906 if (to_y >= it->current_y
8907 && to_y < it->current_y + line_height)
8909 /* When word-wrap is on, TO_X may lie past the end
8910 of a wrapped line. Then it->current is the
8911 character on the next line, so backtrack to the
8912 space before the wrap point. */
8913 if (skip == MOVE_LINE_CONTINUED
8914 && it->line_wrap == WORD_WRAP)
8916 int prev_x = max (it->current_x - 1, 0);
8917 RESTORE_IT (it, &it_backup, backup_data);
8918 skip = move_it_in_display_line_to
8919 (it, -1, prev_x, MOVE_TO_X);
8921 reached = 6;
8925 if (reached)
8926 break;
8928 else if (BUFFERP (it->object)
8929 && (it->method == GET_FROM_BUFFER
8930 || it->method == GET_FROM_STRETCH)
8931 && IT_CHARPOS (*it) >= to_charpos
8932 /* Under bidi iteration, a call to set_iterator_to_next
8933 can scan far beyond to_charpos if the initial
8934 portion of the next line needs to be reordered. In
8935 that case, give move_it_in_display_line_to another
8936 chance below. */
8937 && !(it->bidi_p
8938 && it->bidi_it.scan_dir == -1))
8939 skip = MOVE_POS_MATCH_OR_ZV;
8940 else
8941 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8943 switch (skip)
8945 case MOVE_POS_MATCH_OR_ZV:
8946 reached = 8;
8947 goto out;
8949 case MOVE_NEWLINE_OR_CR:
8950 set_iterator_to_next (it, 1);
8951 it->continuation_lines_width = 0;
8952 break;
8954 case MOVE_LINE_TRUNCATED:
8955 it->continuation_lines_width = 0;
8956 reseat_at_next_visible_line_start (it, 0);
8957 if ((op & MOVE_TO_POS) != 0
8958 && IT_CHARPOS (*it) > to_charpos)
8960 reached = 9;
8961 goto out;
8963 break;
8965 case MOVE_LINE_CONTINUED:
8966 /* For continued lines ending in a tab, some of the glyphs
8967 associated with the tab are displayed on the current
8968 line. Since it->current_x does not include these glyphs,
8969 we use it->last_visible_x instead. */
8970 if (it->c == '\t')
8972 it->continuation_lines_width += it->last_visible_x;
8973 /* When moving by vpos, ensure that the iterator really
8974 advances to the next line (bug#847, bug#969). Fixme:
8975 do we need to do this in other circumstances? */
8976 if (it->current_x != it->last_visible_x
8977 && (op & MOVE_TO_VPOS)
8978 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8980 line_start_x = it->current_x + it->pixel_width
8981 - it->last_visible_x;
8982 set_iterator_to_next (it, 0);
8985 else
8986 it->continuation_lines_width += it->current_x;
8987 break;
8989 default:
8990 emacs_abort ();
8993 /* Reset/increment for the next run. */
8994 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8995 it->current_x = line_start_x;
8996 line_start_x = 0;
8997 it->hpos = 0;
8998 it->current_y += it->max_ascent + it->max_descent;
8999 ++it->vpos;
9000 last_height = it->max_ascent + it->max_descent;
9001 it->max_ascent = it->max_descent = 0;
9004 out:
9006 /* On text terminals, we may stop at the end of a line in the middle
9007 of a multi-character glyph. If the glyph itself is continued,
9008 i.e. it is actually displayed on the next line, don't treat this
9009 stopping point as valid; move to the next line instead (unless
9010 that brings us offscreen). */
9011 if (!FRAME_WINDOW_P (it->f)
9012 && op & MOVE_TO_POS
9013 && IT_CHARPOS (*it) == to_charpos
9014 && it->what == IT_CHARACTER
9015 && it->nglyphs > 1
9016 && it->line_wrap == WINDOW_WRAP
9017 && it->current_x == it->last_visible_x - 1
9018 && it->c != '\n'
9019 && it->c != '\t'
9020 && it->vpos < XFASTINT (it->w->window_end_vpos))
9022 it->continuation_lines_width += it->current_x;
9023 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9024 it->current_y += it->max_ascent + it->max_descent;
9025 ++it->vpos;
9026 last_height = it->max_ascent + it->max_descent;
9029 if (backup_data)
9030 bidi_unshelve_cache (backup_data, 1);
9032 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9036 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9038 If DY > 0, move IT backward at least that many pixels. DY = 0
9039 means move IT backward to the preceding line start or BEGV. This
9040 function may move over more than DY pixels if IT->current_y - DY
9041 ends up in the middle of a line; in this case IT->current_y will be
9042 set to the top of the line moved to. */
9044 void
9045 move_it_vertically_backward (struct it *it, int dy)
9047 int nlines, h;
9048 struct it it2, it3;
9049 void *it2data = NULL, *it3data = NULL;
9050 ptrdiff_t start_pos;
9051 int nchars_per_row
9052 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9053 ptrdiff_t pos_limit;
9055 move_further_back:
9056 eassert (dy >= 0);
9058 start_pos = IT_CHARPOS (*it);
9060 /* Estimate how many newlines we must move back. */
9061 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9062 if (it->line_wrap == TRUNCATE)
9063 pos_limit = BEGV;
9064 else
9065 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9067 /* Set the iterator's position that many lines back. But don't go
9068 back more than NLINES full screen lines -- this wins a day with
9069 buffers which have very long lines. */
9070 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9071 back_to_previous_visible_line_start (it);
9073 /* Reseat the iterator here. When moving backward, we don't want
9074 reseat to skip forward over invisible text, set up the iterator
9075 to deliver from overlay strings at the new position etc. So,
9076 use reseat_1 here. */
9077 reseat_1 (it, it->current.pos, 1);
9079 /* We are now surely at a line start. */
9080 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9081 reordering is in effect. */
9082 it->continuation_lines_width = 0;
9084 /* Move forward and see what y-distance we moved. First move to the
9085 start of the next line so that we get its height. We need this
9086 height to be able to tell whether we reached the specified
9087 y-distance. */
9088 SAVE_IT (it2, *it, it2data);
9089 it2.max_ascent = it2.max_descent = 0;
9092 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9093 MOVE_TO_POS | MOVE_TO_VPOS);
9095 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9096 /* If we are in a display string which starts at START_POS,
9097 and that display string includes a newline, and we are
9098 right after that newline (i.e. at the beginning of a
9099 display line), exit the loop, because otherwise we will
9100 infloop, since move_it_to will see that it is already at
9101 START_POS and will not move. */
9102 || (it2.method == GET_FROM_STRING
9103 && IT_CHARPOS (it2) == start_pos
9104 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9105 eassert (IT_CHARPOS (*it) >= BEGV);
9106 SAVE_IT (it3, it2, it3data);
9108 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9109 eassert (IT_CHARPOS (*it) >= BEGV);
9110 /* H is the actual vertical distance from the position in *IT
9111 and the starting position. */
9112 h = it2.current_y - it->current_y;
9113 /* NLINES is the distance in number of lines. */
9114 nlines = it2.vpos - it->vpos;
9116 /* Correct IT's y and vpos position
9117 so that they are relative to the starting point. */
9118 it->vpos -= nlines;
9119 it->current_y -= h;
9121 if (dy == 0)
9123 /* DY == 0 means move to the start of the screen line. The
9124 value of nlines is > 0 if continuation lines were involved,
9125 or if the original IT position was at start of a line. */
9126 RESTORE_IT (it, it, it2data);
9127 if (nlines > 0)
9128 move_it_by_lines (it, nlines);
9129 /* The above code moves us to some position NLINES down,
9130 usually to its first glyph (leftmost in an L2R line), but
9131 that's not necessarily the start of the line, under bidi
9132 reordering. We want to get to the character position
9133 that is immediately after the newline of the previous
9134 line. */
9135 if (it->bidi_p
9136 && !it->continuation_lines_width
9137 && !STRINGP (it->string)
9138 && IT_CHARPOS (*it) > BEGV
9139 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9141 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9143 DEC_BOTH (cp, bp);
9144 cp = find_newline_no_quit (cp, bp, -1, NULL);
9145 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9147 bidi_unshelve_cache (it3data, 1);
9149 else
9151 /* The y-position we try to reach, relative to *IT.
9152 Note that H has been subtracted in front of the if-statement. */
9153 int target_y = it->current_y + h - dy;
9154 int y0 = it3.current_y;
9155 int y1;
9156 int line_height;
9158 RESTORE_IT (&it3, &it3, it3data);
9159 y1 = line_bottom_y (&it3);
9160 line_height = y1 - y0;
9161 RESTORE_IT (it, it, it2data);
9162 /* If we did not reach target_y, try to move further backward if
9163 we can. If we moved too far backward, try to move forward. */
9164 if (target_y < it->current_y
9165 /* This is heuristic. In a window that's 3 lines high, with
9166 a line height of 13 pixels each, recentering with point
9167 on the bottom line will try to move -39/2 = 19 pixels
9168 backward. Try to avoid moving into the first line. */
9169 && (it->current_y - target_y
9170 > min (window_box_height (it->w), line_height * 2 / 3))
9171 && IT_CHARPOS (*it) > BEGV)
9173 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9174 target_y - it->current_y));
9175 dy = it->current_y - target_y;
9176 goto move_further_back;
9178 else if (target_y >= it->current_y + line_height
9179 && IT_CHARPOS (*it) < ZV)
9181 /* Should move forward by at least one line, maybe more.
9183 Note: Calling move_it_by_lines can be expensive on
9184 terminal frames, where compute_motion is used (via
9185 vmotion) to do the job, when there are very long lines
9186 and truncate-lines is nil. That's the reason for
9187 treating terminal frames specially here. */
9189 if (!FRAME_WINDOW_P (it->f))
9190 move_it_vertically (it, target_y - (it->current_y + line_height));
9191 else
9195 move_it_by_lines (it, 1);
9197 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9204 /* Move IT by a specified amount of pixel lines DY. DY negative means
9205 move backwards. DY = 0 means move to start of screen line. At the
9206 end, IT will be on the start of a screen line. */
9208 void
9209 move_it_vertically (struct it *it, int dy)
9211 if (dy <= 0)
9212 move_it_vertically_backward (it, -dy);
9213 else
9215 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9216 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9217 MOVE_TO_POS | MOVE_TO_Y);
9218 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9220 /* If buffer ends in ZV without a newline, move to the start of
9221 the line to satisfy the post-condition. */
9222 if (IT_CHARPOS (*it) == ZV
9223 && ZV > BEGV
9224 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9225 move_it_by_lines (it, 0);
9230 /* Move iterator IT past the end of the text line it is in. */
9232 void
9233 move_it_past_eol (struct it *it)
9235 enum move_it_result rc;
9237 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9238 if (rc == MOVE_NEWLINE_OR_CR)
9239 set_iterator_to_next (it, 0);
9243 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9244 negative means move up. DVPOS == 0 means move to the start of the
9245 screen line.
9247 Optimization idea: If we would know that IT->f doesn't use
9248 a face with proportional font, we could be faster for
9249 truncate-lines nil. */
9251 void
9252 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9255 /* The commented-out optimization uses vmotion on terminals. This
9256 gives bad results, because elements like it->what, on which
9257 callers such as pos_visible_p rely, aren't updated. */
9258 /* struct position pos;
9259 if (!FRAME_WINDOW_P (it->f))
9261 struct text_pos textpos;
9263 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9264 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9265 reseat (it, textpos, 1);
9266 it->vpos += pos.vpos;
9267 it->current_y += pos.vpos;
9269 else */
9271 if (dvpos == 0)
9273 /* DVPOS == 0 means move to the start of the screen line. */
9274 move_it_vertically_backward (it, 0);
9275 /* Let next call to line_bottom_y calculate real line height */
9276 last_height = 0;
9278 else if (dvpos > 0)
9280 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9281 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9283 /* Only move to the next buffer position if we ended up in a
9284 string from display property, not in an overlay string
9285 (before-string or after-string). That is because the
9286 latter don't conceal the underlying buffer position, so
9287 we can ask to move the iterator to the exact position we
9288 are interested in. Note that, even if we are already at
9289 IT_CHARPOS (*it), the call below is not a no-op, as it
9290 will detect that we are at the end of the string, pop the
9291 iterator, and compute it->current_x and it->hpos
9292 correctly. */
9293 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9294 -1, -1, -1, MOVE_TO_POS);
9297 else
9299 struct it it2;
9300 void *it2data = NULL;
9301 ptrdiff_t start_charpos, i;
9302 int nchars_per_row
9303 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9304 ptrdiff_t pos_limit;
9306 /* Start at the beginning of the screen line containing IT's
9307 position. This may actually move vertically backwards,
9308 in case of overlays, so adjust dvpos accordingly. */
9309 dvpos += it->vpos;
9310 move_it_vertically_backward (it, 0);
9311 dvpos -= it->vpos;
9313 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9314 screen lines, and reseat the iterator there. */
9315 start_charpos = IT_CHARPOS (*it);
9316 if (it->line_wrap == TRUNCATE)
9317 pos_limit = BEGV;
9318 else
9319 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9320 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9321 back_to_previous_visible_line_start (it);
9322 reseat (it, it->current.pos, 1);
9324 /* Move further back if we end up in a string or an image. */
9325 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9327 /* First try to move to start of display line. */
9328 dvpos += it->vpos;
9329 move_it_vertically_backward (it, 0);
9330 dvpos -= it->vpos;
9331 if (IT_POS_VALID_AFTER_MOVE_P (it))
9332 break;
9333 /* If start of line is still in string or image,
9334 move further back. */
9335 back_to_previous_visible_line_start (it);
9336 reseat (it, it->current.pos, 1);
9337 dvpos--;
9340 it->current_x = it->hpos = 0;
9342 /* Above call may have moved too far if continuation lines
9343 are involved. Scan forward and see if it did. */
9344 SAVE_IT (it2, *it, it2data);
9345 it2.vpos = it2.current_y = 0;
9346 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9347 it->vpos -= it2.vpos;
9348 it->current_y -= it2.current_y;
9349 it->current_x = it->hpos = 0;
9351 /* If we moved too far back, move IT some lines forward. */
9352 if (it2.vpos > -dvpos)
9354 int delta = it2.vpos + dvpos;
9356 RESTORE_IT (&it2, &it2, it2data);
9357 SAVE_IT (it2, *it, it2data);
9358 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9359 /* Move back again if we got too far ahead. */
9360 if (IT_CHARPOS (*it) >= start_charpos)
9361 RESTORE_IT (it, &it2, it2data);
9362 else
9363 bidi_unshelve_cache (it2data, 1);
9365 else
9366 RESTORE_IT (it, it, it2data);
9370 /* Return 1 if IT points into the middle of a display vector. */
9373 in_display_vector_p (struct it *it)
9375 return (it->method == GET_FROM_DISPLAY_VECTOR
9376 && it->current.dpvec_index > 0
9377 && it->dpvec + it->current.dpvec_index != it->dpend);
9381 /***********************************************************************
9382 Messages
9383 ***********************************************************************/
9386 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9387 to *Messages*. */
9389 void
9390 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9392 Lisp_Object args[3];
9393 Lisp_Object msg, fmt;
9394 char *buffer;
9395 ptrdiff_t len;
9396 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9397 USE_SAFE_ALLOCA;
9399 fmt = msg = Qnil;
9400 GCPRO4 (fmt, msg, arg1, arg2);
9402 args[0] = fmt = build_string (format);
9403 args[1] = arg1;
9404 args[2] = arg2;
9405 msg = Fformat (3, args);
9407 len = SBYTES (msg) + 1;
9408 buffer = SAFE_ALLOCA (len);
9409 memcpy (buffer, SDATA (msg), len);
9411 message_dolog (buffer, len - 1, 1, 0);
9412 SAFE_FREE ();
9414 UNGCPRO;
9418 /* Output a newline in the *Messages* buffer if "needs" one. */
9420 void
9421 message_log_maybe_newline (void)
9423 if (message_log_need_newline)
9424 message_dolog ("", 0, 1, 0);
9428 /* Add a string M of length NBYTES to the message log, optionally
9429 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9430 true, means interpret the contents of M as multibyte. This
9431 function calls low-level routines in order to bypass text property
9432 hooks, etc. which might not be safe to run.
9434 This may GC (insert may run before/after change hooks),
9435 so the buffer M must NOT point to a Lisp string. */
9437 void
9438 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9440 const unsigned char *msg = (const unsigned char *) m;
9442 if (!NILP (Vmemory_full))
9443 return;
9445 if (!NILP (Vmessage_log_max))
9447 struct buffer *oldbuf;
9448 Lisp_Object oldpoint, oldbegv, oldzv;
9449 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9450 ptrdiff_t point_at_end = 0;
9451 ptrdiff_t zv_at_end = 0;
9452 Lisp_Object old_deactivate_mark;
9453 bool shown;
9454 struct gcpro gcpro1;
9456 old_deactivate_mark = Vdeactivate_mark;
9457 oldbuf = current_buffer;
9458 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9459 bset_undo_list (current_buffer, Qt);
9461 oldpoint = message_dolog_marker1;
9462 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9463 oldbegv = message_dolog_marker2;
9464 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9465 oldzv = message_dolog_marker3;
9466 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9467 GCPRO1 (old_deactivate_mark);
9469 if (PT == Z)
9470 point_at_end = 1;
9471 if (ZV == Z)
9472 zv_at_end = 1;
9474 BEGV = BEG;
9475 BEGV_BYTE = BEG_BYTE;
9476 ZV = Z;
9477 ZV_BYTE = Z_BYTE;
9478 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9480 /* Insert the string--maybe converting multibyte to single byte
9481 or vice versa, so that all the text fits the buffer. */
9482 if (multibyte
9483 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9485 ptrdiff_t i;
9486 int c, char_bytes;
9487 char work[1];
9489 /* Convert a multibyte string to single-byte
9490 for the *Message* buffer. */
9491 for (i = 0; i < nbytes; i += char_bytes)
9493 c = string_char_and_length (msg + i, &char_bytes);
9494 work[0] = (ASCII_CHAR_P (c)
9496 : multibyte_char_to_unibyte (c));
9497 insert_1_both (work, 1, 1, 1, 0, 0);
9500 else if (! multibyte
9501 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9503 ptrdiff_t i;
9504 int c, char_bytes;
9505 unsigned char str[MAX_MULTIBYTE_LENGTH];
9506 /* Convert a single-byte string to multibyte
9507 for the *Message* buffer. */
9508 for (i = 0; i < nbytes; i++)
9510 c = msg[i];
9511 MAKE_CHAR_MULTIBYTE (c);
9512 char_bytes = CHAR_STRING (c, str);
9513 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9516 else if (nbytes)
9517 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9519 if (nlflag)
9521 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9522 printmax_t dups;
9524 insert_1_both ("\n", 1, 1, 1, 0, 0);
9526 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9527 this_bol = PT;
9528 this_bol_byte = PT_BYTE;
9530 /* See if this line duplicates the previous one.
9531 If so, combine duplicates. */
9532 if (this_bol > BEG)
9534 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9535 prev_bol = PT;
9536 prev_bol_byte = PT_BYTE;
9538 dups = message_log_check_duplicate (prev_bol_byte,
9539 this_bol_byte);
9540 if (dups)
9542 del_range_both (prev_bol, prev_bol_byte,
9543 this_bol, this_bol_byte, 0);
9544 if (dups > 1)
9546 char dupstr[sizeof " [ times]"
9547 + INT_STRLEN_BOUND (printmax_t)];
9549 /* If you change this format, don't forget to also
9550 change message_log_check_duplicate. */
9551 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9552 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9553 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9558 /* If we have more than the desired maximum number of lines
9559 in the *Messages* buffer now, delete the oldest ones.
9560 This is safe because we don't have undo in this buffer. */
9562 if (NATNUMP (Vmessage_log_max))
9564 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9565 -XFASTINT (Vmessage_log_max) - 1, 0);
9566 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9569 BEGV = marker_position (oldbegv);
9570 BEGV_BYTE = marker_byte_position (oldbegv);
9572 if (zv_at_end)
9574 ZV = Z;
9575 ZV_BYTE = Z_BYTE;
9577 else
9579 ZV = marker_position (oldzv);
9580 ZV_BYTE = marker_byte_position (oldzv);
9583 if (point_at_end)
9584 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9585 else
9586 /* We can't do Fgoto_char (oldpoint) because it will run some
9587 Lisp code. */
9588 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9589 marker_byte_position (oldpoint));
9591 UNGCPRO;
9592 unchain_marker (XMARKER (oldpoint));
9593 unchain_marker (XMARKER (oldbegv));
9594 unchain_marker (XMARKER (oldzv));
9596 shown = buffer_window_count (current_buffer) > 0;
9597 set_buffer_internal (oldbuf);
9598 /* We called insert_1_both above with its 5th argument (PREPARE)
9599 zero, which prevents insert_1_both from calling
9600 prepare_to_modify_buffer, which in turns prevents us from
9601 incrementing windows_or_buffers_changed even if *Messages* is
9602 shown in some window. So we must manually incrementing
9603 windows_or_buffers_changed here to make up for that. */
9604 if (shown)
9605 windows_or_buffers_changed++;
9606 else
9607 windows_or_buffers_changed = old_windows_or_buffers_changed;
9608 message_log_need_newline = !nlflag;
9609 Vdeactivate_mark = old_deactivate_mark;
9614 /* We are at the end of the buffer after just having inserted a newline.
9615 (Note: We depend on the fact we won't be crossing the gap.)
9616 Check to see if the most recent message looks a lot like the previous one.
9617 Return 0 if different, 1 if the new one should just replace it, or a
9618 value N > 1 if we should also append " [N times]". */
9620 static intmax_t
9621 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9623 ptrdiff_t i;
9624 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9625 int seen_dots = 0;
9626 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9627 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9629 for (i = 0; i < len; i++)
9631 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9632 seen_dots = 1;
9633 if (p1[i] != p2[i])
9634 return seen_dots;
9636 p1 += len;
9637 if (*p1 == '\n')
9638 return 2;
9639 if (*p1++ == ' ' && *p1++ == '[')
9641 char *pend;
9642 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9643 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9644 return n + 1;
9646 return 0;
9650 /* Display an echo area message M with a specified length of NBYTES
9651 bytes. The string may include null characters. If M is not a
9652 string, clear out any existing message, and let the mini-buffer
9653 text show through.
9655 This function cancels echoing. */
9657 void
9658 message3 (Lisp_Object m)
9660 struct gcpro gcpro1;
9662 GCPRO1 (m);
9663 clear_message (1,1);
9664 cancel_echoing ();
9666 /* First flush out any partial line written with print. */
9667 message_log_maybe_newline ();
9668 if (STRINGP (m))
9670 ptrdiff_t nbytes = SBYTES (m);
9671 bool multibyte = STRING_MULTIBYTE (m);
9672 USE_SAFE_ALLOCA;
9673 char *buffer = SAFE_ALLOCA (nbytes);
9674 memcpy (buffer, SDATA (m), nbytes);
9675 message_dolog (buffer, nbytes, 1, multibyte);
9676 SAFE_FREE ();
9678 message3_nolog (m);
9680 UNGCPRO;
9684 /* The non-logging version of message3.
9685 This does not cancel echoing, because it is used for echoing.
9686 Perhaps we need to make a separate function for echoing
9687 and make this cancel echoing. */
9689 void
9690 message3_nolog (Lisp_Object m)
9692 struct frame *sf = SELECTED_FRAME ();
9694 if (FRAME_INITIAL_P (sf))
9696 if (noninteractive_need_newline)
9697 putc ('\n', stderr);
9698 noninteractive_need_newline = 0;
9699 if (STRINGP (m))
9700 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9701 if (cursor_in_echo_area == 0)
9702 fprintf (stderr, "\n");
9703 fflush (stderr);
9705 /* Error messages get reported properly by cmd_error, so this must be just an
9706 informative message; if the frame hasn't really been initialized yet, just
9707 toss it. */
9708 else if (INTERACTIVE && sf->glyphs_initialized_p)
9710 /* Get the frame containing the mini-buffer
9711 that the selected frame is using. */
9712 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9713 Lisp_Object frame = XWINDOW (mini_window)->frame;
9714 struct frame *f = XFRAME (frame);
9716 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9717 Fmake_frame_visible (frame);
9719 if (STRINGP (m) && SCHARS (m) > 0)
9721 set_message (m);
9722 if (minibuffer_auto_raise)
9723 Fraise_frame (frame);
9724 /* Assume we are not echoing.
9725 (If we are, echo_now will override this.) */
9726 echo_message_buffer = Qnil;
9728 else
9729 clear_message (1, 1);
9731 do_pending_window_change (0);
9732 echo_area_display (1);
9733 do_pending_window_change (0);
9734 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9735 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9740 /* Display a null-terminated echo area message M. If M is 0, clear
9741 out any existing message, and let the mini-buffer text show through.
9743 The buffer M must continue to exist until after the echo area gets
9744 cleared or some other message gets displayed there. Do not pass
9745 text that is stored in a Lisp string. Do not pass text in a buffer
9746 that was alloca'd. */
9748 void
9749 message1 (const char *m)
9751 message3 (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9755 /* The non-logging counterpart of message1. */
9757 void
9758 message1_nolog (const char *m)
9760 message3_nolog (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9763 /* Display a message M which contains a single %s
9764 which gets replaced with STRING. */
9766 void
9767 message_with_string (const char *m, Lisp_Object string, int log)
9769 CHECK_STRING (string);
9771 if (noninteractive)
9773 if (m)
9775 if (noninteractive_need_newline)
9776 putc ('\n', stderr);
9777 noninteractive_need_newline = 0;
9778 fprintf (stderr, m, SDATA (string));
9779 if (!cursor_in_echo_area)
9780 fprintf (stderr, "\n");
9781 fflush (stderr);
9784 else if (INTERACTIVE)
9786 /* The frame whose minibuffer we're going to display the message on.
9787 It may be larger than the selected frame, so we need
9788 to use its buffer, not the selected frame's buffer. */
9789 Lisp_Object mini_window;
9790 struct frame *f, *sf = SELECTED_FRAME ();
9792 /* Get the frame containing the minibuffer
9793 that the selected frame is using. */
9794 mini_window = FRAME_MINIBUF_WINDOW (sf);
9795 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9797 /* Error messages get reported properly by cmd_error, so this must be
9798 just an informative message; if the frame hasn't really been
9799 initialized yet, just toss it. */
9800 if (f->glyphs_initialized_p)
9802 Lisp_Object args[2], msg;
9803 struct gcpro gcpro1, gcpro2;
9805 args[0] = build_string (m);
9806 args[1] = msg = string;
9807 GCPRO2 (args[0], msg);
9808 gcpro1.nvars = 2;
9810 msg = Fformat (2, args);
9812 if (log)
9813 message3 (msg);
9814 else
9815 message3_nolog (msg);
9817 UNGCPRO;
9819 /* Print should start at the beginning of the message
9820 buffer next time. */
9821 message_buf_print = 0;
9827 /* Dump an informative message to the minibuf. If M is 0, clear out
9828 any existing message, and let the mini-buffer text show through. */
9830 static void
9831 vmessage (const char *m, va_list ap)
9833 if (noninteractive)
9835 if (m)
9837 if (noninteractive_need_newline)
9838 putc ('\n', stderr);
9839 noninteractive_need_newline = 0;
9840 vfprintf (stderr, m, ap);
9841 if (cursor_in_echo_area == 0)
9842 fprintf (stderr, "\n");
9843 fflush (stderr);
9846 else if (INTERACTIVE)
9848 /* The frame whose mini-buffer we're going to display the message
9849 on. It may be larger than the selected frame, so we need to
9850 use its buffer, not the selected frame's buffer. */
9851 Lisp_Object mini_window;
9852 struct frame *f, *sf = SELECTED_FRAME ();
9854 /* Get the frame containing the mini-buffer
9855 that the selected frame is using. */
9856 mini_window = FRAME_MINIBUF_WINDOW (sf);
9857 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9859 /* Error messages get reported properly by cmd_error, so this must be
9860 just an informative message; if the frame hasn't really been
9861 initialized yet, just toss it. */
9862 if (f->glyphs_initialized_p)
9864 if (m)
9866 ptrdiff_t len;
9867 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9868 char *message_buf = alloca (maxsize + 1);
9870 len = doprnt (message_buf, maxsize, m, (char *)0, ap);
9872 message3 (make_string (message_buf, len));
9874 else
9875 message1 (0);
9877 /* Print should start at the beginning of the message
9878 buffer next time. */
9879 message_buf_print = 0;
9884 void
9885 message (const char *m, ...)
9887 va_list ap;
9888 va_start (ap, m);
9889 vmessage (m, ap);
9890 va_end (ap);
9894 #if 0
9895 /* The non-logging version of message. */
9897 void
9898 message_nolog (const char *m, ...)
9900 Lisp_Object old_log_max;
9901 va_list ap;
9902 va_start (ap, m);
9903 old_log_max = Vmessage_log_max;
9904 Vmessage_log_max = Qnil;
9905 vmessage (m, ap);
9906 Vmessage_log_max = old_log_max;
9907 va_end (ap);
9909 #endif
9912 /* Display the current message in the current mini-buffer. This is
9913 only called from error handlers in process.c, and is not time
9914 critical. */
9916 void
9917 update_echo_area (void)
9919 if (!NILP (echo_area_buffer[0]))
9921 Lisp_Object string;
9922 string = Fcurrent_message ();
9923 message3 (string);
9928 /* Make sure echo area buffers in `echo_buffers' are live.
9929 If they aren't, make new ones. */
9931 static void
9932 ensure_echo_area_buffers (void)
9934 int i;
9936 for (i = 0; i < 2; ++i)
9937 if (!BUFFERP (echo_buffer[i])
9938 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9940 char name[30];
9941 Lisp_Object old_buffer;
9942 int j;
9944 old_buffer = echo_buffer[i];
9945 echo_buffer[i] = Fget_buffer_create
9946 (make_formatted_string (name, " *Echo Area %d*", i));
9947 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9948 /* to force word wrap in echo area -
9949 it was decided to postpone this*/
9950 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9952 for (j = 0; j < 2; ++j)
9953 if (EQ (old_buffer, echo_area_buffer[j]))
9954 echo_area_buffer[j] = echo_buffer[i];
9959 /* Call FN with args A1..A2 with either the current or last displayed
9960 echo_area_buffer as current buffer.
9962 WHICH zero means use the current message buffer
9963 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9964 from echo_buffer[] and clear it.
9966 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9967 suitable buffer from echo_buffer[] and clear it.
9969 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9970 that the current message becomes the last displayed one, make
9971 choose a suitable buffer for echo_area_buffer[0], and clear it.
9973 Value is what FN returns. */
9975 static int
9976 with_echo_area_buffer (struct window *w, int which,
9977 int (*fn) (ptrdiff_t, Lisp_Object),
9978 ptrdiff_t a1, Lisp_Object a2)
9980 Lisp_Object buffer;
9981 int this_one, the_other, clear_buffer_p, rc;
9982 ptrdiff_t count = SPECPDL_INDEX ();
9984 /* If buffers aren't live, make new ones. */
9985 ensure_echo_area_buffers ();
9987 clear_buffer_p = 0;
9989 if (which == 0)
9990 this_one = 0, the_other = 1;
9991 else if (which > 0)
9992 this_one = 1, the_other = 0;
9993 else
9995 this_one = 0, the_other = 1;
9996 clear_buffer_p = 1;
9998 /* We need a fresh one in case the current echo buffer equals
9999 the one containing the last displayed echo area message. */
10000 if (!NILP (echo_area_buffer[this_one])
10001 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10002 echo_area_buffer[this_one] = Qnil;
10005 /* Choose a suitable buffer from echo_buffer[] is we don't
10006 have one. */
10007 if (NILP (echo_area_buffer[this_one]))
10009 echo_area_buffer[this_one]
10010 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10011 ? echo_buffer[the_other]
10012 : echo_buffer[this_one]);
10013 clear_buffer_p = 1;
10016 buffer = echo_area_buffer[this_one];
10018 /* Don't get confused by reusing the buffer used for echoing
10019 for a different purpose. */
10020 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10021 cancel_echoing ();
10023 record_unwind_protect (unwind_with_echo_area_buffer,
10024 with_echo_area_buffer_unwind_data (w));
10026 /* Make the echo area buffer current. Note that for display
10027 purposes, it is not necessary that the displayed window's buffer
10028 == current_buffer, except for text property lookup. So, let's
10029 only set that buffer temporarily here without doing a full
10030 Fset_window_buffer. We must also change w->pointm, though,
10031 because otherwise an assertions in unshow_buffer fails, and Emacs
10032 aborts. */
10033 set_buffer_internal_1 (XBUFFER (buffer));
10034 if (w)
10036 wset_buffer (w, buffer);
10037 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10040 bset_undo_list (current_buffer, Qt);
10041 bset_read_only (current_buffer, Qnil);
10042 specbind (Qinhibit_read_only, Qt);
10043 specbind (Qinhibit_modification_hooks, Qt);
10045 if (clear_buffer_p && Z > BEG)
10046 del_range (BEG, Z);
10048 eassert (BEGV >= BEG);
10049 eassert (ZV <= Z && ZV >= BEGV);
10051 rc = fn (a1, a2);
10053 eassert (BEGV >= BEG);
10054 eassert (ZV <= Z && ZV >= BEGV);
10056 unbind_to (count, Qnil);
10057 return rc;
10061 /* Save state that should be preserved around the call to the function
10062 FN called in with_echo_area_buffer. */
10064 static Lisp_Object
10065 with_echo_area_buffer_unwind_data (struct window *w)
10067 int i = 0;
10068 Lisp_Object vector, tmp;
10070 /* Reduce consing by keeping one vector in
10071 Vwith_echo_area_save_vector. */
10072 vector = Vwith_echo_area_save_vector;
10073 Vwith_echo_area_save_vector = Qnil;
10075 if (NILP (vector))
10076 vector = Fmake_vector (make_number (9), Qnil);
10078 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10079 ASET (vector, i, Vdeactivate_mark); ++i;
10080 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10082 if (w)
10084 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10085 ASET (vector, i, w->contents); ++i;
10086 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10087 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10088 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10089 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10091 else
10093 int end = i + 6;
10094 for (; i < end; ++i)
10095 ASET (vector, i, Qnil);
10098 eassert (i == ASIZE (vector));
10099 return vector;
10103 /* Restore global state from VECTOR which was created by
10104 with_echo_area_buffer_unwind_data. */
10106 static Lisp_Object
10107 unwind_with_echo_area_buffer (Lisp_Object vector)
10109 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10110 Vdeactivate_mark = AREF (vector, 1);
10111 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10113 if (WINDOWP (AREF (vector, 3)))
10115 struct window *w;
10116 Lisp_Object buffer;
10118 w = XWINDOW (AREF (vector, 3));
10119 buffer = AREF (vector, 4);
10121 wset_buffer (w, buffer);
10122 set_marker_both (w->pointm, buffer,
10123 XFASTINT (AREF (vector, 5)),
10124 XFASTINT (AREF (vector, 6)));
10125 set_marker_both (w->start, buffer,
10126 XFASTINT (AREF (vector, 7)),
10127 XFASTINT (AREF (vector, 8)));
10130 Vwith_echo_area_save_vector = vector;
10131 return Qnil;
10135 /* Set up the echo area for use by print functions. MULTIBYTE_P
10136 non-zero means we will print multibyte. */
10138 void
10139 setup_echo_area_for_printing (int multibyte_p)
10141 /* If we can't find an echo area any more, exit. */
10142 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10143 Fkill_emacs (Qnil);
10145 ensure_echo_area_buffers ();
10147 if (!message_buf_print)
10149 /* A message has been output since the last time we printed.
10150 Choose a fresh echo area buffer. */
10151 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10152 echo_area_buffer[0] = echo_buffer[1];
10153 else
10154 echo_area_buffer[0] = echo_buffer[0];
10156 /* Switch to that buffer and clear it. */
10157 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10158 bset_truncate_lines (current_buffer, Qnil);
10160 if (Z > BEG)
10162 ptrdiff_t count = SPECPDL_INDEX ();
10163 specbind (Qinhibit_read_only, Qt);
10164 /* Note that undo recording is always disabled. */
10165 del_range (BEG, Z);
10166 unbind_to (count, Qnil);
10168 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10170 /* Set up the buffer for the multibyteness we need. */
10171 if (multibyte_p
10172 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10173 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10175 /* Raise the frame containing the echo area. */
10176 if (minibuffer_auto_raise)
10178 struct frame *sf = SELECTED_FRAME ();
10179 Lisp_Object mini_window;
10180 mini_window = FRAME_MINIBUF_WINDOW (sf);
10181 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10184 message_log_maybe_newline ();
10185 message_buf_print = 1;
10187 else
10189 if (NILP (echo_area_buffer[0]))
10191 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10192 echo_area_buffer[0] = echo_buffer[1];
10193 else
10194 echo_area_buffer[0] = echo_buffer[0];
10197 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10199 /* Someone switched buffers between print requests. */
10200 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10201 bset_truncate_lines (current_buffer, Qnil);
10207 /* Display an echo area message in window W. Value is non-zero if W's
10208 height is changed. If display_last_displayed_message_p is
10209 non-zero, display the message that was last displayed, otherwise
10210 display the current message. */
10212 static int
10213 display_echo_area (struct window *w)
10215 int i, no_message_p, window_height_changed_p;
10217 /* Temporarily disable garbage collections while displaying the echo
10218 area. This is done because a GC can print a message itself.
10219 That message would modify the echo area buffer's contents while a
10220 redisplay of the buffer is going on, and seriously confuse
10221 redisplay. */
10222 ptrdiff_t count = inhibit_garbage_collection ();
10224 /* If there is no message, we must call display_echo_area_1
10225 nevertheless because it resizes the window. But we will have to
10226 reset the echo_area_buffer in question to nil at the end because
10227 with_echo_area_buffer will sets it to an empty buffer. */
10228 i = display_last_displayed_message_p ? 1 : 0;
10229 no_message_p = NILP (echo_area_buffer[i]);
10231 window_height_changed_p
10232 = with_echo_area_buffer (w, display_last_displayed_message_p,
10233 display_echo_area_1,
10234 (intptr_t) w, Qnil);
10236 if (no_message_p)
10237 echo_area_buffer[i] = Qnil;
10239 unbind_to (count, Qnil);
10240 return window_height_changed_p;
10244 /* Helper for display_echo_area. Display the current buffer which
10245 contains the current echo area message in window W, a mini-window,
10246 a pointer to which is passed in A1. A2..A4 are currently not used.
10247 Change the height of W so that all of the message is displayed.
10248 Value is non-zero if height of W was changed. */
10250 static int
10251 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10253 intptr_t i1 = a1;
10254 struct window *w = (struct window *) i1;
10255 Lisp_Object window;
10256 struct text_pos start;
10257 int window_height_changed_p = 0;
10259 /* Do this before displaying, so that we have a large enough glyph
10260 matrix for the display. If we can't get enough space for the
10261 whole text, display the last N lines. That works by setting w->start. */
10262 window_height_changed_p = resize_mini_window (w, 0);
10264 /* Use the starting position chosen by resize_mini_window. */
10265 SET_TEXT_POS_FROM_MARKER (start, w->start);
10267 /* Display. */
10268 clear_glyph_matrix (w->desired_matrix);
10269 XSETWINDOW (window, w);
10270 try_window (window, start, 0);
10272 return window_height_changed_p;
10276 /* Resize the echo area window to exactly the size needed for the
10277 currently displayed message, if there is one. If a mini-buffer
10278 is active, don't shrink it. */
10280 void
10281 resize_echo_area_exactly (void)
10283 if (BUFFERP (echo_area_buffer[0])
10284 && WINDOWP (echo_area_window))
10286 struct window *w = XWINDOW (echo_area_window);
10287 int resized_p;
10288 Lisp_Object resize_exactly;
10290 if (minibuf_level == 0)
10291 resize_exactly = Qt;
10292 else
10293 resize_exactly = Qnil;
10295 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10296 (intptr_t) w, resize_exactly);
10297 if (resized_p)
10299 ++windows_or_buffers_changed;
10300 ++update_mode_lines;
10301 redisplay_internal ();
10307 /* Callback function for with_echo_area_buffer, when used from
10308 resize_echo_area_exactly. A1 contains a pointer to the window to
10309 resize, EXACTLY non-nil means resize the mini-window exactly to the
10310 size of the text displayed. A3 and A4 are not used. Value is what
10311 resize_mini_window returns. */
10313 static int
10314 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10316 intptr_t i1 = a1;
10317 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10321 /* Resize mini-window W to fit the size of its contents. EXACT_P
10322 means size the window exactly to the size needed. Otherwise, it's
10323 only enlarged until W's buffer is empty.
10325 Set W->start to the right place to begin display. If the whole
10326 contents fit, start at the beginning. Otherwise, start so as
10327 to make the end of the contents appear. This is particularly
10328 important for y-or-n-p, but seems desirable generally.
10330 Value is non-zero if the window height has been changed. */
10333 resize_mini_window (struct window *w, int exact_p)
10335 struct frame *f = XFRAME (w->frame);
10336 int window_height_changed_p = 0;
10338 eassert (MINI_WINDOW_P (w));
10340 /* By default, start display at the beginning. */
10341 set_marker_both (w->start, w->contents,
10342 BUF_BEGV (XBUFFER (w->contents)),
10343 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10345 /* Don't resize windows while redisplaying a window; it would
10346 confuse redisplay functions when the size of the window they are
10347 displaying changes from under them. Such a resizing can happen,
10348 for instance, when which-func prints a long message while
10349 we are running fontification-functions. We're running these
10350 functions with safe_call which binds inhibit-redisplay to t. */
10351 if (!NILP (Vinhibit_redisplay))
10352 return 0;
10354 /* Nil means don't try to resize. */
10355 if (NILP (Vresize_mini_windows)
10356 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10357 return 0;
10359 if (!FRAME_MINIBUF_ONLY_P (f))
10361 struct it it;
10362 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10363 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10364 int height;
10365 EMACS_INT max_height;
10366 int unit = FRAME_LINE_HEIGHT (f);
10367 struct text_pos start;
10368 struct buffer *old_current_buffer = NULL;
10370 if (current_buffer != XBUFFER (w->contents))
10372 old_current_buffer = current_buffer;
10373 set_buffer_internal (XBUFFER (w->contents));
10376 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10378 /* Compute the max. number of lines specified by the user. */
10379 if (FLOATP (Vmax_mini_window_height))
10380 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10381 else if (INTEGERP (Vmax_mini_window_height))
10382 max_height = XINT (Vmax_mini_window_height);
10383 else
10384 max_height = total_height / 4;
10386 /* Correct that max. height if it's bogus. */
10387 max_height = clip_to_bounds (1, max_height, total_height);
10389 /* Find out the height of the text in the window. */
10390 if (it.line_wrap == TRUNCATE)
10391 height = 1;
10392 else
10394 last_height = 0;
10395 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10396 if (it.max_ascent == 0 && it.max_descent == 0)
10397 height = it.current_y + last_height;
10398 else
10399 height = it.current_y + it.max_ascent + it.max_descent;
10400 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10401 height = (height + unit - 1) / unit;
10404 /* Compute a suitable window start. */
10405 if (height > max_height)
10407 height = max_height;
10408 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10409 move_it_vertically_backward (&it, (height - 1) * unit);
10410 start = it.current.pos;
10412 else
10413 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10414 SET_MARKER_FROM_TEXT_POS (w->start, start);
10416 if (EQ (Vresize_mini_windows, Qgrow_only))
10418 /* Let it grow only, until we display an empty message, in which
10419 case the window shrinks again. */
10420 if (height > WINDOW_TOTAL_LINES (w))
10422 int old_height = WINDOW_TOTAL_LINES (w);
10423 freeze_window_starts (f, 1);
10424 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10425 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10427 else if (height < WINDOW_TOTAL_LINES (w)
10428 && (exact_p || BEGV == ZV))
10430 int old_height = WINDOW_TOTAL_LINES (w);
10431 freeze_window_starts (f, 0);
10432 shrink_mini_window (w);
10433 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10436 else
10438 /* Always resize to exact size needed. */
10439 if (height > WINDOW_TOTAL_LINES (w))
10441 int old_height = WINDOW_TOTAL_LINES (w);
10442 freeze_window_starts (f, 1);
10443 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10444 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10446 else if (height < WINDOW_TOTAL_LINES (w))
10448 int old_height = WINDOW_TOTAL_LINES (w);
10449 freeze_window_starts (f, 0);
10450 shrink_mini_window (w);
10452 if (height)
10454 freeze_window_starts (f, 1);
10455 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10458 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10462 if (old_current_buffer)
10463 set_buffer_internal (old_current_buffer);
10466 return window_height_changed_p;
10470 /* Value is the current message, a string, or nil if there is no
10471 current message. */
10473 Lisp_Object
10474 current_message (void)
10476 Lisp_Object msg;
10478 if (!BUFFERP (echo_area_buffer[0]))
10479 msg = Qnil;
10480 else
10482 with_echo_area_buffer (0, 0, current_message_1,
10483 (intptr_t) &msg, Qnil);
10484 if (NILP (msg))
10485 echo_area_buffer[0] = Qnil;
10488 return msg;
10492 static int
10493 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10495 intptr_t i1 = a1;
10496 Lisp_Object *msg = (Lisp_Object *) i1;
10498 if (Z > BEG)
10499 *msg = make_buffer_string (BEG, Z, 1);
10500 else
10501 *msg = Qnil;
10502 return 0;
10506 /* Push the current message on Vmessage_stack for later restoration
10507 by restore_message. Value is non-zero if the current message isn't
10508 empty. This is a relatively infrequent operation, so it's not
10509 worth optimizing. */
10511 bool
10512 push_message (void)
10514 Lisp_Object msg = current_message ();
10515 Vmessage_stack = Fcons (msg, Vmessage_stack);
10516 return STRINGP (msg);
10520 /* Restore message display from the top of Vmessage_stack. */
10522 void
10523 restore_message (void)
10525 eassert (CONSP (Vmessage_stack));
10526 message3_nolog (XCAR (Vmessage_stack));
10530 /* Handler for record_unwind_protect calling pop_message. */
10532 Lisp_Object
10533 pop_message_unwind (Lisp_Object dummy)
10535 pop_message ();
10536 return Qnil;
10539 /* Pop the top-most entry off Vmessage_stack. */
10541 static void
10542 pop_message (void)
10544 eassert (CONSP (Vmessage_stack));
10545 Vmessage_stack = XCDR (Vmessage_stack);
10549 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10550 exits. If the stack is not empty, we have a missing pop_message
10551 somewhere. */
10553 void
10554 check_message_stack (void)
10556 if (!NILP (Vmessage_stack))
10557 emacs_abort ();
10561 /* Truncate to NCHARS what will be displayed in the echo area the next
10562 time we display it---but don't redisplay it now. */
10564 void
10565 truncate_echo_area (ptrdiff_t nchars)
10567 if (nchars == 0)
10568 echo_area_buffer[0] = Qnil;
10569 else if (!noninteractive
10570 && INTERACTIVE
10571 && !NILP (echo_area_buffer[0]))
10573 struct frame *sf = SELECTED_FRAME ();
10574 /* Error messages get reported properly by cmd_error, so this must be
10575 just an informative message; if the frame hasn't really been
10576 initialized yet, just toss it. */
10577 if (sf->glyphs_initialized_p)
10578 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10583 /* Helper function for truncate_echo_area. Truncate the current
10584 message to at most NCHARS characters. */
10586 static int
10587 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10589 if (BEG + nchars < Z)
10590 del_range (BEG + nchars, Z);
10591 if (Z == BEG)
10592 echo_area_buffer[0] = Qnil;
10593 return 0;
10596 /* Set the current message to STRING. */
10598 static void
10599 set_message (Lisp_Object string)
10601 eassert (STRINGP (string));
10603 message_enable_multibyte = STRING_MULTIBYTE (string);
10605 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10606 message_buf_print = 0;
10607 help_echo_showing_p = 0;
10609 if (STRINGP (Vdebug_on_message)
10610 && STRINGP (string)
10611 && fast_string_match (Vdebug_on_message, string) >= 0)
10612 call_debugger (list2 (Qerror, string));
10616 /* Helper function for set_message. First argument is ignored and second
10617 argument has the same meaning as for set_message.
10618 This function is called with the echo area buffer being current. */
10620 static int
10621 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10623 eassert (STRINGP (string));
10625 /* Change multibyteness of the echo buffer appropriately. */
10626 if (message_enable_multibyte
10627 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10628 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10630 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10631 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10632 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10634 /* Insert new message at BEG. */
10635 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10637 /* This function takes care of single/multibyte conversion.
10638 We just have to ensure that the echo area buffer has the right
10639 setting of enable_multibyte_characters. */
10640 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10642 return 0;
10646 /* Clear messages. CURRENT_P non-zero means clear the current
10647 message. LAST_DISPLAYED_P non-zero means clear the message
10648 last displayed. */
10650 void
10651 clear_message (int current_p, int last_displayed_p)
10653 if (current_p)
10655 echo_area_buffer[0] = Qnil;
10656 message_cleared_p = 1;
10659 if (last_displayed_p)
10660 echo_area_buffer[1] = Qnil;
10662 message_buf_print = 0;
10665 /* Clear garbaged frames.
10667 This function is used where the old redisplay called
10668 redraw_garbaged_frames which in turn called redraw_frame which in
10669 turn called clear_frame. The call to clear_frame was a source of
10670 flickering. I believe a clear_frame is not necessary. It should
10671 suffice in the new redisplay to invalidate all current matrices,
10672 and ensure a complete redisplay of all windows. */
10674 static void
10675 clear_garbaged_frames (void)
10677 if (frame_garbaged)
10679 Lisp_Object tail, frame;
10680 int changed_count = 0;
10682 FOR_EACH_FRAME (tail, frame)
10684 struct frame *f = XFRAME (frame);
10686 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10688 if (f->resized_p)
10690 redraw_frame (f);
10691 f->force_flush_display_p = 1;
10693 clear_current_matrices (f);
10694 changed_count++;
10695 f->garbaged = 0;
10696 f->resized_p = 0;
10700 frame_garbaged = 0;
10701 if (changed_count)
10702 ++windows_or_buffers_changed;
10707 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10708 is non-zero update selected_frame. Value is non-zero if the
10709 mini-windows height has been changed. */
10711 static int
10712 echo_area_display (int update_frame_p)
10714 Lisp_Object mini_window;
10715 struct window *w;
10716 struct frame *f;
10717 int window_height_changed_p = 0;
10718 struct frame *sf = SELECTED_FRAME ();
10720 mini_window = FRAME_MINIBUF_WINDOW (sf);
10721 w = XWINDOW (mini_window);
10722 f = XFRAME (WINDOW_FRAME (w));
10724 /* Don't display if frame is invisible or not yet initialized. */
10725 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10726 return 0;
10728 #ifdef HAVE_WINDOW_SYSTEM
10729 /* When Emacs starts, selected_frame may be the initial terminal
10730 frame. If we let this through, a message would be displayed on
10731 the terminal. */
10732 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10733 return 0;
10734 #endif /* HAVE_WINDOW_SYSTEM */
10736 /* Redraw garbaged frames. */
10737 clear_garbaged_frames ();
10739 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10741 echo_area_window = mini_window;
10742 window_height_changed_p = display_echo_area (w);
10743 w->must_be_updated_p = 1;
10745 /* Update the display, unless called from redisplay_internal.
10746 Also don't update the screen during redisplay itself. The
10747 update will happen at the end of redisplay, and an update
10748 here could cause confusion. */
10749 if (update_frame_p && !redisplaying_p)
10751 int n = 0;
10753 /* If the display update has been interrupted by pending
10754 input, update mode lines in the frame. Due to the
10755 pending input, it might have been that redisplay hasn't
10756 been called, so that mode lines above the echo area are
10757 garbaged. This looks odd, so we prevent it here. */
10758 if (!display_completed)
10759 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10761 if (window_height_changed_p
10762 /* Don't do this if Emacs is shutting down. Redisplay
10763 needs to run hooks. */
10764 && !NILP (Vrun_hooks))
10766 /* Must update other windows. Likewise as in other
10767 cases, don't let this update be interrupted by
10768 pending input. */
10769 ptrdiff_t count = SPECPDL_INDEX ();
10770 specbind (Qredisplay_dont_pause, Qt);
10771 windows_or_buffers_changed = 1;
10772 redisplay_internal ();
10773 unbind_to (count, Qnil);
10775 else if (FRAME_WINDOW_P (f) && n == 0)
10777 /* Window configuration is the same as before.
10778 Can do with a display update of the echo area,
10779 unless we displayed some mode lines. */
10780 update_single_window (w, 1);
10781 FRAME_RIF (f)->flush_display (f);
10783 else
10784 update_frame (f, 1, 1);
10786 /* If cursor is in the echo area, make sure that the next
10787 redisplay displays the minibuffer, so that the cursor will
10788 be replaced with what the minibuffer wants. */
10789 if (cursor_in_echo_area)
10790 ++windows_or_buffers_changed;
10793 else if (!EQ (mini_window, selected_window))
10794 windows_or_buffers_changed++;
10796 /* Last displayed message is now the current message. */
10797 echo_area_buffer[1] = echo_area_buffer[0];
10798 /* Inform read_char that we're not echoing. */
10799 echo_message_buffer = Qnil;
10801 /* Prevent redisplay optimization in redisplay_internal by resetting
10802 this_line_start_pos. This is done because the mini-buffer now
10803 displays the message instead of its buffer text. */
10804 if (EQ (mini_window, selected_window))
10805 CHARPOS (this_line_start_pos) = 0;
10807 return window_height_changed_p;
10810 /* Nonzero if the current window's buffer is shown in more than one
10811 window and was modified since last redisplay. */
10813 static int
10814 buffer_shared_and_changed (void)
10816 return (buffer_window_count (current_buffer) > 1
10817 && UNCHANGED_MODIFIED < MODIFF);
10820 /* Nonzero if W doesn't reflect the actual state of current buffer due
10821 to its text or overlays change. FIXME: this may be called when
10822 XBUFFER (w->contents) != current_buffer, which looks suspicious. */
10824 static int
10825 window_outdated (struct window *w)
10827 return (w->last_modified < MODIFF
10828 || w->last_overlay_modified < OVERLAY_MODIFF);
10831 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10832 is enabled and mark of W's buffer was changed since last W's update. */
10834 static int
10835 window_buffer_changed (struct window *w)
10837 struct buffer *b = XBUFFER (w->contents);
10839 eassert (BUFFER_LIVE_P (b));
10841 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10842 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10843 != (w->region_showing != 0)));
10846 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10848 static int
10849 mode_line_update_needed (struct window *w)
10851 return (w->column_number_displayed != -1
10852 && !(PT == w->last_point && !window_outdated (w))
10853 && (w->column_number_displayed != current_column ()));
10856 /***********************************************************************
10857 Mode Lines and Frame Titles
10858 ***********************************************************************/
10860 /* A buffer for constructing non-propertized mode-line strings and
10861 frame titles in it; allocated from the heap in init_xdisp and
10862 resized as needed in store_mode_line_noprop_char. */
10864 static char *mode_line_noprop_buf;
10866 /* The buffer's end, and a current output position in it. */
10868 static char *mode_line_noprop_buf_end;
10869 static char *mode_line_noprop_ptr;
10871 #define MODE_LINE_NOPROP_LEN(start) \
10872 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10874 static enum {
10875 MODE_LINE_DISPLAY = 0,
10876 MODE_LINE_TITLE,
10877 MODE_LINE_NOPROP,
10878 MODE_LINE_STRING
10879 } mode_line_target;
10881 /* Alist that caches the results of :propertize.
10882 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10883 static Lisp_Object mode_line_proptrans_alist;
10885 /* List of strings making up the mode-line. */
10886 static Lisp_Object mode_line_string_list;
10888 /* Base face property when building propertized mode line string. */
10889 static Lisp_Object mode_line_string_face;
10890 static Lisp_Object mode_line_string_face_prop;
10893 /* Unwind data for mode line strings */
10895 static Lisp_Object Vmode_line_unwind_vector;
10897 static Lisp_Object
10898 format_mode_line_unwind_data (struct frame *target_frame,
10899 struct buffer *obuf,
10900 Lisp_Object owin,
10901 int save_proptrans)
10903 Lisp_Object vector, tmp;
10905 /* Reduce consing by keeping one vector in
10906 Vwith_echo_area_save_vector. */
10907 vector = Vmode_line_unwind_vector;
10908 Vmode_line_unwind_vector = Qnil;
10910 if (NILP (vector))
10911 vector = Fmake_vector (make_number (10), Qnil);
10913 ASET (vector, 0, make_number (mode_line_target));
10914 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10915 ASET (vector, 2, mode_line_string_list);
10916 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10917 ASET (vector, 4, mode_line_string_face);
10918 ASET (vector, 5, mode_line_string_face_prop);
10920 if (obuf)
10921 XSETBUFFER (tmp, obuf);
10922 else
10923 tmp = Qnil;
10924 ASET (vector, 6, tmp);
10925 ASET (vector, 7, owin);
10926 if (target_frame)
10928 /* Similarly to `with-selected-window', if the operation selects
10929 a window on another frame, we must restore that frame's
10930 selected window, and (for a tty) the top-frame. */
10931 ASET (vector, 8, target_frame->selected_window);
10932 if (FRAME_TERMCAP_P (target_frame))
10933 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10936 return vector;
10939 static Lisp_Object
10940 unwind_format_mode_line (Lisp_Object vector)
10942 Lisp_Object old_window = AREF (vector, 7);
10943 Lisp_Object target_frame_window = AREF (vector, 8);
10944 Lisp_Object old_top_frame = AREF (vector, 9);
10946 mode_line_target = XINT (AREF (vector, 0));
10947 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10948 mode_line_string_list = AREF (vector, 2);
10949 if (! EQ (AREF (vector, 3), Qt))
10950 mode_line_proptrans_alist = AREF (vector, 3);
10951 mode_line_string_face = AREF (vector, 4);
10952 mode_line_string_face_prop = AREF (vector, 5);
10954 /* Select window before buffer, since it may change the buffer. */
10955 if (!NILP (old_window))
10957 /* If the operation that we are unwinding had selected a window
10958 on a different frame, reset its frame-selected-window. For a
10959 text terminal, reset its top-frame if necessary. */
10960 if (!NILP (target_frame_window))
10962 Lisp_Object frame
10963 = WINDOW_FRAME (XWINDOW (target_frame_window));
10965 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10966 Fselect_window (target_frame_window, Qt);
10968 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10969 Fselect_frame (old_top_frame, Qt);
10972 Fselect_window (old_window, Qt);
10975 if (!NILP (AREF (vector, 6)))
10977 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10978 ASET (vector, 6, Qnil);
10981 Vmode_line_unwind_vector = vector;
10982 return Qnil;
10986 /* Store a single character C for the frame title in mode_line_noprop_buf.
10987 Re-allocate mode_line_noprop_buf if necessary. */
10989 static void
10990 store_mode_line_noprop_char (char c)
10992 /* If output position has reached the end of the allocated buffer,
10993 increase the buffer's size. */
10994 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10996 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10997 ptrdiff_t size = len;
10998 mode_line_noprop_buf =
10999 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11000 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11001 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11004 *mode_line_noprop_ptr++ = c;
11008 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11009 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11010 characters that yield more columns than PRECISION; PRECISION <= 0
11011 means copy the whole string. Pad with spaces until FIELD_WIDTH
11012 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11013 pad. Called from display_mode_element when it is used to build a
11014 frame title. */
11016 static int
11017 store_mode_line_noprop (const char *string, int field_width, int precision)
11019 const unsigned char *str = (const unsigned char *) string;
11020 int n = 0;
11021 ptrdiff_t dummy, nbytes;
11023 /* Copy at most PRECISION chars from STR. */
11024 nbytes = strlen (string);
11025 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11026 while (nbytes--)
11027 store_mode_line_noprop_char (*str++);
11029 /* Fill up with spaces until FIELD_WIDTH reached. */
11030 while (field_width > 0
11031 && n < field_width)
11033 store_mode_line_noprop_char (' ');
11034 ++n;
11037 return n;
11040 /***********************************************************************
11041 Frame Titles
11042 ***********************************************************************/
11044 #ifdef HAVE_WINDOW_SYSTEM
11046 /* Set the title of FRAME, if it has changed. The title format is
11047 Vicon_title_format if FRAME is iconified, otherwise it is
11048 frame_title_format. */
11050 static void
11051 x_consider_frame_title (Lisp_Object frame)
11053 struct frame *f = XFRAME (frame);
11055 if (FRAME_WINDOW_P (f)
11056 || FRAME_MINIBUF_ONLY_P (f)
11057 || f->explicit_name)
11059 /* Do we have more than one visible frame on this X display? */
11060 Lisp_Object tail, other_frame, fmt;
11061 ptrdiff_t title_start;
11062 char *title;
11063 ptrdiff_t len;
11064 struct it it;
11065 ptrdiff_t count = SPECPDL_INDEX ();
11067 FOR_EACH_FRAME (tail, other_frame)
11069 struct frame *tf = XFRAME (other_frame);
11071 if (tf != f
11072 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11073 && !FRAME_MINIBUF_ONLY_P (tf)
11074 && !EQ (other_frame, tip_frame)
11075 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11076 break;
11079 /* Set global variable indicating that multiple frames exist. */
11080 multiple_frames = CONSP (tail);
11082 /* Switch to the buffer of selected window of the frame. Set up
11083 mode_line_target so that display_mode_element will output into
11084 mode_line_noprop_buf; then display the title. */
11085 record_unwind_protect (unwind_format_mode_line,
11086 format_mode_line_unwind_data
11087 (f, current_buffer, selected_window, 0));
11089 Fselect_window (f->selected_window, Qt);
11090 set_buffer_internal_1
11091 (XBUFFER (XWINDOW (f->selected_window)->contents));
11092 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11094 mode_line_target = MODE_LINE_TITLE;
11095 title_start = MODE_LINE_NOPROP_LEN (0);
11096 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11097 NULL, DEFAULT_FACE_ID);
11098 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11099 len = MODE_LINE_NOPROP_LEN (title_start);
11100 title = mode_line_noprop_buf + title_start;
11101 unbind_to (count, Qnil);
11103 /* Set the title only if it's changed. This avoids consing in
11104 the common case where it hasn't. (If it turns out that we've
11105 already wasted too much time by walking through the list with
11106 display_mode_element, then we might need to optimize at a
11107 higher level than this.) */
11108 if (! STRINGP (f->name)
11109 || SBYTES (f->name) != len
11110 || memcmp (title, SDATA (f->name), len) != 0)
11111 x_implicitly_set_name (f, make_string (title, len), Qnil);
11115 #endif /* not HAVE_WINDOW_SYSTEM */
11118 /***********************************************************************
11119 Menu Bars
11120 ***********************************************************************/
11123 /* Prepare for redisplay by updating menu-bar item lists when
11124 appropriate. This can call eval. */
11126 void
11127 prepare_menu_bars (void)
11129 int all_windows;
11130 struct gcpro gcpro1, gcpro2;
11131 struct frame *f;
11132 Lisp_Object tooltip_frame;
11134 #ifdef HAVE_WINDOW_SYSTEM
11135 tooltip_frame = tip_frame;
11136 #else
11137 tooltip_frame = Qnil;
11138 #endif
11140 /* Update all frame titles based on their buffer names, etc. We do
11141 this before the menu bars so that the buffer-menu will show the
11142 up-to-date frame titles. */
11143 #ifdef HAVE_WINDOW_SYSTEM
11144 if (windows_or_buffers_changed || update_mode_lines)
11146 Lisp_Object tail, frame;
11148 FOR_EACH_FRAME (tail, frame)
11150 f = XFRAME (frame);
11151 if (!EQ (frame, tooltip_frame)
11152 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11153 x_consider_frame_title (frame);
11156 #endif /* HAVE_WINDOW_SYSTEM */
11158 /* Update the menu bar item lists, if appropriate. This has to be
11159 done before any actual redisplay or generation of display lines. */
11160 all_windows = (update_mode_lines
11161 || buffer_shared_and_changed ()
11162 || windows_or_buffers_changed);
11163 if (all_windows)
11165 Lisp_Object tail, frame;
11166 ptrdiff_t count = SPECPDL_INDEX ();
11167 /* 1 means that update_menu_bar has run its hooks
11168 so any further calls to update_menu_bar shouldn't do so again. */
11169 int menu_bar_hooks_run = 0;
11171 record_unwind_save_match_data ();
11173 FOR_EACH_FRAME (tail, frame)
11175 f = XFRAME (frame);
11177 /* Ignore tooltip frame. */
11178 if (EQ (frame, tooltip_frame))
11179 continue;
11181 /* If a window on this frame changed size, report that to
11182 the user and clear the size-change flag. */
11183 if (FRAME_WINDOW_SIZES_CHANGED (f))
11185 Lisp_Object functions;
11187 /* Clear flag first in case we get an error below. */
11188 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11189 functions = Vwindow_size_change_functions;
11190 GCPRO2 (tail, functions);
11192 while (CONSP (functions))
11194 if (!EQ (XCAR (functions), Qt))
11195 call1 (XCAR (functions), frame);
11196 functions = XCDR (functions);
11198 UNGCPRO;
11201 GCPRO1 (tail);
11202 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11203 #ifdef HAVE_WINDOW_SYSTEM
11204 update_tool_bar (f, 0);
11205 #endif
11206 #ifdef HAVE_NS
11207 if (windows_or_buffers_changed
11208 && FRAME_NS_P (f))
11209 ns_set_doc_edited
11210 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11211 #endif
11212 UNGCPRO;
11215 unbind_to (count, Qnil);
11217 else
11219 struct frame *sf = SELECTED_FRAME ();
11220 update_menu_bar (sf, 1, 0);
11221 #ifdef HAVE_WINDOW_SYSTEM
11222 update_tool_bar (sf, 1);
11223 #endif
11228 /* Update the menu bar item list for frame F. This has to be done
11229 before we start to fill in any display lines, because it can call
11230 eval.
11232 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11234 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11235 already ran the menu bar hooks for this redisplay, so there
11236 is no need to run them again. The return value is the
11237 updated value of this flag, to pass to the next call. */
11239 static int
11240 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11242 Lisp_Object window;
11243 register struct window *w;
11245 /* If called recursively during a menu update, do nothing. This can
11246 happen when, for instance, an activate-menubar-hook causes a
11247 redisplay. */
11248 if (inhibit_menubar_update)
11249 return hooks_run;
11251 window = FRAME_SELECTED_WINDOW (f);
11252 w = XWINDOW (window);
11254 if (FRAME_WINDOW_P (f)
11256 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11257 || defined (HAVE_NS) || defined (USE_GTK)
11258 FRAME_EXTERNAL_MENU_BAR (f)
11259 #else
11260 FRAME_MENU_BAR_LINES (f) > 0
11261 #endif
11262 : FRAME_MENU_BAR_LINES (f) > 0)
11264 /* If the user has switched buffers or windows, we need to
11265 recompute to reflect the new bindings. But we'll
11266 recompute when update_mode_lines is set too; that means
11267 that people can use force-mode-line-update to request
11268 that the menu bar be recomputed. The adverse effect on
11269 the rest of the redisplay algorithm is about the same as
11270 windows_or_buffers_changed anyway. */
11271 if (windows_or_buffers_changed
11272 /* This used to test w->update_mode_line, but we believe
11273 there is no need to recompute the menu in that case. */
11274 || update_mode_lines
11275 || window_buffer_changed (w))
11277 struct buffer *prev = current_buffer;
11278 ptrdiff_t count = SPECPDL_INDEX ();
11280 specbind (Qinhibit_menubar_update, Qt);
11282 set_buffer_internal_1 (XBUFFER (w->contents));
11283 if (save_match_data)
11284 record_unwind_save_match_data ();
11285 if (NILP (Voverriding_local_map_menu_flag))
11287 specbind (Qoverriding_terminal_local_map, Qnil);
11288 specbind (Qoverriding_local_map, Qnil);
11291 if (!hooks_run)
11293 /* Run the Lucid hook. */
11294 safe_run_hooks (Qactivate_menubar_hook);
11296 /* If it has changed current-menubar from previous value,
11297 really recompute the menu-bar from the value. */
11298 if (! NILP (Vlucid_menu_bar_dirty_flag))
11299 call0 (Qrecompute_lucid_menubar);
11301 safe_run_hooks (Qmenu_bar_update_hook);
11303 hooks_run = 1;
11306 XSETFRAME (Vmenu_updating_frame, f);
11307 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11309 /* Redisplay the menu bar in case we changed it. */
11310 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11311 || defined (HAVE_NS) || defined (USE_GTK)
11312 if (FRAME_WINDOW_P (f))
11314 #if defined (HAVE_NS)
11315 /* All frames on Mac OS share the same menubar. So only
11316 the selected frame should be allowed to set it. */
11317 if (f == SELECTED_FRAME ())
11318 #endif
11319 set_frame_menubar (f, 0, 0);
11321 else
11322 /* On a terminal screen, the menu bar is an ordinary screen
11323 line, and this makes it get updated. */
11324 w->update_mode_line = 1;
11325 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11326 /* In the non-toolkit version, the menu bar is an ordinary screen
11327 line, and this makes it get updated. */
11328 w->update_mode_line = 1;
11329 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11331 unbind_to (count, Qnil);
11332 set_buffer_internal_1 (prev);
11336 return hooks_run;
11341 /***********************************************************************
11342 Output Cursor
11343 ***********************************************************************/
11345 #ifdef HAVE_WINDOW_SYSTEM
11347 /* EXPORT:
11348 Nominal cursor position -- where to draw output.
11349 HPOS and VPOS are window relative glyph matrix coordinates.
11350 X and Y are window relative pixel coordinates. */
11352 struct cursor_pos output_cursor;
11355 /* EXPORT:
11356 Set the global variable output_cursor to CURSOR. All cursor
11357 positions are relative to updated_window. */
11359 void
11360 set_output_cursor (struct cursor_pos *cursor)
11362 output_cursor.hpos = cursor->hpos;
11363 output_cursor.vpos = cursor->vpos;
11364 output_cursor.x = cursor->x;
11365 output_cursor.y = cursor->y;
11369 /* EXPORT for RIF:
11370 Set a nominal cursor position.
11372 HPOS and VPOS are column/row positions in a window glyph matrix. X
11373 and Y are window text area relative pixel positions.
11375 If this is done during an update, updated_window will contain the
11376 window that is being updated and the position is the future output
11377 cursor position for that window. If updated_window is null, use
11378 selected_window and display the cursor at the given position. */
11380 void
11381 x_cursor_to (int vpos, int hpos, int y, int x)
11383 struct window *w;
11385 /* If updated_window is not set, work on selected_window. */
11386 if (updated_window)
11387 w = updated_window;
11388 else
11389 w = XWINDOW (selected_window);
11391 /* Set the output cursor. */
11392 output_cursor.hpos = hpos;
11393 output_cursor.vpos = vpos;
11394 output_cursor.x = x;
11395 output_cursor.y = y;
11397 /* If not called as part of an update, really display the cursor.
11398 This will also set the cursor position of W. */
11399 if (updated_window == NULL)
11401 block_input ();
11402 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11403 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11404 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11405 unblock_input ();
11409 #endif /* HAVE_WINDOW_SYSTEM */
11412 /***********************************************************************
11413 Tool-bars
11414 ***********************************************************************/
11416 #ifdef HAVE_WINDOW_SYSTEM
11418 /* Where the mouse was last time we reported a mouse event. */
11420 FRAME_PTR last_mouse_frame;
11422 /* Tool-bar item index of the item on which a mouse button was pressed
11423 or -1. */
11425 int last_tool_bar_item;
11427 /* Select `frame' temporarily without running all the code in
11428 do_switch_frame.
11429 FIXME: Maybe do_switch_frame should be trimmed down similarly
11430 when `norecord' is set. */
11431 static Lisp_Object
11432 fast_set_selected_frame (Lisp_Object frame)
11434 if (!EQ (selected_frame, frame))
11436 selected_frame = frame;
11437 selected_window = XFRAME (frame)->selected_window;
11439 return Qnil;
11442 /* Update the tool-bar item list for frame F. This has to be done
11443 before we start to fill in any display lines. Called from
11444 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11445 and restore it here. */
11447 static void
11448 update_tool_bar (struct frame *f, int save_match_data)
11450 #if defined (USE_GTK) || defined (HAVE_NS)
11451 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11452 #else
11453 int do_update = WINDOWP (f->tool_bar_window)
11454 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11455 #endif
11457 if (do_update)
11459 Lisp_Object window;
11460 struct window *w;
11462 window = FRAME_SELECTED_WINDOW (f);
11463 w = XWINDOW (window);
11465 /* If the user has switched buffers or windows, we need to
11466 recompute to reflect the new bindings. But we'll
11467 recompute when update_mode_lines is set too; that means
11468 that people can use force-mode-line-update to request
11469 that the menu bar be recomputed. The adverse effect on
11470 the rest of the redisplay algorithm is about the same as
11471 windows_or_buffers_changed anyway. */
11472 if (windows_or_buffers_changed
11473 || w->update_mode_line
11474 || update_mode_lines
11475 || window_buffer_changed (w))
11477 struct buffer *prev = current_buffer;
11478 ptrdiff_t count = SPECPDL_INDEX ();
11479 Lisp_Object frame, new_tool_bar;
11480 int new_n_tool_bar;
11481 struct gcpro gcpro1;
11483 /* Set current_buffer to the buffer of the selected
11484 window of the frame, so that we get the right local
11485 keymaps. */
11486 set_buffer_internal_1 (XBUFFER (w->contents));
11488 /* Save match data, if we must. */
11489 if (save_match_data)
11490 record_unwind_save_match_data ();
11492 /* Make sure that we don't accidentally use bogus keymaps. */
11493 if (NILP (Voverriding_local_map_menu_flag))
11495 specbind (Qoverriding_terminal_local_map, Qnil);
11496 specbind (Qoverriding_local_map, Qnil);
11499 GCPRO1 (new_tool_bar);
11501 /* We must temporarily set the selected frame to this frame
11502 before calling tool_bar_items, because the calculation of
11503 the tool-bar keymap uses the selected frame (see
11504 `tool-bar-make-keymap' in tool-bar.el). */
11505 eassert (EQ (selected_window,
11506 /* Since we only explicitly preserve selected_frame,
11507 check that selected_window would be redundant. */
11508 XFRAME (selected_frame)->selected_window));
11509 record_unwind_protect (fast_set_selected_frame, selected_frame);
11510 XSETFRAME (frame, f);
11511 fast_set_selected_frame (frame);
11513 /* Build desired tool-bar items from keymaps. */
11514 new_tool_bar
11515 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11516 &new_n_tool_bar);
11518 /* Redisplay the tool-bar if we changed it. */
11519 if (new_n_tool_bar != f->n_tool_bar_items
11520 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11522 /* Redisplay that happens asynchronously due to an expose event
11523 may access f->tool_bar_items. Make sure we update both
11524 variables within BLOCK_INPUT so no such event interrupts. */
11525 block_input ();
11526 fset_tool_bar_items (f, new_tool_bar);
11527 f->n_tool_bar_items = new_n_tool_bar;
11528 w->update_mode_line = 1;
11529 unblock_input ();
11532 UNGCPRO;
11534 unbind_to (count, Qnil);
11535 set_buffer_internal_1 (prev);
11541 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11542 F's desired tool-bar contents. F->tool_bar_items must have
11543 been set up previously by calling prepare_menu_bars. */
11545 static void
11546 build_desired_tool_bar_string (struct frame *f)
11548 int i, size, size_needed;
11549 struct gcpro gcpro1, gcpro2, gcpro3;
11550 Lisp_Object image, plist, props;
11552 image = plist = props = Qnil;
11553 GCPRO3 (image, plist, props);
11555 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11556 Otherwise, make a new string. */
11558 /* The size of the string we might be able to reuse. */
11559 size = (STRINGP (f->desired_tool_bar_string)
11560 ? SCHARS (f->desired_tool_bar_string)
11561 : 0);
11563 /* We need one space in the string for each image. */
11564 size_needed = f->n_tool_bar_items;
11566 /* Reuse f->desired_tool_bar_string, if possible. */
11567 if (size < size_needed || NILP (f->desired_tool_bar_string))
11568 fset_desired_tool_bar_string
11569 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11570 else
11572 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11573 Fremove_text_properties (make_number (0), make_number (size),
11574 props, f->desired_tool_bar_string);
11577 /* Put a `display' property on the string for the images to display,
11578 put a `menu_item' property on tool-bar items with a value that
11579 is the index of the item in F's tool-bar item vector. */
11580 for (i = 0; i < f->n_tool_bar_items; ++i)
11582 #define PROP(IDX) \
11583 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11585 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11586 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11587 int hmargin, vmargin, relief, idx, end;
11589 /* If image is a vector, choose the image according to the
11590 button state. */
11591 image = PROP (TOOL_BAR_ITEM_IMAGES);
11592 if (VECTORP (image))
11594 if (enabled_p)
11595 idx = (selected_p
11596 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11597 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11598 else
11599 idx = (selected_p
11600 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11601 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11603 eassert (ASIZE (image) >= idx);
11604 image = AREF (image, idx);
11606 else
11607 idx = -1;
11609 /* Ignore invalid image specifications. */
11610 if (!valid_image_p (image))
11611 continue;
11613 /* Display the tool-bar button pressed, or depressed. */
11614 plist = Fcopy_sequence (XCDR (image));
11616 /* Compute margin and relief to draw. */
11617 relief = (tool_bar_button_relief >= 0
11618 ? tool_bar_button_relief
11619 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11620 hmargin = vmargin = relief;
11622 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11623 INT_MAX - max (hmargin, vmargin)))
11625 hmargin += XFASTINT (Vtool_bar_button_margin);
11626 vmargin += XFASTINT (Vtool_bar_button_margin);
11628 else if (CONSP (Vtool_bar_button_margin))
11630 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11631 INT_MAX - hmargin))
11632 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11634 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11635 INT_MAX - vmargin))
11636 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11639 if (auto_raise_tool_bar_buttons_p)
11641 /* Add a `:relief' property to the image spec if the item is
11642 selected. */
11643 if (selected_p)
11645 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11646 hmargin -= relief;
11647 vmargin -= relief;
11650 else
11652 /* If image is selected, display it pressed, i.e. with a
11653 negative relief. If it's not selected, display it with a
11654 raised relief. */
11655 plist = Fplist_put (plist, QCrelief,
11656 (selected_p
11657 ? make_number (-relief)
11658 : make_number (relief)));
11659 hmargin -= relief;
11660 vmargin -= relief;
11663 /* Put a margin around the image. */
11664 if (hmargin || vmargin)
11666 if (hmargin == vmargin)
11667 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11668 else
11669 plist = Fplist_put (plist, QCmargin,
11670 Fcons (make_number (hmargin),
11671 make_number (vmargin)));
11674 /* If button is not enabled, and we don't have special images
11675 for the disabled state, make the image appear disabled by
11676 applying an appropriate algorithm to it. */
11677 if (!enabled_p && idx < 0)
11678 plist = Fplist_put (plist, QCconversion, Qdisabled);
11680 /* Put a `display' text property on the string for the image to
11681 display. Put a `menu-item' property on the string that gives
11682 the start of this item's properties in the tool-bar items
11683 vector. */
11684 image = Fcons (Qimage, plist);
11685 props = list4 (Qdisplay, image,
11686 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11688 /* Let the last image hide all remaining spaces in the tool bar
11689 string. The string can be longer than needed when we reuse a
11690 previous string. */
11691 if (i + 1 == f->n_tool_bar_items)
11692 end = SCHARS (f->desired_tool_bar_string);
11693 else
11694 end = i + 1;
11695 Fadd_text_properties (make_number (i), make_number (end),
11696 props, f->desired_tool_bar_string);
11697 #undef PROP
11700 UNGCPRO;
11704 /* Display one line of the tool-bar of frame IT->f.
11706 HEIGHT specifies the desired height of the tool-bar line.
11707 If the actual height of the glyph row is less than HEIGHT, the
11708 row's height is increased to HEIGHT, and the icons are centered
11709 vertically in the new height.
11711 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11712 count a final empty row in case the tool-bar width exactly matches
11713 the window width.
11716 static void
11717 display_tool_bar_line (struct it *it, int height)
11719 struct glyph_row *row = it->glyph_row;
11720 int max_x = it->last_visible_x;
11721 struct glyph *last;
11723 prepare_desired_row (row);
11724 row->y = it->current_y;
11726 /* Note that this isn't made use of if the face hasn't a box,
11727 so there's no need to check the face here. */
11728 it->start_of_box_run_p = 1;
11730 while (it->current_x < max_x)
11732 int x, n_glyphs_before, i, nglyphs;
11733 struct it it_before;
11735 /* Get the next display element. */
11736 if (!get_next_display_element (it))
11738 /* Don't count empty row if we are counting needed tool-bar lines. */
11739 if (height < 0 && !it->hpos)
11740 return;
11741 break;
11744 /* Produce glyphs. */
11745 n_glyphs_before = row->used[TEXT_AREA];
11746 it_before = *it;
11748 PRODUCE_GLYPHS (it);
11750 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11751 i = 0;
11752 x = it_before.current_x;
11753 while (i < nglyphs)
11755 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11757 if (x + glyph->pixel_width > max_x)
11759 /* Glyph doesn't fit on line. Backtrack. */
11760 row->used[TEXT_AREA] = n_glyphs_before;
11761 *it = it_before;
11762 /* If this is the only glyph on this line, it will never fit on the
11763 tool-bar, so skip it. But ensure there is at least one glyph,
11764 so we don't accidentally disable the tool-bar. */
11765 if (n_glyphs_before == 0
11766 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11767 break;
11768 goto out;
11771 ++it->hpos;
11772 x += glyph->pixel_width;
11773 ++i;
11776 /* Stop at line end. */
11777 if (ITERATOR_AT_END_OF_LINE_P (it))
11778 break;
11780 set_iterator_to_next (it, 1);
11783 out:;
11785 row->displays_text_p = row->used[TEXT_AREA] != 0;
11787 /* Use default face for the border below the tool bar.
11789 FIXME: When auto-resize-tool-bars is grow-only, there is
11790 no additional border below the possibly empty tool-bar lines.
11791 So to make the extra empty lines look "normal", we have to
11792 use the tool-bar face for the border too. */
11793 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11794 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11795 it->face_id = DEFAULT_FACE_ID;
11797 extend_face_to_end_of_line (it);
11798 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11799 last->right_box_line_p = 1;
11800 if (last == row->glyphs[TEXT_AREA])
11801 last->left_box_line_p = 1;
11803 /* Make line the desired height and center it vertically. */
11804 if ((height -= it->max_ascent + it->max_descent) > 0)
11806 /* Don't add more than one line height. */
11807 height %= FRAME_LINE_HEIGHT (it->f);
11808 it->max_ascent += height / 2;
11809 it->max_descent += (height + 1) / 2;
11812 compute_line_metrics (it);
11814 /* If line is empty, make it occupy the rest of the tool-bar. */
11815 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11817 row->height = row->phys_height = it->last_visible_y - row->y;
11818 row->visible_height = row->height;
11819 row->ascent = row->phys_ascent = 0;
11820 row->extra_line_spacing = 0;
11823 row->full_width_p = 1;
11824 row->continued_p = 0;
11825 row->truncated_on_left_p = 0;
11826 row->truncated_on_right_p = 0;
11828 it->current_x = it->hpos = 0;
11829 it->current_y += row->height;
11830 ++it->vpos;
11831 ++it->glyph_row;
11835 /* Max tool-bar height. */
11837 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11838 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11840 /* Value is the number of screen lines needed to make all tool-bar
11841 items of frame F visible. The number of actual rows needed is
11842 returned in *N_ROWS if non-NULL. */
11844 static int
11845 tool_bar_lines_needed (struct frame *f, int *n_rows)
11847 struct window *w = XWINDOW (f->tool_bar_window);
11848 struct it it;
11849 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11850 the desired matrix, so use (unused) mode-line row as temporary row to
11851 avoid destroying the first tool-bar row. */
11852 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11854 /* Initialize an iterator for iteration over
11855 F->desired_tool_bar_string in the tool-bar window of frame F. */
11856 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11857 it.first_visible_x = 0;
11858 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11859 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11860 it.paragraph_embedding = L2R;
11862 while (!ITERATOR_AT_END_P (&it))
11864 clear_glyph_row (temp_row);
11865 it.glyph_row = temp_row;
11866 display_tool_bar_line (&it, -1);
11868 clear_glyph_row (temp_row);
11870 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11871 if (n_rows)
11872 *n_rows = it.vpos > 0 ? it.vpos : -1;
11874 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11878 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11879 0, 1, 0,
11880 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11881 If FRAME is nil or omitted, use the selected frame. */)
11882 (Lisp_Object frame)
11884 struct frame *f = decode_any_frame (frame);
11885 struct window *w;
11886 int nlines = 0;
11888 if (WINDOWP (f->tool_bar_window)
11889 && (w = XWINDOW (f->tool_bar_window),
11890 WINDOW_TOTAL_LINES (w) > 0))
11892 update_tool_bar (f, 1);
11893 if (f->n_tool_bar_items)
11895 build_desired_tool_bar_string (f);
11896 nlines = tool_bar_lines_needed (f, NULL);
11900 return make_number (nlines);
11904 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11905 height should be changed. */
11907 static int
11908 redisplay_tool_bar (struct frame *f)
11910 struct window *w;
11911 struct it it;
11912 struct glyph_row *row;
11914 #if defined (USE_GTK) || defined (HAVE_NS)
11915 if (FRAME_EXTERNAL_TOOL_BAR (f))
11916 update_frame_tool_bar (f);
11917 return 0;
11918 #endif
11920 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11921 do anything. This means you must start with tool-bar-lines
11922 non-zero to get the auto-sizing effect. Or in other words, you
11923 can turn off tool-bars by specifying tool-bar-lines zero. */
11924 if (!WINDOWP (f->tool_bar_window)
11925 || (w = XWINDOW (f->tool_bar_window),
11926 WINDOW_TOTAL_LINES (w) == 0))
11927 return 0;
11929 /* Set up an iterator for the tool-bar window. */
11930 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11931 it.first_visible_x = 0;
11932 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11933 row = it.glyph_row;
11935 /* Build a string that represents the contents of the tool-bar. */
11936 build_desired_tool_bar_string (f);
11937 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11938 /* FIXME: This should be controlled by a user option. But it
11939 doesn't make sense to have an R2L tool bar if the menu bar cannot
11940 be drawn also R2L, and making the menu bar R2L is tricky due
11941 toolkit-specific code that implements it. If an R2L tool bar is
11942 ever supported, display_tool_bar_line should also be augmented to
11943 call unproduce_glyphs like display_line and display_string
11944 do. */
11945 it.paragraph_embedding = L2R;
11947 if (f->n_tool_bar_rows == 0)
11949 int nlines;
11951 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11952 nlines != WINDOW_TOTAL_LINES (w)))
11954 Lisp_Object frame;
11955 int old_height = WINDOW_TOTAL_LINES (w);
11957 XSETFRAME (frame, f);
11958 Fmodify_frame_parameters (frame,
11959 Fcons (Fcons (Qtool_bar_lines,
11960 make_number (nlines)),
11961 Qnil));
11962 if (WINDOW_TOTAL_LINES (w) != old_height)
11964 clear_glyph_matrix (w->desired_matrix);
11965 fonts_changed_p = 1;
11966 return 1;
11971 /* Display as many lines as needed to display all tool-bar items. */
11973 if (f->n_tool_bar_rows > 0)
11975 int border, rows, height, extra;
11977 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11978 border = XINT (Vtool_bar_border);
11979 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11980 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11981 else if (EQ (Vtool_bar_border, Qborder_width))
11982 border = f->border_width;
11983 else
11984 border = 0;
11985 if (border < 0)
11986 border = 0;
11988 rows = f->n_tool_bar_rows;
11989 height = max (1, (it.last_visible_y - border) / rows);
11990 extra = it.last_visible_y - border - height * rows;
11992 while (it.current_y < it.last_visible_y)
11994 int h = 0;
11995 if (extra > 0 && rows-- > 0)
11997 h = (extra + rows - 1) / rows;
11998 extra -= h;
12000 display_tool_bar_line (&it, height + h);
12003 else
12005 while (it.current_y < it.last_visible_y)
12006 display_tool_bar_line (&it, 0);
12009 /* It doesn't make much sense to try scrolling in the tool-bar
12010 window, so don't do it. */
12011 w->desired_matrix->no_scrolling_p = 1;
12012 w->must_be_updated_p = 1;
12014 if (!NILP (Vauto_resize_tool_bars))
12016 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12017 int change_height_p = 0;
12019 /* If we couldn't display everything, change the tool-bar's
12020 height if there is room for more. */
12021 if (IT_STRING_CHARPOS (it) < it.end_charpos
12022 && it.current_y < max_tool_bar_height)
12023 change_height_p = 1;
12025 row = it.glyph_row - 1;
12027 /* If there are blank lines at the end, except for a partially
12028 visible blank line at the end that is smaller than
12029 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12030 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12031 && row->height >= FRAME_LINE_HEIGHT (f))
12032 change_height_p = 1;
12034 /* If row displays tool-bar items, but is partially visible,
12035 change the tool-bar's height. */
12036 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12037 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12038 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12039 change_height_p = 1;
12041 /* Resize windows as needed by changing the `tool-bar-lines'
12042 frame parameter. */
12043 if (change_height_p)
12045 Lisp_Object frame;
12046 int old_height = WINDOW_TOTAL_LINES (w);
12047 int nrows;
12048 int nlines = tool_bar_lines_needed (f, &nrows);
12050 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12051 && !f->minimize_tool_bar_window_p)
12052 ? (nlines > old_height)
12053 : (nlines != old_height));
12054 f->minimize_tool_bar_window_p = 0;
12056 if (change_height_p)
12058 XSETFRAME (frame, f);
12059 Fmodify_frame_parameters (frame,
12060 Fcons (Fcons (Qtool_bar_lines,
12061 make_number (nlines)),
12062 Qnil));
12063 if (WINDOW_TOTAL_LINES (w) != old_height)
12065 clear_glyph_matrix (w->desired_matrix);
12066 f->n_tool_bar_rows = nrows;
12067 fonts_changed_p = 1;
12068 return 1;
12074 f->minimize_tool_bar_window_p = 0;
12075 return 0;
12079 /* Get information about the tool-bar item which is displayed in GLYPH
12080 on frame F. Return in *PROP_IDX the index where tool-bar item
12081 properties start in F->tool_bar_items. Value is zero if
12082 GLYPH doesn't display a tool-bar item. */
12084 static int
12085 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12087 Lisp_Object prop;
12088 int success_p;
12089 int charpos;
12091 /* This function can be called asynchronously, which means we must
12092 exclude any possibility that Fget_text_property signals an
12093 error. */
12094 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12095 charpos = max (0, charpos);
12097 /* Get the text property `menu-item' at pos. The value of that
12098 property is the start index of this item's properties in
12099 F->tool_bar_items. */
12100 prop = Fget_text_property (make_number (charpos),
12101 Qmenu_item, f->current_tool_bar_string);
12102 if (INTEGERP (prop))
12104 *prop_idx = XINT (prop);
12105 success_p = 1;
12107 else
12108 success_p = 0;
12110 return success_p;
12114 /* Get information about the tool-bar item at position X/Y on frame F.
12115 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12116 the current matrix of the tool-bar window of F, or NULL if not
12117 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12118 item in F->tool_bar_items. Value is
12120 -1 if X/Y is not on a tool-bar item
12121 0 if X/Y is on the same item that was highlighted before.
12122 1 otherwise. */
12124 static int
12125 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12126 int *hpos, int *vpos, int *prop_idx)
12128 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12129 struct window *w = XWINDOW (f->tool_bar_window);
12130 int area;
12132 /* Find the glyph under X/Y. */
12133 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12134 if (*glyph == NULL)
12135 return -1;
12137 /* Get the start of this tool-bar item's properties in
12138 f->tool_bar_items. */
12139 if (!tool_bar_item_info (f, *glyph, prop_idx))
12140 return -1;
12142 /* Is mouse on the highlighted item? */
12143 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12144 && *vpos >= hlinfo->mouse_face_beg_row
12145 && *vpos <= hlinfo->mouse_face_end_row
12146 && (*vpos > hlinfo->mouse_face_beg_row
12147 || *hpos >= hlinfo->mouse_face_beg_col)
12148 && (*vpos < hlinfo->mouse_face_end_row
12149 || *hpos < hlinfo->mouse_face_end_col
12150 || hlinfo->mouse_face_past_end))
12151 return 0;
12153 return 1;
12157 /* EXPORT:
12158 Handle mouse button event on the tool-bar of frame F, at
12159 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12160 0 for button release. MODIFIERS is event modifiers for button
12161 release. */
12163 void
12164 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12165 int modifiers)
12167 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12168 struct window *w = XWINDOW (f->tool_bar_window);
12169 int hpos, vpos, prop_idx;
12170 struct glyph *glyph;
12171 Lisp_Object enabled_p;
12172 int ts;
12174 /* If not on the highlighted tool-bar item, and mouse-highlight is
12175 non-nil, return. This is so we generate the tool-bar button
12176 click only when the mouse button is released on the same item as
12177 where it was pressed. However, when mouse-highlight is disabled,
12178 generate the click when the button is released regardless of the
12179 highlight, since tool-bar items are not highlighted in that
12180 case. */
12181 frame_to_window_pixel_xy (w, &x, &y);
12182 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12183 if (ts == -1
12184 || (ts != 0 && !NILP (Vmouse_highlight)))
12185 return;
12187 /* When mouse-highlight is off, generate the click for the item
12188 where the button was pressed, disregarding where it was
12189 released. */
12190 if (NILP (Vmouse_highlight) && !down_p)
12191 prop_idx = last_tool_bar_item;
12193 /* If item is disabled, do nothing. */
12194 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12195 if (NILP (enabled_p))
12196 return;
12198 if (down_p)
12200 /* Show item in pressed state. */
12201 if (!NILP (Vmouse_highlight))
12202 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12203 last_tool_bar_item = prop_idx;
12205 else
12207 Lisp_Object key, frame;
12208 struct input_event event;
12209 EVENT_INIT (event);
12211 /* Show item in released state. */
12212 if (!NILP (Vmouse_highlight))
12213 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12215 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12217 XSETFRAME (frame, f);
12218 event.kind = TOOL_BAR_EVENT;
12219 event.frame_or_window = frame;
12220 event.arg = frame;
12221 kbd_buffer_store_event (&event);
12223 event.kind = TOOL_BAR_EVENT;
12224 event.frame_or_window = frame;
12225 event.arg = key;
12226 event.modifiers = modifiers;
12227 kbd_buffer_store_event (&event);
12228 last_tool_bar_item = -1;
12233 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12234 tool-bar window-relative coordinates X/Y. Called from
12235 note_mouse_highlight. */
12237 static void
12238 note_tool_bar_highlight (struct frame *f, int x, int y)
12240 Lisp_Object window = f->tool_bar_window;
12241 struct window *w = XWINDOW (window);
12242 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12243 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12244 int hpos, vpos;
12245 struct glyph *glyph;
12246 struct glyph_row *row;
12247 int i;
12248 Lisp_Object enabled_p;
12249 int prop_idx;
12250 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12251 int mouse_down_p, rc;
12253 /* Function note_mouse_highlight is called with negative X/Y
12254 values when mouse moves outside of the frame. */
12255 if (x <= 0 || y <= 0)
12257 clear_mouse_face (hlinfo);
12258 return;
12261 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12262 if (rc < 0)
12264 /* Not on tool-bar item. */
12265 clear_mouse_face (hlinfo);
12266 return;
12268 else if (rc == 0)
12269 /* On same tool-bar item as before. */
12270 goto set_help_echo;
12272 clear_mouse_face (hlinfo);
12274 /* Mouse is down, but on different tool-bar item? */
12275 mouse_down_p = (dpyinfo->grabbed
12276 && f == last_mouse_frame
12277 && FRAME_LIVE_P (f));
12278 if (mouse_down_p
12279 && last_tool_bar_item != prop_idx)
12280 return;
12282 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12284 /* If tool-bar item is not enabled, don't highlight it. */
12285 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12286 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12288 /* Compute the x-position of the glyph. In front and past the
12289 image is a space. We include this in the highlighted area. */
12290 row = MATRIX_ROW (w->current_matrix, vpos);
12291 for (i = x = 0; i < hpos; ++i)
12292 x += row->glyphs[TEXT_AREA][i].pixel_width;
12294 /* Record this as the current active region. */
12295 hlinfo->mouse_face_beg_col = hpos;
12296 hlinfo->mouse_face_beg_row = vpos;
12297 hlinfo->mouse_face_beg_x = x;
12298 hlinfo->mouse_face_beg_y = row->y;
12299 hlinfo->mouse_face_past_end = 0;
12301 hlinfo->mouse_face_end_col = hpos + 1;
12302 hlinfo->mouse_face_end_row = vpos;
12303 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12304 hlinfo->mouse_face_end_y = row->y;
12305 hlinfo->mouse_face_window = window;
12306 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12308 /* Display it as active. */
12309 show_mouse_face (hlinfo, draw);
12312 set_help_echo:
12314 /* Set help_echo_string to a help string to display for this tool-bar item.
12315 XTread_socket does the rest. */
12316 help_echo_object = help_echo_window = Qnil;
12317 help_echo_pos = -1;
12318 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12319 if (NILP (help_echo_string))
12320 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12323 #endif /* HAVE_WINDOW_SYSTEM */
12327 /************************************************************************
12328 Horizontal scrolling
12329 ************************************************************************/
12331 static int hscroll_window_tree (Lisp_Object);
12332 static int hscroll_windows (Lisp_Object);
12334 /* For all leaf windows in the window tree rooted at WINDOW, set their
12335 hscroll value so that PT is (i) visible in the window, and (ii) so
12336 that it is not within a certain margin at the window's left and
12337 right border. Value is non-zero if any window's hscroll has been
12338 changed. */
12340 static int
12341 hscroll_window_tree (Lisp_Object window)
12343 int hscrolled_p = 0;
12344 int hscroll_relative_p = FLOATP (Vhscroll_step);
12345 int hscroll_step_abs = 0;
12346 double hscroll_step_rel = 0;
12348 if (hscroll_relative_p)
12350 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12351 if (hscroll_step_rel < 0)
12353 hscroll_relative_p = 0;
12354 hscroll_step_abs = 0;
12357 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12359 hscroll_step_abs = XINT (Vhscroll_step);
12360 if (hscroll_step_abs < 0)
12361 hscroll_step_abs = 0;
12363 else
12364 hscroll_step_abs = 0;
12366 while (WINDOWP (window))
12368 struct window *w = XWINDOW (window);
12370 if (WINDOWP (w->contents))
12371 hscrolled_p |= hscroll_window_tree (w->contents);
12372 else if (w->cursor.vpos >= 0)
12374 int h_margin;
12375 int text_area_width;
12376 struct glyph_row *current_cursor_row
12377 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12378 struct glyph_row *desired_cursor_row
12379 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12380 struct glyph_row *cursor_row
12381 = (desired_cursor_row->enabled_p
12382 ? desired_cursor_row
12383 : current_cursor_row);
12384 int row_r2l_p = cursor_row->reversed_p;
12386 text_area_width = window_box_width (w, TEXT_AREA);
12388 /* Scroll when cursor is inside this scroll margin. */
12389 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12391 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12392 /* For left-to-right rows, hscroll when cursor is either
12393 (i) inside the right hscroll margin, or (ii) if it is
12394 inside the left margin and the window is already
12395 hscrolled. */
12396 && ((!row_r2l_p
12397 && ((w->hscroll
12398 && w->cursor.x <= h_margin)
12399 || (cursor_row->enabled_p
12400 && cursor_row->truncated_on_right_p
12401 && (w->cursor.x >= text_area_width - h_margin))))
12402 /* For right-to-left rows, the logic is similar,
12403 except that rules for scrolling to left and right
12404 are reversed. E.g., if cursor.x <= h_margin, we
12405 need to hscroll "to the right" unconditionally,
12406 and that will scroll the screen to the left so as
12407 to reveal the next portion of the row. */
12408 || (row_r2l_p
12409 && ((cursor_row->enabled_p
12410 /* FIXME: It is confusing to set the
12411 truncated_on_right_p flag when R2L rows
12412 are actually truncated on the left. */
12413 && cursor_row->truncated_on_right_p
12414 && w->cursor.x <= h_margin)
12415 || (w->hscroll
12416 && (w->cursor.x >= text_area_width - h_margin))))))
12418 struct it it;
12419 ptrdiff_t hscroll;
12420 struct buffer *saved_current_buffer;
12421 ptrdiff_t pt;
12422 int wanted_x;
12424 /* Find point in a display of infinite width. */
12425 saved_current_buffer = current_buffer;
12426 current_buffer = XBUFFER (w->contents);
12428 if (w == XWINDOW (selected_window))
12429 pt = PT;
12430 else
12431 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12433 /* Move iterator to pt starting at cursor_row->start in
12434 a line with infinite width. */
12435 init_to_row_start (&it, w, cursor_row);
12436 it.last_visible_x = INFINITY;
12437 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12438 current_buffer = saved_current_buffer;
12440 /* Position cursor in window. */
12441 if (!hscroll_relative_p && hscroll_step_abs == 0)
12442 hscroll = max (0, (it.current_x
12443 - (ITERATOR_AT_END_OF_LINE_P (&it)
12444 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12445 : (text_area_width / 2))))
12446 / FRAME_COLUMN_WIDTH (it.f);
12447 else if ((!row_r2l_p
12448 && w->cursor.x >= text_area_width - h_margin)
12449 || (row_r2l_p && w->cursor.x <= h_margin))
12451 if (hscroll_relative_p)
12452 wanted_x = text_area_width * (1 - hscroll_step_rel)
12453 - h_margin;
12454 else
12455 wanted_x = text_area_width
12456 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12457 - h_margin;
12458 hscroll
12459 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12461 else
12463 if (hscroll_relative_p)
12464 wanted_x = text_area_width * hscroll_step_rel
12465 + h_margin;
12466 else
12467 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12468 + h_margin;
12469 hscroll
12470 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12472 hscroll = max (hscroll, w->min_hscroll);
12474 /* Don't prevent redisplay optimizations if hscroll
12475 hasn't changed, as it will unnecessarily slow down
12476 redisplay. */
12477 if (w->hscroll != hscroll)
12479 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12480 w->hscroll = hscroll;
12481 hscrolled_p = 1;
12486 window = w->next;
12489 /* Value is non-zero if hscroll of any leaf window has been changed. */
12490 return hscrolled_p;
12494 /* Set hscroll so that cursor is visible and not inside horizontal
12495 scroll margins for all windows in the tree rooted at WINDOW. See
12496 also hscroll_window_tree above. Value is non-zero if any window's
12497 hscroll has been changed. If it has, desired matrices on the frame
12498 of WINDOW are cleared. */
12500 static int
12501 hscroll_windows (Lisp_Object window)
12503 int hscrolled_p = hscroll_window_tree (window);
12504 if (hscrolled_p)
12505 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12506 return hscrolled_p;
12511 /************************************************************************
12512 Redisplay
12513 ************************************************************************/
12515 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12516 to a non-zero value. This is sometimes handy to have in a debugger
12517 session. */
12519 #ifdef GLYPH_DEBUG
12521 /* First and last unchanged row for try_window_id. */
12523 static int debug_first_unchanged_at_end_vpos;
12524 static int debug_last_unchanged_at_beg_vpos;
12526 /* Delta vpos and y. */
12528 static int debug_dvpos, debug_dy;
12530 /* Delta in characters and bytes for try_window_id. */
12532 static ptrdiff_t debug_delta, debug_delta_bytes;
12534 /* Values of window_end_pos and window_end_vpos at the end of
12535 try_window_id. */
12537 static ptrdiff_t debug_end_vpos;
12539 /* Append a string to W->desired_matrix->method. FMT is a printf
12540 format string. If trace_redisplay_p is non-zero also printf the
12541 resulting string to stderr. */
12543 static void debug_method_add (struct window *, char const *, ...)
12544 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12546 static void
12547 debug_method_add (struct window *w, char const *fmt, ...)
12549 char *method = w->desired_matrix->method;
12550 int len = strlen (method);
12551 int size = sizeof w->desired_matrix->method;
12552 int remaining = size - len - 1;
12553 va_list ap;
12555 if (len && remaining)
12557 method[len] = '|';
12558 --remaining, ++len;
12561 va_start (ap, fmt);
12562 vsnprintf (method + len, remaining + 1, fmt, ap);
12563 va_end (ap);
12565 if (trace_redisplay_p)
12566 fprintf (stderr, "%p (%s): %s\n",
12568 ((BUFFERP (w->contents)
12569 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12570 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12571 : "no buffer"),
12572 method + len);
12575 #endif /* GLYPH_DEBUG */
12578 /* Value is non-zero if all changes in window W, which displays
12579 current_buffer, are in the text between START and END. START is a
12580 buffer position, END is given as a distance from Z. Used in
12581 redisplay_internal for display optimization. */
12583 static int
12584 text_outside_line_unchanged_p (struct window *w,
12585 ptrdiff_t start, ptrdiff_t end)
12587 int unchanged_p = 1;
12589 /* If text or overlays have changed, see where. */
12590 if (window_outdated (w))
12592 /* Gap in the line? */
12593 if (GPT < start || Z - GPT < end)
12594 unchanged_p = 0;
12596 /* Changes start in front of the line, or end after it? */
12597 if (unchanged_p
12598 && (BEG_UNCHANGED < start - 1
12599 || END_UNCHANGED < end))
12600 unchanged_p = 0;
12602 /* If selective display, can't optimize if changes start at the
12603 beginning of the line. */
12604 if (unchanged_p
12605 && INTEGERP (BVAR (current_buffer, selective_display))
12606 && XINT (BVAR (current_buffer, selective_display)) > 0
12607 && (BEG_UNCHANGED < start || GPT <= start))
12608 unchanged_p = 0;
12610 /* If there are overlays at the start or end of the line, these
12611 may have overlay strings with newlines in them. A change at
12612 START, for instance, may actually concern the display of such
12613 overlay strings as well, and they are displayed on different
12614 lines. So, quickly rule out this case. (For the future, it
12615 might be desirable to implement something more telling than
12616 just BEG/END_UNCHANGED.) */
12617 if (unchanged_p)
12619 if (BEG + BEG_UNCHANGED == start
12620 && overlay_touches_p (start))
12621 unchanged_p = 0;
12622 if (END_UNCHANGED == end
12623 && overlay_touches_p (Z - end))
12624 unchanged_p = 0;
12627 /* Under bidi reordering, adding or deleting a character in the
12628 beginning of a paragraph, before the first strong directional
12629 character, can change the base direction of the paragraph (unless
12630 the buffer specifies a fixed paragraph direction), which will
12631 require to redisplay the whole paragraph. It might be worthwhile
12632 to find the paragraph limits and widen the range of redisplayed
12633 lines to that, but for now just give up this optimization. */
12634 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12635 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12636 unchanged_p = 0;
12639 return unchanged_p;
12643 /* Do a frame update, taking possible shortcuts into account. This is
12644 the main external entry point for redisplay.
12646 If the last redisplay displayed an echo area message and that message
12647 is no longer requested, we clear the echo area or bring back the
12648 mini-buffer if that is in use. */
12650 void
12651 redisplay (void)
12653 redisplay_internal ();
12657 static Lisp_Object
12658 overlay_arrow_string_or_property (Lisp_Object var)
12660 Lisp_Object val;
12662 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12663 return val;
12665 return Voverlay_arrow_string;
12668 /* Return 1 if there are any overlay-arrows in current_buffer. */
12669 static int
12670 overlay_arrow_in_current_buffer_p (void)
12672 Lisp_Object vlist;
12674 for (vlist = Voverlay_arrow_variable_list;
12675 CONSP (vlist);
12676 vlist = XCDR (vlist))
12678 Lisp_Object var = XCAR (vlist);
12679 Lisp_Object val;
12681 if (!SYMBOLP (var))
12682 continue;
12683 val = find_symbol_value (var);
12684 if (MARKERP (val)
12685 && current_buffer == XMARKER (val)->buffer)
12686 return 1;
12688 return 0;
12692 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12693 has changed. */
12695 static int
12696 overlay_arrows_changed_p (void)
12698 Lisp_Object vlist;
12700 for (vlist = Voverlay_arrow_variable_list;
12701 CONSP (vlist);
12702 vlist = XCDR (vlist))
12704 Lisp_Object var = XCAR (vlist);
12705 Lisp_Object val, pstr;
12707 if (!SYMBOLP (var))
12708 continue;
12709 val = find_symbol_value (var);
12710 if (!MARKERP (val))
12711 continue;
12712 if (! EQ (COERCE_MARKER (val),
12713 Fget (var, Qlast_arrow_position))
12714 || ! (pstr = overlay_arrow_string_or_property (var),
12715 EQ (pstr, Fget (var, Qlast_arrow_string))))
12716 return 1;
12718 return 0;
12721 /* Mark overlay arrows to be updated on next redisplay. */
12723 static void
12724 update_overlay_arrows (int up_to_date)
12726 Lisp_Object vlist;
12728 for (vlist = Voverlay_arrow_variable_list;
12729 CONSP (vlist);
12730 vlist = XCDR (vlist))
12732 Lisp_Object var = XCAR (vlist);
12734 if (!SYMBOLP (var))
12735 continue;
12737 if (up_to_date > 0)
12739 Lisp_Object val = find_symbol_value (var);
12740 Fput (var, Qlast_arrow_position,
12741 COERCE_MARKER (val));
12742 Fput (var, Qlast_arrow_string,
12743 overlay_arrow_string_or_property (var));
12745 else if (up_to_date < 0
12746 || !NILP (Fget (var, Qlast_arrow_position)))
12748 Fput (var, Qlast_arrow_position, Qt);
12749 Fput (var, Qlast_arrow_string, Qt);
12755 /* Return overlay arrow string to display at row.
12756 Return integer (bitmap number) for arrow bitmap in left fringe.
12757 Return nil if no overlay arrow. */
12759 static Lisp_Object
12760 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12762 Lisp_Object vlist;
12764 for (vlist = Voverlay_arrow_variable_list;
12765 CONSP (vlist);
12766 vlist = XCDR (vlist))
12768 Lisp_Object var = XCAR (vlist);
12769 Lisp_Object val;
12771 if (!SYMBOLP (var))
12772 continue;
12774 val = find_symbol_value (var);
12776 if (MARKERP (val)
12777 && current_buffer == XMARKER (val)->buffer
12778 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12780 if (FRAME_WINDOW_P (it->f)
12781 /* FIXME: if ROW->reversed_p is set, this should test
12782 the right fringe, not the left one. */
12783 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12785 #ifdef HAVE_WINDOW_SYSTEM
12786 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12788 int fringe_bitmap;
12789 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12790 return make_number (fringe_bitmap);
12792 #endif
12793 return make_number (-1); /* Use default arrow bitmap. */
12795 return overlay_arrow_string_or_property (var);
12799 return Qnil;
12802 /* Return 1 if point moved out of or into a composition. Otherwise
12803 return 0. PREV_BUF and PREV_PT are the last point buffer and
12804 position. BUF and PT are the current point buffer and position. */
12806 static int
12807 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12808 struct buffer *buf, ptrdiff_t pt)
12810 ptrdiff_t start, end;
12811 Lisp_Object prop;
12812 Lisp_Object buffer;
12814 XSETBUFFER (buffer, buf);
12815 /* Check a composition at the last point if point moved within the
12816 same buffer. */
12817 if (prev_buf == buf)
12819 if (prev_pt == pt)
12820 /* Point didn't move. */
12821 return 0;
12823 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12824 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12825 && COMPOSITION_VALID_P (start, end, prop)
12826 && start < prev_pt && end > prev_pt)
12827 /* The last point was within the composition. Return 1 iff
12828 point moved out of the composition. */
12829 return (pt <= start || pt >= end);
12832 /* Check a composition at the current point. */
12833 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12834 && find_composition (pt, -1, &start, &end, &prop, buffer)
12835 && COMPOSITION_VALID_P (start, end, prop)
12836 && start < pt && end > pt);
12840 /* Reconsider the setting of B->clip_changed which is displayed
12841 in window W. */
12843 static void
12844 reconsider_clip_changes (struct window *w, struct buffer *b)
12846 if (b->clip_changed
12847 && w->window_end_valid
12848 && w->current_matrix->buffer == b
12849 && w->current_matrix->zv == BUF_ZV (b)
12850 && w->current_matrix->begv == BUF_BEGV (b))
12851 b->clip_changed = 0;
12853 /* If display wasn't paused, and W is not a tool bar window, see if
12854 point has been moved into or out of a composition. In that case,
12855 we set b->clip_changed to 1 to force updating the screen. If
12856 b->clip_changed has already been set to 1, we can skip this
12857 check. */
12858 if (!b->clip_changed && BUFFERP (w->contents) && w->window_end_valid)
12860 ptrdiff_t pt;
12862 if (w == XWINDOW (selected_window))
12863 pt = PT;
12864 else
12865 pt = marker_position (w->pointm);
12867 if ((w->current_matrix->buffer != XBUFFER (w->contents)
12868 || pt != w->last_point)
12869 && check_point_in_composition (w->current_matrix->buffer,
12870 w->last_point,
12871 XBUFFER (w->contents), pt))
12872 b->clip_changed = 1;
12877 #define STOP_POLLING \
12878 do { if (! polling_stopped_here) stop_polling (); \
12879 polling_stopped_here = 1; } while (0)
12881 #define RESUME_POLLING \
12882 do { if (polling_stopped_here) start_polling (); \
12883 polling_stopped_here = 0; } while (0)
12886 /* Perhaps in the future avoid recentering windows if it
12887 is not necessary; currently that causes some problems. */
12889 static void
12890 redisplay_internal (void)
12892 struct window *w = XWINDOW (selected_window);
12893 struct window *sw;
12894 struct frame *fr;
12895 int pending;
12896 int must_finish = 0;
12897 struct text_pos tlbufpos, tlendpos;
12898 int number_of_visible_frames;
12899 ptrdiff_t count, count1;
12900 struct frame *sf;
12901 int polling_stopped_here = 0;
12902 Lisp_Object tail, frame;
12904 /* Non-zero means redisplay has to consider all windows on all
12905 frames. Zero means, only selected_window is considered. */
12906 int consider_all_windows_p;
12908 /* Non-zero means redisplay has to redisplay the miniwindow. */
12909 int update_miniwindow_p = 0;
12911 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12913 /* No redisplay if running in batch mode or frame is not yet fully
12914 initialized, or redisplay is explicitly turned off by setting
12915 Vinhibit_redisplay. */
12916 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12917 || !NILP (Vinhibit_redisplay))
12918 return;
12920 /* Don't examine these until after testing Vinhibit_redisplay.
12921 When Emacs is shutting down, perhaps because its connection to
12922 X has dropped, we should not look at them at all. */
12923 fr = XFRAME (w->frame);
12924 sf = SELECTED_FRAME ();
12926 if (!fr->glyphs_initialized_p)
12927 return;
12929 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12930 if (popup_activated ())
12931 return;
12932 #endif
12934 /* I don't think this happens but let's be paranoid. */
12935 if (redisplaying_p)
12936 return;
12938 /* Record a function that clears redisplaying_p
12939 when we leave this function. */
12940 count = SPECPDL_INDEX ();
12941 record_unwind_protect (unwind_redisplay, selected_frame);
12942 redisplaying_p = 1;
12943 specbind (Qinhibit_free_realized_faces, Qnil);
12945 /* Record this function, so it appears on the profiler's backtraces. */
12946 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12948 FOR_EACH_FRAME (tail, frame)
12949 XFRAME (frame)->already_hscrolled_p = 0;
12951 retry:
12952 /* Remember the currently selected window. */
12953 sw = w;
12955 pending = 0;
12956 reconsider_clip_changes (w, current_buffer);
12957 last_escape_glyph_frame = NULL;
12958 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12959 last_glyphless_glyph_frame = NULL;
12960 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12962 /* If new fonts have been loaded that make a glyph matrix adjustment
12963 necessary, do it. */
12964 if (fonts_changed_p)
12966 adjust_glyphs (NULL);
12967 ++windows_or_buffers_changed;
12968 fonts_changed_p = 0;
12971 /* If face_change_count is non-zero, init_iterator will free all
12972 realized faces, which includes the faces referenced from current
12973 matrices. So, we can't reuse current matrices in this case. */
12974 if (face_change_count)
12975 ++windows_or_buffers_changed;
12977 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12978 && FRAME_TTY (sf)->previous_frame != sf)
12980 /* Since frames on a single ASCII terminal share the same
12981 display area, displaying a different frame means redisplay
12982 the whole thing. */
12983 windows_or_buffers_changed++;
12984 SET_FRAME_GARBAGED (sf);
12985 #ifndef DOS_NT
12986 set_tty_color_mode (FRAME_TTY (sf), sf);
12987 #endif
12988 FRAME_TTY (sf)->previous_frame = sf;
12991 /* Set the visible flags for all frames. Do this before checking for
12992 resized or garbaged frames; they want to know if their frames are
12993 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12994 number_of_visible_frames = 0;
12996 FOR_EACH_FRAME (tail, frame)
12998 struct frame *f = XFRAME (frame);
13000 if (FRAME_VISIBLE_P (f))
13001 ++number_of_visible_frames;
13002 clear_desired_matrices (f);
13005 /* Notice any pending interrupt request to change frame size. */
13006 do_pending_window_change (1);
13008 /* do_pending_window_change could change the selected_window due to
13009 frame resizing which makes the selected window too small. */
13010 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13012 sw = w;
13013 reconsider_clip_changes (w, current_buffer);
13016 /* Clear frames marked as garbaged. */
13017 clear_garbaged_frames ();
13019 /* Build menubar and tool-bar items. */
13020 if (NILP (Vmemory_full))
13021 prepare_menu_bars ();
13023 if (windows_or_buffers_changed)
13024 update_mode_lines++;
13026 /* Detect case that we need to write or remove a star in the mode line. */
13027 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13029 w->update_mode_line = 1;
13030 if (buffer_shared_and_changed ())
13031 update_mode_lines++;
13034 /* Avoid invocation of point motion hooks by `current_column' below. */
13035 count1 = SPECPDL_INDEX ();
13036 specbind (Qinhibit_point_motion_hooks, Qt);
13038 if (mode_line_update_needed (w))
13039 w->update_mode_line = 1;
13041 unbind_to (count1, Qnil);
13043 consider_all_windows_p = (update_mode_lines
13044 || buffer_shared_and_changed ()
13045 || cursor_type_changed);
13047 /* If specs for an arrow have changed, do thorough redisplay
13048 to ensure we remove any arrow that should no longer exist. */
13049 if (overlay_arrows_changed_p ())
13050 consider_all_windows_p = windows_or_buffers_changed = 1;
13052 /* Normally the message* functions will have already displayed and
13053 updated the echo area, but the frame may have been trashed, or
13054 the update may have been preempted, so display the echo area
13055 again here. Checking message_cleared_p captures the case that
13056 the echo area should be cleared. */
13057 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13058 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13059 || (message_cleared_p
13060 && minibuf_level == 0
13061 /* If the mini-window is currently selected, this means the
13062 echo-area doesn't show through. */
13063 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13065 int window_height_changed_p = echo_area_display (0);
13067 if (message_cleared_p)
13068 update_miniwindow_p = 1;
13070 must_finish = 1;
13072 /* If we don't display the current message, don't clear the
13073 message_cleared_p flag, because, if we did, we wouldn't clear
13074 the echo area in the next redisplay which doesn't preserve
13075 the echo area. */
13076 if (!display_last_displayed_message_p)
13077 message_cleared_p = 0;
13079 if (fonts_changed_p)
13080 goto retry;
13081 else if (window_height_changed_p)
13083 consider_all_windows_p = 1;
13084 ++update_mode_lines;
13085 ++windows_or_buffers_changed;
13087 /* If window configuration was changed, frames may have been
13088 marked garbaged. Clear them or we will experience
13089 surprises wrt scrolling. */
13090 clear_garbaged_frames ();
13093 else if (EQ (selected_window, minibuf_window)
13094 && (current_buffer->clip_changed || window_outdated (w))
13095 && resize_mini_window (w, 0))
13097 /* Resized active mini-window to fit the size of what it is
13098 showing if its contents might have changed. */
13099 must_finish = 1;
13100 /* FIXME: this causes all frames to be updated, which seems unnecessary
13101 since only the current frame needs to be considered. This function
13102 needs to be rewritten with two variables, consider_all_windows and
13103 consider_all_frames. */
13104 consider_all_windows_p = 1;
13105 ++windows_or_buffers_changed;
13106 ++update_mode_lines;
13108 /* If window configuration was changed, frames may have been
13109 marked garbaged. Clear them or we will experience
13110 surprises wrt scrolling. */
13111 clear_garbaged_frames ();
13114 /* If showing the region, and mark has changed, we must redisplay
13115 the whole window. The assignment to this_line_start_pos prevents
13116 the optimization directly below this if-statement. */
13117 if (((!NILP (Vtransient_mark_mode)
13118 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13119 != (w->region_showing > 0))
13120 || (w->region_showing
13121 && w->region_showing
13122 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13123 CHARPOS (this_line_start_pos) = 0;
13125 /* Optimize the case that only the line containing the cursor in the
13126 selected window has changed. Variables starting with this_ are
13127 set in display_line and record information about the line
13128 containing the cursor. */
13129 tlbufpos = this_line_start_pos;
13130 tlendpos = this_line_end_pos;
13131 if (!consider_all_windows_p
13132 && CHARPOS (tlbufpos) > 0
13133 && !w->update_mode_line
13134 && !current_buffer->clip_changed
13135 && !current_buffer->prevent_redisplay_optimizations_p
13136 && FRAME_VISIBLE_P (XFRAME (w->frame))
13137 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13138 /* Make sure recorded data applies to current buffer, etc. */
13139 && this_line_buffer == current_buffer
13140 && current_buffer == XBUFFER (w->contents)
13141 && !w->force_start
13142 && !w->optional_new_start
13143 /* Point must be on the line that we have info recorded about. */
13144 && PT >= CHARPOS (tlbufpos)
13145 && PT <= Z - CHARPOS (tlendpos)
13146 /* All text outside that line, including its final newline,
13147 must be unchanged. */
13148 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13149 CHARPOS (tlendpos)))
13151 if (CHARPOS (tlbufpos) > BEGV
13152 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13153 && (CHARPOS (tlbufpos) == ZV
13154 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13155 /* Former continuation line has disappeared by becoming empty. */
13156 goto cancel;
13157 else if (window_outdated (w) || MINI_WINDOW_P (w))
13159 /* We have to handle the case of continuation around a
13160 wide-column character (see the comment in indent.c around
13161 line 1340).
13163 For instance, in the following case:
13165 -------- Insert --------
13166 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13167 J_I_ ==> J_I_ `^^' are cursors.
13168 ^^ ^^
13169 -------- --------
13171 As we have to redraw the line above, we cannot use this
13172 optimization. */
13174 struct it it;
13175 int line_height_before = this_line_pixel_height;
13177 /* Note that start_display will handle the case that the
13178 line starting at tlbufpos is a continuation line. */
13179 start_display (&it, w, tlbufpos);
13181 /* Implementation note: It this still necessary? */
13182 if (it.current_x != this_line_start_x)
13183 goto cancel;
13185 TRACE ((stderr, "trying display optimization 1\n"));
13186 w->cursor.vpos = -1;
13187 overlay_arrow_seen = 0;
13188 it.vpos = this_line_vpos;
13189 it.current_y = this_line_y;
13190 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13191 display_line (&it);
13193 /* If line contains point, is not continued,
13194 and ends at same distance from eob as before, we win. */
13195 if (w->cursor.vpos >= 0
13196 /* Line is not continued, otherwise this_line_start_pos
13197 would have been set to 0 in display_line. */
13198 && CHARPOS (this_line_start_pos)
13199 /* Line ends as before. */
13200 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13201 /* Line has same height as before. Otherwise other lines
13202 would have to be shifted up or down. */
13203 && this_line_pixel_height == line_height_before)
13205 /* If this is not the window's last line, we must adjust
13206 the charstarts of the lines below. */
13207 if (it.current_y < it.last_visible_y)
13209 struct glyph_row *row
13210 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13211 ptrdiff_t delta, delta_bytes;
13213 /* We used to distinguish between two cases here,
13214 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13215 when the line ends in a newline or the end of the
13216 buffer's accessible portion. But both cases did
13217 the same, so they were collapsed. */
13218 delta = (Z
13219 - CHARPOS (tlendpos)
13220 - MATRIX_ROW_START_CHARPOS (row));
13221 delta_bytes = (Z_BYTE
13222 - BYTEPOS (tlendpos)
13223 - MATRIX_ROW_START_BYTEPOS (row));
13225 increment_matrix_positions (w->current_matrix,
13226 this_line_vpos + 1,
13227 w->current_matrix->nrows,
13228 delta, delta_bytes);
13231 /* If this row displays text now but previously didn't,
13232 or vice versa, w->window_end_vpos may have to be
13233 adjusted. */
13234 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13236 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13237 wset_window_end_vpos (w, make_number (this_line_vpos));
13239 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13240 && this_line_vpos > 0)
13241 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13242 w->window_end_valid = 0;
13244 /* Update hint: No need to try to scroll in update_window. */
13245 w->desired_matrix->no_scrolling_p = 1;
13247 #ifdef GLYPH_DEBUG
13248 *w->desired_matrix->method = 0;
13249 debug_method_add (w, "optimization 1");
13250 #endif
13251 #ifdef HAVE_WINDOW_SYSTEM
13252 update_window_fringes (w, 0);
13253 #endif
13254 goto update;
13256 else
13257 goto cancel;
13259 else if (/* Cursor position hasn't changed. */
13260 PT == w->last_point
13261 /* Make sure the cursor was last displayed
13262 in this window. Otherwise we have to reposition it. */
13263 && 0 <= w->cursor.vpos
13264 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13266 if (!must_finish)
13268 do_pending_window_change (1);
13269 /* If selected_window changed, redisplay again. */
13270 if (WINDOWP (selected_window)
13271 && (w = XWINDOW (selected_window)) != sw)
13272 goto retry;
13274 /* We used to always goto end_of_redisplay here, but this
13275 isn't enough if we have a blinking cursor. */
13276 if (w->cursor_off_p == w->last_cursor_off_p)
13277 goto end_of_redisplay;
13279 goto update;
13281 /* If highlighting the region, or if the cursor is in the echo area,
13282 then we can't just move the cursor. */
13283 else if (! (!NILP (Vtransient_mark_mode)
13284 && !NILP (BVAR (current_buffer, mark_active)))
13285 && (EQ (selected_window,
13286 BVAR (current_buffer, last_selected_window))
13287 || highlight_nonselected_windows)
13288 && !w->region_showing
13289 && NILP (Vshow_trailing_whitespace)
13290 && !cursor_in_echo_area)
13292 struct it it;
13293 struct glyph_row *row;
13295 /* Skip from tlbufpos to PT and see where it is. Note that
13296 PT may be in invisible text. If so, we will end at the
13297 next visible position. */
13298 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13299 NULL, DEFAULT_FACE_ID);
13300 it.current_x = this_line_start_x;
13301 it.current_y = this_line_y;
13302 it.vpos = this_line_vpos;
13304 /* The call to move_it_to stops in front of PT, but
13305 moves over before-strings. */
13306 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13308 if (it.vpos == this_line_vpos
13309 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13310 row->enabled_p))
13312 eassert (this_line_vpos == it.vpos);
13313 eassert (this_line_y == it.current_y);
13314 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13315 #ifdef GLYPH_DEBUG
13316 *w->desired_matrix->method = 0;
13317 debug_method_add (w, "optimization 3");
13318 #endif
13319 goto update;
13321 else
13322 goto cancel;
13325 cancel:
13326 /* Text changed drastically or point moved off of line. */
13327 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13330 CHARPOS (this_line_start_pos) = 0;
13331 consider_all_windows_p |= buffer_shared_and_changed ();
13332 ++clear_face_cache_count;
13333 #ifdef HAVE_WINDOW_SYSTEM
13334 ++clear_image_cache_count;
13335 #endif
13337 /* Build desired matrices, and update the display. If
13338 consider_all_windows_p is non-zero, do it for all windows on all
13339 frames. Otherwise do it for selected_window, only. */
13341 if (consider_all_windows_p)
13343 FOR_EACH_FRAME (tail, frame)
13344 XFRAME (frame)->updated_p = 0;
13346 FOR_EACH_FRAME (tail, frame)
13348 struct frame *f = XFRAME (frame);
13350 /* We don't have to do anything for unselected terminal
13351 frames. */
13352 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13353 && !EQ (FRAME_TTY (f)->top_frame, frame))
13354 continue;
13356 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13358 /* Mark all the scroll bars to be removed; we'll redeem
13359 the ones we want when we redisplay their windows. */
13360 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13361 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13363 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13364 redisplay_windows (FRAME_ROOT_WINDOW (f));
13366 /* The X error handler may have deleted that frame. */
13367 if (!FRAME_LIVE_P (f))
13368 continue;
13370 /* Any scroll bars which redisplay_windows should have
13371 nuked should now go away. */
13372 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13373 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13375 /* If fonts changed, display again. */
13376 /* ??? rms: I suspect it is a mistake to jump all the way
13377 back to retry here. It should just retry this frame. */
13378 if (fonts_changed_p)
13379 goto retry;
13381 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13383 /* See if we have to hscroll. */
13384 if (!f->already_hscrolled_p)
13386 f->already_hscrolled_p = 1;
13387 if (hscroll_windows (f->root_window))
13388 goto retry;
13391 /* Prevent various kinds of signals during display
13392 update. stdio is not robust about handling
13393 signals, which can cause an apparent I/O
13394 error. */
13395 if (interrupt_input)
13396 unrequest_sigio ();
13397 STOP_POLLING;
13399 /* Update the display. */
13400 set_window_update_flags (XWINDOW (f->root_window), 1);
13401 pending |= update_frame (f, 0, 0);
13402 f->updated_p = 1;
13407 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13409 if (!pending)
13411 /* Do the mark_window_display_accurate after all windows have
13412 been redisplayed because this call resets flags in buffers
13413 which are needed for proper redisplay. */
13414 FOR_EACH_FRAME (tail, frame)
13416 struct frame *f = XFRAME (frame);
13417 if (f->updated_p)
13419 mark_window_display_accurate (f->root_window, 1);
13420 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13421 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13426 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13428 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13429 struct frame *mini_frame;
13431 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13432 /* Use list_of_error, not Qerror, so that
13433 we catch only errors and don't run the debugger. */
13434 internal_condition_case_1 (redisplay_window_1, selected_window,
13435 list_of_error,
13436 redisplay_window_error);
13437 if (update_miniwindow_p)
13438 internal_condition_case_1 (redisplay_window_1, mini_window,
13439 list_of_error,
13440 redisplay_window_error);
13442 /* Compare desired and current matrices, perform output. */
13444 update:
13445 /* If fonts changed, display again. */
13446 if (fonts_changed_p)
13447 goto retry;
13449 /* Prevent various kinds of signals during display update.
13450 stdio is not robust about handling signals,
13451 which can cause an apparent I/O error. */
13452 if (interrupt_input)
13453 unrequest_sigio ();
13454 STOP_POLLING;
13456 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13458 if (hscroll_windows (selected_window))
13459 goto retry;
13461 XWINDOW (selected_window)->must_be_updated_p = 1;
13462 pending = update_frame (sf, 0, 0);
13465 /* We may have called echo_area_display at the top of this
13466 function. If the echo area is on another frame, that may
13467 have put text on a frame other than the selected one, so the
13468 above call to update_frame would not have caught it. Catch
13469 it here. */
13470 mini_window = FRAME_MINIBUF_WINDOW (sf);
13471 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13473 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13475 XWINDOW (mini_window)->must_be_updated_p = 1;
13476 pending |= update_frame (mini_frame, 0, 0);
13477 if (!pending && hscroll_windows (mini_window))
13478 goto retry;
13482 /* If display was paused because of pending input, make sure we do a
13483 thorough update the next time. */
13484 if (pending)
13486 /* Prevent the optimization at the beginning of
13487 redisplay_internal that tries a single-line update of the
13488 line containing the cursor in the selected window. */
13489 CHARPOS (this_line_start_pos) = 0;
13491 /* Let the overlay arrow be updated the next time. */
13492 update_overlay_arrows (0);
13494 /* If we pause after scrolling, some rows in the current
13495 matrices of some windows are not valid. */
13496 if (!WINDOW_FULL_WIDTH_P (w)
13497 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13498 update_mode_lines = 1;
13500 else
13502 if (!consider_all_windows_p)
13504 /* This has already been done above if
13505 consider_all_windows_p is set. */
13506 mark_window_display_accurate_1 (w, 1);
13508 /* Say overlay arrows are up to date. */
13509 update_overlay_arrows (1);
13511 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13512 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13515 update_mode_lines = 0;
13516 windows_or_buffers_changed = 0;
13517 cursor_type_changed = 0;
13520 /* Start SIGIO interrupts coming again. Having them off during the
13521 code above makes it less likely one will discard output, but not
13522 impossible, since there might be stuff in the system buffer here.
13523 But it is much hairier to try to do anything about that. */
13524 if (interrupt_input)
13525 request_sigio ();
13526 RESUME_POLLING;
13528 /* If a frame has become visible which was not before, redisplay
13529 again, so that we display it. Expose events for such a frame
13530 (which it gets when becoming visible) don't call the parts of
13531 redisplay constructing glyphs, so simply exposing a frame won't
13532 display anything in this case. So, we have to display these
13533 frames here explicitly. */
13534 if (!pending)
13536 int new_count = 0;
13538 FOR_EACH_FRAME (tail, frame)
13540 int this_is_visible = 0;
13542 if (XFRAME (frame)->visible)
13543 this_is_visible = 1;
13545 if (this_is_visible)
13546 new_count++;
13549 if (new_count != number_of_visible_frames)
13550 windows_or_buffers_changed++;
13553 /* Change frame size now if a change is pending. */
13554 do_pending_window_change (1);
13556 /* If we just did a pending size change, or have additional
13557 visible frames, or selected_window changed, redisplay again. */
13558 if ((windows_or_buffers_changed && !pending)
13559 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13560 goto retry;
13562 /* Clear the face and image caches.
13564 We used to do this only if consider_all_windows_p. But the cache
13565 needs to be cleared if a timer creates images in the current
13566 buffer (e.g. the test case in Bug#6230). */
13568 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13570 clear_face_cache (0);
13571 clear_face_cache_count = 0;
13574 #ifdef HAVE_WINDOW_SYSTEM
13575 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13577 clear_image_caches (Qnil);
13578 clear_image_cache_count = 0;
13580 #endif /* HAVE_WINDOW_SYSTEM */
13582 end_of_redisplay:
13583 unbind_to (count, Qnil);
13584 RESUME_POLLING;
13588 /* Redisplay, but leave alone any recent echo area message unless
13589 another message has been requested in its place.
13591 This is useful in situations where you need to redisplay but no
13592 user action has occurred, making it inappropriate for the message
13593 area to be cleared. See tracking_off and
13594 wait_reading_process_output for examples of these situations.
13596 FROM_WHERE is an integer saying from where this function was
13597 called. This is useful for debugging. */
13599 void
13600 redisplay_preserve_echo_area (int from_where)
13602 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13604 if (!NILP (echo_area_buffer[1]))
13606 /* We have a previously displayed message, but no current
13607 message. Redisplay the previous message. */
13608 display_last_displayed_message_p = 1;
13609 redisplay_internal ();
13610 display_last_displayed_message_p = 0;
13612 else
13613 redisplay_internal ();
13615 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13616 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13617 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13621 /* Function registered with record_unwind_protect in redisplay_internal.
13622 Clear redisplaying_p. Also select the previously selected frame. */
13624 static Lisp_Object
13625 unwind_redisplay (Lisp_Object old_frame)
13627 redisplaying_p = 0;
13628 return Qnil;
13632 /* Mark the display of leaf window W as accurate or inaccurate.
13633 If ACCURATE_P is non-zero mark display of W as accurate. If
13634 ACCURATE_P is zero, arrange for W to be redisplayed the next
13635 time redisplay_internal is called. */
13637 static void
13638 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13640 struct buffer *b = XBUFFER (w->contents);
13642 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13643 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13644 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13646 if (accurate_p)
13648 b->clip_changed = 0;
13649 b->prevent_redisplay_optimizations_p = 0;
13651 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13652 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13653 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13654 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13656 w->current_matrix->buffer = b;
13657 w->current_matrix->begv = BUF_BEGV (b);
13658 w->current_matrix->zv = BUF_ZV (b);
13660 w->last_cursor = w->cursor;
13661 w->last_cursor_off_p = w->cursor_off_p;
13663 if (w == XWINDOW (selected_window))
13664 w->last_point = BUF_PT (b);
13665 else
13666 w->last_point = marker_position (w->pointm);
13668 w->window_end_valid = 1;
13669 w->update_mode_line = 0;
13674 /* Mark the display of windows in the window tree rooted at WINDOW as
13675 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13676 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13677 be redisplayed the next time redisplay_internal is called. */
13679 void
13680 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13682 struct window *w;
13684 for (; !NILP (window); window = w->next)
13686 w = XWINDOW (window);
13687 if (WINDOWP (w->contents))
13688 mark_window_display_accurate (w->contents, accurate_p);
13689 else
13690 mark_window_display_accurate_1 (w, accurate_p);
13693 if (accurate_p)
13694 update_overlay_arrows (1);
13695 else
13696 /* Force a thorough redisplay the next time by setting
13697 last_arrow_position and last_arrow_string to t, which is
13698 unequal to any useful value of Voverlay_arrow_... */
13699 update_overlay_arrows (-1);
13703 /* Return value in display table DP (Lisp_Char_Table *) for character
13704 C. Since a display table doesn't have any parent, we don't have to
13705 follow parent. Do not call this function directly but use the
13706 macro DISP_CHAR_VECTOR. */
13708 Lisp_Object
13709 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13711 Lisp_Object val;
13713 if (ASCII_CHAR_P (c))
13715 val = dp->ascii;
13716 if (SUB_CHAR_TABLE_P (val))
13717 val = XSUB_CHAR_TABLE (val)->contents[c];
13719 else
13721 Lisp_Object table;
13723 XSETCHAR_TABLE (table, dp);
13724 val = char_table_ref (table, c);
13726 if (NILP (val))
13727 val = dp->defalt;
13728 return val;
13733 /***********************************************************************
13734 Window Redisplay
13735 ***********************************************************************/
13737 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13739 static void
13740 redisplay_windows (Lisp_Object window)
13742 while (!NILP (window))
13744 struct window *w = XWINDOW (window);
13746 if (WINDOWP (w->contents))
13747 redisplay_windows (w->contents);
13748 else if (BUFFERP (w->contents))
13750 displayed_buffer = XBUFFER (w->contents);
13751 /* Use list_of_error, not Qerror, so that
13752 we catch only errors and don't run the debugger. */
13753 internal_condition_case_1 (redisplay_window_0, window,
13754 list_of_error,
13755 redisplay_window_error);
13758 window = w->next;
13762 static Lisp_Object
13763 redisplay_window_error (Lisp_Object ignore)
13765 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13766 return Qnil;
13769 static Lisp_Object
13770 redisplay_window_0 (Lisp_Object window)
13772 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13773 redisplay_window (window, 0);
13774 return Qnil;
13777 static Lisp_Object
13778 redisplay_window_1 (Lisp_Object window)
13780 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13781 redisplay_window (window, 1);
13782 return Qnil;
13786 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13787 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13788 which positions recorded in ROW differ from current buffer
13789 positions.
13791 Return 0 if cursor is not on this row, 1 otherwise. */
13793 static int
13794 set_cursor_from_row (struct window *w, struct glyph_row *row,
13795 struct glyph_matrix *matrix,
13796 ptrdiff_t delta, ptrdiff_t delta_bytes,
13797 int dy, int dvpos)
13799 struct glyph *glyph = row->glyphs[TEXT_AREA];
13800 struct glyph *end = glyph + row->used[TEXT_AREA];
13801 struct glyph *cursor = NULL;
13802 /* The last known character position in row. */
13803 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13804 int x = row->x;
13805 ptrdiff_t pt_old = PT - delta;
13806 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13807 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13808 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13809 /* A glyph beyond the edge of TEXT_AREA which we should never
13810 touch. */
13811 struct glyph *glyphs_end = end;
13812 /* Non-zero means we've found a match for cursor position, but that
13813 glyph has the avoid_cursor_p flag set. */
13814 int match_with_avoid_cursor = 0;
13815 /* Non-zero means we've seen at least one glyph that came from a
13816 display string. */
13817 int string_seen = 0;
13818 /* Largest and smallest buffer positions seen so far during scan of
13819 glyph row. */
13820 ptrdiff_t bpos_max = pos_before;
13821 ptrdiff_t bpos_min = pos_after;
13822 /* Last buffer position covered by an overlay string with an integer
13823 `cursor' property. */
13824 ptrdiff_t bpos_covered = 0;
13825 /* Non-zero means the display string on which to display the cursor
13826 comes from a text property, not from an overlay. */
13827 int string_from_text_prop = 0;
13829 /* Don't even try doing anything if called for a mode-line or
13830 header-line row, since the rest of the code isn't prepared to
13831 deal with such calamities. */
13832 eassert (!row->mode_line_p);
13833 if (row->mode_line_p)
13834 return 0;
13836 /* Skip over glyphs not having an object at the start and the end of
13837 the row. These are special glyphs like truncation marks on
13838 terminal frames. */
13839 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13841 if (!row->reversed_p)
13843 while (glyph < end
13844 && INTEGERP (glyph->object)
13845 && glyph->charpos < 0)
13847 x += glyph->pixel_width;
13848 ++glyph;
13850 while (end > glyph
13851 && INTEGERP ((end - 1)->object)
13852 /* CHARPOS is zero for blanks and stretch glyphs
13853 inserted by extend_face_to_end_of_line. */
13854 && (end - 1)->charpos <= 0)
13855 --end;
13856 glyph_before = glyph - 1;
13857 glyph_after = end;
13859 else
13861 struct glyph *g;
13863 /* If the glyph row is reversed, we need to process it from back
13864 to front, so swap the edge pointers. */
13865 glyphs_end = end = glyph - 1;
13866 glyph += row->used[TEXT_AREA] - 1;
13868 while (glyph > end + 1
13869 && INTEGERP (glyph->object)
13870 && glyph->charpos < 0)
13872 --glyph;
13873 x -= glyph->pixel_width;
13875 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13876 --glyph;
13877 /* By default, in reversed rows we put the cursor on the
13878 rightmost (first in the reading order) glyph. */
13879 for (g = end + 1; g < glyph; g++)
13880 x += g->pixel_width;
13881 while (end < glyph
13882 && INTEGERP ((end + 1)->object)
13883 && (end + 1)->charpos <= 0)
13884 ++end;
13885 glyph_before = glyph + 1;
13886 glyph_after = end;
13889 else if (row->reversed_p)
13891 /* In R2L rows that don't display text, put the cursor on the
13892 rightmost glyph. Case in point: an empty last line that is
13893 part of an R2L paragraph. */
13894 cursor = end - 1;
13895 /* Avoid placing the cursor on the last glyph of the row, where
13896 on terminal frames we hold the vertical border between
13897 adjacent windows. */
13898 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13899 && !WINDOW_RIGHTMOST_P (w)
13900 && cursor == row->glyphs[LAST_AREA] - 1)
13901 cursor--;
13902 x = -1; /* will be computed below, at label compute_x */
13905 /* Step 1: Try to find the glyph whose character position
13906 corresponds to point. If that's not possible, find 2 glyphs
13907 whose character positions are the closest to point, one before
13908 point, the other after it. */
13909 if (!row->reversed_p)
13910 while (/* not marched to end of glyph row */
13911 glyph < end
13912 /* glyph was not inserted by redisplay for internal purposes */
13913 && !INTEGERP (glyph->object))
13915 if (BUFFERP (glyph->object))
13917 ptrdiff_t dpos = glyph->charpos - pt_old;
13919 if (glyph->charpos > bpos_max)
13920 bpos_max = glyph->charpos;
13921 if (glyph->charpos < bpos_min)
13922 bpos_min = glyph->charpos;
13923 if (!glyph->avoid_cursor_p)
13925 /* If we hit point, we've found the glyph on which to
13926 display the cursor. */
13927 if (dpos == 0)
13929 match_with_avoid_cursor = 0;
13930 break;
13932 /* See if we've found a better approximation to
13933 POS_BEFORE or to POS_AFTER. */
13934 if (0 > dpos && dpos > pos_before - pt_old)
13936 pos_before = glyph->charpos;
13937 glyph_before = glyph;
13939 else if (0 < dpos && dpos < pos_after - pt_old)
13941 pos_after = glyph->charpos;
13942 glyph_after = glyph;
13945 else if (dpos == 0)
13946 match_with_avoid_cursor = 1;
13948 else if (STRINGP (glyph->object))
13950 Lisp_Object chprop;
13951 ptrdiff_t glyph_pos = glyph->charpos;
13953 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13954 glyph->object);
13955 if (!NILP (chprop))
13957 /* If the string came from a `display' text property,
13958 look up the buffer position of that property and
13959 use that position to update bpos_max, as if we
13960 actually saw such a position in one of the row's
13961 glyphs. This helps with supporting integer values
13962 of `cursor' property on the display string in
13963 situations where most or all of the row's buffer
13964 text is completely covered by display properties,
13965 so that no glyph with valid buffer positions is
13966 ever seen in the row. */
13967 ptrdiff_t prop_pos =
13968 string_buffer_position_lim (glyph->object, pos_before,
13969 pos_after, 0);
13971 if (prop_pos >= pos_before)
13972 bpos_max = prop_pos - 1;
13974 if (INTEGERP (chprop))
13976 bpos_covered = bpos_max + XINT (chprop);
13977 /* If the `cursor' property covers buffer positions up
13978 to and including point, we should display cursor on
13979 this glyph. Note that, if a `cursor' property on one
13980 of the string's characters has an integer value, we
13981 will break out of the loop below _before_ we get to
13982 the position match above. IOW, integer values of
13983 the `cursor' property override the "exact match for
13984 point" strategy of positioning the cursor. */
13985 /* Implementation note: bpos_max == pt_old when, e.g.,
13986 we are in an empty line, where bpos_max is set to
13987 MATRIX_ROW_START_CHARPOS, see above. */
13988 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13990 cursor = glyph;
13991 break;
13995 string_seen = 1;
13997 x += glyph->pixel_width;
13998 ++glyph;
14000 else if (glyph > end) /* row is reversed */
14001 while (!INTEGERP (glyph->object))
14003 if (BUFFERP (glyph->object))
14005 ptrdiff_t dpos = glyph->charpos - pt_old;
14007 if (glyph->charpos > bpos_max)
14008 bpos_max = glyph->charpos;
14009 if (glyph->charpos < bpos_min)
14010 bpos_min = glyph->charpos;
14011 if (!glyph->avoid_cursor_p)
14013 if (dpos == 0)
14015 match_with_avoid_cursor = 0;
14016 break;
14018 if (0 > dpos && dpos > pos_before - pt_old)
14020 pos_before = glyph->charpos;
14021 glyph_before = glyph;
14023 else if (0 < dpos && dpos < pos_after - pt_old)
14025 pos_after = glyph->charpos;
14026 glyph_after = glyph;
14029 else if (dpos == 0)
14030 match_with_avoid_cursor = 1;
14032 else if (STRINGP (glyph->object))
14034 Lisp_Object chprop;
14035 ptrdiff_t glyph_pos = glyph->charpos;
14037 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14038 glyph->object);
14039 if (!NILP (chprop))
14041 ptrdiff_t prop_pos =
14042 string_buffer_position_lim (glyph->object, pos_before,
14043 pos_after, 0);
14045 if (prop_pos >= pos_before)
14046 bpos_max = prop_pos - 1;
14048 if (INTEGERP (chprop))
14050 bpos_covered = bpos_max + XINT (chprop);
14051 /* If the `cursor' property covers buffer positions up
14052 to and including point, we should display cursor on
14053 this glyph. */
14054 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14056 cursor = glyph;
14057 break;
14060 string_seen = 1;
14062 --glyph;
14063 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14065 x--; /* can't use any pixel_width */
14066 break;
14068 x -= glyph->pixel_width;
14071 /* Step 2: If we didn't find an exact match for point, we need to
14072 look for a proper place to put the cursor among glyphs between
14073 GLYPH_BEFORE and GLYPH_AFTER. */
14074 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14075 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14076 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14078 /* An empty line has a single glyph whose OBJECT is zero and
14079 whose CHARPOS is the position of a newline on that line.
14080 Note that on a TTY, there are more glyphs after that, which
14081 were produced by extend_face_to_end_of_line, but their
14082 CHARPOS is zero or negative. */
14083 int empty_line_p =
14084 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14085 && INTEGERP (glyph->object) && glyph->charpos > 0
14086 /* On a TTY, continued and truncated rows also have a glyph at
14087 their end whose OBJECT is zero and whose CHARPOS is
14088 positive (the continuation and truncation glyphs), but such
14089 rows are obviously not "empty". */
14090 && !(row->continued_p || row->truncated_on_right_p);
14092 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14094 ptrdiff_t ellipsis_pos;
14096 /* Scan back over the ellipsis glyphs. */
14097 if (!row->reversed_p)
14099 ellipsis_pos = (glyph - 1)->charpos;
14100 while (glyph > row->glyphs[TEXT_AREA]
14101 && (glyph - 1)->charpos == ellipsis_pos)
14102 glyph--, x -= glyph->pixel_width;
14103 /* That loop always goes one position too far, including
14104 the glyph before the ellipsis. So scan forward over
14105 that one. */
14106 x += glyph->pixel_width;
14107 glyph++;
14109 else /* row is reversed */
14111 ellipsis_pos = (glyph + 1)->charpos;
14112 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14113 && (glyph + 1)->charpos == ellipsis_pos)
14114 glyph++, x += glyph->pixel_width;
14115 x -= glyph->pixel_width;
14116 glyph--;
14119 else if (match_with_avoid_cursor)
14121 cursor = glyph_after;
14122 x = -1;
14124 else if (string_seen)
14126 int incr = row->reversed_p ? -1 : +1;
14128 /* Need to find the glyph that came out of a string which is
14129 present at point. That glyph is somewhere between
14130 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14131 positioned between POS_BEFORE and POS_AFTER in the
14132 buffer. */
14133 struct glyph *start, *stop;
14134 ptrdiff_t pos = pos_before;
14136 x = -1;
14138 /* If the row ends in a newline from a display string,
14139 reordering could have moved the glyphs belonging to the
14140 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14141 in this case we extend the search to the last glyph in
14142 the row that was not inserted by redisplay. */
14143 if (row->ends_in_newline_from_string_p)
14145 glyph_after = end;
14146 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14149 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14150 correspond to POS_BEFORE and POS_AFTER, respectively. We
14151 need START and STOP in the order that corresponds to the
14152 row's direction as given by its reversed_p flag. If the
14153 directionality of characters between POS_BEFORE and
14154 POS_AFTER is the opposite of the row's base direction,
14155 these characters will have been reordered for display,
14156 and we need to reverse START and STOP. */
14157 if (!row->reversed_p)
14159 start = min (glyph_before, glyph_after);
14160 stop = max (glyph_before, glyph_after);
14162 else
14164 start = max (glyph_before, glyph_after);
14165 stop = min (glyph_before, glyph_after);
14167 for (glyph = start + incr;
14168 row->reversed_p ? glyph > stop : glyph < stop; )
14171 /* Any glyphs that come from the buffer are here because
14172 of bidi reordering. Skip them, and only pay
14173 attention to glyphs that came from some string. */
14174 if (STRINGP (glyph->object))
14176 Lisp_Object str;
14177 ptrdiff_t tem;
14178 /* If the display property covers the newline, we
14179 need to search for it one position farther. */
14180 ptrdiff_t lim = pos_after
14181 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14183 string_from_text_prop = 0;
14184 str = glyph->object;
14185 tem = string_buffer_position_lim (str, pos, lim, 0);
14186 if (tem == 0 /* from overlay */
14187 || pos <= tem)
14189 /* If the string from which this glyph came is
14190 found in the buffer at point, or at position
14191 that is closer to point than pos_after, then
14192 we've found the glyph we've been looking for.
14193 If it comes from an overlay (tem == 0), and
14194 it has the `cursor' property on one of its
14195 glyphs, record that glyph as a candidate for
14196 displaying the cursor. (As in the
14197 unidirectional version, we will display the
14198 cursor on the last candidate we find.) */
14199 if (tem == 0
14200 || tem == pt_old
14201 || (tem - pt_old > 0 && tem < pos_after))
14203 /* The glyphs from this string could have
14204 been reordered. Find the one with the
14205 smallest string position. Or there could
14206 be a character in the string with the
14207 `cursor' property, which means display
14208 cursor on that character's glyph. */
14209 ptrdiff_t strpos = glyph->charpos;
14211 if (tem)
14213 cursor = glyph;
14214 string_from_text_prop = 1;
14216 for ( ;
14217 (row->reversed_p ? glyph > stop : glyph < stop)
14218 && EQ (glyph->object, str);
14219 glyph += incr)
14221 Lisp_Object cprop;
14222 ptrdiff_t gpos = glyph->charpos;
14224 cprop = Fget_char_property (make_number (gpos),
14225 Qcursor,
14226 glyph->object);
14227 if (!NILP (cprop))
14229 cursor = glyph;
14230 break;
14232 if (tem && glyph->charpos < strpos)
14234 strpos = glyph->charpos;
14235 cursor = glyph;
14239 if (tem == pt_old
14240 || (tem - pt_old > 0 && tem < pos_after))
14241 goto compute_x;
14243 if (tem)
14244 pos = tem + 1; /* don't find previous instances */
14246 /* This string is not what we want; skip all of the
14247 glyphs that came from it. */
14248 while ((row->reversed_p ? glyph > stop : glyph < stop)
14249 && EQ (glyph->object, str))
14250 glyph += incr;
14252 else
14253 glyph += incr;
14256 /* If we reached the end of the line, and END was from a string,
14257 the cursor is not on this line. */
14258 if (cursor == NULL
14259 && (row->reversed_p ? glyph <= end : glyph >= end)
14260 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14261 && STRINGP (end->object)
14262 && row->continued_p)
14263 return 0;
14265 /* A truncated row may not include PT among its character positions.
14266 Setting the cursor inside the scroll margin will trigger
14267 recalculation of hscroll in hscroll_window_tree. But if a
14268 display string covers point, defer to the string-handling
14269 code below to figure this out. */
14270 else if (row->truncated_on_left_p && pt_old < bpos_min)
14272 cursor = glyph_before;
14273 x = -1;
14275 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14276 /* Zero-width characters produce no glyphs. */
14277 || (!empty_line_p
14278 && (row->reversed_p
14279 ? glyph_after > glyphs_end
14280 : glyph_after < glyphs_end)))
14282 cursor = glyph_after;
14283 x = -1;
14287 compute_x:
14288 if (cursor != NULL)
14289 glyph = cursor;
14290 else if (glyph == glyphs_end
14291 && pos_before == pos_after
14292 && STRINGP ((row->reversed_p
14293 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14294 : row->glyphs[TEXT_AREA])->object))
14296 /* If all the glyphs of this row came from strings, put the
14297 cursor on the first glyph of the row. This avoids having the
14298 cursor outside of the text area in this very rare and hard
14299 use case. */
14300 glyph =
14301 row->reversed_p
14302 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14303 : row->glyphs[TEXT_AREA];
14305 if (x < 0)
14307 struct glyph *g;
14309 /* Need to compute x that corresponds to GLYPH. */
14310 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14312 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14313 emacs_abort ();
14314 x += g->pixel_width;
14318 /* ROW could be part of a continued line, which, under bidi
14319 reordering, might have other rows whose start and end charpos
14320 occlude point. Only set w->cursor if we found a better
14321 approximation to the cursor position than we have from previously
14322 examined candidate rows belonging to the same continued line. */
14323 if (/* we already have a candidate row */
14324 w->cursor.vpos >= 0
14325 /* that candidate is not the row we are processing */
14326 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14327 /* Make sure cursor.vpos specifies a row whose start and end
14328 charpos occlude point, and it is valid candidate for being a
14329 cursor-row. This is because some callers of this function
14330 leave cursor.vpos at the row where the cursor was displayed
14331 during the last redisplay cycle. */
14332 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14333 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14334 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14336 struct glyph *g1 =
14337 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14339 /* Don't consider glyphs that are outside TEXT_AREA. */
14340 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14341 return 0;
14342 /* Keep the candidate whose buffer position is the closest to
14343 point or has the `cursor' property. */
14344 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14345 w->cursor.hpos >= 0
14346 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14347 && ((BUFFERP (g1->object)
14348 && (g1->charpos == pt_old /* an exact match always wins */
14349 || (BUFFERP (glyph->object)
14350 && eabs (g1->charpos - pt_old)
14351 < eabs (glyph->charpos - pt_old))))
14352 /* previous candidate is a glyph from a string that has
14353 a non-nil `cursor' property */
14354 || (STRINGP (g1->object)
14355 && (!NILP (Fget_char_property (make_number (g1->charpos),
14356 Qcursor, g1->object))
14357 /* previous candidate is from the same display
14358 string as this one, and the display string
14359 came from a text property */
14360 || (EQ (g1->object, glyph->object)
14361 && string_from_text_prop)
14362 /* this candidate is from newline and its
14363 position is not an exact match */
14364 || (INTEGERP (glyph->object)
14365 && glyph->charpos != pt_old)))))
14366 return 0;
14367 /* If this candidate gives an exact match, use that. */
14368 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14369 /* If this candidate is a glyph created for the
14370 terminating newline of a line, and point is on that
14371 newline, it wins because it's an exact match. */
14372 || (!row->continued_p
14373 && INTEGERP (glyph->object)
14374 && glyph->charpos == 0
14375 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14376 /* Otherwise, keep the candidate that comes from a row
14377 spanning less buffer positions. This may win when one or
14378 both candidate positions are on glyphs that came from
14379 display strings, for which we cannot compare buffer
14380 positions. */
14381 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14382 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14383 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14384 return 0;
14386 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14387 w->cursor.x = x;
14388 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14389 w->cursor.y = row->y + dy;
14391 if (w == XWINDOW (selected_window))
14393 if (!row->continued_p
14394 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14395 && row->x == 0)
14397 this_line_buffer = XBUFFER (w->contents);
14399 CHARPOS (this_line_start_pos)
14400 = MATRIX_ROW_START_CHARPOS (row) + delta;
14401 BYTEPOS (this_line_start_pos)
14402 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14404 CHARPOS (this_line_end_pos)
14405 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14406 BYTEPOS (this_line_end_pos)
14407 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14409 this_line_y = w->cursor.y;
14410 this_line_pixel_height = row->height;
14411 this_line_vpos = w->cursor.vpos;
14412 this_line_start_x = row->x;
14414 else
14415 CHARPOS (this_line_start_pos) = 0;
14418 return 1;
14422 /* Run window scroll functions, if any, for WINDOW with new window
14423 start STARTP. Sets the window start of WINDOW to that position.
14425 We assume that the window's buffer is really current. */
14427 static struct text_pos
14428 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14430 struct window *w = XWINDOW (window);
14431 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14433 if (current_buffer != XBUFFER (w->contents))
14434 emacs_abort ();
14436 if (!NILP (Vwindow_scroll_functions))
14438 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14439 make_number (CHARPOS (startp)));
14440 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14441 /* In case the hook functions switch buffers. */
14442 set_buffer_internal (XBUFFER (w->contents));
14445 return startp;
14449 /* Make sure the line containing the cursor is fully visible.
14450 A value of 1 means there is nothing to be done.
14451 (Either the line is fully visible, or it cannot be made so,
14452 or we cannot tell.)
14454 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14455 is higher than window.
14457 A value of 0 means the caller should do scrolling
14458 as if point had gone off the screen. */
14460 static int
14461 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14463 struct glyph_matrix *matrix;
14464 struct glyph_row *row;
14465 int window_height;
14467 if (!make_cursor_line_fully_visible_p)
14468 return 1;
14470 /* It's not always possible to find the cursor, e.g, when a window
14471 is full of overlay strings. Don't do anything in that case. */
14472 if (w->cursor.vpos < 0)
14473 return 1;
14475 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14476 row = MATRIX_ROW (matrix, w->cursor.vpos);
14478 /* If the cursor row is not partially visible, there's nothing to do. */
14479 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14480 return 1;
14482 /* If the row the cursor is in is taller than the window's height,
14483 it's not clear what to do, so do nothing. */
14484 window_height = window_box_height (w);
14485 if (row->height >= window_height)
14487 if (!force_p || MINI_WINDOW_P (w)
14488 || w->vscroll || w->cursor.vpos == 0)
14489 return 1;
14491 return 0;
14495 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14496 non-zero means only WINDOW is redisplayed in redisplay_internal.
14497 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14498 in redisplay_window to bring a partially visible line into view in
14499 the case that only the cursor has moved.
14501 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14502 last screen line's vertical height extends past the end of the screen.
14504 Value is
14506 1 if scrolling succeeded
14508 0 if scrolling didn't find point.
14510 -1 if new fonts have been loaded so that we must interrupt
14511 redisplay, adjust glyph matrices, and try again. */
14513 enum
14515 SCROLLING_SUCCESS,
14516 SCROLLING_FAILED,
14517 SCROLLING_NEED_LARGER_MATRICES
14520 /* If scroll-conservatively is more than this, never recenter.
14522 If you change this, don't forget to update the doc string of
14523 `scroll-conservatively' and the Emacs manual. */
14524 #define SCROLL_LIMIT 100
14526 static int
14527 try_scrolling (Lisp_Object window, int just_this_one_p,
14528 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14529 int temp_scroll_step, int last_line_misfit)
14531 struct window *w = XWINDOW (window);
14532 struct frame *f = XFRAME (w->frame);
14533 struct text_pos pos, startp;
14534 struct it it;
14535 int this_scroll_margin, scroll_max, rc, height;
14536 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14537 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14538 Lisp_Object aggressive;
14539 /* We will never try scrolling more than this number of lines. */
14540 int scroll_limit = SCROLL_LIMIT;
14542 #ifdef GLYPH_DEBUG
14543 debug_method_add (w, "try_scrolling");
14544 #endif
14546 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14548 /* Compute scroll margin height in pixels. We scroll when point is
14549 within this distance from the top or bottom of the window. */
14550 if (scroll_margin > 0)
14551 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14552 * FRAME_LINE_HEIGHT (f);
14553 else
14554 this_scroll_margin = 0;
14556 /* Force arg_scroll_conservatively to have a reasonable value, to
14557 avoid scrolling too far away with slow move_it_* functions. Note
14558 that the user can supply scroll-conservatively equal to
14559 `most-positive-fixnum', which can be larger than INT_MAX. */
14560 if (arg_scroll_conservatively > scroll_limit)
14562 arg_scroll_conservatively = scroll_limit + 1;
14563 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14565 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14566 /* Compute how much we should try to scroll maximally to bring
14567 point into view. */
14568 scroll_max = (max (scroll_step,
14569 max (arg_scroll_conservatively, temp_scroll_step))
14570 * FRAME_LINE_HEIGHT (f));
14571 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14572 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14573 /* We're trying to scroll because of aggressive scrolling but no
14574 scroll_step is set. Choose an arbitrary one. */
14575 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14576 else
14577 scroll_max = 0;
14579 too_near_end:
14581 /* Decide whether to scroll down. */
14582 if (PT > CHARPOS (startp))
14584 int scroll_margin_y;
14586 /* Compute the pixel ypos of the scroll margin, then move IT to
14587 either that ypos or PT, whichever comes first. */
14588 start_display (&it, w, startp);
14589 scroll_margin_y = it.last_visible_y - this_scroll_margin
14590 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14591 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14592 (MOVE_TO_POS | MOVE_TO_Y));
14594 if (PT > CHARPOS (it.current.pos))
14596 int y0 = line_bottom_y (&it);
14597 /* Compute how many pixels below window bottom to stop searching
14598 for PT. This avoids costly search for PT that is far away if
14599 the user limited scrolling by a small number of lines, but
14600 always finds PT if scroll_conservatively is set to a large
14601 number, such as most-positive-fixnum. */
14602 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14603 int y_to_move = it.last_visible_y + slack;
14605 /* Compute the distance from the scroll margin to PT or to
14606 the scroll limit, whichever comes first. This should
14607 include the height of the cursor line, to make that line
14608 fully visible. */
14609 move_it_to (&it, PT, -1, y_to_move,
14610 -1, MOVE_TO_POS | MOVE_TO_Y);
14611 dy = line_bottom_y (&it) - y0;
14613 if (dy > scroll_max)
14614 return SCROLLING_FAILED;
14616 if (dy > 0)
14617 scroll_down_p = 1;
14621 if (scroll_down_p)
14623 /* Point is in or below the bottom scroll margin, so move the
14624 window start down. If scrolling conservatively, move it just
14625 enough down to make point visible. If scroll_step is set,
14626 move it down by scroll_step. */
14627 if (arg_scroll_conservatively)
14628 amount_to_scroll
14629 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14630 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14631 else if (scroll_step || temp_scroll_step)
14632 amount_to_scroll = scroll_max;
14633 else
14635 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14636 height = WINDOW_BOX_TEXT_HEIGHT (w);
14637 if (NUMBERP (aggressive))
14639 double float_amount = XFLOATINT (aggressive) * height;
14640 int aggressive_scroll = float_amount;
14641 if (aggressive_scroll == 0 && float_amount > 0)
14642 aggressive_scroll = 1;
14643 /* Don't let point enter the scroll margin near top of
14644 the window. This could happen if the value of
14645 scroll_up_aggressively is too large and there are
14646 non-zero margins, because scroll_up_aggressively
14647 means put point that fraction of window height
14648 _from_the_bottom_margin_. */
14649 if (aggressive_scroll + 2*this_scroll_margin > height)
14650 aggressive_scroll = height - 2*this_scroll_margin;
14651 amount_to_scroll = dy + aggressive_scroll;
14655 if (amount_to_scroll <= 0)
14656 return SCROLLING_FAILED;
14658 start_display (&it, w, startp);
14659 if (arg_scroll_conservatively <= scroll_limit)
14660 move_it_vertically (&it, amount_to_scroll);
14661 else
14663 /* Extra precision for users who set scroll-conservatively
14664 to a large number: make sure the amount we scroll
14665 the window start is never less than amount_to_scroll,
14666 which was computed as distance from window bottom to
14667 point. This matters when lines at window top and lines
14668 below window bottom have different height. */
14669 struct it it1;
14670 void *it1data = NULL;
14671 /* We use a temporary it1 because line_bottom_y can modify
14672 its argument, if it moves one line down; see there. */
14673 int start_y;
14675 SAVE_IT (it1, it, it1data);
14676 start_y = line_bottom_y (&it1);
14677 do {
14678 RESTORE_IT (&it, &it, it1data);
14679 move_it_by_lines (&it, 1);
14680 SAVE_IT (it1, it, it1data);
14681 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14684 /* If STARTP is unchanged, move it down another screen line. */
14685 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14686 move_it_by_lines (&it, 1);
14687 startp = it.current.pos;
14689 else
14691 struct text_pos scroll_margin_pos = startp;
14692 int y_offset = 0;
14694 /* See if point is inside the scroll margin at the top of the
14695 window. */
14696 if (this_scroll_margin)
14698 int y_start;
14700 start_display (&it, w, startp);
14701 y_start = it.current_y;
14702 move_it_vertically (&it, this_scroll_margin);
14703 scroll_margin_pos = it.current.pos;
14704 /* If we didn't move enough before hitting ZV, request
14705 additional amount of scroll, to move point out of the
14706 scroll margin. */
14707 if (IT_CHARPOS (it) == ZV
14708 && it.current_y - y_start < this_scroll_margin)
14709 y_offset = this_scroll_margin - (it.current_y - y_start);
14712 if (PT < CHARPOS (scroll_margin_pos))
14714 /* Point is in the scroll margin at the top of the window or
14715 above what is displayed in the window. */
14716 int y0, y_to_move;
14718 /* Compute the vertical distance from PT to the scroll
14719 margin position. Move as far as scroll_max allows, or
14720 one screenful, or 10 screen lines, whichever is largest.
14721 Give up if distance is greater than scroll_max or if we
14722 didn't reach the scroll margin position. */
14723 SET_TEXT_POS (pos, PT, PT_BYTE);
14724 start_display (&it, w, pos);
14725 y0 = it.current_y;
14726 y_to_move = max (it.last_visible_y,
14727 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14728 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14729 y_to_move, -1,
14730 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14731 dy = it.current_y - y0;
14732 if (dy > scroll_max
14733 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14734 return SCROLLING_FAILED;
14736 /* Additional scroll for when ZV was too close to point. */
14737 dy += y_offset;
14739 /* Compute new window start. */
14740 start_display (&it, w, startp);
14742 if (arg_scroll_conservatively)
14743 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14744 max (scroll_step, temp_scroll_step));
14745 else if (scroll_step || temp_scroll_step)
14746 amount_to_scroll = scroll_max;
14747 else
14749 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14750 height = WINDOW_BOX_TEXT_HEIGHT (w);
14751 if (NUMBERP (aggressive))
14753 double float_amount = XFLOATINT (aggressive) * height;
14754 int aggressive_scroll = float_amount;
14755 if (aggressive_scroll == 0 && float_amount > 0)
14756 aggressive_scroll = 1;
14757 /* Don't let point enter the scroll margin near
14758 bottom of the window, if the value of
14759 scroll_down_aggressively happens to be too
14760 large. */
14761 if (aggressive_scroll + 2*this_scroll_margin > height)
14762 aggressive_scroll = height - 2*this_scroll_margin;
14763 amount_to_scroll = dy + aggressive_scroll;
14767 if (amount_to_scroll <= 0)
14768 return SCROLLING_FAILED;
14770 move_it_vertically_backward (&it, amount_to_scroll);
14771 startp = it.current.pos;
14775 /* Run window scroll functions. */
14776 startp = run_window_scroll_functions (window, startp);
14778 /* Display the window. Give up if new fonts are loaded, or if point
14779 doesn't appear. */
14780 if (!try_window (window, startp, 0))
14781 rc = SCROLLING_NEED_LARGER_MATRICES;
14782 else if (w->cursor.vpos < 0)
14784 clear_glyph_matrix (w->desired_matrix);
14785 rc = SCROLLING_FAILED;
14787 else
14789 /* Maybe forget recorded base line for line number display. */
14790 if (!just_this_one_p
14791 || current_buffer->clip_changed
14792 || BEG_UNCHANGED < CHARPOS (startp))
14793 w->base_line_number = 0;
14795 /* If cursor ends up on a partially visible line,
14796 treat that as being off the bottom of the screen. */
14797 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14798 /* It's possible that the cursor is on the first line of the
14799 buffer, which is partially obscured due to a vscroll
14800 (Bug#7537). In that case, avoid looping forever . */
14801 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14803 clear_glyph_matrix (w->desired_matrix);
14804 ++extra_scroll_margin_lines;
14805 goto too_near_end;
14807 rc = SCROLLING_SUCCESS;
14810 return rc;
14814 /* Compute a suitable window start for window W if display of W starts
14815 on a continuation line. Value is non-zero if a new window start
14816 was computed.
14818 The new window start will be computed, based on W's width, starting
14819 from the start of the continued line. It is the start of the
14820 screen line with the minimum distance from the old start W->start. */
14822 static int
14823 compute_window_start_on_continuation_line (struct window *w)
14825 struct text_pos pos, start_pos;
14826 int window_start_changed_p = 0;
14828 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14830 /* If window start is on a continuation line... Window start may be
14831 < BEGV in case there's invisible text at the start of the
14832 buffer (M-x rmail, for example). */
14833 if (CHARPOS (start_pos) > BEGV
14834 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14836 struct it it;
14837 struct glyph_row *row;
14839 /* Handle the case that the window start is out of range. */
14840 if (CHARPOS (start_pos) < BEGV)
14841 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14842 else if (CHARPOS (start_pos) > ZV)
14843 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14845 /* Find the start of the continued line. This should be fast
14846 because find_newline is fast (newline cache). */
14847 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14848 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14849 row, DEFAULT_FACE_ID);
14850 reseat_at_previous_visible_line_start (&it);
14852 /* If the line start is "too far" away from the window start,
14853 say it takes too much time to compute a new window start. */
14854 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14855 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14857 int min_distance, distance;
14859 /* Move forward by display lines to find the new window
14860 start. If window width was enlarged, the new start can
14861 be expected to be > the old start. If window width was
14862 decreased, the new window start will be < the old start.
14863 So, we're looking for the display line start with the
14864 minimum distance from the old window start. */
14865 pos = it.current.pos;
14866 min_distance = INFINITY;
14867 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14868 distance < min_distance)
14870 min_distance = distance;
14871 pos = it.current.pos;
14872 move_it_by_lines (&it, 1);
14875 /* Set the window start there. */
14876 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14877 window_start_changed_p = 1;
14881 return window_start_changed_p;
14885 /* Try cursor movement in case text has not changed in window WINDOW,
14886 with window start STARTP. Value is
14888 CURSOR_MOVEMENT_SUCCESS if successful
14890 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14892 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14893 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14894 we want to scroll as if scroll-step were set to 1. See the code.
14896 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14897 which case we have to abort this redisplay, and adjust matrices
14898 first. */
14900 enum
14902 CURSOR_MOVEMENT_SUCCESS,
14903 CURSOR_MOVEMENT_CANNOT_BE_USED,
14904 CURSOR_MOVEMENT_MUST_SCROLL,
14905 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14908 static int
14909 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14911 struct window *w = XWINDOW (window);
14912 struct frame *f = XFRAME (w->frame);
14913 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14915 #ifdef GLYPH_DEBUG
14916 if (inhibit_try_cursor_movement)
14917 return rc;
14918 #endif
14920 /* Previously, there was a check for Lisp integer in the
14921 if-statement below. Now, this field is converted to
14922 ptrdiff_t, thus zero means invalid position in a buffer. */
14923 eassert (w->last_point > 0);
14925 /* Handle case where text has not changed, only point, and it has
14926 not moved off the frame. */
14927 if (/* Point may be in this window. */
14928 PT >= CHARPOS (startp)
14929 /* Selective display hasn't changed. */
14930 && !current_buffer->clip_changed
14931 /* Function force-mode-line-update is used to force a thorough
14932 redisplay. It sets either windows_or_buffers_changed or
14933 update_mode_lines. So don't take a shortcut here for these
14934 cases. */
14935 && !update_mode_lines
14936 && !windows_or_buffers_changed
14937 && !cursor_type_changed
14938 /* Can't use this case if highlighting a region. When a
14939 region exists, cursor movement has to do more than just
14940 set the cursor. */
14941 && markpos_of_region () < 0
14942 && !w->region_showing
14943 && NILP (Vshow_trailing_whitespace)
14944 /* This code is not used for mini-buffer for the sake of the case
14945 of redisplaying to replace an echo area message; since in
14946 that case the mini-buffer contents per se are usually
14947 unchanged. This code is of no real use in the mini-buffer
14948 since the handling of this_line_start_pos, etc., in redisplay
14949 handles the same cases. */
14950 && !EQ (window, minibuf_window)
14951 /* When splitting windows or for new windows, it happens that
14952 redisplay is called with a nil window_end_vpos or one being
14953 larger than the window. This should really be fixed in
14954 window.c. I don't have this on my list, now, so we do
14955 approximately the same as the old redisplay code. --gerd. */
14956 && INTEGERP (w->window_end_vpos)
14957 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14958 && (FRAME_WINDOW_P (f)
14959 || !overlay_arrow_in_current_buffer_p ()))
14961 int this_scroll_margin, top_scroll_margin;
14962 struct glyph_row *row = NULL;
14964 #ifdef GLYPH_DEBUG
14965 debug_method_add (w, "cursor movement");
14966 #endif
14968 /* Scroll if point within this distance from the top or bottom
14969 of the window. This is a pixel value. */
14970 if (scroll_margin > 0)
14972 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14973 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14975 else
14976 this_scroll_margin = 0;
14978 top_scroll_margin = this_scroll_margin;
14979 if (WINDOW_WANTS_HEADER_LINE_P (w))
14980 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14982 /* Start with the row the cursor was displayed during the last
14983 not paused redisplay. Give up if that row is not valid. */
14984 if (w->last_cursor.vpos < 0
14985 || w->last_cursor.vpos >= w->current_matrix->nrows)
14986 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14987 else
14989 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14990 if (row->mode_line_p)
14991 ++row;
14992 if (!row->enabled_p)
14993 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14996 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14998 int scroll_p = 0, must_scroll = 0;
14999 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15001 if (PT > w->last_point)
15003 /* Point has moved forward. */
15004 while (MATRIX_ROW_END_CHARPOS (row) < PT
15005 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15007 eassert (row->enabled_p);
15008 ++row;
15011 /* If the end position of a row equals the start
15012 position of the next row, and PT is at that position,
15013 we would rather display cursor in the next line. */
15014 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15015 && MATRIX_ROW_END_CHARPOS (row) == PT
15016 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15017 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15018 && !cursor_row_p (row))
15019 ++row;
15021 /* If within the scroll margin, scroll. Note that
15022 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15023 the next line would be drawn, and that
15024 this_scroll_margin can be zero. */
15025 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15026 || PT > MATRIX_ROW_END_CHARPOS (row)
15027 /* Line is completely visible last line in window
15028 and PT is to be set in the next line. */
15029 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15030 && PT == MATRIX_ROW_END_CHARPOS (row)
15031 && !row->ends_at_zv_p
15032 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15033 scroll_p = 1;
15035 else if (PT < w->last_point)
15037 /* Cursor has to be moved backward. Note that PT >=
15038 CHARPOS (startp) because of the outer if-statement. */
15039 while (!row->mode_line_p
15040 && (MATRIX_ROW_START_CHARPOS (row) > PT
15041 || (MATRIX_ROW_START_CHARPOS (row) == PT
15042 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15043 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15044 row > w->current_matrix->rows
15045 && (row-1)->ends_in_newline_from_string_p))))
15046 && (row->y > top_scroll_margin
15047 || CHARPOS (startp) == BEGV))
15049 eassert (row->enabled_p);
15050 --row;
15053 /* Consider the following case: Window starts at BEGV,
15054 there is invisible, intangible text at BEGV, so that
15055 display starts at some point START > BEGV. It can
15056 happen that we are called with PT somewhere between
15057 BEGV and START. Try to handle that case. */
15058 if (row < w->current_matrix->rows
15059 || row->mode_line_p)
15061 row = w->current_matrix->rows;
15062 if (row->mode_line_p)
15063 ++row;
15066 /* Due to newlines in overlay strings, we may have to
15067 skip forward over overlay strings. */
15068 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15069 && MATRIX_ROW_END_CHARPOS (row) == PT
15070 && !cursor_row_p (row))
15071 ++row;
15073 /* If within the scroll margin, scroll. */
15074 if (row->y < top_scroll_margin
15075 && CHARPOS (startp) != BEGV)
15076 scroll_p = 1;
15078 else
15080 /* Cursor did not move. So don't scroll even if cursor line
15081 is partially visible, as it was so before. */
15082 rc = CURSOR_MOVEMENT_SUCCESS;
15085 if (PT < MATRIX_ROW_START_CHARPOS (row)
15086 || PT > MATRIX_ROW_END_CHARPOS (row))
15088 /* if PT is not in the glyph row, give up. */
15089 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15090 must_scroll = 1;
15092 else if (rc != CURSOR_MOVEMENT_SUCCESS
15093 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15095 struct glyph_row *row1;
15097 /* If rows are bidi-reordered and point moved, back up
15098 until we find a row that does not belong to a
15099 continuation line. This is because we must consider
15100 all rows of a continued line as candidates for the
15101 new cursor positioning, since row start and end
15102 positions change non-linearly with vertical position
15103 in such rows. */
15104 /* FIXME: Revisit this when glyph ``spilling'' in
15105 continuation lines' rows is implemented for
15106 bidi-reordered rows. */
15107 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15108 MATRIX_ROW_CONTINUATION_LINE_P (row);
15109 --row)
15111 /* If we hit the beginning of the displayed portion
15112 without finding the first row of a continued
15113 line, give up. */
15114 if (row <= row1)
15116 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15117 break;
15119 eassert (row->enabled_p);
15122 if (must_scroll)
15124 else if (rc != CURSOR_MOVEMENT_SUCCESS
15125 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15126 /* Make sure this isn't a header line by any chance, since
15127 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15128 && !row->mode_line_p
15129 && make_cursor_line_fully_visible_p)
15131 if (PT == MATRIX_ROW_END_CHARPOS (row)
15132 && !row->ends_at_zv_p
15133 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15134 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15135 else if (row->height > window_box_height (w))
15137 /* If we end up in a partially visible line, let's
15138 make it fully visible, except when it's taller
15139 than the window, in which case we can't do much
15140 about it. */
15141 *scroll_step = 1;
15142 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15144 else
15146 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15147 if (!cursor_row_fully_visible_p (w, 0, 1))
15148 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15149 else
15150 rc = CURSOR_MOVEMENT_SUCCESS;
15153 else if (scroll_p)
15154 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15155 else if (rc != CURSOR_MOVEMENT_SUCCESS
15156 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15158 /* With bidi-reordered rows, there could be more than
15159 one candidate row whose start and end positions
15160 occlude point. We need to let set_cursor_from_row
15161 find the best candidate. */
15162 /* FIXME: Revisit this when glyph ``spilling'' in
15163 continuation lines' rows is implemented for
15164 bidi-reordered rows. */
15165 int rv = 0;
15169 int at_zv_p = 0, exact_match_p = 0;
15171 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15172 && PT <= MATRIX_ROW_END_CHARPOS (row)
15173 && cursor_row_p (row))
15174 rv |= set_cursor_from_row (w, row, w->current_matrix,
15175 0, 0, 0, 0);
15176 /* As soon as we've found the exact match for point,
15177 or the first suitable row whose ends_at_zv_p flag
15178 is set, we are done. */
15179 at_zv_p =
15180 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15181 if (rv && !at_zv_p
15182 && w->cursor.hpos >= 0
15183 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15184 w->cursor.vpos))
15186 struct glyph_row *candidate =
15187 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15188 struct glyph *g =
15189 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15190 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15192 exact_match_p =
15193 (BUFFERP (g->object) && g->charpos == PT)
15194 || (INTEGERP (g->object)
15195 && (g->charpos == PT
15196 || (g->charpos == 0 && endpos - 1 == PT)));
15198 if (rv && (at_zv_p || exact_match_p))
15200 rc = CURSOR_MOVEMENT_SUCCESS;
15201 break;
15203 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15204 break;
15205 ++row;
15207 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15208 || row->continued_p)
15209 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15210 || (MATRIX_ROW_START_CHARPOS (row) == PT
15211 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15212 /* If we didn't find any candidate rows, or exited the
15213 loop before all the candidates were examined, signal
15214 to the caller that this method failed. */
15215 if (rc != CURSOR_MOVEMENT_SUCCESS
15216 && !(rv
15217 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15218 && !row->continued_p))
15219 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15220 else if (rv)
15221 rc = CURSOR_MOVEMENT_SUCCESS;
15223 else
15227 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15229 rc = CURSOR_MOVEMENT_SUCCESS;
15230 break;
15232 ++row;
15234 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15235 && MATRIX_ROW_START_CHARPOS (row) == PT
15236 && cursor_row_p (row));
15241 return rc;
15244 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15245 static
15246 #endif
15247 void
15248 set_vertical_scroll_bar (struct window *w)
15250 ptrdiff_t start, end, whole;
15252 /* Calculate the start and end positions for the current window.
15253 At some point, it would be nice to choose between scrollbars
15254 which reflect the whole buffer size, with special markers
15255 indicating narrowing, and scrollbars which reflect only the
15256 visible region.
15258 Note that mini-buffers sometimes aren't displaying any text. */
15259 if (!MINI_WINDOW_P (w)
15260 || (w == XWINDOW (minibuf_window)
15261 && NILP (echo_area_buffer[0])))
15263 struct buffer *buf = XBUFFER (w->contents);
15264 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15265 start = marker_position (w->start) - BUF_BEGV (buf);
15266 /* I don't think this is guaranteed to be right. For the
15267 moment, we'll pretend it is. */
15268 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15270 if (end < start)
15271 end = start;
15272 if (whole < (end - start))
15273 whole = end - start;
15275 else
15276 start = end = whole = 0;
15278 /* Indicate what this scroll bar ought to be displaying now. */
15279 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15280 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15281 (w, end - start, whole, start);
15285 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15286 selected_window is redisplayed.
15288 We can return without actually redisplaying the window if
15289 fonts_changed_p. In that case, redisplay_internal will
15290 retry. */
15292 static void
15293 redisplay_window (Lisp_Object window, int just_this_one_p)
15295 struct window *w = XWINDOW (window);
15296 struct frame *f = XFRAME (w->frame);
15297 struct buffer *buffer = XBUFFER (w->contents);
15298 struct buffer *old = current_buffer;
15299 struct text_pos lpoint, opoint, startp;
15300 int update_mode_line;
15301 int tem;
15302 struct it it;
15303 /* Record it now because it's overwritten. */
15304 int current_matrix_up_to_date_p = 0;
15305 int used_current_matrix_p = 0;
15306 /* This is less strict than current_matrix_up_to_date_p.
15307 It indicates that the buffer contents and narrowing are unchanged. */
15308 int buffer_unchanged_p = 0;
15309 int temp_scroll_step = 0;
15310 ptrdiff_t count = SPECPDL_INDEX ();
15311 int rc;
15312 int centering_position = -1;
15313 int last_line_misfit = 0;
15314 ptrdiff_t beg_unchanged, end_unchanged;
15316 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15317 opoint = lpoint;
15319 #ifdef GLYPH_DEBUG
15320 *w->desired_matrix->method = 0;
15321 #endif
15323 /* Make sure that both W's markers are valid. */
15324 eassert (XMARKER (w->start)->buffer == buffer);
15325 eassert (XMARKER (w->pointm)->buffer == buffer);
15327 restart:
15328 reconsider_clip_changes (w, buffer);
15330 /* Has the mode line to be updated? */
15331 update_mode_line = (w->update_mode_line
15332 || update_mode_lines
15333 || buffer->clip_changed
15334 || buffer->prevent_redisplay_optimizations_p);
15336 if (MINI_WINDOW_P (w))
15338 if (w == XWINDOW (echo_area_window)
15339 && !NILP (echo_area_buffer[0]))
15341 if (update_mode_line)
15342 /* We may have to update a tty frame's menu bar or a
15343 tool-bar. Example `M-x C-h C-h C-g'. */
15344 goto finish_menu_bars;
15345 else
15346 /* We've already displayed the echo area glyphs in this window. */
15347 goto finish_scroll_bars;
15349 else if ((w != XWINDOW (minibuf_window)
15350 || minibuf_level == 0)
15351 /* When buffer is nonempty, redisplay window normally. */
15352 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15353 /* Quail displays non-mini buffers in minibuffer window.
15354 In that case, redisplay the window normally. */
15355 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15357 /* W is a mini-buffer window, but it's not active, so clear
15358 it. */
15359 int yb = window_text_bottom_y (w);
15360 struct glyph_row *row;
15361 int y;
15363 for (y = 0, row = w->desired_matrix->rows;
15364 y < yb;
15365 y += row->height, ++row)
15366 blank_row (w, row, y);
15367 goto finish_scroll_bars;
15370 clear_glyph_matrix (w->desired_matrix);
15373 /* Otherwise set up data on this window; select its buffer and point
15374 value. */
15375 /* Really select the buffer, for the sake of buffer-local
15376 variables. */
15377 set_buffer_internal_1 (XBUFFER (w->contents));
15379 current_matrix_up_to_date_p
15380 = (w->window_end_valid
15381 && !current_buffer->clip_changed
15382 && !current_buffer->prevent_redisplay_optimizations_p
15383 && !window_outdated (w));
15385 /* Run the window-bottom-change-functions
15386 if it is possible that the text on the screen has changed
15387 (either due to modification of the text, or any other reason). */
15388 if (!current_matrix_up_to_date_p
15389 && !NILP (Vwindow_text_change_functions))
15391 safe_run_hooks (Qwindow_text_change_functions);
15392 goto restart;
15395 beg_unchanged = BEG_UNCHANGED;
15396 end_unchanged = END_UNCHANGED;
15398 SET_TEXT_POS (opoint, PT, PT_BYTE);
15400 specbind (Qinhibit_point_motion_hooks, Qt);
15402 buffer_unchanged_p
15403 = (w->window_end_valid
15404 && !current_buffer->clip_changed
15405 && !window_outdated (w));
15407 /* When windows_or_buffers_changed is non-zero, we can't rely on
15408 the window end being valid, so set it to nil there. */
15409 if (windows_or_buffers_changed)
15411 /* If window starts on a continuation line, maybe adjust the
15412 window start in case the window's width changed. */
15413 if (XMARKER (w->start)->buffer == current_buffer)
15414 compute_window_start_on_continuation_line (w);
15416 w->window_end_valid = 0;
15419 /* Some sanity checks. */
15420 CHECK_WINDOW_END (w);
15421 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15422 emacs_abort ();
15423 if (BYTEPOS (opoint) < CHARPOS (opoint))
15424 emacs_abort ();
15426 if (mode_line_update_needed (w))
15427 update_mode_line = 1;
15429 /* Point refers normally to the selected window. For any other
15430 window, set up appropriate value. */
15431 if (!EQ (window, selected_window))
15433 ptrdiff_t new_pt = marker_position (w->pointm);
15434 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15435 if (new_pt < BEGV)
15437 new_pt = BEGV;
15438 new_pt_byte = BEGV_BYTE;
15439 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15441 else if (new_pt > (ZV - 1))
15443 new_pt = ZV;
15444 new_pt_byte = ZV_BYTE;
15445 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15448 /* We don't use SET_PT so that the point-motion hooks don't run. */
15449 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15452 /* If any of the character widths specified in the display table
15453 have changed, invalidate the width run cache. It's true that
15454 this may be a bit late to catch such changes, but the rest of
15455 redisplay goes (non-fatally) haywire when the display table is
15456 changed, so why should we worry about doing any better? */
15457 if (current_buffer->width_run_cache)
15459 struct Lisp_Char_Table *disptab = buffer_display_table ();
15461 if (! disptab_matches_widthtab
15462 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15464 invalidate_region_cache (current_buffer,
15465 current_buffer->width_run_cache,
15466 BEG, Z);
15467 recompute_width_table (current_buffer, disptab);
15471 /* If window-start is screwed up, choose a new one. */
15472 if (XMARKER (w->start)->buffer != current_buffer)
15473 goto recenter;
15475 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15477 /* If someone specified a new starting point but did not insist,
15478 check whether it can be used. */
15479 if (w->optional_new_start
15480 && CHARPOS (startp) >= BEGV
15481 && CHARPOS (startp) <= ZV)
15483 w->optional_new_start = 0;
15484 start_display (&it, w, startp);
15485 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15486 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15487 if (IT_CHARPOS (it) == PT)
15488 w->force_start = 1;
15489 /* IT may overshoot PT if text at PT is invisible. */
15490 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15491 w->force_start = 1;
15494 force_start:
15496 /* Handle case where place to start displaying has been specified,
15497 unless the specified location is outside the accessible range. */
15498 if (w->force_start || w->frozen_window_start_p)
15500 /* We set this later on if we have to adjust point. */
15501 int new_vpos = -1;
15503 w->force_start = 0;
15504 w->vscroll = 0;
15505 w->window_end_valid = 0;
15507 /* Forget any recorded base line for line number display. */
15508 if (!buffer_unchanged_p)
15509 w->base_line_number = 0;
15511 /* Redisplay the mode line. Select the buffer properly for that.
15512 Also, run the hook window-scroll-functions
15513 because we have scrolled. */
15514 /* Note, we do this after clearing force_start because
15515 if there's an error, it is better to forget about force_start
15516 than to get into an infinite loop calling the hook functions
15517 and having them get more errors. */
15518 if (!update_mode_line
15519 || ! NILP (Vwindow_scroll_functions))
15521 update_mode_line = 1;
15522 w->update_mode_line = 1;
15523 startp = run_window_scroll_functions (window, startp);
15526 w->last_modified = 0;
15527 w->last_overlay_modified = 0;
15528 if (CHARPOS (startp) < BEGV)
15529 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15530 else if (CHARPOS (startp) > ZV)
15531 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15533 /* Redisplay, then check if cursor has been set during the
15534 redisplay. Give up if new fonts were loaded. */
15535 /* We used to issue a CHECK_MARGINS argument to try_window here,
15536 but this causes scrolling to fail when point begins inside
15537 the scroll margin (bug#148) -- cyd */
15538 if (!try_window (window, startp, 0))
15540 w->force_start = 1;
15541 clear_glyph_matrix (w->desired_matrix);
15542 goto need_larger_matrices;
15545 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15547 /* If point does not appear, try to move point so it does
15548 appear. The desired matrix has been built above, so we
15549 can use it here. */
15550 new_vpos = window_box_height (w) / 2;
15553 if (!cursor_row_fully_visible_p (w, 0, 0))
15555 /* Point does appear, but on a line partly visible at end of window.
15556 Move it back to a fully-visible line. */
15557 new_vpos = window_box_height (w);
15559 else if (w->cursor.vpos >=0)
15561 /* Some people insist on not letting point enter the scroll
15562 margin, even though this part handles windows that didn't
15563 scroll at all. */
15564 int margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15565 int pixel_margin = margin * FRAME_LINE_HEIGHT (f);
15566 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15568 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15569 below, which finds the row to move point to, advances by
15570 the Y coordinate of the _next_ row, see the definition of
15571 MATRIX_ROW_BOTTOM_Y. */
15572 if (w->cursor.vpos < margin + header_line)
15573 new_vpos
15574 = pixel_margin + (header_line
15575 ? CURRENT_HEADER_LINE_HEIGHT (w)
15576 : 0) + FRAME_LINE_HEIGHT (f);
15577 else
15579 int window_height = window_box_height (w);
15581 if (header_line)
15582 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15583 if (w->cursor.y >= window_height - pixel_margin)
15584 new_vpos = window_height - pixel_margin;
15588 /* If we need to move point for either of the above reasons,
15589 now actually do it. */
15590 if (new_vpos >= 0)
15592 struct glyph_row *row;
15594 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15595 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15596 ++row;
15598 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15599 MATRIX_ROW_START_BYTEPOS (row));
15601 if (w != XWINDOW (selected_window))
15602 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15603 else if (current_buffer == old)
15604 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15606 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15608 /* If we are highlighting the region, then we just changed
15609 the region, so redisplay to show it. */
15610 if (markpos_of_region () >= 0)
15612 clear_glyph_matrix (w->desired_matrix);
15613 if (!try_window (window, startp, 0))
15614 goto need_larger_matrices;
15618 #ifdef GLYPH_DEBUG
15619 debug_method_add (w, "forced window start");
15620 #endif
15621 goto done;
15624 /* Handle case where text has not changed, only point, and it has
15625 not moved off the frame, and we are not retrying after hscroll.
15626 (current_matrix_up_to_date_p is nonzero when retrying.) */
15627 if (current_matrix_up_to_date_p
15628 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15629 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15631 switch (rc)
15633 case CURSOR_MOVEMENT_SUCCESS:
15634 used_current_matrix_p = 1;
15635 goto done;
15637 case CURSOR_MOVEMENT_MUST_SCROLL:
15638 goto try_to_scroll;
15640 default:
15641 emacs_abort ();
15644 /* If current starting point was originally the beginning of a line
15645 but no longer is, find a new starting point. */
15646 else if (w->start_at_line_beg
15647 && !(CHARPOS (startp) <= BEGV
15648 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15650 #ifdef GLYPH_DEBUG
15651 debug_method_add (w, "recenter 1");
15652 #endif
15653 goto recenter;
15656 /* Try scrolling with try_window_id. Value is > 0 if update has
15657 been done, it is -1 if we know that the same window start will
15658 not work. It is 0 if unsuccessful for some other reason. */
15659 else if ((tem = try_window_id (w)) != 0)
15661 #ifdef GLYPH_DEBUG
15662 debug_method_add (w, "try_window_id %d", tem);
15663 #endif
15665 if (fonts_changed_p)
15666 goto need_larger_matrices;
15667 if (tem > 0)
15668 goto done;
15670 /* Otherwise try_window_id has returned -1 which means that we
15671 don't want the alternative below this comment to execute. */
15673 else if (CHARPOS (startp) >= BEGV
15674 && CHARPOS (startp) <= ZV
15675 && PT >= CHARPOS (startp)
15676 && (CHARPOS (startp) < ZV
15677 /* Avoid starting at end of buffer. */
15678 || CHARPOS (startp) == BEGV
15679 || !window_outdated (w)))
15681 int d1, d2, d3, d4, d5, d6;
15683 /* If first window line is a continuation line, and window start
15684 is inside the modified region, but the first change is before
15685 current window start, we must select a new window start.
15687 However, if this is the result of a down-mouse event (e.g. by
15688 extending the mouse-drag-overlay), we don't want to select a
15689 new window start, since that would change the position under
15690 the mouse, resulting in an unwanted mouse-movement rather
15691 than a simple mouse-click. */
15692 if (!w->start_at_line_beg
15693 && NILP (do_mouse_tracking)
15694 && CHARPOS (startp) > BEGV
15695 && CHARPOS (startp) > BEG + beg_unchanged
15696 && CHARPOS (startp) <= Z - end_unchanged
15697 /* Even if w->start_at_line_beg is nil, a new window may
15698 start at a line_beg, since that's how set_buffer_window
15699 sets it. So, we need to check the return value of
15700 compute_window_start_on_continuation_line. (See also
15701 bug#197). */
15702 && XMARKER (w->start)->buffer == current_buffer
15703 && compute_window_start_on_continuation_line (w)
15704 /* It doesn't make sense to force the window start like we
15705 do at label force_start if it is already known that point
15706 will not be visible in the resulting window, because
15707 doing so will move point from its correct position
15708 instead of scrolling the window to bring point into view.
15709 See bug#9324. */
15710 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15712 w->force_start = 1;
15713 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15714 goto force_start;
15717 #ifdef GLYPH_DEBUG
15718 debug_method_add (w, "same window start");
15719 #endif
15721 /* Try to redisplay starting at same place as before.
15722 If point has not moved off frame, accept the results. */
15723 if (!current_matrix_up_to_date_p
15724 /* Don't use try_window_reusing_current_matrix in this case
15725 because a window scroll function can have changed the
15726 buffer. */
15727 || !NILP (Vwindow_scroll_functions)
15728 || MINI_WINDOW_P (w)
15729 || !(used_current_matrix_p
15730 = try_window_reusing_current_matrix (w)))
15732 IF_DEBUG (debug_method_add (w, "1"));
15733 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15734 /* -1 means we need to scroll.
15735 0 means we need new matrices, but fonts_changed_p
15736 is set in that case, so we will detect it below. */
15737 goto try_to_scroll;
15740 if (fonts_changed_p)
15741 goto need_larger_matrices;
15743 if (w->cursor.vpos >= 0)
15745 if (!just_this_one_p
15746 || current_buffer->clip_changed
15747 || BEG_UNCHANGED < CHARPOS (startp))
15748 /* Forget any recorded base line for line number display. */
15749 w->base_line_number = 0;
15751 if (!cursor_row_fully_visible_p (w, 1, 0))
15753 clear_glyph_matrix (w->desired_matrix);
15754 last_line_misfit = 1;
15756 /* Drop through and scroll. */
15757 else
15758 goto done;
15760 else
15761 clear_glyph_matrix (w->desired_matrix);
15764 try_to_scroll:
15766 w->last_modified = 0;
15767 w->last_overlay_modified = 0;
15769 /* Redisplay the mode line. Select the buffer properly for that. */
15770 if (!update_mode_line)
15772 update_mode_line = 1;
15773 w->update_mode_line = 1;
15776 /* Try to scroll by specified few lines. */
15777 if ((scroll_conservatively
15778 || emacs_scroll_step
15779 || temp_scroll_step
15780 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15781 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15782 && CHARPOS (startp) >= BEGV
15783 && CHARPOS (startp) <= ZV)
15785 /* The function returns -1 if new fonts were loaded, 1 if
15786 successful, 0 if not successful. */
15787 int ss = try_scrolling (window, just_this_one_p,
15788 scroll_conservatively,
15789 emacs_scroll_step,
15790 temp_scroll_step, last_line_misfit);
15791 switch (ss)
15793 case SCROLLING_SUCCESS:
15794 goto done;
15796 case SCROLLING_NEED_LARGER_MATRICES:
15797 goto need_larger_matrices;
15799 case SCROLLING_FAILED:
15800 break;
15802 default:
15803 emacs_abort ();
15807 /* Finally, just choose a place to start which positions point
15808 according to user preferences. */
15810 recenter:
15812 #ifdef GLYPH_DEBUG
15813 debug_method_add (w, "recenter");
15814 #endif
15816 /* Forget any previously recorded base line for line number display. */
15817 if (!buffer_unchanged_p)
15818 w->base_line_number = 0;
15820 /* Determine the window start relative to point. */
15821 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15822 it.current_y = it.last_visible_y;
15823 if (centering_position < 0)
15825 int margin =
15826 scroll_margin > 0
15827 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15828 : 0;
15829 ptrdiff_t margin_pos = CHARPOS (startp);
15830 Lisp_Object aggressive;
15831 int scrolling_up;
15833 /* If there is a scroll margin at the top of the window, find
15834 its character position. */
15835 if (margin
15836 /* Cannot call start_display if startp is not in the
15837 accessible region of the buffer. This can happen when we
15838 have just switched to a different buffer and/or changed
15839 its restriction. In that case, startp is initialized to
15840 the character position 1 (BEGV) because we did not yet
15841 have chance to display the buffer even once. */
15842 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15844 struct it it1;
15845 void *it1data = NULL;
15847 SAVE_IT (it1, it, it1data);
15848 start_display (&it1, w, startp);
15849 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15850 margin_pos = IT_CHARPOS (it1);
15851 RESTORE_IT (&it, &it, it1data);
15853 scrolling_up = PT > margin_pos;
15854 aggressive =
15855 scrolling_up
15856 ? BVAR (current_buffer, scroll_up_aggressively)
15857 : BVAR (current_buffer, scroll_down_aggressively);
15859 if (!MINI_WINDOW_P (w)
15860 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15862 int pt_offset = 0;
15864 /* Setting scroll-conservatively overrides
15865 scroll-*-aggressively. */
15866 if (!scroll_conservatively && NUMBERP (aggressive))
15868 double float_amount = XFLOATINT (aggressive);
15870 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15871 if (pt_offset == 0 && float_amount > 0)
15872 pt_offset = 1;
15873 if (pt_offset && margin > 0)
15874 margin -= 1;
15876 /* Compute how much to move the window start backward from
15877 point so that point will be displayed where the user
15878 wants it. */
15879 if (scrolling_up)
15881 centering_position = it.last_visible_y;
15882 if (pt_offset)
15883 centering_position -= pt_offset;
15884 centering_position -=
15885 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15886 + WINDOW_HEADER_LINE_HEIGHT (w);
15887 /* Don't let point enter the scroll margin near top of
15888 the window. */
15889 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15890 centering_position = margin * FRAME_LINE_HEIGHT (f);
15892 else
15893 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15895 else
15896 /* Set the window start half the height of the window backward
15897 from point. */
15898 centering_position = window_box_height (w) / 2;
15900 move_it_vertically_backward (&it, centering_position);
15902 eassert (IT_CHARPOS (it) >= BEGV);
15904 /* The function move_it_vertically_backward may move over more
15905 than the specified y-distance. If it->w is small, e.g. a
15906 mini-buffer window, we may end up in front of the window's
15907 display area. Start displaying at the start of the line
15908 containing PT in this case. */
15909 if (it.current_y <= 0)
15911 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15912 move_it_vertically_backward (&it, 0);
15913 it.current_y = 0;
15916 it.current_x = it.hpos = 0;
15918 /* Set the window start position here explicitly, to avoid an
15919 infinite loop in case the functions in window-scroll-functions
15920 get errors. */
15921 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15923 /* Run scroll hooks. */
15924 startp = run_window_scroll_functions (window, it.current.pos);
15926 /* Redisplay the window. */
15927 if (!current_matrix_up_to_date_p
15928 || windows_or_buffers_changed
15929 || cursor_type_changed
15930 /* Don't use try_window_reusing_current_matrix in this case
15931 because it can have changed the buffer. */
15932 || !NILP (Vwindow_scroll_functions)
15933 || !just_this_one_p
15934 || MINI_WINDOW_P (w)
15935 || !(used_current_matrix_p
15936 = try_window_reusing_current_matrix (w)))
15937 try_window (window, startp, 0);
15939 /* If new fonts have been loaded (due to fontsets), give up. We
15940 have to start a new redisplay since we need to re-adjust glyph
15941 matrices. */
15942 if (fonts_changed_p)
15943 goto need_larger_matrices;
15945 /* If cursor did not appear assume that the middle of the window is
15946 in the first line of the window. Do it again with the next line.
15947 (Imagine a window of height 100, displaying two lines of height
15948 60. Moving back 50 from it->last_visible_y will end in the first
15949 line.) */
15950 if (w->cursor.vpos < 0)
15952 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
15954 clear_glyph_matrix (w->desired_matrix);
15955 move_it_by_lines (&it, 1);
15956 try_window (window, it.current.pos, 0);
15958 else if (PT < IT_CHARPOS (it))
15960 clear_glyph_matrix (w->desired_matrix);
15961 move_it_by_lines (&it, -1);
15962 try_window (window, it.current.pos, 0);
15964 else
15966 /* Not much we can do about it. */
15970 /* Consider the following case: Window starts at BEGV, there is
15971 invisible, intangible text at BEGV, so that display starts at
15972 some point START > BEGV. It can happen that we are called with
15973 PT somewhere between BEGV and START. Try to handle that case. */
15974 if (w->cursor.vpos < 0)
15976 struct glyph_row *row = w->current_matrix->rows;
15977 if (row->mode_line_p)
15978 ++row;
15979 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15982 if (!cursor_row_fully_visible_p (w, 0, 0))
15984 /* If vscroll is enabled, disable it and try again. */
15985 if (w->vscroll)
15987 w->vscroll = 0;
15988 clear_glyph_matrix (w->desired_matrix);
15989 goto recenter;
15992 /* Users who set scroll-conservatively to a large number want
15993 point just above/below the scroll margin. If we ended up
15994 with point's row partially visible, move the window start to
15995 make that row fully visible and out of the margin. */
15996 if (scroll_conservatively > SCROLL_LIMIT)
15998 int margin =
15999 scroll_margin > 0
16000 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16001 : 0;
16002 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16004 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16005 clear_glyph_matrix (w->desired_matrix);
16006 if (1 == try_window (window, it.current.pos,
16007 TRY_WINDOW_CHECK_MARGINS))
16008 goto done;
16011 /* If centering point failed to make the whole line visible,
16012 put point at the top instead. That has to make the whole line
16013 visible, if it can be done. */
16014 if (centering_position == 0)
16015 goto done;
16017 clear_glyph_matrix (w->desired_matrix);
16018 centering_position = 0;
16019 goto recenter;
16022 done:
16024 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16025 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16026 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16028 /* Display the mode line, if we must. */
16029 if ((update_mode_line
16030 /* If window not full width, must redo its mode line
16031 if (a) the window to its side is being redone and
16032 (b) we do a frame-based redisplay. This is a consequence
16033 of how inverted lines are drawn in frame-based redisplay. */
16034 || (!just_this_one_p
16035 && !FRAME_WINDOW_P (f)
16036 && !WINDOW_FULL_WIDTH_P (w))
16037 /* Line number to display. */
16038 || w->base_line_pos > 0
16039 /* Column number is displayed and different from the one displayed. */
16040 || (w->column_number_displayed != -1
16041 && (w->column_number_displayed != current_column ())))
16042 /* This means that the window has a mode line. */
16043 && (WINDOW_WANTS_MODELINE_P (w)
16044 || WINDOW_WANTS_HEADER_LINE_P (w)))
16046 display_mode_lines (w);
16048 /* If mode line height has changed, arrange for a thorough
16049 immediate redisplay using the correct mode line height. */
16050 if (WINDOW_WANTS_MODELINE_P (w)
16051 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16053 fonts_changed_p = 1;
16054 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16055 = DESIRED_MODE_LINE_HEIGHT (w);
16058 /* If header line height has changed, arrange for a thorough
16059 immediate redisplay using the correct header line height. */
16060 if (WINDOW_WANTS_HEADER_LINE_P (w)
16061 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16063 fonts_changed_p = 1;
16064 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16065 = DESIRED_HEADER_LINE_HEIGHT (w);
16068 if (fonts_changed_p)
16069 goto need_larger_matrices;
16072 if (!line_number_displayed && w->base_line_pos != -1)
16074 w->base_line_pos = 0;
16075 w->base_line_number = 0;
16078 finish_menu_bars:
16080 /* When we reach a frame's selected window, redo the frame's menu bar. */
16081 if (update_mode_line
16082 && EQ (FRAME_SELECTED_WINDOW (f), window))
16084 int redisplay_menu_p = 0;
16086 if (FRAME_WINDOW_P (f))
16088 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16089 || defined (HAVE_NS) || defined (USE_GTK)
16090 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16091 #else
16092 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16093 #endif
16095 else
16096 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16098 if (redisplay_menu_p)
16099 display_menu_bar (w);
16101 #ifdef HAVE_WINDOW_SYSTEM
16102 if (FRAME_WINDOW_P (f))
16104 #if defined (USE_GTK) || defined (HAVE_NS)
16105 if (FRAME_EXTERNAL_TOOL_BAR (f))
16106 redisplay_tool_bar (f);
16107 #else
16108 if (WINDOWP (f->tool_bar_window)
16109 && (FRAME_TOOL_BAR_LINES (f) > 0
16110 || !NILP (Vauto_resize_tool_bars))
16111 && redisplay_tool_bar (f))
16112 ignore_mouse_drag_p = 1;
16113 #endif
16115 #endif
16118 #ifdef HAVE_WINDOW_SYSTEM
16119 if (FRAME_WINDOW_P (f)
16120 && update_window_fringes (w, (just_this_one_p
16121 || (!used_current_matrix_p && !overlay_arrow_seen)
16122 || w->pseudo_window_p)))
16124 update_begin (f);
16125 block_input ();
16126 if (draw_window_fringes (w, 1))
16127 x_draw_vertical_border (w);
16128 unblock_input ();
16129 update_end (f);
16131 #endif /* HAVE_WINDOW_SYSTEM */
16133 /* We go to this label, with fonts_changed_p set,
16134 if it is necessary to try again using larger glyph matrices.
16135 We have to redeem the scroll bar even in this case,
16136 because the loop in redisplay_internal expects that. */
16137 need_larger_matrices:
16139 finish_scroll_bars:
16141 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16143 /* Set the thumb's position and size. */
16144 set_vertical_scroll_bar (w);
16146 /* Note that we actually used the scroll bar attached to this
16147 window, so it shouldn't be deleted at the end of redisplay. */
16148 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16149 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16152 /* Restore current_buffer and value of point in it. The window
16153 update may have changed the buffer, so first make sure `opoint'
16154 is still valid (Bug#6177). */
16155 if (CHARPOS (opoint) < BEGV)
16156 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16157 else if (CHARPOS (opoint) > ZV)
16158 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16159 else
16160 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16162 set_buffer_internal_1 (old);
16163 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16164 shorter. This can be caused by log truncation in *Messages*. */
16165 if (CHARPOS (lpoint) <= ZV)
16166 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16168 unbind_to (count, Qnil);
16172 /* Build the complete desired matrix of WINDOW with a window start
16173 buffer position POS.
16175 Value is 1 if successful. It is zero if fonts were loaded during
16176 redisplay which makes re-adjusting glyph matrices necessary, and -1
16177 if point would appear in the scroll margins.
16178 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16179 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16180 set in FLAGS.) */
16183 try_window (Lisp_Object window, struct text_pos pos, int flags)
16185 struct window *w = XWINDOW (window);
16186 struct it it;
16187 struct glyph_row *last_text_row = NULL;
16188 struct frame *f = XFRAME (w->frame);
16190 /* Make POS the new window start. */
16191 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16193 /* Mark cursor position as unknown. No overlay arrow seen. */
16194 w->cursor.vpos = -1;
16195 overlay_arrow_seen = 0;
16197 /* Initialize iterator and info to start at POS. */
16198 start_display (&it, w, pos);
16200 /* Display all lines of W. */
16201 while (it.current_y < it.last_visible_y)
16203 if (display_line (&it))
16204 last_text_row = it.glyph_row - 1;
16205 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16206 return 0;
16209 /* Don't let the cursor end in the scroll margins. */
16210 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16211 && !MINI_WINDOW_P (w))
16213 int this_scroll_margin;
16215 if (scroll_margin > 0)
16217 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16218 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16220 else
16221 this_scroll_margin = 0;
16223 if ((w->cursor.y >= 0 /* not vscrolled */
16224 && w->cursor.y < this_scroll_margin
16225 && CHARPOS (pos) > BEGV
16226 && IT_CHARPOS (it) < ZV)
16227 /* rms: considering make_cursor_line_fully_visible_p here
16228 seems to give wrong results. We don't want to recenter
16229 when the last line is partly visible, we want to allow
16230 that case to be handled in the usual way. */
16231 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16233 w->cursor.vpos = -1;
16234 clear_glyph_matrix (w->desired_matrix);
16235 return -1;
16239 /* If bottom moved off end of frame, change mode line percentage. */
16240 if (XFASTINT (w->window_end_pos) <= 0
16241 && Z != IT_CHARPOS (it))
16242 w->update_mode_line = 1;
16244 /* Set window_end_pos to the offset of the last character displayed
16245 on the window from the end of current_buffer. Set
16246 window_end_vpos to its row number. */
16247 if (last_text_row)
16249 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16250 w->window_end_bytepos
16251 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16252 wset_window_end_pos
16253 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16254 wset_window_end_vpos
16255 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16256 eassert
16257 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16258 XFASTINT (w->window_end_vpos))));
16260 else
16262 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16263 wset_window_end_pos (w, make_number (Z - ZV));
16264 wset_window_end_vpos (w, make_number (0));
16267 /* But that is not valid info until redisplay finishes. */
16268 w->window_end_valid = 0;
16269 return 1;
16274 /************************************************************************
16275 Window redisplay reusing current matrix when buffer has not changed
16276 ************************************************************************/
16278 /* Try redisplay of window W showing an unchanged buffer with a
16279 different window start than the last time it was displayed by
16280 reusing its current matrix. Value is non-zero if successful.
16281 W->start is the new window start. */
16283 static int
16284 try_window_reusing_current_matrix (struct window *w)
16286 struct frame *f = XFRAME (w->frame);
16287 struct glyph_row *bottom_row;
16288 struct it it;
16289 struct run run;
16290 struct text_pos start, new_start;
16291 int nrows_scrolled, i;
16292 struct glyph_row *last_text_row;
16293 struct glyph_row *last_reused_text_row;
16294 struct glyph_row *start_row;
16295 int start_vpos, min_y, max_y;
16297 #ifdef GLYPH_DEBUG
16298 if (inhibit_try_window_reusing)
16299 return 0;
16300 #endif
16302 if (/* This function doesn't handle terminal frames. */
16303 !FRAME_WINDOW_P (f)
16304 /* Don't try to reuse the display if windows have been split
16305 or such. */
16306 || windows_or_buffers_changed
16307 || cursor_type_changed)
16308 return 0;
16310 /* Can't do this if region may have changed. */
16311 if (markpos_of_region () >= 0
16312 || w->region_showing
16313 || !NILP (Vshow_trailing_whitespace))
16314 return 0;
16316 /* If top-line visibility has changed, give up. */
16317 if (WINDOW_WANTS_HEADER_LINE_P (w)
16318 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16319 return 0;
16321 /* Give up if old or new display is scrolled vertically. We could
16322 make this function handle this, but right now it doesn't. */
16323 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16324 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16325 return 0;
16327 /* The variable new_start now holds the new window start. The old
16328 start `start' can be determined from the current matrix. */
16329 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16330 start = start_row->minpos;
16331 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16333 /* Clear the desired matrix for the display below. */
16334 clear_glyph_matrix (w->desired_matrix);
16336 if (CHARPOS (new_start) <= CHARPOS (start))
16338 /* Don't use this method if the display starts with an ellipsis
16339 displayed for invisible text. It's not easy to handle that case
16340 below, and it's certainly not worth the effort since this is
16341 not a frequent case. */
16342 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16343 return 0;
16345 IF_DEBUG (debug_method_add (w, "twu1"));
16347 /* Display up to a row that can be reused. The variable
16348 last_text_row is set to the last row displayed that displays
16349 text. Note that it.vpos == 0 if or if not there is a
16350 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16351 start_display (&it, w, new_start);
16352 w->cursor.vpos = -1;
16353 last_text_row = last_reused_text_row = NULL;
16355 while (it.current_y < it.last_visible_y
16356 && !fonts_changed_p)
16358 /* If we have reached into the characters in the START row,
16359 that means the line boundaries have changed. So we
16360 can't start copying with the row START. Maybe it will
16361 work to start copying with the following row. */
16362 while (IT_CHARPOS (it) > CHARPOS (start))
16364 /* Advance to the next row as the "start". */
16365 start_row++;
16366 start = start_row->minpos;
16367 /* If there are no more rows to try, or just one, give up. */
16368 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16369 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16370 || CHARPOS (start) == ZV)
16372 clear_glyph_matrix (w->desired_matrix);
16373 return 0;
16376 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16378 /* If we have reached alignment, we can copy the rest of the
16379 rows. */
16380 if (IT_CHARPOS (it) == CHARPOS (start)
16381 /* Don't accept "alignment" inside a display vector,
16382 since start_row could have started in the middle of
16383 that same display vector (thus their character
16384 positions match), and we have no way of telling if
16385 that is the case. */
16386 && it.current.dpvec_index < 0)
16387 break;
16389 if (display_line (&it))
16390 last_text_row = it.glyph_row - 1;
16394 /* A value of current_y < last_visible_y means that we stopped
16395 at the previous window start, which in turn means that we
16396 have at least one reusable row. */
16397 if (it.current_y < it.last_visible_y)
16399 struct glyph_row *row;
16401 /* IT.vpos always starts from 0; it counts text lines. */
16402 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16404 /* Find PT if not already found in the lines displayed. */
16405 if (w->cursor.vpos < 0)
16407 int dy = it.current_y - start_row->y;
16409 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16410 row = row_containing_pos (w, PT, row, NULL, dy);
16411 if (row)
16412 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16413 dy, nrows_scrolled);
16414 else
16416 clear_glyph_matrix (w->desired_matrix);
16417 return 0;
16421 /* Scroll the display. Do it before the current matrix is
16422 changed. The problem here is that update has not yet
16423 run, i.e. part of the current matrix is not up to date.
16424 scroll_run_hook will clear the cursor, and use the
16425 current matrix to get the height of the row the cursor is
16426 in. */
16427 run.current_y = start_row->y;
16428 run.desired_y = it.current_y;
16429 run.height = it.last_visible_y - it.current_y;
16431 if (run.height > 0 && run.current_y != run.desired_y)
16433 update_begin (f);
16434 FRAME_RIF (f)->update_window_begin_hook (w);
16435 FRAME_RIF (f)->clear_window_mouse_face (w);
16436 FRAME_RIF (f)->scroll_run_hook (w, &run);
16437 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16438 update_end (f);
16441 /* Shift current matrix down by nrows_scrolled lines. */
16442 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16443 rotate_matrix (w->current_matrix,
16444 start_vpos,
16445 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16446 nrows_scrolled);
16448 /* Disable lines that must be updated. */
16449 for (i = 0; i < nrows_scrolled; ++i)
16450 (start_row + i)->enabled_p = 0;
16452 /* Re-compute Y positions. */
16453 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16454 max_y = it.last_visible_y;
16455 for (row = start_row + nrows_scrolled;
16456 row < bottom_row;
16457 ++row)
16459 row->y = it.current_y;
16460 row->visible_height = row->height;
16462 if (row->y < min_y)
16463 row->visible_height -= min_y - row->y;
16464 if (row->y + row->height > max_y)
16465 row->visible_height -= row->y + row->height - max_y;
16466 if (row->fringe_bitmap_periodic_p)
16467 row->redraw_fringe_bitmaps_p = 1;
16469 it.current_y += row->height;
16471 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16472 last_reused_text_row = row;
16473 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16474 break;
16477 /* Disable lines in the current matrix which are now
16478 below the window. */
16479 for (++row; row < bottom_row; ++row)
16480 row->enabled_p = row->mode_line_p = 0;
16483 /* Update window_end_pos etc.; last_reused_text_row is the last
16484 reused row from the current matrix containing text, if any.
16485 The value of last_text_row is the last displayed line
16486 containing text. */
16487 if (last_reused_text_row)
16489 w->window_end_bytepos
16490 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16491 wset_window_end_pos
16492 (w, make_number (Z
16493 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16494 wset_window_end_vpos
16495 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16496 w->current_matrix)));
16498 else if (last_text_row)
16500 w->window_end_bytepos
16501 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16502 wset_window_end_pos
16503 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16504 wset_window_end_vpos
16505 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16506 w->desired_matrix)));
16508 else
16510 /* This window must be completely empty. */
16511 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16512 wset_window_end_pos (w, make_number (Z - ZV));
16513 wset_window_end_vpos (w, make_number (0));
16515 w->window_end_valid = 0;
16517 /* Update hint: don't try scrolling again in update_window. */
16518 w->desired_matrix->no_scrolling_p = 1;
16520 #ifdef GLYPH_DEBUG
16521 debug_method_add (w, "try_window_reusing_current_matrix 1");
16522 #endif
16523 return 1;
16525 else if (CHARPOS (new_start) > CHARPOS (start))
16527 struct glyph_row *pt_row, *row;
16528 struct glyph_row *first_reusable_row;
16529 struct glyph_row *first_row_to_display;
16530 int dy;
16531 int yb = window_text_bottom_y (w);
16533 /* Find the row starting at new_start, if there is one. Don't
16534 reuse a partially visible line at the end. */
16535 first_reusable_row = start_row;
16536 while (first_reusable_row->enabled_p
16537 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16538 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16539 < CHARPOS (new_start)))
16540 ++first_reusable_row;
16542 /* Give up if there is no row to reuse. */
16543 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16544 || !first_reusable_row->enabled_p
16545 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16546 != CHARPOS (new_start)))
16547 return 0;
16549 /* We can reuse fully visible rows beginning with
16550 first_reusable_row to the end of the window. Set
16551 first_row_to_display to the first row that cannot be reused.
16552 Set pt_row to the row containing point, if there is any. */
16553 pt_row = NULL;
16554 for (first_row_to_display = first_reusable_row;
16555 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16556 ++first_row_to_display)
16558 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16559 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16560 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16561 && first_row_to_display->ends_at_zv_p
16562 && pt_row == NULL)))
16563 pt_row = first_row_to_display;
16566 /* Start displaying at the start of first_row_to_display. */
16567 eassert (first_row_to_display->y < yb);
16568 init_to_row_start (&it, w, first_row_to_display);
16570 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16571 - start_vpos);
16572 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16573 - nrows_scrolled);
16574 it.current_y = (first_row_to_display->y - first_reusable_row->y
16575 + WINDOW_HEADER_LINE_HEIGHT (w));
16577 /* Display lines beginning with first_row_to_display in the
16578 desired matrix. Set last_text_row to the last row displayed
16579 that displays text. */
16580 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16581 if (pt_row == NULL)
16582 w->cursor.vpos = -1;
16583 last_text_row = NULL;
16584 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16585 if (display_line (&it))
16586 last_text_row = it.glyph_row - 1;
16588 /* If point is in a reused row, adjust y and vpos of the cursor
16589 position. */
16590 if (pt_row)
16592 w->cursor.vpos -= nrows_scrolled;
16593 w->cursor.y -= first_reusable_row->y - start_row->y;
16596 /* Give up if point isn't in a row displayed or reused. (This
16597 also handles the case where w->cursor.vpos < nrows_scrolled
16598 after the calls to display_line, which can happen with scroll
16599 margins. See bug#1295.) */
16600 if (w->cursor.vpos < 0)
16602 clear_glyph_matrix (w->desired_matrix);
16603 return 0;
16606 /* Scroll the display. */
16607 run.current_y = first_reusable_row->y;
16608 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16609 run.height = it.last_visible_y - run.current_y;
16610 dy = run.current_y - run.desired_y;
16612 if (run.height)
16614 update_begin (f);
16615 FRAME_RIF (f)->update_window_begin_hook (w);
16616 FRAME_RIF (f)->clear_window_mouse_face (w);
16617 FRAME_RIF (f)->scroll_run_hook (w, &run);
16618 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16619 update_end (f);
16622 /* Adjust Y positions of reused rows. */
16623 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16624 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16625 max_y = it.last_visible_y;
16626 for (row = first_reusable_row; row < first_row_to_display; ++row)
16628 row->y -= dy;
16629 row->visible_height = row->height;
16630 if (row->y < min_y)
16631 row->visible_height -= min_y - row->y;
16632 if (row->y + row->height > max_y)
16633 row->visible_height -= row->y + row->height - max_y;
16634 if (row->fringe_bitmap_periodic_p)
16635 row->redraw_fringe_bitmaps_p = 1;
16638 /* Scroll the current matrix. */
16639 eassert (nrows_scrolled > 0);
16640 rotate_matrix (w->current_matrix,
16641 start_vpos,
16642 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16643 -nrows_scrolled);
16645 /* Disable rows not reused. */
16646 for (row -= nrows_scrolled; row < bottom_row; ++row)
16647 row->enabled_p = 0;
16649 /* Point may have moved to a different line, so we cannot assume that
16650 the previous cursor position is valid; locate the correct row. */
16651 if (pt_row)
16653 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16654 row < bottom_row
16655 && PT >= MATRIX_ROW_END_CHARPOS (row)
16656 && !row->ends_at_zv_p;
16657 row++)
16659 w->cursor.vpos++;
16660 w->cursor.y = row->y;
16662 if (row < bottom_row)
16664 /* Can't simply scan the row for point with
16665 bidi-reordered glyph rows. Let set_cursor_from_row
16666 figure out where to put the cursor, and if it fails,
16667 give up. */
16668 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16670 if (!set_cursor_from_row (w, row, w->current_matrix,
16671 0, 0, 0, 0))
16673 clear_glyph_matrix (w->desired_matrix);
16674 return 0;
16677 else
16679 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16680 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16682 for (; glyph < end
16683 && (!BUFFERP (glyph->object)
16684 || glyph->charpos < PT);
16685 glyph++)
16687 w->cursor.hpos++;
16688 w->cursor.x += glyph->pixel_width;
16694 /* Adjust window end. A null value of last_text_row means that
16695 the window end is in reused rows which in turn means that
16696 only its vpos can have changed. */
16697 if (last_text_row)
16699 w->window_end_bytepos
16700 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16701 wset_window_end_pos
16702 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16703 wset_window_end_vpos
16704 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16705 w->desired_matrix)));
16707 else
16709 wset_window_end_vpos
16710 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16713 w->window_end_valid = 0;
16714 w->desired_matrix->no_scrolling_p = 1;
16716 #ifdef GLYPH_DEBUG
16717 debug_method_add (w, "try_window_reusing_current_matrix 2");
16718 #endif
16719 return 1;
16722 return 0;
16727 /************************************************************************
16728 Window redisplay reusing current matrix when buffer has changed
16729 ************************************************************************/
16731 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16732 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16733 ptrdiff_t *, ptrdiff_t *);
16734 static struct glyph_row *
16735 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16736 struct glyph_row *);
16739 /* Return the last row in MATRIX displaying text. If row START is
16740 non-null, start searching with that row. IT gives the dimensions
16741 of the display. Value is null if matrix is empty; otherwise it is
16742 a pointer to the row found. */
16744 static struct glyph_row *
16745 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16746 struct glyph_row *start)
16748 struct glyph_row *row, *row_found;
16750 /* Set row_found to the last row in IT->w's current matrix
16751 displaying text. The loop looks funny but think of partially
16752 visible lines. */
16753 row_found = NULL;
16754 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16755 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16757 eassert (row->enabled_p);
16758 row_found = row;
16759 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16760 break;
16761 ++row;
16764 return row_found;
16768 /* Return the last row in the current matrix of W that is not affected
16769 by changes at the start of current_buffer that occurred since W's
16770 current matrix was built. Value is null if no such row exists.
16772 BEG_UNCHANGED us the number of characters unchanged at the start of
16773 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16774 first changed character in current_buffer. Characters at positions <
16775 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16776 when the current matrix was built. */
16778 static struct glyph_row *
16779 find_last_unchanged_at_beg_row (struct window *w)
16781 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16782 struct glyph_row *row;
16783 struct glyph_row *row_found = NULL;
16784 int yb = window_text_bottom_y (w);
16786 /* Find the last row displaying unchanged text. */
16787 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16788 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16789 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16790 ++row)
16792 if (/* If row ends before first_changed_pos, it is unchanged,
16793 except in some case. */
16794 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16795 /* When row ends in ZV and we write at ZV it is not
16796 unchanged. */
16797 && !row->ends_at_zv_p
16798 /* When first_changed_pos is the end of a continued line,
16799 row is not unchanged because it may be no longer
16800 continued. */
16801 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16802 && (row->continued_p
16803 || row->exact_window_width_line_p))
16804 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16805 needs to be recomputed, so don't consider this row as
16806 unchanged. This happens when the last line was
16807 bidi-reordered and was killed immediately before this
16808 redisplay cycle. In that case, ROW->end stores the
16809 buffer position of the first visual-order character of
16810 the killed text, which is now beyond ZV. */
16811 && CHARPOS (row->end.pos) <= ZV)
16812 row_found = row;
16814 /* Stop if last visible row. */
16815 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16816 break;
16819 return row_found;
16823 /* Find the first glyph row in the current matrix of W that is not
16824 affected by changes at the end of current_buffer since the
16825 time W's current matrix was built.
16827 Return in *DELTA the number of chars by which buffer positions in
16828 unchanged text at the end of current_buffer must be adjusted.
16830 Return in *DELTA_BYTES the corresponding number of bytes.
16832 Value is null if no such row exists, i.e. all rows are affected by
16833 changes. */
16835 static struct glyph_row *
16836 find_first_unchanged_at_end_row (struct window *w,
16837 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16839 struct glyph_row *row;
16840 struct glyph_row *row_found = NULL;
16842 *delta = *delta_bytes = 0;
16844 /* Display must not have been paused, otherwise the current matrix
16845 is not up to date. */
16846 eassert (w->window_end_valid);
16848 /* A value of window_end_pos >= END_UNCHANGED means that the window
16849 end is in the range of changed text. If so, there is no
16850 unchanged row at the end of W's current matrix. */
16851 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16852 return NULL;
16854 /* Set row to the last row in W's current matrix displaying text. */
16855 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16857 /* If matrix is entirely empty, no unchanged row exists. */
16858 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16860 /* The value of row is the last glyph row in the matrix having a
16861 meaningful buffer position in it. The end position of row
16862 corresponds to window_end_pos. This allows us to translate
16863 buffer positions in the current matrix to current buffer
16864 positions for characters not in changed text. */
16865 ptrdiff_t Z_old =
16866 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16867 ptrdiff_t Z_BYTE_old =
16868 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16869 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16870 struct glyph_row *first_text_row
16871 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16873 *delta = Z - Z_old;
16874 *delta_bytes = Z_BYTE - Z_BYTE_old;
16876 /* Set last_unchanged_pos to the buffer position of the last
16877 character in the buffer that has not been changed. Z is the
16878 index + 1 of the last character in current_buffer, i.e. by
16879 subtracting END_UNCHANGED we get the index of the last
16880 unchanged character, and we have to add BEG to get its buffer
16881 position. */
16882 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16883 last_unchanged_pos_old = last_unchanged_pos - *delta;
16885 /* Search backward from ROW for a row displaying a line that
16886 starts at a minimum position >= last_unchanged_pos_old. */
16887 for (; row > first_text_row; --row)
16889 /* This used to abort, but it can happen.
16890 It is ok to just stop the search instead here. KFS. */
16891 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16892 break;
16894 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16895 row_found = row;
16899 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16901 return row_found;
16905 /* Make sure that glyph rows in the current matrix of window W
16906 reference the same glyph memory as corresponding rows in the
16907 frame's frame matrix. This function is called after scrolling W's
16908 current matrix on a terminal frame in try_window_id and
16909 try_window_reusing_current_matrix. */
16911 static void
16912 sync_frame_with_window_matrix_rows (struct window *w)
16914 struct frame *f = XFRAME (w->frame);
16915 struct glyph_row *window_row, *window_row_end, *frame_row;
16917 /* Preconditions: W must be a leaf window and full-width. Its frame
16918 must have a frame matrix. */
16919 eassert (BUFFERP (w->contents));
16920 eassert (WINDOW_FULL_WIDTH_P (w));
16921 eassert (!FRAME_WINDOW_P (f));
16923 /* If W is a full-width window, glyph pointers in W's current matrix
16924 have, by definition, to be the same as glyph pointers in the
16925 corresponding frame matrix. Note that frame matrices have no
16926 marginal areas (see build_frame_matrix). */
16927 window_row = w->current_matrix->rows;
16928 window_row_end = window_row + w->current_matrix->nrows;
16929 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16930 while (window_row < window_row_end)
16932 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16933 struct glyph *end = window_row->glyphs[LAST_AREA];
16935 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16936 frame_row->glyphs[TEXT_AREA] = start;
16937 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16938 frame_row->glyphs[LAST_AREA] = end;
16940 /* Disable frame rows whose corresponding window rows have
16941 been disabled in try_window_id. */
16942 if (!window_row->enabled_p)
16943 frame_row->enabled_p = 0;
16945 ++window_row, ++frame_row;
16950 /* Find the glyph row in window W containing CHARPOS. Consider all
16951 rows between START and END (not inclusive). END null means search
16952 all rows to the end of the display area of W. Value is the row
16953 containing CHARPOS or null. */
16955 struct glyph_row *
16956 row_containing_pos (struct window *w, ptrdiff_t charpos,
16957 struct glyph_row *start, struct glyph_row *end, int dy)
16959 struct glyph_row *row = start;
16960 struct glyph_row *best_row = NULL;
16961 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16962 int last_y;
16964 /* If we happen to start on a header-line, skip that. */
16965 if (row->mode_line_p)
16966 ++row;
16968 if ((end && row >= end) || !row->enabled_p)
16969 return NULL;
16971 last_y = window_text_bottom_y (w) - dy;
16973 while (1)
16975 /* Give up if we have gone too far. */
16976 if (end && row >= end)
16977 return NULL;
16978 /* This formerly returned if they were equal.
16979 I think that both quantities are of a "last plus one" type;
16980 if so, when they are equal, the row is within the screen. -- rms. */
16981 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16982 return NULL;
16984 /* If it is in this row, return this row. */
16985 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16986 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16987 /* The end position of a row equals the start
16988 position of the next row. If CHARPOS is there, we
16989 would rather consider it displayed in the next
16990 line, except when this line ends in ZV. */
16991 && !row_for_charpos_p (row, charpos)))
16992 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16994 struct glyph *g;
16996 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
16997 || (!best_row && !row->continued_p))
16998 return row;
16999 /* In bidi-reordered rows, there could be several rows whose
17000 edges surround CHARPOS, all of these rows belonging to
17001 the same continued line. We need to find the row which
17002 fits CHARPOS the best. */
17003 for (g = row->glyphs[TEXT_AREA];
17004 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17005 g++)
17007 if (!STRINGP (g->object))
17009 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17011 mindif = eabs (g->charpos - charpos);
17012 best_row = row;
17013 /* Exact match always wins. */
17014 if (mindif == 0)
17015 return best_row;
17020 else if (best_row && !row->continued_p)
17021 return best_row;
17022 ++row;
17027 /* Try to redisplay window W by reusing its existing display. W's
17028 current matrix must be up to date when this function is called,
17029 i.e. window_end_valid must be nonzero.
17031 Value is
17033 1 if display has been updated
17034 0 if otherwise unsuccessful
17035 -1 if redisplay with same window start is known not to succeed
17037 The following steps are performed:
17039 1. Find the last row in the current matrix of W that is not
17040 affected by changes at the start of current_buffer. If no such row
17041 is found, give up.
17043 2. Find the first row in W's current matrix that is not affected by
17044 changes at the end of current_buffer. Maybe there is no such row.
17046 3. Display lines beginning with the row + 1 found in step 1 to the
17047 row found in step 2 or, if step 2 didn't find a row, to the end of
17048 the window.
17050 4. If cursor is not known to appear on the window, give up.
17052 5. If display stopped at the row found in step 2, scroll the
17053 display and current matrix as needed.
17055 6. Maybe display some lines at the end of W, if we must. This can
17056 happen under various circumstances, like a partially visible line
17057 becoming fully visible, or because newly displayed lines are displayed
17058 in smaller font sizes.
17060 7. Update W's window end information. */
17062 static int
17063 try_window_id (struct window *w)
17065 struct frame *f = XFRAME (w->frame);
17066 struct glyph_matrix *current_matrix = w->current_matrix;
17067 struct glyph_matrix *desired_matrix = w->desired_matrix;
17068 struct glyph_row *last_unchanged_at_beg_row;
17069 struct glyph_row *first_unchanged_at_end_row;
17070 struct glyph_row *row;
17071 struct glyph_row *bottom_row;
17072 int bottom_vpos;
17073 struct it it;
17074 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17075 int dvpos, dy;
17076 struct text_pos start_pos;
17077 struct run run;
17078 int first_unchanged_at_end_vpos = 0;
17079 struct glyph_row *last_text_row, *last_text_row_at_end;
17080 struct text_pos start;
17081 ptrdiff_t first_changed_charpos, last_changed_charpos;
17083 #ifdef GLYPH_DEBUG
17084 if (inhibit_try_window_id)
17085 return 0;
17086 #endif
17088 /* This is handy for debugging. */
17089 #if 0
17090 #define GIVE_UP(X) \
17091 do { \
17092 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17093 return 0; \
17094 } while (0)
17095 #else
17096 #define GIVE_UP(X) return 0
17097 #endif
17099 SET_TEXT_POS_FROM_MARKER (start, w->start);
17101 /* Don't use this for mini-windows because these can show
17102 messages and mini-buffers, and we don't handle that here. */
17103 if (MINI_WINDOW_P (w))
17104 GIVE_UP (1);
17106 /* This flag is used to prevent redisplay optimizations. */
17107 if (windows_or_buffers_changed || cursor_type_changed)
17108 GIVE_UP (2);
17110 /* Verify that narrowing has not changed.
17111 Also verify that we were not told to prevent redisplay optimizations.
17112 It would be nice to further
17113 reduce the number of cases where this prevents try_window_id. */
17114 if (current_buffer->clip_changed
17115 || current_buffer->prevent_redisplay_optimizations_p)
17116 GIVE_UP (3);
17118 /* Window must either use window-based redisplay or be full width. */
17119 if (!FRAME_WINDOW_P (f)
17120 && (!FRAME_LINE_INS_DEL_OK (f)
17121 || !WINDOW_FULL_WIDTH_P (w)))
17122 GIVE_UP (4);
17124 /* Give up if point is known NOT to appear in W. */
17125 if (PT < CHARPOS (start))
17126 GIVE_UP (5);
17128 /* Another way to prevent redisplay optimizations. */
17129 if (w->last_modified == 0)
17130 GIVE_UP (6);
17132 /* Verify that window is not hscrolled. */
17133 if (w->hscroll != 0)
17134 GIVE_UP (7);
17136 /* Verify that display wasn't paused. */
17137 if (!w->window_end_valid)
17138 GIVE_UP (8);
17140 /* Can't use this if highlighting a region because a cursor movement
17141 will do more than just set the cursor. */
17142 if (markpos_of_region () >= 0)
17143 GIVE_UP (9);
17145 /* Likewise if highlighting trailing whitespace. */
17146 if (!NILP (Vshow_trailing_whitespace))
17147 GIVE_UP (11);
17149 /* Likewise if showing a region. */
17150 if (w->region_showing)
17151 GIVE_UP (10);
17153 /* Can't use this if overlay arrow position and/or string have
17154 changed. */
17155 if (overlay_arrows_changed_p ())
17156 GIVE_UP (12);
17158 /* When word-wrap is on, adding a space to the first word of a
17159 wrapped line can change the wrap position, altering the line
17160 above it. It might be worthwhile to handle this more
17161 intelligently, but for now just redisplay from scratch. */
17162 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17163 GIVE_UP (21);
17165 /* Under bidi reordering, adding or deleting a character in the
17166 beginning of a paragraph, before the first strong directional
17167 character, can change the base direction of the paragraph (unless
17168 the buffer specifies a fixed paragraph direction), which will
17169 require to redisplay the whole paragraph. It might be worthwhile
17170 to find the paragraph limits and widen the range of redisplayed
17171 lines to that, but for now just give up this optimization and
17172 redisplay from scratch. */
17173 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17174 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17175 GIVE_UP (22);
17177 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17178 only if buffer has really changed. The reason is that the gap is
17179 initially at Z for freshly visited files. The code below would
17180 set end_unchanged to 0 in that case. */
17181 if (MODIFF > SAVE_MODIFF
17182 /* This seems to happen sometimes after saving a buffer. */
17183 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17185 if (GPT - BEG < BEG_UNCHANGED)
17186 BEG_UNCHANGED = GPT - BEG;
17187 if (Z - GPT < END_UNCHANGED)
17188 END_UNCHANGED = Z - GPT;
17191 /* The position of the first and last character that has been changed. */
17192 first_changed_charpos = BEG + BEG_UNCHANGED;
17193 last_changed_charpos = Z - END_UNCHANGED;
17195 /* If window starts after a line end, and the last change is in
17196 front of that newline, then changes don't affect the display.
17197 This case happens with stealth-fontification. Note that although
17198 the display is unchanged, glyph positions in the matrix have to
17199 be adjusted, of course. */
17200 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17201 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17202 && ((last_changed_charpos < CHARPOS (start)
17203 && CHARPOS (start) == BEGV)
17204 || (last_changed_charpos < CHARPOS (start) - 1
17205 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17207 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17208 struct glyph_row *r0;
17210 /* Compute how many chars/bytes have been added to or removed
17211 from the buffer. */
17212 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17213 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17214 Z_delta = Z - Z_old;
17215 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17217 /* Give up if PT is not in the window. Note that it already has
17218 been checked at the start of try_window_id that PT is not in
17219 front of the window start. */
17220 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17221 GIVE_UP (13);
17223 /* If window start is unchanged, we can reuse the whole matrix
17224 as is, after adjusting glyph positions. No need to compute
17225 the window end again, since its offset from Z hasn't changed. */
17226 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17227 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17228 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17229 /* PT must not be in a partially visible line. */
17230 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17231 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17233 /* Adjust positions in the glyph matrix. */
17234 if (Z_delta || Z_delta_bytes)
17236 struct glyph_row *r1
17237 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17238 increment_matrix_positions (w->current_matrix,
17239 MATRIX_ROW_VPOS (r0, current_matrix),
17240 MATRIX_ROW_VPOS (r1, current_matrix),
17241 Z_delta, Z_delta_bytes);
17244 /* Set the cursor. */
17245 row = row_containing_pos (w, PT, r0, NULL, 0);
17246 if (row)
17247 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17248 else
17249 emacs_abort ();
17250 return 1;
17254 /* Handle the case that changes are all below what is displayed in
17255 the window, and that PT is in the window. This shortcut cannot
17256 be taken if ZV is visible in the window, and text has been added
17257 there that is visible in the window. */
17258 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17259 /* ZV is not visible in the window, or there are no
17260 changes at ZV, actually. */
17261 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17262 || first_changed_charpos == last_changed_charpos))
17264 struct glyph_row *r0;
17266 /* Give up if PT is not in the window. Note that it already has
17267 been checked at the start of try_window_id that PT is not in
17268 front of the window start. */
17269 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17270 GIVE_UP (14);
17272 /* If window start is unchanged, we can reuse the whole matrix
17273 as is, without changing glyph positions since no text has
17274 been added/removed in front of the window end. */
17275 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17276 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17277 /* PT must not be in a partially visible line. */
17278 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17279 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17281 /* We have to compute the window end anew since text
17282 could have been added/removed after it. */
17283 wset_window_end_pos
17284 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17285 w->window_end_bytepos
17286 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17288 /* Set the cursor. */
17289 row = row_containing_pos (w, PT, r0, NULL, 0);
17290 if (row)
17291 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17292 else
17293 emacs_abort ();
17294 return 2;
17298 /* Give up if window start is in the changed area.
17300 The condition used to read
17302 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17304 but why that was tested escapes me at the moment. */
17305 if (CHARPOS (start) >= first_changed_charpos
17306 && CHARPOS (start) <= last_changed_charpos)
17307 GIVE_UP (15);
17309 /* Check that window start agrees with the start of the first glyph
17310 row in its current matrix. Check this after we know the window
17311 start is not in changed text, otherwise positions would not be
17312 comparable. */
17313 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17314 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17315 GIVE_UP (16);
17317 /* Give up if the window ends in strings. Overlay strings
17318 at the end are difficult to handle, so don't try. */
17319 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17320 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17321 GIVE_UP (20);
17323 /* Compute the position at which we have to start displaying new
17324 lines. Some of the lines at the top of the window might be
17325 reusable because they are not displaying changed text. Find the
17326 last row in W's current matrix not affected by changes at the
17327 start of current_buffer. Value is null if changes start in the
17328 first line of window. */
17329 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17330 if (last_unchanged_at_beg_row)
17332 /* Avoid starting to display in the middle of a character, a TAB
17333 for instance. This is easier than to set up the iterator
17334 exactly, and it's not a frequent case, so the additional
17335 effort wouldn't really pay off. */
17336 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17337 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17338 && last_unchanged_at_beg_row > w->current_matrix->rows)
17339 --last_unchanged_at_beg_row;
17341 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17342 GIVE_UP (17);
17344 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17345 GIVE_UP (18);
17346 start_pos = it.current.pos;
17348 /* Start displaying new lines in the desired matrix at the same
17349 vpos we would use in the current matrix, i.e. below
17350 last_unchanged_at_beg_row. */
17351 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17352 current_matrix);
17353 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17354 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17356 eassert (it.hpos == 0 && it.current_x == 0);
17358 else
17360 /* There are no reusable lines at the start of the window.
17361 Start displaying in the first text line. */
17362 start_display (&it, w, start);
17363 it.vpos = it.first_vpos;
17364 start_pos = it.current.pos;
17367 /* Find the first row that is not affected by changes at the end of
17368 the buffer. Value will be null if there is no unchanged row, in
17369 which case we must redisplay to the end of the window. delta
17370 will be set to the value by which buffer positions beginning with
17371 first_unchanged_at_end_row have to be adjusted due to text
17372 changes. */
17373 first_unchanged_at_end_row
17374 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17375 IF_DEBUG (debug_delta = delta);
17376 IF_DEBUG (debug_delta_bytes = delta_bytes);
17378 /* Set stop_pos to the buffer position up to which we will have to
17379 display new lines. If first_unchanged_at_end_row != NULL, this
17380 is the buffer position of the start of the line displayed in that
17381 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17382 that we don't stop at a buffer position. */
17383 stop_pos = 0;
17384 if (first_unchanged_at_end_row)
17386 eassert (last_unchanged_at_beg_row == NULL
17387 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17389 /* If this is a continuation line, move forward to the next one
17390 that isn't. Changes in lines above affect this line.
17391 Caution: this may move first_unchanged_at_end_row to a row
17392 not displaying text. */
17393 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17394 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17395 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17396 < it.last_visible_y))
17397 ++first_unchanged_at_end_row;
17399 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17400 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17401 >= it.last_visible_y))
17402 first_unchanged_at_end_row = NULL;
17403 else
17405 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17406 + delta);
17407 first_unchanged_at_end_vpos
17408 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17409 eassert (stop_pos >= Z - END_UNCHANGED);
17412 else if (last_unchanged_at_beg_row == NULL)
17413 GIVE_UP (19);
17416 #ifdef GLYPH_DEBUG
17418 /* Either there is no unchanged row at the end, or the one we have
17419 now displays text. This is a necessary condition for the window
17420 end pos calculation at the end of this function. */
17421 eassert (first_unchanged_at_end_row == NULL
17422 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17424 debug_last_unchanged_at_beg_vpos
17425 = (last_unchanged_at_beg_row
17426 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17427 : -1);
17428 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17430 #endif /* GLYPH_DEBUG */
17433 /* Display new lines. Set last_text_row to the last new line
17434 displayed which has text on it, i.e. might end up as being the
17435 line where the window_end_vpos is. */
17436 w->cursor.vpos = -1;
17437 last_text_row = NULL;
17438 overlay_arrow_seen = 0;
17439 while (it.current_y < it.last_visible_y
17440 && !fonts_changed_p
17441 && (first_unchanged_at_end_row == NULL
17442 || IT_CHARPOS (it) < stop_pos))
17444 if (display_line (&it))
17445 last_text_row = it.glyph_row - 1;
17448 if (fonts_changed_p)
17449 return -1;
17452 /* Compute differences in buffer positions, y-positions etc. for
17453 lines reused at the bottom of the window. Compute what we can
17454 scroll. */
17455 if (first_unchanged_at_end_row
17456 /* No lines reused because we displayed everything up to the
17457 bottom of the window. */
17458 && it.current_y < it.last_visible_y)
17460 dvpos = (it.vpos
17461 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17462 current_matrix));
17463 dy = it.current_y - first_unchanged_at_end_row->y;
17464 run.current_y = first_unchanged_at_end_row->y;
17465 run.desired_y = run.current_y + dy;
17466 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17468 else
17470 delta = delta_bytes = dvpos = dy
17471 = run.current_y = run.desired_y = run.height = 0;
17472 first_unchanged_at_end_row = NULL;
17474 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17477 /* Find the cursor if not already found. We have to decide whether
17478 PT will appear on this window (it sometimes doesn't, but this is
17479 not a very frequent case.) This decision has to be made before
17480 the current matrix is altered. A value of cursor.vpos < 0 means
17481 that PT is either in one of the lines beginning at
17482 first_unchanged_at_end_row or below the window. Don't care for
17483 lines that might be displayed later at the window end; as
17484 mentioned, this is not a frequent case. */
17485 if (w->cursor.vpos < 0)
17487 /* Cursor in unchanged rows at the top? */
17488 if (PT < CHARPOS (start_pos)
17489 && last_unchanged_at_beg_row)
17491 row = row_containing_pos (w, PT,
17492 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17493 last_unchanged_at_beg_row + 1, 0);
17494 if (row)
17495 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17498 /* Start from first_unchanged_at_end_row looking for PT. */
17499 else if (first_unchanged_at_end_row)
17501 row = row_containing_pos (w, PT - delta,
17502 first_unchanged_at_end_row, NULL, 0);
17503 if (row)
17504 set_cursor_from_row (w, row, w->current_matrix, delta,
17505 delta_bytes, dy, dvpos);
17508 /* Give up if cursor was not found. */
17509 if (w->cursor.vpos < 0)
17511 clear_glyph_matrix (w->desired_matrix);
17512 return -1;
17516 /* Don't let the cursor end in the scroll margins. */
17518 int this_scroll_margin, cursor_height;
17520 this_scroll_margin =
17521 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17522 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17523 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17525 if ((w->cursor.y < this_scroll_margin
17526 && CHARPOS (start) > BEGV)
17527 /* Old redisplay didn't take scroll margin into account at the bottom,
17528 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17529 || (w->cursor.y + (make_cursor_line_fully_visible_p
17530 ? cursor_height + this_scroll_margin
17531 : 1)) > it.last_visible_y)
17533 w->cursor.vpos = -1;
17534 clear_glyph_matrix (w->desired_matrix);
17535 return -1;
17539 /* Scroll the display. Do it before changing the current matrix so
17540 that xterm.c doesn't get confused about where the cursor glyph is
17541 found. */
17542 if (dy && run.height)
17544 update_begin (f);
17546 if (FRAME_WINDOW_P (f))
17548 FRAME_RIF (f)->update_window_begin_hook (w);
17549 FRAME_RIF (f)->clear_window_mouse_face (w);
17550 FRAME_RIF (f)->scroll_run_hook (w, &run);
17551 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17553 else
17555 /* Terminal frame. In this case, dvpos gives the number of
17556 lines to scroll by; dvpos < 0 means scroll up. */
17557 int from_vpos
17558 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17559 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17560 int end = (WINDOW_TOP_EDGE_LINE (w)
17561 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17562 + window_internal_height (w));
17564 #if defined (HAVE_GPM) || defined (MSDOS)
17565 x_clear_window_mouse_face (w);
17566 #endif
17567 /* Perform the operation on the screen. */
17568 if (dvpos > 0)
17570 /* Scroll last_unchanged_at_beg_row to the end of the
17571 window down dvpos lines. */
17572 set_terminal_window (f, end);
17574 /* On dumb terminals delete dvpos lines at the end
17575 before inserting dvpos empty lines. */
17576 if (!FRAME_SCROLL_REGION_OK (f))
17577 ins_del_lines (f, end - dvpos, -dvpos);
17579 /* Insert dvpos empty lines in front of
17580 last_unchanged_at_beg_row. */
17581 ins_del_lines (f, from, dvpos);
17583 else if (dvpos < 0)
17585 /* Scroll up last_unchanged_at_beg_vpos to the end of
17586 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17587 set_terminal_window (f, end);
17589 /* Delete dvpos lines in front of
17590 last_unchanged_at_beg_vpos. ins_del_lines will set
17591 the cursor to the given vpos and emit |dvpos| delete
17592 line sequences. */
17593 ins_del_lines (f, from + dvpos, dvpos);
17595 /* On a dumb terminal insert dvpos empty lines at the
17596 end. */
17597 if (!FRAME_SCROLL_REGION_OK (f))
17598 ins_del_lines (f, end + dvpos, -dvpos);
17601 set_terminal_window (f, 0);
17604 update_end (f);
17607 /* Shift reused rows of the current matrix to the right position.
17608 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17609 text. */
17610 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17611 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17612 if (dvpos < 0)
17614 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17615 bottom_vpos, dvpos);
17616 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17617 bottom_vpos);
17619 else if (dvpos > 0)
17621 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17622 bottom_vpos, dvpos);
17623 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17624 first_unchanged_at_end_vpos + dvpos);
17627 /* For frame-based redisplay, make sure that current frame and window
17628 matrix are in sync with respect to glyph memory. */
17629 if (!FRAME_WINDOW_P (f))
17630 sync_frame_with_window_matrix_rows (w);
17632 /* Adjust buffer positions in reused rows. */
17633 if (delta || delta_bytes)
17634 increment_matrix_positions (current_matrix,
17635 first_unchanged_at_end_vpos + dvpos,
17636 bottom_vpos, delta, delta_bytes);
17638 /* Adjust Y positions. */
17639 if (dy)
17640 shift_glyph_matrix (w, current_matrix,
17641 first_unchanged_at_end_vpos + dvpos,
17642 bottom_vpos, dy);
17644 if (first_unchanged_at_end_row)
17646 first_unchanged_at_end_row += dvpos;
17647 if (first_unchanged_at_end_row->y >= it.last_visible_y
17648 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17649 first_unchanged_at_end_row = NULL;
17652 /* If scrolling up, there may be some lines to display at the end of
17653 the window. */
17654 last_text_row_at_end = NULL;
17655 if (dy < 0)
17657 /* Scrolling up can leave for example a partially visible line
17658 at the end of the window to be redisplayed. */
17659 /* Set last_row to the glyph row in the current matrix where the
17660 window end line is found. It has been moved up or down in
17661 the matrix by dvpos. */
17662 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17663 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17665 /* If last_row is the window end line, it should display text. */
17666 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17668 /* If window end line was partially visible before, begin
17669 displaying at that line. Otherwise begin displaying with the
17670 line following it. */
17671 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17673 init_to_row_start (&it, w, last_row);
17674 it.vpos = last_vpos;
17675 it.current_y = last_row->y;
17677 else
17679 init_to_row_end (&it, w, last_row);
17680 it.vpos = 1 + last_vpos;
17681 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17682 ++last_row;
17685 /* We may start in a continuation line. If so, we have to
17686 get the right continuation_lines_width and current_x. */
17687 it.continuation_lines_width = last_row->continuation_lines_width;
17688 it.hpos = it.current_x = 0;
17690 /* Display the rest of the lines at the window end. */
17691 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17692 while (it.current_y < it.last_visible_y
17693 && !fonts_changed_p)
17695 /* Is it always sure that the display agrees with lines in
17696 the current matrix? I don't think so, so we mark rows
17697 displayed invalid in the current matrix by setting their
17698 enabled_p flag to zero. */
17699 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17700 if (display_line (&it))
17701 last_text_row_at_end = it.glyph_row - 1;
17705 /* Update window_end_pos and window_end_vpos. */
17706 if (first_unchanged_at_end_row
17707 && !last_text_row_at_end)
17709 /* Window end line if one of the preserved rows from the current
17710 matrix. Set row to the last row displaying text in current
17711 matrix starting at first_unchanged_at_end_row, after
17712 scrolling. */
17713 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17714 row = find_last_row_displaying_text (w->current_matrix, &it,
17715 first_unchanged_at_end_row);
17716 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17718 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17719 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17720 wset_window_end_vpos
17721 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17722 eassert (w->window_end_bytepos >= 0);
17723 IF_DEBUG (debug_method_add (w, "A"));
17725 else if (last_text_row_at_end)
17727 wset_window_end_pos
17728 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17729 w->window_end_bytepos
17730 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17731 wset_window_end_vpos
17732 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17733 desired_matrix)));
17734 eassert (w->window_end_bytepos >= 0);
17735 IF_DEBUG (debug_method_add (w, "B"));
17737 else if (last_text_row)
17739 /* We have displayed either to the end of the window or at the
17740 end of the window, i.e. the last row with text is to be found
17741 in the desired matrix. */
17742 wset_window_end_pos
17743 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17744 w->window_end_bytepos
17745 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17746 wset_window_end_vpos
17747 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17748 eassert (w->window_end_bytepos >= 0);
17750 else if (first_unchanged_at_end_row == NULL
17751 && last_text_row == NULL
17752 && last_text_row_at_end == NULL)
17754 /* Displayed to end of window, but no line containing text was
17755 displayed. Lines were deleted at the end of the window. */
17756 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17757 int vpos = XFASTINT (w->window_end_vpos);
17758 struct glyph_row *current_row = current_matrix->rows + vpos;
17759 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17761 for (row = NULL;
17762 row == NULL && vpos >= first_vpos;
17763 --vpos, --current_row, --desired_row)
17765 if (desired_row->enabled_p)
17767 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17768 row = desired_row;
17770 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17771 row = current_row;
17774 eassert (row != NULL);
17775 wset_window_end_vpos (w, make_number (vpos + 1));
17776 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17777 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17778 eassert (w->window_end_bytepos >= 0);
17779 IF_DEBUG (debug_method_add (w, "C"));
17781 else
17782 emacs_abort ();
17784 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17785 debug_end_vpos = XFASTINT (w->window_end_vpos));
17787 /* Record that display has not been completed. */
17788 w->window_end_valid = 0;
17789 w->desired_matrix->no_scrolling_p = 1;
17790 return 3;
17792 #undef GIVE_UP
17797 /***********************************************************************
17798 More debugging support
17799 ***********************************************************************/
17801 #ifdef GLYPH_DEBUG
17803 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17804 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17805 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17808 /* Dump the contents of glyph matrix MATRIX on stderr.
17810 GLYPHS 0 means don't show glyph contents.
17811 GLYPHS 1 means show glyphs in short form
17812 GLYPHS > 1 means show glyphs in long form. */
17814 void
17815 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17817 int i;
17818 for (i = 0; i < matrix->nrows; ++i)
17819 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17823 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17824 the glyph row and area where the glyph comes from. */
17826 void
17827 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17829 if (glyph->type == CHAR_GLYPH
17830 || glyph->type == GLYPHLESS_GLYPH)
17832 fprintf (stderr,
17833 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17834 glyph - row->glyphs[TEXT_AREA],
17835 (glyph->type == CHAR_GLYPH
17836 ? 'C'
17837 : 'G'),
17838 glyph->charpos,
17839 (BUFFERP (glyph->object)
17840 ? 'B'
17841 : (STRINGP (glyph->object)
17842 ? 'S'
17843 : (INTEGERP (glyph->object)
17844 ? '0'
17845 : '-'))),
17846 glyph->pixel_width,
17847 glyph->u.ch,
17848 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17849 ? glyph->u.ch
17850 : '.'),
17851 glyph->face_id,
17852 glyph->left_box_line_p,
17853 glyph->right_box_line_p);
17855 else if (glyph->type == STRETCH_GLYPH)
17857 fprintf (stderr,
17858 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17859 glyph - row->glyphs[TEXT_AREA],
17860 'S',
17861 glyph->charpos,
17862 (BUFFERP (glyph->object)
17863 ? 'B'
17864 : (STRINGP (glyph->object)
17865 ? 'S'
17866 : (INTEGERP (glyph->object)
17867 ? '0'
17868 : '-'))),
17869 glyph->pixel_width,
17871 ' ',
17872 glyph->face_id,
17873 glyph->left_box_line_p,
17874 glyph->right_box_line_p);
17876 else if (glyph->type == IMAGE_GLYPH)
17878 fprintf (stderr,
17879 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17880 glyph - row->glyphs[TEXT_AREA],
17881 'I',
17882 glyph->charpos,
17883 (BUFFERP (glyph->object)
17884 ? 'B'
17885 : (STRINGP (glyph->object)
17886 ? 'S'
17887 : (INTEGERP (glyph->object)
17888 ? '0'
17889 : '-'))),
17890 glyph->pixel_width,
17891 glyph->u.img_id,
17892 '.',
17893 glyph->face_id,
17894 glyph->left_box_line_p,
17895 glyph->right_box_line_p);
17897 else if (glyph->type == COMPOSITE_GLYPH)
17899 fprintf (stderr,
17900 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17901 glyph - row->glyphs[TEXT_AREA],
17902 '+',
17903 glyph->charpos,
17904 (BUFFERP (glyph->object)
17905 ? 'B'
17906 : (STRINGP (glyph->object)
17907 ? 'S'
17908 : (INTEGERP (glyph->object)
17909 ? '0'
17910 : '-'))),
17911 glyph->pixel_width,
17912 glyph->u.cmp.id);
17913 if (glyph->u.cmp.automatic)
17914 fprintf (stderr,
17915 "[%d-%d]",
17916 glyph->slice.cmp.from, glyph->slice.cmp.to);
17917 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17918 glyph->face_id,
17919 glyph->left_box_line_p,
17920 glyph->right_box_line_p);
17925 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17926 GLYPHS 0 means don't show glyph contents.
17927 GLYPHS 1 means show glyphs in short form
17928 GLYPHS > 1 means show glyphs in long form. */
17930 void
17931 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17933 if (glyphs != 1)
17935 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17936 fprintf (stderr, "==============================================================================\n");
17938 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17939 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17940 vpos,
17941 MATRIX_ROW_START_CHARPOS (row),
17942 MATRIX_ROW_END_CHARPOS (row),
17943 row->used[TEXT_AREA],
17944 row->contains_overlapping_glyphs_p,
17945 row->enabled_p,
17946 row->truncated_on_left_p,
17947 row->truncated_on_right_p,
17948 row->continued_p,
17949 MATRIX_ROW_CONTINUATION_LINE_P (row),
17950 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17951 row->ends_at_zv_p,
17952 row->fill_line_p,
17953 row->ends_in_middle_of_char_p,
17954 row->starts_in_middle_of_char_p,
17955 row->mouse_face_p,
17956 row->x,
17957 row->y,
17958 row->pixel_width,
17959 row->height,
17960 row->visible_height,
17961 row->ascent,
17962 row->phys_ascent);
17963 /* The next 3 lines should align to "Start" in the header. */
17964 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17965 row->end.overlay_string_index,
17966 row->continuation_lines_width);
17967 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17968 CHARPOS (row->start.string_pos),
17969 CHARPOS (row->end.string_pos));
17970 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17971 row->end.dpvec_index);
17974 if (glyphs > 1)
17976 int area;
17978 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17980 struct glyph *glyph = row->glyphs[area];
17981 struct glyph *glyph_end = glyph + row->used[area];
17983 /* Glyph for a line end in text. */
17984 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17985 ++glyph_end;
17987 if (glyph < glyph_end)
17988 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
17990 for (; glyph < glyph_end; ++glyph)
17991 dump_glyph (row, glyph, area);
17994 else if (glyphs == 1)
17996 int area;
17998 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18000 char *s = alloca (row->used[area] + 4);
18001 int i;
18003 for (i = 0; i < row->used[area]; ++i)
18005 struct glyph *glyph = row->glyphs[area] + i;
18006 if (i == row->used[area] - 1
18007 && area == TEXT_AREA
18008 && INTEGERP (glyph->object)
18009 && glyph->type == CHAR_GLYPH
18010 && glyph->u.ch == ' ')
18012 strcpy (&s[i], "[\\n]");
18013 i += 4;
18015 else if (glyph->type == CHAR_GLYPH
18016 && glyph->u.ch < 0x80
18017 && glyph->u.ch >= ' ')
18018 s[i] = glyph->u.ch;
18019 else
18020 s[i] = '.';
18023 s[i] = '\0';
18024 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18030 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18031 Sdump_glyph_matrix, 0, 1, "p",
18032 doc: /* Dump the current matrix of the selected window to stderr.
18033 Shows contents of glyph row structures. With non-nil
18034 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18035 glyphs in short form, otherwise show glyphs in long form. */)
18036 (Lisp_Object glyphs)
18038 struct window *w = XWINDOW (selected_window);
18039 struct buffer *buffer = XBUFFER (w->contents);
18041 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18042 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18043 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18044 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18045 fprintf (stderr, "=============================================\n");
18046 dump_glyph_matrix (w->current_matrix,
18047 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18048 return Qnil;
18052 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18053 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18054 (void)
18056 struct frame *f = XFRAME (selected_frame);
18057 dump_glyph_matrix (f->current_matrix, 1);
18058 return Qnil;
18062 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18063 doc: /* Dump glyph row ROW to stderr.
18064 GLYPH 0 means don't dump glyphs.
18065 GLYPH 1 means dump glyphs in short form.
18066 GLYPH > 1 or omitted means dump glyphs in long form. */)
18067 (Lisp_Object row, Lisp_Object glyphs)
18069 struct glyph_matrix *matrix;
18070 EMACS_INT vpos;
18072 CHECK_NUMBER (row);
18073 matrix = XWINDOW (selected_window)->current_matrix;
18074 vpos = XINT (row);
18075 if (vpos >= 0 && vpos < matrix->nrows)
18076 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18077 vpos,
18078 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18079 return Qnil;
18083 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18084 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18085 GLYPH 0 means don't dump glyphs.
18086 GLYPH 1 means dump glyphs in short form.
18087 GLYPH > 1 or omitted means dump glyphs in long form. */)
18088 (Lisp_Object row, Lisp_Object glyphs)
18090 struct frame *sf = SELECTED_FRAME ();
18091 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18092 EMACS_INT vpos;
18094 CHECK_NUMBER (row);
18095 vpos = XINT (row);
18096 if (vpos >= 0 && vpos < m->nrows)
18097 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18098 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18099 return Qnil;
18103 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18104 doc: /* Toggle tracing of redisplay.
18105 With ARG, turn tracing on if and only if ARG is positive. */)
18106 (Lisp_Object arg)
18108 if (NILP (arg))
18109 trace_redisplay_p = !trace_redisplay_p;
18110 else
18112 arg = Fprefix_numeric_value (arg);
18113 trace_redisplay_p = XINT (arg) > 0;
18116 return Qnil;
18120 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18121 doc: /* Like `format', but print result to stderr.
18122 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18123 (ptrdiff_t nargs, Lisp_Object *args)
18125 Lisp_Object s = Fformat (nargs, args);
18126 fprintf (stderr, "%s", SDATA (s));
18127 return Qnil;
18130 #endif /* GLYPH_DEBUG */
18134 /***********************************************************************
18135 Building Desired Matrix Rows
18136 ***********************************************************************/
18138 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18139 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18141 static struct glyph_row *
18142 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18144 struct frame *f = XFRAME (WINDOW_FRAME (w));
18145 struct buffer *buffer = XBUFFER (w->contents);
18146 struct buffer *old = current_buffer;
18147 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18148 int arrow_len = SCHARS (overlay_arrow_string);
18149 const unsigned char *arrow_end = arrow_string + arrow_len;
18150 const unsigned char *p;
18151 struct it it;
18152 bool multibyte_p;
18153 int n_glyphs_before;
18155 set_buffer_temp (buffer);
18156 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18157 it.glyph_row->used[TEXT_AREA] = 0;
18158 SET_TEXT_POS (it.position, 0, 0);
18160 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18161 p = arrow_string;
18162 while (p < arrow_end)
18164 Lisp_Object face, ilisp;
18166 /* Get the next character. */
18167 if (multibyte_p)
18168 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18169 else
18171 it.c = it.char_to_display = *p, it.len = 1;
18172 if (! ASCII_CHAR_P (it.c))
18173 it.char_to_display = BYTE8_TO_CHAR (it.c);
18175 p += it.len;
18177 /* Get its face. */
18178 ilisp = make_number (p - arrow_string);
18179 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18180 it.face_id = compute_char_face (f, it.char_to_display, face);
18182 /* Compute its width, get its glyphs. */
18183 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18184 SET_TEXT_POS (it.position, -1, -1);
18185 PRODUCE_GLYPHS (&it);
18187 /* If this character doesn't fit any more in the line, we have
18188 to remove some glyphs. */
18189 if (it.current_x > it.last_visible_x)
18191 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18192 break;
18196 set_buffer_temp (old);
18197 return it.glyph_row;
18201 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18202 glyphs to insert is determined by produce_special_glyphs. */
18204 static void
18205 insert_left_trunc_glyphs (struct it *it)
18207 struct it truncate_it;
18208 struct glyph *from, *end, *to, *toend;
18210 eassert (!FRAME_WINDOW_P (it->f)
18211 || (!it->glyph_row->reversed_p
18212 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18213 || (it->glyph_row->reversed_p
18214 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18216 /* Get the truncation glyphs. */
18217 truncate_it = *it;
18218 truncate_it.current_x = 0;
18219 truncate_it.face_id = DEFAULT_FACE_ID;
18220 truncate_it.glyph_row = &scratch_glyph_row;
18221 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18222 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18223 truncate_it.object = make_number (0);
18224 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18226 /* Overwrite glyphs from IT with truncation glyphs. */
18227 if (!it->glyph_row->reversed_p)
18229 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18231 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18232 end = from + tused;
18233 to = it->glyph_row->glyphs[TEXT_AREA];
18234 toend = to + it->glyph_row->used[TEXT_AREA];
18235 if (FRAME_WINDOW_P (it->f))
18237 /* On GUI frames, when variable-size fonts are displayed,
18238 the truncation glyphs may need more pixels than the row's
18239 glyphs they overwrite. We overwrite more glyphs to free
18240 enough screen real estate, and enlarge the stretch glyph
18241 on the right (see display_line), if there is one, to
18242 preserve the screen position of the truncation glyphs on
18243 the right. */
18244 int w = 0;
18245 struct glyph *g = to;
18246 short used;
18248 /* The first glyph could be partially visible, in which case
18249 it->glyph_row->x will be negative. But we want the left
18250 truncation glyphs to be aligned at the left margin of the
18251 window, so we override the x coordinate at which the row
18252 will begin. */
18253 it->glyph_row->x = 0;
18254 while (g < toend && w < it->truncation_pixel_width)
18256 w += g->pixel_width;
18257 ++g;
18259 if (g - to - tused > 0)
18261 memmove (to + tused, g, (toend - g) * sizeof(*g));
18262 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18264 used = it->glyph_row->used[TEXT_AREA];
18265 if (it->glyph_row->truncated_on_right_p
18266 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18267 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18268 == STRETCH_GLYPH)
18270 int extra = w - it->truncation_pixel_width;
18272 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18276 while (from < end)
18277 *to++ = *from++;
18279 /* There may be padding glyphs left over. Overwrite them too. */
18280 if (!FRAME_WINDOW_P (it->f))
18282 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18284 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18285 while (from < end)
18286 *to++ = *from++;
18290 if (to > toend)
18291 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18293 else
18295 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18297 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18298 that back to front. */
18299 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18300 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18301 toend = it->glyph_row->glyphs[TEXT_AREA];
18302 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18303 if (FRAME_WINDOW_P (it->f))
18305 int w = 0;
18306 struct glyph *g = to;
18308 while (g >= toend && w < it->truncation_pixel_width)
18310 w += g->pixel_width;
18311 --g;
18313 if (to - g - tused > 0)
18314 to = g + tused;
18315 if (it->glyph_row->truncated_on_right_p
18316 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18317 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18319 int extra = w - it->truncation_pixel_width;
18321 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18325 while (from >= end && to >= toend)
18326 *to-- = *from--;
18327 if (!FRAME_WINDOW_P (it->f))
18329 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18331 from =
18332 truncate_it.glyph_row->glyphs[TEXT_AREA]
18333 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18334 while (from >= end && to >= toend)
18335 *to-- = *from--;
18338 if (from >= end)
18340 /* Need to free some room before prepending additional
18341 glyphs. */
18342 int move_by = from - end + 1;
18343 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18344 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18346 for ( ; g >= g0; g--)
18347 g[move_by] = *g;
18348 while (from >= end)
18349 *to-- = *from--;
18350 it->glyph_row->used[TEXT_AREA] += move_by;
18355 /* Compute the hash code for ROW. */
18356 unsigned
18357 row_hash (struct glyph_row *row)
18359 int area, k;
18360 unsigned hashval = 0;
18362 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18363 for (k = 0; k < row->used[area]; ++k)
18364 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18365 + row->glyphs[area][k].u.val
18366 + row->glyphs[area][k].face_id
18367 + row->glyphs[area][k].padding_p
18368 + (row->glyphs[area][k].type << 2));
18370 return hashval;
18373 /* Compute the pixel height and width of IT->glyph_row.
18375 Most of the time, ascent and height of a display line will be equal
18376 to the max_ascent and max_height values of the display iterator
18377 structure. This is not the case if
18379 1. We hit ZV without displaying anything. In this case, max_ascent
18380 and max_height will be zero.
18382 2. We have some glyphs that don't contribute to the line height.
18383 (The glyph row flag contributes_to_line_height_p is for future
18384 pixmap extensions).
18386 The first case is easily covered by using default values because in
18387 these cases, the line height does not really matter, except that it
18388 must not be zero. */
18390 static void
18391 compute_line_metrics (struct it *it)
18393 struct glyph_row *row = it->glyph_row;
18395 if (FRAME_WINDOW_P (it->f))
18397 int i, min_y, max_y;
18399 /* The line may consist of one space only, that was added to
18400 place the cursor on it. If so, the row's height hasn't been
18401 computed yet. */
18402 if (row->height == 0)
18404 if (it->max_ascent + it->max_descent == 0)
18405 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18406 row->ascent = it->max_ascent;
18407 row->height = it->max_ascent + it->max_descent;
18408 row->phys_ascent = it->max_phys_ascent;
18409 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18410 row->extra_line_spacing = it->max_extra_line_spacing;
18413 /* Compute the width of this line. */
18414 row->pixel_width = row->x;
18415 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18416 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18418 eassert (row->pixel_width >= 0);
18419 eassert (row->ascent >= 0 && row->height > 0);
18421 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18422 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18424 /* If first line's physical ascent is larger than its logical
18425 ascent, use the physical ascent, and make the row taller.
18426 This makes accented characters fully visible. */
18427 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18428 && row->phys_ascent > row->ascent)
18430 row->height += row->phys_ascent - row->ascent;
18431 row->ascent = row->phys_ascent;
18434 /* Compute how much of the line is visible. */
18435 row->visible_height = row->height;
18437 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18438 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18440 if (row->y < min_y)
18441 row->visible_height -= min_y - row->y;
18442 if (row->y + row->height > max_y)
18443 row->visible_height -= row->y + row->height - max_y;
18445 else
18447 row->pixel_width = row->used[TEXT_AREA];
18448 if (row->continued_p)
18449 row->pixel_width -= it->continuation_pixel_width;
18450 else if (row->truncated_on_right_p)
18451 row->pixel_width -= it->truncation_pixel_width;
18452 row->ascent = row->phys_ascent = 0;
18453 row->height = row->phys_height = row->visible_height = 1;
18454 row->extra_line_spacing = 0;
18457 /* Compute a hash code for this row. */
18458 row->hash = row_hash (row);
18460 it->max_ascent = it->max_descent = 0;
18461 it->max_phys_ascent = it->max_phys_descent = 0;
18465 /* Append one space to the glyph row of iterator IT if doing a
18466 window-based redisplay. The space has the same face as
18467 IT->face_id. Value is non-zero if a space was added.
18469 This function is called to make sure that there is always one glyph
18470 at the end of a glyph row that the cursor can be set on under
18471 window-systems. (If there weren't such a glyph we would not know
18472 how wide and tall a box cursor should be displayed).
18474 At the same time this space let's a nicely handle clearing to the
18475 end of the line if the row ends in italic text. */
18477 static int
18478 append_space_for_newline (struct it *it, int default_face_p)
18480 if (FRAME_WINDOW_P (it->f))
18482 int n = it->glyph_row->used[TEXT_AREA];
18484 if (it->glyph_row->glyphs[TEXT_AREA] + n
18485 < it->glyph_row->glyphs[1 + TEXT_AREA])
18487 /* Save some values that must not be changed.
18488 Must save IT->c and IT->len because otherwise
18489 ITERATOR_AT_END_P wouldn't work anymore after
18490 append_space_for_newline has been called. */
18491 enum display_element_type saved_what = it->what;
18492 int saved_c = it->c, saved_len = it->len;
18493 int saved_char_to_display = it->char_to_display;
18494 int saved_x = it->current_x;
18495 int saved_face_id = it->face_id;
18496 int saved_box_end = it->end_of_box_run_p;
18497 struct text_pos saved_pos;
18498 Lisp_Object saved_object;
18499 struct face *face;
18501 saved_object = it->object;
18502 saved_pos = it->position;
18504 it->what = IT_CHARACTER;
18505 memset (&it->position, 0, sizeof it->position);
18506 it->object = make_number (0);
18507 it->c = it->char_to_display = ' ';
18508 it->len = 1;
18510 /* If the default face was remapped, be sure to use the
18511 remapped face for the appended newline. */
18512 if (default_face_p)
18513 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18514 else if (it->face_before_selective_p)
18515 it->face_id = it->saved_face_id;
18516 face = FACE_FROM_ID (it->f, it->face_id);
18517 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18518 /* In R2L rows, we will prepend a stretch glyph that will
18519 have the end_of_box_run_p flag set for it, so there's no
18520 need for the appended newline glyph to have that flag
18521 set. */
18522 if (it->glyph_row->reversed_p
18523 /* But if the appended newline glyph goes all the way to
18524 the end of the row, there will be no stretch glyph,
18525 so leave the box flag set. */
18526 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18527 it->end_of_box_run_p = 0;
18529 PRODUCE_GLYPHS (it);
18531 it->override_ascent = -1;
18532 it->constrain_row_ascent_descent_p = 0;
18533 it->current_x = saved_x;
18534 it->object = saved_object;
18535 it->position = saved_pos;
18536 it->what = saved_what;
18537 it->face_id = saved_face_id;
18538 it->len = saved_len;
18539 it->c = saved_c;
18540 it->char_to_display = saved_char_to_display;
18541 it->end_of_box_run_p = saved_box_end;
18542 return 1;
18546 return 0;
18550 /* Extend the face of the last glyph in the text area of IT->glyph_row
18551 to the end of the display line. Called from display_line. If the
18552 glyph row is empty, add a space glyph to it so that we know the
18553 face to draw. Set the glyph row flag fill_line_p. If the glyph
18554 row is R2L, prepend a stretch glyph to cover the empty space to the
18555 left of the leftmost glyph. */
18557 static void
18558 extend_face_to_end_of_line (struct it *it)
18560 struct face *face, *default_face;
18561 struct frame *f = it->f;
18563 /* If line is already filled, do nothing. Non window-system frames
18564 get a grace of one more ``pixel'' because their characters are
18565 1-``pixel'' wide, so they hit the equality too early. This grace
18566 is needed only for R2L rows that are not continued, to produce
18567 one extra blank where we could display the cursor. */
18568 if (it->current_x >= it->last_visible_x
18569 + (!FRAME_WINDOW_P (f)
18570 && it->glyph_row->reversed_p
18571 && !it->glyph_row->continued_p))
18572 return;
18574 /* The default face, possibly remapped. */
18575 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18577 /* Face extension extends the background and box of IT->face_id
18578 to the end of the line. If the background equals the background
18579 of the frame, we don't have to do anything. */
18580 if (it->face_before_selective_p)
18581 face = FACE_FROM_ID (f, it->saved_face_id);
18582 else
18583 face = FACE_FROM_ID (f, it->face_id);
18585 if (FRAME_WINDOW_P (f)
18586 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18587 && face->box == FACE_NO_BOX
18588 && face->background == FRAME_BACKGROUND_PIXEL (f)
18589 && !face->stipple
18590 && !it->glyph_row->reversed_p)
18591 return;
18593 /* Set the glyph row flag indicating that the face of the last glyph
18594 in the text area has to be drawn to the end of the text area. */
18595 it->glyph_row->fill_line_p = 1;
18597 /* If current character of IT is not ASCII, make sure we have the
18598 ASCII face. This will be automatically undone the next time
18599 get_next_display_element returns a multibyte character. Note
18600 that the character will always be single byte in unibyte
18601 text. */
18602 if (!ASCII_CHAR_P (it->c))
18604 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18607 if (FRAME_WINDOW_P (f))
18609 /* If the row is empty, add a space with the current face of IT,
18610 so that we know which face to draw. */
18611 if (it->glyph_row->used[TEXT_AREA] == 0)
18613 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18614 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18615 it->glyph_row->used[TEXT_AREA] = 1;
18617 #ifdef HAVE_WINDOW_SYSTEM
18618 if (it->glyph_row->reversed_p)
18620 /* Prepend a stretch glyph to the row, such that the
18621 rightmost glyph will be drawn flushed all the way to the
18622 right margin of the window. The stretch glyph that will
18623 occupy the empty space, if any, to the left of the
18624 glyphs. */
18625 struct font *font = face->font ? face->font : FRAME_FONT (f);
18626 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18627 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18628 struct glyph *g;
18629 int row_width, stretch_ascent, stretch_width;
18630 struct text_pos saved_pos;
18631 int saved_face_id, saved_avoid_cursor, saved_box_start;
18633 for (row_width = 0, g = row_start; g < row_end; g++)
18634 row_width += g->pixel_width;
18635 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18636 if (stretch_width > 0)
18638 stretch_ascent =
18639 (((it->ascent + it->descent)
18640 * FONT_BASE (font)) / FONT_HEIGHT (font));
18641 saved_pos = it->position;
18642 memset (&it->position, 0, sizeof it->position);
18643 saved_avoid_cursor = it->avoid_cursor_p;
18644 it->avoid_cursor_p = 1;
18645 saved_face_id = it->face_id;
18646 saved_box_start = it->start_of_box_run_p;
18647 /* The last row's stretch glyph should get the default
18648 face, to avoid painting the rest of the window with
18649 the region face, if the region ends at ZV. */
18650 if (it->glyph_row->ends_at_zv_p)
18651 it->face_id = default_face->id;
18652 else
18653 it->face_id = face->id;
18654 it->start_of_box_run_p = 0;
18655 append_stretch_glyph (it, make_number (0), stretch_width,
18656 it->ascent + it->descent, stretch_ascent);
18657 it->position = saved_pos;
18658 it->avoid_cursor_p = saved_avoid_cursor;
18659 it->face_id = saved_face_id;
18660 it->start_of_box_run_p = saved_box_start;
18663 #endif /* HAVE_WINDOW_SYSTEM */
18665 else
18667 /* Save some values that must not be changed. */
18668 int saved_x = it->current_x;
18669 struct text_pos saved_pos;
18670 Lisp_Object saved_object;
18671 enum display_element_type saved_what = it->what;
18672 int saved_face_id = it->face_id;
18674 saved_object = it->object;
18675 saved_pos = it->position;
18677 it->what = IT_CHARACTER;
18678 memset (&it->position, 0, sizeof it->position);
18679 it->object = make_number (0);
18680 it->c = it->char_to_display = ' ';
18681 it->len = 1;
18682 /* The last row's blank glyphs should get the default face, to
18683 avoid painting the rest of the window with the region face,
18684 if the region ends at ZV. */
18685 if (it->glyph_row->ends_at_zv_p)
18686 it->face_id = default_face->id;
18687 else
18688 it->face_id = face->id;
18690 PRODUCE_GLYPHS (it);
18692 while (it->current_x <= it->last_visible_x)
18693 PRODUCE_GLYPHS (it);
18695 /* Don't count these blanks really. It would let us insert a left
18696 truncation glyph below and make us set the cursor on them, maybe. */
18697 it->current_x = saved_x;
18698 it->object = saved_object;
18699 it->position = saved_pos;
18700 it->what = saved_what;
18701 it->face_id = saved_face_id;
18706 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18707 trailing whitespace. */
18709 static int
18710 trailing_whitespace_p (ptrdiff_t charpos)
18712 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18713 int c = 0;
18715 while (bytepos < ZV_BYTE
18716 && (c = FETCH_CHAR (bytepos),
18717 c == ' ' || c == '\t'))
18718 ++bytepos;
18720 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18722 if (bytepos != PT_BYTE)
18723 return 1;
18725 return 0;
18729 /* Highlight trailing whitespace, if any, in ROW. */
18731 static void
18732 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18734 int used = row->used[TEXT_AREA];
18736 if (used)
18738 struct glyph *start = row->glyphs[TEXT_AREA];
18739 struct glyph *glyph = start + used - 1;
18741 if (row->reversed_p)
18743 /* Right-to-left rows need to be processed in the opposite
18744 direction, so swap the edge pointers. */
18745 glyph = start;
18746 start = row->glyphs[TEXT_AREA] + used - 1;
18749 /* Skip over glyphs inserted to display the cursor at the
18750 end of a line, for extending the face of the last glyph
18751 to the end of the line on terminals, and for truncation
18752 and continuation glyphs. */
18753 if (!row->reversed_p)
18755 while (glyph >= start
18756 && glyph->type == CHAR_GLYPH
18757 && INTEGERP (glyph->object))
18758 --glyph;
18760 else
18762 while (glyph <= start
18763 && glyph->type == CHAR_GLYPH
18764 && INTEGERP (glyph->object))
18765 ++glyph;
18768 /* If last glyph is a space or stretch, and it's trailing
18769 whitespace, set the face of all trailing whitespace glyphs in
18770 IT->glyph_row to `trailing-whitespace'. */
18771 if ((row->reversed_p ? glyph <= start : glyph >= start)
18772 && BUFFERP (glyph->object)
18773 && (glyph->type == STRETCH_GLYPH
18774 || (glyph->type == CHAR_GLYPH
18775 && glyph->u.ch == ' '))
18776 && trailing_whitespace_p (glyph->charpos))
18778 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18779 if (face_id < 0)
18780 return;
18782 if (!row->reversed_p)
18784 while (glyph >= start
18785 && BUFFERP (glyph->object)
18786 && (glyph->type == STRETCH_GLYPH
18787 || (glyph->type == CHAR_GLYPH
18788 && glyph->u.ch == ' ')))
18789 (glyph--)->face_id = face_id;
18791 else
18793 while (glyph <= start
18794 && BUFFERP (glyph->object)
18795 && (glyph->type == STRETCH_GLYPH
18796 || (glyph->type == CHAR_GLYPH
18797 && glyph->u.ch == ' ')))
18798 (glyph++)->face_id = face_id;
18805 /* Value is non-zero if glyph row ROW should be
18806 considered to hold the buffer position CHARPOS. */
18808 static int
18809 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18811 int result = 1;
18813 if (charpos == CHARPOS (row->end.pos)
18814 || charpos == MATRIX_ROW_END_CHARPOS (row))
18816 /* Suppose the row ends on a string.
18817 Unless the row is continued, that means it ends on a newline
18818 in the string. If it's anything other than a display string
18819 (e.g., a before-string from an overlay), we don't want the
18820 cursor there. (This heuristic seems to give the optimal
18821 behavior for the various types of multi-line strings.)
18822 One exception: if the string has `cursor' property on one of
18823 its characters, we _do_ want the cursor there. */
18824 if (CHARPOS (row->end.string_pos) >= 0)
18826 if (row->continued_p)
18827 result = 1;
18828 else
18830 /* Check for `display' property. */
18831 struct glyph *beg = row->glyphs[TEXT_AREA];
18832 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18833 struct glyph *glyph;
18835 result = 0;
18836 for (glyph = end; glyph >= beg; --glyph)
18837 if (STRINGP (glyph->object))
18839 Lisp_Object prop
18840 = Fget_char_property (make_number (charpos),
18841 Qdisplay, Qnil);
18842 result =
18843 (!NILP (prop)
18844 && display_prop_string_p (prop, glyph->object));
18845 /* If there's a `cursor' property on one of the
18846 string's characters, this row is a cursor row,
18847 even though this is not a display string. */
18848 if (!result)
18850 Lisp_Object s = glyph->object;
18852 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18854 ptrdiff_t gpos = glyph->charpos;
18856 if (!NILP (Fget_char_property (make_number (gpos),
18857 Qcursor, s)))
18859 result = 1;
18860 break;
18864 break;
18868 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18870 /* If the row ends in middle of a real character,
18871 and the line is continued, we want the cursor here.
18872 That's because CHARPOS (ROW->end.pos) would equal
18873 PT if PT is before the character. */
18874 if (!row->ends_in_ellipsis_p)
18875 result = row->continued_p;
18876 else
18877 /* If the row ends in an ellipsis, then
18878 CHARPOS (ROW->end.pos) will equal point after the
18879 invisible text. We want that position to be displayed
18880 after the ellipsis. */
18881 result = 0;
18883 /* If the row ends at ZV, display the cursor at the end of that
18884 row instead of at the start of the row below. */
18885 else if (row->ends_at_zv_p)
18886 result = 1;
18887 else
18888 result = 0;
18891 return result;
18894 /* Value is non-zero if glyph row ROW should be
18895 used to hold the cursor. */
18897 static int
18898 cursor_row_p (struct glyph_row *row)
18900 return row_for_charpos_p (row, PT);
18905 /* Push the property PROP so that it will be rendered at the current
18906 position in IT. Return 1 if PROP was successfully pushed, 0
18907 otherwise. Called from handle_line_prefix to handle the
18908 `line-prefix' and `wrap-prefix' properties. */
18910 static int
18911 push_prefix_prop (struct it *it, Lisp_Object prop)
18913 struct text_pos pos =
18914 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18916 eassert (it->method == GET_FROM_BUFFER
18917 || it->method == GET_FROM_DISPLAY_VECTOR
18918 || it->method == GET_FROM_STRING);
18920 /* We need to save the current buffer/string position, so it will be
18921 restored by pop_it, because iterate_out_of_display_property
18922 depends on that being set correctly, but some situations leave
18923 it->position not yet set when this function is called. */
18924 push_it (it, &pos);
18926 if (STRINGP (prop))
18928 if (SCHARS (prop) == 0)
18930 pop_it (it);
18931 return 0;
18934 it->string = prop;
18935 it->string_from_prefix_prop_p = 1;
18936 it->multibyte_p = STRING_MULTIBYTE (it->string);
18937 it->current.overlay_string_index = -1;
18938 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18939 it->end_charpos = it->string_nchars = SCHARS (it->string);
18940 it->method = GET_FROM_STRING;
18941 it->stop_charpos = 0;
18942 it->prev_stop = 0;
18943 it->base_level_stop = 0;
18945 /* Force paragraph direction to be that of the parent
18946 buffer/string. */
18947 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18948 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18949 else
18950 it->paragraph_embedding = L2R;
18952 /* Set up the bidi iterator for this display string. */
18953 if (it->bidi_p)
18955 it->bidi_it.string.lstring = it->string;
18956 it->bidi_it.string.s = NULL;
18957 it->bidi_it.string.schars = it->end_charpos;
18958 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18959 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18960 it->bidi_it.string.unibyte = !it->multibyte_p;
18961 it->bidi_it.w = it->w;
18962 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18965 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18967 it->method = GET_FROM_STRETCH;
18968 it->object = prop;
18970 #ifdef HAVE_WINDOW_SYSTEM
18971 else if (IMAGEP (prop))
18973 it->what = IT_IMAGE;
18974 it->image_id = lookup_image (it->f, prop);
18975 it->method = GET_FROM_IMAGE;
18977 #endif /* HAVE_WINDOW_SYSTEM */
18978 else
18980 pop_it (it); /* bogus display property, give up */
18981 return 0;
18984 return 1;
18987 /* Return the character-property PROP at the current position in IT. */
18989 static Lisp_Object
18990 get_it_property (struct it *it, Lisp_Object prop)
18992 Lisp_Object position, object = it->object;
18994 if (STRINGP (object))
18995 position = make_number (IT_STRING_CHARPOS (*it));
18996 else if (BUFFERP (object))
18998 position = make_number (IT_CHARPOS (*it));
18999 object = it->window;
19001 else
19002 return Qnil;
19004 return Fget_char_property (position, prop, object);
19007 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19009 static void
19010 handle_line_prefix (struct it *it)
19012 Lisp_Object prefix;
19014 if (it->continuation_lines_width > 0)
19016 prefix = get_it_property (it, Qwrap_prefix);
19017 if (NILP (prefix))
19018 prefix = Vwrap_prefix;
19020 else
19022 prefix = get_it_property (it, Qline_prefix);
19023 if (NILP (prefix))
19024 prefix = Vline_prefix;
19026 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19028 /* If the prefix is wider than the window, and we try to wrap
19029 it, it would acquire its own wrap prefix, and so on till the
19030 iterator stack overflows. So, don't wrap the prefix. */
19031 it->line_wrap = TRUNCATE;
19032 it->avoid_cursor_p = 1;
19038 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19039 only for R2L lines from display_line and display_string, when they
19040 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19041 the line/string needs to be continued on the next glyph row. */
19042 static void
19043 unproduce_glyphs (struct it *it, int n)
19045 struct glyph *glyph, *end;
19047 eassert (it->glyph_row);
19048 eassert (it->glyph_row->reversed_p);
19049 eassert (it->area == TEXT_AREA);
19050 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19052 if (n > it->glyph_row->used[TEXT_AREA])
19053 n = it->glyph_row->used[TEXT_AREA];
19054 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19055 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19056 for ( ; glyph < end; glyph++)
19057 glyph[-n] = *glyph;
19060 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19061 and ROW->maxpos. */
19062 static void
19063 find_row_edges (struct it *it, struct glyph_row *row,
19064 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19065 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19067 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19068 lines' rows is implemented for bidi-reordered rows. */
19070 /* ROW->minpos is the value of min_pos, the minimal buffer position
19071 we have in ROW, or ROW->start.pos if that is smaller. */
19072 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19073 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19074 else
19075 /* We didn't find buffer positions smaller than ROW->start, or
19076 didn't find _any_ valid buffer positions in any of the glyphs,
19077 so we must trust the iterator's computed positions. */
19078 row->minpos = row->start.pos;
19079 if (max_pos <= 0)
19081 max_pos = CHARPOS (it->current.pos);
19082 max_bpos = BYTEPOS (it->current.pos);
19085 /* Here are the various use-cases for ending the row, and the
19086 corresponding values for ROW->maxpos:
19088 Line ends in a newline from buffer eol_pos + 1
19089 Line is continued from buffer max_pos + 1
19090 Line is truncated on right it->current.pos
19091 Line ends in a newline from string max_pos + 1(*)
19092 (*) + 1 only when line ends in a forward scan
19093 Line is continued from string max_pos
19094 Line is continued from display vector max_pos
19095 Line is entirely from a string min_pos == max_pos
19096 Line is entirely from a display vector min_pos == max_pos
19097 Line that ends at ZV ZV
19099 If you discover other use-cases, please add them here as
19100 appropriate. */
19101 if (row->ends_at_zv_p)
19102 row->maxpos = it->current.pos;
19103 else if (row->used[TEXT_AREA])
19105 int seen_this_string = 0;
19106 struct glyph_row *r1 = row - 1;
19108 /* Did we see the same display string on the previous row? */
19109 if (STRINGP (it->object)
19110 /* this is not the first row */
19111 && row > it->w->desired_matrix->rows
19112 /* previous row is not the header line */
19113 && !r1->mode_line_p
19114 /* previous row also ends in a newline from a string */
19115 && r1->ends_in_newline_from_string_p)
19117 struct glyph *start, *end;
19119 /* Search for the last glyph of the previous row that came
19120 from buffer or string. Depending on whether the row is
19121 L2R or R2L, we need to process it front to back or the
19122 other way round. */
19123 if (!r1->reversed_p)
19125 start = r1->glyphs[TEXT_AREA];
19126 end = start + r1->used[TEXT_AREA];
19127 /* Glyphs inserted by redisplay have an integer (zero)
19128 as their object. */
19129 while (end > start
19130 && INTEGERP ((end - 1)->object)
19131 && (end - 1)->charpos <= 0)
19132 --end;
19133 if (end > start)
19135 if (EQ ((end - 1)->object, it->object))
19136 seen_this_string = 1;
19138 else
19139 /* If all the glyphs of the previous row were inserted
19140 by redisplay, it means the previous row was
19141 produced from a single newline, which is only
19142 possible if that newline came from the same string
19143 as the one which produced this ROW. */
19144 seen_this_string = 1;
19146 else
19148 end = r1->glyphs[TEXT_AREA] - 1;
19149 start = end + r1->used[TEXT_AREA];
19150 while (end < start
19151 && INTEGERP ((end + 1)->object)
19152 && (end + 1)->charpos <= 0)
19153 ++end;
19154 if (end < start)
19156 if (EQ ((end + 1)->object, it->object))
19157 seen_this_string = 1;
19159 else
19160 seen_this_string = 1;
19163 /* Take note of each display string that covers a newline only
19164 once, the first time we see it. This is for when a display
19165 string includes more than one newline in it. */
19166 if (row->ends_in_newline_from_string_p && !seen_this_string)
19168 /* If we were scanning the buffer forward when we displayed
19169 the string, we want to account for at least one buffer
19170 position that belongs to this row (position covered by
19171 the display string), so that cursor positioning will
19172 consider this row as a candidate when point is at the end
19173 of the visual line represented by this row. This is not
19174 required when scanning back, because max_pos will already
19175 have a much larger value. */
19176 if (CHARPOS (row->end.pos) > max_pos)
19177 INC_BOTH (max_pos, max_bpos);
19178 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19180 else if (CHARPOS (it->eol_pos) > 0)
19181 SET_TEXT_POS (row->maxpos,
19182 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19183 else if (row->continued_p)
19185 /* If max_pos is different from IT's current position, it
19186 means IT->method does not belong to the display element
19187 at max_pos. However, it also means that the display
19188 element at max_pos was displayed in its entirety on this
19189 line, which is equivalent to saying that the next line
19190 starts at the next buffer position. */
19191 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19192 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19193 else
19195 INC_BOTH (max_pos, max_bpos);
19196 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19199 else if (row->truncated_on_right_p)
19200 /* display_line already called reseat_at_next_visible_line_start,
19201 which puts the iterator at the beginning of the next line, in
19202 the logical order. */
19203 row->maxpos = it->current.pos;
19204 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19205 /* A line that is entirely from a string/image/stretch... */
19206 row->maxpos = row->minpos;
19207 else
19208 emacs_abort ();
19210 else
19211 row->maxpos = it->current.pos;
19214 /* Construct the glyph row IT->glyph_row in the desired matrix of
19215 IT->w from text at the current position of IT. See dispextern.h
19216 for an overview of struct it. Value is non-zero if
19217 IT->glyph_row displays text, as opposed to a line displaying ZV
19218 only. */
19220 static int
19221 display_line (struct it *it)
19223 struct glyph_row *row = it->glyph_row;
19224 Lisp_Object overlay_arrow_string;
19225 struct it wrap_it;
19226 void *wrap_data = NULL;
19227 int may_wrap = 0, wrap_x IF_LINT (= 0);
19228 int wrap_row_used = -1;
19229 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19230 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19231 int wrap_row_extra_line_spacing IF_LINT (= 0);
19232 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19233 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19234 int cvpos;
19235 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19236 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19238 /* We always start displaying at hpos zero even if hscrolled. */
19239 eassert (it->hpos == 0 && it->current_x == 0);
19241 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19242 >= it->w->desired_matrix->nrows)
19244 it->w->nrows_scale_factor++;
19245 fonts_changed_p = 1;
19246 return 0;
19249 /* Is IT->w showing the region? */
19250 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19252 /* Clear the result glyph row and enable it. */
19253 prepare_desired_row (row);
19255 row->y = it->current_y;
19256 row->start = it->start;
19257 row->continuation_lines_width = it->continuation_lines_width;
19258 row->displays_text_p = 1;
19259 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19260 it->starts_in_middle_of_char_p = 0;
19262 /* Arrange the overlays nicely for our purposes. Usually, we call
19263 display_line on only one line at a time, in which case this
19264 can't really hurt too much, or we call it on lines which appear
19265 one after another in the buffer, in which case all calls to
19266 recenter_overlay_lists but the first will be pretty cheap. */
19267 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19269 /* Move over display elements that are not visible because we are
19270 hscrolled. This may stop at an x-position < IT->first_visible_x
19271 if the first glyph is partially visible or if we hit a line end. */
19272 if (it->current_x < it->first_visible_x)
19274 enum move_it_result move_result;
19276 this_line_min_pos = row->start.pos;
19277 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19278 MOVE_TO_POS | MOVE_TO_X);
19279 /* If we are under a large hscroll, move_it_in_display_line_to
19280 could hit the end of the line without reaching
19281 it->first_visible_x. Pretend that we did reach it. This is
19282 especially important on a TTY, where we will call
19283 extend_face_to_end_of_line, which needs to know how many
19284 blank glyphs to produce. */
19285 if (it->current_x < it->first_visible_x
19286 && (move_result == MOVE_NEWLINE_OR_CR
19287 || move_result == MOVE_POS_MATCH_OR_ZV))
19288 it->current_x = it->first_visible_x;
19290 /* Record the smallest positions seen while we moved over
19291 display elements that are not visible. This is needed by
19292 redisplay_internal for optimizing the case where the cursor
19293 stays inside the same line. The rest of this function only
19294 considers positions that are actually displayed, so
19295 RECORD_MAX_MIN_POS will not otherwise record positions that
19296 are hscrolled to the left of the left edge of the window. */
19297 min_pos = CHARPOS (this_line_min_pos);
19298 min_bpos = BYTEPOS (this_line_min_pos);
19300 else
19302 /* We only do this when not calling `move_it_in_display_line_to'
19303 above, because move_it_in_display_line_to calls
19304 handle_line_prefix itself. */
19305 handle_line_prefix (it);
19308 /* Get the initial row height. This is either the height of the
19309 text hscrolled, if there is any, or zero. */
19310 row->ascent = it->max_ascent;
19311 row->height = it->max_ascent + it->max_descent;
19312 row->phys_ascent = it->max_phys_ascent;
19313 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19314 row->extra_line_spacing = it->max_extra_line_spacing;
19316 /* Utility macro to record max and min buffer positions seen until now. */
19317 #define RECORD_MAX_MIN_POS(IT) \
19318 do \
19320 int composition_p = !STRINGP ((IT)->string) \
19321 && ((IT)->what == IT_COMPOSITION); \
19322 ptrdiff_t current_pos = \
19323 composition_p ? (IT)->cmp_it.charpos \
19324 : IT_CHARPOS (*(IT)); \
19325 ptrdiff_t current_bpos = \
19326 composition_p ? CHAR_TO_BYTE (current_pos) \
19327 : IT_BYTEPOS (*(IT)); \
19328 if (current_pos < min_pos) \
19330 min_pos = current_pos; \
19331 min_bpos = current_bpos; \
19333 if (IT_CHARPOS (*it) > max_pos) \
19335 max_pos = IT_CHARPOS (*it); \
19336 max_bpos = IT_BYTEPOS (*it); \
19339 while (0)
19341 /* Loop generating characters. The loop is left with IT on the next
19342 character to display. */
19343 while (1)
19345 int n_glyphs_before, hpos_before, x_before;
19346 int x, nglyphs;
19347 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19349 /* Retrieve the next thing to display. Value is zero if end of
19350 buffer reached. */
19351 if (!get_next_display_element (it))
19353 /* Maybe add a space at the end of this line that is used to
19354 display the cursor there under X. Set the charpos of the
19355 first glyph of blank lines not corresponding to any text
19356 to -1. */
19357 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19358 row->exact_window_width_line_p = 1;
19359 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19360 || row->used[TEXT_AREA] == 0)
19362 row->glyphs[TEXT_AREA]->charpos = -1;
19363 row->displays_text_p = 0;
19365 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19366 && (!MINI_WINDOW_P (it->w)
19367 || (minibuf_level && EQ (it->window, minibuf_window))))
19368 row->indicate_empty_line_p = 1;
19371 it->continuation_lines_width = 0;
19372 row->ends_at_zv_p = 1;
19373 /* A row that displays right-to-left text must always have
19374 its last face extended all the way to the end of line,
19375 even if this row ends in ZV, because we still write to
19376 the screen left to right. We also need to extend the
19377 last face if the default face is remapped to some
19378 different face, otherwise the functions that clear
19379 portions of the screen will clear with the default face's
19380 background color. */
19381 if (row->reversed_p
19382 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19383 extend_face_to_end_of_line (it);
19384 break;
19387 /* Now, get the metrics of what we want to display. This also
19388 generates glyphs in `row' (which is IT->glyph_row). */
19389 n_glyphs_before = row->used[TEXT_AREA];
19390 x = it->current_x;
19392 /* Remember the line height so far in case the next element doesn't
19393 fit on the line. */
19394 if (it->line_wrap != TRUNCATE)
19396 ascent = it->max_ascent;
19397 descent = it->max_descent;
19398 phys_ascent = it->max_phys_ascent;
19399 phys_descent = it->max_phys_descent;
19401 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19403 if (IT_DISPLAYING_WHITESPACE (it))
19404 may_wrap = 1;
19405 else if (may_wrap)
19407 SAVE_IT (wrap_it, *it, wrap_data);
19408 wrap_x = x;
19409 wrap_row_used = row->used[TEXT_AREA];
19410 wrap_row_ascent = row->ascent;
19411 wrap_row_height = row->height;
19412 wrap_row_phys_ascent = row->phys_ascent;
19413 wrap_row_phys_height = row->phys_height;
19414 wrap_row_extra_line_spacing = row->extra_line_spacing;
19415 wrap_row_min_pos = min_pos;
19416 wrap_row_min_bpos = min_bpos;
19417 wrap_row_max_pos = max_pos;
19418 wrap_row_max_bpos = max_bpos;
19419 may_wrap = 0;
19424 PRODUCE_GLYPHS (it);
19426 /* If this display element was in marginal areas, continue with
19427 the next one. */
19428 if (it->area != TEXT_AREA)
19430 row->ascent = max (row->ascent, it->max_ascent);
19431 row->height = max (row->height, it->max_ascent + it->max_descent);
19432 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19433 row->phys_height = max (row->phys_height,
19434 it->max_phys_ascent + it->max_phys_descent);
19435 row->extra_line_spacing = max (row->extra_line_spacing,
19436 it->max_extra_line_spacing);
19437 set_iterator_to_next (it, 1);
19438 continue;
19441 /* Does the display element fit on the line? If we truncate
19442 lines, we should draw past the right edge of the window. If
19443 we don't truncate, we want to stop so that we can display the
19444 continuation glyph before the right margin. If lines are
19445 continued, there are two possible strategies for characters
19446 resulting in more than 1 glyph (e.g. tabs): Display as many
19447 glyphs as possible in this line and leave the rest for the
19448 continuation line, or display the whole element in the next
19449 line. Original redisplay did the former, so we do it also. */
19450 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19451 hpos_before = it->hpos;
19452 x_before = x;
19454 if (/* Not a newline. */
19455 nglyphs > 0
19456 /* Glyphs produced fit entirely in the line. */
19457 && it->current_x < it->last_visible_x)
19459 it->hpos += nglyphs;
19460 row->ascent = max (row->ascent, it->max_ascent);
19461 row->height = max (row->height, it->max_ascent + it->max_descent);
19462 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19463 row->phys_height = max (row->phys_height,
19464 it->max_phys_ascent + it->max_phys_descent);
19465 row->extra_line_spacing = max (row->extra_line_spacing,
19466 it->max_extra_line_spacing);
19467 if (it->current_x - it->pixel_width < it->first_visible_x)
19468 row->x = x - it->first_visible_x;
19469 /* Record the maximum and minimum buffer positions seen so
19470 far in glyphs that will be displayed by this row. */
19471 if (it->bidi_p)
19472 RECORD_MAX_MIN_POS (it);
19474 else
19476 int i, new_x;
19477 struct glyph *glyph;
19479 for (i = 0; i < nglyphs; ++i, x = new_x)
19481 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19482 new_x = x + glyph->pixel_width;
19484 if (/* Lines are continued. */
19485 it->line_wrap != TRUNCATE
19486 && (/* Glyph doesn't fit on the line. */
19487 new_x > it->last_visible_x
19488 /* Or it fits exactly on a window system frame. */
19489 || (new_x == it->last_visible_x
19490 && FRAME_WINDOW_P (it->f)
19491 && (row->reversed_p
19492 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19493 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19495 /* End of a continued line. */
19497 if (it->hpos == 0
19498 || (new_x == it->last_visible_x
19499 && FRAME_WINDOW_P (it->f)
19500 && (row->reversed_p
19501 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19502 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19504 /* Current glyph is the only one on the line or
19505 fits exactly on the line. We must continue
19506 the line because we can't draw the cursor
19507 after the glyph. */
19508 row->continued_p = 1;
19509 it->current_x = new_x;
19510 it->continuation_lines_width += new_x;
19511 ++it->hpos;
19512 if (i == nglyphs - 1)
19514 /* If line-wrap is on, check if a previous
19515 wrap point was found. */
19516 if (wrap_row_used > 0
19517 /* Even if there is a previous wrap
19518 point, continue the line here as
19519 usual, if (i) the previous character
19520 was a space or tab AND (ii) the
19521 current character is not. */
19522 && (!may_wrap
19523 || IT_DISPLAYING_WHITESPACE (it)))
19524 goto back_to_wrap;
19526 /* Record the maximum and minimum buffer
19527 positions seen so far in glyphs that will be
19528 displayed by this row. */
19529 if (it->bidi_p)
19530 RECORD_MAX_MIN_POS (it);
19531 set_iterator_to_next (it, 1);
19532 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19534 if (!get_next_display_element (it))
19536 row->exact_window_width_line_p = 1;
19537 it->continuation_lines_width = 0;
19538 row->continued_p = 0;
19539 row->ends_at_zv_p = 1;
19541 else if (ITERATOR_AT_END_OF_LINE_P (it))
19543 row->continued_p = 0;
19544 row->exact_window_width_line_p = 1;
19548 else if (it->bidi_p)
19549 RECORD_MAX_MIN_POS (it);
19551 else if (CHAR_GLYPH_PADDING_P (*glyph)
19552 && !FRAME_WINDOW_P (it->f))
19554 /* A padding glyph that doesn't fit on this line.
19555 This means the whole character doesn't fit
19556 on the line. */
19557 if (row->reversed_p)
19558 unproduce_glyphs (it, row->used[TEXT_AREA]
19559 - n_glyphs_before);
19560 row->used[TEXT_AREA] = n_glyphs_before;
19562 /* Fill the rest of the row with continuation
19563 glyphs like in 20.x. */
19564 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19565 < row->glyphs[1 + TEXT_AREA])
19566 produce_special_glyphs (it, IT_CONTINUATION);
19568 row->continued_p = 1;
19569 it->current_x = x_before;
19570 it->continuation_lines_width += x_before;
19572 /* Restore the height to what it was before the
19573 element not fitting on the line. */
19574 it->max_ascent = ascent;
19575 it->max_descent = descent;
19576 it->max_phys_ascent = phys_ascent;
19577 it->max_phys_descent = phys_descent;
19579 else if (wrap_row_used > 0)
19581 back_to_wrap:
19582 if (row->reversed_p)
19583 unproduce_glyphs (it,
19584 row->used[TEXT_AREA] - wrap_row_used);
19585 RESTORE_IT (it, &wrap_it, wrap_data);
19586 it->continuation_lines_width += wrap_x;
19587 row->used[TEXT_AREA] = wrap_row_used;
19588 row->ascent = wrap_row_ascent;
19589 row->height = wrap_row_height;
19590 row->phys_ascent = wrap_row_phys_ascent;
19591 row->phys_height = wrap_row_phys_height;
19592 row->extra_line_spacing = wrap_row_extra_line_spacing;
19593 min_pos = wrap_row_min_pos;
19594 min_bpos = wrap_row_min_bpos;
19595 max_pos = wrap_row_max_pos;
19596 max_bpos = wrap_row_max_bpos;
19597 row->continued_p = 1;
19598 row->ends_at_zv_p = 0;
19599 row->exact_window_width_line_p = 0;
19600 it->continuation_lines_width += x;
19602 /* Make sure that a non-default face is extended
19603 up to the right margin of the window. */
19604 extend_face_to_end_of_line (it);
19606 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19608 /* A TAB that extends past the right edge of the
19609 window. This produces a single glyph on
19610 window system frames. We leave the glyph in
19611 this row and let it fill the row, but don't
19612 consume the TAB. */
19613 if ((row->reversed_p
19614 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19615 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19616 produce_special_glyphs (it, IT_CONTINUATION);
19617 it->continuation_lines_width += it->last_visible_x;
19618 row->ends_in_middle_of_char_p = 1;
19619 row->continued_p = 1;
19620 glyph->pixel_width = it->last_visible_x - x;
19621 it->starts_in_middle_of_char_p = 1;
19623 else
19625 /* Something other than a TAB that draws past
19626 the right edge of the window. Restore
19627 positions to values before the element. */
19628 if (row->reversed_p)
19629 unproduce_glyphs (it, row->used[TEXT_AREA]
19630 - (n_glyphs_before + i));
19631 row->used[TEXT_AREA] = n_glyphs_before + i;
19633 /* Display continuation glyphs. */
19634 it->current_x = x_before;
19635 it->continuation_lines_width += x;
19636 if (!FRAME_WINDOW_P (it->f)
19637 || (row->reversed_p
19638 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19639 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19640 produce_special_glyphs (it, IT_CONTINUATION);
19641 row->continued_p = 1;
19643 extend_face_to_end_of_line (it);
19645 if (nglyphs > 1 && i > 0)
19647 row->ends_in_middle_of_char_p = 1;
19648 it->starts_in_middle_of_char_p = 1;
19651 /* Restore the height to what it was before the
19652 element not fitting on the line. */
19653 it->max_ascent = ascent;
19654 it->max_descent = descent;
19655 it->max_phys_ascent = phys_ascent;
19656 it->max_phys_descent = phys_descent;
19659 break;
19661 else if (new_x > it->first_visible_x)
19663 /* Increment number of glyphs actually displayed. */
19664 ++it->hpos;
19666 /* Record the maximum and minimum buffer positions
19667 seen so far in glyphs that will be displayed by
19668 this row. */
19669 if (it->bidi_p)
19670 RECORD_MAX_MIN_POS (it);
19672 if (x < it->first_visible_x)
19673 /* Glyph is partially visible, i.e. row starts at
19674 negative X position. */
19675 row->x = x - it->first_visible_x;
19677 else
19679 /* Glyph is completely off the left margin of the
19680 window. This should not happen because of the
19681 move_it_in_display_line at the start of this
19682 function, unless the text display area of the
19683 window is empty. */
19684 eassert (it->first_visible_x <= it->last_visible_x);
19687 /* Even if this display element produced no glyphs at all,
19688 we want to record its position. */
19689 if (it->bidi_p && nglyphs == 0)
19690 RECORD_MAX_MIN_POS (it);
19692 row->ascent = max (row->ascent, it->max_ascent);
19693 row->height = max (row->height, it->max_ascent + it->max_descent);
19694 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19695 row->phys_height = max (row->phys_height,
19696 it->max_phys_ascent + it->max_phys_descent);
19697 row->extra_line_spacing = max (row->extra_line_spacing,
19698 it->max_extra_line_spacing);
19700 /* End of this display line if row is continued. */
19701 if (row->continued_p || row->ends_at_zv_p)
19702 break;
19705 at_end_of_line:
19706 /* Is this a line end? If yes, we're also done, after making
19707 sure that a non-default face is extended up to the right
19708 margin of the window. */
19709 if (ITERATOR_AT_END_OF_LINE_P (it))
19711 int used_before = row->used[TEXT_AREA];
19713 row->ends_in_newline_from_string_p = STRINGP (it->object);
19715 /* Add a space at the end of the line that is used to
19716 display the cursor there. */
19717 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19718 append_space_for_newline (it, 0);
19720 /* Extend the face to the end of the line. */
19721 extend_face_to_end_of_line (it);
19723 /* Make sure we have the position. */
19724 if (used_before == 0)
19725 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19727 /* Record the position of the newline, for use in
19728 find_row_edges. */
19729 it->eol_pos = it->current.pos;
19731 /* Consume the line end. This skips over invisible lines. */
19732 set_iterator_to_next (it, 1);
19733 it->continuation_lines_width = 0;
19734 break;
19737 /* Proceed with next display element. Note that this skips
19738 over lines invisible because of selective display. */
19739 set_iterator_to_next (it, 1);
19741 /* If we truncate lines, we are done when the last displayed
19742 glyphs reach past the right margin of the window. */
19743 if (it->line_wrap == TRUNCATE
19744 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19745 ? (it->current_x >= it->last_visible_x)
19746 : (it->current_x > it->last_visible_x)))
19748 /* Maybe add truncation glyphs. */
19749 if (!FRAME_WINDOW_P (it->f)
19750 || (row->reversed_p
19751 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19752 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19754 int i, n;
19756 if (!row->reversed_p)
19758 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19759 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19760 break;
19762 else
19764 for (i = 0; i < row->used[TEXT_AREA]; i++)
19765 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19766 break;
19767 /* Remove any padding glyphs at the front of ROW, to
19768 make room for the truncation glyphs we will be
19769 adding below. The loop below always inserts at
19770 least one truncation glyph, so also remove the
19771 last glyph added to ROW. */
19772 unproduce_glyphs (it, i + 1);
19773 /* Adjust i for the loop below. */
19774 i = row->used[TEXT_AREA] - (i + 1);
19777 it->current_x = x_before;
19778 if (!FRAME_WINDOW_P (it->f))
19780 for (n = row->used[TEXT_AREA]; i < n; ++i)
19782 row->used[TEXT_AREA] = i;
19783 produce_special_glyphs (it, IT_TRUNCATION);
19786 else
19788 row->used[TEXT_AREA] = i;
19789 produce_special_glyphs (it, IT_TRUNCATION);
19792 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19794 /* Don't truncate if we can overflow newline into fringe. */
19795 if (!get_next_display_element (it))
19797 it->continuation_lines_width = 0;
19798 row->ends_at_zv_p = 1;
19799 row->exact_window_width_line_p = 1;
19800 break;
19802 if (ITERATOR_AT_END_OF_LINE_P (it))
19804 row->exact_window_width_line_p = 1;
19805 goto at_end_of_line;
19807 it->current_x = x_before;
19810 row->truncated_on_right_p = 1;
19811 it->continuation_lines_width = 0;
19812 reseat_at_next_visible_line_start (it, 0);
19813 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19814 it->hpos = hpos_before;
19815 break;
19819 if (wrap_data)
19820 bidi_unshelve_cache (wrap_data, 1);
19822 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19823 at the left window margin. */
19824 if (it->first_visible_x
19825 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19827 if (!FRAME_WINDOW_P (it->f)
19828 || (row->reversed_p
19829 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19830 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19831 insert_left_trunc_glyphs (it);
19832 row->truncated_on_left_p = 1;
19835 /* Remember the position at which this line ends.
19837 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19838 cannot be before the call to find_row_edges below, since that is
19839 where these positions are determined. */
19840 row->end = it->current;
19841 if (!it->bidi_p)
19843 row->minpos = row->start.pos;
19844 row->maxpos = row->end.pos;
19846 else
19848 /* ROW->minpos and ROW->maxpos must be the smallest and
19849 `1 + the largest' buffer positions in ROW. But if ROW was
19850 bidi-reordered, these two positions can be anywhere in the
19851 row, so we must determine them now. */
19852 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19855 /* If the start of this line is the overlay arrow-position, then
19856 mark this glyph row as the one containing the overlay arrow.
19857 This is clearly a mess with variable size fonts. It would be
19858 better to let it be displayed like cursors under X. */
19859 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19860 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19861 !NILP (overlay_arrow_string)))
19863 /* Overlay arrow in window redisplay is a fringe bitmap. */
19864 if (STRINGP (overlay_arrow_string))
19866 struct glyph_row *arrow_row
19867 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19868 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19869 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19870 struct glyph *p = row->glyphs[TEXT_AREA];
19871 struct glyph *p2, *end;
19873 /* Copy the arrow glyphs. */
19874 while (glyph < arrow_end)
19875 *p++ = *glyph++;
19877 /* Throw away padding glyphs. */
19878 p2 = p;
19879 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19880 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19881 ++p2;
19882 if (p2 > p)
19884 while (p2 < end)
19885 *p++ = *p2++;
19886 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19889 else
19891 eassert (INTEGERP (overlay_arrow_string));
19892 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19894 overlay_arrow_seen = 1;
19897 /* Highlight trailing whitespace. */
19898 if (!NILP (Vshow_trailing_whitespace))
19899 highlight_trailing_whitespace (it->f, it->glyph_row);
19901 /* Compute pixel dimensions of this line. */
19902 compute_line_metrics (it);
19904 /* Implementation note: No changes in the glyphs of ROW or in their
19905 faces can be done past this point, because compute_line_metrics
19906 computes ROW's hash value and stores it within the glyph_row
19907 structure. */
19909 /* Record whether this row ends inside an ellipsis. */
19910 row->ends_in_ellipsis_p
19911 = (it->method == GET_FROM_DISPLAY_VECTOR
19912 && it->ellipsis_p);
19914 /* Save fringe bitmaps in this row. */
19915 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19916 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19917 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19918 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19920 it->left_user_fringe_bitmap = 0;
19921 it->left_user_fringe_face_id = 0;
19922 it->right_user_fringe_bitmap = 0;
19923 it->right_user_fringe_face_id = 0;
19925 /* Maybe set the cursor. */
19926 cvpos = it->w->cursor.vpos;
19927 if ((cvpos < 0
19928 /* In bidi-reordered rows, keep checking for proper cursor
19929 position even if one has been found already, because buffer
19930 positions in such rows change non-linearly with ROW->VPOS,
19931 when a line is continued. One exception: when we are at ZV,
19932 display cursor on the first suitable glyph row, since all
19933 the empty rows after that also have their position set to ZV. */
19934 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19935 lines' rows is implemented for bidi-reordered rows. */
19936 || (it->bidi_p
19937 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19938 && PT >= MATRIX_ROW_START_CHARPOS (row)
19939 && PT <= MATRIX_ROW_END_CHARPOS (row)
19940 && cursor_row_p (row))
19941 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19943 /* Prepare for the next line. This line starts horizontally at (X
19944 HPOS) = (0 0). Vertical positions are incremented. As a
19945 convenience for the caller, IT->glyph_row is set to the next
19946 row to be used. */
19947 it->current_x = it->hpos = 0;
19948 it->current_y += row->height;
19949 SET_TEXT_POS (it->eol_pos, 0, 0);
19950 ++it->vpos;
19951 ++it->glyph_row;
19952 /* The next row should by default use the same value of the
19953 reversed_p flag as this one. set_iterator_to_next decides when
19954 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19955 the flag accordingly. */
19956 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19957 it->glyph_row->reversed_p = row->reversed_p;
19958 it->start = row->end;
19959 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19961 #undef RECORD_MAX_MIN_POS
19964 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19965 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19966 doc: /* Return paragraph direction at point in BUFFER.
19967 Value is either `left-to-right' or `right-to-left'.
19968 If BUFFER is omitted or nil, it defaults to the current buffer.
19970 Paragraph direction determines how the text in the paragraph is displayed.
19971 In left-to-right paragraphs, text begins at the left margin of the window
19972 and the reading direction is generally left to right. In right-to-left
19973 paragraphs, text begins at the right margin and is read from right to left.
19975 See also `bidi-paragraph-direction'. */)
19976 (Lisp_Object buffer)
19978 struct buffer *buf = current_buffer;
19979 struct buffer *old = buf;
19981 if (! NILP (buffer))
19983 CHECK_BUFFER (buffer);
19984 buf = XBUFFER (buffer);
19987 if (NILP (BVAR (buf, bidi_display_reordering))
19988 || NILP (BVAR (buf, enable_multibyte_characters))
19989 /* When we are loading loadup.el, the character property tables
19990 needed for bidi iteration are not yet available. */
19991 || !NILP (Vpurify_flag))
19992 return Qleft_to_right;
19993 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19994 return BVAR (buf, bidi_paragraph_direction);
19995 else
19997 /* Determine the direction from buffer text. We could try to
19998 use current_matrix if it is up to date, but this seems fast
19999 enough as it is. */
20000 struct bidi_it itb;
20001 ptrdiff_t pos = BUF_PT (buf);
20002 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20003 int c;
20004 void *itb_data = bidi_shelve_cache ();
20006 set_buffer_temp (buf);
20007 /* bidi_paragraph_init finds the base direction of the paragraph
20008 by searching forward from paragraph start. We need the base
20009 direction of the current or _previous_ paragraph, so we need
20010 to make sure we are within that paragraph. To that end, find
20011 the previous non-empty line. */
20012 if (pos >= ZV && pos > BEGV)
20013 DEC_BOTH (pos, bytepos);
20014 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20015 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20017 while ((c = FETCH_BYTE (bytepos)) == '\n'
20018 || c == ' ' || c == '\t' || c == '\f')
20020 if (bytepos <= BEGV_BYTE)
20021 break;
20022 bytepos--;
20023 pos--;
20025 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20026 bytepos--;
20028 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20029 itb.paragraph_dir = NEUTRAL_DIR;
20030 itb.string.s = NULL;
20031 itb.string.lstring = Qnil;
20032 itb.string.bufpos = 0;
20033 itb.string.unibyte = 0;
20034 /* We have no window to use here for ignoring window-specific
20035 overlays. Using NULL for window pointer will cause
20036 compute_display_string_pos to use the current buffer. */
20037 itb.w = NULL;
20038 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20039 bidi_unshelve_cache (itb_data, 0);
20040 set_buffer_temp (old);
20041 switch (itb.paragraph_dir)
20043 case L2R:
20044 return Qleft_to_right;
20045 break;
20046 case R2L:
20047 return Qright_to_left;
20048 break;
20049 default:
20050 emacs_abort ();
20055 DEFUN ("move-point-visually", Fmove_point_visually,
20056 Smove_point_visually, 1, 1, 0,
20057 doc: /* Move point in the visual order in the specified DIRECTION.
20058 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20059 left.
20061 Value is the new character position of point. */)
20062 (Lisp_Object direction)
20064 struct window *w = XWINDOW (selected_window);
20065 struct buffer *b = NULL;
20066 struct glyph_row *row;
20067 int dir;
20068 Lisp_Object paragraph_dir;
20070 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20071 (!(ROW)->continued_p \
20072 && INTEGERP ((GLYPH)->object) \
20073 && (GLYPH)->type == CHAR_GLYPH \
20074 && (GLYPH)->u.ch == ' ' \
20075 && (GLYPH)->charpos >= 0 \
20076 && !(GLYPH)->avoid_cursor_p)
20078 CHECK_NUMBER (direction);
20079 dir = XINT (direction);
20080 if (dir > 0)
20081 dir = 1;
20082 else
20083 dir = -1;
20085 if (BUFFERP (w->contents))
20086 b = XBUFFER (w->contents);
20088 /* If current matrix is up-to-date, we can use the information
20089 recorded in the glyphs, at least as long as the goal is on the
20090 screen. */
20091 if (w->window_end_valid
20092 && !windows_or_buffers_changed
20093 && b
20094 && !b->clip_changed
20095 && !b->prevent_redisplay_optimizations_p
20096 && w->last_modified >= BUF_MODIFF (b)
20097 && w->last_overlay_modified >= BUF_OVERLAY_MODIFF (b)
20098 && w->cursor.vpos >= 0
20099 && w->cursor.vpos < w->current_matrix->nrows
20100 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20102 struct glyph *g = row->glyphs[TEXT_AREA];
20103 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20104 struct glyph *gpt = g + w->cursor.hpos;
20106 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20108 if (BUFFERP (g->object) && g->charpos != PT)
20110 SET_PT (g->charpos);
20111 w->cursor.vpos = -1;
20112 return make_number (PT);
20114 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20116 ptrdiff_t new_pos;
20118 if (BUFFERP (gpt->object))
20120 new_pos = PT;
20121 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20122 new_pos += (row->reversed_p ? -dir : dir);
20123 else
20124 new_pos -= (row->reversed_p ? -dir : dir);;
20126 else if (BUFFERP (g->object))
20127 new_pos = g->charpos;
20128 else
20129 break;
20130 SET_PT (new_pos);
20131 w->cursor.vpos = -1;
20132 return make_number (PT);
20134 else if (ROW_GLYPH_NEWLINE_P (row, g))
20136 /* Glyphs inserted at the end of a non-empty line for
20137 positioning the cursor have zero charpos, so we must
20138 deduce the value of point by other means. */
20139 if (g->charpos > 0)
20140 SET_PT (g->charpos);
20141 else if (row->ends_at_zv_p && PT != ZV)
20142 SET_PT (ZV);
20143 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20144 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20145 else
20146 break;
20147 w->cursor.vpos = -1;
20148 return make_number (PT);
20151 if (g == e || INTEGERP (g->object))
20153 if (row->truncated_on_left_p || row->truncated_on_right_p)
20154 goto simulate_display;
20155 if (!row->reversed_p)
20156 row += dir;
20157 else
20158 row -= dir;
20159 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20160 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20161 goto simulate_display;
20163 if (dir > 0)
20165 if (row->reversed_p && !row->continued_p)
20167 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20168 w->cursor.vpos = -1;
20169 return make_number (PT);
20171 g = row->glyphs[TEXT_AREA];
20172 e = g + row->used[TEXT_AREA];
20173 for ( ; g < e; g++)
20175 if (BUFFERP (g->object)
20176 /* Empty lines have only one glyph, which stands
20177 for the newline, and whose charpos is the
20178 buffer position of the newline. */
20179 || ROW_GLYPH_NEWLINE_P (row, g)
20180 /* When the buffer ends in a newline, the line at
20181 EOB also has one glyph, but its charpos is -1. */
20182 || (row->ends_at_zv_p
20183 && !row->reversed_p
20184 && INTEGERP (g->object)
20185 && g->type == CHAR_GLYPH
20186 && g->u.ch == ' '))
20188 if (g->charpos > 0)
20189 SET_PT (g->charpos);
20190 else if (!row->reversed_p
20191 && row->ends_at_zv_p
20192 && PT != ZV)
20193 SET_PT (ZV);
20194 else
20195 continue;
20196 w->cursor.vpos = -1;
20197 return make_number (PT);
20201 else
20203 if (!row->reversed_p && !row->continued_p)
20205 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20206 w->cursor.vpos = -1;
20207 return make_number (PT);
20209 e = row->glyphs[TEXT_AREA];
20210 g = e + row->used[TEXT_AREA] - 1;
20211 for ( ; g >= e; g--)
20213 if (BUFFERP (g->object)
20214 || (ROW_GLYPH_NEWLINE_P (row, g)
20215 && g->charpos > 0)
20216 /* Empty R2L lines on GUI frames have the buffer
20217 position of the newline stored in the stretch
20218 glyph. */
20219 || g->type == STRETCH_GLYPH
20220 || (row->ends_at_zv_p
20221 && row->reversed_p
20222 && INTEGERP (g->object)
20223 && g->type == CHAR_GLYPH
20224 && g->u.ch == ' '))
20226 if (g->charpos > 0)
20227 SET_PT (g->charpos);
20228 else if (row->reversed_p
20229 && row->ends_at_zv_p
20230 && PT != ZV)
20231 SET_PT (ZV);
20232 else
20233 continue;
20234 w->cursor.vpos = -1;
20235 return make_number (PT);
20242 simulate_display:
20244 /* If we wind up here, we failed to move by using the glyphs, so we
20245 need to simulate display instead. */
20247 if (b)
20248 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20249 else
20250 paragraph_dir = Qleft_to_right;
20251 if (EQ (paragraph_dir, Qright_to_left))
20252 dir = -dir;
20253 if (PT <= BEGV && dir < 0)
20254 xsignal0 (Qbeginning_of_buffer);
20255 else if (PT >= ZV && dir > 0)
20256 xsignal0 (Qend_of_buffer);
20257 else
20259 struct text_pos pt;
20260 struct it it;
20261 int pt_x, target_x, pixel_width, pt_vpos;
20262 bool at_eol_p;
20263 bool overshoot_expected = false;
20264 bool target_is_eol_p = false;
20266 /* Setup the arena. */
20267 SET_TEXT_POS (pt, PT, PT_BYTE);
20268 start_display (&it, w, pt);
20270 if (it.cmp_it.id < 0
20271 && it.method == GET_FROM_STRING
20272 && it.area == TEXT_AREA
20273 && it.string_from_display_prop_p
20274 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20275 overshoot_expected = true;
20277 /* Find the X coordinate of point. We start from the beginning
20278 of this or previous line to make sure we are before point in
20279 the logical order (since the move_it_* functions can only
20280 move forward). */
20281 reseat_at_previous_visible_line_start (&it);
20282 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20283 if (IT_CHARPOS (it) != PT)
20284 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20285 -1, -1, -1, MOVE_TO_POS);
20286 pt_x = it.current_x;
20287 pt_vpos = it.vpos;
20288 if (dir > 0 || overshoot_expected)
20290 struct glyph_row *row = it.glyph_row;
20292 /* When point is at beginning of line, we don't have
20293 information about the glyph there loaded into struct
20294 it. Calling get_next_display_element fixes that. */
20295 if (pt_x == 0)
20296 get_next_display_element (&it);
20297 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20298 it.glyph_row = NULL;
20299 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20300 it.glyph_row = row;
20301 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20302 it, lest it will become out of sync with it's buffer
20303 position. */
20304 it.current_x = pt_x;
20306 else
20307 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20308 pixel_width = it.pixel_width;
20309 if (overshoot_expected && at_eol_p)
20310 pixel_width = 0;
20311 else if (pixel_width <= 0)
20312 pixel_width = 1;
20314 /* If there's a display string at point, we are actually at the
20315 glyph to the left of point, so we need to correct the X
20316 coordinate. */
20317 if (overshoot_expected)
20318 pt_x += pixel_width;
20320 /* Compute target X coordinate, either to the left or to the
20321 right of point. On TTY frames, all characters have the same
20322 pixel width of 1, so we can use that. On GUI frames we don't
20323 have an easy way of getting at the pixel width of the
20324 character to the left of point, so we use a different method
20325 of getting to that place. */
20326 if (dir > 0)
20327 target_x = pt_x + pixel_width;
20328 else
20329 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20331 /* Target X coordinate could be one line above or below the line
20332 of point, in which case we need to adjust the target X
20333 coordinate. Also, if moving to the left, we need to begin at
20334 the left edge of the point's screen line. */
20335 if (dir < 0)
20337 if (pt_x > 0)
20339 start_display (&it, w, pt);
20340 reseat_at_previous_visible_line_start (&it);
20341 it.current_x = it.current_y = it.hpos = 0;
20342 if (pt_vpos != 0)
20343 move_it_by_lines (&it, pt_vpos);
20345 else
20347 move_it_by_lines (&it, -1);
20348 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20349 target_is_eol_p = true;
20352 else
20354 if (at_eol_p
20355 || (target_x >= it.last_visible_x
20356 && it.line_wrap != TRUNCATE))
20358 if (pt_x > 0)
20359 move_it_by_lines (&it, 0);
20360 move_it_by_lines (&it, 1);
20361 target_x = 0;
20365 /* Move to the target X coordinate. */
20366 #ifdef HAVE_WINDOW_SYSTEM
20367 /* On GUI frames, as we don't know the X coordinate of the
20368 character to the left of point, moving point to the left
20369 requires walking, one grapheme cluster at a time, until we
20370 find ourself at a place immediately to the left of the
20371 character at point. */
20372 if (FRAME_WINDOW_P (it.f) && dir < 0)
20374 struct text_pos new_pos = it.current.pos;
20375 enum move_it_result rc = MOVE_X_REACHED;
20377 while (it.current_x + it.pixel_width <= target_x
20378 && rc == MOVE_X_REACHED)
20380 int new_x = it.current_x + it.pixel_width;
20382 new_pos = it.current.pos;
20383 if (new_x == it.current_x)
20384 new_x++;
20385 rc = move_it_in_display_line_to (&it, ZV, new_x,
20386 MOVE_TO_POS | MOVE_TO_X);
20387 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20388 break;
20390 /* If we ended up on a composed character inside
20391 bidi-reordered text (e.g., Hebrew text with diacritics),
20392 the iterator gives us the buffer position of the last (in
20393 logical order) character of the composed grapheme cluster,
20394 which is not what we want. So we cheat: we compute the
20395 character position of the character that follows (in the
20396 logical order) the one where the above loop stopped. That
20397 character will appear on display to the left of point. */
20398 if (it.bidi_p
20399 && it.bidi_it.scan_dir == -1
20400 && new_pos.charpos - IT_CHARPOS (it) > 1)
20402 new_pos.charpos = IT_CHARPOS (it) + 1;
20403 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20405 it.current.pos = new_pos;
20407 else
20408 #endif
20409 if (it.current_x != target_x)
20410 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20412 /* When lines are truncated, the above loop will stop at the
20413 window edge. But we want to get to the end of line, even if
20414 it is beyond the window edge; automatic hscroll will then
20415 scroll the window to show point as appropriate. */
20416 if (target_is_eol_p && it.line_wrap == TRUNCATE
20417 && get_next_display_element (&it))
20419 struct text_pos new_pos = it.current.pos;
20421 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20423 set_iterator_to_next (&it, 0);
20424 if (it.method == GET_FROM_BUFFER)
20425 new_pos = it.current.pos;
20426 if (!get_next_display_element (&it))
20427 break;
20430 it.current.pos = new_pos;
20433 /* If we ended up in a display string that covers point, move to
20434 buffer position to the right in the visual order. */
20435 if (dir > 0)
20437 while (IT_CHARPOS (it) == PT)
20439 set_iterator_to_next (&it, 0);
20440 if (!get_next_display_element (&it))
20441 break;
20445 /* Move point to that position. */
20446 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20449 return make_number (PT);
20451 #undef ROW_GLYPH_NEWLINE_P
20455 /***********************************************************************
20456 Menu Bar
20457 ***********************************************************************/
20459 /* Redisplay the menu bar in the frame for window W.
20461 The menu bar of X frames that don't have X toolkit support is
20462 displayed in a special window W->frame->menu_bar_window.
20464 The menu bar of terminal frames is treated specially as far as
20465 glyph matrices are concerned. Menu bar lines are not part of
20466 windows, so the update is done directly on the frame matrix rows
20467 for the menu bar. */
20469 static void
20470 display_menu_bar (struct window *w)
20472 struct frame *f = XFRAME (WINDOW_FRAME (w));
20473 struct it it;
20474 Lisp_Object items;
20475 int i;
20477 /* Don't do all this for graphical frames. */
20478 #ifdef HAVE_NTGUI
20479 if (FRAME_W32_P (f))
20480 return;
20481 #endif
20482 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20483 if (FRAME_X_P (f))
20484 return;
20485 #endif
20487 #ifdef HAVE_NS
20488 if (FRAME_NS_P (f))
20489 return;
20490 #endif /* HAVE_NS */
20492 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20493 eassert (!FRAME_WINDOW_P (f));
20494 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20495 it.first_visible_x = 0;
20496 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20497 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20498 if (FRAME_WINDOW_P (f))
20500 /* Menu bar lines are displayed in the desired matrix of the
20501 dummy window menu_bar_window. */
20502 struct window *menu_w;
20503 menu_w = XWINDOW (f->menu_bar_window);
20504 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20505 MENU_FACE_ID);
20506 it.first_visible_x = 0;
20507 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20509 else
20510 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20512 /* This is a TTY frame, i.e. character hpos/vpos are used as
20513 pixel x/y. */
20514 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20515 MENU_FACE_ID);
20516 it.first_visible_x = 0;
20517 it.last_visible_x = FRAME_COLS (f);
20520 /* FIXME: This should be controlled by a user option. See the
20521 comments in redisplay_tool_bar and display_mode_line about
20522 this. */
20523 it.paragraph_embedding = L2R;
20525 /* Clear all rows of the menu bar. */
20526 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20528 struct glyph_row *row = it.glyph_row + i;
20529 clear_glyph_row (row);
20530 row->enabled_p = 1;
20531 row->full_width_p = 1;
20534 /* Display all items of the menu bar. */
20535 items = FRAME_MENU_BAR_ITEMS (it.f);
20536 for (i = 0; i < ASIZE (items); i += 4)
20538 Lisp_Object string;
20540 /* Stop at nil string. */
20541 string = AREF (items, i + 1);
20542 if (NILP (string))
20543 break;
20545 /* Remember where item was displayed. */
20546 ASET (items, i + 3, make_number (it.hpos));
20548 /* Display the item, pad with one space. */
20549 if (it.current_x < it.last_visible_x)
20550 display_string (NULL, string, Qnil, 0, 0, &it,
20551 SCHARS (string) + 1, 0, 0, -1);
20554 /* Fill out the line with spaces. */
20555 if (it.current_x < it.last_visible_x)
20556 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20558 /* Compute the total height of the lines. */
20559 compute_line_metrics (&it);
20564 /***********************************************************************
20565 Mode Line
20566 ***********************************************************************/
20568 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20569 FORCE is non-zero, redisplay mode lines unconditionally.
20570 Otherwise, redisplay only mode lines that are garbaged. Value is
20571 the number of windows whose mode lines were redisplayed. */
20573 static int
20574 redisplay_mode_lines (Lisp_Object window, int force)
20576 int nwindows = 0;
20578 while (!NILP (window))
20580 struct window *w = XWINDOW (window);
20582 if (WINDOWP (w->contents))
20583 nwindows += redisplay_mode_lines (w->contents, force);
20584 else if (force
20585 || FRAME_GARBAGED_P (XFRAME (w->frame))
20586 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20588 struct text_pos lpoint;
20589 struct buffer *old = current_buffer;
20591 /* Set the window's buffer for the mode line display. */
20592 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20593 set_buffer_internal_1 (XBUFFER (w->contents));
20595 /* Point refers normally to the selected window. For any
20596 other window, set up appropriate value. */
20597 if (!EQ (window, selected_window))
20599 struct text_pos pt;
20601 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20602 if (CHARPOS (pt) < BEGV)
20603 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20604 else if (CHARPOS (pt) > (ZV - 1))
20605 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20606 else
20607 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20610 /* Display mode lines. */
20611 clear_glyph_matrix (w->desired_matrix);
20612 if (display_mode_lines (w))
20614 ++nwindows;
20615 w->must_be_updated_p = 1;
20618 /* Restore old settings. */
20619 set_buffer_internal_1 (old);
20620 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20623 window = w->next;
20626 return nwindows;
20630 /* Display the mode and/or header line of window W. Value is the
20631 sum number of mode lines and header lines displayed. */
20633 static int
20634 display_mode_lines (struct window *w)
20636 Lisp_Object old_selected_window = selected_window;
20637 Lisp_Object old_selected_frame = selected_frame;
20638 Lisp_Object new_frame = w->frame;
20639 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20640 int n = 0;
20642 selected_frame = new_frame;
20643 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20644 or window's point, then we'd need select_window_1 here as well. */
20645 XSETWINDOW (selected_window, w);
20646 XFRAME (new_frame)->selected_window = selected_window;
20648 /* These will be set while the mode line specs are processed. */
20649 line_number_displayed = 0;
20650 w->column_number_displayed = -1;
20652 if (WINDOW_WANTS_MODELINE_P (w))
20654 struct window *sel_w = XWINDOW (old_selected_window);
20656 /* Select mode line face based on the real selected window. */
20657 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20658 BVAR (current_buffer, mode_line_format));
20659 ++n;
20662 if (WINDOW_WANTS_HEADER_LINE_P (w))
20664 display_mode_line (w, HEADER_LINE_FACE_ID,
20665 BVAR (current_buffer, header_line_format));
20666 ++n;
20669 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20670 selected_frame = old_selected_frame;
20671 selected_window = old_selected_window;
20672 return n;
20676 /* Display mode or header line of window W. FACE_ID specifies which
20677 line to display; it is either MODE_LINE_FACE_ID or
20678 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20679 display. Value is the pixel height of the mode/header line
20680 displayed. */
20682 static int
20683 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20685 struct it it;
20686 struct face *face;
20687 ptrdiff_t count = SPECPDL_INDEX ();
20689 init_iterator (&it, w, -1, -1, NULL, face_id);
20690 /* Don't extend on a previously drawn mode-line.
20691 This may happen if called from pos_visible_p. */
20692 it.glyph_row->enabled_p = 0;
20693 prepare_desired_row (it.glyph_row);
20695 it.glyph_row->mode_line_p = 1;
20697 /* FIXME: This should be controlled by a user option. But
20698 supporting such an option is not trivial, since the mode line is
20699 made up of many separate strings. */
20700 it.paragraph_embedding = L2R;
20702 record_unwind_protect (unwind_format_mode_line,
20703 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20705 mode_line_target = MODE_LINE_DISPLAY;
20707 /* Temporarily make frame's keyboard the current kboard so that
20708 kboard-local variables in the mode_line_format will get the right
20709 values. */
20710 push_kboard (FRAME_KBOARD (it.f));
20711 record_unwind_save_match_data ();
20712 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20713 pop_kboard ();
20715 unbind_to (count, Qnil);
20717 /* Fill up with spaces. */
20718 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20720 compute_line_metrics (&it);
20721 it.glyph_row->full_width_p = 1;
20722 it.glyph_row->continued_p = 0;
20723 it.glyph_row->truncated_on_left_p = 0;
20724 it.glyph_row->truncated_on_right_p = 0;
20726 /* Make a 3D mode-line have a shadow at its right end. */
20727 face = FACE_FROM_ID (it.f, face_id);
20728 extend_face_to_end_of_line (&it);
20729 if (face->box != FACE_NO_BOX)
20731 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20732 + it.glyph_row->used[TEXT_AREA] - 1);
20733 last->right_box_line_p = 1;
20736 return it.glyph_row->height;
20739 /* Move element ELT in LIST to the front of LIST.
20740 Return the updated list. */
20742 static Lisp_Object
20743 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20745 register Lisp_Object tail, prev;
20746 register Lisp_Object tem;
20748 tail = list;
20749 prev = Qnil;
20750 while (CONSP (tail))
20752 tem = XCAR (tail);
20754 if (EQ (elt, tem))
20756 /* Splice out the link TAIL. */
20757 if (NILP (prev))
20758 list = XCDR (tail);
20759 else
20760 Fsetcdr (prev, XCDR (tail));
20762 /* Now make it the first. */
20763 Fsetcdr (tail, list);
20764 return tail;
20766 else
20767 prev = tail;
20768 tail = XCDR (tail);
20769 QUIT;
20772 /* Not found--return unchanged LIST. */
20773 return list;
20776 /* Contribute ELT to the mode line for window IT->w. How it
20777 translates into text depends on its data type.
20779 IT describes the display environment in which we display, as usual.
20781 DEPTH is the depth in recursion. It is used to prevent
20782 infinite recursion here.
20784 FIELD_WIDTH is the number of characters the display of ELT should
20785 occupy in the mode line, and PRECISION is the maximum number of
20786 characters to display from ELT's representation. See
20787 display_string for details.
20789 Returns the hpos of the end of the text generated by ELT.
20791 PROPS is a property list to add to any string we encounter.
20793 If RISKY is nonzero, remove (disregard) any properties in any string
20794 we encounter, and ignore :eval and :propertize.
20796 The global variable `mode_line_target' determines whether the
20797 output is passed to `store_mode_line_noprop',
20798 `store_mode_line_string', or `display_string'. */
20800 static int
20801 display_mode_element (struct it *it, int depth, int field_width, int precision,
20802 Lisp_Object elt, Lisp_Object props, int risky)
20804 int n = 0, field, prec;
20805 int literal = 0;
20807 tail_recurse:
20808 if (depth > 100)
20809 elt = build_string ("*too-deep*");
20811 depth++;
20813 switch (XTYPE (elt))
20815 case Lisp_String:
20817 /* A string: output it and check for %-constructs within it. */
20818 unsigned char c;
20819 ptrdiff_t offset = 0;
20821 if (SCHARS (elt) > 0
20822 && (!NILP (props) || risky))
20824 Lisp_Object oprops, aelt;
20825 oprops = Ftext_properties_at (make_number (0), elt);
20827 /* If the starting string's properties are not what
20828 we want, translate the string. Also, if the string
20829 is risky, do that anyway. */
20831 if (NILP (Fequal (props, oprops)) || risky)
20833 /* If the starting string has properties,
20834 merge the specified ones onto the existing ones. */
20835 if (! NILP (oprops) && !risky)
20837 Lisp_Object tem;
20839 oprops = Fcopy_sequence (oprops);
20840 tem = props;
20841 while (CONSP (tem))
20843 oprops = Fplist_put (oprops, XCAR (tem),
20844 XCAR (XCDR (tem)));
20845 tem = XCDR (XCDR (tem));
20847 props = oprops;
20850 aelt = Fassoc (elt, mode_line_proptrans_alist);
20851 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20853 /* AELT is what we want. Move it to the front
20854 without consing. */
20855 elt = XCAR (aelt);
20856 mode_line_proptrans_alist
20857 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20859 else
20861 Lisp_Object tem;
20863 /* If AELT has the wrong props, it is useless.
20864 so get rid of it. */
20865 if (! NILP (aelt))
20866 mode_line_proptrans_alist
20867 = Fdelq (aelt, mode_line_proptrans_alist);
20869 elt = Fcopy_sequence (elt);
20870 Fset_text_properties (make_number (0), Flength (elt),
20871 props, elt);
20872 /* Add this item to mode_line_proptrans_alist. */
20873 mode_line_proptrans_alist
20874 = Fcons (Fcons (elt, props),
20875 mode_line_proptrans_alist);
20876 /* Truncate mode_line_proptrans_alist
20877 to at most 50 elements. */
20878 tem = Fnthcdr (make_number (50),
20879 mode_line_proptrans_alist);
20880 if (! NILP (tem))
20881 XSETCDR (tem, Qnil);
20886 offset = 0;
20888 if (literal)
20890 prec = precision - n;
20891 switch (mode_line_target)
20893 case MODE_LINE_NOPROP:
20894 case MODE_LINE_TITLE:
20895 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20896 break;
20897 case MODE_LINE_STRING:
20898 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20899 break;
20900 case MODE_LINE_DISPLAY:
20901 n += display_string (NULL, elt, Qnil, 0, 0, it,
20902 0, prec, 0, STRING_MULTIBYTE (elt));
20903 break;
20906 break;
20909 /* Handle the non-literal case. */
20911 while ((precision <= 0 || n < precision)
20912 && SREF (elt, offset) != 0
20913 && (mode_line_target != MODE_LINE_DISPLAY
20914 || it->current_x < it->last_visible_x))
20916 ptrdiff_t last_offset = offset;
20918 /* Advance to end of string or next format specifier. */
20919 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20922 if (offset - 1 != last_offset)
20924 ptrdiff_t nchars, nbytes;
20926 /* Output to end of string or up to '%'. Field width
20927 is length of string. Don't output more than
20928 PRECISION allows us. */
20929 offset--;
20931 prec = c_string_width (SDATA (elt) + last_offset,
20932 offset - last_offset, precision - n,
20933 &nchars, &nbytes);
20935 switch (mode_line_target)
20937 case MODE_LINE_NOPROP:
20938 case MODE_LINE_TITLE:
20939 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20940 break;
20941 case MODE_LINE_STRING:
20943 ptrdiff_t bytepos = last_offset;
20944 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20945 ptrdiff_t endpos = (precision <= 0
20946 ? string_byte_to_char (elt, offset)
20947 : charpos + nchars);
20949 n += store_mode_line_string (NULL,
20950 Fsubstring (elt, make_number (charpos),
20951 make_number (endpos)),
20952 0, 0, 0, Qnil);
20954 break;
20955 case MODE_LINE_DISPLAY:
20957 ptrdiff_t bytepos = last_offset;
20958 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20960 if (precision <= 0)
20961 nchars = string_byte_to_char (elt, offset) - charpos;
20962 n += display_string (NULL, elt, Qnil, 0, charpos,
20963 it, 0, nchars, 0,
20964 STRING_MULTIBYTE (elt));
20966 break;
20969 else /* c == '%' */
20971 ptrdiff_t percent_position = offset;
20973 /* Get the specified minimum width. Zero means
20974 don't pad. */
20975 field = 0;
20976 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20977 field = field * 10 + c - '0';
20979 /* Don't pad beyond the total padding allowed. */
20980 if (field_width - n > 0 && field > field_width - n)
20981 field = field_width - n;
20983 /* Note that either PRECISION <= 0 or N < PRECISION. */
20984 prec = precision - n;
20986 if (c == 'M')
20987 n += display_mode_element (it, depth, field, prec,
20988 Vglobal_mode_string, props,
20989 risky);
20990 else if (c != 0)
20992 bool multibyte;
20993 ptrdiff_t bytepos, charpos;
20994 const char *spec;
20995 Lisp_Object string;
20997 bytepos = percent_position;
20998 charpos = (STRING_MULTIBYTE (elt)
20999 ? string_byte_to_char (elt, bytepos)
21000 : bytepos);
21001 spec = decode_mode_spec (it->w, c, field, &string);
21002 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21004 switch (mode_line_target)
21006 case MODE_LINE_NOPROP:
21007 case MODE_LINE_TITLE:
21008 n += store_mode_line_noprop (spec, field, prec);
21009 break;
21010 case MODE_LINE_STRING:
21012 Lisp_Object tem = build_string (spec);
21013 props = Ftext_properties_at (make_number (charpos), elt);
21014 /* Should only keep face property in props */
21015 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21017 break;
21018 case MODE_LINE_DISPLAY:
21020 int nglyphs_before, nwritten;
21022 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21023 nwritten = display_string (spec, string, elt,
21024 charpos, 0, it,
21025 field, prec, 0,
21026 multibyte);
21028 /* Assign to the glyphs written above the
21029 string where the `%x' came from, position
21030 of the `%'. */
21031 if (nwritten > 0)
21033 struct glyph *glyph
21034 = (it->glyph_row->glyphs[TEXT_AREA]
21035 + nglyphs_before);
21036 int i;
21038 for (i = 0; i < nwritten; ++i)
21040 glyph[i].object = elt;
21041 glyph[i].charpos = charpos;
21044 n += nwritten;
21047 break;
21050 else /* c == 0 */
21051 break;
21055 break;
21057 case Lisp_Symbol:
21058 /* A symbol: process the value of the symbol recursively
21059 as if it appeared here directly. Avoid error if symbol void.
21060 Special case: if value of symbol is a string, output the string
21061 literally. */
21063 register Lisp_Object tem;
21065 /* If the variable is not marked as risky to set
21066 then its contents are risky to use. */
21067 if (NILP (Fget (elt, Qrisky_local_variable)))
21068 risky = 1;
21070 tem = Fboundp (elt);
21071 if (!NILP (tem))
21073 tem = Fsymbol_value (elt);
21074 /* If value is a string, output that string literally:
21075 don't check for % within it. */
21076 if (STRINGP (tem))
21077 literal = 1;
21079 if (!EQ (tem, elt))
21081 /* Give up right away for nil or t. */
21082 elt = tem;
21083 goto tail_recurse;
21087 break;
21089 case Lisp_Cons:
21091 register Lisp_Object car, tem;
21093 /* A cons cell: five distinct cases.
21094 If first element is :eval or :propertize, do something special.
21095 If first element is a string or a cons, process all the elements
21096 and effectively concatenate them.
21097 If first element is a negative number, truncate displaying cdr to
21098 at most that many characters. If positive, pad (with spaces)
21099 to at least that many characters.
21100 If first element is a symbol, process the cadr or caddr recursively
21101 according to whether the symbol's value is non-nil or nil. */
21102 car = XCAR (elt);
21103 if (EQ (car, QCeval))
21105 /* An element of the form (:eval FORM) means evaluate FORM
21106 and use the result as mode line elements. */
21108 if (risky)
21109 break;
21111 if (CONSP (XCDR (elt)))
21113 Lisp_Object spec;
21114 spec = safe_eval (XCAR (XCDR (elt)));
21115 n += display_mode_element (it, depth, field_width - n,
21116 precision - n, spec, props,
21117 risky);
21120 else if (EQ (car, QCpropertize))
21122 /* An element of the form (:propertize ELT PROPS...)
21123 means display ELT but applying properties PROPS. */
21125 if (risky)
21126 break;
21128 if (CONSP (XCDR (elt)))
21129 n += display_mode_element (it, depth, field_width - n,
21130 precision - n, XCAR (XCDR (elt)),
21131 XCDR (XCDR (elt)), risky);
21133 else if (SYMBOLP (car))
21135 tem = Fboundp (car);
21136 elt = XCDR (elt);
21137 if (!CONSP (elt))
21138 goto invalid;
21139 /* elt is now the cdr, and we know it is a cons cell.
21140 Use its car if CAR has a non-nil value. */
21141 if (!NILP (tem))
21143 tem = Fsymbol_value (car);
21144 if (!NILP (tem))
21146 elt = XCAR (elt);
21147 goto tail_recurse;
21150 /* Symbol's value is nil (or symbol is unbound)
21151 Get the cddr of the original list
21152 and if possible find the caddr and use that. */
21153 elt = XCDR (elt);
21154 if (NILP (elt))
21155 break;
21156 else if (!CONSP (elt))
21157 goto invalid;
21158 elt = XCAR (elt);
21159 goto tail_recurse;
21161 else if (INTEGERP (car))
21163 register int lim = XINT (car);
21164 elt = XCDR (elt);
21165 if (lim < 0)
21167 /* Negative int means reduce maximum width. */
21168 if (precision <= 0)
21169 precision = -lim;
21170 else
21171 precision = min (precision, -lim);
21173 else if (lim > 0)
21175 /* Padding specified. Don't let it be more than
21176 current maximum. */
21177 if (precision > 0)
21178 lim = min (precision, lim);
21180 /* If that's more padding than already wanted, queue it.
21181 But don't reduce padding already specified even if
21182 that is beyond the current truncation point. */
21183 field_width = max (lim, field_width);
21185 goto tail_recurse;
21187 else if (STRINGP (car) || CONSP (car))
21189 Lisp_Object halftail = elt;
21190 int len = 0;
21192 while (CONSP (elt)
21193 && (precision <= 0 || n < precision))
21195 n += display_mode_element (it, depth,
21196 /* Do padding only after the last
21197 element in the list. */
21198 (! CONSP (XCDR (elt))
21199 ? field_width - n
21200 : 0),
21201 precision - n, XCAR (elt),
21202 props, risky);
21203 elt = XCDR (elt);
21204 len++;
21205 if ((len & 1) == 0)
21206 halftail = XCDR (halftail);
21207 /* Check for cycle. */
21208 if (EQ (halftail, elt))
21209 break;
21213 break;
21215 default:
21216 invalid:
21217 elt = build_string ("*invalid*");
21218 goto tail_recurse;
21221 /* Pad to FIELD_WIDTH. */
21222 if (field_width > 0 && n < field_width)
21224 switch (mode_line_target)
21226 case MODE_LINE_NOPROP:
21227 case MODE_LINE_TITLE:
21228 n += store_mode_line_noprop ("", field_width - n, 0);
21229 break;
21230 case MODE_LINE_STRING:
21231 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21232 break;
21233 case MODE_LINE_DISPLAY:
21234 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21235 0, 0, 0);
21236 break;
21240 return n;
21243 /* Store a mode-line string element in mode_line_string_list.
21245 If STRING is non-null, display that C string. Otherwise, the Lisp
21246 string LISP_STRING is displayed.
21248 FIELD_WIDTH is the minimum number of output glyphs to produce.
21249 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21250 with spaces. FIELD_WIDTH <= 0 means don't pad.
21252 PRECISION is the maximum number of characters to output from
21253 STRING. PRECISION <= 0 means don't truncate the string.
21255 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21256 properties to the string.
21258 PROPS are the properties to add to the string.
21259 The mode_line_string_face face property is always added to the string.
21262 static int
21263 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21264 int field_width, int precision, Lisp_Object props)
21266 ptrdiff_t len;
21267 int n = 0;
21269 if (string != NULL)
21271 len = strlen (string);
21272 if (precision > 0 && len > precision)
21273 len = precision;
21274 lisp_string = make_string (string, len);
21275 if (NILP (props))
21276 props = mode_line_string_face_prop;
21277 else if (!NILP (mode_line_string_face))
21279 Lisp_Object face = Fplist_get (props, Qface);
21280 props = Fcopy_sequence (props);
21281 if (NILP (face))
21282 face = mode_line_string_face;
21283 else
21284 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21285 props = Fplist_put (props, Qface, face);
21287 Fadd_text_properties (make_number (0), make_number (len),
21288 props, lisp_string);
21290 else
21292 len = XFASTINT (Flength (lisp_string));
21293 if (precision > 0 && len > precision)
21295 len = precision;
21296 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21297 precision = -1;
21299 if (!NILP (mode_line_string_face))
21301 Lisp_Object face;
21302 if (NILP (props))
21303 props = Ftext_properties_at (make_number (0), lisp_string);
21304 face = Fplist_get (props, Qface);
21305 if (NILP (face))
21306 face = mode_line_string_face;
21307 else
21308 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21309 props = Fcons (Qface, Fcons (face, Qnil));
21310 if (copy_string)
21311 lisp_string = Fcopy_sequence (lisp_string);
21313 if (!NILP (props))
21314 Fadd_text_properties (make_number (0), make_number (len),
21315 props, lisp_string);
21318 if (len > 0)
21320 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21321 n += len;
21324 if (field_width > len)
21326 field_width -= len;
21327 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21328 if (!NILP (props))
21329 Fadd_text_properties (make_number (0), make_number (field_width),
21330 props, lisp_string);
21331 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21332 n += field_width;
21335 return n;
21339 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21340 1, 4, 0,
21341 doc: /* Format a string out of a mode line format specification.
21342 First arg FORMAT specifies the mode line format (see `mode-line-format'
21343 for details) to use.
21345 By default, the format is evaluated for the currently selected window.
21347 Optional second arg FACE specifies the face property to put on all
21348 characters for which no face is specified. The value nil means the
21349 default face. The value t means whatever face the window's mode line
21350 currently uses (either `mode-line' or `mode-line-inactive',
21351 depending on whether the window is the selected window or not).
21352 An integer value means the value string has no text
21353 properties.
21355 Optional third and fourth args WINDOW and BUFFER specify the window
21356 and buffer to use as the context for the formatting (defaults
21357 are the selected window and the WINDOW's buffer). */)
21358 (Lisp_Object format, Lisp_Object face,
21359 Lisp_Object window, Lisp_Object buffer)
21361 struct it it;
21362 int len;
21363 struct window *w;
21364 struct buffer *old_buffer = NULL;
21365 int face_id;
21366 int no_props = INTEGERP (face);
21367 ptrdiff_t count = SPECPDL_INDEX ();
21368 Lisp_Object str;
21369 int string_start = 0;
21371 w = decode_any_window (window);
21372 XSETWINDOW (window, w);
21374 if (NILP (buffer))
21375 buffer = w->contents;
21376 CHECK_BUFFER (buffer);
21378 /* Make formatting the modeline a non-op when noninteractive, otherwise
21379 there will be problems later caused by a partially initialized frame. */
21380 if (NILP (format) || noninteractive)
21381 return empty_unibyte_string;
21383 if (no_props)
21384 face = Qnil;
21386 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21387 : EQ (face, Qt) ? (EQ (window, selected_window)
21388 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21389 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21390 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21391 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21392 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21393 : DEFAULT_FACE_ID;
21395 old_buffer = current_buffer;
21397 /* Save things including mode_line_proptrans_alist,
21398 and set that to nil so that we don't alter the outer value. */
21399 record_unwind_protect (unwind_format_mode_line,
21400 format_mode_line_unwind_data
21401 (XFRAME (WINDOW_FRAME (w)),
21402 old_buffer, selected_window, 1));
21403 mode_line_proptrans_alist = Qnil;
21405 Fselect_window (window, Qt);
21406 set_buffer_internal_1 (XBUFFER (buffer));
21408 init_iterator (&it, w, -1, -1, NULL, face_id);
21410 if (no_props)
21412 mode_line_target = MODE_LINE_NOPROP;
21413 mode_line_string_face_prop = Qnil;
21414 mode_line_string_list = Qnil;
21415 string_start = MODE_LINE_NOPROP_LEN (0);
21417 else
21419 mode_line_target = MODE_LINE_STRING;
21420 mode_line_string_list = Qnil;
21421 mode_line_string_face = face;
21422 mode_line_string_face_prop
21423 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21426 push_kboard (FRAME_KBOARD (it.f));
21427 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21428 pop_kboard ();
21430 if (no_props)
21432 len = MODE_LINE_NOPROP_LEN (string_start);
21433 str = make_string (mode_line_noprop_buf + string_start, len);
21435 else
21437 mode_line_string_list = Fnreverse (mode_line_string_list);
21438 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21439 empty_unibyte_string);
21442 unbind_to (count, Qnil);
21443 return str;
21446 /* Write a null-terminated, right justified decimal representation of
21447 the positive integer D to BUF using a minimal field width WIDTH. */
21449 static void
21450 pint2str (register char *buf, register int width, register ptrdiff_t d)
21452 register char *p = buf;
21454 if (d <= 0)
21455 *p++ = '0';
21456 else
21458 while (d > 0)
21460 *p++ = d % 10 + '0';
21461 d /= 10;
21465 for (width -= (int) (p - buf); width > 0; --width)
21466 *p++ = ' ';
21467 *p-- = '\0';
21468 while (p > buf)
21470 d = *buf;
21471 *buf++ = *p;
21472 *p-- = d;
21476 /* Write a null-terminated, right justified decimal and "human
21477 readable" representation of the nonnegative integer D to BUF using
21478 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21480 static const char power_letter[] =
21482 0, /* no letter */
21483 'k', /* kilo */
21484 'M', /* mega */
21485 'G', /* giga */
21486 'T', /* tera */
21487 'P', /* peta */
21488 'E', /* exa */
21489 'Z', /* zetta */
21490 'Y' /* yotta */
21493 static void
21494 pint2hrstr (char *buf, int width, ptrdiff_t d)
21496 /* We aim to represent the nonnegative integer D as
21497 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21498 ptrdiff_t quotient = d;
21499 int remainder = 0;
21500 /* -1 means: do not use TENTHS. */
21501 int tenths = -1;
21502 int exponent = 0;
21504 /* Length of QUOTIENT.TENTHS as a string. */
21505 int length;
21507 char * psuffix;
21508 char * p;
21510 if (quotient >= 1000)
21512 /* Scale to the appropriate EXPONENT. */
21515 remainder = quotient % 1000;
21516 quotient /= 1000;
21517 exponent++;
21519 while (quotient >= 1000);
21521 /* Round to nearest and decide whether to use TENTHS or not. */
21522 if (quotient <= 9)
21524 tenths = remainder / 100;
21525 if (remainder % 100 >= 50)
21527 if (tenths < 9)
21528 tenths++;
21529 else
21531 quotient++;
21532 if (quotient == 10)
21533 tenths = -1;
21534 else
21535 tenths = 0;
21539 else
21540 if (remainder >= 500)
21542 if (quotient < 999)
21543 quotient++;
21544 else
21546 quotient = 1;
21547 exponent++;
21548 tenths = 0;
21553 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21554 if (tenths == -1 && quotient <= 99)
21555 if (quotient <= 9)
21556 length = 1;
21557 else
21558 length = 2;
21559 else
21560 length = 3;
21561 p = psuffix = buf + max (width, length);
21563 /* Print EXPONENT. */
21564 *psuffix++ = power_letter[exponent];
21565 *psuffix = '\0';
21567 /* Print TENTHS. */
21568 if (tenths >= 0)
21570 *--p = '0' + tenths;
21571 *--p = '.';
21574 /* Print QUOTIENT. */
21577 int digit = quotient % 10;
21578 *--p = '0' + digit;
21580 while ((quotient /= 10) != 0);
21582 /* Print leading spaces. */
21583 while (buf < p)
21584 *--p = ' ';
21587 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21588 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21589 type of CODING_SYSTEM. Return updated pointer into BUF. */
21591 static unsigned char invalid_eol_type[] = "(*invalid*)";
21593 static char *
21594 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21596 Lisp_Object val;
21597 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21598 const unsigned char *eol_str;
21599 int eol_str_len;
21600 /* The EOL conversion we are using. */
21601 Lisp_Object eoltype;
21603 val = CODING_SYSTEM_SPEC (coding_system);
21604 eoltype = Qnil;
21606 if (!VECTORP (val)) /* Not yet decided. */
21608 *buf++ = multibyte ? '-' : ' ';
21609 if (eol_flag)
21610 eoltype = eol_mnemonic_undecided;
21611 /* Don't mention EOL conversion if it isn't decided. */
21613 else
21615 Lisp_Object attrs;
21616 Lisp_Object eolvalue;
21618 attrs = AREF (val, 0);
21619 eolvalue = AREF (val, 2);
21621 *buf++ = multibyte
21622 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21623 : ' ';
21625 if (eol_flag)
21627 /* The EOL conversion that is normal on this system. */
21629 if (NILP (eolvalue)) /* Not yet decided. */
21630 eoltype = eol_mnemonic_undecided;
21631 else if (VECTORP (eolvalue)) /* Not yet decided. */
21632 eoltype = eol_mnemonic_undecided;
21633 else /* eolvalue is Qunix, Qdos, or Qmac. */
21634 eoltype = (EQ (eolvalue, Qunix)
21635 ? eol_mnemonic_unix
21636 : (EQ (eolvalue, Qdos) == 1
21637 ? eol_mnemonic_dos : eol_mnemonic_mac));
21641 if (eol_flag)
21643 /* Mention the EOL conversion if it is not the usual one. */
21644 if (STRINGP (eoltype))
21646 eol_str = SDATA (eoltype);
21647 eol_str_len = SBYTES (eoltype);
21649 else if (CHARACTERP (eoltype))
21651 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21652 int c = XFASTINT (eoltype);
21653 eol_str_len = CHAR_STRING (c, tmp);
21654 eol_str = tmp;
21656 else
21658 eol_str = invalid_eol_type;
21659 eol_str_len = sizeof (invalid_eol_type) - 1;
21661 memcpy (buf, eol_str, eol_str_len);
21662 buf += eol_str_len;
21665 return buf;
21668 /* Return a string for the output of a mode line %-spec for window W,
21669 generated by character C. FIELD_WIDTH > 0 means pad the string
21670 returned with spaces to that value. Return a Lisp string in
21671 *STRING if the resulting string is taken from that Lisp string.
21673 Note we operate on the current buffer for most purposes. */
21675 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21677 static const char *
21678 decode_mode_spec (struct window *w, register int c, int field_width,
21679 Lisp_Object *string)
21681 Lisp_Object obj;
21682 struct frame *f = XFRAME (WINDOW_FRAME (w));
21683 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21684 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21685 produce strings from numerical values, so limit preposterously
21686 large values of FIELD_WIDTH to avoid overrunning the buffer's
21687 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21688 bytes plus the terminating null. */
21689 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21690 struct buffer *b = current_buffer;
21692 obj = Qnil;
21693 *string = Qnil;
21695 switch (c)
21697 case '*':
21698 if (!NILP (BVAR (b, read_only)))
21699 return "%";
21700 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21701 return "*";
21702 return "-";
21704 case '+':
21705 /* This differs from %* only for a modified read-only buffer. */
21706 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21707 return "*";
21708 if (!NILP (BVAR (b, read_only)))
21709 return "%";
21710 return "-";
21712 case '&':
21713 /* This differs from %* in ignoring read-only-ness. */
21714 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21715 return "*";
21716 return "-";
21718 case '%':
21719 return "%";
21721 case '[':
21723 int i;
21724 char *p;
21726 if (command_loop_level > 5)
21727 return "[[[... ";
21728 p = decode_mode_spec_buf;
21729 for (i = 0; i < command_loop_level; i++)
21730 *p++ = '[';
21731 *p = 0;
21732 return decode_mode_spec_buf;
21735 case ']':
21737 int i;
21738 char *p;
21740 if (command_loop_level > 5)
21741 return " ...]]]";
21742 p = decode_mode_spec_buf;
21743 for (i = 0; i < command_loop_level; i++)
21744 *p++ = ']';
21745 *p = 0;
21746 return decode_mode_spec_buf;
21749 case '-':
21751 register int i;
21753 /* Let lots_of_dashes be a string of infinite length. */
21754 if (mode_line_target == MODE_LINE_NOPROP
21755 || mode_line_target == MODE_LINE_STRING)
21756 return "--";
21757 if (field_width <= 0
21758 || field_width > sizeof (lots_of_dashes))
21760 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21761 decode_mode_spec_buf[i] = '-';
21762 decode_mode_spec_buf[i] = '\0';
21763 return decode_mode_spec_buf;
21765 else
21766 return lots_of_dashes;
21769 case 'b':
21770 obj = BVAR (b, name);
21771 break;
21773 case 'c':
21774 /* %c and %l are ignored in `frame-title-format'.
21775 (In redisplay_internal, the frame title is drawn _before_ the
21776 windows are updated, so the stuff which depends on actual
21777 window contents (such as %l) may fail to render properly, or
21778 even crash emacs.) */
21779 if (mode_line_target == MODE_LINE_TITLE)
21780 return "";
21781 else
21783 ptrdiff_t col = current_column ();
21784 w->column_number_displayed = col;
21785 pint2str (decode_mode_spec_buf, width, col);
21786 return decode_mode_spec_buf;
21789 case 'e':
21790 #ifndef SYSTEM_MALLOC
21792 if (NILP (Vmemory_full))
21793 return "";
21794 else
21795 return "!MEM FULL! ";
21797 #else
21798 return "";
21799 #endif
21801 case 'F':
21802 /* %F displays the frame name. */
21803 if (!NILP (f->title))
21804 return SSDATA (f->title);
21805 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21806 return SSDATA (f->name);
21807 return "Emacs";
21809 case 'f':
21810 obj = BVAR (b, filename);
21811 break;
21813 case 'i':
21815 ptrdiff_t size = ZV - BEGV;
21816 pint2str (decode_mode_spec_buf, width, size);
21817 return decode_mode_spec_buf;
21820 case 'I':
21822 ptrdiff_t size = ZV - BEGV;
21823 pint2hrstr (decode_mode_spec_buf, width, size);
21824 return decode_mode_spec_buf;
21827 case 'l':
21829 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21830 ptrdiff_t topline, nlines, height;
21831 ptrdiff_t junk;
21833 /* %c and %l are ignored in `frame-title-format'. */
21834 if (mode_line_target == MODE_LINE_TITLE)
21835 return "";
21837 startpos = marker_position (w->start);
21838 startpos_byte = marker_byte_position (w->start);
21839 height = WINDOW_TOTAL_LINES (w);
21841 /* If we decided that this buffer isn't suitable for line numbers,
21842 don't forget that too fast. */
21843 if (w->base_line_pos == -1)
21844 goto no_value;
21846 /* If the buffer is very big, don't waste time. */
21847 if (INTEGERP (Vline_number_display_limit)
21848 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21850 w->base_line_pos = 0;
21851 w->base_line_number = 0;
21852 goto no_value;
21855 if (w->base_line_number > 0
21856 && w->base_line_pos > 0
21857 && w->base_line_pos <= startpos)
21859 line = w->base_line_number;
21860 linepos = w->base_line_pos;
21861 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21863 else
21865 line = 1;
21866 linepos = BUF_BEGV (b);
21867 linepos_byte = BUF_BEGV_BYTE (b);
21870 /* Count lines from base line to window start position. */
21871 nlines = display_count_lines (linepos_byte,
21872 startpos_byte,
21873 startpos, &junk);
21875 topline = nlines + line;
21877 /* Determine a new base line, if the old one is too close
21878 or too far away, or if we did not have one.
21879 "Too close" means it's plausible a scroll-down would
21880 go back past it. */
21881 if (startpos == BUF_BEGV (b))
21883 w->base_line_number = topline;
21884 w->base_line_pos = BUF_BEGV (b);
21886 else if (nlines < height + 25 || nlines > height * 3 + 50
21887 || linepos == BUF_BEGV (b))
21889 ptrdiff_t limit = BUF_BEGV (b);
21890 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21891 ptrdiff_t position;
21892 ptrdiff_t distance =
21893 (height * 2 + 30) * line_number_display_limit_width;
21895 if (startpos - distance > limit)
21897 limit = startpos - distance;
21898 limit_byte = CHAR_TO_BYTE (limit);
21901 nlines = display_count_lines (startpos_byte,
21902 limit_byte,
21903 - (height * 2 + 30),
21904 &position);
21905 /* If we couldn't find the lines we wanted within
21906 line_number_display_limit_width chars per line,
21907 give up on line numbers for this window. */
21908 if (position == limit_byte && limit == startpos - distance)
21910 w->base_line_pos = -1;
21911 w->base_line_number = 0;
21912 goto no_value;
21915 w->base_line_number = topline - nlines;
21916 w->base_line_pos = BYTE_TO_CHAR (position);
21919 /* Now count lines from the start pos to point. */
21920 nlines = display_count_lines (startpos_byte,
21921 PT_BYTE, PT, &junk);
21923 /* Record that we did display the line number. */
21924 line_number_displayed = 1;
21926 /* Make the string to show. */
21927 pint2str (decode_mode_spec_buf, width, topline + nlines);
21928 return decode_mode_spec_buf;
21929 no_value:
21931 char* p = decode_mode_spec_buf;
21932 int pad = width - 2;
21933 while (pad-- > 0)
21934 *p++ = ' ';
21935 *p++ = '?';
21936 *p++ = '?';
21937 *p = '\0';
21938 return decode_mode_spec_buf;
21941 break;
21943 case 'm':
21944 obj = BVAR (b, mode_name);
21945 break;
21947 case 'n':
21948 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21949 return " Narrow";
21950 break;
21952 case 'p':
21954 ptrdiff_t pos = marker_position (w->start);
21955 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21957 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21959 if (pos <= BUF_BEGV (b))
21960 return "All";
21961 else
21962 return "Bottom";
21964 else if (pos <= BUF_BEGV (b))
21965 return "Top";
21966 else
21968 if (total > 1000000)
21969 /* Do it differently for a large value, to avoid overflow. */
21970 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21971 else
21972 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21973 /* We can't normally display a 3-digit number,
21974 so get us a 2-digit number that is close. */
21975 if (total == 100)
21976 total = 99;
21977 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21978 return decode_mode_spec_buf;
21982 /* Display percentage of size above the bottom of the screen. */
21983 case 'P':
21985 ptrdiff_t toppos = marker_position (w->start);
21986 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21987 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21989 if (botpos >= BUF_ZV (b))
21991 if (toppos <= BUF_BEGV (b))
21992 return "All";
21993 else
21994 return "Bottom";
21996 else
21998 if (total > 1000000)
21999 /* Do it differently for a large value, to avoid overflow. */
22000 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22001 else
22002 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22003 /* We can't normally display a 3-digit number,
22004 so get us a 2-digit number that is close. */
22005 if (total == 100)
22006 total = 99;
22007 if (toppos <= BUF_BEGV (b))
22008 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22009 else
22010 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22011 return decode_mode_spec_buf;
22015 case 's':
22016 /* status of process */
22017 obj = Fget_buffer_process (Fcurrent_buffer ());
22018 if (NILP (obj))
22019 return "no process";
22020 #ifndef MSDOS
22021 obj = Fsymbol_name (Fprocess_status (obj));
22022 #endif
22023 break;
22025 case '@':
22027 ptrdiff_t count = inhibit_garbage_collection ();
22028 Lisp_Object val = call1 (intern ("file-remote-p"),
22029 BVAR (current_buffer, directory));
22030 unbind_to (count, Qnil);
22032 if (NILP (val))
22033 return "-";
22034 else
22035 return "@";
22038 case 'z':
22039 /* coding-system (not including end-of-line format) */
22040 case 'Z':
22041 /* coding-system (including end-of-line type) */
22043 int eol_flag = (c == 'Z');
22044 char *p = decode_mode_spec_buf;
22046 if (! FRAME_WINDOW_P (f))
22048 /* No need to mention EOL here--the terminal never needs
22049 to do EOL conversion. */
22050 p = decode_mode_spec_coding (CODING_ID_NAME
22051 (FRAME_KEYBOARD_CODING (f)->id),
22052 p, 0);
22053 p = decode_mode_spec_coding (CODING_ID_NAME
22054 (FRAME_TERMINAL_CODING (f)->id),
22055 p, 0);
22057 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22058 p, eol_flag);
22060 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22061 #ifdef subprocesses
22062 obj = Fget_buffer_process (Fcurrent_buffer ());
22063 if (PROCESSP (obj))
22065 p = decode_mode_spec_coding
22066 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22067 p = decode_mode_spec_coding
22068 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22070 #endif /* subprocesses */
22071 #endif /* 0 */
22072 *p = 0;
22073 return decode_mode_spec_buf;
22077 if (STRINGP (obj))
22079 *string = obj;
22080 return SSDATA (obj);
22082 else
22083 return "";
22087 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22088 means count lines back from START_BYTE. But don't go beyond
22089 LIMIT_BYTE. Return the number of lines thus found (always
22090 nonnegative).
22092 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22093 either the position COUNT lines after/before START_BYTE, if we
22094 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22095 COUNT lines. */
22097 static ptrdiff_t
22098 display_count_lines (ptrdiff_t start_byte,
22099 ptrdiff_t limit_byte, ptrdiff_t count,
22100 ptrdiff_t *byte_pos_ptr)
22102 register unsigned char *cursor;
22103 unsigned char *base;
22105 register ptrdiff_t ceiling;
22106 register unsigned char *ceiling_addr;
22107 ptrdiff_t orig_count = count;
22109 /* If we are not in selective display mode,
22110 check only for newlines. */
22111 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22112 && !INTEGERP (BVAR (current_buffer, selective_display)));
22114 if (count > 0)
22116 while (start_byte < limit_byte)
22118 ceiling = BUFFER_CEILING_OF (start_byte);
22119 ceiling = min (limit_byte - 1, ceiling);
22120 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22121 base = (cursor = BYTE_POS_ADDR (start_byte));
22125 if (selective_display)
22127 while (*cursor != '\n' && *cursor != 015
22128 && ++cursor != ceiling_addr)
22129 continue;
22130 if (cursor == ceiling_addr)
22131 break;
22133 else
22135 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22136 if (! cursor)
22137 break;
22140 cursor++;
22142 if (--count == 0)
22144 start_byte += cursor - base;
22145 *byte_pos_ptr = start_byte;
22146 return orig_count;
22149 while (cursor < ceiling_addr);
22151 start_byte += ceiling_addr - base;
22154 else
22156 while (start_byte > limit_byte)
22158 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22159 ceiling = max (limit_byte, ceiling);
22160 ceiling_addr = BYTE_POS_ADDR (ceiling);
22161 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22162 while (1)
22164 if (selective_display)
22166 while (--cursor >= ceiling_addr
22167 && *cursor != '\n' && *cursor != 015)
22168 continue;
22169 if (cursor < ceiling_addr)
22170 break;
22172 else
22174 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22175 if (! cursor)
22176 break;
22179 if (++count == 0)
22181 start_byte += cursor - base + 1;
22182 *byte_pos_ptr = start_byte;
22183 /* When scanning backwards, we should
22184 not count the newline posterior to which we stop. */
22185 return - orig_count - 1;
22188 start_byte += ceiling_addr - base;
22192 *byte_pos_ptr = limit_byte;
22194 if (count < 0)
22195 return - orig_count + count;
22196 return orig_count - count;
22202 /***********************************************************************
22203 Displaying strings
22204 ***********************************************************************/
22206 /* Display a NUL-terminated string, starting with index START.
22208 If STRING is non-null, display that C string. Otherwise, the Lisp
22209 string LISP_STRING is displayed. There's a case that STRING is
22210 non-null and LISP_STRING is not nil. It means STRING is a string
22211 data of LISP_STRING. In that case, we display LISP_STRING while
22212 ignoring its text properties.
22214 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22215 FACE_STRING. Display STRING or LISP_STRING with the face at
22216 FACE_STRING_POS in FACE_STRING:
22218 Display the string in the environment given by IT, but use the
22219 standard display table, temporarily.
22221 FIELD_WIDTH is the minimum number of output glyphs to produce.
22222 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22223 with spaces. If STRING has more characters, more than FIELD_WIDTH
22224 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22226 PRECISION is the maximum number of characters to output from
22227 STRING. PRECISION < 0 means don't truncate the string.
22229 This is roughly equivalent to printf format specifiers:
22231 FIELD_WIDTH PRECISION PRINTF
22232 ----------------------------------------
22233 -1 -1 %s
22234 -1 10 %.10s
22235 10 -1 %10s
22236 20 10 %20.10s
22238 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22239 display them, and < 0 means obey the current buffer's value of
22240 enable_multibyte_characters.
22242 Value is the number of columns displayed. */
22244 static int
22245 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22246 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22247 int field_width, int precision, int max_x, int multibyte)
22249 int hpos_at_start = it->hpos;
22250 int saved_face_id = it->face_id;
22251 struct glyph_row *row = it->glyph_row;
22252 ptrdiff_t it_charpos;
22254 /* Initialize the iterator IT for iteration over STRING beginning
22255 with index START. */
22256 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22257 precision, field_width, multibyte);
22258 if (string && STRINGP (lisp_string))
22259 /* LISP_STRING is the one returned by decode_mode_spec. We should
22260 ignore its text properties. */
22261 it->stop_charpos = it->end_charpos;
22263 /* If displaying STRING, set up the face of the iterator from
22264 FACE_STRING, if that's given. */
22265 if (STRINGP (face_string))
22267 ptrdiff_t endptr;
22268 struct face *face;
22270 it->face_id
22271 = face_at_string_position (it->w, face_string, face_string_pos,
22272 0, it->region_beg_charpos,
22273 it->region_end_charpos,
22274 &endptr, it->base_face_id, 0);
22275 face = FACE_FROM_ID (it->f, it->face_id);
22276 it->face_box_p = face->box != FACE_NO_BOX;
22279 /* Set max_x to the maximum allowed X position. Don't let it go
22280 beyond the right edge of the window. */
22281 if (max_x <= 0)
22282 max_x = it->last_visible_x;
22283 else
22284 max_x = min (max_x, it->last_visible_x);
22286 /* Skip over display elements that are not visible. because IT->w is
22287 hscrolled. */
22288 if (it->current_x < it->first_visible_x)
22289 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22290 MOVE_TO_POS | MOVE_TO_X);
22292 row->ascent = it->max_ascent;
22293 row->height = it->max_ascent + it->max_descent;
22294 row->phys_ascent = it->max_phys_ascent;
22295 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22296 row->extra_line_spacing = it->max_extra_line_spacing;
22298 if (STRINGP (it->string))
22299 it_charpos = IT_STRING_CHARPOS (*it);
22300 else
22301 it_charpos = IT_CHARPOS (*it);
22303 /* This condition is for the case that we are called with current_x
22304 past last_visible_x. */
22305 while (it->current_x < max_x)
22307 int x_before, x, n_glyphs_before, i, nglyphs;
22309 /* Get the next display element. */
22310 if (!get_next_display_element (it))
22311 break;
22313 /* Produce glyphs. */
22314 x_before = it->current_x;
22315 n_glyphs_before = row->used[TEXT_AREA];
22316 PRODUCE_GLYPHS (it);
22318 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22319 i = 0;
22320 x = x_before;
22321 while (i < nglyphs)
22323 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22325 if (it->line_wrap != TRUNCATE
22326 && x + glyph->pixel_width > max_x)
22328 /* End of continued line or max_x reached. */
22329 if (CHAR_GLYPH_PADDING_P (*glyph))
22331 /* A wide character is unbreakable. */
22332 if (row->reversed_p)
22333 unproduce_glyphs (it, row->used[TEXT_AREA]
22334 - n_glyphs_before);
22335 row->used[TEXT_AREA] = n_glyphs_before;
22336 it->current_x = x_before;
22338 else
22340 if (row->reversed_p)
22341 unproduce_glyphs (it, row->used[TEXT_AREA]
22342 - (n_glyphs_before + i));
22343 row->used[TEXT_AREA] = n_glyphs_before + i;
22344 it->current_x = x;
22346 break;
22348 else if (x + glyph->pixel_width >= it->first_visible_x)
22350 /* Glyph is at least partially visible. */
22351 ++it->hpos;
22352 if (x < it->first_visible_x)
22353 row->x = x - it->first_visible_x;
22355 else
22357 /* Glyph is off the left margin of the display area.
22358 Should not happen. */
22359 emacs_abort ();
22362 row->ascent = max (row->ascent, it->max_ascent);
22363 row->height = max (row->height, it->max_ascent + it->max_descent);
22364 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22365 row->phys_height = max (row->phys_height,
22366 it->max_phys_ascent + it->max_phys_descent);
22367 row->extra_line_spacing = max (row->extra_line_spacing,
22368 it->max_extra_line_spacing);
22369 x += glyph->pixel_width;
22370 ++i;
22373 /* Stop if max_x reached. */
22374 if (i < nglyphs)
22375 break;
22377 /* Stop at line ends. */
22378 if (ITERATOR_AT_END_OF_LINE_P (it))
22380 it->continuation_lines_width = 0;
22381 break;
22384 set_iterator_to_next (it, 1);
22385 if (STRINGP (it->string))
22386 it_charpos = IT_STRING_CHARPOS (*it);
22387 else
22388 it_charpos = IT_CHARPOS (*it);
22390 /* Stop if truncating at the right edge. */
22391 if (it->line_wrap == TRUNCATE
22392 && it->current_x >= it->last_visible_x)
22394 /* Add truncation mark, but don't do it if the line is
22395 truncated at a padding space. */
22396 if (it_charpos < it->string_nchars)
22398 if (!FRAME_WINDOW_P (it->f))
22400 int ii, n;
22402 if (it->current_x > it->last_visible_x)
22404 if (!row->reversed_p)
22406 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22407 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22408 break;
22410 else
22412 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22413 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22414 break;
22415 unproduce_glyphs (it, ii + 1);
22416 ii = row->used[TEXT_AREA] - (ii + 1);
22418 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22420 row->used[TEXT_AREA] = ii;
22421 produce_special_glyphs (it, IT_TRUNCATION);
22424 produce_special_glyphs (it, IT_TRUNCATION);
22426 row->truncated_on_right_p = 1;
22428 break;
22432 /* Maybe insert a truncation at the left. */
22433 if (it->first_visible_x
22434 && it_charpos > 0)
22436 if (!FRAME_WINDOW_P (it->f)
22437 || (row->reversed_p
22438 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22439 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22440 insert_left_trunc_glyphs (it);
22441 row->truncated_on_left_p = 1;
22444 it->face_id = saved_face_id;
22446 /* Value is number of columns displayed. */
22447 return it->hpos - hpos_at_start;
22452 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22453 appears as an element of LIST or as the car of an element of LIST.
22454 If PROPVAL is a list, compare each element against LIST in that
22455 way, and return 1/2 if any element of PROPVAL is found in LIST.
22456 Otherwise return 0. This function cannot quit.
22457 The return value is 2 if the text is invisible but with an ellipsis
22458 and 1 if it's invisible and without an ellipsis. */
22461 invisible_p (register Lisp_Object propval, Lisp_Object list)
22463 register Lisp_Object tail, proptail;
22465 for (tail = list; CONSP (tail); tail = XCDR (tail))
22467 register Lisp_Object tem;
22468 tem = XCAR (tail);
22469 if (EQ (propval, tem))
22470 return 1;
22471 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22472 return NILP (XCDR (tem)) ? 1 : 2;
22475 if (CONSP (propval))
22477 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22479 Lisp_Object propelt;
22480 propelt = XCAR (proptail);
22481 for (tail = list; CONSP (tail); tail = XCDR (tail))
22483 register Lisp_Object tem;
22484 tem = XCAR (tail);
22485 if (EQ (propelt, tem))
22486 return 1;
22487 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22488 return NILP (XCDR (tem)) ? 1 : 2;
22493 return 0;
22496 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22497 doc: /* Non-nil if the property makes the text invisible.
22498 POS-OR-PROP can be a marker or number, in which case it is taken to be
22499 a position in the current buffer and the value of the `invisible' property
22500 is checked; or it can be some other value, which is then presumed to be the
22501 value of the `invisible' property of the text of interest.
22502 The non-nil value returned can be t for truly invisible text or something
22503 else if the text is replaced by an ellipsis. */)
22504 (Lisp_Object pos_or_prop)
22506 Lisp_Object prop
22507 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22508 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22509 : pos_or_prop);
22510 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22511 return (invis == 0 ? Qnil
22512 : invis == 1 ? Qt
22513 : make_number (invis));
22516 /* Calculate a width or height in pixels from a specification using
22517 the following elements:
22519 SPEC ::=
22520 NUM - a (fractional) multiple of the default font width/height
22521 (NUM) - specifies exactly NUM pixels
22522 UNIT - a fixed number of pixels, see below.
22523 ELEMENT - size of a display element in pixels, see below.
22524 (NUM . SPEC) - equals NUM * SPEC
22525 (+ SPEC SPEC ...) - add pixel values
22526 (- SPEC SPEC ...) - subtract pixel values
22527 (- SPEC) - negate pixel value
22529 NUM ::=
22530 INT or FLOAT - a number constant
22531 SYMBOL - use symbol's (buffer local) variable binding.
22533 UNIT ::=
22534 in - pixels per inch *)
22535 mm - pixels per 1/1000 meter *)
22536 cm - pixels per 1/100 meter *)
22537 width - width of current font in pixels.
22538 height - height of current font in pixels.
22540 *) using the ratio(s) defined in display-pixels-per-inch.
22542 ELEMENT ::=
22544 left-fringe - left fringe width in pixels
22545 right-fringe - right fringe width in pixels
22547 left-margin - left margin width in pixels
22548 right-margin - right margin width in pixels
22550 scroll-bar - scroll-bar area width in pixels
22552 Examples:
22554 Pixels corresponding to 5 inches:
22555 (5 . in)
22557 Total width of non-text areas on left side of window (if scroll-bar is on left):
22558 '(space :width (+ left-fringe left-margin scroll-bar))
22560 Align to first text column (in header line):
22561 '(space :align-to 0)
22563 Align to middle of text area minus half the width of variable `my-image'
22564 containing a loaded image:
22565 '(space :align-to (0.5 . (- text my-image)))
22567 Width of left margin minus width of 1 character in the default font:
22568 '(space :width (- left-margin 1))
22570 Width of left margin minus width of 2 characters in the current font:
22571 '(space :width (- left-margin (2 . width)))
22573 Center 1 character over left-margin (in header line):
22574 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22576 Different ways to express width of left fringe plus left margin minus one pixel:
22577 '(space :width (- (+ left-fringe left-margin) (1)))
22578 '(space :width (+ left-fringe left-margin (- (1))))
22579 '(space :width (+ left-fringe left-margin (-1)))
22583 static int
22584 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22585 struct font *font, int width_p, int *align_to)
22587 double pixels;
22589 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22590 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22592 if (NILP (prop))
22593 return OK_PIXELS (0);
22595 eassert (FRAME_LIVE_P (it->f));
22597 if (SYMBOLP (prop))
22599 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22601 char *unit = SSDATA (SYMBOL_NAME (prop));
22603 if (unit[0] == 'i' && unit[1] == 'n')
22604 pixels = 1.0;
22605 else if (unit[0] == 'm' && unit[1] == 'm')
22606 pixels = 25.4;
22607 else if (unit[0] == 'c' && unit[1] == 'm')
22608 pixels = 2.54;
22609 else
22610 pixels = 0;
22611 if (pixels > 0)
22613 double ppi = (width_p ? FRAME_RES_X (it->f)
22614 : FRAME_RES_Y (it->f));
22616 if (ppi > 0)
22617 return OK_PIXELS (ppi / pixels);
22618 return 0;
22622 #ifdef HAVE_WINDOW_SYSTEM
22623 if (EQ (prop, Qheight))
22624 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22625 if (EQ (prop, Qwidth))
22626 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22627 #else
22628 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22629 return OK_PIXELS (1);
22630 #endif
22632 if (EQ (prop, Qtext))
22633 return OK_PIXELS (width_p
22634 ? window_box_width (it->w, TEXT_AREA)
22635 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22637 if (align_to && *align_to < 0)
22639 *res = 0;
22640 if (EQ (prop, Qleft))
22641 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22642 if (EQ (prop, Qright))
22643 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22644 if (EQ (prop, Qcenter))
22645 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22646 + window_box_width (it->w, TEXT_AREA) / 2);
22647 if (EQ (prop, Qleft_fringe))
22648 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22649 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22650 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22651 if (EQ (prop, Qright_fringe))
22652 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22653 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22654 : window_box_right_offset (it->w, TEXT_AREA));
22655 if (EQ (prop, Qleft_margin))
22656 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22657 if (EQ (prop, Qright_margin))
22658 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22659 if (EQ (prop, Qscroll_bar))
22660 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22662 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22663 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22664 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22665 : 0)));
22667 else
22669 if (EQ (prop, Qleft_fringe))
22670 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22671 if (EQ (prop, Qright_fringe))
22672 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22673 if (EQ (prop, Qleft_margin))
22674 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22675 if (EQ (prop, Qright_margin))
22676 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22677 if (EQ (prop, Qscroll_bar))
22678 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22681 prop = buffer_local_value_1 (prop, it->w->contents);
22682 if (EQ (prop, Qunbound))
22683 prop = Qnil;
22686 if (INTEGERP (prop) || FLOATP (prop))
22688 int base_unit = (width_p
22689 ? FRAME_COLUMN_WIDTH (it->f)
22690 : FRAME_LINE_HEIGHT (it->f));
22691 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22694 if (CONSP (prop))
22696 Lisp_Object car = XCAR (prop);
22697 Lisp_Object cdr = XCDR (prop);
22699 if (SYMBOLP (car))
22701 #ifdef HAVE_WINDOW_SYSTEM
22702 if (FRAME_WINDOW_P (it->f)
22703 && valid_image_p (prop))
22705 ptrdiff_t id = lookup_image (it->f, prop);
22706 struct image *img = IMAGE_FROM_ID (it->f, id);
22708 return OK_PIXELS (width_p ? img->width : img->height);
22710 #endif
22711 if (EQ (car, Qplus) || EQ (car, Qminus))
22713 int first = 1;
22714 double px;
22716 pixels = 0;
22717 while (CONSP (cdr))
22719 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22720 font, width_p, align_to))
22721 return 0;
22722 if (first)
22723 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22724 else
22725 pixels += px;
22726 cdr = XCDR (cdr);
22728 if (EQ (car, Qminus))
22729 pixels = -pixels;
22730 return OK_PIXELS (pixels);
22733 car = buffer_local_value_1 (car, it->w->contents);
22734 if (EQ (car, Qunbound))
22735 car = Qnil;
22738 if (INTEGERP (car) || FLOATP (car))
22740 double fact;
22741 pixels = XFLOATINT (car);
22742 if (NILP (cdr))
22743 return OK_PIXELS (pixels);
22744 if (calc_pixel_width_or_height (&fact, it, cdr,
22745 font, width_p, align_to))
22746 return OK_PIXELS (pixels * fact);
22747 return 0;
22750 return 0;
22753 return 0;
22757 /***********************************************************************
22758 Glyph Display
22759 ***********************************************************************/
22761 #ifdef HAVE_WINDOW_SYSTEM
22763 #ifdef GLYPH_DEBUG
22765 void
22766 dump_glyph_string (struct glyph_string *s)
22768 fprintf (stderr, "glyph string\n");
22769 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22770 s->x, s->y, s->width, s->height);
22771 fprintf (stderr, " ybase = %d\n", s->ybase);
22772 fprintf (stderr, " hl = %d\n", s->hl);
22773 fprintf (stderr, " left overhang = %d, right = %d\n",
22774 s->left_overhang, s->right_overhang);
22775 fprintf (stderr, " nchars = %d\n", s->nchars);
22776 fprintf (stderr, " extends to end of line = %d\n",
22777 s->extends_to_end_of_line_p);
22778 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22779 fprintf (stderr, " bg width = %d\n", s->background_width);
22782 #endif /* GLYPH_DEBUG */
22784 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22785 of XChar2b structures for S; it can't be allocated in
22786 init_glyph_string because it must be allocated via `alloca'. W
22787 is the window on which S is drawn. ROW and AREA are the glyph row
22788 and area within the row from which S is constructed. START is the
22789 index of the first glyph structure covered by S. HL is a
22790 face-override for drawing S. */
22792 #ifdef HAVE_NTGUI
22793 #define OPTIONAL_HDC(hdc) HDC hdc,
22794 #define DECLARE_HDC(hdc) HDC hdc;
22795 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22796 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22797 #endif
22799 #ifndef OPTIONAL_HDC
22800 #define OPTIONAL_HDC(hdc)
22801 #define DECLARE_HDC(hdc)
22802 #define ALLOCATE_HDC(hdc, f)
22803 #define RELEASE_HDC(hdc, f)
22804 #endif
22806 static void
22807 init_glyph_string (struct glyph_string *s,
22808 OPTIONAL_HDC (hdc)
22809 XChar2b *char2b, struct window *w, struct glyph_row *row,
22810 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22812 memset (s, 0, sizeof *s);
22813 s->w = w;
22814 s->f = XFRAME (w->frame);
22815 #ifdef HAVE_NTGUI
22816 s->hdc = hdc;
22817 #endif
22818 s->display = FRAME_X_DISPLAY (s->f);
22819 s->window = FRAME_X_WINDOW (s->f);
22820 s->char2b = char2b;
22821 s->hl = hl;
22822 s->row = row;
22823 s->area = area;
22824 s->first_glyph = row->glyphs[area] + start;
22825 s->height = row->height;
22826 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22827 s->ybase = s->y + row->ascent;
22831 /* Append the list of glyph strings with head H and tail T to the list
22832 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22834 static void
22835 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22836 struct glyph_string *h, struct glyph_string *t)
22838 if (h)
22840 if (*head)
22841 (*tail)->next = h;
22842 else
22843 *head = h;
22844 h->prev = *tail;
22845 *tail = t;
22850 /* Prepend the list of glyph strings with head H and tail T to the
22851 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22852 result. */
22854 static void
22855 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22856 struct glyph_string *h, struct glyph_string *t)
22858 if (h)
22860 if (*head)
22861 (*head)->prev = t;
22862 else
22863 *tail = t;
22864 t->next = *head;
22865 *head = h;
22870 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22871 Set *HEAD and *TAIL to the resulting list. */
22873 static void
22874 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22875 struct glyph_string *s)
22877 s->next = s->prev = NULL;
22878 append_glyph_string_lists (head, tail, s, s);
22882 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22883 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22884 make sure that X resources for the face returned are allocated.
22885 Value is a pointer to a realized face that is ready for display if
22886 DISPLAY_P is non-zero. */
22888 static struct face *
22889 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22890 XChar2b *char2b, int display_p)
22892 struct face *face = FACE_FROM_ID (f, face_id);
22893 unsigned code = 0;
22895 if (face->font)
22897 code = face->font->driver->encode_char (face->font, c);
22899 if (code == FONT_INVALID_CODE)
22900 code = 0;
22902 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22904 /* Make sure X resources of the face are allocated. */
22905 #ifdef HAVE_X_WINDOWS
22906 if (display_p)
22907 #endif
22909 eassert (face != NULL);
22910 PREPARE_FACE_FOR_DISPLAY (f, face);
22913 return face;
22917 /* Get face and two-byte form of character glyph GLYPH on frame F.
22918 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22919 a pointer to a realized face that is ready for display. */
22921 static struct face *
22922 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22923 XChar2b *char2b, int *two_byte_p)
22925 struct face *face;
22926 unsigned code = 0;
22928 eassert (glyph->type == CHAR_GLYPH);
22929 face = FACE_FROM_ID (f, glyph->face_id);
22931 /* Make sure X resources of the face are allocated. */
22932 eassert (face != NULL);
22933 PREPARE_FACE_FOR_DISPLAY (f, face);
22935 if (two_byte_p)
22936 *two_byte_p = 0;
22938 if (face->font)
22940 if (CHAR_BYTE8_P (glyph->u.ch))
22941 code = CHAR_TO_BYTE8 (glyph->u.ch);
22942 else
22943 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22945 if (code == FONT_INVALID_CODE)
22946 code = 0;
22949 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22950 return face;
22954 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22955 Return 1 if FONT has a glyph for C, otherwise return 0. */
22957 static int
22958 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22960 unsigned code;
22962 if (CHAR_BYTE8_P (c))
22963 code = CHAR_TO_BYTE8 (c);
22964 else
22965 code = font->driver->encode_char (font, c);
22967 if (code == FONT_INVALID_CODE)
22968 return 0;
22969 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22970 return 1;
22974 /* Fill glyph string S with composition components specified by S->cmp.
22976 BASE_FACE is the base face of the composition.
22977 S->cmp_from is the index of the first component for S.
22979 OVERLAPS non-zero means S should draw the foreground only, and use
22980 its physical height for clipping. See also draw_glyphs.
22982 Value is the index of a component not in S. */
22984 static int
22985 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22986 int overlaps)
22988 int i;
22989 /* For all glyphs of this composition, starting at the offset
22990 S->cmp_from, until we reach the end of the definition or encounter a
22991 glyph that requires the different face, add it to S. */
22992 struct face *face;
22994 eassert (s);
22996 s->for_overlaps = overlaps;
22997 s->face = NULL;
22998 s->font = NULL;
22999 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23001 int c = COMPOSITION_GLYPH (s->cmp, i);
23003 /* TAB in a composition means display glyphs with padding space
23004 on the left or right. */
23005 if (c != '\t')
23007 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23008 -1, Qnil);
23010 face = get_char_face_and_encoding (s->f, c, face_id,
23011 s->char2b + i, 1);
23012 if (face)
23014 if (! s->face)
23016 s->face = face;
23017 s->font = s->face->font;
23019 else if (s->face != face)
23020 break;
23023 ++s->nchars;
23025 s->cmp_to = i;
23027 if (s->face == NULL)
23029 s->face = base_face->ascii_face;
23030 s->font = s->face->font;
23033 /* All glyph strings for the same composition has the same width,
23034 i.e. the width set for the first component of the composition. */
23035 s->width = s->first_glyph->pixel_width;
23037 /* If the specified font could not be loaded, use the frame's
23038 default font, but record the fact that we couldn't load it in
23039 the glyph string so that we can draw rectangles for the
23040 characters of the glyph string. */
23041 if (s->font == NULL)
23043 s->font_not_found_p = 1;
23044 s->font = FRAME_FONT (s->f);
23047 /* Adjust base line for subscript/superscript text. */
23048 s->ybase += s->first_glyph->voffset;
23050 /* This glyph string must always be drawn with 16-bit functions. */
23051 s->two_byte_p = 1;
23053 return s->cmp_to;
23056 static int
23057 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23058 int start, int end, int overlaps)
23060 struct glyph *glyph, *last;
23061 Lisp_Object lgstring;
23062 int i;
23064 s->for_overlaps = overlaps;
23065 glyph = s->row->glyphs[s->area] + start;
23066 last = s->row->glyphs[s->area] + end;
23067 s->cmp_id = glyph->u.cmp.id;
23068 s->cmp_from = glyph->slice.cmp.from;
23069 s->cmp_to = glyph->slice.cmp.to + 1;
23070 s->face = FACE_FROM_ID (s->f, face_id);
23071 lgstring = composition_gstring_from_id (s->cmp_id);
23072 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23073 glyph++;
23074 while (glyph < last
23075 && glyph->u.cmp.automatic
23076 && glyph->u.cmp.id == s->cmp_id
23077 && s->cmp_to == glyph->slice.cmp.from)
23078 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23080 for (i = s->cmp_from; i < s->cmp_to; i++)
23082 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23083 unsigned code = LGLYPH_CODE (lglyph);
23085 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23087 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23088 return glyph - s->row->glyphs[s->area];
23092 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23093 See the comment of fill_glyph_string for arguments.
23094 Value is the index of the first glyph not in S. */
23097 static int
23098 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23099 int start, int end, int overlaps)
23101 struct glyph *glyph, *last;
23102 int voffset;
23104 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23105 s->for_overlaps = overlaps;
23106 glyph = s->row->glyphs[s->area] + start;
23107 last = s->row->glyphs[s->area] + end;
23108 voffset = glyph->voffset;
23109 s->face = FACE_FROM_ID (s->f, face_id);
23110 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23111 s->nchars = 1;
23112 s->width = glyph->pixel_width;
23113 glyph++;
23114 while (glyph < last
23115 && glyph->type == GLYPHLESS_GLYPH
23116 && glyph->voffset == voffset
23117 && glyph->face_id == face_id)
23119 s->nchars++;
23120 s->width += glyph->pixel_width;
23121 glyph++;
23123 s->ybase += voffset;
23124 return glyph - s->row->glyphs[s->area];
23128 /* Fill glyph string S from a sequence of character glyphs.
23130 FACE_ID is the face id of the string. START is the index of the
23131 first glyph to consider, END is the index of the last + 1.
23132 OVERLAPS non-zero means S should draw the foreground only, and use
23133 its physical height for clipping. See also draw_glyphs.
23135 Value is the index of the first glyph not in S. */
23137 static int
23138 fill_glyph_string (struct glyph_string *s, int face_id,
23139 int start, int end, int overlaps)
23141 struct glyph *glyph, *last;
23142 int voffset;
23143 int glyph_not_available_p;
23145 eassert (s->f == XFRAME (s->w->frame));
23146 eassert (s->nchars == 0);
23147 eassert (start >= 0 && end > start);
23149 s->for_overlaps = overlaps;
23150 glyph = s->row->glyphs[s->area] + start;
23151 last = s->row->glyphs[s->area] + end;
23152 voffset = glyph->voffset;
23153 s->padding_p = glyph->padding_p;
23154 glyph_not_available_p = glyph->glyph_not_available_p;
23156 while (glyph < last
23157 && glyph->type == CHAR_GLYPH
23158 && glyph->voffset == voffset
23159 /* Same face id implies same font, nowadays. */
23160 && glyph->face_id == face_id
23161 && glyph->glyph_not_available_p == glyph_not_available_p)
23163 int two_byte_p;
23165 s->face = get_glyph_face_and_encoding (s->f, glyph,
23166 s->char2b + s->nchars,
23167 &two_byte_p);
23168 s->two_byte_p = two_byte_p;
23169 ++s->nchars;
23170 eassert (s->nchars <= end - start);
23171 s->width += glyph->pixel_width;
23172 if (glyph++->padding_p != s->padding_p)
23173 break;
23176 s->font = s->face->font;
23178 /* If the specified font could not be loaded, use the frame's font,
23179 but record the fact that we couldn't load it in
23180 S->font_not_found_p so that we can draw rectangles for the
23181 characters of the glyph string. */
23182 if (s->font == NULL || glyph_not_available_p)
23184 s->font_not_found_p = 1;
23185 s->font = FRAME_FONT (s->f);
23188 /* Adjust base line for subscript/superscript text. */
23189 s->ybase += voffset;
23191 eassert (s->face && s->face->gc);
23192 return glyph - s->row->glyphs[s->area];
23196 /* Fill glyph string S from image glyph S->first_glyph. */
23198 static void
23199 fill_image_glyph_string (struct glyph_string *s)
23201 eassert (s->first_glyph->type == IMAGE_GLYPH);
23202 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23203 eassert (s->img);
23204 s->slice = s->first_glyph->slice.img;
23205 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23206 s->font = s->face->font;
23207 s->width = s->first_glyph->pixel_width;
23209 /* Adjust base line for subscript/superscript text. */
23210 s->ybase += s->first_glyph->voffset;
23214 /* Fill glyph string S from a sequence of stretch glyphs.
23216 START is the index of the first glyph to consider,
23217 END is the index of the last + 1.
23219 Value is the index of the first glyph not in S. */
23221 static int
23222 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23224 struct glyph *glyph, *last;
23225 int voffset, face_id;
23227 eassert (s->first_glyph->type == STRETCH_GLYPH);
23229 glyph = s->row->glyphs[s->area] + start;
23230 last = s->row->glyphs[s->area] + end;
23231 face_id = glyph->face_id;
23232 s->face = FACE_FROM_ID (s->f, face_id);
23233 s->font = s->face->font;
23234 s->width = glyph->pixel_width;
23235 s->nchars = 1;
23236 voffset = glyph->voffset;
23238 for (++glyph;
23239 (glyph < last
23240 && glyph->type == STRETCH_GLYPH
23241 && glyph->voffset == voffset
23242 && glyph->face_id == face_id);
23243 ++glyph)
23244 s->width += glyph->pixel_width;
23246 /* Adjust base line for subscript/superscript text. */
23247 s->ybase += voffset;
23249 /* The case that face->gc == 0 is handled when drawing the glyph
23250 string by calling PREPARE_FACE_FOR_DISPLAY. */
23251 eassert (s->face);
23252 return glyph - s->row->glyphs[s->area];
23255 static struct font_metrics *
23256 get_per_char_metric (struct font *font, XChar2b *char2b)
23258 static struct font_metrics metrics;
23259 unsigned code;
23261 if (! font)
23262 return NULL;
23263 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23264 if (code == FONT_INVALID_CODE)
23265 return NULL;
23266 font->driver->text_extents (font, &code, 1, &metrics);
23267 return &metrics;
23270 /* EXPORT for RIF:
23271 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23272 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23273 assumed to be zero. */
23275 void
23276 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23278 *left = *right = 0;
23280 if (glyph->type == CHAR_GLYPH)
23282 struct face *face;
23283 XChar2b char2b;
23284 struct font_metrics *pcm;
23286 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23287 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23289 if (pcm->rbearing > pcm->width)
23290 *right = pcm->rbearing - pcm->width;
23291 if (pcm->lbearing < 0)
23292 *left = -pcm->lbearing;
23295 else if (glyph->type == COMPOSITE_GLYPH)
23297 if (! glyph->u.cmp.automatic)
23299 struct composition *cmp = composition_table[glyph->u.cmp.id];
23301 if (cmp->rbearing > cmp->pixel_width)
23302 *right = cmp->rbearing - cmp->pixel_width;
23303 if (cmp->lbearing < 0)
23304 *left = - cmp->lbearing;
23306 else
23308 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23309 struct font_metrics metrics;
23311 composition_gstring_width (gstring, glyph->slice.cmp.from,
23312 glyph->slice.cmp.to + 1, &metrics);
23313 if (metrics.rbearing > metrics.width)
23314 *right = metrics.rbearing - metrics.width;
23315 if (metrics.lbearing < 0)
23316 *left = - metrics.lbearing;
23322 /* Return the index of the first glyph preceding glyph string S that
23323 is overwritten by S because of S's left overhang. Value is -1
23324 if no glyphs are overwritten. */
23326 static int
23327 left_overwritten (struct glyph_string *s)
23329 int k;
23331 if (s->left_overhang)
23333 int x = 0, i;
23334 struct glyph *glyphs = s->row->glyphs[s->area];
23335 int first = s->first_glyph - glyphs;
23337 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23338 x -= glyphs[i].pixel_width;
23340 k = i + 1;
23342 else
23343 k = -1;
23345 return k;
23349 /* Return the index of the first glyph preceding glyph string S that
23350 is overwriting S because of its right overhang. Value is -1 if no
23351 glyph in front of S overwrites S. */
23353 static int
23354 left_overwriting (struct glyph_string *s)
23356 int i, k, x;
23357 struct glyph *glyphs = s->row->glyphs[s->area];
23358 int first = s->first_glyph - glyphs;
23360 k = -1;
23361 x = 0;
23362 for (i = first - 1; i >= 0; --i)
23364 int left, right;
23365 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23366 if (x + right > 0)
23367 k = i;
23368 x -= glyphs[i].pixel_width;
23371 return k;
23375 /* Return the index of the last glyph following glyph string S that is
23376 overwritten by S because of S's right overhang. Value is -1 if
23377 no such glyph is found. */
23379 static int
23380 right_overwritten (struct glyph_string *s)
23382 int k = -1;
23384 if (s->right_overhang)
23386 int x = 0, i;
23387 struct glyph *glyphs = s->row->glyphs[s->area];
23388 int first = (s->first_glyph - glyphs
23389 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23390 int end = s->row->used[s->area];
23392 for (i = first; i < end && s->right_overhang > x; ++i)
23393 x += glyphs[i].pixel_width;
23395 k = i;
23398 return k;
23402 /* Return the index of the last glyph following glyph string S that
23403 overwrites S because of its left overhang. Value is negative
23404 if no such glyph is found. */
23406 static int
23407 right_overwriting (struct glyph_string *s)
23409 int i, k, x;
23410 int end = s->row->used[s->area];
23411 struct glyph *glyphs = s->row->glyphs[s->area];
23412 int first = (s->first_glyph - glyphs
23413 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23415 k = -1;
23416 x = 0;
23417 for (i = first; i < end; ++i)
23419 int left, right;
23420 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23421 if (x - left < 0)
23422 k = i;
23423 x += glyphs[i].pixel_width;
23426 return k;
23430 /* Set background width of glyph string S. START is the index of the
23431 first glyph following S. LAST_X is the right-most x-position + 1
23432 in the drawing area. */
23434 static void
23435 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23437 /* If the face of this glyph string has to be drawn to the end of
23438 the drawing area, set S->extends_to_end_of_line_p. */
23440 if (start == s->row->used[s->area]
23441 && s->area == TEXT_AREA
23442 && ((s->row->fill_line_p
23443 && (s->hl == DRAW_NORMAL_TEXT
23444 || s->hl == DRAW_IMAGE_RAISED
23445 || s->hl == DRAW_IMAGE_SUNKEN))
23446 || s->hl == DRAW_MOUSE_FACE))
23447 s->extends_to_end_of_line_p = 1;
23449 /* If S extends its face to the end of the line, set its
23450 background_width to the distance to the right edge of the drawing
23451 area. */
23452 if (s->extends_to_end_of_line_p)
23453 s->background_width = last_x - s->x + 1;
23454 else
23455 s->background_width = s->width;
23459 /* Compute overhangs and x-positions for glyph string S and its
23460 predecessors, or successors. X is the starting x-position for S.
23461 BACKWARD_P non-zero means process predecessors. */
23463 static void
23464 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23466 if (backward_p)
23468 while (s)
23470 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23471 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23472 x -= s->width;
23473 s->x = x;
23474 s = s->prev;
23477 else
23479 while (s)
23481 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23482 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23483 s->x = x;
23484 x += s->width;
23485 s = s->next;
23492 /* The following macros are only called from draw_glyphs below.
23493 They reference the following parameters of that function directly:
23494 `w', `row', `area', and `overlap_p'
23495 as well as the following local variables:
23496 `s', `f', and `hdc' (in W32) */
23498 #ifdef HAVE_NTGUI
23499 /* On W32, silently add local `hdc' variable to argument list of
23500 init_glyph_string. */
23501 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23502 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23503 #else
23504 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23505 init_glyph_string (s, char2b, w, row, area, start, hl)
23506 #endif
23508 /* Add a glyph string for a stretch glyph to the list of strings
23509 between HEAD and TAIL. START is the index of the stretch glyph in
23510 row area AREA of glyph row ROW. END is the index of the last glyph
23511 in that glyph row area. X is the current output position assigned
23512 to the new glyph string constructed. HL overrides that face of the
23513 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23514 is the right-most x-position of the drawing area. */
23516 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23517 and below -- keep them on one line. */
23518 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23519 do \
23521 s = alloca (sizeof *s); \
23522 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23523 START = fill_stretch_glyph_string (s, START, END); \
23524 append_glyph_string (&HEAD, &TAIL, s); \
23525 s->x = (X); \
23527 while (0)
23530 /* Add a glyph string for an image glyph to the list of strings
23531 between HEAD and TAIL. START is the index of the image glyph in
23532 row area AREA of glyph row ROW. END is the index of the last glyph
23533 in that glyph row area. X is the current output position assigned
23534 to the new glyph string constructed. HL overrides that face of the
23535 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23536 is the right-most x-position of the drawing area. */
23538 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23539 do \
23541 s = alloca (sizeof *s); \
23542 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23543 fill_image_glyph_string (s); \
23544 append_glyph_string (&HEAD, &TAIL, s); \
23545 ++START; \
23546 s->x = (X); \
23548 while (0)
23551 /* Add a glyph string for a sequence of character glyphs to the list
23552 of strings between HEAD and TAIL. START is the index of the first
23553 glyph in row area AREA of glyph row ROW that is part of the new
23554 glyph string. END is the index of the last glyph in that glyph row
23555 area. X is the current output position assigned to the new glyph
23556 string constructed. HL overrides that face of the glyph; e.g. it
23557 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23558 right-most x-position of the drawing area. */
23560 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23561 do \
23563 int face_id; \
23564 XChar2b *char2b; \
23566 face_id = (row)->glyphs[area][START].face_id; \
23568 s = alloca (sizeof *s); \
23569 char2b = alloca ((END - START) * sizeof *char2b); \
23570 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23571 append_glyph_string (&HEAD, &TAIL, s); \
23572 s->x = (X); \
23573 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23575 while (0)
23578 /* Add a glyph string for a composite sequence to the list of strings
23579 between HEAD and TAIL. START is the index of the first glyph in
23580 row area AREA of glyph row ROW that is part of the new glyph
23581 string. END is the index of the last glyph in that glyph row area.
23582 X is the current output position assigned to the new glyph string
23583 constructed. HL overrides that face of the glyph; e.g. it is
23584 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23585 x-position of the drawing area. */
23587 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23588 do { \
23589 int face_id = (row)->glyphs[area][START].face_id; \
23590 struct face *base_face = FACE_FROM_ID (f, face_id); \
23591 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23592 struct composition *cmp = composition_table[cmp_id]; \
23593 XChar2b *char2b; \
23594 struct glyph_string *first_s = NULL; \
23595 int n; \
23597 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23599 /* Make glyph_strings for each glyph sequence that is drawable by \
23600 the same face, and append them to HEAD/TAIL. */ \
23601 for (n = 0; n < cmp->glyph_len;) \
23603 s = alloca (sizeof *s); \
23604 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23605 append_glyph_string (&(HEAD), &(TAIL), s); \
23606 s->cmp = cmp; \
23607 s->cmp_from = n; \
23608 s->x = (X); \
23609 if (n == 0) \
23610 first_s = s; \
23611 n = fill_composite_glyph_string (s, base_face, overlaps); \
23614 ++START; \
23615 s = first_s; \
23616 } while (0)
23619 /* Add a glyph string for a glyph-string sequence to the list of strings
23620 between HEAD and TAIL. */
23622 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23623 do { \
23624 int face_id; \
23625 XChar2b *char2b; \
23626 Lisp_Object gstring; \
23628 face_id = (row)->glyphs[area][START].face_id; \
23629 gstring = (composition_gstring_from_id \
23630 ((row)->glyphs[area][START].u.cmp.id)); \
23631 s = alloca (sizeof *s); \
23632 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23633 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23634 append_glyph_string (&(HEAD), &(TAIL), s); \
23635 s->x = (X); \
23636 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23637 } while (0)
23640 /* Add a glyph string for a sequence of glyphless character's glyphs
23641 to the list of strings between HEAD and TAIL. The meanings of
23642 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23644 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23645 do \
23647 int face_id; \
23649 face_id = (row)->glyphs[area][START].face_id; \
23651 s = alloca (sizeof *s); \
23652 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23653 append_glyph_string (&HEAD, &TAIL, s); \
23654 s->x = (X); \
23655 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23656 overlaps); \
23658 while (0)
23661 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23662 of AREA of glyph row ROW on window W between indices START and END.
23663 HL overrides the face for drawing glyph strings, e.g. it is
23664 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23665 x-positions of the drawing area.
23667 This is an ugly monster macro construct because we must use alloca
23668 to allocate glyph strings (because draw_glyphs can be called
23669 asynchronously). */
23671 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23672 do \
23674 HEAD = TAIL = NULL; \
23675 while (START < END) \
23677 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23678 switch (first_glyph->type) \
23680 case CHAR_GLYPH: \
23681 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23682 HL, X, LAST_X); \
23683 break; \
23685 case COMPOSITE_GLYPH: \
23686 if (first_glyph->u.cmp.automatic) \
23687 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23688 HL, X, LAST_X); \
23689 else \
23690 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23691 HL, X, LAST_X); \
23692 break; \
23694 case STRETCH_GLYPH: \
23695 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23696 HL, X, LAST_X); \
23697 break; \
23699 case IMAGE_GLYPH: \
23700 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23701 HL, X, LAST_X); \
23702 break; \
23704 case GLYPHLESS_GLYPH: \
23705 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23706 HL, X, LAST_X); \
23707 break; \
23709 default: \
23710 emacs_abort (); \
23713 if (s) \
23715 set_glyph_string_background_width (s, START, LAST_X); \
23716 (X) += s->width; \
23719 } while (0)
23722 /* Draw glyphs between START and END in AREA of ROW on window W,
23723 starting at x-position X. X is relative to AREA in W. HL is a
23724 face-override with the following meaning:
23726 DRAW_NORMAL_TEXT draw normally
23727 DRAW_CURSOR draw in cursor face
23728 DRAW_MOUSE_FACE draw in mouse face.
23729 DRAW_INVERSE_VIDEO draw in mode line face
23730 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23731 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23733 If OVERLAPS is non-zero, draw only the foreground of characters and
23734 clip to the physical height of ROW. Non-zero value also defines
23735 the overlapping part to be drawn:
23737 OVERLAPS_PRED overlap with preceding rows
23738 OVERLAPS_SUCC overlap with succeeding rows
23739 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23740 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23742 Value is the x-position reached, relative to AREA of W. */
23744 static int
23745 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23746 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23747 enum draw_glyphs_face hl, int overlaps)
23749 struct glyph_string *head, *tail;
23750 struct glyph_string *s;
23751 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23752 int i, j, x_reached, last_x, area_left = 0;
23753 struct frame *f = XFRAME (WINDOW_FRAME (w));
23754 DECLARE_HDC (hdc);
23756 ALLOCATE_HDC (hdc, f);
23758 /* Let's rather be paranoid than getting a SEGV. */
23759 end = min (end, row->used[area]);
23760 start = clip_to_bounds (0, start, end);
23762 /* Translate X to frame coordinates. Set last_x to the right
23763 end of the drawing area. */
23764 if (row->full_width_p)
23766 /* X is relative to the left edge of W, without scroll bars
23767 or fringes. */
23768 area_left = WINDOW_LEFT_EDGE_X (w);
23769 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23771 else
23773 area_left = window_box_left (w, area);
23774 last_x = area_left + window_box_width (w, area);
23776 x += area_left;
23778 /* Build a doubly-linked list of glyph_string structures between
23779 head and tail from what we have to draw. Note that the macro
23780 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23781 the reason we use a separate variable `i'. */
23782 i = start;
23783 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23784 if (tail)
23785 x_reached = tail->x + tail->background_width;
23786 else
23787 x_reached = x;
23789 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23790 the row, redraw some glyphs in front or following the glyph
23791 strings built above. */
23792 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23794 struct glyph_string *h, *t;
23795 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23796 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23797 int check_mouse_face = 0;
23798 int dummy_x = 0;
23800 /* If mouse highlighting is on, we may need to draw adjacent
23801 glyphs using mouse-face highlighting. */
23802 if (area == TEXT_AREA && row->mouse_face_p
23803 && hlinfo->mouse_face_beg_row >= 0
23804 && hlinfo->mouse_face_end_row >= 0)
23806 struct glyph_row *mouse_beg_row, *mouse_end_row;
23808 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23809 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23811 if (row >= mouse_beg_row && row <= mouse_end_row)
23813 check_mouse_face = 1;
23814 mouse_beg_col = (row == mouse_beg_row)
23815 ? hlinfo->mouse_face_beg_col : 0;
23816 mouse_end_col = (row == mouse_end_row)
23817 ? hlinfo->mouse_face_end_col
23818 : row->used[TEXT_AREA];
23822 /* Compute overhangs for all glyph strings. */
23823 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23824 for (s = head; s; s = s->next)
23825 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23827 /* Prepend glyph strings for glyphs in front of the first glyph
23828 string that are overwritten because of the first glyph
23829 string's left overhang. The background of all strings
23830 prepended must be drawn because the first glyph string
23831 draws over it. */
23832 i = left_overwritten (head);
23833 if (i >= 0)
23835 enum draw_glyphs_face overlap_hl;
23837 /* If this row contains mouse highlighting, attempt to draw
23838 the overlapped glyphs with the correct highlight. This
23839 code fails if the overlap encompasses more than one glyph
23840 and mouse-highlight spans only some of these glyphs.
23841 However, making it work perfectly involves a lot more
23842 code, and I don't know if the pathological case occurs in
23843 practice, so we'll stick to this for now. --- cyd */
23844 if (check_mouse_face
23845 && mouse_beg_col < start && mouse_end_col > i)
23846 overlap_hl = DRAW_MOUSE_FACE;
23847 else
23848 overlap_hl = DRAW_NORMAL_TEXT;
23850 j = i;
23851 BUILD_GLYPH_STRINGS (j, start, h, t,
23852 overlap_hl, dummy_x, last_x);
23853 start = i;
23854 compute_overhangs_and_x (t, head->x, 1);
23855 prepend_glyph_string_lists (&head, &tail, h, t);
23856 clip_head = head;
23859 /* Prepend glyph strings for glyphs in front of the first glyph
23860 string that overwrite that glyph string because of their
23861 right overhang. For these strings, only the foreground must
23862 be drawn, because it draws over the glyph string at `head'.
23863 The background must not be drawn because this would overwrite
23864 right overhangs of preceding glyphs for which no glyph
23865 strings exist. */
23866 i = left_overwriting (head);
23867 if (i >= 0)
23869 enum draw_glyphs_face overlap_hl;
23871 if (check_mouse_face
23872 && mouse_beg_col < start && mouse_end_col > i)
23873 overlap_hl = DRAW_MOUSE_FACE;
23874 else
23875 overlap_hl = DRAW_NORMAL_TEXT;
23877 clip_head = head;
23878 BUILD_GLYPH_STRINGS (i, start, h, t,
23879 overlap_hl, dummy_x, last_x);
23880 for (s = h; s; s = s->next)
23881 s->background_filled_p = 1;
23882 compute_overhangs_and_x (t, head->x, 1);
23883 prepend_glyph_string_lists (&head, &tail, h, t);
23886 /* Append glyphs strings for glyphs following the last glyph
23887 string tail that are overwritten by tail. The background of
23888 these strings has to be drawn because tail's foreground draws
23889 over it. */
23890 i = right_overwritten (tail);
23891 if (i >= 0)
23893 enum draw_glyphs_face overlap_hl;
23895 if (check_mouse_face
23896 && mouse_beg_col < i && mouse_end_col > end)
23897 overlap_hl = DRAW_MOUSE_FACE;
23898 else
23899 overlap_hl = DRAW_NORMAL_TEXT;
23901 BUILD_GLYPH_STRINGS (end, i, h, t,
23902 overlap_hl, x, last_x);
23903 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23904 we don't have `end = i;' here. */
23905 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23906 append_glyph_string_lists (&head, &tail, h, t);
23907 clip_tail = tail;
23910 /* Append glyph strings for glyphs following the last glyph
23911 string tail that overwrite tail. The foreground of such
23912 glyphs has to be drawn because it writes into the background
23913 of tail. The background must not be drawn because it could
23914 paint over the foreground of following glyphs. */
23915 i = right_overwriting (tail);
23916 if (i >= 0)
23918 enum draw_glyphs_face overlap_hl;
23919 if (check_mouse_face
23920 && mouse_beg_col < i && mouse_end_col > end)
23921 overlap_hl = DRAW_MOUSE_FACE;
23922 else
23923 overlap_hl = DRAW_NORMAL_TEXT;
23925 clip_tail = tail;
23926 i++; /* We must include the Ith glyph. */
23927 BUILD_GLYPH_STRINGS (end, i, h, t,
23928 overlap_hl, x, last_x);
23929 for (s = h; s; s = s->next)
23930 s->background_filled_p = 1;
23931 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23932 append_glyph_string_lists (&head, &tail, h, t);
23934 if (clip_head || clip_tail)
23935 for (s = head; s; s = s->next)
23937 s->clip_head = clip_head;
23938 s->clip_tail = clip_tail;
23942 /* Draw all strings. */
23943 for (s = head; s; s = s->next)
23944 FRAME_RIF (f)->draw_glyph_string (s);
23946 #ifndef HAVE_NS
23947 /* When focus a sole frame and move horizontally, this sets on_p to 0
23948 causing a failure to erase prev cursor position. */
23949 if (area == TEXT_AREA
23950 && !row->full_width_p
23951 /* When drawing overlapping rows, only the glyph strings'
23952 foreground is drawn, which doesn't erase a cursor
23953 completely. */
23954 && !overlaps)
23956 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23957 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23958 : (tail ? tail->x + tail->background_width : x));
23959 x0 -= area_left;
23960 x1 -= area_left;
23962 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23963 row->y, MATRIX_ROW_BOTTOM_Y (row));
23965 #endif
23967 /* Value is the x-position up to which drawn, relative to AREA of W.
23968 This doesn't include parts drawn because of overhangs. */
23969 if (row->full_width_p)
23970 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23971 else
23972 x_reached -= area_left;
23974 RELEASE_HDC (hdc, f);
23976 return x_reached;
23979 /* Expand row matrix if too narrow. Don't expand if area
23980 is not present. */
23982 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23984 if (!fonts_changed_p \
23985 && (it->glyph_row->glyphs[area] \
23986 < it->glyph_row->glyphs[area + 1])) \
23988 it->w->ncols_scale_factor++; \
23989 fonts_changed_p = 1; \
23993 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23994 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23996 static void
23997 append_glyph (struct it *it)
23999 struct glyph *glyph;
24000 enum glyph_row_area area = it->area;
24002 eassert (it->glyph_row);
24003 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24005 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24006 if (glyph < it->glyph_row->glyphs[area + 1])
24008 /* If the glyph row is reversed, we need to prepend the glyph
24009 rather than append it. */
24010 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24012 struct glyph *g;
24014 /* Make room for the additional glyph. */
24015 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24016 g[1] = *g;
24017 glyph = it->glyph_row->glyphs[area];
24019 glyph->charpos = CHARPOS (it->position);
24020 glyph->object = it->object;
24021 if (it->pixel_width > 0)
24023 glyph->pixel_width = it->pixel_width;
24024 glyph->padding_p = 0;
24026 else
24028 /* Assure at least 1-pixel width. Otherwise, cursor can't
24029 be displayed correctly. */
24030 glyph->pixel_width = 1;
24031 glyph->padding_p = 1;
24033 glyph->ascent = it->ascent;
24034 glyph->descent = it->descent;
24035 glyph->voffset = it->voffset;
24036 glyph->type = CHAR_GLYPH;
24037 glyph->avoid_cursor_p = it->avoid_cursor_p;
24038 glyph->multibyte_p = it->multibyte_p;
24039 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24041 /* In R2L rows, the left and the right box edges need to be
24042 drawn in reverse direction. */
24043 glyph->right_box_line_p = it->start_of_box_run_p;
24044 glyph->left_box_line_p = it->end_of_box_run_p;
24046 else
24048 glyph->left_box_line_p = it->start_of_box_run_p;
24049 glyph->right_box_line_p = it->end_of_box_run_p;
24051 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24052 || it->phys_descent > it->descent);
24053 glyph->glyph_not_available_p = it->glyph_not_available_p;
24054 glyph->face_id = it->face_id;
24055 glyph->u.ch = it->char_to_display;
24056 glyph->slice.img = null_glyph_slice;
24057 glyph->font_type = FONT_TYPE_UNKNOWN;
24058 if (it->bidi_p)
24060 glyph->resolved_level = it->bidi_it.resolved_level;
24061 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24062 emacs_abort ();
24063 glyph->bidi_type = it->bidi_it.type;
24065 else
24067 glyph->resolved_level = 0;
24068 glyph->bidi_type = UNKNOWN_BT;
24070 ++it->glyph_row->used[area];
24072 else
24073 IT_EXPAND_MATRIX_WIDTH (it, area);
24076 /* Store one glyph for the composition IT->cmp_it.id in
24077 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24078 non-null. */
24080 static void
24081 append_composite_glyph (struct it *it)
24083 struct glyph *glyph;
24084 enum glyph_row_area area = it->area;
24086 eassert (it->glyph_row);
24088 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24089 if (glyph < it->glyph_row->glyphs[area + 1])
24091 /* If the glyph row is reversed, we need to prepend the glyph
24092 rather than append it. */
24093 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24095 struct glyph *g;
24097 /* Make room for the new glyph. */
24098 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24099 g[1] = *g;
24100 glyph = it->glyph_row->glyphs[it->area];
24102 glyph->charpos = it->cmp_it.charpos;
24103 glyph->object = it->object;
24104 glyph->pixel_width = it->pixel_width;
24105 glyph->ascent = it->ascent;
24106 glyph->descent = it->descent;
24107 glyph->voffset = it->voffset;
24108 glyph->type = COMPOSITE_GLYPH;
24109 if (it->cmp_it.ch < 0)
24111 glyph->u.cmp.automatic = 0;
24112 glyph->u.cmp.id = it->cmp_it.id;
24113 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24115 else
24117 glyph->u.cmp.automatic = 1;
24118 glyph->u.cmp.id = it->cmp_it.id;
24119 glyph->slice.cmp.from = it->cmp_it.from;
24120 glyph->slice.cmp.to = it->cmp_it.to - 1;
24122 glyph->avoid_cursor_p = it->avoid_cursor_p;
24123 glyph->multibyte_p = it->multibyte_p;
24124 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24126 /* In R2L rows, the left and the right box edges need to be
24127 drawn in reverse direction. */
24128 glyph->right_box_line_p = it->start_of_box_run_p;
24129 glyph->left_box_line_p = it->end_of_box_run_p;
24131 else
24133 glyph->left_box_line_p = it->start_of_box_run_p;
24134 glyph->right_box_line_p = it->end_of_box_run_p;
24136 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24137 || it->phys_descent > it->descent);
24138 glyph->padding_p = 0;
24139 glyph->glyph_not_available_p = 0;
24140 glyph->face_id = it->face_id;
24141 glyph->font_type = FONT_TYPE_UNKNOWN;
24142 if (it->bidi_p)
24144 glyph->resolved_level = it->bidi_it.resolved_level;
24145 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24146 emacs_abort ();
24147 glyph->bidi_type = it->bidi_it.type;
24149 ++it->glyph_row->used[area];
24151 else
24152 IT_EXPAND_MATRIX_WIDTH (it, area);
24156 /* Change IT->ascent and IT->height according to the setting of
24157 IT->voffset. */
24159 static void
24160 take_vertical_position_into_account (struct it *it)
24162 if (it->voffset)
24164 if (it->voffset < 0)
24165 /* Increase the ascent so that we can display the text higher
24166 in the line. */
24167 it->ascent -= it->voffset;
24168 else
24169 /* Increase the descent so that we can display the text lower
24170 in the line. */
24171 it->descent += it->voffset;
24176 /* Produce glyphs/get display metrics for the image IT is loaded with.
24177 See the description of struct display_iterator in dispextern.h for
24178 an overview of struct display_iterator. */
24180 static void
24181 produce_image_glyph (struct it *it)
24183 struct image *img;
24184 struct face *face;
24185 int glyph_ascent, crop;
24186 struct glyph_slice slice;
24188 eassert (it->what == IT_IMAGE);
24190 face = FACE_FROM_ID (it->f, it->face_id);
24191 eassert (face);
24192 /* Make sure X resources of the face is loaded. */
24193 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24195 if (it->image_id < 0)
24197 /* Fringe bitmap. */
24198 it->ascent = it->phys_ascent = 0;
24199 it->descent = it->phys_descent = 0;
24200 it->pixel_width = 0;
24201 it->nglyphs = 0;
24202 return;
24205 img = IMAGE_FROM_ID (it->f, it->image_id);
24206 eassert (img);
24207 /* Make sure X resources of the image is loaded. */
24208 prepare_image_for_display (it->f, img);
24210 slice.x = slice.y = 0;
24211 slice.width = img->width;
24212 slice.height = img->height;
24214 if (INTEGERP (it->slice.x))
24215 slice.x = XINT (it->slice.x);
24216 else if (FLOATP (it->slice.x))
24217 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24219 if (INTEGERP (it->slice.y))
24220 slice.y = XINT (it->slice.y);
24221 else if (FLOATP (it->slice.y))
24222 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24224 if (INTEGERP (it->slice.width))
24225 slice.width = XINT (it->slice.width);
24226 else if (FLOATP (it->slice.width))
24227 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24229 if (INTEGERP (it->slice.height))
24230 slice.height = XINT (it->slice.height);
24231 else if (FLOATP (it->slice.height))
24232 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24234 if (slice.x >= img->width)
24235 slice.x = img->width;
24236 if (slice.y >= img->height)
24237 slice.y = img->height;
24238 if (slice.x + slice.width >= img->width)
24239 slice.width = img->width - slice.x;
24240 if (slice.y + slice.height > img->height)
24241 slice.height = img->height - slice.y;
24243 if (slice.width == 0 || slice.height == 0)
24244 return;
24246 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24248 it->descent = slice.height - glyph_ascent;
24249 if (slice.y == 0)
24250 it->descent += img->vmargin;
24251 if (slice.y + slice.height == img->height)
24252 it->descent += img->vmargin;
24253 it->phys_descent = it->descent;
24255 it->pixel_width = slice.width;
24256 if (slice.x == 0)
24257 it->pixel_width += img->hmargin;
24258 if (slice.x + slice.width == img->width)
24259 it->pixel_width += img->hmargin;
24261 /* It's quite possible for images to have an ascent greater than
24262 their height, so don't get confused in that case. */
24263 if (it->descent < 0)
24264 it->descent = 0;
24266 it->nglyphs = 1;
24268 if (face->box != FACE_NO_BOX)
24270 if (face->box_line_width > 0)
24272 if (slice.y == 0)
24273 it->ascent += face->box_line_width;
24274 if (slice.y + slice.height == img->height)
24275 it->descent += face->box_line_width;
24278 if (it->start_of_box_run_p && slice.x == 0)
24279 it->pixel_width += eabs (face->box_line_width);
24280 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24281 it->pixel_width += eabs (face->box_line_width);
24284 take_vertical_position_into_account (it);
24286 /* Automatically crop wide image glyphs at right edge so we can
24287 draw the cursor on same display row. */
24288 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24289 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24291 it->pixel_width -= crop;
24292 slice.width -= crop;
24295 if (it->glyph_row)
24297 struct glyph *glyph;
24298 enum glyph_row_area area = it->area;
24300 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24301 if (glyph < it->glyph_row->glyphs[area + 1])
24303 glyph->charpos = CHARPOS (it->position);
24304 glyph->object = it->object;
24305 glyph->pixel_width = it->pixel_width;
24306 glyph->ascent = glyph_ascent;
24307 glyph->descent = it->descent;
24308 glyph->voffset = it->voffset;
24309 glyph->type = IMAGE_GLYPH;
24310 glyph->avoid_cursor_p = it->avoid_cursor_p;
24311 glyph->multibyte_p = it->multibyte_p;
24312 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24314 /* In R2L rows, the left and the right box edges need to be
24315 drawn in reverse direction. */
24316 glyph->right_box_line_p = it->start_of_box_run_p;
24317 glyph->left_box_line_p = it->end_of_box_run_p;
24319 else
24321 glyph->left_box_line_p = it->start_of_box_run_p;
24322 glyph->right_box_line_p = it->end_of_box_run_p;
24324 glyph->overlaps_vertically_p = 0;
24325 glyph->padding_p = 0;
24326 glyph->glyph_not_available_p = 0;
24327 glyph->face_id = it->face_id;
24328 glyph->u.img_id = img->id;
24329 glyph->slice.img = slice;
24330 glyph->font_type = FONT_TYPE_UNKNOWN;
24331 if (it->bidi_p)
24333 glyph->resolved_level = it->bidi_it.resolved_level;
24334 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24335 emacs_abort ();
24336 glyph->bidi_type = it->bidi_it.type;
24338 ++it->glyph_row->used[area];
24340 else
24341 IT_EXPAND_MATRIX_WIDTH (it, area);
24346 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24347 of the glyph, WIDTH and HEIGHT are the width and height of the
24348 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24350 static void
24351 append_stretch_glyph (struct it *it, Lisp_Object object,
24352 int width, int height, int ascent)
24354 struct glyph *glyph;
24355 enum glyph_row_area area = it->area;
24357 eassert (ascent >= 0 && ascent <= height);
24359 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24360 if (glyph < it->glyph_row->glyphs[area + 1])
24362 /* If the glyph row is reversed, we need to prepend the glyph
24363 rather than append it. */
24364 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24366 struct glyph *g;
24368 /* Make room for the additional glyph. */
24369 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24370 g[1] = *g;
24371 glyph = it->glyph_row->glyphs[area];
24373 glyph->charpos = CHARPOS (it->position);
24374 glyph->object = object;
24375 glyph->pixel_width = width;
24376 glyph->ascent = ascent;
24377 glyph->descent = height - ascent;
24378 glyph->voffset = it->voffset;
24379 glyph->type = STRETCH_GLYPH;
24380 glyph->avoid_cursor_p = it->avoid_cursor_p;
24381 glyph->multibyte_p = it->multibyte_p;
24382 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24384 /* In R2L rows, the left and the right box edges need to be
24385 drawn in reverse direction. */
24386 glyph->right_box_line_p = it->start_of_box_run_p;
24387 glyph->left_box_line_p = it->end_of_box_run_p;
24389 else
24391 glyph->left_box_line_p = it->start_of_box_run_p;
24392 glyph->right_box_line_p = it->end_of_box_run_p;
24394 glyph->overlaps_vertically_p = 0;
24395 glyph->padding_p = 0;
24396 glyph->glyph_not_available_p = 0;
24397 glyph->face_id = it->face_id;
24398 glyph->u.stretch.ascent = ascent;
24399 glyph->u.stretch.height = height;
24400 glyph->slice.img = null_glyph_slice;
24401 glyph->font_type = FONT_TYPE_UNKNOWN;
24402 if (it->bidi_p)
24404 glyph->resolved_level = it->bidi_it.resolved_level;
24405 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24406 emacs_abort ();
24407 glyph->bidi_type = it->bidi_it.type;
24409 else
24411 glyph->resolved_level = 0;
24412 glyph->bidi_type = UNKNOWN_BT;
24414 ++it->glyph_row->used[area];
24416 else
24417 IT_EXPAND_MATRIX_WIDTH (it, area);
24420 #endif /* HAVE_WINDOW_SYSTEM */
24422 /* Produce a stretch glyph for iterator IT. IT->object is the value
24423 of the glyph property displayed. The value must be a list
24424 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24425 being recognized:
24427 1. `:width WIDTH' specifies that the space should be WIDTH *
24428 canonical char width wide. WIDTH may be an integer or floating
24429 point number.
24431 2. `:relative-width FACTOR' specifies that the width of the stretch
24432 should be computed from the width of the first character having the
24433 `glyph' property, and should be FACTOR times that width.
24435 3. `:align-to HPOS' specifies that the space should be wide enough
24436 to reach HPOS, a value in canonical character units.
24438 Exactly one of the above pairs must be present.
24440 4. `:height HEIGHT' specifies that the height of the stretch produced
24441 should be HEIGHT, measured in canonical character units.
24443 5. `:relative-height FACTOR' specifies that the height of the
24444 stretch should be FACTOR times the height of the characters having
24445 the glyph property.
24447 Either none or exactly one of 4 or 5 must be present.
24449 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24450 of the stretch should be used for the ascent of the stretch.
24451 ASCENT must be in the range 0 <= ASCENT <= 100. */
24453 void
24454 produce_stretch_glyph (struct it *it)
24456 /* (space :width WIDTH :height HEIGHT ...) */
24457 Lisp_Object prop, plist;
24458 int width = 0, height = 0, align_to = -1;
24459 int zero_width_ok_p = 0;
24460 double tem;
24461 struct font *font = NULL;
24463 #ifdef HAVE_WINDOW_SYSTEM
24464 int ascent = 0;
24465 int zero_height_ok_p = 0;
24467 if (FRAME_WINDOW_P (it->f))
24469 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24470 font = face->font ? face->font : FRAME_FONT (it->f);
24471 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24473 #endif
24475 /* List should start with `space'. */
24476 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24477 plist = XCDR (it->object);
24479 /* Compute the width of the stretch. */
24480 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24481 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24483 /* Absolute width `:width WIDTH' specified and valid. */
24484 zero_width_ok_p = 1;
24485 width = (int)tem;
24487 #ifdef HAVE_WINDOW_SYSTEM
24488 else if (FRAME_WINDOW_P (it->f)
24489 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24491 /* Relative width `:relative-width FACTOR' specified and valid.
24492 Compute the width of the characters having the `glyph'
24493 property. */
24494 struct it it2;
24495 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24497 it2 = *it;
24498 if (it->multibyte_p)
24499 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24500 else
24502 it2.c = it2.char_to_display = *p, it2.len = 1;
24503 if (! ASCII_CHAR_P (it2.c))
24504 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24507 it2.glyph_row = NULL;
24508 it2.what = IT_CHARACTER;
24509 x_produce_glyphs (&it2);
24510 width = NUMVAL (prop) * it2.pixel_width;
24512 #endif /* HAVE_WINDOW_SYSTEM */
24513 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24514 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24516 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24517 align_to = (align_to < 0
24519 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24520 else if (align_to < 0)
24521 align_to = window_box_left_offset (it->w, TEXT_AREA);
24522 width = max (0, (int)tem + align_to - it->current_x);
24523 zero_width_ok_p = 1;
24525 else
24526 /* Nothing specified -> width defaults to canonical char width. */
24527 width = FRAME_COLUMN_WIDTH (it->f);
24529 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24530 width = 1;
24532 #ifdef HAVE_WINDOW_SYSTEM
24533 /* Compute height. */
24534 if (FRAME_WINDOW_P (it->f))
24536 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24537 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24539 height = (int)tem;
24540 zero_height_ok_p = 1;
24542 else if (prop = Fplist_get (plist, QCrelative_height),
24543 NUMVAL (prop) > 0)
24544 height = FONT_HEIGHT (font) * NUMVAL (prop);
24545 else
24546 height = FONT_HEIGHT (font);
24548 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24549 height = 1;
24551 /* Compute percentage of height used for ascent. If
24552 `:ascent ASCENT' is present and valid, use that. Otherwise,
24553 derive the ascent from the font in use. */
24554 if (prop = Fplist_get (plist, QCascent),
24555 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24556 ascent = height * NUMVAL (prop) / 100.0;
24557 else if (!NILP (prop)
24558 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24559 ascent = min (max (0, (int)tem), height);
24560 else
24561 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24563 else
24564 #endif /* HAVE_WINDOW_SYSTEM */
24565 height = 1;
24567 if (width > 0 && it->line_wrap != TRUNCATE
24568 && it->current_x + width > it->last_visible_x)
24570 width = it->last_visible_x - it->current_x;
24571 #ifdef HAVE_WINDOW_SYSTEM
24572 /* Subtract one more pixel from the stretch width, but only on
24573 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24574 width -= FRAME_WINDOW_P (it->f);
24575 #endif
24578 if (width > 0 && height > 0 && it->glyph_row)
24580 Lisp_Object o_object = it->object;
24581 Lisp_Object object = it->stack[it->sp - 1].string;
24582 int n = width;
24584 if (!STRINGP (object))
24585 object = it->w->contents;
24586 #ifdef HAVE_WINDOW_SYSTEM
24587 if (FRAME_WINDOW_P (it->f))
24588 append_stretch_glyph (it, object, width, height, ascent);
24589 else
24590 #endif
24592 it->object = object;
24593 it->char_to_display = ' ';
24594 it->pixel_width = it->len = 1;
24595 while (n--)
24596 tty_append_glyph (it);
24597 it->object = o_object;
24601 it->pixel_width = width;
24602 #ifdef HAVE_WINDOW_SYSTEM
24603 if (FRAME_WINDOW_P (it->f))
24605 it->ascent = it->phys_ascent = ascent;
24606 it->descent = it->phys_descent = height - it->ascent;
24607 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24608 take_vertical_position_into_account (it);
24610 else
24611 #endif
24612 it->nglyphs = width;
24615 /* Get information about special display element WHAT in an
24616 environment described by IT. WHAT is one of IT_TRUNCATION or
24617 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24618 non-null glyph_row member. This function ensures that fields like
24619 face_id, c, len of IT are left untouched. */
24621 static void
24622 produce_special_glyphs (struct it *it, enum display_element_type what)
24624 struct it temp_it;
24625 Lisp_Object gc;
24626 GLYPH glyph;
24628 temp_it = *it;
24629 temp_it.object = make_number (0);
24630 memset (&temp_it.current, 0, sizeof temp_it.current);
24632 if (what == IT_CONTINUATION)
24634 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24635 if (it->bidi_it.paragraph_dir == R2L)
24636 SET_GLYPH_FROM_CHAR (glyph, '/');
24637 else
24638 SET_GLYPH_FROM_CHAR (glyph, '\\');
24639 if (it->dp
24640 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24642 /* FIXME: Should we mirror GC for R2L lines? */
24643 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24644 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24647 else if (what == IT_TRUNCATION)
24649 /* Truncation glyph. */
24650 SET_GLYPH_FROM_CHAR (glyph, '$');
24651 if (it->dp
24652 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24654 /* FIXME: Should we mirror GC for R2L lines? */
24655 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24656 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24659 else
24660 emacs_abort ();
24662 #ifdef HAVE_WINDOW_SYSTEM
24663 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24664 is turned off, we precede the truncation/continuation glyphs by a
24665 stretch glyph whose width is computed such that these special
24666 glyphs are aligned at the window margin, even when very different
24667 fonts are used in different glyph rows. */
24668 if (FRAME_WINDOW_P (temp_it.f)
24669 /* init_iterator calls this with it->glyph_row == NULL, and it
24670 wants only the pixel width of the truncation/continuation
24671 glyphs. */
24672 && temp_it.glyph_row
24673 /* insert_left_trunc_glyphs calls us at the beginning of the
24674 row, and it has its own calculation of the stretch glyph
24675 width. */
24676 && temp_it.glyph_row->used[TEXT_AREA] > 0
24677 && (temp_it.glyph_row->reversed_p
24678 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24679 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24681 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24683 if (stretch_width > 0)
24685 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24686 struct font *font =
24687 face->font ? face->font : FRAME_FONT (temp_it.f);
24688 int stretch_ascent =
24689 (((temp_it.ascent + temp_it.descent)
24690 * FONT_BASE (font)) / FONT_HEIGHT (font));
24692 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24693 temp_it.ascent + temp_it.descent,
24694 stretch_ascent);
24697 #endif
24699 temp_it.dp = NULL;
24700 temp_it.what = IT_CHARACTER;
24701 temp_it.len = 1;
24702 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24703 temp_it.face_id = GLYPH_FACE (glyph);
24704 temp_it.len = CHAR_BYTES (temp_it.c);
24706 PRODUCE_GLYPHS (&temp_it);
24707 it->pixel_width = temp_it.pixel_width;
24708 it->nglyphs = temp_it.pixel_width;
24711 #ifdef HAVE_WINDOW_SYSTEM
24713 /* Calculate line-height and line-spacing properties.
24714 An integer value specifies explicit pixel value.
24715 A float value specifies relative value to current face height.
24716 A cons (float . face-name) specifies relative value to
24717 height of specified face font.
24719 Returns height in pixels, or nil. */
24722 static Lisp_Object
24723 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24724 int boff, int override)
24726 Lisp_Object face_name = Qnil;
24727 int ascent, descent, height;
24729 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24730 return val;
24732 if (CONSP (val))
24734 face_name = XCAR (val);
24735 val = XCDR (val);
24736 if (!NUMBERP (val))
24737 val = make_number (1);
24738 if (NILP (face_name))
24740 height = it->ascent + it->descent;
24741 goto scale;
24745 if (NILP (face_name))
24747 font = FRAME_FONT (it->f);
24748 boff = FRAME_BASELINE_OFFSET (it->f);
24750 else if (EQ (face_name, Qt))
24752 override = 0;
24754 else
24756 int face_id;
24757 struct face *face;
24759 face_id = lookup_named_face (it->f, face_name, 0);
24760 if (face_id < 0)
24761 return make_number (-1);
24763 face = FACE_FROM_ID (it->f, face_id);
24764 font = face->font;
24765 if (font == NULL)
24766 return make_number (-1);
24767 boff = font->baseline_offset;
24768 if (font->vertical_centering)
24769 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24772 ascent = FONT_BASE (font) + boff;
24773 descent = FONT_DESCENT (font) - boff;
24775 if (override)
24777 it->override_ascent = ascent;
24778 it->override_descent = descent;
24779 it->override_boff = boff;
24782 height = ascent + descent;
24784 scale:
24785 if (FLOATP (val))
24786 height = (int)(XFLOAT_DATA (val) * height);
24787 else if (INTEGERP (val))
24788 height *= XINT (val);
24790 return make_number (height);
24794 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24795 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24796 and only if this is for a character for which no font was found.
24798 If the display method (it->glyphless_method) is
24799 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24800 length of the acronym or the hexadecimal string, UPPER_XOFF and
24801 UPPER_YOFF are pixel offsets for the upper part of the string,
24802 LOWER_XOFF and LOWER_YOFF are for the lower part.
24804 For the other display methods, LEN through LOWER_YOFF are zero. */
24806 static void
24807 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24808 short upper_xoff, short upper_yoff,
24809 short lower_xoff, short lower_yoff)
24811 struct glyph *glyph;
24812 enum glyph_row_area area = it->area;
24814 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24815 if (glyph < it->glyph_row->glyphs[area + 1])
24817 /* If the glyph row is reversed, we need to prepend the glyph
24818 rather than append it. */
24819 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24821 struct glyph *g;
24823 /* Make room for the additional glyph. */
24824 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24825 g[1] = *g;
24826 glyph = it->glyph_row->glyphs[area];
24828 glyph->charpos = CHARPOS (it->position);
24829 glyph->object = it->object;
24830 glyph->pixel_width = it->pixel_width;
24831 glyph->ascent = it->ascent;
24832 glyph->descent = it->descent;
24833 glyph->voffset = it->voffset;
24834 glyph->type = GLYPHLESS_GLYPH;
24835 glyph->u.glyphless.method = it->glyphless_method;
24836 glyph->u.glyphless.for_no_font = for_no_font;
24837 glyph->u.glyphless.len = len;
24838 glyph->u.glyphless.ch = it->c;
24839 glyph->slice.glyphless.upper_xoff = upper_xoff;
24840 glyph->slice.glyphless.upper_yoff = upper_yoff;
24841 glyph->slice.glyphless.lower_xoff = lower_xoff;
24842 glyph->slice.glyphless.lower_yoff = lower_yoff;
24843 glyph->avoid_cursor_p = it->avoid_cursor_p;
24844 glyph->multibyte_p = it->multibyte_p;
24845 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24847 /* In R2L rows, the left and the right box edges need to be
24848 drawn in reverse direction. */
24849 glyph->right_box_line_p = it->start_of_box_run_p;
24850 glyph->left_box_line_p = it->end_of_box_run_p;
24852 else
24854 glyph->left_box_line_p = it->start_of_box_run_p;
24855 glyph->right_box_line_p = it->end_of_box_run_p;
24857 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24858 || it->phys_descent > it->descent);
24859 glyph->padding_p = 0;
24860 glyph->glyph_not_available_p = 0;
24861 glyph->face_id = face_id;
24862 glyph->font_type = FONT_TYPE_UNKNOWN;
24863 if (it->bidi_p)
24865 glyph->resolved_level = it->bidi_it.resolved_level;
24866 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24867 emacs_abort ();
24868 glyph->bidi_type = it->bidi_it.type;
24870 ++it->glyph_row->used[area];
24872 else
24873 IT_EXPAND_MATRIX_WIDTH (it, area);
24877 /* Produce a glyph for a glyphless character for iterator IT.
24878 IT->glyphless_method specifies which method to use for displaying
24879 the character. See the description of enum
24880 glyphless_display_method in dispextern.h for the detail.
24882 FOR_NO_FONT is nonzero if and only if this is for a character for
24883 which no font was found. ACRONYM, if non-nil, is an acronym string
24884 for the character. */
24886 static void
24887 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24889 int face_id;
24890 struct face *face;
24891 struct font *font;
24892 int base_width, base_height, width, height;
24893 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24894 int len;
24896 /* Get the metrics of the base font. We always refer to the current
24897 ASCII face. */
24898 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24899 font = face->font ? face->font : FRAME_FONT (it->f);
24900 it->ascent = FONT_BASE (font) + font->baseline_offset;
24901 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24902 base_height = it->ascent + it->descent;
24903 base_width = font->average_width;
24905 /* Get a face ID for the glyph by utilizing a cache (the same way as
24906 done for `escape-glyph' in get_next_display_element). */
24907 if (it->f == last_glyphless_glyph_frame
24908 && it->face_id == last_glyphless_glyph_face_id)
24910 face_id = last_glyphless_glyph_merged_face_id;
24912 else
24914 /* Merge the `glyphless-char' face into the current face. */
24915 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24916 last_glyphless_glyph_frame = it->f;
24917 last_glyphless_glyph_face_id = it->face_id;
24918 last_glyphless_glyph_merged_face_id = face_id;
24921 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24923 it->pixel_width = THIN_SPACE_WIDTH;
24924 len = 0;
24925 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24927 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24929 width = CHAR_WIDTH (it->c);
24930 if (width == 0)
24931 width = 1;
24932 else if (width > 4)
24933 width = 4;
24934 it->pixel_width = base_width * width;
24935 len = 0;
24936 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24938 else
24940 char buf[7];
24941 const char *str;
24942 unsigned int code[6];
24943 int upper_len;
24944 int ascent, descent;
24945 struct font_metrics metrics_upper, metrics_lower;
24947 face = FACE_FROM_ID (it->f, face_id);
24948 font = face->font ? face->font : FRAME_FONT (it->f);
24949 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24951 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24953 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24954 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24955 if (CONSP (acronym))
24956 acronym = XCAR (acronym);
24957 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24959 else
24961 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24962 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24963 str = buf;
24965 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24966 code[len] = font->driver->encode_char (font, str[len]);
24967 upper_len = (len + 1) / 2;
24968 font->driver->text_extents (font, code, upper_len,
24969 &metrics_upper);
24970 font->driver->text_extents (font, code + upper_len, len - upper_len,
24971 &metrics_lower);
24975 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24976 width = max (metrics_upper.width, metrics_lower.width) + 4;
24977 upper_xoff = upper_yoff = 2; /* the typical case */
24978 if (base_width >= width)
24980 /* Align the upper to the left, the lower to the right. */
24981 it->pixel_width = base_width;
24982 lower_xoff = base_width - 2 - metrics_lower.width;
24984 else
24986 /* Center the shorter one. */
24987 it->pixel_width = width;
24988 if (metrics_upper.width >= metrics_lower.width)
24989 lower_xoff = (width - metrics_lower.width) / 2;
24990 else
24992 /* FIXME: This code doesn't look right. It formerly was
24993 missing the "lower_xoff = 0;", which couldn't have
24994 been right since it left lower_xoff uninitialized. */
24995 lower_xoff = 0;
24996 upper_xoff = (width - metrics_upper.width) / 2;
25000 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25001 top, bottom, and between upper and lower strings. */
25002 height = (metrics_upper.ascent + metrics_upper.descent
25003 + metrics_lower.ascent + metrics_lower.descent) + 5;
25004 /* Center vertically.
25005 H:base_height, D:base_descent
25006 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25008 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25009 descent = D - H/2 + h/2;
25010 lower_yoff = descent - 2 - ld;
25011 upper_yoff = lower_yoff - la - 1 - ud; */
25012 ascent = - (it->descent - (base_height + height + 1) / 2);
25013 descent = it->descent - (base_height - height) / 2;
25014 lower_yoff = descent - 2 - metrics_lower.descent;
25015 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25016 - metrics_upper.descent);
25017 /* Don't make the height shorter than the base height. */
25018 if (height > base_height)
25020 it->ascent = ascent;
25021 it->descent = descent;
25025 it->phys_ascent = it->ascent;
25026 it->phys_descent = it->descent;
25027 if (it->glyph_row)
25028 append_glyphless_glyph (it, face_id, for_no_font, len,
25029 upper_xoff, upper_yoff,
25030 lower_xoff, lower_yoff);
25031 it->nglyphs = 1;
25032 take_vertical_position_into_account (it);
25036 /* RIF:
25037 Produce glyphs/get display metrics for the display element IT is
25038 loaded with. See the description of struct it in dispextern.h
25039 for an overview of struct it. */
25041 void
25042 x_produce_glyphs (struct it *it)
25044 int extra_line_spacing = it->extra_line_spacing;
25046 it->glyph_not_available_p = 0;
25048 if (it->what == IT_CHARACTER)
25050 XChar2b char2b;
25051 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25052 struct font *font = face->font;
25053 struct font_metrics *pcm = NULL;
25054 int boff; /* baseline offset */
25056 if (font == NULL)
25058 /* When no suitable font is found, display this character by
25059 the method specified in the first extra slot of
25060 Vglyphless_char_display. */
25061 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25063 eassert (it->what == IT_GLYPHLESS);
25064 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25065 goto done;
25068 boff = font->baseline_offset;
25069 if (font->vertical_centering)
25070 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25072 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25074 int stretched_p;
25076 it->nglyphs = 1;
25078 if (it->override_ascent >= 0)
25080 it->ascent = it->override_ascent;
25081 it->descent = it->override_descent;
25082 boff = it->override_boff;
25084 else
25086 it->ascent = FONT_BASE (font) + boff;
25087 it->descent = FONT_DESCENT (font) - boff;
25090 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25092 pcm = get_per_char_metric (font, &char2b);
25093 if (pcm->width == 0
25094 && pcm->rbearing == 0 && pcm->lbearing == 0)
25095 pcm = NULL;
25098 if (pcm)
25100 it->phys_ascent = pcm->ascent + boff;
25101 it->phys_descent = pcm->descent - boff;
25102 it->pixel_width = pcm->width;
25104 else
25106 it->glyph_not_available_p = 1;
25107 it->phys_ascent = it->ascent;
25108 it->phys_descent = it->descent;
25109 it->pixel_width = font->space_width;
25112 if (it->constrain_row_ascent_descent_p)
25114 if (it->descent > it->max_descent)
25116 it->ascent += it->descent - it->max_descent;
25117 it->descent = it->max_descent;
25119 if (it->ascent > it->max_ascent)
25121 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25122 it->ascent = it->max_ascent;
25124 it->phys_ascent = min (it->phys_ascent, it->ascent);
25125 it->phys_descent = min (it->phys_descent, it->descent);
25126 extra_line_spacing = 0;
25129 /* If this is a space inside a region of text with
25130 `space-width' property, change its width. */
25131 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25132 if (stretched_p)
25133 it->pixel_width *= XFLOATINT (it->space_width);
25135 /* If face has a box, add the box thickness to the character
25136 height. If character has a box line to the left and/or
25137 right, add the box line width to the character's width. */
25138 if (face->box != FACE_NO_BOX)
25140 int thick = face->box_line_width;
25142 if (thick > 0)
25144 it->ascent += thick;
25145 it->descent += thick;
25147 else
25148 thick = -thick;
25150 if (it->start_of_box_run_p)
25151 it->pixel_width += thick;
25152 if (it->end_of_box_run_p)
25153 it->pixel_width += thick;
25156 /* If face has an overline, add the height of the overline
25157 (1 pixel) and a 1 pixel margin to the character height. */
25158 if (face->overline_p)
25159 it->ascent += overline_margin;
25161 if (it->constrain_row_ascent_descent_p)
25163 if (it->ascent > it->max_ascent)
25164 it->ascent = it->max_ascent;
25165 if (it->descent > it->max_descent)
25166 it->descent = it->max_descent;
25169 take_vertical_position_into_account (it);
25171 /* If we have to actually produce glyphs, do it. */
25172 if (it->glyph_row)
25174 if (stretched_p)
25176 /* Translate a space with a `space-width' property
25177 into a stretch glyph. */
25178 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25179 / FONT_HEIGHT (font));
25180 append_stretch_glyph (it, it->object, it->pixel_width,
25181 it->ascent + it->descent, ascent);
25183 else
25184 append_glyph (it);
25186 /* If characters with lbearing or rbearing are displayed
25187 in this line, record that fact in a flag of the
25188 glyph row. This is used to optimize X output code. */
25189 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25190 it->glyph_row->contains_overlapping_glyphs_p = 1;
25192 if (! stretched_p && it->pixel_width == 0)
25193 /* We assure that all visible glyphs have at least 1-pixel
25194 width. */
25195 it->pixel_width = 1;
25197 else if (it->char_to_display == '\n')
25199 /* A newline has no width, but we need the height of the
25200 line. But if previous part of the line sets a height,
25201 don't increase that height */
25203 Lisp_Object height;
25204 Lisp_Object total_height = Qnil;
25206 it->override_ascent = -1;
25207 it->pixel_width = 0;
25208 it->nglyphs = 0;
25210 height = get_it_property (it, Qline_height);
25211 /* Split (line-height total-height) list */
25212 if (CONSP (height)
25213 && CONSP (XCDR (height))
25214 && NILP (XCDR (XCDR (height))))
25216 total_height = XCAR (XCDR (height));
25217 height = XCAR (height);
25219 height = calc_line_height_property (it, height, font, boff, 1);
25221 if (it->override_ascent >= 0)
25223 it->ascent = it->override_ascent;
25224 it->descent = it->override_descent;
25225 boff = it->override_boff;
25227 else
25229 it->ascent = FONT_BASE (font) + boff;
25230 it->descent = FONT_DESCENT (font) - boff;
25233 if (EQ (height, Qt))
25235 if (it->descent > it->max_descent)
25237 it->ascent += it->descent - it->max_descent;
25238 it->descent = it->max_descent;
25240 if (it->ascent > it->max_ascent)
25242 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25243 it->ascent = it->max_ascent;
25245 it->phys_ascent = min (it->phys_ascent, it->ascent);
25246 it->phys_descent = min (it->phys_descent, it->descent);
25247 it->constrain_row_ascent_descent_p = 1;
25248 extra_line_spacing = 0;
25250 else
25252 Lisp_Object spacing;
25254 it->phys_ascent = it->ascent;
25255 it->phys_descent = it->descent;
25257 if ((it->max_ascent > 0 || it->max_descent > 0)
25258 && face->box != FACE_NO_BOX
25259 && face->box_line_width > 0)
25261 it->ascent += face->box_line_width;
25262 it->descent += face->box_line_width;
25264 if (!NILP (height)
25265 && XINT (height) > it->ascent + it->descent)
25266 it->ascent = XINT (height) - it->descent;
25268 if (!NILP (total_height))
25269 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25270 else
25272 spacing = get_it_property (it, Qline_spacing);
25273 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25275 if (INTEGERP (spacing))
25277 extra_line_spacing = XINT (spacing);
25278 if (!NILP (total_height))
25279 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25283 else /* i.e. (it->char_to_display == '\t') */
25285 if (font->space_width > 0)
25287 int tab_width = it->tab_width * font->space_width;
25288 int x = it->current_x + it->continuation_lines_width;
25289 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25291 /* If the distance from the current position to the next tab
25292 stop is less than a space character width, use the
25293 tab stop after that. */
25294 if (next_tab_x - x < font->space_width)
25295 next_tab_x += tab_width;
25297 it->pixel_width = next_tab_x - x;
25298 it->nglyphs = 1;
25299 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25300 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25302 if (it->glyph_row)
25304 append_stretch_glyph (it, it->object, it->pixel_width,
25305 it->ascent + it->descent, it->ascent);
25308 else
25310 it->pixel_width = 0;
25311 it->nglyphs = 1;
25315 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25317 /* A static composition.
25319 Note: A composition is represented as one glyph in the
25320 glyph matrix. There are no padding glyphs.
25322 Important note: pixel_width, ascent, and descent are the
25323 values of what is drawn by draw_glyphs (i.e. the values of
25324 the overall glyphs composed). */
25325 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25326 int boff; /* baseline offset */
25327 struct composition *cmp = composition_table[it->cmp_it.id];
25328 int glyph_len = cmp->glyph_len;
25329 struct font *font = face->font;
25331 it->nglyphs = 1;
25333 /* If we have not yet calculated pixel size data of glyphs of
25334 the composition for the current face font, calculate them
25335 now. Theoretically, we have to check all fonts for the
25336 glyphs, but that requires much time and memory space. So,
25337 here we check only the font of the first glyph. This may
25338 lead to incorrect display, but it's very rare, and C-l
25339 (recenter-top-bottom) can correct the display anyway. */
25340 if (! cmp->font || cmp->font != font)
25342 /* Ascent and descent of the font of the first character
25343 of this composition (adjusted by baseline offset).
25344 Ascent and descent of overall glyphs should not be less
25345 than these, respectively. */
25346 int font_ascent, font_descent, font_height;
25347 /* Bounding box of the overall glyphs. */
25348 int leftmost, rightmost, lowest, highest;
25349 int lbearing, rbearing;
25350 int i, width, ascent, descent;
25351 int left_padded = 0, right_padded = 0;
25352 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25353 XChar2b char2b;
25354 struct font_metrics *pcm;
25355 int font_not_found_p;
25356 ptrdiff_t pos;
25358 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25359 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25360 break;
25361 if (glyph_len < cmp->glyph_len)
25362 right_padded = 1;
25363 for (i = 0; i < glyph_len; i++)
25365 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25366 break;
25367 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25369 if (i > 0)
25370 left_padded = 1;
25372 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25373 : IT_CHARPOS (*it));
25374 /* If no suitable font is found, use the default font. */
25375 font_not_found_p = font == NULL;
25376 if (font_not_found_p)
25378 face = face->ascii_face;
25379 font = face->font;
25381 boff = font->baseline_offset;
25382 if (font->vertical_centering)
25383 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25384 font_ascent = FONT_BASE (font) + boff;
25385 font_descent = FONT_DESCENT (font) - boff;
25386 font_height = FONT_HEIGHT (font);
25388 cmp->font = font;
25390 pcm = NULL;
25391 if (! font_not_found_p)
25393 get_char_face_and_encoding (it->f, c, it->face_id,
25394 &char2b, 0);
25395 pcm = get_per_char_metric (font, &char2b);
25398 /* Initialize the bounding box. */
25399 if (pcm)
25401 width = cmp->glyph_len > 0 ? pcm->width : 0;
25402 ascent = pcm->ascent;
25403 descent = pcm->descent;
25404 lbearing = pcm->lbearing;
25405 rbearing = pcm->rbearing;
25407 else
25409 width = cmp->glyph_len > 0 ? font->space_width : 0;
25410 ascent = FONT_BASE (font);
25411 descent = FONT_DESCENT (font);
25412 lbearing = 0;
25413 rbearing = width;
25416 rightmost = width;
25417 leftmost = 0;
25418 lowest = - descent + boff;
25419 highest = ascent + boff;
25421 if (! font_not_found_p
25422 && font->default_ascent
25423 && CHAR_TABLE_P (Vuse_default_ascent)
25424 && !NILP (Faref (Vuse_default_ascent,
25425 make_number (it->char_to_display))))
25426 highest = font->default_ascent + boff;
25428 /* Draw the first glyph at the normal position. It may be
25429 shifted to right later if some other glyphs are drawn
25430 at the left. */
25431 cmp->offsets[i * 2] = 0;
25432 cmp->offsets[i * 2 + 1] = boff;
25433 cmp->lbearing = lbearing;
25434 cmp->rbearing = rbearing;
25436 /* Set cmp->offsets for the remaining glyphs. */
25437 for (i++; i < glyph_len; i++)
25439 int left, right, btm, top;
25440 int ch = COMPOSITION_GLYPH (cmp, i);
25441 int face_id;
25442 struct face *this_face;
25444 if (ch == '\t')
25445 ch = ' ';
25446 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25447 this_face = FACE_FROM_ID (it->f, face_id);
25448 font = this_face->font;
25450 if (font == NULL)
25451 pcm = NULL;
25452 else
25454 get_char_face_and_encoding (it->f, ch, face_id,
25455 &char2b, 0);
25456 pcm = get_per_char_metric (font, &char2b);
25458 if (! pcm)
25459 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25460 else
25462 width = pcm->width;
25463 ascent = pcm->ascent;
25464 descent = pcm->descent;
25465 lbearing = pcm->lbearing;
25466 rbearing = pcm->rbearing;
25467 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25469 /* Relative composition with or without
25470 alternate chars. */
25471 left = (leftmost + rightmost - width) / 2;
25472 btm = - descent + boff;
25473 if (font->relative_compose
25474 && (! CHAR_TABLE_P (Vignore_relative_composition)
25475 || NILP (Faref (Vignore_relative_composition,
25476 make_number (ch)))))
25479 if (- descent >= font->relative_compose)
25480 /* One extra pixel between two glyphs. */
25481 btm = highest + 1;
25482 else if (ascent <= 0)
25483 /* One extra pixel between two glyphs. */
25484 btm = lowest - 1 - ascent - descent;
25487 else
25489 /* A composition rule is specified by an integer
25490 value that encodes global and new reference
25491 points (GREF and NREF). GREF and NREF are
25492 specified by numbers as below:
25494 0---1---2 -- ascent
25498 9--10--11 -- center
25500 ---3---4---5--- baseline
25502 6---7---8 -- descent
25504 int rule = COMPOSITION_RULE (cmp, i);
25505 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25507 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25508 grefx = gref % 3, nrefx = nref % 3;
25509 grefy = gref / 3, nrefy = nref / 3;
25510 if (xoff)
25511 xoff = font_height * (xoff - 128) / 256;
25512 if (yoff)
25513 yoff = font_height * (yoff - 128) / 256;
25515 left = (leftmost
25516 + grefx * (rightmost - leftmost) / 2
25517 - nrefx * width / 2
25518 + xoff);
25520 btm = ((grefy == 0 ? highest
25521 : grefy == 1 ? 0
25522 : grefy == 2 ? lowest
25523 : (highest + lowest) / 2)
25524 - (nrefy == 0 ? ascent + descent
25525 : nrefy == 1 ? descent - boff
25526 : nrefy == 2 ? 0
25527 : (ascent + descent) / 2)
25528 + yoff);
25531 cmp->offsets[i * 2] = left;
25532 cmp->offsets[i * 2 + 1] = btm + descent;
25534 /* Update the bounding box of the overall glyphs. */
25535 if (width > 0)
25537 right = left + width;
25538 if (left < leftmost)
25539 leftmost = left;
25540 if (right > rightmost)
25541 rightmost = right;
25543 top = btm + descent + ascent;
25544 if (top > highest)
25545 highest = top;
25546 if (btm < lowest)
25547 lowest = btm;
25549 if (cmp->lbearing > left + lbearing)
25550 cmp->lbearing = left + lbearing;
25551 if (cmp->rbearing < left + rbearing)
25552 cmp->rbearing = left + rbearing;
25556 /* If there are glyphs whose x-offsets are negative,
25557 shift all glyphs to the right and make all x-offsets
25558 non-negative. */
25559 if (leftmost < 0)
25561 for (i = 0; i < cmp->glyph_len; i++)
25562 cmp->offsets[i * 2] -= leftmost;
25563 rightmost -= leftmost;
25564 cmp->lbearing -= leftmost;
25565 cmp->rbearing -= leftmost;
25568 if (left_padded && cmp->lbearing < 0)
25570 for (i = 0; i < cmp->glyph_len; i++)
25571 cmp->offsets[i * 2] -= cmp->lbearing;
25572 rightmost -= cmp->lbearing;
25573 cmp->rbearing -= cmp->lbearing;
25574 cmp->lbearing = 0;
25576 if (right_padded && rightmost < cmp->rbearing)
25578 rightmost = cmp->rbearing;
25581 cmp->pixel_width = rightmost;
25582 cmp->ascent = highest;
25583 cmp->descent = - lowest;
25584 if (cmp->ascent < font_ascent)
25585 cmp->ascent = font_ascent;
25586 if (cmp->descent < font_descent)
25587 cmp->descent = font_descent;
25590 if (it->glyph_row
25591 && (cmp->lbearing < 0
25592 || cmp->rbearing > cmp->pixel_width))
25593 it->glyph_row->contains_overlapping_glyphs_p = 1;
25595 it->pixel_width = cmp->pixel_width;
25596 it->ascent = it->phys_ascent = cmp->ascent;
25597 it->descent = it->phys_descent = cmp->descent;
25598 if (face->box != FACE_NO_BOX)
25600 int thick = face->box_line_width;
25602 if (thick > 0)
25604 it->ascent += thick;
25605 it->descent += thick;
25607 else
25608 thick = - thick;
25610 if (it->start_of_box_run_p)
25611 it->pixel_width += thick;
25612 if (it->end_of_box_run_p)
25613 it->pixel_width += thick;
25616 /* If face has an overline, add the height of the overline
25617 (1 pixel) and a 1 pixel margin to the character height. */
25618 if (face->overline_p)
25619 it->ascent += overline_margin;
25621 take_vertical_position_into_account (it);
25622 if (it->ascent < 0)
25623 it->ascent = 0;
25624 if (it->descent < 0)
25625 it->descent = 0;
25627 if (it->glyph_row && cmp->glyph_len > 0)
25628 append_composite_glyph (it);
25630 else if (it->what == IT_COMPOSITION)
25632 /* A dynamic (automatic) composition. */
25633 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25634 Lisp_Object gstring;
25635 struct font_metrics metrics;
25637 it->nglyphs = 1;
25639 gstring = composition_gstring_from_id (it->cmp_it.id);
25640 it->pixel_width
25641 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25642 &metrics);
25643 if (it->glyph_row
25644 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25645 it->glyph_row->contains_overlapping_glyphs_p = 1;
25646 it->ascent = it->phys_ascent = metrics.ascent;
25647 it->descent = it->phys_descent = metrics.descent;
25648 if (face->box != FACE_NO_BOX)
25650 int thick = face->box_line_width;
25652 if (thick > 0)
25654 it->ascent += thick;
25655 it->descent += thick;
25657 else
25658 thick = - thick;
25660 if (it->start_of_box_run_p)
25661 it->pixel_width += thick;
25662 if (it->end_of_box_run_p)
25663 it->pixel_width += thick;
25665 /* If face has an overline, add the height of the overline
25666 (1 pixel) and a 1 pixel margin to the character height. */
25667 if (face->overline_p)
25668 it->ascent += overline_margin;
25669 take_vertical_position_into_account (it);
25670 if (it->ascent < 0)
25671 it->ascent = 0;
25672 if (it->descent < 0)
25673 it->descent = 0;
25675 if (it->glyph_row)
25676 append_composite_glyph (it);
25678 else if (it->what == IT_GLYPHLESS)
25679 produce_glyphless_glyph (it, 0, Qnil);
25680 else if (it->what == IT_IMAGE)
25681 produce_image_glyph (it);
25682 else if (it->what == IT_STRETCH)
25683 produce_stretch_glyph (it);
25685 done:
25686 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25687 because this isn't true for images with `:ascent 100'. */
25688 eassert (it->ascent >= 0 && it->descent >= 0);
25689 if (it->area == TEXT_AREA)
25690 it->current_x += it->pixel_width;
25692 if (extra_line_spacing > 0)
25694 it->descent += extra_line_spacing;
25695 if (extra_line_spacing > it->max_extra_line_spacing)
25696 it->max_extra_line_spacing = extra_line_spacing;
25699 it->max_ascent = max (it->max_ascent, it->ascent);
25700 it->max_descent = max (it->max_descent, it->descent);
25701 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25702 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25705 /* EXPORT for RIF:
25706 Output LEN glyphs starting at START at the nominal cursor position.
25707 Advance the nominal cursor over the text. The global variable
25708 updated_window contains the window being updated, updated_row is
25709 the glyph row being updated, and updated_area is the area of that
25710 row being updated. */
25712 void
25713 x_write_glyphs (struct glyph *start, int len)
25715 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25717 eassert (updated_window && updated_row);
25718 /* When the window is hscrolled, cursor hpos can legitimately be out
25719 of bounds, but we draw the cursor at the corresponding window
25720 margin in that case. */
25721 if (!updated_row->reversed_p && chpos < 0)
25722 chpos = 0;
25723 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25724 chpos = updated_row->used[TEXT_AREA] - 1;
25726 block_input ();
25728 /* Write glyphs. */
25730 hpos = start - updated_row->glyphs[updated_area];
25731 x = draw_glyphs (updated_window, output_cursor.x,
25732 updated_row, updated_area,
25733 hpos, hpos + len,
25734 DRAW_NORMAL_TEXT, 0);
25736 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25737 if (updated_area == TEXT_AREA
25738 && updated_window->phys_cursor_on_p
25739 && updated_window->phys_cursor.vpos == output_cursor.vpos
25740 && chpos >= hpos
25741 && chpos < hpos + len)
25742 updated_window->phys_cursor_on_p = 0;
25744 unblock_input ();
25746 /* Advance the output cursor. */
25747 output_cursor.hpos += len;
25748 output_cursor.x = x;
25752 /* EXPORT for RIF:
25753 Insert LEN glyphs from START at the nominal cursor position. */
25755 void
25756 x_insert_glyphs (struct glyph *start, int len)
25758 struct frame *f;
25759 struct window *w;
25760 int line_height, shift_by_width, shifted_region_width;
25761 struct glyph_row *row;
25762 struct glyph *glyph;
25763 int frame_x, frame_y;
25764 ptrdiff_t hpos;
25766 eassert (updated_window && updated_row);
25767 block_input ();
25768 w = updated_window;
25769 f = XFRAME (WINDOW_FRAME (w));
25771 /* Get the height of the line we are in. */
25772 row = updated_row;
25773 line_height = row->height;
25775 /* Get the width of the glyphs to insert. */
25776 shift_by_width = 0;
25777 for (glyph = start; glyph < start + len; ++glyph)
25778 shift_by_width += glyph->pixel_width;
25780 /* Get the width of the region to shift right. */
25781 shifted_region_width = (window_box_width (w, updated_area)
25782 - output_cursor.x
25783 - shift_by_width);
25785 /* Shift right. */
25786 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25787 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25789 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25790 line_height, shift_by_width);
25792 /* Write the glyphs. */
25793 hpos = start - row->glyphs[updated_area];
25794 draw_glyphs (w, output_cursor.x, row, updated_area,
25795 hpos, hpos + len,
25796 DRAW_NORMAL_TEXT, 0);
25798 /* Advance the output cursor. */
25799 output_cursor.hpos += len;
25800 output_cursor.x += shift_by_width;
25801 unblock_input ();
25805 /* EXPORT for RIF:
25806 Erase the current text line from the nominal cursor position
25807 (inclusive) to pixel column TO_X (exclusive). The idea is that
25808 everything from TO_X onward is already erased.
25810 TO_X is a pixel position relative to updated_area of
25811 updated_window. TO_X == -1 means clear to the end of this area. */
25813 void
25814 x_clear_end_of_line (int to_x)
25816 struct frame *f;
25817 struct window *w = updated_window;
25818 int max_x, min_y, max_y;
25819 int from_x, from_y, to_y;
25821 eassert (updated_window && updated_row);
25822 f = XFRAME (w->frame);
25824 if (updated_row->full_width_p)
25825 max_x = WINDOW_TOTAL_WIDTH (w);
25826 else
25827 max_x = window_box_width (w, updated_area);
25828 max_y = window_text_bottom_y (w);
25830 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25831 of window. For TO_X > 0, truncate to end of drawing area. */
25832 if (to_x == 0)
25833 return;
25834 else if (to_x < 0)
25835 to_x = max_x;
25836 else
25837 to_x = min (to_x, max_x);
25839 to_y = min (max_y, output_cursor.y + updated_row->height);
25841 /* Notice if the cursor will be cleared by this operation. */
25842 if (!updated_row->full_width_p)
25843 notice_overwritten_cursor (w, updated_area,
25844 output_cursor.x, -1,
25845 updated_row->y,
25846 MATRIX_ROW_BOTTOM_Y (updated_row));
25848 from_x = output_cursor.x;
25850 /* Translate to frame coordinates. */
25851 if (updated_row->full_width_p)
25853 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25854 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25856 else
25858 int area_left = window_box_left (w, updated_area);
25859 from_x += area_left;
25860 to_x += area_left;
25863 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25864 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25865 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25867 /* Prevent inadvertently clearing to end of the X window. */
25868 if (to_x > from_x && to_y > from_y)
25870 block_input ();
25871 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25872 to_x - from_x, to_y - from_y);
25873 unblock_input ();
25877 #endif /* HAVE_WINDOW_SYSTEM */
25881 /***********************************************************************
25882 Cursor types
25883 ***********************************************************************/
25885 /* Value is the internal representation of the specified cursor type
25886 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25887 of the bar cursor. */
25889 static enum text_cursor_kinds
25890 get_specified_cursor_type (Lisp_Object arg, int *width)
25892 enum text_cursor_kinds type;
25894 if (NILP (arg))
25895 return NO_CURSOR;
25897 if (EQ (arg, Qbox))
25898 return FILLED_BOX_CURSOR;
25900 if (EQ (arg, Qhollow))
25901 return HOLLOW_BOX_CURSOR;
25903 if (EQ (arg, Qbar))
25905 *width = 2;
25906 return BAR_CURSOR;
25909 if (CONSP (arg)
25910 && EQ (XCAR (arg), Qbar)
25911 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25913 *width = XINT (XCDR (arg));
25914 return BAR_CURSOR;
25917 if (EQ (arg, Qhbar))
25919 *width = 2;
25920 return HBAR_CURSOR;
25923 if (CONSP (arg)
25924 && EQ (XCAR (arg), Qhbar)
25925 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25927 *width = XINT (XCDR (arg));
25928 return HBAR_CURSOR;
25931 /* Treat anything unknown as "hollow box cursor".
25932 It was bad to signal an error; people have trouble fixing
25933 .Xdefaults with Emacs, when it has something bad in it. */
25934 type = HOLLOW_BOX_CURSOR;
25936 return type;
25939 /* Set the default cursor types for specified frame. */
25940 void
25941 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25943 int width = 1;
25944 Lisp_Object tem;
25946 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25947 FRAME_CURSOR_WIDTH (f) = width;
25949 /* By default, set up the blink-off state depending on the on-state. */
25951 tem = Fassoc (arg, Vblink_cursor_alist);
25952 if (!NILP (tem))
25954 FRAME_BLINK_OFF_CURSOR (f)
25955 = get_specified_cursor_type (XCDR (tem), &width);
25956 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25958 else
25959 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25963 #ifdef HAVE_WINDOW_SYSTEM
25965 /* Return the cursor we want to be displayed in window W. Return
25966 width of bar/hbar cursor through WIDTH arg. Return with
25967 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25968 (i.e. if the `system caret' should track this cursor).
25970 In a mini-buffer window, we want the cursor only to appear if we
25971 are reading input from this window. For the selected window, we
25972 want the cursor type given by the frame parameter or buffer local
25973 setting of cursor-type. If explicitly marked off, draw no cursor.
25974 In all other cases, we want a hollow box cursor. */
25976 static enum text_cursor_kinds
25977 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25978 int *active_cursor)
25980 struct frame *f = XFRAME (w->frame);
25981 struct buffer *b = XBUFFER (w->contents);
25982 int cursor_type = DEFAULT_CURSOR;
25983 Lisp_Object alt_cursor;
25984 int non_selected = 0;
25986 *active_cursor = 1;
25988 /* Echo area */
25989 if (cursor_in_echo_area
25990 && FRAME_HAS_MINIBUF_P (f)
25991 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25993 if (w == XWINDOW (echo_area_window))
25995 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25997 *width = FRAME_CURSOR_WIDTH (f);
25998 return FRAME_DESIRED_CURSOR (f);
26000 else
26001 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26004 *active_cursor = 0;
26005 non_selected = 1;
26008 /* Detect a nonselected window or nonselected frame. */
26009 else if (w != XWINDOW (f->selected_window)
26010 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26012 *active_cursor = 0;
26014 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26015 return NO_CURSOR;
26017 non_selected = 1;
26020 /* Never display a cursor in a window in which cursor-type is nil. */
26021 if (NILP (BVAR (b, cursor_type)))
26022 return NO_CURSOR;
26024 /* Get the normal cursor type for this window. */
26025 if (EQ (BVAR (b, cursor_type), Qt))
26027 cursor_type = FRAME_DESIRED_CURSOR (f);
26028 *width = FRAME_CURSOR_WIDTH (f);
26030 else
26031 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26033 /* Use cursor-in-non-selected-windows instead
26034 for non-selected window or frame. */
26035 if (non_selected)
26037 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26038 if (!EQ (Qt, alt_cursor))
26039 return get_specified_cursor_type (alt_cursor, width);
26040 /* t means modify the normal cursor type. */
26041 if (cursor_type == FILLED_BOX_CURSOR)
26042 cursor_type = HOLLOW_BOX_CURSOR;
26043 else if (cursor_type == BAR_CURSOR && *width > 1)
26044 --*width;
26045 return cursor_type;
26048 /* Use normal cursor if not blinked off. */
26049 if (!w->cursor_off_p)
26051 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26053 if (cursor_type == FILLED_BOX_CURSOR)
26055 /* Using a block cursor on large images can be very annoying.
26056 So use a hollow cursor for "large" images.
26057 If image is not transparent (no mask), also use hollow cursor. */
26058 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26059 if (img != NULL && IMAGEP (img->spec))
26061 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26062 where N = size of default frame font size.
26063 This should cover most of the "tiny" icons people may use. */
26064 if (!img->mask
26065 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26066 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26067 cursor_type = HOLLOW_BOX_CURSOR;
26070 else if (cursor_type != NO_CURSOR)
26072 /* Display current only supports BOX and HOLLOW cursors for images.
26073 So for now, unconditionally use a HOLLOW cursor when cursor is
26074 not a solid box cursor. */
26075 cursor_type = HOLLOW_BOX_CURSOR;
26078 return cursor_type;
26081 /* Cursor is blinked off, so determine how to "toggle" it. */
26083 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26084 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26085 return get_specified_cursor_type (XCDR (alt_cursor), width);
26087 /* Then see if frame has specified a specific blink off cursor type. */
26088 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26090 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26091 return FRAME_BLINK_OFF_CURSOR (f);
26094 #if 0
26095 /* Some people liked having a permanently visible blinking cursor,
26096 while others had very strong opinions against it. So it was
26097 decided to remove it. KFS 2003-09-03 */
26099 /* Finally perform built-in cursor blinking:
26100 filled box <-> hollow box
26101 wide [h]bar <-> narrow [h]bar
26102 narrow [h]bar <-> no cursor
26103 other type <-> no cursor */
26105 if (cursor_type == FILLED_BOX_CURSOR)
26106 return HOLLOW_BOX_CURSOR;
26108 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26110 *width = 1;
26111 return cursor_type;
26113 #endif
26115 return NO_CURSOR;
26119 /* Notice when the text cursor of window W has been completely
26120 overwritten by a drawing operation that outputs glyphs in AREA
26121 starting at X0 and ending at X1 in the line starting at Y0 and
26122 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26123 the rest of the line after X0 has been written. Y coordinates
26124 are window-relative. */
26126 static void
26127 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26128 int x0, int x1, int y0, int y1)
26130 int cx0, cx1, cy0, cy1;
26131 struct glyph_row *row;
26133 if (!w->phys_cursor_on_p)
26134 return;
26135 if (area != TEXT_AREA)
26136 return;
26138 if (w->phys_cursor.vpos < 0
26139 || w->phys_cursor.vpos >= w->current_matrix->nrows
26140 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26141 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26142 return;
26144 if (row->cursor_in_fringe_p)
26146 row->cursor_in_fringe_p = 0;
26147 draw_fringe_bitmap (w, row, row->reversed_p);
26148 w->phys_cursor_on_p = 0;
26149 return;
26152 cx0 = w->phys_cursor.x;
26153 cx1 = cx0 + w->phys_cursor_width;
26154 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26155 return;
26157 /* The cursor image will be completely removed from the
26158 screen if the output area intersects the cursor area in
26159 y-direction. When we draw in [y0 y1[, and some part of
26160 the cursor is at y < y0, that part must have been drawn
26161 before. When scrolling, the cursor is erased before
26162 actually scrolling, so we don't come here. When not
26163 scrolling, the rows above the old cursor row must have
26164 changed, and in this case these rows must have written
26165 over the cursor image.
26167 Likewise if part of the cursor is below y1, with the
26168 exception of the cursor being in the first blank row at
26169 the buffer and window end because update_text_area
26170 doesn't draw that row. (Except when it does, but
26171 that's handled in update_text_area.) */
26173 cy0 = w->phys_cursor.y;
26174 cy1 = cy0 + w->phys_cursor_height;
26175 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26176 return;
26178 w->phys_cursor_on_p = 0;
26181 #endif /* HAVE_WINDOW_SYSTEM */
26184 /************************************************************************
26185 Mouse Face
26186 ************************************************************************/
26188 #ifdef HAVE_WINDOW_SYSTEM
26190 /* EXPORT for RIF:
26191 Fix the display of area AREA of overlapping row ROW in window W
26192 with respect to the overlapping part OVERLAPS. */
26194 void
26195 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26196 enum glyph_row_area area, int overlaps)
26198 int i, x;
26200 block_input ();
26202 x = 0;
26203 for (i = 0; i < row->used[area];)
26205 if (row->glyphs[area][i].overlaps_vertically_p)
26207 int start = i, start_x = x;
26211 x += row->glyphs[area][i].pixel_width;
26212 ++i;
26214 while (i < row->used[area]
26215 && row->glyphs[area][i].overlaps_vertically_p);
26217 draw_glyphs (w, start_x, row, area,
26218 start, i,
26219 DRAW_NORMAL_TEXT, overlaps);
26221 else
26223 x += row->glyphs[area][i].pixel_width;
26224 ++i;
26228 unblock_input ();
26232 /* EXPORT:
26233 Draw the cursor glyph of window W in glyph row ROW. See the
26234 comment of draw_glyphs for the meaning of HL. */
26236 void
26237 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26238 enum draw_glyphs_face hl)
26240 /* If cursor hpos is out of bounds, don't draw garbage. This can
26241 happen in mini-buffer windows when switching between echo area
26242 glyphs and mini-buffer. */
26243 if ((row->reversed_p
26244 ? (w->phys_cursor.hpos >= 0)
26245 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26247 int on_p = w->phys_cursor_on_p;
26248 int x1;
26249 int hpos = w->phys_cursor.hpos;
26251 /* When the window is hscrolled, cursor hpos can legitimately be
26252 out of bounds, but we draw the cursor at the corresponding
26253 window margin in that case. */
26254 if (!row->reversed_p && hpos < 0)
26255 hpos = 0;
26256 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26257 hpos = row->used[TEXT_AREA] - 1;
26259 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26260 hl, 0);
26261 w->phys_cursor_on_p = on_p;
26263 if (hl == DRAW_CURSOR)
26264 w->phys_cursor_width = x1 - w->phys_cursor.x;
26265 /* When we erase the cursor, and ROW is overlapped by other
26266 rows, make sure that these overlapping parts of other rows
26267 are redrawn. */
26268 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26270 w->phys_cursor_width = x1 - w->phys_cursor.x;
26272 if (row > w->current_matrix->rows
26273 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26274 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26275 OVERLAPS_ERASED_CURSOR);
26277 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26278 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26279 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26280 OVERLAPS_ERASED_CURSOR);
26286 /* EXPORT:
26287 Erase the image of a cursor of window W from the screen. */
26289 void
26290 erase_phys_cursor (struct window *w)
26292 struct frame *f = XFRAME (w->frame);
26293 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26294 int hpos = w->phys_cursor.hpos;
26295 int vpos = w->phys_cursor.vpos;
26296 int mouse_face_here_p = 0;
26297 struct glyph_matrix *active_glyphs = w->current_matrix;
26298 struct glyph_row *cursor_row;
26299 struct glyph *cursor_glyph;
26300 enum draw_glyphs_face hl;
26302 /* No cursor displayed or row invalidated => nothing to do on the
26303 screen. */
26304 if (w->phys_cursor_type == NO_CURSOR)
26305 goto mark_cursor_off;
26307 /* VPOS >= active_glyphs->nrows means that window has been resized.
26308 Don't bother to erase the cursor. */
26309 if (vpos >= active_glyphs->nrows)
26310 goto mark_cursor_off;
26312 /* If row containing cursor is marked invalid, there is nothing we
26313 can do. */
26314 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26315 if (!cursor_row->enabled_p)
26316 goto mark_cursor_off;
26318 /* If line spacing is > 0, old cursor may only be partially visible in
26319 window after split-window. So adjust visible height. */
26320 cursor_row->visible_height = min (cursor_row->visible_height,
26321 window_text_bottom_y (w) - cursor_row->y);
26323 /* If row is completely invisible, don't attempt to delete a cursor which
26324 isn't there. This can happen if cursor is at top of a window, and
26325 we switch to a buffer with a header line in that window. */
26326 if (cursor_row->visible_height <= 0)
26327 goto mark_cursor_off;
26329 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26330 if (cursor_row->cursor_in_fringe_p)
26332 cursor_row->cursor_in_fringe_p = 0;
26333 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26334 goto mark_cursor_off;
26337 /* This can happen when the new row is shorter than the old one.
26338 In this case, either draw_glyphs or clear_end_of_line
26339 should have cleared the cursor. Note that we wouldn't be
26340 able to erase the cursor in this case because we don't have a
26341 cursor glyph at hand. */
26342 if ((cursor_row->reversed_p
26343 ? (w->phys_cursor.hpos < 0)
26344 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26345 goto mark_cursor_off;
26347 /* When the window is hscrolled, cursor hpos can legitimately be out
26348 of bounds, but we draw the cursor at the corresponding window
26349 margin in that case. */
26350 if (!cursor_row->reversed_p && hpos < 0)
26351 hpos = 0;
26352 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26353 hpos = cursor_row->used[TEXT_AREA] - 1;
26355 /* If the cursor is in the mouse face area, redisplay that when
26356 we clear the cursor. */
26357 if (! NILP (hlinfo->mouse_face_window)
26358 && coords_in_mouse_face_p (w, hpos, vpos)
26359 /* Don't redraw the cursor's spot in mouse face if it is at the
26360 end of a line (on a newline). The cursor appears there, but
26361 mouse highlighting does not. */
26362 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26363 mouse_face_here_p = 1;
26365 /* Maybe clear the display under the cursor. */
26366 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26368 int x, y, left_x;
26369 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26370 int width;
26372 cursor_glyph = get_phys_cursor_glyph (w);
26373 if (cursor_glyph == NULL)
26374 goto mark_cursor_off;
26376 width = cursor_glyph->pixel_width;
26377 left_x = window_box_left_offset (w, TEXT_AREA);
26378 x = w->phys_cursor.x;
26379 if (x < left_x)
26380 width -= left_x - x;
26381 width = min (width, window_box_width (w, TEXT_AREA) - x);
26382 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26383 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26385 if (width > 0)
26386 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26389 /* Erase the cursor by redrawing the character underneath it. */
26390 if (mouse_face_here_p)
26391 hl = DRAW_MOUSE_FACE;
26392 else
26393 hl = DRAW_NORMAL_TEXT;
26394 draw_phys_cursor_glyph (w, cursor_row, hl);
26396 mark_cursor_off:
26397 w->phys_cursor_on_p = 0;
26398 w->phys_cursor_type = NO_CURSOR;
26402 /* EXPORT:
26403 Display or clear cursor of window W. If ON is zero, clear the
26404 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26405 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26407 void
26408 display_and_set_cursor (struct window *w, int on,
26409 int hpos, int vpos, int x, int y)
26411 struct frame *f = XFRAME (w->frame);
26412 int new_cursor_type;
26413 int new_cursor_width;
26414 int active_cursor;
26415 struct glyph_row *glyph_row;
26416 struct glyph *glyph;
26418 /* This is pointless on invisible frames, and dangerous on garbaged
26419 windows and frames; in the latter case, the frame or window may
26420 be in the midst of changing its size, and x and y may be off the
26421 window. */
26422 if (! FRAME_VISIBLE_P (f)
26423 || FRAME_GARBAGED_P (f)
26424 || vpos >= w->current_matrix->nrows
26425 || hpos >= w->current_matrix->matrix_w)
26426 return;
26428 /* If cursor is off and we want it off, return quickly. */
26429 if (!on && !w->phys_cursor_on_p)
26430 return;
26432 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26433 /* If cursor row is not enabled, we don't really know where to
26434 display the cursor. */
26435 if (!glyph_row->enabled_p)
26437 w->phys_cursor_on_p = 0;
26438 return;
26441 glyph = NULL;
26442 if (!glyph_row->exact_window_width_line_p
26443 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26444 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26446 eassert (input_blocked_p ());
26448 /* Set new_cursor_type to the cursor we want to be displayed. */
26449 new_cursor_type = get_window_cursor_type (w, glyph,
26450 &new_cursor_width, &active_cursor);
26452 /* If cursor is currently being shown and we don't want it to be or
26453 it is in the wrong place, or the cursor type is not what we want,
26454 erase it. */
26455 if (w->phys_cursor_on_p
26456 && (!on
26457 || w->phys_cursor.x != x
26458 || w->phys_cursor.y != y
26459 || new_cursor_type != w->phys_cursor_type
26460 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26461 && new_cursor_width != w->phys_cursor_width)))
26462 erase_phys_cursor (w);
26464 /* Don't check phys_cursor_on_p here because that flag is only set
26465 to zero in some cases where we know that the cursor has been
26466 completely erased, to avoid the extra work of erasing the cursor
26467 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26468 still not be visible, or it has only been partly erased. */
26469 if (on)
26471 w->phys_cursor_ascent = glyph_row->ascent;
26472 w->phys_cursor_height = glyph_row->height;
26474 /* Set phys_cursor_.* before x_draw_.* is called because some
26475 of them may need the information. */
26476 w->phys_cursor.x = x;
26477 w->phys_cursor.y = glyph_row->y;
26478 w->phys_cursor.hpos = hpos;
26479 w->phys_cursor.vpos = vpos;
26482 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26483 new_cursor_type, new_cursor_width,
26484 on, active_cursor);
26488 /* Switch the display of W's cursor on or off, according to the value
26489 of ON. */
26491 static void
26492 update_window_cursor (struct window *w, int on)
26494 /* Don't update cursor in windows whose frame is in the process
26495 of being deleted. */
26496 if (w->current_matrix)
26498 int hpos = w->phys_cursor.hpos;
26499 int vpos = w->phys_cursor.vpos;
26500 struct glyph_row *row;
26502 if (vpos >= w->current_matrix->nrows
26503 || hpos >= w->current_matrix->matrix_w)
26504 return;
26506 row = MATRIX_ROW (w->current_matrix, vpos);
26508 /* When the window is hscrolled, cursor hpos can legitimately be
26509 out of bounds, but we draw the cursor at the corresponding
26510 window margin in that case. */
26511 if (!row->reversed_p && hpos < 0)
26512 hpos = 0;
26513 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26514 hpos = row->used[TEXT_AREA] - 1;
26516 block_input ();
26517 display_and_set_cursor (w, on, hpos, vpos,
26518 w->phys_cursor.x, w->phys_cursor.y);
26519 unblock_input ();
26524 /* Call update_window_cursor with parameter ON_P on all leaf windows
26525 in the window tree rooted at W. */
26527 static void
26528 update_cursor_in_window_tree (struct window *w, int on_p)
26530 while (w)
26532 if (WINDOWP (w->contents))
26533 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26534 else
26535 update_window_cursor (w, on_p);
26537 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26542 /* EXPORT:
26543 Display the cursor on window W, or clear it, according to ON_P.
26544 Don't change the cursor's position. */
26546 void
26547 x_update_cursor (struct frame *f, int on_p)
26549 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26553 /* EXPORT:
26554 Clear the cursor of window W to background color, and mark the
26555 cursor as not shown. This is used when the text where the cursor
26556 is about to be rewritten. */
26558 void
26559 x_clear_cursor (struct window *w)
26561 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26562 update_window_cursor (w, 0);
26565 #endif /* HAVE_WINDOW_SYSTEM */
26567 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26568 and MSDOS. */
26569 static void
26570 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26571 int start_hpos, int end_hpos,
26572 enum draw_glyphs_face draw)
26574 #ifdef HAVE_WINDOW_SYSTEM
26575 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26577 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26578 return;
26580 #endif
26581 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26582 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26583 #endif
26586 /* Display the active region described by mouse_face_* according to DRAW. */
26588 static void
26589 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26591 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26592 struct frame *f = XFRAME (WINDOW_FRAME (w));
26594 if (/* If window is in the process of being destroyed, don't bother
26595 to do anything. */
26596 w->current_matrix != NULL
26597 /* Don't update mouse highlight if hidden */
26598 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26599 /* Recognize when we are called to operate on rows that don't exist
26600 anymore. This can happen when a window is split. */
26601 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26603 int phys_cursor_on_p = w->phys_cursor_on_p;
26604 struct glyph_row *row, *first, *last;
26606 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26607 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26609 for (row = first; row <= last && row->enabled_p; ++row)
26611 int start_hpos, end_hpos, start_x;
26613 /* For all but the first row, the highlight starts at column 0. */
26614 if (row == first)
26616 /* R2L rows have BEG and END in reversed order, but the
26617 screen drawing geometry is always left to right. So
26618 we need to mirror the beginning and end of the
26619 highlighted area in R2L rows. */
26620 if (!row->reversed_p)
26622 start_hpos = hlinfo->mouse_face_beg_col;
26623 start_x = hlinfo->mouse_face_beg_x;
26625 else if (row == last)
26627 start_hpos = hlinfo->mouse_face_end_col;
26628 start_x = hlinfo->mouse_face_end_x;
26630 else
26632 start_hpos = 0;
26633 start_x = 0;
26636 else if (row->reversed_p && row == last)
26638 start_hpos = hlinfo->mouse_face_end_col;
26639 start_x = hlinfo->mouse_face_end_x;
26641 else
26643 start_hpos = 0;
26644 start_x = 0;
26647 if (row == last)
26649 if (!row->reversed_p)
26650 end_hpos = hlinfo->mouse_face_end_col;
26651 else if (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 else if (row->reversed_p && row == first)
26661 end_hpos = hlinfo->mouse_face_beg_col;
26662 else
26664 end_hpos = row->used[TEXT_AREA];
26665 if (draw == DRAW_NORMAL_TEXT)
26666 row->fill_line_p = 1; /* Clear to end of line */
26669 if (end_hpos > start_hpos)
26671 draw_row_with_mouse_face (w, start_x, row,
26672 start_hpos, end_hpos, draw);
26674 row->mouse_face_p
26675 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26679 #ifdef HAVE_WINDOW_SYSTEM
26680 /* When we've written over the cursor, arrange for it to
26681 be displayed again. */
26682 if (FRAME_WINDOW_P (f)
26683 && phys_cursor_on_p && !w->phys_cursor_on_p)
26685 int hpos = w->phys_cursor.hpos;
26687 /* When the window is hscrolled, cursor hpos can legitimately be
26688 out of bounds, but we draw the cursor at the corresponding
26689 window margin in that case. */
26690 if (!row->reversed_p && hpos < 0)
26691 hpos = 0;
26692 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26693 hpos = row->used[TEXT_AREA] - 1;
26695 block_input ();
26696 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26697 w->phys_cursor.x, w->phys_cursor.y);
26698 unblock_input ();
26700 #endif /* HAVE_WINDOW_SYSTEM */
26703 #ifdef HAVE_WINDOW_SYSTEM
26704 /* Change the mouse cursor. */
26705 if (FRAME_WINDOW_P (f))
26707 if (draw == DRAW_NORMAL_TEXT
26708 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26709 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26710 else if (draw == DRAW_MOUSE_FACE)
26711 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26712 else
26713 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26715 #endif /* HAVE_WINDOW_SYSTEM */
26718 /* EXPORT:
26719 Clear out the mouse-highlighted active region.
26720 Redraw it un-highlighted first. Value is non-zero if mouse
26721 face was actually drawn unhighlighted. */
26724 clear_mouse_face (Mouse_HLInfo *hlinfo)
26726 int cleared = 0;
26728 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26730 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26731 cleared = 1;
26734 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26735 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26736 hlinfo->mouse_face_window = Qnil;
26737 hlinfo->mouse_face_overlay = Qnil;
26738 return cleared;
26741 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26742 within the mouse face on that window. */
26743 static int
26744 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26746 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26748 /* Quickly resolve the easy cases. */
26749 if (!(WINDOWP (hlinfo->mouse_face_window)
26750 && XWINDOW (hlinfo->mouse_face_window) == w))
26751 return 0;
26752 if (vpos < hlinfo->mouse_face_beg_row
26753 || vpos > hlinfo->mouse_face_end_row)
26754 return 0;
26755 if (vpos > hlinfo->mouse_face_beg_row
26756 && vpos < hlinfo->mouse_face_end_row)
26757 return 1;
26759 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26761 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26763 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26764 return 1;
26766 else if ((vpos == hlinfo->mouse_face_beg_row
26767 && hpos >= hlinfo->mouse_face_beg_col)
26768 || (vpos == hlinfo->mouse_face_end_row
26769 && hpos < hlinfo->mouse_face_end_col))
26770 return 1;
26772 else
26774 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26776 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26777 return 1;
26779 else if ((vpos == hlinfo->mouse_face_beg_row
26780 && hpos <= hlinfo->mouse_face_beg_col)
26781 || (vpos == hlinfo->mouse_face_end_row
26782 && hpos > hlinfo->mouse_face_end_col))
26783 return 1;
26785 return 0;
26789 /* EXPORT:
26790 Non-zero if physical cursor of window W is within mouse face. */
26793 cursor_in_mouse_face_p (struct window *w)
26795 int hpos = w->phys_cursor.hpos;
26796 int vpos = w->phys_cursor.vpos;
26797 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26799 /* When the window is hscrolled, cursor hpos can legitimately be out
26800 of bounds, but we draw the cursor at the corresponding window
26801 margin in that case. */
26802 if (!row->reversed_p && hpos < 0)
26803 hpos = 0;
26804 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26805 hpos = row->used[TEXT_AREA] - 1;
26807 return coords_in_mouse_face_p (w, hpos, vpos);
26812 /* Find the glyph rows START_ROW and END_ROW of window W that display
26813 characters between buffer positions START_CHARPOS and END_CHARPOS
26814 (excluding END_CHARPOS). DISP_STRING is a display string that
26815 covers these buffer positions. This is similar to
26816 row_containing_pos, but is more accurate when bidi reordering makes
26817 buffer positions change non-linearly with glyph rows. */
26818 static void
26819 rows_from_pos_range (struct window *w,
26820 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26821 Lisp_Object disp_string,
26822 struct glyph_row **start, struct glyph_row **end)
26824 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26825 int last_y = window_text_bottom_y (w);
26826 struct glyph_row *row;
26828 *start = NULL;
26829 *end = NULL;
26831 while (!first->enabled_p
26832 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26833 first++;
26835 /* Find the START row. */
26836 for (row = first;
26837 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26838 row++)
26840 /* A row can potentially be the START row if the range of the
26841 characters it displays intersects the range
26842 [START_CHARPOS..END_CHARPOS). */
26843 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26844 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26845 /* See the commentary in row_containing_pos, for the
26846 explanation of the complicated way to check whether
26847 some position is beyond the end of the characters
26848 displayed by a row. */
26849 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26850 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26851 && !row->ends_at_zv_p
26852 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26853 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26854 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26855 && !row->ends_at_zv_p
26856 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26858 /* Found a candidate row. Now make sure at least one of the
26859 glyphs it displays has a charpos from the range
26860 [START_CHARPOS..END_CHARPOS).
26862 This is not obvious because bidi reordering could make
26863 buffer positions of a row be 1,2,3,102,101,100, and if we
26864 want to highlight characters in [50..60), we don't want
26865 this row, even though [50..60) does intersect [1..103),
26866 the range of character positions given by the row's start
26867 and end positions. */
26868 struct glyph *g = row->glyphs[TEXT_AREA];
26869 struct glyph *e = g + row->used[TEXT_AREA];
26871 while (g < e)
26873 if (((BUFFERP (g->object) || INTEGERP (g->object))
26874 && start_charpos <= g->charpos && g->charpos < end_charpos)
26875 /* A glyph that comes from DISP_STRING is by
26876 definition to be highlighted. */
26877 || EQ (g->object, disp_string))
26878 *start = row;
26879 g++;
26881 if (*start)
26882 break;
26886 /* Find the END row. */
26887 if (!*start
26888 /* If the last row is partially visible, start looking for END
26889 from that row, instead of starting from FIRST. */
26890 && !(row->enabled_p
26891 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26892 row = first;
26893 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26895 struct glyph_row *next = row + 1;
26896 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26898 if (!next->enabled_p
26899 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26900 /* The first row >= START whose range of displayed characters
26901 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26902 is the row END + 1. */
26903 || (start_charpos < next_start
26904 && end_charpos < next_start)
26905 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26906 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26907 && !next->ends_at_zv_p
26908 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26909 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26910 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26911 && !next->ends_at_zv_p
26912 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26914 *end = row;
26915 break;
26917 else
26919 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26920 but none of the characters it displays are in the range, it is
26921 also END + 1. */
26922 struct glyph *g = next->glyphs[TEXT_AREA];
26923 struct glyph *s = g;
26924 struct glyph *e = g + next->used[TEXT_AREA];
26926 while (g < e)
26928 if (((BUFFERP (g->object) || INTEGERP (g->object))
26929 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26930 /* If the buffer position of the first glyph in
26931 the row is equal to END_CHARPOS, it means
26932 the last character to be highlighted is the
26933 newline of ROW, and we must consider NEXT as
26934 END, not END+1. */
26935 || (((!next->reversed_p && g == s)
26936 || (next->reversed_p && g == e - 1))
26937 && (g->charpos == end_charpos
26938 /* Special case for when NEXT is an
26939 empty line at ZV. */
26940 || (g->charpos == -1
26941 && !row->ends_at_zv_p
26942 && next_start == end_charpos)))))
26943 /* A glyph that comes from DISP_STRING is by
26944 definition to be highlighted. */
26945 || EQ (g->object, disp_string))
26946 break;
26947 g++;
26949 if (g == e)
26951 *end = row;
26952 break;
26954 /* The first row that ends at ZV must be the last to be
26955 highlighted. */
26956 else if (next->ends_at_zv_p)
26958 *end = next;
26959 break;
26965 /* This function sets the mouse_face_* elements of HLINFO, assuming
26966 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26967 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26968 for the overlay or run of text properties specifying the mouse
26969 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26970 before-string and after-string that must also be highlighted.
26971 DISP_STRING, if non-nil, is a display string that may cover some
26972 or all of the highlighted text. */
26974 static void
26975 mouse_face_from_buffer_pos (Lisp_Object window,
26976 Mouse_HLInfo *hlinfo,
26977 ptrdiff_t mouse_charpos,
26978 ptrdiff_t start_charpos,
26979 ptrdiff_t end_charpos,
26980 Lisp_Object before_string,
26981 Lisp_Object after_string,
26982 Lisp_Object disp_string)
26984 struct window *w = XWINDOW (window);
26985 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26986 struct glyph_row *r1, *r2;
26987 struct glyph *glyph, *end;
26988 ptrdiff_t ignore, pos;
26989 int x;
26991 eassert (NILP (disp_string) || STRINGP (disp_string));
26992 eassert (NILP (before_string) || STRINGP (before_string));
26993 eassert (NILP (after_string) || STRINGP (after_string));
26995 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26996 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26997 if (r1 == NULL)
26998 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26999 /* If the before-string or display-string contains newlines,
27000 rows_from_pos_range skips to its last row. Move back. */
27001 if (!NILP (before_string) || !NILP (disp_string))
27003 struct glyph_row *prev;
27004 while ((prev = r1 - 1, prev >= first)
27005 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27006 && prev->used[TEXT_AREA] > 0)
27008 struct glyph *beg = prev->glyphs[TEXT_AREA];
27009 glyph = beg + prev->used[TEXT_AREA];
27010 while (--glyph >= beg && INTEGERP (glyph->object));
27011 if (glyph < beg
27012 || !(EQ (glyph->object, before_string)
27013 || EQ (glyph->object, disp_string)))
27014 break;
27015 r1 = prev;
27018 if (r2 == NULL)
27020 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27021 hlinfo->mouse_face_past_end = 1;
27023 else if (!NILP (after_string))
27025 /* If the after-string has newlines, advance to its last row. */
27026 struct glyph_row *next;
27027 struct glyph_row *last
27028 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27030 for (next = r2 + 1;
27031 next <= last
27032 && next->used[TEXT_AREA] > 0
27033 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27034 ++next)
27035 r2 = next;
27037 /* The rest of the display engine assumes that mouse_face_beg_row is
27038 either above mouse_face_end_row or identical to it. But with
27039 bidi-reordered continued lines, the row for START_CHARPOS could
27040 be below the row for END_CHARPOS. If so, swap the rows and store
27041 them in correct order. */
27042 if (r1->y > r2->y)
27044 struct glyph_row *tem = r2;
27046 r2 = r1;
27047 r1 = tem;
27050 hlinfo->mouse_face_beg_y = r1->y;
27051 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27052 hlinfo->mouse_face_end_y = r2->y;
27053 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27055 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27056 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27057 could be anywhere in the row and in any order. The strategy
27058 below is to find the leftmost and the rightmost glyph that
27059 belongs to either of these 3 strings, or whose position is
27060 between START_CHARPOS and END_CHARPOS, and highlight all the
27061 glyphs between those two. This may cover more than just the text
27062 between START_CHARPOS and END_CHARPOS if the range of characters
27063 strides the bidi level boundary, e.g. if the beginning is in R2L
27064 text while the end is in L2R text or vice versa. */
27065 if (!r1->reversed_p)
27067 /* This row is in a left to right paragraph. Scan it left to
27068 right. */
27069 glyph = r1->glyphs[TEXT_AREA];
27070 end = glyph + r1->used[TEXT_AREA];
27071 x = r1->x;
27073 /* Skip truncation glyphs at the start of the glyph row. */
27074 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27075 for (; glyph < end
27076 && INTEGERP (glyph->object)
27077 && glyph->charpos < 0;
27078 ++glyph)
27079 x += glyph->pixel_width;
27081 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27082 or DISP_STRING, and the first glyph from buffer whose
27083 position is between START_CHARPOS and END_CHARPOS. */
27084 for (; glyph < end
27085 && !INTEGERP (glyph->object)
27086 && !EQ (glyph->object, disp_string)
27087 && !(BUFFERP (glyph->object)
27088 && (glyph->charpos >= start_charpos
27089 && glyph->charpos < end_charpos));
27090 ++glyph)
27092 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27093 are present at buffer positions between START_CHARPOS and
27094 END_CHARPOS, or if they come from an overlay. */
27095 if (EQ (glyph->object, before_string))
27097 pos = string_buffer_position (before_string,
27098 start_charpos);
27099 /* If pos == 0, it means before_string came from an
27100 overlay, not from a buffer position. */
27101 if (!pos || (pos >= start_charpos && pos < end_charpos))
27102 break;
27104 else if (EQ (glyph->object, after_string))
27106 pos = string_buffer_position (after_string, end_charpos);
27107 if (!pos || (pos >= start_charpos && pos < end_charpos))
27108 break;
27110 x += glyph->pixel_width;
27112 hlinfo->mouse_face_beg_x = x;
27113 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27115 else
27117 /* This row is in a right to left paragraph. Scan it right to
27118 left. */
27119 struct glyph *g;
27121 end = r1->glyphs[TEXT_AREA] - 1;
27122 glyph = end + r1->used[TEXT_AREA];
27124 /* Skip truncation glyphs at the start of the glyph row. */
27125 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27126 for (; glyph > end
27127 && INTEGERP (glyph->object)
27128 && glyph->charpos < 0;
27129 --glyph)
27132 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27133 or DISP_STRING, and the first glyph from buffer whose
27134 position is between START_CHARPOS and END_CHARPOS. */
27135 for (; glyph > end
27136 && !INTEGERP (glyph->object)
27137 && !EQ (glyph->object, disp_string)
27138 && !(BUFFERP (glyph->object)
27139 && (glyph->charpos >= start_charpos
27140 && glyph->charpos < end_charpos));
27141 --glyph)
27143 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27144 are present at buffer positions between START_CHARPOS and
27145 END_CHARPOS, or if they come from an overlay. */
27146 if (EQ (glyph->object, before_string))
27148 pos = string_buffer_position (before_string, start_charpos);
27149 /* If pos == 0, it means before_string came from an
27150 overlay, not from a buffer position. */
27151 if (!pos || (pos >= start_charpos && pos < end_charpos))
27152 break;
27154 else if (EQ (glyph->object, after_string))
27156 pos = string_buffer_position (after_string, end_charpos);
27157 if (!pos || (pos >= start_charpos && pos < end_charpos))
27158 break;
27162 glyph++; /* first glyph to the right of the highlighted area */
27163 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27164 x += g->pixel_width;
27165 hlinfo->mouse_face_beg_x = x;
27166 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27169 /* If the highlight ends in a different row, compute GLYPH and END
27170 for the end row. Otherwise, reuse the values computed above for
27171 the row where the highlight begins. */
27172 if (r2 != r1)
27174 if (!r2->reversed_p)
27176 glyph = r2->glyphs[TEXT_AREA];
27177 end = glyph + r2->used[TEXT_AREA];
27178 x = r2->x;
27180 else
27182 end = r2->glyphs[TEXT_AREA] - 1;
27183 glyph = end + r2->used[TEXT_AREA];
27187 if (!r2->reversed_p)
27189 /* Skip truncation and continuation glyphs near the end of the
27190 row, and also blanks and stretch glyphs inserted by
27191 extend_face_to_end_of_line. */
27192 while (end > glyph
27193 && INTEGERP ((end - 1)->object))
27194 --end;
27195 /* Scan the rest of the glyph row from the end, looking for the
27196 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27197 DISP_STRING, or whose position is between START_CHARPOS
27198 and END_CHARPOS */
27199 for (--end;
27200 end > glyph
27201 && !INTEGERP (end->object)
27202 && !EQ (end->object, disp_string)
27203 && !(BUFFERP (end->object)
27204 && (end->charpos >= start_charpos
27205 && end->charpos < end_charpos));
27206 --end)
27208 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27209 are present at buffer positions between START_CHARPOS and
27210 END_CHARPOS, or if they come from an overlay. */
27211 if (EQ (end->object, before_string))
27213 pos = string_buffer_position (before_string, start_charpos);
27214 if (!pos || (pos >= start_charpos && pos < end_charpos))
27215 break;
27217 else if (EQ (end->object, after_string))
27219 pos = string_buffer_position (after_string, end_charpos);
27220 if (!pos || (pos >= start_charpos && pos < end_charpos))
27221 break;
27224 /* Find the X coordinate of the last glyph to be highlighted. */
27225 for (; glyph <= end; ++glyph)
27226 x += glyph->pixel_width;
27228 hlinfo->mouse_face_end_x = x;
27229 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27231 else
27233 /* Skip truncation and continuation glyphs near the end of the
27234 row, and also blanks and stretch glyphs inserted by
27235 extend_face_to_end_of_line. */
27236 x = r2->x;
27237 end++;
27238 while (end < glyph
27239 && INTEGERP (end->object))
27241 x += end->pixel_width;
27242 ++end;
27244 /* Scan the rest of the glyph row from the end, looking for the
27245 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27246 DISP_STRING, or whose position is between START_CHARPOS
27247 and END_CHARPOS */
27248 for ( ;
27249 end < glyph
27250 && !INTEGERP (end->object)
27251 && !EQ (end->object, disp_string)
27252 && !(BUFFERP (end->object)
27253 && (end->charpos >= start_charpos
27254 && end->charpos < end_charpos));
27255 ++end)
27257 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27258 are present at buffer positions between START_CHARPOS and
27259 END_CHARPOS, or if they come from an overlay. */
27260 if (EQ (end->object, before_string))
27262 pos = string_buffer_position (before_string, start_charpos);
27263 if (!pos || (pos >= start_charpos && pos < end_charpos))
27264 break;
27266 else if (EQ (end->object, after_string))
27268 pos = string_buffer_position (after_string, end_charpos);
27269 if (!pos || (pos >= start_charpos && pos < end_charpos))
27270 break;
27272 x += end->pixel_width;
27274 /* If we exited the above loop because we arrived at the last
27275 glyph of the row, and its buffer position is still not in
27276 range, it means the last character in range is the preceding
27277 newline. Bump the end column and x values to get past the
27278 last glyph. */
27279 if (end == glyph
27280 && BUFFERP (end->object)
27281 && (end->charpos < start_charpos
27282 || end->charpos >= end_charpos))
27284 x += end->pixel_width;
27285 ++end;
27287 hlinfo->mouse_face_end_x = x;
27288 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27291 hlinfo->mouse_face_window = window;
27292 hlinfo->mouse_face_face_id
27293 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27294 mouse_charpos + 1,
27295 !hlinfo->mouse_face_hidden, -1);
27296 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27299 /* The following function is not used anymore (replaced with
27300 mouse_face_from_string_pos), but I leave it here for the time
27301 being, in case someone would. */
27303 #if 0 /* not used */
27305 /* Find the position of the glyph for position POS in OBJECT in
27306 window W's current matrix, and return in *X, *Y the pixel
27307 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27309 RIGHT_P non-zero means return the position of the right edge of the
27310 glyph, RIGHT_P zero means return the left edge position.
27312 If no glyph for POS exists in the matrix, return the position of
27313 the glyph with the next smaller position that is in the matrix, if
27314 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27315 exists in the matrix, return the position of the glyph with the
27316 next larger position in OBJECT.
27318 Value is non-zero if a glyph was found. */
27320 static int
27321 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27322 int *hpos, int *vpos, int *x, int *y, int right_p)
27324 int yb = window_text_bottom_y (w);
27325 struct glyph_row *r;
27326 struct glyph *best_glyph = NULL;
27327 struct glyph_row *best_row = NULL;
27328 int best_x = 0;
27330 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27331 r->enabled_p && r->y < yb;
27332 ++r)
27334 struct glyph *g = r->glyphs[TEXT_AREA];
27335 struct glyph *e = g + r->used[TEXT_AREA];
27336 int gx;
27338 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27339 if (EQ (g->object, object))
27341 if (g->charpos == pos)
27343 best_glyph = g;
27344 best_x = gx;
27345 best_row = r;
27346 goto found;
27348 else if (best_glyph == NULL
27349 || ((eabs (g->charpos - pos)
27350 < eabs (best_glyph->charpos - pos))
27351 && (right_p
27352 ? g->charpos < pos
27353 : g->charpos > pos)))
27355 best_glyph = g;
27356 best_x = gx;
27357 best_row = r;
27362 found:
27364 if (best_glyph)
27366 *x = best_x;
27367 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27369 if (right_p)
27371 *x += best_glyph->pixel_width;
27372 ++*hpos;
27375 *y = best_row->y;
27376 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27379 return best_glyph != NULL;
27381 #endif /* not used */
27383 /* Find the positions of the first and the last glyphs in window W's
27384 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27385 (assumed to be a string), and return in HLINFO's mouse_face_*
27386 members the pixel and column/row coordinates of those glyphs. */
27388 static void
27389 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27390 Lisp_Object object,
27391 ptrdiff_t startpos, ptrdiff_t endpos)
27393 int yb = window_text_bottom_y (w);
27394 struct glyph_row *r;
27395 struct glyph *g, *e;
27396 int gx;
27397 int found = 0;
27399 /* Find the glyph row with at least one position in the range
27400 [STARTPOS..ENDPOS], and the first glyph in that row whose
27401 position belongs to that range. */
27402 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27403 r->enabled_p && r->y < yb;
27404 ++r)
27406 if (!r->reversed_p)
27408 g = r->glyphs[TEXT_AREA];
27409 e = g + r->used[TEXT_AREA];
27410 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27411 if (EQ (g->object, object)
27412 && startpos <= g->charpos && g->charpos <= endpos)
27414 hlinfo->mouse_face_beg_row
27415 = MATRIX_ROW_VPOS (r, w->current_matrix);
27416 hlinfo->mouse_face_beg_y = r->y;
27417 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27418 hlinfo->mouse_face_beg_x = gx;
27419 found = 1;
27420 break;
27423 else
27425 struct glyph *g1;
27427 e = r->glyphs[TEXT_AREA];
27428 g = e + r->used[TEXT_AREA];
27429 for ( ; g > e; --g)
27430 if (EQ ((g-1)->object, object)
27431 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27433 hlinfo->mouse_face_beg_row
27434 = MATRIX_ROW_VPOS (r, w->current_matrix);
27435 hlinfo->mouse_face_beg_y = r->y;
27436 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27437 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27438 gx += g1->pixel_width;
27439 hlinfo->mouse_face_beg_x = gx;
27440 found = 1;
27441 break;
27444 if (found)
27445 break;
27448 if (!found)
27449 return;
27451 /* Starting with the next row, look for the first row which does NOT
27452 include any glyphs whose positions are in the range. */
27453 for (++r; r->enabled_p && r->y < yb; ++r)
27455 g = r->glyphs[TEXT_AREA];
27456 e = g + r->used[TEXT_AREA];
27457 found = 0;
27458 for ( ; g < e; ++g)
27459 if (EQ (g->object, object)
27460 && startpos <= g->charpos && g->charpos <= endpos)
27462 found = 1;
27463 break;
27465 if (!found)
27466 break;
27469 /* The highlighted region ends on the previous row. */
27470 r--;
27472 /* Set the end row and its vertical pixel coordinate. */
27473 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27474 hlinfo->mouse_face_end_y = r->y;
27476 /* Compute and set the end column and the end column's horizontal
27477 pixel coordinate. */
27478 if (!r->reversed_p)
27480 g = r->glyphs[TEXT_AREA];
27481 e = g + r->used[TEXT_AREA];
27482 for ( ; e > g; --e)
27483 if (EQ ((e-1)->object, object)
27484 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27485 break;
27486 hlinfo->mouse_face_end_col = e - g;
27488 for (gx = r->x; g < e; ++g)
27489 gx += g->pixel_width;
27490 hlinfo->mouse_face_end_x = gx;
27492 else
27494 e = r->glyphs[TEXT_AREA];
27495 g = e + r->used[TEXT_AREA];
27496 for (gx = r->x ; e < g; ++e)
27498 if (EQ (e->object, object)
27499 && startpos <= e->charpos && e->charpos <= endpos)
27500 break;
27501 gx += e->pixel_width;
27503 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27504 hlinfo->mouse_face_end_x = gx;
27508 #ifdef HAVE_WINDOW_SYSTEM
27510 /* See if position X, Y is within a hot-spot of an image. */
27512 static int
27513 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27515 if (!CONSP (hot_spot))
27516 return 0;
27518 if (EQ (XCAR (hot_spot), Qrect))
27520 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27521 Lisp_Object rect = XCDR (hot_spot);
27522 Lisp_Object tem;
27523 if (!CONSP (rect))
27524 return 0;
27525 if (!CONSP (XCAR (rect)))
27526 return 0;
27527 if (!CONSP (XCDR (rect)))
27528 return 0;
27529 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27530 return 0;
27531 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27532 return 0;
27533 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27534 return 0;
27535 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27536 return 0;
27537 return 1;
27539 else if (EQ (XCAR (hot_spot), Qcircle))
27541 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27542 Lisp_Object circ = XCDR (hot_spot);
27543 Lisp_Object lr, lx0, ly0;
27544 if (CONSP (circ)
27545 && CONSP (XCAR (circ))
27546 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27547 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27548 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27550 double r = XFLOATINT (lr);
27551 double dx = XINT (lx0) - x;
27552 double dy = XINT (ly0) - y;
27553 return (dx * dx + dy * dy <= r * r);
27556 else if (EQ (XCAR (hot_spot), Qpoly))
27558 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27559 if (VECTORP (XCDR (hot_spot)))
27561 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27562 Lisp_Object *poly = v->contents;
27563 ptrdiff_t n = v->header.size;
27564 ptrdiff_t i;
27565 int inside = 0;
27566 Lisp_Object lx, ly;
27567 int x0, y0;
27569 /* Need an even number of coordinates, and at least 3 edges. */
27570 if (n < 6 || n & 1)
27571 return 0;
27573 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27574 If count is odd, we are inside polygon. Pixels on edges
27575 may or may not be included depending on actual geometry of the
27576 polygon. */
27577 if ((lx = poly[n-2], !INTEGERP (lx))
27578 || (ly = poly[n-1], !INTEGERP (lx)))
27579 return 0;
27580 x0 = XINT (lx), y0 = XINT (ly);
27581 for (i = 0; i < n; i += 2)
27583 int x1 = x0, y1 = y0;
27584 if ((lx = poly[i], !INTEGERP (lx))
27585 || (ly = poly[i+1], !INTEGERP (ly)))
27586 return 0;
27587 x0 = XINT (lx), y0 = XINT (ly);
27589 /* Does this segment cross the X line? */
27590 if (x0 >= x)
27592 if (x1 >= x)
27593 continue;
27595 else if (x1 < x)
27596 continue;
27597 if (y > y0 && y > y1)
27598 continue;
27599 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27600 inside = !inside;
27602 return inside;
27605 return 0;
27608 Lisp_Object
27609 find_hot_spot (Lisp_Object map, int x, int y)
27611 while (CONSP (map))
27613 if (CONSP (XCAR (map))
27614 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27615 return XCAR (map);
27616 map = XCDR (map);
27619 return Qnil;
27622 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27623 3, 3, 0,
27624 doc: /* Lookup in image map MAP coordinates X and Y.
27625 An image map is an alist where each element has the format (AREA ID PLIST).
27626 An AREA is specified as either a rectangle, a circle, or a polygon:
27627 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27628 pixel coordinates of the upper left and bottom right corners.
27629 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27630 and the radius of the circle; r may be a float or integer.
27631 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27632 vector describes one corner in the polygon.
27633 Returns the alist element for the first matching AREA in MAP. */)
27634 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27636 if (NILP (map))
27637 return Qnil;
27639 CHECK_NUMBER (x);
27640 CHECK_NUMBER (y);
27642 return find_hot_spot (map,
27643 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27644 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27648 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27649 static void
27650 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27652 /* Do not change cursor shape while dragging mouse. */
27653 if (!NILP (do_mouse_tracking))
27654 return;
27656 if (!NILP (pointer))
27658 if (EQ (pointer, Qarrow))
27659 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27660 else if (EQ (pointer, Qhand))
27661 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27662 else if (EQ (pointer, Qtext))
27663 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27664 else if (EQ (pointer, intern ("hdrag")))
27665 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27666 #ifdef HAVE_X_WINDOWS
27667 else if (EQ (pointer, intern ("vdrag")))
27668 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27669 #endif
27670 else if (EQ (pointer, intern ("hourglass")))
27671 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27672 else if (EQ (pointer, Qmodeline))
27673 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27674 else
27675 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27678 if (cursor != No_Cursor)
27679 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27682 #endif /* HAVE_WINDOW_SYSTEM */
27684 /* Take proper action when mouse has moved to the mode or header line
27685 or marginal area AREA of window W, x-position X and y-position Y.
27686 X is relative to the start of the text display area of W, so the
27687 width of bitmap areas and scroll bars must be subtracted to get a
27688 position relative to the start of the mode line. */
27690 static void
27691 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27692 enum window_part area)
27694 struct window *w = XWINDOW (window);
27695 struct frame *f = XFRAME (w->frame);
27696 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27697 #ifdef HAVE_WINDOW_SYSTEM
27698 Display_Info *dpyinfo;
27699 #endif
27700 Cursor cursor = No_Cursor;
27701 Lisp_Object pointer = Qnil;
27702 int dx, dy, width, height;
27703 ptrdiff_t charpos;
27704 Lisp_Object string, object = Qnil;
27705 Lisp_Object pos IF_LINT (= Qnil), help;
27707 Lisp_Object mouse_face;
27708 int original_x_pixel = x;
27709 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27710 struct glyph_row *row IF_LINT (= 0);
27712 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27714 int x0;
27715 struct glyph *end;
27717 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27718 returns them in row/column units! */
27719 string = mode_line_string (w, area, &x, &y, &charpos,
27720 &object, &dx, &dy, &width, &height);
27722 row = (area == ON_MODE_LINE
27723 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27724 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27726 /* Find the glyph under the mouse pointer. */
27727 if (row->mode_line_p && row->enabled_p)
27729 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27730 end = glyph + row->used[TEXT_AREA];
27732 for (x0 = original_x_pixel;
27733 glyph < end && x0 >= glyph->pixel_width;
27734 ++glyph)
27735 x0 -= glyph->pixel_width;
27737 if (glyph >= end)
27738 glyph = NULL;
27741 else
27743 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27744 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27745 returns them in row/column units! */
27746 string = marginal_area_string (w, area, &x, &y, &charpos,
27747 &object, &dx, &dy, &width, &height);
27750 help = Qnil;
27752 #ifdef HAVE_WINDOW_SYSTEM
27753 if (IMAGEP (object))
27755 Lisp_Object image_map, hotspot;
27756 if ((image_map = Fplist_get (XCDR (object), QCmap),
27757 !NILP (image_map))
27758 && (hotspot = find_hot_spot (image_map, dx, dy),
27759 CONSP (hotspot))
27760 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27762 Lisp_Object plist;
27764 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27765 If so, we could look for mouse-enter, mouse-leave
27766 properties in PLIST (and do something...). */
27767 hotspot = XCDR (hotspot);
27768 if (CONSP (hotspot)
27769 && (plist = XCAR (hotspot), CONSP (plist)))
27771 pointer = Fplist_get (plist, Qpointer);
27772 if (NILP (pointer))
27773 pointer = Qhand;
27774 help = Fplist_get (plist, Qhelp_echo);
27775 if (!NILP (help))
27777 help_echo_string = help;
27778 XSETWINDOW (help_echo_window, w);
27779 help_echo_object = w->contents;
27780 help_echo_pos = charpos;
27784 if (NILP (pointer))
27785 pointer = Fplist_get (XCDR (object), QCpointer);
27787 #endif /* HAVE_WINDOW_SYSTEM */
27789 if (STRINGP (string))
27790 pos = make_number (charpos);
27792 /* Set the help text and mouse pointer. If the mouse is on a part
27793 of the mode line without any text (e.g. past the right edge of
27794 the mode line text), use the default help text and pointer. */
27795 if (STRINGP (string) || area == ON_MODE_LINE)
27797 /* Arrange to display the help by setting the global variables
27798 help_echo_string, help_echo_object, and help_echo_pos. */
27799 if (NILP (help))
27801 if (STRINGP (string))
27802 help = Fget_text_property (pos, Qhelp_echo, string);
27804 if (!NILP (help))
27806 help_echo_string = help;
27807 XSETWINDOW (help_echo_window, w);
27808 help_echo_object = string;
27809 help_echo_pos = charpos;
27811 else if (area == ON_MODE_LINE)
27813 Lisp_Object default_help
27814 = buffer_local_value_1 (Qmode_line_default_help_echo,
27815 w->contents);
27817 if (STRINGP (default_help))
27819 help_echo_string = default_help;
27820 XSETWINDOW (help_echo_window, w);
27821 help_echo_object = Qnil;
27822 help_echo_pos = -1;
27827 #ifdef HAVE_WINDOW_SYSTEM
27828 /* Change the mouse pointer according to what is under it. */
27829 if (FRAME_WINDOW_P (f))
27831 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27832 if (STRINGP (string))
27834 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27836 if (NILP (pointer))
27837 pointer = Fget_text_property (pos, Qpointer, string);
27839 /* Change the mouse pointer according to what is under X/Y. */
27840 if (NILP (pointer)
27841 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27843 Lisp_Object map;
27844 map = Fget_text_property (pos, Qlocal_map, string);
27845 if (!KEYMAPP (map))
27846 map = Fget_text_property (pos, Qkeymap, string);
27847 if (!KEYMAPP (map))
27848 cursor = dpyinfo->vertical_scroll_bar_cursor;
27851 else
27852 /* Default mode-line pointer. */
27853 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27855 #endif
27858 /* Change the mouse face according to what is under X/Y. */
27859 if (STRINGP (string))
27861 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27862 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27863 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27864 && glyph)
27866 Lisp_Object b, e;
27868 struct glyph * tmp_glyph;
27870 int gpos;
27871 int gseq_length;
27872 int total_pixel_width;
27873 ptrdiff_t begpos, endpos, ignore;
27875 int vpos, hpos;
27877 b = Fprevious_single_property_change (make_number (charpos + 1),
27878 Qmouse_face, string, Qnil);
27879 if (NILP (b))
27880 begpos = 0;
27881 else
27882 begpos = XINT (b);
27884 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27885 if (NILP (e))
27886 endpos = SCHARS (string);
27887 else
27888 endpos = XINT (e);
27890 /* Calculate the glyph position GPOS of GLYPH in the
27891 displayed string, relative to the beginning of the
27892 highlighted part of the string.
27894 Note: GPOS is different from CHARPOS. CHARPOS is the
27895 position of GLYPH in the internal string object. A mode
27896 line string format has structures which are converted to
27897 a flattened string by the Emacs Lisp interpreter. The
27898 internal string is an element of those structures. The
27899 displayed string is the flattened string. */
27900 tmp_glyph = row_start_glyph;
27901 while (tmp_glyph < glyph
27902 && (!(EQ (tmp_glyph->object, glyph->object)
27903 && begpos <= tmp_glyph->charpos
27904 && tmp_glyph->charpos < endpos)))
27905 tmp_glyph++;
27906 gpos = glyph - tmp_glyph;
27908 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27909 the highlighted part of the displayed string to which
27910 GLYPH belongs. Note: GSEQ_LENGTH is different from
27911 SCHARS (STRING), because the latter returns the length of
27912 the internal string. */
27913 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27914 tmp_glyph > glyph
27915 && (!(EQ (tmp_glyph->object, glyph->object)
27916 && begpos <= tmp_glyph->charpos
27917 && tmp_glyph->charpos < endpos));
27918 tmp_glyph--)
27920 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27922 /* Calculate the total pixel width of all the glyphs between
27923 the beginning of the highlighted area and GLYPH. */
27924 total_pixel_width = 0;
27925 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27926 total_pixel_width += tmp_glyph->pixel_width;
27928 /* Pre calculation of re-rendering position. Note: X is in
27929 column units here, after the call to mode_line_string or
27930 marginal_area_string. */
27931 hpos = x - gpos;
27932 vpos = (area == ON_MODE_LINE
27933 ? (w->current_matrix)->nrows - 1
27934 : 0);
27936 /* If GLYPH's position is included in the region that is
27937 already drawn in mouse face, we have nothing to do. */
27938 if ( EQ (window, hlinfo->mouse_face_window)
27939 && (!row->reversed_p
27940 ? (hlinfo->mouse_face_beg_col <= hpos
27941 && hpos < hlinfo->mouse_face_end_col)
27942 /* In R2L rows we swap BEG and END, see below. */
27943 : (hlinfo->mouse_face_end_col <= hpos
27944 && hpos < hlinfo->mouse_face_beg_col))
27945 && hlinfo->mouse_face_beg_row == vpos )
27946 return;
27948 if (clear_mouse_face (hlinfo))
27949 cursor = No_Cursor;
27951 if (!row->reversed_p)
27953 hlinfo->mouse_face_beg_col = hpos;
27954 hlinfo->mouse_face_beg_x = original_x_pixel
27955 - (total_pixel_width + dx);
27956 hlinfo->mouse_face_end_col = hpos + gseq_length;
27957 hlinfo->mouse_face_end_x = 0;
27959 else
27961 /* In R2L rows, show_mouse_face expects BEG and END
27962 coordinates to be swapped. */
27963 hlinfo->mouse_face_end_col = hpos;
27964 hlinfo->mouse_face_end_x = original_x_pixel
27965 - (total_pixel_width + dx);
27966 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27967 hlinfo->mouse_face_beg_x = 0;
27970 hlinfo->mouse_face_beg_row = vpos;
27971 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27972 hlinfo->mouse_face_beg_y = 0;
27973 hlinfo->mouse_face_end_y = 0;
27974 hlinfo->mouse_face_past_end = 0;
27975 hlinfo->mouse_face_window = window;
27977 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27978 charpos,
27979 0, 0, 0,
27980 &ignore,
27981 glyph->face_id,
27983 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27985 if (NILP (pointer))
27986 pointer = Qhand;
27988 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27989 clear_mouse_face (hlinfo);
27991 #ifdef HAVE_WINDOW_SYSTEM
27992 if (FRAME_WINDOW_P (f))
27993 define_frame_cursor1 (f, cursor, pointer);
27994 #endif
27998 /* EXPORT:
27999 Take proper action when the mouse has moved to position X, Y on
28000 frame F with regards to highlighting portions of display that have
28001 mouse-face properties. Also de-highlight portions of display where
28002 the mouse was before, set the mouse pointer shape as appropriate
28003 for the mouse coordinates, and activate help echo (tooltips).
28004 X and Y can be negative or out of range. */
28006 void
28007 note_mouse_highlight (struct frame *f, int x, int y)
28009 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28010 enum window_part part = ON_NOTHING;
28011 Lisp_Object window;
28012 struct window *w;
28013 Cursor cursor = No_Cursor;
28014 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28015 struct buffer *b;
28017 /* When a menu is active, don't highlight because this looks odd. */
28018 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28019 if (popup_activated ())
28020 return;
28021 #endif
28023 if (!f->glyphs_initialized_p
28024 || f->pointer_invisible)
28025 return;
28027 hlinfo->mouse_face_mouse_x = x;
28028 hlinfo->mouse_face_mouse_y = y;
28029 hlinfo->mouse_face_mouse_frame = f;
28031 if (hlinfo->mouse_face_defer)
28032 return;
28034 /* Which window is that in? */
28035 window = window_from_coordinates (f, x, y, &part, 1);
28037 /* If displaying active text in another window, clear that. */
28038 if (! EQ (window, hlinfo->mouse_face_window)
28039 /* Also clear if we move out of text area in same window. */
28040 || (!NILP (hlinfo->mouse_face_window)
28041 && !NILP (window)
28042 && part != ON_TEXT
28043 && part != ON_MODE_LINE
28044 && part != ON_HEADER_LINE))
28045 clear_mouse_face (hlinfo);
28047 /* Not on a window -> return. */
28048 if (!WINDOWP (window))
28049 return;
28051 /* Reset help_echo_string. It will get recomputed below. */
28052 help_echo_string = Qnil;
28054 /* Convert to window-relative pixel coordinates. */
28055 w = XWINDOW (window);
28056 frame_to_window_pixel_xy (w, &x, &y);
28058 #ifdef HAVE_WINDOW_SYSTEM
28059 /* Handle tool-bar window differently since it doesn't display a
28060 buffer. */
28061 if (EQ (window, f->tool_bar_window))
28063 note_tool_bar_highlight (f, x, y);
28064 return;
28066 #endif
28068 /* Mouse is on the mode, header line or margin? */
28069 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28070 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28072 note_mode_line_or_margin_highlight (window, x, y, part);
28073 return;
28076 #ifdef HAVE_WINDOW_SYSTEM
28077 if (part == ON_VERTICAL_BORDER)
28079 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28080 help_echo_string = build_string ("drag-mouse-1: resize");
28082 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28083 || part == ON_SCROLL_BAR)
28084 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28085 else
28086 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28087 #endif
28089 /* Are we in a window whose display is up to date?
28090 And verify the buffer's text has not changed. */
28091 b = XBUFFER (w->contents);
28092 if (part == ON_TEXT
28093 && w->window_end_valid
28094 && w->last_modified == BUF_MODIFF (b)
28095 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
28097 int hpos, vpos, dx, dy, area = LAST_AREA;
28098 ptrdiff_t pos;
28099 struct glyph *glyph;
28100 Lisp_Object object;
28101 Lisp_Object mouse_face = Qnil, position;
28102 Lisp_Object *overlay_vec = NULL;
28103 ptrdiff_t i, noverlays;
28104 struct buffer *obuf;
28105 ptrdiff_t obegv, ozv;
28106 int same_region;
28108 /* Find the glyph under X/Y. */
28109 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28111 #ifdef HAVE_WINDOW_SYSTEM
28112 /* Look for :pointer property on image. */
28113 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28115 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28116 if (img != NULL && IMAGEP (img->spec))
28118 Lisp_Object image_map, hotspot;
28119 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28120 !NILP (image_map))
28121 && (hotspot = find_hot_spot (image_map,
28122 glyph->slice.img.x + dx,
28123 glyph->slice.img.y + dy),
28124 CONSP (hotspot))
28125 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28127 Lisp_Object plist;
28129 /* Could check XCAR (hotspot) to see if we enter/leave
28130 this hot-spot.
28131 If so, we could look for mouse-enter, mouse-leave
28132 properties in PLIST (and do something...). */
28133 hotspot = XCDR (hotspot);
28134 if (CONSP (hotspot)
28135 && (plist = XCAR (hotspot), CONSP (plist)))
28137 pointer = Fplist_get (plist, Qpointer);
28138 if (NILP (pointer))
28139 pointer = Qhand;
28140 help_echo_string = Fplist_get (plist, Qhelp_echo);
28141 if (!NILP (help_echo_string))
28143 help_echo_window = window;
28144 help_echo_object = glyph->object;
28145 help_echo_pos = glyph->charpos;
28149 if (NILP (pointer))
28150 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28153 #endif /* HAVE_WINDOW_SYSTEM */
28155 /* Clear mouse face if X/Y not over text. */
28156 if (glyph == NULL
28157 || area != TEXT_AREA
28158 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28159 /* Glyph's OBJECT is an integer for glyphs inserted by the
28160 display engine for its internal purposes, like truncation
28161 and continuation glyphs and blanks beyond the end of
28162 line's text on text terminals. If we are over such a
28163 glyph, we are not over any text. */
28164 || INTEGERP (glyph->object)
28165 /* R2L rows have a stretch glyph at their front, which
28166 stands for no text, whereas L2R rows have no glyphs at
28167 all beyond the end of text. Treat such stretch glyphs
28168 like we do with NULL glyphs in L2R rows. */
28169 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28170 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28171 && glyph->type == STRETCH_GLYPH
28172 && glyph->avoid_cursor_p))
28174 if (clear_mouse_face (hlinfo))
28175 cursor = No_Cursor;
28176 #ifdef HAVE_WINDOW_SYSTEM
28177 if (FRAME_WINDOW_P (f) && NILP (pointer))
28179 if (area != TEXT_AREA)
28180 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28181 else
28182 pointer = Vvoid_text_area_pointer;
28184 #endif
28185 goto set_cursor;
28188 pos = glyph->charpos;
28189 object = glyph->object;
28190 if (!STRINGP (object) && !BUFFERP (object))
28191 goto set_cursor;
28193 /* If we get an out-of-range value, return now; avoid an error. */
28194 if (BUFFERP (object) && pos > BUF_Z (b))
28195 goto set_cursor;
28197 /* Make the window's buffer temporarily current for
28198 overlays_at and compute_char_face. */
28199 obuf = current_buffer;
28200 current_buffer = b;
28201 obegv = BEGV;
28202 ozv = ZV;
28203 BEGV = BEG;
28204 ZV = Z;
28206 /* Is this char mouse-active or does it have help-echo? */
28207 position = make_number (pos);
28209 if (BUFFERP (object))
28211 /* Put all the overlays we want in a vector in overlay_vec. */
28212 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28213 /* Sort overlays into increasing priority order. */
28214 noverlays = sort_overlays (overlay_vec, noverlays, w);
28216 else
28217 noverlays = 0;
28219 if (NILP (Vmouse_highlight))
28221 clear_mouse_face (hlinfo);
28222 goto check_help_echo;
28225 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28227 if (same_region)
28228 cursor = No_Cursor;
28230 /* Check mouse-face highlighting. */
28231 if (! same_region
28232 /* If there exists an overlay with mouse-face overlapping
28233 the one we are currently highlighting, we have to
28234 check if we enter the overlapping overlay, and then
28235 highlight only that. */
28236 || (OVERLAYP (hlinfo->mouse_face_overlay)
28237 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28239 /* Find the highest priority overlay with a mouse-face. */
28240 Lisp_Object overlay = Qnil;
28241 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28243 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28244 if (!NILP (mouse_face))
28245 overlay = overlay_vec[i];
28248 /* If we're highlighting the same overlay as before, there's
28249 no need to do that again. */
28250 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28251 goto check_help_echo;
28252 hlinfo->mouse_face_overlay = overlay;
28254 /* Clear the display of the old active region, if any. */
28255 if (clear_mouse_face (hlinfo))
28256 cursor = No_Cursor;
28258 /* If no overlay applies, get a text property. */
28259 if (NILP (overlay))
28260 mouse_face = Fget_text_property (position, Qmouse_face, object);
28262 /* Next, compute the bounds of the mouse highlighting and
28263 display it. */
28264 if (!NILP (mouse_face) && STRINGP (object))
28266 /* The mouse-highlighting comes from a display string
28267 with a mouse-face. */
28268 Lisp_Object s, e;
28269 ptrdiff_t ignore;
28271 s = Fprevious_single_property_change
28272 (make_number (pos + 1), Qmouse_face, object, Qnil);
28273 e = Fnext_single_property_change
28274 (position, Qmouse_face, object, Qnil);
28275 if (NILP (s))
28276 s = make_number (0);
28277 if (NILP (e))
28278 e = make_number (SCHARS (object) - 1);
28279 mouse_face_from_string_pos (w, hlinfo, object,
28280 XINT (s), XINT (e));
28281 hlinfo->mouse_face_past_end = 0;
28282 hlinfo->mouse_face_window = window;
28283 hlinfo->mouse_face_face_id
28284 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28285 glyph->face_id, 1);
28286 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28287 cursor = No_Cursor;
28289 else
28291 /* The mouse-highlighting, if any, comes from an overlay
28292 or text property in the buffer. */
28293 Lisp_Object buffer IF_LINT (= Qnil);
28294 Lisp_Object disp_string IF_LINT (= Qnil);
28296 if (STRINGP (object))
28298 /* If we are on a display string with no mouse-face,
28299 check if the text under it has one. */
28300 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28301 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28302 pos = string_buffer_position (object, start);
28303 if (pos > 0)
28305 mouse_face = get_char_property_and_overlay
28306 (make_number (pos), Qmouse_face, w->contents, &overlay);
28307 buffer = w->contents;
28308 disp_string = object;
28311 else
28313 buffer = object;
28314 disp_string = Qnil;
28317 if (!NILP (mouse_face))
28319 Lisp_Object before, after;
28320 Lisp_Object before_string, after_string;
28321 /* To correctly find the limits of mouse highlight
28322 in a bidi-reordered buffer, we must not use the
28323 optimization of limiting the search in
28324 previous-single-property-change and
28325 next-single-property-change, because
28326 rows_from_pos_range needs the real start and end
28327 positions to DTRT in this case. That's because
28328 the first row visible in a window does not
28329 necessarily display the character whose position
28330 is the smallest. */
28331 Lisp_Object lim1 =
28332 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28333 ? Fmarker_position (w->start)
28334 : Qnil;
28335 Lisp_Object lim2 =
28336 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28337 ? make_number (BUF_Z (XBUFFER (buffer))
28338 - XFASTINT (w->window_end_pos))
28339 : Qnil;
28341 if (NILP (overlay))
28343 /* Handle the text property case. */
28344 before = Fprevious_single_property_change
28345 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28346 after = Fnext_single_property_change
28347 (make_number (pos), Qmouse_face, buffer, lim2);
28348 before_string = after_string = Qnil;
28350 else
28352 /* Handle the overlay case. */
28353 before = Foverlay_start (overlay);
28354 after = Foverlay_end (overlay);
28355 before_string = Foverlay_get (overlay, Qbefore_string);
28356 after_string = Foverlay_get (overlay, Qafter_string);
28358 if (!STRINGP (before_string)) before_string = Qnil;
28359 if (!STRINGP (after_string)) after_string = Qnil;
28362 mouse_face_from_buffer_pos (window, hlinfo, pos,
28363 NILP (before)
28365 : XFASTINT (before),
28366 NILP (after)
28367 ? BUF_Z (XBUFFER (buffer))
28368 : XFASTINT (after),
28369 before_string, after_string,
28370 disp_string);
28371 cursor = No_Cursor;
28376 check_help_echo:
28378 /* Look for a `help-echo' property. */
28379 if (NILP (help_echo_string)) {
28380 Lisp_Object help, overlay;
28382 /* Check overlays first. */
28383 help = overlay = Qnil;
28384 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28386 overlay = overlay_vec[i];
28387 help = Foverlay_get (overlay, Qhelp_echo);
28390 if (!NILP (help))
28392 help_echo_string = help;
28393 help_echo_window = window;
28394 help_echo_object = overlay;
28395 help_echo_pos = pos;
28397 else
28399 Lisp_Object obj = glyph->object;
28400 ptrdiff_t charpos = glyph->charpos;
28402 /* Try text properties. */
28403 if (STRINGP (obj)
28404 && charpos >= 0
28405 && charpos < SCHARS (obj))
28407 help = Fget_text_property (make_number (charpos),
28408 Qhelp_echo, obj);
28409 if (NILP (help))
28411 /* If the string itself doesn't specify a help-echo,
28412 see if the buffer text ``under'' it does. */
28413 struct glyph_row *r
28414 = MATRIX_ROW (w->current_matrix, vpos);
28415 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28416 ptrdiff_t p = string_buffer_position (obj, start);
28417 if (p > 0)
28419 help = Fget_char_property (make_number (p),
28420 Qhelp_echo, w->contents);
28421 if (!NILP (help))
28423 charpos = p;
28424 obj = w->contents;
28429 else if (BUFFERP (obj)
28430 && charpos >= BEGV
28431 && charpos < ZV)
28432 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28433 obj);
28435 if (!NILP (help))
28437 help_echo_string = help;
28438 help_echo_window = window;
28439 help_echo_object = obj;
28440 help_echo_pos = charpos;
28445 #ifdef HAVE_WINDOW_SYSTEM
28446 /* Look for a `pointer' property. */
28447 if (FRAME_WINDOW_P (f) && NILP (pointer))
28449 /* Check overlays first. */
28450 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28451 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28453 if (NILP (pointer))
28455 Lisp_Object obj = glyph->object;
28456 ptrdiff_t charpos = glyph->charpos;
28458 /* Try text properties. */
28459 if (STRINGP (obj)
28460 && charpos >= 0
28461 && charpos < SCHARS (obj))
28463 pointer = Fget_text_property (make_number (charpos),
28464 Qpointer, obj);
28465 if (NILP (pointer))
28467 /* If the string itself doesn't specify a pointer,
28468 see if the buffer text ``under'' it does. */
28469 struct glyph_row *r
28470 = MATRIX_ROW (w->current_matrix, vpos);
28471 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28472 ptrdiff_t p = string_buffer_position (obj, start);
28473 if (p > 0)
28474 pointer = Fget_char_property (make_number (p),
28475 Qpointer, w->contents);
28478 else if (BUFFERP (obj)
28479 && charpos >= BEGV
28480 && charpos < ZV)
28481 pointer = Fget_text_property (make_number (charpos),
28482 Qpointer, obj);
28485 #endif /* HAVE_WINDOW_SYSTEM */
28487 BEGV = obegv;
28488 ZV = ozv;
28489 current_buffer = obuf;
28492 set_cursor:
28494 #ifdef HAVE_WINDOW_SYSTEM
28495 if (FRAME_WINDOW_P (f))
28496 define_frame_cursor1 (f, cursor, pointer);
28497 #else
28498 /* This is here to prevent a compiler error, about "label at end of
28499 compound statement". */
28500 return;
28501 #endif
28505 /* EXPORT for RIF:
28506 Clear any mouse-face on window W. This function is part of the
28507 redisplay interface, and is called from try_window_id and similar
28508 functions to ensure the mouse-highlight is off. */
28510 void
28511 x_clear_window_mouse_face (struct window *w)
28513 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28514 Lisp_Object window;
28516 block_input ();
28517 XSETWINDOW (window, w);
28518 if (EQ (window, hlinfo->mouse_face_window))
28519 clear_mouse_face (hlinfo);
28520 unblock_input ();
28524 /* EXPORT:
28525 Just discard the mouse face information for frame F, if any.
28526 This is used when the size of F is changed. */
28528 void
28529 cancel_mouse_face (struct frame *f)
28531 Lisp_Object window;
28532 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28534 window = hlinfo->mouse_face_window;
28535 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28537 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28538 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28539 hlinfo->mouse_face_window = Qnil;
28545 /***********************************************************************
28546 Exposure Events
28547 ***********************************************************************/
28549 #ifdef HAVE_WINDOW_SYSTEM
28551 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28552 which intersects rectangle R. R is in window-relative coordinates. */
28554 static void
28555 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28556 enum glyph_row_area area)
28558 struct glyph *first = row->glyphs[area];
28559 struct glyph *end = row->glyphs[area] + row->used[area];
28560 struct glyph *last;
28561 int first_x, start_x, x;
28563 if (area == TEXT_AREA && row->fill_line_p)
28564 /* If row extends face to end of line write the whole line. */
28565 draw_glyphs (w, 0, row, area,
28566 0, row->used[area],
28567 DRAW_NORMAL_TEXT, 0);
28568 else
28570 /* Set START_X to the window-relative start position for drawing glyphs of
28571 AREA. The first glyph of the text area can be partially visible.
28572 The first glyphs of other areas cannot. */
28573 start_x = window_box_left_offset (w, area);
28574 x = start_x;
28575 if (area == TEXT_AREA)
28576 x += row->x;
28578 /* Find the first glyph that must be redrawn. */
28579 while (first < end
28580 && x + first->pixel_width < r->x)
28582 x += first->pixel_width;
28583 ++first;
28586 /* Find the last one. */
28587 last = first;
28588 first_x = x;
28589 while (last < end
28590 && x < r->x + r->width)
28592 x += last->pixel_width;
28593 ++last;
28596 /* Repaint. */
28597 if (last > first)
28598 draw_glyphs (w, first_x - start_x, row, area,
28599 first - row->glyphs[area], last - row->glyphs[area],
28600 DRAW_NORMAL_TEXT, 0);
28605 /* Redraw the parts of the glyph row ROW on window W intersecting
28606 rectangle R. R is in window-relative coordinates. Value is
28607 non-zero if mouse-face was overwritten. */
28609 static int
28610 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28612 eassert (row->enabled_p);
28614 if (row->mode_line_p || w->pseudo_window_p)
28615 draw_glyphs (w, 0, row, TEXT_AREA,
28616 0, row->used[TEXT_AREA],
28617 DRAW_NORMAL_TEXT, 0);
28618 else
28620 if (row->used[LEFT_MARGIN_AREA])
28621 expose_area (w, row, r, LEFT_MARGIN_AREA);
28622 if (row->used[TEXT_AREA])
28623 expose_area (w, row, r, TEXT_AREA);
28624 if (row->used[RIGHT_MARGIN_AREA])
28625 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28626 draw_row_fringe_bitmaps (w, row);
28629 return row->mouse_face_p;
28633 /* Redraw those parts of glyphs rows during expose event handling that
28634 overlap other rows. Redrawing of an exposed line writes over parts
28635 of lines overlapping that exposed line; this function fixes that.
28637 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28638 row in W's current matrix that is exposed and overlaps other rows.
28639 LAST_OVERLAPPING_ROW is the last such row. */
28641 static void
28642 expose_overlaps (struct window *w,
28643 struct glyph_row *first_overlapping_row,
28644 struct glyph_row *last_overlapping_row,
28645 XRectangle *r)
28647 struct glyph_row *row;
28649 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28650 if (row->overlapping_p)
28652 eassert (row->enabled_p && !row->mode_line_p);
28654 row->clip = r;
28655 if (row->used[LEFT_MARGIN_AREA])
28656 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28658 if (row->used[TEXT_AREA])
28659 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28661 if (row->used[RIGHT_MARGIN_AREA])
28662 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28663 row->clip = NULL;
28668 /* Return non-zero if W's cursor intersects rectangle R. */
28670 static int
28671 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28673 XRectangle cr, result;
28674 struct glyph *cursor_glyph;
28675 struct glyph_row *row;
28677 if (w->phys_cursor.vpos >= 0
28678 && w->phys_cursor.vpos < w->current_matrix->nrows
28679 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28680 row->enabled_p)
28681 && row->cursor_in_fringe_p)
28683 /* Cursor is in the fringe. */
28684 cr.x = window_box_right_offset (w,
28685 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28686 ? RIGHT_MARGIN_AREA
28687 : TEXT_AREA));
28688 cr.y = row->y;
28689 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28690 cr.height = row->height;
28691 return x_intersect_rectangles (&cr, r, &result);
28694 cursor_glyph = get_phys_cursor_glyph (w);
28695 if (cursor_glyph)
28697 /* r is relative to W's box, but w->phys_cursor.x is relative
28698 to left edge of W's TEXT area. Adjust it. */
28699 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28700 cr.y = w->phys_cursor.y;
28701 cr.width = cursor_glyph->pixel_width;
28702 cr.height = w->phys_cursor_height;
28703 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28704 I assume the effect is the same -- and this is portable. */
28705 return x_intersect_rectangles (&cr, r, &result);
28707 /* If we don't understand the format, pretend we're not in the hot-spot. */
28708 return 0;
28712 /* EXPORT:
28713 Draw a vertical window border to the right of window W if W doesn't
28714 have vertical scroll bars. */
28716 void
28717 x_draw_vertical_border (struct window *w)
28719 struct frame *f = XFRAME (WINDOW_FRAME (w));
28721 /* We could do better, if we knew what type of scroll-bar the adjacent
28722 windows (on either side) have... But we don't :-(
28723 However, I think this works ok. ++KFS 2003-04-25 */
28725 /* Redraw borders between horizontally adjacent windows. Don't
28726 do it for frames with vertical scroll bars because either the
28727 right scroll bar of a window, or the left scroll bar of its
28728 neighbor will suffice as a border. */
28729 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28730 return;
28732 /* Note: It is necessary to redraw both the left and the right
28733 borders, for when only this single window W is being
28734 redisplayed. */
28735 if (!WINDOW_RIGHTMOST_P (w)
28736 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28738 int x0, x1, y0, y1;
28740 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28741 y1 -= 1;
28743 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28744 x1 -= 1;
28746 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28748 if (!WINDOW_LEFTMOST_P (w)
28749 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28751 int x0, x1, y0, y1;
28753 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28754 y1 -= 1;
28756 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28757 x0 -= 1;
28759 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28764 /* Redraw the part of window W intersection rectangle FR. Pixel
28765 coordinates in FR are frame-relative. Call this function with
28766 input blocked. Value is non-zero if the exposure overwrites
28767 mouse-face. */
28769 static int
28770 expose_window (struct window *w, XRectangle *fr)
28772 struct frame *f = XFRAME (w->frame);
28773 XRectangle wr, r;
28774 int mouse_face_overwritten_p = 0;
28776 /* If window is not yet fully initialized, do nothing. This can
28777 happen when toolkit scroll bars are used and a window is split.
28778 Reconfiguring the scroll bar will generate an expose for a newly
28779 created window. */
28780 if (w->current_matrix == NULL)
28781 return 0;
28783 /* When we're currently updating the window, display and current
28784 matrix usually don't agree. Arrange for a thorough display
28785 later. */
28786 if (w == updated_window)
28788 SET_FRAME_GARBAGED (f);
28789 return 0;
28792 /* Frame-relative pixel rectangle of W. */
28793 wr.x = WINDOW_LEFT_EDGE_X (w);
28794 wr.y = WINDOW_TOP_EDGE_Y (w);
28795 wr.width = WINDOW_TOTAL_WIDTH (w);
28796 wr.height = WINDOW_TOTAL_HEIGHT (w);
28798 if (x_intersect_rectangles (fr, &wr, &r))
28800 int yb = window_text_bottom_y (w);
28801 struct glyph_row *row;
28802 int cursor_cleared_p, phys_cursor_on_p;
28803 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28805 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28806 r.x, r.y, r.width, r.height));
28808 /* Convert to window coordinates. */
28809 r.x -= WINDOW_LEFT_EDGE_X (w);
28810 r.y -= WINDOW_TOP_EDGE_Y (w);
28812 /* Turn off the cursor. */
28813 if (!w->pseudo_window_p
28814 && phys_cursor_in_rect_p (w, &r))
28816 x_clear_cursor (w);
28817 cursor_cleared_p = 1;
28819 else
28820 cursor_cleared_p = 0;
28822 /* If the row containing the cursor extends face to end of line,
28823 then expose_area might overwrite the cursor outside the
28824 rectangle and thus notice_overwritten_cursor might clear
28825 w->phys_cursor_on_p. We remember the original value and
28826 check later if it is changed. */
28827 phys_cursor_on_p = w->phys_cursor_on_p;
28829 /* Update lines intersecting rectangle R. */
28830 first_overlapping_row = last_overlapping_row = NULL;
28831 for (row = w->current_matrix->rows;
28832 row->enabled_p;
28833 ++row)
28835 int y0 = row->y;
28836 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28838 if ((y0 >= r.y && y0 < r.y + r.height)
28839 || (y1 > r.y && y1 < r.y + r.height)
28840 || (r.y >= y0 && r.y < y1)
28841 || (r.y + r.height > y0 && r.y + r.height < y1))
28843 /* A header line may be overlapping, but there is no need
28844 to fix overlapping areas for them. KFS 2005-02-12 */
28845 if (row->overlapping_p && !row->mode_line_p)
28847 if (first_overlapping_row == NULL)
28848 first_overlapping_row = row;
28849 last_overlapping_row = row;
28852 row->clip = fr;
28853 if (expose_line (w, row, &r))
28854 mouse_face_overwritten_p = 1;
28855 row->clip = NULL;
28857 else if (row->overlapping_p)
28859 /* We must redraw a row overlapping the exposed area. */
28860 if (y0 < r.y
28861 ? y0 + row->phys_height > r.y
28862 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28864 if (first_overlapping_row == NULL)
28865 first_overlapping_row = row;
28866 last_overlapping_row = row;
28870 if (y1 >= yb)
28871 break;
28874 /* Display the mode line if there is one. */
28875 if (WINDOW_WANTS_MODELINE_P (w)
28876 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28877 row->enabled_p)
28878 && row->y < r.y + r.height)
28880 if (expose_line (w, row, &r))
28881 mouse_face_overwritten_p = 1;
28884 if (!w->pseudo_window_p)
28886 /* Fix the display of overlapping rows. */
28887 if (first_overlapping_row)
28888 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28889 fr);
28891 /* Draw border between windows. */
28892 x_draw_vertical_border (w);
28894 /* Turn the cursor on again. */
28895 if (cursor_cleared_p
28896 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28897 update_window_cursor (w, 1);
28901 return mouse_face_overwritten_p;
28906 /* Redraw (parts) of all windows in the window tree rooted at W that
28907 intersect R. R contains frame pixel coordinates. Value is
28908 non-zero if the exposure overwrites mouse-face. */
28910 static int
28911 expose_window_tree (struct window *w, XRectangle *r)
28913 struct frame *f = XFRAME (w->frame);
28914 int mouse_face_overwritten_p = 0;
28916 while (w && !FRAME_GARBAGED_P (f))
28918 if (WINDOWP (w->contents))
28919 mouse_face_overwritten_p
28920 |= expose_window_tree (XWINDOW (w->contents), r);
28921 else
28922 mouse_face_overwritten_p |= expose_window (w, r);
28924 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28927 return mouse_face_overwritten_p;
28931 /* EXPORT:
28932 Redisplay an exposed area of frame F. X and Y are the upper-left
28933 corner of the exposed rectangle. W and H are width and height of
28934 the exposed area. All are pixel values. W or H zero means redraw
28935 the entire frame. */
28937 void
28938 expose_frame (struct frame *f, int x, int y, int w, int h)
28940 XRectangle r;
28941 int mouse_face_overwritten_p = 0;
28943 TRACE ((stderr, "expose_frame "));
28945 /* No need to redraw if frame will be redrawn soon. */
28946 if (FRAME_GARBAGED_P (f))
28948 TRACE ((stderr, " garbaged\n"));
28949 return;
28952 /* If basic faces haven't been realized yet, there is no point in
28953 trying to redraw anything. This can happen when we get an expose
28954 event while Emacs is starting, e.g. by moving another window. */
28955 if (FRAME_FACE_CACHE (f) == NULL
28956 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28958 TRACE ((stderr, " no faces\n"));
28959 return;
28962 if (w == 0 || h == 0)
28964 r.x = r.y = 0;
28965 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28966 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28968 else
28970 r.x = x;
28971 r.y = y;
28972 r.width = w;
28973 r.height = h;
28976 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28977 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28979 if (WINDOWP (f->tool_bar_window))
28980 mouse_face_overwritten_p
28981 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28983 #ifdef HAVE_X_WINDOWS
28984 #ifndef MSDOS
28985 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28986 if (WINDOWP (f->menu_bar_window))
28987 mouse_face_overwritten_p
28988 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28989 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28990 #endif
28991 #endif
28993 /* Some window managers support a focus-follows-mouse style with
28994 delayed raising of frames. Imagine a partially obscured frame,
28995 and moving the mouse into partially obscured mouse-face on that
28996 frame. The visible part of the mouse-face will be highlighted,
28997 then the WM raises the obscured frame. With at least one WM, KDE
28998 2.1, Emacs is not getting any event for the raising of the frame
28999 (even tried with SubstructureRedirectMask), only Expose events.
29000 These expose events will draw text normally, i.e. not
29001 highlighted. Which means we must redo the highlight here.
29002 Subsume it under ``we love X''. --gerd 2001-08-15 */
29003 /* Included in Windows version because Windows most likely does not
29004 do the right thing if any third party tool offers
29005 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29006 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29008 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29009 if (f == hlinfo->mouse_face_mouse_frame)
29011 int mouse_x = hlinfo->mouse_face_mouse_x;
29012 int mouse_y = hlinfo->mouse_face_mouse_y;
29013 clear_mouse_face (hlinfo);
29014 note_mouse_highlight (f, mouse_x, mouse_y);
29020 /* EXPORT:
29021 Determine the intersection of two rectangles R1 and R2. Return
29022 the intersection in *RESULT. Value is non-zero if RESULT is not
29023 empty. */
29026 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29028 XRectangle *left, *right;
29029 XRectangle *upper, *lower;
29030 int intersection_p = 0;
29032 /* Rearrange so that R1 is the left-most rectangle. */
29033 if (r1->x < r2->x)
29034 left = r1, right = r2;
29035 else
29036 left = r2, right = r1;
29038 /* X0 of the intersection is right.x0, if this is inside R1,
29039 otherwise there is no intersection. */
29040 if (right->x <= left->x + left->width)
29042 result->x = right->x;
29044 /* The right end of the intersection is the minimum of
29045 the right ends of left and right. */
29046 result->width = (min (left->x + left->width, right->x + right->width)
29047 - result->x);
29049 /* Same game for Y. */
29050 if (r1->y < r2->y)
29051 upper = r1, lower = r2;
29052 else
29053 upper = r2, lower = r1;
29055 /* The upper end of the intersection is lower.y0, if this is inside
29056 of upper. Otherwise, there is no intersection. */
29057 if (lower->y <= upper->y + upper->height)
29059 result->y = lower->y;
29061 /* The lower end of the intersection is the minimum of the lower
29062 ends of upper and lower. */
29063 result->height = (min (lower->y + lower->height,
29064 upper->y + upper->height)
29065 - result->y);
29066 intersection_p = 1;
29070 return intersection_p;
29073 #endif /* HAVE_WINDOW_SYSTEM */
29076 /***********************************************************************
29077 Initialization
29078 ***********************************************************************/
29080 void
29081 syms_of_xdisp (void)
29083 Vwith_echo_area_save_vector = Qnil;
29084 staticpro (&Vwith_echo_area_save_vector);
29086 Vmessage_stack = Qnil;
29087 staticpro (&Vmessage_stack);
29089 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29090 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29092 message_dolog_marker1 = Fmake_marker ();
29093 staticpro (&message_dolog_marker1);
29094 message_dolog_marker2 = Fmake_marker ();
29095 staticpro (&message_dolog_marker2);
29096 message_dolog_marker3 = Fmake_marker ();
29097 staticpro (&message_dolog_marker3);
29099 #ifdef GLYPH_DEBUG
29100 defsubr (&Sdump_frame_glyph_matrix);
29101 defsubr (&Sdump_glyph_matrix);
29102 defsubr (&Sdump_glyph_row);
29103 defsubr (&Sdump_tool_bar_row);
29104 defsubr (&Strace_redisplay);
29105 defsubr (&Strace_to_stderr);
29106 #endif
29107 #ifdef HAVE_WINDOW_SYSTEM
29108 defsubr (&Stool_bar_lines_needed);
29109 defsubr (&Slookup_image_map);
29110 #endif
29111 defsubr (&Sline_pixel_height);
29112 defsubr (&Sformat_mode_line);
29113 defsubr (&Sinvisible_p);
29114 defsubr (&Scurrent_bidi_paragraph_direction);
29115 defsubr (&Smove_point_visually);
29117 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29118 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29119 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29120 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29121 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29122 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29123 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29124 DEFSYM (Qeval, "eval");
29125 DEFSYM (QCdata, ":data");
29126 DEFSYM (Qdisplay, "display");
29127 DEFSYM (Qspace_width, "space-width");
29128 DEFSYM (Qraise, "raise");
29129 DEFSYM (Qslice, "slice");
29130 DEFSYM (Qspace, "space");
29131 DEFSYM (Qmargin, "margin");
29132 DEFSYM (Qpointer, "pointer");
29133 DEFSYM (Qleft_margin, "left-margin");
29134 DEFSYM (Qright_margin, "right-margin");
29135 DEFSYM (Qcenter, "center");
29136 DEFSYM (Qline_height, "line-height");
29137 DEFSYM (QCalign_to, ":align-to");
29138 DEFSYM (QCrelative_width, ":relative-width");
29139 DEFSYM (QCrelative_height, ":relative-height");
29140 DEFSYM (QCeval, ":eval");
29141 DEFSYM (QCpropertize, ":propertize");
29142 DEFSYM (QCfile, ":file");
29143 DEFSYM (Qfontified, "fontified");
29144 DEFSYM (Qfontification_functions, "fontification-functions");
29145 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29146 DEFSYM (Qescape_glyph, "escape-glyph");
29147 DEFSYM (Qnobreak_space, "nobreak-space");
29148 DEFSYM (Qimage, "image");
29149 DEFSYM (Qtext, "text");
29150 DEFSYM (Qboth, "both");
29151 DEFSYM (Qboth_horiz, "both-horiz");
29152 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29153 DEFSYM (QCmap, ":map");
29154 DEFSYM (QCpointer, ":pointer");
29155 DEFSYM (Qrect, "rect");
29156 DEFSYM (Qcircle, "circle");
29157 DEFSYM (Qpoly, "poly");
29158 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29159 DEFSYM (Qgrow_only, "grow-only");
29160 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29161 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29162 DEFSYM (Qposition, "position");
29163 DEFSYM (Qbuffer_position, "buffer-position");
29164 DEFSYM (Qobject, "object");
29165 DEFSYM (Qbar, "bar");
29166 DEFSYM (Qhbar, "hbar");
29167 DEFSYM (Qbox, "box");
29168 DEFSYM (Qhollow, "hollow");
29169 DEFSYM (Qhand, "hand");
29170 DEFSYM (Qarrow, "arrow");
29171 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29173 list_of_error = Fcons (Fcons (intern_c_string ("error"),
29174 Fcons (intern_c_string ("void-variable"), Qnil)),
29175 Qnil);
29176 staticpro (&list_of_error);
29178 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29179 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29180 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29181 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29183 echo_buffer[0] = echo_buffer[1] = Qnil;
29184 staticpro (&echo_buffer[0]);
29185 staticpro (&echo_buffer[1]);
29187 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29188 staticpro (&echo_area_buffer[0]);
29189 staticpro (&echo_area_buffer[1]);
29191 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29192 staticpro (&Vmessages_buffer_name);
29194 mode_line_proptrans_alist = Qnil;
29195 staticpro (&mode_line_proptrans_alist);
29196 mode_line_string_list = Qnil;
29197 staticpro (&mode_line_string_list);
29198 mode_line_string_face = Qnil;
29199 staticpro (&mode_line_string_face);
29200 mode_line_string_face_prop = Qnil;
29201 staticpro (&mode_line_string_face_prop);
29202 Vmode_line_unwind_vector = Qnil;
29203 staticpro (&Vmode_line_unwind_vector);
29205 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29207 help_echo_string = Qnil;
29208 staticpro (&help_echo_string);
29209 help_echo_object = Qnil;
29210 staticpro (&help_echo_object);
29211 help_echo_window = Qnil;
29212 staticpro (&help_echo_window);
29213 previous_help_echo_string = Qnil;
29214 staticpro (&previous_help_echo_string);
29215 help_echo_pos = -1;
29217 DEFSYM (Qright_to_left, "right-to-left");
29218 DEFSYM (Qleft_to_right, "left-to-right");
29220 #ifdef HAVE_WINDOW_SYSTEM
29221 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29222 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29223 For example, if a block cursor is over a tab, it will be drawn as
29224 wide as that tab on the display. */);
29225 x_stretch_cursor_p = 0;
29226 #endif
29228 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29229 doc: /* Non-nil means highlight trailing whitespace.
29230 The face used for trailing whitespace is `trailing-whitespace'. */);
29231 Vshow_trailing_whitespace = Qnil;
29233 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29234 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29235 If the value is t, Emacs highlights non-ASCII chars which have the
29236 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29237 or `escape-glyph' face respectively.
29239 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29240 U+2011 (non-breaking hyphen) are affected.
29242 Any other non-nil value means to display these characters as a escape
29243 glyph followed by an ordinary space or hyphen.
29245 A value of nil means no special handling of these characters. */);
29246 Vnobreak_char_display = Qt;
29248 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29249 doc: /* The pointer shape to show in void text areas.
29250 A value of nil means to show the text pointer. Other options are `arrow',
29251 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29252 Vvoid_text_area_pointer = Qarrow;
29254 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29255 doc: /* Non-nil means don't actually do any redisplay.
29256 This is used for internal purposes. */);
29257 Vinhibit_redisplay = Qnil;
29259 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29260 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29261 Vglobal_mode_string = Qnil;
29263 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29264 doc: /* Marker for where to display an arrow on top of the buffer text.
29265 This must be the beginning of a line in order to work.
29266 See also `overlay-arrow-string'. */);
29267 Voverlay_arrow_position = Qnil;
29269 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29270 doc: /* String to display as an arrow in non-window frames.
29271 See also `overlay-arrow-position'. */);
29272 Voverlay_arrow_string = build_pure_c_string ("=>");
29274 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29275 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29276 The symbols on this list are examined during redisplay to determine
29277 where to display overlay arrows. */);
29278 Voverlay_arrow_variable_list
29279 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
29281 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29282 doc: /* The number of lines to try scrolling a window by when point moves out.
29283 If that fails to bring point back on frame, point is centered instead.
29284 If this is zero, point is always centered after it moves off frame.
29285 If you want scrolling to always be a line at a time, you should set
29286 `scroll-conservatively' to a large value rather than set this to 1. */);
29288 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29289 doc: /* Scroll up to this many lines, to bring point back on screen.
29290 If point moves off-screen, redisplay will scroll by up to
29291 `scroll-conservatively' lines in order to bring point just barely
29292 onto the screen again. If that cannot be done, then redisplay
29293 recenters point as usual.
29295 If the value is greater than 100, redisplay will never recenter point,
29296 but will always scroll just enough text to bring point into view, even
29297 if you move far away.
29299 A value of zero means always recenter point if it moves off screen. */);
29300 scroll_conservatively = 0;
29302 DEFVAR_INT ("scroll-margin", scroll_margin,
29303 doc: /* Number of lines of margin at the top and bottom of a window.
29304 Recenter the window whenever point gets within this many lines
29305 of the top or bottom of the window. */);
29306 scroll_margin = 0;
29308 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29309 doc: /* Pixels per inch value for non-window system displays.
29310 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29311 Vdisplay_pixels_per_inch = make_float (72.0);
29313 #ifdef GLYPH_DEBUG
29314 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29315 #endif
29317 DEFVAR_LISP ("truncate-partial-width-windows",
29318 Vtruncate_partial_width_windows,
29319 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29320 For an integer value, truncate lines in each window narrower than the
29321 full frame width, provided the window width is less than that integer;
29322 otherwise, respect the value of `truncate-lines'.
29324 For any other non-nil value, truncate lines in all windows that do
29325 not span the full frame width.
29327 A value of nil means to respect the value of `truncate-lines'.
29329 If `word-wrap' is enabled, you might want to reduce this. */);
29330 Vtruncate_partial_width_windows = make_number (50);
29332 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29333 doc: /* Maximum buffer size for which line number should be displayed.
29334 If the buffer is bigger than this, the line number does not appear
29335 in the mode line. A value of nil means no limit. */);
29336 Vline_number_display_limit = Qnil;
29338 DEFVAR_INT ("line-number-display-limit-width",
29339 line_number_display_limit_width,
29340 doc: /* Maximum line width (in characters) for line number display.
29341 If the average length of the lines near point is bigger than this, then the
29342 line number may be omitted from the mode line. */);
29343 line_number_display_limit_width = 200;
29345 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29346 doc: /* Non-nil means highlight region even in nonselected windows. */);
29347 highlight_nonselected_windows = 0;
29349 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29350 doc: /* Non-nil if more than one frame is visible on this display.
29351 Minibuffer-only frames don't count, but iconified frames do.
29352 This variable is not guaranteed to be accurate except while processing
29353 `frame-title-format' and `icon-title-format'. */);
29355 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29356 doc: /* Template for displaying the title bar of visible frames.
29357 \(Assuming the window manager supports this feature.)
29359 This variable has the same structure as `mode-line-format', except that
29360 the %c and %l constructs are ignored. It is used only on frames for
29361 which no explicit name has been set \(see `modify-frame-parameters'). */);
29363 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29364 doc: /* Template for displaying the title bar of an iconified frame.
29365 \(Assuming the window manager supports this feature.)
29366 This variable has the same structure as `mode-line-format' (which see),
29367 and is used only on frames for which no explicit name has been set
29368 \(see `modify-frame-parameters'). */);
29369 Vicon_title_format
29370 = Vframe_title_format
29371 = listn (CONSTYPE_PURE, 3,
29372 intern_c_string ("multiple-frames"),
29373 build_pure_c_string ("%b"),
29374 listn (CONSTYPE_PURE, 4,
29375 empty_unibyte_string,
29376 intern_c_string ("invocation-name"),
29377 build_pure_c_string ("@"),
29378 intern_c_string ("system-name")));
29380 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29381 doc: /* Maximum number of lines to keep in the message log buffer.
29382 If nil, disable message logging. If t, log messages but don't truncate
29383 the buffer when it becomes large. */);
29384 Vmessage_log_max = make_number (1000);
29386 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29387 doc: /* Functions called before redisplay, if window sizes have changed.
29388 The value should be a list of functions that take one argument.
29389 Just before redisplay, for each frame, if any of its windows have changed
29390 size since the last redisplay, or have been split or deleted,
29391 all the functions in the list are called, with the frame as argument. */);
29392 Vwindow_size_change_functions = Qnil;
29394 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29395 doc: /* List of functions to call before redisplaying a window with scrolling.
29396 Each function is called with two arguments, the window and its new
29397 display-start position. Note that these functions are also called by
29398 `set-window-buffer'. Also note that the value of `window-end' is not
29399 valid when these functions are called.
29401 Warning: Do not use this feature to alter the way the window
29402 is scrolled. It is not designed for that, and such use probably won't
29403 work. */);
29404 Vwindow_scroll_functions = Qnil;
29406 DEFVAR_LISP ("window-text-change-functions",
29407 Vwindow_text_change_functions,
29408 doc: /* Functions to call in redisplay when text in the window might change. */);
29409 Vwindow_text_change_functions = Qnil;
29411 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29412 doc: /* Functions called when redisplay of a window reaches the end trigger.
29413 Each function is called with two arguments, the window and the end trigger value.
29414 See `set-window-redisplay-end-trigger'. */);
29415 Vredisplay_end_trigger_functions = Qnil;
29417 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29418 doc: /* Non-nil means autoselect window with mouse pointer.
29419 If nil, do not autoselect windows.
29420 A positive number means delay autoselection by that many seconds: a
29421 window is autoselected only after the mouse has remained in that
29422 window for the duration of the delay.
29423 A negative number has a similar effect, but causes windows to be
29424 autoselected only after the mouse has stopped moving. \(Because of
29425 the way Emacs compares mouse events, you will occasionally wait twice
29426 that time before the window gets selected.\)
29427 Any other value means to autoselect window instantaneously when the
29428 mouse pointer enters it.
29430 Autoselection selects the minibuffer only if it is active, and never
29431 unselects the minibuffer if it is active.
29433 When customizing this variable make sure that the actual value of
29434 `focus-follows-mouse' matches the behavior of your window manager. */);
29435 Vmouse_autoselect_window = Qnil;
29437 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29438 doc: /* Non-nil means automatically resize tool-bars.
29439 This dynamically changes the tool-bar's height to the minimum height
29440 that is needed to make all tool-bar items visible.
29441 If value is `grow-only', the tool-bar's height is only increased
29442 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29443 Vauto_resize_tool_bars = Qt;
29445 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29446 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29447 auto_raise_tool_bar_buttons_p = 1;
29449 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29450 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29451 make_cursor_line_fully_visible_p = 1;
29453 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29454 doc: /* Border below tool-bar in pixels.
29455 If an integer, use it as the height of the border.
29456 If it is one of `internal-border-width' or `border-width', use the
29457 value of the corresponding frame parameter.
29458 Otherwise, no border is added below the tool-bar. */);
29459 Vtool_bar_border = Qinternal_border_width;
29461 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29462 doc: /* Margin around tool-bar buttons in pixels.
29463 If an integer, use that for both horizontal and vertical margins.
29464 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29465 HORZ specifying the horizontal margin, and VERT specifying the
29466 vertical margin. */);
29467 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29469 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29470 doc: /* Relief thickness of tool-bar buttons. */);
29471 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29473 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29474 doc: /* Tool bar style to use.
29475 It can be one of
29476 image - show images only
29477 text - show text only
29478 both - show both, text below image
29479 both-horiz - show text to the right of the image
29480 text-image-horiz - show text to the left of the image
29481 any other - use system default or image if no system default.
29483 This variable only affects the GTK+ toolkit version of Emacs. */);
29484 Vtool_bar_style = Qnil;
29486 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29487 doc: /* Maximum number of characters a label can have to be shown.
29488 The tool bar style must also show labels for this to have any effect, see
29489 `tool-bar-style'. */);
29490 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29492 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29493 doc: /* List of functions to call to fontify regions of text.
29494 Each function is called with one argument POS. Functions must
29495 fontify a region starting at POS in the current buffer, and give
29496 fontified regions the property `fontified'. */);
29497 Vfontification_functions = Qnil;
29498 Fmake_variable_buffer_local (Qfontification_functions);
29500 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29501 unibyte_display_via_language_environment,
29502 doc: /* Non-nil means display unibyte text according to language environment.
29503 Specifically, this means that raw bytes in the range 160-255 decimal
29504 are displayed by converting them to the equivalent multibyte characters
29505 according to the current language environment. As a result, they are
29506 displayed according to the current fontset.
29508 Note that this variable affects only how these bytes are displayed,
29509 but does not change the fact they are interpreted as raw bytes. */);
29510 unibyte_display_via_language_environment = 0;
29512 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29513 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29514 If a float, it specifies a fraction of the mini-window frame's height.
29515 If an integer, it specifies a number of lines. */);
29516 Vmax_mini_window_height = make_float (0.25);
29518 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29519 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29520 A value of nil means don't automatically resize mini-windows.
29521 A value of t means resize them to fit the text displayed in them.
29522 A value of `grow-only', the default, means let mini-windows grow only;
29523 they return to their normal size when the minibuffer is closed, or the
29524 echo area becomes empty. */);
29525 Vresize_mini_windows = Qgrow_only;
29527 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29528 doc: /* Alist specifying how to blink the cursor off.
29529 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29530 `cursor-type' frame-parameter or variable equals ON-STATE,
29531 comparing using `equal', Emacs uses OFF-STATE to specify
29532 how to blink it off. ON-STATE and OFF-STATE are values for
29533 the `cursor-type' frame parameter.
29535 If a frame's ON-STATE has no entry in this list,
29536 the frame's other specifications determine how to blink the cursor off. */);
29537 Vblink_cursor_alist = Qnil;
29539 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29540 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29541 If non-nil, windows are automatically scrolled horizontally to make
29542 point visible. */);
29543 automatic_hscrolling_p = 1;
29544 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29546 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29547 doc: /* How many columns away from the window edge point is allowed to get
29548 before automatic hscrolling will horizontally scroll the window. */);
29549 hscroll_margin = 5;
29551 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29552 doc: /* How many columns to scroll the window when point gets too close to the edge.
29553 When point is less than `hscroll-margin' columns from the window
29554 edge, automatic hscrolling will scroll the window by the amount of columns
29555 determined by this variable. If its value is a positive integer, scroll that
29556 many columns. If it's a positive floating-point number, it specifies the
29557 fraction of the window's width to scroll. If it's nil or zero, point will be
29558 centered horizontally after the scroll. Any other value, including negative
29559 numbers, are treated as if the value were zero.
29561 Automatic hscrolling always moves point outside the scroll margin, so if
29562 point was more than scroll step columns inside the margin, the window will
29563 scroll more than the value given by the scroll step.
29565 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29566 and `scroll-right' overrides this variable's effect. */);
29567 Vhscroll_step = make_number (0);
29569 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29570 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29571 Bind this around calls to `message' to let it take effect. */);
29572 message_truncate_lines = 0;
29574 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29575 doc: /* Normal hook run to update the menu bar definitions.
29576 Redisplay runs this hook before it redisplays the menu bar.
29577 This is used to update submenus such as Buffers,
29578 whose contents depend on various data. */);
29579 Vmenu_bar_update_hook = Qnil;
29581 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29582 doc: /* Frame for which we are updating a menu.
29583 The enable predicate for a menu binding should check this variable. */);
29584 Vmenu_updating_frame = Qnil;
29586 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29587 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29588 inhibit_menubar_update = 0;
29590 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29591 doc: /* Prefix prepended to all continuation lines at display time.
29592 The value may be a string, an image, or a stretch-glyph; it is
29593 interpreted in the same way as the value of a `display' text property.
29595 This variable is overridden by any `wrap-prefix' text or overlay
29596 property.
29598 To add a prefix to non-continuation lines, use `line-prefix'. */);
29599 Vwrap_prefix = Qnil;
29600 DEFSYM (Qwrap_prefix, "wrap-prefix");
29601 Fmake_variable_buffer_local (Qwrap_prefix);
29603 DEFVAR_LISP ("line-prefix", Vline_prefix,
29604 doc: /* Prefix prepended to all non-continuation lines at display time.
29605 The value may be a string, an image, or a stretch-glyph; it is
29606 interpreted in the same way as the value of a `display' text property.
29608 This variable is overridden by any `line-prefix' text or overlay
29609 property.
29611 To add a prefix to continuation lines, use `wrap-prefix'. */);
29612 Vline_prefix = Qnil;
29613 DEFSYM (Qline_prefix, "line-prefix");
29614 Fmake_variable_buffer_local (Qline_prefix);
29616 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29617 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29618 inhibit_eval_during_redisplay = 0;
29620 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29621 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29622 inhibit_free_realized_faces = 0;
29624 #ifdef GLYPH_DEBUG
29625 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29626 doc: /* Inhibit try_window_id display optimization. */);
29627 inhibit_try_window_id = 0;
29629 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29630 doc: /* Inhibit try_window_reusing display optimization. */);
29631 inhibit_try_window_reusing = 0;
29633 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29634 doc: /* Inhibit try_cursor_movement display optimization. */);
29635 inhibit_try_cursor_movement = 0;
29636 #endif /* GLYPH_DEBUG */
29638 DEFVAR_INT ("overline-margin", overline_margin,
29639 doc: /* Space between overline and text, in pixels.
29640 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29641 margin to the character height. */);
29642 overline_margin = 2;
29644 DEFVAR_INT ("underline-minimum-offset",
29645 underline_minimum_offset,
29646 doc: /* Minimum distance between baseline and underline.
29647 This can improve legibility of underlined text at small font sizes,
29648 particularly when using variable `x-use-underline-position-properties'
29649 with fonts that specify an UNDERLINE_POSITION relatively close to the
29650 baseline. The default value is 1. */);
29651 underline_minimum_offset = 1;
29653 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29654 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29655 This feature only works when on a window system that can change
29656 cursor shapes. */);
29657 display_hourglass_p = 1;
29659 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29660 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29661 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29663 hourglass_atimer = NULL;
29664 hourglass_shown_p = 0;
29666 DEFSYM (Qglyphless_char, "glyphless-char");
29667 DEFSYM (Qhex_code, "hex-code");
29668 DEFSYM (Qempty_box, "empty-box");
29669 DEFSYM (Qthin_space, "thin-space");
29670 DEFSYM (Qzero_width, "zero-width");
29672 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29673 /* Intern this now in case it isn't already done.
29674 Setting this variable twice is harmless.
29675 But don't staticpro it here--that is done in alloc.c. */
29676 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29677 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29679 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29680 doc: /* Char-table defining glyphless characters.
29681 Each element, if non-nil, should be one of the following:
29682 an ASCII acronym string: display this string in a box
29683 `hex-code': display the hexadecimal code of a character in a box
29684 `empty-box': display as an empty box
29685 `thin-space': display as 1-pixel width space
29686 `zero-width': don't display
29687 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29688 display method for graphical terminals and text terminals respectively.
29689 GRAPHICAL and TEXT should each have one of the values listed above.
29691 The char-table has one extra slot to control the display of a character for
29692 which no font is found. This slot only takes effect on graphical terminals.
29693 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29694 `thin-space'. The default is `empty-box'. */);
29695 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29696 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29697 Qempty_box);
29699 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29700 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29701 Vdebug_on_message = Qnil;
29705 /* Initialize this module when Emacs starts. */
29707 void
29708 init_xdisp (void)
29710 current_header_line_height = current_mode_line_height = -1;
29712 CHARPOS (this_line_start_pos) = 0;
29714 if (!noninteractive)
29716 struct window *m = XWINDOW (minibuf_window);
29717 Lisp_Object frame = m->frame;
29718 struct frame *f = XFRAME (frame);
29719 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29720 struct window *r = XWINDOW (root);
29721 int i;
29723 echo_area_window = minibuf_window;
29725 r->top_line = FRAME_TOP_MARGIN (f);
29726 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29727 r->total_cols = FRAME_COLS (f);
29729 m->top_line = FRAME_LINES (f) - 1;
29730 m->total_lines = 1;
29731 m->total_cols = FRAME_COLS (f);
29733 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29734 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29735 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29737 /* The default ellipsis glyphs `...'. */
29738 for (i = 0; i < 3; ++i)
29739 default_invis_vector[i] = make_number ('.');
29743 /* Allocate the buffer for frame titles.
29744 Also used for `format-mode-line'. */
29745 int size = 100;
29746 mode_line_noprop_buf = xmalloc (size);
29747 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29748 mode_line_noprop_ptr = mode_line_noprop_buf;
29749 mode_line_target = MODE_LINE_DISPLAY;
29752 help_echo_showing_p = 0;
29755 /* Platform-independent portion of hourglass implementation. */
29757 /* Cancel a currently active hourglass timer, and start a new one. */
29758 void
29759 start_hourglass (void)
29761 #if defined (HAVE_WINDOW_SYSTEM)
29762 EMACS_TIME delay;
29764 cancel_hourglass ();
29766 if (INTEGERP (Vhourglass_delay)
29767 && XINT (Vhourglass_delay) > 0)
29768 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29769 TYPE_MAXIMUM (time_t)),
29771 else if (FLOATP (Vhourglass_delay)
29772 && XFLOAT_DATA (Vhourglass_delay) > 0)
29773 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29774 else
29775 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29777 #ifdef HAVE_NTGUI
29779 extern void w32_note_current_window (void);
29780 w32_note_current_window ();
29782 #endif /* HAVE_NTGUI */
29784 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29785 show_hourglass, NULL);
29786 #endif
29790 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29791 shown. */
29792 void
29793 cancel_hourglass (void)
29795 #if defined (HAVE_WINDOW_SYSTEM)
29796 if (hourglass_atimer)
29798 cancel_atimer (hourglass_atimer);
29799 hourglass_atimer = NULL;
29802 if (hourglass_shown_p)
29803 hide_hourglass ();
29804 #endif