* charset.c: Fix file descriptor leaks and errno issues.
[emacs.git] / src / xdisp.c
blob1a7369d7a45ef68e3edec16363b9b6ee35856779
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
103 . try_window
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
116 Desired matrices.
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
161 Frame matrices.
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
182 Bidirectional display.
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
225 Bidirectional display and character compositions
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
252 Moving an iterator in bidirectional text
253 without producing glyphs
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
303 #ifdef HAVE_X_WINDOWS
304 #include "xterm.h"
305 #endif
306 #ifdef HAVE_NTGUI
307 #include "w32term.h"
308 #endif
309 #ifdef HAVE_NS
310 #include "nsterm.h"
311 #endif
312 #ifdef USE_GTK
313 #include "gtkutil.h"
314 #endif
316 #ifndef FRAME_X_OUTPUT
317 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
318 #endif
320 #define INFINITY 10000000
322 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
323 Lisp_Object Qwindow_scroll_functions;
324 static Lisp_Object Qwindow_text_change_functions;
325 static Lisp_Object Qredisplay_end_trigger_functions;
326 Lisp_Object Qinhibit_point_motion_hooks;
327 static Lisp_Object QCeval, QCpropertize;
328 Lisp_Object QCfile, QCdata;
329 static Lisp_Object Qfontified;
330 static Lisp_Object Qgrow_only;
331 static Lisp_Object Qinhibit_eval_during_redisplay;
332 static Lisp_Object Qbuffer_position, Qposition, Qobject;
333 static Lisp_Object Qright_to_left, Qleft_to_right;
335 /* Cursor shapes. */
336 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338 /* Pointer shapes. */
339 static Lisp_Object Qarrow, Qhand;
340 Lisp_Object Qtext;
342 /* Holds the list (error). */
343 static Lisp_Object list_of_error;
345 static Lisp_Object Qfontification_functions;
347 static Lisp_Object Qwrap_prefix;
348 static Lisp_Object Qline_prefix;
349 static Lisp_Object Qredisplay_internal;
351 /* Non-nil means don't actually do any redisplay. */
353 Lisp_Object Qinhibit_redisplay;
355 /* Names of text properties relevant for redisplay. */
357 Lisp_Object Qdisplay;
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
368 #ifdef HAVE_WINDOW_SYSTEM
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x)
381 #else /* !HAVE_WINDOW_SYSTEM */
382 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
383 #endif /* HAVE_WINDOW_SYSTEM */
385 /* Test if the display element loaded in IT, or the underlying buffer
386 or string character, is a space or a TAB character. This is used
387 to determine where word wrapping can occur. */
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
391 || ((STRINGP (it->string) \
392 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
393 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
394 || (it->s \
395 && (it->s[IT_BYTEPOS (*it)] == ' ' \
396 || it->s[IT_BYTEPOS (*it)] == '\t')) \
397 || (IT_BYTEPOS (*it) < ZV_BYTE \
398 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
399 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
401 /* Name of the face used to highlight trailing whitespace. */
403 static Lisp_Object Qtrailing_whitespace;
405 /* Name and number of the face used to highlight escape glyphs. */
407 static Lisp_Object Qescape_glyph;
409 /* Name and number of the face used to highlight non-breaking spaces. */
411 static Lisp_Object Qnobreak_space;
413 /* The symbol `image' which is the car of the lists used to represent
414 images in Lisp. Also a tool bar style. */
416 Lisp_Object Qimage;
418 /* The image map types. */
419 Lisp_Object QCmap;
420 static Lisp_Object QCpointer;
421 static Lisp_Object Qrect, Qcircle, Qpoly;
423 /* Tool bar styles */
424 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
426 /* Non-zero means print newline to stdout before next mini-buffer
427 message. */
429 int noninteractive_need_newline;
431 /* Non-zero means print newline to message log before next message. */
433 static int message_log_need_newline;
435 /* Three markers that message_dolog uses.
436 It could allocate them itself, but that causes trouble
437 in handling memory-full errors. */
438 static Lisp_Object message_dolog_marker1;
439 static Lisp_Object message_dolog_marker2;
440 static Lisp_Object message_dolog_marker3;
442 /* The buffer position of the first character appearing entirely or
443 partially on the line of the selected window which contains the
444 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
445 redisplay optimization in redisplay_internal. */
447 static struct text_pos this_line_start_pos;
449 /* Number of characters past the end of the line above, including the
450 terminating newline. */
452 static struct text_pos this_line_end_pos;
454 /* The vertical positions and the height of this line. */
456 static int this_line_vpos;
457 static int this_line_y;
458 static int this_line_pixel_height;
460 /* X position at which this display line starts. Usually zero;
461 negative if first character is partially visible. */
463 static int this_line_start_x;
465 /* The smallest character position seen by move_it_* functions as they
466 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
467 hscrolled lines, see display_line. */
469 static struct text_pos this_line_min_pos;
471 /* Buffer that this_line_.* variables are referring to. */
473 static struct buffer *this_line_buffer;
476 /* Values of those variables at last redisplay are stored as
477 properties on `overlay-arrow-position' symbol. However, if
478 Voverlay_arrow_position is a marker, last-arrow-position is its
479 numerical position. */
481 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
483 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
484 properties on a symbol in overlay-arrow-variable-list. */
486 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
488 Lisp_Object Qmenu_bar_update_hook;
490 /* Nonzero if an overlay arrow has been displayed in this window. */
492 static int overlay_arrow_seen;
494 /* Vector containing glyphs for an ellipsis `...'. */
496 static Lisp_Object default_invis_vector[3];
498 /* This is the window where the echo area message was displayed. It
499 is always a mini-buffer window, but it may not be the same window
500 currently active as a mini-buffer. */
502 Lisp_Object echo_area_window;
504 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
505 pushes the current message and the value of
506 message_enable_multibyte on the stack, the function restore_message
507 pops the stack and displays MESSAGE again. */
509 static Lisp_Object Vmessage_stack;
511 /* Nonzero means multibyte characters were enabled when the echo area
512 message was specified. */
514 static int message_enable_multibyte;
516 /* Nonzero if we should redraw the mode lines on the next redisplay. */
518 int update_mode_lines;
520 /* Nonzero if window sizes or contents have changed since last
521 redisplay that finished. */
523 int windows_or_buffers_changed;
525 /* Nonzero means a frame's cursor type has been changed. */
527 int cursor_type_changed;
529 /* Nonzero after display_mode_line if %l was used and it displayed a
530 line number. */
532 static int line_number_displayed;
534 /* The name of the *Messages* buffer, a string. */
536 static Lisp_Object Vmessages_buffer_name;
538 /* Current, index 0, and last displayed echo area message. Either
539 buffers from echo_buffers, or nil to indicate no message. */
541 Lisp_Object echo_area_buffer[2];
543 /* The buffers referenced from echo_area_buffer. */
545 static Lisp_Object echo_buffer[2];
547 /* A vector saved used in with_area_buffer to reduce consing. */
549 static Lisp_Object Vwith_echo_area_save_vector;
551 /* Non-zero means display_echo_area should display the last echo area
552 message again. Set by redisplay_preserve_echo_area. */
554 static int display_last_displayed_message_p;
556 /* Nonzero if echo area is being used by print; zero if being used by
557 message. */
559 static int message_buf_print;
561 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
563 static Lisp_Object Qinhibit_menubar_update;
564 static Lisp_Object Qmessage_truncate_lines;
566 /* Set to 1 in clear_message to make redisplay_internal aware
567 of an emptied echo area. */
569 static int message_cleared_p;
571 /* A scratch glyph row with contents used for generating truncation
572 glyphs. Also used in direct_output_for_insert. */
574 #define MAX_SCRATCH_GLYPHS 100
575 static struct glyph_row scratch_glyph_row;
576 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
578 /* Ascent and height of the last line processed by move_it_to. */
580 static int last_height;
582 /* Non-zero if there's a help-echo in the echo area. */
584 int help_echo_showing_p;
586 /* If >= 0, computed, exact values of mode-line and header-line height
587 to use in the macros CURRENT_MODE_LINE_HEIGHT and
588 CURRENT_HEADER_LINE_HEIGHT. */
590 int current_mode_line_height, current_header_line_height;
592 /* The maximum distance to look ahead for text properties. Values
593 that are too small let us call compute_char_face and similar
594 functions too often which is expensive. Values that are too large
595 let us call compute_char_face and alike too often because we
596 might not be interested in text properties that far away. */
598 #define TEXT_PROP_DISTANCE_LIMIT 100
600 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
601 iterator state and later restore it. This is needed because the
602 bidi iterator on bidi.c keeps a stacked cache of its states, which
603 is really a singleton. When we use scratch iterator objects to
604 move around the buffer, we can cause the bidi cache to be pushed or
605 popped, and therefore we need to restore the cache state when we
606 return to the original iterator. */
607 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
608 do { \
609 if (CACHE) \
610 bidi_unshelve_cache (CACHE, 1); \
611 ITCOPY = ITORIG; \
612 CACHE = bidi_shelve_cache (); \
613 } while (0)
615 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
616 do { \
617 if (pITORIG != pITCOPY) \
618 *(pITORIG) = *(pITCOPY); \
619 bidi_unshelve_cache (CACHE, 0); \
620 CACHE = NULL; \
621 } while (0)
623 #ifdef GLYPH_DEBUG
625 /* Non-zero means print traces of redisplay if compiled with
626 GLYPH_DEBUG defined. */
628 int trace_redisplay_p;
630 #endif /* GLYPH_DEBUG */
632 #ifdef DEBUG_TRACE_MOVE
633 /* Non-zero means trace with TRACE_MOVE to stderr. */
634 int trace_move;
636 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
637 #else
638 #define TRACE_MOVE(x) (void) 0
639 #endif
641 static Lisp_Object Qauto_hscroll_mode;
643 /* Buffer being redisplayed -- for redisplay_window_error. */
645 static struct buffer *displayed_buffer;
647 /* Value returned from text property handlers (see below). */
649 enum prop_handled
651 HANDLED_NORMALLY,
652 HANDLED_RECOMPUTE_PROPS,
653 HANDLED_OVERLAY_STRING_CONSUMED,
654 HANDLED_RETURN
657 /* A description of text properties that redisplay is interested
658 in. */
660 struct props
662 /* The name of the property. */
663 Lisp_Object *name;
665 /* A unique index for the property. */
666 enum prop_idx idx;
668 /* A handler function called to set up iterator IT from the property
669 at IT's current position. Value is used to steer handle_stop. */
670 enum prop_handled (*handler) (struct it *it);
673 static enum prop_handled handle_face_prop (struct it *);
674 static enum prop_handled handle_invisible_prop (struct it *);
675 static enum prop_handled handle_display_prop (struct it *);
676 static enum prop_handled handle_composition_prop (struct it *);
677 static enum prop_handled handle_overlay_change (struct it *);
678 static enum prop_handled handle_fontified_prop (struct it *);
680 /* Properties handled by iterators. */
682 static struct props it_props[] =
684 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
685 /* Handle `face' before `display' because some sub-properties of
686 `display' need to know the face. */
687 {&Qface, FACE_PROP_IDX, handle_face_prop},
688 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
689 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
690 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
691 {NULL, 0, NULL}
694 /* Value is the position described by X. If X is a marker, value is
695 the marker_position of X. Otherwise, value is X. */
697 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
699 /* Enumeration returned by some move_it_.* functions internally. */
701 enum move_it_result
703 /* Not used. Undefined value. */
704 MOVE_UNDEFINED,
706 /* Move ended at the requested buffer position or ZV. */
707 MOVE_POS_MATCH_OR_ZV,
709 /* Move ended at the requested X pixel position. */
710 MOVE_X_REACHED,
712 /* Move within a line ended at the end of a line that must be
713 continued. */
714 MOVE_LINE_CONTINUED,
716 /* Move within a line ended at the end of a line that would
717 be displayed truncated. */
718 MOVE_LINE_TRUNCATED,
720 /* Move within a line ended at a line end. */
721 MOVE_NEWLINE_OR_CR
724 /* This counter is used to clear the face cache every once in a while
725 in redisplay_internal. It is incremented for each redisplay.
726 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
727 cleared. */
729 #define CLEAR_FACE_CACHE_COUNT 500
730 static int clear_face_cache_count;
732 /* Similarly for the image cache. */
734 #ifdef HAVE_WINDOW_SYSTEM
735 #define CLEAR_IMAGE_CACHE_COUNT 101
736 static int clear_image_cache_count;
738 /* Null glyph slice */
739 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
740 #endif
742 /* True while redisplay_internal is in progress. */
744 bool redisplaying_p;
746 static Lisp_Object Qinhibit_free_realized_faces;
747 static Lisp_Object Qmode_line_default_help_echo;
749 /* If a string, XTread_socket generates an event to display that string.
750 (The display is done in read_char.) */
752 Lisp_Object help_echo_string;
753 Lisp_Object help_echo_window;
754 Lisp_Object help_echo_object;
755 ptrdiff_t help_echo_pos;
757 /* Temporary variable for XTread_socket. */
759 Lisp_Object previous_help_echo_string;
761 /* Platform-independent portion of hourglass implementation. */
763 /* Non-zero means an hourglass cursor is currently shown. */
764 int hourglass_shown_p;
766 /* If non-null, an asynchronous timer that, when it expires, displays
767 an hourglass cursor on all frames. */
768 struct atimer *hourglass_atimer;
770 /* Name of the face used to display glyphless characters. */
771 Lisp_Object Qglyphless_char;
773 /* Symbol for the purpose of Vglyphless_char_display. */
774 static Lisp_Object Qglyphless_char_display;
776 /* Method symbols for Vglyphless_char_display. */
777 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
779 /* Default pixel width of `thin-space' display method. */
780 #define THIN_SPACE_WIDTH 1
782 /* Default number of seconds to wait before displaying an hourglass
783 cursor. */
784 #define DEFAULT_HOURGLASS_DELAY 1
787 /* Function prototypes. */
789 static void setup_for_ellipsis (struct it *, int);
790 static void set_iterator_to_next (struct it *, int);
791 static void mark_window_display_accurate_1 (struct window *, int);
792 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
793 static int display_prop_string_p (Lisp_Object, Lisp_Object);
794 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
795 static int cursor_row_p (struct glyph_row *);
796 static int redisplay_mode_lines (Lisp_Object, int);
797 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
799 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
801 static void handle_line_prefix (struct it *);
803 static void pint2str (char *, int, ptrdiff_t);
804 static void pint2hrstr (char *, int, ptrdiff_t);
805 static struct text_pos run_window_scroll_functions (Lisp_Object,
806 struct text_pos);
807 static void reconsider_clip_changes (struct window *, struct buffer *);
808 static int text_outside_line_unchanged_p (struct window *,
809 ptrdiff_t, ptrdiff_t);
810 static void store_mode_line_noprop_char (char);
811 static int store_mode_line_noprop (const char *, int, int);
812 static void handle_stop (struct it *);
813 static void handle_stop_backwards (struct it *, ptrdiff_t);
814 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
815 static void ensure_echo_area_buffers (void);
816 static void unwind_with_echo_area_buffer (Lisp_Object);
817 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
818 static int with_echo_area_buffer (struct window *, int,
819 int (*) (ptrdiff_t, Lisp_Object),
820 ptrdiff_t, Lisp_Object);
821 static void clear_garbaged_frames (void);
822 static int current_message_1 (ptrdiff_t, Lisp_Object);
823 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
824 static void set_message (Lisp_Object);
825 static int set_message_1 (ptrdiff_t, Lisp_Object);
826 static int display_echo_area (struct window *);
827 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
828 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
829 static void unwind_redisplay (void);
830 static int string_char_and_length (const unsigned char *, int *);
831 static struct text_pos display_prop_end (struct it *, Lisp_Object,
832 struct text_pos);
833 static int compute_window_start_on_continuation_line (struct window *);
834 static void insert_left_trunc_glyphs (struct it *);
835 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
836 Lisp_Object);
837 static void extend_face_to_end_of_line (struct it *);
838 static int append_space_for_newline (struct it *, int);
839 static int cursor_row_fully_visible_p (struct window *, int, int);
840 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
841 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
842 static int trailing_whitespace_p (ptrdiff_t);
843 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
844 static void push_it (struct it *, struct text_pos *);
845 static void iterate_out_of_display_property (struct it *);
846 static void pop_it (struct it *);
847 static void sync_frame_with_window_matrix_rows (struct window *);
848 static void redisplay_internal (void);
849 static int echo_area_display (int);
850 static void redisplay_windows (Lisp_Object);
851 static void redisplay_window (Lisp_Object, int);
852 static Lisp_Object redisplay_window_error (Lisp_Object);
853 static Lisp_Object redisplay_window_0 (Lisp_Object);
854 static Lisp_Object redisplay_window_1 (Lisp_Object);
855 static int set_cursor_from_row (struct window *, struct glyph_row *,
856 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
857 int, int);
858 static int update_menu_bar (struct frame *, int, int);
859 static int try_window_reusing_current_matrix (struct window *);
860 static int try_window_id (struct window *);
861 static int display_line (struct it *);
862 static int display_mode_lines (struct window *);
863 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
864 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
865 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
866 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
867 static void display_menu_bar (struct window *);
868 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
869 ptrdiff_t *);
870 static int display_string (const char *, Lisp_Object, Lisp_Object,
871 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
872 static void compute_line_metrics (struct it *);
873 static void run_redisplay_end_trigger_hook (struct it *);
874 static int get_overlay_strings (struct it *, ptrdiff_t);
875 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
876 static void next_overlay_string (struct it *);
877 static void reseat (struct it *, struct text_pos, int);
878 static void reseat_1 (struct it *, struct text_pos, int);
879 static void back_to_previous_visible_line_start (struct it *);
880 static void reseat_at_next_visible_line_start (struct it *, int);
881 static int next_element_from_ellipsis (struct it *);
882 static int next_element_from_display_vector (struct it *);
883 static int next_element_from_string (struct it *);
884 static int next_element_from_c_string (struct it *);
885 static int next_element_from_buffer (struct it *);
886 static int next_element_from_composition (struct it *);
887 static int next_element_from_image (struct it *);
888 static int next_element_from_stretch (struct it *);
889 static void load_overlay_strings (struct it *, ptrdiff_t);
890 static int init_from_display_pos (struct it *, struct window *,
891 struct display_pos *);
892 static void reseat_to_string (struct it *, const char *,
893 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
894 static int get_next_display_element (struct it *);
895 static enum move_it_result
896 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
897 enum move_operation_enum);
898 static void get_visually_first_element (struct it *);
899 static void init_to_row_start (struct it *, struct window *,
900 struct glyph_row *);
901 static int init_to_row_end (struct it *, struct window *,
902 struct glyph_row *);
903 static void back_to_previous_line_start (struct it *);
904 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
905 static struct text_pos string_pos_nchars_ahead (struct text_pos,
906 Lisp_Object, ptrdiff_t);
907 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
908 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
909 static ptrdiff_t number_of_chars (const char *, bool);
910 static void compute_stop_pos (struct it *);
911 static void compute_string_pos (struct text_pos *, struct text_pos,
912 Lisp_Object);
913 static int face_before_or_after_it_pos (struct it *, int);
914 static ptrdiff_t next_overlay_change (ptrdiff_t);
915 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
916 Lisp_Object, struct text_pos *, ptrdiff_t, int);
917 static int handle_single_display_spec (struct it *, Lisp_Object,
918 Lisp_Object, Lisp_Object,
919 struct text_pos *, ptrdiff_t, int, int);
920 static int underlying_face_id (struct it *);
921 static int in_ellipses_for_invisible_text_p (struct display_pos *,
922 struct window *);
924 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
925 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
927 #ifdef HAVE_WINDOW_SYSTEM
929 static void x_consider_frame_title (Lisp_Object);
930 static int tool_bar_lines_needed (struct frame *, int *);
931 static void update_tool_bar (struct frame *, int);
932 static void build_desired_tool_bar_string (struct frame *f);
933 static int redisplay_tool_bar (struct frame *);
934 static void display_tool_bar_line (struct it *, int);
935 static void notice_overwritten_cursor (struct window *,
936 enum glyph_row_area,
937 int, int, int, int);
938 static void append_stretch_glyph (struct it *, Lisp_Object,
939 int, int, int);
942 #endif /* HAVE_WINDOW_SYSTEM */
944 static void produce_special_glyphs (struct it *, enum display_element_type);
945 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
946 static int coords_in_mouse_face_p (struct window *, int, int);
950 /***********************************************************************
951 Window display dimensions
952 ***********************************************************************/
954 /* Return the bottom boundary y-position for text lines in window W.
955 This is the first y position at which a line cannot start.
956 It is relative to the top of the window.
958 This is the height of W minus the height of a mode line, if any. */
961 window_text_bottom_y (struct window *w)
963 int height = WINDOW_TOTAL_HEIGHT (w);
965 if (WINDOW_WANTS_MODELINE_P (w))
966 height -= CURRENT_MODE_LINE_HEIGHT (w);
967 return height;
970 /* Return the pixel width of display area AREA of window W. AREA < 0
971 means return the total width of W, not including fringes to
972 the left and right of the window. */
975 window_box_width (struct window *w, int area)
977 int cols = w->total_cols;
978 int pixels = 0;
980 if (!w->pseudo_window_p)
982 cols -= WINDOW_SCROLL_BAR_COLS (w);
984 if (area == TEXT_AREA)
986 if (INTEGERP (w->left_margin_cols))
987 cols -= XFASTINT (w->left_margin_cols);
988 if (INTEGERP (w->right_margin_cols))
989 cols -= XFASTINT (w->right_margin_cols);
990 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
992 else if (area == LEFT_MARGIN_AREA)
994 cols = (INTEGERP (w->left_margin_cols)
995 ? XFASTINT (w->left_margin_cols) : 0);
996 pixels = 0;
998 else if (area == RIGHT_MARGIN_AREA)
1000 cols = (INTEGERP (w->right_margin_cols)
1001 ? XFASTINT (w->right_margin_cols) : 0);
1002 pixels = 0;
1006 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1010 /* Return the pixel height of the display area of window W, not
1011 including mode lines of W, if any. */
1014 window_box_height (struct window *w)
1016 struct frame *f = XFRAME (w->frame);
1017 int height = WINDOW_TOTAL_HEIGHT (w);
1019 eassert (height >= 0);
1021 /* Note: the code below that determines the mode-line/header-line
1022 height is essentially the same as that contained in the macro
1023 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1024 the appropriate glyph row has its `mode_line_p' flag set,
1025 and if it doesn't, uses estimate_mode_line_height instead. */
1027 if (WINDOW_WANTS_MODELINE_P (w))
1029 struct glyph_row *ml_row
1030 = (w->current_matrix && w->current_matrix->rows
1031 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1032 : 0);
1033 if (ml_row && ml_row->mode_line_p)
1034 height -= ml_row->height;
1035 else
1036 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1039 if (WINDOW_WANTS_HEADER_LINE_P (w))
1041 struct glyph_row *hl_row
1042 = (w->current_matrix && w->current_matrix->rows
1043 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1044 : 0);
1045 if (hl_row && hl_row->mode_line_p)
1046 height -= hl_row->height;
1047 else
1048 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1051 /* With a very small font and a mode-line that's taller than
1052 default, we might end up with a negative height. */
1053 return max (0, height);
1056 /* Return the window-relative coordinate of the left edge of display
1057 area AREA of window W. AREA < 0 means return the left edge of the
1058 whole window, to the right of the left fringe of W. */
1061 window_box_left_offset (struct window *w, int area)
1063 int x;
1065 if (w->pseudo_window_p)
1066 return 0;
1068 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1070 if (area == TEXT_AREA)
1071 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1072 + window_box_width (w, LEFT_MARGIN_AREA));
1073 else if (area == RIGHT_MARGIN_AREA)
1074 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1075 + window_box_width (w, LEFT_MARGIN_AREA)
1076 + window_box_width (w, TEXT_AREA)
1077 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1079 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1080 else if (area == LEFT_MARGIN_AREA
1081 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1082 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1084 return x;
1088 /* Return the window-relative coordinate of the right edge of display
1089 area AREA of window W. AREA < 0 means return the right edge of the
1090 whole window, to the left of the right fringe of W. */
1093 window_box_right_offset (struct window *w, int area)
1095 return window_box_left_offset (w, area) + window_box_width (w, area);
1098 /* Return the frame-relative coordinate of the left edge of display
1099 area AREA of window W. AREA < 0 means return the left edge of the
1100 whole window, to the right of the left fringe of W. */
1103 window_box_left (struct window *w, int area)
1105 struct frame *f = XFRAME (w->frame);
1106 int x;
1108 if (w->pseudo_window_p)
1109 return FRAME_INTERNAL_BORDER_WIDTH (f);
1111 x = (WINDOW_LEFT_EDGE_X (w)
1112 + window_box_left_offset (w, area));
1114 return x;
1118 /* Return the frame-relative coordinate of the right edge of display
1119 area AREA of window W. AREA < 0 means return the right edge of the
1120 whole window, to the left of the right fringe of W. */
1123 window_box_right (struct window *w, int area)
1125 return window_box_left (w, area) + window_box_width (w, area);
1128 /* Get the bounding box of the display area AREA of window W, without
1129 mode lines, in frame-relative coordinates. AREA < 0 means the
1130 whole window, not including the left and right fringes of
1131 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1132 coordinates of the upper-left corner of the box. Return in
1133 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1135 void
1136 window_box (struct window *w, int area, int *box_x, int *box_y,
1137 int *box_width, int *box_height)
1139 if (box_width)
1140 *box_width = window_box_width (w, area);
1141 if (box_height)
1142 *box_height = window_box_height (w);
1143 if (box_x)
1144 *box_x = window_box_left (w, area);
1145 if (box_y)
1147 *box_y = WINDOW_TOP_EDGE_Y (w);
1148 if (WINDOW_WANTS_HEADER_LINE_P (w))
1149 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1154 /* Get the bounding box of the display area AREA of window W, without
1155 mode lines. AREA < 0 means the whole window, not including the
1156 left and right fringe of the window. Return in *TOP_LEFT_X
1157 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1158 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1159 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1160 box. */
1162 static void
1163 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1164 int *bottom_right_x, int *bottom_right_y)
1166 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1167 bottom_right_y);
1168 *bottom_right_x += *top_left_x;
1169 *bottom_right_y += *top_left_y;
1174 /***********************************************************************
1175 Utilities
1176 ***********************************************************************/
1178 /* Return the bottom y-position of the line the iterator IT is in.
1179 This can modify IT's settings. */
1182 line_bottom_y (struct it *it)
1184 int line_height = it->max_ascent + it->max_descent;
1185 int line_top_y = it->current_y;
1187 if (line_height == 0)
1189 if (last_height)
1190 line_height = last_height;
1191 else if (IT_CHARPOS (*it) < ZV)
1193 move_it_by_lines (it, 1);
1194 line_height = (it->max_ascent || it->max_descent
1195 ? it->max_ascent + it->max_descent
1196 : last_height);
1198 else
1200 struct glyph_row *row = it->glyph_row;
1202 /* Use the default character height. */
1203 it->glyph_row = NULL;
1204 it->what = IT_CHARACTER;
1205 it->c = ' ';
1206 it->len = 1;
1207 PRODUCE_GLYPHS (it);
1208 line_height = it->ascent + it->descent;
1209 it->glyph_row = row;
1213 return line_top_y + line_height;
1216 DEFUN ("line-pixel-height", Fline_pixel_height,
1217 Sline_pixel_height, 0, 0, 0,
1218 doc: /* Return height in pixels of text line in the selected window.
1220 Value is the height in pixels of the line at point. */)
1221 (void)
1223 struct it it;
1224 struct text_pos pt;
1225 struct window *w = XWINDOW (selected_window);
1227 SET_TEXT_POS (pt, PT, PT_BYTE);
1228 start_display (&it, w, pt);
1229 it.vpos = it.current_y = 0;
1230 last_height = 0;
1231 return make_number (line_bottom_y (&it));
1234 /* Return the default pixel height of text lines in window W. The
1235 value is the canonical height of the W frame's default font, plus
1236 any extra space required by the line-spacing variable or frame
1237 parameter.
1239 Implementation note: this ignores any line-spacing text properties
1240 put on the newline characters. This is because those properties
1241 only affect the _screen_ line ending in the newline (i.e., in a
1242 continued line, only the last screen line will be affected), which
1243 means only a small number of lines in a buffer can ever use this
1244 feature. Since this function is used to compute the default pixel
1245 equivalent of text lines in a window, we can safely ignore those
1246 few lines. For the same reasons, we ignore the line-height
1247 properties. */
1249 default_line_pixel_height (struct window *w)
1251 struct frame *f = WINDOW_XFRAME (w);
1252 int height = FRAME_LINE_HEIGHT (f);
1254 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1256 struct buffer *b = XBUFFER (w->contents);
1257 Lisp_Object val = BVAR (b, extra_line_spacing);
1259 if (NILP (val))
1260 val = BVAR (&buffer_defaults, extra_line_spacing);
1261 if (!NILP (val))
1263 if (RANGED_INTEGERP (0, val, INT_MAX))
1264 height += XFASTINT (val);
1265 else if (FLOATP (val))
1267 int addon = XFLOAT_DATA (val) * height + 0.5;
1269 if (addon >= 0)
1270 height += addon;
1273 else
1274 height += f->extra_line_spacing;
1277 return height;
1280 /* Subroutine of pos_visible_p below. Extracts a display string, if
1281 any, from the display spec given as its argument. */
1282 static Lisp_Object
1283 string_from_display_spec (Lisp_Object spec)
1285 if (CONSP (spec))
1287 while (CONSP (spec))
1289 if (STRINGP (XCAR (spec)))
1290 return XCAR (spec);
1291 spec = XCDR (spec);
1294 else if (VECTORP (spec))
1296 ptrdiff_t i;
1298 for (i = 0; i < ASIZE (spec); i++)
1300 if (STRINGP (AREF (spec, i)))
1301 return AREF (spec, i);
1303 return Qnil;
1306 return spec;
1310 /* Limit insanely large values of W->hscroll on frame F to the largest
1311 value that will still prevent first_visible_x and last_visible_x of
1312 'struct it' from overflowing an int. */
1313 static int
1314 window_hscroll_limited (struct window *w, struct frame *f)
1316 ptrdiff_t window_hscroll = w->hscroll;
1317 int window_text_width = window_box_width (w, TEXT_AREA);
1318 int colwidth = FRAME_COLUMN_WIDTH (f);
1320 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1321 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1323 return window_hscroll;
1326 /* Return 1 if position CHARPOS is visible in window W.
1327 CHARPOS < 0 means return info about WINDOW_END position.
1328 If visible, set *X and *Y to pixel coordinates of top left corner.
1329 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1330 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1333 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1334 int *rtop, int *rbot, int *rowh, int *vpos)
1336 struct it it;
1337 void *itdata = bidi_shelve_cache ();
1338 struct text_pos top;
1339 int visible_p = 0;
1340 struct buffer *old_buffer = NULL;
1342 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1343 return visible_p;
1345 if (XBUFFER (w->contents) != current_buffer)
1347 old_buffer = current_buffer;
1348 set_buffer_internal_1 (XBUFFER (w->contents));
1351 SET_TEXT_POS_FROM_MARKER (top, w->start);
1352 /* Scrolling a minibuffer window via scroll bar when the echo area
1353 shows long text sometimes resets the minibuffer contents behind
1354 our backs. */
1355 if (CHARPOS (top) > ZV)
1356 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1358 /* Compute exact mode line heights. */
1359 if (WINDOW_WANTS_MODELINE_P (w))
1360 current_mode_line_height
1361 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1362 BVAR (current_buffer, mode_line_format));
1364 if (WINDOW_WANTS_HEADER_LINE_P (w))
1365 current_header_line_height
1366 = display_mode_line (w, HEADER_LINE_FACE_ID,
1367 BVAR (current_buffer, header_line_format));
1369 start_display (&it, w, top);
1370 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1371 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1373 if (charpos >= 0
1374 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1375 && IT_CHARPOS (it) >= charpos)
1376 /* When scanning backwards under bidi iteration, move_it_to
1377 stops at or _before_ CHARPOS, because it stops at or to
1378 the _right_ of the character at CHARPOS. */
1379 || (it.bidi_p && it.bidi_it.scan_dir == -1
1380 && IT_CHARPOS (it) <= charpos)))
1382 /* We have reached CHARPOS, or passed it. How the call to
1383 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1384 or covered by a display property, move_it_to stops at the end
1385 of the invisible text, to the right of CHARPOS. (ii) If
1386 CHARPOS is in a display vector, move_it_to stops on its last
1387 glyph. */
1388 int top_x = it.current_x;
1389 int top_y = it.current_y;
1390 /* Calling line_bottom_y may change it.method, it.position, etc. */
1391 enum it_method it_method = it.method;
1392 int bottom_y = (last_height = 0, line_bottom_y (&it));
1393 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1395 if (top_y < window_top_y)
1396 visible_p = bottom_y > window_top_y;
1397 else if (top_y < it.last_visible_y)
1398 visible_p = 1;
1399 if (bottom_y >= it.last_visible_y
1400 && it.bidi_p && it.bidi_it.scan_dir == -1
1401 && IT_CHARPOS (it) < charpos)
1403 /* When the last line of the window is scanned backwards
1404 under bidi iteration, we could be duped into thinking
1405 that we have passed CHARPOS, when in fact move_it_to
1406 simply stopped short of CHARPOS because it reached
1407 last_visible_y. To see if that's what happened, we call
1408 move_it_to again with a slightly larger vertical limit,
1409 and see if it actually moved vertically; if it did, we
1410 didn't really reach CHARPOS, which is beyond window end. */
1411 struct it save_it = it;
1412 /* Why 10? because we don't know how many canonical lines
1413 will the height of the next line(s) be. So we guess. */
1414 int ten_more_lines = 10 * default_line_pixel_height (w);
1416 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1417 MOVE_TO_POS | MOVE_TO_Y);
1418 if (it.current_y > top_y)
1419 visible_p = 0;
1421 it = save_it;
1423 if (visible_p)
1425 if (it_method == GET_FROM_DISPLAY_VECTOR)
1427 /* We stopped on the last glyph of a display vector.
1428 Try and recompute. Hack alert! */
1429 if (charpos < 2 || top.charpos >= charpos)
1430 top_x = it.glyph_row->x;
1431 else
1433 struct it it2, it2_prev;
1434 /* The idea is to get to the previous buffer
1435 position, consume the character there, and use
1436 the pixel coordinates we get after that. But if
1437 the previous buffer position is also displayed
1438 from a display vector, we need to consume all of
1439 the glyphs from that display vector. */
1440 start_display (&it2, w, top);
1441 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1442 /* If we didn't get to CHARPOS - 1, there's some
1443 replacing display property at that position, and
1444 we stopped after it. That is exactly the place
1445 whose coordinates we want. */
1446 if (IT_CHARPOS (it2) != charpos - 1)
1447 it2_prev = it2;
1448 else
1450 /* Iterate until we get out of the display
1451 vector that displays the character at
1452 CHARPOS - 1. */
1453 do {
1454 get_next_display_element (&it2);
1455 PRODUCE_GLYPHS (&it2);
1456 it2_prev = it2;
1457 set_iterator_to_next (&it2, 1);
1458 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1459 && IT_CHARPOS (it2) < charpos);
1461 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1462 || it2_prev.current_x > it2_prev.last_visible_x)
1463 top_x = it.glyph_row->x;
1464 else
1466 top_x = it2_prev.current_x;
1467 top_y = it2_prev.current_y;
1471 else if (IT_CHARPOS (it) != charpos)
1473 Lisp_Object cpos = make_number (charpos);
1474 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1475 Lisp_Object string = string_from_display_spec (spec);
1476 struct text_pos tpos;
1477 int replacing_spec_p;
1478 bool newline_in_string
1479 = (STRINGP (string)
1480 && memchr (SDATA (string), '\n', SBYTES (string)));
1482 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1483 replacing_spec_p
1484 = (!NILP (spec)
1485 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1486 charpos, FRAME_WINDOW_P (it.f)));
1487 /* The tricky code below is needed because there's a
1488 discrepancy between move_it_to and how we set cursor
1489 when PT is at the beginning of a portion of text
1490 covered by a display property or an overlay with a
1491 display property, or the display line ends in a
1492 newline from a display string. move_it_to will stop
1493 _after_ such display strings, whereas
1494 set_cursor_from_row conspires with cursor_row_p to
1495 place the cursor on the first glyph produced from the
1496 display string. */
1498 /* We have overshoot PT because it is covered by a
1499 display property that replaces the text it covers.
1500 If the string includes embedded newlines, we are also
1501 in the wrong display line. Backtrack to the correct
1502 line, where the display property begins. */
1503 if (replacing_spec_p)
1505 Lisp_Object startpos, endpos;
1506 EMACS_INT start, end;
1507 struct it it3;
1508 int it3_moved;
1510 /* Find the first and the last buffer positions
1511 covered by the display string. */
1512 endpos =
1513 Fnext_single_char_property_change (cpos, Qdisplay,
1514 Qnil, Qnil);
1515 startpos =
1516 Fprevious_single_char_property_change (endpos, Qdisplay,
1517 Qnil, Qnil);
1518 start = XFASTINT (startpos);
1519 end = XFASTINT (endpos);
1520 /* Move to the last buffer position before the
1521 display property. */
1522 start_display (&it3, w, top);
1523 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1524 /* Move forward one more line if the position before
1525 the display string is a newline or if it is the
1526 rightmost character on a line that is
1527 continued or word-wrapped. */
1528 if (it3.method == GET_FROM_BUFFER
1529 && (it3.c == '\n'
1530 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1531 move_it_by_lines (&it3, 1);
1532 else if (move_it_in_display_line_to (&it3, -1,
1533 it3.current_x
1534 + it3.pixel_width,
1535 MOVE_TO_X)
1536 == MOVE_LINE_CONTINUED)
1538 move_it_by_lines (&it3, 1);
1539 /* When we are under word-wrap, the #$@%!
1540 move_it_by_lines moves 2 lines, so we need to
1541 fix that up. */
1542 if (it3.line_wrap == WORD_WRAP)
1543 move_it_by_lines (&it3, -1);
1546 /* Record the vertical coordinate of the display
1547 line where we wound up. */
1548 top_y = it3.current_y;
1549 if (it3.bidi_p)
1551 /* When characters are reordered for display,
1552 the character displayed to the left of the
1553 display string could be _after_ the display
1554 property in the logical order. Use the
1555 smallest vertical position of these two. */
1556 start_display (&it3, w, top);
1557 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1558 if (it3.current_y < top_y)
1559 top_y = it3.current_y;
1561 /* Move from the top of the window to the beginning
1562 of the display line where the display string
1563 begins. */
1564 start_display (&it3, w, top);
1565 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1566 /* If it3_moved stays zero after the 'while' loop
1567 below, that means we already were at a newline
1568 before the loop (e.g., the display string begins
1569 with a newline), so we don't need to (and cannot)
1570 inspect the glyphs of it3.glyph_row, because
1571 PRODUCE_GLYPHS will not produce anything for a
1572 newline, and thus it3.glyph_row stays at its
1573 stale content it got at top of the window. */
1574 it3_moved = 0;
1575 /* Finally, advance the iterator until we hit the
1576 first display element whose character position is
1577 CHARPOS, or until the first newline from the
1578 display string, which signals the end of the
1579 display line. */
1580 while (get_next_display_element (&it3))
1582 PRODUCE_GLYPHS (&it3);
1583 if (IT_CHARPOS (it3) == charpos
1584 || ITERATOR_AT_END_OF_LINE_P (&it3))
1585 break;
1586 it3_moved = 1;
1587 set_iterator_to_next (&it3, 0);
1589 top_x = it3.current_x - it3.pixel_width;
1590 /* Normally, we would exit the above loop because we
1591 found the display element whose character
1592 position is CHARPOS. For the contingency that we
1593 didn't, and stopped at the first newline from the
1594 display string, move back over the glyphs
1595 produced from the string, until we find the
1596 rightmost glyph not from the string. */
1597 if (it3_moved
1598 && newline_in_string
1599 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1601 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1602 + it3.glyph_row->used[TEXT_AREA];
1604 while (EQ ((g - 1)->object, string))
1606 --g;
1607 top_x -= g->pixel_width;
1609 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1610 + it3.glyph_row->used[TEXT_AREA]);
1615 *x = top_x;
1616 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1617 *rtop = max (0, window_top_y - top_y);
1618 *rbot = max (0, bottom_y - it.last_visible_y);
1619 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1620 - max (top_y, window_top_y)));
1621 *vpos = it.vpos;
1624 else
1626 /* We were asked to provide info about WINDOW_END. */
1627 struct it it2;
1628 void *it2data = NULL;
1630 SAVE_IT (it2, it, it2data);
1631 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1632 move_it_by_lines (&it, 1);
1633 if (charpos < IT_CHARPOS (it)
1634 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1636 visible_p = 1;
1637 RESTORE_IT (&it2, &it2, it2data);
1638 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1639 *x = it2.current_x;
1640 *y = it2.current_y + it2.max_ascent - it2.ascent;
1641 *rtop = max (0, -it2.current_y);
1642 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1643 - it.last_visible_y));
1644 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1645 it.last_visible_y)
1646 - max (it2.current_y,
1647 WINDOW_HEADER_LINE_HEIGHT (w))));
1648 *vpos = it2.vpos;
1650 else
1651 bidi_unshelve_cache (it2data, 1);
1653 bidi_unshelve_cache (itdata, 0);
1655 if (old_buffer)
1656 set_buffer_internal_1 (old_buffer);
1658 current_header_line_height = current_mode_line_height = -1;
1660 if (visible_p && w->hscroll > 0)
1661 *x -=
1662 window_hscroll_limited (w, WINDOW_XFRAME (w))
1663 * WINDOW_FRAME_COLUMN_WIDTH (w);
1665 #if 0
1666 /* Debugging code. */
1667 if (visible_p)
1668 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1669 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1670 else
1671 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1672 #endif
1674 return visible_p;
1678 /* Return the next character from STR. Return in *LEN the length of
1679 the character. This is like STRING_CHAR_AND_LENGTH but never
1680 returns an invalid character. If we find one, we return a `?', but
1681 with the length of the invalid character. */
1683 static int
1684 string_char_and_length (const unsigned char *str, int *len)
1686 int c;
1688 c = STRING_CHAR_AND_LENGTH (str, *len);
1689 if (!CHAR_VALID_P (c))
1690 /* We may not change the length here because other places in Emacs
1691 don't use this function, i.e. they silently accept invalid
1692 characters. */
1693 c = '?';
1695 return c;
1700 /* Given a position POS containing a valid character and byte position
1701 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1703 static struct text_pos
1704 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1706 eassert (STRINGP (string) && nchars >= 0);
1708 if (STRING_MULTIBYTE (string))
1710 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1711 int len;
1713 while (nchars--)
1715 string_char_and_length (p, &len);
1716 p += len;
1717 CHARPOS (pos) += 1;
1718 BYTEPOS (pos) += len;
1721 else
1722 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1724 return pos;
1728 /* Value is the text position, i.e. character and byte position,
1729 for character position CHARPOS in STRING. */
1731 static struct text_pos
1732 string_pos (ptrdiff_t charpos, Lisp_Object string)
1734 struct text_pos pos;
1735 eassert (STRINGP (string));
1736 eassert (charpos >= 0);
1737 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1738 return pos;
1742 /* Value is a text position, i.e. character and byte position, for
1743 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1744 means recognize multibyte characters. */
1746 static struct text_pos
1747 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1749 struct text_pos pos;
1751 eassert (s != NULL);
1752 eassert (charpos >= 0);
1754 if (multibyte_p)
1756 int len;
1758 SET_TEXT_POS (pos, 0, 0);
1759 while (charpos--)
1761 string_char_and_length ((const unsigned char *) s, &len);
1762 s += len;
1763 CHARPOS (pos) += 1;
1764 BYTEPOS (pos) += len;
1767 else
1768 SET_TEXT_POS (pos, charpos, charpos);
1770 return pos;
1774 /* Value is the number of characters in C string S. MULTIBYTE_P
1775 non-zero means recognize multibyte characters. */
1777 static ptrdiff_t
1778 number_of_chars (const char *s, bool multibyte_p)
1780 ptrdiff_t nchars;
1782 if (multibyte_p)
1784 ptrdiff_t rest = strlen (s);
1785 int len;
1786 const unsigned char *p = (const unsigned char *) s;
1788 for (nchars = 0; rest > 0; ++nchars)
1790 string_char_and_length (p, &len);
1791 rest -= len, p += len;
1794 else
1795 nchars = strlen (s);
1797 return nchars;
1801 /* Compute byte position NEWPOS->bytepos corresponding to
1802 NEWPOS->charpos. POS is a known position in string STRING.
1803 NEWPOS->charpos must be >= POS.charpos. */
1805 static void
1806 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1808 eassert (STRINGP (string));
1809 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1811 if (STRING_MULTIBYTE (string))
1812 *newpos = string_pos_nchars_ahead (pos, string,
1813 CHARPOS (*newpos) - CHARPOS (pos));
1814 else
1815 BYTEPOS (*newpos) = CHARPOS (*newpos);
1818 /* EXPORT:
1819 Return an estimation of the pixel height of mode or header lines on
1820 frame F. FACE_ID specifies what line's height to estimate. */
1823 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1825 #ifdef HAVE_WINDOW_SYSTEM
1826 if (FRAME_WINDOW_P (f))
1828 int height = FONT_HEIGHT (FRAME_FONT (f));
1830 /* This function is called so early when Emacs starts that the face
1831 cache and mode line face are not yet initialized. */
1832 if (FRAME_FACE_CACHE (f))
1834 struct face *face = FACE_FROM_ID (f, face_id);
1835 if (face)
1837 if (face->font)
1838 height = FONT_HEIGHT (face->font);
1839 if (face->box_line_width > 0)
1840 height += 2 * face->box_line_width;
1844 return height;
1846 #endif
1848 return 1;
1851 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1852 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1853 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1854 not force the value into range. */
1856 void
1857 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1858 int *x, int *y, NativeRectangle *bounds, int noclip)
1861 #ifdef HAVE_WINDOW_SYSTEM
1862 if (FRAME_WINDOW_P (f))
1864 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1865 even for negative values. */
1866 if (pix_x < 0)
1867 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1868 if (pix_y < 0)
1869 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1871 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1872 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1874 if (bounds)
1875 STORE_NATIVE_RECT (*bounds,
1876 FRAME_COL_TO_PIXEL_X (f, pix_x),
1877 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1878 FRAME_COLUMN_WIDTH (f) - 1,
1879 FRAME_LINE_HEIGHT (f) - 1);
1881 if (!noclip)
1883 if (pix_x < 0)
1884 pix_x = 0;
1885 else if (pix_x > FRAME_TOTAL_COLS (f))
1886 pix_x = FRAME_TOTAL_COLS (f);
1888 if (pix_y < 0)
1889 pix_y = 0;
1890 else if (pix_y > FRAME_LINES (f))
1891 pix_y = FRAME_LINES (f);
1894 #endif
1896 *x = pix_x;
1897 *y = pix_y;
1901 /* Find the glyph under window-relative coordinates X/Y in window W.
1902 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1903 strings. Return in *HPOS and *VPOS the row and column number of
1904 the glyph found. Return in *AREA the glyph area containing X.
1905 Value is a pointer to the glyph found or null if X/Y is not on
1906 text, or we can't tell because W's current matrix is not up to
1907 date. */
1909 static
1910 struct glyph *
1911 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1912 int *dx, int *dy, int *area)
1914 struct glyph *glyph, *end;
1915 struct glyph_row *row = NULL;
1916 int x0, i;
1918 /* Find row containing Y. Give up if some row is not enabled. */
1919 for (i = 0; i < w->current_matrix->nrows; ++i)
1921 row = MATRIX_ROW (w->current_matrix, i);
1922 if (!row->enabled_p)
1923 return NULL;
1924 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1925 break;
1928 *vpos = i;
1929 *hpos = 0;
1931 /* Give up if Y is not in the window. */
1932 if (i == w->current_matrix->nrows)
1933 return NULL;
1935 /* Get the glyph area containing X. */
1936 if (w->pseudo_window_p)
1938 *area = TEXT_AREA;
1939 x0 = 0;
1941 else
1943 if (x < window_box_left_offset (w, TEXT_AREA))
1945 *area = LEFT_MARGIN_AREA;
1946 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1948 else if (x < window_box_right_offset (w, TEXT_AREA))
1950 *area = TEXT_AREA;
1951 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1953 else
1955 *area = RIGHT_MARGIN_AREA;
1956 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1960 /* Find glyph containing X. */
1961 glyph = row->glyphs[*area];
1962 end = glyph + row->used[*area];
1963 x -= x0;
1964 while (glyph < end && x >= glyph->pixel_width)
1966 x -= glyph->pixel_width;
1967 ++glyph;
1970 if (glyph == end)
1971 return NULL;
1973 if (dx)
1975 *dx = x;
1976 *dy = y - (row->y + row->ascent - glyph->ascent);
1979 *hpos = glyph - row->glyphs[*area];
1980 return glyph;
1983 /* Convert frame-relative x/y to coordinates relative to window W.
1984 Takes pseudo-windows into account. */
1986 static void
1987 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1989 if (w->pseudo_window_p)
1991 /* A pseudo-window is always full-width, and starts at the
1992 left edge of the frame, plus a frame border. */
1993 struct frame *f = XFRAME (w->frame);
1994 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1995 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1997 else
1999 *x -= WINDOW_LEFT_EDGE_X (w);
2000 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2004 #ifdef HAVE_WINDOW_SYSTEM
2006 /* EXPORT:
2007 Return in RECTS[] at most N clipping rectangles for glyph string S.
2008 Return the number of stored rectangles. */
2011 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2013 XRectangle r;
2015 if (n <= 0)
2016 return 0;
2018 if (s->row->full_width_p)
2020 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2021 r.x = WINDOW_LEFT_EDGE_X (s->w);
2022 r.width = WINDOW_TOTAL_WIDTH (s->w);
2024 /* Unless displaying a mode or menu bar line, which are always
2025 fully visible, clip to the visible part of the row. */
2026 if (s->w->pseudo_window_p)
2027 r.height = s->row->visible_height;
2028 else
2029 r.height = s->height;
2031 else
2033 /* This is a text line that may be partially visible. */
2034 r.x = window_box_left (s->w, s->area);
2035 r.width = window_box_width (s->w, s->area);
2036 r.height = s->row->visible_height;
2039 if (s->clip_head)
2040 if (r.x < s->clip_head->x)
2042 if (r.width >= s->clip_head->x - r.x)
2043 r.width -= s->clip_head->x - r.x;
2044 else
2045 r.width = 0;
2046 r.x = s->clip_head->x;
2048 if (s->clip_tail)
2049 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2051 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2052 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2053 else
2054 r.width = 0;
2057 /* If S draws overlapping rows, it's sufficient to use the top and
2058 bottom of the window for clipping because this glyph string
2059 intentionally draws over other lines. */
2060 if (s->for_overlaps)
2062 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2063 r.height = window_text_bottom_y (s->w) - r.y;
2065 /* Alas, the above simple strategy does not work for the
2066 environments with anti-aliased text: if the same text is
2067 drawn onto the same place multiple times, it gets thicker.
2068 If the overlap we are processing is for the erased cursor, we
2069 take the intersection with the rectangle of the cursor. */
2070 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2072 XRectangle rc, r_save = r;
2074 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2075 rc.y = s->w->phys_cursor.y;
2076 rc.width = s->w->phys_cursor_width;
2077 rc.height = s->w->phys_cursor_height;
2079 x_intersect_rectangles (&r_save, &rc, &r);
2082 else
2084 /* Don't use S->y for clipping because it doesn't take partially
2085 visible lines into account. For example, it can be negative for
2086 partially visible lines at the top of a window. */
2087 if (!s->row->full_width_p
2088 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2089 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2090 else
2091 r.y = max (0, s->row->y);
2094 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2096 /* If drawing the cursor, don't let glyph draw outside its
2097 advertised boundaries. Cleartype does this under some circumstances. */
2098 if (s->hl == DRAW_CURSOR)
2100 struct glyph *glyph = s->first_glyph;
2101 int height, max_y;
2103 if (s->x > r.x)
2105 r.width -= s->x - r.x;
2106 r.x = s->x;
2108 r.width = min (r.width, glyph->pixel_width);
2110 /* If r.y is below window bottom, ensure that we still see a cursor. */
2111 height = min (glyph->ascent + glyph->descent,
2112 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2113 max_y = window_text_bottom_y (s->w) - height;
2114 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2115 if (s->ybase - glyph->ascent > max_y)
2117 r.y = max_y;
2118 r.height = height;
2120 else
2122 /* Don't draw cursor glyph taller than our actual glyph. */
2123 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2124 if (height < r.height)
2126 max_y = r.y + r.height;
2127 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2128 r.height = min (max_y - r.y, height);
2133 if (s->row->clip)
2135 XRectangle r_save = r;
2137 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2138 r.width = 0;
2141 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2142 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2144 #ifdef CONVERT_FROM_XRECT
2145 CONVERT_FROM_XRECT (r, *rects);
2146 #else
2147 *rects = r;
2148 #endif
2149 return 1;
2151 else
2153 /* If we are processing overlapping and allowed to return
2154 multiple clipping rectangles, we exclude the row of the glyph
2155 string from the clipping rectangle. This is to avoid drawing
2156 the same text on the environment with anti-aliasing. */
2157 #ifdef CONVERT_FROM_XRECT
2158 XRectangle rs[2];
2159 #else
2160 XRectangle *rs = rects;
2161 #endif
2162 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2164 if (s->for_overlaps & OVERLAPS_PRED)
2166 rs[i] = r;
2167 if (r.y + r.height > row_y)
2169 if (r.y < row_y)
2170 rs[i].height = row_y - r.y;
2171 else
2172 rs[i].height = 0;
2174 i++;
2176 if (s->for_overlaps & OVERLAPS_SUCC)
2178 rs[i] = r;
2179 if (r.y < row_y + s->row->visible_height)
2181 if (r.y + r.height > row_y + s->row->visible_height)
2183 rs[i].y = row_y + s->row->visible_height;
2184 rs[i].height = r.y + r.height - rs[i].y;
2186 else
2187 rs[i].height = 0;
2189 i++;
2192 n = i;
2193 #ifdef CONVERT_FROM_XRECT
2194 for (i = 0; i < n; i++)
2195 CONVERT_FROM_XRECT (rs[i], rects[i]);
2196 #endif
2197 return n;
2201 /* EXPORT:
2202 Return in *NR the clipping rectangle for glyph string S. */
2204 void
2205 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2207 get_glyph_string_clip_rects (s, nr, 1);
2211 /* EXPORT:
2212 Return the position and height of the phys cursor in window W.
2213 Set w->phys_cursor_width to width of phys cursor.
2216 void
2217 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2218 struct glyph *glyph, int *xp, int *yp, int *heightp)
2220 struct frame *f = XFRAME (WINDOW_FRAME (w));
2221 int x, y, wd, h, h0, y0;
2223 /* Compute the width of the rectangle to draw. If on a stretch
2224 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2225 rectangle as wide as the glyph, but use a canonical character
2226 width instead. */
2227 wd = glyph->pixel_width - 1;
2228 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2229 wd++; /* Why? */
2230 #endif
2232 x = w->phys_cursor.x;
2233 if (x < 0)
2235 wd += x;
2236 x = 0;
2239 if (glyph->type == STRETCH_GLYPH
2240 && !x_stretch_cursor_p)
2241 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2242 w->phys_cursor_width = wd;
2244 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2246 /* If y is below window bottom, ensure that we still see a cursor. */
2247 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2249 h = max (h0, glyph->ascent + glyph->descent);
2250 h0 = min (h0, glyph->ascent + glyph->descent);
2252 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2253 if (y < y0)
2255 h = max (h - (y0 - y) + 1, h0);
2256 y = y0 - 1;
2258 else
2260 y0 = window_text_bottom_y (w) - h0;
2261 if (y > y0)
2263 h += y - y0;
2264 y = y0;
2268 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2269 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2270 *heightp = h;
2274 * Remember which glyph the mouse is over.
2277 void
2278 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2280 Lisp_Object window;
2281 struct window *w;
2282 struct glyph_row *r, *gr, *end_row;
2283 enum window_part part;
2284 enum glyph_row_area area;
2285 int x, y, width, height;
2287 /* Try to determine frame pixel position and size of the glyph under
2288 frame pixel coordinates X/Y on frame F. */
2290 if (!f->glyphs_initialized_p
2291 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2292 NILP (window)))
2294 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2295 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2296 goto virtual_glyph;
2299 w = XWINDOW (window);
2300 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2301 height = WINDOW_FRAME_LINE_HEIGHT (w);
2303 x = window_relative_x_coord (w, part, gx);
2304 y = gy - WINDOW_TOP_EDGE_Y (w);
2306 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2307 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2309 if (w->pseudo_window_p)
2311 area = TEXT_AREA;
2312 part = ON_MODE_LINE; /* Don't adjust margin. */
2313 goto text_glyph;
2316 switch (part)
2318 case ON_LEFT_MARGIN:
2319 area = LEFT_MARGIN_AREA;
2320 goto text_glyph;
2322 case ON_RIGHT_MARGIN:
2323 area = RIGHT_MARGIN_AREA;
2324 goto text_glyph;
2326 case ON_HEADER_LINE:
2327 case ON_MODE_LINE:
2328 gr = (part == ON_HEADER_LINE
2329 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2330 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2331 gy = gr->y;
2332 area = TEXT_AREA;
2333 goto text_glyph_row_found;
2335 case ON_TEXT:
2336 area = TEXT_AREA;
2338 text_glyph:
2339 gr = 0; gy = 0;
2340 for (; r <= end_row && r->enabled_p; ++r)
2341 if (r->y + r->height > y)
2343 gr = r; gy = r->y;
2344 break;
2347 text_glyph_row_found:
2348 if (gr && gy <= y)
2350 struct glyph *g = gr->glyphs[area];
2351 struct glyph *end = g + gr->used[area];
2353 height = gr->height;
2354 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2355 if (gx + g->pixel_width > x)
2356 break;
2358 if (g < end)
2360 if (g->type == IMAGE_GLYPH)
2362 /* Don't remember when mouse is over image, as
2363 image may have hot-spots. */
2364 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2365 return;
2367 width = g->pixel_width;
2369 else
2371 /* Use nominal char spacing at end of line. */
2372 x -= gx;
2373 gx += (x / width) * width;
2376 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2377 gx += window_box_left_offset (w, area);
2379 else
2381 /* Use nominal line height at end of window. */
2382 gx = (x / width) * width;
2383 y -= gy;
2384 gy += (y / height) * height;
2386 break;
2388 case ON_LEFT_FRINGE:
2389 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2390 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2391 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2392 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2393 goto row_glyph;
2395 case ON_RIGHT_FRINGE:
2396 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2397 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2398 : window_box_right_offset (w, TEXT_AREA));
2399 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2400 goto row_glyph;
2402 case ON_SCROLL_BAR:
2403 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2405 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2406 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2407 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2408 : 0)));
2409 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2411 row_glyph:
2412 gr = 0, gy = 0;
2413 for (; r <= end_row && r->enabled_p; ++r)
2414 if (r->y + r->height > y)
2416 gr = r; gy = r->y;
2417 break;
2420 if (gr && gy <= y)
2421 height = gr->height;
2422 else
2424 /* Use nominal line height at end of window. */
2425 y -= gy;
2426 gy += (y / height) * height;
2428 break;
2430 default:
2432 virtual_glyph:
2433 /* If there is no glyph under the mouse, then we divide the screen
2434 into a grid of the smallest glyph in the frame, and use that
2435 as our "glyph". */
2437 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2438 round down even for negative values. */
2439 if (gx < 0)
2440 gx -= width - 1;
2441 if (gy < 0)
2442 gy -= height - 1;
2444 gx = (gx / width) * width;
2445 gy = (gy / height) * height;
2447 goto store_rect;
2450 gx += WINDOW_LEFT_EDGE_X (w);
2451 gy += WINDOW_TOP_EDGE_Y (w);
2453 store_rect:
2454 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2456 /* Visible feedback for debugging. */
2457 #if 0
2458 #if HAVE_X_WINDOWS
2459 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2460 f->output_data.x->normal_gc,
2461 gx, gy, width, height);
2462 #endif
2463 #endif
2467 #endif /* HAVE_WINDOW_SYSTEM */
2470 /***********************************************************************
2471 Lisp form evaluation
2472 ***********************************************************************/
2474 /* Error handler for safe_eval and safe_call. */
2476 static Lisp_Object
2477 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2479 add_to_log ("Error during redisplay: %S signaled %S",
2480 Flist (nargs, args), arg);
2481 return Qnil;
2484 /* Call function FUNC with the rest of NARGS - 1 arguments
2485 following. Return the result, or nil if something went
2486 wrong. Prevent redisplay during the evaluation. */
2488 Lisp_Object
2489 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2491 Lisp_Object val;
2493 if (inhibit_eval_during_redisplay)
2494 val = Qnil;
2495 else
2497 va_list ap;
2498 ptrdiff_t i;
2499 ptrdiff_t count = SPECPDL_INDEX ();
2500 struct gcpro gcpro1;
2501 Lisp_Object *args = alloca (nargs * word_size);
2503 args[0] = func;
2504 va_start (ap, func);
2505 for (i = 1; i < nargs; i++)
2506 args[i] = va_arg (ap, Lisp_Object);
2507 va_end (ap);
2509 GCPRO1 (args[0]);
2510 gcpro1.nvars = nargs;
2511 specbind (Qinhibit_redisplay, Qt);
2512 /* Use Qt to ensure debugger does not run,
2513 so there is no possibility of wanting to redisplay. */
2514 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2515 safe_eval_handler);
2516 UNGCPRO;
2517 val = unbind_to (count, val);
2520 return val;
2524 /* Call function FN with one argument ARG.
2525 Return the result, or nil if something went wrong. */
2527 Lisp_Object
2528 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2530 return safe_call (2, fn, arg);
2533 static Lisp_Object Qeval;
2535 Lisp_Object
2536 safe_eval (Lisp_Object sexpr)
2538 return safe_call1 (Qeval, sexpr);
2541 /* Call function FN with two arguments ARG1 and ARG2.
2542 Return the result, or nil if something went wrong. */
2544 Lisp_Object
2545 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2547 return safe_call (3, fn, arg1, arg2);
2552 /***********************************************************************
2553 Debugging
2554 ***********************************************************************/
2556 #if 0
2558 /* Define CHECK_IT to perform sanity checks on iterators.
2559 This is for debugging. It is too slow to do unconditionally. */
2561 static void
2562 check_it (struct it *it)
2564 if (it->method == GET_FROM_STRING)
2566 eassert (STRINGP (it->string));
2567 eassert (IT_STRING_CHARPOS (*it) >= 0);
2569 else
2571 eassert (IT_STRING_CHARPOS (*it) < 0);
2572 if (it->method == GET_FROM_BUFFER)
2574 /* Check that character and byte positions agree. */
2575 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2579 if (it->dpvec)
2580 eassert (it->current.dpvec_index >= 0);
2581 else
2582 eassert (it->current.dpvec_index < 0);
2585 #define CHECK_IT(IT) check_it ((IT))
2587 #else /* not 0 */
2589 #define CHECK_IT(IT) (void) 0
2591 #endif /* not 0 */
2594 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2596 /* Check that the window end of window W is what we expect it
2597 to be---the last row in the current matrix displaying text. */
2599 static void
2600 check_window_end (struct window *w)
2602 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2604 struct glyph_row *row;
2605 eassert ((row = MATRIX_ROW (w->current_matrix,
2606 XFASTINT (w->window_end_vpos)),
2607 !row->enabled_p
2608 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2609 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2613 #define CHECK_WINDOW_END(W) check_window_end ((W))
2615 #else
2617 #define CHECK_WINDOW_END(W) (void) 0
2619 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2621 /* Return mark position if current buffer has the region of non-zero length,
2622 or -1 otherwise. */
2624 static ptrdiff_t
2625 markpos_of_region (void)
2627 if (!NILP (Vtransient_mark_mode)
2628 && !NILP (BVAR (current_buffer, mark_active))
2629 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2631 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2633 if (markpos != PT)
2634 return markpos;
2636 return -1;
2639 /***********************************************************************
2640 Iterator initialization
2641 ***********************************************************************/
2643 /* Initialize IT for displaying current_buffer in window W, starting
2644 at character position CHARPOS. CHARPOS < 0 means that no buffer
2645 position is specified which is useful when the iterator is assigned
2646 a position later. BYTEPOS is the byte position corresponding to
2647 CHARPOS.
2649 If ROW is not null, calls to produce_glyphs with IT as parameter
2650 will produce glyphs in that row.
2652 BASE_FACE_ID is the id of a base face to use. It must be one of
2653 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2654 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2655 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2657 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2658 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2659 will be initialized to use the corresponding mode line glyph row of
2660 the desired matrix of W. */
2662 void
2663 init_iterator (struct it *it, struct window *w,
2664 ptrdiff_t charpos, ptrdiff_t bytepos,
2665 struct glyph_row *row, enum face_id base_face_id)
2667 ptrdiff_t markpos;
2668 enum face_id remapped_base_face_id = base_face_id;
2670 /* Some precondition checks. */
2671 eassert (w != NULL && it != NULL);
2672 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2673 && charpos <= ZV));
2675 /* If face attributes have been changed since the last redisplay,
2676 free realized faces now because they depend on face definitions
2677 that might have changed. Don't free faces while there might be
2678 desired matrices pending which reference these faces. */
2679 if (face_change_count && !inhibit_free_realized_faces)
2681 face_change_count = 0;
2682 free_all_realized_faces (Qnil);
2685 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2686 if (! NILP (Vface_remapping_alist))
2687 remapped_base_face_id
2688 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2690 /* Use one of the mode line rows of W's desired matrix if
2691 appropriate. */
2692 if (row == NULL)
2694 if (base_face_id == MODE_LINE_FACE_ID
2695 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2696 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2697 else if (base_face_id == HEADER_LINE_FACE_ID)
2698 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2701 /* Clear IT. */
2702 memset (it, 0, sizeof *it);
2703 it->current.overlay_string_index = -1;
2704 it->current.dpvec_index = -1;
2705 it->base_face_id = remapped_base_face_id;
2706 it->string = Qnil;
2707 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2708 it->paragraph_embedding = L2R;
2709 it->bidi_it.string.lstring = Qnil;
2710 it->bidi_it.string.s = NULL;
2711 it->bidi_it.string.bufpos = 0;
2712 it->bidi_it.w = w;
2714 /* The window in which we iterate over current_buffer: */
2715 XSETWINDOW (it->window, w);
2716 it->w = w;
2717 it->f = XFRAME (w->frame);
2719 it->cmp_it.id = -1;
2721 /* Extra space between lines (on window systems only). */
2722 if (base_face_id == DEFAULT_FACE_ID
2723 && FRAME_WINDOW_P (it->f))
2725 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2726 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2727 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2728 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2729 * FRAME_LINE_HEIGHT (it->f));
2730 else if (it->f->extra_line_spacing > 0)
2731 it->extra_line_spacing = it->f->extra_line_spacing;
2732 it->max_extra_line_spacing = 0;
2735 /* If realized faces have been removed, e.g. because of face
2736 attribute changes of named faces, recompute them. When running
2737 in batch mode, the face cache of the initial frame is null. If
2738 we happen to get called, make a dummy face cache. */
2739 if (FRAME_FACE_CACHE (it->f) == NULL)
2740 init_frame_faces (it->f);
2741 if (FRAME_FACE_CACHE (it->f)->used == 0)
2742 recompute_basic_faces (it->f);
2744 /* Current value of the `slice', `space-width', and 'height' properties. */
2745 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2746 it->space_width = Qnil;
2747 it->font_height = Qnil;
2748 it->override_ascent = -1;
2750 /* Are control characters displayed as `^C'? */
2751 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2753 /* -1 means everything between a CR and the following line end
2754 is invisible. >0 means lines indented more than this value are
2755 invisible. */
2756 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2757 ? (clip_to_bounds
2758 (-1, XINT (BVAR (current_buffer, selective_display)),
2759 PTRDIFF_MAX))
2760 : (!NILP (BVAR (current_buffer, selective_display))
2761 ? -1 : 0));
2762 it->selective_display_ellipsis_p
2763 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2765 /* Display table to use. */
2766 it->dp = window_display_table (w);
2768 /* Are multibyte characters enabled in current_buffer? */
2769 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2771 /* If visible region is of non-zero length, set IT->region_beg_charpos
2772 and IT->region_end_charpos to the start and end of a visible region
2773 in window IT->w. Set both to -1 to indicate no region. */
2774 markpos = markpos_of_region ();
2775 if (markpos >= 0
2776 /* Maybe highlight only in selected window. */
2777 && (/* Either show region everywhere. */
2778 highlight_nonselected_windows
2779 /* Or show region in the selected window. */
2780 || w == XWINDOW (selected_window)
2781 /* Or show the region if we are in the mini-buffer and W is
2782 the window the mini-buffer refers to. */
2783 || (MINI_WINDOW_P (XWINDOW (selected_window))
2784 && WINDOWP (minibuf_selected_window)
2785 && w == XWINDOW (minibuf_selected_window))))
2787 it->region_beg_charpos = min (PT, markpos);
2788 it->region_end_charpos = max (PT, markpos);
2790 else
2791 it->region_beg_charpos = it->region_end_charpos = -1;
2793 /* Get the position at which the redisplay_end_trigger hook should
2794 be run, if it is to be run at all. */
2795 if (MARKERP (w->redisplay_end_trigger)
2796 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2797 it->redisplay_end_trigger_charpos
2798 = marker_position (w->redisplay_end_trigger);
2799 else if (INTEGERP (w->redisplay_end_trigger))
2800 it->redisplay_end_trigger_charpos =
2801 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2803 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2805 /* Are lines in the display truncated? */
2806 if (base_face_id != DEFAULT_FACE_ID
2807 || it->w->hscroll
2808 || (! WINDOW_FULL_WIDTH_P (it->w)
2809 && ((!NILP (Vtruncate_partial_width_windows)
2810 && !INTEGERP (Vtruncate_partial_width_windows))
2811 || (INTEGERP (Vtruncate_partial_width_windows)
2812 && (WINDOW_TOTAL_COLS (it->w)
2813 < XINT (Vtruncate_partial_width_windows))))))
2814 it->line_wrap = TRUNCATE;
2815 else if (NILP (BVAR (current_buffer, truncate_lines)))
2816 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2817 ? WINDOW_WRAP : WORD_WRAP;
2818 else
2819 it->line_wrap = TRUNCATE;
2821 /* Get dimensions of truncation and continuation glyphs. These are
2822 displayed as fringe bitmaps under X, but we need them for such
2823 frames when the fringes are turned off. But leave the dimensions
2824 zero for tooltip frames, as these glyphs look ugly there and also
2825 sabotage calculations of tooltip dimensions in x-show-tip. */
2826 #ifdef HAVE_WINDOW_SYSTEM
2827 if (!(FRAME_WINDOW_P (it->f)
2828 && FRAMEP (tip_frame)
2829 && it->f == XFRAME (tip_frame)))
2830 #endif
2832 if (it->line_wrap == TRUNCATE)
2834 /* We will need the truncation glyph. */
2835 eassert (it->glyph_row == NULL);
2836 produce_special_glyphs (it, IT_TRUNCATION);
2837 it->truncation_pixel_width = it->pixel_width;
2839 else
2841 /* We will need the continuation glyph. */
2842 eassert (it->glyph_row == NULL);
2843 produce_special_glyphs (it, IT_CONTINUATION);
2844 it->continuation_pixel_width = it->pixel_width;
2848 /* Reset these values to zero because the produce_special_glyphs
2849 above has changed them. */
2850 it->pixel_width = it->ascent = it->descent = 0;
2851 it->phys_ascent = it->phys_descent = 0;
2853 /* Set this after getting the dimensions of truncation and
2854 continuation glyphs, so that we don't produce glyphs when calling
2855 produce_special_glyphs, above. */
2856 it->glyph_row = row;
2857 it->area = TEXT_AREA;
2859 /* Forget any previous info about this row being reversed. */
2860 if (it->glyph_row)
2861 it->glyph_row->reversed_p = 0;
2863 /* Get the dimensions of the display area. The display area
2864 consists of the visible window area plus a horizontally scrolled
2865 part to the left of the window. All x-values are relative to the
2866 start of this total display area. */
2867 if (base_face_id != DEFAULT_FACE_ID)
2869 /* Mode lines, menu bar in terminal frames. */
2870 it->first_visible_x = 0;
2871 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2873 else
2875 it->first_visible_x =
2876 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2877 it->last_visible_x = (it->first_visible_x
2878 + window_box_width (w, TEXT_AREA));
2880 /* If we truncate lines, leave room for the truncation glyph(s) at
2881 the right margin. Otherwise, leave room for the continuation
2882 glyph(s). Done only if the window has no fringes. Since we
2883 don't know at this point whether there will be any R2L lines in
2884 the window, we reserve space for truncation/continuation glyphs
2885 even if only one of the fringes is absent. */
2886 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2887 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2889 if (it->line_wrap == TRUNCATE)
2890 it->last_visible_x -= it->truncation_pixel_width;
2891 else
2892 it->last_visible_x -= it->continuation_pixel_width;
2895 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2896 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2899 /* Leave room for a border glyph. */
2900 if (!FRAME_WINDOW_P (it->f)
2901 && !WINDOW_RIGHTMOST_P (it->w))
2902 it->last_visible_x -= 1;
2904 it->last_visible_y = window_text_bottom_y (w);
2906 /* For mode lines and alike, arrange for the first glyph having a
2907 left box line if the face specifies a box. */
2908 if (base_face_id != DEFAULT_FACE_ID)
2910 struct face *face;
2912 it->face_id = remapped_base_face_id;
2914 /* If we have a boxed mode line, make the first character appear
2915 with a left box line. */
2916 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2917 if (face->box != FACE_NO_BOX)
2918 it->start_of_box_run_p = 1;
2921 /* If a buffer position was specified, set the iterator there,
2922 getting overlays and face properties from that position. */
2923 if (charpos >= BUF_BEG (current_buffer))
2925 it->end_charpos = ZV;
2926 eassert (charpos == BYTE_TO_CHAR (bytepos));
2927 IT_CHARPOS (*it) = charpos;
2928 IT_BYTEPOS (*it) = bytepos;
2930 /* We will rely on `reseat' to set this up properly, via
2931 handle_face_prop. */
2932 it->face_id = it->base_face_id;
2934 it->start = it->current;
2935 /* Do we need to reorder bidirectional text? Not if this is a
2936 unibyte buffer: by definition, none of the single-byte
2937 characters are strong R2L, so no reordering is needed. And
2938 bidi.c doesn't support unibyte buffers anyway. Also, don't
2939 reorder while we are loading loadup.el, since the tables of
2940 character properties needed for reordering are not yet
2941 available. */
2942 it->bidi_p =
2943 NILP (Vpurify_flag)
2944 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2945 && it->multibyte_p;
2947 /* If we are to reorder bidirectional text, init the bidi
2948 iterator. */
2949 if (it->bidi_p)
2951 /* Note the paragraph direction that this buffer wants to
2952 use. */
2953 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2954 Qleft_to_right))
2955 it->paragraph_embedding = L2R;
2956 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2957 Qright_to_left))
2958 it->paragraph_embedding = R2L;
2959 else
2960 it->paragraph_embedding = NEUTRAL_DIR;
2961 bidi_unshelve_cache (NULL, 0);
2962 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2963 &it->bidi_it);
2966 /* Compute faces etc. */
2967 reseat (it, it->current.pos, 1);
2970 CHECK_IT (it);
2974 /* Initialize IT for the display of window W with window start POS. */
2976 void
2977 start_display (struct it *it, struct window *w, struct text_pos pos)
2979 struct glyph_row *row;
2980 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2982 row = w->desired_matrix->rows + first_vpos;
2983 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2984 it->first_vpos = first_vpos;
2986 /* Don't reseat to previous visible line start if current start
2987 position is in a string or image. */
2988 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2990 int start_at_line_beg_p;
2991 int first_y = it->current_y;
2993 /* If window start is not at a line start, skip forward to POS to
2994 get the correct continuation lines width. */
2995 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2996 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2997 if (!start_at_line_beg_p)
2999 int new_x;
3001 reseat_at_previous_visible_line_start (it);
3002 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3004 new_x = it->current_x + it->pixel_width;
3006 /* If lines are continued, this line may end in the middle
3007 of a multi-glyph character (e.g. a control character
3008 displayed as \003, or in the middle of an overlay
3009 string). In this case move_it_to above will not have
3010 taken us to the start of the continuation line but to the
3011 end of the continued line. */
3012 if (it->current_x > 0
3013 && it->line_wrap != TRUNCATE /* Lines are continued. */
3014 && (/* And glyph doesn't fit on the line. */
3015 new_x > it->last_visible_x
3016 /* Or it fits exactly and we're on a window
3017 system frame. */
3018 || (new_x == it->last_visible_x
3019 && FRAME_WINDOW_P (it->f)
3020 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3021 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3022 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3024 if ((it->current.dpvec_index >= 0
3025 || it->current.overlay_string_index >= 0)
3026 /* If we are on a newline from a display vector or
3027 overlay string, then we are already at the end of
3028 a screen line; no need to go to the next line in
3029 that case, as this line is not really continued.
3030 (If we do go to the next line, C-e will not DTRT.) */
3031 && it->c != '\n')
3033 set_iterator_to_next (it, 1);
3034 move_it_in_display_line_to (it, -1, -1, 0);
3037 it->continuation_lines_width += it->current_x;
3039 /* If the character at POS is displayed via a display
3040 vector, move_it_to above stops at the final glyph of
3041 IT->dpvec. To make the caller redisplay that character
3042 again (a.k.a. start at POS), we need to reset the
3043 dpvec_index to the beginning of IT->dpvec. */
3044 else if (it->current.dpvec_index >= 0)
3045 it->current.dpvec_index = 0;
3047 /* We're starting a new display line, not affected by the
3048 height of the continued line, so clear the appropriate
3049 fields in the iterator structure. */
3050 it->max_ascent = it->max_descent = 0;
3051 it->max_phys_ascent = it->max_phys_descent = 0;
3053 it->current_y = first_y;
3054 it->vpos = 0;
3055 it->current_x = it->hpos = 0;
3061 /* Return 1 if POS is a position in ellipses displayed for invisible
3062 text. W is the window we display, for text property lookup. */
3064 static int
3065 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3067 Lisp_Object prop, window;
3068 int ellipses_p = 0;
3069 ptrdiff_t charpos = CHARPOS (pos->pos);
3071 /* If POS specifies a position in a display vector, this might
3072 be for an ellipsis displayed for invisible text. We won't
3073 get the iterator set up for delivering that ellipsis unless
3074 we make sure that it gets aware of the invisible text. */
3075 if (pos->dpvec_index >= 0
3076 && pos->overlay_string_index < 0
3077 && CHARPOS (pos->string_pos) < 0
3078 && charpos > BEGV
3079 && (XSETWINDOW (window, w),
3080 prop = Fget_char_property (make_number (charpos),
3081 Qinvisible, window),
3082 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3084 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3085 window);
3086 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3089 return ellipses_p;
3093 /* Initialize IT for stepping through current_buffer in window W,
3094 starting at position POS that includes overlay string and display
3095 vector/ control character translation position information. Value
3096 is zero if there are overlay strings with newlines at POS. */
3098 static int
3099 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3101 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3102 int i, overlay_strings_with_newlines = 0;
3104 /* If POS specifies a position in a display vector, this might
3105 be for an ellipsis displayed for invisible text. We won't
3106 get the iterator set up for delivering that ellipsis unless
3107 we make sure that it gets aware of the invisible text. */
3108 if (in_ellipses_for_invisible_text_p (pos, w))
3110 --charpos;
3111 bytepos = 0;
3114 /* Keep in mind: the call to reseat in init_iterator skips invisible
3115 text, so we might end up at a position different from POS. This
3116 is only a problem when POS is a row start after a newline and an
3117 overlay starts there with an after-string, and the overlay has an
3118 invisible property. Since we don't skip invisible text in
3119 display_line and elsewhere immediately after consuming the
3120 newline before the row start, such a POS will not be in a string,
3121 but the call to init_iterator below will move us to the
3122 after-string. */
3123 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3125 /* This only scans the current chunk -- it should scan all chunks.
3126 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3127 to 16 in 22.1 to make this a lesser problem. */
3128 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3130 const char *s = SSDATA (it->overlay_strings[i]);
3131 const char *e = s + SBYTES (it->overlay_strings[i]);
3133 while (s < e && *s != '\n')
3134 ++s;
3136 if (s < e)
3138 overlay_strings_with_newlines = 1;
3139 break;
3143 /* If position is within an overlay string, set up IT to the right
3144 overlay string. */
3145 if (pos->overlay_string_index >= 0)
3147 int relative_index;
3149 /* If the first overlay string happens to have a `display'
3150 property for an image, the iterator will be set up for that
3151 image, and we have to undo that setup first before we can
3152 correct the overlay string index. */
3153 if (it->method == GET_FROM_IMAGE)
3154 pop_it (it);
3156 /* We already have the first chunk of overlay strings in
3157 IT->overlay_strings. Load more until the one for
3158 pos->overlay_string_index is in IT->overlay_strings. */
3159 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3161 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3162 it->current.overlay_string_index = 0;
3163 while (n--)
3165 load_overlay_strings (it, 0);
3166 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3170 it->current.overlay_string_index = pos->overlay_string_index;
3171 relative_index = (it->current.overlay_string_index
3172 % OVERLAY_STRING_CHUNK_SIZE);
3173 it->string = it->overlay_strings[relative_index];
3174 eassert (STRINGP (it->string));
3175 it->current.string_pos = pos->string_pos;
3176 it->method = GET_FROM_STRING;
3177 it->end_charpos = SCHARS (it->string);
3178 /* Set up the bidi iterator for this overlay string. */
3179 if (it->bidi_p)
3181 it->bidi_it.string.lstring = it->string;
3182 it->bidi_it.string.s = NULL;
3183 it->bidi_it.string.schars = SCHARS (it->string);
3184 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3185 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3186 it->bidi_it.string.unibyte = !it->multibyte_p;
3187 it->bidi_it.w = it->w;
3188 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3189 FRAME_WINDOW_P (it->f), &it->bidi_it);
3191 /* Synchronize the state of the bidi iterator with
3192 pos->string_pos. For any string position other than
3193 zero, this will be done automagically when we resume
3194 iteration over the string and get_visually_first_element
3195 is called. But if string_pos is zero, and the string is
3196 to be reordered for display, we need to resync manually,
3197 since it could be that the iteration state recorded in
3198 pos ended at string_pos of 0 moving backwards in string. */
3199 if (CHARPOS (pos->string_pos) == 0)
3201 get_visually_first_element (it);
3202 if (IT_STRING_CHARPOS (*it) != 0)
3203 do {
3204 /* Paranoia. */
3205 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3206 bidi_move_to_visually_next (&it->bidi_it);
3207 } while (it->bidi_it.charpos != 0);
3209 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3210 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3214 if (CHARPOS (pos->string_pos) >= 0)
3216 /* Recorded position is not in an overlay string, but in another
3217 string. This can only be a string from a `display' property.
3218 IT should already be filled with that string. */
3219 it->current.string_pos = pos->string_pos;
3220 eassert (STRINGP (it->string));
3221 if (it->bidi_p)
3222 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3223 FRAME_WINDOW_P (it->f), &it->bidi_it);
3226 /* Restore position in display vector translations, control
3227 character translations or ellipses. */
3228 if (pos->dpvec_index >= 0)
3230 if (it->dpvec == NULL)
3231 get_next_display_element (it);
3232 eassert (it->dpvec && it->current.dpvec_index == 0);
3233 it->current.dpvec_index = pos->dpvec_index;
3236 CHECK_IT (it);
3237 return !overlay_strings_with_newlines;
3241 /* Initialize IT for stepping through current_buffer in window W
3242 starting at ROW->start. */
3244 static void
3245 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3247 init_from_display_pos (it, w, &row->start);
3248 it->start = row->start;
3249 it->continuation_lines_width = row->continuation_lines_width;
3250 CHECK_IT (it);
3254 /* Initialize IT for stepping through current_buffer in window W
3255 starting in the line following ROW, i.e. starting at ROW->end.
3256 Value is zero if there are overlay strings with newlines at ROW's
3257 end position. */
3259 static int
3260 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3262 int success = 0;
3264 if (init_from_display_pos (it, w, &row->end))
3266 if (row->continued_p)
3267 it->continuation_lines_width
3268 = row->continuation_lines_width + row->pixel_width;
3269 CHECK_IT (it);
3270 success = 1;
3273 return success;
3279 /***********************************************************************
3280 Text properties
3281 ***********************************************************************/
3283 /* Called when IT reaches IT->stop_charpos. Handle text property and
3284 overlay changes. Set IT->stop_charpos to the next position where
3285 to stop. */
3287 static void
3288 handle_stop (struct it *it)
3290 enum prop_handled handled;
3291 int handle_overlay_change_p;
3292 struct props *p;
3294 it->dpvec = NULL;
3295 it->current.dpvec_index = -1;
3296 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3297 it->ignore_overlay_strings_at_pos_p = 0;
3298 it->ellipsis_p = 0;
3300 /* Use face of preceding text for ellipsis (if invisible) */
3301 if (it->selective_display_ellipsis_p)
3302 it->saved_face_id = it->face_id;
3306 handled = HANDLED_NORMALLY;
3308 /* Call text property handlers. */
3309 for (p = it_props; p->handler; ++p)
3311 handled = p->handler (it);
3313 if (handled == HANDLED_RECOMPUTE_PROPS)
3314 break;
3315 else if (handled == HANDLED_RETURN)
3317 /* We still want to show before and after strings from
3318 overlays even if the actual buffer text is replaced. */
3319 if (!handle_overlay_change_p
3320 || it->sp > 1
3321 /* Don't call get_overlay_strings_1 if we already
3322 have overlay strings loaded, because doing so
3323 will load them again and push the iterator state
3324 onto the stack one more time, which is not
3325 expected by the rest of the code that processes
3326 overlay strings. */
3327 || (it->current.overlay_string_index < 0
3328 ? !get_overlay_strings_1 (it, 0, 0)
3329 : 0))
3331 if (it->ellipsis_p)
3332 setup_for_ellipsis (it, 0);
3333 /* When handling a display spec, we might load an
3334 empty string. In that case, discard it here. We
3335 used to discard it in handle_single_display_spec,
3336 but that causes get_overlay_strings_1, above, to
3337 ignore overlay strings that we must check. */
3338 if (STRINGP (it->string) && !SCHARS (it->string))
3339 pop_it (it);
3340 return;
3342 else if (STRINGP (it->string) && !SCHARS (it->string))
3343 pop_it (it);
3344 else
3346 it->ignore_overlay_strings_at_pos_p = 1;
3347 it->string_from_display_prop_p = 0;
3348 it->from_disp_prop_p = 0;
3349 handle_overlay_change_p = 0;
3351 handled = HANDLED_RECOMPUTE_PROPS;
3352 break;
3354 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3355 handle_overlay_change_p = 0;
3358 if (handled != HANDLED_RECOMPUTE_PROPS)
3360 /* Don't check for overlay strings below when set to deliver
3361 characters from a display vector. */
3362 if (it->method == GET_FROM_DISPLAY_VECTOR)
3363 handle_overlay_change_p = 0;
3365 /* Handle overlay changes.
3366 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3367 if it finds overlays. */
3368 if (handle_overlay_change_p)
3369 handled = handle_overlay_change (it);
3372 if (it->ellipsis_p)
3374 setup_for_ellipsis (it, 0);
3375 break;
3378 while (handled == HANDLED_RECOMPUTE_PROPS);
3380 /* Determine where to stop next. */
3381 if (handled == HANDLED_NORMALLY)
3382 compute_stop_pos (it);
3386 /* Compute IT->stop_charpos from text property and overlay change
3387 information for IT's current position. */
3389 static void
3390 compute_stop_pos (struct it *it)
3392 register INTERVAL iv, next_iv;
3393 Lisp_Object object, limit, position;
3394 ptrdiff_t charpos, bytepos;
3396 if (STRINGP (it->string))
3398 /* Strings are usually short, so don't limit the search for
3399 properties. */
3400 it->stop_charpos = it->end_charpos;
3401 object = it->string;
3402 limit = Qnil;
3403 charpos = IT_STRING_CHARPOS (*it);
3404 bytepos = IT_STRING_BYTEPOS (*it);
3406 else
3408 ptrdiff_t pos;
3410 /* If end_charpos is out of range for some reason, such as a
3411 misbehaving display function, rationalize it (Bug#5984). */
3412 if (it->end_charpos > ZV)
3413 it->end_charpos = ZV;
3414 it->stop_charpos = it->end_charpos;
3416 /* If next overlay change is in front of the current stop pos
3417 (which is IT->end_charpos), stop there. Note: value of
3418 next_overlay_change is point-max if no overlay change
3419 follows. */
3420 charpos = IT_CHARPOS (*it);
3421 bytepos = IT_BYTEPOS (*it);
3422 pos = next_overlay_change (charpos);
3423 if (pos < it->stop_charpos)
3424 it->stop_charpos = pos;
3426 /* If showing the region, we have to stop at the region
3427 start or end because the face might change there. */
3428 if (it->region_beg_charpos > 0)
3430 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3431 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3432 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3433 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3436 /* Set up variables for computing the stop position from text
3437 property changes. */
3438 XSETBUFFER (object, current_buffer);
3439 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3442 /* Get the interval containing IT's position. Value is a null
3443 interval if there isn't such an interval. */
3444 position = make_number (charpos);
3445 iv = validate_interval_range (object, &position, &position, 0);
3446 if (iv)
3448 Lisp_Object values_here[LAST_PROP_IDX];
3449 struct props *p;
3451 /* Get properties here. */
3452 for (p = it_props; p->handler; ++p)
3453 values_here[p->idx] = textget (iv->plist, *p->name);
3455 /* Look for an interval following iv that has different
3456 properties. */
3457 for (next_iv = next_interval (iv);
3458 (next_iv
3459 && (NILP (limit)
3460 || XFASTINT (limit) > next_iv->position));
3461 next_iv = next_interval (next_iv))
3463 for (p = it_props; p->handler; ++p)
3465 Lisp_Object new_value;
3467 new_value = textget (next_iv->plist, *p->name);
3468 if (!EQ (values_here[p->idx], new_value))
3469 break;
3472 if (p->handler)
3473 break;
3476 if (next_iv)
3478 if (INTEGERP (limit)
3479 && next_iv->position >= XFASTINT (limit))
3480 /* No text property change up to limit. */
3481 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3482 else
3483 /* Text properties change in next_iv. */
3484 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3488 if (it->cmp_it.id < 0)
3490 ptrdiff_t stoppos = it->end_charpos;
3492 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3493 stoppos = -1;
3494 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3495 stoppos, it->string);
3498 eassert (STRINGP (it->string)
3499 || (it->stop_charpos >= BEGV
3500 && it->stop_charpos >= IT_CHARPOS (*it)));
3504 /* Return the position of the next overlay change after POS in
3505 current_buffer. Value is point-max if no overlay change
3506 follows. This is like `next-overlay-change' but doesn't use
3507 xmalloc. */
3509 static ptrdiff_t
3510 next_overlay_change (ptrdiff_t pos)
3512 ptrdiff_t i, noverlays;
3513 ptrdiff_t endpos;
3514 Lisp_Object *overlays;
3516 /* Get all overlays at the given position. */
3517 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3519 /* If any of these overlays ends before endpos,
3520 use its ending point instead. */
3521 for (i = 0; i < noverlays; ++i)
3523 Lisp_Object oend;
3524 ptrdiff_t oendpos;
3526 oend = OVERLAY_END (overlays[i]);
3527 oendpos = OVERLAY_POSITION (oend);
3528 endpos = min (endpos, oendpos);
3531 return endpos;
3534 /* How many characters forward to search for a display property or
3535 display string. Searching too far forward makes the bidi display
3536 sluggish, especially in small windows. */
3537 #define MAX_DISP_SCAN 250
3539 /* Return the character position of a display string at or after
3540 position specified by POSITION. If no display string exists at or
3541 after POSITION, return ZV. A display string is either an overlay
3542 with `display' property whose value is a string, or a `display'
3543 text property whose value is a string. STRING is data about the
3544 string to iterate; if STRING->lstring is nil, we are iterating a
3545 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3546 on a GUI frame. DISP_PROP is set to zero if we searched
3547 MAX_DISP_SCAN characters forward without finding any display
3548 strings, non-zero otherwise. It is set to 2 if the display string
3549 uses any kind of `(space ...)' spec that will produce a stretch of
3550 white space in the text area. */
3551 ptrdiff_t
3552 compute_display_string_pos (struct text_pos *position,
3553 struct bidi_string_data *string,
3554 struct window *w,
3555 int frame_window_p, int *disp_prop)
3557 /* OBJECT = nil means current buffer. */
3558 Lisp_Object object, object1;
3559 Lisp_Object pos, spec, limpos;
3560 int string_p = (string && (STRINGP (string->lstring) || string->s));
3561 ptrdiff_t eob = string_p ? string->schars : ZV;
3562 ptrdiff_t begb = string_p ? 0 : BEGV;
3563 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3564 ptrdiff_t lim =
3565 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3566 struct text_pos tpos;
3567 int rv = 0;
3569 if (string && STRINGP (string->lstring))
3570 object1 = object = string->lstring;
3571 else if (w && !string_p)
3573 XSETWINDOW (object, w);
3574 object1 = Qnil;
3576 else
3577 object1 = object = Qnil;
3579 *disp_prop = 1;
3581 if (charpos >= eob
3582 /* We don't support display properties whose values are strings
3583 that have display string properties. */
3584 || string->from_disp_str
3585 /* C strings cannot have display properties. */
3586 || (string->s && !STRINGP (object)))
3588 *disp_prop = 0;
3589 return eob;
3592 /* If the character at CHARPOS is where the display string begins,
3593 return CHARPOS. */
3594 pos = make_number (charpos);
3595 if (STRINGP (object))
3596 bufpos = string->bufpos;
3597 else
3598 bufpos = charpos;
3599 tpos = *position;
3600 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3601 && (charpos <= begb
3602 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3603 object),
3604 spec))
3605 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3606 frame_window_p)))
3608 if (rv == 2)
3609 *disp_prop = 2;
3610 return charpos;
3613 /* Look forward for the first character with a `display' property
3614 that will replace the underlying text when displayed. */
3615 limpos = make_number (lim);
3616 do {
3617 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3618 CHARPOS (tpos) = XFASTINT (pos);
3619 if (CHARPOS (tpos) >= lim)
3621 *disp_prop = 0;
3622 break;
3624 if (STRINGP (object))
3625 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3626 else
3627 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3628 spec = Fget_char_property (pos, Qdisplay, object);
3629 if (!STRINGP (object))
3630 bufpos = CHARPOS (tpos);
3631 } while (NILP (spec)
3632 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3633 bufpos, frame_window_p)));
3634 if (rv == 2)
3635 *disp_prop = 2;
3637 return CHARPOS (tpos);
3640 /* Return the character position of the end of the display string that
3641 started at CHARPOS. If there's no display string at CHARPOS,
3642 return -1. A display string is either an overlay with `display'
3643 property whose value is a string or a `display' text property whose
3644 value is a string. */
3645 ptrdiff_t
3646 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3648 /* OBJECT = nil means current buffer. */
3649 Lisp_Object object =
3650 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3651 Lisp_Object pos = make_number (charpos);
3652 ptrdiff_t eob =
3653 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3655 if (charpos >= eob || (string->s && !STRINGP (object)))
3656 return eob;
3658 /* It could happen that the display property or overlay was removed
3659 since we found it in compute_display_string_pos above. One way
3660 this can happen is if JIT font-lock was called (through
3661 handle_fontified_prop), and jit-lock-functions remove text
3662 properties or overlays from the portion of buffer that includes
3663 CHARPOS. Muse mode is known to do that, for example. In this
3664 case, we return -1 to the caller, to signal that no display
3665 string is actually present at CHARPOS. See bidi_fetch_char for
3666 how this is handled.
3668 An alternative would be to never look for display properties past
3669 it->stop_charpos. But neither compute_display_string_pos nor
3670 bidi_fetch_char that calls it know or care where the next
3671 stop_charpos is. */
3672 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3673 return -1;
3675 /* Look forward for the first character where the `display' property
3676 changes. */
3677 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3679 return XFASTINT (pos);
3684 /***********************************************************************
3685 Fontification
3686 ***********************************************************************/
3688 /* Handle changes in the `fontified' property of the current buffer by
3689 calling hook functions from Qfontification_functions to fontify
3690 regions of text. */
3692 static enum prop_handled
3693 handle_fontified_prop (struct it *it)
3695 Lisp_Object prop, pos;
3696 enum prop_handled handled = HANDLED_NORMALLY;
3698 if (!NILP (Vmemory_full))
3699 return handled;
3701 /* Get the value of the `fontified' property at IT's current buffer
3702 position. (The `fontified' property doesn't have a special
3703 meaning in strings.) If the value is nil, call functions from
3704 Qfontification_functions. */
3705 if (!STRINGP (it->string)
3706 && it->s == NULL
3707 && !NILP (Vfontification_functions)
3708 && !NILP (Vrun_hooks)
3709 && (pos = make_number (IT_CHARPOS (*it)),
3710 prop = Fget_char_property (pos, Qfontified, Qnil),
3711 /* Ignore the special cased nil value always present at EOB since
3712 no amount of fontifying will be able to change it. */
3713 NILP (prop) && IT_CHARPOS (*it) < Z))
3715 ptrdiff_t count = SPECPDL_INDEX ();
3716 Lisp_Object val;
3717 struct buffer *obuf = current_buffer;
3718 int begv = BEGV, zv = ZV;
3719 int old_clip_changed = current_buffer->clip_changed;
3721 val = Vfontification_functions;
3722 specbind (Qfontification_functions, Qnil);
3724 eassert (it->end_charpos == ZV);
3726 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3727 safe_call1 (val, pos);
3728 else
3730 Lisp_Object fns, fn;
3731 struct gcpro gcpro1, gcpro2;
3733 fns = Qnil;
3734 GCPRO2 (val, fns);
3736 for (; CONSP (val); val = XCDR (val))
3738 fn = XCAR (val);
3740 if (EQ (fn, Qt))
3742 /* A value of t indicates this hook has a local
3743 binding; it means to run the global binding too.
3744 In a global value, t should not occur. If it
3745 does, we must ignore it to avoid an endless
3746 loop. */
3747 for (fns = Fdefault_value (Qfontification_functions);
3748 CONSP (fns);
3749 fns = XCDR (fns))
3751 fn = XCAR (fns);
3752 if (!EQ (fn, Qt))
3753 safe_call1 (fn, pos);
3756 else
3757 safe_call1 (fn, pos);
3760 UNGCPRO;
3763 unbind_to (count, Qnil);
3765 /* Fontification functions routinely call `save-restriction'.
3766 Normally, this tags clip_changed, which can confuse redisplay
3767 (see discussion in Bug#6671). Since we don't perform any
3768 special handling of fontification changes in the case where
3769 `save-restriction' isn't called, there's no point doing so in
3770 this case either. So, if the buffer's restrictions are
3771 actually left unchanged, reset clip_changed. */
3772 if (obuf == current_buffer)
3774 if (begv == BEGV && zv == ZV)
3775 current_buffer->clip_changed = old_clip_changed;
3777 /* There isn't much we can reasonably do to protect against
3778 misbehaving fontification, but here's a fig leaf. */
3779 else if (BUFFER_LIVE_P (obuf))
3780 set_buffer_internal_1 (obuf);
3782 /* The fontification code may have added/removed text.
3783 It could do even a lot worse, but let's at least protect against
3784 the most obvious case where only the text past `pos' gets changed',
3785 as is/was done in grep.el where some escapes sequences are turned
3786 into face properties (bug#7876). */
3787 it->end_charpos = ZV;
3789 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3790 something. This avoids an endless loop if they failed to
3791 fontify the text for which reason ever. */
3792 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3793 handled = HANDLED_RECOMPUTE_PROPS;
3796 return handled;
3801 /***********************************************************************
3802 Faces
3803 ***********************************************************************/
3805 /* Set up iterator IT from face properties at its current position.
3806 Called from handle_stop. */
3808 static enum prop_handled
3809 handle_face_prop (struct it *it)
3811 int new_face_id;
3812 ptrdiff_t next_stop;
3814 if (!STRINGP (it->string))
3816 new_face_id
3817 = face_at_buffer_position (it->w,
3818 IT_CHARPOS (*it),
3819 it->region_beg_charpos,
3820 it->region_end_charpos,
3821 &next_stop,
3822 (IT_CHARPOS (*it)
3823 + TEXT_PROP_DISTANCE_LIMIT),
3824 0, it->base_face_id);
3826 /* Is this a start of a run of characters with box face?
3827 Caveat: this can be called for a freshly initialized
3828 iterator; face_id is -1 in this case. We know that the new
3829 face will not change until limit, i.e. if the new face has a
3830 box, all characters up to limit will have one. But, as
3831 usual, we don't know whether limit is really the end. */
3832 if (new_face_id != it->face_id)
3834 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3835 /* If it->face_id is -1, old_face below will be NULL, see
3836 the definition of FACE_FROM_ID. This will happen if this
3837 is the initial call that gets the face. */
3838 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3840 /* If the value of face_id of the iterator is -1, we have to
3841 look in front of IT's position and see whether there is a
3842 face there that's different from new_face_id. */
3843 if (!old_face && IT_CHARPOS (*it) > BEG)
3845 int prev_face_id = face_before_it_pos (it);
3847 old_face = FACE_FROM_ID (it->f, prev_face_id);
3850 /* If the new face has a box, but the old face does not,
3851 this is the start of a run of characters with box face,
3852 i.e. this character has a shadow on the left side. */
3853 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3854 && (old_face == NULL || !old_face->box));
3855 it->face_box_p = new_face->box != FACE_NO_BOX;
3858 else
3860 int base_face_id;
3861 ptrdiff_t bufpos;
3862 int i;
3863 Lisp_Object from_overlay
3864 = (it->current.overlay_string_index >= 0
3865 ? it->string_overlays[it->current.overlay_string_index
3866 % OVERLAY_STRING_CHUNK_SIZE]
3867 : Qnil);
3869 /* See if we got to this string directly or indirectly from
3870 an overlay property. That includes the before-string or
3871 after-string of an overlay, strings in display properties
3872 provided by an overlay, their text properties, etc.
3874 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3875 if (! NILP (from_overlay))
3876 for (i = it->sp - 1; i >= 0; i--)
3878 if (it->stack[i].current.overlay_string_index >= 0)
3879 from_overlay
3880 = it->string_overlays[it->stack[i].current.overlay_string_index
3881 % OVERLAY_STRING_CHUNK_SIZE];
3882 else if (! NILP (it->stack[i].from_overlay))
3883 from_overlay = it->stack[i].from_overlay;
3885 if (!NILP (from_overlay))
3886 break;
3889 if (! NILP (from_overlay))
3891 bufpos = IT_CHARPOS (*it);
3892 /* For a string from an overlay, the base face depends
3893 only on text properties and ignores overlays. */
3894 base_face_id
3895 = face_for_overlay_string (it->w,
3896 IT_CHARPOS (*it),
3897 it->region_beg_charpos,
3898 it->region_end_charpos,
3899 &next_stop,
3900 (IT_CHARPOS (*it)
3901 + TEXT_PROP_DISTANCE_LIMIT),
3903 from_overlay);
3905 else
3907 bufpos = 0;
3909 /* For strings from a `display' property, use the face at
3910 IT's current buffer position as the base face to merge
3911 with, so that overlay strings appear in the same face as
3912 surrounding text, unless they specify their own
3913 faces. */
3914 base_face_id = it->string_from_prefix_prop_p
3915 ? DEFAULT_FACE_ID
3916 : underlying_face_id (it);
3919 new_face_id = face_at_string_position (it->w,
3920 it->string,
3921 IT_STRING_CHARPOS (*it),
3922 bufpos,
3923 it->region_beg_charpos,
3924 it->region_end_charpos,
3925 &next_stop,
3926 base_face_id, 0);
3928 /* Is this a start of a run of characters with box? Caveat:
3929 this can be called for a freshly allocated iterator; face_id
3930 is -1 is this case. We know that the new face will not
3931 change until the next check pos, i.e. if the new face has a
3932 box, all characters up to that position will have a
3933 box. But, as usual, we don't know whether that position
3934 is really the end. */
3935 if (new_face_id != it->face_id)
3937 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3938 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3940 /* If new face has a box but old face hasn't, this is the
3941 start of a run of characters with box, i.e. it has a
3942 shadow on the left side. */
3943 it->start_of_box_run_p
3944 = new_face->box && (old_face == NULL || !old_face->box);
3945 it->face_box_p = new_face->box != FACE_NO_BOX;
3949 it->face_id = new_face_id;
3950 return HANDLED_NORMALLY;
3954 /* Return the ID of the face ``underlying'' IT's current position,
3955 which is in a string. If the iterator is associated with a
3956 buffer, return the face at IT's current buffer position.
3957 Otherwise, use the iterator's base_face_id. */
3959 static int
3960 underlying_face_id (struct it *it)
3962 int face_id = it->base_face_id, i;
3964 eassert (STRINGP (it->string));
3966 for (i = it->sp - 1; i >= 0; --i)
3967 if (NILP (it->stack[i].string))
3968 face_id = it->stack[i].face_id;
3970 return face_id;
3974 /* Compute the face one character before or after the current position
3975 of IT, in the visual order. BEFORE_P non-zero means get the face
3976 in front (to the left in L2R paragraphs, to the right in R2L
3977 paragraphs) of IT's screen position. Value is the ID of the face. */
3979 static int
3980 face_before_or_after_it_pos (struct it *it, int before_p)
3982 int face_id, limit;
3983 ptrdiff_t next_check_charpos;
3984 struct it it_copy;
3985 void *it_copy_data = NULL;
3987 eassert (it->s == NULL);
3989 if (STRINGP (it->string))
3991 ptrdiff_t bufpos, charpos;
3992 int base_face_id;
3994 /* No face change past the end of the string (for the case
3995 we are padding with spaces). No face change before the
3996 string start. */
3997 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3998 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3999 return it->face_id;
4001 if (!it->bidi_p)
4003 /* Set charpos to the position before or after IT's current
4004 position, in the logical order, which in the non-bidi
4005 case is the same as the visual order. */
4006 if (before_p)
4007 charpos = IT_STRING_CHARPOS (*it) - 1;
4008 else if (it->what == IT_COMPOSITION)
4009 /* For composition, we must check the character after the
4010 composition. */
4011 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4012 else
4013 charpos = IT_STRING_CHARPOS (*it) + 1;
4015 else
4017 if (before_p)
4019 /* With bidi iteration, the character before the current
4020 in the visual order cannot be found by simple
4021 iteration, because "reverse" reordering is not
4022 supported. Instead, we need to use the move_it_*
4023 family of functions. */
4024 /* Ignore face changes before the first visible
4025 character on this display line. */
4026 if (it->current_x <= it->first_visible_x)
4027 return it->face_id;
4028 SAVE_IT (it_copy, *it, it_copy_data);
4029 /* Implementation note: Since move_it_in_display_line
4030 works in the iterator geometry, and thinks the first
4031 character is always the leftmost, even in R2L lines,
4032 we don't need to distinguish between the R2L and L2R
4033 cases here. */
4034 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4035 it_copy.current_x - 1, MOVE_TO_X);
4036 charpos = IT_STRING_CHARPOS (it_copy);
4037 RESTORE_IT (it, it, it_copy_data);
4039 else
4041 /* Set charpos to the string position of the character
4042 that comes after IT's current position in the visual
4043 order. */
4044 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4046 it_copy = *it;
4047 while (n--)
4048 bidi_move_to_visually_next (&it_copy.bidi_it);
4050 charpos = it_copy.bidi_it.charpos;
4053 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4055 if (it->current.overlay_string_index >= 0)
4056 bufpos = IT_CHARPOS (*it);
4057 else
4058 bufpos = 0;
4060 base_face_id = underlying_face_id (it);
4062 /* Get the face for ASCII, or unibyte. */
4063 face_id = face_at_string_position (it->w,
4064 it->string,
4065 charpos,
4066 bufpos,
4067 it->region_beg_charpos,
4068 it->region_end_charpos,
4069 &next_check_charpos,
4070 base_face_id, 0);
4072 /* Correct the face for charsets different from ASCII. Do it
4073 for the multibyte case only. The face returned above is
4074 suitable for unibyte text if IT->string is unibyte. */
4075 if (STRING_MULTIBYTE (it->string))
4077 struct text_pos pos1 = string_pos (charpos, it->string);
4078 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4079 int c, len;
4080 struct face *face = FACE_FROM_ID (it->f, face_id);
4082 c = string_char_and_length (p, &len);
4083 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4086 else
4088 struct text_pos pos;
4090 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4091 || (IT_CHARPOS (*it) <= BEGV && before_p))
4092 return it->face_id;
4094 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4095 pos = it->current.pos;
4097 if (!it->bidi_p)
4099 if (before_p)
4100 DEC_TEXT_POS (pos, it->multibyte_p);
4101 else
4103 if (it->what == IT_COMPOSITION)
4105 /* For composition, we must check the position after
4106 the composition. */
4107 pos.charpos += it->cmp_it.nchars;
4108 pos.bytepos += it->len;
4110 else
4111 INC_TEXT_POS (pos, it->multibyte_p);
4114 else
4116 if (before_p)
4118 /* With bidi iteration, the character before the current
4119 in the visual order cannot be found by simple
4120 iteration, because "reverse" reordering is not
4121 supported. Instead, we need to use the move_it_*
4122 family of functions. */
4123 /* Ignore face changes before the first visible
4124 character on this display line. */
4125 if (it->current_x <= it->first_visible_x)
4126 return it->face_id;
4127 SAVE_IT (it_copy, *it, it_copy_data);
4128 /* Implementation note: Since move_it_in_display_line
4129 works in the iterator geometry, and thinks the first
4130 character is always the leftmost, even in R2L lines,
4131 we don't need to distinguish between the R2L and L2R
4132 cases here. */
4133 move_it_in_display_line (&it_copy, ZV,
4134 it_copy.current_x - 1, MOVE_TO_X);
4135 pos = it_copy.current.pos;
4136 RESTORE_IT (it, it, it_copy_data);
4138 else
4140 /* Set charpos to the buffer position of the character
4141 that comes after IT's current position in the visual
4142 order. */
4143 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4145 it_copy = *it;
4146 while (n--)
4147 bidi_move_to_visually_next (&it_copy.bidi_it);
4149 SET_TEXT_POS (pos,
4150 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4153 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4155 /* Determine face for CHARSET_ASCII, or unibyte. */
4156 face_id = face_at_buffer_position (it->w,
4157 CHARPOS (pos),
4158 it->region_beg_charpos,
4159 it->region_end_charpos,
4160 &next_check_charpos,
4161 limit, 0, -1);
4163 /* Correct the face for charsets different from ASCII. Do it
4164 for the multibyte case only. The face returned above is
4165 suitable for unibyte text if current_buffer is unibyte. */
4166 if (it->multibyte_p)
4168 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4169 struct face *face = FACE_FROM_ID (it->f, face_id);
4170 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4174 return face_id;
4179 /***********************************************************************
4180 Invisible text
4181 ***********************************************************************/
4183 /* Set up iterator IT from invisible properties at its current
4184 position. Called from handle_stop. */
4186 static enum prop_handled
4187 handle_invisible_prop (struct it *it)
4189 enum prop_handled handled = HANDLED_NORMALLY;
4190 int invis_p;
4191 Lisp_Object prop;
4193 if (STRINGP (it->string))
4195 Lisp_Object end_charpos, limit, charpos;
4197 /* Get the value of the invisible text property at the
4198 current position. Value will be nil if there is no such
4199 property. */
4200 charpos = make_number (IT_STRING_CHARPOS (*it));
4201 prop = Fget_text_property (charpos, Qinvisible, it->string);
4202 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4204 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4206 /* Record whether we have to display an ellipsis for the
4207 invisible text. */
4208 int display_ellipsis_p = (invis_p == 2);
4209 ptrdiff_t len, endpos;
4211 handled = HANDLED_RECOMPUTE_PROPS;
4213 /* Get the position at which the next visible text can be
4214 found in IT->string, if any. */
4215 endpos = len = SCHARS (it->string);
4216 XSETINT (limit, len);
4219 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4220 it->string, limit);
4221 if (INTEGERP (end_charpos))
4223 endpos = XFASTINT (end_charpos);
4224 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4225 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4226 if (invis_p == 2)
4227 display_ellipsis_p = 1;
4230 while (invis_p && endpos < len);
4232 if (display_ellipsis_p)
4233 it->ellipsis_p = 1;
4235 if (endpos < len)
4237 /* Text at END_CHARPOS is visible. Move IT there. */
4238 struct text_pos old;
4239 ptrdiff_t oldpos;
4241 old = it->current.string_pos;
4242 oldpos = CHARPOS (old);
4243 if (it->bidi_p)
4245 if (it->bidi_it.first_elt
4246 && it->bidi_it.charpos < SCHARS (it->string))
4247 bidi_paragraph_init (it->paragraph_embedding,
4248 &it->bidi_it, 1);
4249 /* Bidi-iterate out of the invisible text. */
4252 bidi_move_to_visually_next (&it->bidi_it);
4254 while (oldpos <= it->bidi_it.charpos
4255 && it->bidi_it.charpos < endpos);
4257 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4258 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4259 if (IT_CHARPOS (*it) >= endpos)
4260 it->prev_stop = endpos;
4262 else
4264 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4265 compute_string_pos (&it->current.string_pos, old, it->string);
4268 else
4270 /* The rest of the string is invisible. If this is an
4271 overlay string, proceed with the next overlay string
4272 or whatever comes and return a character from there. */
4273 if (it->current.overlay_string_index >= 0
4274 && !display_ellipsis_p)
4276 next_overlay_string (it);
4277 /* Don't check for overlay strings when we just
4278 finished processing them. */
4279 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4281 else
4283 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4284 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4289 else
4291 ptrdiff_t newpos, next_stop, start_charpos, tem;
4292 Lisp_Object pos, overlay;
4294 /* First of all, is there invisible text at this position? */
4295 tem = start_charpos = IT_CHARPOS (*it);
4296 pos = make_number (tem);
4297 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4298 &overlay);
4299 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4301 /* If we are on invisible text, skip over it. */
4302 if (invis_p && start_charpos < it->end_charpos)
4304 /* Record whether we have to display an ellipsis for the
4305 invisible text. */
4306 int display_ellipsis_p = invis_p == 2;
4308 handled = HANDLED_RECOMPUTE_PROPS;
4310 /* Loop skipping over invisible text. The loop is left at
4311 ZV or with IT on the first char being visible again. */
4314 /* Try to skip some invisible text. Return value is the
4315 position reached which can be equal to where we start
4316 if there is nothing invisible there. This skips both
4317 over invisible text properties and overlays with
4318 invisible property. */
4319 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4321 /* If we skipped nothing at all we weren't at invisible
4322 text in the first place. If everything to the end of
4323 the buffer was skipped, end the loop. */
4324 if (newpos == tem || newpos >= ZV)
4325 invis_p = 0;
4326 else
4328 /* We skipped some characters but not necessarily
4329 all there are. Check if we ended up on visible
4330 text. Fget_char_property returns the property of
4331 the char before the given position, i.e. if we
4332 get invis_p = 0, this means that the char at
4333 newpos is visible. */
4334 pos = make_number (newpos);
4335 prop = Fget_char_property (pos, Qinvisible, it->window);
4336 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4339 /* If we ended up on invisible text, proceed to
4340 skip starting with next_stop. */
4341 if (invis_p)
4342 tem = next_stop;
4344 /* If there are adjacent invisible texts, don't lose the
4345 second one's ellipsis. */
4346 if (invis_p == 2)
4347 display_ellipsis_p = 1;
4349 while (invis_p);
4351 /* The position newpos is now either ZV or on visible text. */
4352 if (it->bidi_p)
4354 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4355 int on_newline =
4356 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4357 int after_newline =
4358 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4360 /* If the invisible text ends on a newline or on a
4361 character after a newline, we can avoid the costly,
4362 character by character, bidi iteration to NEWPOS, and
4363 instead simply reseat the iterator there. That's
4364 because all bidi reordering information is tossed at
4365 the newline. This is a big win for modes that hide
4366 complete lines, like Outline, Org, etc. */
4367 if (on_newline || after_newline)
4369 struct text_pos tpos;
4370 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4372 SET_TEXT_POS (tpos, newpos, bpos);
4373 reseat_1 (it, tpos, 0);
4374 /* If we reseat on a newline/ZV, we need to prep the
4375 bidi iterator for advancing to the next character
4376 after the newline/EOB, keeping the current paragraph
4377 direction (so that PRODUCE_GLYPHS does TRT wrt
4378 prepending/appending glyphs to a glyph row). */
4379 if (on_newline)
4381 it->bidi_it.first_elt = 0;
4382 it->bidi_it.paragraph_dir = pdir;
4383 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4384 it->bidi_it.nchars = 1;
4385 it->bidi_it.ch_len = 1;
4388 else /* Must use the slow method. */
4390 /* With bidi iteration, the region of invisible text
4391 could start and/or end in the middle of a
4392 non-base embedding level. Therefore, we need to
4393 skip invisible text using the bidi iterator,
4394 starting at IT's current position, until we find
4395 ourselves outside of the invisible text.
4396 Skipping invisible text _after_ bidi iteration
4397 avoids affecting the visual order of the
4398 displayed text when invisible properties are
4399 added or removed. */
4400 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4402 /* If we were `reseat'ed to a new paragraph,
4403 determine the paragraph base direction. We
4404 need to do it now because
4405 next_element_from_buffer may not have a
4406 chance to do it, if we are going to skip any
4407 text at the beginning, which resets the
4408 FIRST_ELT flag. */
4409 bidi_paragraph_init (it->paragraph_embedding,
4410 &it->bidi_it, 1);
4414 bidi_move_to_visually_next (&it->bidi_it);
4416 while (it->stop_charpos <= it->bidi_it.charpos
4417 && it->bidi_it.charpos < newpos);
4418 IT_CHARPOS (*it) = it->bidi_it.charpos;
4419 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4420 /* If we overstepped NEWPOS, record its position in
4421 the iterator, so that we skip invisible text if
4422 later the bidi iteration lands us in the
4423 invisible region again. */
4424 if (IT_CHARPOS (*it) >= newpos)
4425 it->prev_stop = newpos;
4428 else
4430 IT_CHARPOS (*it) = newpos;
4431 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4434 /* If there are before-strings at the start of invisible
4435 text, and the text is invisible because of a text
4436 property, arrange to show before-strings because 20.x did
4437 it that way. (If the text is invisible because of an
4438 overlay property instead of a text property, this is
4439 already handled in the overlay code.) */
4440 if (NILP (overlay)
4441 && get_overlay_strings (it, it->stop_charpos))
4443 handled = HANDLED_RECOMPUTE_PROPS;
4444 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4446 else if (display_ellipsis_p)
4448 /* Make sure that the glyphs of the ellipsis will get
4449 correct `charpos' values. If we would not update
4450 it->position here, the glyphs would belong to the
4451 last visible character _before_ the invisible
4452 text, which confuses `set_cursor_from_row'.
4454 We use the last invisible position instead of the
4455 first because this way the cursor is always drawn on
4456 the first "." of the ellipsis, whenever PT is inside
4457 the invisible text. Otherwise the cursor would be
4458 placed _after_ the ellipsis when the point is after the
4459 first invisible character. */
4460 if (!STRINGP (it->object))
4462 it->position.charpos = newpos - 1;
4463 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4465 it->ellipsis_p = 1;
4466 /* Let the ellipsis display before
4467 considering any properties of the following char.
4468 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4469 handled = HANDLED_RETURN;
4474 return handled;
4478 /* Make iterator IT return `...' next.
4479 Replaces LEN characters from buffer. */
4481 static void
4482 setup_for_ellipsis (struct it *it, int len)
4484 /* Use the display table definition for `...'. Invalid glyphs
4485 will be handled by the method returning elements from dpvec. */
4486 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4488 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4489 it->dpvec = v->contents;
4490 it->dpend = v->contents + v->header.size;
4492 else
4494 /* Default `...'. */
4495 it->dpvec = default_invis_vector;
4496 it->dpend = default_invis_vector + 3;
4499 it->dpvec_char_len = len;
4500 it->current.dpvec_index = 0;
4501 it->dpvec_face_id = -1;
4503 /* Remember the current face id in case glyphs specify faces.
4504 IT's face is restored in set_iterator_to_next.
4505 saved_face_id was set to preceding char's face in handle_stop. */
4506 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4507 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4509 it->method = GET_FROM_DISPLAY_VECTOR;
4510 it->ellipsis_p = 1;
4515 /***********************************************************************
4516 'display' property
4517 ***********************************************************************/
4519 /* Set up iterator IT from `display' property at its current position.
4520 Called from handle_stop.
4521 We return HANDLED_RETURN if some part of the display property
4522 overrides the display of the buffer text itself.
4523 Otherwise we return HANDLED_NORMALLY. */
4525 static enum prop_handled
4526 handle_display_prop (struct it *it)
4528 Lisp_Object propval, object, overlay;
4529 struct text_pos *position;
4530 ptrdiff_t bufpos;
4531 /* Nonzero if some property replaces the display of the text itself. */
4532 int display_replaced_p = 0;
4534 if (STRINGP (it->string))
4536 object = it->string;
4537 position = &it->current.string_pos;
4538 bufpos = CHARPOS (it->current.pos);
4540 else
4542 XSETWINDOW (object, it->w);
4543 position = &it->current.pos;
4544 bufpos = CHARPOS (*position);
4547 /* Reset those iterator values set from display property values. */
4548 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4549 it->space_width = Qnil;
4550 it->font_height = Qnil;
4551 it->voffset = 0;
4553 /* We don't support recursive `display' properties, i.e. string
4554 values that have a string `display' property, that have a string
4555 `display' property etc. */
4556 if (!it->string_from_display_prop_p)
4557 it->area = TEXT_AREA;
4559 propval = get_char_property_and_overlay (make_number (position->charpos),
4560 Qdisplay, object, &overlay);
4561 if (NILP (propval))
4562 return HANDLED_NORMALLY;
4563 /* Now OVERLAY is the overlay that gave us this property, or nil
4564 if it was a text property. */
4566 if (!STRINGP (it->string))
4567 object = it->w->contents;
4569 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4570 position, bufpos,
4571 FRAME_WINDOW_P (it->f));
4573 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4576 /* Subroutine of handle_display_prop. Returns non-zero if the display
4577 specification in SPEC is a replacing specification, i.e. it would
4578 replace the text covered by `display' property with something else,
4579 such as an image or a display string. If SPEC includes any kind or
4580 `(space ...) specification, the value is 2; this is used by
4581 compute_display_string_pos, which see.
4583 See handle_single_display_spec for documentation of arguments.
4584 frame_window_p is non-zero if the window being redisplayed is on a
4585 GUI frame; this argument is used only if IT is NULL, see below.
4587 IT can be NULL, if this is called by the bidi reordering code
4588 through compute_display_string_pos, which see. In that case, this
4589 function only examines SPEC, but does not otherwise "handle" it, in
4590 the sense that it doesn't set up members of IT from the display
4591 spec. */
4592 static int
4593 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4594 Lisp_Object overlay, struct text_pos *position,
4595 ptrdiff_t bufpos, int frame_window_p)
4597 int replacing_p = 0;
4598 int rv;
4600 if (CONSP (spec)
4601 /* Simple specifications. */
4602 && !EQ (XCAR (spec), Qimage)
4603 && !EQ (XCAR (spec), Qspace)
4604 && !EQ (XCAR (spec), Qwhen)
4605 && !EQ (XCAR (spec), Qslice)
4606 && !EQ (XCAR (spec), Qspace_width)
4607 && !EQ (XCAR (spec), Qheight)
4608 && !EQ (XCAR (spec), Qraise)
4609 /* Marginal area specifications. */
4610 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4611 && !EQ (XCAR (spec), Qleft_fringe)
4612 && !EQ (XCAR (spec), Qright_fringe)
4613 && !NILP (XCAR (spec)))
4615 for (; CONSP (spec); spec = XCDR (spec))
4617 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4618 overlay, position, bufpos,
4619 replacing_p, frame_window_p)))
4621 replacing_p = rv;
4622 /* If some text in a string is replaced, `position' no
4623 longer points to the position of `object'. */
4624 if (!it || STRINGP (object))
4625 break;
4629 else if (VECTORP (spec))
4631 ptrdiff_t i;
4632 for (i = 0; i < ASIZE (spec); ++i)
4633 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4634 overlay, position, bufpos,
4635 replacing_p, frame_window_p)))
4637 replacing_p = rv;
4638 /* If some text in a string is replaced, `position' no
4639 longer points to the position of `object'. */
4640 if (!it || STRINGP (object))
4641 break;
4644 else
4646 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4647 position, bufpos, 0,
4648 frame_window_p)))
4649 replacing_p = rv;
4652 return replacing_p;
4655 /* Value is the position of the end of the `display' property starting
4656 at START_POS in OBJECT. */
4658 static struct text_pos
4659 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4661 Lisp_Object end;
4662 struct text_pos end_pos;
4664 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4665 Qdisplay, object, Qnil);
4666 CHARPOS (end_pos) = XFASTINT (end);
4667 if (STRINGP (object))
4668 compute_string_pos (&end_pos, start_pos, it->string);
4669 else
4670 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4672 return end_pos;
4676 /* Set up IT from a single `display' property specification SPEC. OBJECT
4677 is the object in which the `display' property was found. *POSITION
4678 is the position in OBJECT at which the `display' property was found.
4679 BUFPOS is the buffer position of OBJECT (different from POSITION if
4680 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4681 previously saw a display specification which already replaced text
4682 display with something else, for example an image; we ignore such
4683 properties after the first one has been processed.
4685 OVERLAY is the overlay this `display' property came from,
4686 or nil if it was a text property.
4688 If SPEC is a `space' or `image' specification, and in some other
4689 cases too, set *POSITION to the position where the `display'
4690 property ends.
4692 If IT is NULL, only examine the property specification in SPEC, but
4693 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4694 is intended to be displayed in a window on a GUI frame.
4696 Value is non-zero if something was found which replaces the display
4697 of buffer or string text. */
4699 static int
4700 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4701 Lisp_Object overlay, struct text_pos *position,
4702 ptrdiff_t bufpos, int display_replaced_p,
4703 int frame_window_p)
4705 Lisp_Object form;
4706 Lisp_Object location, value;
4707 struct text_pos start_pos = *position;
4708 int valid_p;
4710 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4711 If the result is non-nil, use VALUE instead of SPEC. */
4712 form = Qt;
4713 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4715 spec = XCDR (spec);
4716 if (!CONSP (spec))
4717 return 0;
4718 form = XCAR (spec);
4719 spec = XCDR (spec);
4722 if (!NILP (form) && !EQ (form, Qt))
4724 ptrdiff_t count = SPECPDL_INDEX ();
4725 struct gcpro gcpro1;
4727 /* Bind `object' to the object having the `display' property, a
4728 buffer or string. Bind `position' to the position in the
4729 object where the property was found, and `buffer-position'
4730 to the current position in the buffer. */
4732 if (NILP (object))
4733 XSETBUFFER (object, current_buffer);
4734 specbind (Qobject, object);
4735 specbind (Qposition, make_number (CHARPOS (*position)));
4736 specbind (Qbuffer_position, make_number (bufpos));
4737 GCPRO1 (form);
4738 form = safe_eval (form);
4739 UNGCPRO;
4740 unbind_to (count, Qnil);
4743 if (NILP (form))
4744 return 0;
4746 /* Handle `(height HEIGHT)' specifications. */
4747 if (CONSP (spec)
4748 && EQ (XCAR (spec), Qheight)
4749 && CONSP (XCDR (spec)))
4751 if (it)
4753 if (!FRAME_WINDOW_P (it->f))
4754 return 0;
4756 it->font_height = XCAR (XCDR (spec));
4757 if (!NILP (it->font_height))
4759 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4760 int new_height = -1;
4762 if (CONSP (it->font_height)
4763 && (EQ (XCAR (it->font_height), Qplus)
4764 || EQ (XCAR (it->font_height), Qminus))
4765 && CONSP (XCDR (it->font_height))
4766 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4768 /* `(+ N)' or `(- N)' where N is an integer. */
4769 int steps = XINT (XCAR (XCDR (it->font_height)));
4770 if (EQ (XCAR (it->font_height), Qplus))
4771 steps = - steps;
4772 it->face_id = smaller_face (it->f, it->face_id, steps);
4774 else if (FUNCTIONP (it->font_height))
4776 /* Call function with current height as argument.
4777 Value is the new height. */
4778 Lisp_Object height;
4779 height = safe_call1 (it->font_height,
4780 face->lface[LFACE_HEIGHT_INDEX]);
4781 if (NUMBERP (height))
4782 new_height = XFLOATINT (height);
4784 else if (NUMBERP (it->font_height))
4786 /* Value is a multiple of the canonical char height. */
4787 struct face *f;
4789 f = FACE_FROM_ID (it->f,
4790 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4791 new_height = (XFLOATINT (it->font_height)
4792 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4794 else
4796 /* Evaluate IT->font_height with `height' bound to the
4797 current specified height to get the new height. */
4798 ptrdiff_t count = SPECPDL_INDEX ();
4800 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4801 value = safe_eval (it->font_height);
4802 unbind_to (count, Qnil);
4804 if (NUMBERP (value))
4805 new_height = XFLOATINT (value);
4808 if (new_height > 0)
4809 it->face_id = face_with_height (it->f, it->face_id, new_height);
4813 return 0;
4816 /* Handle `(space-width WIDTH)'. */
4817 if (CONSP (spec)
4818 && EQ (XCAR (spec), Qspace_width)
4819 && CONSP (XCDR (spec)))
4821 if (it)
4823 if (!FRAME_WINDOW_P (it->f))
4824 return 0;
4826 value = XCAR (XCDR (spec));
4827 if (NUMBERP (value) && XFLOATINT (value) > 0)
4828 it->space_width = value;
4831 return 0;
4834 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4835 if (CONSP (spec)
4836 && EQ (XCAR (spec), Qslice))
4838 Lisp_Object tem;
4840 if (it)
4842 if (!FRAME_WINDOW_P (it->f))
4843 return 0;
4845 if (tem = XCDR (spec), CONSP (tem))
4847 it->slice.x = XCAR (tem);
4848 if (tem = XCDR (tem), CONSP (tem))
4850 it->slice.y = XCAR (tem);
4851 if (tem = XCDR (tem), CONSP (tem))
4853 it->slice.width = XCAR (tem);
4854 if (tem = XCDR (tem), CONSP (tem))
4855 it->slice.height = XCAR (tem);
4861 return 0;
4864 /* Handle `(raise FACTOR)'. */
4865 if (CONSP (spec)
4866 && EQ (XCAR (spec), Qraise)
4867 && CONSP (XCDR (spec)))
4869 if (it)
4871 if (!FRAME_WINDOW_P (it->f))
4872 return 0;
4874 #ifdef HAVE_WINDOW_SYSTEM
4875 value = XCAR (XCDR (spec));
4876 if (NUMBERP (value))
4878 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4879 it->voffset = - (XFLOATINT (value)
4880 * (FONT_HEIGHT (face->font)));
4882 #endif /* HAVE_WINDOW_SYSTEM */
4885 return 0;
4888 /* Don't handle the other kinds of display specifications
4889 inside a string that we got from a `display' property. */
4890 if (it && it->string_from_display_prop_p)
4891 return 0;
4893 /* Characters having this form of property are not displayed, so
4894 we have to find the end of the property. */
4895 if (it)
4897 start_pos = *position;
4898 *position = display_prop_end (it, object, start_pos);
4900 value = Qnil;
4902 /* Stop the scan at that end position--we assume that all
4903 text properties change there. */
4904 if (it)
4905 it->stop_charpos = position->charpos;
4907 /* Handle `(left-fringe BITMAP [FACE])'
4908 and `(right-fringe BITMAP [FACE])'. */
4909 if (CONSP (spec)
4910 && (EQ (XCAR (spec), Qleft_fringe)
4911 || EQ (XCAR (spec), Qright_fringe))
4912 && CONSP (XCDR (spec)))
4914 int fringe_bitmap;
4916 if (it)
4918 if (!FRAME_WINDOW_P (it->f))
4919 /* If we return here, POSITION has been advanced
4920 across the text with this property. */
4922 /* Synchronize the bidi iterator with POSITION. This is
4923 needed because we are not going to push the iterator
4924 on behalf of this display property, so there will be
4925 no pop_it call to do this synchronization for us. */
4926 if (it->bidi_p)
4928 it->position = *position;
4929 iterate_out_of_display_property (it);
4930 *position = it->position;
4932 return 1;
4935 else if (!frame_window_p)
4936 return 1;
4938 #ifdef HAVE_WINDOW_SYSTEM
4939 value = XCAR (XCDR (spec));
4940 if (!SYMBOLP (value)
4941 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4942 /* If we return here, POSITION has been advanced
4943 across the text with this property. */
4945 if (it && it->bidi_p)
4947 it->position = *position;
4948 iterate_out_of_display_property (it);
4949 *position = it->position;
4951 return 1;
4954 if (it)
4956 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4958 if (CONSP (XCDR (XCDR (spec))))
4960 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4961 int face_id2 = lookup_derived_face (it->f, face_name,
4962 FRINGE_FACE_ID, 0);
4963 if (face_id2 >= 0)
4964 face_id = face_id2;
4967 /* Save current settings of IT so that we can restore them
4968 when we are finished with the glyph property value. */
4969 push_it (it, position);
4971 it->area = TEXT_AREA;
4972 it->what = IT_IMAGE;
4973 it->image_id = -1; /* no image */
4974 it->position = start_pos;
4975 it->object = NILP (object) ? it->w->contents : object;
4976 it->method = GET_FROM_IMAGE;
4977 it->from_overlay = Qnil;
4978 it->face_id = face_id;
4979 it->from_disp_prop_p = 1;
4981 /* Say that we haven't consumed the characters with
4982 `display' property yet. The call to pop_it in
4983 set_iterator_to_next will clean this up. */
4984 *position = start_pos;
4986 if (EQ (XCAR (spec), Qleft_fringe))
4988 it->left_user_fringe_bitmap = fringe_bitmap;
4989 it->left_user_fringe_face_id = face_id;
4991 else
4993 it->right_user_fringe_bitmap = fringe_bitmap;
4994 it->right_user_fringe_face_id = face_id;
4997 #endif /* HAVE_WINDOW_SYSTEM */
4998 return 1;
5001 /* Prepare to handle `((margin left-margin) ...)',
5002 `((margin right-margin) ...)' and `((margin nil) ...)'
5003 prefixes for display specifications. */
5004 location = Qunbound;
5005 if (CONSP (spec) && CONSP (XCAR (spec)))
5007 Lisp_Object tem;
5009 value = XCDR (spec);
5010 if (CONSP (value))
5011 value = XCAR (value);
5013 tem = XCAR (spec);
5014 if (EQ (XCAR (tem), Qmargin)
5015 && (tem = XCDR (tem),
5016 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5017 (NILP (tem)
5018 || EQ (tem, Qleft_margin)
5019 || EQ (tem, Qright_margin))))
5020 location = tem;
5023 if (EQ (location, Qunbound))
5025 location = Qnil;
5026 value = spec;
5029 /* After this point, VALUE is the property after any
5030 margin prefix has been stripped. It must be a string,
5031 an image specification, or `(space ...)'.
5033 LOCATION specifies where to display: `left-margin',
5034 `right-margin' or nil. */
5036 valid_p = (STRINGP (value)
5037 #ifdef HAVE_WINDOW_SYSTEM
5038 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5039 && valid_image_p (value))
5040 #endif /* not HAVE_WINDOW_SYSTEM */
5041 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5043 if (valid_p && !display_replaced_p)
5045 int retval = 1;
5047 if (!it)
5049 /* Callers need to know whether the display spec is any kind
5050 of `(space ...)' spec that is about to affect text-area
5051 display. */
5052 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5053 retval = 2;
5054 return retval;
5057 /* Save current settings of IT so that we can restore them
5058 when we are finished with the glyph property value. */
5059 push_it (it, position);
5060 it->from_overlay = overlay;
5061 it->from_disp_prop_p = 1;
5063 if (NILP (location))
5064 it->area = TEXT_AREA;
5065 else if (EQ (location, Qleft_margin))
5066 it->area = LEFT_MARGIN_AREA;
5067 else
5068 it->area = RIGHT_MARGIN_AREA;
5070 if (STRINGP (value))
5072 it->string = value;
5073 it->multibyte_p = STRING_MULTIBYTE (it->string);
5074 it->current.overlay_string_index = -1;
5075 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5076 it->end_charpos = it->string_nchars = SCHARS (it->string);
5077 it->method = GET_FROM_STRING;
5078 it->stop_charpos = 0;
5079 it->prev_stop = 0;
5080 it->base_level_stop = 0;
5081 it->string_from_display_prop_p = 1;
5082 /* Say that we haven't consumed the characters with
5083 `display' property yet. The call to pop_it in
5084 set_iterator_to_next will clean this up. */
5085 if (BUFFERP (object))
5086 *position = start_pos;
5088 /* Force paragraph direction to be that of the parent
5089 object. If the parent object's paragraph direction is
5090 not yet determined, default to L2R. */
5091 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5092 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5093 else
5094 it->paragraph_embedding = L2R;
5096 /* Set up the bidi iterator for this display string. */
5097 if (it->bidi_p)
5099 it->bidi_it.string.lstring = it->string;
5100 it->bidi_it.string.s = NULL;
5101 it->bidi_it.string.schars = it->end_charpos;
5102 it->bidi_it.string.bufpos = bufpos;
5103 it->bidi_it.string.from_disp_str = 1;
5104 it->bidi_it.string.unibyte = !it->multibyte_p;
5105 it->bidi_it.w = it->w;
5106 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5109 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5111 it->method = GET_FROM_STRETCH;
5112 it->object = value;
5113 *position = it->position = start_pos;
5114 retval = 1 + (it->area == TEXT_AREA);
5116 #ifdef HAVE_WINDOW_SYSTEM
5117 else
5119 it->what = IT_IMAGE;
5120 it->image_id = lookup_image (it->f, value);
5121 it->position = start_pos;
5122 it->object = NILP (object) ? it->w->contents : object;
5123 it->method = GET_FROM_IMAGE;
5125 /* Say that we haven't consumed the characters with
5126 `display' property yet. The call to pop_it in
5127 set_iterator_to_next will clean this up. */
5128 *position = start_pos;
5130 #endif /* HAVE_WINDOW_SYSTEM */
5132 return retval;
5135 /* Invalid property or property not supported. Restore
5136 POSITION to what it was before. */
5137 *position = start_pos;
5138 return 0;
5141 /* Check if PROP is a display property value whose text should be
5142 treated as intangible. OVERLAY is the overlay from which PROP
5143 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5144 specify the buffer position covered by PROP. */
5147 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5148 ptrdiff_t charpos, ptrdiff_t bytepos)
5150 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5151 struct text_pos position;
5153 SET_TEXT_POS (position, charpos, bytepos);
5154 return handle_display_spec (NULL, prop, Qnil, overlay,
5155 &position, charpos, frame_window_p);
5159 /* Return 1 if PROP is a display sub-property value containing STRING.
5161 Implementation note: this and the following function are really
5162 special cases of handle_display_spec and
5163 handle_single_display_spec, and should ideally use the same code.
5164 Until they do, these two pairs must be consistent and must be
5165 modified in sync. */
5167 static int
5168 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5170 if (EQ (string, prop))
5171 return 1;
5173 /* Skip over `when FORM'. */
5174 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5176 prop = XCDR (prop);
5177 if (!CONSP (prop))
5178 return 0;
5179 /* Actually, the condition following `when' should be eval'ed,
5180 like handle_single_display_spec does, and we should return
5181 zero if it evaluates to nil. However, this function is
5182 called only when the buffer was already displayed and some
5183 glyph in the glyph matrix was found to come from a display
5184 string. Therefore, the condition was already evaluated, and
5185 the result was non-nil, otherwise the display string wouldn't
5186 have been displayed and we would have never been called for
5187 this property. Thus, we can skip the evaluation and assume
5188 its result is non-nil. */
5189 prop = XCDR (prop);
5192 if (CONSP (prop))
5193 /* Skip over `margin LOCATION'. */
5194 if (EQ (XCAR (prop), Qmargin))
5196 prop = XCDR (prop);
5197 if (!CONSP (prop))
5198 return 0;
5200 prop = XCDR (prop);
5201 if (!CONSP (prop))
5202 return 0;
5205 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5209 /* Return 1 if STRING appears in the `display' property PROP. */
5211 static int
5212 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5214 if (CONSP (prop)
5215 && !EQ (XCAR (prop), Qwhen)
5216 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5218 /* A list of sub-properties. */
5219 while (CONSP (prop))
5221 if (single_display_spec_string_p (XCAR (prop), string))
5222 return 1;
5223 prop = XCDR (prop);
5226 else if (VECTORP (prop))
5228 /* A vector of sub-properties. */
5229 ptrdiff_t i;
5230 for (i = 0; i < ASIZE (prop); ++i)
5231 if (single_display_spec_string_p (AREF (prop, i), string))
5232 return 1;
5234 else
5235 return single_display_spec_string_p (prop, string);
5237 return 0;
5240 /* Look for STRING in overlays and text properties in the current
5241 buffer, between character positions FROM and TO (excluding TO).
5242 BACK_P non-zero means look back (in this case, TO is supposed to be
5243 less than FROM).
5244 Value is the first character position where STRING was found, or
5245 zero if it wasn't found before hitting TO.
5247 This function may only use code that doesn't eval because it is
5248 called asynchronously from note_mouse_highlight. */
5250 static ptrdiff_t
5251 string_buffer_position_lim (Lisp_Object string,
5252 ptrdiff_t from, ptrdiff_t to, int back_p)
5254 Lisp_Object limit, prop, pos;
5255 int found = 0;
5257 pos = make_number (max (from, BEGV));
5259 if (!back_p) /* looking forward */
5261 limit = make_number (min (to, ZV));
5262 while (!found && !EQ (pos, limit))
5264 prop = Fget_char_property (pos, Qdisplay, Qnil);
5265 if (!NILP (prop) && display_prop_string_p (prop, string))
5266 found = 1;
5267 else
5268 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5269 limit);
5272 else /* looking back */
5274 limit = make_number (max (to, BEGV));
5275 while (!found && !EQ (pos, limit))
5277 prop = Fget_char_property (pos, Qdisplay, Qnil);
5278 if (!NILP (prop) && display_prop_string_p (prop, string))
5279 found = 1;
5280 else
5281 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5282 limit);
5286 return found ? XINT (pos) : 0;
5289 /* Determine which buffer position in current buffer STRING comes from.
5290 AROUND_CHARPOS is an approximate position where it could come from.
5291 Value is the buffer position or 0 if it couldn't be determined.
5293 This function is necessary because we don't record buffer positions
5294 in glyphs generated from strings (to keep struct glyph small).
5295 This function may only use code that doesn't eval because it is
5296 called asynchronously from note_mouse_highlight. */
5298 static ptrdiff_t
5299 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5301 const int MAX_DISTANCE = 1000;
5302 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5303 around_charpos + MAX_DISTANCE,
5306 if (!found)
5307 found = string_buffer_position_lim (string, around_charpos,
5308 around_charpos - MAX_DISTANCE, 1);
5309 return found;
5314 /***********************************************************************
5315 `composition' property
5316 ***********************************************************************/
5318 /* Set up iterator IT from `composition' property at its current
5319 position. Called from handle_stop. */
5321 static enum prop_handled
5322 handle_composition_prop (struct it *it)
5324 Lisp_Object prop, string;
5325 ptrdiff_t pos, pos_byte, start, end;
5327 if (STRINGP (it->string))
5329 unsigned char *s;
5331 pos = IT_STRING_CHARPOS (*it);
5332 pos_byte = IT_STRING_BYTEPOS (*it);
5333 string = it->string;
5334 s = SDATA (string) + pos_byte;
5335 it->c = STRING_CHAR (s);
5337 else
5339 pos = IT_CHARPOS (*it);
5340 pos_byte = IT_BYTEPOS (*it);
5341 string = Qnil;
5342 it->c = FETCH_CHAR (pos_byte);
5345 /* If there's a valid composition and point is not inside of the
5346 composition (in the case that the composition is from the current
5347 buffer), draw a glyph composed from the composition components. */
5348 if (find_composition (pos, -1, &start, &end, &prop, string)
5349 && COMPOSITION_VALID_P (start, end, prop)
5350 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5352 if (start < pos)
5353 /* As we can't handle this situation (perhaps font-lock added
5354 a new composition), we just return here hoping that next
5355 redisplay will detect this composition much earlier. */
5356 return HANDLED_NORMALLY;
5357 if (start != pos)
5359 if (STRINGP (it->string))
5360 pos_byte = string_char_to_byte (it->string, start);
5361 else
5362 pos_byte = CHAR_TO_BYTE (start);
5364 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5365 prop, string);
5367 if (it->cmp_it.id >= 0)
5369 it->cmp_it.ch = -1;
5370 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5371 it->cmp_it.nglyphs = -1;
5375 return HANDLED_NORMALLY;
5380 /***********************************************************************
5381 Overlay strings
5382 ***********************************************************************/
5384 /* The following structure is used to record overlay strings for
5385 later sorting in load_overlay_strings. */
5387 struct overlay_entry
5389 Lisp_Object overlay;
5390 Lisp_Object string;
5391 EMACS_INT priority;
5392 int after_string_p;
5396 /* Set up iterator IT from overlay strings at its current position.
5397 Called from handle_stop. */
5399 static enum prop_handled
5400 handle_overlay_change (struct it *it)
5402 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5403 return HANDLED_RECOMPUTE_PROPS;
5404 else
5405 return HANDLED_NORMALLY;
5409 /* Set up the next overlay string for delivery by IT, if there is an
5410 overlay string to deliver. Called by set_iterator_to_next when the
5411 end of the current overlay string is reached. If there are more
5412 overlay strings to display, IT->string and
5413 IT->current.overlay_string_index are set appropriately here.
5414 Otherwise IT->string is set to nil. */
5416 static void
5417 next_overlay_string (struct it *it)
5419 ++it->current.overlay_string_index;
5420 if (it->current.overlay_string_index == it->n_overlay_strings)
5422 /* No more overlay strings. Restore IT's settings to what
5423 they were before overlay strings were processed, and
5424 continue to deliver from current_buffer. */
5426 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5427 pop_it (it);
5428 eassert (it->sp > 0
5429 || (NILP (it->string)
5430 && it->method == GET_FROM_BUFFER
5431 && it->stop_charpos >= BEGV
5432 && it->stop_charpos <= it->end_charpos));
5433 it->current.overlay_string_index = -1;
5434 it->n_overlay_strings = 0;
5435 it->overlay_strings_charpos = -1;
5436 /* If there's an empty display string on the stack, pop the
5437 stack, to resync the bidi iterator with IT's position. Such
5438 empty strings are pushed onto the stack in
5439 get_overlay_strings_1. */
5440 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5441 pop_it (it);
5443 /* If we're at the end of the buffer, record that we have
5444 processed the overlay strings there already, so that
5445 next_element_from_buffer doesn't try it again. */
5446 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5447 it->overlay_strings_at_end_processed_p = 1;
5449 else
5451 /* There are more overlay strings to process. If
5452 IT->current.overlay_string_index has advanced to a position
5453 where we must load IT->overlay_strings with more strings, do
5454 it. We must load at the IT->overlay_strings_charpos where
5455 IT->n_overlay_strings was originally computed; when invisible
5456 text is present, this might not be IT_CHARPOS (Bug#7016). */
5457 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5459 if (it->current.overlay_string_index && i == 0)
5460 load_overlay_strings (it, it->overlay_strings_charpos);
5462 /* Initialize IT to deliver display elements from the overlay
5463 string. */
5464 it->string = it->overlay_strings[i];
5465 it->multibyte_p = STRING_MULTIBYTE (it->string);
5466 SET_TEXT_POS (it->current.string_pos, 0, 0);
5467 it->method = GET_FROM_STRING;
5468 it->stop_charpos = 0;
5469 it->end_charpos = SCHARS (it->string);
5470 if (it->cmp_it.stop_pos >= 0)
5471 it->cmp_it.stop_pos = 0;
5472 it->prev_stop = 0;
5473 it->base_level_stop = 0;
5475 /* Set up the bidi iterator for this overlay string. */
5476 if (it->bidi_p)
5478 it->bidi_it.string.lstring = it->string;
5479 it->bidi_it.string.s = NULL;
5480 it->bidi_it.string.schars = SCHARS (it->string);
5481 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5482 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5483 it->bidi_it.string.unibyte = !it->multibyte_p;
5484 it->bidi_it.w = it->w;
5485 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5489 CHECK_IT (it);
5493 /* Compare two overlay_entry structures E1 and E2. Used as a
5494 comparison function for qsort in load_overlay_strings. Overlay
5495 strings for the same position are sorted so that
5497 1. All after-strings come in front of before-strings, except
5498 when they come from the same overlay.
5500 2. Within after-strings, strings are sorted so that overlay strings
5501 from overlays with higher priorities come first.
5503 2. Within before-strings, strings are sorted so that overlay
5504 strings from overlays with higher priorities come last.
5506 Value is analogous to strcmp. */
5509 static int
5510 compare_overlay_entries (const void *e1, const void *e2)
5512 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5513 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5514 int result;
5516 if (entry1->after_string_p != entry2->after_string_p)
5518 /* Let after-strings appear in front of before-strings if
5519 they come from different overlays. */
5520 if (EQ (entry1->overlay, entry2->overlay))
5521 result = entry1->after_string_p ? 1 : -1;
5522 else
5523 result = entry1->after_string_p ? -1 : 1;
5525 else if (entry1->priority != entry2->priority)
5527 if (entry1->after_string_p)
5528 /* After-strings sorted in order of decreasing priority. */
5529 result = entry2->priority < entry1->priority ? -1 : 1;
5530 else
5531 /* Before-strings sorted in order of increasing priority. */
5532 result = entry1->priority < entry2->priority ? -1 : 1;
5534 else
5535 result = 0;
5537 return result;
5541 /* Load the vector IT->overlay_strings with overlay strings from IT's
5542 current buffer position, or from CHARPOS if that is > 0. Set
5543 IT->n_overlays to the total number of overlay strings found.
5545 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5546 a time. On entry into load_overlay_strings,
5547 IT->current.overlay_string_index gives the number of overlay
5548 strings that have already been loaded by previous calls to this
5549 function.
5551 IT->add_overlay_start contains an additional overlay start
5552 position to consider for taking overlay strings from, if non-zero.
5553 This position comes into play when the overlay has an `invisible'
5554 property, and both before and after-strings. When we've skipped to
5555 the end of the overlay, because of its `invisible' property, we
5556 nevertheless want its before-string to appear.
5557 IT->add_overlay_start will contain the overlay start position
5558 in this case.
5560 Overlay strings are sorted so that after-string strings come in
5561 front of before-string strings. Within before and after-strings,
5562 strings are sorted by overlay priority. See also function
5563 compare_overlay_entries. */
5565 static void
5566 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5568 Lisp_Object overlay, window, str, invisible;
5569 struct Lisp_Overlay *ov;
5570 ptrdiff_t start, end;
5571 ptrdiff_t size = 20;
5572 ptrdiff_t n = 0, i, j;
5573 int invis_p;
5574 struct overlay_entry *entries = alloca (size * sizeof *entries);
5575 USE_SAFE_ALLOCA;
5577 if (charpos <= 0)
5578 charpos = IT_CHARPOS (*it);
5580 /* Append the overlay string STRING of overlay OVERLAY to vector
5581 `entries' which has size `size' and currently contains `n'
5582 elements. AFTER_P non-zero means STRING is an after-string of
5583 OVERLAY. */
5584 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5585 do \
5587 Lisp_Object priority; \
5589 if (n == size) \
5591 struct overlay_entry *old = entries; \
5592 SAFE_NALLOCA (entries, 2, size); \
5593 memcpy (entries, old, size * sizeof *entries); \
5594 size *= 2; \
5597 entries[n].string = (STRING); \
5598 entries[n].overlay = (OVERLAY); \
5599 priority = Foverlay_get ((OVERLAY), Qpriority); \
5600 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5601 entries[n].after_string_p = (AFTER_P); \
5602 ++n; \
5604 while (0)
5606 /* Process overlay before the overlay center. */
5607 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5609 XSETMISC (overlay, ov);
5610 eassert (OVERLAYP (overlay));
5611 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5612 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5614 if (end < charpos)
5615 break;
5617 /* Skip this overlay if it doesn't start or end at IT's current
5618 position. */
5619 if (end != charpos && start != charpos)
5620 continue;
5622 /* Skip this overlay if it doesn't apply to IT->w. */
5623 window = Foverlay_get (overlay, Qwindow);
5624 if (WINDOWP (window) && XWINDOW (window) != it->w)
5625 continue;
5627 /* If the text ``under'' the overlay is invisible, both before-
5628 and after-strings from this overlay are visible; start and
5629 end position are indistinguishable. */
5630 invisible = Foverlay_get (overlay, Qinvisible);
5631 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5633 /* If overlay has a non-empty before-string, record it. */
5634 if ((start == charpos || (end == charpos && invis_p))
5635 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5636 && SCHARS (str))
5637 RECORD_OVERLAY_STRING (overlay, str, 0);
5639 /* If overlay has a non-empty after-string, record it. */
5640 if ((end == charpos || (start == charpos && invis_p))
5641 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5642 && SCHARS (str))
5643 RECORD_OVERLAY_STRING (overlay, str, 1);
5646 /* Process overlays after the overlay center. */
5647 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5649 XSETMISC (overlay, ov);
5650 eassert (OVERLAYP (overlay));
5651 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5652 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5654 if (start > charpos)
5655 break;
5657 /* Skip this overlay if it doesn't start or end at IT's current
5658 position. */
5659 if (end != charpos && start != charpos)
5660 continue;
5662 /* Skip this overlay if it doesn't apply to IT->w. */
5663 window = Foverlay_get (overlay, Qwindow);
5664 if (WINDOWP (window) && XWINDOW (window) != it->w)
5665 continue;
5667 /* If the text ``under'' the overlay is invisible, it has a zero
5668 dimension, and both before- and after-strings apply. */
5669 invisible = Foverlay_get (overlay, Qinvisible);
5670 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5672 /* If overlay has a non-empty before-string, record it. */
5673 if ((start == charpos || (end == charpos && invis_p))
5674 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5675 && SCHARS (str))
5676 RECORD_OVERLAY_STRING (overlay, str, 0);
5678 /* If overlay has a non-empty after-string, record it. */
5679 if ((end == charpos || (start == charpos && invis_p))
5680 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5681 && SCHARS (str))
5682 RECORD_OVERLAY_STRING (overlay, str, 1);
5685 #undef RECORD_OVERLAY_STRING
5687 /* Sort entries. */
5688 if (n > 1)
5689 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5691 /* Record number of overlay strings, and where we computed it. */
5692 it->n_overlay_strings = n;
5693 it->overlay_strings_charpos = charpos;
5695 /* IT->current.overlay_string_index is the number of overlay strings
5696 that have already been consumed by IT. Copy some of the
5697 remaining overlay strings to IT->overlay_strings. */
5698 i = 0;
5699 j = it->current.overlay_string_index;
5700 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5702 it->overlay_strings[i] = entries[j].string;
5703 it->string_overlays[i++] = entries[j++].overlay;
5706 CHECK_IT (it);
5707 SAFE_FREE ();
5711 /* Get the first chunk of overlay strings at IT's current buffer
5712 position, or at CHARPOS if that is > 0. Value is non-zero if at
5713 least one overlay string was found. */
5715 static int
5716 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5718 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5719 process. This fills IT->overlay_strings with strings, and sets
5720 IT->n_overlay_strings to the total number of strings to process.
5721 IT->pos.overlay_string_index has to be set temporarily to zero
5722 because load_overlay_strings needs this; it must be set to -1
5723 when no overlay strings are found because a zero value would
5724 indicate a position in the first overlay string. */
5725 it->current.overlay_string_index = 0;
5726 load_overlay_strings (it, charpos);
5728 /* If we found overlay strings, set up IT to deliver display
5729 elements from the first one. Otherwise set up IT to deliver
5730 from current_buffer. */
5731 if (it->n_overlay_strings)
5733 /* Make sure we know settings in current_buffer, so that we can
5734 restore meaningful values when we're done with the overlay
5735 strings. */
5736 if (compute_stop_p)
5737 compute_stop_pos (it);
5738 eassert (it->face_id >= 0);
5740 /* Save IT's settings. They are restored after all overlay
5741 strings have been processed. */
5742 eassert (!compute_stop_p || it->sp == 0);
5744 /* When called from handle_stop, there might be an empty display
5745 string loaded. In that case, don't bother saving it. But
5746 don't use this optimization with the bidi iterator, since we
5747 need the corresponding pop_it call to resync the bidi
5748 iterator's position with IT's position, after we are done
5749 with the overlay strings. (The corresponding call to pop_it
5750 in case of an empty display string is in
5751 next_overlay_string.) */
5752 if (!(!it->bidi_p
5753 && STRINGP (it->string) && !SCHARS (it->string)))
5754 push_it (it, NULL);
5756 /* Set up IT to deliver display elements from the first overlay
5757 string. */
5758 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5759 it->string = it->overlay_strings[0];
5760 it->from_overlay = Qnil;
5761 it->stop_charpos = 0;
5762 eassert (STRINGP (it->string));
5763 it->end_charpos = SCHARS (it->string);
5764 it->prev_stop = 0;
5765 it->base_level_stop = 0;
5766 it->multibyte_p = STRING_MULTIBYTE (it->string);
5767 it->method = GET_FROM_STRING;
5768 it->from_disp_prop_p = 0;
5770 /* Force paragraph direction to be that of the parent
5771 buffer. */
5772 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5773 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5774 else
5775 it->paragraph_embedding = L2R;
5777 /* Set up the bidi iterator for this overlay string. */
5778 if (it->bidi_p)
5780 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5782 it->bidi_it.string.lstring = it->string;
5783 it->bidi_it.string.s = NULL;
5784 it->bidi_it.string.schars = SCHARS (it->string);
5785 it->bidi_it.string.bufpos = pos;
5786 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5787 it->bidi_it.string.unibyte = !it->multibyte_p;
5788 it->bidi_it.w = it->w;
5789 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5791 return 1;
5794 it->current.overlay_string_index = -1;
5795 return 0;
5798 static int
5799 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5801 it->string = Qnil;
5802 it->method = GET_FROM_BUFFER;
5804 (void) get_overlay_strings_1 (it, charpos, 1);
5806 CHECK_IT (it);
5808 /* Value is non-zero if we found at least one overlay string. */
5809 return STRINGP (it->string);
5814 /***********************************************************************
5815 Saving and restoring state
5816 ***********************************************************************/
5818 /* Save current settings of IT on IT->stack. Called, for example,
5819 before setting up IT for an overlay string, to be able to restore
5820 IT's settings to what they were after the overlay string has been
5821 processed. If POSITION is non-NULL, it is the position to save on
5822 the stack instead of IT->position. */
5824 static void
5825 push_it (struct it *it, struct text_pos *position)
5827 struct iterator_stack_entry *p;
5829 eassert (it->sp < IT_STACK_SIZE);
5830 p = it->stack + it->sp;
5832 p->stop_charpos = it->stop_charpos;
5833 p->prev_stop = it->prev_stop;
5834 p->base_level_stop = it->base_level_stop;
5835 p->cmp_it = it->cmp_it;
5836 eassert (it->face_id >= 0);
5837 p->face_id = it->face_id;
5838 p->string = it->string;
5839 p->method = it->method;
5840 p->from_overlay = it->from_overlay;
5841 switch (p->method)
5843 case GET_FROM_IMAGE:
5844 p->u.image.object = it->object;
5845 p->u.image.image_id = it->image_id;
5846 p->u.image.slice = it->slice;
5847 break;
5848 case GET_FROM_STRETCH:
5849 p->u.stretch.object = it->object;
5850 break;
5852 p->position = position ? *position : it->position;
5853 p->current = it->current;
5854 p->end_charpos = it->end_charpos;
5855 p->string_nchars = it->string_nchars;
5856 p->area = it->area;
5857 p->multibyte_p = it->multibyte_p;
5858 p->avoid_cursor_p = it->avoid_cursor_p;
5859 p->space_width = it->space_width;
5860 p->font_height = it->font_height;
5861 p->voffset = it->voffset;
5862 p->string_from_display_prop_p = it->string_from_display_prop_p;
5863 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5864 p->display_ellipsis_p = 0;
5865 p->line_wrap = it->line_wrap;
5866 p->bidi_p = it->bidi_p;
5867 p->paragraph_embedding = it->paragraph_embedding;
5868 p->from_disp_prop_p = it->from_disp_prop_p;
5869 ++it->sp;
5871 /* Save the state of the bidi iterator as well. */
5872 if (it->bidi_p)
5873 bidi_push_it (&it->bidi_it);
5876 static void
5877 iterate_out_of_display_property (struct it *it)
5879 int buffer_p = !STRINGP (it->string);
5880 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5881 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5883 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5885 /* Maybe initialize paragraph direction. If we are at the beginning
5886 of a new paragraph, next_element_from_buffer may not have a
5887 chance to do that. */
5888 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5889 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5890 /* prev_stop can be zero, so check against BEGV as well. */
5891 while (it->bidi_it.charpos >= bob
5892 && it->prev_stop <= it->bidi_it.charpos
5893 && it->bidi_it.charpos < CHARPOS (it->position)
5894 && it->bidi_it.charpos < eob)
5895 bidi_move_to_visually_next (&it->bidi_it);
5896 /* Record the stop_pos we just crossed, for when we cross it
5897 back, maybe. */
5898 if (it->bidi_it.charpos > CHARPOS (it->position))
5899 it->prev_stop = CHARPOS (it->position);
5900 /* If we ended up not where pop_it put us, resync IT's
5901 positional members with the bidi iterator. */
5902 if (it->bidi_it.charpos != CHARPOS (it->position))
5903 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5904 if (buffer_p)
5905 it->current.pos = it->position;
5906 else
5907 it->current.string_pos = it->position;
5910 /* Restore IT's settings from IT->stack. Called, for example, when no
5911 more overlay strings must be processed, and we return to delivering
5912 display elements from a buffer, or when the end of a string from a
5913 `display' property is reached and we return to delivering display
5914 elements from an overlay string, or from a buffer. */
5916 static void
5917 pop_it (struct it *it)
5919 struct iterator_stack_entry *p;
5920 int from_display_prop = it->from_disp_prop_p;
5922 eassert (it->sp > 0);
5923 --it->sp;
5924 p = it->stack + it->sp;
5925 it->stop_charpos = p->stop_charpos;
5926 it->prev_stop = p->prev_stop;
5927 it->base_level_stop = p->base_level_stop;
5928 it->cmp_it = p->cmp_it;
5929 it->face_id = p->face_id;
5930 it->current = p->current;
5931 it->position = p->position;
5932 it->string = p->string;
5933 it->from_overlay = p->from_overlay;
5934 if (NILP (it->string))
5935 SET_TEXT_POS (it->current.string_pos, -1, -1);
5936 it->method = p->method;
5937 switch (it->method)
5939 case GET_FROM_IMAGE:
5940 it->image_id = p->u.image.image_id;
5941 it->object = p->u.image.object;
5942 it->slice = p->u.image.slice;
5943 break;
5944 case GET_FROM_STRETCH:
5945 it->object = p->u.stretch.object;
5946 break;
5947 case GET_FROM_BUFFER:
5948 it->object = it->w->contents;
5949 break;
5950 case GET_FROM_STRING:
5951 it->object = it->string;
5952 break;
5953 case GET_FROM_DISPLAY_VECTOR:
5954 if (it->s)
5955 it->method = GET_FROM_C_STRING;
5956 else if (STRINGP (it->string))
5957 it->method = GET_FROM_STRING;
5958 else
5960 it->method = GET_FROM_BUFFER;
5961 it->object = it->w->contents;
5964 it->end_charpos = p->end_charpos;
5965 it->string_nchars = p->string_nchars;
5966 it->area = p->area;
5967 it->multibyte_p = p->multibyte_p;
5968 it->avoid_cursor_p = p->avoid_cursor_p;
5969 it->space_width = p->space_width;
5970 it->font_height = p->font_height;
5971 it->voffset = p->voffset;
5972 it->string_from_display_prop_p = p->string_from_display_prop_p;
5973 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5974 it->line_wrap = p->line_wrap;
5975 it->bidi_p = p->bidi_p;
5976 it->paragraph_embedding = p->paragraph_embedding;
5977 it->from_disp_prop_p = p->from_disp_prop_p;
5978 if (it->bidi_p)
5980 bidi_pop_it (&it->bidi_it);
5981 /* Bidi-iterate until we get out of the portion of text, if any,
5982 covered by a `display' text property or by an overlay with
5983 `display' property. (We cannot just jump there, because the
5984 internal coherency of the bidi iterator state can not be
5985 preserved across such jumps.) We also must determine the
5986 paragraph base direction if the overlay we just processed is
5987 at the beginning of a new paragraph. */
5988 if (from_display_prop
5989 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5990 iterate_out_of_display_property (it);
5992 eassert ((BUFFERP (it->object)
5993 && IT_CHARPOS (*it) == it->bidi_it.charpos
5994 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5995 || (STRINGP (it->object)
5996 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5997 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5998 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6004 /***********************************************************************
6005 Moving over lines
6006 ***********************************************************************/
6008 /* Set IT's current position to the previous line start. */
6010 static void
6011 back_to_previous_line_start (struct it *it)
6013 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6015 DEC_BOTH (cp, bp);
6016 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6020 /* Move IT to the next line start.
6022 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6023 we skipped over part of the text (as opposed to moving the iterator
6024 continuously over the text). Otherwise, don't change the value
6025 of *SKIPPED_P.
6027 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6028 iterator on the newline, if it was found.
6030 Newlines may come from buffer text, overlay strings, or strings
6031 displayed via the `display' property. That's the reason we can't
6032 simply use find_newline_no_quit.
6034 Note that this function may not skip over invisible text that is so
6035 because of text properties and immediately follows a newline. If
6036 it would, function reseat_at_next_visible_line_start, when called
6037 from set_iterator_to_next, would effectively make invisible
6038 characters following a newline part of the wrong glyph row, which
6039 leads to wrong cursor motion. */
6041 static int
6042 forward_to_next_line_start (struct it *it, int *skipped_p,
6043 struct bidi_it *bidi_it_prev)
6045 ptrdiff_t old_selective;
6046 int newline_found_p, n;
6047 const int MAX_NEWLINE_DISTANCE = 500;
6049 /* If already on a newline, just consume it to avoid unintended
6050 skipping over invisible text below. */
6051 if (it->what == IT_CHARACTER
6052 && it->c == '\n'
6053 && CHARPOS (it->position) == IT_CHARPOS (*it))
6055 if (it->bidi_p && bidi_it_prev)
6056 *bidi_it_prev = it->bidi_it;
6057 set_iterator_to_next (it, 0);
6058 it->c = 0;
6059 return 1;
6062 /* Don't handle selective display in the following. It's (a)
6063 unnecessary because it's done by the caller, and (b) leads to an
6064 infinite recursion because next_element_from_ellipsis indirectly
6065 calls this function. */
6066 old_selective = it->selective;
6067 it->selective = 0;
6069 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6070 from buffer text. */
6071 for (n = newline_found_p = 0;
6072 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6073 n += STRINGP (it->string) ? 0 : 1)
6075 if (!get_next_display_element (it))
6076 return 0;
6077 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6078 if (newline_found_p && it->bidi_p && bidi_it_prev)
6079 *bidi_it_prev = it->bidi_it;
6080 set_iterator_to_next (it, 0);
6083 /* If we didn't find a newline near enough, see if we can use a
6084 short-cut. */
6085 if (!newline_found_p)
6087 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6088 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6089 1, &bytepos);
6090 Lisp_Object pos;
6092 eassert (!STRINGP (it->string));
6094 /* If there isn't any `display' property in sight, and no
6095 overlays, we can just use the position of the newline in
6096 buffer text. */
6097 if (it->stop_charpos >= limit
6098 || ((pos = Fnext_single_property_change (make_number (start),
6099 Qdisplay, Qnil,
6100 make_number (limit)),
6101 NILP (pos))
6102 && next_overlay_change (start) == ZV))
6104 if (!it->bidi_p)
6106 IT_CHARPOS (*it) = limit;
6107 IT_BYTEPOS (*it) = bytepos;
6109 else
6111 struct bidi_it bprev;
6113 /* Help bidi.c avoid expensive searches for display
6114 properties and overlays, by telling it that there are
6115 none up to `limit'. */
6116 if (it->bidi_it.disp_pos < limit)
6118 it->bidi_it.disp_pos = limit;
6119 it->bidi_it.disp_prop = 0;
6121 do {
6122 bprev = it->bidi_it;
6123 bidi_move_to_visually_next (&it->bidi_it);
6124 } while (it->bidi_it.charpos != limit);
6125 IT_CHARPOS (*it) = limit;
6126 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6127 if (bidi_it_prev)
6128 *bidi_it_prev = bprev;
6130 *skipped_p = newline_found_p = 1;
6132 else
6134 while (get_next_display_element (it)
6135 && !newline_found_p)
6137 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6138 if (newline_found_p && it->bidi_p && bidi_it_prev)
6139 *bidi_it_prev = it->bidi_it;
6140 set_iterator_to_next (it, 0);
6145 it->selective = old_selective;
6146 return newline_found_p;
6150 /* Set IT's current position to the previous visible line start. Skip
6151 invisible text that is so either due to text properties or due to
6152 selective display. Caution: this does not change IT->current_x and
6153 IT->hpos. */
6155 static void
6156 back_to_previous_visible_line_start (struct it *it)
6158 while (IT_CHARPOS (*it) > BEGV)
6160 back_to_previous_line_start (it);
6162 if (IT_CHARPOS (*it) <= BEGV)
6163 break;
6165 /* If selective > 0, then lines indented more than its value are
6166 invisible. */
6167 if (it->selective > 0
6168 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6169 it->selective))
6170 continue;
6172 /* Check the newline before point for invisibility. */
6174 Lisp_Object prop;
6175 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6176 Qinvisible, it->window);
6177 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6178 continue;
6181 if (IT_CHARPOS (*it) <= BEGV)
6182 break;
6185 struct it it2;
6186 void *it2data = NULL;
6187 ptrdiff_t pos;
6188 ptrdiff_t beg, end;
6189 Lisp_Object val, overlay;
6191 SAVE_IT (it2, *it, it2data);
6193 /* If newline is part of a composition, continue from start of composition */
6194 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6195 && beg < IT_CHARPOS (*it))
6196 goto replaced;
6198 /* If newline is replaced by a display property, find start of overlay
6199 or interval and continue search from that point. */
6200 pos = --IT_CHARPOS (it2);
6201 --IT_BYTEPOS (it2);
6202 it2.sp = 0;
6203 bidi_unshelve_cache (NULL, 0);
6204 it2.string_from_display_prop_p = 0;
6205 it2.from_disp_prop_p = 0;
6206 if (handle_display_prop (&it2) == HANDLED_RETURN
6207 && !NILP (val = get_char_property_and_overlay
6208 (make_number (pos), Qdisplay, Qnil, &overlay))
6209 && (OVERLAYP (overlay)
6210 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6211 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6213 RESTORE_IT (it, it, it2data);
6214 goto replaced;
6217 /* Newline is not replaced by anything -- so we are done. */
6218 RESTORE_IT (it, it, it2data);
6219 break;
6221 replaced:
6222 if (beg < BEGV)
6223 beg = BEGV;
6224 IT_CHARPOS (*it) = beg;
6225 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6229 it->continuation_lines_width = 0;
6231 eassert (IT_CHARPOS (*it) >= BEGV);
6232 eassert (IT_CHARPOS (*it) == BEGV
6233 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6234 CHECK_IT (it);
6238 /* Reseat iterator IT at the previous visible line start. Skip
6239 invisible text that is so either due to text properties or due to
6240 selective display. At the end, update IT's overlay information,
6241 face information etc. */
6243 void
6244 reseat_at_previous_visible_line_start (struct it *it)
6246 back_to_previous_visible_line_start (it);
6247 reseat (it, it->current.pos, 1);
6248 CHECK_IT (it);
6252 /* Reseat iterator IT on the next visible line start in the current
6253 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6254 preceding the line start. Skip over invisible text that is so
6255 because of selective display. Compute faces, overlays etc at the
6256 new position. Note that this function does not skip over text that
6257 is invisible because of text properties. */
6259 static void
6260 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6262 int newline_found_p, skipped_p = 0;
6263 struct bidi_it bidi_it_prev;
6265 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6267 /* Skip over lines that are invisible because they are indented
6268 more than the value of IT->selective. */
6269 if (it->selective > 0)
6270 while (IT_CHARPOS (*it) < ZV
6271 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6272 it->selective))
6274 eassert (IT_BYTEPOS (*it) == BEGV
6275 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6276 newline_found_p =
6277 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6280 /* Position on the newline if that's what's requested. */
6281 if (on_newline_p && newline_found_p)
6283 if (STRINGP (it->string))
6285 if (IT_STRING_CHARPOS (*it) > 0)
6287 if (!it->bidi_p)
6289 --IT_STRING_CHARPOS (*it);
6290 --IT_STRING_BYTEPOS (*it);
6292 else
6294 /* We need to restore the bidi iterator to the state
6295 it had on the newline, and resync the IT's
6296 position with that. */
6297 it->bidi_it = bidi_it_prev;
6298 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6299 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6303 else if (IT_CHARPOS (*it) > BEGV)
6305 if (!it->bidi_p)
6307 --IT_CHARPOS (*it);
6308 --IT_BYTEPOS (*it);
6310 else
6312 /* We need to restore the bidi iterator to the state it
6313 had on the newline and resync IT with that. */
6314 it->bidi_it = bidi_it_prev;
6315 IT_CHARPOS (*it) = it->bidi_it.charpos;
6316 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6318 reseat (it, it->current.pos, 0);
6321 else if (skipped_p)
6322 reseat (it, it->current.pos, 0);
6324 CHECK_IT (it);
6329 /***********************************************************************
6330 Changing an iterator's position
6331 ***********************************************************************/
6333 /* Change IT's current position to POS in current_buffer. If FORCE_P
6334 is non-zero, always check for text properties at the new position.
6335 Otherwise, text properties are only looked up if POS >=
6336 IT->check_charpos of a property. */
6338 static void
6339 reseat (struct it *it, struct text_pos pos, int force_p)
6341 ptrdiff_t original_pos = IT_CHARPOS (*it);
6343 reseat_1 (it, pos, 0);
6345 /* Determine where to check text properties. Avoid doing it
6346 where possible because text property lookup is very expensive. */
6347 if (force_p
6348 || CHARPOS (pos) > it->stop_charpos
6349 || CHARPOS (pos) < original_pos)
6351 if (it->bidi_p)
6353 /* For bidi iteration, we need to prime prev_stop and
6354 base_level_stop with our best estimations. */
6355 /* Implementation note: Of course, POS is not necessarily a
6356 stop position, so assigning prev_pos to it is a lie; we
6357 should have called compute_stop_backwards. However, if
6358 the current buffer does not include any R2L characters,
6359 that call would be a waste of cycles, because the
6360 iterator will never move back, and thus never cross this
6361 "fake" stop position. So we delay that backward search
6362 until the time we really need it, in next_element_from_buffer. */
6363 if (CHARPOS (pos) != it->prev_stop)
6364 it->prev_stop = CHARPOS (pos);
6365 if (CHARPOS (pos) < it->base_level_stop)
6366 it->base_level_stop = 0; /* meaning it's unknown */
6367 handle_stop (it);
6369 else
6371 handle_stop (it);
6372 it->prev_stop = it->base_level_stop = 0;
6377 CHECK_IT (it);
6381 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6382 IT->stop_pos to POS, also. */
6384 static void
6385 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6387 /* Don't call this function when scanning a C string. */
6388 eassert (it->s == NULL);
6390 /* POS must be a reasonable value. */
6391 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6393 it->current.pos = it->position = pos;
6394 it->end_charpos = ZV;
6395 it->dpvec = NULL;
6396 it->current.dpvec_index = -1;
6397 it->current.overlay_string_index = -1;
6398 IT_STRING_CHARPOS (*it) = -1;
6399 IT_STRING_BYTEPOS (*it) = -1;
6400 it->string = Qnil;
6401 it->method = GET_FROM_BUFFER;
6402 it->object = it->w->contents;
6403 it->area = TEXT_AREA;
6404 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6405 it->sp = 0;
6406 it->string_from_display_prop_p = 0;
6407 it->string_from_prefix_prop_p = 0;
6409 it->from_disp_prop_p = 0;
6410 it->face_before_selective_p = 0;
6411 if (it->bidi_p)
6413 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6414 &it->bidi_it);
6415 bidi_unshelve_cache (NULL, 0);
6416 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6417 it->bidi_it.string.s = NULL;
6418 it->bidi_it.string.lstring = Qnil;
6419 it->bidi_it.string.bufpos = 0;
6420 it->bidi_it.string.unibyte = 0;
6421 it->bidi_it.w = it->w;
6424 if (set_stop_p)
6426 it->stop_charpos = CHARPOS (pos);
6427 it->base_level_stop = CHARPOS (pos);
6429 /* This make the information stored in it->cmp_it invalidate. */
6430 it->cmp_it.id = -1;
6434 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6435 If S is non-null, it is a C string to iterate over. Otherwise,
6436 STRING gives a Lisp string to iterate over.
6438 If PRECISION > 0, don't return more then PRECISION number of
6439 characters from the string.
6441 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6442 characters have been returned. FIELD_WIDTH < 0 means an infinite
6443 field width.
6445 MULTIBYTE = 0 means disable processing of multibyte characters,
6446 MULTIBYTE > 0 means enable it,
6447 MULTIBYTE < 0 means use IT->multibyte_p.
6449 IT must be initialized via a prior call to init_iterator before
6450 calling this function. */
6452 static void
6453 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6454 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6455 int multibyte)
6457 /* No region in strings. */
6458 it->region_beg_charpos = it->region_end_charpos = -1;
6460 /* No text property checks performed by default, but see below. */
6461 it->stop_charpos = -1;
6463 /* Set iterator position and end position. */
6464 memset (&it->current, 0, sizeof it->current);
6465 it->current.overlay_string_index = -1;
6466 it->current.dpvec_index = -1;
6467 eassert (charpos >= 0);
6469 /* If STRING is specified, use its multibyteness, otherwise use the
6470 setting of MULTIBYTE, if specified. */
6471 if (multibyte >= 0)
6472 it->multibyte_p = multibyte > 0;
6474 /* Bidirectional reordering of strings is controlled by the default
6475 value of bidi-display-reordering. Don't try to reorder while
6476 loading loadup.el, as the necessary character property tables are
6477 not yet available. */
6478 it->bidi_p =
6479 NILP (Vpurify_flag)
6480 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6482 if (s == NULL)
6484 eassert (STRINGP (string));
6485 it->string = string;
6486 it->s = NULL;
6487 it->end_charpos = it->string_nchars = SCHARS (string);
6488 it->method = GET_FROM_STRING;
6489 it->current.string_pos = string_pos (charpos, string);
6491 if (it->bidi_p)
6493 it->bidi_it.string.lstring = string;
6494 it->bidi_it.string.s = NULL;
6495 it->bidi_it.string.schars = it->end_charpos;
6496 it->bidi_it.string.bufpos = 0;
6497 it->bidi_it.string.from_disp_str = 0;
6498 it->bidi_it.string.unibyte = !it->multibyte_p;
6499 it->bidi_it.w = it->w;
6500 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6501 FRAME_WINDOW_P (it->f), &it->bidi_it);
6504 else
6506 it->s = (const unsigned char *) s;
6507 it->string = Qnil;
6509 /* Note that we use IT->current.pos, not it->current.string_pos,
6510 for displaying C strings. */
6511 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6512 if (it->multibyte_p)
6514 it->current.pos = c_string_pos (charpos, s, 1);
6515 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6517 else
6519 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6520 it->end_charpos = it->string_nchars = strlen (s);
6523 if (it->bidi_p)
6525 it->bidi_it.string.lstring = Qnil;
6526 it->bidi_it.string.s = (const unsigned char *) s;
6527 it->bidi_it.string.schars = it->end_charpos;
6528 it->bidi_it.string.bufpos = 0;
6529 it->bidi_it.string.from_disp_str = 0;
6530 it->bidi_it.string.unibyte = !it->multibyte_p;
6531 it->bidi_it.w = it->w;
6532 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6533 &it->bidi_it);
6535 it->method = GET_FROM_C_STRING;
6538 /* PRECISION > 0 means don't return more than PRECISION characters
6539 from the string. */
6540 if (precision > 0 && it->end_charpos - charpos > precision)
6542 it->end_charpos = it->string_nchars = charpos + precision;
6543 if (it->bidi_p)
6544 it->bidi_it.string.schars = it->end_charpos;
6547 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6548 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6549 FIELD_WIDTH < 0 means infinite field width. This is useful for
6550 padding with `-' at the end of a mode line. */
6551 if (field_width < 0)
6552 field_width = INFINITY;
6553 /* Implementation note: We deliberately don't enlarge
6554 it->bidi_it.string.schars here to fit it->end_charpos, because
6555 the bidi iterator cannot produce characters out of thin air. */
6556 if (field_width > it->end_charpos - charpos)
6557 it->end_charpos = charpos + field_width;
6559 /* Use the standard display table for displaying strings. */
6560 if (DISP_TABLE_P (Vstandard_display_table))
6561 it->dp = XCHAR_TABLE (Vstandard_display_table);
6563 it->stop_charpos = charpos;
6564 it->prev_stop = charpos;
6565 it->base_level_stop = 0;
6566 if (it->bidi_p)
6568 it->bidi_it.first_elt = 1;
6569 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6570 it->bidi_it.disp_pos = -1;
6572 if (s == NULL && it->multibyte_p)
6574 ptrdiff_t endpos = SCHARS (it->string);
6575 if (endpos > it->end_charpos)
6576 endpos = it->end_charpos;
6577 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6578 it->string);
6580 CHECK_IT (it);
6585 /***********************************************************************
6586 Iteration
6587 ***********************************************************************/
6589 /* Map enum it_method value to corresponding next_element_from_* function. */
6591 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6593 next_element_from_buffer,
6594 next_element_from_display_vector,
6595 next_element_from_string,
6596 next_element_from_c_string,
6597 next_element_from_image,
6598 next_element_from_stretch
6601 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6604 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6605 (possibly with the following characters). */
6607 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6608 ((IT)->cmp_it.id >= 0 \
6609 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6610 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6611 END_CHARPOS, (IT)->w, \
6612 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6613 (IT)->string)))
6616 /* Lookup the char-table Vglyphless_char_display for character C (-1
6617 if we want information for no-font case), and return the display
6618 method symbol. By side-effect, update it->what and
6619 it->glyphless_method. This function is called from
6620 get_next_display_element for each character element, and from
6621 x_produce_glyphs when no suitable font was found. */
6623 Lisp_Object
6624 lookup_glyphless_char_display (int c, struct it *it)
6626 Lisp_Object glyphless_method = Qnil;
6628 if (CHAR_TABLE_P (Vglyphless_char_display)
6629 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6631 if (c >= 0)
6633 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6634 if (CONSP (glyphless_method))
6635 glyphless_method = FRAME_WINDOW_P (it->f)
6636 ? XCAR (glyphless_method)
6637 : XCDR (glyphless_method);
6639 else
6640 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6643 retry:
6644 if (NILP (glyphless_method))
6646 if (c >= 0)
6647 /* The default is to display the character by a proper font. */
6648 return Qnil;
6649 /* The default for the no-font case is to display an empty box. */
6650 glyphless_method = Qempty_box;
6652 if (EQ (glyphless_method, Qzero_width))
6654 if (c >= 0)
6655 return glyphless_method;
6656 /* This method can't be used for the no-font case. */
6657 glyphless_method = Qempty_box;
6659 if (EQ (glyphless_method, Qthin_space))
6660 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6661 else if (EQ (glyphless_method, Qempty_box))
6662 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6663 else if (EQ (glyphless_method, Qhex_code))
6664 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6665 else if (STRINGP (glyphless_method))
6666 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6667 else
6669 /* Invalid value. We use the default method. */
6670 glyphless_method = Qnil;
6671 goto retry;
6673 it->what = IT_GLYPHLESS;
6674 return glyphless_method;
6677 /* Load IT's display element fields with information about the next
6678 display element from the current position of IT. Value is zero if
6679 end of buffer (or C string) is reached. */
6681 static struct frame *last_escape_glyph_frame = NULL;
6682 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6683 static int last_escape_glyph_merged_face_id = 0;
6685 struct frame *last_glyphless_glyph_frame = NULL;
6686 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6687 int last_glyphless_glyph_merged_face_id = 0;
6689 static int
6690 get_next_display_element (struct it *it)
6692 /* Non-zero means that we found a display element. Zero means that
6693 we hit the end of what we iterate over. Performance note: the
6694 function pointer `method' used here turns out to be faster than
6695 using a sequence of if-statements. */
6696 int success_p;
6698 get_next:
6699 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6701 if (it->what == IT_CHARACTER)
6703 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6704 and only if (a) the resolved directionality of that character
6705 is R..." */
6706 /* FIXME: Do we need an exception for characters from display
6707 tables? */
6708 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6709 it->c = bidi_mirror_char (it->c);
6710 /* Map via display table or translate control characters.
6711 IT->c, IT->len etc. have been set to the next character by
6712 the function call above. If we have a display table, and it
6713 contains an entry for IT->c, translate it. Don't do this if
6714 IT->c itself comes from a display table, otherwise we could
6715 end up in an infinite recursion. (An alternative could be to
6716 count the recursion depth of this function and signal an
6717 error when a certain maximum depth is reached.) Is it worth
6718 it? */
6719 if (success_p && it->dpvec == NULL)
6721 Lisp_Object dv;
6722 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6723 int nonascii_space_p = 0;
6724 int nonascii_hyphen_p = 0;
6725 int c = it->c; /* This is the character to display. */
6727 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6729 eassert (SINGLE_BYTE_CHAR_P (c));
6730 if (unibyte_display_via_language_environment)
6732 c = DECODE_CHAR (unibyte, c);
6733 if (c < 0)
6734 c = BYTE8_TO_CHAR (it->c);
6736 else
6737 c = BYTE8_TO_CHAR (it->c);
6740 if (it->dp
6741 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6742 VECTORP (dv)))
6744 struct Lisp_Vector *v = XVECTOR (dv);
6746 /* Return the first character from the display table
6747 entry, if not empty. If empty, don't display the
6748 current character. */
6749 if (v->header.size)
6751 it->dpvec_char_len = it->len;
6752 it->dpvec = v->contents;
6753 it->dpend = v->contents + v->header.size;
6754 it->current.dpvec_index = 0;
6755 it->dpvec_face_id = -1;
6756 it->saved_face_id = it->face_id;
6757 it->method = GET_FROM_DISPLAY_VECTOR;
6758 it->ellipsis_p = 0;
6760 else
6762 set_iterator_to_next (it, 0);
6764 goto get_next;
6767 if (! NILP (lookup_glyphless_char_display (c, it)))
6769 if (it->what == IT_GLYPHLESS)
6770 goto done;
6771 /* Don't display this character. */
6772 set_iterator_to_next (it, 0);
6773 goto get_next;
6776 /* If `nobreak-char-display' is non-nil, we display
6777 non-ASCII spaces and hyphens specially. */
6778 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6780 if (c == 0xA0)
6781 nonascii_space_p = 1;
6782 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6783 nonascii_hyphen_p = 1;
6786 /* Translate control characters into `\003' or `^C' form.
6787 Control characters coming from a display table entry are
6788 currently not translated because we use IT->dpvec to hold
6789 the translation. This could easily be changed but I
6790 don't believe that it is worth doing.
6792 The characters handled by `nobreak-char-display' must be
6793 translated too.
6795 Non-printable characters and raw-byte characters are also
6796 translated to octal form. */
6797 if (((c < ' ' || c == 127) /* ASCII control chars */
6798 ? (it->area != TEXT_AREA
6799 /* In mode line, treat \n, \t like other crl chars. */
6800 || (c != '\t'
6801 && it->glyph_row
6802 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6803 || (c != '\n' && c != '\t'))
6804 : (nonascii_space_p
6805 || nonascii_hyphen_p
6806 || CHAR_BYTE8_P (c)
6807 || ! CHAR_PRINTABLE_P (c))))
6809 /* C is a control character, non-ASCII space/hyphen,
6810 raw-byte, or a non-printable character which must be
6811 displayed either as '\003' or as `^C' where the '\\'
6812 and '^' can be defined in the display table. Fill
6813 IT->ctl_chars with glyphs for what we have to
6814 display. Then, set IT->dpvec to these glyphs. */
6815 Lisp_Object gc;
6816 int ctl_len;
6817 int face_id;
6818 int lface_id = 0;
6819 int escape_glyph;
6821 /* Handle control characters with ^. */
6823 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6825 int g;
6827 g = '^'; /* default glyph for Control */
6828 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6829 if (it->dp
6830 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6832 g = GLYPH_CODE_CHAR (gc);
6833 lface_id = GLYPH_CODE_FACE (gc);
6835 if (lface_id)
6837 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6839 else if (it->f == last_escape_glyph_frame
6840 && it->face_id == last_escape_glyph_face_id)
6842 face_id = last_escape_glyph_merged_face_id;
6844 else
6846 /* Merge the escape-glyph face into the current face. */
6847 face_id = merge_faces (it->f, Qescape_glyph, 0,
6848 it->face_id);
6849 last_escape_glyph_frame = it->f;
6850 last_escape_glyph_face_id = it->face_id;
6851 last_escape_glyph_merged_face_id = face_id;
6854 XSETINT (it->ctl_chars[0], g);
6855 XSETINT (it->ctl_chars[1], c ^ 0100);
6856 ctl_len = 2;
6857 goto display_control;
6860 /* Handle non-ascii space in the mode where it only gets
6861 highlighting. */
6863 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6865 /* Merge `nobreak-space' into the current face. */
6866 face_id = merge_faces (it->f, Qnobreak_space, 0,
6867 it->face_id);
6868 XSETINT (it->ctl_chars[0], ' ');
6869 ctl_len = 1;
6870 goto display_control;
6873 /* Handle sequences that start with the "escape glyph". */
6875 /* the default escape glyph is \. */
6876 escape_glyph = '\\';
6878 if (it->dp
6879 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6881 escape_glyph = GLYPH_CODE_CHAR (gc);
6882 lface_id = GLYPH_CODE_FACE (gc);
6884 if (lface_id)
6886 /* The display table specified a face.
6887 Merge it into face_id and also into escape_glyph. */
6888 face_id = merge_faces (it->f, Qt, lface_id,
6889 it->face_id);
6891 else if (it->f == last_escape_glyph_frame
6892 && it->face_id == last_escape_glyph_face_id)
6894 face_id = last_escape_glyph_merged_face_id;
6896 else
6898 /* Merge the escape-glyph face into the current face. */
6899 face_id = merge_faces (it->f, Qescape_glyph, 0,
6900 it->face_id);
6901 last_escape_glyph_frame = it->f;
6902 last_escape_glyph_face_id = it->face_id;
6903 last_escape_glyph_merged_face_id = face_id;
6906 /* Draw non-ASCII hyphen with just highlighting: */
6908 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6910 XSETINT (it->ctl_chars[0], '-');
6911 ctl_len = 1;
6912 goto display_control;
6915 /* Draw non-ASCII space/hyphen with escape glyph: */
6917 if (nonascii_space_p || nonascii_hyphen_p)
6919 XSETINT (it->ctl_chars[0], escape_glyph);
6920 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6921 ctl_len = 2;
6922 goto display_control;
6926 char str[10];
6927 int len, i;
6929 if (CHAR_BYTE8_P (c))
6930 /* Display \200 instead of \17777600. */
6931 c = CHAR_TO_BYTE8 (c);
6932 len = sprintf (str, "%03o", c);
6934 XSETINT (it->ctl_chars[0], escape_glyph);
6935 for (i = 0; i < len; i++)
6936 XSETINT (it->ctl_chars[i + 1], str[i]);
6937 ctl_len = len + 1;
6940 display_control:
6941 /* Set up IT->dpvec and return first character from it. */
6942 it->dpvec_char_len = it->len;
6943 it->dpvec = it->ctl_chars;
6944 it->dpend = it->dpvec + ctl_len;
6945 it->current.dpvec_index = 0;
6946 it->dpvec_face_id = face_id;
6947 it->saved_face_id = it->face_id;
6948 it->method = GET_FROM_DISPLAY_VECTOR;
6949 it->ellipsis_p = 0;
6950 goto get_next;
6952 it->char_to_display = c;
6954 else if (success_p)
6956 it->char_to_display = it->c;
6960 /* Adjust face id for a multibyte character. There are no multibyte
6961 character in unibyte text. */
6962 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6963 && it->multibyte_p
6964 && success_p
6965 && FRAME_WINDOW_P (it->f))
6967 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6969 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6971 /* Automatic composition with glyph-string. */
6972 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6974 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6976 else
6978 ptrdiff_t pos = (it->s ? -1
6979 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6980 : IT_CHARPOS (*it));
6981 int c;
6983 if (it->what == IT_CHARACTER)
6984 c = it->char_to_display;
6985 else
6987 struct composition *cmp = composition_table[it->cmp_it.id];
6988 int i;
6990 c = ' ';
6991 for (i = 0; i < cmp->glyph_len; i++)
6992 /* TAB in a composition means display glyphs with
6993 padding space on the left or right. */
6994 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6995 break;
6997 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7001 done:
7002 /* Is this character the last one of a run of characters with
7003 box? If yes, set IT->end_of_box_run_p to 1. */
7004 if (it->face_box_p
7005 && it->s == NULL)
7007 if (it->method == GET_FROM_STRING && it->sp)
7009 int face_id = underlying_face_id (it);
7010 struct face *face = FACE_FROM_ID (it->f, face_id);
7012 if (face)
7014 if (face->box == FACE_NO_BOX)
7016 /* If the box comes from face properties in a
7017 display string, check faces in that string. */
7018 int string_face_id = face_after_it_pos (it);
7019 it->end_of_box_run_p
7020 = (FACE_FROM_ID (it->f, string_face_id)->box
7021 == FACE_NO_BOX);
7023 /* Otherwise, the box comes from the underlying face.
7024 If this is the last string character displayed, check
7025 the next buffer location. */
7026 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7027 && (it->current.overlay_string_index
7028 == it->n_overlay_strings - 1))
7030 ptrdiff_t ignore;
7031 int next_face_id;
7032 struct text_pos pos = it->current.pos;
7033 INC_TEXT_POS (pos, it->multibyte_p);
7035 next_face_id = face_at_buffer_position
7036 (it->w, CHARPOS (pos), it->region_beg_charpos,
7037 it->region_end_charpos, &ignore,
7038 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7039 -1);
7040 it->end_of_box_run_p
7041 = (FACE_FROM_ID (it->f, next_face_id)->box
7042 == FACE_NO_BOX);
7046 else
7048 int face_id = face_after_it_pos (it);
7049 it->end_of_box_run_p
7050 = (face_id != it->face_id
7051 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7054 /* If we reached the end of the object we've been iterating (e.g., a
7055 display string or an overlay string), and there's something on
7056 IT->stack, proceed with what's on the stack. It doesn't make
7057 sense to return zero if there's unprocessed stuff on the stack,
7058 because otherwise that stuff will never be displayed. */
7059 if (!success_p && it->sp > 0)
7061 set_iterator_to_next (it, 0);
7062 success_p = get_next_display_element (it);
7065 /* Value is 0 if end of buffer or string reached. */
7066 return success_p;
7070 /* Move IT to the next display element.
7072 RESEAT_P non-zero means if called on a newline in buffer text,
7073 skip to the next visible line start.
7075 Functions get_next_display_element and set_iterator_to_next are
7076 separate because I find this arrangement easier to handle than a
7077 get_next_display_element function that also increments IT's
7078 position. The way it is we can first look at an iterator's current
7079 display element, decide whether it fits on a line, and if it does,
7080 increment the iterator position. The other way around we probably
7081 would either need a flag indicating whether the iterator has to be
7082 incremented the next time, or we would have to implement a
7083 decrement position function which would not be easy to write. */
7085 void
7086 set_iterator_to_next (struct it *it, int reseat_p)
7088 /* Reset flags indicating start and end of a sequence of characters
7089 with box. Reset them at the start of this function because
7090 moving the iterator to a new position might set them. */
7091 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7093 switch (it->method)
7095 case GET_FROM_BUFFER:
7096 /* The current display element of IT is a character from
7097 current_buffer. Advance in the buffer, and maybe skip over
7098 invisible lines that are so because of selective display. */
7099 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7100 reseat_at_next_visible_line_start (it, 0);
7101 else if (it->cmp_it.id >= 0)
7103 /* We are currently getting glyphs from a composition. */
7104 int i;
7106 if (! it->bidi_p)
7108 IT_CHARPOS (*it) += it->cmp_it.nchars;
7109 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7110 if (it->cmp_it.to < it->cmp_it.nglyphs)
7112 it->cmp_it.from = it->cmp_it.to;
7114 else
7116 it->cmp_it.id = -1;
7117 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7118 IT_BYTEPOS (*it),
7119 it->end_charpos, Qnil);
7122 else if (! it->cmp_it.reversed_p)
7124 /* Composition created while scanning forward. */
7125 /* Update IT's char/byte positions to point to the first
7126 character of the next grapheme cluster, or to the
7127 character visually after the current composition. */
7128 for (i = 0; i < it->cmp_it.nchars; i++)
7129 bidi_move_to_visually_next (&it->bidi_it);
7130 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7131 IT_CHARPOS (*it) = it->bidi_it.charpos;
7133 if (it->cmp_it.to < it->cmp_it.nglyphs)
7135 /* Proceed to the next grapheme cluster. */
7136 it->cmp_it.from = it->cmp_it.to;
7138 else
7140 /* No more grapheme clusters in this composition.
7141 Find the next stop position. */
7142 ptrdiff_t stop = it->end_charpos;
7143 if (it->bidi_it.scan_dir < 0)
7144 /* Now we are scanning backward and don't know
7145 where to stop. */
7146 stop = -1;
7147 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7148 IT_BYTEPOS (*it), stop, Qnil);
7151 else
7153 /* Composition created while scanning backward. */
7154 /* Update IT's char/byte positions to point to the last
7155 character of the previous grapheme cluster, or the
7156 character visually after the current composition. */
7157 for (i = 0; i < it->cmp_it.nchars; i++)
7158 bidi_move_to_visually_next (&it->bidi_it);
7159 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7160 IT_CHARPOS (*it) = it->bidi_it.charpos;
7161 if (it->cmp_it.from > 0)
7163 /* Proceed to the previous grapheme cluster. */
7164 it->cmp_it.to = it->cmp_it.from;
7166 else
7168 /* No more grapheme clusters in this composition.
7169 Find the next stop position. */
7170 ptrdiff_t stop = it->end_charpos;
7171 if (it->bidi_it.scan_dir < 0)
7172 /* Now we are scanning backward and don't know
7173 where to stop. */
7174 stop = -1;
7175 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7176 IT_BYTEPOS (*it), stop, Qnil);
7180 else
7182 eassert (it->len != 0);
7184 if (!it->bidi_p)
7186 IT_BYTEPOS (*it) += it->len;
7187 IT_CHARPOS (*it) += 1;
7189 else
7191 int prev_scan_dir = it->bidi_it.scan_dir;
7192 /* If this is a new paragraph, determine its base
7193 direction (a.k.a. its base embedding level). */
7194 if (it->bidi_it.new_paragraph)
7195 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7196 bidi_move_to_visually_next (&it->bidi_it);
7197 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7198 IT_CHARPOS (*it) = it->bidi_it.charpos;
7199 if (prev_scan_dir != it->bidi_it.scan_dir)
7201 /* As the scan direction was changed, we must
7202 re-compute the stop position for composition. */
7203 ptrdiff_t stop = it->end_charpos;
7204 if (it->bidi_it.scan_dir < 0)
7205 stop = -1;
7206 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7207 IT_BYTEPOS (*it), stop, Qnil);
7210 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7212 break;
7214 case GET_FROM_C_STRING:
7215 /* Current display element of IT is from a C string. */
7216 if (!it->bidi_p
7217 /* If the string position is beyond string's end, it means
7218 next_element_from_c_string is padding the string with
7219 blanks, in which case we bypass the bidi iterator,
7220 because it cannot deal with such virtual characters. */
7221 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7223 IT_BYTEPOS (*it) += it->len;
7224 IT_CHARPOS (*it) += 1;
7226 else
7228 bidi_move_to_visually_next (&it->bidi_it);
7229 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7230 IT_CHARPOS (*it) = it->bidi_it.charpos;
7232 break;
7234 case GET_FROM_DISPLAY_VECTOR:
7235 /* Current display element of IT is from a display table entry.
7236 Advance in the display table definition. Reset it to null if
7237 end reached, and continue with characters from buffers/
7238 strings. */
7239 ++it->current.dpvec_index;
7241 /* Restore face of the iterator to what they were before the
7242 display vector entry (these entries may contain faces). */
7243 it->face_id = it->saved_face_id;
7245 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7247 int recheck_faces = it->ellipsis_p;
7249 if (it->s)
7250 it->method = GET_FROM_C_STRING;
7251 else if (STRINGP (it->string))
7252 it->method = GET_FROM_STRING;
7253 else
7255 it->method = GET_FROM_BUFFER;
7256 it->object = it->w->contents;
7259 it->dpvec = NULL;
7260 it->current.dpvec_index = -1;
7262 /* Skip over characters which were displayed via IT->dpvec. */
7263 if (it->dpvec_char_len < 0)
7264 reseat_at_next_visible_line_start (it, 1);
7265 else if (it->dpvec_char_len > 0)
7267 if (it->method == GET_FROM_STRING
7268 && it->current.overlay_string_index >= 0
7269 && it->n_overlay_strings > 0)
7270 it->ignore_overlay_strings_at_pos_p = 1;
7271 it->len = it->dpvec_char_len;
7272 set_iterator_to_next (it, reseat_p);
7275 /* Maybe recheck faces after display vector */
7276 if (recheck_faces)
7277 it->stop_charpos = IT_CHARPOS (*it);
7279 break;
7281 case GET_FROM_STRING:
7282 /* Current display element is a character from a Lisp string. */
7283 eassert (it->s == NULL && STRINGP (it->string));
7284 /* Don't advance past string end. These conditions are true
7285 when set_iterator_to_next is called at the end of
7286 get_next_display_element, in which case the Lisp string is
7287 already exhausted, and all we want is pop the iterator
7288 stack. */
7289 if (it->current.overlay_string_index >= 0)
7291 /* This is an overlay string, so there's no padding with
7292 spaces, and the number of characters in the string is
7293 where the string ends. */
7294 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7295 goto consider_string_end;
7297 else
7299 /* Not an overlay string. There could be padding, so test
7300 against it->end_charpos . */
7301 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7302 goto consider_string_end;
7304 if (it->cmp_it.id >= 0)
7306 int i;
7308 if (! it->bidi_p)
7310 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7311 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7312 if (it->cmp_it.to < it->cmp_it.nglyphs)
7313 it->cmp_it.from = it->cmp_it.to;
7314 else
7316 it->cmp_it.id = -1;
7317 composition_compute_stop_pos (&it->cmp_it,
7318 IT_STRING_CHARPOS (*it),
7319 IT_STRING_BYTEPOS (*it),
7320 it->end_charpos, it->string);
7323 else if (! it->cmp_it.reversed_p)
7325 for (i = 0; i < it->cmp_it.nchars; i++)
7326 bidi_move_to_visually_next (&it->bidi_it);
7327 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7328 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7330 if (it->cmp_it.to < it->cmp_it.nglyphs)
7331 it->cmp_it.from = it->cmp_it.to;
7332 else
7334 ptrdiff_t stop = it->end_charpos;
7335 if (it->bidi_it.scan_dir < 0)
7336 stop = -1;
7337 composition_compute_stop_pos (&it->cmp_it,
7338 IT_STRING_CHARPOS (*it),
7339 IT_STRING_BYTEPOS (*it), stop,
7340 it->string);
7343 else
7345 for (i = 0; i < it->cmp_it.nchars; i++)
7346 bidi_move_to_visually_next (&it->bidi_it);
7347 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7348 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7349 if (it->cmp_it.from > 0)
7350 it->cmp_it.to = it->cmp_it.from;
7351 else
7353 ptrdiff_t stop = it->end_charpos;
7354 if (it->bidi_it.scan_dir < 0)
7355 stop = -1;
7356 composition_compute_stop_pos (&it->cmp_it,
7357 IT_STRING_CHARPOS (*it),
7358 IT_STRING_BYTEPOS (*it), stop,
7359 it->string);
7363 else
7365 if (!it->bidi_p
7366 /* If the string position is beyond string's end, it
7367 means next_element_from_string is padding the string
7368 with blanks, in which case we bypass the bidi
7369 iterator, because it cannot deal with such virtual
7370 characters. */
7371 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7373 IT_STRING_BYTEPOS (*it) += it->len;
7374 IT_STRING_CHARPOS (*it) += 1;
7376 else
7378 int prev_scan_dir = it->bidi_it.scan_dir;
7380 bidi_move_to_visually_next (&it->bidi_it);
7381 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7382 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7383 if (prev_scan_dir != it->bidi_it.scan_dir)
7385 ptrdiff_t stop = it->end_charpos;
7387 if (it->bidi_it.scan_dir < 0)
7388 stop = -1;
7389 composition_compute_stop_pos (&it->cmp_it,
7390 IT_STRING_CHARPOS (*it),
7391 IT_STRING_BYTEPOS (*it), stop,
7392 it->string);
7397 consider_string_end:
7399 if (it->current.overlay_string_index >= 0)
7401 /* IT->string is an overlay string. Advance to the
7402 next, if there is one. */
7403 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7405 it->ellipsis_p = 0;
7406 next_overlay_string (it);
7407 if (it->ellipsis_p)
7408 setup_for_ellipsis (it, 0);
7411 else
7413 /* IT->string is not an overlay string. If we reached
7414 its end, and there is something on IT->stack, proceed
7415 with what is on the stack. This can be either another
7416 string, this time an overlay string, or a buffer. */
7417 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7418 && it->sp > 0)
7420 pop_it (it);
7421 if (it->method == GET_FROM_STRING)
7422 goto consider_string_end;
7425 break;
7427 case GET_FROM_IMAGE:
7428 case GET_FROM_STRETCH:
7429 /* The position etc with which we have to proceed are on
7430 the stack. The position may be at the end of a string,
7431 if the `display' property takes up the whole string. */
7432 eassert (it->sp > 0);
7433 pop_it (it);
7434 if (it->method == GET_FROM_STRING)
7435 goto consider_string_end;
7436 break;
7438 default:
7439 /* There are no other methods defined, so this should be a bug. */
7440 emacs_abort ();
7443 eassert (it->method != GET_FROM_STRING
7444 || (STRINGP (it->string)
7445 && IT_STRING_CHARPOS (*it) >= 0));
7448 /* Load IT's display element fields with information about the next
7449 display element which comes from a display table entry or from the
7450 result of translating a control character to one of the forms `^C'
7451 or `\003'.
7453 IT->dpvec holds the glyphs to return as characters.
7454 IT->saved_face_id holds the face id before the display vector--it
7455 is restored into IT->face_id in set_iterator_to_next. */
7457 static int
7458 next_element_from_display_vector (struct it *it)
7460 Lisp_Object gc;
7462 /* Precondition. */
7463 eassert (it->dpvec && it->current.dpvec_index >= 0);
7465 it->face_id = it->saved_face_id;
7467 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7468 That seemed totally bogus - so I changed it... */
7469 gc = it->dpvec[it->current.dpvec_index];
7471 if (GLYPH_CODE_P (gc))
7473 it->c = GLYPH_CODE_CHAR (gc);
7474 it->len = CHAR_BYTES (it->c);
7476 /* The entry may contain a face id to use. Such a face id is
7477 the id of a Lisp face, not a realized face. A face id of
7478 zero means no face is specified. */
7479 if (it->dpvec_face_id >= 0)
7480 it->face_id = it->dpvec_face_id;
7481 else
7483 int lface_id = GLYPH_CODE_FACE (gc);
7484 if (lface_id > 0)
7485 it->face_id = merge_faces (it->f, Qt, lface_id,
7486 it->saved_face_id);
7489 else
7490 /* Display table entry is invalid. Return a space. */
7491 it->c = ' ', it->len = 1;
7493 /* Don't change position and object of the iterator here. They are
7494 still the values of the character that had this display table
7495 entry or was translated, and that's what we want. */
7496 it->what = IT_CHARACTER;
7497 return 1;
7500 /* Get the first element of string/buffer in the visual order, after
7501 being reseated to a new position in a string or a buffer. */
7502 static void
7503 get_visually_first_element (struct it *it)
7505 int string_p = STRINGP (it->string) || it->s;
7506 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7507 ptrdiff_t bob = (string_p ? 0 : BEGV);
7509 if (STRINGP (it->string))
7511 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7512 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7514 else
7516 it->bidi_it.charpos = IT_CHARPOS (*it);
7517 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7520 if (it->bidi_it.charpos == eob)
7522 /* Nothing to do, but reset the FIRST_ELT flag, like
7523 bidi_paragraph_init does, because we are not going to
7524 call it. */
7525 it->bidi_it.first_elt = 0;
7527 else if (it->bidi_it.charpos == bob
7528 || (!string_p
7529 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7530 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7532 /* If we are at the beginning of a line/string, we can produce
7533 the next element right away. */
7534 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7535 bidi_move_to_visually_next (&it->bidi_it);
7537 else
7539 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7541 /* We need to prime the bidi iterator starting at the line's or
7542 string's beginning, before we will be able to produce the
7543 next element. */
7544 if (string_p)
7545 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7546 else
7547 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7548 IT_BYTEPOS (*it), -1,
7549 &it->bidi_it.bytepos);
7550 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7553 /* Now return to buffer/string position where we were asked
7554 to get the next display element, and produce that. */
7555 bidi_move_to_visually_next (&it->bidi_it);
7557 while (it->bidi_it.bytepos != orig_bytepos
7558 && it->bidi_it.charpos < eob);
7561 /* Adjust IT's position information to where we ended up. */
7562 if (STRINGP (it->string))
7564 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7565 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7567 else
7569 IT_CHARPOS (*it) = it->bidi_it.charpos;
7570 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7573 if (STRINGP (it->string) || !it->s)
7575 ptrdiff_t stop, charpos, bytepos;
7577 if (STRINGP (it->string))
7579 eassert (!it->s);
7580 stop = SCHARS (it->string);
7581 if (stop > it->end_charpos)
7582 stop = it->end_charpos;
7583 charpos = IT_STRING_CHARPOS (*it);
7584 bytepos = IT_STRING_BYTEPOS (*it);
7586 else
7588 stop = it->end_charpos;
7589 charpos = IT_CHARPOS (*it);
7590 bytepos = IT_BYTEPOS (*it);
7592 if (it->bidi_it.scan_dir < 0)
7593 stop = -1;
7594 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7595 it->string);
7599 /* Load IT with the next display element from Lisp string IT->string.
7600 IT->current.string_pos is the current position within the string.
7601 If IT->current.overlay_string_index >= 0, the Lisp string is an
7602 overlay string. */
7604 static int
7605 next_element_from_string (struct it *it)
7607 struct text_pos position;
7609 eassert (STRINGP (it->string));
7610 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7611 eassert (IT_STRING_CHARPOS (*it) >= 0);
7612 position = it->current.string_pos;
7614 /* With bidi reordering, the character to display might not be the
7615 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7616 that we were reseat()ed to a new string, whose paragraph
7617 direction is not known. */
7618 if (it->bidi_p && it->bidi_it.first_elt)
7620 get_visually_first_element (it);
7621 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7624 /* Time to check for invisible text? */
7625 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7627 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7629 if (!(!it->bidi_p
7630 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7631 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7633 /* With bidi non-linear iteration, we could find
7634 ourselves far beyond the last computed stop_charpos,
7635 with several other stop positions in between that we
7636 missed. Scan them all now, in buffer's logical
7637 order, until we find and handle the last stop_charpos
7638 that precedes our current position. */
7639 handle_stop_backwards (it, it->stop_charpos);
7640 return GET_NEXT_DISPLAY_ELEMENT (it);
7642 else
7644 if (it->bidi_p)
7646 /* Take note of the stop position we just moved
7647 across, for when we will move back across it. */
7648 it->prev_stop = it->stop_charpos;
7649 /* If we are at base paragraph embedding level, take
7650 note of the last stop position seen at this
7651 level. */
7652 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7653 it->base_level_stop = it->stop_charpos;
7655 handle_stop (it);
7657 /* Since a handler may have changed IT->method, we must
7658 recurse here. */
7659 return GET_NEXT_DISPLAY_ELEMENT (it);
7662 else if (it->bidi_p
7663 /* If we are before prev_stop, we may have overstepped
7664 on our way backwards a stop_pos, and if so, we need
7665 to handle that stop_pos. */
7666 && IT_STRING_CHARPOS (*it) < it->prev_stop
7667 /* We can sometimes back up for reasons that have nothing
7668 to do with bidi reordering. E.g., compositions. The
7669 code below is only needed when we are above the base
7670 embedding level, so test for that explicitly. */
7671 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7673 /* If we lost track of base_level_stop, we have no better
7674 place for handle_stop_backwards to start from than string
7675 beginning. This happens, e.g., when we were reseated to
7676 the previous screenful of text by vertical-motion. */
7677 if (it->base_level_stop <= 0
7678 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7679 it->base_level_stop = 0;
7680 handle_stop_backwards (it, it->base_level_stop);
7681 return GET_NEXT_DISPLAY_ELEMENT (it);
7685 if (it->current.overlay_string_index >= 0)
7687 /* Get the next character from an overlay string. In overlay
7688 strings, there is no field width or padding with spaces to
7689 do. */
7690 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7692 it->what = IT_EOB;
7693 return 0;
7695 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7696 IT_STRING_BYTEPOS (*it),
7697 it->bidi_it.scan_dir < 0
7698 ? -1
7699 : SCHARS (it->string))
7700 && next_element_from_composition (it))
7702 return 1;
7704 else if (STRING_MULTIBYTE (it->string))
7706 const unsigned char *s = (SDATA (it->string)
7707 + IT_STRING_BYTEPOS (*it));
7708 it->c = string_char_and_length (s, &it->len);
7710 else
7712 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7713 it->len = 1;
7716 else
7718 /* Get the next character from a Lisp string that is not an
7719 overlay string. Such strings come from the mode line, for
7720 example. We may have to pad with spaces, or truncate the
7721 string. See also next_element_from_c_string. */
7722 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7724 it->what = IT_EOB;
7725 return 0;
7727 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7729 /* Pad with spaces. */
7730 it->c = ' ', it->len = 1;
7731 CHARPOS (position) = BYTEPOS (position) = -1;
7733 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7734 IT_STRING_BYTEPOS (*it),
7735 it->bidi_it.scan_dir < 0
7736 ? -1
7737 : it->string_nchars)
7738 && next_element_from_composition (it))
7740 return 1;
7742 else if (STRING_MULTIBYTE (it->string))
7744 const unsigned char *s = (SDATA (it->string)
7745 + IT_STRING_BYTEPOS (*it));
7746 it->c = string_char_and_length (s, &it->len);
7748 else
7750 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7751 it->len = 1;
7755 /* Record what we have and where it came from. */
7756 it->what = IT_CHARACTER;
7757 it->object = it->string;
7758 it->position = position;
7759 return 1;
7763 /* Load IT with next display element from C string IT->s.
7764 IT->string_nchars is the maximum number of characters to return
7765 from the string. IT->end_charpos may be greater than
7766 IT->string_nchars when this function is called, in which case we
7767 may have to return padding spaces. Value is zero if end of string
7768 reached, including padding spaces. */
7770 static int
7771 next_element_from_c_string (struct it *it)
7773 int success_p = 1;
7775 eassert (it->s);
7776 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7777 it->what = IT_CHARACTER;
7778 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7779 it->object = Qnil;
7781 /* With bidi reordering, the character to display might not be the
7782 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7783 we were reseated to a new string, whose paragraph direction is
7784 not known. */
7785 if (it->bidi_p && it->bidi_it.first_elt)
7786 get_visually_first_element (it);
7788 /* IT's position can be greater than IT->string_nchars in case a
7789 field width or precision has been specified when the iterator was
7790 initialized. */
7791 if (IT_CHARPOS (*it) >= it->end_charpos)
7793 /* End of the game. */
7794 it->what = IT_EOB;
7795 success_p = 0;
7797 else if (IT_CHARPOS (*it) >= it->string_nchars)
7799 /* Pad with spaces. */
7800 it->c = ' ', it->len = 1;
7801 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7803 else if (it->multibyte_p)
7804 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7805 else
7806 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7808 return success_p;
7812 /* Set up IT to return characters from an ellipsis, if appropriate.
7813 The definition of the ellipsis glyphs may come from a display table
7814 entry. This function fills IT with the first glyph from the
7815 ellipsis if an ellipsis is to be displayed. */
7817 static int
7818 next_element_from_ellipsis (struct it *it)
7820 if (it->selective_display_ellipsis_p)
7821 setup_for_ellipsis (it, it->len);
7822 else
7824 /* The face at the current position may be different from the
7825 face we find after the invisible text. Remember what it
7826 was in IT->saved_face_id, and signal that it's there by
7827 setting face_before_selective_p. */
7828 it->saved_face_id = it->face_id;
7829 it->method = GET_FROM_BUFFER;
7830 it->object = it->w->contents;
7831 reseat_at_next_visible_line_start (it, 1);
7832 it->face_before_selective_p = 1;
7835 return GET_NEXT_DISPLAY_ELEMENT (it);
7839 /* Deliver an image display element. The iterator IT is already
7840 filled with image information (done in handle_display_prop). Value
7841 is always 1. */
7844 static int
7845 next_element_from_image (struct it *it)
7847 it->what = IT_IMAGE;
7848 it->ignore_overlay_strings_at_pos_p = 0;
7849 return 1;
7853 /* Fill iterator IT with next display element from a stretch glyph
7854 property. IT->object is the value of the text property. Value is
7855 always 1. */
7857 static int
7858 next_element_from_stretch (struct it *it)
7860 it->what = IT_STRETCH;
7861 return 1;
7864 /* Scan backwards from IT's current position until we find a stop
7865 position, or until BEGV. This is called when we find ourself
7866 before both the last known prev_stop and base_level_stop while
7867 reordering bidirectional text. */
7869 static void
7870 compute_stop_pos_backwards (struct it *it)
7872 const int SCAN_BACK_LIMIT = 1000;
7873 struct text_pos pos;
7874 struct display_pos save_current = it->current;
7875 struct text_pos save_position = it->position;
7876 ptrdiff_t charpos = IT_CHARPOS (*it);
7877 ptrdiff_t where_we_are = charpos;
7878 ptrdiff_t save_stop_pos = it->stop_charpos;
7879 ptrdiff_t save_end_pos = it->end_charpos;
7881 eassert (NILP (it->string) && !it->s);
7882 eassert (it->bidi_p);
7883 it->bidi_p = 0;
7886 it->end_charpos = min (charpos + 1, ZV);
7887 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7888 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7889 reseat_1 (it, pos, 0);
7890 compute_stop_pos (it);
7891 /* We must advance forward, right? */
7892 if (it->stop_charpos <= charpos)
7893 emacs_abort ();
7895 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7897 if (it->stop_charpos <= where_we_are)
7898 it->prev_stop = it->stop_charpos;
7899 else
7900 it->prev_stop = BEGV;
7901 it->bidi_p = 1;
7902 it->current = save_current;
7903 it->position = save_position;
7904 it->stop_charpos = save_stop_pos;
7905 it->end_charpos = save_end_pos;
7908 /* Scan forward from CHARPOS in the current buffer/string, until we
7909 find a stop position > current IT's position. Then handle the stop
7910 position before that. This is called when we bump into a stop
7911 position while reordering bidirectional text. CHARPOS should be
7912 the last previously processed stop_pos (or BEGV/0, if none were
7913 processed yet) whose position is less that IT's current
7914 position. */
7916 static void
7917 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7919 int bufp = !STRINGP (it->string);
7920 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7921 struct display_pos save_current = it->current;
7922 struct text_pos save_position = it->position;
7923 struct text_pos pos1;
7924 ptrdiff_t next_stop;
7926 /* Scan in strict logical order. */
7927 eassert (it->bidi_p);
7928 it->bidi_p = 0;
7931 it->prev_stop = charpos;
7932 if (bufp)
7934 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7935 reseat_1 (it, pos1, 0);
7937 else
7938 it->current.string_pos = string_pos (charpos, it->string);
7939 compute_stop_pos (it);
7940 /* We must advance forward, right? */
7941 if (it->stop_charpos <= it->prev_stop)
7942 emacs_abort ();
7943 charpos = it->stop_charpos;
7945 while (charpos <= where_we_are);
7947 it->bidi_p = 1;
7948 it->current = save_current;
7949 it->position = save_position;
7950 next_stop = it->stop_charpos;
7951 it->stop_charpos = it->prev_stop;
7952 handle_stop (it);
7953 it->stop_charpos = next_stop;
7956 /* Load IT with the next display element from current_buffer. Value
7957 is zero if end of buffer reached. IT->stop_charpos is the next
7958 position at which to stop and check for text properties or buffer
7959 end. */
7961 static int
7962 next_element_from_buffer (struct it *it)
7964 int success_p = 1;
7966 eassert (IT_CHARPOS (*it) >= BEGV);
7967 eassert (NILP (it->string) && !it->s);
7968 eassert (!it->bidi_p
7969 || (EQ (it->bidi_it.string.lstring, Qnil)
7970 && it->bidi_it.string.s == NULL));
7972 /* With bidi reordering, the character to display might not be the
7973 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7974 we were reseat()ed to a new buffer position, which is potentially
7975 a different paragraph. */
7976 if (it->bidi_p && it->bidi_it.first_elt)
7978 get_visually_first_element (it);
7979 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7982 if (IT_CHARPOS (*it) >= it->stop_charpos)
7984 if (IT_CHARPOS (*it) >= it->end_charpos)
7986 int overlay_strings_follow_p;
7988 /* End of the game, except when overlay strings follow that
7989 haven't been returned yet. */
7990 if (it->overlay_strings_at_end_processed_p)
7991 overlay_strings_follow_p = 0;
7992 else
7994 it->overlay_strings_at_end_processed_p = 1;
7995 overlay_strings_follow_p = get_overlay_strings (it, 0);
7998 if (overlay_strings_follow_p)
7999 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8000 else
8002 it->what = IT_EOB;
8003 it->position = it->current.pos;
8004 success_p = 0;
8007 else if (!(!it->bidi_p
8008 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8009 || IT_CHARPOS (*it) == it->stop_charpos))
8011 /* With bidi non-linear iteration, we could find ourselves
8012 far beyond the last computed stop_charpos, with several
8013 other stop positions in between that we missed. Scan
8014 them all now, in buffer's logical order, until we find
8015 and handle the last stop_charpos that precedes our
8016 current position. */
8017 handle_stop_backwards (it, it->stop_charpos);
8018 return GET_NEXT_DISPLAY_ELEMENT (it);
8020 else
8022 if (it->bidi_p)
8024 /* Take note of the stop position we just moved across,
8025 for when we will move back across it. */
8026 it->prev_stop = it->stop_charpos;
8027 /* If we are at base paragraph embedding level, take
8028 note of the last stop position seen at this
8029 level. */
8030 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8031 it->base_level_stop = it->stop_charpos;
8033 handle_stop (it);
8034 return GET_NEXT_DISPLAY_ELEMENT (it);
8037 else if (it->bidi_p
8038 /* If we are before prev_stop, we may have overstepped on
8039 our way backwards a stop_pos, and if so, we need to
8040 handle that stop_pos. */
8041 && IT_CHARPOS (*it) < it->prev_stop
8042 /* We can sometimes back up for reasons that have nothing
8043 to do with bidi reordering. E.g., compositions. The
8044 code below is only needed when we are above the base
8045 embedding level, so test for that explicitly. */
8046 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8048 if (it->base_level_stop <= 0
8049 || IT_CHARPOS (*it) < it->base_level_stop)
8051 /* If we lost track of base_level_stop, we need to find
8052 prev_stop by looking backwards. This happens, e.g., when
8053 we were reseated to the previous screenful of text by
8054 vertical-motion. */
8055 it->base_level_stop = BEGV;
8056 compute_stop_pos_backwards (it);
8057 handle_stop_backwards (it, it->prev_stop);
8059 else
8060 handle_stop_backwards (it, it->base_level_stop);
8061 return GET_NEXT_DISPLAY_ELEMENT (it);
8063 else
8065 /* No face changes, overlays etc. in sight, so just return a
8066 character from current_buffer. */
8067 unsigned char *p;
8068 ptrdiff_t stop;
8070 /* Maybe run the redisplay end trigger hook. Performance note:
8071 This doesn't seem to cost measurable time. */
8072 if (it->redisplay_end_trigger_charpos
8073 && it->glyph_row
8074 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8075 run_redisplay_end_trigger_hook (it);
8077 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8078 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8079 stop)
8080 && next_element_from_composition (it))
8082 return 1;
8085 /* Get the next character, maybe multibyte. */
8086 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8087 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8088 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8089 else
8090 it->c = *p, it->len = 1;
8092 /* Record what we have and where it came from. */
8093 it->what = IT_CHARACTER;
8094 it->object = it->w->contents;
8095 it->position = it->current.pos;
8097 /* Normally we return the character found above, except when we
8098 really want to return an ellipsis for selective display. */
8099 if (it->selective)
8101 if (it->c == '\n')
8103 /* A value of selective > 0 means hide lines indented more
8104 than that number of columns. */
8105 if (it->selective > 0
8106 && IT_CHARPOS (*it) + 1 < ZV
8107 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8108 IT_BYTEPOS (*it) + 1,
8109 it->selective))
8111 success_p = next_element_from_ellipsis (it);
8112 it->dpvec_char_len = -1;
8115 else if (it->c == '\r' && it->selective == -1)
8117 /* A value of selective == -1 means that everything from the
8118 CR to the end of the line is invisible, with maybe an
8119 ellipsis displayed for it. */
8120 success_p = next_element_from_ellipsis (it);
8121 it->dpvec_char_len = -1;
8126 /* Value is zero if end of buffer reached. */
8127 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8128 return success_p;
8132 /* Run the redisplay end trigger hook for IT. */
8134 static void
8135 run_redisplay_end_trigger_hook (struct it *it)
8137 Lisp_Object args[3];
8139 /* IT->glyph_row should be non-null, i.e. we should be actually
8140 displaying something, or otherwise we should not run the hook. */
8141 eassert (it->glyph_row);
8143 /* Set up hook arguments. */
8144 args[0] = Qredisplay_end_trigger_functions;
8145 args[1] = it->window;
8146 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8147 it->redisplay_end_trigger_charpos = 0;
8149 /* Since we are *trying* to run these functions, don't try to run
8150 them again, even if they get an error. */
8151 wset_redisplay_end_trigger (it->w, Qnil);
8152 Frun_hook_with_args (3, args);
8154 /* Notice if it changed the face of the character we are on. */
8155 handle_face_prop (it);
8159 /* Deliver a composition display element. Unlike the other
8160 next_element_from_XXX, this function is not registered in the array
8161 get_next_element[]. It is called from next_element_from_buffer and
8162 next_element_from_string when necessary. */
8164 static int
8165 next_element_from_composition (struct it *it)
8167 it->what = IT_COMPOSITION;
8168 it->len = it->cmp_it.nbytes;
8169 if (STRINGP (it->string))
8171 if (it->c < 0)
8173 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8174 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8175 return 0;
8177 it->position = it->current.string_pos;
8178 it->object = it->string;
8179 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8180 IT_STRING_BYTEPOS (*it), it->string);
8182 else
8184 if (it->c < 0)
8186 IT_CHARPOS (*it) += it->cmp_it.nchars;
8187 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8188 if (it->bidi_p)
8190 if (it->bidi_it.new_paragraph)
8191 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8192 /* Resync the bidi iterator with IT's new position.
8193 FIXME: this doesn't support bidirectional text. */
8194 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8195 bidi_move_to_visually_next (&it->bidi_it);
8197 return 0;
8199 it->position = it->current.pos;
8200 it->object = it->w->contents;
8201 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8202 IT_BYTEPOS (*it), Qnil);
8204 return 1;
8209 /***********************************************************************
8210 Moving an iterator without producing glyphs
8211 ***********************************************************************/
8213 /* Check if iterator is at a position corresponding to a valid buffer
8214 position after some move_it_ call. */
8216 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8217 ((it)->method == GET_FROM_STRING \
8218 ? IT_STRING_CHARPOS (*it) == 0 \
8219 : 1)
8222 /* Move iterator IT to a specified buffer or X position within one
8223 line on the display without producing glyphs.
8225 OP should be a bit mask including some or all of these bits:
8226 MOVE_TO_X: Stop upon reaching x-position TO_X.
8227 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8228 Regardless of OP's value, stop upon reaching the end of the display line.
8230 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8231 This means, in particular, that TO_X includes window's horizontal
8232 scroll amount.
8234 The return value has several possible values that
8235 say what condition caused the scan to stop:
8237 MOVE_POS_MATCH_OR_ZV
8238 - when TO_POS or ZV was reached.
8240 MOVE_X_REACHED
8241 -when TO_X was reached before TO_POS or ZV were reached.
8243 MOVE_LINE_CONTINUED
8244 - when we reached the end of the display area and the line must
8245 be continued.
8247 MOVE_LINE_TRUNCATED
8248 - when we reached the end of the display area and the line is
8249 truncated.
8251 MOVE_NEWLINE_OR_CR
8252 - when we stopped at a line end, i.e. a newline or a CR and selective
8253 display is on. */
8255 static enum move_it_result
8256 move_it_in_display_line_to (struct it *it,
8257 ptrdiff_t to_charpos, int to_x,
8258 enum move_operation_enum op)
8260 enum move_it_result result = MOVE_UNDEFINED;
8261 struct glyph_row *saved_glyph_row;
8262 struct it wrap_it, atpos_it, atx_it, ppos_it;
8263 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8264 void *ppos_data = NULL;
8265 int may_wrap = 0;
8266 enum it_method prev_method = it->method;
8267 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8268 int saw_smaller_pos = prev_pos < to_charpos;
8270 /* Don't produce glyphs in produce_glyphs. */
8271 saved_glyph_row = it->glyph_row;
8272 it->glyph_row = NULL;
8274 /* Use wrap_it to save a copy of IT wherever a word wrap could
8275 occur. Use atpos_it to save a copy of IT at the desired buffer
8276 position, if found, so that we can scan ahead and check if the
8277 word later overshoots the window edge. Use atx_it similarly, for
8278 pixel positions. */
8279 wrap_it.sp = -1;
8280 atpos_it.sp = -1;
8281 atx_it.sp = -1;
8283 /* Use ppos_it under bidi reordering to save a copy of IT for the
8284 position > CHARPOS that is the closest to CHARPOS. We restore
8285 that position in IT when we have scanned the entire display line
8286 without finding a match for CHARPOS and all the character
8287 positions are greater than CHARPOS. */
8288 if (it->bidi_p)
8290 SAVE_IT (ppos_it, *it, ppos_data);
8291 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8292 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8293 SAVE_IT (ppos_it, *it, ppos_data);
8296 #define BUFFER_POS_REACHED_P() \
8297 ((op & MOVE_TO_POS) != 0 \
8298 && BUFFERP (it->object) \
8299 && (IT_CHARPOS (*it) == to_charpos \
8300 || ((!it->bidi_p \
8301 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8302 && IT_CHARPOS (*it) > to_charpos) \
8303 || (it->what == IT_COMPOSITION \
8304 && ((IT_CHARPOS (*it) > to_charpos \
8305 && to_charpos >= it->cmp_it.charpos) \
8306 || (IT_CHARPOS (*it) < to_charpos \
8307 && to_charpos <= it->cmp_it.charpos)))) \
8308 && (it->method == GET_FROM_BUFFER \
8309 || (it->method == GET_FROM_DISPLAY_VECTOR \
8310 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8312 /* If there's a line-/wrap-prefix, handle it. */
8313 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8314 && it->current_y < it->last_visible_y)
8315 handle_line_prefix (it);
8317 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8318 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8320 while (1)
8322 int x, i, ascent = 0, descent = 0;
8324 /* Utility macro to reset an iterator with x, ascent, and descent. */
8325 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8326 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8327 (IT)->max_descent = descent)
8329 /* Stop if we move beyond TO_CHARPOS (after an image or a
8330 display string or stretch glyph). */
8331 if ((op & MOVE_TO_POS) != 0
8332 && BUFFERP (it->object)
8333 && it->method == GET_FROM_BUFFER
8334 && (((!it->bidi_p
8335 /* When the iterator is at base embedding level, we
8336 are guaranteed that characters are delivered for
8337 display in strictly increasing order of their
8338 buffer positions. */
8339 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8340 && IT_CHARPOS (*it) > to_charpos)
8341 || (it->bidi_p
8342 && (prev_method == GET_FROM_IMAGE
8343 || prev_method == GET_FROM_STRETCH
8344 || prev_method == GET_FROM_STRING)
8345 /* Passed TO_CHARPOS from left to right. */
8346 && ((prev_pos < to_charpos
8347 && IT_CHARPOS (*it) > to_charpos)
8348 /* Passed TO_CHARPOS from right to left. */
8349 || (prev_pos > to_charpos
8350 && IT_CHARPOS (*it) < to_charpos)))))
8352 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8354 result = MOVE_POS_MATCH_OR_ZV;
8355 break;
8357 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8358 /* If wrap_it is valid, the current position might be in a
8359 word that is wrapped. So, save the iterator in
8360 atpos_it and continue to see if wrapping happens. */
8361 SAVE_IT (atpos_it, *it, atpos_data);
8364 /* Stop when ZV reached.
8365 We used to stop here when TO_CHARPOS reached as well, but that is
8366 too soon if this glyph does not fit on this line. So we handle it
8367 explicitly below. */
8368 if (!get_next_display_element (it))
8370 result = MOVE_POS_MATCH_OR_ZV;
8371 break;
8374 if (it->line_wrap == TRUNCATE)
8376 if (BUFFER_POS_REACHED_P ())
8378 result = MOVE_POS_MATCH_OR_ZV;
8379 break;
8382 else
8384 if (it->line_wrap == WORD_WRAP)
8386 if (IT_DISPLAYING_WHITESPACE (it))
8387 may_wrap = 1;
8388 else if (may_wrap)
8390 /* We have reached a glyph that follows one or more
8391 whitespace characters. If the position is
8392 already found, we are done. */
8393 if (atpos_it.sp >= 0)
8395 RESTORE_IT (it, &atpos_it, atpos_data);
8396 result = MOVE_POS_MATCH_OR_ZV;
8397 goto done;
8399 if (atx_it.sp >= 0)
8401 RESTORE_IT (it, &atx_it, atx_data);
8402 result = MOVE_X_REACHED;
8403 goto done;
8405 /* Otherwise, we can wrap here. */
8406 SAVE_IT (wrap_it, *it, wrap_data);
8407 may_wrap = 0;
8412 /* Remember the line height for the current line, in case
8413 the next element doesn't fit on the line. */
8414 ascent = it->max_ascent;
8415 descent = it->max_descent;
8417 /* The call to produce_glyphs will get the metrics of the
8418 display element IT is loaded with. Record the x-position
8419 before this display element, in case it doesn't fit on the
8420 line. */
8421 x = it->current_x;
8423 PRODUCE_GLYPHS (it);
8425 if (it->area != TEXT_AREA)
8427 prev_method = it->method;
8428 if (it->method == GET_FROM_BUFFER)
8429 prev_pos = IT_CHARPOS (*it);
8430 set_iterator_to_next (it, 1);
8431 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8432 SET_TEXT_POS (this_line_min_pos,
8433 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8434 if (it->bidi_p
8435 && (op & MOVE_TO_POS)
8436 && IT_CHARPOS (*it) > to_charpos
8437 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8438 SAVE_IT (ppos_it, *it, ppos_data);
8439 continue;
8442 /* The number of glyphs we get back in IT->nglyphs will normally
8443 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8444 character on a terminal frame, or (iii) a line end. For the
8445 second case, IT->nglyphs - 1 padding glyphs will be present.
8446 (On X frames, there is only one glyph produced for a
8447 composite character.)
8449 The behavior implemented below means, for continuation lines,
8450 that as many spaces of a TAB as fit on the current line are
8451 displayed there. For terminal frames, as many glyphs of a
8452 multi-glyph character are displayed in the current line, too.
8453 This is what the old redisplay code did, and we keep it that
8454 way. Under X, the whole shape of a complex character must
8455 fit on the line or it will be completely displayed in the
8456 next line.
8458 Note that both for tabs and padding glyphs, all glyphs have
8459 the same width. */
8460 if (it->nglyphs)
8462 /* More than one glyph or glyph doesn't fit on line. All
8463 glyphs have the same width. */
8464 int single_glyph_width = it->pixel_width / it->nglyphs;
8465 int new_x;
8466 int x_before_this_char = x;
8467 int hpos_before_this_char = it->hpos;
8469 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8471 new_x = x + single_glyph_width;
8473 /* We want to leave anything reaching TO_X to the caller. */
8474 if ((op & MOVE_TO_X) && new_x > to_x)
8476 if (BUFFER_POS_REACHED_P ())
8478 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8479 goto buffer_pos_reached;
8480 if (atpos_it.sp < 0)
8482 SAVE_IT (atpos_it, *it, atpos_data);
8483 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8486 else
8488 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8490 it->current_x = x;
8491 result = MOVE_X_REACHED;
8492 break;
8494 if (atx_it.sp < 0)
8496 SAVE_IT (atx_it, *it, atx_data);
8497 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8502 if (/* Lines are continued. */
8503 it->line_wrap != TRUNCATE
8504 && (/* And glyph doesn't fit on the line. */
8505 new_x > it->last_visible_x
8506 /* Or it fits exactly and we're on a window
8507 system frame. */
8508 || (new_x == it->last_visible_x
8509 && FRAME_WINDOW_P (it->f)
8510 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8511 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8512 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8514 if (/* IT->hpos == 0 means the very first glyph
8515 doesn't fit on the line, e.g. a wide image. */
8516 it->hpos == 0
8517 || (new_x == it->last_visible_x
8518 && FRAME_WINDOW_P (it->f)))
8520 ++it->hpos;
8521 it->current_x = new_x;
8523 /* The character's last glyph just barely fits
8524 in this row. */
8525 if (i == it->nglyphs - 1)
8527 /* If this is the destination position,
8528 return a position *before* it in this row,
8529 now that we know it fits in this row. */
8530 if (BUFFER_POS_REACHED_P ())
8532 if (it->line_wrap != WORD_WRAP
8533 || wrap_it.sp < 0)
8535 it->hpos = hpos_before_this_char;
8536 it->current_x = x_before_this_char;
8537 result = MOVE_POS_MATCH_OR_ZV;
8538 break;
8540 if (it->line_wrap == WORD_WRAP
8541 && atpos_it.sp < 0)
8543 SAVE_IT (atpos_it, *it, atpos_data);
8544 atpos_it.current_x = x_before_this_char;
8545 atpos_it.hpos = hpos_before_this_char;
8549 prev_method = it->method;
8550 if (it->method == GET_FROM_BUFFER)
8551 prev_pos = IT_CHARPOS (*it);
8552 set_iterator_to_next (it, 1);
8553 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8554 SET_TEXT_POS (this_line_min_pos,
8555 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8556 /* On graphical terminals, newlines may
8557 "overflow" into the fringe if
8558 overflow-newline-into-fringe is non-nil.
8559 On text terminals, and on graphical
8560 terminals with no right margin, newlines
8561 may overflow into the last glyph on the
8562 display line.*/
8563 if (!FRAME_WINDOW_P (it->f)
8564 || ((it->bidi_p
8565 && it->bidi_it.paragraph_dir == R2L)
8566 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8567 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8568 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8570 if (!get_next_display_element (it))
8572 result = MOVE_POS_MATCH_OR_ZV;
8573 break;
8575 if (BUFFER_POS_REACHED_P ())
8577 if (ITERATOR_AT_END_OF_LINE_P (it))
8578 result = MOVE_POS_MATCH_OR_ZV;
8579 else
8580 result = MOVE_LINE_CONTINUED;
8581 break;
8583 if (ITERATOR_AT_END_OF_LINE_P (it)
8584 && (it->line_wrap != WORD_WRAP
8585 || wrap_it.sp < 0))
8587 result = MOVE_NEWLINE_OR_CR;
8588 break;
8593 else
8594 IT_RESET_X_ASCENT_DESCENT (it);
8596 if (wrap_it.sp >= 0)
8598 RESTORE_IT (it, &wrap_it, wrap_data);
8599 atpos_it.sp = -1;
8600 atx_it.sp = -1;
8603 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8604 IT_CHARPOS (*it)));
8605 result = MOVE_LINE_CONTINUED;
8606 break;
8609 if (BUFFER_POS_REACHED_P ())
8611 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8612 goto buffer_pos_reached;
8613 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8615 SAVE_IT (atpos_it, *it, atpos_data);
8616 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8620 if (new_x > it->first_visible_x)
8622 /* Glyph is visible. Increment number of glyphs that
8623 would be displayed. */
8624 ++it->hpos;
8628 if (result != MOVE_UNDEFINED)
8629 break;
8631 else if (BUFFER_POS_REACHED_P ())
8633 buffer_pos_reached:
8634 IT_RESET_X_ASCENT_DESCENT (it);
8635 result = MOVE_POS_MATCH_OR_ZV;
8636 break;
8638 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8640 /* Stop when TO_X specified and reached. This check is
8641 necessary here because of lines consisting of a line end,
8642 only. The line end will not produce any glyphs and we
8643 would never get MOVE_X_REACHED. */
8644 eassert (it->nglyphs == 0);
8645 result = MOVE_X_REACHED;
8646 break;
8649 /* Is this a line end? If yes, we're done. */
8650 if (ITERATOR_AT_END_OF_LINE_P (it))
8652 /* If we are past TO_CHARPOS, but never saw any character
8653 positions smaller than TO_CHARPOS, return
8654 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8655 did. */
8656 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8658 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8660 if (IT_CHARPOS (ppos_it) < ZV)
8662 RESTORE_IT (it, &ppos_it, ppos_data);
8663 result = MOVE_POS_MATCH_OR_ZV;
8665 else
8666 goto buffer_pos_reached;
8668 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8669 && IT_CHARPOS (*it) > to_charpos)
8670 goto buffer_pos_reached;
8671 else
8672 result = MOVE_NEWLINE_OR_CR;
8674 else
8675 result = MOVE_NEWLINE_OR_CR;
8676 break;
8679 prev_method = it->method;
8680 if (it->method == GET_FROM_BUFFER)
8681 prev_pos = IT_CHARPOS (*it);
8682 /* The current display element has been consumed. Advance
8683 to the next. */
8684 set_iterator_to_next (it, 1);
8685 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8686 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8687 if (IT_CHARPOS (*it) < to_charpos)
8688 saw_smaller_pos = 1;
8689 if (it->bidi_p
8690 && (op & MOVE_TO_POS)
8691 && IT_CHARPOS (*it) >= to_charpos
8692 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8693 SAVE_IT (ppos_it, *it, ppos_data);
8695 /* Stop if lines are truncated and IT's current x-position is
8696 past the right edge of the window now. */
8697 if (it->line_wrap == TRUNCATE
8698 && it->current_x >= it->last_visible_x)
8700 if (!FRAME_WINDOW_P (it->f)
8701 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8702 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8703 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8704 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8706 int at_eob_p = 0;
8708 if ((at_eob_p = !get_next_display_element (it))
8709 || BUFFER_POS_REACHED_P ()
8710 /* If we are past TO_CHARPOS, but never saw any
8711 character positions smaller than TO_CHARPOS,
8712 return MOVE_POS_MATCH_OR_ZV, like the
8713 unidirectional display did. */
8714 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8715 && !saw_smaller_pos
8716 && IT_CHARPOS (*it) > to_charpos))
8718 if (it->bidi_p
8719 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8720 RESTORE_IT (it, &ppos_it, ppos_data);
8721 result = MOVE_POS_MATCH_OR_ZV;
8722 break;
8724 if (ITERATOR_AT_END_OF_LINE_P (it))
8726 result = MOVE_NEWLINE_OR_CR;
8727 break;
8730 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8731 && !saw_smaller_pos
8732 && IT_CHARPOS (*it) > to_charpos)
8734 if (IT_CHARPOS (ppos_it) < ZV)
8735 RESTORE_IT (it, &ppos_it, ppos_data);
8736 result = MOVE_POS_MATCH_OR_ZV;
8737 break;
8739 result = MOVE_LINE_TRUNCATED;
8740 break;
8742 #undef IT_RESET_X_ASCENT_DESCENT
8745 #undef BUFFER_POS_REACHED_P
8747 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8748 restore the saved iterator. */
8749 if (atpos_it.sp >= 0)
8750 RESTORE_IT (it, &atpos_it, atpos_data);
8751 else if (atx_it.sp >= 0)
8752 RESTORE_IT (it, &atx_it, atx_data);
8754 done:
8756 if (atpos_data)
8757 bidi_unshelve_cache (atpos_data, 1);
8758 if (atx_data)
8759 bidi_unshelve_cache (atx_data, 1);
8760 if (wrap_data)
8761 bidi_unshelve_cache (wrap_data, 1);
8762 if (ppos_data)
8763 bidi_unshelve_cache (ppos_data, 1);
8765 /* Restore the iterator settings altered at the beginning of this
8766 function. */
8767 it->glyph_row = saved_glyph_row;
8768 return result;
8771 /* For external use. */
8772 void
8773 move_it_in_display_line (struct it *it,
8774 ptrdiff_t to_charpos, int to_x,
8775 enum move_operation_enum op)
8777 if (it->line_wrap == WORD_WRAP
8778 && (op & MOVE_TO_X))
8780 struct it save_it;
8781 void *save_data = NULL;
8782 int skip;
8784 SAVE_IT (save_it, *it, save_data);
8785 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8786 /* When word-wrap is on, TO_X may lie past the end
8787 of a wrapped line. Then it->current is the
8788 character on the next line, so backtrack to the
8789 space before the wrap point. */
8790 if (skip == MOVE_LINE_CONTINUED)
8792 int prev_x = max (it->current_x - 1, 0);
8793 RESTORE_IT (it, &save_it, save_data);
8794 move_it_in_display_line_to
8795 (it, -1, prev_x, MOVE_TO_X);
8797 else
8798 bidi_unshelve_cache (save_data, 1);
8800 else
8801 move_it_in_display_line_to (it, to_charpos, to_x, op);
8805 /* Move IT forward until it satisfies one or more of the criteria in
8806 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8808 OP is a bit-mask that specifies where to stop, and in particular,
8809 which of those four position arguments makes a difference. See the
8810 description of enum move_operation_enum.
8812 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8813 screen line, this function will set IT to the next position that is
8814 displayed to the right of TO_CHARPOS on the screen. */
8816 void
8817 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8819 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8820 int line_height, line_start_x = 0, reached = 0;
8821 void *backup_data = NULL;
8823 for (;;)
8825 if (op & MOVE_TO_VPOS)
8827 /* If no TO_CHARPOS and no TO_X specified, stop at the
8828 start of the line TO_VPOS. */
8829 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8831 if (it->vpos == to_vpos)
8833 reached = 1;
8834 break;
8836 else
8837 skip = move_it_in_display_line_to (it, -1, -1, 0);
8839 else
8841 /* TO_VPOS >= 0 means stop at TO_X in the line at
8842 TO_VPOS, or at TO_POS, whichever comes first. */
8843 if (it->vpos == to_vpos)
8845 reached = 2;
8846 break;
8849 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8851 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8853 reached = 3;
8854 break;
8856 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8858 /* We have reached TO_X but not in the line we want. */
8859 skip = move_it_in_display_line_to (it, to_charpos,
8860 -1, MOVE_TO_POS);
8861 if (skip == MOVE_POS_MATCH_OR_ZV)
8863 reached = 4;
8864 break;
8869 else if (op & MOVE_TO_Y)
8871 struct it it_backup;
8873 if (it->line_wrap == WORD_WRAP)
8874 SAVE_IT (it_backup, *it, backup_data);
8876 /* TO_Y specified means stop at TO_X in the line containing
8877 TO_Y---or at TO_CHARPOS if this is reached first. The
8878 problem is that we can't really tell whether the line
8879 contains TO_Y before we have completely scanned it, and
8880 this may skip past TO_X. What we do is to first scan to
8881 TO_X.
8883 If TO_X is not specified, use a TO_X of zero. The reason
8884 is to make the outcome of this function more predictable.
8885 If we didn't use TO_X == 0, we would stop at the end of
8886 the line which is probably not what a caller would expect
8887 to happen. */
8888 skip = move_it_in_display_line_to
8889 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8890 (MOVE_TO_X | (op & MOVE_TO_POS)));
8892 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8893 if (skip == MOVE_POS_MATCH_OR_ZV)
8894 reached = 5;
8895 else if (skip == MOVE_X_REACHED)
8897 /* If TO_X was reached, we want to know whether TO_Y is
8898 in the line. We know this is the case if the already
8899 scanned glyphs make the line tall enough. Otherwise,
8900 we must check by scanning the rest of the line. */
8901 line_height = it->max_ascent + it->max_descent;
8902 if (to_y >= it->current_y
8903 && to_y < it->current_y + line_height)
8905 reached = 6;
8906 break;
8908 SAVE_IT (it_backup, *it, backup_data);
8909 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8910 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8911 op & MOVE_TO_POS);
8912 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8913 line_height = it->max_ascent + it->max_descent;
8914 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8916 if (to_y >= it->current_y
8917 && to_y < it->current_y + line_height)
8919 /* If TO_Y is in this line and TO_X was reached
8920 above, we scanned too far. We have to restore
8921 IT's settings to the ones before skipping. But
8922 keep the more accurate values of max_ascent and
8923 max_descent we've found while skipping the rest
8924 of the line, for the sake of callers, such as
8925 pos_visible_p, that need to know the line
8926 height. */
8927 int max_ascent = it->max_ascent;
8928 int max_descent = it->max_descent;
8930 RESTORE_IT (it, &it_backup, backup_data);
8931 it->max_ascent = max_ascent;
8932 it->max_descent = max_descent;
8933 reached = 6;
8935 else
8937 skip = skip2;
8938 if (skip == MOVE_POS_MATCH_OR_ZV)
8939 reached = 7;
8942 else
8944 /* Check whether TO_Y is in this line. */
8945 line_height = it->max_ascent + it->max_descent;
8946 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8948 if (to_y >= it->current_y
8949 && to_y < it->current_y + line_height)
8951 /* When word-wrap is on, TO_X may lie past the end
8952 of a wrapped line. Then it->current is the
8953 character on the next line, so backtrack to the
8954 space before the wrap point. */
8955 if (skip == MOVE_LINE_CONTINUED
8956 && it->line_wrap == WORD_WRAP)
8958 int prev_x = max (it->current_x - 1, 0);
8959 RESTORE_IT (it, &it_backup, backup_data);
8960 skip = move_it_in_display_line_to
8961 (it, -1, prev_x, MOVE_TO_X);
8963 reached = 6;
8967 if (reached)
8968 break;
8970 else if (BUFFERP (it->object)
8971 && (it->method == GET_FROM_BUFFER
8972 || it->method == GET_FROM_STRETCH)
8973 && IT_CHARPOS (*it) >= to_charpos
8974 /* Under bidi iteration, a call to set_iterator_to_next
8975 can scan far beyond to_charpos if the initial
8976 portion of the next line needs to be reordered. In
8977 that case, give move_it_in_display_line_to another
8978 chance below. */
8979 && !(it->bidi_p
8980 && it->bidi_it.scan_dir == -1))
8981 skip = MOVE_POS_MATCH_OR_ZV;
8982 else
8983 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8985 switch (skip)
8987 case MOVE_POS_MATCH_OR_ZV:
8988 reached = 8;
8989 goto out;
8991 case MOVE_NEWLINE_OR_CR:
8992 set_iterator_to_next (it, 1);
8993 it->continuation_lines_width = 0;
8994 break;
8996 case MOVE_LINE_TRUNCATED:
8997 it->continuation_lines_width = 0;
8998 reseat_at_next_visible_line_start (it, 0);
8999 if ((op & MOVE_TO_POS) != 0
9000 && IT_CHARPOS (*it) > to_charpos)
9002 reached = 9;
9003 goto out;
9005 break;
9007 case MOVE_LINE_CONTINUED:
9008 /* For continued lines ending in a tab, some of the glyphs
9009 associated with the tab are displayed on the current
9010 line. Since it->current_x does not include these glyphs,
9011 we use it->last_visible_x instead. */
9012 if (it->c == '\t')
9014 it->continuation_lines_width += it->last_visible_x;
9015 /* When moving by vpos, ensure that the iterator really
9016 advances to the next line (bug#847, bug#969). Fixme:
9017 do we need to do this in other circumstances? */
9018 if (it->current_x != it->last_visible_x
9019 && (op & MOVE_TO_VPOS)
9020 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9022 line_start_x = it->current_x + it->pixel_width
9023 - it->last_visible_x;
9024 set_iterator_to_next (it, 0);
9027 else
9028 it->continuation_lines_width += it->current_x;
9029 break;
9031 default:
9032 emacs_abort ();
9035 /* Reset/increment for the next run. */
9036 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9037 it->current_x = line_start_x;
9038 line_start_x = 0;
9039 it->hpos = 0;
9040 it->current_y += it->max_ascent + it->max_descent;
9041 ++it->vpos;
9042 last_height = it->max_ascent + it->max_descent;
9043 it->max_ascent = it->max_descent = 0;
9046 out:
9048 /* On text terminals, we may stop at the end of a line in the middle
9049 of a multi-character glyph. If the glyph itself is continued,
9050 i.e. it is actually displayed on the next line, don't treat this
9051 stopping point as valid; move to the next line instead (unless
9052 that brings us offscreen). */
9053 if (!FRAME_WINDOW_P (it->f)
9054 && op & MOVE_TO_POS
9055 && IT_CHARPOS (*it) == to_charpos
9056 && it->what == IT_CHARACTER
9057 && it->nglyphs > 1
9058 && it->line_wrap == WINDOW_WRAP
9059 && it->current_x == it->last_visible_x - 1
9060 && it->c != '\n'
9061 && it->c != '\t'
9062 && it->vpos < XFASTINT (it->w->window_end_vpos))
9064 it->continuation_lines_width += it->current_x;
9065 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9066 it->current_y += it->max_ascent + it->max_descent;
9067 ++it->vpos;
9068 last_height = it->max_ascent + it->max_descent;
9071 if (backup_data)
9072 bidi_unshelve_cache (backup_data, 1);
9074 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9078 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9080 If DY > 0, move IT backward at least that many pixels. DY = 0
9081 means move IT backward to the preceding line start or BEGV. This
9082 function may move over more than DY pixels if IT->current_y - DY
9083 ends up in the middle of a line; in this case IT->current_y will be
9084 set to the top of the line moved to. */
9086 void
9087 move_it_vertically_backward (struct it *it, int dy)
9089 int nlines, h;
9090 struct it it2, it3;
9091 void *it2data = NULL, *it3data = NULL;
9092 ptrdiff_t start_pos;
9093 int nchars_per_row
9094 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9095 ptrdiff_t pos_limit;
9097 move_further_back:
9098 eassert (dy >= 0);
9100 start_pos = IT_CHARPOS (*it);
9102 /* Estimate how many newlines we must move back. */
9103 nlines = max (1, dy / default_line_pixel_height (it->w));
9104 if (it->line_wrap == TRUNCATE)
9105 pos_limit = BEGV;
9106 else
9107 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9109 /* Set the iterator's position that many lines back. But don't go
9110 back more than NLINES full screen lines -- this wins a day with
9111 buffers which have very long lines. */
9112 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9113 back_to_previous_visible_line_start (it);
9115 /* Reseat the iterator here. When moving backward, we don't want
9116 reseat to skip forward over invisible text, set up the iterator
9117 to deliver from overlay strings at the new position etc. So,
9118 use reseat_1 here. */
9119 reseat_1 (it, it->current.pos, 1);
9121 /* We are now surely at a line start. */
9122 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9123 reordering is in effect. */
9124 it->continuation_lines_width = 0;
9126 /* Move forward and see what y-distance we moved. First move to the
9127 start of the next line so that we get its height. We need this
9128 height to be able to tell whether we reached the specified
9129 y-distance. */
9130 SAVE_IT (it2, *it, it2data);
9131 it2.max_ascent = it2.max_descent = 0;
9134 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9135 MOVE_TO_POS | MOVE_TO_VPOS);
9137 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9138 /* If we are in a display string which starts at START_POS,
9139 and that display string includes a newline, and we are
9140 right after that newline (i.e. at the beginning of a
9141 display line), exit the loop, because otherwise we will
9142 infloop, since move_it_to will see that it is already at
9143 START_POS and will not move. */
9144 || (it2.method == GET_FROM_STRING
9145 && IT_CHARPOS (it2) == start_pos
9146 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9147 eassert (IT_CHARPOS (*it) >= BEGV);
9148 SAVE_IT (it3, it2, it3data);
9150 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9151 eassert (IT_CHARPOS (*it) >= BEGV);
9152 /* H is the actual vertical distance from the position in *IT
9153 and the starting position. */
9154 h = it2.current_y - it->current_y;
9155 /* NLINES is the distance in number of lines. */
9156 nlines = it2.vpos - it->vpos;
9158 /* Correct IT's y and vpos position
9159 so that they are relative to the starting point. */
9160 it->vpos -= nlines;
9161 it->current_y -= h;
9163 if (dy == 0)
9165 /* DY == 0 means move to the start of the screen line. The
9166 value of nlines is > 0 if continuation lines were involved,
9167 or if the original IT position was at start of a line. */
9168 RESTORE_IT (it, it, it2data);
9169 if (nlines > 0)
9170 move_it_by_lines (it, nlines);
9171 /* The above code moves us to some position NLINES down,
9172 usually to its first glyph (leftmost in an L2R line), but
9173 that's not necessarily the start of the line, under bidi
9174 reordering. We want to get to the character position
9175 that is immediately after the newline of the previous
9176 line. */
9177 if (it->bidi_p
9178 && !it->continuation_lines_width
9179 && !STRINGP (it->string)
9180 && IT_CHARPOS (*it) > BEGV
9181 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9183 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9185 DEC_BOTH (cp, bp);
9186 cp = find_newline_no_quit (cp, bp, -1, NULL);
9187 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9189 bidi_unshelve_cache (it3data, 1);
9191 else
9193 /* The y-position we try to reach, relative to *IT.
9194 Note that H has been subtracted in front of the if-statement. */
9195 int target_y = it->current_y + h - dy;
9196 int y0 = it3.current_y;
9197 int y1;
9198 int line_height;
9200 RESTORE_IT (&it3, &it3, it3data);
9201 y1 = line_bottom_y (&it3);
9202 line_height = y1 - y0;
9203 RESTORE_IT (it, it, it2data);
9204 /* If we did not reach target_y, try to move further backward if
9205 we can. If we moved too far backward, try to move forward. */
9206 if (target_y < it->current_y
9207 /* This is heuristic. In a window that's 3 lines high, with
9208 a line height of 13 pixels each, recentering with point
9209 on the bottom line will try to move -39/2 = 19 pixels
9210 backward. Try to avoid moving into the first line. */
9211 && (it->current_y - target_y
9212 > min (window_box_height (it->w), line_height * 2 / 3))
9213 && IT_CHARPOS (*it) > BEGV)
9215 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9216 target_y - it->current_y));
9217 dy = it->current_y - target_y;
9218 goto move_further_back;
9220 else if (target_y >= it->current_y + line_height
9221 && IT_CHARPOS (*it) < ZV)
9223 /* Should move forward by at least one line, maybe more.
9225 Note: Calling move_it_by_lines can be expensive on
9226 terminal frames, where compute_motion is used (via
9227 vmotion) to do the job, when there are very long lines
9228 and truncate-lines is nil. That's the reason for
9229 treating terminal frames specially here. */
9231 if (!FRAME_WINDOW_P (it->f))
9232 move_it_vertically (it, target_y - (it->current_y + line_height));
9233 else
9237 move_it_by_lines (it, 1);
9239 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9246 /* Move IT by a specified amount of pixel lines DY. DY negative means
9247 move backwards. DY = 0 means move to start of screen line. At the
9248 end, IT will be on the start of a screen line. */
9250 void
9251 move_it_vertically (struct it *it, int dy)
9253 if (dy <= 0)
9254 move_it_vertically_backward (it, -dy);
9255 else
9257 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9258 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9259 MOVE_TO_POS | MOVE_TO_Y);
9260 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9262 /* If buffer ends in ZV without a newline, move to the start of
9263 the line to satisfy the post-condition. */
9264 if (IT_CHARPOS (*it) == ZV
9265 && ZV > BEGV
9266 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9267 move_it_by_lines (it, 0);
9272 /* Move iterator IT past the end of the text line it is in. */
9274 void
9275 move_it_past_eol (struct it *it)
9277 enum move_it_result rc;
9279 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9280 if (rc == MOVE_NEWLINE_OR_CR)
9281 set_iterator_to_next (it, 0);
9285 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9286 negative means move up. DVPOS == 0 means move to the start of the
9287 screen line.
9289 Optimization idea: If we would know that IT->f doesn't use
9290 a face with proportional font, we could be faster for
9291 truncate-lines nil. */
9293 void
9294 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9297 /* The commented-out optimization uses vmotion on terminals. This
9298 gives bad results, because elements like it->what, on which
9299 callers such as pos_visible_p rely, aren't updated. */
9300 /* struct position pos;
9301 if (!FRAME_WINDOW_P (it->f))
9303 struct text_pos textpos;
9305 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9306 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9307 reseat (it, textpos, 1);
9308 it->vpos += pos.vpos;
9309 it->current_y += pos.vpos;
9311 else */
9313 if (dvpos == 0)
9315 /* DVPOS == 0 means move to the start of the screen line. */
9316 move_it_vertically_backward (it, 0);
9317 /* Let next call to line_bottom_y calculate real line height */
9318 last_height = 0;
9320 else if (dvpos > 0)
9322 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9323 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9325 /* Only move to the next buffer position if we ended up in a
9326 string from display property, not in an overlay string
9327 (before-string or after-string). That is because the
9328 latter don't conceal the underlying buffer position, so
9329 we can ask to move the iterator to the exact position we
9330 are interested in. Note that, even if we are already at
9331 IT_CHARPOS (*it), the call below is not a no-op, as it
9332 will detect that we are at the end of the string, pop the
9333 iterator, and compute it->current_x and it->hpos
9334 correctly. */
9335 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9336 -1, -1, -1, MOVE_TO_POS);
9339 else
9341 struct it it2;
9342 void *it2data = NULL;
9343 ptrdiff_t start_charpos, i;
9344 int nchars_per_row
9345 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9346 ptrdiff_t pos_limit;
9348 /* Start at the beginning of the screen line containing IT's
9349 position. This may actually move vertically backwards,
9350 in case of overlays, so adjust dvpos accordingly. */
9351 dvpos += it->vpos;
9352 move_it_vertically_backward (it, 0);
9353 dvpos -= it->vpos;
9355 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9356 screen lines, and reseat the iterator there. */
9357 start_charpos = IT_CHARPOS (*it);
9358 if (it->line_wrap == TRUNCATE)
9359 pos_limit = BEGV;
9360 else
9361 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9362 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9363 back_to_previous_visible_line_start (it);
9364 reseat (it, it->current.pos, 1);
9366 /* Move further back if we end up in a string or an image. */
9367 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9369 /* First try to move to start of display line. */
9370 dvpos += it->vpos;
9371 move_it_vertically_backward (it, 0);
9372 dvpos -= it->vpos;
9373 if (IT_POS_VALID_AFTER_MOVE_P (it))
9374 break;
9375 /* If start of line is still in string or image,
9376 move further back. */
9377 back_to_previous_visible_line_start (it);
9378 reseat (it, it->current.pos, 1);
9379 dvpos--;
9382 it->current_x = it->hpos = 0;
9384 /* Above call may have moved too far if continuation lines
9385 are involved. Scan forward and see if it did. */
9386 SAVE_IT (it2, *it, it2data);
9387 it2.vpos = it2.current_y = 0;
9388 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9389 it->vpos -= it2.vpos;
9390 it->current_y -= it2.current_y;
9391 it->current_x = it->hpos = 0;
9393 /* If we moved too far back, move IT some lines forward. */
9394 if (it2.vpos > -dvpos)
9396 int delta = it2.vpos + dvpos;
9398 RESTORE_IT (&it2, &it2, it2data);
9399 SAVE_IT (it2, *it, it2data);
9400 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9401 /* Move back again if we got too far ahead. */
9402 if (IT_CHARPOS (*it) >= start_charpos)
9403 RESTORE_IT (it, &it2, it2data);
9404 else
9405 bidi_unshelve_cache (it2data, 1);
9407 else
9408 RESTORE_IT (it, it, it2data);
9412 /* Return 1 if IT points into the middle of a display vector. */
9415 in_display_vector_p (struct it *it)
9417 return (it->method == GET_FROM_DISPLAY_VECTOR
9418 && it->current.dpvec_index > 0
9419 && it->dpvec + it->current.dpvec_index != it->dpend);
9423 /***********************************************************************
9424 Messages
9425 ***********************************************************************/
9428 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9429 to *Messages*. */
9431 void
9432 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9434 Lisp_Object args[3];
9435 Lisp_Object msg, fmt;
9436 char *buffer;
9437 ptrdiff_t len;
9438 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9439 USE_SAFE_ALLOCA;
9441 fmt = msg = Qnil;
9442 GCPRO4 (fmt, msg, arg1, arg2);
9444 args[0] = fmt = build_string (format);
9445 args[1] = arg1;
9446 args[2] = arg2;
9447 msg = Fformat (3, args);
9449 len = SBYTES (msg) + 1;
9450 buffer = SAFE_ALLOCA (len);
9451 memcpy (buffer, SDATA (msg), len);
9453 message_dolog (buffer, len - 1, 1, 0);
9454 SAFE_FREE ();
9456 UNGCPRO;
9460 /* Output a newline in the *Messages* buffer if "needs" one. */
9462 void
9463 message_log_maybe_newline (void)
9465 if (message_log_need_newline)
9466 message_dolog ("", 0, 1, 0);
9470 /* Add a string M of length NBYTES to the message log, optionally
9471 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9472 true, means interpret the contents of M as multibyte. This
9473 function calls low-level routines in order to bypass text property
9474 hooks, etc. which might not be safe to run.
9476 This may GC (insert may run before/after change hooks),
9477 so the buffer M must NOT point to a Lisp string. */
9479 void
9480 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9482 const unsigned char *msg = (const unsigned char *) m;
9484 if (!NILP (Vmemory_full))
9485 return;
9487 if (!NILP (Vmessage_log_max))
9489 struct buffer *oldbuf;
9490 Lisp_Object oldpoint, oldbegv, oldzv;
9491 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9492 ptrdiff_t point_at_end = 0;
9493 ptrdiff_t zv_at_end = 0;
9494 Lisp_Object old_deactivate_mark;
9495 bool shown;
9496 struct gcpro gcpro1;
9498 old_deactivate_mark = Vdeactivate_mark;
9499 oldbuf = current_buffer;
9500 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9501 bset_undo_list (current_buffer, Qt);
9503 oldpoint = message_dolog_marker1;
9504 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9505 oldbegv = message_dolog_marker2;
9506 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9507 oldzv = message_dolog_marker3;
9508 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9509 GCPRO1 (old_deactivate_mark);
9511 if (PT == Z)
9512 point_at_end = 1;
9513 if (ZV == Z)
9514 zv_at_end = 1;
9516 BEGV = BEG;
9517 BEGV_BYTE = BEG_BYTE;
9518 ZV = Z;
9519 ZV_BYTE = Z_BYTE;
9520 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9522 /* Insert the string--maybe converting multibyte to single byte
9523 or vice versa, so that all the text fits the buffer. */
9524 if (multibyte
9525 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9527 ptrdiff_t i;
9528 int c, char_bytes;
9529 char work[1];
9531 /* Convert a multibyte string to single-byte
9532 for the *Message* buffer. */
9533 for (i = 0; i < nbytes; i += char_bytes)
9535 c = string_char_and_length (msg + i, &char_bytes);
9536 work[0] = (ASCII_CHAR_P (c)
9538 : multibyte_char_to_unibyte (c));
9539 insert_1_both (work, 1, 1, 1, 0, 0);
9542 else if (! multibyte
9543 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9545 ptrdiff_t i;
9546 int c, char_bytes;
9547 unsigned char str[MAX_MULTIBYTE_LENGTH];
9548 /* Convert a single-byte string to multibyte
9549 for the *Message* buffer. */
9550 for (i = 0; i < nbytes; i++)
9552 c = msg[i];
9553 MAKE_CHAR_MULTIBYTE (c);
9554 char_bytes = CHAR_STRING (c, str);
9555 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9558 else if (nbytes)
9559 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9561 if (nlflag)
9563 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9564 printmax_t dups;
9566 insert_1_both ("\n", 1, 1, 1, 0, 0);
9568 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9569 this_bol = PT;
9570 this_bol_byte = PT_BYTE;
9572 /* See if this line duplicates the previous one.
9573 If so, combine duplicates. */
9574 if (this_bol > BEG)
9576 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9577 prev_bol = PT;
9578 prev_bol_byte = PT_BYTE;
9580 dups = message_log_check_duplicate (prev_bol_byte,
9581 this_bol_byte);
9582 if (dups)
9584 del_range_both (prev_bol, prev_bol_byte,
9585 this_bol, this_bol_byte, 0);
9586 if (dups > 1)
9588 char dupstr[sizeof " [ times]"
9589 + INT_STRLEN_BOUND (printmax_t)];
9591 /* If you change this format, don't forget to also
9592 change message_log_check_duplicate. */
9593 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9594 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9595 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9600 /* If we have more than the desired maximum number of lines
9601 in the *Messages* buffer now, delete the oldest ones.
9602 This is safe because we don't have undo in this buffer. */
9604 if (NATNUMP (Vmessage_log_max))
9606 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9607 -XFASTINT (Vmessage_log_max) - 1, 0);
9608 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9611 BEGV = marker_position (oldbegv);
9612 BEGV_BYTE = marker_byte_position (oldbegv);
9614 if (zv_at_end)
9616 ZV = Z;
9617 ZV_BYTE = Z_BYTE;
9619 else
9621 ZV = marker_position (oldzv);
9622 ZV_BYTE = marker_byte_position (oldzv);
9625 if (point_at_end)
9626 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9627 else
9628 /* We can't do Fgoto_char (oldpoint) because it will run some
9629 Lisp code. */
9630 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9631 marker_byte_position (oldpoint));
9633 UNGCPRO;
9634 unchain_marker (XMARKER (oldpoint));
9635 unchain_marker (XMARKER (oldbegv));
9636 unchain_marker (XMARKER (oldzv));
9638 shown = buffer_window_count (current_buffer) > 0;
9639 set_buffer_internal (oldbuf);
9640 /* We called insert_1_both above with its 5th argument (PREPARE)
9641 zero, which prevents insert_1_both from calling
9642 prepare_to_modify_buffer, which in turns prevents us from
9643 incrementing windows_or_buffers_changed even if *Messages* is
9644 shown in some window. So we must manually incrementing
9645 windows_or_buffers_changed here to make up for that. */
9646 if (shown)
9647 windows_or_buffers_changed++;
9648 else
9649 windows_or_buffers_changed = old_windows_or_buffers_changed;
9650 message_log_need_newline = !nlflag;
9651 Vdeactivate_mark = old_deactivate_mark;
9656 /* We are at the end of the buffer after just having inserted a newline.
9657 (Note: We depend on the fact we won't be crossing the gap.)
9658 Check to see if the most recent message looks a lot like the previous one.
9659 Return 0 if different, 1 if the new one should just replace it, or a
9660 value N > 1 if we should also append " [N times]". */
9662 static intmax_t
9663 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9665 ptrdiff_t i;
9666 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9667 int seen_dots = 0;
9668 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9669 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9671 for (i = 0; i < len; i++)
9673 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9674 seen_dots = 1;
9675 if (p1[i] != p2[i])
9676 return seen_dots;
9678 p1 += len;
9679 if (*p1 == '\n')
9680 return 2;
9681 if (*p1++ == ' ' && *p1++ == '[')
9683 char *pend;
9684 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9685 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9686 return n + 1;
9688 return 0;
9692 /* Display an echo area message M with a specified length of NBYTES
9693 bytes. The string may include null characters. If M is not a
9694 string, clear out any existing message, and let the mini-buffer
9695 text show through.
9697 This function cancels echoing. */
9699 void
9700 message3 (Lisp_Object m)
9702 struct gcpro gcpro1;
9704 GCPRO1 (m);
9705 clear_message (1,1);
9706 cancel_echoing ();
9708 /* First flush out any partial line written with print. */
9709 message_log_maybe_newline ();
9710 if (STRINGP (m))
9712 ptrdiff_t nbytes = SBYTES (m);
9713 bool multibyte = STRING_MULTIBYTE (m);
9714 USE_SAFE_ALLOCA;
9715 char *buffer = SAFE_ALLOCA (nbytes);
9716 memcpy (buffer, SDATA (m), nbytes);
9717 message_dolog (buffer, nbytes, 1, multibyte);
9718 SAFE_FREE ();
9720 message3_nolog (m);
9722 UNGCPRO;
9726 /* The non-logging version of message3.
9727 This does not cancel echoing, because it is used for echoing.
9728 Perhaps we need to make a separate function for echoing
9729 and make this cancel echoing. */
9731 void
9732 message3_nolog (Lisp_Object m)
9734 struct frame *sf = SELECTED_FRAME ();
9736 if (FRAME_INITIAL_P (sf))
9738 if (noninteractive_need_newline)
9739 putc ('\n', stderr);
9740 noninteractive_need_newline = 0;
9741 if (STRINGP (m))
9742 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9743 if (cursor_in_echo_area == 0)
9744 fprintf (stderr, "\n");
9745 fflush (stderr);
9747 /* Error messages get reported properly by cmd_error, so this must be just an
9748 informative message; if the frame hasn't really been initialized yet, just
9749 toss it. */
9750 else if (INTERACTIVE && sf->glyphs_initialized_p)
9752 /* Get the frame containing the mini-buffer
9753 that the selected frame is using. */
9754 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9755 Lisp_Object frame = XWINDOW (mini_window)->frame;
9756 struct frame *f = XFRAME (frame);
9758 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9759 Fmake_frame_visible (frame);
9761 if (STRINGP (m) && SCHARS (m) > 0)
9763 set_message (m);
9764 if (minibuffer_auto_raise)
9765 Fraise_frame (frame);
9766 /* Assume we are not echoing.
9767 (If we are, echo_now will override this.) */
9768 echo_message_buffer = Qnil;
9770 else
9771 clear_message (1, 1);
9773 do_pending_window_change (0);
9774 echo_area_display (1);
9775 do_pending_window_change (0);
9776 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9777 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9782 /* Display a null-terminated echo area message M. If M is 0, clear
9783 out any existing message, and let the mini-buffer text show through.
9785 The buffer M must continue to exist until after the echo area gets
9786 cleared or some other message gets displayed there. Do not pass
9787 text that is stored in a Lisp string. Do not pass text in a buffer
9788 that was alloca'd. */
9790 void
9791 message1 (const char *m)
9793 message3 (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9797 /* The non-logging counterpart of message1. */
9799 void
9800 message1_nolog (const char *m)
9802 message3_nolog (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9805 /* Display a message M which contains a single %s
9806 which gets replaced with STRING. */
9808 void
9809 message_with_string (const char *m, Lisp_Object string, int log)
9811 CHECK_STRING (string);
9813 if (noninteractive)
9815 if (m)
9817 if (noninteractive_need_newline)
9818 putc ('\n', stderr);
9819 noninteractive_need_newline = 0;
9820 fprintf (stderr, m, SDATA (string));
9821 if (!cursor_in_echo_area)
9822 fprintf (stderr, "\n");
9823 fflush (stderr);
9826 else if (INTERACTIVE)
9828 /* The frame whose minibuffer we're going to display the message on.
9829 It may be larger than the selected frame, so we need
9830 to use its buffer, not the selected frame's buffer. */
9831 Lisp_Object mini_window;
9832 struct frame *f, *sf = SELECTED_FRAME ();
9834 /* Get the frame containing the minibuffer
9835 that the selected frame is using. */
9836 mini_window = FRAME_MINIBUF_WINDOW (sf);
9837 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9839 /* Error messages get reported properly by cmd_error, so this must be
9840 just an informative message; if the frame hasn't really been
9841 initialized yet, just toss it. */
9842 if (f->glyphs_initialized_p)
9844 Lisp_Object args[2], msg;
9845 struct gcpro gcpro1, gcpro2;
9847 args[0] = build_string (m);
9848 args[1] = msg = string;
9849 GCPRO2 (args[0], msg);
9850 gcpro1.nvars = 2;
9852 msg = Fformat (2, args);
9854 if (log)
9855 message3 (msg);
9856 else
9857 message3_nolog (msg);
9859 UNGCPRO;
9861 /* Print should start at the beginning of the message
9862 buffer next time. */
9863 message_buf_print = 0;
9869 /* Dump an informative message to the minibuf. If M is 0, clear out
9870 any existing message, and let the mini-buffer text show through. */
9872 static void
9873 vmessage (const char *m, va_list ap)
9875 if (noninteractive)
9877 if (m)
9879 if (noninteractive_need_newline)
9880 putc ('\n', stderr);
9881 noninteractive_need_newline = 0;
9882 vfprintf (stderr, m, ap);
9883 if (cursor_in_echo_area == 0)
9884 fprintf (stderr, "\n");
9885 fflush (stderr);
9888 else if (INTERACTIVE)
9890 /* The frame whose mini-buffer we're going to display the message
9891 on. It may be larger than the selected frame, so we need to
9892 use its buffer, not the selected frame's buffer. */
9893 Lisp_Object mini_window;
9894 struct frame *f, *sf = SELECTED_FRAME ();
9896 /* Get the frame containing the mini-buffer
9897 that the selected frame is using. */
9898 mini_window = FRAME_MINIBUF_WINDOW (sf);
9899 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9901 /* Error messages get reported properly by cmd_error, so this must be
9902 just an informative message; if the frame hasn't really been
9903 initialized yet, just toss it. */
9904 if (f->glyphs_initialized_p)
9906 if (m)
9908 ptrdiff_t len;
9909 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9910 char *message_buf = alloca (maxsize + 1);
9912 len = doprnt (message_buf, maxsize, m, (char *)0, ap);
9914 message3 (make_string (message_buf, len));
9916 else
9917 message1 (0);
9919 /* Print should start at the beginning of the message
9920 buffer next time. */
9921 message_buf_print = 0;
9926 void
9927 message (const char *m, ...)
9929 va_list ap;
9930 va_start (ap, m);
9931 vmessage (m, ap);
9932 va_end (ap);
9936 #if 0
9937 /* The non-logging version of message. */
9939 void
9940 message_nolog (const char *m, ...)
9942 Lisp_Object old_log_max;
9943 va_list ap;
9944 va_start (ap, m);
9945 old_log_max = Vmessage_log_max;
9946 Vmessage_log_max = Qnil;
9947 vmessage (m, ap);
9948 Vmessage_log_max = old_log_max;
9949 va_end (ap);
9951 #endif
9954 /* Display the current message in the current mini-buffer. This is
9955 only called from error handlers in process.c, and is not time
9956 critical. */
9958 void
9959 update_echo_area (void)
9961 if (!NILP (echo_area_buffer[0]))
9963 Lisp_Object string;
9964 string = Fcurrent_message ();
9965 message3 (string);
9970 /* Make sure echo area buffers in `echo_buffers' are live.
9971 If they aren't, make new ones. */
9973 static void
9974 ensure_echo_area_buffers (void)
9976 int i;
9978 for (i = 0; i < 2; ++i)
9979 if (!BUFFERP (echo_buffer[i])
9980 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9982 char name[30];
9983 Lisp_Object old_buffer;
9984 int j;
9986 old_buffer = echo_buffer[i];
9987 echo_buffer[i] = Fget_buffer_create
9988 (make_formatted_string (name, " *Echo Area %d*", i));
9989 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9990 /* to force word wrap in echo area -
9991 it was decided to postpone this*/
9992 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9994 for (j = 0; j < 2; ++j)
9995 if (EQ (old_buffer, echo_area_buffer[j]))
9996 echo_area_buffer[j] = echo_buffer[i];
10001 /* Call FN with args A1..A2 with either the current or last displayed
10002 echo_area_buffer as current buffer.
10004 WHICH zero means use the current message buffer
10005 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10006 from echo_buffer[] and clear it.
10008 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10009 suitable buffer from echo_buffer[] and clear it.
10011 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10012 that the current message becomes the last displayed one, make
10013 choose a suitable buffer for echo_area_buffer[0], and clear it.
10015 Value is what FN returns. */
10017 static int
10018 with_echo_area_buffer (struct window *w, int which,
10019 int (*fn) (ptrdiff_t, Lisp_Object),
10020 ptrdiff_t a1, Lisp_Object a2)
10022 Lisp_Object buffer;
10023 int this_one, the_other, clear_buffer_p, rc;
10024 ptrdiff_t count = SPECPDL_INDEX ();
10026 /* If buffers aren't live, make new ones. */
10027 ensure_echo_area_buffers ();
10029 clear_buffer_p = 0;
10031 if (which == 0)
10032 this_one = 0, the_other = 1;
10033 else if (which > 0)
10034 this_one = 1, the_other = 0;
10035 else
10037 this_one = 0, the_other = 1;
10038 clear_buffer_p = 1;
10040 /* We need a fresh one in case the current echo buffer equals
10041 the one containing the last displayed echo area message. */
10042 if (!NILP (echo_area_buffer[this_one])
10043 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10044 echo_area_buffer[this_one] = Qnil;
10047 /* Choose a suitable buffer from echo_buffer[] is we don't
10048 have one. */
10049 if (NILP (echo_area_buffer[this_one]))
10051 echo_area_buffer[this_one]
10052 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10053 ? echo_buffer[the_other]
10054 : echo_buffer[this_one]);
10055 clear_buffer_p = 1;
10058 buffer = echo_area_buffer[this_one];
10060 /* Don't get confused by reusing the buffer used for echoing
10061 for a different purpose. */
10062 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10063 cancel_echoing ();
10065 record_unwind_protect (unwind_with_echo_area_buffer,
10066 with_echo_area_buffer_unwind_data (w));
10068 /* Make the echo area buffer current. Note that for display
10069 purposes, it is not necessary that the displayed window's buffer
10070 == current_buffer, except for text property lookup. So, let's
10071 only set that buffer temporarily here without doing a full
10072 Fset_window_buffer. We must also change w->pointm, though,
10073 because otherwise an assertions in unshow_buffer fails, and Emacs
10074 aborts. */
10075 set_buffer_internal_1 (XBUFFER (buffer));
10076 if (w)
10078 wset_buffer (w, buffer);
10079 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10082 bset_undo_list (current_buffer, Qt);
10083 bset_read_only (current_buffer, Qnil);
10084 specbind (Qinhibit_read_only, Qt);
10085 specbind (Qinhibit_modification_hooks, Qt);
10087 if (clear_buffer_p && Z > BEG)
10088 del_range (BEG, Z);
10090 eassert (BEGV >= BEG);
10091 eassert (ZV <= Z && ZV >= BEGV);
10093 rc = fn (a1, a2);
10095 eassert (BEGV >= BEG);
10096 eassert (ZV <= Z && ZV >= BEGV);
10098 unbind_to (count, Qnil);
10099 return rc;
10103 /* Save state that should be preserved around the call to the function
10104 FN called in with_echo_area_buffer. */
10106 static Lisp_Object
10107 with_echo_area_buffer_unwind_data (struct window *w)
10109 int i = 0;
10110 Lisp_Object vector, tmp;
10112 /* Reduce consing by keeping one vector in
10113 Vwith_echo_area_save_vector. */
10114 vector = Vwith_echo_area_save_vector;
10115 Vwith_echo_area_save_vector = Qnil;
10117 if (NILP (vector))
10118 vector = Fmake_vector (make_number (9), Qnil);
10120 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10121 ASET (vector, i, Vdeactivate_mark); ++i;
10122 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10124 if (w)
10126 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10127 ASET (vector, i, w->contents); ++i;
10128 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10129 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10130 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10131 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10133 else
10135 int end = i + 6;
10136 for (; i < end; ++i)
10137 ASET (vector, i, Qnil);
10140 eassert (i == ASIZE (vector));
10141 return vector;
10145 /* Restore global state from VECTOR which was created by
10146 with_echo_area_buffer_unwind_data. */
10148 static void
10149 unwind_with_echo_area_buffer (Lisp_Object vector)
10151 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10152 Vdeactivate_mark = AREF (vector, 1);
10153 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10155 if (WINDOWP (AREF (vector, 3)))
10157 struct window *w;
10158 Lisp_Object buffer;
10160 w = XWINDOW (AREF (vector, 3));
10161 buffer = AREF (vector, 4);
10163 wset_buffer (w, buffer);
10164 set_marker_both (w->pointm, buffer,
10165 XFASTINT (AREF (vector, 5)),
10166 XFASTINT (AREF (vector, 6)));
10167 set_marker_both (w->start, buffer,
10168 XFASTINT (AREF (vector, 7)),
10169 XFASTINT (AREF (vector, 8)));
10172 Vwith_echo_area_save_vector = vector;
10176 /* Set up the echo area for use by print functions. MULTIBYTE_P
10177 non-zero means we will print multibyte. */
10179 void
10180 setup_echo_area_for_printing (int multibyte_p)
10182 /* If we can't find an echo area any more, exit. */
10183 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10184 Fkill_emacs (Qnil);
10186 ensure_echo_area_buffers ();
10188 if (!message_buf_print)
10190 /* A message has been output since the last time we printed.
10191 Choose a fresh echo area buffer. */
10192 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10193 echo_area_buffer[0] = echo_buffer[1];
10194 else
10195 echo_area_buffer[0] = echo_buffer[0];
10197 /* Switch to that buffer and clear it. */
10198 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10199 bset_truncate_lines (current_buffer, Qnil);
10201 if (Z > BEG)
10203 ptrdiff_t count = SPECPDL_INDEX ();
10204 specbind (Qinhibit_read_only, Qt);
10205 /* Note that undo recording is always disabled. */
10206 del_range (BEG, Z);
10207 unbind_to (count, Qnil);
10209 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10211 /* Set up the buffer for the multibyteness we need. */
10212 if (multibyte_p
10213 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10214 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10216 /* Raise the frame containing the echo area. */
10217 if (minibuffer_auto_raise)
10219 struct frame *sf = SELECTED_FRAME ();
10220 Lisp_Object mini_window;
10221 mini_window = FRAME_MINIBUF_WINDOW (sf);
10222 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10225 message_log_maybe_newline ();
10226 message_buf_print = 1;
10228 else
10230 if (NILP (echo_area_buffer[0]))
10232 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10233 echo_area_buffer[0] = echo_buffer[1];
10234 else
10235 echo_area_buffer[0] = echo_buffer[0];
10238 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10240 /* Someone switched buffers between print requests. */
10241 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10242 bset_truncate_lines (current_buffer, Qnil);
10248 /* Display an echo area message in window W. Value is non-zero if W's
10249 height is changed. If display_last_displayed_message_p is
10250 non-zero, display the message that was last displayed, otherwise
10251 display the current message. */
10253 static int
10254 display_echo_area (struct window *w)
10256 int i, no_message_p, window_height_changed_p;
10258 /* Temporarily disable garbage collections while displaying the echo
10259 area. This is done because a GC can print a message itself.
10260 That message would modify the echo area buffer's contents while a
10261 redisplay of the buffer is going on, and seriously confuse
10262 redisplay. */
10263 ptrdiff_t count = inhibit_garbage_collection ();
10265 /* If there is no message, we must call display_echo_area_1
10266 nevertheless because it resizes the window. But we will have to
10267 reset the echo_area_buffer in question to nil at the end because
10268 with_echo_area_buffer will sets it to an empty buffer. */
10269 i = display_last_displayed_message_p ? 1 : 0;
10270 no_message_p = NILP (echo_area_buffer[i]);
10272 window_height_changed_p
10273 = with_echo_area_buffer (w, display_last_displayed_message_p,
10274 display_echo_area_1,
10275 (intptr_t) w, Qnil);
10277 if (no_message_p)
10278 echo_area_buffer[i] = Qnil;
10280 unbind_to (count, Qnil);
10281 return window_height_changed_p;
10285 /* Helper for display_echo_area. Display the current buffer which
10286 contains the current echo area message in window W, a mini-window,
10287 a pointer to which is passed in A1. A2..A4 are currently not used.
10288 Change the height of W so that all of the message is displayed.
10289 Value is non-zero if height of W was changed. */
10291 static int
10292 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10294 intptr_t i1 = a1;
10295 struct window *w = (struct window *) i1;
10296 Lisp_Object window;
10297 struct text_pos start;
10298 int window_height_changed_p = 0;
10300 /* Do this before displaying, so that we have a large enough glyph
10301 matrix for the display. If we can't get enough space for the
10302 whole text, display the last N lines. That works by setting w->start. */
10303 window_height_changed_p = resize_mini_window (w, 0);
10305 /* Use the starting position chosen by resize_mini_window. */
10306 SET_TEXT_POS_FROM_MARKER (start, w->start);
10308 /* Display. */
10309 clear_glyph_matrix (w->desired_matrix);
10310 XSETWINDOW (window, w);
10311 try_window (window, start, 0);
10313 return window_height_changed_p;
10317 /* Resize the echo area window to exactly the size needed for the
10318 currently displayed message, if there is one. If a mini-buffer
10319 is active, don't shrink it. */
10321 void
10322 resize_echo_area_exactly (void)
10324 if (BUFFERP (echo_area_buffer[0])
10325 && WINDOWP (echo_area_window))
10327 struct window *w = XWINDOW (echo_area_window);
10328 int resized_p;
10329 Lisp_Object resize_exactly;
10331 if (minibuf_level == 0)
10332 resize_exactly = Qt;
10333 else
10334 resize_exactly = Qnil;
10336 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10337 (intptr_t) w, resize_exactly);
10338 if (resized_p)
10340 ++windows_or_buffers_changed;
10341 ++update_mode_lines;
10342 redisplay_internal ();
10348 /* Callback function for with_echo_area_buffer, when used from
10349 resize_echo_area_exactly. A1 contains a pointer to the window to
10350 resize, EXACTLY non-nil means resize the mini-window exactly to the
10351 size of the text displayed. A3 and A4 are not used. Value is what
10352 resize_mini_window returns. */
10354 static int
10355 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10357 intptr_t i1 = a1;
10358 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10362 /* Resize mini-window W to fit the size of its contents. EXACT_P
10363 means size the window exactly to the size needed. Otherwise, it's
10364 only enlarged until W's buffer is empty.
10366 Set W->start to the right place to begin display. If the whole
10367 contents fit, start at the beginning. Otherwise, start so as
10368 to make the end of the contents appear. This is particularly
10369 important for y-or-n-p, but seems desirable generally.
10371 Value is non-zero if the window height has been changed. */
10374 resize_mini_window (struct window *w, int exact_p)
10376 struct frame *f = XFRAME (w->frame);
10377 int window_height_changed_p = 0;
10379 eassert (MINI_WINDOW_P (w));
10381 /* By default, start display at the beginning. */
10382 set_marker_both (w->start, w->contents,
10383 BUF_BEGV (XBUFFER (w->contents)),
10384 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10386 /* Don't resize windows while redisplaying a window; it would
10387 confuse redisplay functions when the size of the window they are
10388 displaying changes from under them. Such a resizing can happen,
10389 for instance, when which-func prints a long message while
10390 we are running fontification-functions. We're running these
10391 functions with safe_call which binds inhibit-redisplay to t. */
10392 if (!NILP (Vinhibit_redisplay))
10393 return 0;
10395 /* Nil means don't try to resize. */
10396 if (NILP (Vresize_mini_windows)
10397 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10398 return 0;
10400 if (!FRAME_MINIBUF_ONLY_P (f))
10402 struct it it;
10403 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10404 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10405 int height;
10406 EMACS_INT max_height;
10407 int unit = FRAME_LINE_HEIGHT (f);
10408 struct text_pos start;
10409 struct buffer *old_current_buffer = NULL;
10411 if (current_buffer != XBUFFER (w->contents))
10413 old_current_buffer = current_buffer;
10414 set_buffer_internal (XBUFFER (w->contents));
10417 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10419 /* Compute the max. number of lines specified by the user. */
10420 if (FLOATP (Vmax_mini_window_height))
10421 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10422 else if (INTEGERP (Vmax_mini_window_height))
10423 max_height = XINT (Vmax_mini_window_height);
10424 else
10425 max_height = total_height / 4;
10427 /* Correct that max. height if it's bogus. */
10428 max_height = clip_to_bounds (1, max_height, total_height);
10430 /* Find out the height of the text in the window. */
10431 if (it.line_wrap == TRUNCATE)
10432 height = 1;
10433 else
10435 last_height = 0;
10436 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10437 if (it.max_ascent == 0 && it.max_descent == 0)
10438 height = it.current_y + last_height;
10439 else
10440 height = it.current_y + it.max_ascent + it.max_descent;
10441 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10442 height = (height + unit - 1) / unit;
10445 /* Compute a suitable window start. */
10446 if (height > max_height)
10448 height = max_height;
10449 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10450 move_it_vertically_backward (&it, (height - 1) * unit);
10451 start = it.current.pos;
10453 else
10454 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10455 SET_MARKER_FROM_TEXT_POS (w->start, start);
10457 if (EQ (Vresize_mini_windows, Qgrow_only))
10459 /* Let it grow only, until we display an empty message, in which
10460 case the window shrinks again. */
10461 if (height > WINDOW_TOTAL_LINES (w))
10463 int old_height = WINDOW_TOTAL_LINES (w);
10464 freeze_window_starts (f, 1);
10465 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10466 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10468 else if (height < WINDOW_TOTAL_LINES (w)
10469 && (exact_p || BEGV == ZV))
10471 int old_height = WINDOW_TOTAL_LINES (w);
10472 freeze_window_starts (f, 0);
10473 shrink_mini_window (w);
10474 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10477 else
10479 /* Always resize to exact size needed. */
10480 if (height > WINDOW_TOTAL_LINES (w))
10482 int old_height = WINDOW_TOTAL_LINES (w);
10483 freeze_window_starts (f, 1);
10484 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10485 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10487 else if (height < WINDOW_TOTAL_LINES (w))
10489 int old_height = WINDOW_TOTAL_LINES (w);
10490 freeze_window_starts (f, 0);
10491 shrink_mini_window (w);
10493 if (height)
10495 freeze_window_starts (f, 1);
10496 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10499 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10503 if (old_current_buffer)
10504 set_buffer_internal (old_current_buffer);
10507 return window_height_changed_p;
10511 /* Value is the current message, a string, or nil if there is no
10512 current message. */
10514 Lisp_Object
10515 current_message (void)
10517 Lisp_Object msg;
10519 if (!BUFFERP (echo_area_buffer[0]))
10520 msg = Qnil;
10521 else
10523 with_echo_area_buffer (0, 0, current_message_1,
10524 (intptr_t) &msg, Qnil);
10525 if (NILP (msg))
10526 echo_area_buffer[0] = Qnil;
10529 return msg;
10533 static int
10534 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10536 intptr_t i1 = a1;
10537 Lisp_Object *msg = (Lisp_Object *) i1;
10539 if (Z > BEG)
10540 *msg = make_buffer_string (BEG, Z, 1);
10541 else
10542 *msg = Qnil;
10543 return 0;
10547 /* Push the current message on Vmessage_stack for later restoration
10548 by restore_message. Value is non-zero if the current message isn't
10549 empty. This is a relatively infrequent operation, so it's not
10550 worth optimizing. */
10552 bool
10553 push_message (void)
10555 Lisp_Object msg = current_message ();
10556 Vmessage_stack = Fcons (msg, Vmessage_stack);
10557 return STRINGP (msg);
10561 /* Restore message display from the top of Vmessage_stack. */
10563 void
10564 restore_message (void)
10566 eassert (CONSP (Vmessage_stack));
10567 message3_nolog (XCAR (Vmessage_stack));
10571 /* Handler for unwind-protect calling pop_message. */
10573 void
10574 pop_message_unwind (void)
10576 /* Pop the top-most entry off Vmessage_stack. */
10577 eassert (CONSP (Vmessage_stack));
10578 Vmessage_stack = XCDR (Vmessage_stack);
10582 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10583 exits. If the stack is not empty, we have a missing pop_message
10584 somewhere. */
10586 void
10587 check_message_stack (void)
10589 if (!NILP (Vmessage_stack))
10590 emacs_abort ();
10594 /* Truncate to NCHARS what will be displayed in the echo area the next
10595 time we display it---but don't redisplay it now. */
10597 void
10598 truncate_echo_area (ptrdiff_t nchars)
10600 if (nchars == 0)
10601 echo_area_buffer[0] = Qnil;
10602 else if (!noninteractive
10603 && INTERACTIVE
10604 && !NILP (echo_area_buffer[0]))
10606 struct frame *sf = SELECTED_FRAME ();
10607 /* Error messages get reported properly by cmd_error, so this must be
10608 just an informative message; if the frame hasn't really been
10609 initialized yet, just toss it. */
10610 if (sf->glyphs_initialized_p)
10611 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10616 /* Helper function for truncate_echo_area. Truncate the current
10617 message to at most NCHARS characters. */
10619 static int
10620 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10622 if (BEG + nchars < Z)
10623 del_range (BEG + nchars, Z);
10624 if (Z == BEG)
10625 echo_area_buffer[0] = Qnil;
10626 return 0;
10629 /* Set the current message to STRING. */
10631 static void
10632 set_message (Lisp_Object string)
10634 eassert (STRINGP (string));
10636 message_enable_multibyte = STRING_MULTIBYTE (string);
10638 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10639 message_buf_print = 0;
10640 help_echo_showing_p = 0;
10642 if (STRINGP (Vdebug_on_message)
10643 && STRINGP (string)
10644 && fast_string_match (Vdebug_on_message, string) >= 0)
10645 call_debugger (list2 (Qerror, string));
10649 /* Helper function for set_message. First argument is ignored and second
10650 argument has the same meaning as for set_message.
10651 This function is called with the echo area buffer being current. */
10653 static int
10654 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10656 eassert (STRINGP (string));
10658 /* Change multibyteness of the echo buffer appropriately. */
10659 if (message_enable_multibyte
10660 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10661 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10663 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10664 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10665 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10667 /* Insert new message at BEG. */
10668 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10670 /* This function takes care of single/multibyte conversion.
10671 We just have to ensure that the echo area buffer has the right
10672 setting of enable_multibyte_characters. */
10673 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10675 return 0;
10679 /* Clear messages. CURRENT_P non-zero means clear the current
10680 message. LAST_DISPLAYED_P non-zero means clear the message
10681 last displayed. */
10683 void
10684 clear_message (int current_p, int last_displayed_p)
10686 if (current_p)
10688 echo_area_buffer[0] = Qnil;
10689 message_cleared_p = 1;
10692 if (last_displayed_p)
10693 echo_area_buffer[1] = Qnil;
10695 message_buf_print = 0;
10698 /* Clear garbaged frames.
10700 This function is used where the old redisplay called
10701 redraw_garbaged_frames which in turn called redraw_frame which in
10702 turn called clear_frame. The call to clear_frame was a source of
10703 flickering. I believe a clear_frame is not necessary. It should
10704 suffice in the new redisplay to invalidate all current matrices,
10705 and ensure a complete redisplay of all windows. */
10707 static void
10708 clear_garbaged_frames (void)
10710 if (frame_garbaged)
10712 Lisp_Object tail, frame;
10713 int changed_count = 0;
10715 FOR_EACH_FRAME (tail, frame)
10717 struct frame *f = XFRAME (frame);
10719 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10721 if (f->resized_p)
10723 redraw_frame (f);
10724 f->force_flush_display_p = 1;
10726 clear_current_matrices (f);
10727 changed_count++;
10728 f->garbaged = 0;
10729 f->resized_p = 0;
10733 frame_garbaged = 0;
10734 if (changed_count)
10735 ++windows_or_buffers_changed;
10740 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10741 is non-zero update selected_frame. Value is non-zero if the
10742 mini-windows height has been changed. */
10744 static int
10745 echo_area_display (int update_frame_p)
10747 Lisp_Object mini_window;
10748 struct window *w;
10749 struct frame *f;
10750 int window_height_changed_p = 0;
10751 struct frame *sf = SELECTED_FRAME ();
10753 mini_window = FRAME_MINIBUF_WINDOW (sf);
10754 w = XWINDOW (mini_window);
10755 f = XFRAME (WINDOW_FRAME (w));
10757 /* Don't display if frame is invisible or not yet initialized. */
10758 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10759 return 0;
10761 #ifdef HAVE_WINDOW_SYSTEM
10762 /* When Emacs starts, selected_frame may be the initial terminal
10763 frame. If we let this through, a message would be displayed on
10764 the terminal. */
10765 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10766 return 0;
10767 #endif /* HAVE_WINDOW_SYSTEM */
10769 /* Redraw garbaged frames. */
10770 clear_garbaged_frames ();
10772 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10774 echo_area_window = mini_window;
10775 window_height_changed_p = display_echo_area (w);
10776 w->must_be_updated_p = 1;
10778 /* Update the display, unless called from redisplay_internal.
10779 Also don't update the screen during redisplay itself. The
10780 update will happen at the end of redisplay, and an update
10781 here could cause confusion. */
10782 if (update_frame_p && !redisplaying_p)
10784 int n = 0;
10786 /* If the display update has been interrupted by pending
10787 input, update mode lines in the frame. Due to the
10788 pending input, it might have been that redisplay hasn't
10789 been called, so that mode lines above the echo area are
10790 garbaged. This looks odd, so we prevent it here. */
10791 if (!display_completed)
10792 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10794 if (window_height_changed_p
10795 /* Don't do this if Emacs is shutting down. Redisplay
10796 needs to run hooks. */
10797 && !NILP (Vrun_hooks))
10799 /* Must update other windows. Likewise as in other
10800 cases, don't let this update be interrupted by
10801 pending input. */
10802 ptrdiff_t count = SPECPDL_INDEX ();
10803 specbind (Qredisplay_dont_pause, Qt);
10804 windows_or_buffers_changed = 1;
10805 redisplay_internal ();
10806 unbind_to (count, Qnil);
10808 else if (FRAME_WINDOW_P (f) && n == 0)
10810 /* Window configuration is the same as before.
10811 Can do with a display update of the echo area,
10812 unless we displayed some mode lines. */
10813 update_single_window (w, 1);
10814 FRAME_RIF (f)->flush_display (f);
10816 else
10817 update_frame (f, 1, 1);
10819 /* If cursor is in the echo area, make sure that the next
10820 redisplay displays the minibuffer, so that the cursor will
10821 be replaced with what the minibuffer wants. */
10822 if (cursor_in_echo_area)
10823 ++windows_or_buffers_changed;
10826 else if (!EQ (mini_window, selected_window))
10827 windows_or_buffers_changed++;
10829 /* Last displayed message is now the current message. */
10830 echo_area_buffer[1] = echo_area_buffer[0];
10831 /* Inform read_char that we're not echoing. */
10832 echo_message_buffer = Qnil;
10834 /* Prevent redisplay optimization in redisplay_internal by resetting
10835 this_line_start_pos. This is done because the mini-buffer now
10836 displays the message instead of its buffer text. */
10837 if (EQ (mini_window, selected_window))
10838 CHARPOS (this_line_start_pos) = 0;
10840 return window_height_changed_p;
10843 /* Nonzero if the current window's buffer is shown in more than one
10844 window and was modified since last redisplay. */
10846 static int
10847 buffer_shared_and_changed (void)
10849 return (buffer_window_count (current_buffer) > 1
10850 && UNCHANGED_MODIFIED < MODIFF);
10853 /* Nonzero if W doesn't reflect the actual state of current buffer due
10854 to its text or overlays change. FIXME: this may be called when
10855 XBUFFER (w->contents) != current_buffer, which looks suspicious. */
10857 static int
10858 window_outdated (struct window *w)
10860 return (w->last_modified < MODIFF
10861 || w->last_overlay_modified < OVERLAY_MODIFF);
10864 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10865 is enabled and mark of W's buffer was changed since last W's update. */
10867 static int
10868 window_buffer_changed (struct window *w)
10870 struct buffer *b = XBUFFER (w->contents);
10872 eassert (BUFFER_LIVE_P (b));
10874 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10875 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10876 != (w->region_showing != 0)));
10879 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10881 static int
10882 mode_line_update_needed (struct window *w)
10884 return (w->column_number_displayed != -1
10885 && !(PT == w->last_point && !window_outdated (w))
10886 && (w->column_number_displayed != current_column ()));
10889 /***********************************************************************
10890 Mode Lines and Frame Titles
10891 ***********************************************************************/
10893 /* A buffer for constructing non-propertized mode-line strings and
10894 frame titles in it; allocated from the heap in init_xdisp and
10895 resized as needed in store_mode_line_noprop_char. */
10897 static char *mode_line_noprop_buf;
10899 /* The buffer's end, and a current output position in it. */
10901 static char *mode_line_noprop_buf_end;
10902 static char *mode_line_noprop_ptr;
10904 #define MODE_LINE_NOPROP_LEN(start) \
10905 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10907 static enum {
10908 MODE_LINE_DISPLAY = 0,
10909 MODE_LINE_TITLE,
10910 MODE_LINE_NOPROP,
10911 MODE_LINE_STRING
10912 } mode_line_target;
10914 /* Alist that caches the results of :propertize.
10915 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10916 static Lisp_Object mode_line_proptrans_alist;
10918 /* List of strings making up the mode-line. */
10919 static Lisp_Object mode_line_string_list;
10921 /* Base face property when building propertized mode line string. */
10922 static Lisp_Object mode_line_string_face;
10923 static Lisp_Object mode_line_string_face_prop;
10926 /* Unwind data for mode line strings */
10928 static Lisp_Object Vmode_line_unwind_vector;
10930 static Lisp_Object
10931 format_mode_line_unwind_data (struct frame *target_frame,
10932 struct buffer *obuf,
10933 Lisp_Object owin,
10934 int save_proptrans)
10936 Lisp_Object vector, tmp;
10938 /* Reduce consing by keeping one vector in
10939 Vwith_echo_area_save_vector. */
10940 vector = Vmode_line_unwind_vector;
10941 Vmode_line_unwind_vector = Qnil;
10943 if (NILP (vector))
10944 vector = Fmake_vector (make_number (10), Qnil);
10946 ASET (vector, 0, make_number (mode_line_target));
10947 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10948 ASET (vector, 2, mode_line_string_list);
10949 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10950 ASET (vector, 4, mode_line_string_face);
10951 ASET (vector, 5, mode_line_string_face_prop);
10953 if (obuf)
10954 XSETBUFFER (tmp, obuf);
10955 else
10956 tmp = Qnil;
10957 ASET (vector, 6, tmp);
10958 ASET (vector, 7, owin);
10959 if (target_frame)
10961 /* Similarly to `with-selected-window', if the operation selects
10962 a window on another frame, we must restore that frame's
10963 selected window, and (for a tty) the top-frame. */
10964 ASET (vector, 8, target_frame->selected_window);
10965 if (FRAME_TERMCAP_P (target_frame))
10966 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10969 return vector;
10972 static void
10973 unwind_format_mode_line (Lisp_Object vector)
10975 Lisp_Object old_window = AREF (vector, 7);
10976 Lisp_Object target_frame_window = AREF (vector, 8);
10977 Lisp_Object old_top_frame = AREF (vector, 9);
10979 mode_line_target = XINT (AREF (vector, 0));
10980 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10981 mode_line_string_list = AREF (vector, 2);
10982 if (! EQ (AREF (vector, 3), Qt))
10983 mode_line_proptrans_alist = AREF (vector, 3);
10984 mode_line_string_face = AREF (vector, 4);
10985 mode_line_string_face_prop = AREF (vector, 5);
10987 /* Select window before buffer, since it may change the buffer. */
10988 if (!NILP (old_window))
10990 /* If the operation that we are unwinding had selected a window
10991 on a different frame, reset its frame-selected-window. For a
10992 text terminal, reset its top-frame if necessary. */
10993 if (!NILP (target_frame_window))
10995 Lisp_Object frame
10996 = WINDOW_FRAME (XWINDOW (target_frame_window));
10998 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10999 Fselect_window (target_frame_window, Qt);
11001 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11002 Fselect_frame (old_top_frame, Qt);
11005 Fselect_window (old_window, Qt);
11008 if (!NILP (AREF (vector, 6)))
11010 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11011 ASET (vector, 6, Qnil);
11014 Vmode_line_unwind_vector = vector;
11018 /* Store a single character C for the frame title in mode_line_noprop_buf.
11019 Re-allocate mode_line_noprop_buf if necessary. */
11021 static void
11022 store_mode_line_noprop_char (char c)
11024 /* If output position has reached the end of the allocated buffer,
11025 increase the buffer's size. */
11026 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11028 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11029 ptrdiff_t size = len;
11030 mode_line_noprop_buf =
11031 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11032 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11033 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11036 *mode_line_noprop_ptr++ = c;
11040 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11041 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11042 characters that yield more columns than PRECISION; PRECISION <= 0
11043 means copy the whole string. Pad with spaces until FIELD_WIDTH
11044 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11045 pad. Called from display_mode_element when it is used to build a
11046 frame title. */
11048 static int
11049 store_mode_line_noprop (const char *string, int field_width, int precision)
11051 const unsigned char *str = (const unsigned char *) string;
11052 int n = 0;
11053 ptrdiff_t dummy, nbytes;
11055 /* Copy at most PRECISION chars from STR. */
11056 nbytes = strlen (string);
11057 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11058 while (nbytes--)
11059 store_mode_line_noprop_char (*str++);
11061 /* Fill up with spaces until FIELD_WIDTH reached. */
11062 while (field_width > 0
11063 && n < field_width)
11065 store_mode_line_noprop_char (' ');
11066 ++n;
11069 return n;
11072 /***********************************************************************
11073 Frame Titles
11074 ***********************************************************************/
11076 #ifdef HAVE_WINDOW_SYSTEM
11078 /* Set the title of FRAME, if it has changed. The title format is
11079 Vicon_title_format if FRAME is iconified, otherwise it is
11080 frame_title_format. */
11082 static void
11083 x_consider_frame_title (Lisp_Object frame)
11085 struct frame *f = XFRAME (frame);
11087 if (FRAME_WINDOW_P (f)
11088 || FRAME_MINIBUF_ONLY_P (f)
11089 || f->explicit_name)
11091 /* Do we have more than one visible frame on this X display? */
11092 Lisp_Object tail, other_frame, fmt;
11093 ptrdiff_t title_start;
11094 char *title;
11095 ptrdiff_t len;
11096 struct it it;
11097 ptrdiff_t count = SPECPDL_INDEX ();
11099 FOR_EACH_FRAME (tail, other_frame)
11101 struct frame *tf = XFRAME (other_frame);
11103 if (tf != f
11104 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11105 && !FRAME_MINIBUF_ONLY_P (tf)
11106 && !EQ (other_frame, tip_frame)
11107 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11108 break;
11111 /* Set global variable indicating that multiple frames exist. */
11112 multiple_frames = CONSP (tail);
11114 /* Switch to the buffer of selected window of the frame. Set up
11115 mode_line_target so that display_mode_element will output into
11116 mode_line_noprop_buf; then display the title. */
11117 record_unwind_protect (unwind_format_mode_line,
11118 format_mode_line_unwind_data
11119 (f, current_buffer, selected_window, 0));
11121 Fselect_window (f->selected_window, Qt);
11122 set_buffer_internal_1
11123 (XBUFFER (XWINDOW (f->selected_window)->contents));
11124 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11126 mode_line_target = MODE_LINE_TITLE;
11127 title_start = MODE_LINE_NOPROP_LEN (0);
11128 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11129 NULL, DEFAULT_FACE_ID);
11130 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11131 len = MODE_LINE_NOPROP_LEN (title_start);
11132 title = mode_line_noprop_buf + title_start;
11133 unbind_to (count, Qnil);
11135 /* Set the title only if it's changed. This avoids consing in
11136 the common case where it hasn't. (If it turns out that we've
11137 already wasted too much time by walking through the list with
11138 display_mode_element, then we might need to optimize at a
11139 higher level than this.) */
11140 if (! STRINGP (f->name)
11141 || SBYTES (f->name) != len
11142 || memcmp (title, SDATA (f->name), len) != 0)
11143 x_implicitly_set_name (f, make_string (title, len), Qnil);
11147 #endif /* not HAVE_WINDOW_SYSTEM */
11150 /***********************************************************************
11151 Menu Bars
11152 ***********************************************************************/
11155 /* Prepare for redisplay by updating menu-bar item lists when
11156 appropriate. This can call eval. */
11158 void
11159 prepare_menu_bars (void)
11161 int all_windows;
11162 struct gcpro gcpro1, gcpro2;
11163 struct frame *f;
11164 Lisp_Object tooltip_frame;
11166 #ifdef HAVE_WINDOW_SYSTEM
11167 tooltip_frame = tip_frame;
11168 #else
11169 tooltip_frame = Qnil;
11170 #endif
11172 /* Update all frame titles based on their buffer names, etc. We do
11173 this before the menu bars so that the buffer-menu will show the
11174 up-to-date frame titles. */
11175 #ifdef HAVE_WINDOW_SYSTEM
11176 if (windows_or_buffers_changed || update_mode_lines)
11178 Lisp_Object tail, frame;
11180 FOR_EACH_FRAME (tail, frame)
11182 f = XFRAME (frame);
11183 if (!EQ (frame, tooltip_frame)
11184 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11185 x_consider_frame_title (frame);
11188 #endif /* HAVE_WINDOW_SYSTEM */
11190 /* Update the menu bar item lists, if appropriate. This has to be
11191 done before any actual redisplay or generation of display lines. */
11192 all_windows = (update_mode_lines
11193 || buffer_shared_and_changed ()
11194 || windows_or_buffers_changed);
11195 if (all_windows)
11197 Lisp_Object tail, frame;
11198 ptrdiff_t count = SPECPDL_INDEX ();
11199 /* 1 means that update_menu_bar has run its hooks
11200 so any further calls to update_menu_bar shouldn't do so again. */
11201 int menu_bar_hooks_run = 0;
11203 record_unwind_save_match_data ();
11205 FOR_EACH_FRAME (tail, frame)
11207 f = XFRAME (frame);
11209 /* Ignore tooltip frame. */
11210 if (EQ (frame, tooltip_frame))
11211 continue;
11213 /* If a window on this frame changed size, report that to
11214 the user and clear the size-change flag. */
11215 if (FRAME_WINDOW_SIZES_CHANGED (f))
11217 Lisp_Object functions;
11219 /* Clear flag first in case we get an error below. */
11220 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11221 functions = Vwindow_size_change_functions;
11222 GCPRO2 (tail, functions);
11224 while (CONSP (functions))
11226 if (!EQ (XCAR (functions), Qt))
11227 call1 (XCAR (functions), frame);
11228 functions = XCDR (functions);
11230 UNGCPRO;
11233 GCPRO1 (tail);
11234 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11235 #ifdef HAVE_WINDOW_SYSTEM
11236 update_tool_bar (f, 0);
11237 #endif
11238 #ifdef HAVE_NS
11239 if (windows_or_buffers_changed
11240 && FRAME_NS_P (f))
11241 ns_set_doc_edited
11242 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11243 #endif
11244 UNGCPRO;
11247 unbind_to (count, Qnil);
11249 else
11251 struct frame *sf = SELECTED_FRAME ();
11252 update_menu_bar (sf, 1, 0);
11253 #ifdef HAVE_WINDOW_SYSTEM
11254 update_tool_bar (sf, 1);
11255 #endif
11260 /* Update the menu bar item list for frame F. This has to be done
11261 before we start to fill in any display lines, because it can call
11262 eval.
11264 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11266 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11267 already ran the menu bar hooks for this redisplay, so there
11268 is no need to run them again. The return value is the
11269 updated value of this flag, to pass to the next call. */
11271 static int
11272 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11274 Lisp_Object window;
11275 register struct window *w;
11277 /* If called recursively during a menu update, do nothing. This can
11278 happen when, for instance, an activate-menubar-hook causes a
11279 redisplay. */
11280 if (inhibit_menubar_update)
11281 return hooks_run;
11283 window = FRAME_SELECTED_WINDOW (f);
11284 w = XWINDOW (window);
11286 if (FRAME_WINDOW_P (f)
11288 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11289 || defined (HAVE_NS) || defined (USE_GTK)
11290 FRAME_EXTERNAL_MENU_BAR (f)
11291 #else
11292 FRAME_MENU_BAR_LINES (f) > 0
11293 #endif
11294 : FRAME_MENU_BAR_LINES (f) > 0)
11296 /* If the user has switched buffers or windows, we need to
11297 recompute to reflect the new bindings. But we'll
11298 recompute when update_mode_lines is set too; that means
11299 that people can use force-mode-line-update to request
11300 that the menu bar be recomputed. The adverse effect on
11301 the rest of the redisplay algorithm is about the same as
11302 windows_or_buffers_changed anyway. */
11303 if (windows_or_buffers_changed
11304 /* This used to test w->update_mode_line, but we believe
11305 there is no need to recompute the menu in that case. */
11306 || update_mode_lines
11307 || window_buffer_changed (w))
11309 struct buffer *prev = current_buffer;
11310 ptrdiff_t count = SPECPDL_INDEX ();
11312 specbind (Qinhibit_menubar_update, Qt);
11314 set_buffer_internal_1 (XBUFFER (w->contents));
11315 if (save_match_data)
11316 record_unwind_save_match_data ();
11317 if (NILP (Voverriding_local_map_menu_flag))
11319 specbind (Qoverriding_terminal_local_map, Qnil);
11320 specbind (Qoverriding_local_map, Qnil);
11323 if (!hooks_run)
11325 /* Run the Lucid hook. */
11326 safe_run_hooks (Qactivate_menubar_hook);
11328 /* If it has changed current-menubar from previous value,
11329 really recompute the menu-bar from the value. */
11330 if (! NILP (Vlucid_menu_bar_dirty_flag))
11331 call0 (Qrecompute_lucid_menubar);
11333 safe_run_hooks (Qmenu_bar_update_hook);
11335 hooks_run = 1;
11338 XSETFRAME (Vmenu_updating_frame, f);
11339 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11341 /* Redisplay the menu bar in case we changed it. */
11342 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11343 || defined (HAVE_NS) || defined (USE_GTK)
11344 if (FRAME_WINDOW_P (f))
11346 #if defined (HAVE_NS)
11347 /* All frames on Mac OS share the same menubar. So only
11348 the selected frame should be allowed to set it. */
11349 if (f == SELECTED_FRAME ())
11350 #endif
11351 set_frame_menubar (f, 0, 0);
11353 else
11354 /* On a terminal screen, the menu bar is an ordinary screen
11355 line, and this makes it get updated. */
11356 w->update_mode_line = 1;
11357 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11358 /* In the non-toolkit version, the menu bar is an ordinary screen
11359 line, and this makes it get updated. */
11360 w->update_mode_line = 1;
11361 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11363 unbind_to (count, Qnil);
11364 set_buffer_internal_1 (prev);
11368 return hooks_run;
11373 /***********************************************************************
11374 Output Cursor
11375 ***********************************************************************/
11377 #ifdef HAVE_WINDOW_SYSTEM
11379 /* EXPORT:
11380 Nominal cursor position -- where to draw output.
11381 HPOS and VPOS are window relative glyph matrix coordinates.
11382 X and Y are window relative pixel coordinates. */
11384 struct cursor_pos output_cursor;
11387 /* EXPORT:
11388 Set the global variable output_cursor to CURSOR. All cursor
11389 positions are relative to updated_window. */
11391 void
11392 set_output_cursor (struct cursor_pos *cursor)
11394 output_cursor.hpos = cursor->hpos;
11395 output_cursor.vpos = cursor->vpos;
11396 output_cursor.x = cursor->x;
11397 output_cursor.y = cursor->y;
11401 /* EXPORT for RIF:
11402 Set a nominal cursor position.
11404 HPOS and VPOS are column/row positions in a window glyph matrix. X
11405 and Y are window text area relative pixel positions.
11407 If this is done during an update, updated_window will contain the
11408 window that is being updated and the position is the future output
11409 cursor position for that window. If updated_window is null, use
11410 selected_window and display the cursor at the given position. */
11412 void
11413 x_cursor_to (int vpos, int hpos, int y, int x)
11415 struct window *w;
11417 /* If updated_window is not set, work on selected_window. */
11418 if (updated_window)
11419 w = updated_window;
11420 else
11421 w = XWINDOW (selected_window);
11423 /* Set the output cursor. */
11424 output_cursor.hpos = hpos;
11425 output_cursor.vpos = vpos;
11426 output_cursor.x = x;
11427 output_cursor.y = y;
11429 /* If not called as part of an update, really display the cursor.
11430 This will also set the cursor position of W. */
11431 if (updated_window == NULL)
11433 block_input ();
11434 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11435 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11436 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11437 unblock_input ();
11441 #endif /* HAVE_WINDOW_SYSTEM */
11444 /***********************************************************************
11445 Tool-bars
11446 ***********************************************************************/
11448 #ifdef HAVE_WINDOW_SYSTEM
11450 /* Where the mouse was last time we reported a mouse event. */
11452 FRAME_PTR last_mouse_frame;
11454 /* Tool-bar item index of the item on which a mouse button was pressed
11455 or -1. */
11457 int last_tool_bar_item;
11459 /* Select `frame' temporarily without running all the code in
11460 do_switch_frame.
11461 FIXME: Maybe do_switch_frame should be trimmed down similarly
11462 when `norecord' is set. */
11463 static void
11464 fast_set_selected_frame (Lisp_Object frame)
11466 if (!EQ (selected_frame, frame))
11468 selected_frame = frame;
11469 selected_window = XFRAME (frame)->selected_window;
11473 /* Update the tool-bar item list for frame F. This has to be done
11474 before we start to fill in any display lines. Called from
11475 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11476 and restore it here. */
11478 static void
11479 update_tool_bar (struct frame *f, int save_match_data)
11481 #if defined (USE_GTK) || defined (HAVE_NS)
11482 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11483 #else
11484 int do_update = WINDOWP (f->tool_bar_window)
11485 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11486 #endif
11488 if (do_update)
11490 Lisp_Object window;
11491 struct window *w;
11493 window = FRAME_SELECTED_WINDOW (f);
11494 w = XWINDOW (window);
11496 /* If the user has switched buffers or windows, we need to
11497 recompute to reflect the new bindings. But we'll
11498 recompute when update_mode_lines is set too; that means
11499 that people can use force-mode-line-update to request
11500 that the menu bar be recomputed. The adverse effect on
11501 the rest of the redisplay algorithm is about the same as
11502 windows_or_buffers_changed anyway. */
11503 if (windows_or_buffers_changed
11504 || w->update_mode_line
11505 || update_mode_lines
11506 || window_buffer_changed (w))
11508 struct buffer *prev = current_buffer;
11509 ptrdiff_t count = SPECPDL_INDEX ();
11510 Lisp_Object frame, new_tool_bar;
11511 int new_n_tool_bar;
11512 struct gcpro gcpro1;
11514 /* Set current_buffer to the buffer of the selected
11515 window of the frame, so that we get the right local
11516 keymaps. */
11517 set_buffer_internal_1 (XBUFFER (w->contents));
11519 /* Save match data, if we must. */
11520 if (save_match_data)
11521 record_unwind_save_match_data ();
11523 /* Make sure that we don't accidentally use bogus keymaps. */
11524 if (NILP (Voverriding_local_map_menu_flag))
11526 specbind (Qoverriding_terminal_local_map, Qnil);
11527 specbind (Qoverriding_local_map, Qnil);
11530 GCPRO1 (new_tool_bar);
11532 /* We must temporarily set the selected frame to this frame
11533 before calling tool_bar_items, because the calculation of
11534 the tool-bar keymap uses the selected frame (see
11535 `tool-bar-make-keymap' in tool-bar.el). */
11536 eassert (EQ (selected_window,
11537 /* Since we only explicitly preserve selected_frame,
11538 check that selected_window would be redundant. */
11539 XFRAME (selected_frame)->selected_window));
11540 record_unwind_protect (fast_set_selected_frame, selected_frame);
11541 XSETFRAME (frame, f);
11542 fast_set_selected_frame (frame);
11544 /* Build desired tool-bar items from keymaps. */
11545 new_tool_bar
11546 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11547 &new_n_tool_bar);
11549 /* Redisplay the tool-bar if we changed it. */
11550 if (new_n_tool_bar != f->n_tool_bar_items
11551 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11553 /* Redisplay that happens asynchronously due to an expose event
11554 may access f->tool_bar_items. Make sure we update both
11555 variables within BLOCK_INPUT so no such event interrupts. */
11556 block_input ();
11557 fset_tool_bar_items (f, new_tool_bar);
11558 f->n_tool_bar_items = new_n_tool_bar;
11559 w->update_mode_line = 1;
11560 unblock_input ();
11563 UNGCPRO;
11565 unbind_to (count, Qnil);
11566 set_buffer_internal_1 (prev);
11572 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11573 F's desired tool-bar contents. F->tool_bar_items must have
11574 been set up previously by calling prepare_menu_bars. */
11576 static void
11577 build_desired_tool_bar_string (struct frame *f)
11579 int i, size, size_needed;
11580 struct gcpro gcpro1, gcpro2, gcpro3;
11581 Lisp_Object image, plist, props;
11583 image = plist = props = Qnil;
11584 GCPRO3 (image, plist, props);
11586 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11587 Otherwise, make a new string. */
11589 /* The size of the string we might be able to reuse. */
11590 size = (STRINGP (f->desired_tool_bar_string)
11591 ? SCHARS (f->desired_tool_bar_string)
11592 : 0);
11594 /* We need one space in the string for each image. */
11595 size_needed = f->n_tool_bar_items;
11597 /* Reuse f->desired_tool_bar_string, if possible. */
11598 if (size < size_needed || NILP (f->desired_tool_bar_string))
11599 fset_desired_tool_bar_string
11600 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11601 else
11603 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11604 Fremove_text_properties (make_number (0), make_number (size),
11605 props, f->desired_tool_bar_string);
11608 /* Put a `display' property on the string for the images to display,
11609 put a `menu_item' property on tool-bar items with a value that
11610 is the index of the item in F's tool-bar item vector. */
11611 for (i = 0; i < f->n_tool_bar_items; ++i)
11613 #define PROP(IDX) \
11614 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11616 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11617 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11618 int hmargin, vmargin, relief, idx, end;
11620 /* If image is a vector, choose the image according to the
11621 button state. */
11622 image = PROP (TOOL_BAR_ITEM_IMAGES);
11623 if (VECTORP (image))
11625 if (enabled_p)
11626 idx = (selected_p
11627 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11628 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11629 else
11630 idx = (selected_p
11631 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11632 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11634 eassert (ASIZE (image) >= idx);
11635 image = AREF (image, idx);
11637 else
11638 idx = -1;
11640 /* Ignore invalid image specifications. */
11641 if (!valid_image_p (image))
11642 continue;
11644 /* Display the tool-bar button pressed, or depressed. */
11645 plist = Fcopy_sequence (XCDR (image));
11647 /* Compute margin and relief to draw. */
11648 relief = (tool_bar_button_relief >= 0
11649 ? tool_bar_button_relief
11650 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11651 hmargin = vmargin = relief;
11653 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11654 INT_MAX - max (hmargin, vmargin)))
11656 hmargin += XFASTINT (Vtool_bar_button_margin);
11657 vmargin += XFASTINT (Vtool_bar_button_margin);
11659 else if (CONSP (Vtool_bar_button_margin))
11661 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11662 INT_MAX - hmargin))
11663 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11665 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11666 INT_MAX - vmargin))
11667 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11670 if (auto_raise_tool_bar_buttons_p)
11672 /* Add a `:relief' property to the image spec if the item is
11673 selected. */
11674 if (selected_p)
11676 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11677 hmargin -= relief;
11678 vmargin -= relief;
11681 else
11683 /* If image is selected, display it pressed, i.e. with a
11684 negative relief. If it's not selected, display it with a
11685 raised relief. */
11686 plist = Fplist_put (plist, QCrelief,
11687 (selected_p
11688 ? make_number (-relief)
11689 : make_number (relief)));
11690 hmargin -= relief;
11691 vmargin -= relief;
11694 /* Put a margin around the image. */
11695 if (hmargin || vmargin)
11697 if (hmargin == vmargin)
11698 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11699 else
11700 plist = Fplist_put (plist, QCmargin,
11701 Fcons (make_number (hmargin),
11702 make_number (vmargin)));
11705 /* If button is not enabled, and we don't have special images
11706 for the disabled state, make the image appear disabled by
11707 applying an appropriate algorithm to it. */
11708 if (!enabled_p && idx < 0)
11709 plist = Fplist_put (plist, QCconversion, Qdisabled);
11711 /* Put a `display' text property on the string for the image to
11712 display. Put a `menu-item' property on the string that gives
11713 the start of this item's properties in the tool-bar items
11714 vector. */
11715 image = Fcons (Qimage, plist);
11716 props = list4 (Qdisplay, image,
11717 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11719 /* Let the last image hide all remaining spaces in the tool bar
11720 string. The string can be longer than needed when we reuse a
11721 previous string. */
11722 if (i + 1 == f->n_tool_bar_items)
11723 end = SCHARS (f->desired_tool_bar_string);
11724 else
11725 end = i + 1;
11726 Fadd_text_properties (make_number (i), make_number (end),
11727 props, f->desired_tool_bar_string);
11728 #undef PROP
11731 UNGCPRO;
11735 /* Display one line of the tool-bar of frame IT->f.
11737 HEIGHT specifies the desired height of the tool-bar line.
11738 If the actual height of the glyph row is less than HEIGHT, the
11739 row's height is increased to HEIGHT, and the icons are centered
11740 vertically in the new height.
11742 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11743 count a final empty row in case the tool-bar width exactly matches
11744 the window width.
11747 static void
11748 display_tool_bar_line (struct it *it, int height)
11750 struct glyph_row *row = it->glyph_row;
11751 int max_x = it->last_visible_x;
11752 struct glyph *last;
11754 prepare_desired_row (row);
11755 row->y = it->current_y;
11757 /* Note that this isn't made use of if the face hasn't a box,
11758 so there's no need to check the face here. */
11759 it->start_of_box_run_p = 1;
11761 while (it->current_x < max_x)
11763 int x, n_glyphs_before, i, nglyphs;
11764 struct it it_before;
11766 /* Get the next display element. */
11767 if (!get_next_display_element (it))
11769 /* Don't count empty row if we are counting needed tool-bar lines. */
11770 if (height < 0 && !it->hpos)
11771 return;
11772 break;
11775 /* Produce glyphs. */
11776 n_glyphs_before = row->used[TEXT_AREA];
11777 it_before = *it;
11779 PRODUCE_GLYPHS (it);
11781 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11782 i = 0;
11783 x = it_before.current_x;
11784 while (i < nglyphs)
11786 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11788 if (x + glyph->pixel_width > max_x)
11790 /* Glyph doesn't fit on line. Backtrack. */
11791 row->used[TEXT_AREA] = n_glyphs_before;
11792 *it = it_before;
11793 /* If this is the only glyph on this line, it will never fit on the
11794 tool-bar, so skip it. But ensure there is at least one glyph,
11795 so we don't accidentally disable the tool-bar. */
11796 if (n_glyphs_before == 0
11797 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11798 break;
11799 goto out;
11802 ++it->hpos;
11803 x += glyph->pixel_width;
11804 ++i;
11807 /* Stop at line end. */
11808 if (ITERATOR_AT_END_OF_LINE_P (it))
11809 break;
11811 set_iterator_to_next (it, 1);
11814 out:;
11816 row->displays_text_p = row->used[TEXT_AREA] != 0;
11818 /* Use default face for the border below the tool bar.
11820 FIXME: When auto-resize-tool-bars is grow-only, there is
11821 no additional border below the possibly empty tool-bar lines.
11822 So to make the extra empty lines look "normal", we have to
11823 use the tool-bar face for the border too. */
11824 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11825 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11826 it->face_id = DEFAULT_FACE_ID;
11828 extend_face_to_end_of_line (it);
11829 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11830 last->right_box_line_p = 1;
11831 if (last == row->glyphs[TEXT_AREA])
11832 last->left_box_line_p = 1;
11834 /* Make line the desired height and center it vertically. */
11835 if ((height -= it->max_ascent + it->max_descent) > 0)
11837 /* Don't add more than one line height. */
11838 height %= FRAME_LINE_HEIGHT (it->f);
11839 it->max_ascent += height / 2;
11840 it->max_descent += (height + 1) / 2;
11843 compute_line_metrics (it);
11845 /* If line is empty, make it occupy the rest of the tool-bar. */
11846 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11848 row->height = row->phys_height = it->last_visible_y - row->y;
11849 row->visible_height = row->height;
11850 row->ascent = row->phys_ascent = 0;
11851 row->extra_line_spacing = 0;
11854 row->full_width_p = 1;
11855 row->continued_p = 0;
11856 row->truncated_on_left_p = 0;
11857 row->truncated_on_right_p = 0;
11859 it->current_x = it->hpos = 0;
11860 it->current_y += row->height;
11861 ++it->vpos;
11862 ++it->glyph_row;
11866 /* Max tool-bar height. */
11868 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11869 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11871 /* Value is the number of screen lines needed to make all tool-bar
11872 items of frame F visible. The number of actual rows needed is
11873 returned in *N_ROWS if non-NULL. */
11875 static int
11876 tool_bar_lines_needed (struct frame *f, int *n_rows)
11878 struct window *w = XWINDOW (f->tool_bar_window);
11879 struct it it;
11880 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11881 the desired matrix, so use (unused) mode-line row as temporary row to
11882 avoid destroying the first tool-bar row. */
11883 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11885 /* Initialize an iterator for iteration over
11886 F->desired_tool_bar_string in the tool-bar window of frame F. */
11887 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11888 it.first_visible_x = 0;
11889 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11890 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11891 it.paragraph_embedding = L2R;
11893 while (!ITERATOR_AT_END_P (&it))
11895 clear_glyph_row (temp_row);
11896 it.glyph_row = temp_row;
11897 display_tool_bar_line (&it, -1);
11899 clear_glyph_row (temp_row);
11901 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11902 if (n_rows)
11903 *n_rows = it.vpos > 0 ? it.vpos : -1;
11905 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11909 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11910 0, 1, 0,
11911 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11912 If FRAME is nil or omitted, use the selected frame. */)
11913 (Lisp_Object frame)
11915 struct frame *f = decode_any_frame (frame);
11916 struct window *w;
11917 int nlines = 0;
11919 if (WINDOWP (f->tool_bar_window)
11920 && (w = XWINDOW (f->tool_bar_window),
11921 WINDOW_TOTAL_LINES (w) > 0))
11923 update_tool_bar (f, 1);
11924 if (f->n_tool_bar_items)
11926 build_desired_tool_bar_string (f);
11927 nlines = tool_bar_lines_needed (f, NULL);
11931 return make_number (nlines);
11935 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11936 height should be changed. */
11938 static int
11939 redisplay_tool_bar (struct frame *f)
11941 struct window *w;
11942 struct it it;
11943 struct glyph_row *row;
11945 #if defined (USE_GTK) || defined (HAVE_NS)
11946 if (FRAME_EXTERNAL_TOOL_BAR (f))
11947 update_frame_tool_bar (f);
11948 return 0;
11949 #endif
11951 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11952 do anything. This means you must start with tool-bar-lines
11953 non-zero to get the auto-sizing effect. Or in other words, you
11954 can turn off tool-bars by specifying tool-bar-lines zero. */
11955 if (!WINDOWP (f->tool_bar_window)
11956 || (w = XWINDOW (f->tool_bar_window),
11957 WINDOW_TOTAL_LINES (w) == 0))
11958 return 0;
11960 /* Set up an iterator for the tool-bar window. */
11961 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11962 it.first_visible_x = 0;
11963 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11964 row = it.glyph_row;
11966 /* Build a string that represents the contents of the tool-bar. */
11967 build_desired_tool_bar_string (f);
11968 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11969 /* FIXME: This should be controlled by a user option. But it
11970 doesn't make sense to have an R2L tool bar if the menu bar cannot
11971 be drawn also R2L, and making the menu bar R2L is tricky due
11972 toolkit-specific code that implements it. If an R2L tool bar is
11973 ever supported, display_tool_bar_line should also be augmented to
11974 call unproduce_glyphs like display_line and display_string
11975 do. */
11976 it.paragraph_embedding = L2R;
11978 if (f->n_tool_bar_rows == 0)
11980 int nlines;
11982 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11983 nlines != WINDOW_TOTAL_LINES (w)))
11985 Lisp_Object frame;
11986 int old_height = WINDOW_TOTAL_LINES (w);
11988 XSETFRAME (frame, f);
11989 Fmodify_frame_parameters (frame,
11990 list1 (Fcons (Qtool_bar_lines,
11991 make_number (nlines))));
11992 if (WINDOW_TOTAL_LINES (w) != old_height)
11994 clear_glyph_matrix (w->desired_matrix);
11995 fonts_changed_p = 1;
11996 return 1;
12001 /* Display as many lines as needed to display all tool-bar items. */
12003 if (f->n_tool_bar_rows > 0)
12005 int border, rows, height, extra;
12007 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12008 border = XINT (Vtool_bar_border);
12009 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12010 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12011 else if (EQ (Vtool_bar_border, Qborder_width))
12012 border = f->border_width;
12013 else
12014 border = 0;
12015 if (border < 0)
12016 border = 0;
12018 rows = f->n_tool_bar_rows;
12019 height = max (1, (it.last_visible_y - border) / rows);
12020 extra = it.last_visible_y - border - height * rows;
12022 while (it.current_y < it.last_visible_y)
12024 int h = 0;
12025 if (extra > 0 && rows-- > 0)
12027 h = (extra + rows - 1) / rows;
12028 extra -= h;
12030 display_tool_bar_line (&it, height + h);
12033 else
12035 while (it.current_y < it.last_visible_y)
12036 display_tool_bar_line (&it, 0);
12039 /* It doesn't make much sense to try scrolling in the tool-bar
12040 window, so don't do it. */
12041 w->desired_matrix->no_scrolling_p = 1;
12042 w->must_be_updated_p = 1;
12044 if (!NILP (Vauto_resize_tool_bars))
12046 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12047 int change_height_p = 0;
12049 /* If we couldn't display everything, change the tool-bar's
12050 height if there is room for more. */
12051 if (IT_STRING_CHARPOS (it) < it.end_charpos
12052 && it.current_y < max_tool_bar_height)
12053 change_height_p = 1;
12055 row = it.glyph_row - 1;
12057 /* If there are blank lines at the end, except for a partially
12058 visible blank line at the end that is smaller than
12059 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12060 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12061 && row->height >= FRAME_LINE_HEIGHT (f))
12062 change_height_p = 1;
12064 /* If row displays tool-bar items, but is partially visible,
12065 change the tool-bar's height. */
12066 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12067 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12068 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12069 change_height_p = 1;
12071 /* Resize windows as needed by changing the `tool-bar-lines'
12072 frame parameter. */
12073 if (change_height_p)
12075 Lisp_Object frame;
12076 int old_height = WINDOW_TOTAL_LINES (w);
12077 int nrows;
12078 int nlines = tool_bar_lines_needed (f, &nrows);
12080 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12081 && !f->minimize_tool_bar_window_p)
12082 ? (nlines > old_height)
12083 : (nlines != old_height));
12084 f->minimize_tool_bar_window_p = 0;
12086 if (change_height_p)
12088 XSETFRAME (frame, f);
12089 Fmodify_frame_parameters (frame,
12090 list1 (Fcons (Qtool_bar_lines,
12091 make_number (nlines))));
12092 if (WINDOW_TOTAL_LINES (w) != old_height)
12094 clear_glyph_matrix (w->desired_matrix);
12095 f->n_tool_bar_rows = nrows;
12096 fonts_changed_p = 1;
12097 return 1;
12103 f->minimize_tool_bar_window_p = 0;
12104 return 0;
12108 /* Get information about the tool-bar item which is displayed in GLYPH
12109 on frame F. Return in *PROP_IDX the index where tool-bar item
12110 properties start in F->tool_bar_items. Value is zero if
12111 GLYPH doesn't display a tool-bar item. */
12113 static int
12114 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12116 Lisp_Object prop;
12117 int success_p;
12118 int charpos;
12120 /* This function can be called asynchronously, which means we must
12121 exclude any possibility that Fget_text_property signals an
12122 error. */
12123 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12124 charpos = max (0, charpos);
12126 /* Get the text property `menu-item' at pos. The value of that
12127 property is the start index of this item's properties in
12128 F->tool_bar_items. */
12129 prop = Fget_text_property (make_number (charpos),
12130 Qmenu_item, f->current_tool_bar_string);
12131 if (INTEGERP (prop))
12133 *prop_idx = XINT (prop);
12134 success_p = 1;
12136 else
12137 success_p = 0;
12139 return success_p;
12143 /* Get information about the tool-bar item at position X/Y on frame F.
12144 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12145 the current matrix of the tool-bar window of F, or NULL if not
12146 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12147 item in F->tool_bar_items. Value is
12149 -1 if X/Y is not on a tool-bar item
12150 0 if X/Y is on the same item that was highlighted before.
12151 1 otherwise. */
12153 static int
12154 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12155 int *hpos, int *vpos, int *prop_idx)
12157 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12158 struct window *w = XWINDOW (f->tool_bar_window);
12159 int area;
12161 /* Find the glyph under X/Y. */
12162 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12163 if (*glyph == NULL)
12164 return -1;
12166 /* Get the start of this tool-bar item's properties in
12167 f->tool_bar_items. */
12168 if (!tool_bar_item_info (f, *glyph, prop_idx))
12169 return -1;
12171 /* Is mouse on the highlighted item? */
12172 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12173 && *vpos >= hlinfo->mouse_face_beg_row
12174 && *vpos <= hlinfo->mouse_face_end_row
12175 && (*vpos > hlinfo->mouse_face_beg_row
12176 || *hpos >= hlinfo->mouse_face_beg_col)
12177 && (*vpos < hlinfo->mouse_face_end_row
12178 || *hpos < hlinfo->mouse_face_end_col
12179 || hlinfo->mouse_face_past_end))
12180 return 0;
12182 return 1;
12186 /* EXPORT:
12187 Handle mouse button event on the tool-bar of frame F, at
12188 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12189 0 for button release. MODIFIERS is event modifiers for button
12190 release. */
12192 void
12193 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12194 int modifiers)
12196 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12197 struct window *w = XWINDOW (f->tool_bar_window);
12198 int hpos, vpos, prop_idx;
12199 struct glyph *glyph;
12200 Lisp_Object enabled_p;
12201 int ts;
12203 /* If not on the highlighted tool-bar item, and mouse-highlight is
12204 non-nil, return. This is so we generate the tool-bar button
12205 click only when the mouse button is released on the same item as
12206 where it was pressed. However, when mouse-highlight is disabled,
12207 generate the click when the button is released regardless of the
12208 highlight, since tool-bar items are not highlighted in that
12209 case. */
12210 frame_to_window_pixel_xy (w, &x, &y);
12211 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12212 if (ts == -1
12213 || (ts != 0 && !NILP (Vmouse_highlight)))
12214 return;
12216 /* When mouse-highlight is off, generate the click for the item
12217 where the button was pressed, disregarding where it was
12218 released. */
12219 if (NILP (Vmouse_highlight) && !down_p)
12220 prop_idx = last_tool_bar_item;
12222 /* If item is disabled, do nothing. */
12223 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12224 if (NILP (enabled_p))
12225 return;
12227 if (down_p)
12229 /* Show item in pressed state. */
12230 if (!NILP (Vmouse_highlight))
12231 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12232 last_tool_bar_item = prop_idx;
12234 else
12236 Lisp_Object key, frame;
12237 struct input_event event;
12238 EVENT_INIT (event);
12240 /* Show item in released state. */
12241 if (!NILP (Vmouse_highlight))
12242 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12244 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12246 XSETFRAME (frame, f);
12247 event.kind = TOOL_BAR_EVENT;
12248 event.frame_or_window = frame;
12249 event.arg = frame;
12250 kbd_buffer_store_event (&event);
12252 event.kind = TOOL_BAR_EVENT;
12253 event.frame_or_window = frame;
12254 event.arg = key;
12255 event.modifiers = modifiers;
12256 kbd_buffer_store_event (&event);
12257 last_tool_bar_item = -1;
12262 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12263 tool-bar window-relative coordinates X/Y. Called from
12264 note_mouse_highlight. */
12266 static void
12267 note_tool_bar_highlight (struct frame *f, int x, int y)
12269 Lisp_Object window = f->tool_bar_window;
12270 struct window *w = XWINDOW (window);
12271 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12272 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12273 int hpos, vpos;
12274 struct glyph *glyph;
12275 struct glyph_row *row;
12276 int i;
12277 Lisp_Object enabled_p;
12278 int prop_idx;
12279 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12280 int mouse_down_p, rc;
12282 /* Function note_mouse_highlight is called with negative X/Y
12283 values when mouse moves outside of the frame. */
12284 if (x <= 0 || y <= 0)
12286 clear_mouse_face (hlinfo);
12287 return;
12290 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12291 if (rc < 0)
12293 /* Not on tool-bar item. */
12294 clear_mouse_face (hlinfo);
12295 return;
12297 else if (rc == 0)
12298 /* On same tool-bar item as before. */
12299 goto set_help_echo;
12301 clear_mouse_face (hlinfo);
12303 /* Mouse is down, but on different tool-bar item? */
12304 mouse_down_p = (dpyinfo->grabbed
12305 && f == last_mouse_frame
12306 && FRAME_LIVE_P (f));
12307 if (mouse_down_p
12308 && last_tool_bar_item != prop_idx)
12309 return;
12311 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12313 /* If tool-bar item is not enabled, don't highlight it. */
12314 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12315 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12317 /* Compute the x-position of the glyph. In front and past the
12318 image is a space. We include this in the highlighted area. */
12319 row = MATRIX_ROW (w->current_matrix, vpos);
12320 for (i = x = 0; i < hpos; ++i)
12321 x += row->glyphs[TEXT_AREA][i].pixel_width;
12323 /* Record this as the current active region. */
12324 hlinfo->mouse_face_beg_col = hpos;
12325 hlinfo->mouse_face_beg_row = vpos;
12326 hlinfo->mouse_face_beg_x = x;
12327 hlinfo->mouse_face_beg_y = row->y;
12328 hlinfo->mouse_face_past_end = 0;
12330 hlinfo->mouse_face_end_col = hpos + 1;
12331 hlinfo->mouse_face_end_row = vpos;
12332 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12333 hlinfo->mouse_face_end_y = row->y;
12334 hlinfo->mouse_face_window = window;
12335 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12337 /* Display it as active. */
12338 show_mouse_face (hlinfo, draw);
12341 set_help_echo:
12343 /* Set help_echo_string to a help string to display for this tool-bar item.
12344 XTread_socket does the rest. */
12345 help_echo_object = help_echo_window = Qnil;
12346 help_echo_pos = -1;
12347 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12348 if (NILP (help_echo_string))
12349 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12352 #endif /* HAVE_WINDOW_SYSTEM */
12356 /************************************************************************
12357 Horizontal scrolling
12358 ************************************************************************/
12360 static int hscroll_window_tree (Lisp_Object);
12361 static int hscroll_windows (Lisp_Object);
12363 /* For all leaf windows in the window tree rooted at WINDOW, set their
12364 hscroll value so that PT is (i) visible in the window, and (ii) so
12365 that it is not within a certain margin at the window's left and
12366 right border. Value is non-zero if any window's hscroll has been
12367 changed. */
12369 static int
12370 hscroll_window_tree (Lisp_Object window)
12372 int hscrolled_p = 0;
12373 int hscroll_relative_p = FLOATP (Vhscroll_step);
12374 int hscroll_step_abs = 0;
12375 double hscroll_step_rel = 0;
12377 if (hscroll_relative_p)
12379 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12380 if (hscroll_step_rel < 0)
12382 hscroll_relative_p = 0;
12383 hscroll_step_abs = 0;
12386 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12388 hscroll_step_abs = XINT (Vhscroll_step);
12389 if (hscroll_step_abs < 0)
12390 hscroll_step_abs = 0;
12392 else
12393 hscroll_step_abs = 0;
12395 while (WINDOWP (window))
12397 struct window *w = XWINDOW (window);
12399 if (WINDOWP (w->contents))
12400 hscrolled_p |= hscroll_window_tree (w->contents);
12401 else if (w->cursor.vpos >= 0)
12403 int h_margin;
12404 int text_area_width;
12405 struct glyph_row *current_cursor_row
12406 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12407 struct glyph_row *desired_cursor_row
12408 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12409 struct glyph_row *cursor_row
12410 = (desired_cursor_row->enabled_p
12411 ? desired_cursor_row
12412 : current_cursor_row);
12413 int row_r2l_p = cursor_row->reversed_p;
12415 text_area_width = window_box_width (w, TEXT_AREA);
12417 /* Scroll when cursor is inside this scroll margin. */
12418 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12420 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12421 /* For left-to-right rows, hscroll when cursor is either
12422 (i) inside the right hscroll margin, or (ii) if it is
12423 inside the left margin and the window is already
12424 hscrolled. */
12425 && ((!row_r2l_p
12426 && ((w->hscroll
12427 && w->cursor.x <= h_margin)
12428 || (cursor_row->enabled_p
12429 && cursor_row->truncated_on_right_p
12430 && (w->cursor.x >= text_area_width - h_margin))))
12431 /* For right-to-left rows, the logic is similar,
12432 except that rules for scrolling to left and right
12433 are reversed. E.g., if cursor.x <= h_margin, we
12434 need to hscroll "to the right" unconditionally,
12435 and that will scroll the screen to the left so as
12436 to reveal the next portion of the row. */
12437 || (row_r2l_p
12438 && ((cursor_row->enabled_p
12439 /* FIXME: It is confusing to set the
12440 truncated_on_right_p flag when R2L rows
12441 are actually truncated on the left. */
12442 && cursor_row->truncated_on_right_p
12443 && w->cursor.x <= h_margin)
12444 || (w->hscroll
12445 && (w->cursor.x >= text_area_width - h_margin))))))
12447 struct it it;
12448 ptrdiff_t hscroll;
12449 struct buffer *saved_current_buffer;
12450 ptrdiff_t pt;
12451 int wanted_x;
12453 /* Find point in a display of infinite width. */
12454 saved_current_buffer = current_buffer;
12455 current_buffer = XBUFFER (w->contents);
12457 if (w == XWINDOW (selected_window))
12458 pt = PT;
12459 else
12460 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12462 /* Move iterator to pt starting at cursor_row->start in
12463 a line with infinite width. */
12464 init_to_row_start (&it, w, cursor_row);
12465 it.last_visible_x = INFINITY;
12466 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12467 current_buffer = saved_current_buffer;
12469 /* Position cursor in window. */
12470 if (!hscroll_relative_p && hscroll_step_abs == 0)
12471 hscroll = max (0, (it.current_x
12472 - (ITERATOR_AT_END_OF_LINE_P (&it)
12473 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12474 : (text_area_width / 2))))
12475 / FRAME_COLUMN_WIDTH (it.f);
12476 else if ((!row_r2l_p
12477 && w->cursor.x >= text_area_width - h_margin)
12478 || (row_r2l_p && w->cursor.x <= h_margin))
12480 if (hscroll_relative_p)
12481 wanted_x = text_area_width * (1 - hscroll_step_rel)
12482 - h_margin;
12483 else
12484 wanted_x = text_area_width
12485 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12486 - h_margin;
12487 hscroll
12488 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12490 else
12492 if (hscroll_relative_p)
12493 wanted_x = text_area_width * hscroll_step_rel
12494 + h_margin;
12495 else
12496 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12497 + h_margin;
12498 hscroll
12499 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12501 hscroll = max (hscroll, w->min_hscroll);
12503 /* Don't prevent redisplay optimizations if hscroll
12504 hasn't changed, as it will unnecessarily slow down
12505 redisplay. */
12506 if (w->hscroll != hscroll)
12508 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12509 w->hscroll = hscroll;
12510 hscrolled_p = 1;
12515 window = w->next;
12518 /* Value is non-zero if hscroll of any leaf window has been changed. */
12519 return hscrolled_p;
12523 /* Set hscroll so that cursor is visible and not inside horizontal
12524 scroll margins for all windows in the tree rooted at WINDOW. See
12525 also hscroll_window_tree above. Value is non-zero if any window's
12526 hscroll has been changed. If it has, desired matrices on the frame
12527 of WINDOW are cleared. */
12529 static int
12530 hscroll_windows (Lisp_Object window)
12532 int hscrolled_p = hscroll_window_tree (window);
12533 if (hscrolled_p)
12534 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12535 return hscrolled_p;
12540 /************************************************************************
12541 Redisplay
12542 ************************************************************************/
12544 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12545 to a non-zero value. This is sometimes handy to have in a debugger
12546 session. */
12548 #ifdef GLYPH_DEBUG
12550 /* First and last unchanged row for try_window_id. */
12552 static int debug_first_unchanged_at_end_vpos;
12553 static int debug_last_unchanged_at_beg_vpos;
12555 /* Delta vpos and y. */
12557 static int debug_dvpos, debug_dy;
12559 /* Delta in characters and bytes for try_window_id. */
12561 static ptrdiff_t debug_delta, debug_delta_bytes;
12563 /* Values of window_end_pos and window_end_vpos at the end of
12564 try_window_id. */
12566 static ptrdiff_t debug_end_vpos;
12568 /* Append a string to W->desired_matrix->method. FMT is a printf
12569 format string. If trace_redisplay_p is non-zero also printf the
12570 resulting string to stderr. */
12572 static void debug_method_add (struct window *, char const *, ...)
12573 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12575 static void
12576 debug_method_add (struct window *w, char const *fmt, ...)
12578 void *ptr = w;
12579 char *method = w->desired_matrix->method;
12580 int len = strlen (method);
12581 int size = sizeof w->desired_matrix->method;
12582 int remaining = size - len - 1;
12583 va_list ap;
12585 if (len && remaining)
12587 method[len] = '|';
12588 --remaining, ++len;
12591 va_start (ap, fmt);
12592 vsnprintf (method + len, remaining + 1, fmt, ap);
12593 va_end (ap);
12595 if (trace_redisplay_p)
12596 fprintf (stderr, "%p (%s): %s\n",
12597 ptr,
12598 ((BUFFERP (w->contents)
12599 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12600 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12601 : "no buffer"),
12602 method + len);
12605 #endif /* GLYPH_DEBUG */
12608 /* Value is non-zero if all changes in window W, which displays
12609 current_buffer, are in the text between START and END. START is a
12610 buffer position, END is given as a distance from Z. Used in
12611 redisplay_internal for display optimization. */
12613 static int
12614 text_outside_line_unchanged_p (struct window *w,
12615 ptrdiff_t start, ptrdiff_t end)
12617 int unchanged_p = 1;
12619 /* If text or overlays have changed, see where. */
12620 if (window_outdated (w))
12622 /* Gap in the line? */
12623 if (GPT < start || Z - GPT < end)
12624 unchanged_p = 0;
12626 /* Changes start in front of the line, or end after it? */
12627 if (unchanged_p
12628 && (BEG_UNCHANGED < start - 1
12629 || END_UNCHANGED < end))
12630 unchanged_p = 0;
12632 /* If selective display, can't optimize if changes start at the
12633 beginning of the line. */
12634 if (unchanged_p
12635 && INTEGERP (BVAR (current_buffer, selective_display))
12636 && XINT (BVAR (current_buffer, selective_display)) > 0
12637 && (BEG_UNCHANGED < start || GPT <= start))
12638 unchanged_p = 0;
12640 /* If there are overlays at the start or end of the line, these
12641 may have overlay strings with newlines in them. A change at
12642 START, for instance, may actually concern the display of such
12643 overlay strings as well, and they are displayed on different
12644 lines. So, quickly rule out this case. (For the future, it
12645 might be desirable to implement something more telling than
12646 just BEG/END_UNCHANGED.) */
12647 if (unchanged_p)
12649 if (BEG + BEG_UNCHANGED == start
12650 && overlay_touches_p (start))
12651 unchanged_p = 0;
12652 if (END_UNCHANGED == end
12653 && overlay_touches_p (Z - end))
12654 unchanged_p = 0;
12657 /* Under bidi reordering, adding or deleting a character in the
12658 beginning of a paragraph, before the first strong directional
12659 character, can change the base direction of the paragraph (unless
12660 the buffer specifies a fixed paragraph direction), which will
12661 require to redisplay the whole paragraph. It might be worthwhile
12662 to find the paragraph limits and widen the range of redisplayed
12663 lines to that, but for now just give up this optimization. */
12664 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12665 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12666 unchanged_p = 0;
12669 return unchanged_p;
12673 /* Do a frame update, taking possible shortcuts into account. This is
12674 the main external entry point for redisplay.
12676 If the last redisplay displayed an echo area message and that message
12677 is no longer requested, we clear the echo area or bring back the
12678 mini-buffer if that is in use. */
12680 void
12681 redisplay (void)
12683 redisplay_internal ();
12687 static Lisp_Object
12688 overlay_arrow_string_or_property (Lisp_Object var)
12690 Lisp_Object val;
12692 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12693 return val;
12695 return Voverlay_arrow_string;
12698 /* Return 1 if there are any overlay-arrows in current_buffer. */
12699 static int
12700 overlay_arrow_in_current_buffer_p (void)
12702 Lisp_Object vlist;
12704 for (vlist = Voverlay_arrow_variable_list;
12705 CONSP (vlist);
12706 vlist = XCDR (vlist))
12708 Lisp_Object var = XCAR (vlist);
12709 Lisp_Object val;
12711 if (!SYMBOLP (var))
12712 continue;
12713 val = find_symbol_value (var);
12714 if (MARKERP (val)
12715 && current_buffer == XMARKER (val)->buffer)
12716 return 1;
12718 return 0;
12722 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12723 has changed. */
12725 static int
12726 overlay_arrows_changed_p (void)
12728 Lisp_Object vlist;
12730 for (vlist = Voverlay_arrow_variable_list;
12731 CONSP (vlist);
12732 vlist = XCDR (vlist))
12734 Lisp_Object var = XCAR (vlist);
12735 Lisp_Object val, pstr;
12737 if (!SYMBOLP (var))
12738 continue;
12739 val = find_symbol_value (var);
12740 if (!MARKERP (val))
12741 continue;
12742 if (! EQ (COERCE_MARKER (val),
12743 Fget (var, Qlast_arrow_position))
12744 || ! (pstr = overlay_arrow_string_or_property (var),
12745 EQ (pstr, Fget (var, Qlast_arrow_string))))
12746 return 1;
12748 return 0;
12751 /* Mark overlay arrows to be updated on next redisplay. */
12753 static void
12754 update_overlay_arrows (int up_to_date)
12756 Lisp_Object vlist;
12758 for (vlist = Voverlay_arrow_variable_list;
12759 CONSP (vlist);
12760 vlist = XCDR (vlist))
12762 Lisp_Object var = XCAR (vlist);
12764 if (!SYMBOLP (var))
12765 continue;
12767 if (up_to_date > 0)
12769 Lisp_Object val = find_symbol_value (var);
12770 Fput (var, Qlast_arrow_position,
12771 COERCE_MARKER (val));
12772 Fput (var, Qlast_arrow_string,
12773 overlay_arrow_string_or_property (var));
12775 else if (up_to_date < 0
12776 || !NILP (Fget (var, Qlast_arrow_position)))
12778 Fput (var, Qlast_arrow_position, Qt);
12779 Fput (var, Qlast_arrow_string, Qt);
12785 /* Return overlay arrow string to display at row.
12786 Return integer (bitmap number) for arrow bitmap in left fringe.
12787 Return nil if no overlay arrow. */
12789 static Lisp_Object
12790 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12792 Lisp_Object vlist;
12794 for (vlist = Voverlay_arrow_variable_list;
12795 CONSP (vlist);
12796 vlist = XCDR (vlist))
12798 Lisp_Object var = XCAR (vlist);
12799 Lisp_Object val;
12801 if (!SYMBOLP (var))
12802 continue;
12804 val = find_symbol_value (var);
12806 if (MARKERP (val)
12807 && current_buffer == XMARKER (val)->buffer
12808 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12810 if (FRAME_WINDOW_P (it->f)
12811 /* FIXME: if ROW->reversed_p is set, this should test
12812 the right fringe, not the left one. */
12813 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12815 #ifdef HAVE_WINDOW_SYSTEM
12816 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12818 int fringe_bitmap;
12819 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12820 return make_number (fringe_bitmap);
12822 #endif
12823 return make_number (-1); /* Use default arrow bitmap. */
12825 return overlay_arrow_string_or_property (var);
12829 return Qnil;
12832 /* Return 1 if point moved out of or into a composition. Otherwise
12833 return 0. PREV_BUF and PREV_PT are the last point buffer and
12834 position. BUF and PT are the current point buffer and position. */
12836 static int
12837 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12838 struct buffer *buf, ptrdiff_t pt)
12840 ptrdiff_t start, end;
12841 Lisp_Object prop;
12842 Lisp_Object buffer;
12844 XSETBUFFER (buffer, buf);
12845 /* Check a composition at the last point if point moved within the
12846 same buffer. */
12847 if (prev_buf == buf)
12849 if (prev_pt == pt)
12850 /* Point didn't move. */
12851 return 0;
12853 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12854 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12855 && COMPOSITION_VALID_P (start, end, prop)
12856 && start < prev_pt && end > prev_pt)
12857 /* The last point was within the composition. Return 1 iff
12858 point moved out of the composition. */
12859 return (pt <= start || pt >= end);
12862 /* Check a composition at the current point. */
12863 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12864 && find_composition (pt, -1, &start, &end, &prop, buffer)
12865 && COMPOSITION_VALID_P (start, end, prop)
12866 && start < pt && end > pt);
12870 /* Reconsider the setting of B->clip_changed which is displayed
12871 in window W. */
12873 static void
12874 reconsider_clip_changes (struct window *w, struct buffer *b)
12876 if (b->clip_changed
12877 && w->window_end_valid
12878 && w->current_matrix->buffer == b
12879 && w->current_matrix->zv == BUF_ZV (b)
12880 && w->current_matrix->begv == BUF_BEGV (b))
12881 b->clip_changed = 0;
12883 /* If display wasn't paused, and W is not a tool bar window, see if
12884 point has been moved into or out of a composition. In that case,
12885 we set b->clip_changed to 1 to force updating the screen. If
12886 b->clip_changed has already been set to 1, we can skip this
12887 check. */
12888 if (!b->clip_changed && BUFFERP (w->contents) && w->window_end_valid)
12890 ptrdiff_t pt;
12892 if (w == XWINDOW (selected_window))
12893 pt = PT;
12894 else
12895 pt = marker_position (w->pointm);
12897 if ((w->current_matrix->buffer != XBUFFER (w->contents)
12898 || pt != w->last_point)
12899 && check_point_in_composition (w->current_matrix->buffer,
12900 w->last_point,
12901 XBUFFER (w->contents), pt))
12902 b->clip_changed = 1;
12907 #define STOP_POLLING \
12908 do { if (! polling_stopped_here) stop_polling (); \
12909 polling_stopped_here = 1; } while (0)
12911 #define RESUME_POLLING \
12912 do { if (polling_stopped_here) start_polling (); \
12913 polling_stopped_here = 0; } while (0)
12916 /* Perhaps in the future avoid recentering windows if it
12917 is not necessary; currently that causes some problems. */
12919 static void
12920 redisplay_internal (void)
12922 struct window *w = XWINDOW (selected_window);
12923 struct window *sw;
12924 struct frame *fr;
12925 int pending;
12926 int must_finish = 0;
12927 struct text_pos tlbufpos, tlendpos;
12928 int number_of_visible_frames;
12929 ptrdiff_t count, count1;
12930 struct frame *sf;
12931 int polling_stopped_here = 0;
12932 Lisp_Object tail, frame;
12934 /* Non-zero means redisplay has to consider all windows on all
12935 frames. Zero means, only selected_window is considered. */
12936 int consider_all_windows_p;
12938 /* Non-zero means redisplay has to redisplay the miniwindow. */
12939 int update_miniwindow_p = 0;
12941 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12943 /* No redisplay if running in batch mode or frame is not yet fully
12944 initialized, or redisplay is explicitly turned off by setting
12945 Vinhibit_redisplay. */
12946 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12947 || !NILP (Vinhibit_redisplay))
12948 return;
12950 /* Don't examine these until after testing Vinhibit_redisplay.
12951 When Emacs is shutting down, perhaps because its connection to
12952 X has dropped, we should not look at them at all. */
12953 fr = XFRAME (w->frame);
12954 sf = SELECTED_FRAME ();
12956 if (!fr->glyphs_initialized_p)
12957 return;
12959 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12960 if (popup_activated ())
12961 return;
12962 #endif
12964 /* I don't think this happens but let's be paranoid. */
12965 if (redisplaying_p)
12966 return;
12968 /* Record a function that clears redisplaying_p
12969 when we leave this function. */
12970 count = SPECPDL_INDEX ();
12971 record_unwind_protect_void (unwind_redisplay);
12972 redisplaying_p = 1;
12973 specbind (Qinhibit_free_realized_faces, Qnil);
12975 /* Record this function, so it appears on the profiler's backtraces. */
12976 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12978 FOR_EACH_FRAME (tail, frame)
12979 XFRAME (frame)->already_hscrolled_p = 0;
12981 retry:
12982 /* Remember the currently selected window. */
12983 sw = w;
12985 pending = 0;
12986 reconsider_clip_changes (w, current_buffer);
12987 last_escape_glyph_frame = NULL;
12988 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12989 last_glyphless_glyph_frame = NULL;
12990 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12992 /* If new fonts have been loaded that make a glyph matrix adjustment
12993 necessary, do it. */
12994 if (fonts_changed_p)
12996 adjust_glyphs (NULL);
12997 ++windows_or_buffers_changed;
12998 fonts_changed_p = 0;
13001 /* If face_change_count is non-zero, init_iterator will free all
13002 realized faces, which includes the faces referenced from current
13003 matrices. So, we can't reuse current matrices in this case. */
13004 if (face_change_count)
13005 ++windows_or_buffers_changed;
13007 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13008 && FRAME_TTY (sf)->previous_frame != sf)
13010 /* Since frames on a single ASCII terminal share the same
13011 display area, displaying a different frame means redisplay
13012 the whole thing. */
13013 windows_or_buffers_changed++;
13014 SET_FRAME_GARBAGED (sf);
13015 #ifndef DOS_NT
13016 set_tty_color_mode (FRAME_TTY (sf), sf);
13017 #endif
13018 FRAME_TTY (sf)->previous_frame = sf;
13021 /* Set the visible flags for all frames. Do this before checking for
13022 resized or garbaged frames; they want to know if their frames are
13023 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13024 number_of_visible_frames = 0;
13026 FOR_EACH_FRAME (tail, frame)
13028 struct frame *f = XFRAME (frame);
13030 if (FRAME_VISIBLE_P (f))
13031 ++number_of_visible_frames;
13032 clear_desired_matrices (f);
13035 /* Notice any pending interrupt request to change frame size. */
13036 do_pending_window_change (1);
13038 /* do_pending_window_change could change the selected_window due to
13039 frame resizing which makes the selected window too small. */
13040 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13042 sw = w;
13043 reconsider_clip_changes (w, current_buffer);
13046 /* Clear frames marked as garbaged. */
13047 clear_garbaged_frames ();
13049 /* Build menubar and tool-bar items. */
13050 if (NILP (Vmemory_full))
13051 prepare_menu_bars ();
13053 if (windows_or_buffers_changed)
13054 update_mode_lines++;
13056 /* Detect case that we need to write or remove a star in the mode line. */
13057 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13059 w->update_mode_line = 1;
13060 if (buffer_shared_and_changed ())
13061 update_mode_lines++;
13064 /* Avoid invocation of point motion hooks by `current_column' below. */
13065 count1 = SPECPDL_INDEX ();
13066 specbind (Qinhibit_point_motion_hooks, Qt);
13068 if (mode_line_update_needed (w))
13069 w->update_mode_line = 1;
13071 unbind_to (count1, Qnil);
13073 consider_all_windows_p = (update_mode_lines
13074 || buffer_shared_and_changed ()
13075 || cursor_type_changed);
13077 /* If specs for an arrow have changed, do thorough redisplay
13078 to ensure we remove any arrow that should no longer exist. */
13079 if (overlay_arrows_changed_p ())
13080 consider_all_windows_p = windows_or_buffers_changed = 1;
13082 /* Normally the message* functions will have already displayed and
13083 updated the echo area, but the frame may have been trashed, or
13084 the update may have been preempted, so display the echo area
13085 again here. Checking message_cleared_p captures the case that
13086 the echo area should be cleared. */
13087 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13088 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13089 || (message_cleared_p
13090 && minibuf_level == 0
13091 /* If the mini-window is currently selected, this means the
13092 echo-area doesn't show through. */
13093 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13095 int window_height_changed_p = echo_area_display (0);
13097 if (message_cleared_p)
13098 update_miniwindow_p = 1;
13100 must_finish = 1;
13102 /* If we don't display the current message, don't clear the
13103 message_cleared_p flag, because, if we did, we wouldn't clear
13104 the echo area in the next redisplay which doesn't preserve
13105 the echo area. */
13106 if (!display_last_displayed_message_p)
13107 message_cleared_p = 0;
13109 if (fonts_changed_p)
13110 goto retry;
13111 else if (window_height_changed_p)
13113 consider_all_windows_p = 1;
13114 ++update_mode_lines;
13115 ++windows_or_buffers_changed;
13117 /* If window configuration was changed, frames may have been
13118 marked garbaged. Clear them or we will experience
13119 surprises wrt scrolling. */
13120 clear_garbaged_frames ();
13123 else if (EQ (selected_window, minibuf_window)
13124 && (current_buffer->clip_changed || window_outdated (w))
13125 && resize_mini_window (w, 0))
13127 /* Resized active mini-window to fit the size of what it is
13128 showing if its contents might have changed. */
13129 must_finish = 1;
13130 /* FIXME: this causes all frames to be updated, which seems unnecessary
13131 since only the current frame needs to be considered. This function
13132 needs to be rewritten with two variables, consider_all_windows and
13133 consider_all_frames. */
13134 consider_all_windows_p = 1;
13135 ++windows_or_buffers_changed;
13136 ++update_mode_lines;
13138 /* If window configuration was changed, frames may have been
13139 marked garbaged. Clear them or we will experience
13140 surprises wrt scrolling. */
13141 clear_garbaged_frames ();
13144 /* If showing the region, and mark has changed, we must redisplay
13145 the whole window. The assignment to this_line_start_pos prevents
13146 the optimization directly below this if-statement. */
13147 if (((!NILP (Vtransient_mark_mode)
13148 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13149 != (w->region_showing > 0))
13150 || (w->region_showing
13151 && w->region_showing
13152 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13153 CHARPOS (this_line_start_pos) = 0;
13155 /* Optimize the case that only the line containing the cursor in the
13156 selected window has changed. Variables starting with this_ are
13157 set in display_line and record information about the line
13158 containing the cursor. */
13159 tlbufpos = this_line_start_pos;
13160 tlendpos = this_line_end_pos;
13161 if (!consider_all_windows_p
13162 && CHARPOS (tlbufpos) > 0
13163 && !w->update_mode_line
13164 && !current_buffer->clip_changed
13165 && !current_buffer->prevent_redisplay_optimizations_p
13166 && FRAME_VISIBLE_P (XFRAME (w->frame))
13167 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13168 /* Make sure recorded data applies to current buffer, etc. */
13169 && this_line_buffer == current_buffer
13170 && current_buffer == XBUFFER (w->contents)
13171 && !w->force_start
13172 && !w->optional_new_start
13173 /* Point must be on the line that we have info recorded about. */
13174 && PT >= CHARPOS (tlbufpos)
13175 && PT <= Z - CHARPOS (tlendpos)
13176 /* All text outside that line, including its final newline,
13177 must be unchanged. */
13178 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13179 CHARPOS (tlendpos)))
13181 if (CHARPOS (tlbufpos) > BEGV
13182 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13183 && (CHARPOS (tlbufpos) == ZV
13184 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13185 /* Former continuation line has disappeared by becoming empty. */
13186 goto cancel;
13187 else if (window_outdated (w) || MINI_WINDOW_P (w))
13189 /* We have to handle the case of continuation around a
13190 wide-column character (see the comment in indent.c around
13191 line 1340).
13193 For instance, in the following case:
13195 -------- Insert --------
13196 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13197 J_I_ ==> J_I_ `^^' are cursors.
13198 ^^ ^^
13199 -------- --------
13201 As we have to redraw the line above, we cannot use this
13202 optimization. */
13204 struct it it;
13205 int line_height_before = this_line_pixel_height;
13207 /* Note that start_display will handle the case that the
13208 line starting at tlbufpos is a continuation line. */
13209 start_display (&it, w, tlbufpos);
13211 /* Implementation note: It this still necessary? */
13212 if (it.current_x != this_line_start_x)
13213 goto cancel;
13215 TRACE ((stderr, "trying display optimization 1\n"));
13216 w->cursor.vpos = -1;
13217 overlay_arrow_seen = 0;
13218 it.vpos = this_line_vpos;
13219 it.current_y = this_line_y;
13220 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13221 display_line (&it);
13223 /* If line contains point, is not continued,
13224 and ends at same distance from eob as before, we win. */
13225 if (w->cursor.vpos >= 0
13226 /* Line is not continued, otherwise this_line_start_pos
13227 would have been set to 0 in display_line. */
13228 && CHARPOS (this_line_start_pos)
13229 /* Line ends as before. */
13230 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13231 /* Line has same height as before. Otherwise other lines
13232 would have to be shifted up or down. */
13233 && this_line_pixel_height == line_height_before)
13235 /* If this is not the window's last line, we must adjust
13236 the charstarts of the lines below. */
13237 if (it.current_y < it.last_visible_y)
13239 struct glyph_row *row
13240 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13241 ptrdiff_t delta, delta_bytes;
13243 /* We used to distinguish between two cases here,
13244 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13245 when the line ends in a newline or the end of the
13246 buffer's accessible portion. But both cases did
13247 the same, so they were collapsed. */
13248 delta = (Z
13249 - CHARPOS (tlendpos)
13250 - MATRIX_ROW_START_CHARPOS (row));
13251 delta_bytes = (Z_BYTE
13252 - BYTEPOS (tlendpos)
13253 - MATRIX_ROW_START_BYTEPOS (row));
13255 increment_matrix_positions (w->current_matrix,
13256 this_line_vpos + 1,
13257 w->current_matrix->nrows,
13258 delta, delta_bytes);
13261 /* If this row displays text now but previously didn't,
13262 or vice versa, w->window_end_vpos may have to be
13263 adjusted. */
13264 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13266 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13267 wset_window_end_vpos (w, make_number (this_line_vpos));
13269 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13270 && this_line_vpos > 0)
13271 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13272 w->window_end_valid = 0;
13274 /* Update hint: No need to try to scroll in update_window. */
13275 w->desired_matrix->no_scrolling_p = 1;
13277 #ifdef GLYPH_DEBUG
13278 *w->desired_matrix->method = 0;
13279 debug_method_add (w, "optimization 1");
13280 #endif
13281 #ifdef HAVE_WINDOW_SYSTEM
13282 update_window_fringes (w, 0);
13283 #endif
13284 goto update;
13286 else
13287 goto cancel;
13289 else if (/* Cursor position hasn't changed. */
13290 PT == w->last_point
13291 /* Make sure the cursor was last displayed
13292 in this window. Otherwise we have to reposition it. */
13293 && 0 <= w->cursor.vpos
13294 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13296 if (!must_finish)
13298 do_pending_window_change (1);
13299 /* If selected_window changed, redisplay again. */
13300 if (WINDOWP (selected_window)
13301 && (w = XWINDOW (selected_window)) != sw)
13302 goto retry;
13304 /* We used to always goto end_of_redisplay here, but this
13305 isn't enough if we have a blinking cursor. */
13306 if (w->cursor_off_p == w->last_cursor_off_p)
13307 goto end_of_redisplay;
13309 goto update;
13311 /* If highlighting the region, or if the cursor is in the echo area,
13312 then we can't just move the cursor. */
13313 else if (! (!NILP (Vtransient_mark_mode)
13314 && !NILP (BVAR (current_buffer, mark_active)))
13315 && (EQ (selected_window,
13316 BVAR (current_buffer, last_selected_window))
13317 || highlight_nonselected_windows)
13318 && !w->region_showing
13319 && NILP (Vshow_trailing_whitespace)
13320 && !cursor_in_echo_area)
13322 struct it it;
13323 struct glyph_row *row;
13325 /* Skip from tlbufpos to PT and see where it is. Note that
13326 PT may be in invisible text. If so, we will end at the
13327 next visible position. */
13328 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13329 NULL, DEFAULT_FACE_ID);
13330 it.current_x = this_line_start_x;
13331 it.current_y = this_line_y;
13332 it.vpos = this_line_vpos;
13334 /* The call to move_it_to stops in front of PT, but
13335 moves over before-strings. */
13336 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13338 if (it.vpos == this_line_vpos
13339 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13340 row->enabled_p))
13342 eassert (this_line_vpos == it.vpos);
13343 eassert (this_line_y == it.current_y);
13344 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13345 #ifdef GLYPH_DEBUG
13346 *w->desired_matrix->method = 0;
13347 debug_method_add (w, "optimization 3");
13348 #endif
13349 goto update;
13351 else
13352 goto cancel;
13355 cancel:
13356 /* Text changed drastically or point moved off of line. */
13357 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13360 CHARPOS (this_line_start_pos) = 0;
13361 consider_all_windows_p |= buffer_shared_and_changed ();
13362 ++clear_face_cache_count;
13363 #ifdef HAVE_WINDOW_SYSTEM
13364 ++clear_image_cache_count;
13365 #endif
13367 /* Build desired matrices, and update the display. If
13368 consider_all_windows_p is non-zero, do it for all windows on all
13369 frames. Otherwise do it for selected_window, only. */
13371 if (consider_all_windows_p)
13373 FOR_EACH_FRAME (tail, frame)
13374 XFRAME (frame)->updated_p = 0;
13376 FOR_EACH_FRAME (tail, frame)
13378 struct frame *f = XFRAME (frame);
13380 /* We don't have to do anything for unselected terminal
13381 frames. */
13382 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13383 && !EQ (FRAME_TTY (f)->top_frame, frame))
13384 continue;
13386 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13388 /* Mark all the scroll bars to be removed; we'll redeem
13389 the ones we want when we redisplay their windows. */
13390 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13391 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13393 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13394 redisplay_windows (FRAME_ROOT_WINDOW (f));
13396 /* The X error handler may have deleted that frame. */
13397 if (!FRAME_LIVE_P (f))
13398 continue;
13400 /* Any scroll bars which redisplay_windows should have
13401 nuked should now go away. */
13402 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13403 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13405 /* If fonts changed, display again. */
13406 /* ??? rms: I suspect it is a mistake to jump all the way
13407 back to retry here. It should just retry this frame. */
13408 if (fonts_changed_p)
13409 goto retry;
13411 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13413 /* See if we have to hscroll. */
13414 if (!f->already_hscrolled_p)
13416 f->already_hscrolled_p = 1;
13417 if (hscroll_windows (f->root_window))
13418 goto retry;
13421 /* Prevent various kinds of signals during display
13422 update. stdio is not robust about handling
13423 signals, which can cause an apparent I/O
13424 error. */
13425 if (interrupt_input)
13426 unrequest_sigio ();
13427 STOP_POLLING;
13429 /* Update the display. */
13430 set_window_update_flags (XWINDOW (f->root_window), 1);
13431 pending |= update_frame (f, 0, 0);
13432 f->updated_p = 1;
13437 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13439 if (!pending)
13441 /* Do the mark_window_display_accurate after all windows have
13442 been redisplayed because this call resets flags in buffers
13443 which are needed for proper redisplay. */
13444 FOR_EACH_FRAME (tail, frame)
13446 struct frame *f = XFRAME (frame);
13447 if (f->updated_p)
13449 mark_window_display_accurate (f->root_window, 1);
13450 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13451 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13456 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13458 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13459 struct frame *mini_frame;
13461 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13462 /* Use list_of_error, not Qerror, so that
13463 we catch only errors and don't run the debugger. */
13464 internal_condition_case_1 (redisplay_window_1, selected_window,
13465 list_of_error,
13466 redisplay_window_error);
13467 if (update_miniwindow_p)
13468 internal_condition_case_1 (redisplay_window_1, mini_window,
13469 list_of_error,
13470 redisplay_window_error);
13472 /* Compare desired and current matrices, perform output. */
13474 update:
13475 /* If fonts changed, display again. */
13476 if (fonts_changed_p)
13477 goto retry;
13479 /* Prevent various kinds of signals during display update.
13480 stdio is not robust about handling signals,
13481 which can cause an apparent I/O error. */
13482 if (interrupt_input)
13483 unrequest_sigio ();
13484 STOP_POLLING;
13486 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13488 if (hscroll_windows (selected_window))
13489 goto retry;
13491 XWINDOW (selected_window)->must_be_updated_p = 1;
13492 pending = update_frame (sf, 0, 0);
13495 /* We may have called echo_area_display at the top of this
13496 function. If the echo area is on another frame, that may
13497 have put text on a frame other than the selected one, so the
13498 above call to update_frame would not have caught it. Catch
13499 it here. */
13500 mini_window = FRAME_MINIBUF_WINDOW (sf);
13501 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13503 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13505 XWINDOW (mini_window)->must_be_updated_p = 1;
13506 pending |= update_frame (mini_frame, 0, 0);
13507 if (!pending && hscroll_windows (mini_window))
13508 goto retry;
13512 /* If display was paused because of pending input, make sure we do a
13513 thorough update the next time. */
13514 if (pending)
13516 /* Prevent the optimization at the beginning of
13517 redisplay_internal that tries a single-line update of the
13518 line containing the cursor in the selected window. */
13519 CHARPOS (this_line_start_pos) = 0;
13521 /* Let the overlay arrow be updated the next time. */
13522 update_overlay_arrows (0);
13524 /* If we pause after scrolling, some rows in the current
13525 matrices of some windows are not valid. */
13526 if (!WINDOW_FULL_WIDTH_P (w)
13527 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13528 update_mode_lines = 1;
13530 else
13532 if (!consider_all_windows_p)
13534 /* This has already been done above if
13535 consider_all_windows_p is set. */
13536 mark_window_display_accurate_1 (w, 1);
13538 /* Say overlay arrows are up to date. */
13539 update_overlay_arrows (1);
13541 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13542 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13545 update_mode_lines = 0;
13546 windows_or_buffers_changed = 0;
13547 cursor_type_changed = 0;
13550 /* Start SIGIO interrupts coming again. Having them off during the
13551 code above makes it less likely one will discard output, but not
13552 impossible, since there might be stuff in the system buffer here.
13553 But it is much hairier to try to do anything about that. */
13554 if (interrupt_input)
13555 request_sigio ();
13556 RESUME_POLLING;
13558 /* If a frame has become visible which was not before, redisplay
13559 again, so that we display it. Expose events for such a frame
13560 (which it gets when becoming visible) don't call the parts of
13561 redisplay constructing glyphs, so simply exposing a frame won't
13562 display anything in this case. So, we have to display these
13563 frames here explicitly. */
13564 if (!pending)
13566 int new_count = 0;
13568 FOR_EACH_FRAME (tail, frame)
13570 int this_is_visible = 0;
13572 if (XFRAME (frame)->visible)
13573 this_is_visible = 1;
13575 if (this_is_visible)
13576 new_count++;
13579 if (new_count != number_of_visible_frames)
13580 windows_or_buffers_changed++;
13583 /* Change frame size now if a change is pending. */
13584 do_pending_window_change (1);
13586 /* If we just did a pending size change, or have additional
13587 visible frames, or selected_window changed, redisplay again. */
13588 if ((windows_or_buffers_changed && !pending)
13589 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13590 goto retry;
13592 /* Clear the face and image caches.
13594 We used to do this only if consider_all_windows_p. But the cache
13595 needs to be cleared if a timer creates images in the current
13596 buffer (e.g. the test case in Bug#6230). */
13598 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13600 clear_face_cache (0);
13601 clear_face_cache_count = 0;
13604 #ifdef HAVE_WINDOW_SYSTEM
13605 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13607 clear_image_caches (Qnil);
13608 clear_image_cache_count = 0;
13610 #endif /* HAVE_WINDOW_SYSTEM */
13612 end_of_redisplay:
13613 unbind_to (count, Qnil);
13614 RESUME_POLLING;
13618 /* Redisplay, but leave alone any recent echo area message unless
13619 another message has been requested in its place.
13621 This is useful in situations where you need to redisplay but no
13622 user action has occurred, making it inappropriate for the message
13623 area to be cleared. See tracking_off and
13624 wait_reading_process_output for examples of these situations.
13626 FROM_WHERE is an integer saying from where this function was
13627 called. This is useful for debugging. */
13629 void
13630 redisplay_preserve_echo_area (int from_where)
13632 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13634 if (!NILP (echo_area_buffer[1]))
13636 /* We have a previously displayed message, but no current
13637 message. Redisplay the previous message. */
13638 display_last_displayed_message_p = 1;
13639 redisplay_internal ();
13640 display_last_displayed_message_p = 0;
13642 else
13643 redisplay_internal ();
13645 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13646 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13647 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13651 /* Function registered with record_unwind_protect in redisplay_internal. */
13653 static void
13654 unwind_redisplay (void)
13656 redisplaying_p = 0;
13660 /* Mark the display of leaf window W as accurate or inaccurate.
13661 If ACCURATE_P is non-zero mark display of W as accurate. If
13662 ACCURATE_P is zero, arrange for W to be redisplayed the next
13663 time redisplay_internal is called. */
13665 static void
13666 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13668 struct buffer *b = XBUFFER (w->contents);
13670 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13671 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13672 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13674 if (accurate_p)
13676 b->clip_changed = 0;
13677 b->prevent_redisplay_optimizations_p = 0;
13679 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13680 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13681 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13682 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13684 w->current_matrix->buffer = b;
13685 w->current_matrix->begv = BUF_BEGV (b);
13686 w->current_matrix->zv = BUF_ZV (b);
13688 w->last_cursor = w->cursor;
13689 w->last_cursor_off_p = w->cursor_off_p;
13691 if (w == XWINDOW (selected_window))
13692 w->last_point = BUF_PT (b);
13693 else
13694 w->last_point = marker_position (w->pointm);
13696 w->window_end_valid = 1;
13697 w->update_mode_line = 0;
13702 /* Mark the display of windows in the window tree rooted at WINDOW as
13703 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13704 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13705 be redisplayed the next time redisplay_internal is called. */
13707 void
13708 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13710 struct window *w;
13712 for (; !NILP (window); window = w->next)
13714 w = XWINDOW (window);
13715 if (WINDOWP (w->contents))
13716 mark_window_display_accurate (w->contents, accurate_p);
13717 else
13718 mark_window_display_accurate_1 (w, accurate_p);
13721 if (accurate_p)
13722 update_overlay_arrows (1);
13723 else
13724 /* Force a thorough redisplay the next time by setting
13725 last_arrow_position and last_arrow_string to t, which is
13726 unequal to any useful value of Voverlay_arrow_... */
13727 update_overlay_arrows (-1);
13731 /* Return value in display table DP (Lisp_Char_Table *) for character
13732 C. Since a display table doesn't have any parent, we don't have to
13733 follow parent. Do not call this function directly but use the
13734 macro DISP_CHAR_VECTOR. */
13736 Lisp_Object
13737 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13739 Lisp_Object val;
13741 if (ASCII_CHAR_P (c))
13743 val = dp->ascii;
13744 if (SUB_CHAR_TABLE_P (val))
13745 val = XSUB_CHAR_TABLE (val)->contents[c];
13747 else
13749 Lisp_Object table;
13751 XSETCHAR_TABLE (table, dp);
13752 val = char_table_ref (table, c);
13754 if (NILP (val))
13755 val = dp->defalt;
13756 return val;
13761 /***********************************************************************
13762 Window Redisplay
13763 ***********************************************************************/
13765 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13767 static void
13768 redisplay_windows (Lisp_Object window)
13770 while (!NILP (window))
13772 struct window *w = XWINDOW (window);
13774 if (WINDOWP (w->contents))
13775 redisplay_windows (w->contents);
13776 else if (BUFFERP (w->contents))
13778 displayed_buffer = XBUFFER (w->contents);
13779 /* Use list_of_error, not Qerror, so that
13780 we catch only errors and don't run the debugger. */
13781 internal_condition_case_1 (redisplay_window_0, window,
13782 list_of_error,
13783 redisplay_window_error);
13786 window = w->next;
13790 static Lisp_Object
13791 redisplay_window_error (Lisp_Object ignore)
13793 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13794 return Qnil;
13797 static Lisp_Object
13798 redisplay_window_0 (Lisp_Object window)
13800 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13801 redisplay_window (window, 0);
13802 return Qnil;
13805 static Lisp_Object
13806 redisplay_window_1 (Lisp_Object window)
13808 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13809 redisplay_window (window, 1);
13810 return Qnil;
13814 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13815 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13816 which positions recorded in ROW differ from current buffer
13817 positions.
13819 Return 0 if cursor is not on this row, 1 otherwise. */
13821 static int
13822 set_cursor_from_row (struct window *w, struct glyph_row *row,
13823 struct glyph_matrix *matrix,
13824 ptrdiff_t delta, ptrdiff_t delta_bytes,
13825 int dy, int dvpos)
13827 struct glyph *glyph = row->glyphs[TEXT_AREA];
13828 struct glyph *end = glyph + row->used[TEXT_AREA];
13829 struct glyph *cursor = NULL;
13830 /* The last known character position in row. */
13831 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13832 int x = row->x;
13833 ptrdiff_t pt_old = PT - delta;
13834 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13835 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13836 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13837 /* A glyph beyond the edge of TEXT_AREA which we should never
13838 touch. */
13839 struct glyph *glyphs_end = end;
13840 /* Non-zero means we've found a match for cursor position, but that
13841 glyph has the avoid_cursor_p flag set. */
13842 int match_with_avoid_cursor = 0;
13843 /* Non-zero means we've seen at least one glyph that came from a
13844 display string. */
13845 int string_seen = 0;
13846 /* Largest and smallest buffer positions seen so far during scan of
13847 glyph row. */
13848 ptrdiff_t bpos_max = pos_before;
13849 ptrdiff_t bpos_min = pos_after;
13850 /* Last buffer position covered by an overlay string with an integer
13851 `cursor' property. */
13852 ptrdiff_t bpos_covered = 0;
13853 /* Non-zero means the display string on which to display the cursor
13854 comes from a text property, not from an overlay. */
13855 int string_from_text_prop = 0;
13857 /* Don't even try doing anything if called for a mode-line or
13858 header-line row, since the rest of the code isn't prepared to
13859 deal with such calamities. */
13860 eassert (!row->mode_line_p);
13861 if (row->mode_line_p)
13862 return 0;
13864 /* Skip over glyphs not having an object at the start and the end of
13865 the row. These are special glyphs like truncation marks on
13866 terminal frames. */
13867 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13869 if (!row->reversed_p)
13871 while (glyph < end
13872 && INTEGERP (glyph->object)
13873 && glyph->charpos < 0)
13875 x += glyph->pixel_width;
13876 ++glyph;
13878 while (end > glyph
13879 && INTEGERP ((end - 1)->object)
13880 /* CHARPOS is zero for blanks and stretch glyphs
13881 inserted by extend_face_to_end_of_line. */
13882 && (end - 1)->charpos <= 0)
13883 --end;
13884 glyph_before = glyph - 1;
13885 glyph_after = end;
13887 else
13889 struct glyph *g;
13891 /* If the glyph row is reversed, we need to process it from back
13892 to front, so swap the edge pointers. */
13893 glyphs_end = end = glyph - 1;
13894 glyph += row->used[TEXT_AREA] - 1;
13896 while (glyph > end + 1
13897 && INTEGERP (glyph->object)
13898 && glyph->charpos < 0)
13900 --glyph;
13901 x -= glyph->pixel_width;
13903 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13904 --glyph;
13905 /* By default, in reversed rows we put the cursor on the
13906 rightmost (first in the reading order) glyph. */
13907 for (g = end + 1; g < glyph; g++)
13908 x += g->pixel_width;
13909 while (end < glyph
13910 && INTEGERP ((end + 1)->object)
13911 && (end + 1)->charpos <= 0)
13912 ++end;
13913 glyph_before = glyph + 1;
13914 glyph_after = end;
13917 else if (row->reversed_p)
13919 /* In R2L rows that don't display text, put the cursor on the
13920 rightmost glyph. Case in point: an empty last line that is
13921 part of an R2L paragraph. */
13922 cursor = end - 1;
13923 /* Avoid placing the cursor on the last glyph of the row, where
13924 on terminal frames we hold the vertical border between
13925 adjacent windows. */
13926 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13927 && !WINDOW_RIGHTMOST_P (w)
13928 && cursor == row->glyphs[LAST_AREA] - 1)
13929 cursor--;
13930 x = -1; /* will be computed below, at label compute_x */
13933 /* Step 1: Try to find the glyph whose character position
13934 corresponds to point. If that's not possible, find 2 glyphs
13935 whose character positions are the closest to point, one before
13936 point, the other after it. */
13937 if (!row->reversed_p)
13938 while (/* not marched to end of glyph row */
13939 glyph < end
13940 /* glyph was not inserted by redisplay for internal purposes */
13941 && !INTEGERP (glyph->object))
13943 if (BUFFERP (glyph->object))
13945 ptrdiff_t dpos = glyph->charpos - pt_old;
13947 if (glyph->charpos > bpos_max)
13948 bpos_max = glyph->charpos;
13949 if (glyph->charpos < bpos_min)
13950 bpos_min = glyph->charpos;
13951 if (!glyph->avoid_cursor_p)
13953 /* If we hit point, we've found the glyph on which to
13954 display the cursor. */
13955 if (dpos == 0)
13957 match_with_avoid_cursor = 0;
13958 break;
13960 /* See if we've found a better approximation to
13961 POS_BEFORE or to POS_AFTER. */
13962 if (0 > dpos && dpos > pos_before - pt_old)
13964 pos_before = glyph->charpos;
13965 glyph_before = glyph;
13967 else if (0 < dpos && dpos < pos_after - pt_old)
13969 pos_after = glyph->charpos;
13970 glyph_after = glyph;
13973 else if (dpos == 0)
13974 match_with_avoid_cursor = 1;
13976 else if (STRINGP (glyph->object))
13978 Lisp_Object chprop;
13979 ptrdiff_t glyph_pos = glyph->charpos;
13981 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13982 glyph->object);
13983 if (!NILP (chprop))
13985 /* If the string came from a `display' text property,
13986 look up the buffer position of that property and
13987 use that position to update bpos_max, as if we
13988 actually saw such a position in one of the row's
13989 glyphs. This helps with supporting integer values
13990 of `cursor' property on the display string in
13991 situations where most or all of the row's buffer
13992 text is completely covered by display properties,
13993 so that no glyph with valid buffer positions is
13994 ever seen in the row. */
13995 ptrdiff_t prop_pos =
13996 string_buffer_position_lim (glyph->object, pos_before,
13997 pos_after, 0);
13999 if (prop_pos >= pos_before)
14000 bpos_max = prop_pos - 1;
14002 if (INTEGERP (chprop))
14004 bpos_covered = bpos_max + XINT (chprop);
14005 /* If the `cursor' property covers buffer positions up
14006 to and including point, we should display cursor on
14007 this glyph. Note that, if a `cursor' property on one
14008 of the string's characters has an integer value, we
14009 will break out of the loop below _before_ we get to
14010 the position match above. IOW, integer values of
14011 the `cursor' property override the "exact match for
14012 point" strategy of positioning the cursor. */
14013 /* Implementation note: bpos_max == pt_old when, e.g.,
14014 we are in an empty line, where bpos_max is set to
14015 MATRIX_ROW_START_CHARPOS, see above. */
14016 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14018 cursor = glyph;
14019 break;
14023 string_seen = 1;
14025 x += glyph->pixel_width;
14026 ++glyph;
14028 else if (glyph > end) /* row is reversed */
14029 while (!INTEGERP (glyph->object))
14031 if (BUFFERP (glyph->object))
14033 ptrdiff_t dpos = glyph->charpos - pt_old;
14035 if (glyph->charpos > bpos_max)
14036 bpos_max = glyph->charpos;
14037 if (glyph->charpos < bpos_min)
14038 bpos_min = glyph->charpos;
14039 if (!glyph->avoid_cursor_p)
14041 if (dpos == 0)
14043 match_with_avoid_cursor = 0;
14044 break;
14046 if (0 > dpos && dpos > pos_before - pt_old)
14048 pos_before = glyph->charpos;
14049 glyph_before = glyph;
14051 else if (0 < dpos && dpos < pos_after - pt_old)
14053 pos_after = glyph->charpos;
14054 glyph_after = glyph;
14057 else if (dpos == 0)
14058 match_with_avoid_cursor = 1;
14060 else if (STRINGP (glyph->object))
14062 Lisp_Object chprop;
14063 ptrdiff_t glyph_pos = glyph->charpos;
14065 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14066 glyph->object);
14067 if (!NILP (chprop))
14069 ptrdiff_t prop_pos =
14070 string_buffer_position_lim (glyph->object, pos_before,
14071 pos_after, 0);
14073 if (prop_pos >= pos_before)
14074 bpos_max = prop_pos - 1;
14076 if (INTEGERP (chprop))
14078 bpos_covered = bpos_max + XINT (chprop);
14079 /* If the `cursor' property covers buffer positions up
14080 to and including point, we should display cursor on
14081 this glyph. */
14082 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14084 cursor = glyph;
14085 break;
14088 string_seen = 1;
14090 --glyph;
14091 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14093 x--; /* can't use any pixel_width */
14094 break;
14096 x -= glyph->pixel_width;
14099 /* Step 2: If we didn't find an exact match for point, we need to
14100 look for a proper place to put the cursor among glyphs between
14101 GLYPH_BEFORE and GLYPH_AFTER. */
14102 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14103 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14104 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14106 /* An empty line has a single glyph whose OBJECT is zero and
14107 whose CHARPOS is the position of a newline on that line.
14108 Note that on a TTY, there are more glyphs after that, which
14109 were produced by extend_face_to_end_of_line, but their
14110 CHARPOS is zero or negative. */
14111 int empty_line_p =
14112 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14113 && INTEGERP (glyph->object) && glyph->charpos > 0
14114 /* On a TTY, continued and truncated rows also have a glyph at
14115 their end whose OBJECT is zero and whose CHARPOS is
14116 positive (the continuation and truncation glyphs), but such
14117 rows are obviously not "empty". */
14118 && !(row->continued_p || row->truncated_on_right_p);
14120 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14122 ptrdiff_t ellipsis_pos;
14124 /* Scan back over the ellipsis glyphs. */
14125 if (!row->reversed_p)
14127 ellipsis_pos = (glyph - 1)->charpos;
14128 while (glyph > row->glyphs[TEXT_AREA]
14129 && (glyph - 1)->charpos == ellipsis_pos)
14130 glyph--, x -= glyph->pixel_width;
14131 /* That loop always goes one position too far, including
14132 the glyph before the ellipsis. So scan forward over
14133 that one. */
14134 x += glyph->pixel_width;
14135 glyph++;
14137 else /* row is reversed */
14139 ellipsis_pos = (glyph + 1)->charpos;
14140 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14141 && (glyph + 1)->charpos == ellipsis_pos)
14142 glyph++, x += glyph->pixel_width;
14143 x -= glyph->pixel_width;
14144 glyph--;
14147 else if (match_with_avoid_cursor)
14149 cursor = glyph_after;
14150 x = -1;
14152 else if (string_seen)
14154 int incr = row->reversed_p ? -1 : +1;
14156 /* Need to find the glyph that came out of a string which is
14157 present at point. That glyph is somewhere between
14158 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14159 positioned between POS_BEFORE and POS_AFTER in the
14160 buffer. */
14161 struct glyph *start, *stop;
14162 ptrdiff_t pos = pos_before;
14164 x = -1;
14166 /* If the row ends in a newline from a display string,
14167 reordering could have moved the glyphs belonging to the
14168 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14169 in this case we extend the search to the last glyph in
14170 the row that was not inserted by redisplay. */
14171 if (row->ends_in_newline_from_string_p)
14173 glyph_after = end;
14174 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14177 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14178 correspond to POS_BEFORE and POS_AFTER, respectively. We
14179 need START and STOP in the order that corresponds to the
14180 row's direction as given by its reversed_p flag. If the
14181 directionality of characters between POS_BEFORE and
14182 POS_AFTER is the opposite of the row's base direction,
14183 these characters will have been reordered for display,
14184 and we need to reverse START and STOP. */
14185 if (!row->reversed_p)
14187 start = min (glyph_before, glyph_after);
14188 stop = max (glyph_before, glyph_after);
14190 else
14192 start = max (glyph_before, glyph_after);
14193 stop = min (glyph_before, glyph_after);
14195 for (glyph = start + incr;
14196 row->reversed_p ? glyph > stop : glyph < stop; )
14199 /* Any glyphs that come from the buffer are here because
14200 of bidi reordering. Skip them, and only pay
14201 attention to glyphs that came from some string. */
14202 if (STRINGP (glyph->object))
14204 Lisp_Object str;
14205 ptrdiff_t tem;
14206 /* If the display property covers the newline, we
14207 need to search for it one position farther. */
14208 ptrdiff_t lim = pos_after
14209 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14211 string_from_text_prop = 0;
14212 str = glyph->object;
14213 tem = string_buffer_position_lim (str, pos, lim, 0);
14214 if (tem == 0 /* from overlay */
14215 || pos <= tem)
14217 /* If the string from which this glyph came is
14218 found in the buffer at point, or at position
14219 that is closer to point than pos_after, then
14220 we've found the glyph we've been looking for.
14221 If it comes from an overlay (tem == 0), and
14222 it has the `cursor' property on one of its
14223 glyphs, record that glyph as a candidate for
14224 displaying the cursor. (As in the
14225 unidirectional version, we will display the
14226 cursor on the last candidate we find.) */
14227 if (tem == 0
14228 || tem == pt_old
14229 || (tem - pt_old > 0 && tem < pos_after))
14231 /* The glyphs from this string could have
14232 been reordered. Find the one with the
14233 smallest string position. Or there could
14234 be a character in the string with the
14235 `cursor' property, which means display
14236 cursor on that character's glyph. */
14237 ptrdiff_t strpos = glyph->charpos;
14239 if (tem)
14241 cursor = glyph;
14242 string_from_text_prop = 1;
14244 for ( ;
14245 (row->reversed_p ? glyph > stop : glyph < stop)
14246 && EQ (glyph->object, str);
14247 glyph += incr)
14249 Lisp_Object cprop;
14250 ptrdiff_t gpos = glyph->charpos;
14252 cprop = Fget_char_property (make_number (gpos),
14253 Qcursor,
14254 glyph->object);
14255 if (!NILP (cprop))
14257 cursor = glyph;
14258 break;
14260 if (tem && glyph->charpos < strpos)
14262 strpos = glyph->charpos;
14263 cursor = glyph;
14267 if (tem == pt_old
14268 || (tem - pt_old > 0 && tem < pos_after))
14269 goto compute_x;
14271 if (tem)
14272 pos = tem + 1; /* don't find previous instances */
14274 /* This string is not what we want; skip all of the
14275 glyphs that came from it. */
14276 while ((row->reversed_p ? glyph > stop : glyph < stop)
14277 && EQ (glyph->object, str))
14278 glyph += incr;
14280 else
14281 glyph += incr;
14284 /* If we reached the end of the line, and END was from a string,
14285 the cursor is not on this line. */
14286 if (cursor == NULL
14287 && (row->reversed_p ? glyph <= end : glyph >= end)
14288 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14289 && STRINGP (end->object)
14290 && row->continued_p)
14291 return 0;
14293 /* A truncated row may not include PT among its character positions.
14294 Setting the cursor inside the scroll margin will trigger
14295 recalculation of hscroll in hscroll_window_tree. But if a
14296 display string covers point, defer to the string-handling
14297 code below to figure this out. */
14298 else if (row->truncated_on_left_p && pt_old < bpos_min)
14300 cursor = glyph_before;
14301 x = -1;
14303 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14304 /* Zero-width characters produce no glyphs. */
14305 || (!empty_line_p
14306 && (row->reversed_p
14307 ? glyph_after > glyphs_end
14308 : glyph_after < glyphs_end)))
14310 cursor = glyph_after;
14311 x = -1;
14315 compute_x:
14316 if (cursor != NULL)
14317 glyph = cursor;
14318 else if (glyph == glyphs_end
14319 && pos_before == pos_after
14320 && STRINGP ((row->reversed_p
14321 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14322 : row->glyphs[TEXT_AREA])->object))
14324 /* If all the glyphs of this row came from strings, put the
14325 cursor on the first glyph of the row. This avoids having the
14326 cursor outside of the text area in this very rare and hard
14327 use case. */
14328 glyph =
14329 row->reversed_p
14330 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14331 : row->glyphs[TEXT_AREA];
14333 if (x < 0)
14335 struct glyph *g;
14337 /* Need to compute x that corresponds to GLYPH. */
14338 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14340 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14341 emacs_abort ();
14342 x += g->pixel_width;
14346 /* ROW could be part of a continued line, which, under bidi
14347 reordering, might have other rows whose start and end charpos
14348 occlude point. Only set w->cursor if we found a better
14349 approximation to the cursor position than we have from previously
14350 examined candidate rows belonging to the same continued line. */
14351 if (/* we already have a candidate row */
14352 w->cursor.vpos >= 0
14353 /* that candidate is not the row we are processing */
14354 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14355 /* Make sure cursor.vpos specifies a row whose start and end
14356 charpos occlude point, and it is valid candidate for being a
14357 cursor-row. This is because some callers of this function
14358 leave cursor.vpos at the row where the cursor was displayed
14359 during the last redisplay cycle. */
14360 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14361 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14362 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14364 struct glyph *g1 =
14365 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14367 /* Don't consider glyphs that are outside TEXT_AREA. */
14368 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14369 return 0;
14370 /* Keep the candidate whose buffer position is the closest to
14371 point or has the `cursor' property. */
14372 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14373 w->cursor.hpos >= 0
14374 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14375 && ((BUFFERP (g1->object)
14376 && (g1->charpos == pt_old /* an exact match always wins */
14377 || (BUFFERP (glyph->object)
14378 && eabs (g1->charpos - pt_old)
14379 < eabs (glyph->charpos - pt_old))))
14380 /* previous candidate is a glyph from a string that has
14381 a non-nil `cursor' property */
14382 || (STRINGP (g1->object)
14383 && (!NILP (Fget_char_property (make_number (g1->charpos),
14384 Qcursor, g1->object))
14385 /* previous candidate is from the same display
14386 string as this one, and the display string
14387 came from a text property */
14388 || (EQ (g1->object, glyph->object)
14389 && string_from_text_prop)
14390 /* this candidate is from newline and its
14391 position is not an exact match */
14392 || (INTEGERP (glyph->object)
14393 && glyph->charpos != pt_old)))))
14394 return 0;
14395 /* If this candidate gives an exact match, use that. */
14396 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14397 /* If this candidate is a glyph created for the
14398 terminating newline of a line, and point is on that
14399 newline, it wins because it's an exact match. */
14400 || (!row->continued_p
14401 && INTEGERP (glyph->object)
14402 && glyph->charpos == 0
14403 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14404 /* Otherwise, keep the candidate that comes from a row
14405 spanning less buffer positions. This may win when one or
14406 both candidate positions are on glyphs that came from
14407 display strings, for which we cannot compare buffer
14408 positions. */
14409 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14410 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14411 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14412 return 0;
14414 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14415 w->cursor.x = x;
14416 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14417 w->cursor.y = row->y + dy;
14419 if (w == XWINDOW (selected_window))
14421 if (!row->continued_p
14422 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14423 && row->x == 0)
14425 this_line_buffer = XBUFFER (w->contents);
14427 CHARPOS (this_line_start_pos)
14428 = MATRIX_ROW_START_CHARPOS (row) + delta;
14429 BYTEPOS (this_line_start_pos)
14430 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14432 CHARPOS (this_line_end_pos)
14433 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14434 BYTEPOS (this_line_end_pos)
14435 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14437 this_line_y = w->cursor.y;
14438 this_line_pixel_height = row->height;
14439 this_line_vpos = w->cursor.vpos;
14440 this_line_start_x = row->x;
14442 else
14443 CHARPOS (this_line_start_pos) = 0;
14446 return 1;
14450 /* Run window scroll functions, if any, for WINDOW with new window
14451 start STARTP. Sets the window start of WINDOW to that position.
14453 We assume that the window's buffer is really current. */
14455 static struct text_pos
14456 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14458 struct window *w = XWINDOW (window);
14459 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14461 if (current_buffer != XBUFFER (w->contents))
14462 emacs_abort ();
14464 if (!NILP (Vwindow_scroll_functions))
14466 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14467 make_number (CHARPOS (startp)));
14468 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14469 /* In case the hook functions switch buffers. */
14470 set_buffer_internal (XBUFFER (w->contents));
14473 return startp;
14477 /* Make sure the line containing the cursor is fully visible.
14478 A value of 1 means there is nothing to be done.
14479 (Either the line is fully visible, or it cannot be made so,
14480 or we cannot tell.)
14482 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14483 is higher than window.
14485 A value of 0 means the caller should do scrolling
14486 as if point had gone off the screen. */
14488 static int
14489 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14491 struct glyph_matrix *matrix;
14492 struct glyph_row *row;
14493 int window_height;
14495 if (!make_cursor_line_fully_visible_p)
14496 return 1;
14498 /* It's not always possible to find the cursor, e.g, when a window
14499 is full of overlay strings. Don't do anything in that case. */
14500 if (w->cursor.vpos < 0)
14501 return 1;
14503 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14504 row = MATRIX_ROW (matrix, w->cursor.vpos);
14506 /* If the cursor row is not partially visible, there's nothing to do. */
14507 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14508 return 1;
14510 /* If the row the cursor is in is taller than the window's height,
14511 it's not clear what to do, so do nothing. */
14512 window_height = window_box_height (w);
14513 if (row->height >= window_height)
14515 if (!force_p || MINI_WINDOW_P (w)
14516 || w->vscroll || w->cursor.vpos == 0)
14517 return 1;
14519 return 0;
14523 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14524 non-zero means only WINDOW is redisplayed in redisplay_internal.
14525 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14526 in redisplay_window to bring a partially visible line into view in
14527 the case that only the cursor has moved.
14529 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14530 last screen line's vertical height extends past the end of the screen.
14532 Value is
14534 1 if scrolling succeeded
14536 0 if scrolling didn't find point.
14538 -1 if new fonts have been loaded so that we must interrupt
14539 redisplay, adjust glyph matrices, and try again. */
14541 enum
14543 SCROLLING_SUCCESS,
14544 SCROLLING_FAILED,
14545 SCROLLING_NEED_LARGER_MATRICES
14548 /* If scroll-conservatively is more than this, never recenter.
14550 If you change this, don't forget to update the doc string of
14551 `scroll-conservatively' and the Emacs manual. */
14552 #define SCROLL_LIMIT 100
14554 static int
14555 try_scrolling (Lisp_Object window, int just_this_one_p,
14556 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14557 int temp_scroll_step, int last_line_misfit)
14559 struct window *w = XWINDOW (window);
14560 struct frame *f = XFRAME (w->frame);
14561 struct text_pos pos, startp;
14562 struct it it;
14563 int this_scroll_margin, scroll_max, rc, height;
14564 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14565 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14566 Lisp_Object aggressive;
14567 /* We will never try scrolling more than this number of lines. */
14568 int scroll_limit = SCROLL_LIMIT;
14569 int frame_line_height = default_line_pixel_height (w);
14570 int window_total_lines
14571 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14573 #ifdef GLYPH_DEBUG
14574 debug_method_add (w, "try_scrolling");
14575 #endif
14577 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14579 /* Compute scroll margin height in pixels. We scroll when point is
14580 within this distance from the top or bottom of the window. */
14581 if (scroll_margin > 0)
14582 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14583 * frame_line_height;
14584 else
14585 this_scroll_margin = 0;
14587 /* Force arg_scroll_conservatively to have a reasonable value, to
14588 avoid scrolling too far away with slow move_it_* functions. Note
14589 that the user can supply scroll-conservatively equal to
14590 `most-positive-fixnum', which can be larger than INT_MAX. */
14591 if (arg_scroll_conservatively > scroll_limit)
14593 arg_scroll_conservatively = scroll_limit + 1;
14594 scroll_max = scroll_limit * frame_line_height;
14596 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14597 /* Compute how much we should try to scroll maximally to bring
14598 point into view. */
14599 scroll_max = (max (scroll_step,
14600 max (arg_scroll_conservatively, temp_scroll_step))
14601 * frame_line_height);
14602 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14603 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14604 /* We're trying to scroll because of aggressive scrolling but no
14605 scroll_step is set. Choose an arbitrary one. */
14606 scroll_max = 10 * frame_line_height;
14607 else
14608 scroll_max = 0;
14610 too_near_end:
14612 /* Decide whether to scroll down. */
14613 if (PT > CHARPOS (startp))
14615 int scroll_margin_y;
14617 /* Compute the pixel ypos of the scroll margin, then move IT to
14618 either that ypos or PT, whichever comes first. */
14619 start_display (&it, w, startp);
14620 scroll_margin_y = it.last_visible_y - this_scroll_margin
14621 - frame_line_height * extra_scroll_margin_lines;
14622 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14623 (MOVE_TO_POS | MOVE_TO_Y));
14625 if (PT > CHARPOS (it.current.pos))
14627 int y0 = line_bottom_y (&it);
14628 /* Compute how many pixels below window bottom to stop searching
14629 for PT. This avoids costly search for PT that is far away if
14630 the user limited scrolling by a small number of lines, but
14631 always finds PT if scroll_conservatively is set to a large
14632 number, such as most-positive-fixnum. */
14633 int slack = max (scroll_max, 10 * frame_line_height);
14634 int y_to_move = it.last_visible_y + slack;
14636 /* Compute the distance from the scroll margin to PT or to
14637 the scroll limit, whichever comes first. This should
14638 include the height of the cursor line, to make that line
14639 fully visible. */
14640 move_it_to (&it, PT, -1, y_to_move,
14641 -1, MOVE_TO_POS | MOVE_TO_Y);
14642 dy = line_bottom_y (&it) - y0;
14644 if (dy > scroll_max)
14645 return SCROLLING_FAILED;
14647 if (dy > 0)
14648 scroll_down_p = 1;
14652 if (scroll_down_p)
14654 /* Point is in or below the bottom scroll margin, so move the
14655 window start down. If scrolling conservatively, move it just
14656 enough down to make point visible. If scroll_step is set,
14657 move it down by scroll_step. */
14658 if (arg_scroll_conservatively)
14659 amount_to_scroll
14660 = min (max (dy, frame_line_height),
14661 frame_line_height * arg_scroll_conservatively);
14662 else if (scroll_step || temp_scroll_step)
14663 amount_to_scroll = scroll_max;
14664 else
14666 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14667 height = WINDOW_BOX_TEXT_HEIGHT (w);
14668 if (NUMBERP (aggressive))
14670 double float_amount = XFLOATINT (aggressive) * height;
14671 int aggressive_scroll = float_amount;
14672 if (aggressive_scroll == 0 && float_amount > 0)
14673 aggressive_scroll = 1;
14674 /* Don't let point enter the scroll margin near top of
14675 the window. This could happen if the value of
14676 scroll_up_aggressively is too large and there are
14677 non-zero margins, because scroll_up_aggressively
14678 means put point that fraction of window height
14679 _from_the_bottom_margin_. */
14680 if (aggressive_scroll + 2*this_scroll_margin > height)
14681 aggressive_scroll = height - 2*this_scroll_margin;
14682 amount_to_scroll = dy + aggressive_scroll;
14686 if (amount_to_scroll <= 0)
14687 return SCROLLING_FAILED;
14689 start_display (&it, w, startp);
14690 if (arg_scroll_conservatively <= scroll_limit)
14691 move_it_vertically (&it, amount_to_scroll);
14692 else
14694 /* Extra precision for users who set scroll-conservatively
14695 to a large number: make sure the amount we scroll
14696 the window start is never less than amount_to_scroll,
14697 which was computed as distance from window bottom to
14698 point. This matters when lines at window top and lines
14699 below window bottom have different height. */
14700 struct it it1;
14701 void *it1data = NULL;
14702 /* We use a temporary it1 because line_bottom_y can modify
14703 its argument, if it moves one line down; see there. */
14704 int start_y;
14706 SAVE_IT (it1, it, it1data);
14707 start_y = line_bottom_y (&it1);
14708 do {
14709 RESTORE_IT (&it, &it, it1data);
14710 move_it_by_lines (&it, 1);
14711 SAVE_IT (it1, it, it1data);
14712 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14715 /* If STARTP is unchanged, move it down another screen line. */
14716 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14717 move_it_by_lines (&it, 1);
14718 startp = it.current.pos;
14720 else
14722 struct text_pos scroll_margin_pos = startp;
14723 int y_offset = 0;
14725 /* See if point is inside the scroll margin at the top of the
14726 window. */
14727 if (this_scroll_margin)
14729 int y_start;
14731 start_display (&it, w, startp);
14732 y_start = it.current_y;
14733 move_it_vertically (&it, this_scroll_margin);
14734 scroll_margin_pos = it.current.pos;
14735 /* If we didn't move enough before hitting ZV, request
14736 additional amount of scroll, to move point out of the
14737 scroll margin. */
14738 if (IT_CHARPOS (it) == ZV
14739 && it.current_y - y_start < this_scroll_margin)
14740 y_offset = this_scroll_margin - (it.current_y - y_start);
14743 if (PT < CHARPOS (scroll_margin_pos))
14745 /* Point is in the scroll margin at the top of the window or
14746 above what is displayed in the window. */
14747 int y0, y_to_move;
14749 /* Compute the vertical distance from PT to the scroll
14750 margin position. Move as far as scroll_max allows, or
14751 one screenful, or 10 screen lines, whichever is largest.
14752 Give up if distance is greater than scroll_max or if we
14753 didn't reach the scroll margin position. */
14754 SET_TEXT_POS (pos, PT, PT_BYTE);
14755 start_display (&it, w, pos);
14756 y0 = it.current_y;
14757 y_to_move = max (it.last_visible_y,
14758 max (scroll_max, 10 * frame_line_height));
14759 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14760 y_to_move, -1,
14761 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14762 dy = it.current_y - y0;
14763 if (dy > scroll_max
14764 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14765 return SCROLLING_FAILED;
14767 /* Additional scroll for when ZV was too close to point. */
14768 dy += y_offset;
14770 /* Compute new window start. */
14771 start_display (&it, w, startp);
14773 if (arg_scroll_conservatively)
14774 amount_to_scroll = max (dy, frame_line_height *
14775 max (scroll_step, temp_scroll_step));
14776 else if (scroll_step || temp_scroll_step)
14777 amount_to_scroll = scroll_max;
14778 else
14780 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14781 height = WINDOW_BOX_TEXT_HEIGHT (w);
14782 if (NUMBERP (aggressive))
14784 double float_amount = XFLOATINT (aggressive) * height;
14785 int aggressive_scroll = float_amount;
14786 if (aggressive_scroll == 0 && float_amount > 0)
14787 aggressive_scroll = 1;
14788 /* Don't let point enter the scroll margin near
14789 bottom of the window, if the value of
14790 scroll_down_aggressively happens to be too
14791 large. */
14792 if (aggressive_scroll + 2*this_scroll_margin > height)
14793 aggressive_scroll = height - 2*this_scroll_margin;
14794 amount_to_scroll = dy + aggressive_scroll;
14798 if (amount_to_scroll <= 0)
14799 return SCROLLING_FAILED;
14801 move_it_vertically_backward (&it, amount_to_scroll);
14802 startp = it.current.pos;
14806 /* Run window scroll functions. */
14807 startp = run_window_scroll_functions (window, startp);
14809 /* Display the window. Give up if new fonts are loaded, or if point
14810 doesn't appear. */
14811 if (!try_window (window, startp, 0))
14812 rc = SCROLLING_NEED_LARGER_MATRICES;
14813 else if (w->cursor.vpos < 0)
14815 clear_glyph_matrix (w->desired_matrix);
14816 rc = SCROLLING_FAILED;
14818 else
14820 /* Maybe forget recorded base line for line number display. */
14821 if (!just_this_one_p
14822 || current_buffer->clip_changed
14823 || BEG_UNCHANGED < CHARPOS (startp))
14824 w->base_line_number = 0;
14826 /* If cursor ends up on a partially visible line,
14827 treat that as being off the bottom of the screen. */
14828 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14829 /* It's possible that the cursor is on the first line of the
14830 buffer, which is partially obscured due to a vscroll
14831 (Bug#7537). In that case, avoid looping forever . */
14832 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14834 clear_glyph_matrix (w->desired_matrix);
14835 ++extra_scroll_margin_lines;
14836 goto too_near_end;
14838 rc = SCROLLING_SUCCESS;
14841 return rc;
14845 /* Compute a suitable window start for window W if display of W starts
14846 on a continuation line. Value is non-zero if a new window start
14847 was computed.
14849 The new window start will be computed, based on W's width, starting
14850 from the start of the continued line. It is the start of the
14851 screen line with the minimum distance from the old start W->start. */
14853 static int
14854 compute_window_start_on_continuation_line (struct window *w)
14856 struct text_pos pos, start_pos;
14857 int window_start_changed_p = 0;
14859 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14861 /* If window start is on a continuation line... Window start may be
14862 < BEGV in case there's invisible text at the start of the
14863 buffer (M-x rmail, for example). */
14864 if (CHARPOS (start_pos) > BEGV
14865 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14867 struct it it;
14868 struct glyph_row *row;
14870 /* Handle the case that the window start is out of range. */
14871 if (CHARPOS (start_pos) < BEGV)
14872 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14873 else if (CHARPOS (start_pos) > ZV)
14874 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14876 /* Find the start of the continued line. This should be fast
14877 because find_newline is fast (newline cache). */
14878 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14879 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14880 row, DEFAULT_FACE_ID);
14881 reseat_at_previous_visible_line_start (&it);
14883 /* If the line start is "too far" away from the window start,
14884 say it takes too much time to compute a new window start. */
14885 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14886 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14888 int min_distance, distance;
14890 /* Move forward by display lines to find the new window
14891 start. If window width was enlarged, the new start can
14892 be expected to be > the old start. If window width was
14893 decreased, the new window start will be < the old start.
14894 So, we're looking for the display line start with the
14895 minimum distance from the old window start. */
14896 pos = it.current.pos;
14897 min_distance = INFINITY;
14898 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14899 distance < min_distance)
14901 min_distance = distance;
14902 pos = it.current.pos;
14903 move_it_by_lines (&it, 1);
14906 /* Set the window start there. */
14907 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14908 window_start_changed_p = 1;
14912 return window_start_changed_p;
14916 /* Try cursor movement in case text has not changed in window WINDOW,
14917 with window start STARTP. Value is
14919 CURSOR_MOVEMENT_SUCCESS if successful
14921 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14923 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14924 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14925 we want to scroll as if scroll-step were set to 1. See the code.
14927 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14928 which case we have to abort this redisplay, and adjust matrices
14929 first. */
14931 enum
14933 CURSOR_MOVEMENT_SUCCESS,
14934 CURSOR_MOVEMENT_CANNOT_BE_USED,
14935 CURSOR_MOVEMENT_MUST_SCROLL,
14936 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14939 static int
14940 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14942 struct window *w = XWINDOW (window);
14943 struct frame *f = XFRAME (w->frame);
14944 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14946 #ifdef GLYPH_DEBUG
14947 if (inhibit_try_cursor_movement)
14948 return rc;
14949 #endif
14951 /* Previously, there was a check for Lisp integer in the
14952 if-statement below. Now, this field is converted to
14953 ptrdiff_t, thus zero means invalid position in a buffer. */
14954 eassert (w->last_point > 0);
14956 /* Handle case where text has not changed, only point, and it has
14957 not moved off the frame. */
14958 if (/* Point may be in this window. */
14959 PT >= CHARPOS (startp)
14960 /* Selective display hasn't changed. */
14961 && !current_buffer->clip_changed
14962 /* Function force-mode-line-update is used to force a thorough
14963 redisplay. It sets either windows_or_buffers_changed or
14964 update_mode_lines. So don't take a shortcut here for these
14965 cases. */
14966 && !update_mode_lines
14967 && !windows_or_buffers_changed
14968 && !cursor_type_changed
14969 /* Can't use this case if highlighting a region. When a
14970 region exists, cursor movement has to do more than just
14971 set the cursor. */
14972 && markpos_of_region () < 0
14973 && !w->region_showing
14974 && NILP (Vshow_trailing_whitespace)
14975 /* This code is not used for mini-buffer for the sake of the case
14976 of redisplaying to replace an echo area message; since in
14977 that case the mini-buffer contents per se are usually
14978 unchanged. This code is of no real use in the mini-buffer
14979 since the handling of this_line_start_pos, etc., in redisplay
14980 handles the same cases. */
14981 && !EQ (window, minibuf_window)
14982 /* When splitting windows or for new windows, it happens that
14983 redisplay is called with a nil window_end_vpos or one being
14984 larger than the window. This should really be fixed in
14985 window.c. I don't have this on my list, now, so we do
14986 approximately the same as the old redisplay code. --gerd. */
14987 && INTEGERP (w->window_end_vpos)
14988 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14989 && (FRAME_WINDOW_P (f)
14990 || !overlay_arrow_in_current_buffer_p ()))
14992 int this_scroll_margin, top_scroll_margin;
14993 struct glyph_row *row = NULL;
14994 int frame_line_height = default_line_pixel_height (w);
14995 int window_total_lines
14996 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14998 #ifdef GLYPH_DEBUG
14999 debug_method_add (w, "cursor movement");
15000 #endif
15002 /* Scroll if point within this distance from the top or bottom
15003 of the window. This is a pixel value. */
15004 if (scroll_margin > 0)
15006 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15007 this_scroll_margin *= frame_line_height;
15009 else
15010 this_scroll_margin = 0;
15012 top_scroll_margin = this_scroll_margin;
15013 if (WINDOW_WANTS_HEADER_LINE_P (w))
15014 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15016 /* Start with the row the cursor was displayed during the last
15017 not paused redisplay. Give up if that row is not valid. */
15018 if (w->last_cursor.vpos < 0
15019 || w->last_cursor.vpos >= w->current_matrix->nrows)
15020 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15021 else
15023 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15024 if (row->mode_line_p)
15025 ++row;
15026 if (!row->enabled_p)
15027 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15030 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15032 int scroll_p = 0, must_scroll = 0;
15033 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15035 if (PT > w->last_point)
15037 /* Point has moved forward. */
15038 while (MATRIX_ROW_END_CHARPOS (row) < PT
15039 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15041 eassert (row->enabled_p);
15042 ++row;
15045 /* If the end position of a row equals the start
15046 position of the next row, and PT is at that position,
15047 we would rather display cursor in the next line. */
15048 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15049 && MATRIX_ROW_END_CHARPOS (row) == PT
15050 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15051 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15052 && !cursor_row_p (row))
15053 ++row;
15055 /* If within the scroll margin, scroll. Note that
15056 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15057 the next line would be drawn, and that
15058 this_scroll_margin can be zero. */
15059 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15060 || PT > MATRIX_ROW_END_CHARPOS (row)
15061 /* Line is completely visible last line in window
15062 and PT is to be set in the next line. */
15063 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15064 && PT == MATRIX_ROW_END_CHARPOS (row)
15065 && !row->ends_at_zv_p
15066 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15067 scroll_p = 1;
15069 else if (PT < w->last_point)
15071 /* Cursor has to be moved backward. Note that PT >=
15072 CHARPOS (startp) because of the outer if-statement. */
15073 while (!row->mode_line_p
15074 && (MATRIX_ROW_START_CHARPOS (row) > PT
15075 || (MATRIX_ROW_START_CHARPOS (row) == PT
15076 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15077 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15078 row > w->current_matrix->rows
15079 && (row-1)->ends_in_newline_from_string_p))))
15080 && (row->y > top_scroll_margin
15081 || CHARPOS (startp) == BEGV))
15083 eassert (row->enabled_p);
15084 --row;
15087 /* Consider the following case: Window starts at BEGV,
15088 there is invisible, intangible text at BEGV, so that
15089 display starts at some point START > BEGV. It can
15090 happen that we are called with PT somewhere between
15091 BEGV and START. Try to handle that case. */
15092 if (row < w->current_matrix->rows
15093 || row->mode_line_p)
15095 row = w->current_matrix->rows;
15096 if (row->mode_line_p)
15097 ++row;
15100 /* Due to newlines in overlay strings, we may have to
15101 skip forward over overlay strings. */
15102 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15103 && MATRIX_ROW_END_CHARPOS (row) == PT
15104 && !cursor_row_p (row))
15105 ++row;
15107 /* If within the scroll margin, scroll. */
15108 if (row->y < top_scroll_margin
15109 && CHARPOS (startp) != BEGV)
15110 scroll_p = 1;
15112 else
15114 /* Cursor did not move. So don't scroll even if cursor line
15115 is partially visible, as it was so before. */
15116 rc = CURSOR_MOVEMENT_SUCCESS;
15119 if (PT < MATRIX_ROW_START_CHARPOS (row)
15120 || PT > MATRIX_ROW_END_CHARPOS (row))
15122 /* if PT is not in the glyph row, give up. */
15123 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15124 must_scroll = 1;
15126 else if (rc != CURSOR_MOVEMENT_SUCCESS
15127 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15129 struct glyph_row *row1;
15131 /* If rows are bidi-reordered and point moved, back up
15132 until we find a row that does not belong to a
15133 continuation line. This is because we must consider
15134 all rows of a continued line as candidates for the
15135 new cursor positioning, since row start and end
15136 positions change non-linearly with vertical position
15137 in such rows. */
15138 /* FIXME: Revisit this when glyph ``spilling'' in
15139 continuation lines' rows is implemented for
15140 bidi-reordered rows. */
15141 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15142 MATRIX_ROW_CONTINUATION_LINE_P (row);
15143 --row)
15145 /* If we hit the beginning of the displayed portion
15146 without finding the first row of a continued
15147 line, give up. */
15148 if (row <= row1)
15150 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15151 break;
15153 eassert (row->enabled_p);
15156 if (must_scroll)
15158 else if (rc != CURSOR_MOVEMENT_SUCCESS
15159 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15160 /* Make sure this isn't a header line by any chance, since
15161 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15162 && !row->mode_line_p
15163 && make_cursor_line_fully_visible_p)
15165 if (PT == MATRIX_ROW_END_CHARPOS (row)
15166 && !row->ends_at_zv_p
15167 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15168 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15169 else if (row->height > window_box_height (w))
15171 /* If we end up in a partially visible line, let's
15172 make it fully visible, except when it's taller
15173 than the window, in which case we can't do much
15174 about it. */
15175 *scroll_step = 1;
15176 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15178 else
15180 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15181 if (!cursor_row_fully_visible_p (w, 0, 1))
15182 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15183 else
15184 rc = CURSOR_MOVEMENT_SUCCESS;
15187 else if (scroll_p)
15188 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15189 else if (rc != CURSOR_MOVEMENT_SUCCESS
15190 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15192 /* With bidi-reordered rows, there could be more than
15193 one candidate row whose start and end positions
15194 occlude point. We need to let set_cursor_from_row
15195 find the best candidate. */
15196 /* FIXME: Revisit this when glyph ``spilling'' in
15197 continuation lines' rows is implemented for
15198 bidi-reordered rows. */
15199 int rv = 0;
15203 int at_zv_p = 0, exact_match_p = 0;
15205 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15206 && PT <= MATRIX_ROW_END_CHARPOS (row)
15207 && cursor_row_p (row))
15208 rv |= set_cursor_from_row (w, row, w->current_matrix,
15209 0, 0, 0, 0);
15210 /* As soon as we've found the exact match for point,
15211 or the first suitable row whose ends_at_zv_p flag
15212 is set, we are done. */
15213 at_zv_p =
15214 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15215 if (rv && !at_zv_p
15216 && w->cursor.hpos >= 0
15217 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15218 w->cursor.vpos))
15220 struct glyph_row *candidate =
15221 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15222 struct glyph *g =
15223 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15224 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15226 exact_match_p =
15227 (BUFFERP (g->object) && g->charpos == PT)
15228 || (INTEGERP (g->object)
15229 && (g->charpos == PT
15230 || (g->charpos == 0 && endpos - 1 == PT)));
15232 if (rv && (at_zv_p || exact_match_p))
15234 rc = CURSOR_MOVEMENT_SUCCESS;
15235 break;
15237 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15238 break;
15239 ++row;
15241 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15242 || row->continued_p)
15243 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15244 || (MATRIX_ROW_START_CHARPOS (row) == PT
15245 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15246 /* If we didn't find any candidate rows, or exited the
15247 loop before all the candidates were examined, signal
15248 to the caller that this method failed. */
15249 if (rc != CURSOR_MOVEMENT_SUCCESS
15250 && !(rv
15251 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15252 && !row->continued_p))
15253 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15254 else if (rv)
15255 rc = CURSOR_MOVEMENT_SUCCESS;
15257 else
15261 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15263 rc = CURSOR_MOVEMENT_SUCCESS;
15264 break;
15266 ++row;
15268 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15269 && MATRIX_ROW_START_CHARPOS (row) == PT
15270 && cursor_row_p (row));
15275 return rc;
15278 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15279 static
15280 #endif
15281 void
15282 set_vertical_scroll_bar (struct window *w)
15284 ptrdiff_t start, end, whole;
15286 /* Calculate the start and end positions for the current window.
15287 At some point, it would be nice to choose between scrollbars
15288 which reflect the whole buffer size, with special markers
15289 indicating narrowing, and scrollbars which reflect only the
15290 visible region.
15292 Note that mini-buffers sometimes aren't displaying any text. */
15293 if (!MINI_WINDOW_P (w)
15294 || (w == XWINDOW (minibuf_window)
15295 && NILP (echo_area_buffer[0])))
15297 struct buffer *buf = XBUFFER (w->contents);
15298 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15299 start = marker_position (w->start) - BUF_BEGV (buf);
15300 /* I don't think this is guaranteed to be right. For the
15301 moment, we'll pretend it is. */
15302 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15304 if (end < start)
15305 end = start;
15306 if (whole < (end - start))
15307 whole = end - start;
15309 else
15310 start = end = whole = 0;
15312 /* Indicate what this scroll bar ought to be displaying now. */
15313 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15314 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15315 (w, end - start, whole, start);
15319 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15320 selected_window is redisplayed.
15322 We can return without actually redisplaying the window if
15323 fonts_changed_p. In that case, redisplay_internal will
15324 retry. */
15326 static void
15327 redisplay_window (Lisp_Object window, int just_this_one_p)
15329 struct window *w = XWINDOW (window);
15330 struct frame *f = XFRAME (w->frame);
15331 struct buffer *buffer = XBUFFER (w->contents);
15332 struct buffer *old = current_buffer;
15333 struct text_pos lpoint, opoint, startp;
15334 int update_mode_line;
15335 int tem;
15336 struct it it;
15337 /* Record it now because it's overwritten. */
15338 int current_matrix_up_to_date_p = 0;
15339 int used_current_matrix_p = 0;
15340 /* This is less strict than current_matrix_up_to_date_p.
15341 It indicates that the buffer contents and narrowing are unchanged. */
15342 int buffer_unchanged_p = 0;
15343 int temp_scroll_step = 0;
15344 ptrdiff_t count = SPECPDL_INDEX ();
15345 int rc;
15346 int centering_position = -1;
15347 int last_line_misfit = 0;
15348 ptrdiff_t beg_unchanged, end_unchanged;
15349 int frame_line_height;
15351 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15352 opoint = lpoint;
15354 #ifdef GLYPH_DEBUG
15355 *w->desired_matrix->method = 0;
15356 #endif
15358 /* Make sure that both W's markers are valid. */
15359 eassert (XMARKER (w->start)->buffer == buffer);
15360 eassert (XMARKER (w->pointm)->buffer == buffer);
15362 restart:
15363 reconsider_clip_changes (w, buffer);
15364 frame_line_height = default_line_pixel_height (w);
15366 /* Has the mode line to be updated? */
15367 update_mode_line = (w->update_mode_line
15368 || update_mode_lines
15369 || buffer->clip_changed
15370 || buffer->prevent_redisplay_optimizations_p);
15372 if (MINI_WINDOW_P (w))
15374 if (w == XWINDOW (echo_area_window)
15375 && !NILP (echo_area_buffer[0]))
15377 if (update_mode_line)
15378 /* We may have to update a tty frame's menu bar or a
15379 tool-bar. Example `M-x C-h C-h C-g'. */
15380 goto finish_menu_bars;
15381 else
15382 /* We've already displayed the echo area glyphs in this window. */
15383 goto finish_scroll_bars;
15385 else if ((w != XWINDOW (minibuf_window)
15386 || minibuf_level == 0)
15387 /* When buffer is nonempty, redisplay window normally. */
15388 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15389 /* Quail displays non-mini buffers in minibuffer window.
15390 In that case, redisplay the window normally. */
15391 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15393 /* W is a mini-buffer window, but it's not active, so clear
15394 it. */
15395 int yb = window_text_bottom_y (w);
15396 struct glyph_row *row;
15397 int y;
15399 for (y = 0, row = w->desired_matrix->rows;
15400 y < yb;
15401 y += row->height, ++row)
15402 blank_row (w, row, y);
15403 goto finish_scroll_bars;
15406 clear_glyph_matrix (w->desired_matrix);
15409 /* Otherwise set up data on this window; select its buffer and point
15410 value. */
15411 /* Really select the buffer, for the sake of buffer-local
15412 variables. */
15413 set_buffer_internal_1 (XBUFFER (w->contents));
15415 current_matrix_up_to_date_p
15416 = (w->window_end_valid
15417 && !current_buffer->clip_changed
15418 && !current_buffer->prevent_redisplay_optimizations_p
15419 && !window_outdated (w));
15421 /* Run the window-bottom-change-functions
15422 if it is possible that the text on the screen has changed
15423 (either due to modification of the text, or any other reason). */
15424 if (!current_matrix_up_to_date_p
15425 && !NILP (Vwindow_text_change_functions))
15427 safe_run_hooks (Qwindow_text_change_functions);
15428 goto restart;
15431 beg_unchanged = BEG_UNCHANGED;
15432 end_unchanged = END_UNCHANGED;
15434 SET_TEXT_POS (opoint, PT, PT_BYTE);
15436 specbind (Qinhibit_point_motion_hooks, Qt);
15438 buffer_unchanged_p
15439 = (w->window_end_valid
15440 && !current_buffer->clip_changed
15441 && !window_outdated (w));
15443 /* When windows_or_buffers_changed is non-zero, we can't rely on
15444 the window end being valid, so set it to nil there. */
15445 if (windows_or_buffers_changed)
15447 /* If window starts on a continuation line, maybe adjust the
15448 window start in case the window's width changed. */
15449 if (XMARKER (w->start)->buffer == current_buffer)
15450 compute_window_start_on_continuation_line (w);
15452 w->window_end_valid = 0;
15455 /* Some sanity checks. */
15456 CHECK_WINDOW_END (w);
15457 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15458 emacs_abort ();
15459 if (BYTEPOS (opoint) < CHARPOS (opoint))
15460 emacs_abort ();
15462 if (mode_line_update_needed (w))
15463 update_mode_line = 1;
15465 /* Point refers normally to the selected window. For any other
15466 window, set up appropriate value. */
15467 if (!EQ (window, selected_window))
15469 ptrdiff_t new_pt = marker_position (w->pointm);
15470 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15471 if (new_pt < BEGV)
15473 new_pt = BEGV;
15474 new_pt_byte = BEGV_BYTE;
15475 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15477 else if (new_pt > (ZV - 1))
15479 new_pt = ZV;
15480 new_pt_byte = ZV_BYTE;
15481 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15484 /* We don't use SET_PT so that the point-motion hooks don't run. */
15485 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15488 /* If any of the character widths specified in the display table
15489 have changed, invalidate the width run cache. It's true that
15490 this may be a bit late to catch such changes, but the rest of
15491 redisplay goes (non-fatally) haywire when the display table is
15492 changed, so why should we worry about doing any better? */
15493 if (current_buffer->width_run_cache)
15495 struct Lisp_Char_Table *disptab = buffer_display_table ();
15497 if (! disptab_matches_widthtab
15498 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15500 invalidate_region_cache (current_buffer,
15501 current_buffer->width_run_cache,
15502 BEG, Z);
15503 recompute_width_table (current_buffer, disptab);
15507 /* If window-start is screwed up, choose a new one. */
15508 if (XMARKER (w->start)->buffer != current_buffer)
15509 goto recenter;
15511 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15513 /* If someone specified a new starting point but did not insist,
15514 check whether it can be used. */
15515 if (w->optional_new_start
15516 && CHARPOS (startp) >= BEGV
15517 && CHARPOS (startp) <= ZV)
15519 w->optional_new_start = 0;
15520 start_display (&it, w, startp);
15521 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15522 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15523 if (IT_CHARPOS (it) == PT)
15524 w->force_start = 1;
15525 /* IT may overshoot PT if text at PT is invisible. */
15526 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15527 w->force_start = 1;
15530 force_start:
15532 /* Handle case where place to start displaying has been specified,
15533 unless the specified location is outside the accessible range. */
15534 if (w->force_start || w->frozen_window_start_p)
15536 /* We set this later on if we have to adjust point. */
15537 int new_vpos = -1;
15539 w->force_start = 0;
15540 w->vscroll = 0;
15541 w->window_end_valid = 0;
15543 /* Forget any recorded base line for line number display. */
15544 if (!buffer_unchanged_p)
15545 w->base_line_number = 0;
15547 /* Redisplay the mode line. Select the buffer properly for that.
15548 Also, run the hook window-scroll-functions
15549 because we have scrolled. */
15550 /* Note, we do this after clearing force_start because
15551 if there's an error, it is better to forget about force_start
15552 than to get into an infinite loop calling the hook functions
15553 and having them get more errors. */
15554 if (!update_mode_line
15555 || ! NILP (Vwindow_scroll_functions))
15557 update_mode_line = 1;
15558 w->update_mode_line = 1;
15559 startp = run_window_scroll_functions (window, startp);
15562 w->last_modified = 0;
15563 w->last_overlay_modified = 0;
15564 if (CHARPOS (startp) < BEGV)
15565 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15566 else if (CHARPOS (startp) > ZV)
15567 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15569 /* Redisplay, then check if cursor has been set during the
15570 redisplay. Give up if new fonts were loaded. */
15571 /* We used to issue a CHECK_MARGINS argument to try_window here,
15572 but this causes scrolling to fail when point begins inside
15573 the scroll margin (bug#148) -- cyd */
15574 if (!try_window (window, startp, 0))
15576 w->force_start = 1;
15577 clear_glyph_matrix (w->desired_matrix);
15578 goto need_larger_matrices;
15581 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15583 /* If point does not appear, try to move point so it does
15584 appear. The desired matrix has been built above, so we
15585 can use it here. */
15586 new_vpos = window_box_height (w) / 2;
15589 if (!cursor_row_fully_visible_p (w, 0, 0))
15591 /* Point does appear, but on a line partly visible at end of window.
15592 Move it back to a fully-visible line. */
15593 new_vpos = window_box_height (w);
15595 else if (w->cursor.vpos >=0)
15597 /* Some people insist on not letting point enter the scroll
15598 margin, even though this part handles windows that didn't
15599 scroll at all. */
15600 int window_total_lines
15601 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15602 int margin = min (scroll_margin, window_total_lines / 4);
15603 int pixel_margin = margin * frame_line_height;
15604 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15606 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15607 below, which finds the row to move point to, advances by
15608 the Y coordinate of the _next_ row, see the definition of
15609 MATRIX_ROW_BOTTOM_Y. */
15610 if (w->cursor.vpos < margin + header_line)
15611 new_vpos
15612 = pixel_margin + (header_line
15613 ? CURRENT_HEADER_LINE_HEIGHT (w)
15614 : 0) + frame_line_height;
15615 else
15617 int window_height = window_box_height (w);
15619 if (header_line)
15620 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15621 if (w->cursor.y >= window_height - pixel_margin)
15622 new_vpos = window_height - pixel_margin;
15626 /* If we need to move point for either of the above reasons,
15627 now actually do it. */
15628 if (new_vpos >= 0)
15630 struct glyph_row *row;
15632 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15633 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15634 ++row;
15636 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15637 MATRIX_ROW_START_BYTEPOS (row));
15639 if (w != XWINDOW (selected_window))
15640 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15641 else if (current_buffer == old)
15642 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15644 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15646 /* If we are highlighting the region, then we just changed
15647 the region, so redisplay to show it. */
15648 if (markpos_of_region () >= 0)
15650 clear_glyph_matrix (w->desired_matrix);
15651 if (!try_window (window, startp, 0))
15652 goto need_larger_matrices;
15656 #ifdef GLYPH_DEBUG
15657 debug_method_add (w, "forced window start");
15658 #endif
15659 goto done;
15662 /* Handle case where text has not changed, only point, and it has
15663 not moved off the frame, and we are not retrying after hscroll.
15664 (current_matrix_up_to_date_p is nonzero when retrying.) */
15665 if (current_matrix_up_to_date_p
15666 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15667 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15669 switch (rc)
15671 case CURSOR_MOVEMENT_SUCCESS:
15672 used_current_matrix_p = 1;
15673 goto done;
15675 case CURSOR_MOVEMENT_MUST_SCROLL:
15676 goto try_to_scroll;
15678 default:
15679 emacs_abort ();
15682 /* If current starting point was originally the beginning of a line
15683 but no longer is, find a new starting point. */
15684 else if (w->start_at_line_beg
15685 && !(CHARPOS (startp) <= BEGV
15686 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15688 #ifdef GLYPH_DEBUG
15689 debug_method_add (w, "recenter 1");
15690 #endif
15691 goto recenter;
15694 /* Try scrolling with try_window_id. Value is > 0 if update has
15695 been done, it is -1 if we know that the same window start will
15696 not work. It is 0 if unsuccessful for some other reason. */
15697 else if ((tem = try_window_id (w)) != 0)
15699 #ifdef GLYPH_DEBUG
15700 debug_method_add (w, "try_window_id %d", tem);
15701 #endif
15703 if (fonts_changed_p)
15704 goto need_larger_matrices;
15705 if (tem > 0)
15706 goto done;
15708 /* Otherwise try_window_id has returned -1 which means that we
15709 don't want the alternative below this comment to execute. */
15711 else if (CHARPOS (startp) >= BEGV
15712 && CHARPOS (startp) <= ZV
15713 && PT >= CHARPOS (startp)
15714 && (CHARPOS (startp) < ZV
15715 /* Avoid starting at end of buffer. */
15716 || CHARPOS (startp) == BEGV
15717 || !window_outdated (w)))
15719 int d1, d2, d3, d4, d5, d6;
15721 /* If first window line is a continuation line, and window start
15722 is inside the modified region, but the first change is before
15723 current window start, we must select a new window start.
15725 However, if this is the result of a down-mouse event (e.g. by
15726 extending the mouse-drag-overlay), we don't want to select a
15727 new window start, since that would change the position under
15728 the mouse, resulting in an unwanted mouse-movement rather
15729 than a simple mouse-click. */
15730 if (!w->start_at_line_beg
15731 && NILP (do_mouse_tracking)
15732 && CHARPOS (startp) > BEGV
15733 && CHARPOS (startp) > BEG + beg_unchanged
15734 && CHARPOS (startp) <= Z - end_unchanged
15735 /* Even if w->start_at_line_beg is nil, a new window may
15736 start at a line_beg, since that's how set_buffer_window
15737 sets it. So, we need to check the return value of
15738 compute_window_start_on_continuation_line. (See also
15739 bug#197). */
15740 && XMARKER (w->start)->buffer == current_buffer
15741 && compute_window_start_on_continuation_line (w)
15742 /* It doesn't make sense to force the window start like we
15743 do at label force_start if it is already known that point
15744 will not be visible in the resulting window, because
15745 doing so will move point from its correct position
15746 instead of scrolling the window to bring point into view.
15747 See bug#9324. */
15748 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15750 w->force_start = 1;
15751 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15752 goto force_start;
15755 #ifdef GLYPH_DEBUG
15756 debug_method_add (w, "same window start");
15757 #endif
15759 /* Try to redisplay starting at same place as before.
15760 If point has not moved off frame, accept the results. */
15761 if (!current_matrix_up_to_date_p
15762 /* Don't use try_window_reusing_current_matrix in this case
15763 because a window scroll function can have changed the
15764 buffer. */
15765 || !NILP (Vwindow_scroll_functions)
15766 || MINI_WINDOW_P (w)
15767 || !(used_current_matrix_p
15768 = try_window_reusing_current_matrix (w)))
15770 IF_DEBUG (debug_method_add (w, "1"));
15771 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15772 /* -1 means we need to scroll.
15773 0 means we need new matrices, but fonts_changed_p
15774 is set in that case, so we will detect it below. */
15775 goto try_to_scroll;
15778 if (fonts_changed_p)
15779 goto need_larger_matrices;
15781 if (w->cursor.vpos >= 0)
15783 if (!just_this_one_p
15784 || current_buffer->clip_changed
15785 || BEG_UNCHANGED < CHARPOS (startp))
15786 /* Forget any recorded base line for line number display. */
15787 w->base_line_number = 0;
15789 if (!cursor_row_fully_visible_p (w, 1, 0))
15791 clear_glyph_matrix (w->desired_matrix);
15792 last_line_misfit = 1;
15794 /* Drop through and scroll. */
15795 else
15796 goto done;
15798 else
15799 clear_glyph_matrix (w->desired_matrix);
15802 try_to_scroll:
15804 w->last_modified = 0;
15805 w->last_overlay_modified = 0;
15807 /* Redisplay the mode line. Select the buffer properly for that. */
15808 if (!update_mode_line)
15810 update_mode_line = 1;
15811 w->update_mode_line = 1;
15814 /* Try to scroll by specified few lines. */
15815 if ((scroll_conservatively
15816 || emacs_scroll_step
15817 || temp_scroll_step
15818 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15819 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15820 && CHARPOS (startp) >= BEGV
15821 && CHARPOS (startp) <= ZV)
15823 /* The function returns -1 if new fonts were loaded, 1 if
15824 successful, 0 if not successful. */
15825 int ss = try_scrolling (window, just_this_one_p,
15826 scroll_conservatively,
15827 emacs_scroll_step,
15828 temp_scroll_step, last_line_misfit);
15829 switch (ss)
15831 case SCROLLING_SUCCESS:
15832 goto done;
15834 case SCROLLING_NEED_LARGER_MATRICES:
15835 goto need_larger_matrices;
15837 case SCROLLING_FAILED:
15838 break;
15840 default:
15841 emacs_abort ();
15845 /* Finally, just choose a place to start which positions point
15846 according to user preferences. */
15848 recenter:
15850 #ifdef GLYPH_DEBUG
15851 debug_method_add (w, "recenter");
15852 #endif
15854 /* Forget any previously recorded base line for line number display. */
15855 if (!buffer_unchanged_p)
15856 w->base_line_number = 0;
15858 /* Determine the window start relative to point. */
15859 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15860 it.current_y = it.last_visible_y;
15861 if (centering_position < 0)
15863 int window_total_lines
15864 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15865 int margin =
15866 scroll_margin > 0
15867 ? min (scroll_margin, window_total_lines / 4)
15868 : 0;
15869 ptrdiff_t margin_pos = CHARPOS (startp);
15870 Lisp_Object aggressive;
15871 int scrolling_up;
15873 /* If there is a scroll margin at the top of the window, find
15874 its character position. */
15875 if (margin
15876 /* Cannot call start_display if startp is not in the
15877 accessible region of the buffer. This can happen when we
15878 have just switched to a different buffer and/or changed
15879 its restriction. In that case, startp is initialized to
15880 the character position 1 (BEGV) because we did not yet
15881 have chance to display the buffer even once. */
15882 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15884 struct it it1;
15885 void *it1data = NULL;
15887 SAVE_IT (it1, it, it1data);
15888 start_display (&it1, w, startp);
15889 move_it_vertically (&it1, margin * frame_line_height);
15890 margin_pos = IT_CHARPOS (it1);
15891 RESTORE_IT (&it, &it, it1data);
15893 scrolling_up = PT > margin_pos;
15894 aggressive =
15895 scrolling_up
15896 ? BVAR (current_buffer, scroll_up_aggressively)
15897 : BVAR (current_buffer, scroll_down_aggressively);
15899 if (!MINI_WINDOW_P (w)
15900 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15902 int pt_offset = 0;
15904 /* Setting scroll-conservatively overrides
15905 scroll-*-aggressively. */
15906 if (!scroll_conservatively && NUMBERP (aggressive))
15908 double float_amount = XFLOATINT (aggressive);
15910 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15911 if (pt_offset == 0 && float_amount > 0)
15912 pt_offset = 1;
15913 if (pt_offset && margin > 0)
15914 margin -= 1;
15916 /* Compute how much to move the window start backward from
15917 point so that point will be displayed where the user
15918 wants it. */
15919 if (scrolling_up)
15921 centering_position = it.last_visible_y;
15922 if (pt_offset)
15923 centering_position -= pt_offset;
15924 centering_position -=
15925 frame_line_height * (1 + margin + (last_line_misfit != 0))
15926 + WINDOW_HEADER_LINE_HEIGHT (w);
15927 /* Don't let point enter the scroll margin near top of
15928 the window. */
15929 if (centering_position < margin * frame_line_height)
15930 centering_position = margin * frame_line_height;
15932 else
15933 centering_position = margin * frame_line_height + pt_offset;
15935 else
15936 /* Set the window start half the height of the window backward
15937 from point. */
15938 centering_position = window_box_height (w) / 2;
15940 move_it_vertically_backward (&it, centering_position);
15942 eassert (IT_CHARPOS (it) >= BEGV);
15944 /* The function move_it_vertically_backward may move over more
15945 than the specified y-distance. If it->w is small, e.g. a
15946 mini-buffer window, we may end up in front of the window's
15947 display area. Start displaying at the start of the line
15948 containing PT in this case. */
15949 if (it.current_y <= 0)
15951 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15952 move_it_vertically_backward (&it, 0);
15953 it.current_y = 0;
15956 it.current_x = it.hpos = 0;
15958 /* Set the window start position here explicitly, to avoid an
15959 infinite loop in case the functions in window-scroll-functions
15960 get errors. */
15961 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15963 /* Run scroll hooks. */
15964 startp = run_window_scroll_functions (window, it.current.pos);
15966 /* Redisplay the window. */
15967 if (!current_matrix_up_to_date_p
15968 || windows_or_buffers_changed
15969 || cursor_type_changed
15970 /* Don't use try_window_reusing_current_matrix in this case
15971 because it can have changed the buffer. */
15972 || !NILP (Vwindow_scroll_functions)
15973 || !just_this_one_p
15974 || MINI_WINDOW_P (w)
15975 || !(used_current_matrix_p
15976 = try_window_reusing_current_matrix (w)))
15977 try_window (window, startp, 0);
15979 /* If new fonts have been loaded (due to fontsets), give up. We
15980 have to start a new redisplay since we need to re-adjust glyph
15981 matrices. */
15982 if (fonts_changed_p)
15983 goto need_larger_matrices;
15985 /* If cursor did not appear assume that the middle of the window is
15986 in the first line of the window. Do it again with the next line.
15987 (Imagine a window of height 100, displaying two lines of height
15988 60. Moving back 50 from it->last_visible_y will end in the first
15989 line.) */
15990 if (w->cursor.vpos < 0)
15992 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
15994 clear_glyph_matrix (w->desired_matrix);
15995 move_it_by_lines (&it, 1);
15996 try_window (window, it.current.pos, 0);
15998 else if (PT < IT_CHARPOS (it))
16000 clear_glyph_matrix (w->desired_matrix);
16001 move_it_by_lines (&it, -1);
16002 try_window (window, it.current.pos, 0);
16004 else
16006 /* Not much we can do about it. */
16010 /* Consider the following case: Window starts at BEGV, there is
16011 invisible, intangible text at BEGV, so that display starts at
16012 some point START > BEGV. It can happen that we are called with
16013 PT somewhere between BEGV and START. Try to handle that case. */
16014 if (w->cursor.vpos < 0)
16016 struct glyph_row *row = w->current_matrix->rows;
16017 if (row->mode_line_p)
16018 ++row;
16019 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16022 if (!cursor_row_fully_visible_p (w, 0, 0))
16024 /* If vscroll is enabled, disable it and try again. */
16025 if (w->vscroll)
16027 w->vscroll = 0;
16028 clear_glyph_matrix (w->desired_matrix);
16029 goto recenter;
16032 /* Users who set scroll-conservatively to a large number want
16033 point just above/below the scroll margin. If we ended up
16034 with point's row partially visible, move the window start to
16035 make that row fully visible and out of the margin. */
16036 if (scroll_conservatively > SCROLL_LIMIT)
16038 int window_total_lines
16039 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16040 int margin =
16041 scroll_margin > 0
16042 ? min (scroll_margin, window_total_lines / 4)
16043 : 0;
16044 int move_down = w->cursor.vpos >= window_total_lines / 2;
16046 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16047 clear_glyph_matrix (w->desired_matrix);
16048 if (1 == try_window (window, it.current.pos,
16049 TRY_WINDOW_CHECK_MARGINS))
16050 goto done;
16053 /* If centering point failed to make the whole line visible,
16054 put point at the top instead. That has to make the whole line
16055 visible, if it can be done. */
16056 if (centering_position == 0)
16057 goto done;
16059 clear_glyph_matrix (w->desired_matrix);
16060 centering_position = 0;
16061 goto recenter;
16064 done:
16066 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16067 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16068 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16070 /* Display the mode line, if we must. */
16071 if ((update_mode_line
16072 /* If window not full width, must redo its mode line
16073 if (a) the window to its side is being redone and
16074 (b) we do a frame-based redisplay. This is a consequence
16075 of how inverted lines are drawn in frame-based redisplay. */
16076 || (!just_this_one_p
16077 && !FRAME_WINDOW_P (f)
16078 && !WINDOW_FULL_WIDTH_P (w))
16079 /* Line number to display. */
16080 || w->base_line_pos > 0
16081 /* Column number is displayed and different from the one displayed. */
16082 || (w->column_number_displayed != -1
16083 && (w->column_number_displayed != current_column ())))
16084 /* This means that the window has a mode line. */
16085 && (WINDOW_WANTS_MODELINE_P (w)
16086 || WINDOW_WANTS_HEADER_LINE_P (w)))
16088 display_mode_lines (w);
16090 /* If mode line height has changed, arrange for a thorough
16091 immediate redisplay using the correct mode line height. */
16092 if (WINDOW_WANTS_MODELINE_P (w)
16093 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16095 fonts_changed_p = 1;
16096 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16097 = DESIRED_MODE_LINE_HEIGHT (w);
16100 /* If header line height has changed, arrange for a thorough
16101 immediate redisplay using the correct header line height. */
16102 if (WINDOW_WANTS_HEADER_LINE_P (w)
16103 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16105 fonts_changed_p = 1;
16106 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16107 = DESIRED_HEADER_LINE_HEIGHT (w);
16110 if (fonts_changed_p)
16111 goto need_larger_matrices;
16114 if (!line_number_displayed && w->base_line_pos != -1)
16116 w->base_line_pos = 0;
16117 w->base_line_number = 0;
16120 finish_menu_bars:
16122 /* When we reach a frame's selected window, redo the frame's menu bar. */
16123 if (update_mode_line
16124 && EQ (FRAME_SELECTED_WINDOW (f), window))
16126 int redisplay_menu_p = 0;
16128 if (FRAME_WINDOW_P (f))
16130 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16131 || defined (HAVE_NS) || defined (USE_GTK)
16132 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16133 #else
16134 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16135 #endif
16137 else
16138 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16140 if (redisplay_menu_p)
16141 display_menu_bar (w);
16143 #ifdef HAVE_WINDOW_SYSTEM
16144 if (FRAME_WINDOW_P (f))
16146 #if defined (USE_GTK) || defined (HAVE_NS)
16147 if (FRAME_EXTERNAL_TOOL_BAR (f))
16148 redisplay_tool_bar (f);
16149 #else
16150 if (WINDOWP (f->tool_bar_window)
16151 && (FRAME_TOOL_BAR_LINES (f) > 0
16152 || !NILP (Vauto_resize_tool_bars))
16153 && redisplay_tool_bar (f))
16154 ignore_mouse_drag_p = 1;
16155 #endif
16157 #endif
16160 #ifdef HAVE_WINDOW_SYSTEM
16161 if (FRAME_WINDOW_P (f)
16162 && update_window_fringes (w, (just_this_one_p
16163 || (!used_current_matrix_p && !overlay_arrow_seen)
16164 || w->pseudo_window_p)))
16166 update_begin (f);
16167 block_input ();
16168 if (draw_window_fringes (w, 1))
16169 x_draw_vertical_border (w);
16170 unblock_input ();
16171 update_end (f);
16173 #endif /* HAVE_WINDOW_SYSTEM */
16175 /* We go to this label, with fonts_changed_p set,
16176 if it is necessary to try again using larger glyph matrices.
16177 We have to redeem the scroll bar even in this case,
16178 because the loop in redisplay_internal expects that. */
16179 need_larger_matrices:
16181 finish_scroll_bars:
16183 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16185 /* Set the thumb's position and size. */
16186 set_vertical_scroll_bar (w);
16188 /* Note that we actually used the scroll bar attached to this
16189 window, so it shouldn't be deleted at the end of redisplay. */
16190 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16191 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16194 /* Restore current_buffer and value of point in it. The window
16195 update may have changed the buffer, so first make sure `opoint'
16196 is still valid (Bug#6177). */
16197 if (CHARPOS (opoint) < BEGV)
16198 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16199 else if (CHARPOS (opoint) > ZV)
16200 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16201 else
16202 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16204 set_buffer_internal_1 (old);
16205 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16206 shorter. This can be caused by log truncation in *Messages*. */
16207 if (CHARPOS (lpoint) <= ZV)
16208 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16210 unbind_to (count, Qnil);
16214 /* Build the complete desired matrix of WINDOW with a window start
16215 buffer position POS.
16217 Value is 1 if successful. It is zero if fonts were loaded during
16218 redisplay which makes re-adjusting glyph matrices necessary, and -1
16219 if point would appear in the scroll margins.
16220 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16221 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16222 set in FLAGS.) */
16225 try_window (Lisp_Object window, struct text_pos pos, int flags)
16227 struct window *w = XWINDOW (window);
16228 struct it it;
16229 struct glyph_row *last_text_row = NULL;
16230 struct frame *f = XFRAME (w->frame);
16231 int frame_line_height = default_line_pixel_height (w);
16233 /* Make POS the new window start. */
16234 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16236 /* Mark cursor position as unknown. No overlay arrow seen. */
16237 w->cursor.vpos = -1;
16238 overlay_arrow_seen = 0;
16240 /* Initialize iterator and info to start at POS. */
16241 start_display (&it, w, pos);
16243 /* Display all lines of W. */
16244 while (it.current_y < it.last_visible_y)
16246 if (display_line (&it))
16247 last_text_row = it.glyph_row - 1;
16248 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16249 return 0;
16252 /* Don't let the cursor end in the scroll margins. */
16253 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16254 && !MINI_WINDOW_P (w))
16256 int this_scroll_margin;
16257 int window_total_lines
16258 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16260 if (scroll_margin > 0)
16262 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16263 this_scroll_margin *= frame_line_height;
16265 else
16266 this_scroll_margin = 0;
16268 if ((w->cursor.y >= 0 /* not vscrolled */
16269 && w->cursor.y < this_scroll_margin
16270 && CHARPOS (pos) > BEGV
16271 && IT_CHARPOS (it) < ZV)
16272 /* rms: considering make_cursor_line_fully_visible_p here
16273 seems to give wrong results. We don't want to recenter
16274 when the last line is partly visible, we want to allow
16275 that case to be handled in the usual way. */
16276 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16278 w->cursor.vpos = -1;
16279 clear_glyph_matrix (w->desired_matrix);
16280 return -1;
16284 /* If bottom moved off end of frame, change mode line percentage. */
16285 if (XFASTINT (w->window_end_pos) <= 0
16286 && Z != IT_CHARPOS (it))
16287 w->update_mode_line = 1;
16289 /* Set window_end_pos to the offset of the last character displayed
16290 on the window from the end of current_buffer. Set
16291 window_end_vpos to its row number. */
16292 if (last_text_row)
16294 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16295 w->window_end_bytepos
16296 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16297 wset_window_end_pos
16298 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16299 wset_window_end_vpos
16300 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16301 eassert
16302 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16303 XFASTINT (w->window_end_vpos))));
16305 else
16307 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16308 wset_window_end_pos (w, make_number (Z - ZV));
16309 wset_window_end_vpos (w, make_number (0));
16312 /* But that is not valid info until redisplay finishes. */
16313 w->window_end_valid = 0;
16314 return 1;
16319 /************************************************************************
16320 Window redisplay reusing current matrix when buffer has not changed
16321 ************************************************************************/
16323 /* Try redisplay of window W showing an unchanged buffer with a
16324 different window start than the last time it was displayed by
16325 reusing its current matrix. Value is non-zero if successful.
16326 W->start is the new window start. */
16328 static int
16329 try_window_reusing_current_matrix (struct window *w)
16331 struct frame *f = XFRAME (w->frame);
16332 struct glyph_row *bottom_row;
16333 struct it it;
16334 struct run run;
16335 struct text_pos start, new_start;
16336 int nrows_scrolled, i;
16337 struct glyph_row *last_text_row;
16338 struct glyph_row *last_reused_text_row;
16339 struct glyph_row *start_row;
16340 int start_vpos, min_y, max_y;
16342 #ifdef GLYPH_DEBUG
16343 if (inhibit_try_window_reusing)
16344 return 0;
16345 #endif
16347 if (/* This function doesn't handle terminal frames. */
16348 !FRAME_WINDOW_P (f)
16349 /* Don't try to reuse the display if windows have been split
16350 or such. */
16351 || windows_or_buffers_changed
16352 || cursor_type_changed)
16353 return 0;
16355 /* Can't do this if region may have changed. */
16356 if (markpos_of_region () >= 0
16357 || w->region_showing
16358 || !NILP (Vshow_trailing_whitespace))
16359 return 0;
16361 /* If top-line visibility has changed, give up. */
16362 if (WINDOW_WANTS_HEADER_LINE_P (w)
16363 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16364 return 0;
16366 /* Give up if old or new display is scrolled vertically. We could
16367 make this function handle this, but right now it doesn't. */
16368 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16369 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16370 return 0;
16372 /* The variable new_start now holds the new window start. The old
16373 start `start' can be determined from the current matrix. */
16374 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16375 start = start_row->minpos;
16376 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16378 /* Clear the desired matrix for the display below. */
16379 clear_glyph_matrix (w->desired_matrix);
16381 if (CHARPOS (new_start) <= CHARPOS (start))
16383 /* Don't use this method if the display starts with an ellipsis
16384 displayed for invisible text. It's not easy to handle that case
16385 below, and it's certainly not worth the effort since this is
16386 not a frequent case. */
16387 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16388 return 0;
16390 IF_DEBUG (debug_method_add (w, "twu1"));
16392 /* Display up to a row that can be reused. The variable
16393 last_text_row is set to the last row displayed that displays
16394 text. Note that it.vpos == 0 if or if not there is a
16395 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16396 start_display (&it, w, new_start);
16397 w->cursor.vpos = -1;
16398 last_text_row = last_reused_text_row = NULL;
16400 while (it.current_y < it.last_visible_y
16401 && !fonts_changed_p)
16403 /* If we have reached into the characters in the START row,
16404 that means the line boundaries have changed. So we
16405 can't start copying with the row START. Maybe it will
16406 work to start copying with the following row. */
16407 while (IT_CHARPOS (it) > CHARPOS (start))
16409 /* Advance to the next row as the "start". */
16410 start_row++;
16411 start = start_row->minpos;
16412 /* If there are no more rows to try, or just one, give up. */
16413 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16414 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16415 || CHARPOS (start) == ZV)
16417 clear_glyph_matrix (w->desired_matrix);
16418 return 0;
16421 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16423 /* If we have reached alignment, we can copy the rest of the
16424 rows. */
16425 if (IT_CHARPOS (it) == CHARPOS (start)
16426 /* Don't accept "alignment" inside a display vector,
16427 since start_row could have started in the middle of
16428 that same display vector (thus their character
16429 positions match), and we have no way of telling if
16430 that is the case. */
16431 && it.current.dpvec_index < 0)
16432 break;
16434 if (display_line (&it))
16435 last_text_row = it.glyph_row - 1;
16439 /* A value of current_y < last_visible_y means that we stopped
16440 at the previous window start, which in turn means that we
16441 have at least one reusable row. */
16442 if (it.current_y < it.last_visible_y)
16444 struct glyph_row *row;
16446 /* IT.vpos always starts from 0; it counts text lines. */
16447 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16449 /* Find PT if not already found in the lines displayed. */
16450 if (w->cursor.vpos < 0)
16452 int dy = it.current_y - start_row->y;
16454 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16455 row = row_containing_pos (w, PT, row, NULL, dy);
16456 if (row)
16457 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16458 dy, nrows_scrolled);
16459 else
16461 clear_glyph_matrix (w->desired_matrix);
16462 return 0;
16466 /* Scroll the display. Do it before the current matrix is
16467 changed. The problem here is that update has not yet
16468 run, i.e. part of the current matrix is not up to date.
16469 scroll_run_hook will clear the cursor, and use the
16470 current matrix to get the height of the row the cursor is
16471 in. */
16472 run.current_y = start_row->y;
16473 run.desired_y = it.current_y;
16474 run.height = it.last_visible_y - it.current_y;
16476 if (run.height > 0 && run.current_y != run.desired_y)
16478 update_begin (f);
16479 FRAME_RIF (f)->update_window_begin_hook (w);
16480 FRAME_RIF (f)->clear_window_mouse_face (w);
16481 FRAME_RIF (f)->scroll_run_hook (w, &run);
16482 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16483 update_end (f);
16486 /* Shift current matrix down by nrows_scrolled lines. */
16487 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16488 rotate_matrix (w->current_matrix,
16489 start_vpos,
16490 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16491 nrows_scrolled);
16493 /* Disable lines that must be updated. */
16494 for (i = 0; i < nrows_scrolled; ++i)
16495 (start_row + i)->enabled_p = 0;
16497 /* Re-compute Y positions. */
16498 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16499 max_y = it.last_visible_y;
16500 for (row = start_row + nrows_scrolled;
16501 row < bottom_row;
16502 ++row)
16504 row->y = it.current_y;
16505 row->visible_height = row->height;
16507 if (row->y < min_y)
16508 row->visible_height -= min_y - row->y;
16509 if (row->y + row->height > max_y)
16510 row->visible_height -= row->y + row->height - max_y;
16511 if (row->fringe_bitmap_periodic_p)
16512 row->redraw_fringe_bitmaps_p = 1;
16514 it.current_y += row->height;
16516 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16517 last_reused_text_row = row;
16518 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16519 break;
16522 /* Disable lines in the current matrix which are now
16523 below the window. */
16524 for (++row; row < bottom_row; ++row)
16525 row->enabled_p = row->mode_line_p = 0;
16528 /* Update window_end_pos etc.; last_reused_text_row is the last
16529 reused row from the current matrix containing text, if any.
16530 The value of last_text_row is the last displayed line
16531 containing text. */
16532 if (last_reused_text_row)
16534 w->window_end_bytepos
16535 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16536 wset_window_end_pos
16537 (w, make_number (Z
16538 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16539 wset_window_end_vpos
16540 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16541 w->current_matrix)));
16543 else if (last_text_row)
16545 w->window_end_bytepos
16546 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16547 wset_window_end_pos
16548 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16549 wset_window_end_vpos
16550 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16551 w->desired_matrix)));
16553 else
16555 /* This window must be completely empty. */
16556 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16557 wset_window_end_pos (w, make_number (Z - ZV));
16558 wset_window_end_vpos (w, make_number (0));
16560 w->window_end_valid = 0;
16562 /* Update hint: don't try scrolling again in update_window. */
16563 w->desired_matrix->no_scrolling_p = 1;
16565 #ifdef GLYPH_DEBUG
16566 debug_method_add (w, "try_window_reusing_current_matrix 1");
16567 #endif
16568 return 1;
16570 else if (CHARPOS (new_start) > CHARPOS (start))
16572 struct glyph_row *pt_row, *row;
16573 struct glyph_row *first_reusable_row;
16574 struct glyph_row *first_row_to_display;
16575 int dy;
16576 int yb = window_text_bottom_y (w);
16578 /* Find the row starting at new_start, if there is one. Don't
16579 reuse a partially visible line at the end. */
16580 first_reusable_row = start_row;
16581 while (first_reusable_row->enabled_p
16582 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16583 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16584 < CHARPOS (new_start)))
16585 ++first_reusable_row;
16587 /* Give up if there is no row to reuse. */
16588 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16589 || !first_reusable_row->enabled_p
16590 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16591 != CHARPOS (new_start)))
16592 return 0;
16594 /* We can reuse fully visible rows beginning with
16595 first_reusable_row to the end of the window. Set
16596 first_row_to_display to the first row that cannot be reused.
16597 Set pt_row to the row containing point, if there is any. */
16598 pt_row = NULL;
16599 for (first_row_to_display = first_reusable_row;
16600 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16601 ++first_row_to_display)
16603 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16604 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16605 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16606 && first_row_to_display->ends_at_zv_p
16607 && pt_row == NULL)))
16608 pt_row = first_row_to_display;
16611 /* Start displaying at the start of first_row_to_display. */
16612 eassert (first_row_to_display->y < yb);
16613 init_to_row_start (&it, w, first_row_to_display);
16615 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16616 - start_vpos);
16617 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16618 - nrows_scrolled);
16619 it.current_y = (first_row_to_display->y - first_reusable_row->y
16620 + WINDOW_HEADER_LINE_HEIGHT (w));
16622 /* Display lines beginning with first_row_to_display in the
16623 desired matrix. Set last_text_row to the last row displayed
16624 that displays text. */
16625 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16626 if (pt_row == NULL)
16627 w->cursor.vpos = -1;
16628 last_text_row = NULL;
16629 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16630 if (display_line (&it))
16631 last_text_row = it.glyph_row - 1;
16633 /* If point is in a reused row, adjust y and vpos of the cursor
16634 position. */
16635 if (pt_row)
16637 w->cursor.vpos -= nrows_scrolled;
16638 w->cursor.y -= first_reusable_row->y - start_row->y;
16641 /* Give up if point isn't in a row displayed or reused. (This
16642 also handles the case where w->cursor.vpos < nrows_scrolled
16643 after the calls to display_line, which can happen with scroll
16644 margins. See bug#1295.) */
16645 if (w->cursor.vpos < 0)
16647 clear_glyph_matrix (w->desired_matrix);
16648 return 0;
16651 /* Scroll the display. */
16652 run.current_y = first_reusable_row->y;
16653 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16654 run.height = it.last_visible_y - run.current_y;
16655 dy = run.current_y - run.desired_y;
16657 if (run.height)
16659 update_begin (f);
16660 FRAME_RIF (f)->update_window_begin_hook (w);
16661 FRAME_RIF (f)->clear_window_mouse_face (w);
16662 FRAME_RIF (f)->scroll_run_hook (w, &run);
16663 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16664 update_end (f);
16667 /* Adjust Y positions of reused rows. */
16668 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16669 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16670 max_y = it.last_visible_y;
16671 for (row = first_reusable_row; row < first_row_to_display; ++row)
16673 row->y -= dy;
16674 row->visible_height = row->height;
16675 if (row->y < min_y)
16676 row->visible_height -= min_y - row->y;
16677 if (row->y + row->height > max_y)
16678 row->visible_height -= row->y + row->height - max_y;
16679 if (row->fringe_bitmap_periodic_p)
16680 row->redraw_fringe_bitmaps_p = 1;
16683 /* Scroll the current matrix. */
16684 eassert (nrows_scrolled > 0);
16685 rotate_matrix (w->current_matrix,
16686 start_vpos,
16687 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16688 -nrows_scrolled);
16690 /* Disable rows not reused. */
16691 for (row -= nrows_scrolled; row < bottom_row; ++row)
16692 row->enabled_p = 0;
16694 /* Point may have moved to a different line, so we cannot assume that
16695 the previous cursor position is valid; locate the correct row. */
16696 if (pt_row)
16698 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16699 row < bottom_row
16700 && PT >= MATRIX_ROW_END_CHARPOS (row)
16701 && !row->ends_at_zv_p;
16702 row++)
16704 w->cursor.vpos++;
16705 w->cursor.y = row->y;
16707 if (row < bottom_row)
16709 /* Can't simply scan the row for point with
16710 bidi-reordered glyph rows. Let set_cursor_from_row
16711 figure out where to put the cursor, and if it fails,
16712 give up. */
16713 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16715 if (!set_cursor_from_row (w, row, w->current_matrix,
16716 0, 0, 0, 0))
16718 clear_glyph_matrix (w->desired_matrix);
16719 return 0;
16722 else
16724 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16725 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16727 for (; glyph < end
16728 && (!BUFFERP (glyph->object)
16729 || glyph->charpos < PT);
16730 glyph++)
16732 w->cursor.hpos++;
16733 w->cursor.x += glyph->pixel_width;
16739 /* Adjust window end. A null value of last_text_row means that
16740 the window end is in reused rows which in turn means that
16741 only its vpos can have changed. */
16742 if (last_text_row)
16744 w->window_end_bytepos
16745 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16746 wset_window_end_pos
16747 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16748 wset_window_end_vpos
16749 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16750 w->desired_matrix)));
16752 else
16754 wset_window_end_vpos
16755 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16758 w->window_end_valid = 0;
16759 w->desired_matrix->no_scrolling_p = 1;
16761 #ifdef GLYPH_DEBUG
16762 debug_method_add (w, "try_window_reusing_current_matrix 2");
16763 #endif
16764 return 1;
16767 return 0;
16772 /************************************************************************
16773 Window redisplay reusing current matrix when buffer has changed
16774 ************************************************************************/
16776 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16777 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16778 ptrdiff_t *, ptrdiff_t *);
16779 static struct glyph_row *
16780 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16781 struct glyph_row *);
16784 /* Return the last row in MATRIX displaying text. If row START is
16785 non-null, start searching with that row. IT gives the dimensions
16786 of the display. Value is null if matrix is empty; otherwise it is
16787 a pointer to the row found. */
16789 static struct glyph_row *
16790 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16791 struct glyph_row *start)
16793 struct glyph_row *row, *row_found;
16795 /* Set row_found to the last row in IT->w's current matrix
16796 displaying text. The loop looks funny but think of partially
16797 visible lines. */
16798 row_found = NULL;
16799 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16800 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16802 eassert (row->enabled_p);
16803 row_found = row;
16804 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16805 break;
16806 ++row;
16809 return row_found;
16813 /* Return the last row in the current matrix of W that is not affected
16814 by changes at the start of current_buffer that occurred since W's
16815 current matrix was built. Value is null if no such row exists.
16817 BEG_UNCHANGED us the number of characters unchanged at the start of
16818 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16819 first changed character in current_buffer. Characters at positions <
16820 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16821 when the current matrix was built. */
16823 static struct glyph_row *
16824 find_last_unchanged_at_beg_row (struct window *w)
16826 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16827 struct glyph_row *row;
16828 struct glyph_row *row_found = NULL;
16829 int yb = window_text_bottom_y (w);
16831 /* Find the last row displaying unchanged text. */
16832 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16833 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16834 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16835 ++row)
16837 if (/* If row ends before first_changed_pos, it is unchanged,
16838 except in some case. */
16839 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16840 /* When row ends in ZV and we write at ZV it is not
16841 unchanged. */
16842 && !row->ends_at_zv_p
16843 /* When first_changed_pos is the end of a continued line,
16844 row is not unchanged because it may be no longer
16845 continued. */
16846 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16847 && (row->continued_p
16848 || row->exact_window_width_line_p))
16849 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16850 needs to be recomputed, so don't consider this row as
16851 unchanged. This happens when the last line was
16852 bidi-reordered and was killed immediately before this
16853 redisplay cycle. In that case, ROW->end stores the
16854 buffer position of the first visual-order character of
16855 the killed text, which is now beyond ZV. */
16856 && CHARPOS (row->end.pos) <= ZV)
16857 row_found = row;
16859 /* Stop if last visible row. */
16860 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16861 break;
16864 return row_found;
16868 /* Find the first glyph row in the current matrix of W that is not
16869 affected by changes at the end of current_buffer since the
16870 time W's current matrix was built.
16872 Return in *DELTA the number of chars by which buffer positions in
16873 unchanged text at the end of current_buffer must be adjusted.
16875 Return in *DELTA_BYTES the corresponding number of bytes.
16877 Value is null if no such row exists, i.e. all rows are affected by
16878 changes. */
16880 static struct glyph_row *
16881 find_first_unchanged_at_end_row (struct window *w,
16882 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16884 struct glyph_row *row;
16885 struct glyph_row *row_found = NULL;
16887 *delta = *delta_bytes = 0;
16889 /* Display must not have been paused, otherwise the current matrix
16890 is not up to date. */
16891 eassert (w->window_end_valid);
16893 /* A value of window_end_pos >= END_UNCHANGED means that the window
16894 end is in the range of changed text. If so, there is no
16895 unchanged row at the end of W's current matrix. */
16896 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16897 return NULL;
16899 /* Set row to the last row in W's current matrix displaying text. */
16900 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16902 /* If matrix is entirely empty, no unchanged row exists. */
16903 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16905 /* The value of row is the last glyph row in the matrix having a
16906 meaningful buffer position in it. The end position of row
16907 corresponds to window_end_pos. This allows us to translate
16908 buffer positions in the current matrix to current buffer
16909 positions for characters not in changed text. */
16910 ptrdiff_t Z_old =
16911 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16912 ptrdiff_t Z_BYTE_old =
16913 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16914 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16915 struct glyph_row *first_text_row
16916 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16918 *delta = Z - Z_old;
16919 *delta_bytes = Z_BYTE - Z_BYTE_old;
16921 /* Set last_unchanged_pos to the buffer position of the last
16922 character in the buffer that has not been changed. Z is the
16923 index + 1 of the last character in current_buffer, i.e. by
16924 subtracting END_UNCHANGED we get the index of the last
16925 unchanged character, and we have to add BEG to get its buffer
16926 position. */
16927 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16928 last_unchanged_pos_old = last_unchanged_pos - *delta;
16930 /* Search backward from ROW for a row displaying a line that
16931 starts at a minimum position >= last_unchanged_pos_old. */
16932 for (; row > first_text_row; --row)
16934 /* This used to abort, but it can happen.
16935 It is ok to just stop the search instead here. KFS. */
16936 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16937 break;
16939 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16940 row_found = row;
16944 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16946 return row_found;
16950 /* Make sure that glyph rows in the current matrix of window W
16951 reference the same glyph memory as corresponding rows in the
16952 frame's frame matrix. This function is called after scrolling W's
16953 current matrix on a terminal frame in try_window_id and
16954 try_window_reusing_current_matrix. */
16956 static void
16957 sync_frame_with_window_matrix_rows (struct window *w)
16959 struct frame *f = XFRAME (w->frame);
16960 struct glyph_row *window_row, *window_row_end, *frame_row;
16962 /* Preconditions: W must be a leaf window and full-width. Its frame
16963 must have a frame matrix. */
16964 eassert (BUFFERP (w->contents));
16965 eassert (WINDOW_FULL_WIDTH_P (w));
16966 eassert (!FRAME_WINDOW_P (f));
16968 /* If W is a full-width window, glyph pointers in W's current matrix
16969 have, by definition, to be the same as glyph pointers in the
16970 corresponding frame matrix. Note that frame matrices have no
16971 marginal areas (see build_frame_matrix). */
16972 window_row = w->current_matrix->rows;
16973 window_row_end = window_row + w->current_matrix->nrows;
16974 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16975 while (window_row < window_row_end)
16977 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16978 struct glyph *end = window_row->glyphs[LAST_AREA];
16980 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16981 frame_row->glyphs[TEXT_AREA] = start;
16982 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16983 frame_row->glyphs[LAST_AREA] = end;
16985 /* Disable frame rows whose corresponding window rows have
16986 been disabled in try_window_id. */
16987 if (!window_row->enabled_p)
16988 frame_row->enabled_p = 0;
16990 ++window_row, ++frame_row;
16995 /* Find the glyph row in window W containing CHARPOS. Consider all
16996 rows between START and END (not inclusive). END null means search
16997 all rows to the end of the display area of W. Value is the row
16998 containing CHARPOS or null. */
17000 struct glyph_row *
17001 row_containing_pos (struct window *w, ptrdiff_t charpos,
17002 struct glyph_row *start, struct glyph_row *end, int dy)
17004 struct glyph_row *row = start;
17005 struct glyph_row *best_row = NULL;
17006 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17007 int last_y;
17009 /* If we happen to start on a header-line, skip that. */
17010 if (row->mode_line_p)
17011 ++row;
17013 if ((end && row >= end) || !row->enabled_p)
17014 return NULL;
17016 last_y = window_text_bottom_y (w) - dy;
17018 while (1)
17020 /* Give up if we have gone too far. */
17021 if (end && row >= end)
17022 return NULL;
17023 /* This formerly returned if they were equal.
17024 I think that both quantities are of a "last plus one" type;
17025 if so, when they are equal, the row is within the screen. -- rms. */
17026 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17027 return NULL;
17029 /* If it is in this row, return this row. */
17030 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17031 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17032 /* The end position of a row equals the start
17033 position of the next row. If CHARPOS is there, we
17034 would rather consider it displayed in the next
17035 line, except when this line ends in ZV. */
17036 && !row_for_charpos_p (row, charpos)))
17037 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17039 struct glyph *g;
17041 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17042 || (!best_row && !row->continued_p))
17043 return row;
17044 /* In bidi-reordered rows, there could be several rows whose
17045 edges surround CHARPOS, all of these rows belonging to
17046 the same continued line. We need to find the row which
17047 fits CHARPOS the best. */
17048 for (g = row->glyphs[TEXT_AREA];
17049 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17050 g++)
17052 if (!STRINGP (g->object))
17054 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17056 mindif = eabs (g->charpos - charpos);
17057 best_row = row;
17058 /* Exact match always wins. */
17059 if (mindif == 0)
17060 return best_row;
17065 else if (best_row && !row->continued_p)
17066 return best_row;
17067 ++row;
17072 /* Try to redisplay window W by reusing its existing display. W's
17073 current matrix must be up to date when this function is called,
17074 i.e. window_end_valid must be nonzero.
17076 Value is
17078 1 if display has been updated
17079 0 if otherwise unsuccessful
17080 -1 if redisplay with same window start is known not to succeed
17082 The following steps are performed:
17084 1. Find the last row in the current matrix of W that is not
17085 affected by changes at the start of current_buffer. If no such row
17086 is found, give up.
17088 2. Find the first row in W's current matrix that is not affected by
17089 changes at the end of current_buffer. Maybe there is no such row.
17091 3. Display lines beginning with the row + 1 found in step 1 to the
17092 row found in step 2 or, if step 2 didn't find a row, to the end of
17093 the window.
17095 4. If cursor is not known to appear on the window, give up.
17097 5. If display stopped at the row found in step 2, scroll the
17098 display and current matrix as needed.
17100 6. Maybe display some lines at the end of W, if we must. This can
17101 happen under various circumstances, like a partially visible line
17102 becoming fully visible, or because newly displayed lines are displayed
17103 in smaller font sizes.
17105 7. Update W's window end information. */
17107 static int
17108 try_window_id (struct window *w)
17110 struct frame *f = XFRAME (w->frame);
17111 struct glyph_matrix *current_matrix = w->current_matrix;
17112 struct glyph_matrix *desired_matrix = w->desired_matrix;
17113 struct glyph_row *last_unchanged_at_beg_row;
17114 struct glyph_row *first_unchanged_at_end_row;
17115 struct glyph_row *row;
17116 struct glyph_row *bottom_row;
17117 int bottom_vpos;
17118 struct it it;
17119 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17120 int dvpos, dy;
17121 struct text_pos start_pos;
17122 struct run run;
17123 int first_unchanged_at_end_vpos = 0;
17124 struct glyph_row *last_text_row, *last_text_row_at_end;
17125 struct text_pos start;
17126 ptrdiff_t first_changed_charpos, last_changed_charpos;
17128 #ifdef GLYPH_DEBUG
17129 if (inhibit_try_window_id)
17130 return 0;
17131 #endif
17133 /* This is handy for debugging. */
17134 #if 0
17135 #define GIVE_UP(X) \
17136 do { \
17137 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17138 return 0; \
17139 } while (0)
17140 #else
17141 #define GIVE_UP(X) return 0
17142 #endif
17144 SET_TEXT_POS_FROM_MARKER (start, w->start);
17146 /* Don't use this for mini-windows because these can show
17147 messages and mini-buffers, and we don't handle that here. */
17148 if (MINI_WINDOW_P (w))
17149 GIVE_UP (1);
17151 /* This flag is used to prevent redisplay optimizations. */
17152 if (windows_or_buffers_changed || cursor_type_changed)
17153 GIVE_UP (2);
17155 /* Verify that narrowing has not changed.
17156 Also verify that we were not told to prevent redisplay optimizations.
17157 It would be nice to further
17158 reduce the number of cases where this prevents try_window_id. */
17159 if (current_buffer->clip_changed
17160 || current_buffer->prevent_redisplay_optimizations_p)
17161 GIVE_UP (3);
17163 /* Window must either use window-based redisplay or be full width. */
17164 if (!FRAME_WINDOW_P (f)
17165 && (!FRAME_LINE_INS_DEL_OK (f)
17166 || !WINDOW_FULL_WIDTH_P (w)))
17167 GIVE_UP (4);
17169 /* Give up if point is known NOT to appear in W. */
17170 if (PT < CHARPOS (start))
17171 GIVE_UP (5);
17173 /* Another way to prevent redisplay optimizations. */
17174 if (w->last_modified == 0)
17175 GIVE_UP (6);
17177 /* Verify that window is not hscrolled. */
17178 if (w->hscroll != 0)
17179 GIVE_UP (7);
17181 /* Verify that display wasn't paused. */
17182 if (!w->window_end_valid)
17183 GIVE_UP (8);
17185 /* Can't use this if highlighting a region because a cursor movement
17186 will do more than just set the cursor. */
17187 if (markpos_of_region () >= 0)
17188 GIVE_UP (9);
17190 /* Likewise if highlighting trailing whitespace. */
17191 if (!NILP (Vshow_trailing_whitespace))
17192 GIVE_UP (11);
17194 /* Likewise if showing a region. */
17195 if (w->region_showing)
17196 GIVE_UP (10);
17198 /* Can't use this if overlay arrow position and/or string have
17199 changed. */
17200 if (overlay_arrows_changed_p ())
17201 GIVE_UP (12);
17203 /* When word-wrap is on, adding a space to the first word of a
17204 wrapped line can change the wrap position, altering the line
17205 above it. It might be worthwhile to handle this more
17206 intelligently, but for now just redisplay from scratch. */
17207 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17208 GIVE_UP (21);
17210 /* Under bidi reordering, adding or deleting a character in the
17211 beginning of a paragraph, before the first strong directional
17212 character, can change the base direction of the paragraph (unless
17213 the buffer specifies a fixed paragraph direction), which will
17214 require to redisplay the whole paragraph. It might be worthwhile
17215 to find the paragraph limits and widen the range of redisplayed
17216 lines to that, but for now just give up this optimization and
17217 redisplay from scratch. */
17218 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17219 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17220 GIVE_UP (22);
17222 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17223 only if buffer has really changed. The reason is that the gap is
17224 initially at Z for freshly visited files. The code below would
17225 set end_unchanged to 0 in that case. */
17226 if (MODIFF > SAVE_MODIFF
17227 /* This seems to happen sometimes after saving a buffer. */
17228 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17230 if (GPT - BEG < BEG_UNCHANGED)
17231 BEG_UNCHANGED = GPT - BEG;
17232 if (Z - GPT < END_UNCHANGED)
17233 END_UNCHANGED = Z - GPT;
17236 /* The position of the first and last character that has been changed. */
17237 first_changed_charpos = BEG + BEG_UNCHANGED;
17238 last_changed_charpos = Z - END_UNCHANGED;
17240 /* If window starts after a line end, and the last change is in
17241 front of that newline, then changes don't affect the display.
17242 This case happens with stealth-fontification. Note that although
17243 the display is unchanged, glyph positions in the matrix have to
17244 be adjusted, of course. */
17245 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17246 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17247 && ((last_changed_charpos < CHARPOS (start)
17248 && CHARPOS (start) == BEGV)
17249 || (last_changed_charpos < CHARPOS (start) - 1
17250 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17252 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17253 struct glyph_row *r0;
17255 /* Compute how many chars/bytes have been added to or removed
17256 from the buffer. */
17257 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17258 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17259 Z_delta = Z - Z_old;
17260 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17262 /* Give up if PT is not in the window. Note that it already has
17263 been checked at the start of try_window_id that PT is not in
17264 front of the window start. */
17265 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17266 GIVE_UP (13);
17268 /* If window start is unchanged, we can reuse the whole matrix
17269 as is, after adjusting glyph positions. No need to compute
17270 the window end again, since its offset from Z hasn't changed. */
17271 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17272 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17273 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17274 /* PT must not be in a partially visible line. */
17275 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17276 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17278 /* Adjust positions in the glyph matrix. */
17279 if (Z_delta || Z_delta_bytes)
17281 struct glyph_row *r1
17282 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17283 increment_matrix_positions (w->current_matrix,
17284 MATRIX_ROW_VPOS (r0, current_matrix),
17285 MATRIX_ROW_VPOS (r1, current_matrix),
17286 Z_delta, Z_delta_bytes);
17289 /* Set the cursor. */
17290 row = row_containing_pos (w, PT, r0, NULL, 0);
17291 if (row)
17292 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17293 else
17294 emacs_abort ();
17295 return 1;
17299 /* Handle the case that changes are all below what is displayed in
17300 the window, and that PT is in the window. This shortcut cannot
17301 be taken if ZV is visible in the window, and text has been added
17302 there that is visible in the window. */
17303 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17304 /* ZV is not visible in the window, or there are no
17305 changes at ZV, actually. */
17306 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17307 || first_changed_charpos == last_changed_charpos))
17309 struct glyph_row *r0;
17311 /* Give up if PT is not in the window. Note that it already has
17312 been checked at the start of try_window_id that PT is not in
17313 front of the window start. */
17314 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17315 GIVE_UP (14);
17317 /* If window start is unchanged, we can reuse the whole matrix
17318 as is, without changing glyph positions since no text has
17319 been added/removed in front of the window end. */
17320 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17321 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17322 /* PT must not be in a partially visible line. */
17323 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17324 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17326 /* We have to compute the window end anew since text
17327 could have been added/removed after it. */
17328 wset_window_end_pos
17329 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17330 w->window_end_bytepos
17331 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17333 /* Set the cursor. */
17334 row = row_containing_pos (w, PT, r0, NULL, 0);
17335 if (row)
17336 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17337 else
17338 emacs_abort ();
17339 return 2;
17343 /* Give up if window start is in the changed area.
17345 The condition used to read
17347 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17349 but why that was tested escapes me at the moment. */
17350 if (CHARPOS (start) >= first_changed_charpos
17351 && CHARPOS (start) <= last_changed_charpos)
17352 GIVE_UP (15);
17354 /* Check that window start agrees with the start of the first glyph
17355 row in its current matrix. Check this after we know the window
17356 start is not in changed text, otherwise positions would not be
17357 comparable. */
17358 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17359 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17360 GIVE_UP (16);
17362 /* Give up if the window ends in strings. Overlay strings
17363 at the end are difficult to handle, so don't try. */
17364 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17365 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17366 GIVE_UP (20);
17368 /* Compute the position at which we have to start displaying new
17369 lines. Some of the lines at the top of the window might be
17370 reusable because they are not displaying changed text. Find the
17371 last row in W's current matrix not affected by changes at the
17372 start of current_buffer. Value is null if changes start in the
17373 first line of window. */
17374 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17375 if (last_unchanged_at_beg_row)
17377 /* Avoid starting to display in the middle of a character, a TAB
17378 for instance. This is easier than to set up the iterator
17379 exactly, and it's not a frequent case, so the additional
17380 effort wouldn't really pay off. */
17381 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17382 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17383 && last_unchanged_at_beg_row > w->current_matrix->rows)
17384 --last_unchanged_at_beg_row;
17386 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17387 GIVE_UP (17);
17389 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17390 GIVE_UP (18);
17391 start_pos = it.current.pos;
17393 /* Start displaying new lines in the desired matrix at the same
17394 vpos we would use in the current matrix, i.e. below
17395 last_unchanged_at_beg_row. */
17396 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17397 current_matrix);
17398 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17399 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17401 eassert (it.hpos == 0 && it.current_x == 0);
17403 else
17405 /* There are no reusable lines at the start of the window.
17406 Start displaying in the first text line. */
17407 start_display (&it, w, start);
17408 it.vpos = it.first_vpos;
17409 start_pos = it.current.pos;
17412 /* Find the first row that is not affected by changes at the end of
17413 the buffer. Value will be null if there is no unchanged row, in
17414 which case we must redisplay to the end of the window. delta
17415 will be set to the value by which buffer positions beginning with
17416 first_unchanged_at_end_row have to be adjusted due to text
17417 changes. */
17418 first_unchanged_at_end_row
17419 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17420 IF_DEBUG (debug_delta = delta);
17421 IF_DEBUG (debug_delta_bytes = delta_bytes);
17423 /* Set stop_pos to the buffer position up to which we will have to
17424 display new lines. If first_unchanged_at_end_row != NULL, this
17425 is the buffer position of the start of the line displayed in that
17426 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17427 that we don't stop at a buffer position. */
17428 stop_pos = 0;
17429 if (first_unchanged_at_end_row)
17431 eassert (last_unchanged_at_beg_row == NULL
17432 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17434 /* If this is a continuation line, move forward to the next one
17435 that isn't. Changes in lines above affect this line.
17436 Caution: this may move first_unchanged_at_end_row to a row
17437 not displaying text. */
17438 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17439 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17440 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17441 < it.last_visible_y))
17442 ++first_unchanged_at_end_row;
17444 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17445 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17446 >= it.last_visible_y))
17447 first_unchanged_at_end_row = NULL;
17448 else
17450 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17451 + delta);
17452 first_unchanged_at_end_vpos
17453 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17454 eassert (stop_pos >= Z - END_UNCHANGED);
17457 else if (last_unchanged_at_beg_row == NULL)
17458 GIVE_UP (19);
17461 #ifdef GLYPH_DEBUG
17463 /* Either there is no unchanged row at the end, or the one we have
17464 now displays text. This is a necessary condition for the window
17465 end pos calculation at the end of this function. */
17466 eassert (first_unchanged_at_end_row == NULL
17467 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17469 debug_last_unchanged_at_beg_vpos
17470 = (last_unchanged_at_beg_row
17471 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17472 : -1);
17473 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17475 #endif /* GLYPH_DEBUG */
17478 /* Display new lines. Set last_text_row to the last new line
17479 displayed which has text on it, i.e. might end up as being the
17480 line where the window_end_vpos is. */
17481 w->cursor.vpos = -1;
17482 last_text_row = NULL;
17483 overlay_arrow_seen = 0;
17484 while (it.current_y < it.last_visible_y
17485 && !fonts_changed_p
17486 && (first_unchanged_at_end_row == NULL
17487 || IT_CHARPOS (it) < stop_pos))
17489 if (display_line (&it))
17490 last_text_row = it.glyph_row - 1;
17493 if (fonts_changed_p)
17494 return -1;
17497 /* Compute differences in buffer positions, y-positions etc. for
17498 lines reused at the bottom of the window. Compute what we can
17499 scroll. */
17500 if (first_unchanged_at_end_row
17501 /* No lines reused because we displayed everything up to the
17502 bottom of the window. */
17503 && it.current_y < it.last_visible_y)
17505 dvpos = (it.vpos
17506 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17507 current_matrix));
17508 dy = it.current_y - first_unchanged_at_end_row->y;
17509 run.current_y = first_unchanged_at_end_row->y;
17510 run.desired_y = run.current_y + dy;
17511 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17513 else
17515 delta = delta_bytes = dvpos = dy
17516 = run.current_y = run.desired_y = run.height = 0;
17517 first_unchanged_at_end_row = NULL;
17519 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17522 /* Find the cursor if not already found. We have to decide whether
17523 PT will appear on this window (it sometimes doesn't, but this is
17524 not a very frequent case.) This decision has to be made before
17525 the current matrix is altered. A value of cursor.vpos < 0 means
17526 that PT is either in one of the lines beginning at
17527 first_unchanged_at_end_row or below the window. Don't care for
17528 lines that might be displayed later at the window end; as
17529 mentioned, this is not a frequent case. */
17530 if (w->cursor.vpos < 0)
17532 /* Cursor in unchanged rows at the top? */
17533 if (PT < CHARPOS (start_pos)
17534 && last_unchanged_at_beg_row)
17536 row = row_containing_pos (w, PT,
17537 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17538 last_unchanged_at_beg_row + 1, 0);
17539 if (row)
17540 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17543 /* Start from first_unchanged_at_end_row looking for PT. */
17544 else if (first_unchanged_at_end_row)
17546 row = row_containing_pos (w, PT - delta,
17547 first_unchanged_at_end_row, NULL, 0);
17548 if (row)
17549 set_cursor_from_row (w, row, w->current_matrix, delta,
17550 delta_bytes, dy, dvpos);
17553 /* Give up if cursor was not found. */
17554 if (w->cursor.vpos < 0)
17556 clear_glyph_matrix (w->desired_matrix);
17557 return -1;
17561 /* Don't let the cursor end in the scroll margins. */
17563 int this_scroll_margin, cursor_height;
17564 int frame_line_height = default_line_pixel_height (w);
17565 int window_total_lines
17566 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17568 this_scroll_margin =
17569 max (0, min (scroll_margin, window_total_lines / 4));
17570 this_scroll_margin *= frame_line_height;
17571 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17573 if ((w->cursor.y < this_scroll_margin
17574 && CHARPOS (start) > BEGV)
17575 /* Old redisplay didn't take scroll margin into account at the bottom,
17576 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17577 || (w->cursor.y + (make_cursor_line_fully_visible_p
17578 ? cursor_height + this_scroll_margin
17579 : 1)) > it.last_visible_y)
17581 w->cursor.vpos = -1;
17582 clear_glyph_matrix (w->desired_matrix);
17583 return -1;
17587 /* Scroll the display. Do it before changing the current matrix so
17588 that xterm.c doesn't get confused about where the cursor glyph is
17589 found. */
17590 if (dy && run.height)
17592 update_begin (f);
17594 if (FRAME_WINDOW_P (f))
17596 FRAME_RIF (f)->update_window_begin_hook (w);
17597 FRAME_RIF (f)->clear_window_mouse_face (w);
17598 FRAME_RIF (f)->scroll_run_hook (w, &run);
17599 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17601 else
17603 /* Terminal frame. In this case, dvpos gives the number of
17604 lines to scroll by; dvpos < 0 means scroll up. */
17605 int from_vpos
17606 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17607 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17608 int end = (WINDOW_TOP_EDGE_LINE (w)
17609 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17610 + window_internal_height (w));
17612 #if defined (HAVE_GPM) || defined (MSDOS)
17613 x_clear_window_mouse_face (w);
17614 #endif
17615 /* Perform the operation on the screen. */
17616 if (dvpos > 0)
17618 /* Scroll last_unchanged_at_beg_row to the end of the
17619 window down dvpos lines. */
17620 set_terminal_window (f, end);
17622 /* On dumb terminals delete dvpos lines at the end
17623 before inserting dvpos empty lines. */
17624 if (!FRAME_SCROLL_REGION_OK (f))
17625 ins_del_lines (f, end - dvpos, -dvpos);
17627 /* Insert dvpos empty lines in front of
17628 last_unchanged_at_beg_row. */
17629 ins_del_lines (f, from, dvpos);
17631 else if (dvpos < 0)
17633 /* Scroll up last_unchanged_at_beg_vpos to the end of
17634 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17635 set_terminal_window (f, end);
17637 /* Delete dvpos lines in front of
17638 last_unchanged_at_beg_vpos. ins_del_lines will set
17639 the cursor to the given vpos and emit |dvpos| delete
17640 line sequences. */
17641 ins_del_lines (f, from + dvpos, dvpos);
17643 /* On a dumb terminal insert dvpos empty lines at the
17644 end. */
17645 if (!FRAME_SCROLL_REGION_OK (f))
17646 ins_del_lines (f, end + dvpos, -dvpos);
17649 set_terminal_window (f, 0);
17652 update_end (f);
17655 /* Shift reused rows of the current matrix to the right position.
17656 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17657 text. */
17658 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17659 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17660 if (dvpos < 0)
17662 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17663 bottom_vpos, dvpos);
17664 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17665 bottom_vpos);
17667 else if (dvpos > 0)
17669 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17670 bottom_vpos, dvpos);
17671 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17672 first_unchanged_at_end_vpos + dvpos);
17675 /* For frame-based redisplay, make sure that current frame and window
17676 matrix are in sync with respect to glyph memory. */
17677 if (!FRAME_WINDOW_P (f))
17678 sync_frame_with_window_matrix_rows (w);
17680 /* Adjust buffer positions in reused rows. */
17681 if (delta || delta_bytes)
17682 increment_matrix_positions (current_matrix,
17683 first_unchanged_at_end_vpos + dvpos,
17684 bottom_vpos, delta, delta_bytes);
17686 /* Adjust Y positions. */
17687 if (dy)
17688 shift_glyph_matrix (w, current_matrix,
17689 first_unchanged_at_end_vpos + dvpos,
17690 bottom_vpos, dy);
17692 if (first_unchanged_at_end_row)
17694 first_unchanged_at_end_row += dvpos;
17695 if (first_unchanged_at_end_row->y >= it.last_visible_y
17696 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17697 first_unchanged_at_end_row = NULL;
17700 /* If scrolling up, there may be some lines to display at the end of
17701 the window. */
17702 last_text_row_at_end = NULL;
17703 if (dy < 0)
17705 /* Scrolling up can leave for example a partially visible line
17706 at the end of the window to be redisplayed. */
17707 /* Set last_row to the glyph row in the current matrix where the
17708 window end line is found. It has been moved up or down in
17709 the matrix by dvpos. */
17710 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17711 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17713 /* If last_row is the window end line, it should display text. */
17714 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17716 /* If window end line was partially visible before, begin
17717 displaying at that line. Otherwise begin displaying with the
17718 line following it. */
17719 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17721 init_to_row_start (&it, w, last_row);
17722 it.vpos = last_vpos;
17723 it.current_y = last_row->y;
17725 else
17727 init_to_row_end (&it, w, last_row);
17728 it.vpos = 1 + last_vpos;
17729 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17730 ++last_row;
17733 /* We may start in a continuation line. If so, we have to
17734 get the right continuation_lines_width and current_x. */
17735 it.continuation_lines_width = last_row->continuation_lines_width;
17736 it.hpos = it.current_x = 0;
17738 /* Display the rest of the lines at the window end. */
17739 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17740 while (it.current_y < it.last_visible_y
17741 && !fonts_changed_p)
17743 /* Is it always sure that the display agrees with lines in
17744 the current matrix? I don't think so, so we mark rows
17745 displayed invalid in the current matrix by setting their
17746 enabled_p flag to zero. */
17747 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17748 if (display_line (&it))
17749 last_text_row_at_end = it.glyph_row - 1;
17753 /* Update window_end_pos and window_end_vpos. */
17754 if (first_unchanged_at_end_row
17755 && !last_text_row_at_end)
17757 /* Window end line if one of the preserved rows from the current
17758 matrix. Set row to the last row displaying text in current
17759 matrix starting at first_unchanged_at_end_row, after
17760 scrolling. */
17761 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17762 row = find_last_row_displaying_text (w->current_matrix, &it,
17763 first_unchanged_at_end_row);
17764 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17766 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17767 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17768 wset_window_end_vpos
17769 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17770 eassert (w->window_end_bytepos >= 0);
17771 IF_DEBUG (debug_method_add (w, "A"));
17773 else if (last_text_row_at_end)
17775 wset_window_end_pos
17776 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17777 w->window_end_bytepos
17778 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17779 wset_window_end_vpos
17780 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17781 desired_matrix)));
17782 eassert (w->window_end_bytepos >= 0);
17783 IF_DEBUG (debug_method_add (w, "B"));
17785 else if (last_text_row)
17787 /* We have displayed either to the end of the window or at the
17788 end of the window, i.e. the last row with text is to be found
17789 in the desired matrix. */
17790 wset_window_end_pos
17791 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17792 w->window_end_bytepos
17793 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17794 wset_window_end_vpos
17795 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17796 eassert (w->window_end_bytepos >= 0);
17798 else if (first_unchanged_at_end_row == NULL
17799 && last_text_row == NULL
17800 && last_text_row_at_end == NULL)
17802 /* Displayed to end of window, but no line containing text was
17803 displayed. Lines were deleted at the end of the window. */
17804 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17805 int vpos = XFASTINT (w->window_end_vpos);
17806 struct glyph_row *current_row = current_matrix->rows + vpos;
17807 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17809 for (row = NULL;
17810 row == NULL && vpos >= first_vpos;
17811 --vpos, --current_row, --desired_row)
17813 if (desired_row->enabled_p)
17815 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17816 row = desired_row;
17818 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17819 row = current_row;
17822 eassert (row != NULL);
17823 wset_window_end_vpos (w, make_number (vpos + 1));
17824 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17825 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17826 eassert (w->window_end_bytepos >= 0);
17827 IF_DEBUG (debug_method_add (w, "C"));
17829 else
17830 emacs_abort ();
17832 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17833 debug_end_vpos = XFASTINT (w->window_end_vpos));
17835 /* Record that display has not been completed. */
17836 w->window_end_valid = 0;
17837 w->desired_matrix->no_scrolling_p = 1;
17838 return 3;
17840 #undef GIVE_UP
17845 /***********************************************************************
17846 More debugging support
17847 ***********************************************************************/
17849 #ifdef GLYPH_DEBUG
17851 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17852 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17853 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17856 /* Dump the contents of glyph matrix MATRIX on stderr.
17858 GLYPHS 0 means don't show glyph contents.
17859 GLYPHS 1 means show glyphs in short form
17860 GLYPHS > 1 means show glyphs in long form. */
17862 void
17863 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17865 int i;
17866 for (i = 0; i < matrix->nrows; ++i)
17867 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17871 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17872 the glyph row and area where the glyph comes from. */
17874 void
17875 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17877 if (glyph->type == CHAR_GLYPH
17878 || glyph->type == GLYPHLESS_GLYPH)
17880 fprintf (stderr,
17881 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17882 glyph - row->glyphs[TEXT_AREA],
17883 (glyph->type == CHAR_GLYPH
17884 ? 'C'
17885 : 'G'),
17886 glyph->charpos,
17887 (BUFFERP (glyph->object)
17888 ? 'B'
17889 : (STRINGP (glyph->object)
17890 ? 'S'
17891 : (INTEGERP (glyph->object)
17892 ? '0'
17893 : '-'))),
17894 glyph->pixel_width,
17895 glyph->u.ch,
17896 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17897 ? glyph->u.ch
17898 : '.'),
17899 glyph->face_id,
17900 glyph->left_box_line_p,
17901 glyph->right_box_line_p);
17903 else if (glyph->type == STRETCH_GLYPH)
17905 fprintf (stderr,
17906 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17907 glyph - row->glyphs[TEXT_AREA],
17908 'S',
17909 glyph->charpos,
17910 (BUFFERP (glyph->object)
17911 ? 'B'
17912 : (STRINGP (glyph->object)
17913 ? 'S'
17914 : (INTEGERP (glyph->object)
17915 ? '0'
17916 : '-'))),
17917 glyph->pixel_width,
17919 ' ',
17920 glyph->face_id,
17921 glyph->left_box_line_p,
17922 glyph->right_box_line_p);
17924 else if (glyph->type == IMAGE_GLYPH)
17926 fprintf (stderr,
17927 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17928 glyph - row->glyphs[TEXT_AREA],
17929 'I',
17930 glyph->charpos,
17931 (BUFFERP (glyph->object)
17932 ? 'B'
17933 : (STRINGP (glyph->object)
17934 ? 'S'
17935 : (INTEGERP (glyph->object)
17936 ? '0'
17937 : '-'))),
17938 glyph->pixel_width,
17939 glyph->u.img_id,
17940 '.',
17941 glyph->face_id,
17942 glyph->left_box_line_p,
17943 glyph->right_box_line_p);
17945 else if (glyph->type == COMPOSITE_GLYPH)
17947 fprintf (stderr,
17948 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17949 glyph - row->glyphs[TEXT_AREA],
17950 '+',
17951 glyph->charpos,
17952 (BUFFERP (glyph->object)
17953 ? 'B'
17954 : (STRINGP (glyph->object)
17955 ? 'S'
17956 : (INTEGERP (glyph->object)
17957 ? '0'
17958 : '-'))),
17959 glyph->pixel_width,
17960 glyph->u.cmp.id);
17961 if (glyph->u.cmp.automatic)
17962 fprintf (stderr,
17963 "[%d-%d]",
17964 glyph->slice.cmp.from, glyph->slice.cmp.to);
17965 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17966 glyph->face_id,
17967 glyph->left_box_line_p,
17968 glyph->right_box_line_p);
17973 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17974 GLYPHS 0 means don't show glyph contents.
17975 GLYPHS 1 means show glyphs in short form
17976 GLYPHS > 1 means show glyphs in long form. */
17978 void
17979 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17981 if (glyphs != 1)
17983 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17984 fprintf (stderr, "==============================================================================\n");
17986 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17987 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17988 vpos,
17989 MATRIX_ROW_START_CHARPOS (row),
17990 MATRIX_ROW_END_CHARPOS (row),
17991 row->used[TEXT_AREA],
17992 row->contains_overlapping_glyphs_p,
17993 row->enabled_p,
17994 row->truncated_on_left_p,
17995 row->truncated_on_right_p,
17996 row->continued_p,
17997 MATRIX_ROW_CONTINUATION_LINE_P (row),
17998 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17999 row->ends_at_zv_p,
18000 row->fill_line_p,
18001 row->ends_in_middle_of_char_p,
18002 row->starts_in_middle_of_char_p,
18003 row->mouse_face_p,
18004 row->x,
18005 row->y,
18006 row->pixel_width,
18007 row->height,
18008 row->visible_height,
18009 row->ascent,
18010 row->phys_ascent);
18011 /* The next 3 lines should align to "Start" in the header. */
18012 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18013 row->end.overlay_string_index,
18014 row->continuation_lines_width);
18015 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18016 CHARPOS (row->start.string_pos),
18017 CHARPOS (row->end.string_pos));
18018 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18019 row->end.dpvec_index);
18022 if (glyphs > 1)
18024 int area;
18026 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18028 struct glyph *glyph = row->glyphs[area];
18029 struct glyph *glyph_end = glyph + row->used[area];
18031 /* Glyph for a line end in text. */
18032 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18033 ++glyph_end;
18035 if (glyph < glyph_end)
18036 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18038 for (; glyph < glyph_end; ++glyph)
18039 dump_glyph (row, glyph, area);
18042 else if (glyphs == 1)
18044 int area;
18046 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18048 char *s = alloca (row->used[area] + 4);
18049 int i;
18051 for (i = 0; i < row->used[area]; ++i)
18053 struct glyph *glyph = row->glyphs[area] + i;
18054 if (i == row->used[area] - 1
18055 && area == TEXT_AREA
18056 && INTEGERP (glyph->object)
18057 && glyph->type == CHAR_GLYPH
18058 && glyph->u.ch == ' ')
18060 strcpy (&s[i], "[\\n]");
18061 i += 4;
18063 else if (glyph->type == CHAR_GLYPH
18064 && glyph->u.ch < 0x80
18065 && glyph->u.ch >= ' ')
18066 s[i] = glyph->u.ch;
18067 else
18068 s[i] = '.';
18071 s[i] = '\0';
18072 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18078 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18079 Sdump_glyph_matrix, 0, 1, "p",
18080 doc: /* Dump the current matrix of the selected window to stderr.
18081 Shows contents of glyph row structures. With non-nil
18082 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18083 glyphs in short form, otherwise show glyphs in long form. */)
18084 (Lisp_Object glyphs)
18086 struct window *w = XWINDOW (selected_window);
18087 struct buffer *buffer = XBUFFER (w->contents);
18089 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18090 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18091 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18092 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18093 fprintf (stderr, "=============================================\n");
18094 dump_glyph_matrix (w->current_matrix,
18095 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18096 return Qnil;
18100 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18101 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18102 (void)
18104 struct frame *f = XFRAME (selected_frame);
18105 dump_glyph_matrix (f->current_matrix, 1);
18106 return Qnil;
18110 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18111 doc: /* Dump glyph row ROW to stderr.
18112 GLYPH 0 means don't dump glyphs.
18113 GLYPH 1 means dump glyphs in short form.
18114 GLYPH > 1 or omitted means dump glyphs in long form. */)
18115 (Lisp_Object row, Lisp_Object glyphs)
18117 struct glyph_matrix *matrix;
18118 EMACS_INT vpos;
18120 CHECK_NUMBER (row);
18121 matrix = XWINDOW (selected_window)->current_matrix;
18122 vpos = XINT (row);
18123 if (vpos >= 0 && vpos < matrix->nrows)
18124 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18125 vpos,
18126 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18127 return Qnil;
18131 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18132 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18133 GLYPH 0 means don't dump glyphs.
18134 GLYPH 1 means dump glyphs in short form.
18135 GLYPH > 1 or omitted means dump glyphs in long form. */)
18136 (Lisp_Object row, Lisp_Object glyphs)
18138 struct frame *sf = SELECTED_FRAME ();
18139 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18140 EMACS_INT vpos;
18142 CHECK_NUMBER (row);
18143 vpos = XINT (row);
18144 if (vpos >= 0 && vpos < m->nrows)
18145 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18146 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18147 return Qnil;
18151 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18152 doc: /* Toggle tracing of redisplay.
18153 With ARG, turn tracing on if and only if ARG is positive. */)
18154 (Lisp_Object arg)
18156 if (NILP (arg))
18157 trace_redisplay_p = !trace_redisplay_p;
18158 else
18160 arg = Fprefix_numeric_value (arg);
18161 trace_redisplay_p = XINT (arg) > 0;
18164 return Qnil;
18168 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18169 doc: /* Like `format', but print result to stderr.
18170 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18171 (ptrdiff_t nargs, Lisp_Object *args)
18173 Lisp_Object s = Fformat (nargs, args);
18174 fprintf (stderr, "%s", SDATA (s));
18175 return Qnil;
18178 #endif /* GLYPH_DEBUG */
18182 /***********************************************************************
18183 Building Desired Matrix Rows
18184 ***********************************************************************/
18186 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18187 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18189 static struct glyph_row *
18190 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18192 struct frame *f = XFRAME (WINDOW_FRAME (w));
18193 struct buffer *buffer = XBUFFER (w->contents);
18194 struct buffer *old = current_buffer;
18195 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18196 int arrow_len = SCHARS (overlay_arrow_string);
18197 const unsigned char *arrow_end = arrow_string + arrow_len;
18198 const unsigned char *p;
18199 struct it it;
18200 bool multibyte_p;
18201 int n_glyphs_before;
18203 set_buffer_temp (buffer);
18204 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18205 it.glyph_row->used[TEXT_AREA] = 0;
18206 SET_TEXT_POS (it.position, 0, 0);
18208 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18209 p = arrow_string;
18210 while (p < arrow_end)
18212 Lisp_Object face, ilisp;
18214 /* Get the next character. */
18215 if (multibyte_p)
18216 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18217 else
18219 it.c = it.char_to_display = *p, it.len = 1;
18220 if (! ASCII_CHAR_P (it.c))
18221 it.char_to_display = BYTE8_TO_CHAR (it.c);
18223 p += it.len;
18225 /* Get its face. */
18226 ilisp = make_number (p - arrow_string);
18227 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18228 it.face_id = compute_char_face (f, it.char_to_display, face);
18230 /* Compute its width, get its glyphs. */
18231 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18232 SET_TEXT_POS (it.position, -1, -1);
18233 PRODUCE_GLYPHS (&it);
18235 /* If this character doesn't fit any more in the line, we have
18236 to remove some glyphs. */
18237 if (it.current_x > it.last_visible_x)
18239 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18240 break;
18244 set_buffer_temp (old);
18245 return it.glyph_row;
18249 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18250 glyphs to insert is determined by produce_special_glyphs. */
18252 static void
18253 insert_left_trunc_glyphs (struct it *it)
18255 struct it truncate_it;
18256 struct glyph *from, *end, *to, *toend;
18258 eassert (!FRAME_WINDOW_P (it->f)
18259 || (!it->glyph_row->reversed_p
18260 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18261 || (it->glyph_row->reversed_p
18262 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18264 /* Get the truncation glyphs. */
18265 truncate_it = *it;
18266 truncate_it.current_x = 0;
18267 truncate_it.face_id = DEFAULT_FACE_ID;
18268 truncate_it.glyph_row = &scratch_glyph_row;
18269 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18270 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18271 truncate_it.object = make_number (0);
18272 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18274 /* Overwrite glyphs from IT with truncation glyphs. */
18275 if (!it->glyph_row->reversed_p)
18277 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18279 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18280 end = from + tused;
18281 to = it->glyph_row->glyphs[TEXT_AREA];
18282 toend = to + it->glyph_row->used[TEXT_AREA];
18283 if (FRAME_WINDOW_P (it->f))
18285 /* On GUI frames, when variable-size fonts are displayed,
18286 the truncation glyphs may need more pixels than the row's
18287 glyphs they overwrite. We overwrite more glyphs to free
18288 enough screen real estate, and enlarge the stretch glyph
18289 on the right (see display_line), if there is one, to
18290 preserve the screen position of the truncation glyphs on
18291 the right. */
18292 int w = 0;
18293 struct glyph *g = to;
18294 short used;
18296 /* The first glyph could be partially visible, in which case
18297 it->glyph_row->x will be negative. But we want the left
18298 truncation glyphs to be aligned at the left margin of the
18299 window, so we override the x coordinate at which the row
18300 will begin. */
18301 it->glyph_row->x = 0;
18302 while (g < toend && w < it->truncation_pixel_width)
18304 w += g->pixel_width;
18305 ++g;
18307 if (g - to - tused > 0)
18309 memmove (to + tused, g, (toend - g) * sizeof(*g));
18310 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18312 used = it->glyph_row->used[TEXT_AREA];
18313 if (it->glyph_row->truncated_on_right_p
18314 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18315 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18316 == STRETCH_GLYPH)
18318 int extra = w - it->truncation_pixel_width;
18320 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18324 while (from < end)
18325 *to++ = *from++;
18327 /* There may be padding glyphs left over. Overwrite them too. */
18328 if (!FRAME_WINDOW_P (it->f))
18330 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18332 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18333 while (from < end)
18334 *to++ = *from++;
18338 if (to > toend)
18339 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18341 else
18343 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18345 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18346 that back to front. */
18347 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18348 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18349 toend = it->glyph_row->glyphs[TEXT_AREA];
18350 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18351 if (FRAME_WINDOW_P (it->f))
18353 int w = 0;
18354 struct glyph *g = to;
18356 while (g >= toend && w < it->truncation_pixel_width)
18358 w += g->pixel_width;
18359 --g;
18361 if (to - g - tused > 0)
18362 to = g + tused;
18363 if (it->glyph_row->truncated_on_right_p
18364 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18365 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18367 int extra = w - it->truncation_pixel_width;
18369 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18373 while (from >= end && to >= toend)
18374 *to-- = *from--;
18375 if (!FRAME_WINDOW_P (it->f))
18377 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18379 from =
18380 truncate_it.glyph_row->glyphs[TEXT_AREA]
18381 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18382 while (from >= end && to >= toend)
18383 *to-- = *from--;
18386 if (from >= end)
18388 /* Need to free some room before prepending additional
18389 glyphs. */
18390 int move_by = from - end + 1;
18391 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18392 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18394 for ( ; g >= g0; g--)
18395 g[move_by] = *g;
18396 while (from >= end)
18397 *to-- = *from--;
18398 it->glyph_row->used[TEXT_AREA] += move_by;
18403 /* Compute the hash code for ROW. */
18404 unsigned
18405 row_hash (struct glyph_row *row)
18407 int area, k;
18408 unsigned hashval = 0;
18410 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18411 for (k = 0; k < row->used[area]; ++k)
18412 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18413 + row->glyphs[area][k].u.val
18414 + row->glyphs[area][k].face_id
18415 + row->glyphs[area][k].padding_p
18416 + (row->glyphs[area][k].type << 2));
18418 return hashval;
18421 /* Compute the pixel height and width of IT->glyph_row.
18423 Most of the time, ascent and height of a display line will be equal
18424 to the max_ascent and max_height values of the display iterator
18425 structure. This is not the case if
18427 1. We hit ZV without displaying anything. In this case, max_ascent
18428 and max_height will be zero.
18430 2. We have some glyphs that don't contribute to the line height.
18431 (The glyph row flag contributes_to_line_height_p is for future
18432 pixmap extensions).
18434 The first case is easily covered by using default values because in
18435 these cases, the line height does not really matter, except that it
18436 must not be zero. */
18438 static void
18439 compute_line_metrics (struct it *it)
18441 struct glyph_row *row = it->glyph_row;
18443 if (FRAME_WINDOW_P (it->f))
18445 int i, min_y, max_y;
18447 /* The line may consist of one space only, that was added to
18448 place the cursor on it. If so, the row's height hasn't been
18449 computed yet. */
18450 if (row->height == 0)
18452 if (it->max_ascent + it->max_descent == 0)
18453 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18454 row->ascent = it->max_ascent;
18455 row->height = it->max_ascent + it->max_descent;
18456 row->phys_ascent = it->max_phys_ascent;
18457 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18458 row->extra_line_spacing = it->max_extra_line_spacing;
18461 /* Compute the width of this line. */
18462 row->pixel_width = row->x;
18463 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18464 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18466 eassert (row->pixel_width >= 0);
18467 eassert (row->ascent >= 0 && row->height > 0);
18469 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18470 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18472 /* If first line's physical ascent is larger than its logical
18473 ascent, use the physical ascent, and make the row taller.
18474 This makes accented characters fully visible. */
18475 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18476 && row->phys_ascent > row->ascent)
18478 row->height += row->phys_ascent - row->ascent;
18479 row->ascent = row->phys_ascent;
18482 /* Compute how much of the line is visible. */
18483 row->visible_height = row->height;
18485 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18486 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18488 if (row->y < min_y)
18489 row->visible_height -= min_y - row->y;
18490 if (row->y + row->height > max_y)
18491 row->visible_height -= row->y + row->height - max_y;
18493 else
18495 row->pixel_width = row->used[TEXT_AREA];
18496 if (row->continued_p)
18497 row->pixel_width -= it->continuation_pixel_width;
18498 else if (row->truncated_on_right_p)
18499 row->pixel_width -= it->truncation_pixel_width;
18500 row->ascent = row->phys_ascent = 0;
18501 row->height = row->phys_height = row->visible_height = 1;
18502 row->extra_line_spacing = 0;
18505 /* Compute a hash code for this row. */
18506 row->hash = row_hash (row);
18508 it->max_ascent = it->max_descent = 0;
18509 it->max_phys_ascent = it->max_phys_descent = 0;
18513 /* Append one space to the glyph row of iterator IT if doing a
18514 window-based redisplay. The space has the same face as
18515 IT->face_id. Value is non-zero if a space was added.
18517 This function is called to make sure that there is always one glyph
18518 at the end of a glyph row that the cursor can be set on under
18519 window-systems. (If there weren't such a glyph we would not know
18520 how wide and tall a box cursor should be displayed).
18522 At the same time this space let's a nicely handle clearing to the
18523 end of the line if the row ends in italic text. */
18525 static int
18526 append_space_for_newline (struct it *it, int default_face_p)
18528 if (FRAME_WINDOW_P (it->f))
18530 int n = it->glyph_row->used[TEXT_AREA];
18532 if (it->glyph_row->glyphs[TEXT_AREA] + n
18533 < it->glyph_row->glyphs[1 + TEXT_AREA])
18535 /* Save some values that must not be changed.
18536 Must save IT->c and IT->len because otherwise
18537 ITERATOR_AT_END_P wouldn't work anymore after
18538 append_space_for_newline has been called. */
18539 enum display_element_type saved_what = it->what;
18540 int saved_c = it->c, saved_len = it->len;
18541 int saved_char_to_display = it->char_to_display;
18542 int saved_x = it->current_x;
18543 int saved_face_id = it->face_id;
18544 int saved_box_end = it->end_of_box_run_p;
18545 struct text_pos saved_pos;
18546 Lisp_Object saved_object;
18547 struct face *face;
18549 saved_object = it->object;
18550 saved_pos = it->position;
18552 it->what = IT_CHARACTER;
18553 memset (&it->position, 0, sizeof it->position);
18554 it->object = make_number (0);
18555 it->c = it->char_to_display = ' ';
18556 it->len = 1;
18558 /* If the default face was remapped, be sure to use the
18559 remapped face for the appended newline. */
18560 if (default_face_p)
18561 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18562 else if (it->face_before_selective_p)
18563 it->face_id = it->saved_face_id;
18564 face = FACE_FROM_ID (it->f, it->face_id);
18565 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18566 /* In R2L rows, we will prepend a stretch glyph that will
18567 have the end_of_box_run_p flag set for it, so there's no
18568 need for the appended newline glyph to have that flag
18569 set. */
18570 if (it->glyph_row->reversed_p
18571 /* But if the appended newline glyph goes all the way to
18572 the end of the row, there will be no stretch glyph,
18573 so leave the box flag set. */
18574 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18575 it->end_of_box_run_p = 0;
18577 PRODUCE_GLYPHS (it);
18579 it->override_ascent = -1;
18580 it->constrain_row_ascent_descent_p = 0;
18581 it->current_x = saved_x;
18582 it->object = saved_object;
18583 it->position = saved_pos;
18584 it->what = saved_what;
18585 it->face_id = saved_face_id;
18586 it->len = saved_len;
18587 it->c = saved_c;
18588 it->char_to_display = saved_char_to_display;
18589 it->end_of_box_run_p = saved_box_end;
18590 return 1;
18594 return 0;
18598 /* Extend the face of the last glyph in the text area of IT->glyph_row
18599 to the end of the display line. Called from display_line. If the
18600 glyph row is empty, add a space glyph to it so that we know the
18601 face to draw. Set the glyph row flag fill_line_p. If the glyph
18602 row is R2L, prepend a stretch glyph to cover the empty space to the
18603 left of the leftmost glyph. */
18605 static void
18606 extend_face_to_end_of_line (struct it *it)
18608 struct face *face, *default_face;
18609 struct frame *f = it->f;
18611 /* If line is already filled, do nothing. Non window-system frames
18612 get a grace of one more ``pixel'' because their characters are
18613 1-``pixel'' wide, so they hit the equality too early. This grace
18614 is needed only for R2L rows that are not continued, to produce
18615 one extra blank where we could display the cursor. */
18616 if (it->current_x >= it->last_visible_x
18617 + (!FRAME_WINDOW_P (f)
18618 && it->glyph_row->reversed_p
18619 && !it->glyph_row->continued_p))
18620 return;
18622 /* The default face, possibly remapped. */
18623 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18625 /* Face extension extends the background and box of IT->face_id
18626 to the end of the line. If the background equals the background
18627 of the frame, we don't have to do anything. */
18628 if (it->face_before_selective_p)
18629 face = FACE_FROM_ID (f, it->saved_face_id);
18630 else
18631 face = FACE_FROM_ID (f, it->face_id);
18633 if (FRAME_WINDOW_P (f)
18634 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18635 && face->box == FACE_NO_BOX
18636 && face->background == FRAME_BACKGROUND_PIXEL (f)
18637 && !face->stipple
18638 && !it->glyph_row->reversed_p)
18639 return;
18641 /* Set the glyph row flag indicating that the face of the last glyph
18642 in the text area has to be drawn to the end of the text area. */
18643 it->glyph_row->fill_line_p = 1;
18645 /* If current character of IT is not ASCII, make sure we have the
18646 ASCII face. This will be automatically undone the next time
18647 get_next_display_element returns a multibyte character. Note
18648 that the character will always be single byte in unibyte
18649 text. */
18650 if (!ASCII_CHAR_P (it->c))
18652 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18655 if (FRAME_WINDOW_P (f))
18657 /* If the row is empty, add a space with the current face of IT,
18658 so that we know which face to draw. */
18659 if (it->glyph_row->used[TEXT_AREA] == 0)
18661 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18662 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18663 it->glyph_row->used[TEXT_AREA] = 1;
18665 #ifdef HAVE_WINDOW_SYSTEM
18666 if (it->glyph_row->reversed_p)
18668 /* Prepend a stretch glyph to the row, such that the
18669 rightmost glyph will be drawn flushed all the way to the
18670 right margin of the window. The stretch glyph that will
18671 occupy the empty space, if any, to the left of the
18672 glyphs. */
18673 struct font *font = face->font ? face->font : FRAME_FONT (f);
18674 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18675 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18676 struct glyph *g;
18677 int row_width, stretch_ascent, stretch_width;
18678 struct text_pos saved_pos;
18679 int saved_face_id, saved_avoid_cursor, saved_box_start;
18681 for (row_width = 0, g = row_start; g < row_end; g++)
18682 row_width += g->pixel_width;
18683 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18684 if (stretch_width > 0)
18686 stretch_ascent =
18687 (((it->ascent + it->descent)
18688 * FONT_BASE (font)) / FONT_HEIGHT (font));
18689 saved_pos = it->position;
18690 memset (&it->position, 0, sizeof it->position);
18691 saved_avoid_cursor = it->avoid_cursor_p;
18692 it->avoid_cursor_p = 1;
18693 saved_face_id = it->face_id;
18694 saved_box_start = it->start_of_box_run_p;
18695 /* The last row's stretch glyph should get the default
18696 face, to avoid painting the rest of the window with
18697 the region face, if the region ends at ZV. */
18698 if (it->glyph_row->ends_at_zv_p)
18699 it->face_id = default_face->id;
18700 else
18701 it->face_id = face->id;
18702 it->start_of_box_run_p = 0;
18703 append_stretch_glyph (it, make_number (0), stretch_width,
18704 it->ascent + it->descent, stretch_ascent);
18705 it->position = saved_pos;
18706 it->avoid_cursor_p = saved_avoid_cursor;
18707 it->face_id = saved_face_id;
18708 it->start_of_box_run_p = saved_box_start;
18711 #endif /* HAVE_WINDOW_SYSTEM */
18713 else
18715 /* Save some values that must not be changed. */
18716 int saved_x = it->current_x;
18717 struct text_pos saved_pos;
18718 Lisp_Object saved_object;
18719 enum display_element_type saved_what = it->what;
18720 int saved_face_id = it->face_id;
18722 saved_object = it->object;
18723 saved_pos = it->position;
18725 it->what = IT_CHARACTER;
18726 memset (&it->position, 0, sizeof it->position);
18727 it->object = make_number (0);
18728 it->c = it->char_to_display = ' ';
18729 it->len = 1;
18730 /* The last row's blank glyphs should get the default face, to
18731 avoid painting the rest of the window with the region face,
18732 if the region ends at ZV. */
18733 if (it->glyph_row->ends_at_zv_p)
18734 it->face_id = default_face->id;
18735 else
18736 it->face_id = face->id;
18738 PRODUCE_GLYPHS (it);
18740 while (it->current_x <= it->last_visible_x)
18741 PRODUCE_GLYPHS (it);
18743 /* Don't count these blanks really. It would let us insert a left
18744 truncation glyph below and make us set the cursor on them, maybe. */
18745 it->current_x = saved_x;
18746 it->object = saved_object;
18747 it->position = saved_pos;
18748 it->what = saved_what;
18749 it->face_id = saved_face_id;
18754 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18755 trailing whitespace. */
18757 static int
18758 trailing_whitespace_p (ptrdiff_t charpos)
18760 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18761 int c = 0;
18763 while (bytepos < ZV_BYTE
18764 && (c = FETCH_CHAR (bytepos),
18765 c == ' ' || c == '\t'))
18766 ++bytepos;
18768 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18770 if (bytepos != PT_BYTE)
18771 return 1;
18773 return 0;
18777 /* Highlight trailing whitespace, if any, in ROW. */
18779 static void
18780 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18782 int used = row->used[TEXT_AREA];
18784 if (used)
18786 struct glyph *start = row->glyphs[TEXT_AREA];
18787 struct glyph *glyph = start + used - 1;
18789 if (row->reversed_p)
18791 /* Right-to-left rows need to be processed in the opposite
18792 direction, so swap the edge pointers. */
18793 glyph = start;
18794 start = row->glyphs[TEXT_AREA] + used - 1;
18797 /* Skip over glyphs inserted to display the cursor at the
18798 end of a line, for extending the face of the last glyph
18799 to the end of the line on terminals, and for truncation
18800 and continuation glyphs. */
18801 if (!row->reversed_p)
18803 while (glyph >= start
18804 && glyph->type == CHAR_GLYPH
18805 && INTEGERP (glyph->object))
18806 --glyph;
18808 else
18810 while (glyph <= start
18811 && glyph->type == CHAR_GLYPH
18812 && INTEGERP (glyph->object))
18813 ++glyph;
18816 /* If last glyph is a space or stretch, and it's trailing
18817 whitespace, set the face of all trailing whitespace glyphs in
18818 IT->glyph_row to `trailing-whitespace'. */
18819 if ((row->reversed_p ? glyph <= start : glyph >= start)
18820 && BUFFERP (glyph->object)
18821 && (glyph->type == STRETCH_GLYPH
18822 || (glyph->type == CHAR_GLYPH
18823 && glyph->u.ch == ' '))
18824 && trailing_whitespace_p (glyph->charpos))
18826 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18827 if (face_id < 0)
18828 return;
18830 if (!row->reversed_p)
18832 while (glyph >= start
18833 && BUFFERP (glyph->object)
18834 && (glyph->type == STRETCH_GLYPH
18835 || (glyph->type == CHAR_GLYPH
18836 && glyph->u.ch == ' ')))
18837 (glyph--)->face_id = face_id;
18839 else
18841 while (glyph <= start
18842 && BUFFERP (glyph->object)
18843 && (glyph->type == STRETCH_GLYPH
18844 || (glyph->type == CHAR_GLYPH
18845 && glyph->u.ch == ' ')))
18846 (glyph++)->face_id = face_id;
18853 /* Value is non-zero if glyph row ROW should be
18854 considered to hold the buffer position CHARPOS. */
18856 static int
18857 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18859 int result = 1;
18861 if (charpos == CHARPOS (row->end.pos)
18862 || charpos == MATRIX_ROW_END_CHARPOS (row))
18864 /* Suppose the row ends on a string.
18865 Unless the row is continued, that means it ends on a newline
18866 in the string. If it's anything other than a display string
18867 (e.g., a before-string from an overlay), we don't want the
18868 cursor there. (This heuristic seems to give the optimal
18869 behavior for the various types of multi-line strings.)
18870 One exception: if the string has `cursor' property on one of
18871 its characters, we _do_ want the cursor there. */
18872 if (CHARPOS (row->end.string_pos) >= 0)
18874 if (row->continued_p)
18875 result = 1;
18876 else
18878 /* Check for `display' property. */
18879 struct glyph *beg = row->glyphs[TEXT_AREA];
18880 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18881 struct glyph *glyph;
18883 result = 0;
18884 for (glyph = end; glyph >= beg; --glyph)
18885 if (STRINGP (glyph->object))
18887 Lisp_Object prop
18888 = Fget_char_property (make_number (charpos),
18889 Qdisplay, Qnil);
18890 result =
18891 (!NILP (prop)
18892 && display_prop_string_p (prop, glyph->object));
18893 /* If there's a `cursor' property on one of the
18894 string's characters, this row is a cursor row,
18895 even though this is not a display string. */
18896 if (!result)
18898 Lisp_Object s = glyph->object;
18900 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18902 ptrdiff_t gpos = glyph->charpos;
18904 if (!NILP (Fget_char_property (make_number (gpos),
18905 Qcursor, s)))
18907 result = 1;
18908 break;
18912 break;
18916 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18918 /* If the row ends in middle of a real character,
18919 and the line is continued, we want the cursor here.
18920 That's because CHARPOS (ROW->end.pos) would equal
18921 PT if PT is before the character. */
18922 if (!row->ends_in_ellipsis_p)
18923 result = row->continued_p;
18924 else
18925 /* If the row ends in an ellipsis, then
18926 CHARPOS (ROW->end.pos) will equal point after the
18927 invisible text. We want that position to be displayed
18928 after the ellipsis. */
18929 result = 0;
18931 /* If the row ends at ZV, display the cursor at the end of that
18932 row instead of at the start of the row below. */
18933 else if (row->ends_at_zv_p)
18934 result = 1;
18935 else
18936 result = 0;
18939 return result;
18942 /* Value is non-zero if glyph row ROW should be
18943 used to hold the cursor. */
18945 static int
18946 cursor_row_p (struct glyph_row *row)
18948 return row_for_charpos_p (row, PT);
18953 /* Push the property PROP so that it will be rendered at the current
18954 position in IT. Return 1 if PROP was successfully pushed, 0
18955 otherwise. Called from handle_line_prefix to handle the
18956 `line-prefix' and `wrap-prefix' properties. */
18958 static int
18959 push_prefix_prop (struct it *it, Lisp_Object prop)
18961 struct text_pos pos =
18962 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18964 eassert (it->method == GET_FROM_BUFFER
18965 || it->method == GET_FROM_DISPLAY_VECTOR
18966 || it->method == GET_FROM_STRING);
18968 /* We need to save the current buffer/string position, so it will be
18969 restored by pop_it, because iterate_out_of_display_property
18970 depends on that being set correctly, but some situations leave
18971 it->position not yet set when this function is called. */
18972 push_it (it, &pos);
18974 if (STRINGP (prop))
18976 if (SCHARS (prop) == 0)
18978 pop_it (it);
18979 return 0;
18982 it->string = prop;
18983 it->string_from_prefix_prop_p = 1;
18984 it->multibyte_p = STRING_MULTIBYTE (it->string);
18985 it->current.overlay_string_index = -1;
18986 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18987 it->end_charpos = it->string_nchars = SCHARS (it->string);
18988 it->method = GET_FROM_STRING;
18989 it->stop_charpos = 0;
18990 it->prev_stop = 0;
18991 it->base_level_stop = 0;
18993 /* Force paragraph direction to be that of the parent
18994 buffer/string. */
18995 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18996 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18997 else
18998 it->paragraph_embedding = L2R;
19000 /* Set up the bidi iterator for this display string. */
19001 if (it->bidi_p)
19003 it->bidi_it.string.lstring = it->string;
19004 it->bidi_it.string.s = NULL;
19005 it->bidi_it.string.schars = it->end_charpos;
19006 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19007 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19008 it->bidi_it.string.unibyte = !it->multibyte_p;
19009 it->bidi_it.w = it->w;
19010 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19013 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19015 it->method = GET_FROM_STRETCH;
19016 it->object = prop;
19018 #ifdef HAVE_WINDOW_SYSTEM
19019 else if (IMAGEP (prop))
19021 it->what = IT_IMAGE;
19022 it->image_id = lookup_image (it->f, prop);
19023 it->method = GET_FROM_IMAGE;
19025 #endif /* HAVE_WINDOW_SYSTEM */
19026 else
19028 pop_it (it); /* bogus display property, give up */
19029 return 0;
19032 return 1;
19035 /* Return the character-property PROP at the current position in IT. */
19037 static Lisp_Object
19038 get_it_property (struct it *it, Lisp_Object prop)
19040 Lisp_Object position, object = it->object;
19042 if (STRINGP (object))
19043 position = make_number (IT_STRING_CHARPOS (*it));
19044 else if (BUFFERP (object))
19046 position = make_number (IT_CHARPOS (*it));
19047 object = it->window;
19049 else
19050 return Qnil;
19052 return Fget_char_property (position, prop, object);
19055 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19057 static void
19058 handle_line_prefix (struct it *it)
19060 Lisp_Object prefix;
19062 if (it->continuation_lines_width > 0)
19064 prefix = get_it_property (it, Qwrap_prefix);
19065 if (NILP (prefix))
19066 prefix = Vwrap_prefix;
19068 else
19070 prefix = get_it_property (it, Qline_prefix);
19071 if (NILP (prefix))
19072 prefix = Vline_prefix;
19074 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19076 /* If the prefix is wider than the window, and we try to wrap
19077 it, it would acquire its own wrap prefix, and so on till the
19078 iterator stack overflows. So, don't wrap the prefix. */
19079 it->line_wrap = TRUNCATE;
19080 it->avoid_cursor_p = 1;
19086 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19087 only for R2L lines from display_line and display_string, when they
19088 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19089 the line/string needs to be continued on the next glyph row. */
19090 static void
19091 unproduce_glyphs (struct it *it, int n)
19093 struct glyph *glyph, *end;
19095 eassert (it->glyph_row);
19096 eassert (it->glyph_row->reversed_p);
19097 eassert (it->area == TEXT_AREA);
19098 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19100 if (n > it->glyph_row->used[TEXT_AREA])
19101 n = it->glyph_row->used[TEXT_AREA];
19102 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19103 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19104 for ( ; glyph < end; glyph++)
19105 glyph[-n] = *glyph;
19108 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19109 and ROW->maxpos. */
19110 static void
19111 find_row_edges (struct it *it, struct glyph_row *row,
19112 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19113 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19115 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19116 lines' rows is implemented for bidi-reordered rows. */
19118 /* ROW->minpos is the value of min_pos, the minimal buffer position
19119 we have in ROW, or ROW->start.pos if that is smaller. */
19120 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19121 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19122 else
19123 /* We didn't find buffer positions smaller than ROW->start, or
19124 didn't find _any_ valid buffer positions in any of the glyphs,
19125 so we must trust the iterator's computed positions. */
19126 row->minpos = row->start.pos;
19127 if (max_pos <= 0)
19129 max_pos = CHARPOS (it->current.pos);
19130 max_bpos = BYTEPOS (it->current.pos);
19133 /* Here are the various use-cases for ending the row, and the
19134 corresponding values for ROW->maxpos:
19136 Line ends in a newline from buffer eol_pos + 1
19137 Line is continued from buffer max_pos + 1
19138 Line is truncated on right it->current.pos
19139 Line ends in a newline from string max_pos + 1(*)
19140 (*) + 1 only when line ends in a forward scan
19141 Line is continued from string max_pos
19142 Line is continued from display vector max_pos
19143 Line is entirely from a string min_pos == max_pos
19144 Line is entirely from a display vector min_pos == max_pos
19145 Line that ends at ZV ZV
19147 If you discover other use-cases, please add them here as
19148 appropriate. */
19149 if (row->ends_at_zv_p)
19150 row->maxpos = it->current.pos;
19151 else if (row->used[TEXT_AREA])
19153 int seen_this_string = 0;
19154 struct glyph_row *r1 = row - 1;
19156 /* Did we see the same display string on the previous row? */
19157 if (STRINGP (it->object)
19158 /* this is not the first row */
19159 && row > it->w->desired_matrix->rows
19160 /* previous row is not the header line */
19161 && !r1->mode_line_p
19162 /* previous row also ends in a newline from a string */
19163 && r1->ends_in_newline_from_string_p)
19165 struct glyph *start, *end;
19167 /* Search for the last glyph of the previous row that came
19168 from buffer or string. Depending on whether the row is
19169 L2R or R2L, we need to process it front to back or the
19170 other way round. */
19171 if (!r1->reversed_p)
19173 start = r1->glyphs[TEXT_AREA];
19174 end = start + r1->used[TEXT_AREA];
19175 /* Glyphs inserted by redisplay have an integer (zero)
19176 as their object. */
19177 while (end > start
19178 && INTEGERP ((end - 1)->object)
19179 && (end - 1)->charpos <= 0)
19180 --end;
19181 if (end > start)
19183 if (EQ ((end - 1)->object, it->object))
19184 seen_this_string = 1;
19186 else
19187 /* If all the glyphs of the previous row were inserted
19188 by redisplay, it means the previous row was
19189 produced from a single newline, which is only
19190 possible if that newline came from the same string
19191 as the one which produced this ROW. */
19192 seen_this_string = 1;
19194 else
19196 end = r1->glyphs[TEXT_AREA] - 1;
19197 start = end + r1->used[TEXT_AREA];
19198 while (end < start
19199 && INTEGERP ((end + 1)->object)
19200 && (end + 1)->charpos <= 0)
19201 ++end;
19202 if (end < start)
19204 if (EQ ((end + 1)->object, it->object))
19205 seen_this_string = 1;
19207 else
19208 seen_this_string = 1;
19211 /* Take note of each display string that covers a newline only
19212 once, the first time we see it. This is for when a display
19213 string includes more than one newline in it. */
19214 if (row->ends_in_newline_from_string_p && !seen_this_string)
19216 /* If we were scanning the buffer forward when we displayed
19217 the string, we want to account for at least one buffer
19218 position that belongs to this row (position covered by
19219 the display string), so that cursor positioning will
19220 consider this row as a candidate when point is at the end
19221 of the visual line represented by this row. This is not
19222 required when scanning back, because max_pos will already
19223 have a much larger value. */
19224 if (CHARPOS (row->end.pos) > max_pos)
19225 INC_BOTH (max_pos, max_bpos);
19226 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19228 else if (CHARPOS (it->eol_pos) > 0)
19229 SET_TEXT_POS (row->maxpos,
19230 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19231 else if (row->continued_p)
19233 /* If max_pos is different from IT's current position, it
19234 means IT->method does not belong to the display element
19235 at max_pos. However, it also means that the display
19236 element at max_pos was displayed in its entirety on this
19237 line, which is equivalent to saying that the next line
19238 starts at the next buffer position. */
19239 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19240 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19241 else
19243 INC_BOTH (max_pos, max_bpos);
19244 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19247 else if (row->truncated_on_right_p)
19248 /* display_line already called reseat_at_next_visible_line_start,
19249 which puts the iterator at the beginning of the next line, in
19250 the logical order. */
19251 row->maxpos = it->current.pos;
19252 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19253 /* A line that is entirely from a string/image/stretch... */
19254 row->maxpos = row->minpos;
19255 else
19256 emacs_abort ();
19258 else
19259 row->maxpos = it->current.pos;
19262 /* Construct the glyph row IT->glyph_row in the desired matrix of
19263 IT->w from text at the current position of IT. See dispextern.h
19264 for an overview of struct it. Value is non-zero if
19265 IT->glyph_row displays text, as opposed to a line displaying ZV
19266 only. */
19268 static int
19269 display_line (struct it *it)
19271 struct glyph_row *row = it->glyph_row;
19272 Lisp_Object overlay_arrow_string;
19273 struct it wrap_it;
19274 void *wrap_data = NULL;
19275 int may_wrap = 0, wrap_x IF_LINT (= 0);
19276 int wrap_row_used = -1;
19277 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19278 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19279 int wrap_row_extra_line_spacing IF_LINT (= 0);
19280 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19281 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19282 int cvpos;
19283 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19284 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19286 /* We always start displaying at hpos zero even if hscrolled. */
19287 eassert (it->hpos == 0 && it->current_x == 0);
19289 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19290 >= it->w->desired_matrix->nrows)
19292 it->w->nrows_scale_factor++;
19293 fonts_changed_p = 1;
19294 return 0;
19297 /* Is IT->w showing the region? */
19298 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19300 /* Clear the result glyph row and enable it. */
19301 prepare_desired_row (row);
19303 row->y = it->current_y;
19304 row->start = it->start;
19305 row->continuation_lines_width = it->continuation_lines_width;
19306 row->displays_text_p = 1;
19307 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19308 it->starts_in_middle_of_char_p = 0;
19310 /* Arrange the overlays nicely for our purposes. Usually, we call
19311 display_line on only one line at a time, in which case this
19312 can't really hurt too much, or we call it on lines which appear
19313 one after another in the buffer, in which case all calls to
19314 recenter_overlay_lists but the first will be pretty cheap. */
19315 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19317 /* Move over display elements that are not visible because we are
19318 hscrolled. This may stop at an x-position < IT->first_visible_x
19319 if the first glyph is partially visible or if we hit a line end. */
19320 if (it->current_x < it->first_visible_x)
19322 enum move_it_result move_result;
19324 this_line_min_pos = row->start.pos;
19325 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19326 MOVE_TO_POS | MOVE_TO_X);
19327 /* If we are under a large hscroll, move_it_in_display_line_to
19328 could hit the end of the line without reaching
19329 it->first_visible_x. Pretend that we did reach it. This is
19330 especially important on a TTY, where we will call
19331 extend_face_to_end_of_line, which needs to know how many
19332 blank glyphs to produce. */
19333 if (it->current_x < it->first_visible_x
19334 && (move_result == MOVE_NEWLINE_OR_CR
19335 || move_result == MOVE_POS_MATCH_OR_ZV))
19336 it->current_x = it->first_visible_x;
19338 /* Record the smallest positions seen while we moved over
19339 display elements that are not visible. This is needed by
19340 redisplay_internal for optimizing the case where the cursor
19341 stays inside the same line. The rest of this function only
19342 considers positions that are actually displayed, so
19343 RECORD_MAX_MIN_POS will not otherwise record positions that
19344 are hscrolled to the left of the left edge of the window. */
19345 min_pos = CHARPOS (this_line_min_pos);
19346 min_bpos = BYTEPOS (this_line_min_pos);
19348 else
19350 /* We only do this when not calling `move_it_in_display_line_to'
19351 above, because move_it_in_display_line_to calls
19352 handle_line_prefix itself. */
19353 handle_line_prefix (it);
19356 /* Get the initial row height. This is either the height of the
19357 text hscrolled, if there is any, or zero. */
19358 row->ascent = it->max_ascent;
19359 row->height = it->max_ascent + it->max_descent;
19360 row->phys_ascent = it->max_phys_ascent;
19361 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19362 row->extra_line_spacing = it->max_extra_line_spacing;
19364 /* Utility macro to record max and min buffer positions seen until now. */
19365 #define RECORD_MAX_MIN_POS(IT) \
19366 do \
19368 int composition_p = !STRINGP ((IT)->string) \
19369 && ((IT)->what == IT_COMPOSITION); \
19370 ptrdiff_t current_pos = \
19371 composition_p ? (IT)->cmp_it.charpos \
19372 : IT_CHARPOS (*(IT)); \
19373 ptrdiff_t current_bpos = \
19374 composition_p ? CHAR_TO_BYTE (current_pos) \
19375 : IT_BYTEPOS (*(IT)); \
19376 if (current_pos < min_pos) \
19378 min_pos = current_pos; \
19379 min_bpos = current_bpos; \
19381 if (IT_CHARPOS (*it) > max_pos) \
19383 max_pos = IT_CHARPOS (*it); \
19384 max_bpos = IT_BYTEPOS (*it); \
19387 while (0)
19389 /* Loop generating characters. The loop is left with IT on the next
19390 character to display. */
19391 while (1)
19393 int n_glyphs_before, hpos_before, x_before;
19394 int x, nglyphs;
19395 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19397 /* Retrieve the next thing to display. Value is zero if end of
19398 buffer reached. */
19399 if (!get_next_display_element (it))
19401 /* Maybe add a space at the end of this line that is used to
19402 display the cursor there under X. Set the charpos of the
19403 first glyph of blank lines not corresponding to any text
19404 to -1. */
19405 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19406 row->exact_window_width_line_p = 1;
19407 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19408 || row->used[TEXT_AREA] == 0)
19410 row->glyphs[TEXT_AREA]->charpos = -1;
19411 row->displays_text_p = 0;
19413 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19414 && (!MINI_WINDOW_P (it->w)
19415 || (minibuf_level && EQ (it->window, minibuf_window))))
19416 row->indicate_empty_line_p = 1;
19419 it->continuation_lines_width = 0;
19420 row->ends_at_zv_p = 1;
19421 /* A row that displays right-to-left text must always have
19422 its last face extended all the way to the end of line,
19423 even if this row ends in ZV, because we still write to
19424 the screen left to right. We also need to extend the
19425 last face if the default face is remapped to some
19426 different face, otherwise the functions that clear
19427 portions of the screen will clear with the default face's
19428 background color. */
19429 if (row->reversed_p
19430 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19431 extend_face_to_end_of_line (it);
19432 break;
19435 /* Now, get the metrics of what we want to display. This also
19436 generates glyphs in `row' (which is IT->glyph_row). */
19437 n_glyphs_before = row->used[TEXT_AREA];
19438 x = it->current_x;
19440 /* Remember the line height so far in case the next element doesn't
19441 fit on the line. */
19442 if (it->line_wrap != TRUNCATE)
19444 ascent = it->max_ascent;
19445 descent = it->max_descent;
19446 phys_ascent = it->max_phys_ascent;
19447 phys_descent = it->max_phys_descent;
19449 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19451 if (IT_DISPLAYING_WHITESPACE (it))
19452 may_wrap = 1;
19453 else if (may_wrap)
19455 SAVE_IT (wrap_it, *it, wrap_data);
19456 wrap_x = x;
19457 wrap_row_used = row->used[TEXT_AREA];
19458 wrap_row_ascent = row->ascent;
19459 wrap_row_height = row->height;
19460 wrap_row_phys_ascent = row->phys_ascent;
19461 wrap_row_phys_height = row->phys_height;
19462 wrap_row_extra_line_spacing = row->extra_line_spacing;
19463 wrap_row_min_pos = min_pos;
19464 wrap_row_min_bpos = min_bpos;
19465 wrap_row_max_pos = max_pos;
19466 wrap_row_max_bpos = max_bpos;
19467 may_wrap = 0;
19472 PRODUCE_GLYPHS (it);
19474 /* If this display element was in marginal areas, continue with
19475 the next one. */
19476 if (it->area != TEXT_AREA)
19478 row->ascent = max (row->ascent, it->max_ascent);
19479 row->height = max (row->height, it->max_ascent + it->max_descent);
19480 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19481 row->phys_height = max (row->phys_height,
19482 it->max_phys_ascent + it->max_phys_descent);
19483 row->extra_line_spacing = max (row->extra_line_spacing,
19484 it->max_extra_line_spacing);
19485 set_iterator_to_next (it, 1);
19486 continue;
19489 /* Does the display element fit on the line? If we truncate
19490 lines, we should draw past the right edge of the window. If
19491 we don't truncate, we want to stop so that we can display the
19492 continuation glyph before the right margin. If lines are
19493 continued, there are two possible strategies for characters
19494 resulting in more than 1 glyph (e.g. tabs): Display as many
19495 glyphs as possible in this line and leave the rest for the
19496 continuation line, or display the whole element in the next
19497 line. Original redisplay did the former, so we do it also. */
19498 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19499 hpos_before = it->hpos;
19500 x_before = x;
19502 if (/* Not a newline. */
19503 nglyphs > 0
19504 /* Glyphs produced fit entirely in the line. */
19505 && it->current_x < it->last_visible_x)
19507 it->hpos += nglyphs;
19508 row->ascent = max (row->ascent, it->max_ascent);
19509 row->height = max (row->height, it->max_ascent + it->max_descent);
19510 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19511 row->phys_height = max (row->phys_height,
19512 it->max_phys_ascent + it->max_phys_descent);
19513 row->extra_line_spacing = max (row->extra_line_spacing,
19514 it->max_extra_line_spacing);
19515 if (it->current_x - it->pixel_width < it->first_visible_x)
19516 row->x = x - it->first_visible_x;
19517 /* Record the maximum and minimum buffer positions seen so
19518 far in glyphs that will be displayed by this row. */
19519 if (it->bidi_p)
19520 RECORD_MAX_MIN_POS (it);
19522 else
19524 int i, new_x;
19525 struct glyph *glyph;
19527 for (i = 0; i < nglyphs; ++i, x = new_x)
19529 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19530 new_x = x + glyph->pixel_width;
19532 if (/* Lines are continued. */
19533 it->line_wrap != TRUNCATE
19534 && (/* Glyph doesn't fit on the line. */
19535 new_x > it->last_visible_x
19536 /* Or it fits exactly on a window system frame. */
19537 || (new_x == it->last_visible_x
19538 && FRAME_WINDOW_P (it->f)
19539 && (row->reversed_p
19540 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19541 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19543 /* End of a continued line. */
19545 if (it->hpos == 0
19546 || (new_x == it->last_visible_x
19547 && FRAME_WINDOW_P (it->f)
19548 && (row->reversed_p
19549 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19550 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19552 /* Current glyph is the only one on the line or
19553 fits exactly on the line. We must continue
19554 the line because we can't draw the cursor
19555 after the glyph. */
19556 row->continued_p = 1;
19557 it->current_x = new_x;
19558 it->continuation_lines_width += new_x;
19559 ++it->hpos;
19560 if (i == nglyphs - 1)
19562 /* If line-wrap is on, check if a previous
19563 wrap point was found. */
19564 if (wrap_row_used > 0
19565 /* Even if there is a previous wrap
19566 point, continue the line here as
19567 usual, if (i) the previous character
19568 was a space or tab AND (ii) the
19569 current character is not. */
19570 && (!may_wrap
19571 || IT_DISPLAYING_WHITESPACE (it)))
19572 goto back_to_wrap;
19574 /* Record the maximum and minimum buffer
19575 positions seen so far in glyphs that will be
19576 displayed by this row. */
19577 if (it->bidi_p)
19578 RECORD_MAX_MIN_POS (it);
19579 set_iterator_to_next (it, 1);
19580 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19582 if (!get_next_display_element (it))
19584 row->exact_window_width_line_p = 1;
19585 it->continuation_lines_width = 0;
19586 row->continued_p = 0;
19587 row->ends_at_zv_p = 1;
19589 else if (ITERATOR_AT_END_OF_LINE_P (it))
19591 row->continued_p = 0;
19592 row->exact_window_width_line_p = 1;
19596 else if (it->bidi_p)
19597 RECORD_MAX_MIN_POS (it);
19599 else if (CHAR_GLYPH_PADDING_P (*glyph)
19600 && !FRAME_WINDOW_P (it->f))
19602 /* A padding glyph that doesn't fit on this line.
19603 This means the whole character doesn't fit
19604 on the line. */
19605 if (row->reversed_p)
19606 unproduce_glyphs (it, row->used[TEXT_AREA]
19607 - n_glyphs_before);
19608 row->used[TEXT_AREA] = n_glyphs_before;
19610 /* Fill the rest of the row with continuation
19611 glyphs like in 20.x. */
19612 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19613 < row->glyphs[1 + TEXT_AREA])
19614 produce_special_glyphs (it, IT_CONTINUATION);
19616 row->continued_p = 1;
19617 it->current_x = x_before;
19618 it->continuation_lines_width += x_before;
19620 /* Restore the height to what it was before the
19621 element not fitting on the line. */
19622 it->max_ascent = ascent;
19623 it->max_descent = descent;
19624 it->max_phys_ascent = phys_ascent;
19625 it->max_phys_descent = phys_descent;
19627 else if (wrap_row_used > 0)
19629 back_to_wrap:
19630 if (row->reversed_p)
19631 unproduce_glyphs (it,
19632 row->used[TEXT_AREA] - wrap_row_used);
19633 RESTORE_IT (it, &wrap_it, wrap_data);
19634 it->continuation_lines_width += wrap_x;
19635 row->used[TEXT_AREA] = wrap_row_used;
19636 row->ascent = wrap_row_ascent;
19637 row->height = wrap_row_height;
19638 row->phys_ascent = wrap_row_phys_ascent;
19639 row->phys_height = wrap_row_phys_height;
19640 row->extra_line_spacing = wrap_row_extra_line_spacing;
19641 min_pos = wrap_row_min_pos;
19642 min_bpos = wrap_row_min_bpos;
19643 max_pos = wrap_row_max_pos;
19644 max_bpos = wrap_row_max_bpos;
19645 row->continued_p = 1;
19646 row->ends_at_zv_p = 0;
19647 row->exact_window_width_line_p = 0;
19648 it->continuation_lines_width += x;
19650 /* Make sure that a non-default face is extended
19651 up to the right margin of the window. */
19652 extend_face_to_end_of_line (it);
19654 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19656 /* A TAB that extends past the right edge of the
19657 window. This produces a single glyph on
19658 window system frames. We leave the glyph in
19659 this row and let it fill the row, but don't
19660 consume the TAB. */
19661 if ((row->reversed_p
19662 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19663 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19664 produce_special_glyphs (it, IT_CONTINUATION);
19665 it->continuation_lines_width += it->last_visible_x;
19666 row->ends_in_middle_of_char_p = 1;
19667 row->continued_p = 1;
19668 glyph->pixel_width = it->last_visible_x - x;
19669 it->starts_in_middle_of_char_p = 1;
19671 else
19673 /* Something other than a TAB that draws past
19674 the right edge of the window. Restore
19675 positions to values before the element. */
19676 if (row->reversed_p)
19677 unproduce_glyphs (it, row->used[TEXT_AREA]
19678 - (n_glyphs_before + i));
19679 row->used[TEXT_AREA] = n_glyphs_before + i;
19681 /* Display continuation glyphs. */
19682 it->current_x = x_before;
19683 it->continuation_lines_width += x;
19684 if (!FRAME_WINDOW_P (it->f)
19685 || (row->reversed_p
19686 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19687 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19688 produce_special_glyphs (it, IT_CONTINUATION);
19689 row->continued_p = 1;
19691 extend_face_to_end_of_line (it);
19693 if (nglyphs > 1 && i > 0)
19695 row->ends_in_middle_of_char_p = 1;
19696 it->starts_in_middle_of_char_p = 1;
19699 /* Restore the height to what it was before the
19700 element not fitting on the line. */
19701 it->max_ascent = ascent;
19702 it->max_descent = descent;
19703 it->max_phys_ascent = phys_ascent;
19704 it->max_phys_descent = phys_descent;
19707 break;
19709 else if (new_x > it->first_visible_x)
19711 /* Increment number of glyphs actually displayed. */
19712 ++it->hpos;
19714 /* Record the maximum and minimum buffer positions
19715 seen so far in glyphs that will be displayed by
19716 this row. */
19717 if (it->bidi_p)
19718 RECORD_MAX_MIN_POS (it);
19720 if (x < it->first_visible_x)
19721 /* Glyph is partially visible, i.e. row starts at
19722 negative X position. */
19723 row->x = x - it->first_visible_x;
19725 else
19727 /* Glyph is completely off the left margin of the
19728 window. This should not happen because of the
19729 move_it_in_display_line at the start of this
19730 function, unless the text display area of the
19731 window is empty. */
19732 eassert (it->first_visible_x <= it->last_visible_x);
19735 /* Even if this display element produced no glyphs at all,
19736 we want to record its position. */
19737 if (it->bidi_p && nglyphs == 0)
19738 RECORD_MAX_MIN_POS (it);
19740 row->ascent = max (row->ascent, it->max_ascent);
19741 row->height = max (row->height, it->max_ascent + it->max_descent);
19742 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19743 row->phys_height = max (row->phys_height,
19744 it->max_phys_ascent + it->max_phys_descent);
19745 row->extra_line_spacing = max (row->extra_line_spacing,
19746 it->max_extra_line_spacing);
19748 /* End of this display line if row is continued. */
19749 if (row->continued_p || row->ends_at_zv_p)
19750 break;
19753 at_end_of_line:
19754 /* Is this a line end? If yes, we're also done, after making
19755 sure that a non-default face is extended up to the right
19756 margin of the window. */
19757 if (ITERATOR_AT_END_OF_LINE_P (it))
19759 int used_before = row->used[TEXT_AREA];
19761 row->ends_in_newline_from_string_p = STRINGP (it->object);
19763 /* Add a space at the end of the line that is used to
19764 display the cursor there. */
19765 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19766 append_space_for_newline (it, 0);
19768 /* Extend the face to the end of the line. */
19769 extend_face_to_end_of_line (it);
19771 /* Make sure we have the position. */
19772 if (used_before == 0)
19773 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19775 /* Record the position of the newline, for use in
19776 find_row_edges. */
19777 it->eol_pos = it->current.pos;
19779 /* Consume the line end. This skips over invisible lines. */
19780 set_iterator_to_next (it, 1);
19781 it->continuation_lines_width = 0;
19782 break;
19785 /* Proceed with next display element. Note that this skips
19786 over lines invisible because of selective display. */
19787 set_iterator_to_next (it, 1);
19789 /* If we truncate lines, we are done when the last displayed
19790 glyphs reach past the right margin of the window. */
19791 if (it->line_wrap == TRUNCATE
19792 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19793 ? (it->current_x >= it->last_visible_x)
19794 : (it->current_x > it->last_visible_x)))
19796 /* Maybe add truncation glyphs. */
19797 if (!FRAME_WINDOW_P (it->f)
19798 || (row->reversed_p
19799 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19800 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19802 int i, n;
19804 if (!row->reversed_p)
19806 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19807 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19808 break;
19810 else
19812 for (i = 0; i < row->used[TEXT_AREA]; i++)
19813 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19814 break;
19815 /* Remove any padding glyphs at the front of ROW, to
19816 make room for the truncation glyphs we will be
19817 adding below. The loop below always inserts at
19818 least one truncation glyph, so also remove the
19819 last glyph added to ROW. */
19820 unproduce_glyphs (it, i + 1);
19821 /* Adjust i for the loop below. */
19822 i = row->used[TEXT_AREA] - (i + 1);
19825 it->current_x = x_before;
19826 if (!FRAME_WINDOW_P (it->f))
19828 for (n = row->used[TEXT_AREA]; i < n; ++i)
19830 row->used[TEXT_AREA] = i;
19831 produce_special_glyphs (it, IT_TRUNCATION);
19834 else
19836 row->used[TEXT_AREA] = i;
19837 produce_special_glyphs (it, IT_TRUNCATION);
19840 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19842 /* Don't truncate if we can overflow newline into fringe. */
19843 if (!get_next_display_element (it))
19845 it->continuation_lines_width = 0;
19846 row->ends_at_zv_p = 1;
19847 row->exact_window_width_line_p = 1;
19848 break;
19850 if (ITERATOR_AT_END_OF_LINE_P (it))
19852 row->exact_window_width_line_p = 1;
19853 goto at_end_of_line;
19855 it->current_x = x_before;
19858 row->truncated_on_right_p = 1;
19859 it->continuation_lines_width = 0;
19860 reseat_at_next_visible_line_start (it, 0);
19861 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19862 it->hpos = hpos_before;
19863 break;
19867 if (wrap_data)
19868 bidi_unshelve_cache (wrap_data, 1);
19870 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19871 at the left window margin. */
19872 if (it->first_visible_x
19873 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19875 if (!FRAME_WINDOW_P (it->f)
19876 || (row->reversed_p
19877 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19878 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19879 insert_left_trunc_glyphs (it);
19880 row->truncated_on_left_p = 1;
19883 /* Remember the position at which this line ends.
19885 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19886 cannot be before the call to find_row_edges below, since that is
19887 where these positions are determined. */
19888 row->end = it->current;
19889 if (!it->bidi_p)
19891 row->minpos = row->start.pos;
19892 row->maxpos = row->end.pos;
19894 else
19896 /* ROW->minpos and ROW->maxpos must be the smallest and
19897 `1 + the largest' buffer positions in ROW. But if ROW was
19898 bidi-reordered, these two positions can be anywhere in the
19899 row, so we must determine them now. */
19900 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19903 /* If the start of this line is the overlay arrow-position, then
19904 mark this glyph row as the one containing the overlay arrow.
19905 This is clearly a mess with variable size fonts. It would be
19906 better to let it be displayed like cursors under X. */
19907 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19908 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19909 !NILP (overlay_arrow_string)))
19911 /* Overlay arrow in window redisplay is a fringe bitmap. */
19912 if (STRINGP (overlay_arrow_string))
19914 struct glyph_row *arrow_row
19915 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19916 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19917 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19918 struct glyph *p = row->glyphs[TEXT_AREA];
19919 struct glyph *p2, *end;
19921 /* Copy the arrow glyphs. */
19922 while (glyph < arrow_end)
19923 *p++ = *glyph++;
19925 /* Throw away padding glyphs. */
19926 p2 = p;
19927 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19928 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19929 ++p2;
19930 if (p2 > p)
19932 while (p2 < end)
19933 *p++ = *p2++;
19934 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19937 else
19939 eassert (INTEGERP (overlay_arrow_string));
19940 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19942 overlay_arrow_seen = 1;
19945 /* Highlight trailing whitespace. */
19946 if (!NILP (Vshow_trailing_whitespace))
19947 highlight_trailing_whitespace (it->f, it->glyph_row);
19949 /* Compute pixel dimensions of this line. */
19950 compute_line_metrics (it);
19952 /* Implementation note: No changes in the glyphs of ROW or in their
19953 faces can be done past this point, because compute_line_metrics
19954 computes ROW's hash value and stores it within the glyph_row
19955 structure. */
19957 /* Record whether this row ends inside an ellipsis. */
19958 row->ends_in_ellipsis_p
19959 = (it->method == GET_FROM_DISPLAY_VECTOR
19960 && it->ellipsis_p);
19962 /* Save fringe bitmaps in this row. */
19963 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19964 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19965 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19966 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19968 it->left_user_fringe_bitmap = 0;
19969 it->left_user_fringe_face_id = 0;
19970 it->right_user_fringe_bitmap = 0;
19971 it->right_user_fringe_face_id = 0;
19973 /* Maybe set the cursor. */
19974 cvpos = it->w->cursor.vpos;
19975 if ((cvpos < 0
19976 /* In bidi-reordered rows, keep checking for proper cursor
19977 position even if one has been found already, because buffer
19978 positions in such rows change non-linearly with ROW->VPOS,
19979 when a line is continued. One exception: when we are at ZV,
19980 display cursor on the first suitable glyph row, since all
19981 the empty rows after that also have their position set to ZV. */
19982 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19983 lines' rows is implemented for bidi-reordered rows. */
19984 || (it->bidi_p
19985 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19986 && PT >= MATRIX_ROW_START_CHARPOS (row)
19987 && PT <= MATRIX_ROW_END_CHARPOS (row)
19988 && cursor_row_p (row))
19989 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19991 /* Prepare for the next line. This line starts horizontally at (X
19992 HPOS) = (0 0). Vertical positions are incremented. As a
19993 convenience for the caller, IT->glyph_row is set to the next
19994 row to be used. */
19995 it->current_x = it->hpos = 0;
19996 it->current_y += row->height;
19997 SET_TEXT_POS (it->eol_pos, 0, 0);
19998 ++it->vpos;
19999 ++it->glyph_row;
20000 /* The next row should by default use the same value of the
20001 reversed_p flag as this one. set_iterator_to_next decides when
20002 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20003 the flag accordingly. */
20004 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20005 it->glyph_row->reversed_p = row->reversed_p;
20006 it->start = row->end;
20007 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20009 #undef RECORD_MAX_MIN_POS
20012 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20013 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20014 doc: /* Return paragraph direction at point in BUFFER.
20015 Value is either `left-to-right' or `right-to-left'.
20016 If BUFFER is omitted or nil, it defaults to the current buffer.
20018 Paragraph direction determines how the text in the paragraph is displayed.
20019 In left-to-right paragraphs, text begins at the left margin of the window
20020 and the reading direction is generally left to right. In right-to-left
20021 paragraphs, text begins at the right margin and is read from right to left.
20023 See also `bidi-paragraph-direction'. */)
20024 (Lisp_Object buffer)
20026 struct buffer *buf = current_buffer;
20027 struct buffer *old = buf;
20029 if (! NILP (buffer))
20031 CHECK_BUFFER (buffer);
20032 buf = XBUFFER (buffer);
20035 if (NILP (BVAR (buf, bidi_display_reordering))
20036 || NILP (BVAR (buf, enable_multibyte_characters))
20037 /* When we are loading loadup.el, the character property tables
20038 needed for bidi iteration are not yet available. */
20039 || !NILP (Vpurify_flag))
20040 return Qleft_to_right;
20041 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20042 return BVAR (buf, bidi_paragraph_direction);
20043 else
20045 /* Determine the direction from buffer text. We could try to
20046 use current_matrix if it is up to date, but this seems fast
20047 enough as it is. */
20048 struct bidi_it itb;
20049 ptrdiff_t pos = BUF_PT (buf);
20050 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20051 int c;
20052 void *itb_data = bidi_shelve_cache ();
20054 set_buffer_temp (buf);
20055 /* bidi_paragraph_init finds the base direction of the paragraph
20056 by searching forward from paragraph start. We need the base
20057 direction of the current or _previous_ paragraph, so we need
20058 to make sure we are within that paragraph. To that end, find
20059 the previous non-empty line. */
20060 if (pos >= ZV && pos > BEGV)
20061 DEC_BOTH (pos, bytepos);
20062 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20063 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20065 while ((c = FETCH_BYTE (bytepos)) == '\n'
20066 || c == ' ' || c == '\t' || c == '\f')
20068 if (bytepos <= BEGV_BYTE)
20069 break;
20070 bytepos--;
20071 pos--;
20073 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20074 bytepos--;
20076 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20077 itb.paragraph_dir = NEUTRAL_DIR;
20078 itb.string.s = NULL;
20079 itb.string.lstring = Qnil;
20080 itb.string.bufpos = 0;
20081 itb.string.unibyte = 0;
20082 /* We have no window to use here for ignoring window-specific
20083 overlays. Using NULL for window pointer will cause
20084 compute_display_string_pos to use the current buffer. */
20085 itb.w = NULL;
20086 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20087 bidi_unshelve_cache (itb_data, 0);
20088 set_buffer_temp (old);
20089 switch (itb.paragraph_dir)
20091 case L2R:
20092 return Qleft_to_right;
20093 break;
20094 case R2L:
20095 return Qright_to_left;
20096 break;
20097 default:
20098 emacs_abort ();
20103 DEFUN ("move-point-visually", Fmove_point_visually,
20104 Smove_point_visually, 1, 1, 0,
20105 doc: /* Move point in the visual order in the specified DIRECTION.
20106 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20107 left.
20109 Value is the new character position of point. */)
20110 (Lisp_Object direction)
20112 struct window *w = XWINDOW (selected_window);
20113 struct buffer *b = NULL;
20114 struct glyph_row *row;
20115 int dir;
20116 Lisp_Object paragraph_dir;
20118 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20119 (!(ROW)->continued_p \
20120 && INTEGERP ((GLYPH)->object) \
20121 && (GLYPH)->type == CHAR_GLYPH \
20122 && (GLYPH)->u.ch == ' ' \
20123 && (GLYPH)->charpos >= 0 \
20124 && !(GLYPH)->avoid_cursor_p)
20126 CHECK_NUMBER (direction);
20127 dir = XINT (direction);
20128 if (dir > 0)
20129 dir = 1;
20130 else
20131 dir = -1;
20133 if (BUFFERP (w->contents))
20134 b = XBUFFER (w->contents);
20136 /* If current matrix is up-to-date, we can use the information
20137 recorded in the glyphs, at least as long as the goal is on the
20138 screen. */
20139 if (w->window_end_valid
20140 && !windows_or_buffers_changed
20141 && b
20142 && !b->clip_changed
20143 && !b->prevent_redisplay_optimizations_p
20144 && w->last_modified >= BUF_MODIFF (b)
20145 && w->last_overlay_modified >= BUF_OVERLAY_MODIFF (b)
20146 && w->cursor.vpos >= 0
20147 && w->cursor.vpos < w->current_matrix->nrows
20148 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20150 struct glyph *g = row->glyphs[TEXT_AREA];
20151 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20152 struct glyph *gpt = g + w->cursor.hpos;
20154 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20156 if (BUFFERP (g->object) && g->charpos != PT)
20158 SET_PT (g->charpos);
20159 w->cursor.vpos = -1;
20160 return make_number (PT);
20162 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20164 ptrdiff_t new_pos;
20166 if (BUFFERP (gpt->object))
20168 new_pos = PT;
20169 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20170 new_pos += (row->reversed_p ? -dir : dir);
20171 else
20172 new_pos -= (row->reversed_p ? -dir : dir);;
20174 else if (BUFFERP (g->object))
20175 new_pos = g->charpos;
20176 else
20177 break;
20178 SET_PT (new_pos);
20179 w->cursor.vpos = -1;
20180 return make_number (PT);
20182 else if (ROW_GLYPH_NEWLINE_P (row, g))
20184 /* Glyphs inserted at the end of a non-empty line for
20185 positioning the cursor have zero charpos, so we must
20186 deduce the value of point by other means. */
20187 if (g->charpos > 0)
20188 SET_PT (g->charpos);
20189 else if (row->ends_at_zv_p && PT != ZV)
20190 SET_PT (ZV);
20191 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20192 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20193 else
20194 break;
20195 w->cursor.vpos = -1;
20196 return make_number (PT);
20199 if (g == e || INTEGERP (g->object))
20201 if (row->truncated_on_left_p || row->truncated_on_right_p)
20202 goto simulate_display;
20203 if (!row->reversed_p)
20204 row += dir;
20205 else
20206 row -= dir;
20207 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20208 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20209 goto simulate_display;
20211 if (dir > 0)
20213 if (row->reversed_p && !row->continued_p)
20215 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20216 w->cursor.vpos = -1;
20217 return make_number (PT);
20219 g = row->glyphs[TEXT_AREA];
20220 e = g + row->used[TEXT_AREA];
20221 for ( ; g < e; g++)
20223 if (BUFFERP (g->object)
20224 /* Empty lines have only one glyph, which stands
20225 for the newline, and whose charpos is the
20226 buffer position of the newline. */
20227 || ROW_GLYPH_NEWLINE_P (row, g)
20228 /* When the buffer ends in a newline, the line at
20229 EOB also has one glyph, but its charpos is -1. */
20230 || (row->ends_at_zv_p
20231 && !row->reversed_p
20232 && INTEGERP (g->object)
20233 && g->type == CHAR_GLYPH
20234 && g->u.ch == ' '))
20236 if (g->charpos > 0)
20237 SET_PT (g->charpos);
20238 else if (!row->reversed_p
20239 && row->ends_at_zv_p
20240 && PT != ZV)
20241 SET_PT (ZV);
20242 else
20243 continue;
20244 w->cursor.vpos = -1;
20245 return make_number (PT);
20249 else
20251 if (!row->reversed_p && !row->continued_p)
20253 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20254 w->cursor.vpos = -1;
20255 return make_number (PT);
20257 e = row->glyphs[TEXT_AREA];
20258 g = e + row->used[TEXT_AREA] - 1;
20259 for ( ; g >= e; g--)
20261 if (BUFFERP (g->object)
20262 || (ROW_GLYPH_NEWLINE_P (row, g)
20263 && g->charpos > 0)
20264 /* Empty R2L lines on GUI frames have the buffer
20265 position of the newline stored in the stretch
20266 glyph. */
20267 || g->type == STRETCH_GLYPH
20268 || (row->ends_at_zv_p
20269 && row->reversed_p
20270 && INTEGERP (g->object)
20271 && g->type == CHAR_GLYPH
20272 && g->u.ch == ' '))
20274 if (g->charpos > 0)
20275 SET_PT (g->charpos);
20276 else if (row->reversed_p
20277 && row->ends_at_zv_p
20278 && PT != ZV)
20279 SET_PT (ZV);
20280 else
20281 continue;
20282 w->cursor.vpos = -1;
20283 return make_number (PT);
20290 simulate_display:
20292 /* If we wind up here, we failed to move by using the glyphs, so we
20293 need to simulate display instead. */
20295 if (b)
20296 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20297 else
20298 paragraph_dir = Qleft_to_right;
20299 if (EQ (paragraph_dir, Qright_to_left))
20300 dir = -dir;
20301 if (PT <= BEGV && dir < 0)
20302 xsignal0 (Qbeginning_of_buffer);
20303 else if (PT >= ZV && dir > 0)
20304 xsignal0 (Qend_of_buffer);
20305 else
20307 struct text_pos pt;
20308 struct it it;
20309 int pt_x, target_x, pixel_width, pt_vpos;
20310 bool at_eol_p;
20311 bool overshoot_expected = false;
20312 bool target_is_eol_p = false;
20314 /* Setup the arena. */
20315 SET_TEXT_POS (pt, PT, PT_BYTE);
20316 start_display (&it, w, pt);
20318 if (it.cmp_it.id < 0
20319 && it.method == GET_FROM_STRING
20320 && it.area == TEXT_AREA
20321 && it.string_from_display_prop_p
20322 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20323 overshoot_expected = true;
20325 /* Find the X coordinate of point. We start from the beginning
20326 of this or previous line to make sure we are before point in
20327 the logical order (since the move_it_* functions can only
20328 move forward). */
20329 reseat_at_previous_visible_line_start (&it);
20330 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20331 if (IT_CHARPOS (it) != PT)
20332 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20333 -1, -1, -1, MOVE_TO_POS);
20334 pt_x = it.current_x;
20335 pt_vpos = it.vpos;
20336 if (dir > 0 || overshoot_expected)
20338 struct glyph_row *row = it.glyph_row;
20340 /* When point is at beginning of line, we don't have
20341 information about the glyph there loaded into struct
20342 it. Calling get_next_display_element fixes that. */
20343 if (pt_x == 0)
20344 get_next_display_element (&it);
20345 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20346 it.glyph_row = NULL;
20347 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20348 it.glyph_row = row;
20349 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20350 it, lest it will become out of sync with it's buffer
20351 position. */
20352 it.current_x = pt_x;
20354 else
20355 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20356 pixel_width = it.pixel_width;
20357 if (overshoot_expected && at_eol_p)
20358 pixel_width = 0;
20359 else if (pixel_width <= 0)
20360 pixel_width = 1;
20362 /* If there's a display string at point, we are actually at the
20363 glyph to the left of point, so we need to correct the X
20364 coordinate. */
20365 if (overshoot_expected)
20366 pt_x += pixel_width;
20368 /* Compute target X coordinate, either to the left or to the
20369 right of point. On TTY frames, all characters have the same
20370 pixel width of 1, so we can use that. On GUI frames we don't
20371 have an easy way of getting at the pixel width of the
20372 character to the left of point, so we use a different method
20373 of getting to that place. */
20374 if (dir > 0)
20375 target_x = pt_x + pixel_width;
20376 else
20377 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20379 /* Target X coordinate could be one line above or below the line
20380 of point, in which case we need to adjust the target X
20381 coordinate. Also, if moving to the left, we need to begin at
20382 the left edge of the point's screen line. */
20383 if (dir < 0)
20385 if (pt_x > 0)
20387 start_display (&it, w, pt);
20388 reseat_at_previous_visible_line_start (&it);
20389 it.current_x = it.current_y = it.hpos = 0;
20390 if (pt_vpos != 0)
20391 move_it_by_lines (&it, pt_vpos);
20393 else
20395 move_it_by_lines (&it, -1);
20396 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20397 target_is_eol_p = true;
20400 else
20402 if (at_eol_p
20403 || (target_x >= it.last_visible_x
20404 && it.line_wrap != TRUNCATE))
20406 if (pt_x > 0)
20407 move_it_by_lines (&it, 0);
20408 move_it_by_lines (&it, 1);
20409 target_x = 0;
20413 /* Move to the target X coordinate. */
20414 #ifdef HAVE_WINDOW_SYSTEM
20415 /* On GUI frames, as we don't know the X coordinate of the
20416 character to the left of point, moving point to the left
20417 requires walking, one grapheme cluster at a time, until we
20418 find ourself at a place immediately to the left of the
20419 character at point. */
20420 if (FRAME_WINDOW_P (it.f) && dir < 0)
20422 struct text_pos new_pos = it.current.pos;
20423 enum move_it_result rc = MOVE_X_REACHED;
20425 while (it.current_x + it.pixel_width <= target_x
20426 && rc == MOVE_X_REACHED)
20428 int new_x = it.current_x + it.pixel_width;
20430 new_pos = it.current.pos;
20431 if (new_x == it.current_x)
20432 new_x++;
20433 rc = move_it_in_display_line_to (&it, ZV, new_x,
20434 MOVE_TO_POS | MOVE_TO_X);
20435 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20436 break;
20438 /* If we ended up on a composed character inside
20439 bidi-reordered text (e.g., Hebrew text with diacritics),
20440 the iterator gives us the buffer position of the last (in
20441 logical order) character of the composed grapheme cluster,
20442 which is not what we want. So we cheat: we compute the
20443 character position of the character that follows (in the
20444 logical order) the one where the above loop stopped. That
20445 character will appear on display to the left of point. */
20446 if (it.bidi_p
20447 && it.bidi_it.scan_dir == -1
20448 && new_pos.charpos - IT_CHARPOS (it) > 1)
20450 new_pos.charpos = IT_CHARPOS (it) + 1;
20451 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20453 it.current.pos = new_pos;
20455 else
20456 #endif
20457 if (it.current_x != target_x)
20458 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20460 /* When lines are truncated, the above loop will stop at the
20461 window edge. But we want to get to the end of line, even if
20462 it is beyond the window edge; automatic hscroll will then
20463 scroll the window to show point as appropriate. */
20464 if (target_is_eol_p && it.line_wrap == TRUNCATE
20465 && get_next_display_element (&it))
20467 struct text_pos new_pos = it.current.pos;
20469 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20471 set_iterator_to_next (&it, 0);
20472 if (it.method == GET_FROM_BUFFER)
20473 new_pos = it.current.pos;
20474 if (!get_next_display_element (&it))
20475 break;
20478 it.current.pos = new_pos;
20481 /* If we ended up in a display string that covers point, move to
20482 buffer position to the right in the visual order. */
20483 if (dir > 0)
20485 while (IT_CHARPOS (it) == PT)
20487 set_iterator_to_next (&it, 0);
20488 if (!get_next_display_element (&it))
20489 break;
20493 /* Move point to that position. */
20494 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20497 return make_number (PT);
20499 #undef ROW_GLYPH_NEWLINE_P
20503 /***********************************************************************
20504 Menu Bar
20505 ***********************************************************************/
20507 /* Redisplay the menu bar in the frame for window W.
20509 The menu bar of X frames that don't have X toolkit support is
20510 displayed in a special window W->frame->menu_bar_window.
20512 The menu bar of terminal frames is treated specially as far as
20513 glyph matrices are concerned. Menu bar lines are not part of
20514 windows, so the update is done directly on the frame matrix rows
20515 for the menu bar. */
20517 static void
20518 display_menu_bar (struct window *w)
20520 struct frame *f = XFRAME (WINDOW_FRAME (w));
20521 struct it it;
20522 Lisp_Object items;
20523 int i;
20525 /* Don't do all this for graphical frames. */
20526 #ifdef HAVE_NTGUI
20527 if (FRAME_W32_P (f))
20528 return;
20529 #endif
20530 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20531 if (FRAME_X_P (f))
20532 return;
20533 #endif
20535 #ifdef HAVE_NS
20536 if (FRAME_NS_P (f))
20537 return;
20538 #endif /* HAVE_NS */
20540 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20541 eassert (!FRAME_WINDOW_P (f));
20542 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20543 it.first_visible_x = 0;
20544 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20545 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20546 if (FRAME_WINDOW_P (f))
20548 /* Menu bar lines are displayed in the desired matrix of the
20549 dummy window menu_bar_window. */
20550 struct window *menu_w;
20551 menu_w = XWINDOW (f->menu_bar_window);
20552 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20553 MENU_FACE_ID);
20554 it.first_visible_x = 0;
20555 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20557 else
20558 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20560 /* This is a TTY frame, i.e. character hpos/vpos are used as
20561 pixel x/y. */
20562 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20563 MENU_FACE_ID);
20564 it.first_visible_x = 0;
20565 it.last_visible_x = FRAME_COLS (f);
20568 /* FIXME: This should be controlled by a user option. See the
20569 comments in redisplay_tool_bar and display_mode_line about
20570 this. */
20571 it.paragraph_embedding = L2R;
20573 /* Clear all rows of the menu bar. */
20574 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20576 struct glyph_row *row = it.glyph_row + i;
20577 clear_glyph_row (row);
20578 row->enabled_p = 1;
20579 row->full_width_p = 1;
20582 /* Display all items of the menu bar. */
20583 items = FRAME_MENU_BAR_ITEMS (it.f);
20584 for (i = 0; i < ASIZE (items); i += 4)
20586 Lisp_Object string;
20588 /* Stop at nil string. */
20589 string = AREF (items, i + 1);
20590 if (NILP (string))
20591 break;
20593 /* Remember where item was displayed. */
20594 ASET (items, i + 3, make_number (it.hpos));
20596 /* Display the item, pad with one space. */
20597 if (it.current_x < it.last_visible_x)
20598 display_string (NULL, string, Qnil, 0, 0, &it,
20599 SCHARS (string) + 1, 0, 0, -1);
20602 /* Fill out the line with spaces. */
20603 if (it.current_x < it.last_visible_x)
20604 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20606 /* Compute the total height of the lines. */
20607 compute_line_metrics (&it);
20612 /***********************************************************************
20613 Mode Line
20614 ***********************************************************************/
20616 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20617 FORCE is non-zero, redisplay mode lines unconditionally.
20618 Otherwise, redisplay only mode lines that are garbaged. Value is
20619 the number of windows whose mode lines were redisplayed. */
20621 static int
20622 redisplay_mode_lines (Lisp_Object window, int force)
20624 int nwindows = 0;
20626 while (!NILP (window))
20628 struct window *w = XWINDOW (window);
20630 if (WINDOWP (w->contents))
20631 nwindows += redisplay_mode_lines (w->contents, force);
20632 else if (force
20633 || FRAME_GARBAGED_P (XFRAME (w->frame))
20634 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20636 struct text_pos lpoint;
20637 struct buffer *old = current_buffer;
20639 /* Set the window's buffer for the mode line display. */
20640 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20641 set_buffer_internal_1 (XBUFFER (w->contents));
20643 /* Point refers normally to the selected window. For any
20644 other window, set up appropriate value. */
20645 if (!EQ (window, selected_window))
20647 struct text_pos pt;
20649 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20650 if (CHARPOS (pt) < BEGV)
20651 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20652 else if (CHARPOS (pt) > (ZV - 1))
20653 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20654 else
20655 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20658 /* Display mode lines. */
20659 clear_glyph_matrix (w->desired_matrix);
20660 if (display_mode_lines (w))
20662 ++nwindows;
20663 w->must_be_updated_p = 1;
20666 /* Restore old settings. */
20667 set_buffer_internal_1 (old);
20668 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20671 window = w->next;
20674 return nwindows;
20678 /* Display the mode and/or header line of window W. Value is the
20679 sum number of mode lines and header lines displayed. */
20681 static int
20682 display_mode_lines (struct window *w)
20684 Lisp_Object old_selected_window = selected_window;
20685 Lisp_Object old_selected_frame = selected_frame;
20686 Lisp_Object new_frame = w->frame;
20687 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20688 int n = 0;
20690 selected_frame = new_frame;
20691 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20692 or window's point, then we'd need select_window_1 here as well. */
20693 XSETWINDOW (selected_window, w);
20694 XFRAME (new_frame)->selected_window = selected_window;
20696 /* These will be set while the mode line specs are processed. */
20697 line_number_displayed = 0;
20698 w->column_number_displayed = -1;
20700 if (WINDOW_WANTS_MODELINE_P (w))
20702 struct window *sel_w = XWINDOW (old_selected_window);
20704 /* Select mode line face based on the real selected window. */
20705 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20706 BVAR (current_buffer, mode_line_format));
20707 ++n;
20710 if (WINDOW_WANTS_HEADER_LINE_P (w))
20712 display_mode_line (w, HEADER_LINE_FACE_ID,
20713 BVAR (current_buffer, header_line_format));
20714 ++n;
20717 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20718 selected_frame = old_selected_frame;
20719 selected_window = old_selected_window;
20720 return n;
20724 /* Display mode or header line of window W. FACE_ID specifies which
20725 line to display; it is either MODE_LINE_FACE_ID or
20726 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20727 display. Value is the pixel height of the mode/header line
20728 displayed. */
20730 static int
20731 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20733 struct it it;
20734 struct face *face;
20735 ptrdiff_t count = SPECPDL_INDEX ();
20737 init_iterator (&it, w, -1, -1, NULL, face_id);
20738 /* Don't extend on a previously drawn mode-line.
20739 This may happen if called from pos_visible_p. */
20740 it.glyph_row->enabled_p = 0;
20741 prepare_desired_row (it.glyph_row);
20743 it.glyph_row->mode_line_p = 1;
20745 /* FIXME: This should be controlled by a user option. But
20746 supporting such an option is not trivial, since the mode line is
20747 made up of many separate strings. */
20748 it.paragraph_embedding = L2R;
20750 record_unwind_protect (unwind_format_mode_line,
20751 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20753 mode_line_target = MODE_LINE_DISPLAY;
20755 /* Temporarily make frame's keyboard the current kboard so that
20756 kboard-local variables in the mode_line_format will get the right
20757 values. */
20758 push_kboard (FRAME_KBOARD (it.f));
20759 record_unwind_save_match_data ();
20760 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20761 pop_kboard ();
20763 unbind_to (count, Qnil);
20765 /* Fill up with spaces. */
20766 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20768 compute_line_metrics (&it);
20769 it.glyph_row->full_width_p = 1;
20770 it.glyph_row->continued_p = 0;
20771 it.glyph_row->truncated_on_left_p = 0;
20772 it.glyph_row->truncated_on_right_p = 0;
20774 /* Make a 3D mode-line have a shadow at its right end. */
20775 face = FACE_FROM_ID (it.f, face_id);
20776 extend_face_to_end_of_line (&it);
20777 if (face->box != FACE_NO_BOX)
20779 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20780 + it.glyph_row->used[TEXT_AREA] - 1);
20781 last->right_box_line_p = 1;
20784 return it.glyph_row->height;
20787 /* Move element ELT in LIST to the front of LIST.
20788 Return the updated list. */
20790 static Lisp_Object
20791 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20793 register Lisp_Object tail, prev;
20794 register Lisp_Object tem;
20796 tail = list;
20797 prev = Qnil;
20798 while (CONSP (tail))
20800 tem = XCAR (tail);
20802 if (EQ (elt, tem))
20804 /* Splice out the link TAIL. */
20805 if (NILP (prev))
20806 list = XCDR (tail);
20807 else
20808 Fsetcdr (prev, XCDR (tail));
20810 /* Now make it the first. */
20811 Fsetcdr (tail, list);
20812 return tail;
20814 else
20815 prev = tail;
20816 tail = XCDR (tail);
20817 QUIT;
20820 /* Not found--return unchanged LIST. */
20821 return list;
20824 /* Contribute ELT to the mode line for window IT->w. How it
20825 translates into text depends on its data type.
20827 IT describes the display environment in which we display, as usual.
20829 DEPTH is the depth in recursion. It is used to prevent
20830 infinite recursion here.
20832 FIELD_WIDTH is the number of characters the display of ELT should
20833 occupy in the mode line, and PRECISION is the maximum number of
20834 characters to display from ELT's representation. See
20835 display_string for details.
20837 Returns the hpos of the end of the text generated by ELT.
20839 PROPS is a property list to add to any string we encounter.
20841 If RISKY is nonzero, remove (disregard) any properties in any string
20842 we encounter, and ignore :eval and :propertize.
20844 The global variable `mode_line_target' determines whether the
20845 output is passed to `store_mode_line_noprop',
20846 `store_mode_line_string', or `display_string'. */
20848 static int
20849 display_mode_element (struct it *it, int depth, int field_width, int precision,
20850 Lisp_Object elt, Lisp_Object props, int risky)
20852 int n = 0, field, prec;
20853 int literal = 0;
20855 tail_recurse:
20856 if (depth > 100)
20857 elt = build_string ("*too-deep*");
20859 depth++;
20861 switch (XTYPE (elt))
20863 case Lisp_String:
20865 /* A string: output it and check for %-constructs within it. */
20866 unsigned char c;
20867 ptrdiff_t offset = 0;
20869 if (SCHARS (elt) > 0
20870 && (!NILP (props) || risky))
20872 Lisp_Object oprops, aelt;
20873 oprops = Ftext_properties_at (make_number (0), elt);
20875 /* If the starting string's properties are not what
20876 we want, translate the string. Also, if the string
20877 is risky, do that anyway. */
20879 if (NILP (Fequal (props, oprops)) || risky)
20881 /* If the starting string has properties,
20882 merge the specified ones onto the existing ones. */
20883 if (! NILP (oprops) && !risky)
20885 Lisp_Object tem;
20887 oprops = Fcopy_sequence (oprops);
20888 tem = props;
20889 while (CONSP (tem))
20891 oprops = Fplist_put (oprops, XCAR (tem),
20892 XCAR (XCDR (tem)));
20893 tem = XCDR (XCDR (tem));
20895 props = oprops;
20898 aelt = Fassoc (elt, mode_line_proptrans_alist);
20899 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20901 /* AELT is what we want. Move it to the front
20902 without consing. */
20903 elt = XCAR (aelt);
20904 mode_line_proptrans_alist
20905 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20907 else
20909 Lisp_Object tem;
20911 /* If AELT has the wrong props, it is useless.
20912 so get rid of it. */
20913 if (! NILP (aelt))
20914 mode_line_proptrans_alist
20915 = Fdelq (aelt, mode_line_proptrans_alist);
20917 elt = Fcopy_sequence (elt);
20918 Fset_text_properties (make_number (0), Flength (elt),
20919 props, elt);
20920 /* Add this item to mode_line_proptrans_alist. */
20921 mode_line_proptrans_alist
20922 = Fcons (Fcons (elt, props),
20923 mode_line_proptrans_alist);
20924 /* Truncate mode_line_proptrans_alist
20925 to at most 50 elements. */
20926 tem = Fnthcdr (make_number (50),
20927 mode_line_proptrans_alist);
20928 if (! NILP (tem))
20929 XSETCDR (tem, Qnil);
20934 offset = 0;
20936 if (literal)
20938 prec = precision - n;
20939 switch (mode_line_target)
20941 case MODE_LINE_NOPROP:
20942 case MODE_LINE_TITLE:
20943 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20944 break;
20945 case MODE_LINE_STRING:
20946 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20947 break;
20948 case MODE_LINE_DISPLAY:
20949 n += display_string (NULL, elt, Qnil, 0, 0, it,
20950 0, prec, 0, STRING_MULTIBYTE (elt));
20951 break;
20954 break;
20957 /* Handle the non-literal case. */
20959 while ((precision <= 0 || n < precision)
20960 && SREF (elt, offset) != 0
20961 && (mode_line_target != MODE_LINE_DISPLAY
20962 || it->current_x < it->last_visible_x))
20964 ptrdiff_t last_offset = offset;
20966 /* Advance to end of string or next format specifier. */
20967 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20970 if (offset - 1 != last_offset)
20972 ptrdiff_t nchars, nbytes;
20974 /* Output to end of string or up to '%'. Field width
20975 is length of string. Don't output more than
20976 PRECISION allows us. */
20977 offset--;
20979 prec = c_string_width (SDATA (elt) + last_offset,
20980 offset - last_offset, precision - n,
20981 &nchars, &nbytes);
20983 switch (mode_line_target)
20985 case MODE_LINE_NOPROP:
20986 case MODE_LINE_TITLE:
20987 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20988 break;
20989 case MODE_LINE_STRING:
20991 ptrdiff_t bytepos = last_offset;
20992 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20993 ptrdiff_t endpos = (precision <= 0
20994 ? string_byte_to_char (elt, offset)
20995 : charpos + nchars);
20997 n += store_mode_line_string (NULL,
20998 Fsubstring (elt, make_number (charpos),
20999 make_number (endpos)),
21000 0, 0, 0, Qnil);
21002 break;
21003 case MODE_LINE_DISPLAY:
21005 ptrdiff_t bytepos = last_offset;
21006 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21008 if (precision <= 0)
21009 nchars = string_byte_to_char (elt, offset) - charpos;
21010 n += display_string (NULL, elt, Qnil, 0, charpos,
21011 it, 0, nchars, 0,
21012 STRING_MULTIBYTE (elt));
21014 break;
21017 else /* c == '%' */
21019 ptrdiff_t percent_position = offset;
21021 /* Get the specified minimum width. Zero means
21022 don't pad. */
21023 field = 0;
21024 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21025 field = field * 10 + c - '0';
21027 /* Don't pad beyond the total padding allowed. */
21028 if (field_width - n > 0 && field > field_width - n)
21029 field = field_width - n;
21031 /* Note that either PRECISION <= 0 or N < PRECISION. */
21032 prec = precision - n;
21034 if (c == 'M')
21035 n += display_mode_element (it, depth, field, prec,
21036 Vglobal_mode_string, props,
21037 risky);
21038 else if (c != 0)
21040 bool multibyte;
21041 ptrdiff_t bytepos, charpos;
21042 const char *spec;
21043 Lisp_Object string;
21045 bytepos = percent_position;
21046 charpos = (STRING_MULTIBYTE (elt)
21047 ? string_byte_to_char (elt, bytepos)
21048 : bytepos);
21049 spec = decode_mode_spec (it->w, c, field, &string);
21050 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21052 switch (mode_line_target)
21054 case MODE_LINE_NOPROP:
21055 case MODE_LINE_TITLE:
21056 n += store_mode_line_noprop (spec, field, prec);
21057 break;
21058 case MODE_LINE_STRING:
21060 Lisp_Object tem = build_string (spec);
21061 props = Ftext_properties_at (make_number (charpos), elt);
21062 /* Should only keep face property in props */
21063 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21065 break;
21066 case MODE_LINE_DISPLAY:
21068 int nglyphs_before, nwritten;
21070 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21071 nwritten = display_string (spec, string, elt,
21072 charpos, 0, it,
21073 field, prec, 0,
21074 multibyte);
21076 /* Assign to the glyphs written above the
21077 string where the `%x' came from, position
21078 of the `%'. */
21079 if (nwritten > 0)
21081 struct glyph *glyph
21082 = (it->glyph_row->glyphs[TEXT_AREA]
21083 + nglyphs_before);
21084 int i;
21086 for (i = 0; i < nwritten; ++i)
21088 glyph[i].object = elt;
21089 glyph[i].charpos = charpos;
21092 n += nwritten;
21095 break;
21098 else /* c == 0 */
21099 break;
21103 break;
21105 case Lisp_Symbol:
21106 /* A symbol: process the value of the symbol recursively
21107 as if it appeared here directly. Avoid error if symbol void.
21108 Special case: if value of symbol is a string, output the string
21109 literally. */
21111 register Lisp_Object tem;
21113 /* If the variable is not marked as risky to set
21114 then its contents are risky to use. */
21115 if (NILP (Fget (elt, Qrisky_local_variable)))
21116 risky = 1;
21118 tem = Fboundp (elt);
21119 if (!NILP (tem))
21121 tem = Fsymbol_value (elt);
21122 /* If value is a string, output that string literally:
21123 don't check for % within it. */
21124 if (STRINGP (tem))
21125 literal = 1;
21127 if (!EQ (tem, elt))
21129 /* Give up right away for nil or t. */
21130 elt = tem;
21131 goto tail_recurse;
21135 break;
21137 case Lisp_Cons:
21139 register Lisp_Object car, tem;
21141 /* A cons cell: five distinct cases.
21142 If first element is :eval or :propertize, do something special.
21143 If first element is a string or a cons, process all the elements
21144 and effectively concatenate them.
21145 If first element is a negative number, truncate displaying cdr to
21146 at most that many characters. If positive, pad (with spaces)
21147 to at least that many characters.
21148 If first element is a symbol, process the cadr or caddr recursively
21149 according to whether the symbol's value is non-nil or nil. */
21150 car = XCAR (elt);
21151 if (EQ (car, QCeval))
21153 /* An element of the form (:eval FORM) means evaluate FORM
21154 and use the result as mode line elements. */
21156 if (risky)
21157 break;
21159 if (CONSP (XCDR (elt)))
21161 Lisp_Object spec;
21162 spec = safe_eval (XCAR (XCDR (elt)));
21163 n += display_mode_element (it, depth, field_width - n,
21164 precision - n, spec, props,
21165 risky);
21168 else if (EQ (car, QCpropertize))
21170 /* An element of the form (:propertize ELT PROPS...)
21171 means display ELT but applying properties PROPS. */
21173 if (risky)
21174 break;
21176 if (CONSP (XCDR (elt)))
21177 n += display_mode_element (it, depth, field_width - n,
21178 precision - n, XCAR (XCDR (elt)),
21179 XCDR (XCDR (elt)), risky);
21181 else if (SYMBOLP (car))
21183 tem = Fboundp (car);
21184 elt = XCDR (elt);
21185 if (!CONSP (elt))
21186 goto invalid;
21187 /* elt is now the cdr, and we know it is a cons cell.
21188 Use its car if CAR has a non-nil value. */
21189 if (!NILP (tem))
21191 tem = Fsymbol_value (car);
21192 if (!NILP (tem))
21194 elt = XCAR (elt);
21195 goto tail_recurse;
21198 /* Symbol's value is nil (or symbol is unbound)
21199 Get the cddr of the original list
21200 and if possible find the caddr and use that. */
21201 elt = XCDR (elt);
21202 if (NILP (elt))
21203 break;
21204 else if (!CONSP (elt))
21205 goto invalid;
21206 elt = XCAR (elt);
21207 goto tail_recurse;
21209 else if (INTEGERP (car))
21211 register int lim = XINT (car);
21212 elt = XCDR (elt);
21213 if (lim < 0)
21215 /* Negative int means reduce maximum width. */
21216 if (precision <= 0)
21217 precision = -lim;
21218 else
21219 precision = min (precision, -lim);
21221 else if (lim > 0)
21223 /* Padding specified. Don't let it be more than
21224 current maximum. */
21225 if (precision > 0)
21226 lim = min (precision, lim);
21228 /* If that's more padding than already wanted, queue it.
21229 But don't reduce padding already specified even if
21230 that is beyond the current truncation point. */
21231 field_width = max (lim, field_width);
21233 goto tail_recurse;
21235 else if (STRINGP (car) || CONSP (car))
21237 Lisp_Object halftail = elt;
21238 int len = 0;
21240 while (CONSP (elt)
21241 && (precision <= 0 || n < precision))
21243 n += display_mode_element (it, depth,
21244 /* Do padding only after the last
21245 element in the list. */
21246 (! CONSP (XCDR (elt))
21247 ? field_width - n
21248 : 0),
21249 precision - n, XCAR (elt),
21250 props, risky);
21251 elt = XCDR (elt);
21252 len++;
21253 if ((len & 1) == 0)
21254 halftail = XCDR (halftail);
21255 /* Check for cycle. */
21256 if (EQ (halftail, elt))
21257 break;
21261 break;
21263 default:
21264 invalid:
21265 elt = build_string ("*invalid*");
21266 goto tail_recurse;
21269 /* Pad to FIELD_WIDTH. */
21270 if (field_width > 0 && n < field_width)
21272 switch (mode_line_target)
21274 case MODE_LINE_NOPROP:
21275 case MODE_LINE_TITLE:
21276 n += store_mode_line_noprop ("", field_width - n, 0);
21277 break;
21278 case MODE_LINE_STRING:
21279 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21280 break;
21281 case MODE_LINE_DISPLAY:
21282 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21283 0, 0, 0);
21284 break;
21288 return n;
21291 /* Store a mode-line string element in mode_line_string_list.
21293 If STRING is non-null, display that C string. Otherwise, the Lisp
21294 string LISP_STRING is displayed.
21296 FIELD_WIDTH is the minimum number of output glyphs to produce.
21297 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21298 with spaces. FIELD_WIDTH <= 0 means don't pad.
21300 PRECISION is the maximum number of characters to output from
21301 STRING. PRECISION <= 0 means don't truncate the string.
21303 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21304 properties to the string.
21306 PROPS are the properties to add to the string.
21307 The mode_line_string_face face property is always added to the string.
21310 static int
21311 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21312 int field_width, int precision, Lisp_Object props)
21314 ptrdiff_t len;
21315 int n = 0;
21317 if (string != NULL)
21319 len = strlen (string);
21320 if (precision > 0 && len > precision)
21321 len = precision;
21322 lisp_string = make_string (string, len);
21323 if (NILP (props))
21324 props = mode_line_string_face_prop;
21325 else if (!NILP (mode_line_string_face))
21327 Lisp_Object face = Fplist_get (props, Qface);
21328 props = Fcopy_sequence (props);
21329 if (NILP (face))
21330 face = mode_line_string_face;
21331 else
21332 face = list2 (face, mode_line_string_face);
21333 props = Fplist_put (props, Qface, face);
21335 Fadd_text_properties (make_number (0), make_number (len),
21336 props, lisp_string);
21338 else
21340 len = XFASTINT (Flength (lisp_string));
21341 if (precision > 0 && len > precision)
21343 len = precision;
21344 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21345 precision = -1;
21347 if (!NILP (mode_line_string_face))
21349 Lisp_Object face;
21350 if (NILP (props))
21351 props = Ftext_properties_at (make_number (0), lisp_string);
21352 face = Fplist_get (props, Qface);
21353 if (NILP (face))
21354 face = mode_line_string_face;
21355 else
21356 face = list2 (face, mode_line_string_face);
21357 props = list2 (Qface, face);
21358 if (copy_string)
21359 lisp_string = Fcopy_sequence (lisp_string);
21361 if (!NILP (props))
21362 Fadd_text_properties (make_number (0), make_number (len),
21363 props, lisp_string);
21366 if (len > 0)
21368 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21369 n += len;
21372 if (field_width > len)
21374 field_width -= len;
21375 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21376 if (!NILP (props))
21377 Fadd_text_properties (make_number (0), make_number (field_width),
21378 props, lisp_string);
21379 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21380 n += field_width;
21383 return n;
21387 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21388 1, 4, 0,
21389 doc: /* Format a string out of a mode line format specification.
21390 First arg FORMAT specifies the mode line format (see `mode-line-format'
21391 for details) to use.
21393 By default, the format is evaluated for the currently selected window.
21395 Optional second arg FACE specifies the face property to put on all
21396 characters for which no face is specified. The value nil means the
21397 default face. The value t means whatever face the window's mode line
21398 currently uses (either `mode-line' or `mode-line-inactive',
21399 depending on whether the window is the selected window or not).
21400 An integer value means the value string has no text
21401 properties.
21403 Optional third and fourth args WINDOW and BUFFER specify the window
21404 and buffer to use as the context for the formatting (defaults
21405 are the selected window and the WINDOW's buffer). */)
21406 (Lisp_Object format, Lisp_Object face,
21407 Lisp_Object window, Lisp_Object buffer)
21409 struct it it;
21410 int len;
21411 struct window *w;
21412 struct buffer *old_buffer = NULL;
21413 int face_id;
21414 int no_props = INTEGERP (face);
21415 ptrdiff_t count = SPECPDL_INDEX ();
21416 Lisp_Object str;
21417 int string_start = 0;
21419 w = decode_any_window (window);
21420 XSETWINDOW (window, w);
21422 if (NILP (buffer))
21423 buffer = w->contents;
21424 CHECK_BUFFER (buffer);
21426 /* Make formatting the modeline a non-op when noninteractive, otherwise
21427 there will be problems later caused by a partially initialized frame. */
21428 if (NILP (format) || noninteractive)
21429 return empty_unibyte_string;
21431 if (no_props)
21432 face = Qnil;
21434 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21435 : EQ (face, Qt) ? (EQ (window, selected_window)
21436 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21437 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21438 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21439 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21440 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21441 : DEFAULT_FACE_ID;
21443 old_buffer = current_buffer;
21445 /* Save things including mode_line_proptrans_alist,
21446 and set that to nil so that we don't alter the outer value. */
21447 record_unwind_protect (unwind_format_mode_line,
21448 format_mode_line_unwind_data
21449 (XFRAME (WINDOW_FRAME (w)),
21450 old_buffer, selected_window, 1));
21451 mode_line_proptrans_alist = Qnil;
21453 Fselect_window (window, Qt);
21454 set_buffer_internal_1 (XBUFFER (buffer));
21456 init_iterator (&it, w, -1, -1, NULL, face_id);
21458 if (no_props)
21460 mode_line_target = MODE_LINE_NOPROP;
21461 mode_line_string_face_prop = Qnil;
21462 mode_line_string_list = Qnil;
21463 string_start = MODE_LINE_NOPROP_LEN (0);
21465 else
21467 mode_line_target = MODE_LINE_STRING;
21468 mode_line_string_list = Qnil;
21469 mode_line_string_face = face;
21470 mode_line_string_face_prop
21471 = NILP (face) ? Qnil : list2 (Qface, face);
21474 push_kboard (FRAME_KBOARD (it.f));
21475 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21476 pop_kboard ();
21478 if (no_props)
21480 len = MODE_LINE_NOPROP_LEN (string_start);
21481 str = make_string (mode_line_noprop_buf + string_start, len);
21483 else
21485 mode_line_string_list = Fnreverse (mode_line_string_list);
21486 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21487 empty_unibyte_string);
21490 unbind_to (count, Qnil);
21491 return str;
21494 /* Write a null-terminated, right justified decimal representation of
21495 the positive integer D to BUF using a minimal field width WIDTH. */
21497 static void
21498 pint2str (register char *buf, register int width, register ptrdiff_t d)
21500 register char *p = buf;
21502 if (d <= 0)
21503 *p++ = '0';
21504 else
21506 while (d > 0)
21508 *p++ = d % 10 + '0';
21509 d /= 10;
21513 for (width -= (int) (p - buf); width > 0; --width)
21514 *p++ = ' ';
21515 *p-- = '\0';
21516 while (p > buf)
21518 d = *buf;
21519 *buf++ = *p;
21520 *p-- = d;
21524 /* Write a null-terminated, right justified decimal and "human
21525 readable" representation of the nonnegative integer D to BUF using
21526 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21528 static const char power_letter[] =
21530 0, /* no letter */
21531 'k', /* kilo */
21532 'M', /* mega */
21533 'G', /* giga */
21534 'T', /* tera */
21535 'P', /* peta */
21536 'E', /* exa */
21537 'Z', /* zetta */
21538 'Y' /* yotta */
21541 static void
21542 pint2hrstr (char *buf, int width, ptrdiff_t d)
21544 /* We aim to represent the nonnegative integer D as
21545 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21546 ptrdiff_t quotient = d;
21547 int remainder = 0;
21548 /* -1 means: do not use TENTHS. */
21549 int tenths = -1;
21550 int exponent = 0;
21552 /* Length of QUOTIENT.TENTHS as a string. */
21553 int length;
21555 char * psuffix;
21556 char * p;
21558 if (quotient >= 1000)
21560 /* Scale to the appropriate EXPONENT. */
21563 remainder = quotient % 1000;
21564 quotient /= 1000;
21565 exponent++;
21567 while (quotient >= 1000);
21569 /* Round to nearest and decide whether to use TENTHS or not. */
21570 if (quotient <= 9)
21572 tenths = remainder / 100;
21573 if (remainder % 100 >= 50)
21575 if (tenths < 9)
21576 tenths++;
21577 else
21579 quotient++;
21580 if (quotient == 10)
21581 tenths = -1;
21582 else
21583 tenths = 0;
21587 else
21588 if (remainder >= 500)
21590 if (quotient < 999)
21591 quotient++;
21592 else
21594 quotient = 1;
21595 exponent++;
21596 tenths = 0;
21601 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21602 if (tenths == -1 && quotient <= 99)
21603 if (quotient <= 9)
21604 length = 1;
21605 else
21606 length = 2;
21607 else
21608 length = 3;
21609 p = psuffix = buf + max (width, length);
21611 /* Print EXPONENT. */
21612 *psuffix++ = power_letter[exponent];
21613 *psuffix = '\0';
21615 /* Print TENTHS. */
21616 if (tenths >= 0)
21618 *--p = '0' + tenths;
21619 *--p = '.';
21622 /* Print QUOTIENT. */
21625 int digit = quotient % 10;
21626 *--p = '0' + digit;
21628 while ((quotient /= 10) != 0);
21630 /* Print leading spaces. */
21631 while (buf < p)
21632 *--p = ' ';
21635 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21636 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21637 type of CODING_SYSTEM. Return updated pointer into BUF. */
21639 static unsigned char invalid_eol_type[] = "(*invalid*)";
21641 static char *
21642 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21644 Lisp_Object val;
21645 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21646 const unsigned char *eol_str;
21647 int eol_str_len;
21648 /* The EOL conversion we are using. */
21649 Lisp_Object eoltype;
21651 val = CODING_SYSTEM_SPEC (coding_system);
21652 eoltype = Qnil;
21654 if (!VECTORP (val)) /* Not yet decided. */
21656 *buf++ = multibyte ? '-' : ' ';
21657 if (eol_flag)
21658 eoltype = eol_mnemonic_undecided;
21659 /* Don't mention EOL conversion if it isn't decided. */
21661 else
21663 Lisp_Object attrs;
21664 Lisp_Object eolvalue;
21666 attrs = AREF (val, 0);
21667 eolvalue = AREF (val, 2);
21669 *buf++ = multibyte
21670 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21671 : ' ';
21673 if (eol_flag)
21675 /* The EOL conversion that is normal on this system. */
21677 if (NILP (eolvalue)) /* Not yet decided. */
21678 eoltype = eol_mnemonic_undecided;
21679 else if (VECTORP (eolvalue)) /* Not yet decided. */
21680 eoltype = eol_mnemonic_undecided;
21681 else /* eolvalue is Qunix, Qdos, or Qmac. */
21682 eoltype = (EQ (eolvalue, Qunix)
21683 ? eol_mnemonic_unix
21684 : (EQ (eolvalue, Qdos) == 1
21685 ? eol_mnemonic_dos : eol_mnemonic_mac));
21689 if (eol_flag)
21691 /* Mention the EOL conversion if it is not the usual one. */
21692 if (STRINGP (eoltype))
21694 eol_str = SDATA (eoltype);
21695 eol_str_len = SBYTES (eoltype);
21697 else if (CHARACTERP (eoltype))
21699 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21700 int c = XFASTINT (eoltype);
21701 eol_str_len = CHAR_STRING (c, tmp);
21702 eol_str = tmp;
21704 else
21706 eol_str = invalid_eol_type;
21707 eol_str_len = sizeof (invalid_eol_type) - 1;
21709 memcpy (buf, eol_str, eol_str_len);
21710 buf += eol_str_len;
21713 return buf;
21716 /* Return a string for the output of a mode line %-spec for window W,
21717 generated by character C. FIELD_WIDTH > 0 means pad the string
21718 returned with spaces to that value. Return a Lisp string in
21719 *STRING if the resulting string is taken from that Lisp string.
21721 Note we operate on the current buffer for most purposes. */
21723 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21725 static const char *
21726 decode_mode_spec (struct window *w, register int c, int field_width,
21727 Lisp_Object *string)
21729 Lisp_Object obj;
21730 struct frame *f = XFRAME (WINDOW_FRAME (w));
21731 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21732 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21733 produce strings from numerical values, so limit preposterously
21734 large values of FIELD_WIDTH to avoid overrunning the buffer's
21735 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21736 bytes plus the terminating null. */
21737 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21738 struct buffer *b = current_buffer;
21740 obj = Qnil;
21741 *string = Qnil;
21743 switch (c)
21745 case '*':
21746 if (!NILP (BVAR (b, read_only)))
21747 return "%";
21748 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21749 return "*";
21750 return "-";
21752 case '+':
21753 /* This differs from %* only for a modified read-only buffer. */
21754 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21755 return "*";
21756 if (!NILP (BVAR (b, read_only)))
21757 return "%";
21758 return "-";
21760 case '&':
21761 /* This differs from %* in ignoring read-only-ness. */
21762 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21763 return "*";
21764 return "-";
21766 case '%':
21767 return "%";
21769 case '[':
21771 int i;
21772 char *p;
21774 if (command_loop_level > 5)
21775 return "[[[... ";
21776 p = decode_mode_spec_buf;
21777 for (i = 0; i < command_loop_level; i++)
21778 *p++ = '[';
21779 *p = 0;
21780 return decode_mode_spec_buf;
21783 case ']':
21785 int i;
21786 char *p;
21788 if (command_loop_level > 5)
21789 return " ...]]]";
21790 p = decode_mode_spec_buf;
21791 for (i = 0; i < command_loop_level; i++)
21792 *p++ = ']';
21793 *p = 0;
21794 return decode_mode_spec_buf;
21797 case '-':
21799 register int i;
21801 /* Let lots_of_dashes be a string of infinite length. */
21802 if (mode_line_target == MODE_LINE_NOPROP
21803 || mode_line_target == MODE_LINE_STRING)
21804 return "--";
21805 if (field_width <= 0
21806 || field_width > sizeof (lots_of_dashes))
21808 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21809 decode_mode_spec_buf[i] = '-';
21810 decode_mode_spec_buf[i] = '\0';
21811 return decode_mode_spec_buf;
21813 else
21814 return lots_of_dashes;
21817 case 'b':
21818 obj = BVAR (b, name);
21819 break;
21821 case 'c':
21822 /* %c and %l are ignored in `frame-title-format'.
21823 (In redisplay_internal, the frame title is drawn _before_ the
21824 windows are updated, so the stuff which depends on actual
21825 window contents (such as %l) may fail to render properly, or
21826 even crash emacs.) */
21827 if (mode_line_target == MODE_LINE_TITLE)
21828 return "";
21829 else
21831 ptrdiff_t col = current_column ();
21832 w->column_number_displayed = col;
21833 pint2str (decode_mode_spec_buf, width, col);
21834 return decode_mode_spec_buf;
21837 case 'e':
21838 #ifndef SYSTEM_MALLOC
21840 if (NILP (Vmemory_full))
21841 return "";
21842 else
21843 return "!MEM FULL! ";
21845 #else
21846 return "";
21847 #endif
21849 case 'F':
21850 /* %F displays the frame name. */
21851 if (!NILP (f->title))
21852 return SSDATA (f->title);
21853 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21854 return SSDATA (f->name);
21855 return "Emacs";
21857 case 'f':
21858 obj = BVAR (b, filename);
21859 break;
21861 case 'i':
21863 ptrdiff_t size = ZV - BEGV;
21864 pint2str (decode_mode_spec_buf, width, size);
21865 return decode_mode_spec_buf;
21868 case 'I':
21870 ptrdiff_t size = ZV - BEGV;
21871 pint2hrstr (decode_mode_spec_buf, width, size);
21872 return decode_mode_spec_buf;
21875 case 'l':
21877 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21878 ptrdiff_t topline, nlines, height;
21879 ptrdiff_t junk;
21881 /* %c and %l are ignored in `frame-title-format'. */
21882 if (mode_line_target == MODE_LINE_TITLE)
21883 return "";
21885 startpos = marker_position (w->start);
21886 startpos_byte = marker_byte_position (w->start);
21887 height = WINDOW_TOTAL_LINES (w);
21889 /* If we decided that this buffer isn't suitable for line numbers,
21890 don't forget that too fast. */
21891 if (w->base_line_pos == -1)
21892 goto no_value;
21894 /* If the buffer is very big, don't waste time. */
21895 if (INTEGERP (Vline_number_display_limit)
21896 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21898 w->base_line_pos = 0;
21899 w->base_line_number = 0;
21900 goto no_value;
21903 if (w->base_line_number > 0
21904 && w->base_line_pos > 0
21905 && w->base_line_pos <= startpos)
21907 line = w->base_line_number;
21908 linepos = w->base_line_pos;
21909 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21911 else
21913 line = 1;
21914 linepos = BUF_BEGV (b);
21915 linepos_byte = BUF_BEGV_BYTE (b);
21918 /* Count lines from base line to window start position. */
21919 nlines = display_count_lines (linepos_byte,
21920 startpos_byte,
21921 startpos, &junk);
21923 topline = nlines + line;
21925 /* Determine a new base line, if the old one is too close
21926 or too far away, or if we did not have one.
21927 "Too close" means it's plausible a scroll-down would
21928 go back past it. */
21929 if (startpos == BUF_BEGV (b))
21931 w->base_line_number = topline;
21932 w->base_line_pos = BUF_BEGV (b);
21934 else if (nlines < height + 25 || nlines > height * 3 + 50
21935 || linepos == BUF_BEGV (b))
21937 ptrdiff_t limit = BUF_BEGV (b);
21938 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21939 ptrdiff_t position;
21940 ptrdiff_t distance =
21941 (height * 2 + 30) * line_number_display_limit_width;
21943 if (startpos - distance > limit)
21945 limit = startpos - distance;
21946 limit_byte = CHAR_TO_BYTE (limit);
21949 nlines = display_count_lines (startpos_byte,
21950 limit_byte,
21951 - (height * 2 + 30),
21952 &position);
21953 /* If we couldn't find the lines we wanted within
21954 line_number_display_limit_width chars per line,
21955 give up on line numbers for this window. */
21956 if (position == limit_byte && limit == startpos - distance)
21958 w->base_line_pos = -1;
21959 w->base_line_number = 0;
21960 goto no_value;
21963 w->base_line_number = topline - nlines;
21964 w->base_line_pos = BYTE_TO_CHAR (position);
21967 /* Now count lines from the start pos to point. */
21968 nlines = display_count_lines (startpos_byte,
21969 PT_BYTE, PT, &junk);
21971 /* Record that we did display the line number. */
21972 line_number_displayed = 1;
21974 /* Make the string to show. */
21975 pint2str (decode_mode_spec_buf, width, topline + nlines);
21976 return decode_mode_spec_buf;
21977 no_value:
21979 char* p = decode_mode_spec_buf;
21980 int pad = width - 2;
21981 while (pad-- > 0)
21982 *p++ = ' ';
21983 *p++ = '?';
21984 *p++ = '?';
21985 *p = '\0';
21986 return decode_mode_spec_buf;
21989 break;
21991 case 'm':
21992 obj = BVAR (b, mode_name);
21993 break;
21995 case 'n':
21996 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21997 return " Narrow";
21998 break;
22000 case 'p':
22002 ptrdiff_t pos = marker_position (w->start);
22003 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22005 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
22007 if (pos <= BUF_BEGV (b))
22008 return "All";
22009 else
22010 return "Bottom";
22012 else if (pos <= BUF_BEGV (b))
22013 return "Top";
22014 else
22016 if (total > 1000000)
22017 /* Do it differently for a large value, to avoid overflow. */
22018 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22019 else
22020 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22021 /* We can't normally display a 3-digit number,
22022 so get us a 2-digit number that is close. */
22023 if (total == 100)
22024 total = 99;
22025 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22026 return decode_mode_spec_buf;
22030 /* Display percentage of size above the bottom of the screen. */
22031 case 'P':
22033 ptrdiff_t toppos = marker_position (w->start);
22034 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
22035 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22037 if (botpos >= BUF_ZV (b))
22039 if (toppos <= BUF_BEGV (b))
22040 return "All";
22041 else
22042 return "Bottom";
22044 else
22046 if (total > 1000000)
22047 /* Do it differently for a large value, to avoid overflow. */
22048 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22049 else
22050 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22051 /* We can't normally display a 3-digit number,
22052 so get us a 2-digit number that is close. */
22053 if (total == 100)
22054 total = 99;
22055 if (toppos <= BUF_BEGV (b))
22056 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22057 else
22058 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22059 return decode_mode_spec_buf;
22063 case 's':
22064 /* status of process */
22065 obj = Fget_buffer_process (Fcurrent_buffer ());
22066 if (NILP (obj))
22067 return "no process";
22068 #ifndef MSDOS
22069 obj = Fsymbol_name (Fprocess_status (obj));
22070 #endif
22071 break;
22073 case '@':
22075 ptrdiff_t count = inhibit_garbage_collection ();
22076 Lisp_Object val = call1 (intern ("file-remote-p"),
22077 BVAR (current_buffer, directory));
22078 unbind_to (count, Qnil);
22080 if (NILP (val))
22081 return "-";
22082 else
22083 return "@";
22086 case 'z':
22087 /* coding-system (not including end-of-line format) */
22088 case 'Z':
22089 /* coding-system (including end-of-line type) */
22091 int eol_flag = (c == 'Z');
22092 char *p = decode_mode_spec_buf;
22094 if (! FRAME_WINDOW_P (f))
22096 /* No need to mention EOL here--the terminal never needs
22097 to do EOL conversion. */
22098 p = decode_mode_spec_coding (CODING_ID_NAME
22099 (FRAME_KEYBOARD_CODING (f)->id),
22100 p, 0);
22101 p = decode_mode_spec_coding (CODING_ID_NAME
22102 (FRAME_TERMINAL_CODING (f)->id),
22103 p, 0);
22105 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22106 p, eol_flag);
22108 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22109 #ifdef subprocesses
22110 obj = Fget_buffer_process (Fcurrent_buffer ());
22111 if (PROCESSP (obj))
22113 p = decode_mode_spec_coding
22114 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22115 p = decode_mode_spec_coding
22116 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22118 #endif /* subprocesses */
22119 #endif /* 0 */
22120 *p = 0;
22121 return decode_mode_spec_buf;
22125 if (STRINGP (obj))
22127 *string = obj;
22128 return SSDATA (obj);
22130 else
22131 return "";
22135 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22136 means count lines back from START_BYTE. But don't go beyond
22137 LIMIT_BYTE. Return the number of lines thus found (always
22138 nonnegative).
22140 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22141 either the position COUNT lines after/before START_BYTE, if we
22142 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22143 COUNT lines. */
22145 static ptrdiff_t
22146 display_count_lines (ptrdiff_t start_byte,
22147 ptrdiff_t limit_byte, ptrdiff_t count,
22148 ptrdiff_t *byte_pos_ptr)
22150 register unsigned char *cursor;
22151 unsigned char *base;
22153 register ptrdiff_t ceiling;
22154 register unsigned char *ceiling_addr;
22155 ptrdiff_t orig_count = count;
22157 /* If we are not in selective display mode,
22158 check only for newlines. */
22159 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22160 && !INTEGERP (BVAR (current_buffer, selective_display)));
22162 if (count > 0)
22164 while (start_byte < limit_byte)
22166 ceiling = BUFFER_CEILING_OF (start_byte);
22167 ceiling = min (limit_byte - 1, ceiling);
22168 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22169 base = (cursor = BYTE_POS_ADDR (start_byte));
22173 if (selective_display)
22175 while (*cursor != '\n' && *cursor != 015
22176 && ++cursor != ceiling_addr)
22177 continue;
22178 if (cursor == ceiling_addr)
22179 break;
22181 else
22183 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22184 if (! cursor)
22185 break;
22188 cursor++;
22190 if (--count == 0)
22192 start_byte += cursor - base;
22193 *byte_pos_ptr = start_byte;
22194 return orig_count;
22197 while (cursor < ceiling_addr);
22199 start_byte += ceiling_addr - base;
22202 else
22204 while (start_byte > limit_byte)
22206 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22207 ceiling = max (limit_byte, ceiling);
22208 ceiling_addr = BYTE_POS_ADDR (ceiling);
22209 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22210 while (1)
22212 if (selective_display)
22214 while (--cursor >= ceiling_addr
22215 && *cursor != '\n' && *cursor != 015)
22216 continue;
22217 if (cursor < ceiling_addr)
22218 break;
22220 else
22222 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22223 if (! cursor)
22224 break;
22227 if (++count == 0)
22229 start_byte += cursor - base + 1;
22230 *byte_pos_ptr = start_byte;
22231 /* When scanning backwards, we should
22232 not count the newline posterior to which we stop. */
22233 return - orig_count - 1;
22236 start_byte += ceiling_addr - base;
22240 *byte_pos_ptr = limit_byte;
22242 if (count < 0)
22243 return - orig_count + count;
22244 return orig_count - count;
22250 /***********************************************************************
22251 Displaying strings
22252 ***********************************************************************/
22254 /* Display a NUL-terminated string, starting with index START.
22256 If STRING is non-null, display that C string. Otherwise, the Lisp
22257 string LISP_STRING is displayed. There's a case that STRING is
22258 non-null and LISP_STRING is not nil. It means STRING is a string
22259 data of LISP_STRING. In that case, we display LISP_STRING while
22260 ignoring its text properties.
22262 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22263 FACE_STRING. Display STRING or LISP_STRING with the face at
22264 FACE_STRING_POS in FACE_STRING:
22266 Display the string in the environment given by IT, but use the
22267 standard display table, temporarily.
22269 FIELD_WIDTH is the minimum number of output glyphs to produce.
22270 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22271 with spaces. If STRING has more characters, more than FIELD_WIDTH
22272 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22274 PRECISION is the maximum number of characters to output from
22275 STRING. PRECISION < 0 means don't truncate the string.
22277 This is roughly equivalent to printf format specifiers:
22279 FIELD_WIDTH PRECISION PRINTF
22280 ----------------------------------------
22281 -1 -1 %s
22282 -1 10 %.10s
22283 10 -1 %10s
22284 20 10 %20.10s
22286 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22287 display them, and < 0 means obey the current buffer's value of
22288 enable_multibyte_characters.
22290 Value is the number of columns displayed. */
22292 static int
22293 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22294 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22295 int field_width, int precision, int max_x, int multibyte)
22297 int hpos_at_start = it->hpos;
22298 int saved_face_id = it->face_id;
22299 struct glyph_row *row = it->glyph_row;
22300 ptrdiff_t it_charpos;
22302 /* Initialize the iterator IT for iteration over STRING beginning
22303 with index START. */
22304 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22305 precision, field_width, multibyte);
22306 if (string && STRINGP (lisp_string))
22307 /* LISP_STRING is the one returned by decode_mode_spec. We should
22308 ignore its text properties. */
22309 it->stop_charpos = it->end_charpos;
22311 /* If displaying STRING, set up the face of the iterator from
22312 FACE_STRING, if that's given. */
22313 if (STRINGP (face_string))
22315 ptrdiff_t endptr;
22316 struct face *face;
22318 it->face_id
22319 = face_at_string_position (it->w, face_string, face_string_pos,
22320 0, it->region_beg_charpos,
22321 it->region_end_charpos,
22322 &endptr, it->base_face_id, 0);
22323 face = FACE_FROM_ID (it->f, it->face_id);
22324 it->face_box_p = face->box != FACE_NO_BOX;
22327 /* Set max_x to the maximum allowed X position. Don't let it go
22328 beyond the right edge of the window. */
22329 if (max_x <= 0)
22330 max_x = it->last_visible_x;
22331 else
22332 max_x = min (max_x, it->last_visible_x);
22334 /* Skip over display elements that are not visible. because IT->w is
22335 hscrolled. */
22336 if (it->current_x < it->first_visible_x)
22337 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22338 MOVE_TO_POS | MOVE_TO_X);
22340 row->ascent = it->max_ascent;
22341 row->height = it->max_ascent + it->max_descent;
22342 row->phys_ascent = it->max_phys_ascent;
22343 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22344 row->extra_line_spacing = it->max_extra_line_spacing;
22346 if (STRINGP (it->string))
22347 it_charpos = IT_STRING_CHARPOS (*it);
22348 else
22349 it_charpos = IT_CHARPOS (*it);
22351 /* This condition is for the case that we are called with current_x
22352 past last_visible_x. */
22353 while (it->current_x < max_x)
22355 int x_before, x, n_glyphs_before, i, nglyphs;
22357 /* Get the next display element. */
22358 if (!get_next_display_element (it))
22359 break;
22361 /* Produce glyphs. */
22362 x_before = it->current_x;
22363 n_glyphs_before = row->used[TEXT_AREA];
22364 PRODUCE_GLYPHS (it);
22366 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22367 i = 0;
22368 x = x_before;
22369 while (i < nglyphs)
22371 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22373 if (it->line_wrap != TRUNCATE
22374 && x + glyph->pixel_width > max_x)
22376 /* End of continued line or max_x reached. */
22377 if (CHAR_GLYPH_PADDING_P (*glyph))
22379 /* A wide character is unbreakable. */
22380 if (row->reversed_p)
22381 unproduce_glyphs (it, row->used[TEXT_AREA]
22382 - n_glyphs_before);
22383 row->used[TEXT_AREA] = n_glyphs_before;
22384 it->current_x = x_before;
22386 else
22388 if (row->reversed_p)
22389 unproduce_glyphs (it, row->used[TEXT_AREA]
22390 - (n_glyphs_before + i));
22391 row->used[TEXT_AREA] = n_glyphs_before + i;
22392 it->current_x = x;
22394 break;
22396 else if (x + glyph->pixel_width >= it->first_visible_x)
22398 /* Glyph is at least partially visible. */
22399 ++it->hpos;
22400 if (x < it->first_visible_x)
22401 row->x = x - it->first_visible_x;
22403 else
22405 /* Glyph is off the left margin of the display area.
22406 Should not happen. */
22407 emacs_abort ();
22410 row->ascent = max (row->ascent, it->max_ascent);
22411 row->height = max (row->height, it->max_ascent + it->max_descent);
22412 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22413 row->phys_height = max (row->phys_height,
22414 it->max_phys_ascent + it->max_phys_descent);
22415 row->extra_line_spacing = max (row->extra_line_spacing,
22416 it->max_extra_line_spacing);
22417 x += glyph->pixel_width;
22418 ++i;
22421 /* Stop if max_x reached. */
22422 if (i < nglyphs)
22423 break;
22425 /* Stop at line ends. */
22426 if (ITERATOR_AT_END_OF_LINE_P (it))
22428 it->continuation_lines_width = 0;
22429 break;
22432 set_iterator_to_next (it, 1);
22433 if (STRINGP (it->string))
22434 it_charpos = IT_STRING_CHARPOS (*it);
22435 else
22436 it_charpos = IT_CHARPOS (*it);
22438 /* Stop if truncating at the right edge. */
22439 if (it->line_wrap == TRUNCATE
22440 && it->current_x >= it->last_visible_x)
22442 /* Add truncation mark, but don't do it if the line is
22443 truncated at a padding space. */
22444 if (it_charpos < it->string_nchars)
22446 if (!FRAME_WINDOW_P (it->f))
22448 int ii, n;
22450 if (it->current_x > it->last_visible_x)
22452 if (!row->reversed_p)
22454 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22455 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22456 break;
22458 else
22460 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22461 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22462 break;
22463 unproduce_glyphs (it, ii + 1);
22464 ii = row->used[TEXT_AREA] - (ii + 1);
22466 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22468 row->used[TEXT_AREA] = ii;
22469 produce_special_glyphs (it, IT_TRUNCATION);
22472 produce_special_glyphs (it, IT_TRUNCATION);
22474 row->truncated_on_right_p = 1;
22476 break;
22480 /* Maybe insert a truncation at the left. */
22481 if (it->first_visible_x
22482 && it_charpos > 0)
22484 if (!FRAME_WINDOW_P (it->f)
22485 || (row->reversed_p
22486 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22487 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22488 insert_left_trunc_glyphs (it);
22489 row->truncated_on_left_p = 1;
22492 it->face_id = saved_face_id;
22494 /* Value is number of columns displayed. */
22495 return it->hpos - hpos_at_start;
22500 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22501 appears as an element of LIST or as the car of an element of LIST.
22502 If PROPVAL is a list, compare each element against LIST in that
22503 way, and return 1/2 if any element of PROPVAL is found in LIST.
22504 Otherwise return 0. This function cannot quit.
22505 The return value is 2 if the text is invisible but with an ellipsis
22506 and 1 if it's invisible and without an ellipsis. */
22509 invisible_p (register Lisp_Object propval, Lisp_Object list)
22511 register Lisp_Object tail, proptail;
22513 for (tail = list; CONSP (tail); tail = XCDR (tail))
22515 register Lisp_Object tem;
22516 tem = XCAR (tail);
22517 if (EQ (propval, tem))
22518 return 1;
22519 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22520 return NILP (XCDR (tem)) ? 1 : 2;
22523 if (CONSP (propval))
22525 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22527 Lisp_Object propelt;
22528 propelt = XCAR (proptail);
22529 for (tail = list; CONSP (tail); tail = XCDR (tail))
22531 register Lisp_Object tem;
22532 tem = XCAR (tail);
22533 if (EQ (propelt, tem))
22534 return 1;
22535 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22536 return NILP (XCDR (tem)) ? 1 : 2;
22541 return 0;
22544 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22545 doc: /* Non-nil if the property makes the text invisible.
22546 POS-OR-PROP can be a marker or number, in which case it is taken to be
22547 a position in the current buffer and the value of the `invisible' property
22548 is checked; or it can be some other value, which is then presumed to be the
22549 value of the `invisible' property of the text of interest.
22550 The non-nil value returned can be t for truly invisible text or something
22551 else if the text is replaced by an ellipsis. */)
22552 (Lisp_Object pos_or_prop)
22554 Lisp_Object prop
22555 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22556 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22557 : pos_or_prop);
22558 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22559 return (invis == 0 ? Qnil
22560 : invis == 1 ? Qt
22561 : make_number (invis));
22564 /* Calculate a width or height in pixels from a specification using
22565 the following elements:
22567 SPEC ::=
22568 NUM - a (fractional) multiple of the default font width/height
22569 (NUM) - specifies exactly NUM pixels
22570 UNIT - a fixed number of pixels, see below.
22571 ELEMENT - size of a display element in pixels, see below.
22572 (NUM . SPEC) - equals NUM * SPEC
22573 (+ SPEC SPEC ...) - add pixel values
22574 (- SPEC SPEC ...) - subtract pixel values
22575 (- SPEC) - negate pixel value
22577 NUM ::=
22578 INT or FLOAT - a number constant
22579 SYMBOL - use symbol's (buffer local) variable binding.
22581 UNIT ::=
22582 in - pixels per inch *)
22583 mm - pixels per 1/1000 meter *)
22584 cm - pixels per 1/100 meter *)
22585 width - width of current font in pixels.
22586 height - height of current font in pixels.
22588 *) using the ratio(s) defined in display-pixels-per-inch.
22590 ELEMENT ::=
22592 left-fringe - left fringe width in pixels
22593 right-fringe - right fringe width in pixels
22595 left-margin - left margin width in pixels
22596 right-margin - right margin width in pixels
22598 scroll-bar - scroll-bar area width in pixels
22600 Examples:
22602 Pixels corresponding to 5 inches:
22603 (5 . in)
22605 Total width of non-text areas on left side of window (if scroll-bar is on left):
22606 '(space :width (+ left-fringe left-margin scroll-bar))
22608 Align to first text column (in header line):
22609 '(space :align-to 0)
22611 Align to middle of text area minus half the width of variable `my-image'
22612 containing a loaded image:
22613 '(space :align-to (0.5 . (- text my-image)))
22615 Width of left margin minus width of 1 character in the default font:
22616 '(space :width (- left-margin 1))
22618 Width of left margin minus width of 2 characters in the current font:
22619 '(space :width (- left-margin (2 . width)))
22621 Center 1 character over left-margin (in header line):
22622 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22624 Different ways to express width of left fringe plus left margin minus one pixel:
22625 '(space :width (- (+ left-fringe left-margin) (1)))
22626 '(space :width (+ left-fringe left-margin (- (1))))
22627 '(space :width (+ left-fringe left-margin (-1)))
22631 static int
22632 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22633 struct font *font, int width_p, int *align_to)
22635 double pixels;
22637 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22638 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22640 if (NILP (prop))
22641 return OK_PIXELS (0);
22643 eassert (FRAME_LIVE_P (it->f));
22645 if (SYMBOLP (prop))
22647 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22649 char *unit = SSDATA (SYMBOL_NAME (prop));
22651 if (unit[0] == 'i' && unit[1] == 'n')
22652 pixels = 1.0;
22653 else if (unit[0] == 'm' && unit[1] == 'm')
22654 pixels = 25.4;
22655 else if (unit[0] == 'c' && unit[1] == 'm')
22656 pixels = 2.54;
22657 else
22658 pixels = 0;
22659 if (pixels > 0)
22661 double ppi = (width_p ? FRAME_RES_X (it->f)
22662 : FRAME_RES_Y (it->f));
22664 if (ppi > 0)
22665 return OK_PIXELS (ppi / pixels);
22666 return 0;
22670 #ifdef HAVE_WINDOW_SYSTEM
22671 if (EQ (prop, Qheight))
22672 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22673 if (EQ (prop, Qwidth))
22674 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22675 #else
22676 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22677 return OK_PIXELS (1);
22678 #endif
22680 if (EQ (prop, Qtext))
22681 return OK_PIXELS (width_p
22682 ? window_box_width (it->w, TEXT_AREA)
22683 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22685 if (align_to && *align_to < 0)
22687 *res = 0;
22688 if (EQ (prop, Qleft))
22689 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22690 if (EQ (prop, Qright))
22691 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22692 if (EQ (prop, Qcenter))
22693 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22694 + window_box_width (it->w, TEXT_AREA) / 2);
22695 if (EQ (prop, Qleft_fringe))
22696 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22697 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22698 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22699 if (EQ (prop, Qright_fringe))
22700 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22701 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22702 : window_box_right_offset (it->w, TEXT_AREA));
22703 if (EQ (prop, Qleft_margin))
22704 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22705 if (EQ (prop, Qright_margin))
22706 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22707 if (EQ (prop, Qscroll_bar))
22708 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22710 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22711 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22712 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22713 : 0)));
22715 else
22717 if (EQ (prop, Qleft_fringe))
22718 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22719 if (EQ (prop, Qright_fringe))
22720 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22721 if (EQ (prop, Qleft_margin))
22722 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22723 if (EQ (prop, Qright_margin))
22724 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22725 if (EQ (prop, Qscroll_bar))
22726 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22729 prop = buffer_local_value_1 (prop, it->w->contents);
22730 if (EQ (prop, Qunbound))
22731 prop = Qnil;
22734 if (INTEGERP (prop) || FLOATP (prop))
22736 int base_unit = (width_p
22737 ? FRAME_COLUMN_WIDTH (it->f)
22738 : FRAME_LINE_HEIGHT (it->f));
22739 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22742 if (CONSP (prop))
22744 Lisp_Object car = XCAR (prop);
22745 Lisp_Object cdr = XCDR (prop);
22747 if (SYMBOLP (car))
22749 #ifdef HAVE_WINDOW_SYSTEM
22750 if (FRAME_WINDOW_P (it->f)
22751 && valid_image_p (prop))
22753 ptrdiff_t id = lookup_image (it->f, prop);
22754 struct image *img = IMAGE_FROM_ID (it->f, id);
22756 return OK_PIXELS (width_p ? img->width : img->height);
22758 #endif
22759 if (EQ (car, Qplus) || EQ (car, Qminus))
22761 int first = 1;
22762 double px;
22764 pixels = 0;
22765 while (CONSP (cdr))
22767 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22768 font, width_p, align_to))
22769 return 0;
22770 if (first)
22771 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22772 else
22773 pixels += px;
22774 cdr = XCDR (cdr);
22776 if (EQ (car, Qminus))
22777 pixels = -pixels;
22778 return OK_PIXELS (pixels);
22781 car = buffer_local_value_1 (car, it->w->contents);
22782 if (EQ (car, Qunbound))
22783 car = Qnil;
22786 if (INTEGERP (car) || FLOATP (car))
22788 double fact;
22789 pixels = XFLOATINT (car);
22790 if (NILP (cdr))
22791 return OK_PIXELS (pixels);
22792 if (calc_pixel_width_or_height (&fact, it, cdr,
22793 font, width_p, align_to))
22794 return OK_PIXELS (pixels * fact);
22795 return 0;
22798 return 0;
22801 return 0;
22805 /***********************************************************************
22806 Glyph Display
22807 ***********************************************************************/
22809 #ifdef HAVE_WINDOW_SYSTEM
22811 #ifdef GLYPH_DEBUG
22813 void
22814 dump_glyph_string (struct glyph_string *s)
22816 fprintf (stderr, "glyph string\n");
22817 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22818 s->x, s->y, s->width, s->height);
22819 fprintf (stderr, " ybase = %d\n", s->ybase);
22820 fprintf (stderr, " hl = %d\n", s->hl);
22821 fprintf (stderr, " left overhang = %d, right = %d\n",
22822 s->left_overhang, s->right_overhang);
22823 fprintf (stderr, " nchars = %d\n", s->nchars);
22824 fprintf (stderr, " extends to end of line = %d\n",
22825 s->extends_to_end_of_line_p);
22826 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22827 fprintf (stderr, " bg width = %d\n", s->background_width);
22830 #endif /* GLYPH_DEBUG */
22832 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22833 of XChar2b structures for S; it can't be allocated in
22834 init_glyph_string because it must be allocated via `alloca'. W
22835 is the window on which S is drawn. ROW and AREA are the glyph row
22836 and area within the row from which S is constructed. START is the
22837 index of the first glyph structure covered by S. HL is a
22838 face-override for drawing S. */
22840 #ifdef HAVE_NTGUI
22841 #define OPTIONAL_HDC(hdc) HDC hdc,
22842 #define DECLARE_HDC(hdc) HDC hdc;
22843 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22844 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22845 #endif
22847 #ifndef OPTIONAL_HDC
22848 #define OPTIONAL_HDC(hdc)
22849 #define DECLARE_HDC(hdc)
22850 #define ALLOCATE_HDC(hdc, f)
22851 #define RELEASE_HDC(hdc, f)
22852 #endif
22854 static void
22855 init_glyph_string (struct glyph_string *s,
22856 OPTIONAL_HDC (hdc)
22857 XChar2b *char2b, struct window *w, struct glyph_row *row,
22858 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22860 memset (s, 0, sizeof *s);
22861 s->w = w;
22862 s->f = XFRAME (w->frame);
22863 #ifdef HAVE_NTGUI
22864 s->hdc = hdc;
22865 #endif
22866 s->display = FRAME_X_DISPLAY (s->f);
22867 s->window = FRAME_X_WINDOW (s->f);
22868 s->char2b = char2b;
22869 s->hl = hl;
22870 s->row = row;
22871 s->area = area;
22872 s->first_glyph = row->glyphs[area] + start;
22873 s->height = row->height;
22874 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22875 s->ybase = s->y + row->ascent;
22879 /* Append the list of glyph strings with head H and tail T to the list
22880 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22882 static void
22883 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22884 struct glyph_string *h, struct glyph_string *t)
22886 if (h)
22888 if (*head)
22889 (*tail)->next = h;
22890 else
22891 *head = h;
22892 h->prev = *tail;
22893 *tail = t;
22898 /* Prepend the list of glyph strings with head H and tail T to the
22899 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22900 result. */
22902 static void
22903 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22904 struct glyph_string *h, struct glyph_string *t)
22906 if (h)
22908 if (*head)
22909 (*head)->prev = t;
22910 else
22911 *tail = t;
22912 t->next = *head;
22913 *head = h;
22918 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22919 Set *HEAD and *TAIL to the resulting list. */
22921 static void
22922 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22923 struct glyph_string *s)
22925 s->next = s->prev = NULL;
22926 append_glyph_string_lists (head, tail, s, s);
22930 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22931 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22932 make sure that X resources for the face returned are allocated.
22933 Value is a pointer to a realized face that is ready for display if
22934 DISPLAY_P is non-zero. */
22936 static struct face *
22937 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22938 XChar2b *char2b, int display_p)
22940 struct face *face = FACE_FROM_ID (f, face_id);
22941 unsigned code = 0;
22943 if (face->font)
22945 code = face->font->driver->encode_char (face->font, c);
22947 if (code == FONT_INVALID_CODE)
22948 code = 0;
22950 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22952 /* Make sure X resources of the face are allocated. */
22953 #ifdef HAVE_X_WINDOWS
22954 if (display_p)
22955 #endif
22957 eassert (face != NULL);
22958 PREPARE_FACE_FOR_DISPLAY (f, face);
22961 return face;
22965 /* Get face and two-byte form of character glyph GLYPH on frame F.
22966 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22967 a pointer to a realized face that is ready for display. */
22969 static struct face *
22970 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22971 XChar2b *char2b, int *two_byte_p)
22973 struct face *face;
22974 unsigned code = 0;
22976 eassert (glyph->type == CHAR_GLYPH);
22977 face = FACE_FROM_ID (f, glyph->face_id);
22979 /* Make sure X resources of the face are allocated. */
22980 eassert (face != NULL);
22981 PREPARE_FACE_FOR_DISPLAY (f, face);
22983 if (two_byte_p)
22984 *two_byte_p = 0;
22986 if (face->font)
22988 if (CHAR_BYTE8_P (glyph->u.ch))
22989 code = CHAR_TO_BYTE8 (glyph->u.ch);
22990 else
22991 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22993 if (code == FONT_INVALID_CODE)
22994 code = 0;
22997 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22998 return face;
23002 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23003 Return 1 if FONT has a glyph for C, otherwise return 0. */
23005 static int
23006 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23008 unsigned code;
23010 if (CHAR_BYTE8_P (c))
23011 code = CHAR_TO_BYTE8 (c);
23012 else
23013 code = font->driver->encode_char (font, c);
23015 if (code == FONT_INVALID_CODE)
23016 return 0;
23017 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23018 return 1;
23022 /* Fill glyph string S with composition components specified by S->cmp.
23024 BASE_FACE is the base face of the composition.
23025 S->cmp_from is the index of the first component for S.
23027 OVERLAPS non-zero means S should draw the foreground only, and use
23028 its physical height for clipping. See also draw_glyphs.
23030 Value is the index of a component not in S. */
23032 static int
23033 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23034 int overlaps)
23036 int i;
23037 /* For all glyphs of this composition, starting at the offset
23038 S->cmp_from, until we reach the end of the definition or encounter a
23039 glyph that requires the different face, add it to S. */
23040 struct face *face;
23042 eassert (s);
23044 s->for_overlaps = overlaps;
23045 s->face = NULL;
23046 s->font = NULL;
23047 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23049 int c = COMPOSITION_GLYPH (s->cmp, i);
23051 /* TAB in a composition means display glyphs with padding space
23052 on the left or right. */
23053 if (c != '\t')
23055 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23056 -1, Qnil);
23058 face = get_char_face_and_encoding (s->f, c, face_id,
23059 s->char2b + i, 1);
23060 if (face)
23062 if (! s->face)
23064 s->face = face;
23065 s->font = s->face->font;
23067 else if (s->face != face)
23068 break;
23071 ++s->nchars;
23073 s->cmp_to = i;
23075 if (s->face == NULL)
23077 s->face = base_face->ascii_face;
23078 s->font = s->face->font;
23081 /* All glyph strings for the same composition has the same width,
23082 i.e. the width set for the first component of the composition. */
23083 s->width = s->first_glyph->pixel_width;
23085 /* If the specified font could not be loaded, use the frame's
23086 default font, but record the fact that we couldn't load it in
23087 the glyph string so that we can draw rectangles for the
23088 characters of the glyph string. */
23089 if (s->font == NULL)
23091 s->font_not_found_p = 1;
23092 s->font = FRAME_FONT (s->f);
23095 /* Adjust base line for subscript/superscript text. */
23096 s->ybase += s->first_glyph->voffset;
23098 /* This glyph string must always be drawn with 16-bit functions. */
23099 s->two_byte_p = 1;
23101 return s->cmp_to;
23104 static int
23105 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23106 int start, int end, int overlaps)
23108 struct glyph *glyph, *last;
23109 Lisp_Object lgstring;
23110 int i;
23112 s->for_overlaps = overlaps;
23113 glyph = s->row->glyphs[s->area] + start;
23114 last = s->row->glyphs[s->area] + end;
23115 s->cmp_id = glyph->u.cmp.id;
23116 s->cmp_from = glyph->slice.cmp.from;
23117 s->cmp_to = glyph->slice.cmp.to + 1;
23118 s->face = FACE_FROM_ID (s->f, face_id);
23119 lgstring = composition_gstring_from_id (s->cmp_id);
23120 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23121 glyph++;
23122 while (glyph < last
23123 && glyph->u.cmp.automatic
23124 && glyph->u.cmp.id == s->cmp_id
23125 && s->cmp_to == glyph->slice.cmp.from)
23126 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23128 for (i = s->cmp_from; i < s->cmp_to; i++)
23130 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23131 unsigned code = LGLYPH_CODE (lglyph);
23133 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23135 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23136 return glyph - s->row->glyphs[s->area];
23140 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23141 See the comment of fill_glyph_string for arguments.
23142 Value is the index of the first glyph not in S. */
23145 static int
23146 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23147 int start, int end, int overlaps)
23149 struct glyph *glyph, *last;
23150 int voffset;
23152 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23153 s->for_overlaps = overlaps;
23154 glyph = s->row->glyphs[s->area] + start;
23155 last = s->row->glyphs[s->area] + end;
23156 voffset = glyph->voffset;
23157 s->face = FACE_FROM_ID (s->f, face_id);
23158 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23159 s->nchars = 1;
23160 s->width = glyph->pixel_width;
23161 glyph++;
23162 while (glyph < last
23163 && glyph->type == GLYPHLESS_GLYPH
23164 && glyph->voffset == voffset
23165 && glyph->face_id == face_id)
23167 s->nchars++;
23168 s->width += glyph->pixel_width;
23169 glyph++;
23171 s->ybase += voffset;
23172 return glyph - s->row->glyphs[s->area];
23176 /* Fill glyph string S from a sequence of character glyphs.
23178 FACE_ID is the face id of the string. START is the index of the
23179 first glyph to consider, END is the index of the last + 1.
23180 OVERLAPS non-zero means S should draw the foreground only, and use
23181 its physical height for clipping. See also draw_glyphs.
23183 Value is the index of the first glyph not in S. */
23185 static int
23186 fill_glyph_string (struct glyph_string *s, int face_id,
23187 int start, int end, int overlaps)
23189 struct glyph *glyph, *last;
23190 int voffset;
23191 int glyph_not_available_p;
23193 eassert (s->f == XFRAME (s->w->frame));
23194 eassert (s->nchars == 0);
23195 eassert (start >= 0 && end > start);
23197 s->for_overlaps = overlaps;
23198 glyph = s->row->glyphs[s->area] + start;
23199 last = s->row->glyphs[s->area] + end;
23200 voffset = glyph->voffset;
23201 s->padding_p = glyph->padding_p;
23202 glyph_not_available_p = glyph->glyph_not_available_p;
23204 while (glyph < last
23205 && glyph->type == CHAR_GLYPH
23206 && glyph->voffset == voffset
23207 /* Same face id implies same font, nowadays. */
23208 && glyph->face_id == face_id
23209 && glyph->glyph_not_available_p == glyph_not_available_p)
23211 int two_byte_p;
23213 s->face = get_glyph_face_and_encoding (s->f, glyph,
23214 s->char2b + s->nchars,
23215 &two_byte_p);
23216 s->two_byte_p = two_byte_p;
23217 ++s->nchars;
23218 eassert (s->nchars <= end - start);
23219 s->width += glyph->pixel_width;
23220 if (glyph++->padding_p != s->padding_p)
23221 break;
23224 s->font = s->face->font;
23226 /* If the specified font could not be loaded, use the frame's font,
23227 but record the fact that we couldn't load it in
23228 S->font_not_found_p so that we can draw rectangles for the
23229 characters of the glyph string. */
23230 if (s->font == NULL || glyph_not_available_p)
23232 s->font_not_found_p = 1;
23233 s->font = FRAME_FONT (s->f);
23236 /* Adjust base line for subscript/superscript text. */
23237 s->ybase += voffset;
23239 eassert (s->face && s->face->gc);
23240 return glyph - s->row->glyphs[s->area];
23244 /* Fill glyph string S from image glyph S->first_glyph. */
23246 static void
23247 fill_image_glyph_string (struct glyph_string *s)
23249 eassert (s->first_glyph->type == IMAGE_GLYPH);
23250 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23251 eassert (s->img);
23252 s->slice = s->first_glyph->slice.img;
23253 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23254 s->font = s->face->font;
23255 s->width = s->first_glyph->pixel_width;
23257 /* Adjust base line for subscript/superscript text. */
23258 s->ybase += s->first_glyph->voffset;
23262 /* Fill glyph string S from a sequence of stretch glyphs.
23264 START is the index of the first glyph to consider,
23265 END is the index of the last + 1.
23267 Value is the index of the first glyph not in S. */
23269 static int
23270 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23272 struct glyph *glyph, *last;
23273 int voffset, face_id;
23275 eassert (s->first_glyph->type == STRETCH_GLYPH);
23277 glyph = s->row->glyphs[s->area] + start;
23278 last = s->row->glyphs[s->area] + end;
23279 face_id = glyph->face_id;
23280 s->face = FACE_FROM_ID (s->f, face_id);
23281 s->font = s->face->font;
23282 s->width = glyph->pixel_width;
23283 s->nchars = 1;
23284 voffset = glyph->voffset;
23286 for (++glyph;
23287 (glyph < last
23288 && glyph->type == STRETCH_GLYPH
23289 && glyph->voffset == voffset
23290 && glyph->face_id == face_id);
23291 ++glyph)
23292 s->width += glyph->pixel_width;
23294 /* Adjust base line for subscript/superscript text. */
23295 s->ybase += voffset;
23297 /* The case that face->gc == 0 is handled when drawing the glyph
23298 string by calling PREPARE_FACE_FOR_DISPLAY. */
23299 eassert (s->face);
23300 return glyph - s->row->glyphs[s->area];
23303 static struct font_metrics *
23304 get_per_char_metric (struct font *font, XChar2b *char2b)
23306 static struct font_metrics metrics;
23307 unsigned code;
23309 if (! font)
23310 return NULL;
23311 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23312 if (code == FONT_INVALID_CODE)
23313 return NULL;
23314 font->driver->text_extents (font, &code, 1, &metrics);
23315 return &metrics;
23318 /* EXPORT for RIF:
23319 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23320 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23321 assumed to be zero. */
23323 void
23324 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23326 *left = *right = 0;
23328 if (glyph->type == CHAR_GLYPH)
23330 struct face *face;
23331 XChar2b char2b;
23332 struct font_metrics *pcm;
23334 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23335 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23337 if (pcm->rbearing > pcm->width)
23338 *right = pcm->rbearing - pcm->width;
23339 if (pcm->lbearing < 0)
23340 *left = -pcm->lbearing;
23343 else if (glyph->type == COMPOSITE_GLYPH)
23345 if (! glyph->u.cmp.automatic)
23347 struct composition *cmp = composition_table[glyph->u.cmp.id];
23349 if (cmp->rbearing > cmp->pixel_width)
23350 *right = cmp->rbearing - cmp->pixel_width;
23351 if (cmp->lbearing < 0)
23352 *left = - cmp->lbearing;
23354 else
23356 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23357 struct font_metrics metrics;
23359 composition_gstring_width (gstring, glyph->slice.cmp.from,
23360 glyph->slice.cmp.to + 1, &metrics);
23361 if (metrics.rbearing > metrics.width)
23362 *right = metrics.rbearing - metrics.width;
23363 if (metrics.lbearing < 0)
23364 *left = - metrics.lbearing;
23370 /* Return the index of the first glyph preceding glyph string S that
23371 is overwritten by S because of S's left overhang. Value is -1
23372 if no glyphs are overwritten. */
23374 static int
23375 left_overwritten (struct glyph_string *s)
23377 int k;
23379 if (s->left_overhang)
23381 int x = 0, i;
23382 struct glyph *glyphs = s->row->glyphs[s->area];
23383 int first = s->first_glyph - glyphs;
23385 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23386 x -= glyphs[i].pixel_width;
23388 k = i + 1;
23390 else
23391 k = -1;
23393 return k;
23397 /* Return the index of the first glyph preceding glyph string S that
23398 is overwriting S because of its right overhang. Value is -1 if no
23399 glyph in front of S overwrites S. */
23401 static int
23402 left_overwriting (struct glyph_string *s)
23404 int i, k, x;
23405 struct glyph *glyphs = s->row->glyphs[s->area];
23406 int first = s->first_glyph - glyphs;
23408 k = -1;
23409 x = 0;
23410 for (i = first - 1; i >= 0; --i)
23412 int left, right;
23413 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23414 if (x + right > 0)
23415 k = i;
23416 x -= glyphs[i].pixel_width;
23419 return k;
23423 /* Return the index of the last glyph following glyph string S that is
23424 overwritten by S because of S's right overhang. Value is -1 if
23425 no such glyph is found. */
23427 static int
23428 right_overwritten (struct glyph_string *s)
23430 int k = -1;
23432 if (s->right_overhang)
23434 int x = 0, i;
23435 struct glyph *glyphs = s->row->glyphs[s->area];
23436 int first = (s->first_glyph - glyphs
23437 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23438 int end = s->row->used[s->area];
23440 for (i = first; i < end && s->right_overhang > x; ++i)
23441 x += glyphs[i].pixel_width;
23443 k = i;
23446 return k;
23450 /* Return the index of the last glyph following glyph string S that
23451 overwrites S because of its left overhang. Value is negative
23452 if no such glyph is found. */
23454 static int
23455 right_overwriting (struct glyph_string *s)
23457 int i, k, x;
23458 int end = s->row->used[s->area];
23459 struct glyph *glyphs = s->row->glyphs[s->area];
23460 int first = (s->first_glyph - glyphs
23461 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23463 k = -1;
23464 x = 0;
23465 for (i = first; i < end; ++i)
23467 int left, right;
23468 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23469 if (x - left < 0)
23470 k = i;
23471 x += glyphs[i].pixel_width;
23474 return k;
23478 /* Set background width of glyph string S. START is the index of the
23479 first glyph following S. LAST_X is the right-most x-position + 1
23480 in the drawing area. */
23482 static void
23483 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23485 /* If the face of this glyph string has to be drawn to the end of
23486 the drawing area, set S->extends_to_end_of_line_p. */
23488 if (start == s->row->used[s->area]
23489 && s->area == TEXT_AREA
23490 && ((s->row->fill_line_p
23491 && (s->hl == DRAW_NORMAL_TEXT
23492 || s->hl == DRAW_IMAGE_RAISED
23493 || s->hl == DRAW_IMAGE_SUNKEN))
23494 || s->hl == DRAW_MOUSE_FACE))
23495 s->extends_to_end_of_line_p = 1;
23497 /* If S extends its face to the end of the line, set its
23498 background_width to the distance to the right edge of the drawing
23499 area. */
23500 if (s->extends_to_end_of_line_p)
23501 s->background_width = last_x - s->x + 1;
23502 else
23503 s->background_width = s->width;
23507 /* Compute overhangs and x-positions for glyph string S and its
23508 predecessors, or successors. X is the starting x-position for S.
23509 BACKWARD_P non-zero means process predecessors. */
23511 static void
23512 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23514 if (backward_p)
23516 while (s)
23518 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23519 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23520 x -= s->width;
23521 s->x = x;
23522 s = s->prev;
23525 else
23527 while (s)
23529 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23530 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23531 s->x = x;
23532 x += s->width;
23533 s = s->next;
23540 /* The following macros are only called from draw_glyphs below.
23541 They reference the following parameters of that function directly:
23542 `w', `row', `area', and `overlap_p'
23543 as well as the following local variables:
23544 `s', `f', and `hdc' (in W32) */
23546 #ifdef HAVE_NTGUI
23547 /* On W32, silently add local `hdc' variable to argument list of
23548 init_glyph_string. */
23549 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23550 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23551 #else
23552 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23553 init_glyph_string (s, char2b, w, row, area, start, hl)
23554 #endif
23556 /* Add a glyph string for a stretch glyph to the list of strings
23557 between HEAD and TAIL. START is the index of the stretch glyph in
23558 row area AREA of glyph row ROW. END is the index of the last glyph
23559 in that glyph row area. X is the current output position assigned
23560 to the new glyph string constructed. HL overrides that face of the
23561 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23562 is the right-most x-position of the drawing area. */
23564 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23565 and below -- keep them on one line. */
23566 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23567 do \
23569 s = alloca (sizeof *s); \
23570 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23571 START = fill_stretch_glyph_string (s, START, END); \
23572 append_glyph_string (&HEAD, &TAIL, s); \
23573 s->x = (X); \
23575 while (0)
23578 /* Add a glyph string for an image glyph to the list of strings
23579 between HEAD and TAIL. START is the index of the image glyph in
23580 row area AREA of glyph row ROW. END is the index of the last glyph
23581 in that glyph row area. X is the current output position assigned
23582 to the new glyph string constructed. HL overrides that face of the
23583 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23584 is the right-most x-position of the drawing area. */
23586 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23587 do \
23589 s = alloca (sizeof *s); \
23590 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23591 fill_image_glyph_string (s); \
23592 append_glyph_string (&HEAD, &TAIL, s); \
23593 ++START; \
23594 s->x = (X); \
23596 while (0)
23599 /* Add a glyph string for a sequence of character glyphs to the list
23600 of strings between HEAD and TAIL. START is the index of the first
23601 glyph in row area AREA of glyph row ROW that is part of the new
23602 glyph string. END is the index of the last glyph in that glyph row
23603 area. X is the current output position assigned to the new glyph
23604 string constructed. HL overrides that face of the glyph; e.g. it
23605 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23606 right-most x-position of the drawing area. */
23608 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23609 do \
23611 int face_id; \
23612 XChar2b *char2b; \
23614 face_id = (row)->glyphs[area][START].face_id; \
23616 s = alloca (sizeof *s); \
23617 char2b = alloca ((END - START) * sizeof *char2b); \
23618 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23619 append_glyph_string (&HEAD, &TAIL, s); \
23620 s->x = (X); \
23621 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23623 while (0)
23626 /* Add a glyph string for a composite sequence to the list of strings
23627 between HEAD and TAIL. START is the index of the first glyph in
23628 row area AREA of glyph row ROW that is part of the new glyph
23629 string. END is the index of the last glyph in that glyph row area.
23630 X is the current output position assigned to the new glyph string
23631 constructed. HL overrides that face of the glyph; e.g. it is
23632 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23633 x-position of the drawing area. */
23635 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23636 do { \
23637 int face_id = (row)->glyphs[area][START].face_id; \
23638 struct face *base_face = FACE_FROM_ID (f, face_id); \
23639 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23640 struct composition *cmp = composition_table[cmp_id]; \
23641 XChar2b *char2b; \
23642 struct glyph_string *first_s = NULL; \
23643 int n; \
23645 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23647 /* Make glyph_strings for each glyph sequence that is drawable by \
23648 the same face, and append them to HEAD/TAIL. */ \
23649 for (n = 0; n < cmp->glyph_len;) \
23651 s = alloca (sizeof *s); \
23652 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23653 append_glyph_string (&(HEAD), &(TAIL), s); \
23654 s->cmp = cmp; \
23655 s->cmp_from = n; \
23656 s->x = (X); \
23657 if (n == 0) \
23658 first_s = s; \
23659 n = fill_composite_glyph_string (s, base_face, overlaps); \
23662 ++START; \
23663 s = first_s; \
23664 } while (0)
23667 /* Add a glyph string for a glyph-string sequence to the list of strings
23668 between HEAD and TAIL. */
23670 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23671 do { \
23672 int face_id; \
23673 XChar2b *char2b; \
23674 Lisp_Object gstring; \
23676 face_id = (row)->glyphs[area][START].face_id; \
23677 gstring = (composition_gstring_from_id \
23678 ((row)->glyphs[area][START].u.cmp.id)); \
23679 s = alloca (sizeof *s); \
23680 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23681 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23682 append_glyph_string (&(HEAD), &(TAIL), s); \
23683 s->x = (X); \
23684 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23685 } while (0)
23688 /* Add a glyph string for a sequence of glyphless character's glyphs
23689 to the list of strings between HEAD and TAIL. The meanings of
23690 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23692 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23693 do \
23695 int face_id; \
23697 face_id = (row)->glyphs[area][START].face_id; \
23699 s = alloca (sizeof *s); \
23700 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23701 append_glyph_string (&HEAD, &TAIL, s); \
23702 s->x = (X); \
23703 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23704 overlaps); \
23706 while (0)
23709 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23710 of AREA of glyph row ROW on window W between indices START and END.
23711 HL overrides the face for drawing glyph strings, e.g. it is
23712 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23713 x-positions of the drawing area.
23715 This is an ugly monster macro construct because we must use alloca
23716 to allocate glyph strings (because draw_glyphs can be called
23717 asynchronously). */
23719 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23720 do \
23722 HEAD = TAIL = NULL; \
23723 while (START < END) \
23725 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23726 switch (first_glyph->type) \
23728 case CHAR_GLYPH: \
23729 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23730 HL, X, LAST_X); \
23731 break; \
23733 case COMPOSITE_GLYPH: \
23734 if (first_glyph->u.cmp.automatic) \
23735 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23736 HL, X, LAST_X); \
23737 else \
23738 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23739 HL, X, LAST_X); \
23740 break; \
23742 case STRETCH_GLYPH: \
23743 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23744 HL, X, LAST_X); \
23745 break; \
23747 case IMAGE_GLYPH: \
23748 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23749 HL, X, LAST_X); \
23750 break; \
23752 case GLYPHLESS_GLYPH: \
23753 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23754 HL, X, LAST_X); \
23755 break; \
23757 default: \
23758 emacs_abort (); \
23761 if (s) \
23763 set_glyph_string_background_width (s, START, LAST_X); \
23764 (X) += s->width; \
23767 } while (0)
23770 /* Draw glyphs between START and END in AREA of ROW on window W,
23771 starting at x-position X. X is relative to AREA in W. HL is a
23772 face-override with the following meaning:
23774 DRAW_NORMAL_TEXT draw normally
23775 DRAW_CURSOR draw in cursor face
23776 DRAW_MOUSE_FACE draw in mouse face.
23777 DRAW_INVERSE_VIDEO draw in mode line face
23778 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23779 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23781 If OVERLAPS is non-zero, draw only the foreground of characters and
23782 clip to the physical height of ROW. Non-zero value also defines
23783 the overlapping part to be drawn:
23785 OVERLAPS_PRED overlap with preceding rows
23786 OVERLAPS_SUCC overlap with succeeding rows
23787 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23788 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23790 Value is the x-position reached, relative to AREA of W. */
23792 static int
23793 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23794 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23795 enum draw_glyphs_face hl, int overlaps)
23797 struct glyph_string *head, *tail;
23798 struct glyph_string *s;
23799 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23800 int i, j, x_reached, last_x, area_left = 0;
23801 struct frame *f = XFRAME (WINDOW_FRAME (w));
23802 DECLARE_HDC (hdc);
23804 ALLOCATE_HDC (hdc, f);
23806 /* Let's rather be paranoid than getting a SEGV. */
23807 end = min (end, row->used[area]);
23808 start = clip_to_bounds (0, start, end);
23810 /* Translate X to frame coordinates. Set last_x to the right
23811 end of the drawing area. */
23812 if (row->full_width_p)
23814 /* X is relative to the left edge of W, without scroll bars
23815 or fringes. */
23816 area_left = WINDOW_LEFT_EDGE_X (w);
23817 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23819 else
23821 area_left = window_box_left (w, area);
23822 last_x = area_left + window_box_width (w, area);
23824 x += area_left;
23826 /* Build a doubly-linked list of glyph_string structures between
23827 head and tail from what we have to draw. Note that the macro
23828 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23829 the reason we use a separate variable `i'. */
23830 i = start;
23831 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23832 if (tail)
23833 x_reached = tail->x + tail->background_width;
23834 else
23835 x_reached = x;
23837 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23838 the row, redraw some glyphs in front or following the glyph
23839 strings built above. */
23840 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23842 struct glyph_string *h, *t;
23843 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23844 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23845 int check_mouse_face = 0;
23846 int dummy_x = 0;
23848 /* If mouse highlighting is on, we may need to draw adjacent
23849 glyphs using mouse-face highlighting. */
23850 if (area == TEXT_AREA && row->mouse_face_p
23851 && hlinfo->mouse_face_beg_row >= 0
23852 && hlinfo->mouse_face_end_row >= 0)
23854 struct glyph_row *mouse_beg_row, *mouse_end_row;
23856 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23857 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23859 if (row >= mouse_beg_row && row <= mouse_end_row)
23861 check_mouse_face = 1;
23862 mouse_beg_col = (row == mouse_beg_row)
23863 ? hlinfo->mouse_face_beg_col : 0;
23864 mouse_end_col = (row == mouse_end_row)
23865 ? hlinfo->mouse_face_end_col
23866 : row->used[TEXT_AREA];
23870 /* Compute overhangs for all glyph strings. */
23871 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23872 for (s = head; s; s = s->next)
23873 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23875 /* Prepend glyph strings for glyphs in front of the first glyph
23876 string that are overwritten because of the first glyph
23877 string's left overhang. The background of all strings
23878 prepended must be drawn because the first glyph string
23879 draws over it. */
23880 i = left_overwritten (head);
23881 if (i >= 0)
23883 enum draw_glyphs_face overlap_hl;
23885 /* If this row contains mouse highlighting, attempt to draw
23886 the overlapped glyphs with the correct highlight. This
23887 code fails if the overlap encompasses more than one glyph
23888 and mouse-highlight spans only some of these glyphs.
23889 However, making it work perfectly involves a lot more
23890 code, and I don't know if the pathological case occurs in
23891 practice, so we'll stick to this for now. --- cyd */
23892 if (check_mouse_face
23893 && mouse_beg_col < start && mouse_end_col > i)
23894 overlap_hl = DRAW_MOUSE_FACE;
23895 else
23896 overlap_hl = DRAW_NORMAL_TEXT;
23898 j = i;
23899 BUILD_GLYPH_STRINGS (j, start, h, t,
23900 overlap_hl, dummy_x, last_x);
23901 start = i;
23902 compute_overhangs_and_x (t, head->x, 1);
23903 prepend_glyph_string_lists (&head, &tail, h, t);
23904 clip_head = head;
23907 /* Prepend glyph strings for glyphs in front of the first glyph
23908 string that overwrite that glyph string because of their
23909 right overhang. For these strings, only the foreground must
23910 be drawn, because it draws over the glyph string at `head'.
23911 The background must not be drawn because this would overwrite
23912 right overhangs of preceding glyphs for which no glyph
23913 strings exist. */
23914 i = left_overwriting (head);
23915 if (i >= 0)
23917 enum draw_glyphs_face overlap_hl;
23919 if (check_mouse_face
23920 && mouse_beg_col < start && mouse_end_col > i)
23921 overlap_hl = DRAW_MOUSE_FACE;
23922 else
23923 overlap_hl = DRAW_NORMAL_TEXT;
23925 clip_head = head;
23926 BUILD_GLYPH_STRINGS (i, start, h, t,
23927 overlap_hl, dummy_x, last_x);
23928 for (s = h; s; s = s->next)
23929 s->background_filled_p = 1;
23930 compute_overhangs_and_x (t, head->x, 1);
23931 prepend_glyph_string_lists (&head, &tail, h, t);
23934 /* Append glyphs strings for glyphs following the last glyph
23935 string tail that are overwritten by tail. The background of
23936 these strings has to be drawn because tail's foreground draws
23937 over it. */
23938 i = right_overwritten (tail);
23939 if (i >= 0)
23941 enum draw_glyphs_face overlap_hl;
23943 if (check_mouse_face
23944 && mouse_beg_col < i && mouse_end_col > end)
23945 overlap_hl = DRAW_MOUSE_FACE;
23946 else
23947 overlap_hl = DRAW_NORMAL_TEXT;
23949 BUILD_GLYPH_STRINGS (end, i, h, t,
23950 overlap_hl, x, last_x);
23951 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23952 we don't have `end = i;' here. */
23953 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23954 append_glyph_string_lists (&head, &tail, h, t);
23955 clip_tail = tail;
23958 /* Append glyph strings for glyphs following the last glyph
23959 string tail that overwrite tail. The foreground of such
23960 glyphs has to be drawn because it writes into the background
23961 of tail. The background must not be drawn because it could
23962 paint over the foreground of following glyphs. */
23963 i = right_overwriting (tail);
23964 if (i >= 0)
23966 enum draw_glyphs_face overlap_hl;
23967 if (check_mouse_face
23968 && mouse_beg_col < i && mouse_end_col > end)
23969 overlap_hl = DRAW_MOUSE_FACE;
23970 else
23971 overlap_hl = DRAW_NORMAL_TEXT;
23973 clip_tail = tail;
23974 i++; /* We must include the Ith glyph. */
23975 BUILD_GLYPH_STRINGS (end, i, h, t,
23976 overlap_hl, x, last_x);
23977 for (s = h; s; s = s->next)
23978 s->background_filled_p = 1;
23979 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23980 append_glyph_string_lists (&head, &tail, h, t);
23982 if (clip_head || clip_tail)
23983 for (s = head; s; s = s->next)
23985 s->clip_head = clip_head;
23986 s->clip_tail = clip_tail;
23990 /* Draw all strings. */
23991 for (s = head; s; s = s->next)
23992 FRAME_RIF (f)->draw_glyph_string (s);
23994 #ifndef HAVE_NS
23995 /* When focus a sole frame and move horizontally, this sets on_p to 0
23996 causing a failure to erase prev cursor position. */
23997 if (area == TEXT_AREA
23998 && !row->full_width_p
23999 /* When drawing overlapping rows, only the glyph strings'
24000 foreground is drawn, which doesn't erase a cursor
24001 completely. */
24002 && !overlaps)
24004 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24005 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24006 : (tail ? tail->x + tail->background_width : x));
24007 x0 -= area_left;
24008 x1 -= area_left;
24010 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24011 row->y, MATRIX_ROW_BOTTOM_Y (row));
24013 #endif
24015 /* Value is the x-position up to which drawn, relative to AREA of W.
24016 This doesn't include parts drawn because of overhangs. */
24017 if (row->full_width_p)
24018 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24019 else
24020 x_reached -= area_left;
24022 RELEASE_HDC (hdc, f);
24024 return x_reached;
24027 /* Expand row matrix if too narrow. Don't expand if area
24028 is not present. */
24030 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24032 if (!fonts_changed_p \
24033 && (it->glyph_row->glyphs[area] \
24034 < it->glyph_row->glyphs[area + 1])) \
24036 it->w->ncols_scale_factor++; \
24037 fonts_changed_p = 1; \
24041 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24042 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24044 static void
24045 append_glyph (struct it *it)
24047 struct glyph *glyph;
24048 enum glyph_row_area area = it->area;
24050 eassert (it->glyph_row);
24051 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24053 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24054 if (glyph < it->glyph_row->glyphs[area + 1])
24056 /* If the glyph row is reversed, we need to prepend the glyph
24057 rather than append it. */
24058 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24060 struct glyph *g;
24062 /* Make room for the additional glyph. */
24063 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24064 g[1] = *g;
24065 glyph = it->glyph_row->glyphs[area];
24067 glyph->charpos = CHARPOS (it->position);
24068 glyph->object = it->object;
24069 if (it->pixel_width > 0)
24071 glyph->pixel_width = it->pixel_width;
24072 glyph->padding_p = 0;
24074 else
24076 /* Assure at least 1-pixel width. Otherwise, cursor can't
24077 be displayed correctly. */
24078 glyph->pixel_width = 1;
24079 glyph->padding_p = 1;
24081 glyph->ascent = it->ascent;
24082 glyph->descent = it->descent;
24083 glyph->voffset = it->voffset;
24084 glyph->type = CHAR_GLYPH;
24085 glyph->avoid_cursor_p = it->avoid_cursor_p;
24086 glyph->multibyte_p = it->multibyte_p;
24087 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24089 /* In R2L rows, the left and the right box edges need to be
24090 drawn in reverse direction. */
24091 glyph->right_box_line_p = it->start_of_box_run_p;
24092 glyph->left_box_line_p = it->end_of_box_run_p;
24094 else
24096 glyph->left_box_line_p = it->start_of_box_run_p;
24097 glyph->right_box_line_p = it->end_of_box_run_p;
24099 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24100 || it->phys_descent > it->descent);
24101 glyph->glyph_not_available_p = it->glyph_not_available_p;
24102 glyph->face_id = it->face_id;
24103 glyph->u.ch = it->char_to_display;
24104 glyph->slice.img = null_glyph_slice;
24105 glyph->font_type = FONT_TYPE_UNKNOWN;
24106 if (it->bidi_p)
24108 glyph->resolved_level = it->bidi_it.resolved_level;
24109 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24110 emacs_abort ();
24111 glyph->bidi_type = it->bidi_it.type;
24113 else
24115 glyph->resolved_level = 0;
24116 glyph->bidi_type = UNKNOWN_BT;
24118 ++it->glyph_row->used[area];
24120 else
24121 IT_EXPAND_MATRIX_WIDTH (it, area);
24124 /* Store one glyph for the composition IT->cmp_it.id in
24125 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24126 non-null. */
24128 static void
24129 append_composite_glyph (struct it *it)
24131 struct glyph *glyph;
24132 enum glyph_row_area area = it->area;
24134 eassert (it->glyph_row);
24136 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24137 if (glyph < it->glyph_row->glyphs[area + 1])
24139 /* If the glyph row is reversed, we need to prepend the glyph
24140 rather than append it. */
24141 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24143 struct glyph *g;
24145 /* Make room for the new glyph. */
24146 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24147 g[1] = *g;
24148 glyph = it->glyph_row->glyphs[it->area];
24150 glyph->charpos = it->cmp_it.charpos;
24151 glyph->object = it->object;
24152 glyph->pixel_width = it->pixel_width;
24153 glyph->ascent = it->ascent;
24154 glyph->descent = it->descent;
24155 glyph->voffset = it->voffset;
24156 glyph->type = COMPOSITE_GLYPH;
24157 if (it->cmp_it.ch < 0)
24159 glyph->u.cmp.automatic = 0;
24160 glyph->u.cmp.id = it->cmp_it.id;
24161 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24163 else
24165 glyph->u.cmp.automatic = 1;
24166 glyph->u.cmp.id = it->cmp_it.id;
24167 glyph->slice.cmp.from = it->cmp_it.from;
24168 glyph->slice.cmp.to = it->cmp_it.to - 1;
24170 glyph->avoid_cursor_p = it->avoid_cursor_p;
24171 glyph->multibyte_p = it->multibyte_p;
24172 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24174 /* In R2L rows, the left and the right box edges need to be
24175 drawn in reverse direction. */
24176 glyph->right_box_line_p = it->start_of_box_run_p;
24177 glyph->left_box_line_p = it->end_of_box_run_p;
24179 else
24181 glyph->left_box_line_p = it->start_of_box_run_p;
24182 glyph->right_box_line_p = it->end_of_box_run_p;
24184 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24185 || it->phys_descent > it->descent);
24186 glyph->padding_p = 0;
24187 glyph->glyph_not_available_p = 0;
24188 glyph->face_id = it->face_id;
24189 glyph->font_type = FONT_TYPE_UNKNOWN;
24190 if (it->bidi_p)
24192 glyph->resolved_level = it->bidi_it.resolved_level;
24193 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24194 emacs_abort ();
24195 glyph->bidi_type = it->bidi_it.type;
24197 ++it->glyph_row->used[area];
24199 else
24200 IT_EXPAND_MATRIX_WIDTH (it, area);
24204 /* Change IT->ascent and IT->height according to the setting of
24205 IT->voffset. */
24207 static void
24208 take_vertical_position_into_account (struct it *it)
24210 if (it->voffset)
24212 if (it->voffset < 0)
24213 /* Increase the ascent so that we can display the text higher
24214 in the line. */
24215 it->ascent -= it->voffset;
24216 else
24217 /* Increase the descent so that we can display the text lower
24218 in the line. */
24219 it->descent += it->voffset;
24224 /* Produce glyphs/get display metrics for the image IT is loaded with.
24225 See the description of struct display_iterator in dispextern.h for
24226 an overview of struct display_iterator. */
24228 static void
24229 produce_image_glyph (struct it *it)
24231 struct image *img;
24232 struct face *face;
24233 int glyph_ascent, crop;
24234 struct glyph_slice slice;
24236 eassert (it->what == IT_IMAGE);
24238 face = FACE_FROM_ID (it->f, it->face_id);
24239 eassert (face);
24240 /* Make sure X resources of the face is loaded. */
24241 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24243 if (it->image_id < 0)
24245 /* Fringe bitmap. */
24246 it->ascent = it->phys_ascent = 0;
24247 it->descent = it->phys_descent = 0;
24248 it->pixel_width = 0;
24249 it->nglyphs = 0;
24250 return;
24253 img = IMAGE_FROM_ID (it->f, it->image_id);
24254 eassert (img);
24255 /* Make sure X resources of the image is loaded. */
24256 prepare_image_for_display (it->f, img);
24258 slice.x = slice.y = 0;
24259 slice.width = img->width;
24260 slice.height = img->height;
24262 if (INTEGERP (it->slice.x))
24263 slice.x = XINT (it->slice.x);
24264 else if (FLOATP (it->slice.x))
24265 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24267 if (INTEGERP (it->slice.y))
24268 slice.y = XINT (it->slice.y);
24269 else if (FLOATP (it->slice.y))
24270 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24272 if (INTEGERP (it->slice.width))
24273 slice.width = XINT (it->slice.width);
24274 else if (FLOATP (it->slice.width))
24275 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24277 if (INTEGERP (it->slice.height))
24278 slice.height = XINT (it->slice.height);
24279 else if (FLOATP (it->slice.height))
24280 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24282 if (slice.x >= img->width)
24283 slice.x = img->width;
24284 if (slice.y >= img->height)
24285 slice.y = img->height;
24286 if (slice.x + slice.width >= img->width)
24287 slice.width = img->width - slice.x;
24288 if (slice.y + slice.height > img->height)
24289 slice.height = img->height - slice.y;
24291 if (slice.width == 0 || slice.height == 0)
24292 return;
24294 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24296 it->descent = slice.height - glyph_ascent;
24297 if (slice.y == 0)
24298 it->descent += img->vmargin;
24299 if (slice.y + slice.height == img->height)
24300 it->descent += img->vmargin;
24301 it->phys_descent = it->descent;
24303 it->pixel_width = slice.width;
24304 if (slice.x == 0)
24305 it->pixel_width += img->hmargin;
24306 if (slice.x + slice.width == img->width)
24307 it->pixel_width += img->hmargin;
24309 /* It's quite possible for images to have an ascent greater than
24310 their height, so don't get confused in that case. */
24311 if (it->descent < 0)
24312 it->descent = 0;
24314 it->nglyphs = 1;
24316 if (face->box != FACE_NO_BOX)
24318 if (face->box_line_width > 0)
24320 if (slice.y == 0)
24321 it->ascent += face->box_line_width;
24322 if (slice.y + slice.height == img->height)
24323 it->descent += face->box_line_width;
24326 if (it->start_of_box_run_p && slice.x == 0)
24327 it->pixel_width += eabs (face->box_line_width);
24328 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24329 it->pixel_width += eabs (face->box_line_width);
24332 take_vertical_position_into_account (it);
24334 /* Automatically crop wide image glyphs at right edge so we can
24335 draw the cursor on same display row. */
24336 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24337 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24339 it->pixel_width -= crop;
24340 slice.width -= crop;
24343 if (it->glyph_row)
24345 struct glyph *glyph;
24346 enum glyph_row_area area = it->area;
24348 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24349 if (glyph < it->glyph_row->glyphs[area + 1])
24351 glyph->charpos = CHARPOS (it->position);
24352 glyph->object = it->object;
24353 glyph->pixel_width = it->pixel_width;
24354 glyph->ascent = glyph_ascent;
24355 glyph->descent = it->descent;
24356 glyph->voffset = it->voffset;
24357 glyph->type = IMAGE_GLYPH;
24358 glyph->avoid_cursor_p = it->avoid_cursor_p;
24359 glyph->multibyte_p = it->multibyte_p;
24360 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24362 /* In R2L rows, the left and the right box edges need to be
24363 drawn in reverse direction. */
24364 glyph->right_box_line_p = it->start_of_box_run_p;
24365 glyph->left_box_line_p = it->end_of_box_run_p;
24367 else
24369 glyph->left_box_line_p = it->start_of_box_run_p;
24370 glyph->right_box_line_p = it->end_of_box_run_p;
24372 glyph->overlaps_vertically_p = 0;
24373 glyph->padding_p = 0;
24374 glyph->glyph_not_available_p = 0;
24375 glyph->face_id = it->face_id;
24376 glyph->u.img_id = img->id;
24377 glyph->slice.img = slice;
24378 glyph->font_type = FONT_TYPE_UNKNOWN;
24379 if (it->bidi_p)
24381 glyph->resolved_level = it->bidi_it.resolved_level;
24382 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24383 emacs_abort ();
24384 glyph->bidi_type = it->bidi_it.type;
24386 ++it->glyph_row->used[area];
24388 else
24389 IT_EXPAND_MATRIX_WIDTH (it, area);
24394 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24395 of the glyph, WIDTH and HEIGHT are the width and height of the
24396 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24398 static void
24399 append_stretch_glyph (struct it *it, Lisp_Object object,
24400 int width, int height, int ascent)
24402 struct glyph *glyph;
24403 enum glyph_row_area area = it->area;
24405 eassert (ascent >= 0 && ascent <= height);
24407 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24408 if (glyph < it->glyph_row->glyphs[area + 1])
24410 /* If the glyph row is reversed, we need to prepend the glyph
24411 rather than append it. */
24412 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24414 struct glyph *g;
24416 /* Make room for the additional glyph. */
24417 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24418 g[1] = *g;
24419 glyph = it->glyph_row->glyphs[area];
24421 glyph->charpos = CHARPOS (it->position);
24422 glyph->object = object;
24423 glyph->pixel_width = width;
24424 glyph->ascent = ascent;
24425 glyph->descent = height - ascent;
24426 glyph->voffset = it->voffset;
24427 glyph->type = STRETCH_GLYPH;
24428 glyph->avoid_cursor_p = it->avoid_cursor_p;
24429 glyph->multibyte_p = it->multibyte_p;
24430 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24432 /* In R2L rows, the left and the right box edges need to be
24433 drawn in reverse direction. */
24434 glyph->right_box_line_p = it->start_of_box_run_p;
24435 glyph->left_box_line_p = it->end_of_box_run_p;
24437 else
24439 glyph->left_box_line_p = it->start_of_box_run_p;
24440 glyph->right_box_line_p = it->end_of_box_run_p;
24442 glyph->overlaps_vertically_p = 0;
24443 glyph->padding_p = 0;
24444 glyph->glyph_not_available_p = 0;
24445 glyph->face_id = it->face_id;
24446 glyph->u.stretch.ascent = ascent;
24447 glyph->u.stretch.height = height;
24448 glyph->slice.img = null_glyph_slice;
24449 glyph->font_type = FONT_TYPE_UNKNOWN;
24450 if (it->bidi_p)
24452 glyph->resolved_level = it->bidi_it.resolved_level;
24453 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24454 emacs_abort ();
24455 glyph->bidi_type = it->bidi_it.type;
24457 else
24459 glyph->resolved_level = 0;
24460 glyph->bidi_type = UNKNOWN_BT;
24462 ++it->glyph_row->used[area];
24464 else
24465 IT_EXPAND_MATRIX_WIDTH (it, area);
24468 #endif /* HAVE_WINDOW_SYSTEM */
24470 /* Produce a stretch glyph for iterator IT. IT->object is the value
24471 of the glyph property displayed. The value must be a list
24472 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24473 being recognized:
24475 1. `:width WIDTH' specifies that the space should be WIDTH *
24476 canonical char width wide. WIDTH may be an integer or floating
24477 point number.
24479 2. `:relative-width FACTOR' specifies that the width of the stretch
24480 should be computed from the width of the first character having the
24481 `glyph' property, and should be FACTOR times that width.
24483 3. `:align-to HPOS' specifies that the space should be wide enough
24484 to reach HPOS, a value in canonical character units.
24486 Exactly one of the above pairs must be present.
24488 4. `:height HEIGHT' specifies that the height of the stretch produced
24489 should be HEIGHT, measured in canonical character units.
24491 5. `:relative-height FACTOR' specifies that the height of the
24492 stretch should be FACTOR times the height of the characters having
24493 the glyph property.
24495 Either none or exactly one of 4 or 5 must be present.
24497 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24498 of the stretch should be used for the ascent of the stretch.
24499 ASCENT must be in the range 0 <= ASCENT <= 100. */
24501 void
24502 produce_stretch_glyph (struct it *it)
24504 /* (space :width WIDTH :height HEIGHT ...) */
24505 Lisp_Object prop, plist;
24506 int width = 0, height = 0, align_to = -1;
24507 int zero_width_ok_p = 0;
24508 double tem;
24509 struct font *font = NULL;
24511 #ifdef HAVE_WINDOW_SYSTEM
24512 int ascent = 0;
24513 int zero_height_ok_p = 0;
24515 if (FRAME_WINDOW_P (it->f))
24517 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24518 font = face->font ? face->font : FRAME_FONT (it->f);
24519 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24521 #endif
24523 /* List should start with `space'. */
24524 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24525 plist = XCDR (it->object);
24527 /* Compute the width of the stretch. */
24528 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24529 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24531 /* Absolute width `:width WIDTH' specified and valid. */
24532 zero_width_ok_p = 1;
24533 width = (int)tem;
24535 #ifdef HAVE_WINDOW_SYSTEM
24536 else if (FRAME_WINDOW_P (it->f)
24537 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24539 /* Relative width `:relative-width FACTOR' specified and valid.
24540 Compute the width of the characters having the `glyph'
24541 property. */
24542 struct it it2;
24543 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24545 it2 = *it;
24546 if (it->multibyte_p)
24547 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24548 else
24550 it2.c = it2.char_to_display = *p, it2.len = 1;
24551 if (! ASCII_CHAR_P (it2.c))
24552 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24555 it2.glyph_row = NULL;
24556 it2.what = IT_CHARACTER;
24557 x_produce_glyphs (&it2);
24558 width = NUMVAL (prop) * it2.pixel_width;
24560 #endif /* HAVE_WINDOW_SYSTEM */
24561 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24562 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24564 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24565 align_to = (align_to < 0
24567 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24568 else if (align_to < 0)
24569 align_to = window_box_left_offset (it->w, TEXT_AREA);
24570 width = max (0, (int)tem + align_to - it->current_x);
24571 zero_width_ok_p = 1;
24573 else
24574 /* Nothing specified -> width defaults to canonical char width. */
24575 width = FRAME_COLUMN_WIDTH (it->f);
24577 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24578 width = 1;
24580 #ifdef HAVE_WINDOW_SYSTEM
24581 /* Compute height. */
24582 if (FRAME_WINDOW_P (it->f))
24584 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24585 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24587 height = (int)tem;
24588 zero_height_ok_p = 1;
24590 else if (prop = Fplist_get (plist, QCrelative_height),
24591 NUMVAL (prop) > 0)
24592 height = FONT_HEIGHT (font) * NUMVAL (prop);
24593 else
24594 height = FONT_HEIGHT (font);
24596 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24597 height = 1;
24599 /* Compute percentage of height used for ascent. If
24600 `:ascent ASCENT' is present and valid, use that. Otherwise,
24601 derive the ascent from the font in use. */
24602 if (prop = Fplist_get (plist, QCascent),
24603 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24604 ascent = height * NUMVAL (prop) / 100.0;
24605 else if (!NILP (prop)
24606 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24607 ascent = min (max (0, (int)tem), height);
24608 else
24609 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24611 else
24612 #endif /* HAVE_WINDOW_SYSTEM */
24613 height = 1;
24615 if (width > 0 && it->line_wrap != TRUNCATE
24616 && it->current_x + width > it->last_visible_x)
24618 width = it->last_visible_x - it->current_x;
24619 #ifdef HAVE_WINDOW_SYSTEM
24620 /* Subtract one more pixel from the stretch width, but only on
24621 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24622 width -= FRAME_WINDOW_P (it->f);
24623 #endif
24626 if (width > 0 && height > 0 && it->glyph_row)
24628 Lisp_Object o_object = it->object;
24629 Lisp_Object object = it->stack[it->sp - 1].string;
24630 int n = width;
24632 if (!STRINGP (object))
24633 object = it->w->contents;
24634 #ifdef HAVE_WINDOW_SYSTEM
24635 if (FRAME_WINDOW_P (it->f))
24636 append_stretch_glyph (it, object, width, height, ascent);
24637 else
24638 #endif
24640 it->object = object;
24641 it->char_to_display = ' ';
24642 it->pixel_width = it->len = 1;
24643 while (n--)
24644 tty_append_glyph (it);
24645 it->object = o_object;
24649 it->pixel_width = width;
24650 #ifdef HAVE_WINDOW_SYSTEM
24651 if (FRAME_WINDOW_P (it->f))
24653 it->ascent = it->phys_ascent = ascent;
24654 it->descent = it->phys_descent = height - it->ascent;
24655 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24656 take_vertical_position_into_account (it);
24658 else
24659 #endif
24660 it->nglyphs = width;
24663 /* Get information about special display element WHAT in an
24664 environment described by IT. WHAT is one of IT_TRUNCATION or
24665 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24666 non-null glyph_row member. This function ensures that fields like
24667 face_id, c, len of IT are left untouched. */
24669 static void
24670 produce_special_glyphs (struct it *it, enum display_element_type what)
24672 struct it temp_it;
24673 Lisp_Object gc;
24674 GLYPH glyph;
24676 temp_it = *it;
24677 temp_it.object = make_number (0);
24678 memset (&temp_it.current, 0, sizeof temp_it.current);
24680 if (what == IT_CONTINUATION)
24682 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24683 if (it->bidi_it.paragraph_dir == R2L)
24684 SET_GLYPH_FROM_CHAR (glyph, '/');
24685 else
24686 SET_GLYPH_FROM_CHAR (glyph, '\\');
24687 if (it->dp
24688 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24690 /* FIXME: Should we mirror GC for R2L lines? */
24691 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24692 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24695 else if (what == IT_TRUNCATION)
24697 /* Truncation glyph. */
24698 SET_GLYPH_FROM_CHAR (glyph, '$');
24699 if (it->dp
24700 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24702 /* FIXME: Should we mirror GC for R2L lines? */
24703 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24704 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24707 else
24708 emacs_abort ();
24710 #ifdef HAVE_WINDOW_SYSTEM
24711 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24712 is turned off, we precede the truncation/continuation glyphs by a
24713 stretch glyph whose width is computed such that these special
24714 glyphs are aligned at the window margin, even when very different
24715 fonts are used in different glyph rows. */
24716 if (FRAME_WINDOW_P (temp_it.f)
24717 /* init_iterator calls this with it->glyph_row == NULL, and it
24718 wants only the pixel width of the truncation/continuation
24719 glyphs. */
24720 && temp_it.glyph_row
24721 /* insert_left_trunc_glyphs calls us at the beginning of the
24722 row, and it has its own calculation of the stretch glyph
24723 width. */
24724 && temp_it.glyph_row->used[TEXT_AREA] > 0
24725 && (temp_it.glyph_row->reversed_p
24726 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24727 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24729 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24731 if (stretch_width > 0)
24733 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24734 struct font *font =
24735 face->font ? face->font : FRAME_FONT (temp_it.f);
24736 int stretch_ascent =
24737 (((temp_it.ascent + temp_it.descent)
24738 * FONT_BASE (font)) / FONT_HEIGHT (font));
24740 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24741 temp_it.ascent + temp_it.descent,
24742 stretch_ascent);
24745 #endif
24747 temp_it.dp = NULL;
24748 temp_it.what = IT_CHARACTER;
24749 temp_it.len = 1;
24750 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24751 temp_it.face_id = GLYPH_FACE (glyph);
24752 temp_it.len = CHAR_BYTES (temp_it.c);
24754 PRODUCE_GLYPHS (&temp_it);
24755 it->pixel_width = temp_it.pixel_width;
24756 it->nglyphs = temp_it.pixel_width;
24759 #ifdef HAVE_WINDOW_SYSTEM
24761 /* Calculate line-height and line-spacing properties.
24762 An integer value specifies explicit pixel value.
24763 A float value specifies relative value to current face height.
24764 A cons (float . face-name) specifies relative value to
24765 height of specified face font.
24767 Returns height in pixels, or nil. */
24770 static Lisp_Object
24771 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24772 int boff, int override)
24774 Lisp_Object face_name = Qnil;
24775 int ascent, descent, height;
24777 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24778 return val;
24780 if (CONSP (val))
24782 face_name = XCAR (val);
24783 val = XCDR (val);
24784 if (!NUMBERP (val))
24785 val = make_number (1);
24786 if (NILP (face_name))
24788 height = it->ascent + it->descent;
24789 goto scale;
24793 if (NILP (face_name))
24795 font = FRAME_FONT (it->f);
24796 boff = FRAME_BASELINE_OFFSET (it->f);
24798 else if (EQ (face_name, Qt))
24800 override = 0;
24802 else
24804 int face_id;
24805 struct face *face;
24807 face_id = lookup_named_face (it->f, face_name, 0);
24808 if (face_id < 0)
24809 return make_number (-1);
24811 face = FACE_FROM_ID (it->f, face_id);
24812 font = face->font;
24813 if (font == NULL)
24814 return make_number (-1);
24815 boff = font->baseline_offset;
24816 if (font->vertical_centering)
24817 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24820 ascent = FONT_BASE (font) + boff;
24821 descent = FONT_DESCENT (font) - boff;
24823 if (override)
24825 it->override_ascent = ascent;
24826 it->override_descent = descent;
24827 it->override_boff = boff;
24830 height = ascent + descent;
24832 scale:
24833 if (FLOATP (val))
24834 height = (int)(XFLOAT_DATA (val) * height);
24835 else if (INTEGERP (val))
24836 height *= XINT (val);
24838 return make_number (height);
24842 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24843 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24844 and only if this is for a character for which no font was found.
24846 If the display method (it->glyphless_method) is
24847 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24848 length of the acronym or the hexadecimal string, UPPER_XOFF and
24849 UPPER_YOFF are pixel offsets for the upper part of the string,
24850 LOWER_XOFF and LOWER_YOFF are for the lower part.
24852 For the other display methods, LEN through LOWER_YOFF are zero. */
24854 static void
24855 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24856 short upper_xoff, short upper_yoff,
24857 short lower_xoff, short lower_yoff)
24859 struct glyph *glyph;
24860 enum glyph_row_area area = it->area;
24862 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24863 if (glyph < it->glyph_row->glyphs[area + 1])
24865 /* If the glyph row is reversed, we need to prepend the glyph
24866 rather than append it. */
24867 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24869 struct glyph *g;
24871 /* Make room for the additional glyph. */
24872 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24873 g[1] = *g;
24874 glyph = it->glyph_row->glyphs[area];
24876 glyph->charpos = CHARPOS (it->position);
24877 glyph->object = it->object;
24878 glyph->pixel_width = it->pixel_width;
24879 glyph->ascent = it->ascent;
24880 glyph->descent = it->descent;
24881 glyph->voffset = it->voffset;
24882 glyph->type = GLYPHLESS_GLYPH;
24883 glyph->u.glyphless.method = it->glyphless_method;
24884 glyph->u.glyphless.for_no_font = for_no_font;
24885 glyph->u.glyphless.len = len;
24886 glyph->u.glyphless.ch = it->c;
24887 glyph->slice.glyphless.upper_xoff = upper_xoff;
24888 glyph->slice.glyphless.upper_yoff = upper_yoff;
24889 glyph->slice.glyphless.lower_xoff = lower_xoff;
24890 glyph->slice.glyphless.lower_yoff = lower_yoff;
24891 glyph->avoid_cursor_p = it->avoid_cursor_p;
24892 glyph->multibyte_p = it->multibyte_p;
24893 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24895 /* In R2L rows, the left and the right box edges need to be
24896 drawn in reverse direction. */
24897 glyph->right_box_line_p = it->start_of_box_run_p;
24898 glyph->left_box_line_p = it->end_of_box_run_p;
24900 else
24902 glyph->left_box_line_p = it->start_of_box_run_p;
24903 glyph->right_box_line_p = it->end_of_box_run_p;
24905 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24906 || it->phys_descent > it->descent);
24907 glyph->padding_p = 0;
24908 glyph->glyph_not_available_p = 0;
24909 glyph->face_id = face_id;
24910 glyph->font_type = FONT_TYPE_UNKNOWN;
24911 if (it->bidi_p)
24913 glyph->resolved_level = it->bidi_it.resolved_level;
24914 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24915 emacs_abort ();
24916 glyph->bidi_type = it->bidi_it.type;
24918 ++it->glyph_row->used[area];
24920 else
24921 IT_EXPAND_MATRIX_WIDTH (it, area);
24925 /* Produce a glyph for a glyphless character for iterator IT.
24926 IT->glyphless_method specifies which method to use for displaying
24927 the character. See the description of enum
24928 glyphless_display_method in dispextern.h for the detail.
24930 FOR_NO_FONT is nonzero if and only if this is for a character for
24931 which no font was found. ACRONYM, if non-nil, is an acronym string
24932 for the character. */
24934 static void
24935 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24937 int face_id;
24938 struct face *face;
24939 struct font *font;
24940 int base_width, base_height, width, height;
24941 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24942 int len;
24944 /* Get the metrics of the base font. We always refer to the current
24945 ASCII face. */
24946 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24947 font = face->font ? face->font : FRAME_FONT (it->f);
24948 it->ascent = FONT_BASE (font) + font->baseline_offset;
24949 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24950 base_height = it->ascent + it->descent;
24951 base_width = font->average_width;
24953 /* Get a face ID for the glyph by utilizing a cache (the same way as
24954 done for `escape-glyph' in get_next_display_element). */
24955 if (it->f == last_glyphless_glyph_frame
24956 && it->face_id == last_glyphless_glyph_face_id)
24958 face_id = last_glyphless_glyph_merged_face_id;
24960 else
24962 /* Merge the `glyphless-char' face into the current face. */
24963 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24964 last_glyphless_glyph_frame = it->f;
24965 last_glyphless_glyph_face_id = it->face_id;
24966 last_glyphless_glyph_merged_face_id = face_id;
24969 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24971 it->pixel_width = THIN_SPACE_WIDTH;
24972 len = 0;
24973 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24975 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24977 width = CHAR_WIDTH (it->c);
24978 if (width == 0)
24979 width = 1;
24980 else if (width > 4)
24981 width = 4;
24982 it->pixel_width = base_width * width;
24983 len = 0;
24984 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24986 else
24988 char buf[7];
24989 const char *str;
24990 unsigned int code[6];
24991 int upper_len;
24992 int ascent, descent;
24993 struct font_metrics metrics_upper, metrics_lower;
24995 face = FACE_FROM_ID (it->f, face_id);
24996 font = face->font ? face->font : FRAME_FONT (it->f);
24997 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24999 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25001 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25002 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25003 if (CONSP (acronym))
25004 acronym = XCAR (acronym);
25005 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25007 else
25009 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25010 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25011 str = buf;
25013 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25014 code[len] = font->driver->encode_char (font, str[len]);
25015 upper_len = (len + 1) / 2;
25016 font->driver->text_extents (font, code, upper_len,
25017 &metrics_upper);
25018 font->driver->text_extents (font, code + upper_len, len - upper_len,
25019 &metrics_lower);
25023 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25024 width = max (metrics_upper.width, metrics_lower.width) + 4;
25025 upper_xoff = upper_yoff = 2; /* the typical case */
25026 if (base_width >= width)
25028 /* Align the upper to the left, the lower to the right. */
25029 it->pixel_width = base_width;
25030 lower_xoff = base_width - 2 - metrics_lower.width;
25032 else
25034 /* Center the shorter one. */
25035 it->pixel_width = width;
25036 if (metrics_upper.width >= metrics_lower.width)
25037 lower_xoff = (width - metrics_lower.width) / 2;
25038 else
25040 /* FIXME: This code doesn't look right. It formerly was
25041 missing the "lower_xoff = 0;", which couldn't have
25042 been right since it left lower_xoff uninitialized. */
25043 lower_xoff = 0;
25044 upper_xoff = (width - metrics_upper.width) / 2;
25048 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25049 top, bottom, and between upper and lower strings. */
25050 height = (metrics_upper.ascent + metrics_upper.descent
25051 + metrics_lower.ascent + metrics_lower.descent) + 5;
25052 /* Center vertically.
25053 H:base_height, D:base_descent
25054 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25056 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25057 descent = D - H/2 + h/2;
25058 lower_yoff = descent - 2 - ld;
25059 upper_yoff = lower_yoff - la - 1 - ud; */
25060 ascent = - (it->descent - (base_height + height + 1) / 2);
25061 descent = it->descent - (base_height - height) / 2;
25062 lower_yoff = descent - 2 - metrics_lower.descent;
25063 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25064 - metrics_upper.descent);
25065 /* Don't make the height shorter than the base height. */
25066 if (height > base_height)
25068 it->ascent = ascent;
25069 it->descent = descent;
25073 it->phys_ascent = it->ascent;
25074 it->phys_descent = it->descent;
25075 if (it->glyph_row)
25076 append_glyphless_glyph (it, face_id, for_no_font, len,
25077 upper_xoff, upper_yoff,
25078 lower_xoff, lower_yoff);
25079 it->nglyphs = 1;
25080 take_vertical_position_into_account (it);
25084 /* RIF:
25085 Produce glyphs/get display metrics for the display element IT is
25086 loaded with. See the description of struct it in dispextern.h
25087 for an overview of struct it. */
25089 void
25090 x_produce_glyphs (struct it *it)
25092 int extra_line_spacing = it->extra_line_spacing;
25094 it->glyph_not_available_p = 0;
25096 if (it->what == IT_CHARACTER)
25098 XChar2b char2b;
25099 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25100 struct font *font = face->font;
25101 struct font_metrics *pcm = NULL;
25102 int boff; /* baseline offset */
25104 if (font == NULL)
25106 /* When no suitable font is found, display this character by
25107 the method specified in the first extra slot of
25108 Vglyphless_char_display. */
25109 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25111 eassert (it->what == IT_GLYPHLESS);
25112 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25113 goto done;
25116 boff = font->baseline_offset;
25117 if (font->vertical_centering)
25118 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25120 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25122 int stretched_p;
25124 it->nglyphs = 1;
25126 if (it->override_ascent >= 0)
25128 it->ascent = it->override_ascent;
25129 it->descent = it->override_descent;
25130 boff = it->override_boff;
25132 else
25134 it->ascent = FONT_BASE (font) + boff;
25135 it->descent = FONT_DESCENT (font) - boff;
25138 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25140 pcm = get_per_char_metric (font, &char2b);
25141 if (pcm->width == 0
25142 && pcm->rbearing == 0 && pcm->lbearing == 0)
25143 pcm = NULL;
25146 if (pcm)
25148 it->phys_ascent = pcm->ascent + boff;
25149 it->phys_descent = pcm->descent - boff;
25150 it->pixel_width = pcm->width;
25152 else
25154 it->glyph_not_available_p = 1;
25155 it->phys_ascent = it->ascent;
25156 it->phys_descent = it->descent;
25157 it->pixel_width = font->space_width;
25160 if (it->constrain_row_ascent_descent_p)
25162 if (it->descent > it->max_descent)
25164 it->ascent += it->descent - it->max_descent;
25165 it->descent = it->max_descent;
25167 if (it->ascent > it->max_ascent)
25169 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25170 it->ascent = it->max_ascent;
25172 it->phys_ascent = min (it->phys_ascent, it->ascent);
25173 it->phys_descent = min (it->phys_descent, it->descent);
25174 extra_line_spacing = 0;
25177 /* If this is a space inside a region of text with
25178 `space-width' property, change its width. */
25179 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25180 if (stretched_p)
25181 it->pixel_width *= XFLOATINT (it->space_width);
25183 /* If face has a box, add the box thickness to the character
25184 height. If character has a box line to the left and/or
25185 right, add the box line width to the character's width. */
25186 if (face->box != FACE_NO_BOX)
25188 int thick = face->box_line_width;
25190 if (thick > 0)
25192 it->ascent += thick;
25193 it->descent += thick;
25195 else
25196 thick = -thick;
25198 if (it->start_of_box_run_p)
25199 it->pixel_width += thick;
25200 if (it->end_of_box_run_p)
25201 it->pixel_width += thick;
25204 /* If face has an overline, add the height of the overline
25205 (1 pixel) and a 1 pixel margin to the character height. */
25206 if (face->overline_p)
25207 it->ascent += overline_margin;
25209 if (it->constrain_row_ascent_descent_p)
25211 if (it->ascent > it->max_ascent)
25212 it->ascent = it->max_ascent;
25213 if (it->descent > it->max_descent)
25214 it->descent = it->max_descent;
25217 take_vertical_position_into_account (it);
25219 /* If we have to actually produce glyphs, do it. */
25220 if (it->glyph_row)
25222 if (stretched_p)
25224 /* Translate a space with a `space-width' property
25225 into a stretch glyph. */
25226 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25227 / FONT_HEIGHT (font));
25228 append_stretch_glyph (it, it->object, it->pixel_width,
25229 it->ascent + it->descent, ascent);
25231 else
25232 append_glyph (it);
25234 /* If characters with lbearing or rbearing are displayed
25235 in this line, record that fact in a flag of the
25236 glyph row. This is used to optimize X output code. */
25237 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25238 it->glyph_row->contains_overlapping_glyphs_p = 1;
25240 if (! stretched_p && it->pixel_width == 0)
25241 /* We assure that all visible glyphs have at least 1-pixel
25242 width. */
25243 it->pixel_width = 1;
25245 else if (it->char_to_display == '\n')
25247 /* A newline has no width, but we need the height of the
25248 line. But if previous part of the line sets a height,
25249 don't increase that height */
25251 Lisp_Object height;
25252 Lisp_Object total_height = Qnil;
25254 it->override_ascent = -1;
25255 it->pixel_width = 0;
25256 it->nglyphs = 0;
25258 height = get_it_property (it, Qline_height);
25259 /* Split (line-height total-height) list */
25260 if (CONSP (height)
25261 && CONSP (XCDR (height))
25262 && NILP (XCDR (XCDR (height))))
25264 total_height = XCAR (XCDR (height));
25265 height = XCAR (height);
25267 height = calc_line_height_property (it, height, font, boff, 1);
25269 if (it->override_ascent >= 0)
25271 it->ascent = it->override_ascent;
25272 it->descent = it->override_descent;
25273 boff = it->override_boff;
25275 else
25277 it->ascent = FONT_BASE (font) + boff;
25278 it->descent = FONT_DESCENT (font) - boff;
25281 if (EQ (height, Qt))
25283 if (it->descent > it->max_descent)
25285 it->ascent += it->descent - it->max_descent;
25286 it->descent = it->max_descent;
25288 if (it->ascent > it->max_ascent)
25290 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25291 it->ascent = it->max_ascent;
25293 it->phys_ascent = min (it->phys_ascent, it->ascent);
25294 it->phys_descent = min (it->phys_descent, it->descent);
25295 it->constrain_row_ascent_descent_p = 1;
25296 extra_line_spacing = 0;
25298 else
25300 Lisp_Object spacing;
25302 it->phys_ascent = it->ascent;
25303 it->phys_descent = it->descent;
25305 if ((it->max_ascent > 0 || it->max_descent > 0)
25306 && face->box != FACE_NO_BOX
25307 && face->box_line_width > 0)
25309 it->ascent += face->box_line_width;
25310 it->descent += face->box_line_width;
25312 if (!NILP (height)
25313 && XINT (height) > it->ascent + it->descent)
25314 it->ascent = XINT (height) - it->descent;
25316 if (!NILP (total_height))
25317 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25318 else
25320 spacing = get_it_property (it, Qline_spacing);
25321 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25323 if (INTEGERP (spacing))
25325 extra_line_spacing = XINT (spacing);
25326 if (!NILP (total_height))
25327 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25331 else /* i.e. (it->char_to_display == '\t') */
25333 if (font->space_width > 0)
25335 int tab_width = it->tab_width * font->space_width;
25336 int x = it->current_x + it->continuation_lines_width;
25337 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25339 /* If the distance from the current position to the next tab
25340 stop is less than a space character width, use the
25341 tab stop after that. */
25342 if (next_tab_x - x < font->space_width)
25343 next_tab_x += tab_width;
25345 it->pixel_width = next_tab_x - x;
25346 it->nglyphs = 1;
25347 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25348 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25350 if (it->glyph_row)
25352 append_stretch_glyph (it, it->object, it->pixel_width,
25353 it->ascent + it->descent, it->ascent);
25356 else
25358 it->pixel_width = 0;
25359 it->nglyphs = 1;
25363 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25365 /* A static composition.
25367 Note: A composition is represented as one glyph in the
25368 glyph matrix. There are no padding glyphs.
25370 Important note: pixel_width, ascent, and descent are the
25371 values of what is drawn by draw_glyphs (i.e. the values of
25372 the overall glyphs composed). */
25373 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25374 int boff; /* baseline offset */
25375 struct composition *cmp = composition_table[it->cmp_it.id];
25376 int glyph_len = cmp->glyph_len;
25377 struct font *font = face->font;
25379 it->nglyphs = 1;
25381 /* If we have not yet calculated pixel size data of glyphs of
25382 the composition for the current face font, calculate them
25383 now. Theoretically, we have to check all fonts for the
25384 glyphs, but that requires much time and memory space. So,
25385 here we check only the font of the first glyph. This may
25386 lead to incorrect display, but it's very rare, and C-l
25387 (recenter-top-bottom) can correct the display anyway. */
25388 if (! cmp->font || cmp->font != font)
25390 /* Ascent and descent of the font of the first character
25391 of this composition (adjusted by baseline offset).
25392 Ascent and descent of overall glyphs should not be less
25393 than these, respectively. */
25394 int font_ascent, font_descent, font_height;
25395 /* Bounding box of the overall glyphs. */
25396 int leftmost, rightmost, lowest, highest;
25397 int lbearing, rbearing;
25398 int i, width, ascent, descent;
25399 int left_padded = 0, right_padded = 0;
25400 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25401 XChar2b char2b;
25402 struct font_metrics *pcm;
25403 int font_not_found_p;
25404 ptrdiff_t pos;
25406 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25407 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25408 break;
25409 if (glyph_len < cmp->glyph_len)
25410 right_padded = 1;
25411 for (i = 0; i < glyph_len; i++)
25413 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25414 break;
25415 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25417 if (i > 0)
25418 left_padded = 1;
25420 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25421 : IT_CHARPOS (*it));
25422 /* If no suitable font is found, use the default font. */
25423 font_not_found_p = font == NULL;
25424 if (font_not_found_p)
25426 face = face->ascii_face;
25427 font = face->font;
25429 boff = font->baseline_offset;
25430 if (font->vertical_centering)
25431 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25432 font_ascent = FONT_BASE (font) + boff;
25433 font_descent = FONT_DESCENT (font) - boff;
25434 font_height = FONT_HEIGHT (font);
25436 cmp->font = font;
25438 pcm = NULL;
25439 if (! font_not_found_p)
25441 get_char_face_and_encoding (it->f, c, it->face_id,
25442 &char2b, 0);
25443 pcm = get_per_char_metric (font, &char2b);
25446 /* Initialize the bounding box. */
25447 if (pcm)
25449 width = cmp->glyph_len > 0 ? pcm->width : 0;
25450 ascent = pcm->ascent;
25451 descent = pcm->descent;
25452 lbearing = pcm->lbearing;
25453 rbearing = pcm->rbearing;
25455 else
25457 width = cmp->glyph_len > 0 ? font->space_width : 0;
25458 ascent = FONT_BASE (font);
25459 descent = FONT_DESCENT (font);
25460 lbearing = 0;
25461 rbearing = width;
25464 rightmost = width;
25465 leftmost = 0;
25466 lowest = - descent + boff;
25467 highest = ascent + boff;
25469 if (! font_not_found_p
25470 && font->default_ascent
25471 && CHAR_TABLE_P (Vuse_default_ascent)
25472 && !NILP (Faref (Vuse_default_ascent,
25473 make_number (it->char_to_display))))
25474 highest = font->default_ascent + boff;
25476 /* Draw the first glyph at the normal position. It may be
25477 shifted to right later if some other glyphs are drawn
25478 at the left. */
25479 cmp->offsets[i * 2] = 0;
25480 cmp->offsets[i * 2 + 1] = boff;
25481 cmp->lbearing = lbearing;
25482 cmp->rbearing = rbearing;
25484 /* Set cmp->offsets for the remaining glyphs. */
25485 for (i++; i < glyph_len; i++)
25487 int left, right, btm, top;
25488 int ch = COMPOSITION_GLYPH (cmp, i);
25489 int face_id;
25490 struct face *this_face;
25492 if (ch == '\t')
25493 ch = ' ';
25494 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25495 this_face = FACE_FROM_ID (it->f, face_id);
25496 font = this_face->font;
25498 if (font == NULL)
25499 pcm = NULL;
25500 else
25502 get_char_face_and_encoding (it->f, ch, face_id,
25503 &char2b, 0);
25504 pcm = get_per_char_metric (font, &char2b);
25506 if (! pcm)
25507 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25508 else
25510 width = pcm->width;
25511 ascent = pcm->ascent;
25512 descent = pcm->descent;
25513 lbearing = pcm->lbearing;
25514 rbearing = pcm->rbearing;
25515 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25517 /* Relative composition with or without
25518 alternate chars. */
25519 left = (leftmost + rightmost - width) / 2;
25520 btm = - descent + boff;
25521 if (font->relative_compose
25522 && (! CHAR_TABLE_P (Vignore_relative_composition)
25523 || NILP (Faref (Vignore_relative_composition,
25524 make_number (ch)))))
25527 if (- descent >= font->relative_compose)
25528 /* One extra pixel between two glyphs. */
25529 btm = highest + 1;
25530 else if (ascent <= 0)
25531 /* One extra pixel between two glyphs. */
25532 btm = lowest - 1 - ascent - descent;
25535 else
25537 /* A composition rule is specified by an integer
25538 value that encodes global and new reference
25539 points (GREF and NREF). GREF and NREF are
25540 specified by numbers as below:
25542 0---1---2 -- ascent
25546 9--10--11 -- center
25548 ---3---4---5--- baseline
25550 6---7---8 -- descent
25552 int rule = COMPOSITION_RULE (cmp, i);
25553 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25555 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25556 grefx = gref % 3, nrefx = nref % 3;
25557 grefy = gref / 3, nrefy = nref / 3;
25558 if (xoff)
25559 xoff = font_height * (xoff - 128) / 256;
25560 if (yoff)
25561 yoff = font_height * (yoff - 128) / 256;
25563 left = (leftmost
25564 + grefx * (rightmost - leftmost) / 2
25565 - nrefx * width / 2
25566 + xoff);
25568 btm = ((grefy == 0 ? highest
25569 : grefy == 1 ? 0
25570 : grefy == 2 ? lowest
25571 : (highest + lowest) / 2)
25572 - (nrefy == 0 ? ascent + descent
25573 : nrefy == 1 ? descent - boff
25574 : nrefy == 2 ? 0
25575 : (ascent + descent) / 2)
25576 + yoff);
25579 cmp->offsets[i * 2] = left;
25580 cmp->offsets[i * 2 + 1] = btm + descent;
25582 /* Update the bounding box of the overall glyphs. */
25583 if (width > 0)
25585 right = left + width;
25586 if (left < leftmost)
25587 leftmost = left;
25588 if (right > rightmost)
25589 rightmost = right;
25591 top = btm + descent + ascent;
25592 if (top > highest)
25593 highest = top;
25594 if (btm < lowest)
25595 lowest = btm;
25597 if (cmp->lbearing > left + lbearing)
25598 cmp->lbearing = left + lbearing;
25599 if (cmp->rbearing < left + rbearing)
25600 cmp->rbearing = left + rbearing;
25604 /* If there are glyphs whose x-offsets are negative,
25605 shift all glyphs to the right and make all x-offsets
25606 non-negative. */
25607 if (leftmost < 0)
25609 for (i = 0; i < cmp->glyph_len; i++)
25610 cmp->offsets[i * 2] -= leftmost;
25611 rightmost -= leftmost;
25612 cmp->lbearing -= leftmost;
25613 cmp->rbearing -= leftmost;
25616 if (left_padded && cmp->lbearing < 0)
25618 for (i = 0; i < cmp->glyph_len; i++)
25619 cmp->offsets[i * 2] -= cmp->lbearing;
25620 rightmost -= cmp->lbearing;
25621 cmp->rbearing -= cmp->lbearing;
25622 cmp->lbearing = 0;
25624 if (right_padded && rightmost < cmp->rbearing)
25626 rightmost = cmp->rbearing;
25629 cmp->pixel_width = rightmost;
25630 cmp->ascent = highest;
25631 cmp->descent = - lowest;
25632 if (cmp->ascent < font_ascent)
25633 cmp->ascent = font_ascent;
25634 if (cmp->descent < font_descent)
25635 cmp->descent = font_descent;
25638 if (it->glyph_row
25639 && (cmp->lbearing < 0
25640 || cmp->rbearing > cmp->pixel_width))
25641 it->glyph_row->contains_overlapping_glyphs_p = 1;
25643 it->pixel_width = cmp->pixel_width;
25644 it->ascent = it->phys_ascent = cmp->ascent;
25645 it->descent = it->phys_descent = cmp->descent;
25646 if (face->box != FACE_NO_BOX)
25648 int thick = face->box_line_width;
25650 if (thick > 0)
25652 it->ascent += thick;
25653 it->descent += thick;
25655 else
25656 thick = - thick;
25658 if (it->start_of_box_run_p)
25659 it->pixel_width += thick;
25660 if (it->end_of_box_run_p)
25661 it->pixel_width += thick;
25664 /* If face has an overline, add the height of the overline
25665 (1 pixel) and a 1 pixel margin to the character height. */
25666 if (face->overline_p)
25667 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 && cmp->glyph_len > 0)
25676 append_composite_glyph (it);
25678 else if (it->what == IT_COMPOSITION)
25680 /* A dynamic (automatic) composition. */
25681 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25682 Lisp_Object gstring;
25683 struct font_metrics metrics;
25685 it->nglyphs = 1;
25687 gstring = composition_gstring_from_id (it->cmp_it.id);
25688 it->pixel_width
25689 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25690 &metrics);
25691 if (it->glyph_row
25692 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25693 it->glyph_row->contains_overlapping_glyphs_p = 1;
25694 it->ascent = it->phys_ascent = metrics.ascent;
25695 it->descent = it->phys_descent = metrics.descent;
25696 if (face->box != FACE_NO_BOX)
25698 int thick = face->box_line_width;
25700 if (thick > 0)
25702 it->ascent += thick;
25703 it->descent += thick;
25705 else
25706 thick = - thick;
25708 if (it->start_of_box_run_p)
25709 it->pixel_width += thick;
25710 if (it->end_of_box_run_p)
25711 it->pixel_width += thick;
25713 /* If face has an overline, add the height of the overline
25714 (1 pixel) and a 1 pixel margin to the character height. */
25715 if (face->overline_p)
25716 it->ascent += overline_margin;
25717 take_vertical_position_into_account (it);
25718 if (it->ascent < 0)
25719 it->ascent = 0;
25720 if (it->descent < 0)
25721 it->descent = 0;
25723 if (it->glyph_row)
25724 append_composite_glyph (it);
25726 else if (it->what == IT_GLYPHLESS)
25727 produce_glyphless_glyph (it, 0, Qnil);
25728 else if (it->what == IT_IMAGE)
25729 produce_image_glyph (it);
25730 else if (it->what == IT_STRETCH)
25731 produce_stretch_glyph (it);
25733 done:
25734 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25735 because this isn't true for images with `:ascent 100'. */
25736 eassert (it->ascent >= 0 && it->descent >= 0);
25737 if (it->area == TEXT_AREA)
25738 it->current_x += it->pixel_width;
25740 if (extra_line_spacing > 0)
25742 it->descent += extra_line_spacing;
25743 if (extra_line_spacing > it->max_extra_line_spacing)
25744 it->max_extra_line_spacing = extra_line_spacing;
25747 it->max_ascent = max (it->max_ascent, it->ascent);
25748 it->max_descent = max (it->max_descent, it->descent);
25749 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25750 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25753 /* EXPORT for RIF:
25754 Output LEN glyphs starting at START at the nominal cursor position.
25755 Advance the nominal cursor over the text. The global variable
25756 updated_window contains the window being updated, updated_row is
25757 the glyph row being updated, and updated_area is the area of that
25758 row being updated. */
25760 void
25761 x_write_glyphs (struct glyph *start, int len)
25763 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25765 eassert (updated_window && updated_row);
25766 /* When the window is hscrolled, cursor hpos can legitimately be out
25767 of bounds, but we draw the cursor at the corresponding window
25768 margin in that case. */
25769 if (!updated_row->reversed_p && chpos < 0)
25770 chpos = 0;
25771 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25772 chpos = updated_row->used[TEXT_AREA] - 1;
25774 block_input ();
25776 /* Write glyphs. */
25778 hpos = start - updated_row->glyphs[updated_area];
25779 x = draw_glyphs (updated_window, output_cursor.x,
25780 updated_row, updated_area,
25781 hpos, hpos + len,
25782 DRAW_NORMAL_TEXT, 0);
25784 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25785 if (updated_area == TEXT_AREA
25786 && updated_window->phys_cursor_on_p
25787 && updated_window->phys_cursor.vpos == output_cursor.vpos
25788 && chpos >= hpos
25789 && chpos < hpos + len)
25790 updated_window->phys_cursor_on_p = 0;
25792 unblock_input ();
25794 /* Advance the output cursor. */
25795 output_cursor.hpos += len;
25796 output_cursor.x = x;
25800 /* EXPORT for RIF:
25801 Insert LEN glyphs from START at the nominal cursor position. */
25803 void
25804 x_insert_glyphs (struct glyph *start, int len)
25806 struct frame *f;
25807 struct window *w;
25808 int line_height, shift_by_width, shifted_region_width;
25809 struct glyph_row *row;
25810 struct glyph *glyph;
25811 int frame_x, frame_y;
25812 ptrdiff_t hpos;
25814 eassert (updated_window && updated_row);
25815 block_input ();
25816 w = updated_window;
25817 f = XFRAME (WINDOW_FRAME (w));
25819 /* Get the height of the line we are in. */
25820 row = updated_row;
25821 line_height = row->height;
25823 /* Get the width of the glyphs to insert. */
25824 shift_by_width = 0;
25825 for (glyph = start; glyph < start + len; ++glyph)
25826 shift_by_width += glyph->pixel_width;
25828 /* Get the width of the region to shift right. */
25829 shifted_region_width = (window_box_width (w, updated_area)
25830 - output_cursor.x
25831 - shift_by_width);
25833 /* Shift right. */
25834 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25835 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25837 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25838 line_height, shift_by_width);
25840 /* Write the glyphs. */
25841 hpos = start - row->glyphs[updated_area];
25842 draw_glyphs (w, output_cursor.x, row, updated_area,
25843 hpos, hpos + len,
25844 DRAW_NORMAL_TEXT, 0);
25846 /* Advance the output cursor. */
25847 output_cursor.hpos += len;
25848 output_cursor.x += shift_by_width;
25849 unblock_input ();
25853 /* EXPORT for RIF:
25854 Erase the current text line from the nominal cursor position
25855 (inclusive) to pixel column TO_X (exclusive). The idea is that
25856 everything from TO_X onward is already erased.
25858 TO_X is a pixel position relative to updated_area of
25859 updated_window. TO_X == -1 means clear to the end of this area. */
25861 void
25862 x_clear_end_of_line (int to_x)
25864 struct frame *f;
25865 struct window *w = updated_window;
25866 int max_x, min_y, max_y;
25867 int from_x, from_y, to_y;
25869 eassert (updated_window && updated_row);
25870 f = XFRAME (w->frame);
25872 if (updated_row->full_width_p)
25873 max_x = WINDOW_TOTAL_WIDTH (w);
25874 else
25875 max_x = window_box_width (w, updated_area);
25876 max_y = window_text_bottom_y (w);
25878 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25879 of window. For TO_X > 0, truncate to end of drawing area. */
25880 if (to_x == 0)
25881 return;
25882 else if (to_x < 0)
25883 to_x = max_x;
25884 else
25885 to_x = min (to_x, max_x);
25887 to_y = min (max_y, output_cursor.y + updated_row->height);
25889 /* Notice if the cursor will be cleared by this operation. */
25890 if (!updated_row->full_width_p)
25891 notice_overwritten_cursor (w, updated_area,
25892 output_cursor.x, -1,
25893 updated_row->y,
25894 MATRIX_ROW_BOTTOM_Y (updated_row));
25896 from_x = output_cursor.x;
25898 /* Translate to frame coordinates. */
25899 if (updated_row->full_width_p)
25901 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25902 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25904 else
25906 int area_left = window_box_left (w, updated_area);
25907 from_x += area_left;
25908 to_x += area_left;
25911 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25912 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25913 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25915 /* Prevent inadvertently clearing to end of the X window. */
25916 if (to_x > from_x && to_y > from_y)
25918 block_input ();
25919 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25920 to_x - from_x, to_y - from_y);
25921 unblock_input ();
25925 #endif /* HAVE_WINDOW_SYSTEM */
25929 /***********************************************************************
25930 Cursor types
25931 ***********************************************************************/
25933 /* Value is the internal representation of the specified cursor type
25934 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25935 of the bar cursor. */
25937 static enum text_cursor_kinds
25938 get_specified_cursor_type (Lisp_Object arg, int *width)
25940 enum text_cursor_kinds type;
25942 if (NILP (arg))
25943 return NO_CURSOR;
25945 if (EQ (arg, Qbox))
25946 return FILLED_BOX_CURSOR;
25948 if (EQ (arg, Qhollow))
25949 return HOLLOW_BOX_CURSOR;
25951 if (EQ (arg, Qbar))
25953 *width = 2;
25954 return BAR_CURSOR;
25957 if (CONSP (arg)
25958 && EQ (XCAR (arg), Qbar)
25959 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25961 *width = XINT (XCDR (arg));
25962 return BAR_CURSOR;
25965 if (EQ (arg, Qhbar))
25967 *width = 2;
25968 return HBAR_CURSOR;
25971 if (CONSP (arg)
25972 && EQ (XCAR (arg), Qhbar)
25973 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25975 *width = XINT (XCDR (arg));
25976 return HBAR_CURSOR;
25979 /* Treat anything unknown as "hollow box cursor".
25980 It was bad to signal an error; people have trouble fixing
25981 .Xdefaults with Emacs, when it has something bad in it. */
25982 type = HOLLOW_BOX_CURSOR;
25984 return type;
25987 /* Set the default cursor types for specified frame. */
25988 void
25989 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25991 int width = 1;
25992 Lisp_Object tem;
25994 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25995 FRAME_CURSOR_WIDTH (f) = width;
25997 /* By default, set up the blink-off state depending on the on-state. */
25999 tem = Fassoc (arg, Vblink_cursor_alist);
26000 if (!NILP (tem))
26002 FRAME_BLINK_OFF_CURSOR (f)
26003 = get_specified_cursor_type (XCDR (tem), &width);
26004 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26006 else
26007 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26011 #ifdef HAVE_WINDOW_SYSTEM
26013 /* Return the cursor we want to be displayed in window W. Return
26014 width of bar/hbar cursor through WIDTH arg. Return with
26015 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26016 (i.e. if the `system caret' should track this cursor).
26018 In a mini-buffer window, we want the cursor only to appear if we
26019 are reading input from this window. For the selected window, we
26020 want the cursor type given by the frame parameter or buffer local
26021 setting of cursor-type. If explicitly marked off, draw no cursor.
26022 In all other cases, we want a hollow box cursor. */
26024 static enum text_cursor_kinds
26025 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26026 int *active_cursor)
26028 struct frame *f = XFRAME (w->frame);
26029 struct buffer *b = XBUFFER (w->contents);
26030 int cursor_type = DEFAULT_CURSOR;
26031 Lisp_Object alt_cursor;
26032 int non_selected = 0;
26034 *active_cursor = 1;
26036 /* Echo area */
26037 if (cursor_in_echo_area
26038 && FRAME_HAS_MINIBUF_P (f)
26039 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26041 if (w == XWINDOW (echo_area_window))
26043 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26045 *width = FRAME_CURSOR_WIDTH (f);
26046 return FRAME_DESIRED_CURSOR (f);
26048 else
26049 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26052 *active_cursor = 0;
26053 non_selected = 1;
26056 /* Detect a nonselected window or nonselected frame. */
26057 else if (w != XWINDOW (f->selected_window)
26058 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26060 *active_cursor = 0;
26062 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26063 return NO_CURSOR;
26065 non_selected = 1;
26068 /* Never display a cursor in a window in which cursor-type is nil. */
26069 if (NILP (BVAR (b, cursor_type)))
26070 return NO_CURSOR;
26072 /* Get the normal cursor type for this window. */
26073 if (EQ (BVAR (b, cursor_type), Qt))
26075 cursor_type = FRAME_DESIRED_CURSOR (f);
26076 *width = FRAME_CURSOR_WIDTH (f);
26078 else
26079 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26081 /* Use cursor-in-non-selected-windows instead
26082 for non-selected window or frame. */
26083 if (non_selected)
26085 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26086 if (!EQ (Qt, alt_cursor))
26087 return get_specified_cursor_type (alt_cursor, width);
26088 /* t means modify the normal cursor type. */
26089 if (cursor_type == FILLED_BOX_CURSOR)
26090 cursor_type = HOLLOW_BOX_CURSOR;
26091 else if (cursor_type == BAR_CURSOR && *width > 1)
26092 --*width;
26093 return cursor_type;
26096 /* Use normal cursor if not blinked off. */
26097 if (!w->cursor_off_p)
26099 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26101 if (cursor_type == FILLED_BOX_CURSOR)
26103 /* Using a block cursor on large images can be very annoying.
26104 So use a hollow cursor for "large" images.
26105 If image is not transparent (no mask), also use hollow cursor. */
26106 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26107 if (img != NULL && IMAGEP (img->spec))
26109 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26110 where N = size of default frame font size.
26111 This should cover most of the "tiny" icons people may use. */
26112 if (!img->mask
26113 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26114 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26115 cursor_type = HOLLOW_BOX_CURSOR;
26118 else if (cursor_type != NO_CURSOR)
26120 /* Display current only supports BOX and HOLLOW cursors for images.
26121 So for now, unconditionally use a HOLLOW cursor when cursor is
26122 not a solid box cursor. */
26123 cursor_type = HOLLOW_BOX_CURSOR;
26126 return cursor_type;
26129 /* Cursor is blinked off, so determine how to "toggle" it. */
26131 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26132 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26133 return get_specified_cursor_type (XCDR (alt_cursor), width);
26135 /* Then see if frame has specified a specific blink off cursor type. */
26136 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26138 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26139 return FRAME_BLINK_OFF_CURSOR (f);
26142 #if 0
26143 /* Some people liked having a permanently visible blinking cursor,
26144 while others had very strong opinions against it. So it was
26145 decided to remove it. KFS 2003-09-03 */
26147 /* Finally perform built-in cursor blinking:
26148 filled box <-> hollow box
26149 wide [h]bar <-> narrow [h]bar
26150 narrow [h]bar <-> no cursor
26151 other type <-> no cursor */
26153 if (cursor_type == FILLED_BOX_CURSOR)
26154 return HOLLOW_BOX_CURSOR;
26156 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26158 *width = 1;
26159 return cursor_type;
26161 #endif
26163 return NO_CURSOR;
26167 /* Notice when the text cursor of window W has been completely
26168 overwritten by a drawing operation that outputs glyphs in AREA
26169 starting at X0 and ending at X1 in the line starting at Y0 and
26170 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26171 the rest of the line after X0 has been written. Y coordinates
26172 are window-relative. */
26174 static void
26175 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26176 int x0, int x1, int y0, int y1)
26178 int cx0, cx1, cy0, cy1;
26179 struct glyph_row *row;
26181 if (!w->phys_cursor_on_p)
26182 return;
26183 if (area != TEXT_AREA)
26184 return;
26186 if (w->phys_cursor.vpos < 0
26187 || w->phys_cursor.vpos >= w->current_matrix->nrows
26188 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26189 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26190 return;
26192 if (row->cursor_in_fringe_p)
26194 row->cursor_in_fringe_p = 0;
26195 draw_fringe_bitmap (w, row, row->reversed_p);
26196 w->phys_cursor_on_p = 0;
26197 return;
26200 cx0 = w->phys_cursor.x;
26201 cx1 = cx0 + w->phys_cursor_width;
26202 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26203 return;
26205 /* The cursor image will be completely removed from the
26206 screen if the output area intersects the cursor area in
26207 y-direction. When we draw in [y0 y1[, and some part of
26208 the cursor is at y < y0, that part must have been drawn
26209 before. When scrolling, the cursor is erased before
26210 actually scrolling, so we don't come here. When not
26211 scrolling, the rows above the old cursor row must have
26212 changed, and in this case these rows must have written
26213 over the cursor image.
26215 Likewise if part of the cursor is below y1, with the
26216 exception of the cursor being in the first blank row at
26217 the buffer and window end because update_text_area
26218 doesn't draw that row. (Except when it does, but
26219 that's handled in update_text_area.) */
26221 cy0 = w->phys_cursor.y;
26222 cy1 = cy0 + w->phys_cursor_height;
26223 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26224 return;
26226 w->phys_cursor_on_p = 0;
26229 #endif /* HAVE_WINDOW_SYSTEM */
26232 /************************************************************************
26233 Mouse Face
26234 ************************************************************************/
26236 #ifdef HAVE_WINDOW_SYSTEM
26238 /* EXPORT for RIF:
26239 Fix the display of area AREA of overlapping row ROW in window W
26240 with respect to the overlapping part OVERLAPS. */
26242 void
26243 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26244 enum glyph_row_area area, int overlaps)
26246 int i, x;
26248 block_input ();
26250 x = 0;
26251 for (i = 0; i < row->used[area];)
26253 if (row->glyphs[area][i].overlaps_vertically_p)
26255 int start = i, start_x = x;
26259 x += row->glyphs[area][i].pixel_width;
26260 ++i;
26262 while (i < row->used[area]
26263 && row->glyphs[area][i].overlaps_vertically_p);
26265 draw_glyphs (w, start_x, row, area,
26266 start, i,
26267 DRAW_NORMAL_TEXT, overlaps);
26269 else
26271 x += row->glyphs[area][i].pixel_width;
26272 ++i;
26276 unblock_input ();
26280 /* EXPORT:
26281 Draw the cursor glyph of window W in glyph row ROW. See the
26282 comment of draw_glyphs for the meaning of HL. */
26284 void
26285 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26286 enum draw_glyphs_face hl)
26288 /* If cursor hpos is out of bounds, don't draw garbage. This can
26289 happen in mini-buffer windows when switching between echo area
26290 glyphs and mini-buffer. */
26291 if ((row->reversed_p
26292 ? (w->phys_cursor.hpos >= 0)
26293 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26295 int on_p = w->phys_cursor_on_p;
26296 int x1;
26297 int hpos = w->phys_cursor.hpos;
26299 /* When the window is hscrolled, cursor hpos can legitimately be
26300 out of bounds, but we draw the cursor at the corresponding
26301 window margin in that case. */
26302 if (!row->reversed_p && hpos < 0)
26303 hpos = 0;
26304 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26305 hpos = row->used[TEXT_AREA] - 1;
26307 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26308 hl, 0);
26309 w->phys_cursor_on_p = on_p;
26311 if (hl == DRAW_CURSOR)
26312 w->phys_cursor_width = x1 - w->phys_cursor.x;
26313 /* When we erase the cursor, and ROW is overlapped by other
26314 rows, make sure that these overlapping parts of other rows
26315 are redrawn. */
26316 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26318 w->phys_cursor_width = x1 - w->phys_cursor.x;
26320 if (row > w->current_matrix->rows
26321 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26322 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26323 OVERLAPS_ERASED_CURSOR);
26325 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26326 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26327 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26328 OVERLAPS_ERASED_CURSOR);
26334 /* EXPORT:
26335 Erase the image of a cursor of window W from the screen. */
26337 void
26338 erase_phys_cursor (struct window *w)
26340 struct frame *f = XFRAME (w->frame);
26341 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26342 int hpos = w->phys_cursor.hpos;
26343 int vpos = w->phys_cursor.vpos;
26344 int mouse_face_here_p = 0;
26345 struct glyph_matrix *active_glyphs = w->current_matrix;
26346 struct glyph_row *cursor_row;
26347 struct glyph *cursor_glyph;
26348 enum draw_glyphs_face hl;
26350 /* No cursor displayed or row invalidated => nothing to do on the
26351 screen. */
26352 if (w->phys_cursor_type == NO_CURSOR)
26353 goto mark_cursor_off;
26355 /* VPOS >= active_glyphs->nrows means that window has been resized.
26356 Don't bother to erase the cursor. */
26357 if (vpos >= active_glyphs->nrows)
26358 goto mark_cursor_off;
26360 /* If row containing cursor is marked invalid, there is nothing we
26361 can do. */
26362 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26363 if (!cursor_row->enabled_p)
26364 goto mark_cursor_off;
26366 /* If line spacing is > 0, old cursor may only be partially visible in
26367 window after split-window. So adjust visible height. */
26368 cursor_row->visible_height = min (cursor_row->visible_height,
26369 window_text_bottom_y (w) - cursor_row->y);
26371 /* If row is completely invisible, don't attempt to delete a cursor which
26372 isn't there. This can happen if cursor is at top of a window, and
26373 we switch to a buffer with a header line in that window. */
26374 if (cursor_row->visible_height <= 0)
26375 goto mark_cursor_off;
26377 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26378 if (cursor_row->cursor_in_fringe_p)
26380 cursor_row->cursor_in_fringe_p = 0;
26381 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26382 goto mark_cursor_off;
26385 /* This can happen when the new row is shorter than the old one.
26386 In this case, either draw_glyphs or clear_end_of_line
26387 should have cleared the cursor. Note that we wouldn't be
26388 able to erase the cursor in this case because we don't have a
26389 cursor glyph at hand. */
26390 if ((cursor_row->reversed_p
26391 ? (w->phys_cursor.hpos < 0)
26392 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26393 goto mark_cursor_off;
26395 /* When the window is hscrolled, cursor hpos can legitimately be out
26396 of bounds, but we draw the cursor at the corresponding window
26397 margin in that case. */
26398 if (!cursor_row->reversed_p && hpos < 0)
26399 hpos = 0;
26400 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26401 hpos = cursor_row->used[TEXT_AREA] - 1;
26403 /* If the cursor is in the mouse face area, redisplay that when
26404 we clear the cursor. */
26405 if (! NILP (hlinfo->mouse_face_window)
26406 && coords_in_mouse_face_p (w, hpos, vpos)
26407 /* Don't redraw the cursor's spot in mouse face if it is at the
26408 end of a line (on a newline). The cursor appears there, but
26409 mouse highlighting does not. */
26410 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26411 mouse_face_here_p = 1;
26413 /* Maybe clear the display under the cursor. */
26414 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26416 int x, y, left_x;
26417 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26418 int width;
26420 cursor_glyph = get_phys_cursor_glyph (w);
26421 if (cursor_glyph == NULL)
26422 goto mark_cursor_off;
26424 width = cursor_glyph->pixel_width;
26425 left_x = window_box_left_offset (w, TEXT_AREA);
26426 x = w->phys_cursor.x;
26427 if (x < left_x)
26428 width -= left_x - x;
26429 width = min (width, window_box_width (w, TEXT_AREA) - x);
26430 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26431 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26433 if (width > 0)
26434 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26437 /* Erase the cursor by redrawing the character underneath it. */
26438 if (mouse_face_here_p)
26439 hl = DRAW_MOUSE_FACE;
26440 else
26441 hl = DRAW_NORMAL_TEXT;
26442 draw_phys_cursor_glyph (w, cursor_row, hl);
26444 mark_cursor_off:
26445 w->phys_cursor_on_p = 0;
26446 w->phys_cursor_type = NO_CURSOR;
26450 /* EXPORT:
26451 Display or clear cursor of window W. If ON is zero, clear the
26452 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26453 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26455 void
26456 display_and_set_cursor (struct window *w, int on,
26457 int hpos, int vpos, int x, int y)
26459 struct frame *f = XFRAME (w->frame);
26460 int new_cursor_type;
26461 int new_cursor_width;
26462 int active_cursor;
26463 struct glyph_row *glyph_row;
26464 struct glyph *glyph;
26466 /* This is pointless on invisible frames, and dangerous on garbaged
26467 windows and frames; in the latter case, the frame or window may
26468 be in the midst of changing its size, and x and y may be off the
26469 window. */
26470 if (! FRAME_VISIBLE_P (f)
26471 || FRAME_GARBAGED_P (f)
26472 || vpos >= w->current_matrix->nrows
26473 || hpos >= w->current_matrix->matrix_w)
26474 return;
26476 /* If cursor is off and we want it off, return quickly. */
26477 if (!on && !w->phys_cursor_on_p)
26478 return;
26480 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26481 /* If cursor row is not enabled, we don't really know where to
26482 display the cursor. */
26483 if (!glyph_row->enabled_p)
26485 w->phys_cursor_on_p = 0;
26486 return;
26489 glyph = NULL;
26490 if (!glyph_row->exact_window_width_line_p
26491 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26492 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26494 eassert (input_blocked_p ());
26496 /* Set new_cursor_type to the cursor we want to be displayed. */
26497 new_cursor_type = get_window_cursor_type (w, glyph,
26498 &new_cursor_width, &active_cursor);
26500 /* If cursor is currently being shown and we don't want it to be or
26501 it is in the wrong place, or the cursor type is not what we want,
26502 erase it. */
26503 if (w->phys_cursor_on_p
26504 && (!on
26505 || w->phys_cursor.x != x
26506 || w->phys_cursor.y != y
26507 || new_cursor_type != w->phys_cursor_type
26508 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26509 && new_cursor_width != w->phys_cursor_width)))
26510 erase_phys_cursor (w);
26512 /* Don't check phys_cursor_on_p here because that flag is only set
26513 to zero in some cases where we know that the cursor has been
26514 completely erased, to avoid the extra work of erasing the cursor
26515 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26516 still not be visible, or it has only been partly erased. */
26517 if (on)
26519 w->phys_cursor_ascent = glyph_row->ascent;
26520 w->phys_cursor_height = glyph_row->height;
26522 /* Set phys_cursor_.* before x_draw_.* is called because some
26523 of them may need the information. */
26524 w->phys_cursor.x = x;
26525 w->phys_cursor.y = glyph_row->y;
26526 w->phys_cursor.hpos = hpos;
26527 w->phys_cursor.vpos = vpos;
26530 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26531 new_cursor_type, new_cursor_width,
26532 on, active_cursor);
26536 /* Switch the display of W's cursor on or off, according to the value
26537 of ON. */
26539 static void
26540 update_window_cursor (struct window *w, int on)
26542 /* Don't update cursor in windows whose frame is in the process
26543 of being deleted. */
26544 if (w->current_matrix)
26546 int hpos = w->phys_cursor.hpos;
26547 int vpos = w->phys_cursor.vpos;
26548 struct glyph_row *row;
26550 if (vpos >= w->current_matrix->nrows
26551 || hpos >= w->current_matrix->matrix_w)
26552 return;
26554 row = MATRIX_ROW (w->current_matrix, vpos);
26556 /* When the window is hscrolled, cursor hpos can legitimately be
26557 out of bounds, but we draw the cursor at the corresponding
26558 window margin in that case. */
26559 if (!row->reversed_p && hpos < 0)
26560 hpos = 0;
26561 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26562 hpos = row->used[TEXT_AREA] - 1;
26564 block_input ();
26565 display_and_set_cursor (w, on, hpos, vpos,
26566 w->phys_cursor.x, w->phys_cursor.y);
26567 unblock_input ();
26572 /* Call update_window_cursor with parameter ON_P on all leaf windows
26573 in the window tree rooted at W. */
26575 static void
26576 update_cursor_in_window_tree (struct window *w, int on_p)
26578 while (w)
26580 if (WINDOWP (w->contents))
26581 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26582 else
26583 update_window_cursor (w, on_p);
26585 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26590 /* EXPORT:
26591 Display the cursor on window W, or clear it, according to ON_P.
26592 Don't change the cursor's position. */
26594 void
26595 x_update_cursor (struct frame *f, int on_p)
26597 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26601 /* EXPORT:
26602 Clear the cursor of window W to background color, and mark the
26603 cursor as not shown. This is used when the text where the cursor
26604 is about to be rewritten. */
26606 void
26607 x_clear_cursor (struct window *w)
26609 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26610 update_window_cursor (w, 0);
26613 #endif /* HAVE_WINDOW_SYSTEM */
26615 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26616 and MSDOS. */
26617 static void
26618 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26619 int start_hpos, int end_hpos,
26620 enum draw_glyphs_face draw)
26622 #ifdef HAVE_WINDOW_SYSTEM
26623 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26625 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26626 return;
26628 #endif
26629 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26630 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26631 #endif
26634 /* Display the active region described by mouse_face_* according to DRAW. */
26636 static void
26637 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26639 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26640 struct frame *f = XFRAME (WINDOW_FRAME (w));
26642 if (/* If window is in the process of being destroyed, don't bother
26643 to do anything. */
26644 w->current_matrix != NULL
26645 /* Don't update mouse highlight if hidden */
26646 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26647 /* Recognize when we are called to operate on rows that don't exist
26648 anymore. This can happen when a window is split. */
26649 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26651 int phys_cursor_on_p = w->phys_cursor_on_p;
26652 struct glyph_row *row, *first, *last;
26654 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26655 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26657 for (row = first; row <= last && row->enabled_p; ++row)
26659 int start_hpos, end_hpos, start_x;
26661 /* For all but the first row, the highlight starts at column 0. */
26662 if (row == first)
26664 /* R2L rows have BEG and END in reversed order, but the
26665 screen drawing geometry is always left to right. So
26666 we need to mirror the beginning and end of the
26667 highlighted area in R2L rows. */
26668 if (!row->reversed_p)
26670 start_hpos = hlinfo->mouse_face_beg_col;
26671 start_x = hlinfo->mouse_face_beg_x;
26673 else if (row == last)
26675 start_hpos = hlinfo->mouse_face_end_col;
26676 start_x = hlinfo->mouse_face_end_x;
26678 else
26680 start_hpos = 0;
26681 start_x = 0;
26684 else if (row->reversed_p && row == last)
26686 start_hpos = hlinfo->mouse_face_end_col;
26687 start_x = hlinfo->mouse_face_end_x;
26689 else
26691 start_hpos = 0;
26692 start_x = 0;
26695 if (row == last)
26697 if (!row->reversed_p)
26698 end_hpos = hlinfo->mouse_face_end_col;
26699 else if (row == first)
26700 end_hpos = hlinfo->mouse_face_beg_col;
26701 else
26703 end_hpos = row->used[TEXT_AREA];
26704 if (draw == DRAW_NORMAL_TEXT)
26705 row->fill_line_p = 1; /* Clear to end of line */
26708 else if (row->reversed_p && row == first)
26709 end_hpos = hlinfo->mouse_face_beg_col;
26710 else
26712 end_hpos = row->used[TEXT_AREA];
26713 if (draw == DRAW_NORMAL_TEXT)
26714 row->fill_line_p = 1; /* Clear to end of line */
26717 if (end_hpos > start_hpos)
26719 draw_row_with_mouse_face (w, start_x, row,
26720 start_hpos, end_hpos, draw);
26722 row->mouse_face_p
26723 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26727 #ifdef HAVE_WINDOW_SYSTEM
26728 /* When we've written over the cursor, arrange for it to
26729 be displayed again. */
26730 if (FRAME_WINDOW_P (f)
26731 && phys_cursor_on_p && !w->phys_cursor_on_p)
26733 int hpos = w->phys_cursor.hpos;
26735 /* When the window is hscrolled, cursor hpos can legitimately be
26736 out of bounds, but we draw the cursor at the corresponding
26737 window margin in that case. */
26738 if (!row->reversed_p && hpos < 0)
26739 hpos = 0;
26740 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26741 hpos = row->used[TEXT_AREA] - 1;
26743 block_input ();
26744 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26745 w->phys_cursor.x, w->phys_cursor.y);
26746 unblock_input ();
26748 #endif /* HAVE_WINDOW_SYSTEM */
26751 #ifdef HAVE_WINDOW_SYSTEM
26752 /* Change the mouse cursor. */
26753 if (FRAME_WINDOW_P (f))
26755 if (draw == DRAW_NORMAL_TEXT
26756 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26757 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26758 else if (draw == DRAW_MOUSE_FACE)
26759 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26760 else
26761 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26763 #endif /* HAVE_WINDOW_SYSTEM */
26766 /* EXPORT:
26767 Clear out the mouse-highlighted active region.
26768 Redraw it un-highlighted first. Value is non-zero if mouse
26769 face was actually drawn unhighlighted. */
26772 clear_mouse_face (Mouse_HLInfo *hlinfo)
26774 int cleared = 0;
26776 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26778 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26779 cleared = 1;
26782 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26783 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26784 hlinfo->mouse_face_window = Qnil;
26785 hlinfo->mouse_face_overlay = Qnil;
26786 return cleared;
26789 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26790 within the mouse face on that window. */
26791 static int
26792 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26794 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26796 /* Quickly resolve the easy cases. */
26797 if (!(WINDOWP (hlinfo->mouse_face_window)
26798 && XWINDOW (hlinfo->mouse_face_window) == w))
26799 return 0;
26800 if (vpos < hlinfo->mouse_face_beg_row
26801 || vpos > hlinfo->mouse_face_end_row)
26802 return 0;
26803 if (vpos > hlinfo->mouse_face_beg_row
26804 && vpos < hlinfo->mouse_face_end_row)
26805 return 1;
26807 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26809 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26811 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26812 return 1;
26814 else if ((vpos == hlinfo->mouse_face_beg_row
26815 && hpos >= hlinfo->mouse_face_beg_col)
26816 || (vpos == hlinfo->mouse_face_end_row
26817 && hpos < hlinfo->mouse_face_end_col))
26818 return 1;
26820 else
26822 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26824 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26825 return 1;
26827 else if ((vpos == hlinfo->mouse_face_beg_row
26828 && hpos <= hlinfo->mouse_face_beg_col)
26829 || (vpos == hlinfo->mouse_face_end_row
26830 && hpos > hlinfo->mouse_face_end_col))
26831 return 1;
26833 return 0;
26837 /* EXPORT:
26838 Non-zero if physical cursor of window W is within mouse face. */
26841 cursor_in_mouse_face_p (struct window *w)
26843 int hpos = w->phys_cursor.hpos;
26844 int vpos = w->phys_cursor.vpos;
26845 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26847 /* When the window is hscrolled, cursor hpos can legitimately be out
26848 of bounds, but we draw the cursor at the corresponding window
26849 margin in that case. */
26850 if (!row->reversed_p && hpos < 0)
26851 hpos = 0;
26852 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26853 hpos = row->used[TEXT_AREA] - 1;
26855 return coords_in_mouse_face_p (w, hpos, vpos);
26860 /* Find the glyph rows START_ROW and END_ROW of window W that display
26861 characters between buffer positions START_CHARPOS and END_CHARPOS
26862 (excluding END_CHARPOS). DISP_STRING is a display string that
26863 covers these buffer positions. This is similar to
26864 row_containing_pos, but is more accurate when bidi reordering makes
26865 buffer positions change non-linearly with glyph rows. */
26866 static void
26867 rows_from_pos_range (struct window *w,
26868 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26869 Lisp_Object disp_string,
26870 struct glyph_row **start, struct glyph_row **end)
26872 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26873 int last_y = window_text_bottom_y (w);
26874 struct glyph_row *row;
26876 *start = NULL;
26877 *end = NULL;
26879 while (!first->enabled_p
26880 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26881 first++;
26883 /* Find the START row. */
26884 for (row = first;
26885 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26886 row++)
26888 /* A row can potentially be the START row if the range of the
26889 characters it displays intersects the range
26890 [START_CHARPOS..END_CHARPOS). */
26891 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26892 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26893 /* See the commentary in row_containing_pos, for the
26894 explanation of the complicated way to check whether
26895 some position is beyond the end of the characters
26896 displayed by a row. */
26897 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26898 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26899 && !row->ends_at_zv_p
26900 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26901 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26902 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26903 && !row->ends_at_zv_p
26904 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26906 /* Found a candidate row. Now make sure at least one of the
26907 glyphs it displays has a charpos from the range
26908 [START_CHARPOS..END_CHARPOS).
26910 This is not obvious because bidi reordering could make
26911 buffer positions of a row be 1,2,3,102,101,100, and if we
26912 want to highlight characters in [50..60), we don't want
26913 this row, even though [50..60) does intersect [1..103),
26914 the range of character positions given by the row's start
26915 and end positions. */
26916 struct glyph *g = row->glyphs[TEXT_AREA];
26917 struct glyph *e = g + row->used[TEXT_AREA];
26919 while (g < e)
26921 if (((BUFFERP (g->object) || INTEGERP (g->object))
26922 && start_charpos <= g->charpos && g->charpos < end_charpos)
26923 /* A glyph that comes from DISP_STRING is by
26924 definition to be highlighted. */
26925 || EQ (g->object, disp_string))
26926 *start = row;
26927 g++;
26929 if (*start)
26930 break;
26934 /* Find the END row. */
26935 if (!*start
26936 /* If the last row is partially visible, start looking for END
26937 from that row, instead of starting from FIRST. */
26938 && !(row->enabled_p
26939 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26940 row = first;
26941 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26943 struct glyph_row *next = row + 1;
26944 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26946 if (!next->enabled_p
26947 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26948 /* The first row >= START whose range of displayed characters
26949 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26950 is the row END + 1. */
26951 || (start_charpos < next_start
26952 && end_charpos < next_start)
26953 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26954 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26955 && !next->ends_at_zv_p
26956 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26957 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26958 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26959 && !next->ends_at_zv_p
26960 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26962 *end = row;
26963 break;
26965 else
26967 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26968 but none of the characters it displays are in the range, it is
26969 also END + 1. */
26970 struct glyph *g = next->glyphs[TEXT_AREA];
26971 struct glyph *s = g;
26972 struct glyph *e = g + next->used[TEXT_AREA];
26974 while (g < e)
26976 if (((BUFFERP (g->object) || INTEGERP (g->object))
26977 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26978 /* If the buffer position of the first glyph in
26979 the row is equal to END_CHARPOS, it means
26980 the last character to be highlighted is the
26981 newline of ROW, and we must consider NEXT as
26982 END, not END+1. */
26983 || (((!next->reversed_p && g == s)
26984 || (next->reversed_p && g == e - 1))
26985 && (g->charpos == end_charpos
26986 /* Special case for when NEXT is an
26987 empty line at ZV. */
26988 || (g->charpos == -1
26989 && !row->ends_at_zv_p
26990 && next_start == end_charpos)))))
26991 /* A glyph that comes from DISP_STRING is by
26992 definition to be highlighted. */
26993 || EQ (g->object, disp_string))
26994 break;
26995 g++;
26997 if (g == e)
26999 *end = row;
27000 break;
27002 /* The first row that ends at ZV must be the last to be
27003 highlighted. */
27004 else if (next->ends_at_zv_p)
27006 *end = next;
27007 break;
27013 /* This function sets the mouse_face_* elements of HLINFO, assuming
27014 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27015 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27016 for the overlay or run of text properties specifying the mouse
27017 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27018 before-string and after-string that must also be highlighted.
27019 DISP_STRING, if non-nil, is a display string that may cover some
27020 or all of the highlighted text. */
27022 static void
27023 mouse_face_from_buffer_pos (Lisp_Object window,
27024 Mouse_HLInfo *hlinfo,
27025 ptrdiff_t mouse_charpos,
27026 ptrdiff_t start_charpos,
27027 ptrdiff_t end_charpos,
27028 Lisp_Object before_string,
27029 Lisp_Object after_string,
27030 Lisp_Object disp_string)
27032 struct window *w = XWINDOW (window);
27033 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27034 struct glyph_row *r1, *r2;
27035 struct glyph *glyph, *end;
27036 ptrdiff_t ignore, pos;
27037 int x;
27039 eassert (NILP (disp_string) || STRINGP (disp_string));
27040 eassert (NILP (before_string) || STRINGP (before_string));
27041 eassert (NILP (after_string) || STRINGP (after_string));
27043 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27044 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27045 if (r1 == NULL)
27046 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27047 /* If the before-string or display-string contains newlines,
27048 rows_from_pos_range skips to its last row. Move back. */
27049 if (!NILP (before_string) || !NILP (disp_string))
27051 struct glyph_row *prev;
27052 while ((prev = r1 - 1, prev >= first)
27053 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27054 && prev->used[TEXT_AREA] > 0)
27056 struct glyph *beg = prev->glyphs[TEXT_AREA];
27057 glyph = beg + prev->used[TEXT_AREA];
27058 while (--glyph >= beg && INTEGERP (glyph->object));
27059 if (glyph < beg
27060 || !(EQ (glyph->object, before_string)
27061 || EQ (glyph->object, disp_string)))
27062 break;
27063 r1 = prev;
27066 if (r2 == NULL)
27068 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27069 hlinfo->mouse_face_past_end = 1;
27071 else if (!NILP (after_string))
27073 /* If the after-string has newlines, advance to its last row. */
27074 struct glyph_row *next;
27075 struct glyph_row *last
27076 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27078 for (next = r2 + 1;
27079 next <= last
27080 && next->used[TEXT_AREA] > 0
27081 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27082 ++next)
27083 r2 = next;
27085 /* The rest of the display engine assumes that mouse_face_beg_row is
27086 either above mouse_face_end_row or identical to it. But with
27087 bidi-reordered continued lines, the row for START_CHARPOS could
27088 be below the row for END_CHARPOS. If so, swap the rows and store
27089 them in correct order. */
27090 if (r1->y > r2->y)
27092 struct glyph_row *tem = r2;
27094 r2 = r1;
27095 r1 = tem;
27098 hlinfo->mouse_face_beg_y = r1->y;
27099 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27100 hlinfo->mouse_face_end_y = r2->y;
27101 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27103 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27104 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27105 could be anywhere in the row and in any order. The strategy
27106 below is to find the leftmost and the rightmost glyph that
27107 belongs to either of these 3 strings, or whose position is
27108 between START_CHARPOS and END_CHARPOS, and highlight all the
27109 glyphs between those two. This may cover more than just the text
27110 between START_CHARPOS and END_CHARPOS if the range of characters
27111 strides the bidi level boundary, e.g. if the beginning is in R2L
27112 text while the end is in L2R text or vice versa. */
27113 if (!r1->reversed_p)
27115 /* This row is in a left to right paragraph. Scan it left to
27116 right. */
27117 glyph = r1->glyphs[TEXT_AREA];
27118 end = glyph + r1->used[TEXT_AREA];
27119 x = r1->x;
27121 /* Skip truncation glyphs at the start of the glyph row. */
27122 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27123 for (; glyph < end
27124 && INTEGERP (glyph->object)
27125 && glyph->charpos < 0;
27126 ++glyph)
27127 x += glyph->pixel_width;
27129 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27130 or DISP_STRING, and the first glyph from buffer whose
27131 position is between START_CHARPOS and END_CHARPOS. */
27132 for (; glyph < end
27133 && !INTEGERP (glyph->object)
27134 && !EQ (glyph->object, disp_string)
27135 && !(BUFFERP (glyph->object)
27136 && (glyph->charpos >= start_charpos
27137 && glyph->charpos < end_charpos));
27138 ++glyph)
27140 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27141 are present at buffer positions between START_CHARPOS and
27142 END_CHARPOS, or if they come from an overlay. */
27143 if (EQ (glyph->object, before_string))
27145 pos = string_buffer_position (before_string,
27146 start_charpos);
27147 /* If pos == 0, it means before_string came from an
27148 overlay, not from a buffer position. */
27149 if (!pos || (pos >= start_charpos && pos < end_charpos))
27150 break;
27152 else if (EQ (glyph->object, after_string))
27154 pos = string_buffer_position (after_string, end_charpos);
27155 if (!pos || (pos >= start_charpos && pos < end_charpos))
27156 break;
27158 x += glyph->pixel_width;
27160 hlinfo->mouse_face_beg_x = x;
27161 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27163 else
27165 /* This row is in a right to left paragraph. Scan it right to
27166 left. */
27167 struct glyph *g;
27169 end = r1->glyphs[TEXT_AREA] - 1;
27170 glyph = end + r1->used[TEXT_AREA];
27172 /* Skip truncation glyphs at the start of the glyph row. */
27173 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27174 for (; glyph > end
27175 && INTEGERP (glyph->object)
27176 && glyph->charpos < 0;
27177 --glyph)
27180 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27181 or DISP_STRING, and the first glyph from buffer whose
27182 position is between START_CHARPOS and END_CHARPOS. */
27183 for (; glyph > end
27184 && !INTEGERP (glyph->object)
27185 && !EQ (glyph->object, disp_string)
27186 && !(BUFFERP (glyph->object)
27187 && (glyph->charpos >= start_charpos
27188 && glyph->charpos < end_charpos));
27189 --glyph)
27191 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27192 are present at buffer positions between START_CHARPOS and
27193 END_CHARPOS, or if they come from an overlay. */
27194 if (EQ (glyph->object, before_string))
27196 pos = string_buffer_position (before_string, start_charpos);
27197 /* If pos == 0, it means before_string came from an
27198 overlay, not from a buffer position. */
27199 if (!pos || (pos >= start_charpos && pos < end_charpos))
27200 break;
27202 else if (EQ (glyph->object, after_string))
27204 pos = string_buffer_position (after_string, end_charpos);
27205 if (!pos || (pos >= start_charpos && pos < end_charpos))
27206 break;
27210 glyph++; /* first glyph to the right of the highlighted area */
27211 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27212 x += g->pixel_width;
27213 hlinfo->mouse_face_beg_x = x;
27214 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27217 /* If the highlight ends in a different row, compute GLYPH and END
27218 for the end row. Otherwise, reuse the values computed above for
27219 the row where the highlight begins. */
27220 if (r2 != r1)
27222 if (!r2->reversed_p)
27224 glyph = r2->glyphs[TEXT_AREA];
27225 end = glyph + r2->used[TEXT_AREA];
27226 x = r2->x;
27228 else
27230 end = r2->glyphs[TEXT_AREA] - 1;
27231 glyph = end + r2->used[TEXT_AREA];
27235 if (!r2->reversed_p)
27237 /* Skip truncation and continuation glyphs near the end of the
27238 row, and also blanks and stretch glyphs inserted by
27239 extend_face_to_end_of_line. */
27240 while (end > glyph
27241 && INTEGERP ((end - 1)->object))
27242 --end;
27243 /* Scan the rest of the glyph row from the end, looking for the
27244 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27245 DISP_STRING, or whose position is between START_CHARPOS
27246 and END_CHARPOS */
27247 for (--end;
27248 end > glyph
27249 && !INTEGERP (end->object)
27250 && !EQ (end->object, disp_string)
27251 && !(BUFFERP (end->object)
27252 && (end->charpos >= start_charpos
27253 && end->charpos < end_charpos));
27254 --end)
27256 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27257 are present at buffer positions between START_CHARPOS and
27258 END_CHARPOS, or if they come from an overlay. */
27259 if (EQ (end->object, before_string))
27261 pos = string_buffer_position (before_string, start_charpos);
27262 if (!pos || (pos >= start_charpos && pos < end_charpos))
27263 break;
27265 else if (EQ (end->object, after_string))
27267 pos = string_buffer_position (after_string, end_charpos);
27268 if (!pos || (pos >= start_charpos && pos < end_charpos))
27269 break;
27272 /* Find the X coordinate of the last glyph to be highlighted. */
27273 for (; glyph <= end; ++glyph)
27274 x += glyph->pixel_width;
27276 hlinfo->mouse_face_end_x = x;
27277 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27279 else
27281 /* Skip truncation and continuation glyphs near the end of the
27282 row, and also blanks and stretch glyphs inserted by
27283 extend_face_to_end_of_line. */
27284 x = r2->x;
27285 end++;
27286 while (end < glyph
27287 && INTEGERP (end->object))
27289 x += end->pixel_width;
27290 ++end;
27292 /* Scan the rest of the glyph row from the end, looking for the
27293 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27294 DISP_STRING, or whose position is between START_CHARPOS
27295 and END_CHARPOS */
27296 for ( ;
27297 end < glyph
27298 && !INTEGERP (end->object)
27299 && !EQ (end->object, disp_string)
27300 && !(BUFFERP (end->object)
27301 && (end->charpos >= start_charpos
27302 && end->charpos < end_charpos));
27303 ++end)
27305 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27306 are present at buffer positions between START_CHARPOS and
27307 END_CHARPOS, or if they come from an overlay. */
27308 if (EQ (end->object, before_string))
27310 pos = string_buffer_position (before_string, start_charpos);
27311 if (!pos || (pos >= start_charpos && pos < end_charpos))
27312 break;
27314 else if (EQ (end->object, after_string))
27316 pos = string_buffer_position (after_string, end_charpos);
27317 if (!pos || (pos >= start_charpos && pos < end_charpos))
27318 break;
27320 x += end->pixel_width;
27322 /* If we exited the above loop because we arrived at the last
27323 glyph of the row, and its buffer position is still not in
27324 range, it means the last character in range is the preceding
27325 newline. Bump the end column and x values to get past the
27326 last glyph. */
27327 if (end == glyph
27328 && BUFFERP (end->object)
27329 && (end->charpos < start_charpos
27330 || end->charpos >= end_charpos))
27332 x += end->pixel_width;
27333 ++end;
27335 hlinfo->mouse_face_end_x = x;
27336 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27339 hlinfo->mouse_face_window = window;
27340 hlinfo->mouse_face_face_id
27341 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27342 mouse_charpos + 1,
27343 !hlinfo->mouse_face_hidden, -1);
27344 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27347 /* The following function is not used anymore (replaced with
27348 mouse_face_from_string_pos), but I leave it here for the time
27349 being, in case someone would. */
27351 #if 0 /* not used */
27353 /* Find the position of the glyph for position POS in OBJECT in
27354 window W's current matrix, and return in *X, *Y the pixel
27355 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27357 RIGHT_P non-zero means return the position of the right edge of the
27358 glyph, RIGHT_P zero means return the left edge position.
27360 If no glyph for POS exists in the matrix, return the position of
27361 the glyph with the next smaller position that is in the matrix, if
27362 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27363 exists in the matrix, return the position of the glyph with the
27364 next larger position in OBJECT.
27366 Value is non-zero if a glyph was found. */
27368 static int
27369 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27370 int *hpos, int *vpos, int *x, int *y, int right_p)
27372 int yb = window_text_bottom_y (w);
27373 struct glyph_row *r;
27374 struct glyph *best_glyph = NULL;
27375 struct glyph_row *best_row = NULL;
27376 int best_x = 0;
27378 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27379 r->enabled_p && r->y < yb;
27380 ++r)
27382 struct glyph *g = r->glyphs[TEXT_AREA];
27383 struct glyph *e = g + r->used[TEXT_AREA];
27384 int gx;
27386 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27387 if (EQ (g->object, object))
27389 if (g->charpos == pos)
27391 best_glyph = g;
27392 best_x = gx;
27393 best_row = r;
27394 goto found;
27396 else if (best_glyph == NULL
27397 || ((eabs (g->charpos - pos)
27398 < eabs (best_glyph->charpos - pos))
27399 && (right_p
27400 ? g->charpos < pos
27401 : g->charpos > pos)))
27403 best_glyph = g;
27404 best_x = gx;
27405 best_row = r;
27410 found:
27412 if (best_glyph)
27414 *x = best_x;
27415 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27417 if (right_p)
27419 *x += best_glyph->pixel_width;
27420 ++*hpos;
27423 *y = best_row->y;
27424 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27427 return best_glyph != NULL;
27429 #endif /* not used */
27431 /* Find the positions of the first and the last glyphs in window W's
27432 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27433 (assumed to be a string), and return in HLINFO's mouse_face_*
27434 members the pixel and column/row coordinates of those glyphs. */
27436 static void
27437 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27438 Lisp_Object object,
27439 ptrdiff_t startpos, ptrdiff_t endpos)
27441 int yb = window_text_bottom_y (w);
27442 struct glyph_row *r;
27443 struct glyph *g, *e;
27444 int gx;
27445 int found = 0;
27447 /* Find the glyph row with at least one position in the range
27448 [STARTPOS..ENDPOS], and the first glyph in that row whose
27449 position belongs to that range. */
27450 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27451 r->enabled_p && r->y < yb;
27452 ++r)
27454 if (!r->reversed_p)
27456 g = r->glyphs[TEXT_AREA];
27457 e = g + r->used[TEXT_AREA];
27458 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27459 if (EQ (g->object, object)
27460 && startpos <= g->charpos && g->charpos <= endpos)
27462 hlinfo->mouse_face_beg_row
27463 = MATRIX_ROW_VPOS (r, w->current_matrix);
27464 hlinfo->mouse_face_beg_y = r->y;
27465 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27466 hlinfo->mouse_face_beg_x = gx;
27467 found = 1;
27468 break;
27471 else
27473 struct glyph *g1;
27475 e = r->glyphs[TEXT_AREA];
27476 g = e + r->used[TEXT_AREA];
27477 for ( ; g > e; --g)
27478 if (EQ ((g-1)->object, object)
27479 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27481 hlinfo->mouse_face_beg_row
27482 = MATRIX_ROW_VPOS (r, w->current_matrix);
27483 hlinfo->mouse_face_beg_y = r->y;
27484 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27485 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27486 gx += g1->pixel_width;
27487 hlinfo->mouse_face_beg_x = gx;
27488 found = 1;
27489 break;
27492 if (found)
27493 break;
27496 if (!found)
27497 return;
27499 /* Starting with the next row, look for the first row which does NOT
27500 include any glyphs whose positions are in the range. */
27501 for (++r; r->enabled_p && r->y < yb; ++r)
27503 g = r->glyphs[TEXT_AREA];
27504 e = g + r->used[TEXT_AREA];
27505 found = 0;
27506 for ( ; g < e; ++g)
27507 if (EQ (g->object, object)
27508 && startpos <= g->charpos && g->charpos <= endpos)
27510 found = 1;
27511 break;
27513 if (!found)
27514 break;
27517 /* The highlighted region ends on the previous row. */
27518 r--;
27520 /* Set the end row and its vertical pixel coordinate. */
27521 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27522 hlinfo->mouse_face_end_y = r->y;
27524 /* Compute and set the end column and the end column's horizontal
27525 pixel coordinate. */
27526 if (!r->reversed_p)
27528 g = r->glyphs[TEXT_AREA];
27529 e = g + r->used[TEXT_AREA];
27530 for ( ; e > g; --e)
27531 if (EQ ((e-1)->object, object)
27532 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27533 break;
27534 hlinfo->mouse_face_end_col = e - g;
27536 for (gx = r->x; g < e; ++g)
27537 gx += g->pixel_width;
27538 hlinfo->mouse_face_end_x = gx;
27540 else
27542 e = r->glyphs[TEXT_AREA];
27543 g = e + r->used[TEXT_AREA];
27544 for (gx = r->x ; e < g; ++e)
27546 if (EQ (e->object, object)
27547 && startpos <= e->charpos && e->charpos <= endpos)
27548 break;
27549 gx += e->pixel_width;
27551 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27552 hlinfo->mouse_face_end_x = gx;
27556 #ifdef HAVE_WINDOW_SYSTEM
27558 /* See if position X, Y is within a hot-spot of an image. */
27560 static int
27561 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27563 if (!CONSP (hot_spot))
27564 return 0;
27566 if (EQ (XCAR (hot_spot), Qrect))
27568 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27569 Lisp_Object rect = XCDR (hot_spot);
27570 Lisp_Object tem;
27571 if (!CONSP (rect))
27572 return 0;
27573 if (!CONSP (XCAR (rect)))
27574 return 0;
27575 if (!CONSP (XCDR (rect)))
27576 return 0;
27577 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27578 return 0;
27579 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27580 return 0;
27581 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27582 return 0;
27583 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27584 return 0;
27585 return 1;
27587 else if (EQ (XCAR (hot_spot), Qcircle))
27589 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27590 Lisp_Object circ = XCDR (hot_spot);
27591 Lisp_Object lr, lx0, ly0;
27592 if (CONSP (circ)
27593 && CONSP (XCAR (circ))
27594 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27595 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27596 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27598 double r = XFLOATINT (lr);
27599 double dx = XINT (lx0) - x;
27600 double dy = XINT (ly0) - y;
27601 return (dx * dx + dy * dy <= r * r);
27604 else if (EQ (XCAR (hot_spot), Qpoly))
27606 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27607 if (VECTORP (XCDR (hot_spot)))
27609 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27610 Lisp_Object *poly = v->contents;
27611 ptrdiff_t n = v->header.size;
27612 ptrdiff_t i;
27613 int inside = 0;
27614 Lisp_Object lx, ly;
27615 int x0, y0;
27617 /* Need an even number of coordinates, and at least 3 edges. */
27618 if (n < 6 || n & 1)
27619 return 0;
27621 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27622 If count is odd, we are inside polygon. Pixels on edges
27623 may or may not be included depending on actual geometry of the
27624 polygon. */
27625 if ((lx = poly[n-2], !INTEGERP (lx))
27626 || (ly = poly[n-1], !INTEGERP (lx)))
27627 return 0;
27628 x0 = XINT (lx), y0 = XINT (ly);
27629 for (i = 0; i < n; i += 2)
27631 int x1 = x0, y1 = y0;
27632 if ((lx = poly[i], !INTEGERP (lx))
27633 || (ly = poly[i+1], !INTEGERP (ly)))
27634 return 0;
27635 x0 = XINT (lx), y0 = XINT (ly);
27637 /* Does this segment cross the X line? */
27638 if (x0 >= x)
27640 if (x1 >= x)
27641 continue;
27643 else if (x1 < x)
27644 continue;
27645 if (y > y0 && y > y1)
27646 continue;
27647 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27648 inside = !inside;
27650 return inside;
27653 return 0;
27656 Lisp_Object
27657 find_hot_spot (Lisp_Object map, int x, int y)
27659 while (CONSP (map))
27661 if (CONSP (XCAR (map))
27662 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27663 return XCAR (map);
27664 map = XCDR (map);
27667 return Qnil;
27670 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27671 3, 3, 0,
27672 doc: /* Lookup in image map MAP coordinates X and Y.
27673 An image map is an alist where each element has the format (AREA ID PLIST).
27674 An AREA is specified as either a rectangle, a circle, or a polygon:
27675 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27676 pixel coordinates of the upper left and bottom right corners.
27677 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27678 and the radius of the circle; r may be a float or integer.
27679 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27680 vector describes one corner in the polygon.
27681 Returns the alist element for the first matching AREA in MAP. */)
27682 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27684 if (NILP (map))
27685 return Qnil;
27687 CHECK_NUMBER (x);
27688 CHECK_NUMBER (y);
27690 return find_hot_spot (map,
27691 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27692 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27696 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27697 static void
27698 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27700 /* Do not change cursor shape while dragging mouse. */
27701 if (!NILP (do_mouse_tracking))
27702 return;
27704 if (!NILP (pointer))
27706 if (EQ (pointer, Qarrow))
27707 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27708 else if (EQ (pointer, Qhand))
27709 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27710 else if (EQ (pointer, Qtext))
27711 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27712 else if (EQ (pointer, intern ("hdrag")))
27713 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27714 #ifdef HAVE_X_WINDOWS
27715 else if (EQ (pointer, intern ("vdrag")))
27716 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27717 #endif
27718 else if (EQ (pointer, intern ("hourglass")))
27719 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27720 else if (EQ (pointer, Qmodeline))
27721 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27722 else
27723 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27726 if (cursor != No_Cursor)
27727 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27730 #endif /* HAVE_WINDOW_SYSTEM */
27732 /* Take proper action when mouse has moved to the mode or header line
27733 or marginal area AREA of window W, x-position X and y-position Y.
27734 X is relative to the start of the text display area of W, so the
27735 width of bitmap areas and scroll bars must be subtracted to get a
27736 position relative to the start of the mode line. */
27738 static void
27739 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27740 enum window_part area)
27742 struct window *w = XWINDOW (window);
27743 struct frame *f = XFRAME (w->frame);
27744 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27745 #ifdef HAVE_WINDOW_SYSTEM
27746 Display_Info *dpyinfo;
27747 #endif
27748 Cursor cursor = No_Cursor;
27749 Lisp_Object pointer = Qnil;
27750 int dx, dy, width, height;
27751 ptrdiff_t charpos;
27752 Lisp_Object string, object = Qnil;
27753 Lisp_Object pos IF_LINT (= Qnil), help;
27755 Lisp_Object mouse_face;
27756 int original_x_pixel = x;
27757 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27758 struct glyph_row *row IF_LINT (= 0);
27760 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27762 int x0;
27763 struct glyph *end;
27765 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27766 returns them in row/column units! */
27767 string = mode_line_string (w, area, &x, &y, &charpos,
27768 &object, &dx, &dy, &width, &height);
27770 row = (area == ON_MODE_LINE
27771 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27772 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27774 /* Find the glyph under the mouse pointer. */
27775 if (row->mode_line_p && row->enabled_p)
27777 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27778 end = glyph + row->used[TEXT_AREA];
27780 for (x0 = original_x_pixel;
27781 glyph < end && x0 >= glyph->pixel_width;
27782 ++glyph)
27783 x0 -= glyph->pixel_width;
27785 if (glyph >= end)
27786 glyph = NULL;
27789 else
27791 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27792 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27793 returns them in row/column units! */
27794 string = marginal_area_string (w, area, &x, &y, &charpos,
27795 &object, &dx, &dy, &width, &height);
27798 help = Qnil;
27800 #ifdef HAVE_WINDOW_SYSTEM
27801 if (IMAGEP (object))
27803 Lisp_Object image_map, hotspot;
27804 if ((image_map = Fplist_get (XCDR (object), QCmap),
27805 !NILP (image_map))
27806 && (hotspot = find_hot_spot (image_map, dx, dy),
27807 CONSP (hotspot))
27808 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27810 Lisp_Object plist;
27812 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27813 If so, we could look for mouse-enter, mouse-leave
27814 properties in PLIST (and do something...). */
27815 hotspot = XCDR (hotspot);
27816 if (CONSP (hotspot)
27817 && (plist = XCAR (hotspot), CONSP (plist)))
27819 pointer = Fplist_get (plist, Qpointer);
27820 if (NILP (pointer))
27821 pointer = Qhand;
27822 help = Fplist_get (plist, Qhelp_echo);
27823 if (!NILP (help))
27825 help_echo_string = help;
27826 XSETWINDOW (help_echo_window, w);
27827 help_echo_object = w->contents;
27828 help_echo_pos = charpos;
27832 if (NILP (pointer))
27833 pointer = Fplist_get (XCDR (object), QCpointer);
27835 #endif /* HAVE_WINDOW_SYSTEM */
27837 if (STRINGP (string))
27838 pos = make_number (charpos);
27840 /* Set the help text and mouse pointer. If the mouse is on a part
27841 of the mode line without any text (e.g. past the right edge of
27842 the mode line text), use the default help text and pointer. */
27843 if (STRINGP (string) || area == ON_MODE_LINE)
27845 /* Arrange to display the help by setting the global variables
27846 help_echo_string, help_echo_object, and help_echo_pos. */
27847 if (NILP (help))
27849 if (STRINGP (string))
27850 help = Fget_text_property (pos, Qhelp_echo, string);
27852 if (!NILP (help))
27854 help_echo_string = help;
27855 XSETWINDOW (help_echo_window, w);
27856 help_echo_object = string;
27857 help_echo_pos = charpos;
27859 else if (area == ON_MODE_LINE)
27861 Lisp_Object default_help
27862 = buffer_local_value_1 (Qmode_line_default_help_echo,
27863 w->contents);
27865 if (STRINGP (default_help))
27867 help_echo_string = default_help;
27868 XSETWINDOW (help_echo_window, w);
27869 help_echo_object = Qnil;
27870 help_echo_pos = -1;
27875 #ifdef HAVE_WINDOW_SYSTEM
27876 /* Change the mouse pointer according to what is under it. */
27877 if (FRAME_WINDOW_P (f))
27879 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27880 if (STRINGP (string))
27882 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27884 if (NILP (pointer))
27885 pointer = Fget_text_property (pos, Qpointer, string);
27887 /* Change the mouse pointer according to what is under X/Y. */
27888 if (NILP (pointer)
27889 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27891 Lisp_Object map;
27892 map = Fget_text_property (pos, Qlocal_map, string);
27893 if (!KEYMAPP (map))
27894 map = Fget_text_property (pos, Qkeymap, string);
27895 if (!KEYMAPP (map))
27896 cursor = dpyinfo->vertical_scroll_bar_cursor;
27899 else
27900 /* Default mode-line pointer. */
27901 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27903 #endif
27906 /* Change the mouse face according to what is under X/Y. */
27907 if (STRINGP (string))
27909 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27910 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27911 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27912 && glyph)
27914 Lisp_Object b, e;
27916 struct glyph * tmp_glyph;
27918 int gpos;
27919 int gseq_length;
27920 int total_pixel_width;
27921 ptrdiff_t begpos, endpos, ignore;
27923 int vpos, hpos;
27925 b = Fprevious_single_property_change (make_number (charpos + 1),
27926 Qmouse_face, string, Qnil);
27927 if (NILP (b))
27928 begpos = 0;
27929 else
27930 begpos = XINT (b);
27932 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27933 if (NILP (e))
27934 endpos = SCHARS (string);
27935 else
27936 endpos = XINT (e);
27938 /* Calculate the glyph position GPOS of GLYPH in the
27939 displayed string, relative to the beginning of the
27940 highlighted part of the string.
27942 Note: GPOS is different from CHARPOS. CHARPOS is the
27943 position of GLYPH in the internal string object. A mode
27944 line string format has structures which are converted to
27945 a flattened string by the Emacs Lisp interpreter. The
27946 internal string is an element of those structures. The
27947 displayed string is the flattened string. */
27948 tmp_glyph = row_start_glyph;
27949 while (tmp_glyph < glyph
27950 && (!(EQ (tmp_glyph->object, glyph->object)
27951 && begpos <= tmp_glyph->charpos
27952 && tmp_glyph->charpos < endpos)))
27953 tmp_glyph++;
27954 gpos = glyph - tmp_glyph;
27956 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27957 the highlighted part of the displayed string to which
27958 GLYPH belongs. Note: GSEQ_LENGTH is different from
27959 SCHARS (STRING), because the latter returns the length of
27960 the internal string. */
27961 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27962 tmp_glyph > glyph
27963 && (!(EQ (tmp_glyph->object, glyph->object)
27964 && begpos <= tmp_glyph->charpos
27965 && tmp_glyph->charpos < endpos));
27966 tmp_glyph--)
27968 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27970 /* Calculate the total pixel width of all the glyphs between
27971 the beginning of the highlighted area and GLYPH. */
27972 total_pixel_width = 0;
27973 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27974 total_pixel_width += tmp_glyph->pixel_width;
27976 /* Pre calculation of re-rendering position. Note: X is in
27977 column units here, after the call to mode_line_string or
27978 marginal_area_string. */
27979 hpos = x - gpos;
27980 vpos = (area == ON_MODE_LINE
27981 ? (w->current_matrix)->nrows - 1
27982 : 0);
27984 /* If GLYPH's position is included in the region that is
27985 already drawn in mouse face, we have nothing to do. */
27986 if ( EQ (window, hlinfo->mouse_face_window)
27987 && (!row->reversed_p
27988 ? (hlinfo->mouse_face_beg_col <= hpos
27989 && hpos < hlinfo->mouse_face_end_col)
27990 /* In R2L rows we swap BEG and END, see below. */
27991 : (hlinfo->mouse_face_end_col <= hpos
27992 && hpos < hlinfo->mouse_face_beg_col))
27993 && hlinfo->mouse_face_beg_row == vpos )
27994 return;
27996 if (clear_mouse_face (hlinfo))
27997 cursor = No_Cursor;
27999 if (!row->reversed_p)
28001 hlinfo->mouse_face_beg_col = hpos;
28002 hlinfo->mouse_face_beg_x = original_x_pixel
28003 - (total_pixel_width + dx);
28004 hlinfo->mouse_face_end_col = hpos + gseq_length;
28005 hlinfo->mouse_face_end_x = 0;
28007 else
28009 /* In R2L rows, show_mouse_face expects BEG and END
28010 coordinates to be swapped. */
28011 hlinfo->mouse_face_end_col = hpos;
28012 hlinfo->mouse_face_end_x = original_x_pixel
28013 - (total_pixel_width + dx);
28014 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28015 hlinfo->mouse_face_beg_x = 0;
28018 hlinfo->mouse_face_beg_row = vpos;
28019 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28020 hlinfo->mouse_face_beg_y = 0;
28021 hlinfo->mouse_face_end_y = 0;
28022 hlinfo->mouse_face_past_end = 0;
28023 hlinfo->mouse_face_window = window;
28025 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28026 charpos,
28027 0, 0, 0,
28028 &ignore,
28029 glyph->face_id,
28031 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28033 if (NILP (pointer))
28034 pointer = Qhand;
28036 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28037 clear_mouse_face (hlinfo);
28039 #ifdef HAVE_WINDOW_SYSTEM
28040 if (FRAME_WINDOW_P (f))
28041 define_frame_cursor1 (f, cursor, pointer);
28042 #endif
28046 /* EXPORT:
28047 Take proper action when the mouse has moved to position X, Y on
28048 frame F with regards to highlighting portions of display that have
28049 mouse-face properties. Also de-highlight portions of display where
28050 the mouse was before, set the mouse pointer shape as appropriate
28051 for the mouse coordinates, and activate help echo (tooltips).
28052 X and Y can be negative or out of range. */
28054 void
28055 note_mouse_highlight (struct frame *f, int x, int y)
28057 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28058 enum window_part part = ON_NOTHING;
28059 Lisp_Object window;
28060 struct window *w;
28061 Cursor cursor = No_Cursor;
28062 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28063 struct buffer *b;
28065 /* When a menu is active, don't highlight because this looks odd. */
28066 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28067 if (popup_activated ())
28068 return;
28069 #endif
28071 if (!f->glyphs_initialized_p
28072 || f->pointer_invisible)
28073 return;
28075 hlinfo->mouse_face_mouse_x = x;
28076 hlinfo->mouse_face_mouse_y = y;
28077 hlinfo->mouse_face_mouse_frame = f;
28079 if (hlinfo->mouse_face_defer)
28080 return;
28082 /* Which window is that in? */
28083 window = window_from_coordinates (f, x, y, &part, 1);
28085 /* If displaying active text in another window, clear that. */
28086 if (! EQ (window, hlinfo->mouse_face_window)
28087 /* Also clear if we move out of text area in same window. */
28088 || (!NILP (hlinfo->mouse_face_window)
28089 && !NILP (window)
28090 && part != ON_TEXT
28091 && part != ON_MODE_LINE
28092 && part != ON_HEADER_LINE))
28093 clear_mouse_face (hlinfo);
28095 /* Not on a window -> return. */
28096 if (!WINDOWP (window))
28097 return;
28099 /* Reset help_echo_string. It will get recomputed below. */
28100 help_echo_string = Qnil;
28102 /* Convert to window-relative pixel coordinates. */
28103 w = XWINDOW (window);
28104 frame_to_window_pixel_xy (w, &x, &y);
28106 #ifdef HAVE_WINDOW_SYSTEM
28107 /* Handle tool-bar window differently since it doesn't display a
28108 buffer. */
28109 if (EQ (window, f->tool_bar_window))
28111 note_tool_bar_highlight (f, x, y);
28112 return;
28114 #endif
28116 /* Mouse is on the mode, header line or margin? */
28117 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28118 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28120 note_mode_line_or_margin_highlight (window, x, y, part);
28121 return;
28124 #ifdef HAVE_WINDOW_SYSTEM
28125 if (part == ON_VERTICAL_BORDER)
28127 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28128 help_echo_string = build_string ("drag-mouse-1: resize");
28130 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28131 || part == ON_SCROLL_BAR)
28132 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28133 else
28134 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28135 #endif
28137 /* Are we in a window whose display is up to date?
28138 And verify the buffer's text has not changed. */
28139 b = XBUFFER (w->contents);
28140 if (part == ON_TEXT
28141 && w->window_end_valid
28142 && w->last_modified == BUF_MODIFF (b)
28143 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
28145 int hpos, vpos, dx, dy, area = LAST_AREA;
28146 ptrdiff_t pos;
28147 struct glyph *glyph;
28148 Lisp_Object object;
28149 Lisp_Object mouse_face = Qnil, position;
28150 Lisp_Object *overlay_vec = NULL;
28151 ptrdiff_t i, noverlays;
28152 struct buffer *obuf;
28153 ptrdiff_t obegv, ozv;
28154 int same_region;
28156 /* Find the glyph under X/Y. */
28157 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28159 #ifdef HAVE_WINDOW_SYSTEM
28160 /* Look for :pointer property on image. */
28161 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28163 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28164 if (img != NULL && IMAGEP (img->spec))
28166 Lisp_Object image_map, hotspot;
28167 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28168 !NILP (image_map))
28169 && (hotspot = find_hot_spot (image_map,
28170 glyph->slice.img.x + dx,
28171 glyph->slice.img.y + dy),
28172 CONSP (hotspot))
28173 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28175 Lisp_Object plist;
28177 /* Could check XCAR (hotspot) to see if we enter/leave
28178 this hot-spot.
28179 If so, we could look for mouse-enter, mouse-leave
28180 properties in PLIST (and do something...). */
28181 hotspot = XCDR (hotspot);
28182 if (CONSP (hotspot)
28183 && (plist = XCAR (hotspot), CONSP (plist)))
28185 pointer = Fplist_get (plist, Qpointer);
28186 if (NILP (pointer))
28187 pointer = Qhand;
28188 help_echo_string = Fplist_get (plist, Qhelp_echo);
28189 if (!NILP (help_echo_string))
28191 help_echo_window = window;
28192 help_echo_object = glyph->object;
28193 help_echo_pos = glyph->charpos;
28197 if (NILP (pointer))
28198 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28201 #endif /* HAVE_WINDOW_SYSTEM */
28203 /* Clear mouse face if X/Y not over text. */
28204 if (glyph == NULL
28205 || area != TEXT_AREA
28206 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28207 /* Glyph's OBJECT is an integer for glyphs inserted by the
28208 display engine for its internal purposes, like truncation
28209 and continuation glyphs and blanks beyond the end of
28210 line's text on text terminals. If we are over such a
28211 glyph, we are not over any text. */
28212 || INTEGERP (glyph->object)
28213 /* R2L rows have a stretch glyph at their front, which
28214 stands for no text, whereas L2R rows have no glyphs at
28215 all beyond the end of text. Treat such stretch glyphs
28216 like we do with NULL glyphs in L2R rows. */
28217 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28218 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28219 && glyph->type == STRETCH_GLYPH
28220 && glyph->avoid_cursor_p))
28222 if (clear_mouse_face (hlinfo))
28223 cursor = No_Cursor;
28224 #ifdef HAVE_WINDOW_SYSTEM
28225 if (FRAME_WINDOW_P (f) && NILP (pointer))
28227 if (area != TEXT_AREA)
28228 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28229 else
28230 pointer = Vvoid_text_area_pointer;
28232 #endif
28233 goto set_cursor;
28236 pos = glyph->charpos;
28237 object = glyph->object;
28238 if (!STRINGP (object) && !BUFFERP (object))
28239 goto set_cursor;
28241 /* If we get an out-of-range value, return now; avoid an error. */
28242 if (BUFFERP (object) && pos > BUF_Z (b))
28243 goto set_cursor;
28245 /* Make the window's buffer temporarily current for
28246 overlays_at and compute_char_face. */
28247 obuf = current_buffer;
28248 current_buffer = b;
28249 obegv = BEGV;
28250 ozv = ZV;
28251 BEGV = BEG;
28252 ZV = Z;
28254 /* Is this char mouse-active or does it have help-echo? */
28255 position = make_number (pos);
28257 if (BUFFERP (object))
28259 /* Put all the overlays we want in a vector in overlay_vec. */
28260 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28261 /* Sort overlays into increasing priority order. */
28262 noverlays = sort_overlays (overlay_vec, noverlays, w);
28264 else
28265 noverlays = 0;
28267 if (NILP (Vmouse_highlight))
28269 clear_mouse_face (hlinfo);
28270 goto check_help_echo;
28273 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28275 if (same_region)
28276 cursor = No_Cursor;
28278 /* Check mouse-face highlighting. */
28279 if (! same_region
28280 /* If there exists an overlay with mouse-face overlapping
28281 the one we are currently highlighting, we have to
28282 check if we enter the overlapping overlay, and then
28283 highlight only that. */
28284 || (OVERLAYP (hlinfo->mouse_face_overlay)
28285 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28287 /* Find the highest priority overlay with a mouse-face. */
28288 Lisp_Object overlay = Qnil;
28289 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28291 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28292 if (!NILP (mouse_face))
28293 overlay = overlay_vec[i];
28296 /* If we're highlighting the same overlay as before, there's
28297 no need to do that again. */
28298 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28299 goto check_help_echo;
28300 hlinfo->mouse_face_overlay = overlay;
28302 /* Clear the display of the old active region, if any. */
28303 if (clear_mouse_face (hlinfo))
28304 cursor = No_Cursor;
28306 /* If no overlay applies, get a text property. */
28307 if (NILP (overlay))
28308 mouse_face = Fget_text_property (position, Qmouse_face, object);
28310 /* Next, compute the bounds of the mouse highlighting and
28311 display it. */
28312 if (!NILP (mouse_face) && STRINGP (object))
28314 /* The mouse-highlighting comes from a display string
28315 with a mouse-face. */
28316 Lisp_Object s, e;
28317 ptrdiff_t ignore;
28319 s = Fprevious_single_property_change
28320 (make_number (pos + 1), Qmouse_face, object, Qnil);
28321 e = Fnext_single_property_change
28322 (position, Qmouse_face, object, Qnil);
28323 if (NILP (s))
28324 s = make_number (0);
28325 if (NILP (e))
28326 e = make_number (SCHARS (object) - 1);
28327 mouse_face_from_string_pos (w, hlinfo, object,
28328 XINT (s), XINT (e));
28329 hlinfo->mouse_face_past_end = 0;
28330 hlinfo->mouse_face_window = window;
28331 hlinfo->mouse_face_face_id
28332 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28333 glyph->face_id, 1);
28334 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28335 cursor = No_Cursor;
28337 else
28339 /* The mouse-highlighting, if any, comes from an overlay
28340 or text property in the buffer. */
28341 Lisp_Object buffer IF_LINT (= Qnil);
28342 Lisp_Object disp_string IF_LINT (= Qnil);
28344 if (STRINGP (object))
28346 /* If we are on a display string with no mouse-face,
28347 check if the text under it has one. */
28348 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28349 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28350 pos = string_buffer_position (object, start);
28351 if (pos > 0)
28353 mouse_face = get_char_property_and_overlay
28354 (make_number (pos), Qmouse_face, w->contents, &overlay);
28355 buffer = w->contents;
28356 disp_string = object;
28359 else
28361 buffer = object;
28362 disp_string = Qnil;
28365 if (!NILP (mouse_face))
28367 Lisp_Object before, after;
28368 Lisp_Object before_string, after_string;
28369 /* To correctly find the limits of mouse highlight
28370 in a bidi-reordered buffer, we must not use the
28371 optimization of limiting the search in
28372 previous-single-property-change and
28373 next-single-property-change, because
28374 rows_from_pos_range needs the real start and end
28375 positions to DTRT in this case. That's because
28376 the first row visible in a window does not
28377 necessarily display the character whose position
28378 is the smallest. */
28379 Lisp_Object lim1 =
28380 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28381 ? Fmarker_position (w->start)
28382 : Qnil;
28383 Lisp_Object lim2 =
28384 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28385 ? make_number (BUF_Z (XBUFFER (buffer))
28386 - XFASTINT (w->window_end_pos))
28387 : Qnil;
28389 if (NILP (overlay))
28391 /* Handle the text property case. */
28392 before = Fprevious_single_property_change
28393 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28394 after = Fnext_single_property_change
28395 (make_number (pos), Qmouse_face, buffer, lim2);
28396 before_string = after_string = Qnil;
28398 else
28400 /* Handle the overlay case. */
28401 before = Foverlay_start (overlay);
28402 after = Foverlay_end (overlay);
28403 before_string = Foverlay_get (overlay, Qbefore_string);
28404 after_string = Foverlay_get (overlay, Qafter_string);
28406 if (!STRINGP (before_string)) before_string = Qnil;
28407 if (!STRINGP (after_string)) after_string = Qnil;
28410 mouse_face_from_buffer_pos (window, hlinfo, pos,
28411 NILP (before)
28413 : XFASTINT (before),
28414 NILP (after)
28415 ? BUF_Z (XBUFFER (buffer))
28416 : XFASTINT (after),
28417 before_string, after_string,
28418 disp_string);
28419 cursor = No_Cursor;
28424 check_help_echo:
28426 /* Look for a `help-echo' property. */
28427 if (NILP (help_echo_string)) {
28428 Lisp_Object help, overlay;
28430 /* Check overlays first. */
28431 help = overlay = Qnil;
28432 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28434 overlay = overlay_vec[i];
28435 help = Foverlay_get (overlay, Qhelp_echo);
28438 if (!NILP (help))
28440 help_echo_string = help;
28441 help_echo_window = window;
28442 help_echo_object = overlay;
28443 help_echo_pos = pos;
28445 else
28447 Lisp_Object obj = glyph->object;
28448 ptrdiff_t charpos = glyph->charpos;
28450 /* Try text properties. */
28451 if (STRINGP (obj)
28452 && charpos >= 0
28453 && charpos < SCHARS (obj))
28455 help = Fget_text_property (make_number (charpos),
28456 Qhelp_echo, obj);
28457 if (NILP (help))
28459 /* If the string itself doesn't specify a help-echo,
28460 see if the buffer text ``under'' it does. */
28461 struct glyph_row *r
28462 = MATRIX_ROW (w->current_matrix, vpos);
28463 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28464 ptrdiff_t p = string_buffer_position (obj, start);
28465 if (p > 0)
28467 help = Fget_char_property (make_number (p),
28468 Qhelp_echo, w->contents);
28469 if (!NILP (help))
28471 charpos = p;
28472 obj = w->contents;
28477 else if (BUFFERP (obj)
28478 && charpos >= BEGV
28479 && charpos < ZV)
28480 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28481 obj);
28483 if (!NILP (help))
28485 help_echo_string = help;
28486 help_echo_window = window;
28487 help_echo_object = obj;
28488 help_echo_pos = charpos;
28493 #ifdef HAVE_WINDOW_SYSTEM
28494 /* Look for a `pointer' property. */
28495 if (FRAME_WINDOW_P (f) && NILP (pointer))
28497 /* Check overlays first. */
28498 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28499 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28501 if (NILP (pointer))
28503 Lisp_Object obj = glyph->object;
28504 ptrdiff_t charpos = glyph->charpos;
28506 /* Try text properties. */
28507 if (STRINGP (obj)
28508 && charpos >= 0
28509 && charpos < SCHARS (obj))
28511 pointer = Fget_text_property (make_number (charpos),
28512 Qpointer, obj);
28513 if (NILP (pointer))
28515 /* If the string itself doesn't specify a pointer,
28516 see if the buffer text ``under'' it does. */
28517 struct glyph_row *r
28518 = MATRIX_ROW (w->current_matrix, vpos);
28519 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28520 ptrdiff_t p = string_buffer_position (obj, start);
28521 if (p > 0)
28522 pointer = Fget_char_property (make_number (p),
28523 Qpointer, w->contents);
28526 else if (BUFFERP (obj)
28527 && charpos >= BEGV
28528 && charpos < ZV)
28529 pointer = Fget_text_property (make_number (charpos),
28530 Qpointer, obj);
28533 #endif /* HAVE_WINDOW_SYSTEM */
28535 BEGV = obegv;
28536 ZV = ozv;
28537 current_buffer = obuf;
28540 set_cursor:
28542 #ifdef HAVE_WINDOW_SYSTEM
28543 if (FRAME_WINDOW_P (f))
28544 define_frame_cursor1 (f, cursor, pointer);
28545 #else
28546 /* This is here to prevent a compiler error, about "label at end of
28547 compound statement". */
28548 return;
28549 #endif
28553 /* EXPORT for RIF:
28554 Clear any mouse-face on window W. This function is part of the
28555 redisplay interface, and is called from try_window_id and similar
28556 functions to ensure the mouse-highlight is off. */
28558 void
28559 x_clear_window_mouse_face (struct window *w)
28561 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28562 Lisp_Object window;
28564 block_input ();
28565 XSETWINDOW (window, w);
28566 if (EQ (window, hlinfo->mouse_face_window))
28567 clear_mouse_face (hlinfo);
28568 unblock_input ();
28572 /* EXPORT:
28573 Just discard the mouse face information for frame F, if any.
28574 This is used when the size of F is changed. */
28576 void
28577 cancel_mouse_face (struct frame *f)
28579 Lisp_Object window;
28580 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28582 window = hlinfo->mouse_face_window;
28583 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28585 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28586 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28587 hlinfo->mouse_face_window = Qnil;
28593 /***********************************************************************
28594 Exposure Events
28595 ***********************************************************************/
28597 #ifdef HAVE_WINDOW_SYSTEM
28599 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28600 which intersects rectangle R. R is in window-relative coordinates. */
28602 static void
28603 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28604 enum glyph_row_area area)
28606 struct glyph *first = row->glyphs[area];
28607 struct glyph *end = row->glyphs[area] + row->used[area];
28608 struct glyph *last;
28609 int first_x, start_x, x;
28611 if (area == TEXT_AREA && row->fill_line_p)
28612 /* If row extends face to end of line write the whole line. */
28613 draw_glyphs (w, 0, row, area,
28614 0, row->used[area],
28615 DRAW_NORMAL_TEXT, 0);
28616 else
28618 /* Set START_X to the window-relative start position for drawing glyphs of
28619 AREA. The first glyph of the text area can be partially visible.
28620 The first glyphs of other areas cannot. */
28621 start_x = window_box_left_offset (w, area);
28622 x = start_x;
28623 if (area == TEXT_AREA)
28624 x += row->x;
28626 /* Find the first glyph that must be redrawn. */
28627 while (first < end
28628 && x + first->pixel_width < r->x)
28630 x += first->pixel_width;
28631 ++first;
28634 /* Find the last one. */
28635 last = first;
28636 first_x = x;
28637 while (last < end
28638 && x < r->x + r->width)
28640 x += last->pixel_width;
28641 ++last;
28644 /* Repaint. */
28645 if (last > first)
28646 draw_glyphs (w, first_x - start_x, row, area,
28647 first - row->glyphs[area], last - row->glyphs[area],
28648 DRAW_NORMAL_TEXT, 0);
28653 /* Redraw the parts of the glyph row ROW on window W intersecting
28654 rectangle R. R is in window-relative coordinates. Value is
28655 non-zero if mouse-face was overwritten. */
28657 static int
28658 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28660 eassert (row->enabled_p);
28662 if (row->mode_line_p || w->pseudo_window_p)
28663 draw_glyphs (w, 0, row, TEXT_AREA,
28664 0, row->used[TEXT_AREA],
28665 DRAW_NORMAL_TEXT, 0);
28666 else
28668 if (row->used[LEFT_MARGIN_AREA])
28669 expose_area (w, row, r, LEFT_MARGIN_AREA);
28670 if (row->used[TEXT_AREA])
28671 expose_area (w, row, r, TEXT_AREA);
28672 if (row->used[RIGHT_MARGIN_AREA])
28673 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28674 draw_row_fringe_bitmaps (w, row);
28677 return row->mouse_face_p;
28681 /* Redraw those parts of glyphs rows during expose event handling that
28682 overlap other rows. Redrawing of an exposed line writes over parts
28683 of lines overlapping that exposed line; this function fixes that.
28685 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28686 row in W's current matrix that is exposed and overlaps other rows.
28687 LAST_OVERLAPPING_ROW is the last such row. */
28689 static void
28690 expose_overlaps (struct window *w,
28691 struct glyph_row *first_overlapping_row,
28692 struct glyph_row *last_overlapping_row,
28693 XRectangle *r)
28695 struct glyph_row *row;
28697 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28698 if (row->overlapping_p)
28700 eassert (row->enabled_p && !row->mode_line_p);
28702 row->clip = r;
28703 if (row->used[LEFT_MARGIN_AREA])
28704 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28706 if (row->used[TEXT_AREA])
28707 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28709 if (row->used[RIGHT_MARGIN_AREA])
28710 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28711 row->clip = NULL;
28716 /* Return non-zero if W's cursor intersects rectangle R. */
28718 static int
28719 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28721 XRectangle cr, result;
28722 struct glyph *cursor_glyph;
28723 struct glyph_row *row;
28725 if (w->phys_cursor.vpos >= 0
28726 && w->phys_cursor.vpos < w->current_matrix->nrows
28727 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28728 row->enabled_p)
28729 && row->cursor_in_fringe_p)
28731 /* Cursor is in the fringe. */
28732 cr.x = window_box_right_offset (w,
28733 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28734 ? RIGHT_MARGIN_AREA
28735 : TEXT_AREA));
28736 cr.y = row->y;
28737 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28738 cr.height = row->height;
28739 return x_intersect_rectangles (&cr, r, &result);
28742 cursor_glyph = get_phys_cursor_glyph (w);
28743 if (cursor_glyph)
28745 /* r is relative to W's box, but w->phys_cursor.x is relative
28746 to left edge of W's TEXT area. Adjust it. */
28747 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28748 cr.y = w->phys_cursor.y;
28749 cr.width = cursor_glyph->pixel_width;
28750 cr.height = w->phys_cursor_height;
28751 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28752 I assume the effect is the same -- and this is portable. */
28753 return x_intersect_rectangles (&cr, r, &result);
28755 /* If we don't understand the format, pretend we're not in the hot-spot. */
28756 return 0;
28760 /* EXPORT:
28761 Draw a vertical window border to the right of window W if W doesn't
28762 have vertical scroll bars. */
28764 void
28765 x_draw_vertical_border (struct window *w)
28767 struct frame *f = XFRAME (WINDOW_FRAME (w));
28769 /* We could do better, if we knew what type of scroll-bar the adjacent
28770 windows (on either side) have... But we don't :-(
28771 However, I think this works ok. ++KFS 2003-04-25 */
28773 /* Redraw borders between horizontally adjacent windows. Don't
28774 do it for frames with vertical scroll bars because either the
28775 right scroll bar of a window, or the left scroll bar of its
28776 neighbor will suffice as a border. */
28777 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28778 return;
28780 /* Note: It is necessary to redraw both the left and the right
28781 borders, for when only this single window W is being
28782 redisplayed. */
28783 if (!WINDOW_RIGHTMOST_P (w)
28784 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28786 int x0, x1, y0, y1;
28788 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28789 y1 -= 1;
28791 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28792 x1 -= 1;
28794 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28796 if (!WINDOW_LEFTMOST_P (w)
28797 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28799 int x0, x1, y0, y1;
28801 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28802 y1 -= 1;
28804 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28805 x0 -= 1;
28807 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28812 /* Redraw the part of window W intersection rectangle FR. Pixel
28813 coordinates in FR are frame-relative. Call this function with
28814 input blocked. Value is non-zero if the exposure overwrites
28815 mouse-face. */
28817 static int
28818 expose_window (struct window *w, XRectangle *fr)
28820 struct frame *f = XFRAME (w->frame);
28821 XRectangle wr, r;
28822 int mouse_face_overwritten_p = 0;
28824 /* If window is not yet fully initialized, do nothing. This can
28825 happen when toolkit scroll bars are used and a window is split.
28826 Reconfiguring the scroll bar will generate an expose for a newly
28827 created window. */
28828 if (w->current_matrix == NULL)
28829 return 0;
28831 /* When we're currently updating the window, display and current
28832 matrix usually don't agree. Arrange for a thorough display
28833 later. */
28834 if (w == updated_window)
28836 SET_FRAME_GARBAGED (f);
28837 return 0;
28840 /* Frame-relative pixel rectangle of W. */
28841 wr.x = WINDOW_LEFT_EDGE_X (w);
28842 wr.y = WINDOW_TOP_EDGE_Y (w);
28843 wr.width = WINDOW_TOTAL_WIDTH (w);
28844 wr.height = WINDOW_TOTAL_HEIGHT (w);
28846 if (x_intersect_rectangles (fr, &wr, &r))
28848 int yb = window_text_bottom_y (w);
28849 struct glyph_row *row;
28850 int cursor_cleared_p, phys_cursor_on_p;
28851 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28853 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28854 r.x, r.y, r.width, r.height));
28856 /* Convert to window coordinates. */
28857 r.x -= WINDOW_LEFT_EDGE_X (w);
28858 r.y -= WINDOW_TOP_EDGE_Y (w);
28860 /* Turn off the cursor. */
28861 if (!w->pseudo_window_p
28862 && phys_cursor_in_rect_p (w, &r))
28864 x_clear_cursor (w);
28865 cursor_cleared_p = 1;
28867 else
28868 cursor_cleared_p = 0;
28870 /* If the row containing the cursor extends face to end of line,
28871 then expose_area might overwrite the cursor outside the
28872 rectangle and thus notice_overwritten_cursor might clear
28873 w->phys_cursor_on_p. We remember the original value and
28874 check later if it is changed. */
28875 phys_cursor_on_p = w->phys_cursor_on_p;
28877 /* Update lines intersecting rectangle R. */
28878 first_overlapping_row = last_overlapping_row = NULL;
28879 for (row = w->current_matrix->rows;
28880 row->enabled_p;
28881 ++row)
28883 int y0 = row->y;
28884 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28886 if ((y0 >= r.y && y0 < r.y + r.height)
28887 || (y1 > r.y && y1 < r.y + r.height)
28888 || (r.y >= y0 && r.y < y1)
28889 || (r.y + r.height > y0 && r.y + r.height < y1))
28891 /* A header line may be overlapping, but there is no need
28892 to fix overlapping areas for them. KFS 2005-02-12 */
28893 if (row->overlapping_p && !row->mode_line_p)
28895 if (first_overlapping_row == NULL)
28896 first_overlapping_row = row;
28897 last_overlapping_row = row;
28900 row->clip = fr;
28901 if (expose_line (w, row, &r))
28902 mouse_face_overwritten_p = 1;
28903 row->clip = NULL;
28905 else if (row->overlapping_p)
28907 /* We must redraw a row overlapping the exposed area. */
28908 if (y0 < r.y
28909 ? y0 + row->phys_height > r.y
28910 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28912 if (first_overlapping_row == NULL)
28913 first_overlapping_row = row;
28914 last_overlapping_row = row;
28918 if (y1 >= yb)
28919 break;
28922 /* Display the mode line if there is one. */
28923 if (WINDOW_WANTS_MODELINE_P (w)
28924 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28925 row->enabled_p)
28926 && row->y < r.y + r.height)
28928 if (expose_line (w, row, &r))
28929 mouse_face_overwritten_p = 1;
28932 if (!w->pseudo_window_p)
28934 /* Fix the display of overlapping rows. */
28935 if (first_overlapping_row)
28936 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28937 fr);
28939 /* Draw border between windows. */
28940 x_draw_vertical_border (w);
28942 /* Turn the cursor on again. */
28943 if (cursor_cleared_p
28944 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28945 update_window_cursor (w, 1);
28949 return mouse_face_overwritten_p;
28954 /* Redraw (parts) of all windows in the window tree rooted at W that
28955 intersect R. R contains frame pixel coordinates. Value is
28956 non-zero if the exposure overwrites mouse-face. */
28958 static int
28959 expose_window_tree (struct window *w, XRectangle *r)
28961 struct frame *f = XFRAME (w->frame);
28962 int mouse_face_overwritten_p = 0;
28964 while (w && !FRAME_GARBAGED_P (f))
28966 if (WINDOWP (w->contents))
28967 mouse_face_overwritten_p
28968 |= expose_window_tree (XWINDOW (w->contents), r);
28969 else
28970 mouse_face_overwritten_p |= expose_window (w, r);
28972 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28975 return mouse_face_overwritten_p;
28979 /* EXPORT:
28980 Redisplay an exposed area of frame F. X and Y are the upper-left
28981 corner of the exposed rectangle. W and H are width and height of
28982 the exposed area. All are pixel values. W or H zero means redraw
28983 the entire frame. */
28985 void
28986 expose_frame (struct frame *f, int x, int y, int w, int h)
28988 XRectangle r;
28989 int mouse_face_overwritten_p = 0;
28991 TRACE ((stderr, "expose_frame "));
28993 /* No need to redraw if frame will be redrawn soon. */
28994 if (FRAME_GARBAGED_P (f))
28996 TRACE ((stderr, " garbaged\n"));
28997 return;
29000 /* If basic faces haven't been realized yet, there is no point in
29001 trying to redraw anything. This can happen when we get an expose
29002 event while Emacs is starting, e.g. by moving another window. */
29003 if (FRAME_FACE_CACHE (f) == NULL
29004 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29006 TRACE ((stderr, " no faces\n"));
29007 return;
29010 if (w == 0 || h == 0)
29012 r.x = r.y = 0;
29013 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29014 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29016 else
29018 r.x = x;
29019 r.y = y;
29020 r.width = w;
29021 r.height = h;
29024 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29025 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29027 if (WINDOWP (f->tool_bar_window))
29028 mouse_face_overwritten_p
29029 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29031 #ifdef HAVE_X_WINDOWS
29032 #ifndef MSDOS
29033 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29034 if (WINDOWP (f->menu_bar_window))
29035 mouse_face_overwritten_p
29036 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29037 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29038 #endif
29039 #endif
29041 /* Some window managers support a focus-follows-mouse style with
29042 delayed raising of frames. Imagine a partially obscured frame,
29043 and moving the mouse into partially obscured mouse-face on that
29044 frame. The visible part of the mouse-face will be highlighted,
29045 then the WM raises the obscured frame. With at least one WM, KDE
29046 2.1, Emacs is not getting any event for the raising of the frame
29047 (even tried with SubstructureRedirectMask), only Expose events.
29048 These expose events will draw text normally, i.e. not
29049 highlighted. Which means we must redo the highlight here.
29050 Subsume it under ``we love X''. --gerd 2001-08-15 */
29051 /* Included in Windows version because Windows most likely does not
29052 do the right thing if any third party tool offers
29053 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29054 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29056 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29057 if (f == hlinfo->mouse_face_mouse_frame)
29059 int mouse_x = hlinfo->mouse_face_mouse_x;
29060 int mouse_y = hlinfo->mouse_face_mouse_y;
29061 clear_mouse_face (hlinfo);
29062 note_mouse_highlight (f, mouse_x, mouse_y);
29068 /* EXPORT:
29069 Determine the intersection of two rectangles R1 and R2. Return
29070 the intersection in *RESULT. Value is non-zero if RESULT is not
29071 empty. */
29074 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29076 XRectangle *left, *right;
29077 XRectangle *upper, *lower;
29078 int intersection_p = 0;
29080 /* Rearrange so that R1 is the left-most rectangle. */
29081 if (r1->x < r2->x)
29082 left = r1, right = r2;
29083 else
29084 left = r2, right = r1;
29086 /* X0 of the intersection is right.x0, if this is inside R1,
29087 otherwise there is no intersection. */
29088 if (right->x <= left->x + left->width)
29090 result->x = right->x;
29092 /* The right end of the intersection is the minimum of
29093 the right ends of left and right. */
29094 result->width = (min (left->x + left->width, right->x + right->width)
29095 - result->x);
29097 /* Same game for Y. */
29098 if (r1->y < r2->y)
29099 upper = r1, lower = r2;
29100 else
29101 upper = r2, lower = r1;
29103 /* The upper end of the intersection is lower.y0, if this is inside
29104 of upper. Otherwise, there is no intersection. */
29105 if (lower->y <= upper->y + upper->height)
29107 result->y = lower->y;
29109 /* The lower end of the intersection is the minimum of the lower
29110 ends of upper and lower. */
29111 result->height = (min (lower->y + lower->height,
29112 upper->y + upper->height)
29113 - result->y);
29114 intersection_p = 1;
29118 return intersection_p;
29121 #endif /* HAVE_WINDOW_SYSTEM */
29124 /***********************************************************************
29125 Initialization
29126 ***********************************************************************/
29128 void
29129 syms_of_xdisp (void)
29131 Vwith_echo_area_save_vector = Qnil;
29132 staticpro (&Vwith_echo_area_save_vector);
29134 Vmessage_stack = Qnil;
29135 staticpro (&Vmessage_stack);
29137 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29138 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29140 message_dolog_marker1 = Fmake_marker ();
29141 staticpro (&message_dolog_marker1);
29142 message_dolog_marker2 = Fmake_marker ();
29143 staticpro (&message_dolog_marker2);
29144 message_dolog_marker3 = Fmake_marker ();
29145 staticpro (&message_dolog_marker3);
29147 #ifdef GLYPH_DEBUG
29148 defsubr (&Sdump_frame_glyph_matrix);
29149 defsubr (&Sdump_glyph_matrix);
29150 defsubr (&Sdump_glyph_row);
29151 defsubr (&Sdump_tool_bar_row);
29152 defsubr (&Strace_redisplay);
29153 defsubr (&Strace_to_stderr);
29154 #endif
29155 #ifdef HAVE_WINDOW_SYSTEM
29156 defsubr (&Stool_bar_lines_needed);
29157 defsubr (&Slookup_image_map);
29158 #endif
29159 defsubr (&Sline_pixel_height);
29160 defsubr (&Sformat_mode_line);
29161 defsubr (&Sinvisible_p);
29162 defsubr (&Scurrent_bidi_paragraph_direction);
29163 defsubr (&Smove_point_visually);
29165 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29166 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29167 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29168 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29169 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29170 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29171 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29172 DEFSYM (Qeval, "eval");
29173 DEFSYM (QCdata, ":data");
29174 DEFSYM (Qdisplay, "display");
29175 DEFSYM (Qspace_width, "space-width");
29176 DEFSYM (Qraise, "raise");
29177 DEFSYM (Qslice, "slice");
29178 DEFSYM (Qspace, "space");
29179 DEFSYM (Qmargin, "margin");
29180 DEFSYM (Qpointer, "pointer");
29181 DEFSYM (Qleft_margin, "left-margin");
29182 DEFSYM (Qright_margin, "right-margin");
29183 DEFSYM (Qcenter, "center");
29184 DEFSYM (Qline_height, "line-height");
29185 DEFSYM (QCalign_to, ":align-to");
29186 DEFSYM (QCrelative_width, ":relative-width");
29187 DEFSYM (QCrelative_height, ":relative-height");
29188 DEFSYM (QCeval, ":eval");
29189 DEFSYM (QCpropertize, ":propertize");
29190 DEFSYM (QCfile, ":file");
29191 DEFSYM (Qfontified, "fontified");
29192 DEFSYM (Qfontification_functions, "fontification-functions");
29193 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29194 DEFSYM (Qescape_glyph, "escape-glyph");
29195 DEFSYM (Qnobreak_space, "nobreak-space");
29196 DEFSYM (Qimage, "image");
29197 DEFSYM (Qtext, "text");
29198 DEFSYM (Qboth, "both");
29199 DEFSYM (Qboth_horiz, "both-horiz");
29200 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29201 DEFSYM (QCmap, ":map");
29202 DEFSYM (QCpointer, ":pointer");
29203 DEFSYM (Qrect, "rect");
29204 DEFSYM (Qcircle, "circle");
29205 DEFSYM (Qpoly, "poly");
29206 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29207 DEFSYM (Qgrow_only, "grow-only");
29208 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29209 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29210 DEFSYM (Qposition, "position");
29211 DEFSYM (Qbuffer_position, "buffer-position");
29212 DEFSYM (Qobject, "object");
29213 DEFSYM (Qbar, "bar");
29214 DEFSYM (Qhbar, "hbar");
29215 DEFSYM (Qbox, "box");
29216 DEFSYM (Qhollow, "hollow");
29217 DEFSYM (Qhand, "hand");
29218 DEFSYM (Qarrow, "arrow");
29219 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29221 list_of_error = list1 (list2 (intern_c_string ("error"),
29222 intern_c_string ("void-variable")));
29223 staticpro (&list_of_error);
29225 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29226 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29227 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29228 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29230 echo_buffer[0] = echo_buffer[1] = Qnil;
29231 staticpro (&echo_buffer[0]);
29232 staticpro (&echo_buffer[1]);
29234 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29235 staticpro (&echo_area_buffer[0]);
29236 staticpro (&echo_area_buffer[1]);
29238 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29239 staticpro (&Vmessages_buffer_name);
29241 mode_line_proptrans_alist = Qnil;
29242 staticpro (&mode_line_proptrans_alist);
29243 mode_line_string_list = Qnil;
29244 staticpro (&mode_line_string_list);
29245 mode_line_string_face = Qnil;
29246 staticpro (&mode_line_string_face);
29247 mode_line_string_face_prop = Qnil;
29248 staticpro (&mode_line_string_face_prop);
29249 Vmode_line_unwind_vector = Qnil;
29250 staticpro (&Vmode_line_unwind_vector);
29252 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29254 help_echo_string = Qnil;
29255 staticpro (&help_echo_string);
29256 help_echo_object = Qnil;
29257 staticpro (&help_echo_object);
29258 help_echo_window = Qnil;
29259 staticpro (&help_echo_window);
29260 previous_help_echo_string = Qnil;
29261 staticpro (&previous_help_echo_string);
29262 help_echo_pos = -1;
29264 DEFSYM (Qright_to_left, "right-to-left");
29265 DEFSYM (Qleft_to_right, "left-to-right");
29267 #ifdef HAVE_WINDOW_SYSTEM
29268 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29269 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29270 For example, if a block cursor is over a tab, it will be drawn as
29271 wide as that tab on the display. */);
29272 x_stretch_cursor_p = 0;
29273 #endif
29275 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29276 doc: /* Non-nil means highlight trailing whitespace.
29277 The face used for trailing whitespace is `trailing-whitespace'. */);
29278 Vshow_trailing_whitespace = Qnil;
29280 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29281 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29282 If the value is t, Emacs highlights non-ASCII chars which have the
29283 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29284 or `escape-glyph' face respectively.
29286 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29287 U+2011 (non-breaking hyphen) are affected.
29289 Any other non-nil value means to display these characters as a escape
29290 glyph followed by an ordinary space or hyphen.
29292 A value of nil means no special handling of these characters. */);
29293 Vnobreak_char_display = Qt;
29295 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29296 doc: /* The pointer shape to show in void text areas.
29297 A value of nil means to show the text pointer. Other options are `arrow',
29298 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29299 Vvoid_text_area_pointer = Qarrow;
29301 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29302 doc: /* Non-nil means don't actually do any redisplay.
29303 This is used for internal purposes. */);
29304 Vinhibit_redisplay = Qnil;
29306 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29307 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29308 Vglobal_mode_string = Qnil;
29310 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29311 doc: /* Marker for where to display an arrow on top of the buffer text.
29312 This must be the beginning of a line in order to work.
29313 See also `overlay-arrow-string'. */);
29314 Voverlay_arrow_position = Qnil;
29316 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29317 doc: /* String to display as an arrow in non-window frames.
29318 See also `overlay-arrow-position'. */);
29319 Voverlay_arrow_string = build_pure_c_string ("=>");
29321 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29322 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29323 The symbols on this list are examined during redisplay to determine
29324 where to display overlay arrows. */);
29325 Voverlay_arrow_variable_list
29326 = list1 (intern_c_string ("overlay-arrow-position"));
29328 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29329 doc: /* The number of lines to try scrolling a window by when point moves out.
29330 If that fails to bring point back on frame, point is centered instead.
29331 If this is zero, point is always centered after it moves off frame.
29332 If you want scrolling to always be a line at a time, you should set
29333 `scroll-conservatively' to a large value rather than set this to 1. */);
29335 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29336 doc: /* Scroll up to this many lines, to bring point back on screen.
29337 If point moves off-screen, redisplay will scroll by up to
29338 `scroll-conservatively' lines in order to bring point just barely
29339 onto the screen again. If that cannot be done, then redisplay
29340 recenters point as usual.
29342 If the value is greater than 100, redisplay will never recenter point,
29343 but will always scroll just enough text to bring point into view, even
29344 if you move far away.
29346 A value of zero means always recenter point if it moves off screen. */);
29347 scroll_conservatively = 0;
29349 DEFVAR_INT ("scroll-margin", scroll_margin,
29350 doc: /* Number of lines of margin at the top and bottom of a window.
29351 Recenter the window whenever point gets within this many lines
29352 of the top or bottom of the window. */);
29353 scroll_margin = 0;
29355 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29356 doc: /* Pixels per inch value for non-window system displays.
29357 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29358 Vdisplay_pixels_per_inch = make_float (72.0);
29360 #ifdef GLYPH_DEBUG
29361 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29362 #endif
29364 DEFVAR_LISP ("truncate-partial-width-windows",
29365 Vtruncate_partial_width_windows,
29366 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29367 For an integer value, truncate lines in each window narrower than the
29368 full frame width, provided the window width is less than that integer;
29369 otherwise, respect the value of `truncate-lines'.
29371 For any other non-nil value, truncate lines in all windows that do
29372 not span the full frame width.
29374 A value of nil means to respect the value of `truncate-lines'.
29376 If `word-wrap' is enabled, you might want to reduce this. */);
29377 Vtruncate_partial_width_windows = make_number (50);
29379 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29380 doc: /* Maximum buffer size for which line number should be displayed.
29381 If the buffer is bigger than this, the line number does not appear
29382 in the mode line. A value of nil means no limit. */);
29383 Vline_number_display_limit = Qnil;
29385 DEFVAR_INT ("line-number-display-limit-width",
29386 line_number_display_limit_width,
29387 doc: /* Maximum line width (in characters) for line number display.
29388 If the average length of the lines near point is bigger than this, then the
29389 line number may be omitted from the mode line. */);
29390 line_number_display_limit_width = 200;
29392 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29393 doc: /* Non-nil means highlight region even in nonselected windows. */);
29394 highlight_nonselected_windows = 0;
29396 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29397 doc: /* Non-nil if more than one frame is visible on this display.
29398 Minibuffer-only frames don't count, but iconified frames do.
29399 This variable is not guaranteed to be accurate except while processing
29400 `frame-title-format' and `icon-title-format'. */);
29402 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29403 doc: /* Template for displaying the title bar of visible frames.
29404 \(Assuming the window manager supports this feature.)
29406 This variable has the same structure as `mode-line-format', except that
29407 the %c and %l constructs are ignored. It is used only on frames for
29408 which no explicit name has been set \(see `modify-frame-parameters'). */);
29410 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29411 doc: /* Template for displaying the title bar of an iconified frame.
29412 \(Assuming the window manager supports this feature.)
29413 This variable has the same structure as `mode-line-format' (which see),
29414 and is used only on frames for which no explicit name has been set
29415 \(see `modify-frame-parameters'). */);
29416 Vicon_title_format
29417 = Vframe_title_format
29418 = listn (CONSTYPE_PURE, 3,
29419 intern_c_string ("multiple-frames"),
29420 build_pure_c_string ("%b"),
29421 listn (CONSTYPE_PURE, 4,
29422 empty_unibyte_string,
29423 intern_c_string ("invocation-name"),
29424 build_pure_c_string ("@"),
29425 intern_c_string ("system-name")));
29427 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29428 doc: /* Maximum number of lines to keep in the message log buffer.
29429 If nil, disable message logging. If t, log messages but don't truncate
29430 the buffer when it becomes large. */);
29431 Vmessage_log_max = make_number (1000);
29433 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29434 doc: /* Functions called before redisplay, if window sizes have changed.
29435 The value should be a list of functions that take one argument.
29436 Just before redisplay, for each frame, if any of its windows have changed
29437 size since the last redisplay, or have been split or deleted,
29438 all the functions in the list are called, with the frame as argument. */);
29439 Vwindow_size_change_functions = Qnil;
29441 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29442 doc: /* List of functions to call before redisplaying a window with scrolling.
29443 Each function is called with two arguments, the window and its new
29444 display-start position. Note that these functions are also called by
29445 `set-window-buffer'. Also note that the value of `window-end' is not
29446 valid when these functions are called.
29448 Warning: Do not use this feature to alter the way the window
29449 is scrolled. It is not designed for that, and such use probably won't
29450 work. */);
29451 Vwindow_scroll_functions = Qnil;
29453 DEFVAR_LISP ("window-text-change-functions",
29454 Vwindow_text_change_functions,
29455 doc: /* Functions to call in redisplay when text in the window might change. */);
29456 Vwindow_text_change_functions = Qnil;
29458 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29459 doc: /* Functions called when redisplay of a window reaches the end trigger.
29460 Each function is called with two arguments, the window and the end trigger value.
29461 See `set-window-redisplay-end-trigger'. */);
29462 Vredisplay_end_trigger_functions = Qnil;
29464 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29465 doc: /* Non-nil means autoselect window with mouse pointer.
29466 If nil, do not autoselect windows.
29467 A positive number means delay autoselection by that many seconds: a
29468 window is autoselected only after the mouse has remained in that
29469 window for the duration of the delay.
29470 A negative number has a similar effect, but causes windows to be
29471 autoselected only after the mouse has stopped moving. \(Because of
29472 the way Emacs compares mouse events, you will occasionally wait twice
29473 that time before the window gets selected.\)
29474 Any other value means to autoselect window instantaneously when the
29475 mouse pointer enters it.
29477 Autoselection selects the minibuffer only if it is active, and never
29478 unselects the minibuffer if it is active.
29480 When customizing this variable make sure that the actual value of
29481 `focus-follows-mouse' matches the behavior of your window manager. */);
29482 Vmouse_autoselect_window = Qnil;
29484 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29485 doc: /* Non-nil means automatically resize tool-bars.
29486 This dynamically changes the tool-bar's height to the minimum height
29487 that is needed to make all tool-bar items visible.
29488 If value is `grow-only', the tool-bar's height is only increased
29489 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29490 Vauto_resize_tool_bars = Qt;
29492 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29493 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29494 auto_raise_tool_bar_buttons_p = 1;
29496 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29497 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29498 make_cursor_line_fully_visible_p = 1;
29500 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29501 doc: /* Border below tool-bar in pixels.
29502 If an integer, use it as the height of the border.
29503 If it is one of `internal-border-width' or `border-width', use the
29504 value of the corresponding frame parameter.
29505 Otherwise, no border is added below the tool-bar. */);
29506 Vtool_bar_border = Qinternal_border_width;
29508 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29509 doc: /* Margin around tool-bar buttons in pixels.
29510 If an integer, use that for both horizontal and vertical margins.
29511 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29512 HORZ specifying the horizontal margin, and VERT specifying the
29513 vertical margin. */);
29514 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29516 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29517 doc: /* Relief thickness of tool-bar buttons. */);
29518 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29520 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29521 doc: /* Tool bar style to use.
29522 It can be one of
29523 image - show images only
29524 text - show text only
29525 both - show both, text below image
29526 both-horiz - show text to the right of the image
29527 text-image-horiz - show text to the left of the image
29528 any other - use system default or image if no system default.
29530 This variable only affects the GTK+ toolkit version of Emacs. */);
29531 Vtool_bar_style = Qnil;
29533 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29534 doc: /* Maximum number of characters a label can have to be shown.
29535 The tool bar style must also show labels for this to have any effect, see
29536 `tool-bar-style'. */);
29537 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29539 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29540 doc: /* List of functions to call to fontify regions of text.
29541 Each function is called with one argument POS. Functions must
29542 fontify a region starting at POS in the current buffer, and give
29543 fontified regions the property `fontified'. */);
29544 Vfontification_functions = Qnil;
29545 Fmake_variable_buffer_local (Qfontification_functions);
29547 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29548 unibyte_display_via_language_environment,
29549 doc: /* Non-nil means display unibyte text according to language environment.
29550 Specifically, this means that raw bytes in the range 160-255 decimal
29551 are displayed by converting them to the equivalent multibyte characters
29552 according to the current language environment. As a result, they are
29553 displayed according to the current fontset.
29555 Note that this variable affects only how these bytes are displayed,
29556 but does not change the fact they are interpreted as raw bytes. */);
29557 unibyte_display_via_language_environment = 0;
29559 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29560 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29561 If a float, it specifies a fraction of the mini-window frame's height.
29562 If an integer, it specifies a number of lines. */);
29563 Vmax_mini_window_height = make_float (0.25);
29565 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29566 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29567 A value of nil means don't automatically resize mini-windows.
29568 A value of t means resize them to fit the text displayed in them.
29569 A value of `grow-only', the default, means let mini-windows grow only;
29570 they return to their normal size when the minibuffer is closed, or the
29571 echo area becomes empty. */);
29572 Vresize_mini_windows = Qgrow_only;
29574 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29575 doc: /* Alist specifying how to blink the cursor off.
29576 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29577 `cursor-type' frame-parameter or variable equals ON-STATE,
29578 comparing using `equal', Emacs uses OFF-STATE to specify
29579 how to blink it off. ON-STATE and OFF-STATE are values for
29580 the `cursor-type' frame parameter.
29582 If a frame's ON-STATE has no entry in this list,
29583 the frame's other specifications determine how to blink the cursor off. */);
29584 Vblink_cursor_alist = Qnil;
29586 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29587 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29588 If non-nil, windows are automatically scrolled horizontally to make
29589 point visible. */);
29590 automatic_hscrolling_p = 1;
29591 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29593 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29594 doc: /* How many columns away from the window edge point is allowed to get
29595 before automatic hscrolling will horizontally scroll the window. */);
29596 hscroll_margin = 5;
29598 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29599 doc: /* How many columns to scroll the window when point gets too close to the edge.
29600 When point is less than `hscroll-margin' columns from the window
29601 edge, automatic hscrolling will scroll the window by the amount of columns
29602 determined by this variable. If its value is a positive integer, scroll that
29603 many columns. If it's a positive floating-point number, it specifies the
29604 fraction of the window's width to scroll. If it's nil or zero, point will be
29605 centered horizontally after the scroll. Any other value, including negative
29606 numbers, are treated as if the value were zero.
29608 Automatic hscrolling always moves point outside the scroll margin, so if
29609 point was more than scroll step columns inside the margin, the window will
29610 scroll more than the value given by the scroll step.
29612 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29613 and `scroll-right' overrides this variable's effect. */);
29614 Vhscroll_step = make_number (0);
29616 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29617 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29618 Bind this around calls to `message' to let it take effect. */);
29619 message_truncate_lines = 0;
29621 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29622 doc: /* Normal hook run to update the menu bar definitions.
29623 Redisplay runs this hook before it redisplays the menu bar.
29624 This is used to update submenus such as Buffers,
29625 whose contents depend on various data. */);
29626 Vmenu_bar_update_hook = Qnil;
29628 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29629 doc: /* Frame for which we are updating a menu.
29630 The enable predicate for a menu binding should check this variable. */);
29631 Vmenu_updating_frame = Qnil;
29633 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29634 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29635 inhibit_menubar_update = 0;
29637 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29638 doc: /* Prefix prepended to all continuation lines at display time.
29639 The value may be a string, an image, or a stretch-glyph; it is
29640 interpreted in the same way as the value of a `display' text property.
29642 This variable is overridden by any `wrap-prefix' text or overlay
29643 property.
29645 To add a prefix to non-continuation lines, use `line-prefix'. */);
29646 Vwrap_prefix = Qnil;
29647 DEFSYM (Qwrap_prefix, "wrap-prefix");
29648 Fmake_variable_buffer_local (Qwrap_prefix);
29650 DEFVAR_LISP ("line-prefix", Vline_prefix,
29651 doc: /* Prefix prepended to all non-continuation lines at display time.
29652 The value may be a string, an image, or a stretch-glyph; it is
29653 interpreted in the same way as the value of a `display' text property.
29655 This variable is overridden by any `line-prefix' text or overlay
29656 property.
29658 To add a prefix to continuation lines, use `wrap-prefix'. */);
29659 Vline_prefix = Qnil;
29660 DEFSYM (Qline_prefix, "line-prefix");
29661 Fmake_variable_buffer_local (Qline_prefix);
29663 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29664 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29665 inhibit_eval_during_redisplay = 0;
29667 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29668 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29669 inhibit_free_realized_faces = 0;
29671 #ifdef GLYPH_DEBUG
29672 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29673 doc: /* Inhibit try_window_id display optimization. */);
29674 inhibit_try_window_id = 0;
29676 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29677 doc: /* Inhibit try_window_reusing display optimization. */);
29678 inhibit_try_window_reusing = 0;
29680 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29681 doc: /* Inhibit try_cursor_movement display optimization. */);
29682 inhibit_try_cursor_movement = 0;
29683 #endif /* GLYPH_DEBUG */
29685 DEFVAR_INT ("overline-margin", overline_margin,
29686 doc: /* Space between overline and text, in pixels.
29687 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29688 margin to the character height. */);
29689 overline_margin = 2;
29691 DEFVAR_INT ("underline-minimum-offset",
29692 underline_minimum_offset,
29693 doc: /* Minimum distance between baseline and underline.
29694 This can improve legibility of underlined text at small font sizes,
29695 particularly when using variable `x-use-underline-position-properties'
29696 with fonts that specify an UNDERLINE_POSITION relatively close to the
29697 baseline. The default value is 1. */);
29698 underline_minimum_offset = 1;
29700 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29701 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29702 This feature only works when on a window system that can change
29703 cursor shapes. */);
29704 display_hourglass_p = 1;
29706 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29707 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29708 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29710 hourglass_atimer = NULL;
29711 hourglass_shown_p = 0;
29713 DEFSYM (Qglyphless_char, "glyphless-char");
29714 DEFSYM (Qhex_code, "hex-code");
29715 DEFSYM (Qempty_box, "empty-box");
29716 DEFSYM (Qthin_space, "thin-space");
29717 DEFSYM (Qzero_width, "zero-width");
29719 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29720 /* Intern this now in case it isn't already done.
29721 Setting this variable twice is harmless.
29722 But don't staticpro it here--that is done in alloc.c. */
29723 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29724 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29726 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29727 doc: /* Char-table defining glyphless characters.
29728 Each element, if non-nil, should be one of the following:
29729 an ASCII acronym string: display this string in a box
29730 `hex-code': display the hexadecimal code of a character in a box
29731 `empty-box': display as an empty box
29732 `thin-space': display as 1-pixel width space
29733 `zero-width': don't display
29734 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29735 display method for graphical terminals and text terminals respectively.
29736 GRAPHICAL and TEXT should each have one of the values listed above.
29738 The char-table has one extra slot to control the display of a character for
29739 which no font is found. This slot only takes effect on graphical terminals.
29740 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29741 `thin-space'. The default is `empty-box'. */);
29742 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29743 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29744 Qempty_box);
29746 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29747 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29748 Vdebug_on_message = Qnil;
29752 /* Initialize this module when Emacs starts. */
29754 void
29755 init_xdisp (void)
29757 current_header_line_height = current_mode_line_height = -1;
29759 CHARPOS (this_line_start_pos) = 0;
29761 if (!noninteractive)
29763 struct window *m = XWINDOW (minibuf_window);
29764 Lisp_Object frame = m->frame;
29765 struct frame *f = XFRAME (frame);
29766 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29767 struct window *r = XWINDOW (root);
29768 int i;
29770 echo_area_window = minibuf_window;
29772 r->top_line = FRAME_TOP_MARGIN (f);
29773 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29774 r->total_cols = FRAME_COLS (f);
29776 m->top_line = FRAME_LINES (f) - 1;
29777 m->total_lines = 1;
29778 m->total_cols = FRAME_COLS (f);
29780 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29781 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29782 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29784 /* The default ellipsis glyphs `...'. */
29785 for (i = 0; i < 3; ++i)
29786 default_invis_vector[i] = make_number ('.');
29790 /* Allocate the buffer for frame titles.
29791 Also used for `format-mode-line'. */
29792 int size = 100;
29793 mode_line_noprop_buf = xmalloc (size);
29794 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29795 mode_line_noprop_ptr = mode_line_noprop_buf;
29796 mode_line_target = MODE_LINE_DISPLAY;
29799 help_echo_showing_p = 0;
29802 /* Platform-independent portion of hourglass implementation. */
29804 /* Cancel a currently active hourglass timer, and start a new one. */
29805 void
29806 start_hourglass (void)
29808 #if defined (HAVE_WINDOW_SYSTEM)
29809 EMACS_TIME delay;
29811 cancel_hourglass ();
29813 if (INTEGERP (Vhourglass_delay)
29814 && XINT (Vhourglass_delay) > 0)
29815 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29816 TYPE_MAXIMUM (time_t)),
29818 else if (FLOATP (Vhourglass_delay)
29819 && XFLOAT_DATA (Vhourglass_delay) > 0)
29820 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29821 else
29822 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29824 #ifdef HAVE_NTGUI
29826 extern void w32_note_current_window (void);
29827 w32_note_current_window ();
29829 #endif /* HAVE_NTGUI */
29831 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29832 show_hourglass, NULL);
29833 #endif
29837 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29838 shown. */
29839 void
29840 cancel_hourglass (void)
29842 #if defined (HAVE_WINDOW_SYSTEM)
29843 if (hourglass_atimer)
29845 cancel_atimer (hourglass_atimer);
29846 hourglass_atimer = NULL;
29849 if (hourglass_shown_p)
29850 hide_hourglass ();
29851 #endif