* todo-mode.el: Offer to convert legacy file. Update commentary.
[emacs.git] / src / xdisp.c
blobe1d6b0c9a27bc416ac8a5e4eb69d3fc158922a17
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
103 . try_window
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
116 Desired matrices.
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
161 Frame matrices.
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
182 Bidirectional display.
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
225 Bidirectional display and character compositions
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
252 Moving an iterator in bidirectional text
253 without producing glyphs
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
303 #ifdef HAVE_X_WINDOWS
304 #include "xterm.h"
305 #endif
306 #ifdef HAVE_NTGUI
307 #include "w32term.h"
308 #endif
309 #ifdef HAVE_NS
310 #include "nsterm.h"
311 #endif
312 #ifdef USE_GTK
313 #include "gtkutil.h"
314 #endif
316 #include "font.h"
318 #ifndef FRAME_X_OUTPUT
319 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
320 #endif
322 #define INFINITY 10000000
324 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
325 Lisp_Object Qwindow_scroll_functions;
326 static Lisp_Object Qwindow_text_change_functions;
327 static Lisp_Object Qredisplay_end_trigger_functions;
328 Lisp_Object Qinhibit_point_motion_hooks;
329 static Lisp_Object QCeval, QCpropertize;
330 Lisp_Object QCfile, QCdata;
331 static Lisp_Object Qfontified;
332 static Lisp_Object Qgrow_only;
333 static Lisp_Object Qinhibit_eval_during_redisplay;
334 static Lisp_Object Qbuffer_position, Qposition, Qobject;
335 static Lisp_Object Qright_to_left, Qleft_to_right;
337 /* Cursor shapes. */
338 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
340 /* Pointer shapes. */
341 static Lisp_Object Qarrow, Qhand;
342 Lisp_Object Qtext;
344 /* Holds the list (error). */
345 static Lisp_Object list_of_error;
347 static Lisp_Object Qfontification_functions;
349 static Lisp_Object Qwrap_prefix;
350 static Lisp_Object Qline_prefix;
351 static Lisp_Object Qredisplay_internal;
353 /* Non-nil means don't actually do any redisplay. */
355 Lisp_Object Qinhibit_redisplay;
357 /* Names of text properties relevant for redisplay. */
359 Lisp_Object Qdisplay;
361 Lisp_Object Qspace, QCalign_to;
362 static Lisp_Object QCrelative_width, QCrelative_height;
363 Lisp_Object Qleft_margin, Qright_margin;
364 static Lisp_Object Qspace_width, Qraise;
365 static Lisp_Object Qslice;
366 Lisp_Object Qcenter;
367 static Lisp_Object Qmargin, Qpointer;
368 static Lisp_Object Qline_height;
370 #ifdef HAVE_WINDOW_SYSTEM
372 /* Test if overflow newline into fringe. Called with iterator IT
373 at or past right window margin, and with IT->current_x set. */
375 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
376 (!NILP (Voverflow_newline_into_fringe) \
377 && FRAME_WINDOW_P ((IT)->f) \
378 && ((IT)->bidi_it.paragraph_dir == R2L \
379 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
380 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
381 && (IT)->current_x == (IT)->last_visible_x \
382 && (IT)->line_wrap != WORD_WRAP)
384 #else /* !HAVE_WINDOW_SYSTEM */
385 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
386 #endif /* HAVE_WINDOW_SYSTEM */
388 /* Test if the display element loaded in IT, or the underlying buffer
389 or string character, is a space or a TAB character. This is used
390 to determine where word wrapping can occur. */
392 #define IT_DISPLAYING_WHITESPACE(it) \
393 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
394 || ((STRINGP (it->string) \
395 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
396 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
397 || (it->s \
398 && (it->s[IT_BYTEPOS (*it)] == ' ' \
399 || it->s[IT_BYTEPOS (*it)] == '\t')) \
400 || (IT_BYTEPOS (*it) < ZV_BYTE \
401 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
402 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
404 /* Name of the face used to highlight trailing whitespace. */
406 static Lisp_Object Qtrailing_whitespace;
408 /* Name and number of the face used to highlight escape glyphs. */
410 static Lisp_Object Qescape_glyph;
412 /* Name and number of the face used to highlight non-breaking spaces. */
414 static Lisp_Object Qnobreak_space;
416 /* The symbol `image' which is the car of the lists used to represent
417 images in Lisp. Also a tool bar style. */
419 Lisp_Object Qimage;
421 /* The image map types. */
422 Lisp_Object QCmap;
423 static Lisp_Object QCpointer;
424 static Lisp_Object Qrect, Qcircle, Qpoly;
426 /* Tool bar styles */
427 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
429 /* Non-zero means print newline to stdout before next mini-buffer
430 message. */
432 int noninteractive_need_newline;
434 /* Non-zero means print newline to message log before next message. */
436 static int message_log_need_newline;
438 /* Three markers that message_dolog uses.
439 It could allocate them itself, but that causes trouble
440 in handling memory-full errors. */
441 static Lisp_Object message_dolog_marker1;
442 static Lisp_Object message_dolog_marker2;
443 static Lisp_Object message_dolog_marker3;
445 /* The buffer position of the first character appearing entirely or
446 partially on the line of the selected window which contains the
447 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
448 redisplay optimization in redisplay_internal. */
450 static struct text_pos this_line_start_pos;
452 /* Number of characters past the end of the line above, including the
453 terminating newline. */
455 static struct text_pos this_line_end_pos;
457 /* The vertical positions and the height of this line. */
459 static int this_line_vpos;
460 static int this_line_y;
461 static int this_line_pixel_height;
463 /* X position at which this display line starts. Usually zero;
464 negative if first character is partially visible. */
466 static int this_line_start_x;
468 /* The smallest character position seen by move_it_* functions as they
469 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
470 hscrolled lines, see display_line. */
472 static struct text_pos this_line_min_pos;
474 /* Buffer that this_line_.* variables are referring to. */
476 static struct buffer *this_line_buffer;
479 /* Values of those variables at last redisplay are stored as
480 properties on `overlay-arrow-position' symbol. However, if
481 Voverlay_arrow_position is a marker, last-arrow-position is its
482 numerical position. */
484 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
486 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
487 properties on a symbol in overlay-arrow-variable-list. */
489 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
491 Lisp_Object Qmenu_bar_update_hook;
493 /* Nonzero if an overlay arrow has been displayed in this window. */
495 static int overlay_arrow_seen;
497 /* Vector containing glyphs for an ellipsis `...'. */
499 static Lisp_Object default_invis_vector[3];
501 /* This is the window where the echo area message was displayed. It
502 is always a mini-buffer window, but it may not be the same window
503 currently active as a mini-buffer. */
505 Lisp_Object echo_area_window;
507 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
508 pushes the current message and the value of
509 message_enable_multibyte on the stack, the function restore_message
510 pops the stack and displays MESSAGE again. */
512 static Lisp_Object Vmessage_stack;
514 /* Nonzero means multibyte characters were enabled when the echo area
515 message was specified. */
517 static int message_enable_multibyte;
519 /* Nonzero if we should redraw the mode lines on the next redisplay. */
521 int update_mode_lines;
523 /* Nonzero if window sizes or contents have changed since last
524 redisplay that finished. */
526 int windows_or_buffers_changed;
528 /* Nonzero means a frame's cursor type has been changed. */
530 int cursor_type_changed;
532 /* Nonzero after display_mode_line if %l was used and it displayed a
533 line number. */
535 static int line_number_displayed;
537 /* The name of the *Messages* buffer, a string. */
539 static Lisp_Object Vmessages_buffer_name;
541 /* Current, index 0, and last displayed echo area message. Either
542 buffers from echo_buffers, or nil to indicate no message. */
544 Lisp_Object echo_area_buffer[2];
546 /* The buffers referenced from echo_area_buffer. */
548 static Lisp_Object echo_buffer[2];
550 /* A vector saved used in with_area_buffer to reduce consing. */
552 static Lisp_Object Vwith_echo_area_save_vector;
554 /* Non-zero means display_echo_area should display the last echo area
555 message again. Set by redisplay_preserve_echo_area. */
557 static int display_last_displayed_message_p;
559 /* Nonzero if echo area is being used by print; zero if being used by
560 message. */
562 static int message_buf_print;
564 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
566 static Lisp_Object Qinhibit_menubar_update;
567 static Lisp_Object Qmessage_truncate_lines;
569 /* Set to 1 in clear_message to make redisplay_internal aware
570 of an emptied echo area. */
572 static int message_cleared_p;
574 /* A scratch glyph row with contents used for generating truncation
575 glyphs. Also used in direct_output_for_insert. */
577 #define MAX_SCRATCH_GLYPHS 100
578 static struct glyph_row scratch_glyph_row;
579 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
581 /* Ascent and height of the last line processed by move_it_to. */
583 static int last_height;
585 /* Non-zero if there's a help-echo in the echo area. */
587 int help_echo_showing_p;
589 /* If >= 0, computed, exact values of mode-line and header-line height
590 to use in the macros CURRENT_MODE_LINE_HEIGHT and
591 CURRENT_HEADER_LINE_HEIGHT. */
593 int current_mode_line_height, current_header_line_height;
595 /* The maximum distance to look ahead for text properties. Values
596 that are too small let us call compute_char_face and similar
597 functions too often which is expensive. Values that are too large
598 let us call compute_char_face and alike too often because we
599 might not be interested in text properties that far away. */
601 #define TEXT_PROP_DISTANCE_LIMIT 100
603 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
604 iterator state and later restore it. This is needed because the
605 bidi iterator on bidi.c keeps a stacked cache of its states, which
606 is really a singleton. When we use scratch iterator objects to
607 move around the buffer, we can cause the bidi cache to be pushed or
608 popped, and therefore we need to restore the cache state when we
609 return to the original iterator. */
610 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
611 do { \
612 if (CACHE) \
613 bidi_unshelve_cache (CACHE, 1); \
614 ITCOPY = ITORIG; \
615 CACHE = bidi_shelve_cache (); \
616 } while (0)
618 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
619 do { \
620 if (pITORIG != pITCOPY) \
621 *(pITORIG) = *(pITCOPY); \
622 bidi_unshelve_cache (CACHE, 0); \
623 CACHE = NULL; \
624 } while (0)
626 #ifdef GLYPH_DEBUG
628 /* Non-zero means print traces of redisplay if compiled with
629 GLYPH_DEBUG defined. */
631 int trace_redisplay_p;
633 #endif /* GLYPH_DEBUG */
635 #ifdef DEBUG_TRACE_MOVE
636 /* Non-zero means trace with TRACE_MOVE to stderr. */
637 int trace_move;
639 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
640 #else
641 #define TRACE_MOVE(x) (void) 0
642 #endif
644 static Lisp_Object Qauto_hscroll_mode;
646 /* Buffer being redisplayed -- for redisplay_window_error. */
648 static struct buffer *displayed_buffer;
650 /* Value returned from text property handlers (see below). */
652 enum prop_handled
654 HANDLED_NORMALLY,
655 HANDLED_RECOMPUTE_PROPS,
656 HANDLED_OVERLAY_STRING_CONSUMED,
657 HANDLED_RETURN
660 /* A description of text properties that redisplay is interested
661 in. */
663 struct props
665 /* The name of the property. */
666 Lisp_Object *name;
668 /* A unique index for the property. */
669 enum prop_idx idx;
671 /* A handler function called to set up iterator IT from the property
672 at IT's current position. Value is used to steer handle_stop. */
673 enum prop_handled (*handler) (struct it *it);
676 static enum prop_handled handle_face_prop (struct it *);
677 static enum prop_handled handle_invisible_prop (struct it *);
678 static enum prop_handled handle_display_prop (struct it *);
679 static enum prop_handled handle_composition_prop (struct it *);
680 static enum prop_handled handle_overlay_change (struct it *);
681 static enum prop_handled handle_fontified_prop (struct it *);
683 /* Properties handled by iterators. */
685 static struct props it_props[] =
687 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
688 /* Handle `face' before `display' because some sub-properties of
689 `display' need to know the face. */
690 {&Qface, FACE_PROP_IDX, handle_face_prop},
691 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
692 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
693 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
694 {NULL, 0, NULL}
697 /* Value is the position described by X. If X is a marker, value is
698 the marker_position of X. Otherwise, value is X. */
700 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
702 /* Enumeration returned by some move_it_.* functions internally. */
704 enum move_it_result
706 /* Not used. Undefined value. */
707 MOVE_UNDEFINED,
709 /* Move ended at the requested buffer position or ZV. */
710 MOVE_POS_MATCH_OR_ZV,
712 /* Move ended at the requested X pixel position. */
713 MOVE_X_REACHED,
715 /* Move within a line ended at the end of a line that must be
716 continued. */
717 MOVE_LINE_CONTINUED,
719 /* Move within a line ended at the end of a line that would
720 be displayed truncated. */
721 MOVE_LINE_TRUNCATED,
723 /* Move within a line ended at a line end. */
724 MOVE_NEWLINE_OR_CR
727 /* This counter is used to clear the face cache every once in a while
728 in redisplay_internal. It is incremented for each redisplay.
729 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
730 cleared. */
732 #define CLEAR_FACE_CACHE_COUNT 500
733 static int clear_face_cache_count;
735 /* Similarly for the image cache. */
737 #ifdef HAVE_WINDOW_SYSTEM
738 #define CLEAR_IMAGE_CACHE_COUNT 101
739 static int clear_image_cache_count;
741 /* Null glyph slice */
742 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
743 #endif
745 /* True while redisplay_internal is in progress. */
747 bool redisplaying_p;
749 static Lisp_Object Qinhibit_free_realized_faces;
750 static Lisp_Object Qmode_line_default_help_echo;
752 /* If a string, XTread_socket generates an event to display that string.
753 (The display is done in read_char.) */
755 Lisp_Object help_echo_string;
756 Lisp_Object help_echo_window;
757 Lisp_Object help_echo_object;
758 ptrdiff_t help_echo_pos;
760 /* Temporary variable for XTread_socket. */
762 Lisp_Object previous_help_echo_string;
764 /* Platform-independent portion of hourglass implementation. */
766 /* Non-zero means an hourglass cursor is currently shown. */
767 int hourglass_shown_p;
769 /* If non-null, an asynchronous timer that, when it expires, displays
770 an hourglass cursor on all frames. */
771 struct atimer *hourglass_atimer;
773 /* Name of the face used to display glyphless characters. */
774 Lisp_Object Qglyphless_char;
776 /* Symbol for the purpose of Vglyphless_char_display. */
777 static Lisp_Object Qglyphless_char_display;
779 /* Method symbols for Vglyphless_char_display. */
780 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
782 /* Default pixel width of `thin-space' display method. */
783 #define THIN_SPACE_WIDTH 1
785 /* Default number of seconds to wait before displaying an hourglass
786 cursor. */
787 #define DEFAULT_HOURGLASS_DELAY 1
790 /* Function prototypes. */
792 static void setup_for_ellipsis (struct it *, int);
793 static void set_iterator_to_next (struct it *, int);
794 static void mark_window_display_accurate_1 (struct window *, int);
795 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
796 static int display_prop_string_p (Lisp_Object, Lisp_Object);
797 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
798 static int cursor_row_p (struct glyph_row *);
799 static int redisplay_mode_lines (Lisp_Object, int);
800 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
802 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
804 static void handle_line_prefix (struct it *);
806 static void pint2str (char *, int, ptrdiff_t);
807 static void pint2hrstr (char *, int, ptrdiff_t);
808 static struct text_pos run_window_scroll_functions (Lisp_Object,
809 struct text_pos);
810 static void reconsider_clip_changes (struct window *, struct buffer *);
811 static int text_outside_line_unchanged_p (struct window *,
812 ptrdiff_t, ptrdiff_t);
813 static void store_mode_line_noprop_char (char);
814 static int store_mode_line_noprop (const char *, int, int);
815 static void handle_stop (struct it *);
816 static void handle_stop_backwards (struct it *, ptrdiff_t);
817 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
818 static void ensure_echo_area_buffers (void);
819 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
820 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
821 static int with_echo_area_buffer (struct window *, int,
822 int (*) (ptrdiff_t, Lisp_Object),
823 ptrdiff_t, Lisp_Object);
824 static void clear_garbaged_frames (void);
825 static int current_message_1 (ptrdiff_t, Lisp_Object);
826 static void pop_message (void);
827 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
828 static void set_message (Lisp_Object);
829 static int set_message_1 (ptrdiff_t, Lisp_Object);
830 static int display_echo_area (struct window *);
831 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
832 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
833 static Lisp_Object unwind_redisplay (Lisp_Object);
834 static int string_char_and_length (const unsigned char *, int *);
835 static struct text_pos display_prop_end (struct it *, Lisp_Object,
836 struct text_pos);
837 static int compute_window_start_on_continuation_line (struct window *);
838 static void insert_left_trunc_glyphs (struct it *);
839 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
840 Lisp_Object);
841 static void extend_face_to_end_of_line (struct it *);
842 static int append_space_for_newline (struct it *, int);
843 static int cursor_row_fully_visible_p (struct window *, int, int);
844 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
845 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
846 static int trailing_whitespace_p (ptrdiff_t);
847 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
848 static void push_it (struct it *, struct text_pos *);
849 static void iterate_out_of_display_property (struct it *);
850 static void pop_it (struct it *);
851 static void sync_frame_with_window_matrix_rows (struct window *);
852 static void redisplay_internal (void);
853 static int echo_area_display (int);
854 static void redisplay_windows (Lisp_Object);
855 static void redisplay_window (Lisp_Object, int);
856 static Lisp_Object redisplay_window_error (Lisp_Object);
857 static Lisp_Object redisplay_window_0 (Lisp_Object);
858 static Lisp_Object redisplay_window_1 (Lisp_Object);
859 static int set_cursor_from_row (struct window *, struct glyph_row *,
860 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
861 int, int);
862 static int update_menu_bar (struct frame *, int, int);
863 static int try_window_reusing_current_matrix (struct window *);
864 static int try_window_id (struct window *);
865 static int display_line (struct it *);
866 static int display_mode_lines (struct window *);
867 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
868 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
869 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
870 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
871 static void display_menu_bar (struct window *);
872 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
873 ptrdiff_t *);
874 static int display_string (const char *, Lisp_Object, Lisp_Object,
875 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
876 static void compute_line_metrics (struct it *);
877 static void run_redisplay_end_trigger_hook (struct it *);
878 static int get_overlay_strings (struct it *, ptrdiff_t);
879 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
880 static void next_overlay_string (struct it *);
881 static void reseat (struct it *, struct text_pos, int);
882 static void reseat_1 (struct it *, struct text_pos, int);
883 static void back_to_previous_visible_line_start (struct it *);
884 static void reseat_at_next_visible_line_start (struct it *, int);
885 static int next_element_from_ellipsis (struct it *);
886 static int next_element_from_display_vector (struct it *);
887 static int next_element_from_string (struct it *);
888 static int next_element_from_c_string (struct it *);
889 static int next_element_from_buffer (struct it *);
890 static int next_element_from_composition (struct it *);
891 static int next_element_from_image (struct it *);
892 static int next_element_from_stretch (struct it *);
893 static void load_overlay_strings (struct it *, ptrdiff_t);
894 static int init_from_display_pos (struct it *, struct window *,
895 struct display_pos *);
896 static void reseat_to_string (struct it *, const char *,
897 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
898 static int get_next_display_element (struct it *);
899 static enum move_it_result
900 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
901 enum move_operation_enum);
902 static void get_visually_first_element (struct it *);
903 static void init_to_row_start (struct it *, struct window *,
904 struct glyph_row *);
905 static int init_to_row_end (struct it *, struct window *,
906 struct glyph_row *);
907 static void back_to_previous_line_start (struct it *);
908 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
909 static struct text_pos string_pos_nchars_ahead (struct text_pos,
910 Lisp_Object, ptrdiff_t);
911 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
912 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
913 static ptrdiff_t number_of_chars (const char *, bool);
914 static void compute_stop_pos (struct it *);
915 static void compute_string_pos (struct text_pos *, struct text_pos,
916 Lisp_Object);
917 static int face_before_or_after_it_pos (struct it *, int);
918 static ptrdiff_t next_overlay_change (ptrdiff_t);
919 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
920 Lisp_Object, struct text_pos *, ptrdiff_t, int);
921 static int handle_single_display_spec (struct it *, Lisp_Object,
922 Lisp_Object, Lisp_Object,
923 struct text_pos *, ptrdiff_t, int, int);
924 static int underlying_face_id (struct it *);
925 static int in_ellipses_for_invisible_text_p (struct display_pos *,
926 struct window *);
928 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
929 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
931 #ifdef HAVE_WINDOW_SYSTEM
933 static void x_consider_frame_title (Lisp_Object);
934 static int tool_bar_lines_needed (struct frame *, int *);
935 static void update_tool_bar (struct frame *, int);
936 static void build_desired_tool_bar_string (struct frame *f);
937 static int redisplay_tool_bar (struct frame *);
938 static void display_tool_bar_line (struct it *, int);
939 static void notice_overwritten_cursor (struct window *,
940 enum glyph_row_area,
941 int, int, int, int);
942 static void append_stretch_glyph (struct it *, Lisp_Object,
943 int, int, int);
946 #endif /* HAVE_WINDOW_SYSTEM */
948 static void produce_special_glyphs (struct it *, enum display_element_type);
949 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
950 static int coords_in_mouse_face_p (struct window *, int, int);
954 /***********************************************************************
955 Window display dimensions
956 ***********************************************************************/
958 /* Return the bottom boundary y-position for text lines in window W.
959 This is the first y position at which a line cannot start.
960 It is relative to the top of the window.
962 This is the height of W minus the height of a mode line, if any. */
965 window_text_bottom_y (struct window *w)
967 int height = WINDOW_TOTAL_HEIGHT (w);
969 if (WINDOW_WANTS_MODELINE_P (w))
970 height -= CURRENT_MODE_LINE_HEIGHT (w);
971 return height;
974 /* Return the pixel width of display area AREA of window W. AREA < 0
975 means return the total width of W, not including fringes to
976 the left and right of the window. */
979 window_box_width (struct window *w, int area)
981 int cols = w->total_cols;
982 int pixels = 0;
984 if (!w->pseudo_window_p)
986 cols -= WINDOW_SCROLL_BAR_COLS (w);
988 if (area == TEXT_AREA)
990 if (INTEGERP (w->left_margin_cols))
991 cols -= XFASTINT (w->left_margin_cols);
992 if (INTEGERP (w->right_margin_cols))
993 cols -= XFASTINT (w->right_margin_cols);
994 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
996 else if (area == LEFT_MARGIN_AREA)
998 cols = (INTEGERP (w->left_margin_cols)
999 ? XFASTINT (w->left_margin_cols) : 0);
1000 pixels = 0;
1002 else if (area == RIGHT_MARGIN_AREA)
1004 cols = (INTEGERP (w->right_margin_cols)
1005 ? XFASTINT (w->right_margin_cols) : 0);
1006 pixels = 0;
1010 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1014 /* Return the pixel height of the display area of window W, not
1015 including mode lines of W, if any. */
1018 window_box_height (struct window *w)
1020 struct frame *f = XFRAME (w->frame);
1021 int height = WINDOW_TOTAL_HEIGHT (w);
1023 eassert (height >= 0);
1025 /* Note: the code below that determines the mode-line/header-line
1026 height is essentially the same as that contained in the macro
1027 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1028 the appropriate glyph row has its `mode_line_p' flag set,
1029 and if it doesn't, uses estimate_mode_line_height instead. */
1031 if (WINDOW_WANTS_MODELINE_P (w))
1033 struct glyph_row *ml_row
1034 = (w->current_matrix && w->current_matrix->rows
1035 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1036 : 0);
1037 if (ml_row && ml_row->mode_line_p)
1038 height -= ml_row->height;
1039 else
1040 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1043 if (WINDOW_WANTS_HEADER_LINE_P (w))
1045 struct glyph_row *hl_row
1046 = (w->current_matrix && w->current_matrix->rows
1047 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1048 : 0);
1049 if (hl_row && hl_row->mode_line_p)
1050 height -= hl_row->height;
1051 else
1052 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1055 /* With a very small font and a mode-line that's taller than
1056 default, we might end up with a negative height. */
1057 return max (0, height);
1060 /* Return the window-relative coordinate of the left edge of display
1061 area AREA of window W. AREA < 0 means return the left edge of the
1062 whole window, to the right of the left fringe of W. */
1065 window_box_left_offset (struct window *w, int area)
1067 int x;
1069 if (w->pseudo_window_p)
1070 return 0;
1072 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1074 if (area == TEXT_AREA)
1075 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1076 + window_box_width (w, LEFT_MARGIN_AREA));
1077 else if (area == RIGHT_MARGIN_AREA)
1078 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1079 + window_box_width (w, LEFT_MARGIN_AREA)
1080 + window_box_width (w, TEXT_AREA)
1081 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1083 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1084 else if (area == LEFT_MARGIN_AREA
1085 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1086 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1088 return x;
1092 /* Return the window-relative coordinate of the right edge of display
1093 area AREA of window W. AREA < 0 means return the right edge of the
1094 whole window, to the left of the right fringe of W. */
1097 window_box_right_offset (struct window *w, int area)
1099 return window_box_left_offset (w, area) + window_box_width (w, area);
1102 /* Return the frame-relative coordinate of the left edge of display
1103 area AREA of window W. AREA < 0 means return the left edge of the
1104 whole window, to the right of the left fringe of W. */
1107 window_box_left (struct window *w, int area)
1109 struct frame *f = XFRAME (w->frame);
1110 int x;
1112 if (w->pseudo_window_p)
1113 return FRAME_INTERNAL_BORDER_WIDTH (f);
1115 x = (WINDOW_LEFT_EDGE_X (w)
1116 + window_box_left_offset (w, area));
1118 return x;
1122 /* Return the frame-relative coordinate of the right edge of display
1123 area AREA of window W. AREA < 0 means return the right edge of the
1124 whole window, to the left of the right fringe of W. */
1127 window_box_right (struct window *w, int area)
1129 return window_box_left (w, area) + window_box_width (w, area);
1132 /* Get the bounding box of the display area AREA of window W, without
1133 mode lines, in frame-relative coordinates. AREA < 0 means the
1134 whole window, not including the left and right fringes of
1135 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1136 coordinates of the upper-left corner of the box. Return in
1137 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1139 void
1140 window_box (struct window *w, int area, int *box_x, int *box_y,
1141 int *box_width, int *box_height)
1143 if (box_width)
1144 *box_width = window_box_width (w, area);
1145 if (box_height)
1146 *box_height = window_box_height (w);
1147 if (box_x)
1148 *box_x = window_box_left (w, area);
1149 if (box_y)
1151 *box_y = WINDOW_TOP_EDGE_Y (w);
1152 if (WINDOW_WANTS_HEADER_LINE_P (w))
1153 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1158 /* Get the bounding box of the display area AREA of window W, without
1159 mode lines. AREA < 0 means the whole window, not including the
1160 left and right fringe of the window. Return in *TOP_LEFT_X
1161 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1162 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1163 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1164 box. */
1166 static void
1167 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1168 int *bottom_right_x, int *bottom_right_y)
1170 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1171 bottom_right_y);
1172 *bottom_right_x += *top_left_x;
1173 *bottom_right_y += *top_left_y;
1178 /***********************************************************************
1179 Utilities
1180 ***********************************************************************/
1182 /* Return the bottom y-position of the line the iterator IT is in.
1183 This can modify IT's settings. */
1186 line_bottom_y (struct it *it)
1188 int line_height = it->max_ascent + it->max_descent;
1189 int line_top_y = it->current_y;
1191 if (line_height == 0)
1193 if (last_height)
1194 line_height = last_height;
1195 else if (IT_CHARPOS (*it) < ZV)
1197 move_it_by_lines (it, 1);
1198 line_height = (it->max_ascent || it->max_descent
1199 ? it->max_ascent + it->max_descent
1200 : last_height);
1202 else
1204 struct glyph_row *row = it->glyph_row;
1206 /* Use the default character height. */
1207 it->glyph_row = NULL;
1208 it->what = IT_CHARACTER;
1209 it->c = ' ';
1210 it->len = 1;
1211 PRODUCE_GLYPHS (it);
1212 line_height = it->ascent + it->descent;
1213 it->glyph_row = row;
1217 return line_top_y + line_height;
1220 /* Subroutine of pos_visible_p below. Extracts a display string, if
1221 any, from the display spec given as its argument. */
1222 static Lisp_Object
1223 string_from_display_spec (Lisp_Object spec)
1225 if (CONSP (spec))
1227 while (CONSP (spec))
1229 if (STRINGP (XCAR (spec)))
1230 return XCAR (spec);
1231 spec = XCDR (spec);
1234 else if (VECTORP (spec))
1236 ptrdiff_t i;
1238 for (i = 0; i < ASIZE (spec); i++)
1240 if (STRINGP (AREF (spec, i)))
1241 return AREF (spec, i);
1243 return Qnil;
1246 return spec;
1250 /* Limit insanely large values of W->hscroll on frame F to the largest
1251 value that will still prevent first_visible_x and last_visible_x of
1252 'struct it' from overflowing an int. */
1253 static int
1254 window_hscroll_limited (struct window *w, struct frame *f)
1256 ptrdiff_t window_hscroll = w->hscroll;
1257 int window_text_width = window_box_width (w, TEXT_AREA);
1258 int colwidth = FRAME_COLUMN_WIDTH (f);
1260 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1261 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1263 return window_hscroll;
1266 /* Return 1 if position CHARPOS is visible in window W.
1267 CHARPOS < 0 means return info about WINDOW_END position.
1268 If visible, set *X and *Y to pixel coordinates of top left corner.
1269 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1270 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1273 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1274 int *rtop, int *rbot, int *rowh, int *vpos)
1276 struct it it;
1277 void *itdata = bidi_shelve_cache ();
1278 struct text_pos top;
1279 int visible_p = 0;
1280 struct buffer *old_buffer = NULL;
1282 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1283 return visible_p;
1285 if (XBUFFER (w->contents) != current_buffer)
1287 old_buffer = current_buffer;
1288 set_buffer_internal_1 (XBUFFER (w->contents));
1291 SET_TEXT_POS_FROM_MARKER (top, w->start);
1292 /* Scrolling a minibuffer window via scroll bar when the echo area
1293 shows long text sometimes resets the minibuffer contents behind
1294 our backs. */
1295 if (CHARPOS (top) > ZV)
1296 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1298 /* Compute exact mode line heights. */
1299 if (WINDOW_WANTS_MODELINE_P (w))
1300 current_mode_line_height
1301 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1302 BVAR (current_buffer, mode_line_format));
1304 if (WINDOW_WANTS_HEADER_LINE_P (w))
1305 current_header_line_height
1306 = display_mode_line (w, HEADER_LINE_FACE_ID,
1307 BVAR (current_buffer, header_line_format));
1309 start_display (&it, w, top);
1310 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1311 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1313 if (charpos >= 0
1314 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1315 && IT_CHARPOS (it) >= charpos)
1316 /* When scanning backwards under bidi iteration, move_it_to
1317 stops at or _before_ CHARPOS, because it stops at or to
1318 the _right_ of the character at CHARPOS. */
1319 || (it.bidi_p && it.bidi_it.scan_dir == -1
1320 && IT_CHARPOS (it) <= charpos)))
1322 /* We have reached CHARPOS, or passed it. How the call to
1323 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1324 or covered by a display property, move_it_to stops at the end
1325 of the invisible text, to the right of CHARPOS. (ii) If
1326 CHARPOS is in a display vector, move_it_to stops on its last
1327 glyph. */
1328 int top_x = it.current_x;
1329 int top_y = it.current_y;
1330 /* Calling line_bottom_y may change it.method, it.position, etc. */
1331 enum it_method it_method = it.method;
1332 int bottom_y = (last_height = 0, line_bottom_y (&it));
1333 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1335 if (top_y < window_top_y)
1336 visible_p = bottom_y > window_top_y;
1337 else if (top_y < it.last_visible_y)
1338 visible_p = 1;
1339 if (bottom_y >= it.last_visible_y
1340 && it.bidi_p && it.bidi_it.scan_dir == -1
1341 && IT_CHARPOS (it) < charpos)
1343 /* When the last line of the window is scanned backwards
1344 under bidi iteration, we could be duped into thinking
1345 that we have passed CHARPOS, when in fact move_it_to
1346 simply stopped short of CHARPOS because it reached
1347 last_visible_y. To see if that's what happened, we call
1348 move_it_to again with a slightly larger vertical limit,
1349 and see if it actually moved vertically; if it did, we
1350 didn't really reach CHARPOS, which is beyond window end. */
1351 struct it save_it = it;
1352 /* Why 10? because we don't know how many canonical lines
1353 will the height of the next line(s) be. So we guess. */
1354 int ten_more_lines =
1355 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1357 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1358 MOVE_TO_POS | MOVE_TO_Y);
1359 if (it.current_y > top_y)
1360 visible_p = 0;
1362 it = save_it;
1364 if (visible_p)
1366 if (it_method == GET_FROM_DISPLAY_VECTOR)
1368 /* We stopped on the last glyph of a display vector.
1369 Try and recompute. Hack alert! */
1370 if (charpos < 2 || top.charpos >= charpos)
1371 top_x = it.glyph_row->x;
1372 else
1374 struct it it2, it2_prev;
1375 /* The idea is to get to the previous buffer
1376 position, consume the character there, and use
1377 the pixel coordinates we get after that. But if
1378 the previous buffer position is also displayed
1379 from a display vector, we need to consume all of
1380 the glyphs from that display vector. */
1381 start_display (&it2, w, top);
1382 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1383 /* If we didn't get to CHARPOS - 1, there's some
1384 replacing display property at that position, and
1385 we stopped after it. That is exactly the place
1386 whose coordinates we want. */
1387 if (IT_CHARPOS (it2) != charpos - 1)
1388 it2_prev = it2;
1389 else
1391 /* Iterate until we get out of the display
1392 vector that displays the character at
1393 CHARPOS - 1. */
1394 do {
1395 get_next_display_element (&it2);
1396 PRODUCE_GLYPHS (&it2);
1397 it2_prev = it2;
1398 set_iterator_to_next (&it2, 1);
1399 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1400 && IT_CHARPOS (it2) < charpos);
1402 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1403 || it2_prev.current_x > it2_prev.last_visible_x)
1404 top_x = it.glyph_row->x;
1405 else
1407 top_x = it2_prev.current_x;
1408 top_y = it2_prev.current_y;
1412 else if (IT_CHARPOS (it) != charpos)
1414 Lisp_Object cpos = make_number (charpos);
1415 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1416 Lisp_Object string = string_from_display_spec (spec);
1417 struct text_pos tpos;
1418 int replacing_spec_p;
1419 bool newline_in_string
1420 = (STRINGP (string)
1421 && memchr (SDATA (string), '\n', SBYTES (string)));
1423 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1424 replacing_spec_p
1425 = (!NILP (spec)
1426 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1427 charpos, FRAME_WINDOW_P (it.f)));
1428 /* The tricky code below is needed because there's a
1429 discrepancy between move_it_to and how we set cursor
1430 when PT is at the beginning of a portion of text
1431 covered by a display property or an overlay with a
1432 display property, or the display line ends in a
1433 newline from a display string. move_it_to will stop
1434 _after_ such display strings, whereas
1435 set_cursor_from_row conspires with cursor_row_p to
1436 place the cursor on the first glyph produced from the
1437 display string. */
1439 /* We have overshoot PT because it is covered by a
1440 display property that replaces the text it covers.
1441 If the string includes embedded newlines, we are also
1442 in the wrong display line. Backtrack to the correct
1443 line, where the display property begins. */
1444 if (replacing_spec_p)
1446 Lisp_Object startpos, endpos;
1447 EMACS_INT start, end;
1448 struct it it3;
1449 int it3_moved;
1451 /* Find the first and the last buffer positions
1452 covered by the display string. */
1453 endpos =
1454 Fnext_single_char_property_change (cpos, Qdisplay,
1455 Qnil, Qnil);
1456 startpos =
1457 Fprevious_single_char_property_change (endpos, Qdisplay,
1458 Qnil, Qnil);
1459 start = XFASTINT (startpos);
1460 end = XFASTINT (endpos);
1461 /* Move to the last buffer position before the
1462 display property. */
1463 start_display (&it3, w, top);
1464 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1465 /* Move forward one more line if the position before
1466 the display string is a newline or if it is the
1467 rightmost character on a line that is
1468 continued or word-wrapped. */
1469 if (it3.method == GET_FROM_BUFFER
1470 && (it3.c == '\n'
1471 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1472 move_it_by_lines (&it3, 1);
1473 else if (move_it_in_display_line_to (&it3, -1,
1474 it3.current_x
1475 + it3.pixel_width,
1476 MOVE_TO_X)
1477 == MOVE_LINE_CONTINUED)
1479 move_it_by_lines (&it3, 1);
1480 /* When we are under word-wrap, the #$@%!
1481 move_it_by_lines moves 2 lines, so we need to
1482 fix that up. */
1483 if (it3.line_wrap == WORD_WRAP)
1484 move_it_by_lines (&it3, -1);
1487 /* Record the vertical coordinate of the display
1488 line where we wound up. */
1489 top_y = it3.current_y;
1490 if (it3.bidi_p)
1492 /* When characters are reordered for display,
1493 the character displayed to the left of the
1494 display string could be _after_ the display
1495 property in the logical order. Use the
1496 smallest vertical position of these two. */
1497 start_display (&it3, w, top);
1498 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1499 if (it3.current_y < top_y)
1500 top_y = it3.current_y;
1502 /* Move from the top of the window to the beginning
1503 of the display line where the display string
1504 begins. */
1505 start_display (&it3, w, top);
1506 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1507 /* If it3_moved stays zero after the 'while' loop
1508 below, that means we already were at a newline
1509 before the loop (e.g., the display string begins
1510 with a newline), so we don't need to (and cannot)
1511 inspect the glyphs of it3.glyph_row, because
1512 PRODUCE_GLYPHS will not produce anything for a
1513 newline, and thus it3.glyph_row stays at its
1514 stale content it got at top of the window. */
1515 it3_moved = 0;
1516 /* Finally, advance the iterator until we hit the
1517 first display element whose character position is
1518 CHARPOS, or until the first newline from the
1519 display string, which signals the end of the
1520 display line. */
1521 while (get_next_display_element (&it3))
1523 PRODUCE_GLYPHS (&it3);
1524 if (IT_CHARPOS (it3) == charpos
1525 || ITERATOR_AT_END_OF_LINE_P (&it3))
1526 break;
1527 it3_moved = 1;
1528 set_iterator_to_next (&it3, 0);
1530 top_x = it3.current_x - it3.pixel_width;
1531 /* Normally, we would exit the above loop because we
1532 found the display element whose character
1533 position is CHARPOS. For the contingency that we
1534 didn't, and stopped at the first newline from the
1535 display string, move back over the glyphs
1536 produced from the string, until we find the
1537 rightmost glyph not from the string. */
1538 if (it3_moved
1539 && newline_in_string
1540 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1542 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1543 + it3.glyph_row->used[TEXT_AREA];
1545 while (EQ ((g - 1)->object, string))
1547 --g;
1548 top_x -= g->pixel_width;
1550 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1551 + it3.glyph_row->used[TEXT_AREA]);
1556 *x = top_x;
1557 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1558 *rtop = max (0, window_top_y - top_y);
1559 *rbot = max (0, bottom_y - it.last_visible_y);
1560 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1561 - max (top_y, window_top_y)));
1562 *vpos = it.vpos;
1565 else
1567 /* We were asked to provide info about WINDOW_END. */
1568 struct it it2;
1569 void *it2data = NULL;
1571 SAVE_IT (it2, it, it2data);
1572 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1573 move_it_by_lines (&it, 1);
1574 if (charpos < IT_CHARPOS (it)
1575 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1577 visible_p = 1;
1578 RESTORE_IT (&it2, &it2, it2data);
1579 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1580 *x = it2.current_x;
1581 *y = it2.current_y + it2.max_ascent - it2.ascent;
1582 *rtop = max (0, -it2.current_y);
1583 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1584 - it.last_visible_y));
1585 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1586 it.last_visible_y)
1587 - max (it2.current_y,
1588 WINDOW_HEADER_LINE_HEIGHT (w))));
1589 *vpos = it2.vpos;
1591 else
1592 bidi_unshelve_cache (it2data, 1);
1594 bidi_unshelve_cache (itdata, 0);
1596 if (old_buffer)
1597 set_buffer_internal_1 (old_buffer);
1599 current_header_line_height = current_mode_line_height = -1;
1601 if (visible_p && w->hscroll > 0)
1602 *x -=
1603 window_hscroll_limited (w, WINDOW_XFRAME (w))
1604 * WINDOW_FRAME_COLUMN_WIDTH (w);
1606 #if 0
1607 /* Debugging code. */
1608 if (visible_p)
1609 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1610 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1611 else
1612 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1613 #endif
1615 return visible_p;
1619 /* Return the next character from STR. Return in *LEN the length of
1620 the character. This is like STRING_CHAR_AND_LENGTH but never
1621 returns an invalid character. If we find one, we return a `?', but
1622 with the length of the invalid character. */
1624 static int
1625 string_char_and_length (const unsigned char *str, int *len)
1627 int c;
1629 c = STRING_CHAR_AND_LENGTH (str, *len);
1630 if (!CHAR_VALID_P (c))
1631 /* We may not change the length here because other places in Emacs
1632 don't use this function, i.e. they silently accept invalid
1633 characters. */
1634 c = '?';
1636 return c;
1641 /* Given a position POS containing a valid character and byte position
1642 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1644 static struct text_pos
1645 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1647 eassert (STRINGP (string) && nchars >= 0);
1649 if (STRING_MULTIBYTE (string))
1651 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1652 int len;
1654 while (nchars--)
1656 string_char_and_length (p, &len);
1657 p += len;
1658 CHARPOS (pos) += 1;
1659 BYTEPOS (pos) += len;
1662 else
1663 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1665 return pos;
1669 /* Value is the text position, i.e. character and byte position,
1670 for character position CHARPOS in STRING. */
1672 static struct text_pos
1673 string_pos (ptrdiff_t charpos, Lisp_Object string)
1675 struct text_pos pos;
1676 eassert (STRINGP (string));
1677 eassert (charpos >= 0);
1678 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1679 return pos;
1683 /* Value is a text position, i.e. character and byte position, for
1684 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1685 means recognize multibyte characters. */
1687 static struct text_pos
1688 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1690 struct text_pos pos;
1692 eassert (s != NULL);
1693 eassert (charpos >= 0);
1695 if (multibyte_p)
1697 int len;
1699 SET_TEXT_POS (pos, 0, 0);
1700 while (charpos--)
1702 string_char_and_length ((const unsigned char *) s, &len);
1703 s += len;
1704 CHARPOS (pos) += 1;
1705 BYTEPOS (pos) += len;
1708 else
1709 SET_TEXT_POS (pos, charpos, charpos);
1711 return pos;
1715 /* Value is the number of characters in C string S. MULTIBYTE_P
1716 non-zero means recognize multibyte characters. */
1718 static ptrdiff_t
1719 number_of_chars (const char *s, bool multibyte_p)
1721 ptrdiff_t nchars;
1723 if (multibyte_p)
1725 ptrdiff_t rest = strlen (s);
1726 int len;
1727 const unsigned char *p = (const unsigned char *) s;
1729 for (nchars = 0; rest > 0; ++nchars)
1731 string_char_and_length (p, &len);
1732 rest -= len, p += len;
1735 else
1736 nchars = strlen (s);
1738 return nchars;
1742 /* Compute byte position NEWPOS->bytepos corresponding to
1743 NEWPOS->charpos. POS is a known position in string STRING.
1744 NEWPOS->charpos must be >= POS.charpos. */
1746 static void
1747 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1749 eassert (STRINGP (string));
1750 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1752 if (STRING_MULTIBYTE (string))
1753 *newpos = string_pos_nchars_ahead (pos, string,
1754 CHARPOS (*newpos) - CHARPOS (pos));
1755 else
1756 BYTEPOS (*newpos) = CHARPOS (*newpos);
1759 /* EXPORT:
1760 Return an estimation of the pixel height of mode or header lines on
1761 frame F. FACE_ID specifies what line's height to estimate. */
1764 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1766 #ifdef HAVE_WINDOW_SYSTEM
1767 if (FRAME_WINDOW_P (f))
1769 int height = FONT_HEIGHT (FRAME_FONT (f));
1771 /* This function is called so early when Emacs starts that the face
1772 cache and mode line face are not yet initialized. */
1773 if (FRAME_FACE_CACHE (f))
1775 struct face *face = FACE_FROM_ID (f, face_id);
1776 if (face)
1778 if (face->font)
1779 height = FONT_HEIGHT (face->font);
1780 if (face->box_line_width > 0)
1781 height += 2 * face->box_line_width;
1785 return height;
1787 #endif
1789 return 1;
1792 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1793 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1794 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1795 not force the value into range. */
1797 void
1798 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1799 int *x, int *y, NativeRectangle *bounds, int noclip)
1802 #ifdef HAVE_WINDOW_SYSTEM
1803 if (FRAME_WINDOW_P (f))
1805 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1806 even for negative values. */
1807 if (pix_x < 0)
1808 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1809 if (pix_y < 0)
1810 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1812 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1813 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1815 if (bounds)
1816 STORE_NATIVE_RECT (*bounds,
1817 FRAME_COL_TO_PIXEL_X (f, pix_x),
1818 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1819 FRAME_COLUMN_WIDTH (f) - 1,
1820 FRAME_LINE_HEIGHT (f) - 1);
1822 if (!noclip)
1824 if (pix_x < 0)
1825 pix_x = 0;
1826 else if (pix_x > FRAME_TOTAL_COLS (f))
1827 pix_x = FRAME_TOTAL_COLS (f);
1829 if (pix_y < 0)
1830 pix_y = 0;
1831 else if (pix_y > FRAME_LINES (f))
1832 pix_y = FRAME_LINES (f);
1835 #endif
1837 *x = pix_x;
1838 *y = pix_y;
1842 /* Find the glyph under window-relative coordinates X/Y in window W.
1843 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1844 strings. Return in *HPOS and *VPOS the row and column number of
1845 the glyph found. Return in *AREA the glyph area containing X.
1846 Value is a pointer to the glyph found or null if X/Y is not on
1847 text, or we can't tell because W's current matrix is not up to
1848 date. */
1850 static
1851 struct glyph *
1852 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1853 int *dx, int *dy, int *area)
1855 struct glyph *glyph, *end;
1856 struct glyph_row *row = NULL;
1857 int x0, i;
1859 /* Find row containing Y. Give up if some row is not enabled. */
1860 for (i = 0; i < w->current_matrix->nrows; ++i)
1862 row = MATRIX_ROW (w->current_matrix, i);
1863 if (!row->enabled_p)
1864 return NULL;
1865 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1866 break;
1869 *vpos = i;
1870 *hpos = 0;
1872 /* Give up if Y is not in the window. */
1873 if (i == w->current_matrix->nrows)
1874 return NULL;
1876 /* Get the glyph area containing X. */
1877 if (w->pseudo_window_p)
1879 *area = TEXT_AREA;
1880 x0 = 0;
1882 else
1884 if (x < window_box_left_offset (w, TEXT_AREA))
1886 *area = LEFT_MARGIN_AREA;
1887 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1889 else if (x < window_box_right_offset (w, TEXT_AREA))
1891 *area = TEXT_AREA;
1892 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1894 else
1896 *area = RIGHT_MARGIN_AREA;
1897 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1901 /* Find glyph containing X. */
1902 glyph = row->glyphs[*area];
1903 end = glyph + row->used[*area];
1904 x -= x0;
1905 while (glyph < end && x >= glyph->pixel_width)
1907 x -= glyph->pixel_width;
1908 ++glyph;
1911 if (glyph == end)
1912 return NULL;
1914 if (dx)
1916 *dx = x;
1917 *dy = y - (row->y + row->ascent - glyph->ascent);
1920 *hpos = glyph - row->glyphs[*area];
1921 return glyph;
1924 /* Convert frame-relative x/y to coordinates relative to window W.
1925 Takes pseudo-windows into account. */
1927 static void
1928 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1930 if (w->pseudo_window_p)
1932 /* A pseudo-window is always full-width, and starts at the
1933 left edge of the frame, plus a frame border. */
1934 struct frame *f = XFRAME (w->frame);
1935 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1936 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1938 else
1940 *x -= WINDOW_LEFT_EDGE_X (w);
1941 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1945 #ifdef HAVE_WINDOW_SYSTEM
1947 /* EXPORT:
1948 Return in RECTS[] at most N clipping rectangles for glyph string S.
1949 Return the number of stored rectangles. */
1952 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1954 XRectangle r;
1956 if (n <= 0)
1957 return 0;
1959 if (s->row->full_width_p)
1961 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1962 r.x = WINDOW_LEFT_EDGE_X (s->w);
1963 r.width = WINDOW_TOTAL_WIDTH (s->w);
1965 /* Unless displaying a mode or menu bar line, which are always
1966 fully visible, clip to the visible part of the row. */
1967 if (s->w->pseudo_window_p)
1968 r.height = s->row->visible_height;
1969 else
1970 r.height = s->height;
1972 else
1974 /* This is a text line that may be partially visible. */
1975 r.x = window_box_left (s->w, s->area);
1976 r.width = window_box_width (s->w, s->area);
1977 r.height = s->row->visible_height;
1980 if (s->clip_head)
1981 if (r.x < s->clip_head->x)
1983 if (r.width >= s->clip_head->x - r.x)
1984 r.width -= s->clip_head->x - r.x;
1985 else
1986 r.width = 0;
1987 r.x = s->clip_head->x;
1989 if (s->clip_tail)
1990 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1992 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1993 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1994 else
1995 r.width = 0;
1998 /* If S draws overlapping rows, it's sufficient to use the top and
1999 bottom of the window for clipping because this glyph string
2000 intentionally draws over other lines. */
2001 if (s->for_overlaps)
2003 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2004 r.height = window_text_bottom_y (s->w) - r.y;
2006 /* Alas, the above simple strategy does not work for the
2007 environments with anti-aliased text: if the same text is
2008 drawn onto the same place multiple times, it gets thicker.
2009 If the overlap we are processing is for the erased cursor, we
2010 take the intersection with the rectangle of the cursor. */
2011 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2013 XRectangle rc, r_save = r;
2015 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2016 rc.y = s->w->phys_cursor.y;
2017 rc.width = s->w->phys_cursor_width;
2018 rc.height = s->w->phys_cursor_height;
2020 x_intersect_rectangles (&r_save, &rc, &r);
2023 else
2025 /* Don't use S->y for clipping because it doesn't take partially
2026 visible lines into account. For example, it can be negative for
2027 partially visible lines at the top of a window. */
2028 if (!s->row->full_width_p
2029 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2030 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2031 else
2032 r.y = max (0, s->row->y);
2035 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2037 /* If drawing the cursor, don't let glyph draw outside its
2038 advertised boundaries. Cleartype does this under some circumstances. */
2039 if (s->hl == DRAW_CURSOR)
2041 struct glyph *glyph = s->first_glyph;
2042 int height, max_y;
2044 if (s->x > r.x)
2046 r.width -= s->x - r.x;
2047 r.x = s->x;
2049 r.width = min (r.width, glyph->pixel_width);
2051 /* If r.y is below window bottom, ensure that we still see a cursor. */
2052 height = min (glyph->ascent + glyph->descent,
2053 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2054 max_y = window_text_bottom_y (s->w) - height;
2055 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2056 if (s->ybase - glyph->ascent > max_y)
2058 r.y = max_y;
2059 r.height = height;
2061 else
2063 /* Don't draw cursor glyph taller than our actual glyph. */
2064 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2065 if (height < r.height)
2067 max_y = r.y + r.height;
2068 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2069 r.height = min (max_y - r.y, height);
2074 if (s->row->clip)
2076 XRectangle r_save = r;
2078 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2079 r.width = 0;
2082 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2083 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2085 #ifdef CONVERT_FROM_XRECT
2086 CONVERT_FROM_XRECT (r, *rects);
2087 #else
2088 *rects = r;
2089 #endif
2090 return 1;
2092 else
2094 /* If we are processing overlapping and allowed to return
2095 multiple clipping rectangles, we exclude the row of the glyph
2096 string from the clipping rectangle. This is to avoid drawing
2097 the same text on the environment with anti-aliasing. */
2098 #ifdef CONVERT_FROM_XRECT
2099 XRectangle rs[2];
2100 #else
2101 XRectangle *rs = rects;
2102 #endif
2103 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2105 if (s->for_overlaps & OVERLAPS_PRED)
2107 rs[i] = r;
2108 if (r.y + r.height > row_y)
2110 if (r.y < row_y)
2111 rs[i].height = row_y - r.y;
2112 else
2113 rs[i].height = 0;
2115 i++;
2117 if (s->for_overlaps & OVERLAPS_SUCC)
2119 rs[i] = r;
2120 if (r.y < row_y + s->row->visible_height)
2122 if (r.y + r.height > row_y + s->row->visible_height)
2124 rs[i].y = row_y + s->row->visible_height;
2125 rs[i].height = r.y + r.height - rs[i].y;
2127 else
2128 rs[i].height = 0;
2130 i++;
2133 n = i;
2134 #ifdef CONVERT_FROM_XRECT
2135 for (i = 0; i < n; i++)
2136 CONVERT_FROM_XRECT (rs[i], rects[i]);
2137 #endif
2138 return n;
2142 /* EXPORT:
2143 Return in *NR the clipping rectangle for glyph string S. */
2145 void
2146 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2148 get_glyph_string_clip_rects (s, nr, 1);
2152 /* EXPORT:
2153 Return the position and height of the phys cursor in window W.
2154 Set w->phys_cursor_width to width of phys cursor.
2157 void
2158 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2159 struct glyph *glyph, int *xp, int *yp, int *heightp)
2161 struct frame *f = XFRAME (WINDOW_FRAME (w));
2162 int x, y, wd, h, h0, y0;
2164 /* Compute the width of the rectangle to draw. If on a stretch
2165 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2166 rectangle as wide as the glyph, but use a canonical character
2167 width instead. */
2168 wd = glyph->pixel_width - 1;
2169 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2170 wd++; /* Why? */
2171 #endif
2173 x = w->phys_cursor.x;
2174 if (x < 0)
2176 wd += x;
2177 x = 0;
2180 if (glyph->type == STRETCH_GLYPH
2181 && !x_stretch_cursor_p)
2182 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2183 w->phys_cursor_width = wd;
2185 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2187 /* If y is below window bottom, ensure that we still see a cursor. */
2188 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2190 h = max (h0, glyph->ascent + glyph->descent);
2191 h0 = min (h0, glyph->ascent + glyph->descent);
2193 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2194 if (y < y0)
2196 h = max (h - (y0 - y) + 1, h0);
2197 y = y0 - 1;
2199 else
2201 y0 = window_text_bottom_y (w) - h0;
2202 if (y > y0)
2204 h += y - y0;
2205 y = y0;
2209 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2210 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2211 *heightp = h;
2215 * Remember which glyph the mouse is over.
2218 void
2219 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2221 Lisp_Object window;
2222 struct window *w;
2223 struct glyph_row *r, *gr, *end_row;
2224 enum window_part part;
2225 enum glyph_row_area area;
2226 int x, y, width, height;
2228 /* Try to determine frame pixel position and size of the glyph under
2229 frame pixel coordinates X/Y on frame F. */
2231 if (!f->glyphs_initialized_p
2232 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2233 NILP (window)))
2235 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2236 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2237 goto virtual_glyph;
2240 w = XWINDOW (window);
2241 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2242 height = WINDOW_FRAME_LINE_HEIGHT (w);
2244 x = window_relative_x_coord (w, part, gx);
2245 y = gy - WINDOW_TOP_EDGE_Y (w);
2247 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2248 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2250 if (w->pseudo_window_p)
2252 area = TEXT_AREA;
2253 part = ON_MODE_LINE; /* Don't adjust margin. */
2254 goto text_glyph;
2257 switch (part)
2259 case ON_LEFT_MARGIN:
2260 area = LEFT_MARGIN_AREA;
2261 goto text_glyph;
2263 case ON_RIGHT_MARGIN:
2264 area = RIGHT_MARGIN_AREA;
2265 goto text_glyph;
2267 case ON_HEADER_LINE:
2268 case ON_MODE_LINE:
2269 gr = (part == ON_HEADER_LINE
2270 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2271 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2272 gy = gr->y;
2273 area = TEXT_AREA;
2274 goto text_glyph_row_found;
2276 case ON_TEXT:
2277 area = TEXT_AREA;
2279 text_glyph:
2280 gr = 0; gy = 0;
2281 for (; r <= end_row && r->enabled_p; ++r)
2282 if (r->y + r->height > y)
2284 gr = r; gy = r->y;
2285 break;
2288 text_glyph_row_found:
2289 if (gr && gy <= y)
2291 struct glyph *g = gr->glyphs[area];
2292 struct glyph *end = g + gr->used[area];
2294 height = gr->height;
2295 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2296 if (gx + g->pixel_width > x)
2297 break;
2299 if (g < end)
2301 if (g->type == IMAGE_GLYPH)
2303 /* Don't remember when mouse is over image, as
2304 image may have hot-spots. */
2305 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2306 return;
2308 width = g->pixel_width;
2310 else
2312 /* Use nominal char spacing at end of line. */
2313 x -= gx;
2314 gx += (x / width) * width;
2317 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2318 gx += window_box_left_offset (w, area);
2320 else
2322 /* Use nominal line height at end of window. */
2323 gx = (x / width) * width;
2324 y -= gy;
2325 gy += (y / height) * height;
2327 break;
2329 case ON_LEFT_FRINGE:
2330 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2331 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2332 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2333 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2334 goto row_glyph;
2336 case ON_RIGHT_FRINGE:
2337 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2338 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2339 : window_box_right_offset (w, TEXT_AREA));
2340 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2341 goto row_glyph;
2343 case ON_SCROLL_BAR:
2344 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2346 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2347 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2348 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2349 : 0)));
2350 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2352 row_glyph:
2353 gr = 0, gy = 0;
2354 for (; r <= end_row && r->enabled_p; ++r)
2355 if (r->y + r->height > y)
2357 gr = r; gy = r->y;
2358 break;
2361 if (gr && gy <= y)
2362 height = gr->height;
2363 else
2365 /* Use nominal line height at end of window. */
2366 y -= gy;
2367 gy += (y / height) * height;
2369 break;
2371 default:
2373 virtual_glyph:
2374 /* If there is no glyph under the mouse, then we divide the screen
2375 into a grid of the smallest glyph in the frame, and use that
2376 as our "glyph". */
2378 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2379 round down even for negative values. */
2380 if (gx < 0)
2381 gx -= width - 1;
2382 if (gy < 0)
2383 gy -= height - 1;
2385 gx = (gx / width) * width;
2386 gy = (gy / height) * height;
2388 goto store_rect;
2391 gx += WINDOW_LEFT_EDGE_X (w);
2392 gy += WINDOW_TOP_EDGE_Y (w);
2394 store_rect:
2395 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2397 /* Visible feedback for debugging. */
2398 #if 0
2399 #if HAVE_X_WINDOWS
2400 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2401 f->output_data.x->normal_gc,
2402 gx, gy, width, height);
2403 #endif
2404 #endif
2408 #endif /* HAVE_WINDOW_SYSTEM */
2411 /***********************************************************************
2412 Lisp form evaluation
2413 ***********************************************************************/
2415 /* Error handler for safe_eval and safe_call. */
2417 static Lisp_Object
2418 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2420 add_to_log ("Error during redisplay: %S signaled %S",
2421 Flist (nargs, args), arg);
2422 return Qnil;
2425 /* Call function FUNC with the rest of NARGS - 1 arguments
2426 following. Return the result, or nil if something went
2427 wrong. Prevent redisplay during the evaluation. */
2429 Lisp_Object
2430 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2432 Lisp_Object val;
2434 if (inhibit_eval_during_redisplay)
2435 val = Qnil;
2436 else
2438 va_list ap;
2439 ptrdiff_t i;
2440 ptrdiff_t count = SPECPDL_INDEX ();
2441 struct gcpro gcpro1;
2442 Lisp_Object *args = alloca (nargs * word_size);
2444 args[0] = func;
2445 va_start (ap, func);
2446 for (i = 1; i < nargs; i++)
2447 args[i] = va_arg (ap, Lisp_Object);
2448 va_end (ap);
2450 GCPRO1 (args[0]);
2451 gcpro1.nvars = nargs;
2452 specbind (Qinhibit_redisplay, Qt);
2453 /* Use Qt to ensure debugger does not run,
2454 so there is no possibility of wanting to redisplay. */
2455 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2456 safe_eval_handler);
2457 UNGCPRO;
2458 val = unbind_to (count, val);
2461 return val;
2465 /* Call function FN with one argument ARG.
2466 Return the result, or nil if something went wrong. */
2468 Lisp_Object
2469 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2471 return safe_call (2, fn, arg);
2474 static Lisp_Object Qeval;
2476 Lisp_Object
2477 safe_eval (Lisp_Object sexpr)
2479 return safe_call1 (Qeval, sexpr);
2482 /* Call function FN with two arguments ARG1 and ARG2.
2483 Return the result, or nil if something went wrong. */
2485 Lisp_Object
2486 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2488 return safe_call (3, fn, arg1, arg2);
2493 /***********************************************************************
2494 Debugging
2495 ***********************************************************************/
2497 #if 0
2499 /* Define CHECK_IT to perform sanity checks on iterators.
2500 This is for debugging. It is too slow to do unconditionally. */
2502 static void
2503 check_it (struct it *it)
2505 if (it->method == GET_FROM_STRING)
2507 eassert (STRINGP (it->string));
2508 eassert (IT_STRING_CHARPOS (*it) >= 0);
2510 else
2512 eassert (IT_STRING_CHARPOS (*it) < 0);
2513 if (it->method == GET_FROM_BUFFER)
2515 /* Check that character and byte positions agree. */
2516 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2520 if (it->dpvec)
2521 eassert (it->current.dpvec_index >= 0);
2522 else
2523 eassert (it->current.dpvec_index < 0);
2526 #define CHECK_IT(IT) check_it ((IT))
2528 #else /* not 0 */
2530 #define CHECK_IT(IT) (void) 0
2532 #endif /* not 0 */
2535 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2537 /* Check that the window end of window W is what we expect it
2538 to be---the last row in the current matrix displaying text. */
2540 static void
2541 check_window_end (struct window *w)
2543 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2545 struct glyph_row *row;
2546 eassert ((row = MATRIX_ROW (w->current_matrix,
2547 XFASTINT (w->window_end_vpos)),
2548 !row->enabled_p
2549 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2550 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2554 #define CHECK_WINDOW_END(W) check_window_end ((W))
2556 #else
2558 #define CHECK_WINDOW_END(W) (void) 0
2560 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2562 /* Return mark position if current buffer has the region of non-zero length,
2563 or -1 otherwise. */
2565 static ptrdiff_t
2566 markpos_of_region (void)
2568 if (!NILP (Vtransient_mark_mode)
2569 && !NILP (BVAR (current_buffer, mark_active))
2570 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2572 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2574 if (markpos != PT)
2575 return markpos;
2577 return -1;
2580 /***********************************************************************
2581 Iterator initialization
2582 ***********************************************************************/
2584 /* Initialize IT for displaying current_buffer in window W, starting
2585 at character position CHARPOS. CHARPOS < 0 means that no buffer
2586 position is specified which is useful when the iterator is assigned
2587 a position later. BYTEPOS is the byte position corresponding to
2588 CHARPOS.
2590 If ROW is not null, calls to produce_glyphs with IT as parameter
2591 will produce glyphs in that row.
2593 BASE_FACE_ID is the id of a base face to use. It must be one of
2594 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2595 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2596 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2598 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2599 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2600 will be initialized to use the corresponding mode line glyph row of
2601 the desired matrix of W. */
2603 void
2604 init_iterator (struct it *it, struct window *w,
2605 ptrdiff_t charpos, ptrdiff_t bytepos,
2606 struct glyph_row *row, enum face_id base_face_id)
2608 ptrdiff_t markpos;
2609 enum face_id remapped_base_face_id = base_face_id;
2611 /* Some precondition checks. */
2612 eassert (w != NULL && it != NULL);
2613 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2614 && charpos <= ZV));
2616 /* If face attributes have been changed since the last redisplay,
2617 free realized faces now because they depend on face definitions
2618 that might have changed. Don't free faces while there might be
2619 desired matrices pending which reference these faces. */
2620 if (face_change_count && !inhibit_free_realized_faces)
2622 face_change_count = 0;
2623 free_all_realized_faces (Qnil);
2626 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2627 if (! NILP (Vface_remapping_alist))
2628 remapped_base_face_id
2629 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2631 /* Use one of the mode line rows of W's desired matrix if
2632 appropriate. */
2633 if (row == NULL)
2635 if (base_face_id == MODE_LINE_FACE_ID
2636 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2637 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2638 else if (base_face_id == HEADER_LINE_FACE_ID)
2639 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2642 /* Clear IT. */
2643 memset (it, 0, sizeof *it);
2644 it->current.overlay_string_index = -1;
2645 it->current.dpvec_index = -1;
2646 it->base_face_id = remapped_base_face_id;
2647 it->string = Qnil;
2648 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2649 it->paragraph_embedding = L2R;
2650 it->bidi_it.string.lstring = Qnil;
2651 it->bidi_it.string.s = NULL;
2652 it->bidi_it.string.bufpos = 0;
2653 it->bidi_it.w = w;
2655 /* The window in which we iterate over current_buffer: */
2656 XSETWINDOW (it->window, w);
2657 it->w = w;
2658 it->f = XFRAME (w->frame);
2660 it->cmp_it.id = -1;
2662 /* Extra space between lines (on window systems only). */
2663 if (base_face_id == DEFAULT_FACE_ID
2664 && FRAME_WINDOW_P (it->f))
2666 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2667 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2668 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2669 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2670 * FRAME_LINE_HEIGHT (it->f));
2671 else if (it->f->extra_line_spacing > 0)
2672 it->extra_line_spacing = it->f->extra_line_spacing;
2673 it->max_extra_line_spacing = 0;
2676 /* If realized faces have been removed, e.g. because of face
2677 attribute changes of named faces, recompute them. When running
2678 in batch mode, the face cache of the initial frame is null. If
2679 we happen to get called, make a dummy face cache. */
2680 if (FRAME_FACE_CACHE (it->f) == NULL)
2681 init_frame_faces (it->f);
2682 if (FRAME_FACE_CACHE (it->f)->used == 0)
2683 recompute_basic_faces (it->f);
2685 /* Current value of the `slice', `space-width', and 'height' properties. */
2686 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2687 it->space_width = Qnil;
2688 it->font_height = Qnil;
2689 it->override_ascent = -1;
2691 /* Are control characters displayed as `^C'? */
2692 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2694 /* -1 means everything between a CR and the following line end
2695 is invisible. >0 means lines indented more than this value are
2696 invisible. */
2697 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2698 ? (clip_to_bounds
2699 (-1, XINT (BVAR (current_buffer, selective_display)),
2700 PTRDIFF_MAX))
2701 : (!NILP (BVAR (current_buffer, selective_display))
2702 ? -1 : 0));
2703 it->selective_display_ellipsis_p
2704 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2706 /* Display table to use. */
2707 it->dp = window_display_table (w);
2709 /* Are multibyte characters enabled in current_buffer? */
2710 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2712 /* If visible region is of non-zero length, set IT->region_beg_charpos
2713 and IT->region_end_charpos to the start and end of a visible region
2714 in window IT->w. Set both to -1 to indicate no region. */
2715 markpos = markpos_of_region ();
2716 if (markpos >= 0
2717 /* Maybe highlight only in selected window. */
2718 && (/* Either show region everywhere. */
2719 highlight_nonselected_windows
2720 /* Or show region in the selected window. */
2721 || w == XWINDOW (selected_window)
2722 /* Or show the region if we are in the mini-buffer and W is
2723 the window the mini-buffer refers to. */
2724 || (MINI_WINDOW_P (XWINDOW (selected_window))
2725 && WINDOWP (minibuf_selected_window)
2726 && w == XWINDOW (minibuf_selected_window))))
2728 it->region_beg_charpos = min (PT, markpos);
2729 it->region_end_charpos = max (PT, markpos);
2731 else
2732 it->region_beg_charpos = it->region_end_charpos = -1;
2734 /* Get the position at which the redisplay_end_trigger hook should
2735 be run, if it is to be run at all. */
2736 if (MARKERP (w->redisplay_end_trigger)
2737 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2738 it->redisplay_end_trigger_charpos
2739 = marker_position (w->redisplay_end_trigger);
2740 else if (INTEGERP (w->redisplay_end_trigger))
2741 it->redisplay_end_trigger_charpos =
2742 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2744 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2746 /* Are lines in the display truncated? */
2747 if (base_face_id != DEFAULT_FACE_ID
2748 || it->w->hscroll
2749 || (! WINDOW_FULL_WIDTH_P (it->w)
2750 && ((!NILP (Vtruncate_partial_width_windows)
2751 && !INTEGERP (Vtruncate_partial_width_windows))
2752 || (INTEGERP (Vtruncate_partial_width_windows)
2753 && (WINDOW_TOTAL_COLS (it->w)
2754 < XINT (Vtruncate_partial_width_windows))))))
2755 it->line_wrap = TRUNCATE;
2756 else if (NILP (BVAR (current_buffer, truncate_lines)))
2757 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2758 ? WINDOW_WRAP : WORD_WRAP;
2759 else
2760 it->line_wrap = TRUNCATE;
2762 /* Get dimensions of truncation and continuation glyphs. These are
2763 displayed as fringe bitmaps under X, but we need them for such
2764 frames when the fringes are turned off. But leave the dimensions
2765 zero for tooltip frames, as these glyphs look ugly there and also
2766 sabotage calculations of tooltip dimensions in x-show-tip. */
2767 #ifdef HAVE_WINDOW_SYSTEM
2768 if (!(FRAME_WINDOW_P (it->f)
2769 && FRAMEP (tip_frame)
2770 && it->f == XFRAME (tip_frame)))
2771 #endif
2773 if (it->line_wrap == TRUNCATE)
2775 /* We will need the truncation glyph. */
2776 eassert (it->glyph_row == NULL);
2777 produce_special_glyphs (it, IT_TRUNCATION);
2778 it->truncation_pixel_width = it->pixel_width;
2780 else
2782 /* We will need the continuation glyph. */
2783 eassert (it->glyph_row == NULL);
2784 produce_special_glyphs (it, IT_CONTINUATION);
2785 it->continuation_pixel_width = it->pixel_width;
2789 /* Reset these values to zero because the produce_special_glyphs
2790 above has changed them. */
2791 it->pixel_width = it->ascent = it->descent = 0;
2792 it->phys_ascent = it->phys_descent = 0;
2794 /* Set this after getting the dimensions of truncation and
2795 continuation glyphs, so that we don't produce glyphs when calling
2796 produce_special_glyphs, above. */
2797 it->glyph_row = row;
2798 it->area = TEXT_AREA;
2800 /* Forget any previous info about this row being reversed. */
2801 if (it->glyph_row)
2802 it->glyph_row->reversed_p = 0;
2804 /* Get the dimensions of the display area. The display area
2805 consists of the visible window area plus a horizontally scrolled
2806 part to the left of the window. All x-values are relative to the
2807 start of this total display area. */
2808 if (base_face_id != DEFAULT_FACE_ID)
2810 /* Mode lines, menu bar in terminal frames. */
2811 it->first_visible_x = 0;
2812 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2814 else
2816 it->first_visible_x =
2817 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2818 it->last_visible_x = (it->first_visible_x
2819 + window_box_width (w, TEXT_AREA));
2821 /* If we truncate lines, leave room for the truncation glyph(s) at
2822 the right margin. Otherwise, leave room for the continuation
2823 glyph(s). Done only if the window has no fringes. Since we
2824 don't know at this point whether there will be any R2L lines in
2825 the window, we reserve space for truncation/continuation glyphs
2826 even if only one of the fringes is absent. */
2827 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2828 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2830 if (it->line_wrap == TRUNCATE)
2831 it->last_visible_x -= it->truncation_pixel_width;
2832 else
2833 it->last_visible_x -= it->continuation_pixel_width;
2836 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2837 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2840 /* Leave room for a border glyph. */
2841 if (!FRAME_WINDOW_P (it->f)
2842 && !WINDOW_RIGHTMOST_P (it->w))
2843 it->last_visible_x -= 1;
2845 it->last_visible_y = window_text_bottom_y (w);
2847 /* For mode lines and alike, arrange for the first glyph having a
2848 left box line if the face specifies a box. */
2849 if (base_face_id != DEFAULT_FACE_ID)
2851 struct face *face;
2853 it->face_id = remapped_base_face_id;
2855 /* If we have a boxed mode line, make the first character appear
2856 with a left box line. */
2857 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2858 if (face->box != FACE_NO_BOX)
2859 it->start_of_box_run_p = 1;
2862 /* If a buffer position was specified, set the iterator there,
2863 getting overlays and face properties from that position. */
2864 if (charpos >= BUF_BEG (current_buffer))
2866 it->end_charpos = ZV;
2867 eassert (charpos == BYTE_TO_CHAR (bytepos));
2868 IT_CHARPOS (*it) = charpos;
2869 IT_BYTEPOS (*it) = bytepos;
2871 /* We will rely on `reseat' to set this up properly, via
2872 handle_face_prop. */
2873 it->face_id = it->base_face_id;
2875 it->start = it->current;
2876 /* Do we need to reorder bidirectional text? Not if this is a
2877 unibyte buffer: by definition, none of the single-byte
2878 characters are strong R2L, so no reordering is needed. And
2879 bidi.c doesn't support unibyte buffers anyway. Also, don't
2880 reorder while we are loading loadup.el, since the tables of
2881 character properties needed for reordering are not yet
2882 available. */
2883 it->bidi_p =
2884 NILP (Vpurify_flag)
2885 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2886 && it->multibyte_p;
2888 /* If we are to reorder bidirectional text, init the bidi
2889 iterator. */
2890 if (it->bidi_p)
2892 /* Note the paragraph direction that this buffer wants to
2893 use. */
2894 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2895 Qleft_to_right))
2896 it->paragraph_embedding = L2R;
2897 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2898 Qright_to_left))
2899 it->paragraph_embedding = R2L;
2900 else
2901 it->paragraph_embedding = NEUTRAL_DIR;
2902 bidi_unshelve_cache (NULL, 0);
2903 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2904 &it->bidi_it);
2907 /* Compute faces etc. */
2908 reseat (it, it->current.pos, 1);
2911 CHECK_IT (it);
2915 /* Initialize IT for the display of window W with window start POS. */
2917 void
2918 start_display (struct it *it, struct window *w, struct text_pos pos)
2920 struct glyph_row *row;
2921 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2923 row = w->desired_matrix->rows + first_vpos;
2924 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2925 it->first_vpos = first_vpos;
2927 /* Don't reseat to previous visible line start if current start
2928 position is in a string or image. */
2929 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2931 int start_at_line_beg_p;
2932 int first_y = it->current_y;
2934 /* If window start is not at a line start, skip forward to POS to
2935 get the correct continuation lines width. */
2936 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2937 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2938 if (!start_at_line_beg_p)
2940 int new_x;
2942 reseat_at_previous_visible_line_start (it);
2943 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2945 new_x = it->current_x + it->pixel_width;
2947 /* If lines are continued, this line may end in the middle
2948 of a multi-glyph character (e.g. a control character
2949 displayed as \003, or in the middle of an overlay
2950 string). In this case move_it_to above will not have
2951 taken us to the start of the continuation line but to the
2952 end of the continued line. */
2953 if (it->current_x > 0
2954 && it->line_wrap != TRUNCATE /* Lines are continued. */
2955 && (/* And glyph doesn't fit on the line. */
2956 new_x > it->last_visible_x
2957 /* Or it fits exactly and we're on a window
2958 system frame. */
2959 || (new_x == it->last_visible_x
2960 && FRAME_WINDOW_P (it->f)
2961 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2962 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2963 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2965 if ((it->current.dpvec_index >= 0
2966 || it->current.overlay_string_index >= 0)
2967 /* If we are on a newline from a display vector or
2968 overlay string, then we are already at the end of
2969 a screen line; no need to go to the next line in
2970 that case, as this line is not really continued.
2971 (If we do go to the next line, C-e will not DTRT.) */
2972 && it->c != '\n')
2974 set_iterator_to_next (it, 1);
2975 move_it_in_display_line_to (it, -1, -1, 0);
2978 it->continuation_lines_width += it->current_x;
2980 /* If the character at POS is displayed via a display
2981 vector, move_it_to above stops at the final glyph of
2982 IT->dpvec. To make the caller redisplay that character
2983 again (a.k.a. start at POS), we need to reset the
2984 dpvec_index to the beginning of IT->dpvec. */
2985 else if (it->current.dpvec_index >= 0)
2986 it->current.dpvec_index = 0;
2988 /* We're starting a new display line, not affected by the
2989 height of the continued line, so clear the appropriate
2990 fields in the iterator structure. */
2991 it->max_ascent = it->max_descent = 0;
2992 it->max_phys_ascent = it->max_phys_descent = 0;
2994 it->current_y = first_y;
2995 it->vpos = 0;
2996 it->current_x = it->hpos = 0;
3002 /* Return 1 if POS is a position in ellipses displayed for invisible
3003 text. W is the window we display, for text property lookup. */
3005 static int
3006 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3008 Lisp_Object prop, window;
3009 int ellipses_p = 0;
3010 ptrdiff_t charpos = CHARPOS (pos->pos);
3012 /* If POS specifies a position in a display vector, this might
3013 be for an ellipsis displayed for invisible text. We won't
3014 get the iterator set up for delivering that ellipsis unless
3015 we make sure that it gets aware of the invisible text. */
3016 if (pos->dpvec_index >= 0
3017 && pos->overlay_string_index < 0
3018 && CHARPOS (pos->string_pos) < 0
3019 && charpos > BEGV
3020 && (XSETWINDOW (window, w),
3021 prop = Fget_char_property (make_number (charpos),
3022 Qinvisible, window),
3023 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3025 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3026 window);
3027 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3030 return ellipses_p;
3034 /* Initialize IT for stepping through current_buffer in window W,
3035 starting at position POS that includes overlay string and display
3036 vector/ control character translation position information. Value
3037 is zero if there are overlay strings with newlines at POS. */
3039 static int
3040 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3042 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3043 int i, overlay_strings_with_newlines = 0;
3045 /* If POS specifies a position in a display vector, this might
3046 be for an ellipsis displayed for invisible text. We won't
3047 get the iterator set up for delivering that ellipsis unless
3048 we make sure that it gets aware of the invisible text. */
3049 if (in_ellipses_for_invisible_text_p (pos, w))
3051 --charpos;
3052 bytepos = 0;
3055 /* Keep in mind: the call to reseat in init_iterator skips invisible
3056 text, so we might end up at a position different from POS. This
3057 is only a problem when POS is a row start after a newline and an
3058 overlay starts there with an after-string, and the overlay has an
3059 invisible property. Since we don't skip invisible text in
3060 display_line and elsewhere immediately after consuming the
3061 newline before the row start, such a POS will not be in a string,
3062 but the call to init_iterator below will move us to the
3063 after-string. */
3064 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3066 /* This only scans the current chunk -- it should scan all chunks.
3067 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3068 to 16 in 22.1 to make this a lesser problem. */
3069 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3071 const char *s = SSDATA (it->overlay_strings[i]);
3072 const char *e = s + SBYTES (it->overlay_strings[i]);
3074 while (s < e && *s != '\n')
3075 ++s;
3077 if (s < e)
3079 overlay_strings_with_newlines = 1;
3080 break;
3084 /* If position is within an overlay string, set up IT to the right
3085 overlay string. */
3086 if (pos->overlay_string_index >= 0)
3088 int relative_index;
3090 /* If the first overlay string happens to have a `display'
3091 property for an image, the iterator will be set up for that
3092 image, and we have to undo that setup first before we can
3093 correct the overlay string index. */
3094 if (it->method == GET_FROM_IMAGE)
3095 pop_it (it);
3097 /* We already have the first chunk of overlay strings in
3098 IT->overlay_strings. Load more until the one for
3099 pos->overlay_string_index is in IT->overlay_strings. */
3100 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3102 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3103 it->current.overlay_string_index = 0;
3104 while (n--)
3106 load_overlay_strings (it, 0);
3107 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3111 it->current.overlay_string_index = pos->overlay_string_index;
3112 relative_index = (it->current.overlay_string_index
3113 % OVERLAY_STRING_CHUNK_SIZE);
3114 it->string = it->overlay_strings[relative_index];
3115 eassert (STRINGP (it->string));
3116 it->current.string_pos = pos->string_pos;
3117 it->method = GET_FROM_STRING;
3118 it->end_charpos = SCHARS (it->string);
3119 /* Set up the bidi iterator for this overlay string. */
3120 if (it->bidi_p)
3122 it->bidi_it.string.lstring = it->string;
3123 it->bidi_it.string.s = NULL;
3124 it->bidi_it.string.schars = SCHARS (it->string);
3125 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3126 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3127 it->bidi_it.string.unibyte = !it->multibyte_p;
3128 it->bidi_it.w = it->w;
3129 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3130 FRAME_WINDOW_P (it->f), &it->bidi_it);
3132 /* Synchronize the state of the bidi iterator with
3133 pos->string_pos. For any string position other than
3134 zero, this will be done automagically when we resume
3135 iteration over the string and get_visually_first_element
3136 is called. But if string_pos is zero, and the string is
3137 to be reordered for display, we need to resync manually,
3138 since it could be that the iteration state recorded in
3139 pos ended at string_pos of 0 moving backwards in string. */
3140 if (CHARPOS (pos->string_pos) == 0)
3142 get_visually_first_element (it);
3143 if (IT_STRING_CHARPOS (*it) != 0)
3144 do {
3145 /* Paranoia. */
3146 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3147 bidi_move_to_visually_next (&it->bidi_it);
3148 } while (it->bidi_it.charpos != 0);
3150 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3151 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3155 if (CHARPOS (pos->string_pos) >= 0)
3157 /* Recorded position is not in an overlay string, but in another
3158 string. This can only be a string from a `display' property.
3159 IT should already be filled with that string. */
3160 it->current.string_pos = pos->string_pos;
3161 eassert (STRINGP (it->string));
3162 if (it->bidi_p)
3163 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3164 FRAME_WINDOW_P (it->f), &it->bidi_it);
3167 /* Restore position in display vector translations, control
3168 character translations or ellipses. */
3169 if (pos->dpvec_index >= 0)
3171 if (it->dpvec == NULL)
3172 get_next_display_element (it);
3173 eassert (it->dpvec && it->current.dpvec_index == 0);
3174 it->current.dpvec_index = pos->dpvec_index;
3177 CHECK_IT (it);
3178 return !overlay_strings_with_newlines;
3182 /* Initialize IT for stepping through current_buffer in window W
3183 starting at ROW->start. */
3185 static void
3186 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3188 init_from_display_pos (it, w, &row->start);
3189 it->start = row->start;
3190 it->continuation_lines_width = row->continuation_lines_width;
3191 CHECK_IT (it);
3195 /* Initialize IT for stepping through current_buffer in window W
3196 starting in the line following ROW, i.e. starting at ROW->end.
3197 Value is zero if there are overlay strings with newlines at ROW's
3198 end position. */
3200 static int
3201 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3203 int success = 0;
3205 if (init_from_display_pos (it, w, &row->end))
3207 if (row->continued_p)
3208 it->continuation_lines_width
3209 = row->continuation_lines_width + row->pixel_width;
3210 CHECK_IT (it);
3211 success = 1;
3214 return success;
3220 /***********************************************************************
3221 Text properties
3222 ***********************************************************************/
3224 /* Called when IT reaches IT->stop_charpos. Handle text property and
3225 overlay changes. Set IT->stop_charpos to the next position where
3226 to stop. */
3228 static void
3229 handle_stop (struct it *it)
3231 enum prop_handled handled;
3232 int handle_overlay_change_p;
3233 struct props *p;
3235 it->dpvec = NULL;
3236 it->current.dpvec_index = -1;
3237 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3238 it->ignore_overlay_strings_at_pos_p = 0;
3239 it->ellipsis_p = 0;
3241 /* Use face of preceding text for ellipsis (if invisible) */
3242 if (it->selective_display_ellipsis_p)
3243 it->saved_face_id = it->face_id;
3247 handled = HANDLED_NORMALLY;
3249 /* Call text property handlers. */
3250 for (p = it_props; p->handler; ++p)
3252 handled = p->handler (it);
3254 if (handled == HANDLED_RECOMPUTE_PROPS)
3255 break;
3256 else if (handled == HANDLED_RETURN)
3258 /* We still want to show before and after strings from
3259 overlays even if the actual buffer text is replaced. */
3260 if (!handle_overlay_change_p
3261 || it->sp > 1
3262 /* Don't call get_overlay_strings_1 if we already
3263 have overlay strings loaded, because doing so
3264 will load them again and push the iterator state
3265 onto the stack one more time, which is not
3266 expected by the rest of the code that processes
3267 overlay strings. */
3268 || (it->current.overlay_string_index < 0
3269 ? !get_overlay_strings_1 (it, 0, 0)
3270 : 0))
3272 if (it->ellipsis_p)
3273 setup_for_ellipsis (it, 0);
3274 /* When handling a display spec, we might load an
3275 empty string. In that case, discard it here. We
3276 used to discard it in handle_single_display_spec,
3277 but that causes get_overlay_strings_1, above, to
3278 ignore overlay strings that we must check. */
3279 if (STRINGP (it->string) && !SCHARS (it->string))
3280 pop_it (it);
3281 return;
3283 else if (STRINGP (it->string) && !SCHARS (it->string))
3284 pop_it (it);
3285 else
3287 it->ignore_overlay_strings_at_pos_p = 1;
3288 it->string_from_display_prop_p = 0;
3289 it->from_disp_prop_p = 0;
3290 handle_overlay_change_p = 0;
3292 handled = HANDLED_RECOMPUTE_PROPS;
3293 break;
3295 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3296 handle_overlay_change_p = 0;
3299 if (handled != HANDLED_RECOMPUTE_PROPS)
3301 /* Don't check for overlay strings below when set to deliver
3302 characters from a display vector. */
3303 if (it->method == GET_FROM_DISPLAY_VECTOR)
3304 handle_overlay_change_p = 0;
3306 /* Handle overlay changes.
3307 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3308 if it finds overlays. */
3309 if (handle_overlay_change_p)
3310 handled = handle_overlay_change (it);
3313 if (it->ellipsis_p)
3315 setup_for_ellipsis (it, 0);
3316 break;
3319 while (handled == HANDLED_RECOMPUTE_PROPS);
3321 /* Determine where to stop next. */
3322 if (handled == HANDLED_NORMALLY)
3323 compute_stop_pos (it);
3327 /* Compute IT->stop_charpos from text property and overlay change
3328 information for IT's current position. */
3330 static void
3331 compute_stop_pos (struct it *it)
3333 register INTERVAL iv, next_iv;
3334 Lisp_Object object, limit, position;
3335 ptrdiff_t charpos, bytepos;
3337 if (STRINGP (it->string))
3339 /* Strings are usually short, so don't limit the search for
3340 properties. */
3341 it->stop_charpos = it->end_charpos;
3342 object = it->string;
3343 limit = Qnil;
3344 charpos = IT_STRING_CHARPOS (*it);
3345 bytepos = IT_STRING_BYTEPOS (*it);
3347 else
3349 ptrdiff_t pos;
3351 /* If end_charpos is out of range for some reason, such as a
3352 misbehaving display function, rationalize it (Bug#5984). */
3353 if (it->end_charpos > ZV)
3354 it->end_charpos = ZV;
3355 it->stop_charpos = it->end_charpos;
3357 /* If next overlay change is in front of the current stop pos
3358 (which is IT->end_charpos), stop there. Note: value of
3359 next_overlay_change is point-max if no overlay change
3360 follows. */
3361 charpos = IT_CHARPOS (*it);
3362 bytepos = IT_BYTEPOS (*it);
3363 pos = next_overlay_change (charpos);
3364 if (pos < it->stop_charpos)
3365 it->stop_charpos = pos;
3367 /* If showing the region, we have to stop at the region
3368 start or end because the face might change there. */
3369 if (it->region_beg_charpos > 0)
3371 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3372 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3373 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3374 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3377 /* Set up variables for computing the stop position from text
3378 property changes. */
3379 XSETBUFFER (object, current_buffer);
3380 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3383 /* Get the interval containing IT's position. Value is a null
3384 interval if there isn't such an interval. */
3385 position = make_number (charpos);
3386 iv = validate_interval_range (object, &position, &position, 0);
3387 if (iv)
3389 Lisp_Object values_here[LAST_PROP_IDX];
3390 struct props *p;
3392 /* Get properties here. */
3393 for (p = it_props; p->handler; ++p)
3394 values_here[p->idx] = textget (iv->plist, *p->name);
3396 /* Look for an interval following iv that has different
3397 properties. */
3398 for (next_iv = next_interval (iv);
3399 (next_iv
3400 && (NILP (limit)
3401 || XFASTINT (limit) > next_iv->position));
3402 next_iv = next_interval (next_iv))
3404 for (p = it_props; p->handler; ++p)
3406 Lisp_Object new_value;
3408 new_value = textget (next_iv->plist, *p->name);
3409 if (!EQ (values_here[p->idx], new_value))
3410 break;
3413 if (p->handler)
3414 break;
3417 if (next_iv)
3419 if (INTEGERP (limit)
3420 && next_iv->position >= XFASTINT (limit))
3421 /* No text property change up to limit. */
3422 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3423 else
3424 /* Text properties change in next_iv. */
3425 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3429 if (it->cmp_it.id < 0)
3431 ptrdiff_t stoppos = it->end_charpos;
3433 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3434 stoppos = -1;
3435 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3436 stoppos, it->string);
3439 eassert (STRINGP (it->string)
3440 || (it->stop_charpos >= BEGV
3441 && it->stop_charpos >= IT_CHARPOS (*it)));
3445 /* Return the position of the next overlay change after POS in
3446 current_buffer. Value is point-max if no overlay change
3447 follows. This is like `next-overlay-change' but doesn't use
3448 xmalloc. */
3450 static ptrdiff_t
3451 next_overlay_change (ptrdiff_t pos)
3453 ptrdiff_t i, noverlays;
3454 ptrdiff_t endpos;
3455 Lisp_Object *overlays;
3457 /* Get all overlays at the given position. */
3458 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3460 /* If any of these overlays ends before endpos,
3461 use its ending point instead. */
3462 for (i = 0; i < noverlays; ++i)
3464 Lisp_Object oend;
3465 ptrdiff_t oendpos;
3467 oend = OVERLAY_END (overlays[i]);
3468 oendpos = OVERLAY_POSITION (oend);
3469 endpos = min (endpos, oendpos);
3472 return endpos;
3475 /* How many characters forward to search for a display property or
3476 display string. Searching too far forward makes the bidi display
3477 sluggish, especially in small windows. */
3478 #define MAX_DISP_SCAN 250
3480 /* Return the character position of a display string at or after
3481 position specified by POSITION. If no display string exists at or
3482 after POSITION, return ZV. A display string is either an overlay
3483 with `display' property whose value is a string, or a `display'
3484 text property whose value is a string. STRING is data about the
3485 string to iterate; if STRING->lstring is nil, we are iterating a
3486 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3487 on a GUI frame. DISP_PROP is set to zero if we searched
3488 MAX_DISP_SCAN characters forward without finding any display
3489 strings, non-zero otherwise. It is set to 2 if the display string
3490 uses any kind of `(space ...)' spec that will produce a stretch of
3491 white space in the text area. */
3492 ptrdiff_t
3493 compute_display_string_pos (struct text_pos *position,
3494 struct bidi_string_data *string,
3495 struct window *w,
3496 int frame_window_p, int *disp_prop)
3498 /* OBJECT = nil means current buffer. */
3499 Lisp_Object object, object1;
3500 Lisp_Object pos, spec, limpos;
3501 int string_p = (string && (STRINGP (string->lstring) || string->s));
3502 ptrdiff_t eob = string_p ? string->schars : ZV;
3503 ptrdiff_t begb = string_p ? 0 : BEGV;
3504 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3505 ptrdiff_t lim =
3506 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3507 struct text_pos tpos;
3508 int rv = 0;
3510 if (string && STRINGP (string->lstring))
3511 object1 = object = string->lstring;
3512 else if (w && !string_p)
3514 XSETWINDOW (object, w);
3515 object1 = Qnil;
3517 else
3518 object1 = object = Qnil;
3520 *disp_prop = 1;
3522 if (charpos >= eob
3523 /* We don't support display properties whose values are strings
3524 that have display string properties. */
3525 || string->from_disp_str
3526 /* C strings cannot have display properties. */
3527 || (string->s && !STRINGP (object)))
3529 *disp_prop = 0;
3530 return eob;
3533 /* If the character at CHARPOS is where the display string begins,
3534 return CHARPOS. */
3535 pos = make_number (charpos);
3536 if (STRINGP (object))
3537 bufpos = string->bufpos;
3538 else
3539 bufpos = charpos;
3540 tpos = *position;
3541 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3542 && (charpos <= begb
3543 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3544 object),
3545 spec))
3546 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3547 frame_window_p)))
3549 if (rv == 2)
3550 *disp_prop = 2;
3551 return charpos;
3554 /* Look forward for the first character with a `display' property
3555 that will replace the underlying text when displayed. */
3556 limpos = make_number (lim);
3557 do {
3558 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3559 CHARPOS (tpos) = XFASTINT (pos);
3560 if (CHARPOS (tpos) >= lim)
3562 *disp_prop = 0;
3563 break;
3565 if (STRINGP (object))
3566 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3567 else
3568 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3569 spec = Fget_char_property (pos, Qdisplay, object);
3570 if (!STRINGP (object))
3571 bufpos = CHARPOS (tpos);
3572 } while (NILP (spec)
3573 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3574 bufpos, frame_window_p)));
3575 if (rv == 2)
3576 *disp_prop = 2;
3578 return CHARPOS (tpos);
3581 /* Return the character position of the end of the display string that
3582 started at CHARPOS. If there's no display string at CHARPOS,
3583 return -1. A display string is either an overlay with `display'
3584 property whose value is a string or a `display' text property whose
3585 value is a string. */
3586 ptrdiff_t
3587 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3589 /* OBJECT = nil means current buffer. */
3590 Lisp_Object object =
3591 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3592 Lisp_Object pos = make_number (charpos);
3593 ptrdiff_t eob =
3594 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3596 if (charpos >= eob || (string->s && !STRINGP (object)))
3597 return eob;
3599 /* It could happen that the display property or overlay was removed
3600 since we found it in compute_display_string_pos above. One way
3601 this can happen is if JIT font-lock was called (through
3602 handle_fontified_prop), and jit-lock-functions remove text
3603 properties or overlays from the portion of buffer that includes
3604 CHARPOS. Muse mode is known to do that, for example. In this
3605 case, we return -1 to the caller, to signal that no display
3606 string is actually present at CHARPOS. See bidi_fetch_char for
3607 how this is handled.
3609 An alternative would be to never look for display properties past
3610 it->stop_charpos. But neither compute_display_string_pos nor
3611 bidi_fetch_char that calls it know or care where the next
3612 stop_charpos is. */
3613 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3614 return -1;
3616 /* Look forward for the first character where the `display' property
3617 changes. */
3618 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3620 return XFASTINT (pos);
3625 /***********************************************************************
3626 Fontification
3627 ***********************************************************************/
3629 /* Handle changes in the `fontified' property of the current buffer by
3630 calling hook functions from Qfontification_functions to fontify
3631 regions of text. */
3633 static enum prop_handled
3634 handle_fontified_prop (struct it *it)
3636 Lisp_Object prop, pos;
3637 enum prop_handled handled = HANDLED_NORMALLY;
3639 if (!NILP (Vmemory_full))
3640 return handled;
3642 /* Get the value of the `fontified' property at IT's current buffer
3643 position. (The `fontified' property doesn't have a special
3644 meaning in strings.) If the value is nil, call functions from
3645 Qfontification_functions. */
3646 if (!STRINGP (it->string)
3647 && it->s == NULL
3648 && !NILP (Vfontification_functions)
3649 && !NILP (Vrun_hooks)
3650 && (pos = make_number (IT_CHARPOS (*it)),
3651 prop = Fget_char_property (pos, Qfontified, Qnil),
3652 /* Ignore the special cased nil value always present at EOB since
3653 no amount of fontifying will be able to change it. */
3654 NILP (prop) && IT_CHARPOS (*it) < Z))
3656 ptrdiff_t count = SPECPDL_INDEX ();
3657 Lisp_Object val;
3658 struct buffer *obuf = current_buffer;
3659 int begv = BEGV, zv = ZV;
3660 int old_clip_changed = current_buffer->clip_changed;
3662 val = Vfontification_functions;
3663 specbind (Qfontification_functions, Qnil);
3665 eassert (it->end_charpos == ZV);
3667 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3668 safe_call1 (val, pos);
3669 else
3671 Lisp_Object fns, fn;
3672 struct gcpro gcpro1, gcpro2;
3674 fns = Qnil;
3675 GCPRO2 (val, fns);
3677 for (; CONSP (val); val = XCDR (val))
3679 fn = XCAR (val);
3681 if (EQ (fn, Qt))
3683 /* A value of t indicates this hook has a local
3684 binding; it means to run the global binding too.
3685 In a global value, t should not occur. If it
3686 does, we must ignore it to avoid an endless
3687 loop. */
3688 for (fns = Fdefault_value (Qfontification_functions);
3689 CONSP (fns);
3690 fns = XCDR (fns))
3692 fn = XCAR (fns);
3693 if (!EQ (fn, Qt))
3694 safe_call1 (fn, pos);
3697 else
3698 safe_call1 (fn, pos);
3701 UNGCPRO;
3704 unbind_to (count, Qnil);
3706 /* Fontification functions routinely call `save-restriction'.
3707 Normally, this tags clip_changed, which can confuse redisplay
3708 (see discussion in Bug#6671). Since we don't perform any
3709 special handling of fontification changes in the case where
3710 `save-restriction' isn't called, there's no point doing so in
3711 this case either. So, if the buffer's restrictions are
3712 actually left unchanged, reset clip_changed. */
3713 if (obuf == current_buffer)
3715 if (begv == BEGV && zv == ZV)
3716 current_buffer->clip_changed = old_clip_changed;
3718 /* There isn't much we can reasonably do to protect against
3719 misbehaving fontification, but here's a fig leaf. */
3720 else if (BUFFER_LIVE_P (obuf))
3721 set_buffer_internal_1 (obuf);
3723 /* The fontification code may have added/removed text.
3724 It could do even a lot worse, but let's at least protect against
3725 the most obvious case where only the text past `pos' gets changed',
3726 as is/was done in grep.el where some escapes sequences are turned
3727 into face properties (bug#7876). */
3728 it->end_charpos = ZV;
3730 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3731 something. This avoids an endless loop if they failed to
3732 fontify the text for which reason ever. */
3733 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3734 handled = HANDLED_RECOMPUTE_PROPS;
3737 return handled;
3742 /***********************************************************************
3743 Faces
3744 ***********************************************************************/
3746 /* Set up iterator IT from face properties at its current position.
3747 Called from handle_stop. */
3749 static enum prop_handled
3750 handle_face_prop (struct it *it)
3752 int new_face_id;
3753 ptrdiff_t next_stop;
3755 if (!STRINGP (it->string))
3757 new_face_id
3758 = face_at_buffer_position (it->w,
3759 IT_CHARPOS (*it),
3760 it->region_beg_charpos,
3761 it->region_end_charpos,
3762 &next_stop,
3763 (IT_CHARPOS (*it)
3764 + TEXT_PROP_DISTANCE_LIMIT),
3765 0, it->base_face_id);
3767 /* Is this a start of a run of characters with box face?
3768 Caveat: this can be called for a freshly initialized
3769 iterator; face_id is -1 in this case. We know that the new
3770 face will not change until limit, i.e. if the new face has a
3771 box, all characters up to limit will have one. But, as
3772 usual, we don't know whether limit is really the end. */
3773 if (new_face_id != it->face_id)
3775 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3776 /* If it->face_id is -1, old_face below will be NULL, see
3777 the definition of FACE_FROM_ID. This will happen if this
3778 is the initial call that gets the face. */
3779 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3781 /* If the value of face_id of the iterator is -1, we have to
3782 look in front of IT's position and see whether there is a
3783 face there that's different from new_face_id. */
3784 if (!old_face && IT_CHARPOS (*it) > BEG)
3786 int prev_face_id = face_before_it_pos (it);
3788 old_face = FACE_FROM_ID (it->f, prev_face_id);
3791 /* If the new face has a box, but the old face does not,
3792 this is the start of a run of characters with box face,
3793 i.e. this character has a shadow on the left side. */
3794 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3795 && (old_face == NULL || !old_face->box));
3796 it->face_box_p = new_face->box != FACE_NO_BOX;
3799 else
3801 int base_face_id;
3802 ptrdiff_t bufpos;
3803 int i;
3804 Lisp_Object from_overlay
3805 = (it->current.overlay_string_index >= 0
3806 ? it->string_overlays[it->current.overlay_string_index
3807 % OVERLAY_STRING_CHUNK_SIZE]
3808 : Qnil);
3810 /* See if we got to this string directly or indirectly from
3811 an overlay property. That includes the before-string or
3812 after-string of an overlay, strings in display properties
3813 provided by an overlay, their text properties, etc.
3815 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3816 if (! NILP (from_overlay))
3817 for (i = it->sp - 1; i >= 0; i--)
3819 if (it->stack[i].current.overlay_string_index >= 0)
3820 from_overlay
3821 = it->string_overlays[it->stack[i].current.overlay_string_index
3822 % OVERLAY_STRING_CHUNK_SIZE];
3823 else if (! NILP (it->stack[i].from_overlay))
3824 from_overlay = it->stack[i].from_overlay;
3826 if (!NILP (from_overlay))
3827 break;
3830 if (! NILP (from_overlay))
3832 bufpos = IT_CHARPOS (*it);
3833 /* For a string from an overlay, the base face depends
3834 only on text properties and ignores overlays. */
3835 base_face_id
3836 = face_for_overlay_string (it->w,
3837 IT_CHARPOS (*it),
3838 it->region_beg_charpos,
3839 it->region_end_charpos,
3840 &next_stop,
3841 (IT_CHARPOS (*it)
3842 + TEXT_PROP_DISTANCE_LIMIT),
3844 from_overlay);
3846 else
3848 bufpos = 0;
3850 /* For strings from a `display' property, use the face at
3851 IT's current buffer position as the base face to merge
3852 with, so that overlay strings appear in the same face as
3853 surrounding text, unless they specify their own
3854 faces. */
3855 base_face_id = it->string_from_prefix_prop_p
3856 ? DEFAULT_FACE_ID
3857 : underlying_face_id (it);
3860 new_face_id = face_at_string_position (it->w,
3861 it->string,
3862 IT_STRING_CHARPOS (*it),
3863 bufpos,
3864 it->region_beg_charpos,
3865 it->region_end_charpos,
3866 &next_stop,
3867 base_face_id, 0);
3869 /* Is this a start of a run of characters with box? Caveat:
3870 this can be called for a freshly allocated iterator; face_id
3871 is -1 is this case. We know that the new face will not
3872 change until the next check pos, i.e. if the new face has a
3873 box, all characters up to that position will have a
3874 box. But, as usual, we don't know whether that position
3875 is really the end. */
3876 if (new_face_id != it->face_id)
3878 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3879 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3881 /* If new face has a box but old face hasn't, this is the
3882 start of a run of characters with box, i.e. it has a
3883 shadow on the left side. */
3884 it->start_of_box_run_p
3885 = new_face->box && (old_face == NULL || !old_face->box);
3886 it->face_box_p = new_face->box != FACE_NO_BOX;
3890 it->face_id = new_face_id;
3891 return HANDLED_NORMALLY;
3895 /* Return the ID of the face ``underlying'' IT's current position,
3896 which is in a string. If the iterator is associated with a
3897 buffer, return the face at IT's current buffer position.
3898 Otherwise, use the iterator's base_face_id. */
3900 static int
3901 underlying_face_id (struct it *it)
3903 int face_id = it->base_face_id, i;
3905 eassert (STRINGP (it->string));
3907 for (i = it->sp - 1; i >= 0; --i)
3908 if (NILP (it->stack[i].string))
3909 face_id = it->stack[i].face_id;
3911 return face_id;
3915 /* Compute the face one character before or after the current position
3916 of IT, in the visual order. BEFORE_P non-zero means get the face
3917 in front (to the left in L2R paragraphs, to the right in R2L
3918 paragraphs) of IT's screen position. Value is the ID of the face. */
3920 static int
3921 face_before_or_after_it_pos (struct it *it, int before_p)
3923 int face_id, limit;
3924 ptrdiff_t next_check_charpos;
3925 struct it it_copy;
3926 void *it_copy_data = NULL;
3928 eassert (it->s == NULL);
3930 if (STRINGP (it->string))
3932 ptrdiff_t bufpos, charpos;
3933 int base_face_id;
3935 /* No face change past the end of the string (for the case
3936 we are padding with spaces). No face change before the
3937 string start. */
3938 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3939 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3940 return it->face_id;
3942 if (!it->bidi_p)
3944 /* Set charpos to the position before or after IT's current
3945 position, in the logical order, which in the non-bidi
3946 case is the same as the visual order. */
3947 if (before_p)
3948 charpos = IT_STRING_CHARPOS (*it) - 1;
3949 else if (it->what == IT_COMPOSITION)
3950 /* For composition, we must check the character after the
3951 composition. */
3952 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3953 else
3954 charpos = IT_STRING_CHARPOS (*it) + 1;
3956 else
3958 if (before_p)
3960 /* With bidi iteration, the character before the current
3961 in the visual order cannot be found by simple
3962 iteration, because "reverse" reordering is not
3963 supported. Instead, we need to use the move_it_*
3964 family of functions. */
3965 /* Ignore face changes before the first visible
3966 character on this display line. */
3967 if (it->current_x <= it->first_visible_x)
3968 return it->face_id;
3969 SAVE_IT (it_copy, *it, it_copy_data);
3970 /* Implementation note: Since move_it_in_display_line
3971 works in the iterator geometry, and thinks the first
3972 character is always the leftmost, even in R2L lines,
3973 we don't need to distinguish between the R2L and L2R
3974 cases here. */
3975 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3976 it_copy.current_x - 1, MOVE_TO_X);
3977 charpos = IT_STRING_CHARPOS (it_copy);
3978 RESTORE_IT (it, it, it_copy_data);
3980 else
3982 /* Set charpos to the string position of the character
3983 that comes after IT's current position in the visual
3984 order. */
3985 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3987 it_copy = *it;
3988 while (n--)
3989 bidi_move_to_visually_next (&it_copy.bidi_it);
3991 charpos = it_copy.bidi_it.charpos;
3994 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3996 if (it->current.overlay_string_index >= 0)
3997 bufpos = IT_CHARPOS (*it);
3998 else
3999 bufpos = 0;
4001 base_face_id = underlying_face_id (it);
4003 /* Get the face for ASCII, or unibyte. */
4004 face_id = face_at_string_position (it->w,
4005 it->string,
4006 charpos,
4007 bufpos,
4008 it->region_beg_charpos,
4009 it->region_end_charpos,
4010 &next_check_charpos,
4011 base_face_id, 0);
4013 /* Correct the face for charsets different from ASCII. Do it
4014 for the multibyte case only. The face returned above is
4015 suitable for unibyte text if IT->string is unibyte. */
4016 if (STRING_MULTIBYTE (it->string))
4018 struct text_pos pos1 = string_pos (charpos, it->string);
4019 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4020 int c, len;
4021 struct face *face = FACE_FROM_ID (it->f, face_id);
4023 c = string_char_and_length (p, &len);
4024 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4027 else
4029 struct text_pos pos;
4031 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4032 || (IT_CHARPOS (*it) <= BEGV && before_p))
4033 return it->face_id;
4035 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4036 pos = it->current.pos;
4038 if (!it->bidi_p)
4040 if (before_p)
4041 DEC_TEXT_POS (pos, it->multibyte_p);
4042 else
4044 if (it->what == IT_COMPOSITION)
4046 /* For composition, we must check the position after
4047 the composition. */
4048 pos.charpos += it->cmp_it.nchars;
4049 pos.bytepos += it->len;
4051 else
4052 INC_TEXT_POS (pos, it->multibyte_p);
4055 else
4057 if (before_p)
4059 /* With bidi iteration, the character before the current
4060 in the visual order cannot be found by simple
4061 iteration, because "reverse" reordering is not
4062 supported. Instead, we need to use the move_it_*
4063 family of functions. */
4064 /* Ignore face changes before the first visible
4065 character on this display line. */
4066 if (it->current_x <= it->first_visible_x)
4067 return it->face_id;
4068 SAVE_IT (it_copy, *it, it_copy_data);
4069 /* Implementation note: Since move_it_in_display_line
4070 works in the iterator geometry, and thinks the first
4071 character is always the leftmost, even in R2L lines,
4072 we don't need to distinguish between the R2L and L2R
4073 cases here. */
4074 move_it_in_display_line (&it_copy, ZV,
4075 it_copy.current_x - 1, MOVE_TO_X);
4076 pos = it_copy.current.pos;
4077 RESTORE_IT (it, it, it_copy_data);
4079 else
4081 /* Set charpos to the buffer position of the character
4082 that comes after IT's current position in the visual
4083 order. */
4084 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4086 it_copy = *it;
4087 while (n--)
4088 bidi_move_to_visually_next (&it_copy.bidi_it);
4090 SET_TEXT_POS (pos,
4091 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4094 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4096 /* Determine face for CHARSET_ASCII, or unibyte. */
4097 face_id = face_at_buffer_position (it->w,
4098 CHARPOS (pos),
4099 it->region_beg_charpos,
4100 it->region_end_charpos,
4101 &next_check_charpos,
4102 limit, 0, -1);
4104 /* Correct the face for charsets different from ASCII. Do it
4105 for the multibyte case only. The face returned above is
4106 suitable for unibyte text if current_buffer is unibyte. */
4107 if (it->multibyte_p)
4109 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4110 struct face *face = FACE_FROM_ID (it->f, face_id);
4111 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4115 return face_id;
4120 /***********************************************************************
4121 Invisible text
4122 ***********************************************************************/
4124 /* Set up iterator IT from invisible properties at its current
4125 position. Called from handle_stop. */
4127 static enum prop_handled
4128 handle_invisible_prop (struct it *it)
4130 enum prop_handled handled = HANDLED_NORMALLY;
4131 int invis_p;
4132 Lisp_Object prop;
4134 if (STRINGP (it->string))
4136 Lisp_Object end_charpos, limit, charpos;
4138 /* Get the value of the invisible text property at the
4139 current position. Value will be nil if there is no such
4140 property. */
4141 charpos = make_number (IT_STRING_CHARPOS (*it));
4142 prop = Fget_text_property (charpos, Qinvisible, it->string);
4143 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4145 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4147 /* Record whether we have to display an ellipsis for the
4148 invisible text. */
4149 int display_ellipsis_p = (invis_p == 2);
4150 ptrdiff_t len, endpos;
4152 handled = HANDLED_RECOMPUTE_PROPS;
4154 /* Get the position at which the next visible text can be
4155 found in IT->string, if any. */
4156 endpos = len = SCHARS (it->string);
4157 XSETINT (limit, len);
4160 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4161 it->string, limit);
4162 if (INTEGERP (end_charpos))
4164 endpos = XFASTINT (end_charpos);
4165 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4166 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4167 if (invis_p == 2)
4168 display_ellipsis_p = 1;
4171 while (invis_p && endpos < len);
4173 if (display_ellipsis_p)
4174 it->ellipsis_p = 1;
4176 if (endpos < len)
4178 /* Text at END_CHARPOS is visible. Move IT there. */
4179 struct text_pos old;
4180 ptrdiff_t oldpos;
4182 old = it->current.string_pos;
4183 oldpos = CHARPOS (old);
4184 if (it->bidi_p)
4186 if (it->bidi_it.first_elt
4187 && it->bidi_it.charpos < SCHARS (it->string))
4188 bidi_paragraph_init (it->paragraph_embedding,
4189 &it->bidi_it, 1);
4190 /* Bidi-iterate out of the invisible text. */
4193 bidi_move_to_visually_next (&it->bidi_it);
4195 while (oldpos <= it->bidi_it.charpos
4196 && it->bidi_it.charpos < endpos);
4198 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4199 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4200 if (IT_CHARPOS (*it) >= endpos)
4201 it->prev_stop = endpos;
4203 else
4205 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4206 compute_string_pos (&it->current.string_pos, old, it->string);
4209 else
4211 /* The rest of the string is invisible. If this is an
4212 overlay string, proceed with the next overlay string
4213 or whatever comes and return a character from there. */
4214 if (it->current.overlay_string_index >= 0
4215 && !display_ellipsis_p)
4217 next_overlay_string (it);
4218 /* Don't check for overlay strings when we just
4219 finished processing them. */
4220 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4222 else
4224 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4225 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4230 else
4232 ptrdiff_t newpos, next_stop, start_charpos, tem;
4233 Lisp_Object pos, overlay;
4235 /* First of all, is there invisible text at this position? */
4236 tem = start_charpos = IT_CHARPOS (*it);
4237 pos = make_number (tem);
4238 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4239 &overlay);
4240 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4242 /* If we are on invisible text, skip over it. */
4243 if (invis_p && start_charpos < it->end_charpos)
4245 /* Record whether we have to display an ellipsis for the
4246 invisible text. */
4247 int display_ellipsis_p = invis_p == 2;
4249 handled = HANDLED_RECOMPUTE_PROPS;
4251 /* Loop skipping over invisible text. The loop is left at
4252 ZV or with IT on the first char being visible again. */
4255 /* Try to skip some invisible text. Return value is the
4256 position reached which can be equal to where we start
4257 if there is nothing invisible there. This skips both
4258 over invisible text properties and overlays with
4259 invisible property. */
4260 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4262 /* If we skipped nothing at all we weren't at invisible
4263 text in the first place. If everything to the end of
4264 the buffer was skipped, end the loop. */
4265 if (newpos == tem || newpos >= ZV)
4266 invis_p = 0;
4267 else
4269 /* We skipped some characters but not necessarily
4270 all there are. Check if we ended up on visible
4271 text. Fget_char_property returns the property of
4272 the char before the given position, i.e. if we
4273 get invis_p = 0, this means that the char at
4274 newpos is visible. */
4275 pos = make_number (newpos);
4276 prop = Fget_char_property (pos, Qinvisible, it->window);
4277 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4280 /* If we ended up on invisible text, proceed to
4281 skip starting with next_stop. */
4282 if (invis_p)
4283 tem = next_stop;
4285 /* If there are adjacent invisible texts, don't lose the
4286 second one's ellipsis. */
4287 if (invis_p == 2)
4288 display_ellipsis_p = 1;
4290 while (invis_p);
4292 /* The position newpos is now either ZV or on visible text. */
4293 if (it->bidi_p)
4295 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4296 int on_newline =
4297 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4298 int after_newline =
4299 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4301 /* If the invisible text ends on a newline or on a
4302 character after a newline, we can avoid the costly,
4303 character by character, bidi iteration to NEWPOS, and
4304 instead simply reseat the iterator there. That's
4305 because all bidi reordering information is tossed at
4306 the newline. This is a big win for modes that hide
4307 complete lines, like Outline, Org, etc. */
4308 if (on_newline || after_newline)
4310 struct text_pos tpos;
4311 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4313 SET_TEXT_POS (tpos, newpos, bpos);
4314 reseat_1 (it, tpos, 0);
4315 /* If we reseat on a newline/ZV, we need to prep the
4316 bidi iterator for advancing to the next character
4317 after the newline/EOB, keeping the current paragraph
4318 direction (so that PRODUCE_GLYPHS does TRT wrt
4319 prepending/appending glyphs to a glyph row). */
4320 if (on_newline)
4322 it->bidi_it.first_elt = 0;
4323 it->bidi_it.paragraph_dir = pdir;
4324 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4325 it->bidi_it.nchars = 1;
4326 it->bidi_it.ch_len = 1;
4329 else /* Must use the slow method. */
4331 /* With bidi iteration, the region of invisible text
4332 could start and/or end in the middle of a
4333 non-base embedding level. Therefore, we need to
4334 skip invisible text using the bidi iterator,
4335 starting at IT's current position, until we find
4336 ourselves outside of the invisible text.
4337 Skipping invisible text _after_ bidi iteration
4338 avoids affecting the visual order of the
4339 displayed text when invisible properties are
4340 added or removed. */
4341 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4343 /* If we were `reseat'ed to a new paragraph,
4344 determine the paragraph base direction. We
4345 need to do it now because
4346 next_element_from_buffer may not have a
4347 chance to do it, if we are going to skip any
4348 text at the beginning, which resets the
4349 FIRST_ELT flag. */
4350 bidi_paragraph_init (it->paragraph_embedding,
4351 &it->bidi_it, 1);
4355 bidi_move_to_visually_next (&it->bidi_it);
4357 while (it->stop_charpos <= it->bidi_it.charpos
4358 && it->bidi_it.charpos < newpos);
4359 IT_CHARPOS (*it) = it->bidi_it.charpos;
4360 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4361 /* If we overstepped NEWPOS, record its position in
4362 the iterator, so that we skip invisible text if
4363 later the bidi iteration lands us in the
4364 invisible region again. */
4365 if (IT_CHARPOS (*it) >= newpos)
4366 it->prev_stop = newpos;
4369 else
4371 IT_CHARPOS (*it) = newpos;
4372 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4375 /* If there are before-strings at the start of invisible
4376 text, and the text is invisible because of a text
4377 property, arrange to show before-strings because 20.x did
4378 it that way. (If the text is invisible because of an
4379 overlay property instead of a text property, this is
4380 already handled in the overlay code.) */
4381 if (NILP (overlay)
4382 && get_overlay_strings (it, it->stop_charpos))
4384 handled = HANDLED_RECOMPUTE_PROPS;
4385 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4387 else if (display_ellipsis_p)
4389 /* Make sure that the glyphs of the ellipsis will get
4390 correct `charpos' values. If we would not update
4391 it->position here, the glyphs would belong to the
4392 last visible character _before_ the invisible
4393 text, which confuses `set_cursor_from_row'.
4395 We use the last invisible position instead of the
4396 first because this way the cursor is always drawn on
4397 the first "." of the ellipsis, whenever PT is inside
4398 the invisible text. Otherwise the cursor would be
4399 placed _after_ the ellipsis when the point is after the
4400 first invisible character. */
4401 if (!STRINGP (it->object))
4403 it->position.charpos = newpos - 1;
4404 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4406 it->ellipsis_p = 1;
4407 /* Let the ellipsis display before
4408 considering any properties of the following char.
4409 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4410 handled = HANDLED_RETURN;
4415 return handled;
4419 /* Make iterator IT return `...' next.
4420 Replaces LEN characters from buffer. */
4422 static void
4423 setup_for_ellipsis (struct it *it, int len)
4425 /* Use the display table definition for `...'. Invalid glyphs
4426 will be handled by the method returning elements from dpvec. */
4427 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4429 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4430 it->dpvec = v->contents;
4431 it->dpend = v->contents + v->header.size;
4433 else
4435 /* Default `...'. */
4436 it->dpvec = default_invis_vector;
4437 it->dpend = default_invis_vector + 3;
4440 it->dpvec_char_len = len;
4441 it->current.dpvec_index = 0;
4442 it->dpvec_face_id = -1;
4444 /* Remember the current face id in case glyphs specify faces.
4445 IT's face is restored in set_iterator_to_next.
4446 saved_face_id was set to preceding char's face in handle_stop. */
4447 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4448 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4450 it->method = GET_FROM_DISPLAY_VECTOR;
4451 it->ellipsis_p = 1;
4456 /***********************************************************************
4457 'display' property
4458 ***********************************************************************/
4460 /* Set up iterator IT from `display' property at its current position.
4461 Called from handle_stop.
4462 We return HANDLED_RETURN if some part of the display property
4463 overrides the display of the buffer text itself.
4464 Otherwise we return HANDLED_NORMALLY. */
4466 static enum prop_handled
4467 handle_display_prop (struct it *it)
4469 Lisp_Object propval, object, overlay;
4470 struct text_pos *position;
4471 ptrdiff_t bufpos;
4472 /* Nonzero if some property replaces the display of the text itself. */
4473 int display_replaced_p = 0;
4475 if (STRINGP (it->string))
4477 object = it->string;
4478 position = &it->current.string_pos;
4479 bufpos = CHARPOS (it->current.pos);
4481 else
4483 XSETWINDOW (object, it->w);
4484 position = &it->current.pos;
4485 bufpos = CHARPOS (*position);
4488 /* Reset those iterator values set from display property values. */
4489 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4490 it->space_width = Qnil;
4491 it->font_height = Qnil;
4492 it->voffset = 0;
4494 /* We don't support recursive `display' properties, i.e. string
4495 values that have a string `display' property, that have a string
4496 `display' property etc. */
4497 if (!it->string_from_display_prop_p)
4498 it->area = TEXT_AREA;
4500 propval = get_char_property_and_overlay (make_number (position->charpos),
4501 Qdisplay, object, &overlay);
4502 if (NILP (propval))
4503 return HANDLED_NORMALLY;
4504 /* Now OVERLAY is the overlay that gave us this property, or nil
4505 if it was a text property. */
4507 if (!STRINGP (it->string))
4508 object = it->w->contents;
4510 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4511 position, bufpos,
4512 FRAME_WINDOW_P (it->f));
4514 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4517 /* Subroutine of handle_display_prop. Returns non-zero if the display
4518 specification in SPEC is a replacing specification, i.e. it would
4519 replace the text covered by `display' property with something else,
4520 such as an image or a display string. If SPEC includes any kind or
4521 `(space ...) specification, the value is 2; this is used by
4522 compute_display_string_pos, which see.
4524 See handle_single_display_spec for documentation of arguments.
4525 frame_window_p is non-zero if the window being redisplayed is on a
4526 GUI frame; this argument is used only if IT is NULL, see below.
4528 IT can be NULL, if this is called by the bidi reordering code
4529 through compute_display_string_pos, which see. In that case, this
4530 function only examines SPEC, but does not otherwise "handle" it, in
4531 the sense that it doesn't set up members of IT from the display
4532 spec. */
4533 static int
4534 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4535 Lisp_Object overlay, struct text_pos *position,
4536 ptrdiff_t bufpos, int frame_window_p)
4538 int replacing_p = 0;
4539 int rv;
4541 if (CONSP (spec)
4542 /* Simple specifications. */
4543 && !EQ (XCAR (spec), Qimage)
4544 && !EQ (XCAR (spec), Qspace)
4545 && !EQ (XCAR (spec), Qwhen)
4546 && !EQ (XCAR (spec), Qslice)
4547 && !EQ (XCAR (spec), Qspace_width)
4548 && !EQ (XCAR (spec), Qheight)
4549 && !EQ (XCAR (spec), Qraise)
4550 /* Marginal area specifications. */
4551 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4552 && !EQ (XCAR (spec), Qleft_fringe)
4553 && !EQ (XCAR (spec), Qright_fringe)
4554 && !NILP (XCAR (spec)))
4556 for (; CONSP (spec); spec = XCDR (spec))
4558 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4559 overlay, position, bufpos,
4560 replacing_p, frame_window_p)))
4562 replacing_p = rv;
4563 /* If some text in a string is replaced, `position' no
4564 longer points to the position of `object'. */
4565 if (!it || STRINGP (object))
4566 break;
4570 else if (VECTORP (spec))
4572 ptrdiff_t i;
4573 for (i = 0; i < ASIZE (spec); ++i)
4574 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4575 overlay, position, bufpos,
4576 replacing_p, frame_window_p)))
4578 replacing_p = rv;
4579 /* If some text in a string is replaced, `position' no
4580 longer points to the position of `object'. */
4581 if (!it || STRINGP (object))
4582 break;
4585 else
4587 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4588 position, bufpos, 0,
4589 frame_window_p)))
4590 replacing_p = rv;
4593 return replacing_p;
4596 /* Value is the position of the end of the `display' property starting
4597 at START_POS in OBJECT. */
4599 static struct text_pos
4600 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4602 Lisp_Object end;
4603 struct text_pos end_pos;
4605 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4606 Qdisplay, object, Qnil);
4607 CHARPOS (end_pos) = XFASTINT (end);
4608 if (STRINGP (object))
4609 compute_string_pos (&end_pos, start_pos, it->string);
4610 else
4611 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4613 return end_pos;
4617 /* Set up IT from a single `display' property specification SPEC. OBJECT
4618 is the object in which the `display' property was found. *POSITION
4619 is the position in OBJECT at which the `display' property was found.
4620 BUFPOS is the buffer position of OBJECT (different from POSITION if
4621 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4622 previously saw a display specification which already replaced text
4623 display with something else, for example an image; we ignore such
4624 properties after the first one has been processed.
4626 OVERLAY is the overlay this `display' property came from,
4627 or nil if it was a text property.
4629 If SPEC is a `space' or `image' specification, and in some other
4630 cases too, set *POSITION to the position where the `display'
4631 property ends.
4633 If IT is NULL, only examine the property specification in SPEC, but
4634 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4635 is intended to be displayed in a window on a GUI frame.
4637 Value is non-zero if something was found which replaces the display
4638 of buffer or string text. */
4640 static int
4641 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4642 Lisp_Object overlay, struct text_pos *position,
4643 ptrdiff_t bufpos, int display_replaced_p,
4644 int frame_window_p)
4646 Lisp_Object form;
4647 Lisp_Object location, value;
4648 struct text_pos start_pos = *position;
4649 int valid_p;
4651 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4652 If the result is non-nil, use VALUE instead of SPEC. */
4653 form = Qt;
4654 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4656 spec = XCDR (spec);
4657 if (!CONSP (spec))
4658 return 0;
4659 form = XCAR (spec);
4660 spec = XCDR (spec);
4663 if (!NILP (form) && !EQ (form, Qt))
4665 ptrdiff_t count = SPECPDL_INDEX ();
4666 struct gcpro gcpro1;
4668 /* Bind `object' to the object having the `display' property, a
4669 buffer or string. Bind `position' to the position in the
4670 object where the property was found, and `buffer-position'
4671 to the current position in the buffer. */
4673 if (NILP (object))
4674 XSETBUFFER (object, current_buffer);
4675 specbind (Qobject, object);
4676 specbind (Qposition, make_number (CHARPOS (*position)));
4677 specbind (Qbuffer_position, make_number (bufpos));
4678 GCPRO1 (form);
4679 form = safe_eval (form);
4680 UNGCPRO;
4681 unbind_to (count, Qnil);
4684 if (NILP (form))
4685 return 0;
4687 /* Handle `(height HEIGHT)' specifications. */
4688 if (CONSP (spec)
4689 && EQ (XCAR (spec), Qheight)
4690 && CONSP (XCDR (spec)))
4692 if (it)
4694 if (!FRAME_WINDOW_P (it->f))
4695 return 0;
4697 it->font_height = XCAR (XCDR (spec));
4698 if (!NILP (it->font_height))
4700 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4701 int new_height = -1;
4703 if (CONSP (it->font_height)
4704 && (EQ (XCAR (it->font_height), Qplus)
4705 || EQ (XCAR (it->font_height), Qminus))
4706 && CONSP (XCDR (it->font_height))
4707 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4709 /* `(+ N)' or `(- N)' where N is an integer. */
4710 int steps = XINT (XCAR (XCDR (it->font_height)));
4711 if (EQ (XCAR (it->font_height), Qplus))
4712 steps = - steps;
4713 it->face_id = smaller_face (it->f, it->face_id, steps);
4715 else if (FUNCTIONP (it->font_height))
4717 /* Call function with current height as argument.
4718 Value is the new height. */
4719 Lisp_Object height;
4720 height = safe_call1 (it->font_height,
4721 face->lface[LFACE_HEIGHT_INDEX]);
4722 if (NUMBERP (height))
4723 new_height = XFLOATINT (height);
4725 else if (NUMBERP (it->font_height))
4727 /* Value is a multiple of the canonical char height. */
4728 struct face *f;
4730 f = FACE_FROM_ID (it->f,
4731 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4732 new_height = (XFLOATINT (it->font_height)
4733 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4735 else
4737 /* Evaluate IT->font_height with `height' bound to the
4738 current specified height to get the new height. */
4739 ptrdiff_t count = SPECPDL_INDEX ();
4741 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4742 value = safe_eval (it->font_height);
4743 unbind_to (count, Qnil);
4745 if (NUMBERP (value))
4746 new_height = XFLOATINT (value);
4749 if (new_height > 0)
4750 it->face_id = face_with_height (it->f, it->face_id, new_height);
4754 return 0;
4757 /* Handle `(space-width WIDTH)'. */
4758 if (CONSP (spec)
4759 && EQ (XCAR (spec), Qspace_width)
4760 && CONSP (XCDR (spec)))
4762 if (it)
4764 if (!FRAME_WINDOW_P (it->f))
4765 return 0;
4767 value = XCAR (XCDR (spec));
4768 if (NUMBERP (value) && XFLOATINT (value) > 0)
4769 it->space_width = value;
4772 return 0;
4775 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4776 if (CONSP (spec)
4777 && EQ (XCAR (spec), Qslice))
4779 Lisp_Object tem;
4781 if (it)
4783 if (!FRAME_WINDOW_P (it->f))
4784 return 0;
4786 if (tem = XCDR (spec), CONSP (tem))
4788 it->slice.x = XCAR (tem);
4789 if (tem = XCDR (tem), CONSP (tem))
4791 it->slice.y = XCAR (tem);
4792 if (tem = XCDR (tem), CONSP (tem))
4794 it->slice.width = XCAR (tem);
4795 if (tem = XCDR (tem), CONSP (tem))
4796 it->slice.height = XCAR (tem);
4802 return 0;
4805 /* Handle `(raise FACTOR)'. */
4806 if (CONSP (spec)
4807 && EQ (XCAR (spec), Qraise)
4808 && CONSP (XCDR (spec)))
4810 if (it)
4812 if (!FRAME_WINDOW_P (it->f))
4813 return 0;
4815 #ifdef HAVE_WINDOW_SYSTEM
4816 value = XCAR (XCDR (spec));
4817 if (NUMBERP (value))
4819 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4820 it->voffset = - (XFLOATINT (value)
4821 * (FONT_HEIGHT (face->font)));
4823 #endif /* HAVE_WINDOW_SYSTEM */
4826 return 0;
4829 /* Don't handle the other kinds of display specifications
4830 inside a string that we got from a `display' property. */
4831 if (it && it->string_from_display_prop_p)
4832 return 0;
4834 /* Characters having this form of property are not displayed, so
4835 we have to find the end of the property. */
4836 if (it)
4838 start_pos = *position;
4839 *position = display_prop_end (it, object, start_pos);
4841 value = Qnil;
4843 /* Stop the scan at that end position--we assume that all
4844 text properties change there. */
4845 if (it)
4846 it->stop_charpos = position->charpos;
4848 /* Handle `(left-fringe BITMAP [FACE])'
4849 and `(right-fringe BITMAP [FACE])'. */
4850 if (CONSP (spec)
4851 && (EQ (XCAR (spec), Qleft_fringe)
4852 || EQ (XCAR (spec), Qright_fringe))
4853 && CONSP (XCDR (spec)))
4855 int fringe_bitmap;
4857 if (it)
4859 if (!FRAME_WINDOW_P (it->f))
4860 /* If we return here, POSITION has been advanced
4861 across the text with this property. */
4863 /* Synchronize the bidi iterator with POSITION. This is
4864 needed because we are not going to push the iterator
4865 on behalf of this display property, so there will be
4866 no pop_it call to do this synchronization for us. */
4867 if (it->bidi_p)
4869 it->position = *position;
4870 iterate_out_of_display_property (it);
4871 *position = it->position;
4873 return 1;
4876 else if (!frame_window_p)
4877 return 1;
4879 #ifdef HAVE_WINDOW_SYSTEM
4880 value = XCAR (XCDR (spec));
4881 if (!SYMBOLP (value)
4882 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4883 /* If we return here, POSITION has been advanced
4884 across the text with this property. */
4886 if (it && it->bidi_p)
4888 it->position = *position;
4889 iterate_out_of_display_property (it);
4890 *position = it->position;
4892 return 1;
4895 if (it)
4897 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4899 if (CONSP (XCDR (XCDR (spec))))
4901 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4902 int face_id2 = lookup_derived_face (it->f, face_name,
4903 FRINGE_FACE_ID, 0);
4904 if (face_id2 >= 0)
4905 face_id = face_id2;
4908 /* Save current settings of IT so that we can restore them
4909 when we are finished with the glyph property value. */
4910 push_it (it, position);
4912 it->area = TEXT_AREA;
4913 it->what = IT_IMAGE;
4914 it->image_id = -1; /* no image */
4915 it->position = start_pos;
4916 it->object = NILP (object) ? it->w->contents : object;
4917 it->method = GET_FROM_IMAGE;
4918 it->from_overlay = Qnil;
4919 it->face_id = face_id;
4920 it->from_disp_prop_p = 1;
4922 /* Say that we haven't consumed the characters with
4923 `display' property yet. The call to pop_it in
4924 set_iterator_to_next will clean this up. */
4925 *position = start_pos;
4927 if (EQ (XCAR (spec), Qleft_fringe))
4929 it->left_user_fringe_bitmap = fringe_bitmap;
4930 it->left_user_fringe_face_id = face_id;
4932 else
4934 it->right_user_fringe_bitmap = fringe_bitmap;
4935 it->right_user_fringe_face_id = face_id;
4938 #endif /* HAVE_WINDOW_SYSTEM */
4939 return 1;
4942 /* Prepare to handle `((margin left-margin) ...)',
4943 `((margin right-margin) ...)' and `((margin nil) ...)'
4944 prefixes for display specifications. */
4945 location = Qunbound;
4946 if (CONSP (spec) && CONSP (XCAR (spec)))
4948 Lisp_Object tem;
4950 value = XCDR (spec);
4951 if (CONSP (value))
4952 value = XCAR (value);
4954 tem = XCAR (spec);
4955 if (EQ (XCAR (tem), Qmargin)
4956 && (tem = XCDR (tem),
4957 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4958 (NILP (tem)
4959 || EQ (tem, Qleft_margin)
4960 || EQ (tem, Qright_margin))))
4961 location = tem;
4964 if (EQ (location, Qunbound))
4966 location = Qnil;
4967 value = spec;
4970 /* After this point, VALUE is the property after any
4971 margin prefix has been stripped. It must be a string,
4972 an image specification, or `(space ...)'.
4974 LOCATION specifies where to display: `left-margin',
4975 `right-margin' or nil. */
4977 valid_p = (STRINGP (value)
4978 #ifdef HAVE_WINDOW_SYSTEM
4979 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4980 && valid_image_p (value))
4981 #endif /* not HAVE_WINDOW_SYSTEM */
4982 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4984 if (valid_p && !display_replaced_p)
4986 int retval = 1;
4988 if (!it)
4990 /* Callers need to know whether the display spec is any kind
4991 of `(space ...)' spec that is about to affect text-area
4992 display. */
4993 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4994 retval = 2;
4995 return retval;
4998 /* Save current settings of IT so that we can restore them
4999 when we are finished with the glyph property value. */
5000 push_it (it, position);
5001 it->from_overlay = overlay;
5002 it->from_disp_prop_p = 1;
5004 if (NILP (location))
5005 it->area = TEXT_AREA;
5006 else if (EQ (location, Qleft_margin))
5007 it->area = LEFT_MARGIN_AREA;
5008 else
5009 it->area = RIGHT_MARGIN_AREA;
5011 if (STRINGP (value))
5013 it->string = value;
5014 it->multibyte_p = STRING_MULTIBYTE (it->string);
5015 it->current.overlay_string_index = -1;
5016 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5017 it->end_charpos = it->string_nchars = SCHARS (it->string);
5018 it->method = GET_FROM_STRING;
5019 it->stop_charpos = 0;
5020 it->prev_stop = 0;
5021 it->base_level_stop = 0;
5022 it->string_from_display_prop_p = 1;
5023 /* Say that we haven't consumed the characters with
5024 `display' property yet. The call to pop_it in
5025 set_iterator_to_next will clean this up. */
5026 if (BUFFERP (object))
5027 *position = start_pos;
5029 /* Force paragraph direction to be that of the parent
5030 object. If the parent object's paragraph direction is
5031 not yet determined, default to L2R. */
5032 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5033 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5034 else
5035 it->paragraph_embedding = L2R;
5037 /* Set up the bidi iterator for this display string. */
5038 if (it->bidi_p)
5040 it->bidi_it.string.lstring = it->string;
5041 it->bidi_it.string.s = NULL;
5042 it->bidi_it.string.schars = it->end_charpos;
5043 it->bidi_it.string.bufpos = bufpos;
5044 it->bidi_it.string.from_disp_str = 1;
5045 it->bidi_it.string.unibyte = !it->multibyte_p;
5046 it->bidi_it.w = it->w;
5047 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5050 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5052 it->method = GET_FROM_STRETCH;
5053 it->object = value;
5054 *position = it->position = start_pos;
5055 retval = 1 + (it->area == TEXT_AREA);
5057 #ifdef HAVE_WINDOW_SYSTEM
5058 else
5060 it->what = IT_IMAGE;
5061 it->image_id = lookup_image (it->f, value);
5062 it->position = start_pos;
5063 it->object = NILP (object) ? it->w->contents : object;
5064 it->method = GET_FROM_IMAGE;
5066 /* Say that we haven't consumed the characters with
5067 `display' property yet. The call to pop_it in
5068 set_iterator_to_next will clean this up. */
5069 *position = start_pos;
5071 #endif /* HAVE_WINDOW_SYSTEM */
5073 return retval;
5076 /* Invalid property or property not supported. Restore
5077 POSITION to what it was before. */
5078 *position = start_pos;
5079 return 0;
5082 /* Check if PROP is a display property value whose text should be
5083 treated as intangible. OVERLAY is the overlay from which PROP
5084 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5085 specify the buffer position covered by PROP. */
5088 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5089 ptrdiff_t charpos, ptrdiff_t bytepos)
5091 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5092 struct text_pos position;
5094 SET_TEXT_POS (position, charpos, bytepos);
5095 return handle_display_spec (NULL, prop, Qnil, overlay,
5096 &position, charpos, frame_window_p);
5100 /* Return 1 if PROP is a display sub-property value containing STRING.
5102 Implementation note: this and the following function are really
5103 special cases of handle_display_spec and
5104 handle_single_display_spec, and should ideally use the same code.
5105 Until they do, these two pairs must be consistent and must be
5106 modified in sync. */
5108 static int
5109 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5111 if (EQ (string, prop))
5112 return 1;
5114 /* Skip over `when FORM'. */
5115 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5117 prop = XCDR (prop);
5118 if (!CONSP (prop))
5119 return 0;
5120 /* Actually, the condition following `when' should be eval'ed,
5121 like handle_single_display_spec does, and we should return
5122 zero if it evaluates to nil. However, this function is
5123 called only when the buffer was already displayed and some
5124 glyph in the glyph matrix was found to come from a display
5125 string. Therefore, the condition was already evaluated, and
5126 the result was non-nil, otherwise the display string wouldn't
5127 have been displayed and we would have never been called for
5128 this property. Thus, we can skip the evaluation and assume
5129 its result is non-nil. */
5130 prop = XCDR (prop);
5133 if (CONSP (prop))
5134 /* Skip over `margin LOCATION'. */
5135 if (EQ (XCAR (prop), Qmargin))
5137 prop = XCDR (prop);
5138 if (!CONSP (prop))
5139 return 0;
5141 prop = XCDR (prop);
5142 if (!CONSP (prop))
5143 return 0;
5146 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5150 /* Return 1 if STRING appears in the `display' property PROP. */
5152 static int
5153 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5155 if (CONSP (prop)
5156 && !EQ (XCAR (prop), Qwhen)
5157 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5159 /* A list of sub-properties. */
5160 while (CONSP (prop))
5162 if (single_display_spec_string_p (XCAR (prop), string))
5163 return 1;
5164 prop = XCDR (prop);
5167 else if (VECTORP (prop))
5169 /* A vector of sub-properties. */
5170 ptrdiff_t i;
5171 for (i = 0; i < ASIZE (prop); ++i)
5172 if (single_display_spec_string_p (AREF (prop, i), string))
5173 return 1;
5175 else
5176 return single_display_spec_string_p (prop, string);
5178 return 0;
5181 /* Look for STRING in overlays and text properties in the current
5182 buffer, between character positions FROM and TO (excluding TO).
5183 BACK_P non-zero means look back (in this case, TO is supposed to be
5184 less than FROM).
5185 Value is the first character position where STRING was found, or
5186 zero if it wasn't found before hitting TO.
5188 This function may only use code that doesn't eval because it is
5189 called asynchronously from note_mouse_highlight. */
5191 static ptrdiff_t
5192 string_buffer_position_lim (Lisp_Object string,
5193 ptrdiff_t from, ptrdiff_t to, int back_p)
5195 Lisp_Object limit, prop, pos;
5196 int found = 0;
5198 pos = make_number (max (from, BEGV));
5200 if (!back_p) /* looking forward */
5202 limit = make_number (min (to, ZV));
5203 while (!found && !EQ (pos, limit))
5205 prop = Fget_char_property (pos, Qdisplay, Qnil);
5206 if (!NILP (prop) && display_prop_string_p (prop, string))
5207 found = 1;
5208 else
5209 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5210 limit);
5213 else /* looking back */
5215 limit = make_number (max (to, BEGV));
5216 while (!found && !EQ (pos, limit))
5218 prop = Fget_char_property (pos, Qdisplay, Qnil);
5219 if (!NILP (prop) && display_prop_string_p (prop, string))
5220 found = 1;
5221 else
5222 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5223 limit);
5227 return found ? XINT (pos) : 0;
5230 /* Determine which buffer position in current buffer STRING comes from.
5231 AROUND_CHARPOS is an approximate position where it could come from.
5232 Value is the buffer position or 0 if it couldn't be determined.
5234 This function is necessary because we don't record buffer positions
5235 in glyphs generated from strings (to keep struct glyph small).
5236 This function may only use code that doesn't eval because it is
5237 called asynchronously from note_mouse_highlight. */
5239 static ptrdiff_t
5240 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5242 const int MAX_DISTANCE = 1000;
5243 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5244 around_charpos + MAX_DISTANCE,
5247 if (!found)
5248 found = string_buffer_position_lim (string, around_charpos,
5249 around_charpos - MAX_DISTANCE, 1);
5250 return found;
5255 /***********************************************************************
5256 `composition' property
5257 ***********************************************************************/
5259 /* Set up iterator IT from `composition' property at its current
5260 position. Called from handle_stop. */
5262 static enum prop_handled
5263 handle_composition_prop (struct it *it)
5265 Lisp_Object prop, string;
5266 ptrdiff_t pos, pos_byte, start, end;
5268 if (STRINGP (it->string))
5270 unsigned char *s;
5272 pos = IT_STRING_CHARPOS (*it);
5273 pos_byte = IT_STRING_BYTEPOS (*it);
5274 string = it->string;
5275 s = SDATA (string) + pos_byte;
5276 it->c = STRING_CHAR (s);
5278 else
5280 pos = IT_CHARPOS (*it);
5281 pos_byte = IT_BYTEPOS (*it);
5282 string = Qnil;
5283 it->c = FETCH_CHAR (pos_byte);
5286 /* If there's a valid composition and point is not inside of the
5287 composition (in the case that the composition is from the current
5288 buffer), draw a glyph composed from the composition components. */
5289 if (find_composition (pos, -1, &start, &end, &prop, string)
5290 && COMPOSITION_VALID_P (start, end, prop)
5291 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5293 if (start < pos)
5294 /* As we can't handle this situation (perhaps font-lock added
5295 a new composition), we just return here hoping that next
5296 redisplay will detect this composition much earlier. */
5297 return HANDLED_NORMALLY;
5298 if (start != pos)
5300 if (STRINGP (it->string))
5301 pos_byte = string_char_to_byte (it->string, start);
5302 else
5303 pos_byte = CHAR_TO_BYTE (start);
5305 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5306 prop, string);
5308 if (it->cmp_it.id >= 0)
5310 it->cmp_it.ch = -1;
5311 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5312 it->cmp_it.nglyphs = -1;
5316 return HANDLED_NORMALLY;
5321 /***********************************************************************
5322 Overlay strings
5323 ***********************************************************************/
5325 /* The following structure is used to record overlay strings for
5326 later sorting in load_overlay_strings. */
5328 struct overlay_entry
5330 Lisp_Object overlay;
5331 Lisp_Object string;
5332 EMACS_INT priority;
5333 int after_string_p;
5337 /* Set up iterator IT from overlay strings at its current position.
5338 Called from handle_stop. */
5340 static enum prop_handled
5341 handle_overlay_change (struct it *it)
5343 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5344 return HANDLED_RECOMPUTE_PROPS;
5345 else
5346 return HANDLED_NORMALLY;
5350 /* Set up the next overlay string for delivery by IT, if there is an
5351 overlay string to deliver. Called by set_iterator_to_next when the
5352 end of the current overlay string is reached. If there are more
5353 overlay strings to display, IT->string and
5354 IT->current.overlay_string_index are set appropriately here.
5355 Otherwise IT->string is set to nil. */
5357 static void
5358 next_overlay_string (struct it *it)
5360 ++it->current.overlay_string_index;
5361 if (it->current.overlay_string_index == it->n_overlay_strings)
5363 /* No more overlay strings. Restore IT's settings to what
5364 they were before overlay strings were processed, and
5365 continue to deliver from current_buffer. */
5367 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5368 pop_it (it);
5369 eassert (it->sp > 0
5370 || (NILP (it->string)
5371 && it->method == GET_FROM_BUFFER
5372 && it->stop_charpos >= BEGV
5373 && it->stop_charpos <= it->end_charpos));
5374 it->current.overlay_string_index = -1;
5375 it->n_overlay_strings = 0;
5376 it->overlay_strings_charpos = -1;
5377 /* If there's an empty display string on the stack, pop the
5378 stack, to resync the bidi iterator with IT's position. Such
5379 empty strings are pushed onto the stack in
5380 get_overlay_strings_1. */
5381 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5382 pop_it (it);
5384 /* If we're at the end of the buffer, record that we have
5385 processed the overlay strings there already, so that
5386 next_element_from_buffer doesn't try it again. */
5387 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5388 it->overlay_strings_at_end_processed_p = 1;
5390 else
5392 /* There are more overlay strings to process. If
5393 IT->current.overlay_string_index has advanced to a position
5394 where we must load IT->overlay_strings with more strings, do
5395 it. We must load at the IT->overlay_strings_charpos where
5396 IT->n_overlay_strings was originally computed; when invisible
5397 text is present, this might not be IT_CHARPOS (Bug#7016). */
5398 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5400 if (it->current.overlay_string_index && i == 0)
5401 load_overlay_strings (it, it->overlay_strings_charpos);
5403 /* Initialize IT to deliver display elements from the overlay
5404 string. */
5405 it->string = it->overlay_strings[i];
5406 it->multibyte_p = STRING_MULTIBYTE (it->string);
5407 SET_TEXT_POS (it->current.string_pos, 0, 0);
5408 it->method = GET_FROM_STRING;
5409 it->stop_charpos = 0;
5410 it->end_charpos = SCHARS (it->string);
5411 if (it->cmp_it.stop_pos >= 0)
5412 it->cmp_it.stop_pos = 0;
5413 it->prev_stop = 0;
5414 it->base_level_stop = 0;
5416 /* Set up the bidi iterator for this overlay string. */
5417 if (it->bidi_p)
5419 it->bidi_it.string.lstring = it->string;
5420 it->bidi_it.string.s = NULL;
5421 it->bidi_it.string.schars = SCHARS (it->string);
5422 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5423 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5424 it->bidi_it.string.unibyte = !it->multibyte_p;
5425 it->bidi_it.w = it->w;
5426 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5430 CHECK_IT (it);
5434 /* Compare two overlay_entry structures E1 and E2. Used as a
5435 comparison function for qsort in load_overlay_strings. Overlay
5436 strings for the same position are sorted so that
5438 1. All after-strings come in front of before-strings, except
5439 when they come from the same overlay.
5441 2. Within after-strings, strings are sorted so that overlay strings
5442 from overlays with higher priorities come first.
5444 2. Within before-strings, strings are sorted so that overlay
5445 strings from overlays with higher priorities come last.
5447 Value is analogous to strcmp. */
5450 static int
5451 compare_overlay_entries (const void *e1, const void *e2)
5453 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5454 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5455 int result;
5457 if (entry1->after_string_p != entry2->after_string_p)
5459 /* Let after-strings appear in front of before-strings if
5460 they come from different overlays. */
5461 if (EQ (entry1->overlay, entry2->overlay))
5462 result = entry1->after_string_p ? 1 : -1;
5463 else
5464 result = entry1->after_string_p ? -1 : 1;
5466 else if (entry1->priority != entry2->priority)
5468 if (entry1->after_string_p)
5469 /* After-strings sorted in order of decreasing priority. */
5470 result = entry2->priority < entry1->priority ? -1 : 1;
5471 else
5472 /* Before-strings sorted in order of increasing priority. */
5473 result = entry1->priority < entry2->priority ? -1 : 1;
5475 else
5476 result = 0;
5478 return result;
5482 /* Load the vector IT->overlay_strings with overlay strings from IT's
5483 current buffer position, or from CHARPOS if that is > 0. Set
5484 IT->n_overlays to the total number of overlay strings found.
5486 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5487 a time. On entry into load_overlay_strings,
5488 IT->current.overlay_string_index gives the number of overlay
5489 strings that have already been loaded by previous calls to this
5490 function.
5492 IT->add_overlay_start contains an additional overlay start
5493 position to consider for taking overlay strings from, if non-zero.
5494 This position comes into play when the overlay has an `invisible'
5495 property, and both before and after-strings. When we've skipped to
5496 the end of the overlay, because of its `invisible' property, we
5497 nevertheless want its before-string to appear.
5498 IT->add_overlay_start will contain the overlay start position
5499 in this case.
5501 Overlay strings are sorted so that after-string strings come in
5502 front of before-string strings. Within before and after-strings,
5503 strings are sorted by overlay priority. See also function
5504 compare_overlay_entries. */
5506 static void
5507 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5509 Lisp_Object overlay, window, str, invisible;
5510 struct Lisp_Overlay *ov;
5511 ptrdiff_t start, end;
5512 ptrdiff_t size = 20;
5513 ptrdiff_t n = 0, i, j;
5514 int invis_p;
5515 struct overlay_entry *entries = alloca (size * sizeof *entries);
5516 USE_SAFE_ALLOCA;
5518 if (charpos <= 0)
5519 charpos = IT_CHARPOS (*it);
5521 /* Append the overlay string STRING of overlay OVERLAY to vector
5522 `entries' which has size `size' and currently contains `n'
5523 elements. AFTER_P non-zero means STRING is an after-string of
5524 OVERLAY. */
5525 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5526 do \
5528 Lisp_Object priority; \
5530 if (n == size) \
5532 struct overlay_entry *old = entries; \
5533 SAFE_NALLOCA (entries, 2, size); \
5534 memcpy (entries, old, size * sizeof *entries); \
5535 size *= 2; \
5538 entries[n].string = (STRING); \
5539 entries[n].overlay = (OVERLAY); \
5540 priority = Foverlay_get ((OVERLAY), Qpriority); \
5541 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5542 entries[n].after_string_p = (AFTER_P); \
5543 ++n; \
5545 while (0)
5547 /* Process overlay before the overlay center. */
5548 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5550 XSETMISC (overlay, ov);
5551 eassert (OVERLAYP (overlay));
5552 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5553 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5555 if (end < charpos)
5556 break;
5558 /* Skip this overlay if it doesn't start or end at IT's current
5559 position. */
5560 if (end != charpos && start != charpos)
5561 continue;
5563 /* Skip this overlay if it doesn't apply to IT->w. */
5564 window = Foverlay_get (overlay, Qwindow);
5565 if (WINDOWP (window) && XWINDOW (window) != it->w)
5566 continue;
5568 /* If the text ``under'' the overlay is invisible, both before-
5569 and after-strings from this overlay are visible; start and
5570 end position are indistinguishable. */
5571 invisible = Foverlay_get (overlay, Qinvisible);
5572 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5574 /* If overlay has a non-empty before-string, record it. */
5575 if ((start == charpos || (end == charpos && invis_p))
5576 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5577 && SCHARS (str))
5578 RECORD_OVERLAY_STRING (overlay, str, 0);
5580 /* If overlay has a non-empty after-string, record it. */
5581 if ((end == charpos || (start == charpos && invis_p))
5582 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5583 && SCHARS (str))
5584 RECORD_OVERLAY_STRING (overlay, str, 1);
5587 /* Process overlays after the overlay center. */
5588 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5590 XSETMISC (overlay, ov);
5591 eassert (OVERLAYP (overlay));
5592 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5593 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5595 if (start > charpos)
5596 break;
5598 /* Skip this overlay if it doesn't start or end at IT's current
5599 position. */
5600 if (end != charpos && start != charpos)
5601 continue;
5603 /* Skip this overlay if it doesn't apply to IT->w. */
5604 window = Foverlay_get (overlay, Qwindow);
5605 if (WINDOWP (window) && XWINDOW (window) != it->w)
5606 continue;
5608 /* If the text ``under'' the overlay is invisible, it has a zero
5609 dimension, and both before- and after-strings apply. */
5610 invisible = Foverlay_get (overlay, Qinvisible);
5611 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5613 /* If overlay has a non-empty before-string, record it. */
5614 if ((start == charpos || (end == charpos && invis_p))
5615 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5616 && SCHARS (str))
5617 RECORD_OVERLAY_STRING (overlay, str, 0);
5619 /* If overlay has a non-empty after-string, record it. */
5620 if ((end == charpos || (start == charpos && invis_p))
5621 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5622 && SCHARS (str))
5623 RECORD_OVERLAY_STRING (overlay, str, 1);
5626 #undef RECORD_OVERLAY_STRING
5628 /* Sort entries. */
5629 if (n > 1)
5630 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5632 /* Record number of overlay strings, and where we computed it. */
5633 it->n_overlay_strings = n;
5634 it->overlay_strings_charpos = charpos;
5636 /* IT->current.overlay_string_index is the number of overlay strings
5637 that have already been consumed by IT. Copy some of the
5638 remaining overlay strings to IT->overlay_strings. */
5639 i = 0;
5640 j = it->current.overlay_string_index;
5641 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5643 it->overlay_strings[i] = entries[j].string;
5644 it->string_overlays[i++] = entries[j++].overlay;
5647 CHECK_IT (it);
5648 SAFE_FREE ();
5652 /* Get the first chunk of overlay strings at IT's current buffer
5653 position, or at CHARPOS if that is > 0. Value is non-zero if at
5654 least one overlay string was found. */
5656 static int
5657 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5659 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5660 process. This fills IT->overlay_strings with strings, and sets
5661 IT->n_overlay_strings to the total number of strings to process.
5662 IT->pos.overlay_string_index has to be set temporarily to zero
5663 because load_overlay_strings needs this; it must be set to -1
5664 when no overlay strings are found because a zero value would
5665 indicate a position in the first overlay string. */
5666 it->current.overlay_string_index = 0;
5667 load_overlay_strings (it, charpos);
5669 /* If we found overlay strings, set up IT to deliver display
5670 elements from the first one. Otherwise set up IT to deliver
5671 from current_buffer. */
5672 if (it->n_overlay_strings)
5674 /* Make sure we know settings in current_buffer, so that we can
5675 restore meaningful values when we're done with the overlay
5676 strings. */
5677 if (compute_stop_p)
5678 compute_stop_pos (it);
5679 eassert (it->face_id >= 0);
5681 /* Save IT's settings. They are restored after all overlay
5682 strings have been processed. */
5683 eassert (!compute_stop_p || it->sp == 0);
5685 /* When called from handle_stop, there might be an empty display
5686 string loaded. In that case, don't bother saving it. But
5687 don't use this optimization with the bidi iterator, since we
5688 need the corresponding pop_it call to resync the bidi
5689 iterator's position with IT's position, after we are done
5690 with the overlay strings. (The corresponding call to pop_it
5691 in case of an empty display string is in
5692 next_overlay_string.) */
5693 if (!(!it->bidi_p
5694 && STRINGP (it->string) && !SCHARS (it->string)))
5695 push_it (it, NULL);
5697 /* Set up IT to deliver display elements from the first overlay
5698 string. */
5699 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5700 it->string = it->overlay_strings[0];
5701 it->from_overlay = Qnil;
5702 it->stop_charpos = 0;
5703 eassert (STRINGP (it->string));
5704 it->end_charpos = SCHARS (it->string);
5705 it->prev_stop = 0;
5706 it->base_level_stop = 0;
5707 it->multibyte_p = STRING_MULTIBYTE (it->string);
5708 it->method = GET_FROM_STRING;
5709 it->from_disp_prop_p = 0;
5711 /* Force paragraph direction to be that of the parent
5712 buffer. */
5713 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5714 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5715 else
5716 it->paragraph_embedding = L2R;
5718 /* Set up the bidi iterator for this overlay string. */
5719 if (it->bidi_p)
5721 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5723 it->bidi_it.string.lstring = it->string;
5724 it->bidi_it.string.s = NULL;
5725 it->bidi_it.string.schars = SCHARS (it->string);
5726 it->bidi_it.string.bufpos = pos;
5727 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5728 it->bidi_it.string.unibyte = !it->multibyte_p;
5729 it->bidi_it.w = it->w;
5730 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5732 return 1;
5735 it->current.overlay_string_index = -1;
5736 return 0;
5739 static int
5740 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5742 it->string = Qnil;
5743 it->method = GET_FROM_BUFFER;
5745 (void) get_overlay_strings_1 (it, charpos, 1);
5747 CHECK_IT (it);
5749 /* Value is non-zero if we found at least one overlay string. */
5750 return STRINGP (it->string);
5755 /***********************************************************************
5756 Saving and restoring state
5757 ***********************************************************************/
5759 /* Save current settings of IT on IT->stack. Called, for example,
5760 before setting up IT for an overlay string, to be able to restore
5761 IT's settings to what they were after the overlay string has been
5762 processed. If POSITION is non-NULL, it is the position to save on
5763 the stack instead of IT->position. */
5765 static void
5766 push_it (struct it *it, struct text_pos *position)
5768 struct iterator_stack_entry *p;
5770 eassert (it->sp < IT_STACK_SIZE);
5771 p = it->stack + it->sp;
5773 p->stop_charpos = it->stop_charpos;
5774 p->prev_stop = it->prev_stop;
5775 p->base_level_stop = it->base_level_stop;
5776 p->cmp_it = it->cmp_it;
5777 eassert (it->face_id >= 0);
5778 p->face_id = it->face_id;
5779 p->string = it->string;
5780 p->method = it->method;
5781 p->from_overlay = it->from_overlay;
5782 switch (p->method)
5784 case GET_FROM_IMAGE:
5785 p->u.image.object = it->object;
5786 p->u.image.image_id = it->image_id;
5787 p->u.image.slice = it->slice;
5788 break;
5789 case GET_FROM_STRETCH:
5790 p->u.stretch.object = it->object;
5791 break;
5793 p->position = position ? *position : it->position;
5794 p->current = it->current;
5795 p->end_charpos = it->end_charpos;
5796 p->string_nchars = it->string_nchars;
5797 p->area = it->area;
5798 p->multibyte_p = it->multibyte_p;
5799 p->avoid_cursor_p = it->avoid_cursor_p;
5800 p->space_width = it->space_width;
5801 p->font_height = it->font_height;
5802 p->voffset = it->voffset;
5803 p->string_from_display_prop_p = it->string_from_display_prop_p;
5804 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5805 p->display_ellipsis_p = 0;
5806 p->line_wrap = it->line_wrap;
5807 p->bidi_p = it->bidi_p;
5808 p->paragraph_embedding = it->paragraph_embedding;
5809 p->from_disp_prop_p = it->from_disp_prop_p;
5810 ++it->sp;
5812 /* Save the state of the bidi iterator as well. */
5813 if (it->bidi_p)
5814 bidi_push_it (&it->bidi_it);
5817 static void
5818 iterate_out_of_display_property (struct it *it)
5820 int buffer_p = !STRINGP (it->string);
5821 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5822 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5824 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5826 /* Maybe initialize paragraph direction. If we are at the beginning
5827 of a new paragraph, next_element_from_buffer may not have a
5828 chance to do that. */
5829 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5830 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5831 /* prev_stop can be zero, so check against BEGV as well. */
5832 while (it->bidi_it.charpos >= bob
5833 && it->prev_stop <= it->bidi_it.charpos
5834 && it->bidi_it.charpos < CHARPOS (it->position)
5835 && it->bidi_it.charpos < eob)
5836 bidi_move_to_visually_next (&it->bidi_it);
5837 /* Record the stop_pos we just crossed, for when we cross it
5838 back, maybe. */
5839 if (it->bidi_it.charpos > CHARPOS (it->position))
5840 it->prev_stop = CHARPOS (it->position);
5841 /* If we ended up not where pop_it put us, resync IT's
5842 positional members with the bidi iterator. */
5843 if (it->bidi_it.charpos != CHARPOS (it->position))
5844 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5845 if (buffer_p)
5846 it->current.pos = it->position;
5847 else
5848 it->current.string_pos = it->position;
5851 /* Restore IT's settings from IT->stack. Called, for example, when no
5852 more overlay strings must be processed, and we return to delivering
5853 display elements from a buffer, or when the end of a string from a
5854 `display' property is reached and we return to delivering display
5855 elements from an overlay string, or from a buffer. */
5857 static void
5858 pop_it (struct it *it)
5860 struct iterator_stack_entry *p;
5861 int from_display_prop = it->from_disp_prop_p;
5863 eassert (it->sp > 0);
5864 --it->sp;
5865 p = it->stack + it->sp;
5866 it->stop_charpos = p->stop_charpos;
5867 it->prev_stop = p->prev_stop;
5868 it->base_level_stop = p->base_level_stop;
5869 it->cmp_it = p->cmp_it;
5870 it->face_id = p->face_id;
5871 it->current = p->current;
5872 it->position = p->position;
5873 it->string = p->string;
5874 it->from_overlay = p->from_overlay;
5875 if (NILP (it->string))
5876 SET_TEXT_POS (it->current.string_pos, -1, -1);
5877 it->method = p->method;
5878 switch (it->method)
5880 case GET_FROM_IMAGE:
5881 it->image_id = p->u.image.image_id;
5882 it->object = p->u.image.object;
5883 it->slice = p->u.image.slice;
5884 break;
5885 case GET_FROM_STRETCH:
5886 it->object = p->u.stretch.object;
5887 break;
5888 case GET_FROM_BUFFER:
5889 it->object = it->w->contents;
5890 break;
5891 case GET_FROM_STRING:
5892 it->object = it->string;
5893 break;
5894 case GET_FROM_DISPLAY_VECTOR:
5895 if (it->s)
5896 it->method = GET_FROM_C_STRING;
5897 else if (STRINGP (it->string))
5898 it->method = GET_FROM_STRING;
5899 else
5901 it->method = GET_FROM_BUFFER;
5902 it->object = it->w->contents;
5905 it->end_charpos = p->end_charpos;
5906 it->string_nchars = p->string_nchars;
5907 it->area = p->area;
5908 it->multibyte_p = p->multibyte_p;
5909 it->avoid_cursor_p = p->avoid_cursor_p;
5910 it->space_width = p->space_width;
5911 it->font_height = p->font_height;
5912 it->voffset = p->voffset;
5913 it->string_from_display_prop_p = p->string_from_display_prop_p;
5914 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5915 it->line_wrap = p->line_wrap;
5916 it->bidi_p = p->bidi_p;
5917 it->paragraph_embedding = p->paragraph_embedding;
5918 it->from_disp_prop_p = p->from_disp_prop_p;
5919 if (it->bidi_p)
5921 bidi_pop_it (&it->bidi_it);
5922 /* Bidi-iterate until we get out of the portion of text, if any,
5923 covered by a `display' text property or by an overlay with
5924 `display' property. (We cannot just jump there, because the
5925 internal coherency of the bidi iterator state can not be
5926 preserved across such jumps.) We also must determine the
5927 paragraph base direction if the overlay we just processed is
5928 at the beginning of a new paragraph. */
5929 if (from_display_prop
5930 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5931 iterate_out_of_display_property (it);
5933 eassert ((BUFFERP (it->object)
5934 && IT_CHARPOS (*it) == it->bidi_it.charpos
5935 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5936 || (STRINGP (it->object)
5937 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5938 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5939 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5945 /***********************************************************************
5946 Moving over lines
5947 ***********************************************************************/
5949 /* Set IT's current position to the previous line start. */
5951 static void
5952 back_to_previous_line_start (struct it *it)
5954 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
5956 DEC_BOTH (cp, bp);
5957 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
5961 /* Move IT to the next line start.
5963 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5964 we skipped over part of the text (as opposed to moving the iterator
5965 continuously over the text). Otherwise, don't change the value
5966 of *SKIPPED_P.
5968 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5969 iterator on the newline, if it was found.
5971 Newlines may come from buffer text, overlay strings, or strings
5972 displayed via the `display' property. That's the reason we can't
5973 simply use find_newline_no_quit.
5975 Note that this function may not skip over invisible text that is so
5976 because of text properties and immediately follows a newline. If
5977 it would, function reseat_at_next_visible_line_start, when called
5978 from set_iterator_to_next, would effectively make invisible
5979 characters following a newline part of the wrong glyph row, which
5980 leads to wrong cursor motion. */
5982 static int
5983 forward_to_next_line_start (struct it *it, int *skipped_p,
5984 struct bidi_it *bidi_it_prev)
5986 ptrdiff_t old_selective;
5987 int newline_found_p, n;
5988 const int MAX_NEWLINE_DISTANCE = 500;
5990 /* If already on a newline, just consume it to avoid unintended
5991 skipping over invisible text below. */
5992 if (it->what == IT_CHARACTER
5993 && it->c == '\n'
5994 && CHARPOS (it->position) == IT_CHARPOS (*it))
5996 if (it->bidi_p && bidi_it_prev)
5997 *bidi_it_prev = it->bidi_it;
5998 set_iterator_to_next (it, 0);
5999 it->c = 0;
6000 return 1;
6003 /* Don't handle selective display in the following. It's (a)
6004 unnecessary because it's done by the caller, and (b) leads to an
6005 infinite recursion because next_element_from_ellipsis indirectly
6006 calls this function. */
6007 old_selective = it->selective;
6008 it->selective = 0;
6010 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6011 from buffer text. */
6012 for (n = newline_found_p = 0;
6013 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6014 n += STRINGP (it->string) ? 0 : 1)
6016 if (!get_next_display_element (it))
6017 return 0;
6018 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6019 if (newline_found_p && it->bidi_p && bidi_it_prev)
6020 *bidi_it_prev = it->bidi_it;
6021 set_iterator_to_next (it, 0);
6024 /* If we didn't find a newline near enough, see if we can use a
6025 short-cut. */
6026 if (!newline_found_p)
6028 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6029 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6030 1, &bytepos);
6031 Lisp_Object pos;
6033 eassert (!STRINGP (it->string));
6035 /* If there isn't any `display' property in sight, and no
6036 overlays, we can just use the position of the newline in
6037 buffer text. */
6038 if (it->stop_charpos >= limit
6039 || ((pos = Fnext_single_property_change (make_number (start),
6040 Qdisplay, Qnil,
6041 make_number (limit)),
6042 NILP (pos))
6043 && next_overlay_change (start) == ZV))
6045 if (!it->bidi_p)
6047 IT_CHARPOS (*it) = limit;
6048 IT_BYTEPOS (*it) = bytepos;
6050 else
6052 struct bidi_it bprev;
6054 /* Help bidi.c avoid expensive searches for display
6055 properties and overlays, by telling it that there are
6056 none up to `limit'. */
6057 if (it->bidi_it.disp_pos < limit)
6059 it->bidi_it.disp_pos = limit;
6060 it->bidi_it.disp_prop = 0;
6062 do {
6063 bprev = it->bidi_it;
6064 bidi_move_to_visually_next (&it->bidi_it);
6065 } while (it->bidi_it.charpos != limit);
6066 IT_CHARPOS (*it) = limit;
6067 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6068 if (bidi_it_prev)
6069 *bidi_it_prev = bprev;
6071 *skipped_p = newline_found_p = 1;
6073 else
6075 while (get_next_display_element (it)
6076 && !newline_found_p)
6078 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6079 if (newline_found_p && it->bidi_p && bidi_it_prev)
6080 *bidi_it_prev = it->bidi_it;
6081 set_iterator_to_next (it, 0);
6086 it->selective = old_selective;
6087 return newline_found_p;
6091 /* Set IT's current position to the previous visible line start. Skip
6092 invisible text that is so either due to text properties or due to
6093 selective display. Caution: this does not change IT->current_x and
6094 IT->hpos. */
6096 static void
6097 back_to_previous_visible_line_start (struct it *it)
6099 while (IT_CHARPOS (*it) > BEGV)
6101 back_to_previous_line_start (it);
6103 if (IT_CHARPOS (*it) <= BEGV)
6104 break;
6106 /* If selective > 0, then lines indented more than its value are
6107 invisible. */
6108 if (it->selective > 0
6109 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6110 it->selective))
6111 continue;
6113 /* Check the newline before point for invisibility. */
6115 Lisp_Object prop;
6116 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6117 Qinvisible, it->window);
6118 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6119 continue;
6122 if (IT_CHARPOS (*it) <= BEGV)
6123 break;
6126 struct it it2;
6127 void *it2data = NULL;
6128 ptrdiff_t pos;
6129 ptrdiff_t beg, end;
6130 Lisp_Object val, overlay;
6132 SAVE_IT (it2, *it, it2data);
6134 /* If newline is part of a composition, continue from start of composition */
6135 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6136 && beg < IT_CHARPOS (*it))
6137 goto replaced;
6139 /* If newline is replaced by a display property, find start of overlay
6140 or interval and continue search from that point. */
6141 pos = --IT_CHARPOS (it2);
6142 --IT_BYTEPOS (it2);
6143 it2.sp = 0;
6144 bidi_unshelve_cache (NULL, 0);
6145 it2.string_from_display_prop_p = 0;
6146 it2.from_disp_prop_p = 0;
6147 if (handle_display_prop (&it2) == HANDLED_RETURN
6148 && !NILP (val = get_char_property_and_overlay
6149 (make_number (pos), Qdisplay, Qnil, &overlay))
6150 && (OVERLAYP (overlay)
6151 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6152 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6154 RESTORE_IT (it, it, it2data);
6155 goto replaced;
6158 /* Newline is not replaced by anything -- so we are done. */
6159 RESTORE_IT (it, it, it2data);
6160 break;
6162 replaced:
6163 if (beg < BEGV)
6164 beg = BEGV;
6165 IT_CHARPOS (*it) = beg;
6166 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6170 it->continuation_lines_width = 0;
6172 eassert (IT_CHARPOS (*it) >= BEGV);
6173 eassert (IT_CHARPOS (*it) == BEGV
6174 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6175 CHECK_IT (it);
6179 /* Reseat iterator IT at the previous visible line start. Skip
6180 invisible text that is so either due to text properties or due to
6181 selective display. At the end, update IT's overlay information,
6182 face information etc. */
6184 void
6185 reseat_at_previous_visible_line_start (struct it *it)
6187 back_to_previous_visible_line_start (it);
6188 reseat (it, it->current.pos, 1);
6189 CHECK_IT (it);
6193 /* Reseat iterator IT on the next visible line start in the current
6194 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6195 preceding the line start. Skip over invisible text that is so
6196 because of selective display. Compute faces, overlays etc at the
6197 new position. Note that this function does not skip over text that
6198 is invisible because of text properties. */
6200 static void
6201 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6203 int newline_found_p, skipped_p = 0;
6204 struct bidi_it bidi_it_prev;
6206 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6208 /* Skip over lines that are invisible because they are indented
6209 more than the value of IT->selective. */
6210 if (it->selective > 0)
6211 while (IT_CHARPOS (*it) < ZV
6212 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6213 it->selective))
6215 eassert (IT_BYTEPOS (*it) == BEGV
6216 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6217 newline_found_p =
6218 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6221 /* Position on the newline if that's what's requested. */
6222 if (on_newline_p && newline_found_p)
6224 if (STRINGP (it->string))
6226 if (IT_STRING_CHARPOS (*it) > 0)
6228 if (!it->bidi_p)
6230 --IT_STRING_CHARPOS (*it);
6231 --IT_STRING_BYTEPOS (*it);
6233 else
6235 /* We need to restore the bidi iterator to the state
6236 it had on the newline, and resync the IT's
6237 position with that. */
6238 it->bidi_it = bidi_it_prev;
6239 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6240 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6244 else if (IT_CHARPOS (*it) > BEGV)
6246 if (!it->bidi_p)
6248 --IT_CHARPOS (*it);
6249 --IT_BYTEPOS (*it);
6251 else
6253 /* We need to restore the bidi iterator to the state it
6254 had on the newline and resync IT with that. */
6255 it->bidi_it = bidi_it_prev;
6256 IT_CHARPOS (*it) = it->bidi_it.charpos;
6257 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6259 reseat (it, it->current.pos, 0);
6262 else if (skipped_p)
6263 reseat (it, it->current.pos, 0);
6265 CHECK_IT (it);
6270 /***********************************************************************
6271 Changing an iterator's position
6272 ***********************************************************************/
6274 /* Change IT's current position to POS in current_buffer. If FORCE_P
6275 is non-zero, always check for text properties at the new position.
6276 Otherwise, text properties are only looked up if POS >=
6277 IT->check_charpos of a property. */
6279 static void
6280 reseat (struct it *it, struct text_pos pos, int force_p)
6282 ptrdiff_t original_pos = IT_CHARPOS (*it);
6284 reseat_1 (it, pos, 0);
6286 /* Determine where to check text properties. Avoid doing it
6287 where possible because text property lookup is very expensive. */
6288 if (force_p
6289 || CHARPOS (pos) > it->stop_charpos
6290 || CHARPOS (pos) < original_pos)
6292 if (it->bidi_p)
6294 /* For bidi iteration, we need to prime prev_stop and
6295 base_level_stop with our best estimations. */
6296 /* Implementation note: Of course, POS is not necessarily a
6297 stop position, so assigning prev_pos to it is a lie; we
6298 should have called compute_stop_backwards. However, if
6299 the current buffer does not include any R2L characters,
6300 that call would be a waste of cycles, because the
6301 iterator will never move back, and thus never cross this
6302 "fake" stop position. So we delay that backward search
6303 until the time we really need it, in next_element_from_buffer. */
6304 if (CHARPOS (pos) != it->prev_stop)
6305 it->prev_stop = CHARPOS (pos);
6306 if (CHARPOS (pos) < it->base_level_stop)
6307 it->base_level_stop = 0; /* meaning it's unknown */
6308 handle_stop (it);
6310 else
6312 handle_stop (it);
6313 it->prev_stop = it->base_level_stop = 0;
6318 CHECK_IT (it);
6322 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6323 IT->stop_pos to POS, also. */
6325 static void
6326 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6328 /* Don't call this function when scanning a C string. */
6329 eassert (it->s == NULL);
6331 /* POS must be a reasonable value. */
6332 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6334 it->current.pos = it->position = pos;
6335 it->end_charpos = ZV;
6336 it->dpvec = NULL;
6337 it->current.dpvec_index = -1;
6338 it->current.overlay_string_index = -1;
6339 IT_STRING_CHARPOS (*it) = -1;
6340 IT_STRING_BYTEPOS (*it) = -1;
6341 it->string = Qnil;
6342 it->method = GET_FROM_BUFFER;
6343 it->object = it->w->contents;
6344 it->area = TEXT_AREA;
6345 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6346 it->sp = 0;
6347 it->string_from_display_prop_p = 0;
6348 it->string_from_prefix_prop_p = 0;
6350 it->from_disp_prop_p = 0;
6351 it->face_before_selective_p = 0;
6352 if (it->bidi_p)
6354 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6355 &it->bidi_it);
6356 bidi_unshelve_cache (NULL, 0);
6357 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6358 it->bidi_it.string.s = NULL;
6359 it->bidi_it.string.lstring = Qnil;
6360 it->bidi_it.string.bufpos = 0;
6361 it->bidi_it.string.unibyte = 0;
6362 it->bidi_it.w = it->w;
6365 if (set_stop_p)
6367 it->stop_charpos = CHARPOS (pos);
6368 it->base_level_stop = CHARPOS (pos);
6370 /* This make the information stored in it->cmp_it invalidate. */
6371 it->cmp_it.id = -1;
6375 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6376 If S is non-null, it is a C string to iterate over. Otherwise,
6377 STRING gives a Lisp string to iterate over.
6379 If PRECISION > 0, don't return more then PRECISION number of
6380 characters from the string.
6382 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6383 characters have been returned. FIELD_WIDTH < 0 means an infinite
6384 field width.
6386 MULTIBYTE = 0 means disable processing of multibyte characters,
6387 MULTIBYTE > 0 means enable it,
6388 MULTIBYTE < 0 means use IT->multibyte_p.
6390 IT must be initialized via a prior call to init_iterator before
6391 calling this function. */
6393 static void
6394 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6395 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6396 int multibyte)
6398 /* No region in strings. */
6399 it->region_beg_charpos = it->region_end_charpos = -1;
6401 /* No text property checks performed by default, but see below. */
6402 it->stop_charpos = -1;
6404 /* Set iterator position and end position. */
6405 memset (&it->current, 0, sizeof it->current);
6406 it->current.overlay_string_index = -1;
6407 it->current.dpvec_index = -1;
6408 eassert (charpos >= 0);
6410 /* If STRING is specified, use its multibyteness, otherwise use the
6411 setting of MULTIBYTE, if specified. */
6412 if (multibyte >= 0)
6413 it->multibyte_p = multibyte > 0;
6415 /* Bidirectional reordering of strings is controlled by the default
6416 value of bidi-display-reordering. Don't try to reorder while
6417 loading loadup.el, as the necessary character property tables are
6418 not yet available. */
6419 it->bidi_p =
6420 NILP (Vpurify_flag)
6421 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6423 if (s == NULL)
6425 eassert (STRINGP (string));
6426 it->string = string;
6427 it->s = NULL;
6428 it->end_charpos = it->string_nchars = SCHARS (string);
6429 it->method = GET_FROM_STRING;
6430 it->current.string_pos = string_pos (charpos, string);
6432 if (it->bidi_p)
6434 it->bidi_it.string.lstring = string;
6435 it->bidi_it.string.s = NULL;
6436 it->bidi_it.string.schars = it->end_charpos;
6437 it->bidi_it.string.bufpos = 0;
6438 it->bidi_it.string.from_disp_str = 0;
6439 it->bidi_it.string.unibyte = !it->multibyte_p;
6440 it->bidi_it.w = it->w;
6441 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6442 FRAME_WINDOW_P (it->f), &it->bidi_it);
6445 else
6447 it->s = (const unsigned char *) s;
6448 it->string = Qnil;
6450 /* Note that we use IT->current.pos, not it->current.string_pos,
6451 for displaying C strings. */
6452 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6453 if (it->multibyte_p)
6455 it->current.pos = c_string_pos (charpos, s, 1);
6456 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6458 else
6460 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6461 it->end_charpos = it->string_nchars = strlen (s);
6464 if (it->bidi_p)
6466 it->bidi_it.string.lstring = Qnil;
6467 it->bidi_it.string.s = (const unsigned char *) s;
6468 it->bidi_it.string.schars = it->end_charpos;
6469 it->bidi_it.string.bufpos = 0;
6470 it->bidi_it.string.from_disp_str = 0;
6471 it->bidi_it.string.unibyte = !it->multibyte_p;
6472 it->bidi_it.w = it->w;
6473 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6474 &it->bidi_it);
6476 it->method = GET_FROM_C_STRING;
6479 /* PRECISION > 0 means don't return more than PRECISION characters
6480 from the string. */
6481 if (precision > 0 && it->end_charpos - charpos > precision)
6483 it->end_charpos = it->string_nchars = charpos + precision;
6484 if (it->bidi_p)
6485 it->bidi_it.string.schars = it->end_charpos;
6488 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6489 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6490 FIELD_WIDTH < 0 means infinite field width. This is useful for
6491 padding with `-' at the end of a mode line. */
6492 if (field_width < 0)
6493 field_width = INFINITY;
6494 /* Implementation note: We deliberately don't enlarge
6495 it->bidi_it.string.schars here to fit it->end_charpos, because
6496 the bidi iterator cannot produce characters out of thin air. */
6497 if (field_width > it->end_charpos - charpos)
6498 it->end_charpos = charpos + field_width;
6500 /* Use the standard display table for displaying strings. */
6501 if (DISP_TABLE_P (Vstandard_display_table))
6502 it->dp = XCHAR_TABLE (Vstandard_display_table);
6504 it->stop_charpos = charpos;
6505 it->prev_stop = charpos;
6506 it->base_level_stop = 0;
6507 if (it->bidi_p)
6509 it->bidi_it.first_elt = 1;
6510 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6511 it->bidi_it.disp_pos = -1;
6513 if (s == NULL && it->multibyte_p)
6515 ptrdiff_t endpos = SCHARS (it->string);
6516 if (endpos > it->end_charpos)
6517 endpos = it->end_charpos;
6518 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6519 it->string);
6521 CHECK_IT (it);
6526 /***********************************************************************
6527 Iteration
6528 ***********************************************************************/
6530 /* Map enum it_method value to corresponding next_element_from_* function. */
6532 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6534 next_element_from_buffer,
6535 next_element_from_display_vector,
6536 next_element_from_string,
6537 next_element_from_c_string,
6538 next_element_from_image,
6539 next_element_from_stretch
6542 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6545 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6546 (possibly with the following characters). */
6548 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6549 ((IT)->cmp_it.id >= 0 \
6550 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6551 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6552 END_CHARPOS, (IT)->w, \
6553 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6554 (IT)->string)))
6557 /* Lookup the char-table Vglyphless_char_display for character C (-1
6558 if we want information for no-font case), and return the display
6559 method symbol. By side-effect, update it->what and
6560 it->glyphless_method. This function is called from
6561 get_next_display_element for each character element, and from
6562 x_produce_glyphs when no suitable font was found. */
6564 Lisp_Object
6565 lookup_glyphless_char_display (int c, struct it *it)
6567 Lisp_Object glyphless_method = Qnil;
6569 if (CHAR_TABLE_P (Vglyphless_char_display)
6570 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6572 if (c >= 0)
6574 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6575 if (CONSP (glyphless_method))
6576 glyphless_method = FRAME_WINDOW_P (it->f)
6577 ? XCAR (glyphless_method)
6578 : XCDR (glyphless_method);
6580 else
6581 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6584 retry:
6585 if (NILP (glyphless_method))
6587 if (c >= 0)
6588 /* The default is to display the character by a proper font. */
6589 return Qnil;
6590 /* The default for the no-font case is to display an empty box. */
6591 glyphless_method = Qempty_box;
6593 if (EQ (glyphless_method, Qzero_width))
6595 if (c >= 0)
6596 return glyphless_method;
6597 /* This method can't be used for the no-font case. */
6598 glyphless_method = Qempty_box;
6600 if (EQ (glyphless_method, Qthin_space))
6601 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6602 else if (EQ (glyphless_method, Qempty_box))
6603 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6604 else if (EQ (glyphless_method, Qhex_code))
6605 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6606 else if (STRINGP (glyphless_method))
6607 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6608 else
6610 /* Invalid value. We use the default method. */
6611 glyphless_method = Qnil;
6612 goto retry;
6614 it->what = IT_GLYPHLESS;
6615 return glyphless_method;
6618 /* Load IT's display element fields with information about the next
6619 display element from the current position of IT. Value is zero if
6620 end of buffer (or C string) is reached. */
6622 static struct frame *last_escape_glyph_frame = NULL;
6623 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6624 static int last_escape_glyph_merged_face_id = 0;
6626 struct frame *last_glyphless_glyph_frame = NULL;
6627 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6628 int last_glyphless_glyph_merged_face_id = 0;
6630 static int
6631 get_next_display_element (struct it *it)
6633 /* Non-zero means that we found a display element. Zero means that
6634 we hit the end of what we iterate over. Performance note: the
6635 function pointer `method' used here turns out to be faster than
6636 using a sequence of if-statements. */
6637 int success_p;
6639 get_next:
6640 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6642 if (it->what == IT_CHARACTER)
6644 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6645 and only if (a) the resolved directionality of that character
6646 is R..." */
6647 /* FIXME: Do we need an exception for characters from display
6648 tables? */
6649 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6650 it->c = bidi_mirror_char (it->c);
6651 /* Map via display table or translate control characters.
6652 IT->c, IT->len etc. have been set to the next character by
6653 the function call above. If we have a display table, and it
6654 contains an entry for IT->c, translate it. Don't do this if
6655 IT->c itself comes from a display table, otherwise we could
6656 end up in an infinite recursion. (An alternative could be to
6657 count the recursion depth of this function and signal an
6658 error when a certain maximum depth is reached.) Is it worth
6659 it? */
6660 if (success_p && it->dpvec == NULL)
6662 Lisp_Object dv;
6663 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6664 int nonascii_space_p = 0;
6665 int nonascii_hyphen_p = 0;
6666 int c = it->c; /* This is the character to display. */
6668 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6670 eassert (SINGLE_BYTE_CHAR_P (c));
6671 if (unibyte_display_via_language_environment)
6673 c = DECODE_CHAR (unibyte, c);
6674 if (c < 0)
6675 c = BYTE8_TO_CHAR (it->c);
6677 else
6678 c = BYTE8_TO_CHAR (it->c);
6681 if (it->dp
6682 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6683 VECTORP (dv)))
6685 struct Lisp_Vector *v = XVECTOR (dv);
6687 /* Return the first character from the display table
6688 entry, if not empty. If empty, don't display the
6689 current character. */
6690 if (v->header.size)
6692 it->dpvec_char_len = it->len;
6693 it->dpvec = v->contents;
6694 it->dpend = v->contents + v->header.size;
6695 it->current.dpvec_index = 0;
6696 it->dpvec_face_id = -1;
6697 it->saved_face_id = it->face_id;
6698 it->method = GET_FROM_DISPLAY_VECTOR;
6699 it->ellipsis_p = 0;
6701 else
6703 set_iterator_to_next (it, 0);
6705 goto get_next;
6708 if (! NILP (lookup_glyphless_char_display (c, it)))
6710 if (it->what == IT_GLYPHLESS)
6711 goto done;
6712 /* Don't display this character. */
6713 set_iterator_to_next (it, 0);
6714 goto get_next;
6717 /* If `nobreak-char-display' is non-nil, we display
6718 non-ASCII spaces and hyphens specially. */
6719 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6721 if (c == 0xA0)
6722 nonascii_space_p = 1;
6723 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6724 nonascii_hyphen_p = 1;
6727 /* Translate control characters into `\003' or `^C' form.
6728 Control characters coming from a display table entry are
6729 currently not translated because we use IT->dpvec to hold
6730 the translation. This could easily be changed but I
6731 don't believe that it is worth doing.
6733 The characters handled by `nobreak-char-display' must be
6734 translated too.
6736 Non-printable characters and raw-byte characters are also
6737 translated to octal form. */
6738 if (((c < ' ' || c == 127) /* ASCII control chars */
6739 ? (it->area != TEXT_AREA
6740 /* In mode line, treat \n, \t like other crl chars. */
6741 || (c != '\t'
6742 && it->glyph_row
6743 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6744 || (c != '\n' && c != '\t'))
6745 : (nonascii_space_p
6746 || nonascii_hyphen_p
6747 || CHAR_BYTE8_P (c)
6748 || ! CHAR_PRINTABLE_P (c))))
6750 /* C is a control character, non-ASCII space/hyphen,
6751 raw-byte, or a non-printable character which must be
6752 displayed either as '\003' or as `^C' where the '\\'
6753 and '^' can be defined in the display table. Fill
6754 IT->ctl_chars with glyphs for what we have to
6755 display. Then, set IT->dpvec to these glyphs. */
6756 Lisp_Object gc;
6757 int ctl_len;
6758 int face_id;
6759 int lface_id = 0;
6760 int escape_glyph;
6762 /* Handle control characters with ^. */
6764 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6766 int g;
6768 g = '^'; /* default glyph for Control */
6769 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6770 if (it->dp
6771 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6773 g = GLYPH_CODE_CHAR (gc);
6774 lface_id = GLYPH_CODE_FACE (gc);
6776 if (lface_id)
6778 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6780 else if (it->f == last_escape_glyph_frame
6781 && it->face_id == last_escape_glyph_face_id)
6783 face_id = last_escape_glyph_merged_face_id;
6785 else
6787 /* Merge the escape-glyph face into the current face. */
6788 face_id = merge_faces (it->f, Qescape_glyph, 0,
6789 it->face_id);
6790 last_escape_glyph_frame = it->f;
6791 last_escape_glyph_face_id = it->face_id;
6792 last_escape_glyph_merged_face_id = face_id;
6795 XSETINT (it->ctl_chars[0], g);
6796 XSETINT (it->ctl_chars[1], c ^ 0100);
6797 ctl_len = 2;
6798 goto display_control;
6801 /* Handle non-ascii space in the mode where it only gets
6802 highlighting. */
6804 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6806 /* Merge `nobreak-space' into the current face. */
6807 face_id = merge_faces (it->f, Qnobreak_space, 0,
6808 it->face_id);
6809 XSETINT (it->ctl_chars[0], ' ');
6810 ctl_len = 1;
6811 goto display_control;
6814 /* Handle sequences that start with the "escape glyph". */
6816 /* the default escape glyph is \. */
6817 escape_glyph = '\\';
6819 if (it->dp
6820 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6822 escape_glyph = GLYPH_CODE_CHAR (gc);
6823 lface_id = GLYPH_CODE_FACE (gc);
6825 if (lface_id)
6827 /* The display table specified a face.
6828 Merge it into face_id and also into escape_glyph. */
6829 face_id = merge_faces (it->f, Qt, lface_id,
6830 it->face_id);
6832 else if (it->f == last_escape_glyph_frame
6833 && it->face_id == last_escape_glyph_face_id)
6835 face_id = last_escape_glyph_merged_face_id;
6837 else
6839 /* Merge the escape-glyph face into the current face. */
6840 face_id = merge_faces (it->f, Qescape_glyph, 0,
6841 it->face_id);
6842 last_escape_glyph_frame = it->f;
6843 last_escape_glyph_face_id = it->face_id;
6844 last_escape_glyph_merged_face_id = face_id;
6847 /* Draw non-ASCII hyphen with just highlighting: */
6849 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6851 XSETINT (it->ctl_chars[0], '-');
6852 ctl_len = 1;
6853 goto display_control;
6856 /* Draw non-ASCII space/hyphen with escape glyph: */
6858 if (nonascii_space_p || nonascii_hyphen_p)
6860 XSETINT (it->ctl_chars[0], escape_glyph);
6861 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6862 ctl_len = 2;
6863 goto display_control;
6867 char str[10];
6868 int len, i;
6870 if (CHAR_BYTE8_P (c))
6871 /* Display \200 instead of \17777600. */
6872 c = CHAR_TO_BYTE8 (c);
6873 len = sprintf (str, "%03o", c);
6875 XSETINT (it->ctl_chars[0], escape_glyph);
6876 for (i = 0; i < len; i++)
6877 XSETINT (it->ctl_chars[i + 1], str[i]);
6878 ctl_len = len + 1;
6881 display_control:
6882 /* Set up IT->dpvec and return first character from it. */
6883 it->dpvec_char_len = it->len;
6884 it->dpvec = it->ctl_chars;
6885 it->dpend = it->dpvec + ctl_len;
6886 it->current.dpvec_index = 0;
6887 it->dpvec_face_id = face_id;
6888 it->saved_face_id = it->face_id;
6889 it->method = GET_FROM_DISPLAY_VECTOR;
6890 it->ellipsis_p = 0;
6891 goto get_next;
6893 it->char_to_display = c;
6895 else if (success_p)
6897 it->char_to_display = it->c;
6901 /* Adjust face id for a multibyte character. There are no multibyte
6902 character in unibyte text. */
6903 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6904 && it->multibyte_p
6905 && success_p
6906 && FRAME_WINDOW_P (it->f))
6908 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6910 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6912 /* Automatic composition with glyph-string. */
6913 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6915 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6917 else
6919 ptrdiff_t pos = (it->s ? -1
6920 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6921 : IT_CHARPOS (*it));
6922 int c;
6924 if (it->what == IT_CHARACTER)
6925 c = it->char_to_display;
6926 else
6928 struct composition *cmp = composition_table[it->cmp_it.id];
6929 int i;
6931 c = ' ';
6932 for (i = 0; i < cmp->glyph_len; i++)
6933 /* TAB in a composition means display glyphs with
6934 padding space on the left or right. */
6935 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6936 break;
6938 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6942 done:
6943 /* Is this character the last one of a run of characters with
6944 box? If yes, set IT->end_of_box_run_p to 1. */
6945 if (it->face_box_p
6946 && it->s == NULL)
6948 if (it->method == GET_FROM_STRING && it->sp)
6950 int face_id = underlying_face_id (it);
6951 struct face *face = FACE_FROM_ID (it->f, face_id);
6953 if (face)
6955 if (face->box == FACE_NO_BOX)
6957 /* If the box comes from face properties in a
6958 display string, check faces in that string. */
6959 int string_face_id = face_after_it_pos (it);
6960 it->end_of_box_run_p
6961 = (FACE_FROM_ID (it->f, string_face_id)->box
6962 == FACE_NO_BOX);
6964 /* Otherwise, the box comes from the underlying face.
6965 If this is the last string character displayed, check
6966 the next buffer location. */
6967 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6968 && (it->current.overlay_string_index
6969 == it->n_overlay_strings - 1))
6971 ptrdiff_t ignore;
6972 int next_face_id;
6973 struct text_pos pos = it->current.pos;
6974 INC_TEXT_POS (pos, it->multibyte_p);
6976 next_face_id = face_at_buffer_position
6977 (it->w, CHARPOS (pos), it->region_beg_charpos,
6978 it->region_end_charpos, &ignore,
6979 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6980 -1);
6981 it->end_of_box_run_p
6982 = (FACE_FROM_ID (it->f, next_face_id)->box
6983 == FACE_NO_BOX);
6987 else
6989 int face_id = face_after_it_pos (it);
6990 it->end_of_box_run_p
6991 = (face_id != it->face_id
6992 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6995 /* If we reached the end of the object we've been iterating (e.g., a
6996 display string or an overlay string), and there's something on
6997 IT->stack, proceed with what's on the stack. It doesn't make
6998 sense to return zero if there's unprocessed stuff on the stack,
6999 because otherwise that stuff will never be displayed. */
7000 if (!success_p && it->sp > 0)
7002 set_iterator_to_next (it, 0);
7003 success_p = get_next_display_element (it);
7006 /* Value is 0 if end of buffer or string reached. */
7007 return success_p;
7011 /* Move IT to the next display element.
7013 RESEAT_P non-zero means if called on a newline in buffer text,
7014 skip to the next visible line start.
7016 Functions get_next_display_element and set_iterator_to_next are
7017 separate because I find this arrangement easier to handle than a
7018 get_next_display_element function that also increments IT's
7019 position. The way it is we can first look at an iterator's current
7020 display element, decide whether it fits on a line, and if it does,
7021 increment the iterator position. The other way around we probably
7022 would either need a flag indicating whether the iterator has to be
7023 incremented the next time, or we would have to implement a
7024 decrement position function which would not be easy to write. */
7026 void
7027 set_iterator_to_next (struct it *it, int reseat_p)
7029 /* Reset flags indicating start and end of a sequence of characters
7030 with box. Reset them at the start of this function because
7031 moving the iterator to a new position might set them. */
7032 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7034 switch (it->method)
7036 case GET_FROM_BUFFER:
7037 /* The current display element of IT is a character from
7038 current_buffer. Advance in the buffer, and maybe skip over
7039 invisible lines that are so because of selective display. */
7040 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7041 reseat_at_next_visible_line_start (it, 0);
7042 else if (it->cmp_it.id >= 0)
7044 /* We are currently getting glyphs from a composition. */
7045 int i;
7047 if (! it->bidi_p)
7049 IT_CHARPOS (*it) += it->cmp_it.nchars;
7050 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7051 if (it->cmp_it.to < it->cmp_it.nglyphs)
7053 it->cmp_it.from = it->cmp_it.to;
7055 else
7057 it->cmp_it.id = -1;
7058 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7059 IT_BYTEPOS (*it),
7060 it->end_charpos, Qnil);
7063 else if (! it->cmp_it.reversed_p)
7065 /* Composition created while scanning forward. */
7066 /* Update IT's char/byte positions to point to the first
7067 character of the next grapheme cluster, or to the
7068 character visually after the current composition. */
7069 for (i = 0; i < it->cmp_it.nchars; i++)
7070 bidi_move_to_visually_next (&it->bidi_it);
7071 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7072 IT_CHARPOS (*it) = it->bidi_it.charpos;
7074 if (it->cmp_it.to < it->cmp_it.nglyphs)
7076 /* Proceed to the next grapheme cluster. */
7077 it->cmp_it.from = it->cmp_it.to;
7079 else
7081 /* No more grapheme clusters in this composition.
7082 Find the next stop position. */
7083 ptrdiff_t stop = it->end_charpos;
7084 if (it->bidi_it.scan_dir < 0)
7085 /* Now we are scanning backward and don't know
7086 where to stop. */
7087 stop = -1;
7088 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7089 IT_BYTEPOS (*it), stop, Qnil);
7092 else
7094 /* Composition created while scanning backward. */
7095 /* Update IT's char/byte positions to point to the last
7096 character of the previous grapheme cluster, or the
7097 character visually after the current composition. */
7098 for (i = 0; i < it->cmp_it.nchars; i++)
7099 bidi_move_to_visually_next (&it->bidi_it);
7100 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7101 IT_CHARPOS (*it) = it->bidi_it.charpos;
7102 if (it->cmp_it.from > 0)
7104 /* Proceed to the previous grapheme cluster. */
7105 it->cmp_it.to = it->cmp_it.from;
7107 else
7109 /* No more grapheme clusters in this composition.
7110 Find the next stop position. */
7111 ptrdiff_t stop = it->end_charpos;
7112 if (it->bidi_it.scan_dir < 0)
7113 /* Now we are scanning backward and don't know
7114 where to stop. */
7115 stop = -1;
7116 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7117 IT_BYTEPOS (*it), stop, Qnil);
7121 else
7123 eassert (it->len != 0);
7125 if (!it->bidi_p)
7127 IT_BYTEPOS (*it) += it->len;
7128 IT_CHARPOS (*it) += 1;
7130 else
7132 int prev_scan_dir = it->bidi_it.scan_dir;
7133 /* If this is a new paragraph, determine its base
7134 direction (a.k.a. its base embedding level). */
7135 if (it->bidi_it.new_paragraph)
7136 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7137 bidi_move_to_visually_next (&it->bidi_it);
7138 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7139 IT_CHARPOS (*it) = it->bidi_it.charpos;
7140 if (prev_scan_dir != it->bidi_it.scan_dir)
7142 /* As the scan direction was changed, we must
7143 re-compute the stop position for composition. */
7144 ptrdiff_t stop = it->end_charpos;
7145 if (it->bidi_it.scan_dir < 0)
7146 stop = -1;
7147 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7148 IT_BYTEPOS (*it), stop, Qnil);
7151 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7153 break;
7155 case GET_FROM_C_STRING:
7156 /* Current display element of IT is from a C string. */
7157 if (!it->bidi_p
7158 /* If the string position is beyond string's end, it means
7159 next_element_from_c_string is padding the string with
7160 blanks, in which case we bypass the bidi iterator,
7161 because it cannot deal with such virtual characters. */
7162 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7164 IT_BYTEPOS (*it) += it->len;
7165 IT_CHARPOS (*it) += 1;
7167 else
7169 bidi_move_to_visually_next (&it->bidi_it);
7170 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7171 IT_CHARPOS (*it) = it->bidi_it.charpos;
7173 break;
7175 case GET_FROM_DISPLAY_VECTOR:
7176 /* Current display element of IT is from a display table entry.
7177 Advance in the display table definition. Reset it to null if
7178 end reached, and continue with characters from buffers/
7179 strings. */
7180 ++it->current.dpvec_index;
7182 /* Restore face of the iterator to what they were before the
7183 display vector entry (these entries may contain faces). */
7184 it->face_id = it->saved_face_id;
7186 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7188 int recheck_faces = it->ellipsis_p;
7190 if (it->s)
7191 it->method = GET_FROM_C_STRING;
7192 else if (STRINGP (it->string))
7193 it->method = GET_FROM_STRING;
7194 else
7196 it->method = GET_FROM_BUFFER;
7197 it->object = it->w->contents;
7200 it->dpvec = NULL;
7201 it->current.dpvec_index = -1;
7203 /* Skip over characters which were displayed via IT->dpvec. */
7204 if (it->dpvec_char_len < 0)
7205 reseat_at_next_visible_line_start (it, 1);
7206 else if (it->dpvec_char_len > 0)
7208 if (it->method == GET_FROM_STRING
7209 && it->current.overlay_string_index >= 0
7210 && it->n_overlay_strings > 0)
7211 it->ignore_overlay_strings_at_pos_p = 1;
7212 it->len = it->dpvec_char_len;
7213 set_iterator_to_next (it, reseat_p);
7216 /* Maybe recheck faces after display vector */
7217 if (recheck_faces)
7218 it->stop_charpos = IT_CHARPOS (*it);
7220 break;
7222 case GET_FROM_STRING:
7223 /* Current display element is a character from a Lisp string. */
7224 eassert (it->s == NULL && STRINGP (it->string));
7225 /* Don't advance past string end. These conditions are true
7226 when set_iterator_to_next is called at the end of
7227 get_next_display_element, in which case the Lisp string is
7228 already exhausted, and all we want is pop the iterator
7229 stack. */
7230 if (it->current.overlay_string_index >= 0)
7232 /* This is an overlay string, so there's no padding with
7233 spaces, and the number of characters in the string is
7234 where the string ends. */
7235 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7236 goto consider_string_end;
7238 else
7240 /* Not an overlay string. There could be padding, so test
7241 against it->end_charpos . */
7242 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7243 goto consider_string_end;
7245 if (it->cmp_it.id >= 0)
7247 int i;
7249 if (! it->bidi_p)
7251 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7252 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7253 if (it->cmp_it.to < it->cmp_it.nglyphs)
7254 it->cmp_it.from = it->cmp_it.to;
7255 else
7257 it->cmp_it.id = -1;
7258 composition_compute_stop_pos (&it->cmp_it,
7259 IT_STRING_CHARPOS (*it),
7260 IT_STRING_BYTEPOS (*it),
7261 it->end_charpos, it->string);
7264 else if (! it->cmp_it.reversed_p)
7266 for (i = 0; i < it->cmp_it.nchars; i++)
7267 bidi_move_to_visually_next (&it->bidi_it);
7268 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7269 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7271 if (it->cmp_it.to < it->cmp_it.nglyphs)
7272 it->cmp_it.from = it->cmp_it.to;
7273 else
7275 ptrdiff_t stop = it->end_charpos;
7276 if (it->bidi_it.scan_dir < 0)
7277 stop = -1;
7278 composition_compute_stop_pos (&it->cmp_it,
7279 IT_STRING_CHARPOS (*it),
7280 IT_STRING_BYTEPOS (*it), stop,
7281 it->string);
7284 else
7286 for (i = 0; i < it->cmp_it.nchars; i++)
7287 bidi_move_to_visually_next (&it->bidi_it);
7288 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7289 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7290 if (it->cmp_it.from > 0)
7291 it->cmp_it.to = it->cmp_it.from;
7292 else
7294 ptrdiff_t stop = it->end_charpos;
7295 if (it->bidi_it.scan_dir < 0)
7296 stop = -1;
7297 composition_compute_stop_pos (&it->cmp_it,
7298 IT_STRING_CHARPOS (*it),
7299 IT_STRING_BYTEPOS (*it), stop,
7300 it->string);
7304 else
7306 if (!it->bidi_p
7307 /* If the string position is beyond string's end, it
7308 means next_element_from_string is padding the string
7309 with blanks, in which case we bypass the bidi
7310 iterator, because it cannot deal with such virtual
7311 characters. */
7312 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7314 IT_STRING_BYTEPOS (*it) += it->len;
7315 IT_STRING_CHARPOS (*it) += 1;
7317 else
7319 int prev_scan_dir = it->bidi_it.scan_dir;
7321 bidi_move_to_visually_next (&it->bidi_it);
7322 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7323 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7324 if (prev_scan_dir != it->bidi_it.scan_dir)
7326 ptrdiff_t stop = it->end_charpos;
7328 if (it->bidi_it.scan_dir < 0)
7329 stop = -1;
7330 composition_compute_stop_pos (&it->cmp_it,
7331 IT_STRING_CHARPOS (*it),
7332 IT_STRING_BYTEPOS (*it), stop,
7333 it->string);
7338 consider_string_end:
7340 if (it->current.overlay_string_index >= 0)
7342 /* IT->string is an overlay string. Advance to the
7343 next, if there is one. */
7344 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7346 it->ellipsis_p = 0;
7347 next_overlay_string (it);
7348 if (it->ellipsis_p)
7349 setup_for_ellipsis (it, 0);
7352 else
7354 /* IT->string is not an overlay string. If we reached
7355 its end, and there is something on IT->stack, proceed
7356 with what is on the stack. This can be either another
7357 string, this time an overlay string, or a buffer. */
7358 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7359 && it->sp > 0)
7361 pop_it (it);
7362 if (it->method == GET_FROM_STRING)
7363 goto consider_string_end;
7366 break;
7368 case GET_FROM_IMAGE:
7369 case GET_FROM_STRETCH:
7370 /* The position etc with which we have to proceed are on
7371 the stack. The position may be at the end of a string,
7372 if the `display' property takes up the whole string. */
7373 eassert (it->sp > 0);
7374 pop_it (it);
7375 if (it->method == GET_FROM_STRING)
7376 goto consider_string_end;
7377 break;
7379 default:
7380 /* There are no other methods defined, so this should be a bug. */
7381 emacs_abort ();
7384 eassert (it->method != GET_FROM_STRING
7385 || (STRINGP (it->string)
7386 && IT_STRING_CHARPOS (*it) >= 0));
7389 /* Load IT's display element fields with information about the next
7390 display element which comes from a display table entry or from the
7391 result of translating a control character to one of the forms `^C'
7392 or `\003'.
7394 IT->dpvec holds the glyphs to return as characters.
7395 IT->saved_face_id holds the face id before the display vector--it
7396 is restored into IT->face_id in set_iterator_to_next. */
7398 static int
7399 next_element_from_display_vector (struct it *it)
7401 Lisp_Object gc;
7403 /* Precondition. */
7404 eassert (it->dpvec && it->current.dpvec_index >= 0);
7406 it->face_id = it->saved_face_id;
7408 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7409 That seemed totally bogus - so I changed it... */
7410 gc = it->dpvec[it->current.dpvec_index];
7412 if (GLYPH_CODE_P (gc))
7414 it->c = GLYPH_CODE_CHAR (gc);
7415 it->len = CHAR_BYTES (it->c);
7417 /* The entry may contain a face id to use. Such a face id is
7418 the id of a Lisp face, not a realized face. A face id of
7419 zero means no face is specified. */
7420 if (it->dpvec_face_id >= 0)
7421 it->face_id = it->dpvec_face_id;
7422 else
7424 int lface_id = GLYPH_CODE_FACE (gc);
7425 if (lface_id > 0)
7426 it->face_id = merge_faces (it->f, Qt, lface_id,
7427 it->saved_face_id);
7430 else
7431 /* Display table entry is invalid. Return a space. */
7432 it->c = ' ', it->len = 1;
7434 /* Don't change position and object of the iterator here. They are
7435 still the values of the character that had this display table
7436 entry or was translated, and that's what we want. */
7437 it->what = IT_CHARACTER;
7438 return 1;
7441 /* Get the first element of string/buffer in the visual order, after
7442 being reseated to a new position in a string or a buffer. */
7443 static void
7444 get_visually_first_element (struct it *it)
7446 int string_p = STRINGP (it->string) || it->s;
7447 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7448 ptrdiff_t bob = (string_p ? 0 : BEGV);
7450 if (STRINGP (it->string))
7452 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7453 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7455 else
7457 it->bidi_it.charpos = IT_CHARPOS (*it);
7458 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7461 if (it->bidi_it.charpos == eob)
7463 /* Nothing to do, but reset the FIRST_ELT flag, like
7464 bidi_paragraph_init does, because we are not going to
7465 call it. */
7466 it->bidi_it.first_elt = 0;
7468 else if (it->bidi_it.charpos == bob
7469 || (!string_p
7470 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7471 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7473 /* If we are at the beginning of a line/string, we can produce
7474 the next element right away. */
7475 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7476 bidi_move_to_visually_next (&it->bidi_it);
7478 else
7480 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7482 /* We need to prime the bidi iterator starting at the line's or
7483 string's beginning, before we will be able to produce the
7484 next element. */
7485 if (string_p)
7486 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7487 else
7488 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7489 IT_BYTEPOS (*it), -1,
7490 &it->bidi_it.bytepos);
7491 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7494 /* Now return to buffer/string position where we were asked
7495 to get the next display element, and produce that. */
7496 bidi_move_to_visually_next (&it->bidi_it);
7498 while (it->bidi_it.bytepos != orig_bytepos
7499 && it->bidi_it.charpos < eob);
7502 /* Adjust IT's position information to where we ended up. */
7503 if (STRINGP (it->string))
7505 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7506 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7508 else
7510 IT_CHARPOS (*it) = it->bidi_it.charpos;
7511 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7514 if (STRINGP (it->string) || !it->s)
7516 ptrdiff_t stop, charpos, bytepos;
7518 if (STRINGP (it->string))
7520 eassert (!it->s);
7521 stop = SCHARS (it->string);
7522 if (stop > it->end_charpos)
7523 stop = it->end_charpos;
7524 charpos = IT_STRING_CHARPOS (*it);
7525 bytepos = IT_STRING_BYTEPOS (*it);
7527 else
7529 stop = it->end_charpos;
7530 charpos = IT_CHARPOS (*it);
7531 bytepos = IT_BYTEPOS (*it);
7533 if (it->bidi_it.scan_dir < 0)
7534 stop = -1;
7535 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7536 it->string);
7540 /* Load IT with the next display element from Lisp string IT->string.
7541 IT->current.string_pos is the current position within the string.
7542 If IT->current.overlay_string_index >= 0, the Lisp string is an
7543 overlay string. */
7545 static int
7546 next_element_from_string (struct it *it)
7548 struct text_pos position;
7550 eassert (STRINGP (it->string));
7551 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7552 eassert (IT_STRING_CHARPOS (*it) >= 0);
7553 position = it->current.string_pos;
7555 /* With bidi reordering, the character to display might not be the
7556 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7557 that we were reseat()ed to a new string, whose paragraph
7558 direction is not known. */
7559 if (it->bidi_p && it->bidi_it.first_elt)
7561 get_visually_first_element (it);
7562 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7565 /* Time to check for invisible text? */
7566 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7568 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7570 if (!(!it->bidi_p
7571 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7572 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7574 /* With bidi non-linear iteration, we could find
7575 ourselves far beyond the last computed stop_charpos,
7576 with several other stop positions in between that we
7577 missed. Scan them all now, in buffer's logical
7578 order, until we find and handle the last stop_charpos
7579 that precedes our current position. */
7580 handle_stop_backwards (it, it->stop_charpos);
7581 return GET_NEXT_DISPLAY_ELEMENT (it);
7583 else
7585 if (it->bidi_p)
7587 /* Take note of the stop position we just moved
7588 across, for when we will move back across it. */
7589 it->prev_stop = it->stop_charpos;
7590 /* If we are at base paragraph embedding level, take
7591 note of the last stop position seen at this
7592 level. */
7593 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7594 it->base_level_stop = it->stop_charpos;
7596 handle_stop (it);
7598 /* Since a handler may have changed IT->method, we must
7599 recurse here. */
7600 return GET_NEXT_DISPLAY_ELEMENT (it);
7603 else if (it->bidi_p
7604 /* If we are before prev_stop, we may have overstepped
7605 on our way backwards a stop_pos, and if so, we need
7606 to handle that stop_pos. */
7607 && IT_STRING_CHARPOS (*it) < it->prev_stop
7608 /* We can sometimes back up for reasons that have nothing
7609 to do with bidi reordering. E.g., compositions. The
7610 code below is only needed when we are above the base
7611 embedding level, so test for that explicitly. */
7612 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7614 /* If we lost track of base_level_stop, we have no better
7615 place for handle_stop_backwards to start from than string
7616 beginning. This happens, e.g., when we were reseated to
7617 the previous screenful of text by vertical-motion. */
7618 if (it->base_level_stop <= 0
7619 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7620 it->base_level_stop = 0;
7621 handle_stop_backwards (it, it->base_level_stop);
7622 return GET_NEXT_DISPLAY_ELEMENT (it);
7626 if (it->current.overlay_string_index >= 0)
7628 /* Get the next character from an overlay string. In overlay
7629 strings, there is no field width or padding with spaces to
7630 do. */
7631 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7633 it->what = IT_EOB;
7634 return 0;
7636 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7637 IT_STRING_BYTEPOS (*it),
7638 it->bidi_it.scan_dir < 0
7639 ? -1
7640 : SCHARS (it->string))
7641 && next_element_from_composition (it))
7643 return 1;
7645 else if (STRING_MULTIBYTE (it->string))
7647 const unsigned char *s = (SDATA (it->string)
7648 + IT_STRING_BYTEPOS (*it));
7649 it->c = string_char_and_length (s, &it->len);
7651 else
7653 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7654 it->len = 1;
7657 else
7659 /* Get the next character from a Lisp string that is not an
7660 overlay string. Such strings come from the mode line, for
7661 example. We may have to pad with spaces, or truncate the
7662 string. See also next_element_from_c_string. */
7663 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7665 it->what = IT_EOB;
7666 return 0;
7668 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7670 /* Pad with spaces. */
7671 it->c = ' ', it->len = 1;
7672 CHARPOS (position) = BYTEPOS (position) = -1;
7674 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7675 IT_STRING_BYTEPOS (*it),
7676 it->bidi_it.scan_dir < 0
7677 ? -1
7678 : it->string_nchars)
7679 && next_element_from_composition (it))
7681 return 1;
7683 else if (STRING_MULTIBYTE (it->string))
7685 const unsigned char *s = (SDATA (it->string)
7686 + IT_STRING_BYTEPOS (*it));
7687 it->c = string_char_and_length (s, &it->len);
7689 else
7691 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7692 it->len = 1;
7696 /* Record what we have and where it came from. */
7697 it->what = IT_CHARACTER;
7698 it->object = it->string;
7699 it->position = position;
7700 return 1;
7704 /* Load IT with next display element from C string IT->s.
7705 IT->string_nchars is the maximum number of characters to return
7706 from the string. IT->end_charpos may be greater than
7707 IT->string_nchars when this function is called, in which case we
7708 may have to return padding spaces. Value is zero if end of string
7709 reached, including padding spaces. */
7711 static int
7712 next_element_from_c_string (struct it *it)
7714 int success_p = 1;
7716 eassert (it->s);
7717 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7718 it->what = IT_CHARACTER;
7719 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7720 it->object = Qnil;
7722 /* With bidi reordering, the character to display might not be the
7723 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7724 we were reseated to a new string, whose paragraph direction is
7725 not known. */
7726 if (it->bidi_p && it->bidi_it.first_elt)
7727 get_visually_first_element (it);
7729 /* IT's position can be greater than IT->string_nchars in case a
7730 field width or precision has been specified when the iterator was
7731 initialized. */
7732 if (IT_CHARPOS (*it) >= it->end_charpos)
7734 /* End of the game. */
7735 it->what = IT_EOB;
7736 success_p = 0;
7738 else if (IT_CHARPOS (*it) >= it->string_nchars)
7740 /* Pad with spaces. */
7741 it->c = ' ', it->len = 1;
7742 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7744 else if (it->multibyte_p)
7745 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7746 else
7747 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7749 return success_p;
7753 /* Set up IT to return characters from an ellipsis, if appropriate.
7754 The definition of the ellipsis glyphs may come from a display table
7755 entry. This function fills IT with the first glyph from the
7756 ellipsis if an ellipsis is to be displayed. */
7758 static int
7759 next_element_from_ellipsis (struct it *it)
7761 if (it->selective_display_ellipsis_p)
7762 setup_for_ellipsis (it, it->len);
7763 else
7765 /* The face at the current position may be different from the
7766 face we find after the invisible text. Remember what it
7767 was in IT->saved_face_id, and signal that it's there by
7768 setting face_before_selective_p. */
7769 it->saved_face_id = it->face_id;
7770 it->method = GET_FROM_BUFFER;
7771 it->object = it->w->contents;
7772 reseat_at_next_visible_line_start (it, 1);
7773 it->face_before_selective_p = 1;
7776 return GET_NEXT_DISPLAY_ELEMENT (it);
7780 /* Deliver an image display element. The iterator IT is already
7781 filled with image information (done in handle_display_prop). Value
7782 is always 1. */
7785 static int
7786 next_element_from_image (struct it *it)
7788 it->what = IT_IMAGE;
7789 it->ignore_overlay_strings_at_pos_p = 0;
7790 return 1;
7794 /* Fill iterator IT with next display element from a stretch glyph
7795 property. IT->object is the value of the text property. Value is
7796 always 1. */
7798 static int
7799 next_element_from_stretch (struct it *it)
7801 it->what = IT_STRETCH;
7802 return 1;
7805 /* Scan backwards from IT's current position until we find a stop
7806 position, or until BEGV. This is called when we find ourself
7807 before both the last known prev_stop and base_level_stop while
7808 reordering bidirectional text. */
7810 static void
7811 compute_stop_pos_backwards (struct it *it)
7813 const int SCAN_BACK_LIMIT = 1000;
7814 struct text_pos pos;
7815 struct display_pos save_current = it->current;
7816 struct text_pos save_position = it->position;
7817 ptrdiff_t charpos = IT_CHARPOS (*it);
7818 ptrdiff_t where_we_are = charpos;
7819 ptrdiff_t save_stop_pos = it->stop_charpos;
7820 ptrdiff_t save_end_pos = it->end_charpos;
7822 eassert (NILP (it->string) && !it->s);
7823 eassert (it->bidi_p);
7824 it->bidi_p = 0;
7827 it->end_charpos = min (charpos + 1, ZV);
7828 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7829 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7830 reseat_1 (it, pos, 0);
7831 compute_stop_pos (it);
7832 /* We must advance forward, right? */
7833 if (it->stop_charpos <= charpos)
7834 emacs_abort ();
7836 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7838 if (it->stop_charpos <= where_we_are)
7839 it->prev_stop = it->stop_charpos;
7840 else
7841 it->prev_stop = BEGV;
7842 it->bidi_p = 1;
7843 it->current = save_current;
7844 it->position = save_position;
7845 it->stop_charpos = save_stop_pos;
7846 it->end_charpos = save_end_pos;
7849 /* Scan forward from CHARPOS in the current buffer/string, until we
7850 find a stop position > current IT's position. Then handle the stop
7851 position before that. This is called when we bump into a stop
7852 position while reordering bidirectional text. CHARPOS should be
7853 the last previously processed stop_pos (or BEGV/0, if none were
7854 processed yet) whose position is less that IT's current
7855 position. */
7857 static void
7858 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7860 int bufp = !STRINGP (it->string);
7861 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7862 struct display_pos save_current = it->current;
7863 struct text_pos save_position = it->position;
7864 struct text_pos pos1;
7865 ptrdiff_t next_stop;
7867 /* Scan in strict logical order. */
7868 eassert (it->bidi_p);
7869 it->bidi_p = 0;
7872 it->prev_stop = charpos;
7873 if (bufp)
7875 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7876 reseat_1 (it, pos1, 0);
7878 else
7879 it->current.string_pos = string_pos (charpos, it->string);
7880 compute_stop_pos (it);
7881 /* We must advance forward, right? */
7882 if (it->stop_charpos <= it->prev_stop)
7883 emacs_abort ();
7884 charpos = it->stop_charpos;
7886 while (charpos <= where_we_are);
7888 it->bidi_p = 1;
7889 it->current = save_current;
7890 it->position = save_position;
7891 next_stop = it->stop_charpos;
7892 it->stop_charpos = it->prev_stop;
7893 handle_stop (it);
7894 it->stop_charpos = next_stop;
7897 /* Load IT with the next display element from current_buffer. Value
7898 is zero if end of buffer reached. IT->stop_charpos is the next
7899 position at which to stop and check for text properties or buffer
7900 end. */
7902 static int
7903 next_element_from_buffer (struct it *it)
7905 int success_p = 1;
7907 eassert (IT_CHARPOS (*it) >= BEGV);
7908 eassert (NILP (it->string) && !it->s);
7909 eassert (!it->bidi_p
7910 || (EQ (it->bidi_it.string.lstring, Qnil)
7911 && it->bidi_it.string.s == NULL));
7913 /* With bidi reordering, the character to display might not be the
7914 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7915 we were reseat()ed to a new buffer position, which is potentially
7916 a different paragraph. */
7917 if (it->bidi_p && it->bidi_it.first_elt)
7919 get_visually_first_element (it);
7920 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7923 if (IT_CHARPOS (*it) >= it->stop_charpos)
7925 if (IT_CHARPOS (*it) >= it->end_charpos)
7927 int overlay_strings_follow_p;
7929 /* End of the game, except when overlay strings follow that
7930 haven't been returned yet. */
7931 if (it->overlay_strings_at_end_processed_p)
7932 overlay_strings_follow_p = 0;
7933 else
7935 it->overlay_strings_at_end_processed_p = 1;
7936 overlay_strings_follow_p = get_overlay_strings (it, 0);
7939 if (overlay_strings_follow_p)
7940 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7941 else
7943 it->what = IT_EOB;
7944 it->position = it->current.pos;
7945 success_p = 0;
7948 else if (!(!it->bidi_p
7949 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7950 || IT_CHARPOS (*it) == it->stop_charpos))
7952 /* With bidi non-linear iteration, we could find ourselves
7953 far beyond the last computed stop_charpos, with several
7954 other stop positions in between that we missed. Scan
7955 them all now, in buffer's logical order, until we find
7956 and handle the last stop_charpos that precedes our
7957 current position. */
7958 handle_stop_backwards (it, it->stop_charpos);
7959 return GET_NEXT_DISPLAY_ELEMENT (it);
7961 else
7963 if (it->bidi_p)
7965 /* Take note of the stop position we just moved across,
7966 for when we will move back across it. */
7967 it->prev_stop = it->stop_charpos;
7968 /* If we are at base paragraph embedding level, take
7969 note of the last stop position seen at this
7970 level. */
7971 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7972 it->base_level_stop = it->stop_charpos;
7974 handle_stop (it);
7975 return GET_NEXT_DISPLAY_ELEMENT (it);
7978 else if (it->bidi_p
7979 /* If we are before prev_stop, we may have overstepped on
7980 our way backwards a stop_pos, and if so, we need to
7981 handle that stop_pos. */
7982 && IT_CHARPOS (*it) < it->prev_stop
7983 /* We can sometimes back up for reasons that have nothing
7984 to do with bidi reordering. E.g., compositions. The
7985 code below is only needed when we are above the base
7986 embedding level, so test for that explicitly. */
7987 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7989 if (it->base_level_stop <= 0
7990 || IT_CHARPOS (*it) < it->base_level_stop)
7992 /* If we lost track of base_level_stop, we need to find
7993 prev_stop by looking backwards. This happens, e.g., when
7994 we were reseated to the previous screenful of text by
7995 vertical-motion. */
7996 it->base_level_stop = BEGV;
7997 compute_stop_pos_backwards (it);
7998 handle_stop_backwards (it, it->prev_stop);
8000 else
8001 handle_stop_backwards (it, it->base_level_stop);
8002 return GET_NEXT_DISPLAY_ELEMENT (it);
8004 else
8006 /* No face changes, overlays etc. in sight, so just return a
8007 character from current_buffer. */
8008 unsigned char *p;
8009 ptrdiff_t stop;
8011 /* Maybe run the redisplay end trigger hook. Performance note:
8012 This doesn't seem to cost measurable time. */
8013 if (it->redisplay_end_trigger_charpos
8014 && it->glyph_row
8015 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8016 run_redisplay_end_trigger_hook (it);
8018 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8019 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8020 stop)
8021 && next_element_from_composition (it))
8023 return 1;
8026 /* Get the next character, maybe multibyte. */
8027 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8028 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8029 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8030 else
8031 it->c = *p, it->len = 1;
8033 /* Record what we have and where it came from. */
8034 it->what = IT_CHARACTER;
8035 it->object = it->w->contents;
8036 it->position = it->current.pos;
8038 /* Normally we return the character found above, except when we
8039 really want to return an ellipsis for selective display. */
8040 if (it->selective)
8042 if (it->c == '\n')
8044 /* A value of selective > 0 means hide lines indented more
8045 than that number of columns. */
8046 if (it->selective > 0
8047 && IT_CHARPOS (*it) + 1 < ZV
8048 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8049 IT_BYTEPOS (*it) + 1,
8050 it->selective))
8052 success_p = next_element_from_ellipsis (it);
8053 it->dpvec_char_len = -1;
8056 else if (it->c == '\r' && it->selective == -1)
8058 /* A value of selective == -1 means that everything from the
8059 CR to the end of the line is invisible, with maybe an
8060 ellipsis displayed for it. */
8061 success_p = next_element_from_ellipsis (it);
8062 it->dpvec_char_len = -1;
8067 /* Value is zero if end of buffer reached. */
8068 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8069 return success_p;
8073 /* Run the redisplay end trigger hook for IT. */
8075 static void
8076 run_redisplay_end_trigger_hook (struct it *it)
8078 Lisp_Object args[3];
8080 /* IT->glyph_row should be non-null, i.e. we should be actually
8081 displaying something, or otherwise we should not run the hook. */
8082 eassert (it->glyph_row);
8084 /* Set up hook arguments. */
8085 args[0] = Qredisplay_end_trigger_functions;
8086 args[1] = it->window;
8087 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8088 it->redisplay_end_trigger_charpos = 0;
8090 /* Since we are *trying* to run these functions, don't try to run
8091 them again, even if they get an error. */
8092 wset_redisplay_end_trigger (it->w, Qnil);
8093 Frun_hook_with_args (3, args);
8095 /* Notice if it changed the face of the character we are on. */
8096 handle_face_prop (it);
8100 /* Deliver a composition display element. Unlike the other
8101 next_element_from_XXX, this function is not registered in the array
8102 get_next_element[]. It is called from next_element_from_buffer and
8103 next_element_from_string when necessary. */
8105 static int
8106 next_element_from_composition (struct it *it)
8108 it->what = IT_COMPOSITION;
8109 it->len = it->cmp_it.nbytes;
8110 if (STRINGP (it->string))
8112 if (it->c < 0)
8114 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8115 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8116 return 0;
8118 it->position = it->current.string_pos;
8119 it->object = it->string;
8120 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8121 IT_STRING_BYTEPOS (*it), it->string);
8123 else
8125 if (it->c < 0)
8127 IT_CHARPOS (*it) += it->cmp_it.nchars;
8128 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8129 if (it->bidi_p)
8131 if (it->bidi_it.new_paragraph)
8132 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8133 /* Resync the bidi iterator with IT's new position.
8134 FIXME: this doesn't support bidirectional text. */
8135 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8136 bidi_move_to_visually_next (&it->bidi_it);
8138 return 0;
8140 it->position = it->current.pos;
8141 it->object = it->w->contents;
8142 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8143 IT_BYTEPOS (*it), Qnil);
8145 return 1;
8150 /***********************************************************************
8151 Moving an iterator without producing glyphs
8152 ***********************************************************************/
8154 /* Check if iterator is at a position corresponding to a valid buffer
8155 position after some move_it_ call. */
8157 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8158 ((it)->method == GET_FROM_STRING \
8159 ? IT_STRING_CHARPOS (*it) == 0 \
8160 : 1)
8163 /* Move iterator IT to a specified buffer or X position within one
8164 line on the display without producing glyphs.
8166 OP should be a bit mask including some or all of these bits:
8167 MOVE_TO_X: Stop upon reaching x-position TO_X.
8168 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8169 Regardless of OP's value, stop upon reaching the end of the display line.
8171 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8172 This means, in particular, that TO_X includes window's horizontal
8173 scroll amount.
8175 The return value has several possible values that
8176 say what condition caused the scan to stop:
8178 MOVE_POS_MATCH_OR_ZV
8179 - when TO_POS or ZV was reached.
8181 MOVE_X_REACHED
8182 -when TO_X was reached before TO_POS or ZV were reached.
8184 MOVE_LINE_CONTINUED
8185 - when we reached the end of the display area and the line must
8186 be continued.
8188 MOVE_LINE_TRUNCATED
8189 - when we reached the end of the display area and the line is
8190 truncated.
8192 MOVE_NEWLINE_OR_CR
8193 - when we stopped at a line end, i.e. a newline or a CR and selective
8194 display is on. */
8196 static enum move_it_result
8197 move_it_in_display_line_to (struct it *it,
8198 ptrdiff_t to_charpos, int to_x,
8199 enum move_operation_enum op)
8201 enum move_it_result result = MOVE_UNDEFINED;
8202 struct glyph_row *saved_glyph_row;
8203 struct it wrap_it, atpos_it, atx_it, ppos_it;
8204 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8205 void *ppos_data = NULL;
8206 int may_wrap = 0;
8207 enum it_method prev_method = it->method;
8208 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8209 int saw_smaller_pos = prev_pos < to_charpos;
8211 /* Don't produce glyphs in produce_glyphs. */
8212 saved_glyph_row = it->glyph_row;
8213 it->glyph_row = NULL;
8215 /* Use wrap_it to save a copy of IT wherever a word wrap could
8216 occur. Use atpos_it to save a copy of IT at the desired buffer
8217 position, if found, so that we can scan ahead and check if the
8218 word later overshoots the window edge. Use atx_it similarly, for
8219 pixel positions. */
8220 wrap_it.sp = -1;
8221 atpos_it.sp = -1;
8222 atx_it.sp = -1;
8224 /* Use ppos_it under bidi reordering to save a copy of IT for the
8225 position > CHARPOS that is the closest to CHARPOS. We restore
8226 that position in IT when we have scanned the entire display line
8227 without finding a match for CHARPOS and all the character
8228 positions are greater than CHARPOS. */
8229 if (it->bidi_p)
8231 SAVE_IT (ppos_it, *it, ppos_data);
8232 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8233 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8234 SAVE_IT (ppos_it, *it, ppos_data);
8237 #define BUFFER_POS_REACHED_P() \
8238 ((op & MOVE_TO_POS) != 0 \
8239 && BUFFERP (it->object) \
8240 && (IT_CHARPOS (*it) == to_charpos \
8241 || ((!it->bidi_p \
8242 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8243 && IT_CHARPOS (*it) > to_charpos) \
8244 || (it->what == IT_COMPOSITION \
8245 && ((IT_CHARPOS (*it) > to_charpos \
8246 && to_charpos >= it->cmp_it.charpos) \
8247 || (IT_CHARPOS (*it) < to_charpos \
8248 && to_charpos <= it->cmp_it.charpos)))) \
8249 && (it->method == GET_FROM_BUFFER \
8250 || (it->method == GET_FROM_DISPLAY_VECTOR \
8251 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8253 /* If there's a line-/wrap-prefix, handle it. */
8254 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8255 && it->current_y < it->last_visible_y)
8256 handle_line_prefix (it);
8258 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8259 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8261 while (1)
8263 int x, i, ascent = 0, descent = 0;
8265 /* Utility macro to reset an iterator with x, ascent, and descent. */
8266 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8267 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8268 (IT)->max_descent = descent)
8270 /* Stop if we move beyond TO_CHARPOS (after an image or a
8271 display string or stretch glyph). */
8272 if ((op & MOVE_TO_POS) != 0
8273 && BUFFERP (it->object)
8274 && it->method == GET_FROM_BUFFER
8275 && (((!it->bidi_p
8276 /* When the iterator is at base embedding level, we
8277 are guaranteed that characters are delivered for
8278 display in strictly increasing order of their
8279 buffer positions. */
8280 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8281 && IT_CHARPOS (*it) > to_charpos)
8282 || (it->bidi_p
8283 && (prev_method == GET_FROM_IMAGE
8284 || prev_method == GET_FROM_STRETCH
8285 || prev_method == GET_FROM_STRING)
8286 /* Passed TO_CHARPOS from left to right. */
8287 && ((prev_pos < to_charpos
8288 && IT_CHARPOS (*it) > to_charpos)
8289 /* Passed TO_CHARPOS from right to left. */
8290 || (prev_pos > to_charpos
8291 && IT_CHARPOS (*it) < to_charpos)))))
8293 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8295 result = MOVE_POS_MATCH_OR_ZV;
8296 break;
8298 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8299 /* If wrap_it is valid, the current position might be in a
8300 word that is wrapped. So, save the iterator in
8301 atpos_it and continue to see if wrapping happens. */
8302 SAVE_IT (atpos_it, *it, atpos_data);
8305 /* Stop when ZV reached.
8306 We used to stop here when TO_CHARPOS reached as well, but that is
8307 too soon if this glyph does not fit on this line. So we handle it
8308 explicitly below. */
8309 if (!get_next_display_element (it))
8311 result = MOVE_POS_MATCH_OR_ZV;
8312 break;
8315 if (it->line_wrap == TRUNCATE)
8317 if (BUFFER_POS_REACHED_P ())
8319 result = MOVE_POS_MATCH_OR_ZV;
8320 break;
8323 else
8325 if (it->line_wrap == WORD_WRAP)
8327 if (IT_DISPLAYING_WHITESPACE (it))
8328 may_wrap = 1;
8329 else if (may_wrap)
8331 /* We have reached a glyph that follows one or more
8332 whitespace characters. If the position is
8333 already found, we are done. */
8334 if (atpos_it.sp >= 0)
8336 RESTORE_IT (it, &atpos_it, atpos_data);
8337 result = MOVE_POS_MATCH_OR_ZV;
8338 goto done;
8340 if (atx_it.sp >= 0)
8342 RESTORE_IT (it, &atx_it, atx_data);
8343 result = MOVE_X_REACHED;
8344 goto done;
8346 /* Otherwise, we can wrap here. */
8347 SAVE_IT (wrap_it, *it, wrap_data);
8348 may_wrap = 0;
8353 /* Remember the line height for the current line, in case
8354 the next element doesn't fit on the line. */
8355 ascent = it->max_ascent;
8356 descent = it->max_descent;
8358 /* The call to produce_glyphs will get the metrics of the
8359 display element IT is loaded with. Record the x-position
8360 before this display element, in case it doesn't fit on the
8361 line. */
8362 x = it->current_x;
8364 PRODUCE_GLYPHS (it);
8366 if (it->area != TEXT_AREA)
8368 prev_method = it->method;
8369 if (it->method == GET_FROM_BUFFER)
8370 prev_pos = IT_CHARPOS (*it);
8371 set_iterator_to_next (it, 1);
8372 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8373 SET_TEXT_POS (this_line_min_pos,
8374 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8375 if (it->bidi_p
8376 && (op & MOVE_TO_POS)
8377 && IT_CHARPOS (*it) > to_charpos
8378 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8379 SAVE_IT (ppos_it, *it, ppos_data);
8380 continue;
8383 /* The number of glyphs we get back in IT->nglyphs will normally
8384 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8385 character on a terminal frame, or (iii) a line end. For the
8386 second case, IT->nglyphs - 1 padding glyphs will be present.
8387 (On X frames, there is only one glyph produced for a
8388 composite character.)
8390 The behavior implemented below means, for continuation lines,
8391 that as many spaces of a TAB as fit on the current line are
8392 displayed there. For terminal frames, as many glyphs of a
8393 multi-glyph character are displayed in the current line, too.
8394 This is what the old redisplay code did, and we keep it that
8395 way. Under X, the whole shape of a complex character must
8396 fit on the line or it will be completely displayed in the
8397 next line.
8399 Note that both for tabs and padding glyphs, all glyphs have
8400 the same width. */
8401 if (it->nglyphs)
8403 /* More than one glyph or glyph doesn't fit on line. All
8404 glyphs have the same width. */
8405 int single_glyph_width = it->pixel_width / it->nglyphs;
8406 int new_x;
8407 int x_before_this_char = x;
8408 int hpos_before_this_char = it->hpos;
8410 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8412 new_x = x + single_glyph_width;
8414 /* We want to leave anything reaching TO_X to the caller. */
8415 if ((op & MOVE_TO_X) && new_x > to_x)
8417 if (BUFFER_POS_REACHED_P ())
8419 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8420 goto buffer_pos_reached;
8421 if (atpos_it.sp < 0)
8423 SAVE_IT (atpos_it, *it, atpos_data);
8424 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8427 else
8429 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8431 it->current_x = x;
8432 result = MOVE_X_REACHED;
8433 break;
8435 if (atx_it.sp < 0)
8437 SAVE_IT (atx_it, *it, atx_data);
8438 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8443 if (/* Lines are continued. */
8444 it->line_wrap != TRUNCATE
8445 && (/* And glyph doesn't fit on the line. */
8446 new_x > it->last_visible_x
8447 /* Or it fits exactly and we're on a window
8448 system frame. */
8449 || (new_x == it->last_visible_x
8450 && FRAME_WINDOW_P (it->f)
8451 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8452 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8453 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8455 if (/* IT->hpos == 0 means the very first glyph
8456 doesn't fit on the line, e.g. a wide image. */
8457 it->hpos == 0
8458 || (new_x == it->last_visible_x
8459 && FRAME_WINDOW_P (it->f)))
8461 ++it->hpos;
8462 it->current_x = new_x;
8464 /* The character's last glyph just barely fits
8465 in this row. */
8466 if (i == it->nglyphs - 1)
8468 /* If this is the destination position,
8469 return a position *before* it in this row,
8470 now that we know it fits in this row. */
8471 if (BUFFER_POS_REACHED_P ())
8473 if (it->line_wrap != WORD_WRAP
8474 || wrap_it.sp < 0)
8476 it->hpos = hpos_before_this_char;
8477 it->current_x = x_before_this_char;
8478 result = MOVE_POS_MATCH_OR_ZV;
8479 break;
8481 if (it->line_wrap == WORD_WRAP
8482 && atpos_it.sp < 0)
8484 SAVE_IT (atpos_it, *it, atpos_data);
8485 atpos_it.current_x = x_before_this_char;
8486 atpos_it.hpos = hpos_before_this_char;
8490 prev_method = it->method;
8491 if (it->method == GET_FROM_BUFFER)
8492 prev_pos = IT_CHARPOS (*it);
8493 set_iterator_to_next (it, 1);
8494 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8495 SET_TEXT_POS (this_line_min_pos,
8496 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8497 /* On graphical terminals, newlines may
8498 "overflow" into the fringe if
8499 overflow-newline-into-fringe is non-nil.
8500 On text terminals, and on graphical
8501 terminals with no right margin, newlines
8502 may overflow into the last glyph on the
8503 display line.*/
8504 if (!FRAME_WINDOW_P (it->f)
8505 || ((it->bidi_p
8506 && it->bidi_it.paragraph_dir == R2L)
8507 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8508 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8509 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8511 if (!get_next_display_element (it))
8513 result = MOVE_POS_MATCH_OR_ZV;
8514 break;
8516 if (BUFFER_POS_REACHED_P ())
8518 if (ITERATOR_AT_END_OF_LINE_P (it))
8519 result = MOVE_POS_MATCH_OR_ZV;
8520 else
8521 result = MOVE_LINE_CONTINUED;
8522 break;
8524 if (ITERATOR_AT_END_OF_LINE_P (it))
8526 result = MOVE_NEWLINE_OR_CR;
8527 break;
8532 else
8533 IT_RESET_X_ASCENT_DESCENT (it);
8535 if (wrap_it.sp >= 0)
8537 RESTORE_IT (it, &wrap_it, wrap_data);
8538 atpos_it.sp = -1;
8539 atx_it.sp = -1;
8542 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8543 IT_CHARPOS (*it)));
8544 result = MOVE_LINE_CONTINUED;
8545 break;
8548 if (BUFFER_POS_REACHED_P ())
8550 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8551 goto buffer_pos_reached;
8552 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8554 SAVE_IT (atpos_it, *it, atpos_data);
8555 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8559 if (new_x > it->first_visible_x)
8561 /* Glyph is visible. Increment number of glyphs that
8562 would be displayed. */
8563 ++it->hpos;
8567 if (result != MOVE_UNDEFINED)
8568 break;
8570 else if (BUFFER_POS_REACHED_P ())
8572 buffer_pos_reached:
8573 IT_RESET_X_ASCENT_DESCENT (it);
8574 result = MOVE_POS_MATCH_OR_ZV;
8575 break;
8577 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8579 /* Stop when TO_X specified and reached. This check is
8580 necessary here because of lines consisting of a line end,
8581 only. The line end will not produce any glyphs and we
8582 would never get MOVE_X_REACHED. */
8583 eassert (it->nglyphs == 0);
8584 result = MOVE_X_REACHED;
8585 break;
8588 /* Is this a line end? If yes, we're done. */
8589 if (ITERATOR_AT_END_OF_LINE_P (it))
8591 /* If we are past TO_CHARPOS, but never saw any character
8592 positions smaller than TO_CHARPOS, return
8593 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8594 did. */
8595 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8597 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8599 if (IT_CHARPOS (ppos_it) < ZV)
8601 RESTORE_IT (it, &ppos_it, ppos_data);
8602 result = MOVE_POS_MATCH_OR_ZV;
8604 else
8605 goto buffer_pos_reached;
8607 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8608 && IT_CHARPOS (*it) > to_charpos)
8609 goto buffer_pos_reached;
8610 else
8611 result = MOVE_NEWLINE_OR_CR;
8613 else
8614 result = MOVE_NEWLINE_OR_CR;
8615 break;
8618 prev_method = it->method;
8619 if (it->method == GET_FROM_BUFFER)
8620 prev_pos = IT_CHARPOS (*it);
8621 /* The current display element has been consumed. Advance
8622 to the next. */
8623 set_iterator_to_next (it, 1);
8624 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8625 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8626 if (IT_CHARPOS (*it) < to_charpos)
8627 saw_smaller_pos = 1;
8628 if (it->bidi_p
8629 && (op & MOVE_TO_POS)
8630 && IT_CHARPOS (*it) >= to_charpos
8631 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8632 SAVE_IT (ppos_it, *it, ppos_data);
8634 /* Stop if lines are truncated and IT's current x-position is
8635 past the right edge of the window now. */
8636 if (it->line_wrap == TRUNCATE
8637 && it->current_x >= it->last_visible_x)
8639 if (!FRAME_WINDOW_P (it->f)
8640 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8641 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8642 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8643 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8645 int at_eob_p = 0;
8647 if ((at_eob_p = !get_next_display_element (it))
8648 || BUFFER_POS_REACHED_P ()
8649 /* If we are past TO_CHARPOS, but never saw any
8650 character positions smaller than TO_CHARPOS,
8651 return MOVE_POS_MATCH_OR_ZV, like the
8652 unidirectional display did. */
8653 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8654 && !saw_smaller_pos
8655 && IT_CHARPOS (*it) > to_charpos))
8657 if (it->bidi_p
8658 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8659 RESTORE_IT (it, &ppos_it, ppos_data);
8660 result = MOVE_POS_MATCH_OR_ZV;
8661 break;
8663 if (ITERATOR_AT_END_OF_LINE_P (it))
8665 result = MOVE_NEWLINE_OR_CR;
8666 break;
8669 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8670 && !saw_smaller_pos
8671 && IT_CHARPOS (*it) > to_charpos)
8673 if (IT_CHARPOS (ppos_it) < ZV)
8674 RESTORE_IT (it, &ppos_it, ppos_data);
8675 result = MOVE_POS_MATCH_OR_ZV;
8676 break;
8678 result = MOVE_LINE_TRUNCATED;
8679 break;
8681 #undef IT_RESET_X_ASCENT_DESCENT
8684 #undef BUFFER_POS_REACHED_P
8686 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8687 restore the saved iterator. */
8688 if (atpos_it.sp >= 0)
8689 RESTORE_IT (it, &atpos_it, atpos_data);
8690 else if (atx_it.sp >= 0)
8691 RESTORE_IT (it, &atx_it, atx_data);
8693 done:
8695 if (atpos_data)
8696 bidi_unshelve_cache (atpos_data, 1);
8697 if (atx_data)
8698 bidi_unshelve_cache (atx_data, 1);
8699 if (wrap_data)
8700 bidi_unshelve_cache (wrap_data, 1);
8701 if (ppos_data)
8702 bidi_unshelve_cache (ppos_data, 1);
8704 /* Restore the iterator settings altered at the beginning of this
8705 function. */
8706 it->glyph_row = saved_glyph_row;
8707 return result;
8710 /* For external use. */
8711 void
8712 move_it_in_display_line (struct it *it,
8713 ptrdiff_t to_charpos, int to_x,
8714 enum move_operation_enum op)
8716 if (it->line_wrap == WORD_WRAP
8717 && (op & MOVE_TO_X))
8719 struct it save_it;
8720 void *save_data = NULL;
8721 int skip;
8723 SAVE_IT (save_it, *it, save_data);
8724 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8725 /* When word-wrap is on, TO_X may lie past the end
8726 of a wrapped line. Then it->current is the
8727 character on the next line, so backtrack to the
8728 space before the wrap point. */
8729 if (skip == MOVE_LINE_CONTINUED)
8731 int prev_x = max (it->current_x - 1, 0);
8732 RESTORE_IT (it, &save_it, save_data);
8733 move_it_in_display_line_to
8734 (it, -1, prev_x, MOVE_TO_X);
8736 else
8737 bidi_unshelve_cache (save_data, 1);
8739 else
8740 move_it_in_display_line_to (it, to_charpos, to_x, op);
8744 /* Move IT forward until it satisfies one or more of the criteria in
8745 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8747 OP is a bit-mask that specifies where to stop, and in particular,
8748 which of those four position arguments makes a difference. See the
8749 description of enum move_operation_enum.
8751 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8752 screen line, this function will set IT to the next position that is
8753 displayed to the right of TO_CHARPOS on the screen. */
8755 void
8756 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8758 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8759 int line_height, line_start_x = 0, reached = 0;
8760 void *backup_data = NULL;
8762 for (;;)
8764 if (op & MOVE_TO_VPOS)
8766 /* If no TO_CHARPOS and no TO_X specified, stop at the
8767 start of the line TO_VPOS. */
8768 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8770 if (it->vpos == to_vpos)
8772 reached = 1;
8773 break;
8775 else
8776 skip = move_it_in_display_line_to (it, -1, -1, 0);
8778 else
8780 /* TO_VPOS >= 0 means stop at TO_X in the line at
8781 TO_VPOS, or at TO_POS, whichever comes first. */
8782 if (it->vpos == to_vpos)
8784 reached = 2;
8785 break;
8788 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8790 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8792 reached = 3;
8793 break;
8795 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8797 /* We have reached TO_X but not in the line we want. */
8798 skip = move_it_in_display_line_to (it, to_charpos,
8799 -1, MOVE_TO_POS);
8800 if (skip == MOVE_POS_MATCH_OR_ZV)
8802 reached = 4;
8803 break;
8808 else if (op & MOVE_TO_Y)
8810 struct it it_backup;
8812 if (it->line_wrap == WORD_WRAP)
8813 SAVE_IT (it_backup, *it, backup_data);
8815 /* TO_Y specified means stop at TO_X in the line containing
8816 TO_Y---or at TO_CHARPOS if this is reached first. The
8817 problem is that we can't really tell whether the line
8818 contains TO_Y before we have completely scanned it, and
8819 this may skip past TO_X. What we do is to first scan to
8820 TO_X.
8822 If TO_X is not specified, use a TO_X of zero. The reason
8823 is to make the outcome of this function more predictable.
8824 If we didn't use TO_X == 0, we would stop at the end of
8825 the line which is probably not what a caller would expect
8826 to happen. */
8827 skip = move_it_in_display_line_to
8828 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8829 (MOVE_TO_X | (op & MOVE_TO_POS)));
8831 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8832 if (skip == MOVE_POS_MATCH_OR_ZV)
8833 reached = 5;
8834 else if (skip == MOVE_X_REACHED)
8836 /* If TO_X was reached, we want to know whether TO_Y is
8837 in the line. We know this is the case if the already
8838 scanned glyphs make the line tall enough. Otherwise,
8839 we must check by scanning the rest of the line. */
8840 line_height = it->max_ascent + it->max_descent;
8841 if (to_y >= it->current_y
8842 && to_y < it->current_y + line_height)
8844 reached = 6;
8845 break;
8847 SAVE_IT (it_backup, *it, backup_data);
8848 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8849 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8850 op & MOVE_TO_POS);
8851 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8852 line_height = it->max_ascent + it->max_descent;
8853 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8855 if (to_y >= it->current_y
8856 && to_y < it->current_y + line_height)
8858 /* If TO_Y is in this line and TO_X was reached
8859 above, we scanned too far. We have to restore
8860 IT's settings to the ones before skipping. But
8861 keep the more accurate values of max_ascent and
8862 max_descent we've found while skipping the rest
8863 of the line, for the sake of callers, such as
8864 pos_visible_p, that need to know the line
8865 height. */
8866 int max_ascent = it->max_ascent;
8867 int max_descent = it->max_descent;
8869 RESTORE_IT (it, &it_backup, backup_data);
8870 it->max_ascent = max_ascent;
8871 it->max_descent = max_descent;
8872 reached = 6;
8874 else
8876 skip = skip2;
8877 if (skip == MOVE_POS_MATCH_OR_ZV)
8878 reached = 7;
8881 else
8883 /* Check whether TO_Y is in this line. */
8884 line_height = it->max_ascent + it->max_descent;
8885 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8887 if (to_y >= it->current_y
8888 && to_y < it->current_y + line_height)
8890 /* When word-wrap is on, TO_X may lie past the end
8891 of a wrapped line. Then it->current is the
8892 character on the next line, so backtrack to the
8893 space before the wrap point. */
8894 if (skip == MOVE_LINE_CONTINUED
8895 && it->line_wrap == WORD_WRAP)
8897 int prev_x = max (it->current_x - 1, 0);
8898 RESTORE_IT (it, &it_backup, backup_data);
8899 skip = move_it_in_display_line_to
8900 (it, -1, prev_x, MOVE_TO_X);
8902 reached = 6;
8906 if (reached)
8907 break;
8909 else if (BUFFERP (it->object)
8910 && (it->method == GET_FROM_BUFFER
8911 || it->method == GET_FROM_STRETCH)
8912 && IT_CHARPOS (*it) >= to_charpos
8913 /* Under bidi iteration, a call to set_iterator_to_next
8914 can scan far beyond to_charpos if the initial
8915 portion of the next line needs to be reordered. In
8916 that case, give move_it_in_display_line_to another
8917 chance below. */
8918 && !(it->bidi_p
8919 && it->bidi_it.scan_dir == -1))
8920 skip = MOVE_POS_MATCH_OR_ZV;
8921 else
8922 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8924 switch (skip)
8926 case MOVE_POS_MATCH_OR_ZV:
8927 reached = 8;
8928 goto out;
8930 case MOVE_NEWLINE_OR_CR:
8931 set_iterator_to_next (it, 1);
8932 it->continuation_lines_width = 0;
8933 break;
8935 case MOVE_LINE_TRUNCATED:
8936 it->continuation_lines_width = 0;
8937 reseat_at_next_visible_line_start (it, 0);
8938 if ((op & MOVE_TO_POS) != 0
8939 && IT_CHARPOS (*it) > to_charpos)
8941 reached = 9;
8942 goto out;
8944 break;
8946 case MOVE_LINE_CONTINUED:
8947 /* For continued lines ending in a tab, some of the glyphs
8948 associated with the tab are displayed on the current
8949 line. Since it->current_x does not include these glyphs,
8950 we use it->last_visible_x instead. */
8951 if (it->c == '\t')
8953 it->continuation_lines_width += it->last_visible_x;
8954 /* When moving by vpos, ensure that the iterator really
8955 advances to the next line (bug#847, bug#969). Fixme:
8956 do we need to do this in other circumstances? */
8957 if (it->current_x != it->last_visible_x
8958 && (op & MOVE_TO_VPOS)
8959 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8961 line_start_x = it->current_x + it->pixel_width
8962 - it->last_visible_x;
8963 set_iterator_to_next (it, 0);
8966 else
8967 it->continuation_lines_width += it->current_x;
8968 break;
8970 default:
8971 emacs_abort ();
8974 /* Reset/increment for the next run. */
8975 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8976 it->current_x = line_start_x;
8977 line_start_x = 0;
8978 it->hpos = 0;
8979 it->current_y += it->max_ascent + it->max_descent;
8980 ++it->vpos;
8981 last_height = it->max_ascent + it->max_descent;
8982 it->max_ascent = it->max_descent = 0;
8985 out:
8987 /* On text terminals, we may stop at the end of a line in the middle
8988 of a multi-character glyph. If the glyph itself is continued,
8989 i.e. it is actually displayed on the next line, don't treat this
8990 stopping point as valid; move to the next line instead (unless
8991 that brings us offscreen). */
8992 if (!FRAME_WINDOW_P (it->f)
8993 && op & MOVE_TO_POS
8994 && IT_CHARPOS (*it) == to_charpos
8995 && it->what == IT_CHARACTER
8996 && it->nglyphs > 1
8997 && it->line_wrap == WINDOW_WRAP
8998 && it->current_x == it->last_visible_x - 1
8999 && it->c != '\n'
9000 && it->c != '\t'
9001 && it->vpos < XFASTINT (it->w->window_end_vpos))
9003 it->continuation_lines_width += it->current_x;
9004 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9005 it->current_y += it->max_ascent + it->max_descent;
9006 ++it->vpos;
9007 last_height = it->max_ascent + it->max_descent;
9010 if (backup_data)
9011 bidi_unshelve_cache (backup_data, 1);
9013 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9017 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9019 If DY > 0, move IT backward at least that many pixels. DY = 0
9020 means move IT backward to the preceding line start or BEGV. This
9021 function may move over more than DY pixels if IT->current_y - DY
9022 ends up in the middle of a line; in this case IT->current_y will be
9023 set to the top of the line moved to. */
9025 void
9026 move_it_vertically_backward (struct it *it, int dy)
9028 int nlines, h;
9029 struct it it2, it3;
9030 void *it2data = NULL, *it3data = NULL;
9031 ptrdiff_t start_pos;
9032 int nchars_per_row
9033 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9034 ptrdiff_t pos_limit;
9036 move_further_back:
9037 eassert (dy >= 0);
9039 start_pos = IT_CHARPOS (*it);
9041 /* Estimate how many newlines we must move back. */
9042 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9043 if (it->line_wrap == TRUNCATE)
9044 pos_limit = BEGV;
9045 else
9046 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9048 /* Set the iterator's position that many lines back. But don't go
9049 back more than NLINES full screen lines -- this wins a day with
9050 buffers which have very long lines. */
9051 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9052 back_to_previous_visible_line_start (it);
9054 /* Reseat the iterator here. When moving backward, we don't want
9055 reseat to skip forward over invisible text, set up the iterator
9056 to deliver from overlay strings at the new position etc. So,
9057 use reseat_1 here. */
9058 reseat_1 (it, it->current.pos, 1);
9060 /* We are now surely at a line start. */
9061 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9062 reordering is in effect. */
9063 it->continuation_lines_width = 0;
9065 /* Move forward and see what y-distance we moved. First move to the
9066 start of the next line so that we get its height. We need this
9067 height to be able to tell whether we reached the specified
9068 y-distance. */
9069 SAVE_IT (it2, *it, it2data);
9070 it2.max_ascent = it2.max_descent = 0;
9073 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9074 MOVE_TO_POS | MOVE_TO_VPOS);
9076 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9077 /* If we are in a display string which starts at START_POS,
9078 and that display string includes a newline, and we are
9079 right after that newline (i.e. at the beginning of a
9080 display line), exit the loop, because otherwise we will
9081 infloop, since move_it_to will see that it is already at
9082 START_POS and will not move. */
9083 || (it2.method == GET_FROM_STRING
9084 && IT_CHARPOS (it2) == start_pos
9085 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9086 eassert (IT_CHARPOS (*it) >= BEGV);
9087 SAVE_IT (it3, it2, it3data);
9089 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9090 eassert (IT_CHARPOS (*it) >= BEGV);
9091 /* H is the actual vertical distance from the position in *IT
9092 and the starting position. */
9093 h = it2.current_y - it->current_y;
9094 /* NLINES is the distance in number of lines. */
9095 nlines = it2.vpos - it->vpos;
9097 /* Correct IT's y and vpos position
9098 so that they are relative to the starting point. */
9099 it->vpos -= nlines;
9100 it->current_y -= h;
9102 if (dy == 0)
9104 /* DY == 0 means move to the start of the screen line. The
9105 value of nlines is > 0 if continuation lines were involved,
9106 or if the original IT position was at start of a line. */
9107 RESTORE_IT (it, it, it2data);
9108 if (nlines > 0)
9109 move_it_by_lines (it, nlines);
9110 /* The above code moves us to some position NLINES down,
9111 usually to its first glyph (leftmost in an L2R line), but
9112 that's not necessarily the start of the line, under bidi
9113 reordering. We want to get to the character position
9114 that is immediately after the newline of the previous
9115 line. */
9116 if (it->bidi_p
9117 && !it->continuation_lines_width
9118 && !STRINGP (it->string)
9119 && IT_CHARPOS (*it) > BEGV
9120 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9122 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9124 DEC_BOTH (cp, bp);
9125 cp = find_newline_no_quit (cp, bp, -1, NULL);
9126 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9128 bidi_unshelve_cache (it3data, 1);
9130 else
9132 /* The y-position we try to reach, relative to *IT.
9133 Note that H has been subtracted in front of the if-statement. */
9134 int target_y = it->current_y + h - dy;
9135 int y0 = it3.current_y;
9136 int y1;
9137 int line_height;
9139 RESTORE_IT (&it3, &it3, it3data);
9140 y1 = line_bottom_y (&it3);
9141 line_height = y1 - y0;
9142 RESTORE_IT (it, it, it2data);
9143 /* If we did not reach target_y, try to move further backward if
9144 we can. If we moved too far backward, try to move forward. */
9145 if (target_y < it->current_y
9146 /* This is heuristic. In a window that's 3 lines high, with
9147 a line height of 13 pixels each, recentering with point
9148 on the bottom line will try to move -39/2 = 19 pixels
9149 backward. Try to avoid moving into the first line. */
9150 && (it->current_y - target_y
9151 > min (window_box_height (it->w), line_height * 2 / 3))
9152 && IT_CHARPOS (*it) > BEGV)
9154 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9155 target_y - it->current_y));
9156 dy = it->current_y - target_y;
9157 goto move_further_back;
9159 else if (target_y >= it->current_y + line_height
9160 && IT_CHARPOS (*it) < ZV)
9162 /* Should move forward by at least one line, maybe more.
9164 Note: Calling move_it_by_lines can be expensive on
9165 terminal frames, where compute_motion is used (via
9166 vmotion) to do the job, when there are very long lines
9167 and truncate-lines is nil. That's the reason for
9168 treating terminal frames specially here. */
9170 if (!FRAME_WINDOW_P (it->f))
9171 move_it_vertically (it, target_y - (it->current_y + line_height));
9172 else
9176 move_it_by_lines (it, 1);
9178 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9185 /* Move IT by a specified amount of pixel lines DY. DY negative means
9186 move backwards. DY = 0 means move to start of screen line. At the
9187 end, IT will be on the start of a screen line. */
9189 void
9190 move_it_vertically (struct it *it, int dy)
9192 if (dy <= 0)
9193 move_it_vertically_backward (it, -dy);
9194 else
9196 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9197 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9198 MOVE_TO_POS | MOVE_TO_Y);
9199 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9201 /* If buffer ends in ZV without a newline, move to the start of
9202 the line to satisfy the post-condition. */
9203 if (IT_CHARPOS (*it) == ZV
9204 && ZV > BEGV
9205 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9206 move_it_by_lines (it, 0);
9211 /* Move iterator IT past the end of the text line it is in. */
9213 void
9214 move_it_past_eol (struct it *it)
9216 enum move_it_result rc;
9218 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9219 if (rc == MOVE_NEWLINE_OR_CR)
9220 set_iterator_to_next (it, 0);
9224 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9225 negative means move up. DVPOS == 0 means move to the start of the
9226 screen line.
9228 Optimization idea: If we would know that IT->f doesn't use
9229 a face with proportional font, we could be faster for
9230 truncate-lines nil. */
9232 void
9233 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9236 /* The commented-out optimization uses vmotion on terminals. This
9237 gives bad results, because elements like it->what, on which
9238 callers such as pos_visible_p rely, aren't updated. */
9239 /* struct position pos;
9240 if (!FRAME_WINDOW_P (it->f))
9242 struct text_pos textpos;
9244 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9245 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9246 reseat (it, textpos, 1);
9247 it->vpos += pos.vpos;
9248 it->current_y += pos.vpos;
9250 else */
9252 if (dvpos == 0)
9254 /* DVPOS == 0 means move to the start of the screen line. */
9255 move_it_vertically_backward (it, 0);
9256 /* Let next call to line_bottom_y calculate real line height */
9257 last_height = 0;
9259 else if (dvpos > 0)
9261 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9262 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9264 /* Only move to the next buffer position if we ended up in a
9265 string from display property, not in an overlay string
9266 (before-string or after-string). That is because the
9267 latter don't conceal the underlying buffer position, so
9268 we can ask to move the iterator to the exact position we
9269 are interested in. Note that, even if we are already at
9270 IT_CHARPOS (*it), the call below is not a no-op, as it
9271 will detect that we are at the end of the string, pop the
9272 iterator, and compute it->current_x and it->hpos
9273 correctly. */
9274 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9275 -1, -1, -1, MOVE_TO_POS);
9278 else
9280 struct it it2;
9281 void *it2data = NULL;
9282 ptrdiff_t start_charpos, i;
9283 int nchars_per_row
9284 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9285 ptrdiff_t pos_limit;
9287 /* Start at the beginning of the screen line containing IT's
9288 position. This may actually move vertically backwards,
9289 in case of overlays, so adjust dvpos accordingly. */
9290 dvpos += it->vpos;
9291 move_it_vertically_backward (it, 0);
9292 dvpos -= it->vpos;
9294 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9295 screen lines, and reseat the iterator there. */
9296 start_charpos = IT_CHARPOS (*it);
9297 if (it->line_wrap == TRUNCATE)
9298 pos_limit = BEGV;
9299 else
9300 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9301 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9302 back_to_previous_visible_line_start (it);
9303 reseat (it, it->current.pos, 1);
9305 /* Move further back if we end up in a string or an image. */
9306 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9308 /* First try to move to start of display line. */
9309 dvpos += it->vpos;
9310 move_it_vertically_backward (it, 0);
9311 dvpos -= it->vpos;
9312 if (IT_POS_VALID_AFTER_MOVE_P (it))
9313 break;
9314 /* If start of line is still in string or image,
9315 move further back. */
9316 back_to_previous_visible_line_start (it);
9317 reseat (it, it->current.pos, 1);
9318 dvpos--;
9321 it->current_x = it->hpos = 0;
9323 /* Above call may have moved too far if continuation lines
9324 are involved. Scan forward and see if it did. */
9325 SAVE_IT (it2, *it, it2data);
9326 it2.vpos = it2.current_y = 0;
9327 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9328 it->vpos -= it2.vpos;
9329 it->current_y -= it2.current_y;
9330 it->current_x = it->hpos = 0;
9332 /* If we moved too far back, move IT some lines forward. */
9333 if (it2.vpos > -dvpos)
9335 int delta = it2.vpos + dvpos;
9337 RESTORE_IT (&it2, &it2, it2data);
9338 SAVE_IT (it2, *it, it2data);
9339 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9340 /* Move back again if we got too far ahead. */
9341 if (IT_CHARPOS (*it) >= start_charpos)
9342 RESTORE_IT (it, &it2, it2data);
9343 else
9344 bidi_unshelve_cache (it2data, 1);
9346 else
9347 RESTORE_IT (it, it, it2data);
9351 /* Return 1 if IT points into the middle of a display vector. */
9354 in_display_vector_p (struct it *it)
9356 return (it->method == GET_FROM_DISPLAY_VECTOR
9357 && it->current.dpvec_index > 0
9358 && it->dpvec + it->current.dpvec_index != it->dpend);
9362 /***********************************************************************
9363 Messages
9364 ***********************************************************************/
9367 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9368 to *Messages*. */
9370 void
9371 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9373 Lisp_Object args[3];
9374 Lisp_Object msg, fmt;
9375 char *buffer;
9376 ptrdiff_t len;
9377 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9378 USE_SAFE_ALLOCA;
9380 fmt = msg = Qnil;
9381 GCPRO4 (fmt, msg, arg1, arg2);
9383 args[0] = fmt = build_string (format);
9384 args[1] = arg1;
9385 args[2] = arg2;
9386 msg = Fformat (3, args);
9388 len = SBYTES (msg) + 1;
9389 buffer = SAFE_ALLOCA (len);
9390 memcpy (buffer, SDATA (msg), len);
9392 message_dolog (buffer, len - 1, 1, 0);
9393 SAFE_FREE ();
9395 UNGCPRO;
9399 /* Output a newline in the *Messages* buffer if "needs" one. */
9401 void
9402 message_log_maybe_newline (void)
9404 if (message_log_need_newline)
9405 message_dolog ("", 0, 1, 0);
9409 /* Add a string M of length NBYTES to the message log, optionally
9410 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9411 true, means interpret the contents of M as multibyte. This
9412 function calls low-level routines in order to bypass text property
9413 hooks, etc. which might not be safe to run.
9415 This may GC (insert may run before/after change hooks),
9416 so the buffer M must NOT point to a Lisp string. */
9418 void
9419 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9421 const unsigned char *msg = (const unsigned char *) m;
9423 if (!NILP (Vmemory_full))
9424 return;
9426 if (!NILP (Vmessage_log_max))
9428 struct buffer *oldbuf;
9429 Lisp_Object oldpoint, oldbegv, oldzv;
9430 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9431 ptrdiff_t point_at_end = 0;
9432 ptrdiff_t zv_at_end = 0;
9433 Lisp_Object old_deactivate_mark;
9434 bool shown;
9435 struct gcpro gcpro1;
9437 old_deactivate_mark = Vdeactivate_mark;
9438 oldbuf = current_buffer;
9439 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9440 bset_undo_list (current_buffer, Qt);
9442 oldpoint = message_dolog_marker1;
9443 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9444 oldbegv = message_dolog_marker2;
9445 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9446 oldzv = message_dolog_marker3;
9447 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9448 GCPRO1 (old_deactivate_mark);
9450 if (PT == Z)
9451 point_at_end = 1;
9452 if (ZV == Z)
9453 zv_at_end = 1;
9455 BEGV = BEG;
9456 BEGV_BYTE = BEG_BYTE;
9457 ZV = Z;
9458 ZV_BYTE = Z_BYTE;
9459 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9461 /* Insert the string--maybe converting multibyte to single byte
9462 or vice versa, so that all the text fits the buffer. */
9463 if (multibyte
9464 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9466 ptrdiff_t i;
9467 int c, char_bytes;
9468 char work[1];
9470 /* Convert a multibyte string to single-byte
9471 for the *Message* buffer. */
9472 for (i = 0; i < nbytes; i += char_bytes)
9474 c = string_char_and_length (msg + i, &char_bytes);
9475 work[0] = (ASCII_CHAR_P (c)
9477 : multibyte_char_to_unibyte (c));
9478 insert_1_both (work, 1, 1, 1, 0, 0);
9481 else if (! multibyte
9482 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9484 ptrdiff_t i;
9485 int c, char_bytes;
9486 unsigned char str[MAX_MULTIBYTE_LENGTH];
9487 /* Convert a single-byte string to multibyte
9488 for the *Message* buffer. */
9489 for (i = 0; i < nbytes; i++)
9491 c = msg[i];
9492 MAKE_CHAR_MULTIBYTE (c);
9493 char_bytes = CHAR_STRING (c, str);
9494 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9497 else if (nbytes)
9498 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9500 if (nlflag)
9502 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9503 printmax_t dups;
9505 insert_1_both ("\n", 1, 1, 1, 0, 0);
9507 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9508 this_bol = PT;
9509 this_bol_byte = PT_BYTE;
9511 /* See if this line duplicates the previous one.
9512 If so, combine duplicates. */
9513 if (this_bol > BEG)
9515 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9516 prev_bol = PT;
9517 prev_bol_byte = PT_BYTE;
9519 dups = message_log_check_duplicate (prev_bol_byte,
9520 this_bol_byte);
9521 if (dups)
9523 del_range_both (prev_bol, prev_bol_byte,
9524 this_bol, this_bol_byte, 0);
9525 if (dups > 1)
9527 char dupstr[sizeof " [ times]"
9528 + INT_STRLEN_BOUND (printmax_t)];
9530 /* If you change this format, don't forget to also
9531 change message_log_check_duplicate. */
9532 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9533 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9534 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9539 /* If we have more than the desired maximum number of lines
9540 in the *Messages* buffer now, delete the oldest ones.
9541 This is safe because we don't have undo in this buffer. */
9543 if (NATNUMP (Vmessage_log_max))
9545 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9546 -XFASTINT (Vmessage_log_max) - 1, 0);
9547 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9550 BEGV = marker_position (oldbegv);
9551 BEGV_BYTE = marker_byte_position (oldbegv);
9553 if (zv_at_end)
9555 ZV = Z;
9556 ZV_BYTE = Z_BYTE;
9558 else
9560 ZV = marker_position (oldzv);
9561 ZV_BYTE = marker_byte_position (oldzv);
9564 if (point_at_end)
9565 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9566 else
9567 /* We can't do Fgoto_char (oldpoint) because it will run some
9568 Lisp code. */
9569 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9570 marker_byte_position (oldpoint));
9572 UNGCPRO;
9573 unchain_marker (XMARKER (oldpoint));
9574 unchain_marker (XMARKER (oldbegv));
9575 unchain_marker (XMARKER (oldzv));
9577 shown = buffer_window_count (current_buffer) > 0;
9578 set_buffer_internal (oldbuf);
9579 /* We called insert_1_both above with its 5th argument (PREPARE)
9580 zero, which prevents insert_1_both from calling
9581 prepare_to_modify_buffer, which in turns prevents us from
9582 incrementing windows_or_buffers_changed even if *Messages* is
9583 shown in some window. So we must manually incrementing
9584 windows_or_buffers_changed here to make up for that. */
9585 if (shown)
9586 windows_or_buffers_changed++;
9587 else
9588 windows_or_buffers_changed = old_windows_or_buffers_changed;
9589 message_log_need_newline = !nlflag;
9590 Vdeactivate_mark = old_deactivate_mark;
9595 /* We are at the end of the buffer after just having inserted a newline.
9596 (Note: We depend on the fact we won't be crossing the gap.)
9597 Check to see if the most recent message looks a lot like the previous one.
9598 Return 0 if different, 1 if the new one should just replace it, or a
9599 value N > 1 if we should also append " [N times]". */
9601 static intmax_t
9602 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9604 ptrdiff_t i;
9605 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9606 int seen_dots = 0;
9607 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9608 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9610 for (i = 0; i < len; i++)
9612 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9613 seen_dots = 1;
9614 if (p1[i] != p2[i])
9615 return seen_dots;
9617 p1 += len;
9618 if (*p1 == '\n')
9619 return 2;
9620 if (*p1++ == ' ' && *p1++ == '[')
9622 char *pend;
9623 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9624 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9625 return n + 1;
9627 return 0;
9631 /* Display an echo area message M with a specified length of NBYTES
9632 bytes. The string may include null characters. If M is not a
9633 string, clear out any existing message, and let the mini-buffer
9634 text show through.
9636 This function cancels echoing. */
9638 void
9639 message3 (Lisp_Object m)
9641 struct gcpro gcpro1;
9643 GCPRO1 (m);
9644 clear_message (1,1);
9645 cancel_echoing ();
9647 /* First flush out any partial line written with print. */
9648 message_log_maybe_newline ();
9649 if (STRINGP (m))
9651 ptrdiff_t nbytes = SBYTES (m);
9652 bool multibyte = STRING_MULTIBYTE (m);
9653 USE_SAFE_ALLOCA;
9654 char *buffer = SAFE_ALLOCA (nbytes);
9655 memcpy (buffer, SDATA (m), nbytes);
9656 message_dolog (buffer, nbytes, 1, multibyte);
9657 SAFE_FREE ();
9659 message3_nolog (m);
9661 UNGCPRO;
9665 /* The non-logging version of message3.
9666 This does not cancel echoing, because it is used for echoing.
9667 Perhaps we need to make a separate function for echoing
9668 and make this cancel echoing. */
9670 void
9671 message3_nolog (Lisp_Object m)
9673 struct frame *sf = SELECTED_FRAME ();
9675 if (FRAME_INITIAL_P (sf))
9677 if (noninteractive_need_newline)
9678 putc ('\n', stderr);
9679 noninteractive_need_newline = 0;
9680 if (STRINGP (m))
9681 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9682 if (cursor_in_echo_area == 0)
9683 fprintf (stderr, "\n");
9684 fflush (stderr);
9686 /* Error messages get reported properly by cmd_error, so this must be just an
9687 informative message; if the frame hasn't really been initialized yet, just
9688 toss it. */
9689 else if (INTERACTIVE && sf->glyphs_initialized_p)
9691 /* Get the frame containing the mini-buffer
9692 that the selected frame is using. */
9693 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9694 Lisp_Object frame = XWINDOW (mini_window)->frame;
9695 struct frame *f = XFRAME (frame);
9697 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9698 Fmake_frame_visible (frame);
9700 if (STRINGP (m) && SCHARS (m) > 0)
9702 set_message (m);
9703 if (minibuffer_auto_raise)
9704 Fraise_frame (frame);
9705 /* Assume we are not echoing.
9706 (If we are, echo_now will override this.) */
9707 echo_message_buffer = Qnil;
9709 else
9710 clear_message (1, 1);
9712 do_pending_window_change (0);
9713 echo_area_display (1);
9714 do_pending_window_change (0);
9715 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9716 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9721 /* Display a null-terminated echo area message M. If M is 0, clear
9722 out any existing message, and let the mini-buffer text show through.
9724 The buffer M must continue to exist until after the echo area gets
9725 cleared or some other message gets displayed there. Do not pass
9726 text that is stored in a Lisp string. Do not pass text in a buffer
9727 that was alloca'd. */
9729 void
9730 message1 (const char *m)
9732 message3 (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9736 /* The non-logging counterpart of message1. */
9738 void
9739 message1_nolog (const char *m)
9741 message3_nolog (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9744 /* Display a message M which contains a single %s
9745 which gets replaced with STRING. */
9747 void
9748 message_with_string (const char *m, Lisp_Object string, int log)
9750 CHECK_STRING (string);
9752 if (noninteractive)
9754 if (m)
9756 if (noninteractive_need_newline)
9757 putc ('\n', stderr);
9758 noninteractive_need_newline = 0;
9759 fprintf (stderr, m, SDATA (string));
9760 if (!cursor_in_echo_area)
9761 fprintf (stderr, "\n");
9762 fflush (stderr);
9765 else if (INTERACTIVE)
9767 /* The frame whose minibuffer we're going to display the message on.
9768 It may be larger than the selected frame, so we need
9769 to use its buffer, not the selected frame's buffer. */
9770 Lisp_Object mini_window;
9771 struct frame *f, *sf = SELECTED_FRAME ();
9773 /* Get the frame containing the minibuffer
9774 that the selected frame is using. */
9775 mini_window = FRAME_MINIBUF_WINDOW (sf);
9776 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9778 /* Error messages get reported properly by cmd_error, so this must be
9779 just an informative message; if the frame hasn't really been
9780 initialized yet, just toss it. */
9781 if (f->glyphs_initialized_p)
9783 Lisp_Object args[2], msg;
9784 struct gcpro gcpro1, gcpro2;
9786 args[0] = build_string (m);
9787 args[1] = msg = string;
9788 GCPRO2 (args[0], msg);
9789 gcpro1.nvars = 2;
9791 msg = Fformat (2, args);
9793 if (log)
9794 message3 (msg);
9795 else
9796 message3_nolog (msg);
9798 UNGCPRO;
9800 /* Print should start at the beginning of the message
9801 buffer next time. */
9802 message_buf_print = 0;
9808 /* Dump an informative message to the minibuf. If M is 0, clear out
9809 any existing message, and let the mini-buffer text show through. */
9811 static void
9812 vmessage (const char *m, va_list ap)
9814 if (noninteractive)
9816 if (m)
9818 if (noninteractive_need_newline)
9819 putc ('\n', stderr);
9820 noninteractive_need_newline = 0;
9821 vfprintf (stderr, m, ap);
9822 if (cursor_in_echo_area == 0)
9823 fprintf (stderr, "\n");
9824 fflush (stderr);
9827 else if (INTERACTIVE)
9829 /* The frame whose mini-buffer we're going to display the message
9830 on. It may be larger than the selected frame, so we need to
9831 use its buffer, not the selected frame's buffer. */
9832 Lisp_Object mini_window;
9833 struct frame *f, *sf = SELECTED_FRAME ();
9835 /* Get the frame containing the mini-buffer
9836 that the selected frame is using. */
9837 mini_window = FRAME_MINIBUF_WINDOW (sf);
9838 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9840 /* Error messages get reported properly by cmd_error, so this must be
9841 just an informative message; if the frame hasn't really been
9842 initialized yet, just toss it. */
9843 if (f->glyphs_initialized_p)
9845 if (m)
9847 ptrdiff_t len;
9848 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9849 char *message_buf = alloca (maxsize + 1);
9851 len = doprnt (message_buf, maxsize, m, (char *)0, ap);
9853 message3 (make_string (message_buf, len));
9855 else
9856 message1 (0);
9858 /* Print should start at the beginning of the message
9859 buffer next time. */
9860 message_buf_print = 0;
9865 void
9866 message (const char *m, ...)
9868 va_list ap;
9869 va_start (ap, m);
9870 vmessage (m, ap);
9871 va_end (ap);
9875 #if 0
9876 /* The non-logging version of message. */
9878 void
9879 message_nolog (const char *m, ...)
9881 Lisp_Object old_log_max;
9882 va_list ap;
9883 va_start (ap, m);
9884 old_log_max = Vmessage_log_max;
9885 Vmessage_log_max = Qnil;
9886 vmessage (m, ap);
9887 Vmessage_log_max = old_log_max;
9888 va_end (ap);
9890 #endif
9893 /* Display the current message in the current mini-buffer. This is
9894 only called from error handlers in process.c, and is not time
9895 critical. */
9897 void
9898 update_echo_area (void)
9900 if (!NILP (echo_area_buffer[0]))
9902 Lisp_Object string;
9903 string = Fcurrent_message ();
9904 message3 (string);
9909 /* Make sure echo area buffers in `echo_buffers' are live.
9910 If they aren't, make new ones. */
9912 static void
9913 ensure_echo_area_buffers (void)
9915 int i;
9917 for (i = 0; i < 2; ++i)
9918 if (!BUFFERP (echo_buffer[i])
9919 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9921 char name[30];
9922 Lisp_Object old_buffer;
9923 int j;
9925 old_buffer = echo_buffer[i];
9926 echo_buffer[i] = Fget_buffer_create
9927 (make_formatted_string (name, " *Echo Area %d*", i));
9928 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9929 /* to force word wrap in echo area -
9930 it was decided to postpone this*/
9931 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9933 for (j = 0; j < 2; ++j)
9934 if (EQ (old_buffer, echo_area_buffer[j]))
9935 echo_area_buffer[j] = echo_buffer[i];
9940 /* Call FN with args A1..A2 with either the current or last displayed
9941 echo_area_buffer as current buffer.
9943 WHICH zero means use the current message buffer
9944 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9945 from echo_buffer[] and clear it.
9947 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9948 suitable buffer from echo_buffer[] and clear it.
9950 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9951 that the current message becomes the last displayed one, make
9952 choose a suitable buffer for echo_area_buffer[0], and clear it.
9954 Value is what FN returns. */
9956 static int
9957 with_echo_area_buffer (struct window *w, int which,
9958 int (*fn) (ptrdiff_t, Lisp_Object),
9959 ptrdiff_t a1, Lisp_Object a2)
9961 Lisp_Object buffer;
9962 int this_one, the_other, clear_buffer_p, rc;
9963 ptrdiff_t count = SPECPDL_INDEX ();
9965 /* If buffers aren't live, make new ones. */
9966 ensure_echo_area_buffers ();
9968 clear_buffer_p = 0;
9970 if (which == 0)
9971 this_one = 0, the_other = 1;
9972 else if (which > 0)
9973 this_one = 1, the_other = 0;
9974 else
9976 this_one = 0, the_other = 1;
9977 clear_buffer_p = 1;
9979 /* We need a fresh one in case the current echo buffer equals
9980 the one containing the last displayed echo area message. */
9981 if (!NILP (echo_area_buffer[this_one])
9982 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9983 echo_area_buffer[this_one] = Qnil;
9986 /* Choose a suitable buffer from echo_buffer[] is we don't
9987 have one. */
9988 if (NILP (echo_area_buffer[this_one]))
9990 echo_area_buffer[this_one]
9991 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9992 ? echo_buffer[the_other]
9993 : echo_buffer[this_one]);
9994 clear_buffer_p = 1;
9997 buffer = echo_area_buffer[this_one];
9999 /* Don't get confused by reusing the buffer used for echoing
10000 for a different purpose. */
10001 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10002 cancel_echoing ();
10004 record_unwind_protect (unwind_with_echo_area_buffer,
10005 with_echo_area_buffer_unwind_data (w));
10007 /* Make the echo area buffer current. Note that for display
10008 purposes, it is not necessary that the displayed window's buffer
10009 == current_buffer, except for text property lookup. So, let's
10010 only set that buffer temporarily here without doing a full
10011 Fset_window_buffer. We must also change w->pointm, though,
10012 because otherwise an assertions in unshow_buffer fails, and Emacs
10013 aborts. */
10014 set_buffer_internal_1 (XBUFFER (buffer));
10015 if (w)
10017 wset_buffer (w, buffer);
10018 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10021 bset_undo_list (current_buffer, Qt);
10022 bset_read_only (current_buffer, Qnil);
10023 specbind (Qinhibit_read_only, Qt);
10024 specbind (Qinhibit_modification_hooks, Qt);
10026 if (clear_buffer_p && Z > BEG)
10027 del_range (BEG, Z);
10029 eassert (BEGV >= BEG);
10030 eassert (ZV <= Z && ZV >= BEGV);
10032 rc = fn (a1, a2);
10034 eassert (BEGV >= BEG);
10035 eassert (ZV <= Z && ZV >= BEGV);
10037 unbind_to (count, Qnil);
10038 return rc;
10042 /* Save state that should be preserved around the call to the function
10043 FN called in with_echo_area_buffer. */
10045 static Lisp_Object
10046 with_echo_area_buffer_unwind_data (struct window *w)
10048 int i = 0;
10049 Lisp_Object vector, tmp;
10051 /* Reduce consing by keeping one vector in
10052 Vwith_echo_area_save_vector. */
10053 vector = Vwith_echo_area_save_vector;
10054 Vwith_echo_area_save_vector = Qnil;
10056 if (NILP (vector))
10057 vector = Fmake_vector (make_number (9), Qnil);
10059 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10060 ASET (vector, i, Vdeactivate_mark); ++i;
10061 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10063 if (w)
10065 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10066 ASET (vector, i, w->contents); ++i;
10067 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10068 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10069 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10070 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10072 else
10074 int end = i + 6;
10075 for (; i < end; ++i)
10076 ASET (vector, i, Qnil);
10079 eassert (i == ASIZE (vector));
10080 return vector;
10084 /* Restore global state from VECTOR which was created by
10085 with_echo_area_buffer_unwind_data. */
10087 static Lisp_Object
10088 unwind_with_echo_area_buffer (Lisp_Object vector)
10090 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10091 Vdeactivate_mark = AREF (vector, 1);
10092 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10094 if (WINDOWP (AREF (vector, 3)))
10096 struct window *w;
10097 Lisp_Object buffer;
10099 w = XWINDOW (AREF (vector, 3));
10100 buffer = AREF (vector, 4);
10102 wset_buffer (w, buffer);
10103 set_marker_both (w->pointm, buffer,
10104 XFASTINT (AREF (vector, 5)),
10105 XFASTINT (AREF (vector, 6)));
10106 set_marker_both (w->start, buffer,
10107 XFASTINT (AREF (vector, 7)),
10108 XFASTINT (AREF (vector, 8)));
10111 Vwith_echo_area_save_vector = vector;
10112 return Qnil;
10116 /* Set up the echo area for use by print functions. MULTIBYTE_P
10117 non-zero means we will print multibyte. */
10119 void
10120 setup_echo_area_for_printing (int multibyte_p)
10122 /* If we can't find an echo area any more, exit. */
10123 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10124 Fkill_emacs (Qnil);
10126 ensure_echo_area_buffers ();
10128 if (!message_buf_print)
10130 /* A message has been output since the last time we printed.
10131 Choose a fresh echo area buffer. */
10132 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10133 echo_area_buffer[0] = echo_buffer[1];
10134 else
10135 echo_area_buffer[0] = echo_buffer[0];
10137 /* Switch to that buffer and clear it. */
10138 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10139 bset_truncate_lines (current_buffer, Qnil);
10141 if (Z > BEG)
10143 ptrdiff_t count = SPECPDL_INDEX ();
10144 specbind (Qinhibit_read_only, Qt);
10145 /* Note that undo recording is always disabled. */
10146 del_range (BEG, Z);
10147 unbind_to (count, Qnil);
10149 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10151 /* Set up the buffer for the multibyteness we need. */
10152 if (multibyte_p
10153 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10154 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10156 /* Raise the frame containing the echo area. */
10157 if (minibuffer_auto_raise)
10159 struct frame *sf = SELECTED_FRAME ();
10160 Lisp_Object mini_window;
10161 mini_window = FRAME_MINIBUF_WINDOW (sf);
10162 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10165 message_log_maybe_newline ();
10166 message_buf_print = 1;
10168 else
10170 if (NILP (echo_area_buffer[0]))
10172 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10173 echo_area_buffer[0] = echo_buffer[1];
10174 else
10175 echo_area_buffer[0] = echo_buffer[0];
10178 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10180 /* Someone switched buffers between print requests. */
10181 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10182 bset_truncate_lines (current_buffer, Qnil);
10188 /* Display an echo area message in window W. Value is non-zero if W's
10189 height is changed. If display_last_displayed_message_p is
10190 non-zero, display the message that was last displayed, otherwise
10191 display the current message. */
10193 static int
10194 display_echo_area (struct window *w)
10196 int i, no_message_p, window_height_changed_p;
10198 /* Temporarily disable garbage collections while displaying the echo
10199 area. This is done because a GC can print a message itself.
10200 That message would modify the echo area buffer's contents while a
10201 redisplay of the buffer is going on, and seriously confuse
10202 redisplay. */
10203 ptrdiff_t count = inhibit_garbage_collection ();
10205 /* If there is no message, we must call display_echo_area_1
10206 nevertheless because it resizes the window. But we will have to
10207 reset the echo_area_buffer in question to nil at the end because
10208 with_echo_area_buffer will sets it to an empty buffer. */
10209 i = display_last_displayed_message_p ? 1 : 0;
10210 no_message_p = NILP (echo_area_buffer[i]);
10212 window_height_changed_p
10213 = with_echo_area_buffer (w, display_last_displayed_message_p,
10214 display_echo_area_1,
10215 (intptr_t) w, Qnil);
10217 if (no_message_p)
10218 echo_area_buffer[i] = Qnil;
10220 unbind_to (count, Qnil);
10221 return window_height_changed_p;
10225 /* Helper for display_echo_area. Display the current buffer which
10226 contains the current echo area message in window W, a mini-window,
10227 a pointer to which is passed in A1. A2..A4 are currently not used.
10228 Change the height of W so that all of the message is displayed.
10229 Value is non-zero if height of W was changed. */
10231 static int
10232 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10234 intptr_t i1 = a1;
10235 struct window *w = (struct window *) i1;
10236 Lisp_Object window;
10237 struct text_pos start;
10238 int window_height_changed_p = 0;
10240 /* Do this before displaying, so that we have a large enough glyph
10241 matrix for the display. If we can't get enough space for the
10242 whole text, display the last N lines. That works by setting w->start. */
10243 window_height_changed_p = resize_mini_window (w, 0);
10245 /* Use the starting position chosen by resize_mini_window. */
10246 SET_TEXT_POS_FROM_MARKER (start, w->start);
10248 /* Display. */
10249 clear_glyph_matrix (w->desired_matrix);
10250 XSETWINDOW (window, w);
10251 try_window (window, start, 0);
10253 return window_height_changed_p;
10257 /* Resize the echo area window to exactly the size needed for the
10258 currently displayed message, if there is one. If a mini-buffer
10259 is active, don't shrink it. */
10261 void
10262 resize_echo_area_exactly (void)
10264 if (BUFFERP (echo_area_buffer[0])
10265 && WINDOWP (echo_area_window))
10267 struct window *w = XWINDOW (echo_area_window);
10268 int resized_p;
10269 Lisp_Object resize_exactly;
10271 if (minibuf_level == 0)
10272 resize_exactly = Qt;
10273 else
10274 resize_exactly = Qnil;
10276 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10277 (intptr_t) w, resize_exactly);
10278 if (resized_p)
10280 ++windows_or_buffers_changed;
10281 ++update_mode_lines;
10282 redisplay_internal ();
10288 /* Callback function for with_echo_area_buffer, when used from
10289 resize_echo_area_exactly. A1 contains a pointer to the window to
10290 resize, EXACTLY non-nil means resize the mini-window exactly to the
10291 size of the text displayed. A3 and A4 are not used. Value is what
10292 resize_mini_window returns. */
10294 static int
10295 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10297 intptr_t i1 = a1;
10298 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10302 /* Resize mini-window W to fit the size of its contents. EXACT_P
10303 means size the window exactly to the size needed. Otherwise, it's
10304 only enlarged until W's buffer is empty.
10306 Set W->start to the right place to begin display. If the whole
10307 contents fit, start at the beginning. Otherwise, start so as
10308 to make the end of the contents appear. This is particularly
10309 important for y-or-n-p, but seems desirable generally.
10311 Value is non-zero if the window height has been changed. */
10314 resize_mini_window (struct window *w, int exact_p)
10316 struct frame *f = XFRAME (w->frame);
10317 int window_height_changed_p = 0;
10319 eassert (MINI_WINDOW_P (w));
10321 /* By default, start display at the beginning. */
10322 set_marker_both (w->start, w->contents,
10323 BUF_BEGV (XBUFFER (w->contents)),
10324 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10326 /* Don't resize windows while redisplaying a window; it would
10327 confuse redisplay functions when the size of the window they are
10328 displaying changes from under them. Such a resizing can happen,
10329 for instance, when which-func prints a long message while
10330 we are running fontification-functions. We're running these
10331 functions with safe_call which binds inhibit-redisplay to t. */
10332 if (!NILP (Vinhibit_redisplay))
10333 return 0;
10335 /* Nil means don't try to resize. */
10336 if (NILP (Vresize_mini_windows)
10337 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10338 return 0;
10340 if (!FRAME_MINIBUF_ONLY_P (f))
10342 struct it it;
10343 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10344 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10345 int height;
10346 EMACS_INT max_height;
10347 int unit = FRAME_LINE_HEIGHT (f);
10348 struct text_pos start;
10349 struct buffer *old_current_buffer = NULL;
10351 if (current_buffer != XBUFFER (w->contents))
10353 old_current_buffer = current_buffer;
10354 set_buffer_internal (XBUFFER (w->contents));
10357 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10359 /* Compute the max. number of lines specified by the user. */
10360 if (FLOATP (Vmax_mini_window_height))
10361 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10362 else if (INTEGERP (Vmax_mini_window_height))
10363 max_height = XINT (Vmax_mini_window_height);
10364 else
10365 max_height = total_height / 4;
10367 /* Correct that max. height if it's bogus. */
10368 max_height = clip_to_bounds (1, max_height, total_height);
10370 /* Find out the height of the text in the window. */
10371 if (it.line_wrap == TRUNCATE)
10372 height = 1;
10373 else
10375 last_height = 0;
10376 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10377 if (it.max_ascent == 0 && it.max_descent == 0)
10378 height = it.current_y + last_height;
10379 else
10380 height = it.current_y + it.max_ascent + it.max_descent;
10381 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10382 height = (height + unit - 1) / unit;
10385 /* Compute a suitable window start. */
10386 if (height > max_height)
10388 height = max_height;
10389 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10390 move_it_vertically_backward (&it, (height - 1) * unit);
10391 start = it.current.pos;
10393 else
10394 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10395 SET_MARKER_FROM_TEXT_POS (w->start, start);
10397 if (EQ (Vresize_mini_windows, Qgrow_only))
10399 /* Let it grow only, until we display an empty message, in which
10400 case the window shrinks again. */
10401 if (height > WINDOW_TOTAL_LINES (w))
10403 int old_height = WINDOW_TOTAL_LINES (w);
10404 freeze_window_starts (f, 1);
10405 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10406 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10408 else if (height < WINDOW_TOTAL_LINES (w)
10409 && (exact_p || BEGV == ZV))
10411 int old_height = WINDOW_TOTAL_LINES (w);
10412 freeze_window_starts (f, 0);
10413 shrink_mini_window (w);
10414 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10417 else
10419 /* Always resize to exact size needed. */
10420 if (height > WINDOW_TOTAL_LINES (w))
10422 int old_height = WINDOW_TOTAL_LINES (w);
10423 freeze_window_starts (f, 1);
10424 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10425 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10427 else if (height < WINDOW_TOTAL_LINES (w))
10429 int old_height = WINDOW_TOTAL_LINES (w);
10430 freeze_window_starts (f, 0);
10431 shrink_mini_window (w);
10433 if (height)
10435 freeze_window_starts (f, 1);
10436 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10439 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10443 if (old_current_buffer)
10444 set_buffer_internal (old_current_buffer);
10447 return window_height_changed_p;
10451 /* Value is the current message, a string, or nil if there is no
10452 current message. */
10454 Lisp_Object
10455 current_message (void)
10457 Lisp_Object msg;
10459 if (!BUFFERP (echo_area_buffer[0]))
10460 msg = Qnil;
10461 else
10463 with_echo_area_buffer (0, 0, current_message_1,
10464 (intptr_t) &msg, Qnil);
10465 if (NILP (msg))
10466 echo_area_buffer[0] = Qnil;
10469 return msg;
10473 static int
10474 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10476 intptr_t i1 = a1;
10477 Lisp_Object *msg = (Lisp_Object *) i1;
10479 if (Z > BEG)
10480 *msg = make_buffer_string (BEG, Z, 1);
10481 else
10482 *msg = Qnil;
10483 return 0;
10487 /* Push the current message on Vmessage_stack for later restoration
10488 by restore_message. Value is non-zero if the current message isn't
10489 empty. This is a relatively infrequent operation, so it's not
10490 worth optimizing. */
10492 bool
10493 push_message (void)
10495 Lisp_Object msg = current_message ();
10496 Vmessage_stack = Fcons (msg, Vmessage_stack);
10497 return STRINGP (msg);
10501 /* Restore message display from the top of Vmessage_stack. */
10503 void
10504 restore_message (void)
10506 eassert (CONSP (Vmessage_stack));
10507 message3_nolog (XCAR (Vmessage_stack));
10511 /* Handler for record_unwind_protect calling pop_message. */
10513 Lisp_Object
10514 pop_message_unwind (Lisp_Object dummy)
10516 pop_message ();
10517 return Qnil;
10520 /* Pop the top-most entry off Vmessage_stack. */
10522 static void
10523 pop_message (void)
10525 eassert (CONSP (Vmessage_stack));
10526 Vmessage_stack = XCDR (Vmessage_stack);
10530 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10531 exits. If the stack is not empty, we have a missing pop_message
10532 somewhere. */
10534 void
10535 check_message_stack (void)
10537 if (!NILP (Vmessage_stack))
10538 emacs_abort ();
10542 /* Truncate to NCHARS what will be displayed in the echo area the next
10543 time we display it---but don't redisplay it now. */
10545 void
10546 truncate_echo_area (ptrdiff_t nchars)
10548 if (nchars == 0)
10549 echo_area_buffer[0] = Qnil;
10550 else if (!noninteractive
10551 && INTERACTIVE
10552 && !NILP (echo_area_buffer[0]))
10554 struct frame *sf = SELECTED_FRAME ();
10555 /* Error messages get reported properly by cmd_error, so this must be
10556 just an informative message; if the frame hasn't really been
10557 initialized yet, just toss it. */
10558 if (sf->glyphs_initialized_p)
10559 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10564 /* Helper function for truncate_echo_area. Truncate the current
10565 message to at most NCHARS characters. */
10567 static int
10568 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10570 if (BEG + nchars < Z)
10571 del_range (BEG + nchars, Z);
10572 if (Z == BEG)
10573 echo_area_buffer[0] = Qnil;
10574 return 0;
10577 /* Set the current message to STRING. */
10579 static void
10580 set_message (Lisp_Object string)
10582 eassert (STRINGP (string));
10584 message_enable_multibyte = STRING_MULTIBYTE (string);
10586 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10587 message_buf_print = 0;
10588 help_echo_showing_p = 0;
10590 if (STRINGP (Vdebug_on_message)
10591 && STRINGP (string)
10592 && fast_string_match (Vdebug_on_message, string) >= 0)
10593 call_debugger (list2 (Qerror, string));
10597 /* Helper function for set_message. First argument is ignored and second
10598 argument has the same meaning as for set_message.
10599 This function is called with the echo area buffer being current. */
10601 static int
10602 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10604 eassert (STRINGP (string));
10606 /* Change multibyteness of the echo buffer appropriately. */
10607 if (message_enable_multibyte
10608 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10609 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10611 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10612 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10613 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10615 /* Insert new message at BEG. */
10616 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10618 /* This function takes care of single/multibyte conversion.
10619 We just have to ensure that the echo area buffer has the right
10620 setting of enable_multibyte_characters. */
10621 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10623 return 0;
10627 /* Clear messages. CURRENT_P non-zero means clear the current
10628 message. LAST_DISPLAYED_P non-zero means clear the message
10629 last displayed. */
10631 void
10632 clear_message (int current_p, int last_displayed_p)
10634 if (current_p)
10636 echo_area_buffer[0] = Qnil;
10637 message_cleared_p = 1;
10640 if (last_displayed_p)
10641 echo_area_buffer[1] = Qnil;
10643 message_buf_print = 0;
10646 /* Clear garbaged frames.
10648 This function is used where the old redisplay called
10649 redraw_garbaged_frames which in turn called redraw_frame which in
10650 turn called clear_frame. The call to clear_frame was a source of
10651 flickering. I believe a clear_frame is not necessary. It should
10652 suffice in the new redisplay to invalidate all current matrices,
10653 and ensure a complete redisplay of all windows. */
10655 static void
10656 clear_garbaged_frames (void)
10658 if (frame_garbaged)
10660 Lisp_Object tail, frame;
10661 int changed_count = 0;
10663 FOR_EACH_FRAME (tail, frame)
10665 struct frame *f = XFRAME (frame);
10667 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10669 if (f->resized_p)
10671 redraw_frame (f);
10672 f->force_flush_display_p = 1;
10674 clear_current_matrices (f);
10675 changed_count++;
10676 f->garbaged = 0;
10677 f->resized_p = 0;
10681 frame_garbaged = 0;
10682 if (changed_count)
10683 ++windows_or_buffers_changed;
10688 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10689 is non-zero update selected_frame. Value is non-zero if the
10690 mini-windows height has been changed. */
10692 static int
10693 echo_area_display (int update_frame_p)
10695 Lisp_Object mini_window;
10696 struct window *w;
10697 struct frame *f;
10698 int window_height_changed_p = 0;
10699 struct frame *sf = SELECTED_FRAME ();
10701 mini_window = FRAME_MINIBUF_WINDOW (sf);
10702 w = XWINDOW (mini_window);
10703 f = XFRAME (WINDOW_FRAME (w));
10705 /* Don't display if frame is invisible or not yet initialized. */
10706 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10707 return 0;
10709 #ifdef HAVE_WINDOW_SYSTEM
10710 /* When Emacs starts, selected_frame may be the initial terminal
10711 frame. If we let this through, a message would be displayed on
10712 the terminal. */
10713 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10714 return 0;
10715 #endif /* HAVE_WINDOW_SYSTEM */
10717 /* Redraw garbaged frames. */
10718 clear_garbaged_frames ();
10720 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10722 echo_area_window = mini_window;
10723 window_height_changed_p = display_echo_area (w);
10724 w->must_be_updated_p = 1;
10726 /* Update the display, unless called from redisplay_internal.
10727 Also don't update the screen during redisplay itself. The
10728 update will happen at the end of redisplay, and an update
10729 here could cause confusion. */
10730 if (update_frame_p && !redisplaying_p)
10732 int n = 0;
10734 /* If the display update has been interrupted by pending
10735 input, update mode lines in the frame. Due to the
10736 pending input, it might have been that redisplay hasn't
10737 been called, so that mode lines above the echo area are
10738 garbaged. This looks odd, so we prevent it here. */
10739 if (!display_completed)
10740 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10742 if (window_height_changed_p
10743 /* Don't do this if Emacs is shutting down. Redisplay
10744 needs to run hooks. */
10745 && !NILP (Vrun_hooks))
10747 /* Must update other windows. Likewise as in other
10748 cases, don't let this update be interrupted by
10749 pending input. */
10750 ptrdiff_t count = SPECPDL_INDEX ();
10751 specbind (Qredisplay_dont_pause, Qt);
10752 windows_or_buffers_changed = 1;
10753 redisplay_internal ();
10754 unbind_to (count, Qnil);
10756 else if (FRAME_WINDOW_P (f) && n == 0)
10758 /* Window configuration is the same as before.
10759 Can do with a display update of the echo area,
10760 unless we displayed some mode lines. */
10761 update_single_window (w, 1);
10762 FRAME_RIF (f)->flush_display (f);
10764 else
10765 update_frame (f, 1, 1);
10767 /* If cursor is in the echo area, make sure that the next
10768 redisplay displays the minibuffer, so that the cursor will
10769 be replaced with what the minibuffer wants. */
10770 if (cursor_in_echo_area)
10771 ++windows_or_buffers_changed;
10774 else if (!EQ (mini_window, selected_window))
10775 windows_or_buffers_changed++;
10777 /* Last displayed message is now the current message. */
10778 echo_area_buffer[1] = echo_area_buffer[0];
10779 /* Inform read_char that we're not echoing. */
10780 echo_message_buffer = Qnil;
10782 /* Prevent redisplay optimization in redisplay_internal by resetting
10783 this_line_start_pos. This is done because the mini-buffer now
10784 displays the message instead of its buffer text. */
10785 if (EQ (mini_window, selected_window))
10786 CHARPOS (this_line_start_pos) = 0;
10788 return window_height_changed_p;
10791 /* Nonzero if the current window's buffer is shown in more than one
10792 window and was modified since last redisplay. */
10794 static int
10795 buffer_shared_and_changed (void)
10797 return (buffer_window_count (current_buffer) > 1
10798 && UNCHANGED_MODIFIED < MODIFF);
10801 /* Nonzero if W doesn't reflect the actual state of current buffer due
10802 to its text or overlays change. FIXME: this may be called when
10803 XBUFFER (w->contents) != current_buffer, which looks suspicious. */
10805 static int
10806 window_outdated (struct window *w)
10808 return (w->last_modified < MODIFF
10809 || w->last_overlay_modified < OVERLAY_MODIFF);
10812 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10813 is enabled and mark of W's buffer was changed since last W's update. */
10815 static int
10816 window_buffer_changed (struct window *w)
10818 struct buffer *b = XBUFFER (w->contents);
10820 eassert (BUFFER_LIVE_P (b));
10822 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10823 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10824 != (w->region_showing != 0)));
10827 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10829 static int
10830 mode_line_update_needed (struct window *w)
10832 return (w->column_number_displayed != -1
10833 && !(PT == w->last_point && !window_outdated (w))
10834 && (w->column_number_displayed != current_column ()));
10837 /***********************************************************************
10838 Mode Lines and Frame Titles
10839 ***********************************************************************/
10841 /* A buffer for constructing non-propertized mode-line strings and
10842 frame titles in it; allocated from the heap in init_xdisp and
10843 resized as needed in store_mode_line_noprop_char. */
10845 static char *mode_line_noprop_buf;
10847 /* The buffer's end, and a current output position in it. */
10849 static char *mode_line_noprop_buf_end;
10850 static char *mode_line_noprop_ptr;
10852 #define MODE_LINE_NOPROP_LEN(start) \
10853 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10855 static enum {
10856 MODE_LINE_DISPLAY = 0,
10857 MODE_LINE_TITLE,
10858 MODE_LINE_NOPROP,
10859 MODE_LINE_STRING
10860 } mode_line_target;
10862 /* Alist that caches the results of :propertize.
10863 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10864 static Lisp_Object mode_line_proptrans_alist;
10866 /* List of strings making up the mode-line. */
10867 static Lisp_Object mode_line_string_list;
10869 /* Base face property when building propertized mode line string. */
10870 static Lisp_Object mode_line_string_face;
10871 static Lisp_Object mode_line_string_face_prop;
10874 /* Unwind data for mode line strings */
10876 static Lisp_Object Vmode_line_unwind_vector;
10878 static Lisp_Object
10879 format_mode_line_unwind_data (struct frame *target_frame,
10880 struct buffer *obuf,
10881 Lisp_Object owin,
10882 int save_proptrans)
10884 Lisp_Object vector, tmp;
10886 /* Reduce consing by keeping one vector in
10887 Vwith_echo_area_save_vector. */
10888 vector = Vmode_line_unwind_vector;
10889 Vmode_line_unwind_vector = Qnil;
10891 if (NILP (vector))
10892 vector = Fmake_vector (make_number (10), Qnil);
10894 ASET (vector, 0, make_number (mode_line_target));
10895 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10896 ASET (vector, 2, mode_line_string_list);
10897 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10898 ASET (vector, 4, mode_line_string_face);
10899 ASET (vector, 5, mode_line_string_face_prop);
10901 if (obuf)
10902 XSETBUFFER (tmp, obuf);
10903 else
10904 tmp = Qnil;
10905 ASET (vector, 6, tmp);
10906 ASET (vector, 7, owin);
10907 if (target_frame)
10909 /* Similarly to `with-selected-window', if the operation selects
10910 a window on another frame, we must restore that frame's
10911 selected window, and (for a tty) the top-frame. */
10912 ASET (vector, 8, target_frame->selected_window);
10913 if (FRAME_TERMCAP_P (target_frame))
10914 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10917 return vector;
10920 static Lisp_Object
10921 unwind_format_mode_line (Lisp_Object vector)
10923 Lisp_Object old_window = AREF (vector, 7);
10924 Lisp_Object target_frame_window = AREF (vector, 8);
10925 Lisp_Object old_top_frame = AREF (vector, 9);
10927 mode_line_target = XINT (AREF (vector, 0));
10928 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10929 mode_line_string_list = AREF (vector, 2);
10930 if (! EQ (AREF (vector, 3), Qt))
10931 mode_line_proptrans_alist = AREF (vector, 3);
10932 mode_line_string_face = AREF (vector, 4);
10933 mode_line_string_face_prop = AREF (vector, 5);
10935 /* Select window before buffer, since it may change the buffer. */
10936 if (!NILP (old_window))
10938 /* If the operation that we are unwinding had selected a window
10939 on a different frame, reset its frame-selected-window. For a
10940 text terminal, reset its top-frame if necessary. */
10941 if (!NILP (target_frame_window))
10943 Lisp_Object frame
10944 = WINDOW_FRAME (XWINDOW (target_frame_window));
10946 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10947 Fselect_window (target_frame_window, Qt);
10949 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10950 Fselect_frame (old_top_frame, Qt);
10953 Fselect_window (old_window, Qt);
10956 if (!NILP (AREF (vector, 6)))
10958 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10959 ASET (vector, 6, Qnil);
10962 Vmode_line_unwind_vector = vector;
10963 return Qnil;
10967 /* Store a single character C for the frame title in mode_line_noprop_buf.
10968 Re-allocate mode_line_noprop_buf if necessary. */
10970 static void
10971 store_mode_line_noprop_char (char c)
10973 /* If output position has reached the end of the allocated buffer,
10974 increase the buffer's size. */
10975 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10977 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10978 ptrdiff_t size = len;
10979 mode_line_noprop_buf =
10980 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10981 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10982 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10985 *mode_line_noprop_ptr++ = c;
10989 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10990 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10991 characters that yield more columns than PRECISION; PRECISION <= 0
10992 means copy the whole string. Pad with spaces until FIELD_WIDTH
10993 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10994 pad. Called from display_mode_element when it is used to build a
10995 frame title. */
10997 static int
10998 store_mode_line_noprop (const char *string, int field_width, int precision)
11000 const unsigned char *str = (const unsigned char *) string;
11001 int n = 0;
11002 ptrdiff_t dummy, nbytes;
11004 /* Copy at most PRECISION chars from STR. */
11005 nbytes = strlen (string);
11006 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11007 while (nbytes--)
11008 store_mode_line_noprop_char (*str++);
11010 /* Fill up with spaces until FIELD_WIDTH reached. */
11011 while (field_width > 0
11012 && n < field_width)
11014 store_mode_line_noprop_char (' ');
11015 ++n;
11018 return n;
11021 /***********************************************************************
11022 Frame Titles
11023 ***********************************************************************/
11025 #ifdef HAVE_WINDOW_SYSTEM
11027 /* Set the title of FRAME, if it has changed. The title format is
11028 Vicon_title_format if FRAME is iconified, otherwise it is
11029 frame_title_format. */
11031 static void
11032 x_consider_frame_title (Lisp_Object frame)
11034 struct frame *f = XFRAME (frame);
11036 if (FRAME_WINDOW_P (f)
11037 || FRAME_MINIBUF_ONLY_P (f)
11038 || f->explicit_name)
11040 /* Do we have more than one visible frame on this X display? */
11041 Lisp_Object tail, other_frame, fmt;
11042 ptrdiff_t title_start;
11043 char *title;
11044 ptrdiff_t len;
11045 struct it it;
11046 ptrdiff_t count = SPECPDL_INDEX ();
11048 FOR_EACH_FRAME (tail, other_frame)
11050 struct frame *tf = XFRAME (other_frame);
11052 if (tf != f
11053 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11054 && !FRAME_MINIBUF_ONLY_P (tf)
11055 && !EQ (other_frame, tip_frame)
11056 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11057 break;
11060 /* Set global variable indicating that multiple frames exist. */
11061 multiple_frames = CONSP (tail);
11063 /* Switch to the buffer of selected window of the frame. Set up
11064 mode_line_target so that display_mode_element will output into
11065 mode_line_noprop_buf; then display the title. */
11066 record_unwind_protect (unwind_format_mode_line,
11067 format_mode_line_unwind_data
11068 (f, current_buffer, selected_window, 0));
11070 Fselect_window (f->selected_window, Qt);
11071 set_buffer_internal_1
11072 (XBUFFER (XWINDOW (f->selected_window)->contents));
11073 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11075 mode_line_target = MODE_LINE_TITLE;
11076 title_start = MODE_LINE_NOPROP_LEN (0);
11077 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11078 NULL, DEFAULT_FACE_ID);
11079 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11080 len = MODE_LINE_NOPROP_LEN (title_start);
11081 title = mode_line_noprop_buf + title_start;
11082 unbind_to (count, Qnil);
11084 /* Set the title only if it's changed. This avoids consing in
11085 the common case where it hasn't. (If it turns out that we've
11086 already wasted too much time by walking through the list with
11087 display_mode_element, then we might need to optimize at a
11088 higher level than this.) */
11089 if (! STRINGP (f->name)
11090 || SBYTES (f->name) != len
11091 || memcmp (title, SDATA (f->name), len) != 0)
11092 x_implicitly_set_name (f, make_string (title, len), Qnil);
11096 #endif /* not HAVE_WINDOW_SYSTEM */
11099 /***********************************************************************
11100 Menu Bars
11101 ***********************************************************************/
11104 /* Prepare for redisplay by updating menu-bar item lists when
11105 appropriate. This can call eval. */
11107 void
11108 prepare_menu_bars (void)
11110 int all_windows;
11111 struct gcpro gcpro1, gcpro2;
11112 struct frame *f;
11113 Lisp_Object tooltip_frame;
11115 #ifdef HAVE_WINDOW_SYSTEM
11116 tooltip_frame = tip_frame;
11117 #else
11118 tooltip_frame = Qnil;
11119 #endif
11121 /* Update all frame titles based on their buffer names, etc. We do
11122 this before the menu bars so that the buffer-menu will show the
11123 up-to-date frame titles. */
11124 #ifdef HAVE_WINDOW_SYSTEM
11125 if (windows_or_buffers_changed || update_mode_lines)
11127 Lisp_Object tail, frame;
11129 FOR_EACH_FRAME (tail, frame)
11131 f = XFRAME (frame);
11132 if (!EQ (frame, tooltip_frame)
11133 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11134 x_consider_frame_title (frame);
11137 #endif /* HAVE_WINDOW_SYSTEM */
11139 /* Update the menu bar item lists, if appropriate. This has to be
11140 done before any actual redisplay or generation of display lines. */
11141 all_windows = (update_mode_lines
11142 || buffer_shared_and_changed ()
11143 || windows_or_buffers_changed);
11144 if (all_windows)
11146 Lisp_Object tail, frame;
11147 ptrdiff_t count = SPECPDL_INDEX ();
11148 /* 1 means that update_menu_bar has run its hooks
11149 so any further calls to update_menu_bar shouldn't do so again. */
11150 int menu_bar_hooks_run = 0;
11152 record_unwind_save_match_data ();
11154 FOR_EACH_FRAME (tail, frame)
11156 f = XFRAME (frame);
11158 /* Ignore tooltip frame. */
11159 if (EQ (frame, tooltip_frame))
11160 continue;
11162 /* If a window on this frame changed size, report that to
11163 the user and clear the size-change flag. */
11164 if (FRAME_WINDOW_SIZES_CHANGED (f))
11166 Lisp_Object functions;
11168 /* Clear flag first in case we get an error below. */
11169 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11170 functions = Vwindow_size_change_functions;
11171 GCPRO2 (tail, functions);
11173 while (CONSP (functions))
11175 if (!EQ (XCAR (functions), Qt))
11176 call1 (XCAR (functions), frame);
11177 functions = XCDR (functions);
11179 UNGCPRO;
11182 GCPRO1 (tail);
11183 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11184 #ifdef HAVE_WINDOW_SYSTEM
11185 update_tool_bar (f, 0);
11186 #endif
11187 #ifdef HAVE_NS
11188 if (windows_or_buffers_changed
11189 && FRAME_NS_P (f))
11190 ns_set_doc_edited
11191 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11192 #endif
11193 UNGCPRO;
11196 unbind_to (count, Qnil);
11198 else
11200 struct frame *sf = SELECTED_FRAME ();
11201 update_menu_bar (sf, 1, 0);
11202 #ifdef HAVE_WINDOW_SYSTEM
11203 update_tool_bar (sf, 1);
11204 #endif
11209 /* Update the menu bar item list for frame F. This has to be done
11210 before we start to fill in any display lines, because it can call
11211 eval.
11213 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11215 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11216 already ran the menu bar hooks for this redisplay, so there
11217 is no need to run them again. The return value is the
11218 updated value of this flag, to pass to the next call. */
11220 static int
11221 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11223 Lisp_Object window;
11224 register struct window *w;
11226 /* If called recursively during a menu update, do nothing. This can
11227 happen when, for instance, an activate-menubar-hook causes a
11228 redisplay. */
11229 if (inhibit_menubar_update)
11230 return hooks_run;
11232 window = FRAME_SELECTED_WINDOW (f);
11233 w = XWINDOW (window);
11235 if (FRAME_WINDOW_P (f)
11237 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11238 || defined (HAVE_NS) || defined (USE_GTK)
11239 FRAME_EXTERNAL_MENU_BAR (f)
11240 #else
11241 FRAME_MENU_BAR_LINES (f) > 0
11242 #endif
11243 : FRAME_MENU_BAR_LINES (f) > 0)
11245 /* If the user has switched buffers or windows, we need to
11246 recompute to reflect the new bindings. But we'll
11247 recompute when update_mode_lines is set too; that means
11248 that people can use force-mode-line-update to request
11249 that the menu bar be recomputed. The adverse effect on
11250 the rest of the redisplay algorithm is about the same as
11251 windows_or_buffers_changed anyway. */
11252 if (windows_or_buffers_changed
11253 /* This used to test w->update_mode_line, but we believe
11254 there is no need to recompute the menu in that case. */
11255 || update_mode_lines
11256 || window_buffer_changed (w))
11258 struct buffer *prev = current_buffer;
11259 ptrdiff_t count = SPECPDL_INDEX ();
11261 specbind (Qinhibit_menubar_update, Qt);
11263 set_buffer_internal_1 (XBUFFER (w->contents));
11264 if (save_match_data)
11265 record_unwind_save_match_data ();
11266 if (NILP (Voverriding_local_map_menu_flag))
11268 specbind (Qoverriding_terminal_local_map, Qnil);
11269 specbind (Qoverriding_local_map, Qnil);
11272 if (!hooks_run)
11274 /* Run the Lucid hook. */
11275 safe_run_hooks (Qactivate_menubar_hook);
11277 /* If it has changed current-menubar from previous value,
11278 really recompute the menu-bar from the value. */
11279 if (! NILP (Vlucid_menu_bar_dirty_flag))
11280 call0 (Qrecompute_lucid_menubar);
11282 safe_run_hooks (Qmenu_bar_update_hook);
11284 hooks_run = 1;
11287 XSETFRAME (Vmenu_updating_frame, f);
11288 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11290 /* Redisplay the menu bar in case we changed it. */
11291 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11292 || defined (HAVE_NS) || defined (USE_GTK)
11293 if (FRAME_WINDOW_P (f))
11295 #if defined (HAVE_NS)
11296 /* All frames on Mac OS share the same menubar. So only
11297 the selected frame should be allowed to set it. */
11298 if (f == SELECTED_FRAME ())
11299 #endif
11300 set_frame_menubar (f, 0, 0);
11302 else
11303 /* On a terminal screen, the menu bar is an ordinary screen
11304 line, and this makes it get updated. */
11305 w->update_mode_line = 1;
11306 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11307 /* In the non-toolkit version, the menu bar is an ordinary screen
11308 line, and this makes it get updated. */
11309 w->update_mode_line = 1;
11310 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11312 unbind_to (count, Qnil);
11313 set_buffer_internal_1 (prev);
11317 return hooks_run;
11322 /***********************************************************************
11323 Output Cursor
11324 ***********************************************************************/
11326 #ifdef HAVE_WINDOW_SYSTEM
11328 /* EXPORT:
11329 Nominal cursor position -- where to draw output.
11330 HPOS and VPOS are window relative glyph matrix coordinates.
11331 X and Y are window relative pixel coordinates. */
11333 struct cursor_pos output_cursor;
11336 /* EXPORT:
11337 Set the global variable output_cursor to CURSOR. All cursor
11338 positions are relative to updated_window. */
11340 void
11341 set_output_cursor (struct cursor_pos *cursor)
11343 output_cursor.hpos = cursor->hpos;
11344 output_cursor.vpos = cursor->vpos;
11345 output_cursor.x = cursor->x;
11346 output_cursor.y = cursor->y;
11350 /* EXPORT for RIF:
11351 Set a nominal cursor position.
11353 HPOS and VPOS are column/row positions in a window glyph matrix. X
11354 and Y are window text area relative pixel positions.
11356 If this is done during an update, updated_window will contain the
11357 window that is being updated and the position is the future output
11358 cursor position for that window. If updated_window is null, use
11359 selected_window and display the cursor at the given position. */
11361 void
11362 x_cursor_to (int vpos, int hpos, int y, int x)
11364 struct window *w;
11366 /* If updated_window is not set, work on selected_window. */
11367 if (updated_window)
11368 w = updated_window;
11369 else
11370 w = XWINDOW (selected_window);
11372 /* Set the output cursor. */
11373 output_cursor.hpos = hpos;
11374 output_cursor.vpos = vpos;
11375 output_cursor.x = x;
11376 output_cursor.y = y;
11378 /* If not called as part of an update, really display the cursor.
11379 This will also set the cursor position of W. */
11380 if (updated_window == NULL)
11382 block_input ();
11383 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11384 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11385 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11386 unblock_input ();
11390 #endif /* HAVE_WINDOW_SYSTEM */
11393 /***********************************************************************
11394 Tool-bars
11395 ***********************************************************************/
11397 #ifdef HAVE_WINDOW_SYSTEM
11399 /* Where the mouse was last time we reported a mouse event. */
11401 FRAME_PTR last_mouse_frame;
11403 /* Tool-bar item index of the item on which a mouse button was pressed
11404 or -1. */
11406 int last_tool_bar_item;
11408 /* Select `frame' temporarily without running all the code in
11409 do_switch_frame.
11410 FIXME: Maybe do_switch_frame should be trimmed down similarly
11411 when `norecord' is set. */
11412 static Lisp_Object
11413 fast_set_selected_frame (Lisp_Object frame)
11415 if (!EQ (selected_frame, frame))
11417 selected_frame = frame;
11418 selected_window = XFRAME (frame)->selected_window;
11420 return Qnil;
11423 /* Update the tool-bar item list for frame F. This has to be done
11424 before we start to fill in any display lines. Called from
11425 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11426 and restore it here. */
11428 static void
11429 update_tool_bar (struct frame *f, int save_match_data)
11431 #if defined (USE_GTK) || defined (HAVE_NS)
11432 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11433 #else
11434 int do_update = WINDOWP (f->tool_bar_window)
11435 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11436 #endif
11438 if (do_update)
11440 Lisp_Object window;
11441 struct window *w;
11443 window = FRAME_SELECTED_WINDOW (f);
11444 w = XWINDOW (window);
11446 /* If the user has switched buffers or windows, we need to
11447 recompute to reflect the new bindings. But we'll
11448 recompute when update_mode_lines is set too; that means
11449 that people can use force-mode-line-update to request
11450 that the menu bar be recomputed. The adverse effect on
11451 the rest of the redisplay algorithm is about the same as
11452 windows_or_buffers_changed anyway. */
11453 if (windows_or_buffers_changed
11454 || w->update_mode_line
11455 || update_mode_lines
11456 || window_buffer_changed (w))
11458 struct buffer *prev = current_buffer;
11459 ptrdiff_t count = SPECPDL_INDEX ();
11460 Lisp_Object frame, new_tool_bar;
11461 int new_n_tool_bar;
11462 struct gcpro gcpro1;
11464 /* Set current_buffer to the buffer of the selected
11465 window of the frame, so that we get the right local
11466 keymaps. */
11467 set_buffer_internal_1 (XBUFFER (w->contents));
11469 /* Save match data, if we must. */
11470 if (save_match_data)
11471 record_unwind_save_match_data ();
11473 /* Make sure that we don't accidentally use bogus keymaps. */
11474 if (NILP (Voverriding_local_map_menu_flag))
11476 specbind (Qoverriding_terminal_local_map, Qnil);
11477 specbind (Qoverriding_local_map, Qnil);
11480 GCPRO1 (new_tool_bar);
11482 /* We must temporarily set the selected frame to this frame
11483 before calling tool_bar_items, because the calculation of
11484 the tool-bar keymap uses the selected frame (see
11485 `tool-bar-make-keymap' in tool-bar.el). */
11486 eassert (EQ (selected_window,
11487 /* Since we only explicitly preserve selected_frame,
11488 check that selected_window would be redundant. */
11489 XFRAME (selected_frame)->selected_window));
11490 record_unwind_protect (fast_set_selected_frame, selected_frame);
11491 XSETFRAME (frame, f);
11492 fast_set_selected_frame (frame);
11494 /* Build desired tool-bar items from keymaps. */
11495 new_tool_bar
11496 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11497 &new_n_tool_bar);
11499 /* Redisplay the tool-bar if we changed it. */
11500 if (new_n_tool_bar != f->n_tool_bar_items
11501 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11503 /* Redisplay that happens asynchronously due to an expose event
11504 may access f->tool_bar_items. Make sure we update both
11505 variables within BLOCK_INPUT so no such event interrupts. */
11506 block_input ();
11507 fset_tool_bar_items (f, new_tool_bar);
11508 f->n_tool_bar_items = new_n_tool_bar;
11509 w->update_mode_line = 1;
11510 unblock_input ();
11513 UNGCPRO;
11515 unbind_to (count, Qnil);
11516 set_buffer_internal_1 (prev);
11522 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11523 F's desired tool-bar contents. F->tool_bar_items must have
11524 been set up previously by calling prepare_menu_bars. */
11526 static void
11527 build_desired_tool_bar_string (struct frame *f)
11529 int i, size, size_needed;
11530 struct gcpro gcpro1, gcpro2, gcpro3;
11531 Lisp_Object image, plist, props;
11533 image = plist = props = Qnil;
11534 GCPRO3 (image, plist, props);
11536 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11537 Otherwise, make a new string. */
11539 /* The size of the string we might be able to reuse. */
11540 size = (STRINGP (f->desired_tool_bar_string)
11541 ? SCHARS (f->desired_tool_bar_string)
11542 : 0);
11544 /* We need one space in the string for each image. */
11545 size_needed = f->n_tool_bar_items;
11547 /* Reuse f->desired_tool_bar_string, if possible. */
11548 if (size < size_needed || NILP (f->desired_tool_bar_string))
11549 fset_desired_tool_bar_string
11550 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11551 else
11553 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11554 Fremove_text_properties (make_number (0), make_number (size),
11555 props, f->desired_tool_bar_string);
11558 /* Put a `display' property on the string for the images to display,
11559 put a `menu_item' property on tool-bar items with a value that
11560 is the index of the item in F's tool-bar item vector. */
11561 for (i = 0; i < f->n_tool_bar_items; ++i)
11563 #define PROP(IDX) \
11564 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11566 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11567 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11568 int hmargin, vmargin, relief, idx, end;
11570 /* If image is a vector, choose the image according to the
11571 button state. */
11572 image = PROP (TOOL_BAR_ITEM_IMAGES);
11573 if (VECTORP (image))
11575 if (enabled_p)
11576 idx = (selected_p
11577 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11578 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11579 else
11580 idx = (selected_p
11581 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11582 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11584 eassert (ASIZE (image) >= idx);
11585 image = AREF (image, idx);
11587 else
11588 idx = -1;
11590 /* Ignore invalid image specifications. */
11591 if (!valid_image_p (image))
11592 continue;
11594 /* Display the tool-bar button pressed, or depressed. */
11595 plist = Fcopy_sequence (XCDR (image));
11597 /* Compute margin and relief to draw. */
11598 relief = (tool_bar_button_relief >= 0
11599 ? tool_bar_button_relief
11600 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11601 hmargin = vmargin = relief;
11603 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11604 INT_MAX - max (hmargin, vmargin)))
11606 hmargin += XFASTINT (Vtool_bar_button_margin);
11607 vmargin += XFASTINT (Vtool_bar_button_margin);
11609 else if (CONSP (Vtool_bar_button_margin))
11611 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11612 INT_MAX - hmargin))
11613 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11615 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11616 INT_MAX - vmargin))
11617 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11620 if (auto_raise_tool_bar_buttons_p)
11622 /* Add a `:relief' property to the image spec if the item is
11623 selected. */
11624 if (selected_p)
11626 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11627 hmargin -= relief;
11628 vmargin -= relief;
11631 else
11633 /* If image is selected, display it pressed, i.e. with a
11634 negative relief. If it's not selected, display it with a
11635 raised relief. */
11636 plist = Fplist_put (plist, QCrelief,
11637 (selected_p
11638 ? make_number (-relief)
11639 : make_number (relief)));
11640 hmargin -= relief;
11641 vmargin -= relief;
11644 /* Put a margin around the image. */
11645 if (hmargin || vmargin)
11647 if (hmargin == vmargin)
11648 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11649 else
11650 plist = Fplist_put (plist, QCmargin,
11651 Fcons (make_number (hmargin),
11652 make_number (vmargin)));
11655 /* If button is not enabled, and we don't have special images
11656 for the disabled state, make the image appear disabled by
11657 applying an appropriate algorithm to it. */
11658 if (!enabled_p && idx < 0)
11659 plist = Fplist_put (plist, QCconversion, Qdisabled);
11661 /* Put a `display' text property on the string for the image to
11662 display. Put a `menu-item' property on the string that gives
11663 the start of this item's properties in the tool-bar items
11664 vector. */
11665 image = Fcons (Qimage, plist);
11666 props = list4 (Qdisplay, image,
11667 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11669 /* Let the last image hide all remaining spaces in the tool bar
11670 string. The string can be longer than needed when we reuse a
11671 previous string. */
11672 if (i + 1 == f->n_tool_bar_items)
11673 end = SCHARS (f->desired_tool_bar_string);
11674 else
11675 end = i + 1;
11676 Fadd_text_properties (make_number (i), make_number (end),
11677 props, f->desired_tool_bar_string);
11678 #undef PROP
11681 UNGCPRO;
11685 /* Display one line of the tool-bar of frame IT->f.
11687 HEIGHT specifies the desired height of the tool-bar line.
11688 If the actual height of the glyph row is less than HEIGHT, the
11689 row's height is increased to HEIGHT, and the icons are centered
11690 vertically in the new height.
11692 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11693 count a final empty row in case the tool-bar width exactly matches
11694 the window width.
11697 static void
11698 display_tool_bar_line (struct it *it, int height)
11700 struct glyph_row *row = it->glyph_row;
11701 int max_x = it->last_visible_x;
11702 struct glyph *last;
11704 prepare_desired_row (row);
11705 row->y = it->current_y;
11707 /* Note that this isn't made use of if the face hasn't a box,
11708 so there's no need to check the face here. */
11709 it->start_of_box_run_p = 1;
11711 while (it->current_x < max_x)
11713 int x, n_glyphs_before, i, nglyphs;
11714 struct it it_before;
11716 /* Get the next display element. */
11717 if (!get_next_display_element (it))
11719 /* Don't count empty row if we are counting needed tool-bar lines. */
11720 if (height < 0 && !it->hpos)
11721 return;
11722 break;
11725 /* Produce glyphs. */
11726 n_glyphs_before = row->used[TEXT_AREA];
11727 it_before = *it;
11729 PRODUCE_GLYPHS (it);
11731 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11732 i = 0;
11733 x = it_before.current_x;
11734 while (i < nglyphs)
11736 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11738 if (x + glyph->pixel_width > max_x)
11740 /* Glyph doesn't fit on line. Backtrack. */
11741 row->used[TEXT_AREA] = n_glyphs_before;
11742 *it = it_before;
11743 /* If this is the only glyph on this line, it will never fit on the
11744 tool-bar, so skip it. But ensure there is at least one glyph,
11745 so we don't accidentally disable the tool-bar. */
11746 if (n_glyphs_before == 0
11747 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11748 break;
11749 goto out;
11752 ++it->hpos;
11753 x += glyph->pixel_width;
11754 ++i;
11757 /* Stop at line end. */
11758 if (ITERATOR_AT_END_OF_LINE_P (it))
11759 break;
11761 set_iterator_to_next (it, 1);
11764 out:;
11766 row->displays_text_p = row->used[TEXT_AREA] != 0;
11768 /* Use default face for the border below the tool bar.
11770 FIXME: When auto-resize-tool-bars is grow-only, there is
11771 no additional border below the possibly empty tool-bar lines.
11772 So to make the extra empty lines look "normal", we have to
11773 use the tool-bar face for the border too. */
11774 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11775 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11776 it->face_id = DEFAULT_FACE_ID;
11778 extend_face_to_end_of_line (it);
11779 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11780 last->right_box_line_p = 1;
11781 if (last == row->glyphs[TEXT_AREA])
11782 last->left_box_line_p = 1;
11784 /* Make line the desired height and center it vertically. */
11785 if ((height -= it->max_ascent + it->max_descent) > 0)
11787 /* Don't add more than one line height. */
11788 height %= FRAME_LINE_HEIGHT (it->f);
11789 it->max_ascent += height / 2;
11790 it->max_descent += (height + 1) / 2;
11793 compute_line_metrics (it);
11795 /* If line is empty, make it occupy the rest of the tool-bar. */
11796 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11798 row->height = row->phys_height = it->last_visible_y - row->y;
11799 row->visible_height = row->height;
11800 row->ascent = row->phys_ascent = 0;
11801 row->extra_line_spacing = 0;
11804 row->full_width_p = 1;
11805 row->continued_p = 0;
11806 row->truncated_on_left_p = 0;
11807 row->truncated_on_right_p = 0;
11809 it->current_x = it->hpos = 0;
11810 it->current_y += row->height;
11811 ++it->vpos;
11812 ++it->glyph_row;
11816 /* Max tool-bar height. */
11818 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11819 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11821 /* Value is the number of screen lines needed to make all tool-bar
11822 items of frame F visible. The number of actual rows needed is
11823 returned in *N_ROWS if non-NULL. */
11825 static int
11826 tool_bar_lines_needed (struct frame *f, int *n_rows)
11828 struct window *w = XWINDOW (f->tool_bar_window);
11829 struct it it;
11830 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11831 the desired matrix, so use (unused) mode-line row as temporary row to
11832 avoid destroying the first tool-bar row. */
11833 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11835 /* Initialize an iterator for iteration over
11836 F->desired_tool_bar_string in the tool-bar window of frame F. */
11837 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11838 it.first_visible_x = 0;
11839 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11840 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11841 it.paragraph_embedding = L2R;
11843 while (!ITERATOR_AT_END_P (&it))
11845 clear_glyph_row (temp_row);
11846 it.glyph_row = temp_row;
11847 display_tool_bar_line (&it, -1);
11849 clear_glyph_row (temp_row);
11851 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11852 if (n_rows)
11853 *n_rows = it.vpos > 0 ? it.vpos : -1;
11855 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11859 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11860 0, 1, 0,
11861 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11862 If FRAME is nil or omitted, use the selected frame. */)
11863 (Lisp_Object frame)
11865 struct frame *f = decode_any_frame (frame);
11866 struct window *w;
11867 int nlines = 0;
11869 if (WINDOWP (f->tool_bar_window)
11870 && (w = XWINDOW (f->tool_bar_window),
11871 WINDOW_TOTAL_LINES (w) > 0))
11873 update_tool_bar (f, 1);
11874 if (f->n_tool_bar_items)
11876 build_desired_tool_bar_string (f);
11877 nlines = tool_bar_lines_needed (f, NULL);
11881 return make_number (nlines);
11885 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11886 height should be changed. */
11888 static int
11889 redisplay_tool_bar (struct frame *f)
11891 struct window *w;
11892 struct it it;
11893 struct glyph_row *row;
11895 #if defined (USE_GTK) || defined (HAVE_NS)
11896 if (FRAME_EXTERNAL_TOOL_BAR (f))
11897 update_frame_tool_bar (f);
11898 return 0;
11899 #endif
11901 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11902 do anything. This means you must start with tool-bar-lines
11903 non-zero to get the auto-sizing effect. Or in other words, you
11904 can turn off tool-bars by specifying tool-bar-lines zero. */
11905 if (!WINDOWP (f->tool_bar_window)
11906 || (w = XWINDOW (f->tool_bar_window),
11907 WINDOW_TOTAL_LINES (w) == 0))
11908 return 0;
11910 /* Set up an iterator for the tool-bar window. */
11911 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11912 it.first_visible_x = 0;
11913 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11914 row = it.glyph_row;
11916 /* Build a string that represents the contents of the tool-bar. */
11917 build_desired_tool_bar_string (f);
11918 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11919 /* FIXME: This should be controlled by a user option. But it
11920 doesn't make sense to have an R2L tool bar if the menu bar cannot
11921 be drawn also R2L, and making the menu bar R2L is tricky due
11922 toolkit-specific code that implements it. If an R2L tool bar is
11923 ever supported, display_tool_bar_line should also be augmented to
11924 call unproduce_glyphs like display_line and display_string
11925 do. */
11926 it.paragraph_embedding = L2R;
11928 if (f->n_tool_bar_rows == 0)
11930 int nlines;
11932 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11933 nlines != WINDOW_TOTAL_LINES (w)))
11935 Lisp_Object frame;
11936 int old_height = WINDOW_TOTAL_LINES (w);
11938 XSETFRAME (frame, f);
11939 Fmodify_frame_parameters (frame,
11940 Fcons (Fcons (Qtool_bar_lines,
11941 make_number (nlines)),
11942 Qnil));
11943 if (WINDOW_TOTAL_LINES (w) != old_height)
11945 clear_glyph_matrix (w->desired_matrix);
11946 fonts_changed_p = 1;
11947 return 1;
11952 /* Display as many lines as needed to display all tool-bar items. */
11954 if (f->n_tool_bar_rows > 0)
11956 int border, rows, height, extra;
11958 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11959 border = XINT (Vtool_bar_border);
11960 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11961 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11962 else if (EQ (Vtool_bar_border, Qborder_width))
11963 border = f->border_width;
11964 else
11965 border = 0;
11966 if (border < 0)
11967 border = 0;
11969 rows = f->n_tool_bar_rows;
11970 height = max (1, (it.last_visible_y - border) / rows);
11971 extra = it.last_visible_y - border - height * rows;
11973 while (it.current_y < it.last_visible_y)
11975 int h = 0;
11976 if (extra > 0 && rows-- > 0)
11978 h = (extra + rows - 1) / rows;
11979 extra -= h;
11981 display_tool_bar_line (&it, height + h);
11984 else
11986 while (it.current_y < it.last_visible_y)
11987 display_tool_bar_line (&it, 0);
11990 /* It doesn't make much sense to try scrolling in the tool-bar
11991 window, so don't do it. */
11992 w->desired_matrix->no_scrolling_p = 1;
11993 w->must_be_updated_p = 1;
11995 if (!NILP (Vauto_resize_tool_bars))
11997 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11998 int change_height_p = 0;
12000 /* If we couldn't display everything, change the tool-bar's
12001 height if there is room for more. */
12002 if (IT_STRING_CHARPOS (it) < it.end_charpos
12003 && it.current_y < max_tool_bar_height)
12004 change_height_p = 1;
12006 row = it.glyph_row - 1;
12008 /* If there are blank lines at the end, except for a partially
12009 visible blank line at the end that is smaller than
12010 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12011 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12012 && row->height >= FRAME_LINE_HEIGHT (f))
12013 change_height_p = 1;
12015 /* If row displays tool-bar items, but is partially visible,
12016 change the tool-bar's height. */
12017 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12018 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12019 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12020 change_height_p = 1;
12022 /* Resize windows as needed by changing the `tool-bar-lines'
12023 frame parameter. */
12024 if (change_height_p)
12026 Lisp_Object frame;
12027 int old_height = WINDOW_TOTAL_LINES (w);
12028 int nrows;
12029 int nlines = tool_bar_lines_needed (f, &nrows);
12031 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12032 && !f->minimize_tool_bar_window_p)
12033 ? (nlines > old_height)
12034 : (nlines != old_height));
12035 f->minimize_tool_bar_window_p = 0;
12037 if (change_height_p)
12039 XSETFRAME (frame, f);
12040 Fmodify_frame_parameters (frame,
12041 Fcons (Fcons (Qtool_bar_lines,
12042 make_number (nlines)),
12043 Qnil));
12044 if (WINDOW_TOTAL_LINES (w) != old_height)
12046 clear_glyph_matrix (w->desired_matrix);
12047 f->n_tool_bar_rows = nrows;
12048 fonts_changed_p = 1;
12049 return 1;
12055 f->minimize_tool_bar_window_p = 0;
12056 return 0;
12060 /* Get information about the tool-bar item which is displayed in GLYPH
12061 on frame F. Return in *PROP_IDX the index where tool-bar item
12062 properties start in F->tool_bar_items. Value is zero if
12063 GLYPH doesn't display a tool-bar item. */
12065 static int
12066 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12068 Lisp_Object prop;
12069 int success_p;
12070 int charpos;
12072 /* This function can be called asynchronously, which means we must
12073 exclude any possibility that Fget_text_property signals an
12074 error. */
12075 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12076 charpos = max (0, charpos);
12078 /* Get the text property `menu-item' at pos. The value of that
12079 property is the start index of this item's properties in
12080 F->tool_bar_items. */
12081 prop = Fget_text_property (make_number (charpos),
12082 Qmenu_item, f->current_tool_bar_string);
12083 if (INTEGERP (prop))
12085 *prop_idx = XINT (prop);
12086 success_p = 1;
12088 else
12089 success_p = 0;
12091 return success_p;
12095 /* Get information about the tool-bar item at position X/Y on frame F.
12096 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12097 the current matrix of the tool-bar window of F, or NULL if not
12098 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12099 item in F->tool_bar_items. Value is
12101 -1 if X/Y is not on a tool-bar item
12102 0 if X/Y is on the same item that was highlighted before.
12103 1 otherwise. */
12105 static int
12106 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12107 int *hpos, int *vpos, int *prop_idx)
12109 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12110 struct window *w = XWINDOW (f->tool_bar_window);
12111 int area;
12113 /* Find the glyph under X/Y. */
12114 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12115 if (*glyph == NULL)
12116 return -1;
12118 /* Get the start of this tool-bar item's properties in
12119 f->tool_bar_items. */
12120 if (!tool_bar_item_info (f, *glyph, prop_idx))
12121 return -1;
12123 /* Is mouse on the highlighted item? */
12124 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12125 && *vpos >= hlinfo->mouse_face_beg_row
12126 && *vpos <= hlinfo->mouse_face_end_row
12127 && (*vpos > hlinfo->mouse_face_beg_row
12128 || *hpos >= hlinfo->mouse_face_beg_col)
12129 && (*vpos < hlinfo->mouse_face_end_row
12130 || *hpos < hlinfo->mouse_face_end_col
12131 || hlinfo->mouse_face_past_end))
12132 return 0;
12134 return 1;
12138 /* EXPORT:
12139 Handle mouse button event on the tool-bar of frame F, at
12140 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12141 0 for button release. MODIFIERS is event modifiers for button
12142 release. */
12144 void
12145 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12146 int modifiers)
12148 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12149 struct window *w = XWINDOW (f->tool_bar_window);
12150 int hpos, vpos, prop_idx;
12151 struct glyph *glyph;
12152 Lisp_Object enabled_p;
12153 int ts;
12155 /* If not on the highlighted tool-bar item, and mouse-highlight is
12156 non-nil, return. This is so we generate the tool-bar button
12157 click only when the mouse button is released on the same item as
12158 where it was pressed. However, when mouse-highlight is disabled,
12159 generate the click when the button is released regardless of the
12160 highlight, since tool-bar items are not highlighted in that
12161 case. */
12162 frame_to_window_pixel_xy (w, &x, &y);
12163 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12164 if (ts == -1
12165 || (ts != 0 && !NILP (Vmouse_highlight)))
12166 return;
12168 /* When mouse-highlight is off, generate the click for the item
12169 where the button was pressed, disregarding where it was
12170 released. */
12171 if (NILP (Vmouse_highlight) && !down_p)
12172 prop_idx = last_tool_bar_item;
12174 /* If item is disabled, do nothing. */
12175 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12176 if (NILP (enabled_p))
12177 return;
12179 if (down_p)
12181 /* Show item in pressed state. */
12182 if (!NILP (Vmouse_highlight))
12183 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12184 last_tool_bar_item = prop_idx;
12186 else
12188 Lisp_Object key, frame;
12189 struct input_event event;
12190 EVENT_INIT (event);
12192 /* Show item in released state. */
12193 if (!NILP (Vmouse_highlight))
12194 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12196 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12198 XSETFRAME (frame, f);
12199 event.kind = TOOL_BAR_EVENT;
12200 event.frame_or_window = frame;
12201 event.arg = frame;
12202 kbd_buffer_store_event (&event);
12204 event.kind = TOOL_BAR_EVENT;
12205 event.frame_or_window = frame;
12206 event.arg = key;
12207 event.modifiers = modifiers;
12208 kbd_buffer_store_event (&event);
12209 last_tool_bar_item = -1;
12214 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12215 tool-bar window-relative coordinates X/Y. Called from
12216 note_mouse_highlight. */
12218 static void
12219 note_tool_bar_highlight (struct frame *f, int x, int y)
12221 Lisp_Object window = f->tool_bar_window;
12222 struct window *w = XWINDOW (window);
12223 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12224 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12225 int hpos, vpos;
12226 struct glyph *glyph;
12227 struct glyph_row *row;
12228 int i;
12229 Lisp_Object enabled_p;
12230 int prop_idx;
12231 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12232 int mouse_down_p, rc;
12234 /* Function note_mouse_highlight is called with negative X/Y
12235 values when mouse moves outside of the frame. */
12236 if (x <= 0 || y <= 0)
12238 clear_mouse_face (hlinfo);
12239 return;
12242 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12243 if (rc < 0)
12245 /* Not on tool-bar item. */
12246 clear_mouse_face (hlinfo);
12247 return;
12249 else if (rc == 0)
12250 /* On same tool-bar item as before. */
12251 goto set_help_echo;
12253 clear_mouse_face (hlinfo);
12255 /* Mouse is down, but on different tool-bar item? */
12256 mouse_down_p = (dpyinfo->grabbed
12257 && f == last_mouse_frame
12258 && FRAME_LIVE_P (f));
12259 if (mouse_down_p
12260 && last_tool_bar_item != prop_idx)
12261 return;
12263 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12265 /* If tool-bar item is not enabled, don't highlight it. */
12266 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12267 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12269 /* Compute the x-position of the glyph. In front and past the
12270 image is a space. We include this in the highlighted area. */
12271 row = MATRIX_ROW (w->current_matrix, vpos);
12272 for (i = x = 0; i < hpos; ++i)
12273 x += row->glyphs[TEXT_AREA][i].pixel_width;
12275 /* Record this as the current active region. */
12276 hlinfo->mouse_face_beg_col = hpos;
12277 hlinfo->mouse_face_beg_row = vpos;
12278 hlinfo->mouse_face_beg_x = x;
12279 hlinfo->mouse_face_beg_y = row->y;
12280 hlinfo->mouse_face_past_end = 0;
12282 hlinfo->mouse_face_end_col = hpos + 1;
12283 hlinfo->mouse_face_end_row = vpos;
12284 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12285 hlinfo->mouse_face_end_y = row->y;
12286 hlinfo->mouse_face_window = window;
12287 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12289 /* Display it as active. */
12290 show_mouse_face (hlinfo, draw);
12293 set_help_echo:
12295 /* Set help_echo_string to a help string to display for this tool-bar item.
12296 XTread_socket does the rest. */
12297 help_echo_object = help_echo_window = Qnil;
12298 help_echo_pos = -1;
12299 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12300 if (NILP (help_echo_string))
12301 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12304 #endif /* HAVE_WINDOW_SYSTEM */
12308 /************************************************************************
12309 Horizontal scrolling
12310 ************************************************************************/
12312 static int hscroll_window_tree (Lisp_Object);
12313 static int hscroll_windows (Lisp_Object);
12315 /* For all leaf windows in the window tree rooted at WINDOW, set their
12316 hscroll value so that PT is (i) visible in the window, and (ii) so
12317 that it is not within a certain margin at the window's left and
12318 right border. Value is non-zero if any window's hscroll has been
12319 changed. */
12321 static int
12322 hscroll_window_tree (Lisp_Object window)
12324 int hscrolled_p = 0;
12325 int hscroll_relative_p = FLOATP (Vhscroll_step);
12326 int hscroll_step_abs = 0;
12327 double hscroll_step_rel = 0;
12329 if (hscroll_relative_p)
12331 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12332 if (hscroll_step_rel < 0)
12334 hscroll_relative_p = 0;
12335 hscroll_step_abs = 0;
12338 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12340 hscroll_step_abs = XINT (Vhscroll_step);
12341 if (hscroll_step_abs < 0)
12342 hscroll_step_abs = 0;
12344 else
12345 hscroll_step_abs = 0;
12347 while (WINDOWP (window))
12349 struct window *w = XWINDOW (window);
12351 if (WINDOWP (w->contents))
12352 hscrolled_p |= hscroll_window_tree (w->contents);
12353 else if (w->cursor.vpos >= 0)
12355 int h_margin;
12356 int text_area_width;
12357 struct glyph_row *current_cursor_row
12358 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12359 struct glyph_row *desired_cursor_row
12360 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12361 struct glyph_row *cursor_row
12362 = (desired_cursor_row->enabled_p
12363 ? desired_cursor_row
12364 : current_cursor_row);
12365 int row_r2l_p = cursor_row->reversed_p;
12367 text_area_width = window_box_width (w, TEXT_AREA);
12369 /* Scroll when cursor is inside this scroll margin. */
12370 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12372 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12373 /* For left-to-right rows, hscroll when cursor is either
12374 (i) inside the right hscroll margin, or (ii) if it is
12375 inside the left margin and the window is already
12376 hscrolled. */
12377 && ((!row_r2l_p
12378 && ((w->hscroll
12379 && w->cursor.x <= h_margin)
12380 || (cursor_row->enabled_p
12381 && cursor_row->truncated_on_right_p
12382 && (w->cursor.x >= text_area_width - h_margin))))
12383 /* For right-to-left rows, the logic is similar,
12384 except that rules for scrolling to left and right
12385 are reversed. E.g., if cursor.x <= h_margin, we
12386 need to hscroll "to the right" unconditionally,
12387 and that will scroll the screen to the left so as
12388 to reveal the next portion of the row. */
12389 || (row_r2l_p
12390 && ((cursor_row->enabled_p
12391 /* FIXME: It is confusing to set the
12392 truncated_on_right_p flag when R2L rows
12393 are actually truncated on the left. */
12394 && cursor_row->truncated_on_right_p
12395 && w->cursor.x <= h_margin)
12396 || (w->hscroll
12397 && (w->cursor.x >= text_area_width - h_margin))))))
12399 struct it it;
12400 ptrdiff_t hscroll;
12401 struct buffer *saved_current_buffer;
12402 ptrdiff_t pt;
12403 int wanted_x;
12405 /* Find point in a display of infinite width. */
12406 saved_current_buffer = current_buffer;
12407 current_buffer = XBUFFER (w->contents);
12409 if (w == XWINDOW (selected_window))
12410 pt = PT;
12411 else
12412 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12414 /* Move iterator to pt starting at cursor_row->start in
12415 a line with infinite width. */
12416 init_to_row_start (&it, w, cursor_row);
12417 it.last_visible_x = INFINITY;
12418 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12419 current_buffer = saved_current_buffer;
12421 /* Position cursor in window. */
12422 if (!hscroll_relative_p && hscroll_step_abs == 0)
12423 hscroll = max (0, (it.current_x
12424 - (ITERATOR_AT_END_OF_LINE_P (&it)
12425 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12426 : (text_area_width / 2))))
12427 / FRAME_COLUMN_WIDTH (it.f);
12428 else if ((!row_r2l_p
12429 && w->cursor.x >= text_area_width - h_margin)
12430 || (row_r2l_p && w->cursor.x <= h_margin))
12432 if (hscroll_relative_p)
12433 wanted_x = text_area_width * (1 - hscroll_step_rel)
12434 - h_margin;
12435 else
12436 wanted_x = text_area_width
12437 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12438 - h_margin;
12439 hscroll
12440 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12442 else
12444 if (hscroll_relative_p)
12445 wanted_x = text_area_width * hscroll_step_rel
12446 + h_margin;
12447 else
12448 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12449 + h_margin;
12450 hscroll
12451 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12453 hscroll = max (hscroll, w->min_hscroll);
12455 /* Don't prevent redisplay optimizations if hscroll
12456 hasn't changed, as it will unnecessarily slow down
12457 redisplay. */
12458 if (w->hscroll != hscroll)
12460 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12461 w->hscroll = hscroll;
12462 hscrolled_p = 1;
12467 window = w->next;
12470 /* Value is non-zero if hscroll of any leaf window has been changed. */
12471 return hscrolled_p;
12475 /* Set hscroll so that cursor is visible and not inside horizontal
12476 scroll margins for all windows in the tree rooted at WINDOW. See
12477 also hscroll_window_tree above. Value is non-zero if any window's
12478 hscroll has been changed. If it has, desired matrices on the frame
12479 of WINDOW are cleared. */
12481 static int
12482 hscroll_windows (Lisp_Object window)
12484 int hscrolled_p = hscroll_window_tree (window);
12485 if (hscrolled_p)
12486 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12487 return hscrolled_p;
12492 /************************************************************************
12493 Redisplay
12494 ************************************************************************/
12496 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12497 to a non-zero value. This is sometimes handy to have in a debugger
12498 session. */
12500 #ifdef GLYPH_DEBUG
12502 /* First and last unchanged row for try_window_id. */
12504 static int debug_first_unchanged_at_end_vpos;
12505 static int debug_last_unchanged_at_beg_vpos;
12507 /* Delta vpos and y. */
12509 static int debug_dvpos, debug_dy;
12511 /* Delta in characters and bytes for try_window_id. */
12513 static ptrdiff_t debug_delta, debug_delta_bytes;
12515 /* Values of window_end_pos and window_end_vpos at the end of
12516 try_window_id. */
12518 static ptrdiff_t debug_end_vpos;
12520 /* Append a string to W->desired_matrix->method. FMT is a printf
12521 format string. If trace_redisplay_p is non-zero also printf the
12522 resulting string to stderr. */
12524 static void debug_method_add (struct window *, char const *, ...)
12525 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12527 static void
12528 debug_method_add (struct window *w, char const *fmt, ...)
12530 char *method = w->desired_matrix->method;
12531 int len = strlen (method);
12532 int size = sizeof w->desired_matrix->method;
12533 int remaining = size - len - 1;
12534 va_list ap;
12536 if (len && remaining)
12538 method[len] = '|';
12539 --remaining, ++len;
12542 va_start (ap, fmt);
12543 vsnprintf (method + len, remaining + 1, fmt, ap);
12544 va_end (ap);
12546 if (trace_redisplay_p)
12547 fprintf (stderr, "%p (%s): %s\n",
12549 ((BUFFERP (w->contents)
12550 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12551 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12552 : "no buffer"),
12553 method + len);
12556 #endif /* GLYPH_DEBUG */
12559 /* Value is non-zero if all changes in window W, which displays
12560 current_buffer, are in the text between START and END. START is a
12561 buffer position, END is given as a distance from Z. Used in
12562 redisplay_internal for display optimization. */
12564 static int
12565 text_outside_line_unchanged_p (struct window *w,
12566 ptrdiff_t start, ptrdiff_t end)
12568 int unchanged_p = 1;
12570 /* If text or overlays have changed, see where. */
12571 if (window_outdated (w))
12573 /* Gap in the line? */
12574 if (GPT < start || Z - GPT < end)
12575 unchanged_p = 0;
12577 /* Changes start in front of the line, or end after it? */
12578 if (unchanged_p
12579 && (BEG_UNCHANGED < start - 1
12580 || END_UNCHANGED < end))
12581 unchanged_p = 0;
12583 /* If selective display, can't optimize if changes start at the
12584 beginning of the line. */
12585 if (unchanged_p
12586 && INTEGERP (BVAR (current_buffer, selective_display))
12587 && XINT (BVAR (current_buffer, selective_display)) > 0
12588 && (BEG_UNCHANGED < start || GPT <= start))
12589 unchanged_p = 0;
12591 /* If there are overlays at the start or end of the line, these
12592 may have overlay strings with newlines in them. A change at
12593 START, for instance, may actually concern the display of such
12594 overlay strings as well, and they are displayed on different
12595 lines. So, quickly rule out this case. (For the future, it
12596 might be desirable to implement something more telling than
12597 just BEG/END_UNCHANGED.) */
12598 if (unchanged_p)
12600 if (BEG + BEG_UNCHANGED == start
12601 && overlay_touches_p (start))
12602 unchanged_p = 0;
12603 if (END_UNCHANGED == end
12604 && overlay_touches_p (Z - end))
12605 unchanged_p = 0;
12608 /* Under bidi reordering, adding or deleting a character in the
12609 beginning of a paragraph, before the first strong directional
12610 character, can change the base direction of the paragraph (unless
12611 the buffer specifies a fixed paragraph direction), which will
12612 require to redisplay the whole paragraph. It might be worthwhile
12613 to find the paragraph limits and widen the range of redisplayed
12614 lines to that, but for now just give up this optimization. */
12615 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12616 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12617 unchanged_p = 0;
12620 return unchanged_p;
12624 /* Do a frame update, taking possible shortcuts into account. This is
12625 the main external entry point for redisplay.
12627 If the last redisplay displayed an echo area message and that message
12628 is no longer requested, we clear the echo area or bring back the
12629 mini-buffer if that is in use. */
12631 void
12632 redisplay (void)
12634 redisplay_internal ();
12638 static Lisp_Object
12639 overlay_arrow_string_or_property (Lisp_Object var)
12641 Lisp_Object val;
12643 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12644 return val;
12646 return Voverlay_arrow_string;
12649 /* Return 1 if there are any overlay-arrows in current_buffer. */
12650 static int
12651 overlay_arrow_in_current_buffer_p (void)
12653 Lisp_Object vlist;
12655 for (vlist = Voverlay_arrow_variable_list;
12656 CONSP (vlist);
12657 vlist = XCDR (vlist))
12659 Lisp_Object var = XCAR (vlist);
12660 Lisp_Object val;
12662 if (!SYMBOLP (var))
12663 continue;
12664 val = find_symbol_value (var);
12665 if (MARKERP (val)
12666 && current_buffer == XMARKER (val)->buffer)
12667 return 1;
12669 return 0;
12673 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12674 has changed. */
12676 static int
12677 overlay_arrows_changed_p (void)
12679 Lisp_Object vlist;
12681 for (vlist = Voverlay_arrow_variable_list;
12682 CONSP (vlist);
12683 vlist = XCDR (vlist))
12685 Lisp_Object var = XCAR (vlist);
12686 Lisp_Object val, pstr;
12688 if (!SYMBOLP (var))
12689 continue;
12690 val = find_symbol_value (var);
12691 if (!MARKERP (val))
12692 continue;
12693 if (! EQ (COERCE_MARKER (val),
12694 Fget (var, Qlast_arrow_position))
12695 || ! (pstr = overlay_arrow_string_or_property (var),
12696 EQ (pstr, Fget (var, Qlast_arrow_string))))
12697 return 1;
12699 return 0;
12702 /* Mark overlay arrows to be updated on next redisplay. */
12704 static void
12705 update_overlay_arrows (int up_to_date)
12707 Lisp_Object vlist;
12709 for (vlist = Voverlay_arrow_variable_list;
12710 CONSP (vlist);
12711 vlist = XCDR (vlist))
12713 Lisp_Object var = XCAR (vlist);
12715 if (!SYMBOLP (var))
12716 continue;
12718 if (up_to_date > 0)
12720 Lisp_Object val = find_symbol_value (var);
12721 Fput (var, Qlast_arrow_position,
12722 COERCE_MARKER (val));
12723 Fput (var, Qlast_arrow_string,
12724 overlay_arrow_string_or_property (var));
12726 else if (up_to_date < 0
12727 || !NILP (Fget (var, Qlast_arrow_position)))
12729 Fput (var, Qlast_arrow_position, Qt);
12730 Fput (var, Qlast_arrow_string, Qt);
12736 /* Return overlay arrow string to display at row.
12737 Return integer (bitmap number) for arrow bitmap in left fringe.
12738 Return nil if no overlay arrow. */
12740 static Lisp_Object
12741 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12743 Lisp_Object vlist;
12745 for (vlist = Voverlay_arrow_variable_list;
12746 CONSP (vlist);
12747 vlist = XCDR (vlist))
12749 Lisp_Object var = XCAR (vlist);
12750 Lisp_Object val;
12752 if (!SYMBOLP (var))
12753 continue;
12755 val = find_symbol_value (var);
12757 if (MARKERP (val)
12758 && current_buffer == XMARKER (val)->buffer
12759 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12761 if (FRAME_WINDOW_P (it->f)
12762 /* FIXME: if ROW->reversed_p is set, this should test
12763 the right fringe, not the left one. */
12764 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12766 #ifdef HAVE_WINDOW_SYSTEM
12767 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12769 int fringe_bitmap;
12770 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12771 return make_number (fringe_bitmap);
12773 #endif
12774 return make_number (-1); /* Use default arrow bitmap. */
12776 return overlay_arrow_string_or_property (var);
12780 return Qnil;
12783 /* Return 1 if point moved out of or into a composition. Otherwise
12784 return 0. PREV_BUF and PREV_PT are the last point buffer and
12785 position. BUF and PT are the current point buffer and position. */
12787 static int
12788 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12789 struct buffer *buf, ptrdiff_t pt)
12791 ptrdiff_t start, end;
12792 Lisp_Object prop;
12793 Lisp_Object buffer;
12795 XSETBUFFER (buffer, buf);
12796 /* Check a composition at the last point if point moved within the
12797 same buffer. */
12798 if (prev_buf == buf)
12800 if (prev_pt == pt)
12801 /* Point didn't move. */
12802 return 0;
12804 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12805 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12806 && COMPOSITION_VALID_P (start, end, prop)
12807 && start < prev_pt && end > prev_pt)
12808 /* The last point was within the composition. Return 1 iff
12809 point moved out of the composition. */
12810 return (pt <= start || pt >= end);
12813 /* Check a composition at the current point. */
12814 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12815 && find_composition (pt, -1, &start, &end, &prop, buffer)
12816 && COMPOSITION_VALID_P (start, end, prop)
12817 && start < pt && end > pt);
12821 /* Reconsider the setting of B->clip_changed which is displayed
12822 in window W. */
12824 static void
12825 reconsider_clip_changes (struct window *w, struct buffer *b)
12827 if (b->clip_changed
12828 && w->window_end_valid
12829 && w->current_matrix->buffer == b
12830 && w->current_matrix->zv == BUF_ZV (b)
12831 && w->current_matrix->begv == BUF_BEGV (b))
12832 b->clip_changed = 0;
12834 /* If display wasn't paused, and W is not a tool bar window, see if
12835 point has been moved into or out of a composition. In that case,
12836 we set b->clip_changed to 1 to force updating the screen. If
12837 b->clip_changed has already been set to 1, we can skip this
12838 check. */
12839 if (!b->clip_changed && BUFFERP (w->contents) && w->window_end_valid)
12841 ptrdiff_t pt;
12843 if (w == XWINDOW (selected_window))
12844 pt = PT;
12845 else
12846 pt = marker_position (w->pointm);
12848 if ((w->current_matrix->buffer != XBUFFER (w->contents)
12849 || pt != w->last_point)
12850 && check_point_in_composition (w->current_matrix->buffer,
12851 w->last_point,
12852 XBUFFER (w->contents), pt))
12853 b->clip_changed = 1;
12858 #define STOP_POLLING \
12859 do { if (! polling_stopped_here) stop_polling (); \
12860 polling_stopped_here = 1; } while (0)
12862 #define RESUME_POLLING \
12863 do { if (polling_stopped_here) start_polling (); \
12864 polling_stopped_here = 0; } while (0)
12867 /* Perhaps in the future avoid recentering windows if it
12868 is not necessary; currently that causes some problems. */
12870 static void
12871 redisplay_internal (void)
12873 struct window *w = XWINDOW (selected_window);
12874 struct window *sw;
12875 struct frame *fr;
12876 int pending;
12877 int must_finish = 0;
12878 struct text_pos tlbufpos, tlendpos;
12879 int number_of_visible_frames;
12880 ptrdiff_t count, count1;
12881 struct frame *sf;
12882 int polling_stopped_here = 0;
12883 Lisp_Object tail, frame;
12885 /* Non-zero means redisplay has to consider all windows on all
12886 frames. Zero means, only selected_window is considered. */
12887 int consider_all_windows_p;
12889 /* Non-zero means redisplay has to redisplay the miniwindow. */
12890 int update_miniwindow_p = 0;
12892 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12894 /* No redisplay if running in batch mode or frame is not yet fully
12895 initialized, or redisplay is explicitly turned off by setting
12896 Vinhibit_redisplay. */
12897 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12898 || !NILP (Vinhibit_redisplay))
12899 return;
12901 /* Don't examine these until after testing Vinhibit_redisplay.
12902 When Emacs is shutting down, perhaps because its connection to
12903 X has dropped, we should not look at them at all. */
12904 fr = XFRAME (w->frame);
12905 sf = SELECTED_FRAME ();
12907 if (!fr->glyphs_initialized_p)
12908 return;
12910 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12911 if (popup_activated ())
12912 return;
12913 #endif
12915 /* I don't think this happens but let's be paranoid. */
12916 if (redisplaying_p)
12917 return;
12919 /* Record a function that clears redisplaying_p
12920 when we leave this function. */
12921 count = SPECPDL_INDEX ();
12922 record_unwind_protect (unwind_redisplay, selected_frame);
12923 redisplaying_p = 1;
12924 specbind (Qinhibit_free_realized_faces, Qnil);
12926 /* Record this function, so it appears on the profiler's backtraces. */
12927 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12929 FOR_EACH_FRAME (tail, frame)
12930 XFRAME (frame)->already_hscrolled_p = 0;
12932 retry:
12933 /* Remember the currently selected window. */
12934 sw = w;
12936 pending = 0;
12937 reconsider_clip_changes (w, current_buffer);
12938 last_escape_glyph_frame = NULL;
12939 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12940 last_glyphless_glyph_frame = NULL;
12941 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12943 /* If new fonts have been loaded that make a glyph matrix adjustment
12944 necessary, do it. */
12945 if (fonts_changed_p)
12947 adjust_glyphs (NULL);
12948 ++windows_or_buffers_changed;
12949 fonts_changed_p = 0;
12952 /* If face_change_count is non-zero, init_iterator will free all
12953 realized faces, which includes the faces referenced from current
12954 matrices. So, we can't reuse current matrices in this case. */
12955 if (face_change_count)
12956 ++windows_or_buffers_changed;
12958 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12959 && FRAME_TTY (sf)->previous_frame != sf)
12961 /* Since frames on a single ASCII terminal share the same
12962 display area, displaying a different frame means redisplay
12963 the whole thing. */
12964 windows_or_buffers_changed++;
12965 SET_FRAME_GARBAGED (sf);
12966 #ifndef DOS_NT
12967 set_tty_color_mode (FRAME_TTY (sf), sf);
12968 #endif
12969 FRAME_TTY (sf)->previous_frame = sf;
12972 /* Set the visible flags for all frames. Do this before checking for
12973 resized or garbaged frames; they want to know if their frames are
12974 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12975 number_of_visible_frames = 0;
12977 FOR_EACH_FRAME (tail, frame)
12979 struct frame *f = XFRAME (frame);
12981 if (FRAME_VISIBLE_P (f))
12982 ++number_of_visible_frames;
12983 clear_desired_matrices (f);
12986 /* Notice any pending interrupt request to change frame size. */
12987 do_pending_window_change (1);
12989 /* do_pending_window_change could change the selected_window due to
12990 frame resizing which makes the selected window too small. */
12991 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12993 sw = w;
12994 reconsider_clip_changes (w, current_buffer);
12997 /* Clear frames marked as garbaged. */
12998 clear_garbaged_frames ();
13000 /* Build menubar and tool-bar items. */
13001 if (NILP (Vmemory_full))
13002 prepare_menu_bars ();
13004 if (windows_or_buffers_changed)
13005 update_mode_lines++;
13007 /* Detect case that we need to write or remove a star in the mode line. */
13008 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13010 w->update_mode_line = 1;
13011 if (buffer_shared_and_changed ())
13012 update_mode_lines++;
13015 /* Avoid invocation of point motion hooks by `current_column' below. */
13016 count1 = SPECPDL_INDEX ();
13017 specbind (Qinhibit_point_motion_hooks, Qt);
13019 if (mode_line_update_needed (w))
13020 w->update_mode_line = 1;
13022 unbind_to (count1, Qnil);
13024 consider_all_windows_p = (update_mode_lines
13025 || buffer_shared_and_changed ()
13026 || cursor_type_changed);
13028 /* If specs for an arrow have changed, do thorough redisplay
13029 to ensure we remove any arrow that should no longer exist. */
13030 if (overlay_arrows_changed_p ())
13031 consider_all_windows_p = windows_or_buffers_changed = 1;
13033 /* Normally the message* functions will have already displayed and
13034 updated the echo area, but the frame may have been trashed, or
13035 the update may have been preempted, so display the echo area
13036 again here. Checking message_cleared_p captures the case that
13037 the echo area should be cleared. */
13038 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13039 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13040 || (message_cleared_p
13041 && minibuf_level == 0
13042 /* If the mini-window is currently selected, this means the
13043 echo-area doesn't show through. */
13044 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13046 int window_height_changed_p = echo_area_display (0);
13048 if (message_cleared_p)
13049 update_miniwindow_p = 1;
13051 must_finish = 1;
13053 /* If we don't display the current message, don't clear the
13054 message_cleared_p flag, because, if we did, we wouldn't clear
13055 the echo area in the next redisplay which doesn't preserve
13056 the echo area. */
13057 if (!display_last_displayed_message_p)
13058 message_cleared_p = 0;
13060 if (fonts_changed_p)
13061 goto retry;
13062 else if (window_height_changed_p)
13064 consider_all_windows_p = 1;
13065 ++update_mode_lines;
13066 ++windows_or_buffers_changed;
13068 /* If window configuration was changed, frames may have been
13069 marked garbaged. Clear them or we will experience
13070 surprises wrt scrolling. */
13071 clear_garbaged_frames ();
13074 else if (EQ (selected_window, minibuf_window)
13075 && (current_buffer->clip_changed || window_outdated (w))
13076 && resize_mini_window (w, 0))
13078 /* Resized active mini-window to fit the size of what it is
13079 showing if its contents might have changed. */
13080 must_finish = 1;
13081 /* FIXME: this causes all frames to be updated, which seems unnecessary
13082 since only the current frame needs to be considered. This function
13083 needs to be rewritten with two variables, consider_all_windows and
13084 consider_all_frames. */
13085 consider_all_windows_p = 1;
13086 ++windows_or_buffers_changed;
13087 ++update_mode_lines;
13089 /* If window configuration was changed, frames may have been
13090 marked garbaged. Clear them or we will experience
13091 surprises wrt scrolling. */
13092 clear_garbaged_frames ();
13095 /* If showing the region, and mark has changed, we must redisplay
13096 the whole window. The assignment to this_line_start_pos prevents
13097 the optimization directly below this if-statement. */
13098 if (((!NILP (Vtransient_mark_mode)
13099 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13100 != (w->region_showing > 0))
13101 || (w->region_showing
13102 && w->region_showing
13103 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13104 CHARPOS (this_line_start_pos) = 0;
13106 /* Optimize the case that only the line containing the cursor in the
13107 selected window has changed. Variables starting with this_ are
13108 set in display_line and record information about the line
13109 containing the cursor. */
13110 tlbufpos = this_line_start_pos;
13111 tlendpos = this_line_end_pos;
13112 if (!consider_all_windows_p
13113 && CHARPOS (tlbufpos) > 0
13114 && !w->update_mode_line
13115 && !current_buffer->clip_changed
13116 && !current_buffer->prevent_redisplay_optimizations_p
13117 && FRAME_VISIBLE_P (XFRAME (w->frame))
13118 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13119 /* Make sure recorded data applies to current buffer, etc. */
13120 && this_line_buffer == current_buffer
13121 && current_buffer == XBUFFER (w->contents)
13122 && !w->force_start
13123 && !w->optional_new_start
13124 /* Point must be on the line that we have info recorded about. */
13125 && PT >= CHARPOS (tlbufpos)
13126 && PT <= Z - CHARPOS (tlendpos)
13127 /* All text outside that line, including its final newline,
13128 must be unchanged. */
13129 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13130 CHARPOS (tlendpos)))
13132 if (CHARPOS (tlbufpos) > BEGV
13133 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13134 && (CHARPOS (tlbufpos) == ZV
13135 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13136 /* Former continuation line has disappeared by becoming empty. */
13137 goto cancel;
13138 else if (window_outdated (w) || MINI_WINDOW_P (w))
13140 /* We have to handle the case of continuation around a
13141 wide-column character (see the comment in indent.c around
13142 line 1340).
13144 For instance, in the following case:
13146 -------- Insert --------
13147 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13148 J_I_ ==> J_I_ `^^' are cursors.
13149 ^^ ^^
13150 -------- --------
13152 As we have to redraw the line above, we cannot use this
13153 optimization. */
13155 struct it it;
13156 int line_height_before = this_line_pixel_height;
13158 /* Note that start_display will handle the case that the
13159 line starting at tlbufpos is a continuation line. */
13160 start_display (&it, w, tlbufpos);
13162 /* Implementation note: It this still necessary? */
13163 if (it.current_x != this_line_start_x)
13164 goto cancel;
13166 TRACE ((stderr, "trying display optimization 1\n"));
13167 w->cursor.vpos = -1;
13168 overlay_arrow_seen = 0;
13169 it.vpos = this_line_vpos;
13170 it.current_y = this_line_y;
13171 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13172 display_line (&it);
13174 /* If line contains point, is not continued,
13175 and ends at same distance from eob as before, we win. */
13176 if (w->cursor.vpos >= 0
13177 /* Line is not continued, otherwise this_line_start_pos
13178 would have been set to 0 in display_line. */
13179 && CHARPOS (this_line_start_pos)
13180 /* Line ends as before. */
13181 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13182 /* Line has same height as before. Otherwise other lines
13183 would have to be shifted up or down. */
13184 && this_line_pixel_height == line_height_before)
13186 /* If this is not the window's last line, we must adjust
13187 the charstarts of the lines below. */
13188 if (it.current_y < it.last_visible_y)
13190 struct glyph_row *row
13191 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13192 ptrdiff_t delta, delta_bytes;
13194 /* We used to distinguish between two cases here,
13195 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13196 when the line ends in a newline or the end of the
13197 buffer's accessible portion. But both cases did
13198 the same, so they were collapsed. */
13199 delta = (Z
13200 - CHARPOS (tlendpos)
13201 - MATRIX_ROW_START_CHARPOS (row));
13202 delta_bytes = (Z_BYTE
13203 - BYTEPOS (tlendpos)
13204 - MATRIX_ROW_START_BYTEPOS (row));
13206 increment_matrix_positions (w->current_matrix,
13207 this_line_vpos + 1,
13208 w->current_matrix->nrows,
13209 delta, delta_bytes);
13212 /* If this row displays text now but previously didn't,
13213 or vice versa, w->window_end_vpos may have to be
13214 adjusted. */
13215 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13217 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13218 wset_window_end_vpos (w, make_number (this_line_vpos));
13220 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13221 && this_line_vpos > 0)
13222 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13223 w->window_end_valid = 0;
13225 /* Update hint: No need to try to scroll in update_window. */
13226 w->desired_matrix->no_scrolling_p = 1;
13228 #ifdef GLYPH_DEBUG
13229 *w->desired_matrix->method = 0;
13230 debug_method_add (w, "optimization 1");
13231 #endif
13232 #ifdef HAVE_WINDOW_SYSTEM
13233 update_window_fringes (w, 0);
13234 #endif
13235 goto update;
13237 else
13238 goto cancel;
13240 else if (/* Cursor position hasn't changed. */
13241 PT == w->last_point
13242 /* Make sure the cursor was last displayed
13243 in this window. Otherwise we have to reposition it. */
13244 && 0 <= w->cursor.vpos
13245 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13247 if (!must_finish)
13249 do_pending_window_change (1);
13250 /* If selected_window changed, redisplay again. */
13251 if (WINDOWP (selected_window)
13252 && (w = XWINDOW (selected_window)) != sw)
13253 goto retry;
13255 /* We used to always goto end_of_redisplay here, but this
13256 isn't enough if we have a blinking cursor. */
13257 if (w->cursor_off_p == w->last_cursor_off_p)
13258 goto end_of_redisplay;
13260 goto update;
13262 /* If highlighting the region, or if the cursor is in the echo area,
13263 then we can't just move the cursor. */
13264 else if (! (!NILP (Vtransient_mark_mode)
13265 && !NILP (BVAR (current_buffer, mark_active)))
13266 && (EQ (selected_window,
13267 BVAR (current_buffer, last_selected_window))
13268 || highlight_nonselected_windows)
13269 && !w->region_showing
13270 && NILP (Vshow_trailing_whitespace)
13271 && !cursor_in_echo_area)
13273 struct it it;
13274 struct glyph_row *row;
13276 /* Skip from tlbufpos to PT and see where it is. Note that
13277 PT may be in invisible text. If so, we will end at the
13278 next visible position. */
13279 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13280 NULL, DEFAULT_FACE_ID);
13281 it.current_x = this_line_start_x;
13282 it.current_y = this_line_y;
13283 it.vpos = this_line_vpos;
13285 /* The call to move_it_to stops in front of PT, but
13286 moves over before-strings. */
13287 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13289 if (it.vpos == this_line_vpos
13290 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13291 row->enabled_p))
13293 eassert (this_line_vpos == it.vpos);
13294 eassert (this_line_y == it.current_y);
13295 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13296 #ifdef GLYPH_DEBUG
13297 *w->desired_matrix->method = 0;
13298 debug_method_add (w, "optimization 3");
13299 #endif
13300 goto update;
13302 else
13303 goto cancel;
13306 cancel:
13307 /* Text changed drastically or point moved off of line. */
13308 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13311 CHARPOS (this_line_start_pos) = 0;
13312 consider_all_windows_p |= buffer_shared_and_changed ();
13313 ++clear_face_cache_count;
13314 #ifdef HAVE_WINDOW_SYSTEM
13315 ++clear_image_cache_count;
13316 #endif
13318 /* Build desired matrices, and update the display. If
13319 consider_all_windows_p is non-zero, do it for all windows on all
13320 frames. Otherwise do it for selected_window, only. */
13322 if (consider_all_windows_p)
13324 FOR_EACH_FRAME (tail, frame)
13325 XFRAME (frame)->updated_p = 0;
13327 FOR_EACH_FRAME (tail, frame)
13329 struct frame *f = XFRAME (frame);
13331 /* We don't have to do anything for unselected terminal
13332 frames. */
13333 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13334 && !EQ (FRAME_TTY (f)->top_frame, frame))
13335 continue;
13337 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13339 /* Mark all the scroll bars to be removed; we'll redeem
13340 the ones we want when we redisplay their windows. */
13341 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13342 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13344 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13345 redisplay_windows (FRAME_ROOT_WINDOW (f));
13347 /* The X error handler may have deleted that frame. */
13348 if (!FRAME_LIVE_P (f))
13349 continue;
13351 /* Any scroll bars which redisplay_windows should have
13352 nuked should now go away. */
13353 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13354 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13356 /* If fonts changed, display again. */
13357 /* ??? rms: I suspect it is a mistake to jump all the way
13358 back to retry here. It should just retry this frame. */
13359 if (fonts_changed_p)
13360 goto retry;
13362 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13364 /* See if we have to hscroll. */
13365 if (!f->already_hscrolled_p)
13367 f->already_hscrolled_p = 1;
13368 if (hscroll_windows (f->root_window))
13369 goto retry;
13372 /* Prevent various kinds of signals during display
13373 update. stdio is not robust about handling
13374 signals, which can cause an apparent I/O
13375 error. */
13376 if (interrupt_input)
13377 unrequest_sigio ();
13378 STOP_POLLING;
13380 /* Update the display. */
13381 set_window_update_flags (XWINDOW (f->root_window), 1);
13382 pending |= update_frame (f, 0, 0);
13383 f->updated_p = 1;
13388 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13390 if (!pending)
13392 /* Do the mark_window_display_accurate after all windows have
13393 been redisplayed because this call resets flags in buffers
13394 which are needed for proper redisplay. */
13395 FOR_EACH_FRAME (tail, frame)
13397 struct frame *f = XFRAME (frame);
13398 if (f->updated_p)
13400 mark_window_display_accurate (f->root_window, 1);
13401 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13402 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13407 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13409 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13410 struct frame *mini_frame;
13412 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13413 /* Use list_of_error, not Qerror, so that
13414 we catch only errors and don't run the debugger. */
13415 internal_condition_case_1 (redisplay_window_1, selected_window,
13416 list_of_error,
13417 redisplay_window_error);
13418 if (update_miniwindow_p)
13419 internal_condition_case_1 (redisplay_window_1, mini_window,
13420 list_of_error,
13421 redisplay_window_error);
13423 /* Compare desired and current matrices, perform output. */
13425 update:
13426 /* If fonts changed, display again. */
13427 if (fonts_changed_p)
13428 goto retry;
13430 /* Prevent various kinds of signals during display update.
13431 stdio is not robust about handling signals,
13432 which can cause an apparent I/O error. */
13433 if (interrupt_input)
13434 unrequest_sigio ();
13435 STOP_POLLING;
13437 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13439 if (hscroll_windows (selected_window))
13440 goto retry;
13442 XWINDOW (selected_window)->must_be_updated_p = 1;
13443 pending = update_frame (sf, 0, 0);
13446 /* We may have called echo_area_display at the top of this
13447 function. If the echo area is on another frame, that may
13448 have put text on a frame other than the selected one, so the
13449 above call to update_frame would not have caught it. Catch
13450 it here. */
13451 mini_window = FRAME_MINIBUF_WINDOW (sf);
13452 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13454 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13456 XWINDOW (mini_window)->must_be_updated_p = 1;
13457 pending |= update_frame (mini_frame, 0, 0);
13458 if (!pending && hscroll_windows (mini_window))
13459 goto retry;
13463 /* If display was paused because of pending input, make sure we do a
13464 thorough update the next time. */
13465 if (pending)
13467 /* Prevent the optimization at the beginning of
13468 redisplay_internal that tries a single-line update of the
13469 line containing the cursor in the selected window. */
13470 CHARPOS (this_line_start_pos) = 0;
13472 /* Let the overlay arrow be updated the next time. */
13473 update_overlay_arrows (0);
13475 /* If we pause after scrolling, some rows in the current
13476 matrices of some windows are not valid. */
13477 if (!WINDOW_FULL_WIDTH_P (w)
13478 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13479 update_mode_lines = 1;
13481 else
13483 if (!consider_all_windows_p)
13485 /* This has already been done above if
13486 consider_all_windows_p is set. */
13487 mark_window_display_accurate_1 (w, 1);
13489 /* Say overlay arrows are up to date. */
13490 update_overlay_arrows (1);
13492 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13493 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13496 update_mode_lines = 0;
13497 windows_or_buffers_changed = 0;
13498 cursor_type_changed = 0;
13501 /* Start SIGIO interrupts coming again. Having them off during the
13502 code above makes it less likely one will discard output, but not
13503 impossible, since there might be stuff in the system buffer here.
13504 But it is much hairier to try to do anything about that. */
13505 if (interrupt_input)
13506 request_sigio ();
13507 RESUME_POLLING;
13509 /* If a frame has become visible which was not before, redisplay
13510 again, so that we display it. Expose events for such a frame
13511 (which it gets when becoming visible) don't call the parts of
13512 redisplay constructing glyphs, so simply exposing a frame won't
13513 display anything in this case. So, we have to display these
13514 frames here explicitly. */
13515 if (!pending)
13517 int new_count = 0;
13519 FOR_EACH_FRAME (tail, frame)
13521 int this_is_visible = 0;
13523 if (XFRAME (frame)->visible)
13524 this_is_visible = 1;
13526 if (this_is_visible)
13527 new_count++;
13530 if (new_count != number_of_visible_frames)
13531 windows_or_buffers_changed++;
13534 /* Change frame size now if a change is pending. */
13535 do_pending_window_change (1);
13537 /* If we just did a pending size change, or have additional
13538 visible frames, or selected_window changed, redisplay again. */
13539 if ((windows_or_buffers_changed && !pending)
13540 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13541 goto retry;
13543 /* Clear the face and image caches.
13545 We used to do this only if consider_all_windows_p. But the cache
13546 needs to be cleared if a timer creates images in the current
13547 buffer (e.g. the test case in Bug#6230). */
13549 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13551 clear_face_cache (0);
13552 clear_face_cache_count = 0;
13555 #ifdef HAVE_WINDOW_SYSTEM
13556 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13558 clear_image_caches (Qnil);
13559 clear_image_cache_count = 0;
13561 #endif /* HAVE_WINDOW_SYSTEM */
13563 end_of_redisplay:
13564 unbind_to (count, Qnil);
13565 RESUME_POLLING;
13569 /* Redisplay, but leave alone any recent echo area message unless
13570 another message has been requested in its place.
13572 This is useful in situations where you need to redisplay but no
13573 user action has occurred, making it inappropriate for the message
13574 area to be cleared. See tracking_off and
13575 wait_reading_process_output for examples of these situations.
13577 FROM_WHERE is an integer saying from where this function was
13578 called. This is useful for debugging. */
13580 void
13581 redisplay_preserve_echo_area (int from_where)
13583 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13585 if (!NILP (echo_area_buffer[1]))
13587 /* We have a previously displayed message, but no current
13588 message. Redisplay the previous message. */
13589 display_last_displayed_message_p = 1;
13590 redisplay_internal ();
13591 display_last_displayed_message_p = 0;
13593 else
13594 redisplay_internal ();
13596 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13597 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13598 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13602 /* Function registered with record_unwind_protect in redisplay_internal.
13603 Clear redisplaying_p. Also select the previously selected frame. */
13605 static Lisp_Object
13606 unwind_redisplay (Lisp_Object old_frame)
13608 redisplaying_p = 0;
13609 return Qnil;
13613 /* Mark the display of leaf window W as accurate or inaccurate.
13614 If ACCURATE_P is non-zero mark display of W as accurate. If
13615 ACCURATE_P is zero, arrange for W to be redisplayed the next
13616 time redisplay_internal is called. */
13618 static void
13619 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13621 struct buffer *b = XBUFFER (w->contents);
13623 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13624 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13625 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13627 if (accurate_p)
13629 b->clip_changed = 0;
13630 b->prevent_redisplay_optimizations_p = 0;
13632 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13633 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13634 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13635 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13637 w->current_matrix->buffer = b;
13638 w->current_matrix->begv = BUF_BEGV (b);
13639 w->current_matrix->zv = BUF_ZV (b);
13641 w->last_cursor = w->cursor;
13642 w->last_cursor_off_p = w->cursor_off_p;
13644 if (w == XWINDOW (selected_window))
13645 w->last_point = BUF_PT (b);
13646 else
13647 w->last_point = marker_position (w->pointm);
13649 w->window_end_valid = 1;
13650 w->update_mode_line = 0;
13655 /* Mark the display of windows in the window tree rooted at WINDOW as
13656 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13657 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13658 be redisplayed the next time redisplay_internal is called. */
13660 void
13661 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13663 struct window *w;
13665 for (; !NILP (window); window = w->next)
13667 w = XWINDOW (window);
13668 if (WINDOWP (w->contents))
13669 mark_window_display_accurate (w->contents, accurate_p);
13670 else
13671 mark_window_display_accurate_1 (w, accurate_p);
13674 if (accurate_p)
13675 update_overlay_arrows (1);
13676 else
13677 /* Force a thorough redisplay the next time by setting
13678 last_arrow_position and last_arrow_string to t, which is
13679 unequal to any useful value of Voverlay_arrow_... */
13680 update_overlay_arrows (-1);
13684 /* Return value in display table DP (Lisp_Char_Table *) for character
13685 C. Since a display table doesn't have any parent, we don't have to
13686 follow parent. Do not call this function directly but use the
13687 macro DISP_CHAR_VECTOR. */
13689 Lisp_Object
13690 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13692 Lisp_Object val;
13694 if (ASCII_CHAR_P (c))
13696 val = dp->ascii;
13697 if (SUB_CHAR_TABLE_P (val))
13698 val = XSUB_CHAR_TABLE (val)->contents[c];
13700 else
13702 Lisp_Object table;
13704 XSETCHAR_TABLE (table, dp);
13705 val = char_table_ref (table, c);
13707 if (NILP (val))
13708 val = dp->defalt;
13709 return val;
13714 /***********************************************************************
13715 Window Redisplay
13716 ***********************************************************************/
13718 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13720 static void
13721 redisplay_windows (Lisp_Object window)
13723 while (!NILP (window))
13725 struct window *w = XWINDOW (window);
13727 if (WINDOWP (w->contents))
13728 redisplay_windows (w->contents);
13729 else if (BUFFERP (w->contents))
13731 displayed_buffer = XBUFFER (w->contents);
13732 /* Use list_of_error, not Qerror, so that
13733 we catch only errors and don't run the debugger. */
13734 internal_condition_case_1 (redisplay_window_0, window,
13735 list_of_error,
13736 redisplay_window_error);
13739 window = w->next;
13743 static Lisp_Object
13744 redisplay_window_error (Lisp_Object ignore)
13746 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13747 return Qnil;
13750 static Lisp_Object
13751 redisplay_window_0 (Lisp_Object window)
13753 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13754 redisplay_window (window, 0);
13755 return Qnil;
13758 static Lisp_Object
13759 redisplay_window_1 (Lisp_Object window)
13761 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13762 redisplay_window (window, 1);
13763 return Qnil;
13767 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13768 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13769 which positions recorded in ROW differ from current buffer
13770 positions.
13772 Return 0 if cursor is not on this row, 1 otherwise. */
13774 static int
13775 set_cursor_from_row (struct window *w, struct glyph_row *row,
13776 struct glyph_matrix *matrix,
13777 ptrdiff_t delta, ptrdiff_t delta_bytes,
13778 int dy, int dvpos)
13780 struct glyph *glyph = row->glyphs[TEXT_AREA];
13781 struct glyph *end = glyph + row->used[TEXT_AREA];
13782 struct glyph *cursor = NULL;
13783 /* The last known character position in row. */
13784 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13785 int x = row->x;
13786 ptrdiff_t pt_old = PT - delta;
13787 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13788 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13789 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13790 /* A glyph beyond the edge of TEXT_AREA which we should never
13791 touch. */
13792 struct glyph *glyphs_end = end;
13793 /* Non-zero means we've found a match for cursor position, but that
13794 glyph has the avoid_cursor_p flag set. */
13795 int match_with_avoid_cursor = 0;
13796 /* Non-zero means we've seen at least one glyph that came from a
13797 display string. */
13798 int string_seen = 0;
13799 /* Largest and smallest buffer positions seen so far during scan of
13800 glyph row. */
13801 ptrdiff_t bpos_max = pos_before;
13802 ptrdiff_t bpos_min = pos_after;
13803 /* Last buffer position covered by an overlay string with an integer
13804 `cursor' property. */
13805 ptrdiff_t bpos_covered = 0;
13806 /* Non-zero means the display string on which to display the cursor
13807 comes from a text property, not from an overlay. */
13808 int string_from_text_prop = 0;
13810 /* Don't even try doing anything if called for a mode-line or
13811 header-line row, since the rest of the code isn't prepared to
13812 deal with such calamities. */
13813 eassert (!row->mode_line_p);
13814 if (row->mode_line_p)
13815 return 0;
13817 /* Skip over glyphs not having an object at the start and the end of
13818 the row. These are special glyphs like truncation marks on
13819 terminal frames. */
13820 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13822 if (!row->reversed_p)
13824 while (glyph < end
13825 && INTEGERP (glyph->object)
13826 && glyph->charpos < 0)
13828 x += glyph->pixel_width;
13829 ++glyph;
13831 while (end > glyph
13832 && INTEGERP ((end - 1)->object)
13833 /* CHARPOS is zero for blanks and stretch glyphs
13834 inserted by extend_face_to_end_of_line. */
13835 && (end - 1)->charpos <= 0)
13836 --end;
13837 glyph_before = glyph - 1;
13838 glyph_after = end;
13840 else
13842 struct glyph *g;
13844 /* If the glyph row is reversed, we need to process it from back
13845 to front, so swap the edge pointers. */
13846 glyphs_end = end = glyph - 1;
13847 glyph += row->used[TEXT_AREA] - 1;
13849 while (glyph > end + 1
13850 && INTEGERP (glyph->object)
13851 && glyph->charpos < 0)
13853 --glyph;
13854 x -= glyph->pixel_width;
13856 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13857 --glyph;
13858 /* By default, in reversed rows we put the cursor on the
13859 rightmost (first in the reading order) glyph. */
13860 for (g = end + 1; g < glyph; g++)
13861 x += g->pixel_width;
13862 while (end < glyph
13863 && INTEGERP ((end + 1)->object)
13864 && (end + 1)->charpos <= 0)
13865 ++end;
13866 glyph_before = glyph + 1;
13867 glyph_after = end;
13870 else if (row->reversed_p)
13872 /* In R2L rows that don't display text, put the cursor on the
13873 rightmost glyph. Case in point: an empty last line that is
13874 part of an R2L paragraph. */
13875 cursor = end - 1;
13876 /* Avoid placing the cursor on the last glyph of the row, where
13877 on terminal frames we hold the vertical border between
13878 adjacent windows. */
13879 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13880 && !WINDOW_RIGHTMOST_P (w)
13881 && cursor == row->glyphs[LAST_AREA] - 1)
13882 cursor--;
13883 x = -1; /* will be computed below, at label compute_x */
13886 /* Step 1: Try to find the glyph whose character position
13887 corresponds to point. If that's not possible, find 2 glyphs
13888 whose character positions are the closest to point, one before
13889 point, the other after it. */
13890 if (!row->reversed_p)
13891 while (/* not marched to end of glyph row */
13892 glyph < end
13893 /* glyph was not inserted by redisplay for internal purposes */
13894 && !INTEGERP (glyph->object))
13896 if (BUFFERP (glyph->object))
13898 ptrdiff_t dpos = glyph->charpos - pt_old;
13900 if (glyph->charpos > bpos_max)
13901 bpos_max = glyph->charpos;
13902 if (glyph->charpos < bpos_min)
13903 bpos_min = glyph->charpos;
13904 if (!glyph->avoid_cursor_p)
13906 /* If we hit point, we've found the glyph on which to
13907 display the cursor. */
13908 if (dpos == 0)
13910 match_with_avoid_cursor = 0;
13911 break;
13913 /* See if we've found a better approximation to
13914 POS_BEFORE or to POS_AFTER. */
13915 if (0 > dpos && dpos > pos_before - pt_old)
13917 pos_before = glyph->charpos;
13918 glyph_before = glyph;
13920 else if (0 < dpos && dpos < pos_after - pt_old)
13922 pos_after = glyph->charpos;
13923 glyph_after = glyph;
13926 else if (dpos == 0)
13927 match_with_avoid_cursor = 1;
13929 else if (STRINGP (glyph->object))
13931 Lisp_Object chprop;
13932 ptrdiff_t glyph_pos = glyph->charpos;
13934 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13935 glyph->object);
13936 if (!NILP (chprop))
13938 /* If the string came from a `display' text property,
13939 look up the buffer position of that property and
13940 use that position to update bpos_max, as if we
13941 actually saw such a position in one of the row's
13942 glyphs. This helps with supporting integer values
13943 of `cursor' property on the display string in
13944 situations where most or all of the row's buffer
13945 text is completely covered by display properties,
13946 so that no glyph with valid buffer positions is
13947 ever seen in the row. */
13948 ptrdiff_t prop_pos =
13949 string_buffer_position_lim (glyph->object, pos_before,
13950 pos_after, 0);
13952 if (prop_pos >= pos_before)
13953 bpos_max = prop_pos - 1;
13955 if (INTEGERP (chprop))
13957 bpos_covered = bpos_max + XINT (chprop);
13958 /* If the `cursor' property covers buffer positions up
13959 to and including point, we should display cursor on
13960 this glyph. Note that, if a `cursor' property on one
13961 of the string's characters has an integer value, we
13962 will break out of the loop below _before_ we get to
13963 the position match above. IOW, integer values of
13964 the `cursor' property override the "exact match for
13965 point" strategy of positioning the cursor. */
13966 /* Implementation note: bpos_max == pt_old when, e.g.,
13967 we are in an empty line, where bpos_max is set to
13968 MATRIX_ROW_START_CHARPOS, see above. */
13969 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13971 cursor = glyph;
13972 break;
13976 string_seen = 1;
13978 x += glyph->pixel_width;
13979 ++glyph;
13981 else if (glyph > end) /* row is reversed */
13982 while (!INTEGERP (glyph->object))
13984 if (BUFFERP (glyph->object))
13986 ptrdiff_t dpos = glyph->charpos - pt_old;
13988 if (glyph->charpos > bpos_max)
13989 bpos_max = glyph->charpos;
13990 if (glyph->charpos < bpos_min)
13991 bpos_min = glyph->charpos;
13992 if (!glyph->avoid_cursor_p)
13994 if (dpos == 0)
13996 match_with_avoid_cursor = 0;
13997 break;
13999 if (0 > dpos && dpos > pos_before - pt_old)
14001 pos_before = glyph->charpos;
14002 glyph_before = glyph;
14004 else if (0 < dpos && dpos < pos_after - pt_old)
14006 pos_after = glyph->charpos;
14007 glyph_after = glyph;
14010 else if (dpos == 0)
14011 match_with_avoid_cursor = 1;
14013 else if (STRINGP (glyph->object))
14015 Lisp_Object chprop;
14016 ptrdiff_t glyph_pos = glyph->charpos;
14018 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14019 glyph->object);
14020 if (!NILP (chprop))
14022 ptrdiff_t prop_pos =
14023 string_buffer_position_lim (glyph->object, pos_before,
14024 pos_after, 0);
14026 if (prop_pos >= pos_before)
14027 bpos_max = prop_pos - 1;
14029 if (INTEGERP (chprop))
14031 bpos_covered = bpos_max + XINT (chprop);
14032 /* If the `cursor' property covers buffer positions up
14033 to and including point, we should display cursor on
14034 this glyph. */
14035 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14037 cursor = glyph;
14038 break;
14041 string_seen = 1;
14043 --glyph;
14044 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14046 x--; /* can't use any pixel_width */
14047 break;
14049 x -= glyph->pixel_width;
14052 /* Step 2: If we didn't find an exact match for point, we need to
14053 look for a proper place to put the cursor among glyphs between
14054 GLYPH_BEFORE and GLYPH_AFTER. */
14055 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14056 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14057 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14059 /* An empty line has a single glyph whose OBJECT is zero and
14060 whose CHARPOS is the position of a newline on that line.
14061 Note that on a TTY, there are more glyphs after that, which
14062 were produced by extend_face_to_end_of_line, but their
14063 CHARPOS is zero or negative. */
14064 int empty_line_p =
14065 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14066 && INTEGERP (glyph->object) && glyph->charpos > 0
14067 /* On a TTY, continued and truncated rows also have a glyph at
14068 their end whose OBJECT is zero and whose CHARPOS is
14069 positive (the continuation and truncation glyphs), but such
14070 rows are obviously not "empty". */
14071 && !(row->continued_p || row->truncated_on_right_p);
14073 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14075 ptrdiff_t ellipsis_pos;
14077 /* Scan back over the ellipsis glyphs. */
14078 if (!row->reversed_p)
14080 ellipsis_pos = (glyph - 1)->charpos;
14081 while (glyph > row->glyphs[TEXT_AREA]
14082 && (glyph - 1)->charpos == ellipsis_pos)
14083 glyph--, x -= glyph->pixel_width;
14084 /* That loop always goes one position too far, including
14085 the glyph before the ellipsis. So scan forward over
14086 that one. */
14087 x += glyph->pixel_width;
14088 glyph++;
14090 else /* row is reversed */
14092 ellipsis_pos = (glyph + 1)->charpos;
14093 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14094 && (glyph + 1)->charpos == ellipsis_pos)
14095 glyph++, x += glyph->pixel_width;
14096 x -= glyph->pixel_width;
14097 glyph--;
14100 else if (match_with_avoid_cursor)
14102 cursor = glyph_after;
14103 x = -1;
14105 else if (string_seen)
14107 int incr = row->reversed_p ? -1 : +1;
14109 /* Need to find the glyph that came out of a string which is
14110 present at point. That glyph is somewhere between
14111 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14112 positioned between POS_BEFORE and POS_AFTER in the
14113 buffer. */
14114 struct glyph *start, *stop;
14115 ptrdiff_t pos = pos_before;
14117 x = -1;
14119 /* If the row ends in a newline from a display string,
14120 reordering could have moved the glyphs belonging to the
14121 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14122 in this case we extend the search to the last glyph in
14123 the row that was not inserted by redisplay. */
14124 if (row->ends_in_newline_from_string_p)
14126 glyph_after = end;
14127 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14130 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14131 correspond to POS_BEFORE and POS_AFTER, respectively. We
14132 need START and STOP in the order that corresponds to the
14133 row's direction as given by its reversed_p flag. If the
14134 directionality of characters between POS_BEFORE and
14135 POS_AFTER is the opposite of the row's base direction,
14136 these characters will have been reordered for display,
14137 and we need to reverse START and STOP. */
14138 if (!row->reversed_p)
14140 start = min (glyph_before, glyph_after);
14141 stop = max (glyph_before, glyph_after);
14143 else
14145 start = max (glyph_before, glyph_after);
14146 stop = min (glyph_before, glyph_after);
14148 for (glyph = start + incr;
14149 row->reversed_p ? glyph > stop : glyph < stop; )
14152 /* Any glyphs that come from the buffer are here because
14153 of bidi reordering. Skip them, and only pay
14154 attention to glyphs that came from some string. */
14155 if (STRINGP (glyph->object))
14157 Lisp_Object str;
14158 ptrdiff_t tem;
14159 /* If the display property covers the newline, we
14160 need to search for it one position farther. */
14161 ptrdiff_t lim = pos_after
14162 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14164 string_from_text_prop = 0;
14165 str = glyph->object;
14166 tem = string_buffer_position_lim (str, pos, lim, 0);
14167 if (tem == 0 /* from overlay */
14168 || pos <= tem)
14170 /* If the string from which this glyph came is
14171 found in the buffer at point, or at position
14172 that is closer to point than pos_after, then
14173 we've found the glyph we've been looking for.
14174 If it comes from an overlay (tem == 0), and
14175 it has the `cursor' property on one of its
14176 glyphs, record that glyph as a candidate for
14177 displaying the cursor. (As in the
14178 unidirectional version, we will display the
14179 cursor on the last candidate we find.) */
14180 if (tem == 0
14181 || tem == pt_old
14182 || (tem - pt_old > 0 && tem < pos_after))
14184 /* The glyphs from this string could have
14185 been reordered. Find the one with the
14186 smallest string position. Or there could
14187 be a character in the string with the
14188 `cursor' property, which means display
14189 cursor on that character's glyph. */
14190 ptrdiff_t strpos = glyph->charpos;
14192 if (tem)
14194 cursor = glyph;
14195 string_from_text_prop = 1;
14197 for ( ;
14198 (row->reversed_p ? glyph > stop : glyph < stop)
14199 && EQ (glyph->object, str);
14200 glyph += incr)
14202 Lisp_Object cprop;
14203 ptrdiff_t gpos = glyph->charpos;
14205 cprop = Fget_char_property (make_number (gpos),
14206 Qcursor,
14207 glyph->object);
14208 if (!NILP (cprop))
14210 cursor = glyph;
14211 break;
14213 if (tem && glyph->charpos < strpos)
14215 strpos = glyph->charpos;
14216 cursor = glyph;
14220 if (tem == pt_old
14221 || (tem - pt_old > 0 && tem < pos_after))
14222 goto compute_x;
14224 if (tem)
14225 pos = tem + 1; /* don't find previous instances */
14227 /* This string is not what we want; skip all of the
14228 glyphs that came from it. */
14229 while ((row->reversed_p ? glyph > stop : glyph < stop)
14230 && EQ (glyph->object, str))
14231 glyph += incr;
14233 else
14234 glyph += incr;
14237 /* If we reached the end of the line, and END was from a string,
14238 the cursor is not on this line. */
14239 if (cursor == NULL
14240 && (row->reversed_p ? glyph <= end : glyph >= end)
14241 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14242 && STRINGP (end->object)
14243 && row->continued_p)
14244 return 0;
14246 /* A truncated row may not include PT among its character positions.
14247 Setting the cursor inside the scroll margin will trigger
14248 recalculation of hscroll in hscroll_window_tree. But if a
14249 display string covers point, defer to the string-handling
14250 code below to figure this out. */
14251 else if (row->truncated_on_left_p && pt_old < bpos_min)
14253 cursor = glyph_before;
14254 x = -1;
14256 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14257 /* Zero-width characters produce no glyphs. */
14258 || (!empty_line_p
14259 && (row->reversed_p
14260 ? glyph_after > glyphs_end
14261 : glyph_after < glyphs_end)))
14263 cursor = glyph_after;
14264 x = -1;
14268 compute_x:
14269 if (cursor != NULL)
14270 glyph = cursor;
14271 else if (glyph == glyphs_end
14272 && pos_before == pos_after
14273 && STRINGP ((row->reversed_p
14274 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14275 : row->glyphs[TEXT_AREA])->object))
14277 /* If all the glyphs of this row came from strings, put the
14278 cursor on the first glyph of the row. This avoids having the
14279 cursor outside of the text area in this very rare and hard
14280 use case. */
14281 glyph =
14282 row->reversed_p
14283 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14284 : row->glyphs[TEXT_AREA];
14286 if (x < 0)
14288 struct glyph *g;
14290 /* Need to compute x that corresponds to GLYPH. */
14291 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14293 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14294 emacs_abort ();
14295 x += g->pixel_width;
14299 /* ROW could be part of a continued line, which, under bidi
14300 reordering, might have other rows whose start and end charpos
14301 occlude point. Only set w->cursor if we found a better
14302 approximation to the cursor position than we have from previously
14303 examined candidate rows belonging to the same continued line. */
14304 if (/* we already have a candidate row */
14305 w->cursor.vpos >= 0
14306 /* that candidate is not the row we are processing */
14307 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14308 /* Make sure cursor.vpos specifies a row whose start and end
14309 charpos occlude point, and it is valid candidate for being a
14310 cursor-row. This is because some callers of this function
14311 leave cursor.vpos at the row where the cursor was displayed
14312 during the last redisplay cycle. */
14313 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14314 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14315 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14317 struct glyph *g1 =
14318 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14320 /* Don't consider glyphs that are outside TEXT_AREA. */
14321 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14322 return 0;
14323 /* Keep the candidate whose buffer position is the closest to
14324 point or has the `cursor' property. */
14325 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14326 w->cursor.hpos >= 0
14327 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14328 && ((BUFFERP (g1->object)
14329 && (g1->charpos == pt_old /* an exact match always wins */
14330 || (BUFFERP (glyph->object)
14331 && eabs (g1->charpos - pt_old)
14332 < eabs (glyph->charpos - pt_old))))
14333 /* previous candidate is a glyph from a string that has
14334 a non-nil `cursor' property */
14335 || (STRINGP (g1->object)
14336 && (!NILP (Fget_char_property (make_number (g1->charpos),
14337 Qcursor, g1->object))
14338 /* previous candidate is from the same display
14339 string as this one, and the display string
14340 came from a text property */
14341 || (EQ (g1->object, glyph->object)
14342 && string_from_text_prop)
14343 /* this candidate is from newline and its
14344 position is not an exact match */
14345 || (INTEGERP (glyph->object)
14346 && glyph->charpos != pt_old)))))
14347 return 0;
14348 /* If this candidate gives an exact match, use that. */
14349 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14350 /* If this candidate is a glyph created for the
14351 terminating newline of a line, and point is on that
14352 newline, it wins because it's an exact match. */
14353 || (!row->continued_p
14354 && INTEGERP (glyph->object)
14355 && glyph->charpos == 0
14356 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14357 /* Otherwise, keep the candidate that comes from a row
14358 spanning less buffer positions. This may win when one or
14359 both candidate positions are on glyphs that came from
14360 display strings, for which we cannot compare buffer
14361 positions. */
14362 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14363 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14364 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14365 return 0;
14367 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14368 w->cursor.x = x;
14369 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14370 w->cursor.y = row->y + dy;
14372 if (w == XWINDOW (selected_window))
14374 if (!row->continued_p
14375 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14376 && row->x == 0)
14378 this_line_buffer = XBUFFER (w->contents);
14380 CHARPOS (this_line_start_pos)
14381 = MATRIX_ROW_START_CHARPOS (row) + delta;
14382 BYTEPOS (this_line_start_pos)
14383 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14385 CHARPOS (this_line_end_pos)
14386 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14387 BYTEPOS (this_line_end_pos)
14388 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14390 this_line_y = w->cursor.y;
14391 this_line_pixel_height = row->height;
14392 this_line_vpos = w->cursor.vpos;
14393 this_line_start_x = row->x;
14395 else
14396 CHARPOS (this_line_start_pos) = 0;
14399 return 1;
14403 /* Run window scroll functions, if any, for WINDOW with new window
14404 start STARTP. Sets the window start of WINDOW to that position.
14406 We assume that the window's buffer is really current. */
14408 static struct text_pos
14409 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14411 struct window *w = XWINDOW (window);
14412 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14414 if (current_buffer != XBUFFER (w->contents))
14415 emacs_abort ();
14417 if (!NILP (Vwindow_scroll_functions))
14419 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14420 make_number (CHARPOS (startp)));
14421 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14422 /* In case the hook functions switch buffers. */
14423 set_buffer_internal (XBUFFER (w->contents));
14426 return startp;
14430 /* Make sure the line containing the cursor is fully visible.
14431 A value of 1 means there is nothing to be done.
14432 (Either the line is fully visible, or it cannot be made so,
14433 or we cannot tell.)
14435 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14436 is higher than window.
14438 A value of 0 means the caller should do scrolling
14439 as if point had gone off the screen. */
14441 static int
14442 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14444 struct glyph_matrix *matrix;
14445 struct glyph_row *row;
14446 int window_height;
14448 if (!make_cursor_line_fully_visible_p)
14449 return 1;
14451 /* It's not always possible to find the cursor, e.g, when a window
14452 is full of overlay strings. Don't do anything in that case. */
14453 if (w->cursor.vpos < 0)
14454 return 1;
14456 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14457 row = MATRIX_ROW (matrix, w->cursor.vpos);
14459 /* If the cursor row is not partially visible, there's nothing to do. */
14460 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14461 return 1;
14463 /* If the row the cursor is in is taller than the window's height,
14464 it's not clear what to do, so do nothing. */
14465 window_height = window_box_height (w);
14466 if (row->height >= window_height)
14468 if (!force_p || MINI_WINDOW_P (w)
14469 || w->vscroll || w->cursor.vpos == 0)
14470 return 1;
14472 return 0;
14476 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14477 non-zero means only WINDOW is redisplayed in redisplay_internal.
14478 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14479 in redisplay_window to bring a partially visible line into view in
14480 the case that only the cursor has moved.
14482 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14483 last screen line's vertical height extends past the end of the screen.
14485 Value is
14487 1 if scrolling succeeded
14489 0 if scrolling didn't find point.
14491 -1 if new fonts have been loaded so that we must interrupt
14492 redisplay, adjust glyph matrices, and try again. */
14494 enum
14496 SCROLLING_SUCCESS,
14497 SCROLLING_FAILED,
14498 SCROLLING_NEED_LARGER_MATRICES
14501 /* If scroll-conservatively is more than this, never recenter.
14503 If you change this, don't forget to update the doc string of
14504 `scroll-conservatively' and the Emacs manual. */
14505 #define SCROLL_LIMIT 100
14507 static int
14508 try_scrolling (Lisp_Object window, int just_this_one_p,
14509 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14510 int temp_scroll_step, int last_line_misfit)
14512 struct window *w = XWINDOW (window);
14513 struct frame *f = XFRAME (w->frame);
14514 struct text_pos pos, startp;
14515 struct it it;
14516 int this_scroll_margin, scroll_max, rc, height;
14517 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14518 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14519 Lisp_Object aggressive;
14520 /* We will never try scrolling more than this number of lines. */
14521 int scroll_limit = SCROLL_LIMIT;
14523 #ifdef GLYPH_DEBUG
14524 debug_method_add (w, "try_scrolling");
14525 #endif
14527 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14529 /* Compute scroll margin height in pixels. We scroll when point is
14530 within this distance from the top or bottom of the window. */
14531 if (scroll_margin > 0)
14532 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14533 * FRAME_LINE_HEIGHT (f);
14534 else
14535 this_scroll_margin = 0;
14537 /* Force arg_scroll_conservatively to have a reasonable value, to
14538 avoid scrolling too far away with slow move_it_* functions. Note
14539 that the user can supply scroll-conservatively equal to
14540 `most-positive-fixnum', which can be larger than INT_MAX. */
14541 if (arg_scroll_conservatively > scroll_limit)
14543 arg_scroll_conservatively = scroll_limit + 1;
14544 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14546 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14547 /* Compute how much we should try to scroll maximally to bring
14548 point into view. */
14549 scroll_max = (max (scroll_step,
14550 max (arg_scroll_conservatively, temp_scroll_step))
14551 * FRAME_LINE_HEIGHT (f));
14552 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14553 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14554 /* We're trying to scroll because of aggressive scrolling but no
14555 scroll_step is set. Choose an arbitrary one. */
14556 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14557 else
14558 scroll_max = 0;
14560 too_near_end:
14562 /* Decide whether to scroll down. */
14563 if (PT > CHARPOS (startp))
14565 int scroll_margin_y;
14567 /* Compute the pixel ypos of the scroll margin, then move IT to
14568 either that ypos or PT, whichever comes first. */
14569 start_display (&it, w, startp);
14570 scroll_margin_y = it.last_visible_y - this_scroll_margin
14571 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14572 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14573 (MOVE_TO_POS | MOVE_TO_Y));
14575 if (PT > CHARPOS (it.current.pos))
14577 int y0 = line_bottom_y (&it);
14578 /* Compute how many pixels below window bottom to stop searching
14579 for PT. This avoids costly search for PT that is far away if
14580 the user limited scrolling by a small number of lines, but
14581 always finds PT if scroll_conservatively is set to a large
14582 number, such as most-positive-fixnum. */
14583 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14584 int y_to_move = it.last_visible_y + slack;
14586 /* Compute the distance from the scroll margin to PT or to
14587 the scroll limit, whichever comes first. This should
14588 include the height of the cursor line, to make that line
14589 fully visible. */
14590 move_it_to (&it, PT, -1, y_to_move,
14591 -1, MOVE_TO_POS | MOVE_TO_Y);
14592 dy = line_bottom_y (&it) - y0;
14594 if (dy > scroll_max)
14595 return SCROLLING_FAILED;
14597 if (dy > 0)
14598 scroll_down_p = 1;
14602 if (scroll_down_p)
14604 /* Point is in or below the bottom scroll margin, so move the
14605 window start down. If scrolling conservatively, move it just
14606 enough down to make point visible. If scroll_step is set,
14607 move it down by scroll_step. */
14608 if (arg_scroll_conservatively)
14609 amount_to_scroll
14610 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14611 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14612 else if (scroll_step || temp_scroll_step)
14613 amount_to_scroll = scroll_max;
14614 else
14616 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14617 height = WINDOW_BOX_TEXT_HEIGHT (w);
14618 if (NUMBERP (aggressive))
14620 double float_amount = XFLOATINT (aggressive) * height;
14621 int aggressive_scroll = float_amount;
14622 if (aggressive_scroll == 0 && float_amount > 0)
14623 aggressive_scroll = 1;
14624 /* Don't let point enter the scroll margin near top of
14625 the window. This could happen if the value of
14626 scroll_up_aggressively is too large and there are
14627 non-zero margins, because scroll_up_aggressively
14628 means put point that fraction of window height
14629 _from_the_bottom_margin_. */
14630 if (aggressive_scroll + 2*this_scroll_margin > height)
14631 aggressive_scroll = height - 2*this_scroll_margin;
14632 amount_to_scroll = dy + aggressive_scroll;
14636 if (amount_to_scroll <= 0)
14637 return SCROLLING_FAILED;
14639 start_display (&it, w, startp);
14640 if (arg_scroll_conservatively <= scroll_limit)
14641 move_it_vertically (&it, amount_to_scroll);
14642 else
14644 /* Extra precision for users who set scroll-conservatively
14645 to a large number: make sure the amount we scroll
14646 the window start is never less than amount_to_scroll,
14647 which was computed as distance from window bottom to
14648 point. This matters when lines at window top and lines
14649 below window bottom have different height. */
14650 struct it it1;
14651 void *it1data = NULL;
14652 /* We use a temporary it1 because line_bottom_y can modify
14653 its argument, if it moves one line down; see there. */
14654 int start_y;
14656 SAVE_IT (it1, it, it1data);
14657 start_y = line_bottom_y (&it1);
14658 do {
14659 RESTORE_IT (&it, &it, it1data);
14660 move_it_by_lines (&it, 1);
14661 SAVE_IT (it1, it, it1data);
14662 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14665 /* If STARTP is unchanged, move it down another screen line. */
14666 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14667 move_it_by_lines (&it, 1);
14668 startp = it.current.pos;
14670 else
14672 struct text_pos scroll_margin_pos = startp;
14673 int y_offset = 0;
14675 /* See if point is inside the scroll margin at the top of the
14676 window. */
14677 if (this_scroll_margin)
14679 int y_start;
14681 start_display (&it, w, startp);
14682 y_start = it.current_y;
14683 move_it_vertically (&it, this_scroll_margin);
14684 scroll_margin_pos = it.current.pos;
14685 /* If we didn't move enough before hitting ZV, request
14686 additional amount of scroll, to move point out of the
14687 scroll margin. */
14688 if (IT_CHARPOS (it) == ZV
14689 && it.current_y - y_start < this_scroll_margin)
14690 y_offset = this_scroll_margin - (it.current_y - y_start);
14693 if (PT < CHARPOS (scroll_margin_pos))
14695 /* Point is in the scroll margin at the top of the window or
14696 above what is displayed in the window. */
14697 int y0, y_to_move;
14699 /* Compute the vertical distance from PT to the scroll
14700 margin position. Move as far as scroll_max allows, or
14701 one screenful, or 10 screen lines, whichever is largest.
14702 Give up if distance is greater than scroll_max or if we
14703 didn't reach the scroll margin position. */
14704 SET_TEXT_POS (pos, PT, PT_BYTE);
14705 start_display (&it, w, pos);
14706 y0 = it.current_y;
14707 y_to_move = max (it.last_visible_y,
14708 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14709 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14710 y_to_move, -1,
14711 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14712 dy = it.current_y - y0;
14713 if (dy > scroll_max
14714 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14715 return SCROLLING_FAILED;
14717 /* Additional scroll for when ZV was too close to point. */
14718 dy += y_offset;
14720 /* Compute new window start. */
14721 start_display (&it, w, startp);
14723 if (arg_scroll_conservatively)
14724 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14725 max (scroll_step, temp_scroll_step));
14726 else if (scroll_step || temp_scroll_step)
14727 amount_to_scroll = scroll_max;
14728 else
14730 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14731 height = WINDOW_BOX_TEXT_HEIGHT (w);
14732 if (NUMBERP (aggressive))
14734 double float_amount = XFLOATINT (aggressive) * height;
14735 int aggressive_scroll = float_amount;
14736 if (aggressive_scroll == 0 && float_amount > 0)
14737 aggressive_scroll = 1;
14738 /* Don't let point enter the scroll margin near
14739 bottom of the window, if the value of
14740 scroll_down_aggressively happens to be too
14741 large. */
14742 if (aggressive_scroll + 2*this_scroll_margin > height)
14743 aggressive_scroll = height - 2*this_scroll_margin;
14744 amount_to_scroll = dy + aggressive_scroll;
14748 if (amount_to_scroll <= 0)
14749 return SCROLLING_FAILED;
14751 move_it_vertically_backward (&it, amount_to_scroll);
14752 startp = it.current.pos;
14756 /* Run window scroll functions. */
14757 startp = run_window_scroll_functions (window, startp);
14759 /* Display the window. Give up if new fonts are loaded, or if point
14760 doesn't appear. */
14761 if (!try_window (window, startp, 0))
14762 rc = SCROLLING_NEED_LARGER_MATRICES;
14763 else if (w->cursor.vpos < 0)
14765 clear_glyph_matrix (w->desired_matrix);
14766 rc = SCROLLING_FAILED;
14768 else
14770 /* Maybe forget recorded base line for line number display. */
14771 if (!just_this_one_p
14772 || current_buffer->clip_changed
14773 || BEG_UNCHANGED < CHARPOS (startp))
14774 w->base_line_number = 0;
14776 /* If cursor ends up on a partially visible line,
14777 treat that as being off the bottom of the screen. */
14778 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14779 /* It's possible that the cursor is on the first line of the
14780 buffer, which is partially obscured due to a vscroll
14781 (Bug#7537). In that case, avoid looping forever . */
14782 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14784 clear_glyph_matrix (w->desired_matrix);
14785 ++extra_scroll_margin_lines;
14786 goto too_near_end;
14788 rc = SCROLLING_SUCCESS;
14791 return rc;
14795 /* Compute a suitable window start for window W if display of W starts
14796 on a continuation line. Value is non-zero if a new window start
14797 was computed.
14799 The new window start will be computed, based on W's width, starting
14800 from the start of the continued line. It is the start of the
14801 screen line with the minimum distance from the old start W->start. */
14803 static int
14804 compute_window_start_on_continuation_line (struct window *w)
14806 struct text_pos pos, start_pos;
14807 int window_start_changed_p = 0;
14809 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14811 /* If window start is on a continuation line... Window start may be
14812 < BEGV in case there's invisible text at the start of the
14813 buffer (M-x rmail, for example). */
14814 if (CHARPOS (start_pos) > BEGV
14815 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14817 struct it it;
14818 struct glyph_row *row;
14820 /* Handle the case that the window start is out of range. */
14821 if (CHARPOS (start_pos) < BEGV)
14822 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14823 else if (CHARPOS (start_pos) > ZV)
14824 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14826 /* Find the start of the continued line. This should be fast
14827 because find_newline is fast (newline cache). */
14828 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14829 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14830 row, DEFAULT_FACE_ID);
14831 reseat_at_previous_visible_line_start (&it);
14833 /* If the line start is "too far" away from the window start,
14834 say it takes too much time to compute a new window start. */
14835 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14836 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14838 int min_distance, distance;
14840 /* Move forward by display lines to find the new window
14841 start. If window width was enlarged, the new start can
14842 be expected to be > the old start. If window width was
14843 decreased, the new window start will be < the old start.
14844 So, we're looking for the display line start with the
14845 minimum distance from the old window start. */
14846 pos = it.current.pos;
14847 min_distance = INFINITY;
14848 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14849 distance < min_distance)
14851 min_distance = distance;
14852 pos = it.current.pos;
14853 move_it_by_lines (&it, 1);
14856 /* Set the window start there. */
14857 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14858 window_start_changed_p = 1;
14862 return window_start_changed_p;
14866 /* Try cursor movement in case text has not changed in window WINDOW,
14867 with window start STARTP. Value is
14869 CURSOR_MOVEMENT_SUCCESS if successful
14871 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14873 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14874 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14875 we want to scroll as if scroll-step were set to 1. See the code.
14877 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14878 which case we have to abort this redisplay, and adjust matrices
14879 first. */
14881 enum
14883 CURSOR_MOVEMENT_SUCCESS,
14884 CURSOR_MOVEMENT_CANNOT_BE_USED,
14885 CURSOR_MOVEMENT_MUST_SCROLL,
14886 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14889 static int
14890 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14892 struct window *w = XWINDOW (window);
14893 struct frame *f = XFRAME (w->frame);
14894 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14896 #ifdef GLYPH_DEBUG
14897 if (inhibit_try_cursor_movement)
14898 return rc;
14899 #endif
14901 /* Previously, there was a check for Lisp integer in the
14902 if-statement below. Now, this field is converted to
14903 ptrdiff_t, thus zero means invalid position in a buffer. */
14904 eassert (w->last_point > 0);
14906 /* Handle case where text has not changed, only point, and it has
14907 not moved off the frame. */
14908 if (/* Point may be in this window. */
14909 PT >= CHARPOS (startp)
14910 /* Selective display hasn't changed. */
14911 && !current_buffer->clip_changed
14912 /* Function force-mode-line-update is used to force a thorough
14913 redisplay. It sets either windows_or_buffers_changed or
14914 update_mode_lines. So don't take a shortcut here for these
14915 cases. */
14916 && !update_mode_lines
14917 && !windows_or_buffers_changed
14918 && !cursor_type_changed
14919 /* Can't use this case if highlighting a region. When a
14920 region exists, cursor movement has to do more than just
14921 set the cursor. */
14922 && markpos_of_region () < 0
14923 && !w->region_showing
14924 && NILP (Vshow_trailing_whitespace)
14925 /* This code is not used for mini-buffer for the sake of the case
14926 of redisplaying to replace an echo area message; since in
14927 that case the mini-buffer contents per se are usually
14928 unchanged. This code is of no real use in the mini-buffer
14929 since the handling of this_line_start_pos, etc., in redisplay
14930 handles the same cases. */
14931 && !EQ (window, minibuf_window)
14932 /* When splitting windows or for new windows, it happens that
14933 redisplay is called with a nil window_end_vpos or one being
14934 larger than the window. This should really be fixed in
14935 window.c. I don't have this on my list, now, so we do
14936 approximately the same as the old redisplay code. --gerd. */
14937 && INTEGERP (w->window_end_vpos)
14938 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14939 && (FRAME_WINDOW_P (f)
14940 || !overlay_arrow_in_current_buffer_p ()))
14942 int this_scroll_margin, top_scroll_margin;
14943 struct glyph_row *row = NULL;
14945 #ifdef GLYPH_DEBUG
14946 debug_method_add (w, "cursor movement");
14947 #endif
14949 /* Scroll if point within this distance from the top or bottom
14950 of the window. This is a pixel value. */
14951 if (scroll_margin > 0)
14953 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14954 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14956 else
14957 this_scroll_margin = 0;
14959 top_scroll_margin = this_scroll_margin;
14960 if (WINDOW_WANTS_HEADER_LINE_P (w))
14961 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14963 /* Start with the row the cursor was displayed during the last
14964 not paused redisplay. Give up if that row is not valid. */
14965 if (w->last_cursor.vpos < 0
14966 || w->last_cursor.vpos >= w->current_matrix->nrows)
14967 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14968 else
14970 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14971 if (row->mode_line_p)
14972 ++row;
14973 if (!row->enabled_p)
14974 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14977 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14979 int scroll_p = 0, must_scroll = 0;
14980 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14982 if (PT > w->last_point)
14984 /* Point has moved forward. */
14985 while (MATRIX_ROW_END_CHARPOS (row) < PT
14986 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14988 eassert (row->enabled_p);
14989 ++row;
14992 /* If the end position of a row equals the start
14993 position of the next row, and PT is at that position,
14994 we would rather display cursor in the next line. */
14995 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14996 && MATRIX_ROW_END_CHARPOS (row) == PT
14997 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
14998 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14999 && !cursor_row_p (row))
15000 ++row;
15002 /* If within the scroll margin, scroll. Note that
15003 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15004 the next line would be drawn, and that
15005 this_scroll_margin can be zero. */
15006 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15007 || PT > MATRIX_ROW_END_CHARPOS (row)
15008 /* Line is completely visible last line in window
15009 and PT is to be set in the next line. */
15010 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15011 && PT == MATRIX_ROW_END_CHARPOS (row)
15012 && !row->ends_at_zv_p
15013 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15014 scroll_p = 1;
15016 else if (PT < w->last_point)
15018 /* Cursor has to be moved backward. Note that PT >=
15019 CHARPOS (startp) because of the outer if-statement. */
15020 while (!row->mode_line_p
15021 && (MATRIX_ROW_START_CHARPOS (row) > PT
15022 || (MATRIX_ROW_START_CHARPOS (row) == PT
15023 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15024 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15025 row > w->current_matrix->rows
15026 && (row-1)->ends_in_newline_from_string_p))))
15027 && (row->y > top_scroll_margin
15028 || CHARPOS (startp) == BEGV))
15030 eassert (row->enabled_p);
15031 --row;
15034 /* Consider the following case: Window starts at BEGV,
15035 there is invisible, intangible text at BEGV, so that
15036 display starts at some point START > BEGV. It can
15037 happen that we are called with PT somewhere between
15038 BEGV and START. Try to handle that case. */
15039 if (row < w->current_matrix->rows
15040 || row->mode_line_p)
15042 row = w->current_matrix->rows;
15043 if (row->mode_line_p)
15044 ++row;
15047 /* Due to newlines in overlay strings, we may have to
15048 skip forward over overlay strings. */
15049 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15050 && MATRIX_ROW_END_CHARPOS (row) == PT
15051 && !cursor_row_p (row))
15052 ++row;
15054 /* If within the scroll margin, scroll. */
15055 if (row->y < top_scroll_margin
15056 && CHARPOS (startp) != BEGV)
15057 scroll_p = 1;
15059 else
15061 /* Cursor did not move. So don't scroll even if cursor line
15062 is partially visible, as it was so before. */
15063 rc = CURSOR_MOVEMENT_SUCCESS;
15066 if (PT < MATRIX_ROW_START_CHARPOS (row)
15067 || PT > MATRIX_ROW_END_CHARPOS (row))
15069 /* if PT is not in the glyph row, give up. */
15070 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15071 must_scroll = 1;
15073 else if (rc != CURSOR_MOVEMENT_SUCCESS
15074 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15076 struct glyph_row *row1;
15078 /* If rows are bidi-reordered and point moved, back up
15079 until we find a row that does not belong to a
15080 continuation line. This is because we must consider
15081 all rows of a continued line as candidates for the
15082 new cursor positioning, since row start and end
15083 positions change non-linearly with vertical position
15084 in such rows. */
15085 /* FIXME: Revisit this when glyph ``spilling'' in
15086 continuation lines' rows is implemented for
15087 bidi-reordered rows. */
15088 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15089 MATRIX_ROW_CONTINUATION_LINE_P (row);
15090 --row)
15092 /* If we hit the beginning of the displayed portion
15093 without finding the first row of a continued
15094 line, give up. */
15095 if (row <= row1)
15097 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15098 break;
15100 eassert (row->enabled_p);
15103 if (must_scroll)
15105 else if (rc != CURSOR_MOVEMENT_SUCCESS
15106 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15107 /* Make sure this isn't a header line by any chance, since
15108 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15109 && !row->mode_line_p
15110 && make_cursor_line_fully_visible_p)
15112 if (PT == MATRIX_ROW_END_CHARPOS (row)
15113 && !row->ends_at_zv_p
15114 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15115 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15116 else if (row->height > window_box_height (w))
15118 /* If we end up in a partially visible line, let's
15119 make it fully visible, except when it's taller
15120 than the window, in which case we can't do much
15121 about it. */
15122 *scroll_step = 1;
15123 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15125 else
15127 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15128 if (!cursor_row_fully_visible_p (w, 0, 1))
15129 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15130 else
15131 rc = CURSOR_MOVEMENT_SUCCESS;
15134 else if (scroll_p)
15135 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15136 else if (rc != CURSOR_MOVEMENT_SUCCESS
15137 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15139 /* With bidi-reordered rows, there could be more than
15140 one candidate row whose start and end positions
15141 occlude point. We need to let set_cursor_from_row
15142 find the best candidate. */
15143 /* FIXME: Revisit this when glyph ``spilling'' in
15144 continuation lines' rows is implemented for
15145 bidi-reordered rows. */
15146 int rv = 0;
15150 int at_zv_p = 0, exact_match_p = 0;
15152 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15153 && PT <= MATRIX_ROW_END_CHARPOS (row)
15154 && cursor_row_p (row))
15155 rv |= set_cursor_from_row (w, row, w->current_matrix,
15156 0, 0, 0, 0);
15157 /* As soon as we've found the exact match for point,
15158 or the first suitable row whose ends_at_zv_p flag
15159 is set, we are done. */
15160 at_zv_p =
15161 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15162 if (rv && !at_zv_p
15163 && w->cursor.hpos >= 0
15164 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15165 w->cursor.vpos))
15167 struct glyph_row *candidate =
15168 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15169 struct glyph *g =
15170 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15171 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15173 exact_match_p =
15174 (BUFFERP (g->object) && g->charpos == PT)
15175 || (INTEGERP (g->object)
15176 && (g->charpos == PT
15177 || (g->charpos == 0 && endpos - 1 == PT)));
15179 if (rv && (at_zv_p || exact_match_p))
15181 rc = CURSOR_MOVEMENT_SUCCESS;
15182 break;
15184 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15185 break;
15186 ++row;
15188 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15189 || row->continued_p)
15190 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15191 || (MATRIX_ROW_START_CHARPOS (row) == PT
15192 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15193 /* If we didn't find any candidate rows, or exited the
15194 loop before all the candidates were examined, signal
15195 to the caller that this method failed. */
15196 if (rc != CURSOR_MOVEMENT_SUCCESS
15197 && !(rv
15198 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15199 && !row->continued_p))
15200 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15201 else if (rv)
15202 rc = CURSOR_MOVEMENT_SUCCESS;
15204 else
15208 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15210 rc = CURSOR_MOVEMENT_SUCCESS;
15211 break;
15213 ++row;
15215 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15216 && MATRIX_ROW_START_CHARPOS (row) == PT
15217 && cursor_row_p (row));
15222 return rc;
15225 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15226 static
15227 #endif
15228 void
15229 set_vertical_scroll_bar (struct window *w)
15231 ptrdiff_t start, end, whole;
15233 /* Calculate the start and end positions for the current window.
15234 At some point, it would be nice to choose between scrollbars
15235 which reflect the whole buffer size, with special markers
15236 indicating narrowing, and scrollbars which reflect only the
15237 visible region.
15239 Note that mini-buffers sometimes aren't displaying any text. */
15240 if (!MINI_WINDOW_P (w)
15241 || (w == XWINDOW (minibuf_window)
15242 && NILP (echo_area_buffer[0])))
15244 struct buffer *buf = XBUFFER (w->contents);
15245 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15246 start = marker_position (w->start) - BUF_BEGV (buf);
15247 /* I don't think this is guaranteed to be right. For the
15248 moment, we'll pretend it is. */
15249 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15251 if (end < start)
15252 end = start;
15253 if (whole < (end - start))
15254 whole = end - start;
15256 else
15257 start = end = whole = 0;
15259 /* Indicate what this scroll bar ought to be displaying now. */
15260 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15261 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15262 (w, end - start, whole, start);
15266 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15267 selected_window is redisplayed.
15269 We can return without actually redisplaying the window if
15270 fonts_changed_p. In that case, redisplay_internal will
15271 retry. */
15273 static void
15274 redisplay_window (Lisp_Object window, int just_this_one_p)
15276 struct window *w = XWINDOW (window);
15277 struct frame *f = XFRAME (w->frame);
15278 struct buffer *buffer = XBUFFER (w->contents);
15279 struct buffer *old = current_buffer;
15280 struct text_pos lpoint, opoint, startp;
15281 int update_mode_line;
15282 int tem;
15283 struct it it;
15284 /* Record it now because it's overwritten. */
15285 int current_matrix_up_to_date_p = 0;
15286 int used_current_matrix_p = 0;
15287 /* This is less strict than current_matrix_up_to_date_p.
15288 It indicates that the buffer contents and narrowing are unchanged. */
15289 int buffer_unchanged_p = 0;
15290 int temp_scroll_step = 0;
15291 ptrdiff_t count = SPECPDL_INDEX ();
15292 int rc;
15293 int centering_position = -1;
15294 int last_line_misfit = 0;
15295 ptrdiff_t beg_unchanged, end_unchanged;
15297 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15298 opoint = lpoint;
15300 #ifdef GLYPH_DEBUG
15301 *w->desired_matrix->method = 0;
15302 #endif
15304 /* Make sure that both W's markers are valid. */
15305 eassert (XMARKER (w->start)->buffer == buffer);
15306 eassert (XMARKER (w->pointm)->buffer == buffer);
15308 restart:
15309 reconsider_clip_changes (w, buffer);
15311 /* Has the mode line to be updated? */
15312 update_mode_line = (w->update_mode_line
15313 || update_mode_lines
15314 || buffer->clip_changed
15315 || buffer->prevent_redisplay_optimizations_p);
15317 if (MINI_WINDOW_P (w))
15319 if (w == XWINDOW (echo_area_window)
15320 && !NILP (echo_area_buffer[0]))
15322 if (update_mode_line)
15323 /* We may have to update a tty frame's menu bar or a
15324 tool-bar. Example `M-x C-h C-h C-g'. */
15325 goto finish_menu_bars;
15326 else
15327 /* We've already displayed the echo area glyphs in this window. */
15328 goto finish_scroll_bars;
15330 else if ((w != XWINDOW (minibuf_window)
15331 || minibuf_level == 0)
15332 /* When buffer is nonempty, redisplay window normally. */
15333 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15334 /* Quail displays non-mini buffers in minibuffer window.
15335 In that case, redisplay the window normally. */
15336 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15338 /* W is a mini-buffer window, but it's not active, so clear
15339 it. */
15340 int yb = window_text_bottom_y (w);
15341 struct glyph_row *row;
15342 int y;
15344 for (y = 0, row = w->desired_matrix->rows;
15345 y < yb;
15346 y += row->height, ++row)
15347 blank_row (w, row, y);
15348 goto finish_scroll_bars;
15351 clear_glyph_matrix (w->desired_matrix);
15354 /* Otherwise set up data on this window; select its buffer and point
15355 value. */
15356 /* Really select the buffer, for the sake of buffer-local
15357 variables. */
15358 set_buffer_internal_1 (XBUFFER (w->contents));
15360 current_matrix_up_to_date_p
15361 = (w->window_end_valid
15362 && !current_buffer->clip_changed
15363 && !current_buffer->prevent_redisplay_optimizations_p
15364 && !window_outdated (w));
15366 /* Run the window-bottom-change-functions
15367 if it is possible that the text on the screen has changed
15368 (either due to modification of the text, or any other reason). */
15369 if (!current_matrix_up_to_date_p
15370 && !NILP (Vwindow_text_change_functions))
15372 safe_run_hooks (Qwindow_text_change_functions);
15373 goto restart;
15376 beg_unchanged = BEG_UNCHANGED;
15377 end_unchanged = END_UNCHANGED;
15379 SET_TEXT_POS (opoint, PT, PT_BYTE);
15381 specbind (Qinhibit_point_motion_hooks, Qt);
15383 buffer_unchanged_p
15384 = (w->window_end_valid
15385 && !current_buffer->clip_changed
15386 && !window_outdated (w));
15388 /* When windows_or_buffers_changed is non-zero, we can't rely on
15389 the window end being valid, so set it to nil there. */
15390 if (windows_or_buffers_changed)
15392 /* If window starts on a continuation line, maybe adjust the
15393 window start in case the window's width changed. */
15394 if (XMARKER (w->start)->buffer == current_buffer)
15395 compute_window_start_on_continuation_line (w);
15397 w->window_end_valid = 0;
15400 /* Some sanity checks. */
15401 CHECK_WINDOW_END (w);
15402 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15403 emacs_abort ();
15404 if (BYTEPOS (opoint) < CHARPOS (opoint))
15405 emacs_abort ();
15407 if (mode_line_update_needed (w))
15408 update_mode_line = 1;
15410 /* Point refers normally to the selected window. For any other
15411 window, set up appropriate value. */
15412 if (!EQ (window, selected_window))
15414 ptrdiff_t new_pt = marker_position (w->pointm);
15415 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15416 if (new_pt < BEGV)
15418 new_pt = BEGV;
15419 new_pt_byte = BEGV_BYTE;
15420 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15422 else if (new_pt > (ZV - 1))
15424 new_pt = ZV;
15425 new_pt_byte = ZV_BYTE;
15426 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15429 /* We don't use SET_PT so that the point-motion hooks don't run. */
15430 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15433 /* If any of the character widths specified in the display table
15434 have changed, invalidate the width run cache. It's true that
15435 this may be a bit late to catch such changes, but the rest of
15436 redisplay goes (non-fatally) haywire when the display table is
15437 changed, so why should we worry about doing any better? */
15438 if (current_buffer->width_run_cache)
15440 struct Lisp_Char_Table *disptab = buffer_display_table ();
15442 if (! disptab_matches_widthtab
15443 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15445 invalidate_region_cache (current_buffer,
15446 current_buffer->width_run_cache,
15447 BEG, Z);
15448 recompute_width_table (current_buffer, disptab);
15452 /* If window-start is screwed up, choose a new one. */
15453 if (XMARKER (w->start)->buffer != current_buffer)
15454 goto recenter;
15456 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15458 /* If someone specified a new starting point but did not insist,
15459 check whether it can be used. */
15460 if (w->optional_new_start
15461 && CHARPOS (startp) >= BEGV
15462 && CHARPOS (startp) <= ZV)
15464 w->optional_new_start = 0;
15465 start_display (&it, w, startp);
15466 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15467 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15468 if (IT_CHARPOS (it) == PT)
15469 w->force_start = 1;
15470 /* IT may overshoot PT if text at PT is invisible. */
15471 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15472 w->force_start = 1;
15475 force_start:
15477 /* Handle case where place to start displaying has been specified,
15478 unless the specified location is outside the accessible range. */
15479 if (w->force_start || w->frozen_window_start_p)
15481 /* We set this later on if we have to adjust point. */
15482 int new_vpos = -1;
15484 w->force_start = 0;
15485 w->vscroll = 0;
15486 w->window_end_valid = 0;
15488 /* Forget any recorded base line for line number display. */
15489 if (!buffer_unchanged_p)
15490 w->base_line_number = 0;
15492 /* Redisplay the mode line. Select the buffer properly for that.
15493 Also, run the hook window-scroll-functions
15494 because we have scrolled. */
15495 /* Note, we do this after clearing force_start because
15496 if there's an error, it is better to forget about force_start
15497 than to get into an infinite loop calling the hook functions
15498 and having them get more errors. */
15499 if (!update_mode_line
15500 || ! NILP (Vwindow_scroll_functions))
15502 update_mode_line = 1;
15503 w->update_mode_line = 1;
15504 startp = run_window_scroll_functions (window, startp);
15507 w->last_modified = 0;
15508 w->last_overlay_modified = 0;
15509 if (CHARPOS (startp) < BEGV)
15510 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15511 else if (CHARPOS (startp) > ZV)
15512 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15514 /* Redisplay, then check if cursor has been set during the
15515 redisplay. Give up if new fonts were loaded. */
15516 /* We used to issue a CHECK_MARGINS argument to try_window here,
15517 but this causes scrolling to fail when point begins inside
15518 the scroll margin (bug#148) -- cyd */
15519 if (!try_window (window, startp, 0))
15521 w->force_start = 1;
15522 clear_glyph_matrix (w->desired_matrix);
15523 goto need_larger_matrices;
15526 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15528 /* If point does not appear, try to move point so it does
15529 appear. The desired matrix has been built above, so we
15530 can use it here. */
15531 new_vpos = window_box_height (w) / 2;
15534 if (!cursor_row_fully_visible_p (w, 0, 0))
15536 /* Point does appear, but on a line partly visible at end of window.
15537 Move it back to a fully-visible line. */
15538 new_vpos = window_box_height (w);
15540 else if (w->cursor.vpos >=0)
15542 /* Some people insist on not letting point enter the scroll
15543 margin, even though this part handles windows that didn't
15544 scroll at all. */
15545 int margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15546 int pixel_margin = margin * FRAME_LINE_HEIGHT (f);
15547 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15549 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15550 below, which finds the row to move point to, advances by
15551 the Y coordinate of the _next_ row, see the definition of
15552 MATRIX_ROW_BOTTOM_Y. */
15553 if (w->cursor.vpos < margin + header_line)
15554 new_vpos
15555 = pixel_margin + (header_line
15556 ? CURRENT_HEADER_LINE_HEIGHT (w)
15557 : 0) + FRAME_LINE_HEIGHT (f);
15558 else
15560 int window_height = window_box_height (w);
15562 if (header_line)
15563 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15564 if (w->cursor.y >= window_height - pixel_margin)
15565 new_vpos = window_height - pixel_margin;
15569 /* If we need to move point for either of the above reasons,
15570 now actually do it. */
15571 if (new_vpos >= 0)
15573 struct glyph_row *row;
15575 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15576 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15577 ++row;
15579 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15580 MATRIX_ROW_START_BYTEPOS (row));
15582 if (w != XWINDOW (selected_window))
15583 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15584 else if (current_buffer == old)
15585 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15587 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15589 /* If we are highlighting the region, then we just changed
15590 the region, so redisplay to show it. */
15591 if (markpos_of_region () >= 0)
15593 clear_glyph_matrix (w->desired_matrix);
15594 if (!try_window (window, startp, 0))
15595 goto need_larger_matrices;
15599 #ifdef GLYPH_DEBUG
15600 debug_method_add (w, "forced window start");
15601 #endif
15602 goto done;
15605 /* Handle case where text has not changed, only point, and it has
15606 not moved off the frame, and we are not retrying after hscroll.
15607 (current_matrix_up_to_date_p is nonzero when retrying.) */
15608 if (current_matrix_up_to_date_p
15609 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15610 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15612 switch (rc)
15614 case CURSOR_MOVEMENT_SUCCESS:
15615 used_current_matrix_p = 1;
15616 goto done;
15618 case CURSOR_MOVEMENT_MUST_SCROLL:
15619 goto try_to_scroll;
15621 default:
15622 emacs_abort ();
15625 /* If current starting point was originally the beginning of a line
15626 but no longer is, find a new starting point. */
15627 else if (w->start_at_line_beg
15628 && !(CHARPOS (startp) <= BEGV
15629 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15631 #ifdef GLYPH_DEBUG
15632 debug_method_add (w, "recenter 1");
15633 #endif
15634 goto recenter;
15637 /* Try scrolling with try_window_id. Value is > 0 if update has
15638 been done, it is -1 if we know that the same window start will
15639 not work. It is 0 if unsuccessful for some other reason. */
15640 else if ((tem = try_window_id (w)) != 0)
15642 #ifdef GLYPH_DEBUG
15643 debug_method_add (w, "try_window_id %d", tem);
15644 #endif
15646 if (fonts_changed_p)
15647 goto need_larger_matrices;
15648 if (tem > 0)
15649 goto done;
15651 /* Otherwise try_window_id has returned -1 which means that we
15652 don't want the alternative below this comment to execute. */
15654 else if (CHARPOS (startp) >= BEGV
15655 && CHARPOS (startp) <= ZV
15656 && PT >= CHARPOS (startp)
15657 && (CHARPOS (startp) < ZV
15658 /* Avoid starting at end of buffer. */
15659 || CHARPOS (startp) == BEGV
15660 || !window_outdated (w)))
15662 int d1, d2, d3, d4, d5, d6;
15664 /* If first window line is a continuation line, and window start
15665 is inside the modified region, but the first change is before
15666 current window start, we must select a new window start.
15668 However, if this is the result of a down-mouse event (e.g. by
15669 extending the mouse-drag-overlay), we don't want to select a
15670 new window start, since that would change the position under
15671 the mouse, resulting in an unwanted mouse-movement rather
15672 than a simple mouse-click. */
15673 if (!w->start_at_line_beg
15674 && NILP (do_mouse_tracking)
15675 && CHARPOS (startp) > BEGV
15676 && CHARPOS (startp) > BEG + beg_unchanged
15677 && CHARPOS (startp) <= Z - end_unchanged
15678 /* Even if w->start_at_line_beg is nil, a new window may
15679 start at a line_beg, since that's how set_buffer_window
15680 sets it. So, we need to check the return value of
15681 compute_window_start_on_continuation_line. (See also
15682 bug#197). */
15683 && XMARKER (w->start)->buffer == current_buffer
15684 && compute_window_start_on_continuation_line (w)
15685 /* It doesn't make sense to force the window start like we
15686 do at label force_start if it is already known that point
15687 will not be visible in the resulting window, because
15688 doing so will move point from its correct position
15689 instead of scrolling the window to bring point into view.
15690 See bug#9324. */
15691 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15693 w->force_start = 1;
15694 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15695 goto force_start;
15698 #ifdef GLYPH_DEBUG
15699 debug_method_add (w, "same window start");
15700 #endif
15702 /* Try to redisplay starting at same place as before.
15703 If point has not moved off frame, accept the results. */
15704 if (!current_matrix_up_to_date_p
15705 /* Don't use try_window_reusing_current_matrix in this case
15706 because a window scroll function can have changed the
15707 buffer. */
15708 || !NILP (Vwindow_scroll_functions)
15709 || MINI_WINDOW_P (w)
15710 || !(used_current_matrix_p
15711 = try_window_reusing_current_matrix (w)))
15713 IF_DEBUG (debug_method_add (w, "1"));
15714 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15715 /* -1 means we need to scroll.
15716 0 means we need new matrices, but fonts_changed_p
15717 is set in that case, so we will detect it below. */
15718 goto try_to_scroll;
15721 if (fonts_changed_p)
15722 goto need_larger_matrices;
15724 if (w->cursor.vpos >= 0)
15726 if (!just_this_one_p
15727 || current_buffer->clip_changed
15728 || BEG_UNCHANGED < CHARPOS (startp))
15729 /* Forget any recorded base line for line number display. */
15730 w->base_line_number = 0;
15732 if (!cursor_row_fully_visible_p (w, 1, 0))
15734 clear_glyph_matrix (w->desired_matrix);
15735 last_line_misfit = 1;
15737 /* Drop through and scroll. */
15738 else
15739 goto done;
15741 else
15742 clear_glyph_matrix (w->desired_matrix);
15745 try_to_scroll:
15747 w->last_modified = 0;
15748 w->last_overlay_modified = 0;
15750 /* Redisplay the mode line. Select the buffer properly for that. */
15751 if (!update_mode_line)
15753 update_mode_line = 1;
15754 w->update_mode_line = 1;
15757 /* Try to scroll by specified few lines. */
15758 if ((scroll_conservatively
15759 || emacs_scroll_step
15760 || temp_scroll_step
15761 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15762 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15763 && CHARPOS (startp) >= BEGV
15764 && CHARPOS (startp) <= ZV)
15766 /* The function returns -1 if new fonts were loaded, 1 if
15767 successful, 0 if not successful. */
15768 int ss = try_scrolling (window, just_this_one_p,
15769 scroll_conservatively,
15770 emacs_scroll_step,
15771 temp_scroll_step, last_line_misfit);
15772 switch (ss)
15774 case SCROLLING_SUCCESS:
15775 goto done;
15777 case SCROLLING_NEED_LARGER_MATRICES:
15778 goto need_larger_matrices;
15780 case SCROLLING_FAILED:
15781 break;
15783 default:
15784 emacs_abort ();
15788 /* Finally, just choose a place to start which positions point
15789 according to user preferences. */
15791 recenter:
15793 #ifdef GLYPH_DEBUG
15794 debug_method_add (w, "recenter");
15795 #endif
15797 /* Forget any previously recorded base line for line number display. */
15798 if (!buffer_unchanged_p)
15799 w->base_line_number = 0;
15801 /* Determine the window start relative to point. */
15802 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15803 it.current_y = it.last_visible_y;
15804 if (centering_position < 0)
15806 int margin =
15807 scroll_margin > 0
15808 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15809 : 0;
15810 ptrdiff_t margin_pos = CHARPOS (startp);
15811 Lisp_Object aggressive;
15812 int scrolling_up;
15814 /* If there is a scroll margin at the top of the window, find
15815 its character position. */
15816 if (margin
15817 /* Cannot call start_display if startp is not in the
15818 accessible region of the buffer. This can happen when we
15819 have just switched to a different buffer and/or changed
15820 its restriction. In that case, startp is initialized to
15821 the character position 1 (BEGV) because we did not yet
15822 have chance to display the buffer even once. */
15823 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15825 struct it it1;
15826 void *it1data = NULL;
15828 SAVE_IT (it1, it, it1data);
15829 start_display (&it1, w, startp);
15830 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15831 margin_pos = IT_CHARPOS (it1);
15832 RESTORE_IT (&it, &it, it1data);
15834 scrolling_up = PT > margin_pos;
15835 aggressive =
15836 scrolling_up
15837 ? BVAR (current_buffer, scroll_up_aggressively)
15838 : BVAR (current_buffer, scroll_down_aggressively);
15840 if (!MINI_WINDOW_P (w)
15841 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15843 int pt_offset = 0;
15845 /* Setting scroll-conservatively overrides
15846 scroll-*-aggressively. */
15847 if (!scroll_conservatively && NUMBERP (aggressive))
15849 double float_amount = XFLOATINT (aggressive);
15851 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15852 if (pt_offset == 0 && float_amount > 0)
15853 pt_offset = 1;
15854 if (pt_offset && margin > 0)
15855 margin -= 1;
15857 /* Compute how much to move the window start backward from
15858 point so that point will be displayed where the user
15859 wants it. */
15860 if (scrolling_up)
15862 centering_position = it.last_visible_y;
15863 if (pt_offset)
15864 centering_position -= pt_offset;
15865 centering_position -=
15866 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15867 + WINDOW_HEADER_LINE_HEIGHT (w);
15868 /* Don't let point enter the scroll margin near top of
15869 the window. */
15870 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15871 centering_position = margin * FRAME_LINE_HEIGHT (f);
15873 else
15874 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15876 else
15877 /* Set the window start half the height of the window backward
15878 from point. */
15879 centering_position = window_box_height (w) / 2;
15881 move_it_vertically_backward (&it, centering_position);
15883 eassert (IT_CHARPOS (it) >= BEGV);
15885 /* The function move_it_vertically_backward may move over more
15886 than the specified y-distance. If it->w is small, e.g. a
15887 mini-buffer window, we may end up in front of the window's
15888 display area. Start displaying at the start of the line
15889 containing PT in this case. */
15890 if (it.current_y <= 0)
15892 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15893 move_it_vertically_backward (&it, 0);
15894 it.current_y = 0;
15897 it.current_x = it.hpos = 0;
15899 /* Set the window start position here explicitly, to avoid an
15900 infinite loop in case the functions in window-scroll-functions
15901 get errors. */
15902 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15904 /* Run scroll hooks. */
15905 startp = run_window_scroll_functions (window, it.current.pos);
15907 /* Redisplay the window. */
15908 if (!current_matrix_up_to_date_p
15909 || windows_or_buffers_changed
15910 || cursor_type_changed
15911 /* Don't use try_window_reusing_current_matrix in this case
15912 because it can have changed the buffer. */
15913 || !NILP (Vwindow_scroll_functions)
15914 || !just_this_one_p
15915 || MINI_WINDOW_P (w)
15916 || !(used_current_matrix_p
15917 = try_window_reusing_current_matrix (w)))
15918 try_window (window, startp, 0);
15920 /* If new fonts have been loaded (due to fontsets), give up. We
15921 have to start a new redisplay since we need to re-adjust glyph
15922 matrices. */
15923 if (fonts_changed_p)
15924 goto need_larger_matrices;
15926 /* If cursor did not appear assume that the middle of the window is
15927 in the first line of the window. Do it again with the next line.
15928 (Imagine a window of height 100, displaying two lines of height
15929 60. Moving back 50 from it->last_visible_y will end in the first
15930 line.) */
15931 if (w->cursor.vpos < 0)
15933 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
15935 clear_glyph_matrix (w->desired_matrix);
15936 move_it_by_lines (&it, 1);
15937 try_window (window, it.current.pos, 0);
15939 else if (PT < IT_CHARPOS (it))
15941 clear_glyph_matrix (w->desired_matrix);
15942 move_it_by_lines (&it, -1);
15943 try_window (window, it.current.pos, 0);
15945 else
15947 /* Not much we can do about it. */
15951 /* Consider the following case: Window starts at BEGV, there is
15952 invisible, intangible text at BEGV, so that display starts at
15953 some point START > BEGV. It can happen that we are called with
15954 PT somewhere between BEGV and START. Try to handle that case. */
15955 if (w->cursor.vpos < 0)
15957 struct glyph_row *row = w->current_matrix->rows;
15958 if (row->mode_line_p)
15959 ++row;
15960 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15963 if (!cursor_row_fully_visible_p (w, 0, 0))
15965 /* If vscroll is enabled, disable it and try again. */
15966 if (w->vscroll)
15968 w->vscroll = 0;
15969 clear_glyph_matrix (w->desired_matrix);
15970 goto recenter;
15973 /* Users who set scroll-conservatively to a large number want
15974 point just above/below the scroll margin. If we ended up
15975 with point's row partially visible, move the window start to
15976 make that row fully visible and out of the margin. */
15977 if (scroll_conservatively > SCROLL_LIMIT)
15979 int margin =
15980 scroll_margin > 0
15981 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15982 : 0;
15983 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15985 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15986 clear_glyph_matrix (w->desired_matrix);
15987 if (1 == try_window (window, it.current.pos,
15988 TRY_WINDOW_CHECK_MARGINS))
15989 goto done;
15992 /* If centering point failed to make the whole line visible,
15993 put point at the top instead. That has to make the whole line
15994 visible, if it can be done. */
15995 if (centering_position == 0)
15996 goto done;
15998 clear_glyph_matrix (w->desired_matrix);
15999 centering_position = 0;
16000 goto recenter;
16003 done:
16005 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16006 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16007 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16009 /* Display the mode line, if we must. */
16010 if ((update_mode_line
16011 /* If window not full width, must redo its mode line
16012 if (a) the window to its side is being redone and
16013 (b) we do a frame-based redisplay. This is a consequence
16014 of how inverted lines are drawn in frame-based redisplay. */
16015 || (!just_this_one_p
16016 && !FRAME_WINDOW_P (f)
16017 && !WINDOW_FULL_WIDTH_P (w))
16018 /* Line number to display. */
16019 || w->base_line_pos > 0
16020 /* Column number is displayed and different from the one displayed. */
16021 || (w->column_number_displayed != -1
16022 && (w->column_number_displayed != current_column ())))
16023 /* This means that the window has a mode line. */
16024 && (WINDOW_WANTS_MODELINE_P (w)
16025 || WINDOW_WANTS_HEADER_LINE_P (w)))
16027 display_mode_lines (w);
16029 /* If mode line height has changed, arrange for a thorough
16030 immediate redisplay using the correct mode line height. */
16031 if (WINDOW_WANTS_MODELINE_P (w)
16032 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16034 fonts_changed_p = 1;
16035 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16036 = DESIRED_MODE_LINE_HEIGHT (w);
16039 /* If header line height has changed, arrange for a thorough
16040 immediate redisplay using the correct header line height. */
16041 if (WINDOW_WANTS_HEADER_LINE_P (w)
16042 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16044 fonts_changed_p = 1;
16045 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16046 = DESIRED_HEADER_LINE_HEIGHT (w);
16049 if (fonts_changed_p)
16050 goto need_larger_matrices;
16053 if (!line_number_displayed && w->base_line_pos != -1)
16055 w->base_line_pos = 0;
16056 w->base_line_number = 0;
16059 finish_menu_bars:
16061 /* When we reach a frame's selected window, redo the frame's menu bar. */
16062 if (update_mode_line
16063 && EQ (FRAME_SELECTED_WINDOW (f), window))
16065 int redisplay_menu_p = 0;
16067 if (FRAME_WINDOW_P (f))
16069 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16070 || defined (HAVE_NS) || defined (USE_GTK)
16071 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16072 #else
16073 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16074 #endif
16076 else
16077 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16079 if (redisplay_menu_p)
16080 display_menu_bar (w);
16082 #ifdef HAVE_WINDOW_SYSTEM
16083 if (FRAME_WINDOW_P (f))
16085 #if defined (USE_GTK) || defined (HAVE_NS)
16086 if (FRAME_EXTERNAL_TOOL_BAR (f))
16087 redisplay_tool_bar (f);
16088 #else
16089 if (WINDOWP (f->tool_bar_window)
16090 && (FRAME_TOOL_BAR_LINES (f) > 0
16091 || !NILP (Vauto_resize_tool_bars))
16092 && redisplay_tool_bar (f))
16093 ignore_mouse_drag_p = 1;
16094 #endif
16096 #endif
16099 #ifdef HAVE_WINDOW_SYSTEM
16100 if (FRAME_WINDOW_P (f)
16101 && update_window_fringes (w, (just_this_one_p
16102 || (!used_current_matrix_p && !overlay_arrow_seen)
16103 || w->pseudo_window_p)))
16105 update_begin (f);
16106 block_input ();
16107 if (draw_window_fringes (w, 1))
16108 x_draw_vertical_border (w);
16109 unblock_input ();
16110 update_end (f);
16112 #endif /* HAVE_WINDOW_SYSTEM */
16114 /* We go to this label, with fonts_changed_p set,
16115 if it is necessary to try again using larger glyph matrices.
16116 We have to redeem the scroll bar even in this case,
16117 because the loop in redisplay_internal expects that. */
16118 need_larger_matrices:
16120 finish_scroll_bars:
16122 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16124 /* Set the thumb's position and size. */
16125 set_vertical_scroll_bar (w);
16127 /* Note that we actually used the scroll bar attached to this
16128 window, so it shouldn't be deleted at the end of redisplay. */
16129 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16130 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16133 /* Restore current_buffer and value of point in it. The window
16134 update may have changed the buffer, so first make sure `opoint'
16135 is still valid (Bug#6177). */
16136 if (CHARPOS (opoint) < BEGV)
16137 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16138 else if (CHARPOS (opoint) > ZV)
16139 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16140 else
16141 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16143 set_buffer_internal_1 (old);
16144 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16145 shorter. This can be caused by log truncation in *Messages*. */
16146 if (CHARPOS (lpoint) <= ZV)
16147 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16149 unbind_to (count, Qnil);
16153 /* Build the complete desired matrix of WINDOW with a window start
16154 buffer position POS.
16156 Value is 1 if successful. It is zero if fonts were loaded during
16157 redisplay which makes re-adjusting glyph matrices necessary, and -1
16158 if point would appear in the scroll margins.
16159 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16160 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16161 set in FLAGS.) */
16164 try_window (Lisp_Object window, struct text_pos pos, int flags)
16166 struct window *w = XWINDOW (window);
16167 struct it it;
16168 struct glyph_row *last_text_row = NULL;
16169 struct frame *f = XFRAME (w->frame);
16171 /* Make POS the new window start. */
16172 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16174 /* Mark cursor position as unknown. No overlay arrow seen. */
16175 w->cursor.vpos = -1;
16176 overlay_arrow_seen = 0;
16178 /* Initialize iterator and info to start at POS. */
16179 start_display (&it, w, pos);
16181 /* Display all lines of W. */
16182 while (it.current_y < it.last_visible_y)
16184 if (display_line (&it))
16185 last_text_row = it.glyph_row - 1;
16186 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16187 return 0;
16190 /* Don't let the cursor end in the scroll margins. */
16191 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16192 && !MINI_WINDOW_P (w))
16194 int this_scroll_margin;
16196 if (scroll_margin > 0)
16198 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16199 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16201 else
16202 this_scroll_margin = 0;
16204 if ((w->cursor.y >= 0 /* not vscrolled */
16205 && w->cursor.y < this_scroll_margin
16206 && CHARPOS (pos) > BEGV
16207 && IT_CHARPOS (it) < ZV)
16208 /* rms: considering make_cursor_line_fully_visible_p here
16209 seems to give wrong results. We don't want to recenter
16210 when the last line is partly visible, we want to allow
16211 that case to be handled in the usual way. */
16212 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16214 w->cursor.vpos = -1;
16215 clear_glyph_matrix (w->desired_matrix);
16216 return -1;
16220 /* If bottom moved off end of frame, change mode line percentage. */
16221 if (XFASTINT (w->window_end_pos) <= 0
16222 && Z != IT_CHARPOS (it))
16223 w->update_mode_line = 1;
16225 /* Set window_end_pos to the offset of the last character displayed
16226 on the window from the end of current_buffer. Set
16227 window_end_vpos to its row number. */
16228 if (last_text_row)
16230 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16231 w->window_end_bytepos
16232 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16233 wset_window_end_pos
16234 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16235 wset_window_end_vpos
16236 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16237 eassert
16238 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16239 XFASTINT (w->window_end_vpos))));
16241 else
16243 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16244 wset_window_end_pos (w, make_number (Z - ZV));
16245 wset_window_end_vpos (w, make_number (0));
16248 /* But that is not valid info until redisplay finishes. */
16249 w->window_end_valid = 0;
16250 return 1;
16255 /************************************************************************
16256 Window redisplay reusing current matrix when buffer has not changed
16257 ************************************************************************/
16259 /* Try redisplay of window W showing an unchanged buffer with a
16260 different window start than the last time it was displayed by
16261 reusing its current matrix. Value is non-zero if successful.
16262 W->start is the new window start. */
16264 static int
16265 try_window_reusing_current_matrix (struct window *w)
16267 struct frame *f = XFRAME (w->frame);
16268 struct glyph_row *bottom_row;
16269 struct it it;
16270 struct run run;
16271 struct text_pos start, new_start;
16272 int nrows_scrolled, i;
16273 struct glyph_row *last_text_row;
16274 struct glyph_row *last_reused_text_row;
16275 struct glyph_row *start_row;
16276 int start_vpos, min_y, max_y;
16278 #ifdef GLYPH_DEBUG
16279 if (inhibit_try_window_reusing)
16280 return 0;
16281 #endif
16283 if (/* This function doesn't handle terminal frames. */
16284 !FRAME_WINDOW_P (f)
16285 /* Don't try to reuse the display if windows have been split
16286 or such. */
16287 || windows_or_buffers_changed
16288 || cursor_type_changed)
16289 return 0;
16291 /* Can't do this if region may have changed. */
16292 if (markpos_of_region () >= 0
16293 || w->region_showing
16294 || !NILP (Vshow_trailing_whitespace))
16295 return 0;
16297 /* If top-line visibility has changed, give up. */
16298 if (WINDOW_WANTS_HEADER_LINE_P (w)
16299 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16300 return 0;
16302 /* Give up if old or new display is scrolled vertically. We could
16303 make this function handle this, but right now it doesn't. */
16304 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16305 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16306 return 0;
16308 /* The variable new_start now holds the new window start. The old
16309 start `start' can be determined from the current matrix. */
16310 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16311 start = start_row->minpos;
16312 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16314 /* Clear the desired matrix for the display below. */
16315 clear_glyph_matrix (w->desired_matrix);
16317 if (CHARPOS (new_start) <= CHARPOS (start))
16319 /* Don't use this method if the display starts with an ellipsis
16320 displayed for invisible text. It's not easy to handle that case
16321 below, and it's certainly not worth the effort since this is
16322 not a frequent case. */
16323 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16324 return 0;
16326 IF_DEBUG (debug_method_add (w, "twu1"));
16328 /* Display up to a row that can be reused. The variable
16329 last_text_row is set to the last row displayed that displays
16330 text. Note that it.vpos == 0 if or if not there is a
16331 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16332 start_display (&it, w, new_start);
16333 w->cursor.vpos = -1;
16334 last_text_row = last_reused_text_row = NULL;
16336 while (it.current_y < it.last_visible_y
16337 && !fonts_changed_p)
16339 /* If we have reached into the characters in the START row,
16340 that means the line boundaries have changed. So we
16341 can't start copying with the row START. Maybe it will
16342 work to start copying with the following row. */
16343 while (IT_CHARPOS (it) > CHARPOS (start))
16345 /* Advance to the next row as the "start". */
16346 start_row++;
16347 start = start_row->minpos;
16348 /* If there are no more rows to try, or just one, give up. */
16349 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16350 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16351 || CHARPOS (start) == ZV)
16353 clear_glyph_matrix (w->desired_matrix);
16354 return 0;
16357 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16359 /* If we have reached alignment, we can copy the rest of the
16360 rows. */
16361 if (IT_CHARPOS (it) == CHARPOS (start)
16362 /* Don't accept "alignment" inside a display vector,
16363 since start_row could have started in the middle of
16364 that same display vector (thus their character
16365 positions match), and we have no way of telling if
16366 that is the case. */
16367 && it.current.dpvec_index < 0)
16368 break;
16370 if (display_line (&it))
16371 last_text_row = it.glyph_row - 1;
16375 /* A value of current_y < last_visible_y means that we stopped
16376 at the previous window start, which in turn means that we
16377 have at least one reusable row. */
16378 if (it.current_y < it.last_visible_y)
16380 struct glyph_row *row;
16382 /* IT.vpos always starts from 0; it counts text lines. */
16383 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16385 /* Find PT if not already found in the lines displayed. */
16386 if (w->cursor.vpos < 0)
16388 int dy = it.current_y - start_row->y;
16390 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16391 row = row_containing_pos (w, PT, row, NULL, dy);
16392 if (row)
16393 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16394 dy, nrows_scrolled);
16395 else
16397 clear_glyph_matrix (w->desired_matrix);
16398 return 0;
16402 /* Scroll the display. Do it before the current matrix is
16403 changed. The problem here is that update has not yet
16404 run, i.e. part of the current matrix is not up to date.
16405 scroll_run_hook will clear the cursor, and use the
16406 current matrix to get the height of the row the cursor is
16407 in. */
16408 run.current_y = start_row->y;
16409 run.desired_y = it.current_y;
16410 run.height = it.last_visible_y - it.current_y;
16412 if (run.height > 0 && run.current_y != run.desired_y)
16414 update_begin (f);
16415 FRAME_RIF (f)->update_window_begin_hook (w);
16416 FRAME_RIF (f)->clear_window_mouse_face (w);
16417 FRAME_RIF (f)->scroll_run_hook (w, &run);
16418 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16419 update_end (f);
16422 /* Shift current matrix down by nrows_scrolled lines. */
16423 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16424 rotate_matrix (w->current_matrix,
16425 start_vpos,
16426 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16427 nrows_scrolled);
16429 /* Disable lines that must be updated. */
16430 for (i = 0; i < nrows_scrolled; ++i)
16431 (start_row + i)->enabled_p = 0;
16433 /* Re-compute Y positions. */
16434 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16435 max_y = it.last_visible_y;
16436 for (row = start_row + nrows_scrolled;
16437 row < bottom_row;
16438 ++row)
16440 row->y = it.current_y;
16441 row->visible_height = row->height;
16443 if (row->y < min_y)
16444 row->visible_height -= min_y - row->y;
16445 if (row->y + row->height > max_y)
16446 row->visible_height -= row->y + row->height - max_y;
16447 if (row->fringe_bitmap_periodic_p)
16448 row->redraw_fringe_bitmaps_p = 1;
16450 it.current_y += row->height;
16452 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16453 last_reused_text_row = row;
16454 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16455 break;
16458 /* Disable lines in the current matrix which are now
16459 below the window. */
16460 for (++row; row < bottom_row; ++row)
16461 row->enabled_p = row->mode_line_p = 0;
16464 /* Update window_end_pos etc.; last_reused_text_row is the last
16465 reused row from the current matrix containing text, if any.
16466 The value of last_text_row is the last displayed line
16467 containing text. */
16468 if (last_reused_text_row)
16470 w->window_end_bytepos
16471 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16472 wset_window_end_pos
16473 (w, make_number (Z
16474 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16475 wset_window_end_vpos
16476 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16477 w->current_matrix)));
16479 else if (last_text_row)
16481 w->window_end_bytepos
16482 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16483 wset_window_end_pos
16484 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16485 wset_window_end_vpos
16486 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16487 w->desired_matrix)));
16489 else
16491 /* This window must be completely empty. */
16492 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16493 wset_window_end_pos (w, make_number (Z - ZV));
16494 wset_window_end_vpos (w, make_number (0));
16496 w->window_end_valid = 0;
16498 /* Update hint: don't try scrolling again in update_window. */
16499 w->desired_matrix->no_scrolling_p = 1;
16501 #ifdef GLYPH_DEBUG
16502 debug_method_add (w, "try_window_reusing_current_matrix 1");
16503 #endif
16504 return 1;
16506 else if (CHARPOS (new_start) > CHARPOS (start))
16508 struct glyph_row *pt_row, *row;
16509 struct glyph_row *first_reusable_row;
16510 struct glyph_row *first_row_to_display;
16511 int dy;
16512 int yb = window_text_bottom_y (w);
16514 /* Find the row starting at new_start, if there is one. Don't
16515 reuse a partially visible line at the end. */
16516 first_reusable_row = start_row;
16517 while (first_reusable_row->enabled_p
16518 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16519 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16520 < CHARPOS (new_start)))
16521 ++first_reusable_row;
16523 /* Give up if there is no row to reuse. */
16524 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16525 || !first_reusable_row->enabled_p
16526 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16527 != CHARPOS (new_start)))
16528 return 0;
16530 /* We can reuse fully visible rows beginning with
16531 first_reusable_row to the end of the window. Set
16532 first_row_to_display to the first row that cannot be reused.
16533 Set pt_row to the row containing point, if there is any. */
16534 pt_row = NULL;
16535 for (first_row_to_display = first_reusable_row;
16536 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16537 ++first_row_to_display)
16539 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16540 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16541 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16542 && first_row_to_display->ends_at_zv_p
16543 && pt_row == NULL)))
16544 pt_row = first_row_to_display;
16547 /* Start displaying at the start of first_row_to_display. */
16548 eassert (first_row_to_display->y < yb);
16549 init_to_row_start (&it, w, first_row_to_display);
16551 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16552 - start_vpos);
16553 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16554 - nrows_scrolled);
16555 it.current_y = (first_row_to_display->y - first_reusable_row->y
16556 + WINDOW_HEADER_LINE_HEIGHT (w));
16558 /* Display lines beginning with first_row_to_display in the
16559 desired matrix. Set last_text_row to the last row displayed
16560 that displays text. */
16561 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16562 if (pt_row == NULL)
16563 w->cursor.vpos = -1;
16564 last_text_row = NULL;
16565 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16566 if (display_line (&it))
16567 last_text_row = it.glyph_row - 1;
16569 /* If point is in a reused row, adjust y and vpos of the cursor
16570 position. */
16571 if (pt_row)
16573 w->cursor.vpos -= nrows_scrolled;
16574 w->cursor.y -= first_reusable_row->y - start_row->y;
16577 /* Give up if point isn't in a row displayed or reused. (This
16578 also handles the case where w->cursor.vpos < nrows_scrolled
16579 after the calls to display_line, which can happen with scroll
16580 margins. See bug#1295.) */
16581 if (w->cursor.vpos < 0)
16583 clear_glyph_matrix (w->desired_matrix);
16584 return 0;
16587 /* Scroll the display. */
16588 run.current_y = first_reusable_row->y;
16589 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16590 run.height = it.last_visible_y - run.current_y;
16591 dy = run.current_y - run.desired_y;
16593 if (run.height)
16595 update_begin (f);
16596 FRAME_RIF (f)->update_window_begin_hook (w);
16597 FRAME_RIF (f)->clear_window_mouse_face (w);
16598 FRAME_RIF (f)->scroll_run_hook (w, &run);
16599 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16600 update_end (f);
16603 /* Adjust Y positions of reused rows. */
16604 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16605 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16606 max_y = it.last_visible_y;
16607 for (row = first_reusable_row; row < first_row_to_display; ++row)
16609 row->y -= dy;
16610 row->visible_height = row->height;
16611 if (row->y < min_y)
16612 row->visible_height -= min_y - row->y;
16613 if (row->y + row->height > max_y)
16614 row->visible_height -= row->y + row->height - max_y;
16615 if (row->fringe_bitmap_periodic_p)
16616 row->redraw_fringe_bitmaps_p = 1;
16619 /* Scroll the current matrix. */
16620 eassert (nrows_scrolled > 0);
16621 rotate_matrix (w->current_matrix,
16622 start_vpos,
16623 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16624 -nrows_scrolled);
16626 /* Disable rows not reused. */
16627 for (row -= nrows_scrolled; row < bottom_row; ++row)
16628 row->enabled_p = 0;
16630 /* Point may have moved to a different line, so we cannot assume that
16631 the previous cursor position is valid; locate the correct row. */
16632 if (pt_row)
16634 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16635 row < bottom_row
16636 && PT >= MATRIX_ROW_END_CHARPOS (row)
16637 && !row->ends_at_zv_p;
16638 row++)
16640 w->cursor.vpos++;
16641 w->cursor.y = row->y;
16643 if (row < bottom_row)
16645 /* Can't simply scan the row for point with
16646 bidi-reordered glyph rows. Let set_cursor_from_row
16647 figure out where to put the cursor, and if it fails,
16648 give up. */
16649 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16651 if (!set_cursor_from_row (w, row, w->current_matrix,
16652 0, 0, 0, 0))
16654 clear_glyph_matrix (w->desired_matrix);
16655 return 0;
16658 else
16660 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16661 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16663 for (; glyph < end
16664 && (!BUFFERP (glyph->object)
16665 || glyph->charpos < PT);
16666 glyph++)
16668 w->cursor.hpos++;
16669 w->cursor.x += glyph->pixel_width;
16675 /* Adjust window end. A null value of last_text_row means that
16676 the window end is in reused rows which in turn means that
16677 only its vpos can have changed. */
16678 if (last_text_row)
16680 w->window_end_bytepos
16681 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16682 wset_window_end_pos
16683 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16684 wset_window_end_vpos
16685 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16686 w->desired_matrix)));
16688 else
16690 wset_window_end_vpos
16691 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16694 w->window_end_valid = 0;
16695 w->desired_matrix->no_scrolling_p = 1;
16697 #ifdef GLYPH_DEBUG
16698 debug_method_add (w, "try_window_reusing_current_matrix 2");
16699 #endif
16700 return 1;
16703 return 0;
16708 /************************************************************************
16709 Window redisplay reusing current matrix when buffer has changed
16710 ************************************************************************/
16712 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16713 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16714 ptrdiff_t *, ptrdiff_t *);
16715 static struct glyph_row *
16716 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16717 struct glyph_row *);
16720 /* Return the last row in MATRIX displaying text. If row START is
16721 non-null, start searching with that row. IT gives the dimensions
16722 of the display. Value is null if matrix is empty; otherwise it is
16723 a pointer to the row found. */
16725 static struct glyph_row *
16726 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16727 struct glyph_row *start)
16729 struct glyph_row *row, *row_found;
16731 /* Set row_found to the last row in IT->w's current matrix
16732 displaying text. The loop looks funny but think of partially
16733 visible lines. */
16734 row_found = NULL;
16735 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16736 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16738 eassert (row->enabled_p);
16739 row_found = row;
16740 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16741 break;
16742 ++row;
16745 return row_found;
16749 /* Return the last row in the current matrix of W that is not affected
16750 by changes at the start of current_buffer that occurred since W's
16751 current matrix was built. Value is null if no such row exists.
16753 BEG_UNCHANGED us the number of characters unchanged at the start of
16754 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16755 first changed character in current_buffer. Characters at positions <
16756 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16757 when the current matrix was built. */
16759 static struct glyph_row *
16760 find_last_unchanged_at_beg_row (struct window *w)
16762 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16763 struct glyph_row *row;
16764 struct glyph_row *row_found = NULL;
16765 int yb = window_text_bottom_y (w);
16767 /* Find the last row displaying unchanged text. */
16768 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16769 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16770 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16771 ++row)
16773 if (/* If row ends before first_changed_pos, it is unchanged,
16774 except in some case. */
16775 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16776 /* When row ends in ZV and we write at ZV it is not
16777 unchanged. */
16778 && !row->ends_at_zv_p
16779 /* When first_changed_pos is the end of a continued line,
16780 row is not unchanged because it may be no longer
16781 continued. */
16782 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16783 && (row->continued_p
16784 || row->exact_window_width_line_p))
16785 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16786 needs to be recomputed, so don't consider this row as
16787 unchanged. This happens when the last line was
16788 bidi-reordered and was killed immediately before this
16789 redisplay cycle. In that case, ROW->end stores the
16790 buffer position of the first visual-order character of
16791 the killed text, which is now beyond ZV. */
16792 && CHARPOS (row->end.pos) <= ZV)
16793 row_found = row;
16795 /* Stop if last visible row. */
16796 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16797 break;
16800 return row_found;
16804 /* Find the first glyph row in the current matrix of W that is not
16805 affected by changes at the end of current_buffer since the
16806 time W's current matrix was built.
16808 Return in *DELTA the number of chars by which buffer positions in
16809 unchanged text at the end of current_buffer must be adjusted.
16811 Return in *DELTA_BYTES the corresponding number of bytes.
16813 Value is null if no such row exists, i.e. all rows are affected by
16814 changes. */
16816 static struct glyph_row *
16817 find_first_unchanged_at_end_row (struct window *w,
16818 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16820 struct glyph_row *row;
16821 struct glyph_row *row_found = NULL;
16823 *delta = *delta_bytes = 0;
16825 /* Display must not have been paused, otherwise the current matrix
16826 is not up to date. */
16827 eassert (w->window_end_valid);
16829 /* A value of window_end_pos >= END_UNCHANGED means that the window
16830 end is in the range of changed text. If so, there is no
16831 unchanged row at the end of W's current matrix. */
16832 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16833 return NULL;
16835 /* Set row to the last row in W's current matrix displaying text. */
16836 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16838 /* If matrix is entirely empty, no unchanged row exists. */
16839 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16841 /* The value of row is the last glyph row in the matrix having a
16842 meaningful buffer position in it. The end position of row
16843 corresponds to window_end_pos. This allows us to translate
16844 buffer positions in the current matrix to current buffer
16845 positions for characters not in changed text. */
16846 ptrdiff_t Z_old =
16847 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16848 ptrdiff_t Z_BYTE_old =
16849 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16850 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16851 struct glyph_row *first_text_row
16852 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16854 *delta = Z - Z_old;
16855 *delta_bytes = Z_BYTE - Z_BYTE_old;
16857 /* Set last_unchanged_pos to the buffer position of the last
16858 character in the buffer that has not been changed. Z is the
16859 index + 1 of the last character in current_buffer, i.e. by
16860 subtracting END_UNCHANGED we get the index of the last
16861 unchanged character, and we have to add BEG to get its buffer
16862 position. */
16863 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16864 last_unchanged_pos_old = last_unchanged_pos - *delta;
16866 /* Search backward from ROW for a row displaying a line that
16867 starts at a minimum position >= last_unchanged_pos_old. */
16868 for (; row > first_text_row; --row)
16870 /* This used to abort, but it can happen.
16871 It is ok to just stop the search instead here. KFS. */
16872 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16873 break;
16875 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16876 row_found = row;
16880 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16882 return row_found;
16886 /* Make sure that glyph rows in the current matrix of window W
16887 reference the same glyph memory as corresponding rows in the
16888 frame's frame matrix. This function is called after scrolling W's
16889 current matrix on a terminal frame in try_window_id and
16890 try_window_reusing_current_matrix. */
16892 static void
16893 sync_frame_with_window_matrix_rows (struct window *w)
16895 struct frame *f = XFRAME (w->frame);
16896 struct glyph_row *window_row, *window_row_end, *frame_row;
16898 /* Preconditions: W must be a leaf window and full-width. Its frame
16899 must have a frame matrix. */
16900 eassert (BUFFERP (w->contents));
16901 eassert (WINDOW_FULL_WIDTH_P (w));
16902 eassert (!FRAME_WINDOW_P (f));
16904 /* If W is a full-width window, glyph pointers in W's current matrix
16905 have, by definition, to be the same as glyph pointers in the
16906 corresponding frame matrix. Note that frame matrices have no
16907 marginal areas (see build_frame_matrix). */
16908 window_row = w->current_matrix->rows;
16909 window_row_end = window_row + w->current_matrix->nrows;
16910 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16911 while (window_row < window_row_end)
16913 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16914 struct glyph *end = window_row->glyphs[LAST_AREA];
16916 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16917 frame_row->glyphs[TEXT_AREA] = start;
16918 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16919 frame_row->glyphs[LAST_AREA] = end;
16921 /* Disable frame rows whose corresponding window rows have
16922 been disabled in try_window_id. */
16923 if (!window_row->enabled_p)
16924 frame_row->enabled_p = 0;
16926 ++window_row, ++frame_row;
16931 /* Find the glyph row in window W containing CHARPOS. Consider all
16932 rows between START and END (not inclusive). END null means search
16933 all rows to the end of the display area of W. Value is the row
16934 containing CHARPOS or null. */
16936 struct glyph_row *
16937 row_containing_pos (struct window *w, ptrdiff_t charpos,
16938 struct glyph_row *start, struct glyph_row *end, int dy)
16940 struct glyph_row *row = start;
16941 struct glyph_row *best_row = NULL;
16942 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16943 int last_y;
16945 /* If we happen to start on a header-line, skip that. */
16946 if (row->mode_line_p)
16947 ++row;
16949 if ((end && row >= end) || !row->enabled_p)
16950 return NULL;
16952 last_y = window_text_bottom_y (w) - dy;
16954 while (1)
16956 /* Give up if we have gone too far. */
16957 if (end && row >= end)
16958 return NULL;
16959 /* This formerly returned if they were equal.
16960 I think that both quantities are of a "last plus one" type;
16961 if so, when they are equal, the row is within the screen. -- rms. */
16962 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16963 return NULL;
16965 /* If it is in this row, return this row. */
16966 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16967 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16968 /* The end position of a row equals the start
16969 position of the next row. If CHARPOS is there, we
16970 would rather consider it displayed in the next
16971 line, except when this line ends in ZV. */
16972 && !row_for_charpos_p (row, charpos)))
16973 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16975 struct glyph *g;
16977 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
16978 || (!best_row && !row->continued_p))
16979 return row;
16980 /* In bidi-reordered rows, there could be several rows whose
16981 edges surround CHARPOS, all of these rows belonging to
16982 the same continued line. We need to find the row which
16983 fits CHARPOS the best. */
16984 for (g = row->glyphs[TEXT_AREA];
16985 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16986 g++)
16988 if (!STRINGP (g->object))
16990 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16992 mindif = eabs (g->charpos - charpos);
16993 best_row = row;
16994 /* Exact match always wins. */
16995 if (mindif == 0)
16996 return best_row;
17001 else if (best_row && !row->continued_p)
17002 return best_row;
17003 ++row;
17008 /* Try to redisplay window W by reusing its existing display. W's
17009 current matrix must be up to date when this function is called,
17010 i.e. window_end_valid must be nonzero.
17012 Value is
17014 1 if display has been updated
17015 0 if otherwise unsuccessful
17016 -1 if redisplay with same window start is known not to succeed
17018 The following steps are performed:
17020 1. Find the last row in the current matrix of W that is not
17021 affected by changes at the start of current_buffer. If no such row
17022 is found, give up.
17024 2. Find the first row in W's current matrix that is not affected by
17025 changes at the end of current_buffer. Maybe there is no such row.
17027 3. Display lines beginning with the row + 1 found in step 1 to the
17028 row found in step 2 or, if step 2 didn't find a row, to the end of
17029 the window.
17031 4. If cursor is not known to appear on the window, give up.
17033 5. If display stopped at the row found in step 2, scroll the
17034 display and current matrix as needed.
17036 6. Maybe display some lines at the end of W, if we must. This can
17037 happen under various circumstances, like a partially visible line
17038 becoming fully visible, or because newly displayed lines are displayed
17039 in smaller font sizes.
17041 7. Update W's window end information. */
17043 static int
17044 try_window_id (struct window *w)
17046 struct frame *f = XFRAME (w->frame);
17047 struct glyph_matrix *current_matrix = w->current_matrix;
17048 struct glyph_matrix *desired_matrix = w->desired_matrix;
17049 struct glyph_row *last_unchanged_at_beg_row;
17050 struct glyph_row *first_unchanged_at_end_row;
17051 struct glyph_row *row;
17052 struct glyph_row *bottom_row;
17053 int bottom_vpos;
17054 struct it it;
17055 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17056 int dvpos, dy;
17057 struct text_pos start_pos;
17058 struct run run;
17059 int first_unchanged_at_end_vpos = 0;
17060 struct glyph_row *last_text_row, *last_text_row_at_end;
17061 struct text_pos start;
17062 ptrdiff_t first_changed_charpos, last_changed_charpos;
17064 #ifdef GLYPH_DEBUG
17065 if (inhibit_try_window_id)
17066 return 0;
17067 #endif
17069 /* This is handy for debugging. */
17070 #if 0
17071 #define GIVE_UP(X) \
17072 do { \
17073 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17074 return 0; \
17075 } while (0)
17076 #else
17077 #define GIVE_UP(X) return 0
17078 #endif
17080 SET_TEXT_POS_FROM_MARKER (start, w->start);
17082 /* Don't use this for mini-windows because these can show
17083 messages and mini-buffers, and we don't handle that here. */
17084 if (MINI_WINDOW_P (w))
17085 GIVE_UP (1);
17087 /* This flag is used to prevent redisplay optimizations. */
17088 if (windows_or_buffers_changed || cursor_type_changed)
17089 GIVE_UP (2);
17091 /* Verify that narrowing has not changed.
17092 Also verify that we were not told to prevent redisplay optimizations.
17093 It would be nice to further
17094 reduce the number of cases where this prevents try_window_id. */
17095 if (current_buffer->clip_changed
17096 || current_buffer->prevent_redisplay_optimizations_p)
17097 GIVE_UP (3);
17099 /* Window must either use window-based redisplay or be full width. */
17100 if (!FRAME_WINDOW_P (f)
17101 && (!FRAME_LINE_INS_DEL_OK (f)
17102 || !WINDOW_FULL_WIDTH_P (w)))
17103 GIVE_UP (4);
17105 /* Give up if point is known NOT to appear in W. */
17106 if (PT < CHARPOS (start))
17107 GIVE_UP (5);
17109 /* Another way to prevent redisplay optimizations. */
17110 if (w->last_modified == 0)
17111 GIVE_UP (6);
17113 /* Verify that window is not hscrolled. */
17114 if (w->hscroll != 0)
17115 GIVE_UP (7);
17117 /* Verify that display wasn't paused. */
17118 if (!w->window_end_valid)
17119 GIVE_UP (8);
17121 /* Can't use this if highlighting a region because a cursor movement
17122 will do more than just set the cursor. */
17123 if (markpos_of_region () >= 0)
17124 GIVE_UP (9);
17126 /* Likewise if highlighting trailing whitespace. */
17127 if (!NILP (Vshow_trailing_whitespace))
17128 GIVE_UP (11);
17130 /* Likewise if showing a region. */
17131 if (w->region_showing)
17132 GIVE_UP (10);
17134 /* Can't use this if overlay arrow position and/or string have
17135 changed. */
17136 if (overlay_arrows_changed_p ())
17137 GIVE_UP (12);
17139 /* When word-wrap is on, adding a space to the first word of a
17140 wrapped line can change the wrap position, altering the line
17141 above it. It might be worthwhile to handle this more
17142 intelligently, but for now just redisplay from scratch. */
17143 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17144 GIVE_UP (21);
17146 /* Under bidi reordering, adding or deleting a character in the
17147 beginning of a paragraph, before the first strong directional
17148 character, can change the base direction of the paragraph (unless
17149 the buffer specifies a fixed paragraph direction), which will
17150 require to redisplay the whole paragraph. It might be worthwhile
17151 to find the paragraph limits and widen the range of redisplayed
17152 lines to that, but for now just give up this optimization and
17153 redisplay from scratch. */
17154 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17155 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17156 GIVE_UP (22);
17158 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17159 only if buffer has really changed. The reason is that the gap is
17160 initially at Z for freshly visited files. The code below would
17161 set end_unchanged to 0 in that case. */
17162 if (MODIFF > SAVE_MODIFF
17163 /* This seems to happen sometimes after saving a buffer. */
17164 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17166 if (GPT - BEG < BEG_UNCHANGED)
17167 BEG_UNCHANGED = GPT - BEG;
17168 if (Z - GPT < END_UNCHANGED)
17169 END_UNCHANGED = Z - GPT;
17172 /* The position of the first and last character that has been changed. */
17173 first_changed_charpos = BEG + BEG_UNCHANGED;
17174 last_changed_charpos = Z - END_UNCHANGED;
17176 /* If window starts after a line end, and the last change is in
17177 front of that newline, then changes don't affect the display.
17178 This case happens with stealth-fontification. Note that although
17179 the display is unchanged, glyph positions in the matrix have to
17180 be adjusted, of course. */
17181 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17182 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17183 && ((last_changed_charpos < CHARPOS (start)
17184 && CHARPOS (start) == BEGV)
17185 || (last_changed_charpos < CHARPOS (start) - 1
17186 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17188 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17189 struct glyph_row *r0;
17191 /* Compute how many chars/bytes have been added to or removed
17192 from the buffer. */
17193 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17194 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17195 Z_delta = Z - Z_old;
17196 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17198 /* Give up if PT is not in the window. Note that it already has
17199 been checked at the start of try_window_id that PT is not in
17200 front of the window start. */
17201 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17202 GIVE_UP (13);
17204 /* If window start is unchanged, we can reuse the whole matrix
17205 as is, after adjusting glyph positions. No need to compute
17206 the window end again, since its offset from Z hasn't changed. */
17207 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17208 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17209 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17210 /* PT must not be in a partially visible line. */
17211 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17212 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17214 /* Adjust positions in the glyph matrix. */
17215 if (Z_delta || Z_delta_bytes)
17217 struct glyph_row *r1
17218 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17219 increment_matrix_positions (w->current_matrix,
17220 MATRIX_ROW_VPOS (r0, current_matrix),
17221 MATRIX_ROW_VPOS (r1, current_matrix),
17222 Z_delta, Z_delta_bytes);
17225 /* Set the cursor. */
17226 row = row_containing_pos (w, PT, r0, NULL, 0);
17227 if (row)
17228 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17229 else
17230 emacs_abort ();
17231 return 1;
17235 /* Handle the case that changes are all below what is displayed in
17236 the window, and that PT is in the window. This shortcut cannot
17237 be taken if ZV is visible in the window, and text has been added
17238 there that is visible in the window. */
17239 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17240 /* ZV is not visible in the window, or there are no
17241 changes at ZV, actually. */
17242 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17243 || first_changed_charpos == last_changed_charpos))
17245 struct glyph_row *r0;
17247 /* Give up if PT is not in the window. Note that it already has
17248 been checked at the start of try_window_id that PT is not in
17249 front of the window start. */
17250 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17251 GIVE_UP (14);
17253 /* If window start is unchanged, we can reuse the whole matrix
17254 as is, without changing glyph positions since no text has
17255 been added/removed in front of the window end. */
17256 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17257 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17258 /* PT must not be in a partially visible line. */
17259 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17260 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17262 /* We have to compute the window end anew since text
17263 could have been added/removed after it. */
17264 wset_window_end_pos
17265 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17266 w->window_end_bytepos
17267 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17269 /* Set the cursor. */
17270 row = row_containing_pos (w, PT, r0, NULL, 0);
17271 if (row)
17272 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17273 else
17274 emacs_abort ();
17275 return 2;
17279 /* Give up if window start is in the changed area.
17281 The condition used to read
17283 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17285 but why that was tested escapes me at the moment. */
17286 if (CHARPOS (start) >= first_changed_charpos
17287 && CHARPOS (start) <= last_changed_charpos)
17288 GIVE_UP (15);
17290 /* Check that window start agrees with the start of the first glyph
17291 row in its current matrix. Check this after we know the window
17292 start is not in changed text, otherwise positions would not be
17293 comparable. */
17294 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17295 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17296 GIVE_UP (16);
17298 /* Give up if the window ends in strings. Overlay strings
17299 at the end are difficult to handle, so don't try. */
17300 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17301 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17302 GIVE_UP (20);
17304 /* Compute the position at which we have to start displaying new
17305 lines. Some of the lines at the top of the window might be
17306 reusable because they are not displaying changed text. Find the
17307 last row in W's current matrix not affected by changes at the
17308 start of current_buffer. Value is null if changes start in the
17309 first line of window. */
17310 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17311 if (last_unchanged_at_beg_row)
17313 /* Avoid starting to display in the middle of a character, a TAB
17314 for instance. This is easier than to set up the iterator
17315 exactly, and it's not a frequent case, so the additional
17316 effort wouldn't really pay off. */
17317 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17318 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17319 && last_unchanged_at_beg_row > w->current_matrix->rows)
17320 --last_unchanged_at_beg_row;
17322 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17323 GIVE_UP (17);
17325 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17326 GIVE_UP (18);
17327 start_pos = it.current.pos;
17329 /* Start displaying new lines in the desired matrix at the same
17330 vpos we would use in the current matrix, i.e. below
17331 last_unchanged_at_beg_row. */
17332 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17333 current_matrix);
17334 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17335 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17337 eassert (it.hpos == 0 && it.current_x == 0);
17339 else
17341 /* There are no reusable lines at the start of the window.
17342 Start displaying in the first text line. */
17343 start_display (&it, w, start);
17344 it.vpos = it.first_vpos;
17345 start_pos = it.current.pos;
17348 /* Find the first row that is not affected by changes at the end of
17349 the buffer. Value will be null if there is no unchanged row, in
17350 which case we must redisplay to the end of the window. delta
17351 will be set to the value by which buffer positions beginning with
17352 first_unchanged_at_end_row have to be adjusted due to text
17353 changes. */
17354 first_unchanged_at_end_row
17355 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17356 IF_DEBUG (debug_delta = delta);
17357 IF_DEBUG (debug_delta_bytes = delta_bytes);
17359 /* Set stop_pos to the buffer position up to which we will have to
17360 display new lines. If first_unchanged_at_end_row != NULL, this
17361 is the buffer position of the start of the line displayed in that
17362 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17363 that we don't stop at a buffer position. */
17364 stop_pos = 0;
17365 if (first_unchanged_at_end_row)
17367 eassert (last_unchanged_at_beg_row == NULL
17368 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17370 /* If this is a continuation line, move forward to the next one
17371 that isn't. Changes in lines above affect this line.
17372 Caution: this may move first_unchanged_at_end_row to a row
17373 not displaying text. */
17374 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17375 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17376 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17377 < it.last_visible_y))
17378 ++first_unchanged_at_end_row;
17380 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17381 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17382 >= it.last_visible_y))
17383 first_unchanged_at_end_row = NULL;
17384 else
17386 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17387 + delta);
17388 first_unchanged_at_end_vpos
17389 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17390 eassert (stop_pos >= Z - END_UNCHANGED);
17393 else if (last_unchanged_at_beg_row == NULL)
17394 GIVE_UP (19);
17397 #ifdef GLYPH_DEBUG
17399 /* Either there is no unchanged row at the end, or the one we have
17400 now displays text. This is a necessary condition for the window
17401 end pos calculation at the end of this function. */
17402 eassert (first_unchanged_at_end_row == NULL
17403 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17405 debug_last_unchanged_at_beg_vpos
17406 = (last_unchanged_at_beg_row
17407 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17408 : -1);
17409 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17411 #endif /* GLYPH_DEBUG */
17414 /* Display new lines. Set last_text_row to the last new line
17415 displayed which has text on it, i.e. might end up as being the
17416 line where the window_end_vpos is. */
17417 w->cursor.vpos = -1;
17418 last_text_row = NULL;
17419 overlay_arrow_seen = 0;
17420 while (it.current_y < it.last_visible_y
17421 && !fonts_changed_p
17422 && (first_unchanged_at_end_row == NULL
17423 || IT_CHARPOS (it) < stop_pos))
17425 if (display_line (&it))
17426 last_text_row = it.glyph_row - 1;
17429 if (fonts_changed_p)
17430 return -1;
17433 /* Compute differences in buffer positions, y-positions etc. for
17434 lines reused at the bottom of the window. Compute what we can
17435 scroll. */
17436 if (first_unchanged_at_end_row
17437 /* No lines reused because we displayed everything up to the
17438 bottom of the window. */
17439 && it.current_y < it.last_visible_y)
17441 dvpos = (it.vpos
17442 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17443 current_matrix));
17444 dy = it.current_y - first_unchanged_at_end_row->y;
17445 run.current_y = first_unchanged_at_end_row->y;
17446 run.desired_y = run.current_y + dy;
17447 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17449 else
17451 delta = delta_bytes = dvpos = dy
17452 = run.current_y = run.desired_y = run.height = 0;
17453 first_unchanged_at_end_row = NULL;
17455 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17458 /* Find the cursor if not already found. We have to decide whether
17459 PT will appear on this window (it sometimes doesn't, but this is
17460 not a very frequent case.) This decision has to be made before
17461 the current matrix is altered. A value of cursor.vpos < 0 means
17462 that PT is either in one of the lines beginning at
17463 first_unchanged_at_end_row or below the window. Don't care for
17464 lines that might be displayed later at the window end; as
17465 mentioned, this is not a frequent case. */
17466 if (w->cursor.vpos < 0)
17468 /* Cursor in unchanged rows at the top? */
17469 if (PT < CHARPOS (start_pos)
17470 && last_unchanged_at_beg_row)
17472 row = row_containing_pos (w, PT,
17473 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17474 last_unchanged_at_beg_row + 1, 0);
17475 if (row)
17476 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17479 /* Start from first_unchanged_at_end_row looking for PT. */
17480 else if (first_unchanged_at_end_row)
17482 row = row_containing_pos (w, PT - delta,
17483 first_unchanged_at_end_row, NULL, 0);
17484 if (row)
17485 set_cursor_from_row (w, row, w->current_matrix, delta,
17486 delta_bytes, dy, dvpos);
17489 /* Give up if cursor was not found. */
17490 if (w->cursor.vpos < 0)
17492 clear_glyph_matrix (w->desired_matrix);
17493 return -1;
17497 /* Don't let the cursor end in the scroll margins. */
17499 int this_scroll_margin, cursor_height;
17501 this_scroll_margin =
17502 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17503 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17504 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17506 if ((w->cursor.y < this_scroll_margin
17507 && CHARPOS (start) > BEGV)
17508 /* Old redisplay didn't take scroll margin into account at the bottom,
17509 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17510 || (w->cursor.y + (make_cursor_line_fully_visible_p
17511 ? cursor_height + this_scroll_margin
17512 : 1)) > it.last_visible_y)
17514 w->cursor.vpos = -1;
17515 clear_glyph_matrix (w->desired_matrix);
17516 return -1;
17520 /* Scroll the display. Do it before changing the current matrix so
17521 that xterm.c doesn't get confused about where the cursor glyph is
17522 found. */
17523 if (dy && run.height)
17525 update_begin (f);
17527 if (FRAME_WINDOW_P (f))
17529 FRAME_RIF (f)->update_window_begin_hook (w);
17530 FRAME_RIF (f)->clear_window_mouse_face (w);
17531 FRAME_RIF (f)->scroll_run_hook (w, &run);
17532 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17534 else
17536 /* Terminal frame. In this case, dvpos gives the number of
17537 lines to scroll by; dvpos < 0 means scroll up. */
17538 int from_vpos
17539 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17540 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17541 int end = (WINDOW_TOP_EDGE_LINE (w)
17542 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17543 + window_internal_height (w));
17545 #if defined (HAVE_GPM) || defined (MSDOS)
17546 x_clear_window_mouse_face (w);
17547 #endif
17548 /* Perform the operation on the screen. */
17549 if (dvpos > 0)
17551 /* Scroll last_unchanged_at_beg_row to the end of the
17552 window down dvpos lines. */
17553 set_terminal_window (f, end);
17555 /* On dumb terminals delete dvpos lines at the end
17556 before inserting dvpos empty lines. */
17557 if (!FRAME_SCROLL_REGION_OK (f))
17558 ins_del_lines (f, end - dvpos, -dvpos);
17560 /* Insert dvpos empty lines in front of
17561 last_unchanged_at_beg_row. */
17562 ins_del_lines (f, from, dvpos);
17564 else if (dvpos < 0)
17566 /* Scroll up last_unchanged_at_beg_vpos to the end of
17567 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17568 set_terminal_window (f, end);
17570 /* Delete dvpos lines in front of
17571 last_unchanged_at_beg_vpos. ins_del_lines will set
17572 the cursor to the given vpos and emit |dvpos| delete
17573 line sequences. */
17574 ins_del_lines (f, from + dvpos, dvpos);
17576 /* On a dumb terminal insert dvpos empty lines at the
17577 end. */
17578 if (!FRAME_SCROLL_REGION_OK (f))
17579 ins_del_lines (f, end + dvpos, -dvpos);
17582 set_terminal_window (f, 0);
17585 update_end (f);
17588 /* Shift reused rows of the current matrix to the right position.
17589 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17590 text. */
17591 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17592 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17593 if (dvpos < 0)
17595 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17596 bottom_vpos, dvpos);
17597 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17598 bottom_vpos);
17600 else if (dvpos > 0)
17602 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17603 bottom_vpos, dvpos);
17604 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17605 first_unchanged_at_end_vpos + dvpos);
17608 /* For frame-based redisplay, make sure that current frame and window
17609 matrix are in sync with respect to glyph memory. */
17610 if (!FRAME_WINDOW_P (f))
17611 sync_frame_with_window_matrix_rows (w);
17613 /* Adjust buffer positions in reused rows. */
17614 if (delta || delta_bytes)
17615 increment_matrix_positions (current_matrix,
17616 first_unchanged_at_end_vpos + dvpos,
17617 bottom_vpos, delta, delta_bytes);
17619 /* Adjust Y positions. */
17620 if (dy)
17621 shift_glyph_matrix (w, current_matrix,
17622 first_unchanged_at_end_vpos + dvpos,
17623 bottom_vpos, dy);
17625 if (first_unchanged_at_end_row)
17627 first_unchanged_at_end_row += dvpos;
17628 if (first_unchanged_at_end_row->y >= it.last_visible_y
17629 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17630 first_unchanged_at_end_row = NULL;
17633 /* If scrolling up, there may be some lines to display at the end of
17634 the window. */
17635 last_text_row_at_end = NULL;
17636 if (dy < 0)
17638 /* Scrolling up can leave for example a partially visible line
17639 at the end of the window to be redisplayed. */
17640 /* Set last_row to the glyph row in the current matrix where the
17641 window end line is found. It has been moved up or down in
17642 the matrix by dvpos. */
17643 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17644 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17646 /* If last_row is the window end line, it should display text. */
17647 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17649 /* If window end line was partially visible before, begin
17650 displaying at that line. Otherwise begin displaying with the
17651 line following it. */
17652 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17654 init_to_row_start (&it, w, last_row);
17655 it.vpos = last_vpos;
17656 it.current_y = last_row->y;
17658 else
17660 init_to_row_end (&it, w, last_row);
17661 it.vpos = 1 + last_vpos;
17662 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17663 ++last_row;
17666 /* We may start in a continuation line. If so, we have to
17667 get the right continuation_lines_width and current_x. */
17668 it.continuation_lines_width = last_row->continuation_lines_width;
17669 it.hpos = it.current_x = 0;
17671 /* Display the rest of the lines at the window end. */
17672 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17673 while (it.current_y < it.last_visible_y
17674 && !fonts_changed_p)
17676 /* Is it always sure that the display agrees with lines in
17677 the current matrix? I don't think so, so we mark rows
17678 displayed invalid in the current matrix by setting their
17679 enabled_p flag to zero. */
17680 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17681 if (display_line (&it))
17682 last_text_row_at_end = it.glyph_row - 1;
17686 /* Update window_end_pos and window_end_vpos. */
17687 if (first_unchanged_at_end_row
17688 && !last_text_row_at_end)
17690 /* Window end line if one of the preserved rows from the current
17691 matrix. Set row to the last row displaying text in current
17692 matrix starting at first_unchanged_at_end_row, after
17693 scrolling. */
17694 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17695 row = find_last_row_displaying_text (w->current_matrix, &it,
17696 first_unchanged_at_end_row);
17697 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17699 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17700 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17701 wset_window_end_vpos
17702 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17703 eassert (w->window_end_bytepos >= 0);
17704 IF_DEBUG (debug_method_add (w, "A"));
17706 else if (last_text_row_at_end)
17708 wset_window_end_pos
17709 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17710 w->window_end_bytepos
17711 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17712 wset_window_end_vpos
17713 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17714 desired_matrix)));
17715 eassert (w->window_end_bytepos >= 0);
17716 IF_DEBUG (debug_method_add (w, "B"));
17718 else if (last_text_row)
17720 /* We have displayed either to the end of the window or at the
17721 end of the window, i.e. the last row with text is to be found
17722 in the desired matrix. */
17723 wset_window_end_pos
17724 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17725 w->window_end_bytepos
17726 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17727 wset_window_end_vpos
17728 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17729 eassert (w->window_end_bytepos >= 0);
17731 else if (first_unchanged_at_end_row == NULL
17732 && last_text_row == NULL
17733 && last_text_row_at_end == NULL)
17735 /* Displayed to end of window, but no line containing text was
17736 displayed. Lines were deleted at the end of the window. */
17737 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17738 int vpos = XFASTINT (w->window_end_vpos);
17739 struct glyph_row *current_row = current_matrix->rows + vpos;
17740 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17742 for (row = NULL;
17743 row == NULL && vpos >= first_vpos;
17744 --vpos, --current_row, --desired_row)
17746 if (desired_row->enabled_p)
17748 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17749 row = desired_row;
17751 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17752 row = current_row;
17755 eassert (row != NULL);
17756 wset_window_end_vpos (w, make_number (vpos + 1));
17757 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17758 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17759 eassert (w->window_end_bytepos >= 0);
17760 IF_DEBUG (debug_method_add (w, "C"));
17762 else
17763 emacs_abort ();
17765 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17766 debug_end_vpos = XFASTINT (w->window_end_vpos));
17768 /* Record that display has not been completed. */
17769 w->window_end_valid = 0;
17770 w->desired_matrix->no_scrolling_p = 1;
17771 return 3;
17773 #undef GIVE_UP
17778 /***********************************************************************
17779 More debugging support
17780 ***********************************************************************/
17782 #ifdef GLYPH_DEBUG
17784 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17785 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17786 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17789 /* Dump the contents of glyph matrix MATRIX on stderr.
17791 GLYPHS 0 means don't show glyph contents.
17792 GLYPHS 1 means show glyphs in short form
17793 GLYPHS > 1 means show glyphs in long form. */
17795 void
17796 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17798 int i;
17799 for (i = 0; i < matrix->nrows; ++i)
17800 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17804 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17805 the glyph row and area where the glyph comes from. */
17807 void
17808 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17810 if (glyph->type == CHAR_GLYPH
17811 || glyph->type == GLYPHLESS_GLYPH)
17813 fprintf (stderr,
17814 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17815 glyph - row->glyphs[TEXT_AREA],
17816 (glyph->type == CHAR_GLYPH
17817 ? 'C'
17818 : 'G'),
17819 glyph->charpos,
17820 (BUFFERP (glyph->object)
17821 ? 'B'
17822 : (STRINGP (glyph->object)
17823 ? 'S'
17824 : (INTEGERP (glyph->object)
17825 ? '0'
17826 : '-'))),
17827 glyph->pixel_width,
17828 glyph->u.ch,
17829 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17830 ? glyph->u.ch
17831 : '.'),
17832 glyph->face_id,
17833 glyph->left_box_line_p,
17834 glyph->right_box_line_p);
17836 else if (glyph->type == STRETCH_GLYPH)
17838 fprintf (stderr,
17839 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17840 glyph - row->glyphs[TEXT_AREA],
17841 'S',
17842 glyph->charpos,
17843 (BUFFERP (glyph->object)
17844 ? 'B'
17845 : (STRINGP (glyph->object)
17846 ? 'S'
17847 : (INTEGERP (glyph->object)
17848 ? '0'
17849 : '-'))),
17850 glyph->pixel_width,
17852 ' ',
17853 glyph->face_id,
17854 glyph->left_box_line_p,
17855 glyph->right_box_line_p);
17857 else if (glyph->type == IMAGE_GLYPH)
17859 fprintf (stderr,
17860 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17861 glyph - row->glyphs[TEXT_AREA],
17862 'I',
17863 glyph->charpos,
17864 (BUFFERP (glyph->object)
17865 ? 'B'
17866 : (STRINGP (glyph->object)
17867 ? 'S'
17868 : (INTEGERP (glyph->object)
17869 ? '0'
17870 : '-'))),
17871 glyph->pixel_width,
17872 glyph->u.img_id,
17873 '.',
17874 glyph->face_id,
17875 glyph->left_box_line_p,
17876 glyph->right_box_line_p);
17878 else if (glyph->type == COMPOSITE_GLYPH)
17880 fprintf (stderr,
17881 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17882 glyph - row->glyphs[TEXT_AREA],
17883 '+',
17884 glyph->charpos,
17885 (BUFFERP (glyph->object)
17886 ? 'B'
17887 : (STRINGP (glyph->object)
17888 ? 'S'
17889 : (INTEGERP (glyph->object)
17890 ? '0'
17891 : '-'))),
17892 glyph->pixel_width,
17893 glyph->u.cmp.id);
17894 if (glyph->u.cmp.automatic)
17895 fprintf (stderr,
17896 "[%d-%d]",
17897 glyph->slice.cmp.from, glyph->slice.cmp.to);
17898 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17899 glyph->face_id,
17900 glyph->left_box_line_p,
17901 glyph->right_box_line_p);
17906 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17907 GLYPHS 0 means don't show glyph contents.
17908 GLYPHS 1 means show glyphs in short form
17909 GLYPHS > 1 means show glyphs in long form. */
17911 void
17912 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17914 if (glyphs != 1)
17916 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17917 fprintf (stderr, "==============================================================================\n");
17919 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17920 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17921 vpos,
17922 MATRIX_ROW_START_CHARPOS (row),
17923 MATRIX_ROW_END_CHARPOS (row),
17924 row->used[TEXT_AREA],
17925 row->contains_overlapping_glyphs_p,
17926 row->enabled_p,
17927 row->truncated_on_left_p,
17928 row->truncated_on_right_p,
17929 row->continued_p,
17930 MATRIX_ROW_CONTINUATION_LINE_P (row),
17931 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17932 row->ends_at_zv_p,
17933 row->fill_line_p,
17934 row->ends_in_middle_of_char_p,
17935 row->starts_in_middle_of_char_p,
17936 row->mouse_face_p,
17937 row->x,
17938 row->y,
17939 row->pixel_width,
17940 row->height,
17941 row->visible_height,
17942 row->ascent,
17943 row->phys_ascent);
17944 /* The next 3 lines should align to "Start" in the header. */
17945 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17946 row->end.overlay_string_index,
17947 row->continuation_lines_width);
17948 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17949 CHARPOS (row->start.string_pos),
17950 CHARPOS (row->end.string_pos));
17951 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17952 row->end.dpvec_index);
17955 if (glyphs > 1)
17957 int area;
17959 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17961 struct glyph *glyph = row->glyphs[area];
17962 struct glyph *glyph_end = glyph + row->used[area];
17964 /* Glyph for a line end in text. */
17965 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17966 ++glyph_end;
17968 if (glyph < glyph_end)
17969 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
17971 for (; glyph < glyph_end; ++glyph)
17972 dump_glyph (row, glyph, area);
17975 else if (glyphs == 1)
17977 int area;
17979 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17981 char *s = alloca (row->used[area] + 4);
17982 int i;
17984 for (i = 0; i < row->used[area]; ++i)
17986 struct glyph *glyph = row->glyphs[area] + i;
17987 if (i == row->used[area] - 1
17988 && area == TEXT_AREA
17989 && INTEGERP (glyph->object)
17990 && glyph->type == CHAR_GLYPH
17991 && glyph->u.ch == ' ')
17993 strcpy (&s[i], "[\\n]");
17994 i += 4;
17996 else if (glyph->type == CHAR_GLYPH
17997 && glyph->u.ch < 0x80
17998 && glyph->u.ch >= ' ')
17999 s[i] = glyph->u.ch;
18000 else
18001 s[i] = '.';
18004 s[i] = '\0';
18005 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18011 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18012 Sdump_glyph_matrix, 0, 1, "p",
18013 doc: /* Dump the current matrix of the selected window to stderr.
18014 Shows contents of glyph row structures. With non-nil
18015 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18016 glyphs in short form, otherwise show glyphs in long form. */)
18017 (Lisp_Object glyphs)
18019 struct window *w = XWINDOW (selected_window);
18020 struct buffer *buffer = XBUFFER (w->contents);
18022 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18023 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18024 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18025 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18026 fprintf (stderr, "=============================================\n");
18027 dump_glyph_matrix (w->current_matrix,
18028 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18029 return Qnil;
18033 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18034 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18035 (void)
18037 struct frame *f = XFRAME (selected_frame);
18038 dump_glyph_matrix (f->current_matrix, 1);
18039 return Qnil;
18043 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18044 doc: /* Dump glyph row ROW to stderr.
18045 GLYPH 0 means don't dump glyphs.
18046 GLYPH 1 means dump glyphs in short form.
18047 GLYPH > 1 or omitted means dump glyphs in long form. */)
18048 (Lisp_Object row, Lisp_Object glyphs)
18050 struct glyph_matrix *matrix;
18051 EMACS_INT vpos;
18053 CHECK_NUMBER (row);
18054 matrix = XWINDOW (selected_window)->current_matrix;
18055 vpos = XINT (row);
18056 if (vpos >= 0 && vpos < matrix->nrows)
18057 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18058 vpos,
18059 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18060 return Qnil;
18064 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18065 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18066 GLYPH 0 means don't dump glyphs.
18067 GLYPH 1 means dump glyphs in short form.
18068 GLYPH > 1 or omitted means dump glyphs in long form. */)
18069 (Lisp_Object row, Lisp_Object glyphs)
18071 struct frame *sf = SELECTED_FRAME ();
18072 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18073 EMACS_INT vpos;
18075 CHECK_NUMBER (row);
18076 vpos = XINT (row);
18077 if (vpos >= 0 && vpos < m->nrows)
18078 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18079 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18080 return Qnil;
18084 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18085 doc: /* Toggle tracing of redisplay.
18086 With ARG, turn tracing on if and only if ARG is positive. */)
18087 (Lisp_Object arg)
18089 if (NILP (arg))
18090 trace_redisplay_p = !trace_redisplay_p;
18091 else
18093 arg = Fprefix_numeric_value (arg);
18094 trace_redisplay_p = XINT (arg) > 0;
18097 return Qnil;
18101 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18102 doc: /* Like `format', but print result to stderr.
18103 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18104 (ptrdiff_t nargs, Lisp_Object *args)
18106 Lisp_Object s = Fformat (nargs, args);
18107 fprintf (stderr, "%s", SDATA (s));
18108 return Qnil;
18111 #endif /* GLYPH_DEBUG */
18115 /***********************************************************************
18116 Building Desired Matrix Rows
18117 ***********************************************************************/
18119 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18120 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18122 static struct glyph_row *
18123 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18125 struct frame *f = XFRAME (WINDOW_FRAME (w));
18126 struct buffer *buffer = XBUFFER (w->contents);
18127 struct buffer *old = current_buffer;
18128 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18129 int arrow_len = SCHARS (overlay_arrow_string);
18130 const unsigned char *arrow_end = arrow_string + arrow_len;
18131 const unsigned char *p;
18132 struct it it;
18133 bool multibyte_p;
18134 int n_glyphs_before;
18136 set_buffer_temp (buffer);
18137 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18138 it.glyph_row->used[TEXT_AREA] = 0;
18139 SET_TEXT_POS (it.position, 0, 0);
18141 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18142 p = arrow_string;
18143 while (p < arrow_end)
18145 Lisp_Object face, ilisp;
18147 /* Get the next character. */
18148 if (multibyte_p)
18149 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18150 else
18152 it.c = it.char_to_display = *p, it.len = 1;
18153 if (! ASCII_CHAR_P (it.c))
18154 it.char_to_display = BYTE8_TO_CHAR (it.c);
18156 p += it.len;
18158 /* Get its face. */
18159 ilisp = make_number (p - arrow_string);
18160 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18161 it.face_id = compute_char_face (f, it.char_to_display, face);
18163 /* Compute its width, get its glyphs. */
18164 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18165 SET_TEXT_POS (it.position, -1, -1);
18166 PRODUCE_GLYPHS (&it);
18168 /* If this character doesn't fit any more in the line, we have
18169 to remove some glyphs. */
18170 if (it.current_x > it.last_visible_x)
18172 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18173 break;
18177 set_buffer_temp (old);
18178 return it.glyph_row;
18182 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18183 glyphs to insert is determined by produce_special_glyphs. */
18185 static void
18186 insert_left_trunc_glyphs (struct it *it)
18188 struct it truncate_it;
18189 struct glyph *from, *end, *to, *toend;
18191 eassert (!FRAME_WINDOW_P (it->f)
18192 || (!it->glyph_row->reversed_p
18193 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18194 || (it->glyph_row->reversed_p
18195 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18197 /* Get the truncation glyphs. */
18198 truncate_it = *it;
18199 truncate_it.current_x = 0;
18200 truncate_it.face_id = DEFAULT_FACE_ID;
18201 truncate_it.glyph_row = &scratch_glyph_row;
18202 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18203 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18204 truncate_it.object = make_number (0);
18205 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18207 /* Overwrite glyphs from IT with truncation glyphs. */
18208 if (!it->glyph_row->reversed_p)
18210 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18212 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18213 end = from + tused;
18214 to = it->glyph_row->glyphs[TEXT_AREA];
18215 toend = to + it->glyph_row->used[TEXT_AREA];
18216 if (FRAME_WINDOW_P (it->f))
18218 /* On GUI frames, when variable-size fonts are displayed,
18219 the truncation glyphs may need more pixels than the row's
18220 glyphs they overwrite. We overwrite more glyphs to free
18221 enough screen real estate, and enlarge the stretch glyph
18222 on the right (see display_line), if there is one, to
18223 preserve the screen position of the truncation glyphs on
18224 the right. */
18225 int w = 0;
18226 struct glyph *g = to;
18227 short used;
18229 /* The first glyph could be partially visible, in which case
18230 it->glyph_row->x will be negative. But we want the left
18231 truncation glyphs to be aligned at the left margin of the
18232 window, so we override the x coordinate at which the row
18233 will begin. */
18234 it->glyph_row->x = 0;
18235 while (g < toend && w < it->truncation_pixel_width)
18237 w += g->pixel_width;
18238 ++g;
18240 if (g - to - tused > 0)
18242 memmove (to + tused, g, (toend - g) * sizeof(*g));
18243 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18245 used = it->glyph_row->used[TEXT_AREA];
18246 if (it->glyph_row->truncated_on_right_p
18247 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18248 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18249 == STRETCH_GLYPH)
18251 int extra = w - it->truncation_pixel_width;
18253 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18257 while (from < end)
18258 *to++ = *from++;
18260 /* There may be padding glyphs left over. Overwrite them too. */
18261 if (!FRAME_WINDOW_P (it->f))
18263 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18265 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18266 while (from < end)
18267 *to++ = *from++;
18271 if (to > toend)
18272 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18274 else
18276 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18278 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18279 that back to front. */
18280 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18281 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18282 toend = it->glyph_row->glyphs[TEXT_AREA];
18283 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18284 if (FRAME_WINDOW_P (it->f))
18286 int w = 0;
18287 struct glyph *g = to;
18289 while (g >= toend && w < it->truncation_pixel_width)
18291 w += g->pixel_width;
18292 --g;
18294 if (to - g - tused > 0)
18295 to = g + tused;
18296 if (it->glyph_row->truncated_on_right_p
18297 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18298 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18300 int extra = w - it->truncation_pixel_width;
18302 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18306 while (from >= end && to >= toend)
18307 *to-- = *from--;
18308 if (!FRAME_WINDOW_P (it->f))
18310 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18312 from =
18313 truncate_it.glyph_row->glyphs[TEXT_AREA]
18314 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18315 while (from >= end && to >= toend)
18316 *to-- = *from--;
18319 if (from >= end)
18321 /* Need to free some room before prepending additional
18322 glyphs. */
18323 int move_by = from - end + 1;
18324 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18325 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18327 for ( ; g >= g0; g--)
18328 g[move_by] = *g;
18329 while (from >= end)
18330 *to-- = *from--;
18331 it->glyph_row->used[TEXT_AREA] += move_by;
18336 /* Compute the hash code for ROW. */
18337 unsigned
18338 row_hash (struct glyph_row *row)
18340 int area, k;
18341 unsigned hashval = 0;
18343 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18344 for (k = 0; k < row->used[area]; ++k)
18345 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18346 + row->glyphs[area][k].u.val
18347 + row->glyphs[area][k].face_id
18348 + row->glyphs[area][k].padding_p
18349 + (row->glyphs[area][k].type << 2));
18351 return hashval;
18354 /* Compute the pixel height and width of IT->glyph_row.
18356 Most of the time, ascent and height of a display line will be equal
18357 to the max_ascent and max_height values of the display iterator
18358 structure. This is not the case if
18360 1. We hit ZV without displaying anything. In this case, max_ascent
18361 and max_height will be zero.
18363 2. We have some glyphs that don't contribute to the line height.
18364 (The glyph row flag contributes_to_line_height_p is for future
18365 pixmap extensions).
18367 The first case is easily covered by using default values because in
18368 these cases, the line height does not really matter, except that it
18369 must not be zero. */
18371 static void
18372 compute_line_metrics (struct it *it)
18374 struct glyph_row *row = it->glyph_row;
18376 if (FRAME_WINDOW_P (it->f))
18378 int i, min_y, max_y;
18380 /* The line may consist of one space only, that was added to
18381 place the cursor on it. If so, the row's height hasn't been
18382 computed yet. */
18383 if (row->height == 0)
18385 if (it->max_ascent + it->max_descent == 0)
18386 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18387 row->ascent = it->max_ascent;
18388 row->height = it->max_ascent + it->max_descent;
18389 row->phys_ascent = it->max_phys_ascent;
18390 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18391 row->extra_line_spacing = it->max_extra_line_spacing;
18394 /* Compute the width of this line. */
18395 row->pixel_width = row->x;
18396 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18397 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18399 eassert (row->pixel_width >= 0);
18400 eassert (row->ascent >= 0 && row->height > 0);
18402 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18403 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18405 /* If first line's physical ascent is larger than its logical
18406 ascent, use the physical ascent, and make the row taller.
18407 This makes accented characters fully visible. */
18408 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18409 && row->phys_ascent > row->ascent)
18411 row->height += row->phys_ascent - row->ascent;
18412 row->ascent = row->phys_ascent;
18415 /* Compute how much of the line is visible. */
18416 row->visible_height = row->height;
18418 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18419 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18421 if (row->y < min_y)
18422 row->visible_height -= min_y - row->y;
18423 if (row->y + row->height > max_y)
18424 row->visible_height -= row->y + row->height - max_y;
18426 else
18428 row->pixel_width = row->used[TEXT_AREA];
18429 if (row->continued_p)
18430 row->pixel_width -= it->continuation_pixel_width;
18431 else if (row->truncated_on_right_p)
18432 row->pixel_width -= it->truncation_pixel_width;
18433 row->ascent = row->phys_ascent = 0;
18434 row->height = row->phys_height = row->visible_height = 1;
18435 row->extra_line_spacing = 0;
18438 /* Compute a hash code for this row. */
18439 row->hash = row_hash (row);
18441 it->max_ascent = it->max_descent = 0;
18442 it->max_phys_ascent = it->max_phys_descent = 0;
18446 /* Append one space to the glyph row of iterator IT if doing a
18447 window-based redisplay. The space has the same face as
18448 IT->face_id. Value is non-zero if a space was added.
18450 This function is called to make sure that there is always one glyph
18451 at the end of a glyph row that the cursor can be set on under
18452 window-systems. (If there weren't such a glyph we would not know
18453 how wide and tall a box cursor should be displayed).
18455 At the same time this space let's a nicely handle clearing to the
18456 end of the line if the row ends in italic text. */
18458 static int
18459 append_space_for_newline (struct it *it, int default_face_p)
18461 if (FRAME_WINDOW_P (it->f))
18463 int n = it->glyph_row->used[TEXT_AREA];
18465 if (it->glyph_row->glyphs[TEXT_AREA] + n
18466 < it->glyph_row->glyphs[1 + TEXT_AREA])
18468 /* Save some values that must not be changed.
18469 Must save IT->c and IT->len because otherwise
18470 ITERATOR_AT_END_P wouldn't work anymore after
18471 append_space_for_newline has been called. */
18472 enum display_element_type saved_what = it->what;
18473 int saved_c = it->c, saved_len = it->len;
18474 int saved_char_to_display = it->char_to_display;
18475 int saved_x = it->current_x;
18476 int saved_face_id = it->face_id;
18477 int saved_box_end = it->end_of_box_run_p;
18478 struct text_pos saved_pos;
18479 Lisp_Object saved_object;
18480 struct face *face;
18482 saved_object = it->object;
18483 saved_pos = it->position;
18485 it->what = IT_CHARACTER;
18486 memset (&it->position, 0, sizeof it->position);
18487 it->object = make_number (0);
18488 it->c = it->char_to_display = ' ';
18489 it->len = 1;
18491 /* If the default face was remapped, be sure to use the
18492 remapped face for the appended newline. */
18493 if (default_face_p)
18494 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18495 else if (it->face_before_selective_p)
18496 it->face_id = it->saved_face_id;
18497 face = FACE_FROM_ID (it->f, it->face_id);
18498 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18499 /* In R2L rows, we will prepend a stretch glyph that will
18500 have the end_of_box_run_p flag set for it, so there's no
18501 need for the appended newline glyph to have that flag
18502 set. */
18503 if (it->glyph_row->reversed_p
18504 /* But if the appended newline glyph goes all the way to
18505 the end of the row, there will be no stretch glyph,
18506 so leave the box flag set. */
18507 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18508 it->end_of_box_run_p = 0;
18510 PRODUCE_GLYPHS (it);
18512 it->override_ascent = -1;
18513 it->constrain_row_ascent_descent_p = 0;
18514 it->current_x = saved_x;
18515 it->object = saved_object;
18516 it->position = saved_pos;
18517 it->what = saved_what;
18518 it->face_id = saved_face_id;
18519 it->len = saved_len;
18520 it->c = saved_c;
18521 it->char_to_display = saved_char_to_display;
18522 it->end_of_box_run_p = saved_box_end;
18523 return 1;
18527 return 0;
18531 /* Extend the face of the last glyph in the text area of IT->glyph_row
18532 to the end of the display line. Called from display_line. If the
18533 glyph row is empty, add a space glyph to it so that we know the
18534 face to draw. Set the glyph row flag fill_line_p. If the glyph
18535 row is R2L, prepend a stretch glyph to cover the empty space to the
18536 left of the leftmost glyph. */
18538 static void
18539 extend_face_to_end_of_line (struct it *it)
18541 struct face *face, *default_face;
18542 struct frame *f = it->f;
18544 /* If line is already filled, do nothing. Non window-system frames
18545 get a grace of one more ``pixel'' because their characters are
18546 1-``pixel'' wide, so they hit the equality too early. This grace
18547 is needed only for R2L rows that are not continued, to produce
18548 one extra blank where we could display the cursor. */
18549 if (it->current_x >= it->last_visible_x
18550 + (!FRAME_WINDOW_P (f)
18551 && it->glyph_row->reversed_p
18552 && !it->glyph_row->continued_p))
18553 return;
18555 /* The default face, possibly remapped. */
18556 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18558 /* Face extension extends the background and box of IT->face_id
18559 to the end of the line. If the background equals the background
18560 of the frame, we don't have to do anything. */
18561 if (it->face_before_selective_p)
18562 face = FACE_FROM_ID (f, it->saved_face_id);
18563 else
18564 face = FACE_FROM_ID (f, it->face_id);
18566 if (FRAME_WINDOW_P (f)
18567 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18568 && face->box == FACE_NO_BOX
18569 && face->background == FRAME_BACKGROUND_PIXEL (f)
18570 && !face->stipple
18571 && !it->glyph_row->reversed_p)
18572 return;
18574 /* Set the glyph row flag indicating that the face of the last glyph
18575 in the text area has to be drawn to the end of the text area. */
18576 it->glyph_row->fill_line_p = 1;
18578 /* If current character of IT is not ASCII, make sure we have the
18579 ASCII face. This will be automatically undone the next time
18580 get_next_display_element returns a multibyte character. Note
18581 that the character will always be single byte in unibyte
18582 text. */
18583 if (!ASCII_CHAR_P (it->c))
18585 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18588 if (FRAME_WINDOW_P (f))
18590 /* If the row is empty, add a space with the current face of IT,
18591 so that we know which face to draw. */
18592 if (it->glyph_row->used[TEXT_AREA] == 0)
18594 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18595 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18596 it->glyph_row->used[TEXT_AREA] = 1;
18598 #ifdef HAVE_WINDOW_SYSTEM
18599 if (it->glyph_row->reversed_p)
18601 /* Prepend a stretch glyph to the row, such that the
18602 rightmost glyph will be drawn flushed all the way to the
18603 right margin of the window. The stretch glyph that will
18604 occupy the empty space, if any, to the left of the
18605 glyphs. */
18606 struct font *font = face->font ? face->font : FRAME_FONT (f);
18607 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18608 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18609 struct glyph *g;
18610 int row_width, stretch_ascent, stretch_width;
18611 struct text_pos saved_pos;
18612 int saved_face_id, saved_avoid_cursor, saved_box_start;
18614 for (row_width = 0, g = row_start; g < row_end; g++)
18615 row_width += g->pixel_width;
18616 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18617 if (stretch_width > 0)
18619 stretch_ascent =
18620 (((it->ascent + it->descent)
18621 * FONT_BASE (font)) / FONT_HEIGHT (font));
18622 saved_pos = it->position;
18623 memset (&it->position, 0, sizeof it->position);
18624 saved_avoid_cursor = it->avoid_cursor_p;
18625 it->avoid_cursor_p = 1;
18626 saved_face_id = it->face_id;
18627 saved_box_start = it->start_of_box_run_p;
18628 /* The last row's stretch glyph should get the default
18629 face, to avoid painting the rest of the window with
18630 the region face, if the region ends at ZV. */
18631 if (it->glyph_row->ends_at_zv_p)
18632 it->face_id = default_face->id;
18633 else
18634 it->face_id = face->id;
18635 it->start_of_box_run_p = 0;
18636 append_stretch_glyph (it, make_number (0), stretch_width,
18637 it->ascent + it->descent, stretch_ascent);
18638 it->position = saved_pos;
18639 it->avoid_cursor_p = saved_avoid_cursor;
18640 it->face_id = saved_face_id;
18641 it->start_of_box_run_p = saved_box_start;
18644 #endif /* HAVE_WINDOW_SYSTEM */
18646 else
18648 /* Save some values that must not be changed. */
18649 int saved_x = it->current_x;
18650 struct text_pos saved_pos;
18651 Lisp_Object saved_object;
18652 enum display_element_type saved_what = it->what;
18653 int saved_face_id = it->face_id;
18655 saved_object = it->object;
18656 saved_pos = it->position;
18658 it->what = IT_CHARACTER;
18659 memset (&it->position, 0, sizeof it->position);
18660 it->object = make_number (0);
18661 it->c = it->char_to_display = ' ';
18662 it->len = 1;
18663 /* The last row's blank glyphs should get the default face, to
18664 avoid painting the rest of the window with the region face,
18665 if the region ends at ZV. */
18666 if (it->glyph_row->ends_at_zv_p)
18667 it->face_id = default_face->id;
18668 else
18669 it->face_id = face->id;
18671 PRODUCE_GLYPHS (it);
18673 while (it->current_x <= it->last_visible_x)
18674 PRODUCE_GLYPHS (it);
18676 /* Don't count these blanks really. It would let us insert a left
18677 truncation glyph below and make us set the cursor on them, maybe. */
18678 it->current_x = saved_x;
18679 it->object = saved_object;
18680 it->position = saved_pos;
18681 it->what = saved_what;
18682 it->face_id = saved_face_id;
18687 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18688 trailing whitespace. */
18690 static int
18691 trailing_whitespace_p (ptrdiff_t charpos)
18693 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18694 int c = 0;
18696 while (bytepos < ZV_BYTE
18697 && (c = FETCH_CHAR (bytepos),
18698 c == ' ' || c == '\t'))
18699 ++bytepos;
18701 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18703 if (bytepos != PT_BYTE)
18704 return 1;
18706 return 0;
18710 /* Highlight trailing whitespace, if any, in ROW. */
18712 static void
18713 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18715 int used = row->used[TEXT_AREA];
18717 if (used)
18719 struct glyph *start = row->glyphs[TEXT_AREA];
18720 struct glyph *glyph = start + used - 1;
18722 if (row->reversed_p)
18724 /* Right-to-left rows need to be processed in the opposite
18725 direction, so swap the edge pointers. */
18726 glyph = start;
18727 start = row->glyphs[TEXT_AREA] + used - 1;
18730 /* Skip over glyphs inserted to display the cursor at the
18731 end of a line, for extending the face of the last glyph
18732 to the end of the line on terminals, and for truncation
18733 and continuation glyphs. */
18734 if (!row->reversed_p)
18736 while (glyph >= start
18737 && glyph->type == CHAR_GLYPH
18738 && INTEGERP (glyph->object))
18739 --glyph;
18741 else
18743 while (glyph <= start
18744 && glyph->type == CHAR_GLYPH
18745 && INTEGERP (glyph->object))
18746 ++glyph;
18749 /* If last glyph is a space or stretch, and it's trailing
18750 whitespace, set the face of all trailing whitespace glyphs in
18751 IT->glyph_row to `trailing-whitespace'. */
18752 if ((row->reversed_p ? glyph <= start : glyph >= start)
18753 && BUFFERP (glyph->object)
18754 && (glyph->type == STRETCH_GLYPH
18755 || (glyph->type == CHAR_GLYPH
18756 && glyph->u.ch == ' '))
18757 && trailing_whitespace_p (glyph->charpos))
18759 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18760 if (face_id < 0)
18761 return;
18763 if (!row->reversed_p)
18765 while (glyph >= start
18766 && BUFFERP (glyph->object)
18767 && (glyph->type == STRETCH_GLYPH
18768 || (glyph->type == CHAR_GLYPH
18769 && glyph->u.ch == ' ')))
18770 (glyph--)->face_id = face_id;
18772 else
18774 while (glyph <= start
18775 && BUFFERP (glyph->object)
18776 && (glyph->type == STRETCH_GLYPH
18777 || (glyph->type == CHAR_GLYPH
18778 && glyph->u.ch == ' ')))
18779 (glyph++)->face_id = face_id;
18786 /* Value is non-zero if glyph row ROW should be
18787 considered to hold the buffer position CHARPOS. */
18789 static int
18790 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18792 int result = 1;
18794 if (charpos == CHARPOS (row->end.pos)
18795 || charpos == MATRIX_ROW_END_CHARPOS (row))
18797 /* Suppose the row ends on a string.
18798 Unless the row is continued, that means it ends on a newline
18799 in the string. If it's anything other than a display string
18800 (e.g., a before-string from an overlay), we don't want the
18801 cursor there. (This heuristic seems to give the optimal
18802 behavior for the various types of multi-line strings.)
18803 One exception: if the string has `cursor' property on one of
18804 its characters, we _do_ want the cursor there. */
18805 if (CHARPOS (row->end.string_pos) >= 0)
18807 if (row->continued_p)
18808 result = 1;
18809 else
18811 /* Check for `display' property. */
18812 struct glyph *beg = row->glyphs[TEXT_AREA];
18813 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18814 struct glyph *glyph;
18816 result = 0;
18817 for (glyph = end; glyph >= beg; --glyph)
18818 if (STRINGP (glyph->object))
18820 Lisp_Object prop
18821 = Fget_char_property (make_number (charpos),
18822 Qdisplay, Qnil);
18823 result =
18824 (!NILP (prop)
18825 && display_prop_string_p (prop, glyph->object));
18826 /* If there's a `cursor' property on one of the
18827 string's characters, this row is a cursor row,
18828 even though this is not a display string. */
18829 if (!result)
18831 Lisp_Object s = glyph->object;
18833 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18835 ptrdiff_t gpos = glyph->charpos;
18837 if (!NILP (Fget_char_property (make_number (gpos),
18838 Qcursor, s)))
18840 result = 1;
18841 break;
18845 break;
18849 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18851 /* If the row ends in middle of a real character,
18852 and the line is continued, we want the cursor here.
18853 That's because CHARPOS (ROW->end.pos) would equal
18854 PT if PT is before the character. */
18855 if (!row->ends_in_ellipsis_p)
18856 result = row->continued_p;
18857 else
18858 /* If the row ends in an ellipsis, then
18859 CHARPOS (ROW->end.pos) will equal point after the
18860 invisible text. We want that position to be displayed
18861 after the ellipsis. */
18862 result = 0;
18864 /* If the row ends at ZV, display the cursor at the end of that
18865 row instead of at the start of the row below. */
18866 else if (row->ends_at_zv_p)
18867 result = 1;
18868 else
18869 result = 0;
18872 return result;
18875 /* Value is non-zero if glyph row ROW should be
18876 used to hold the cursor. */
18878 static int
18879 cursor_row_p (struct glyph_row *row)
18881 return row_for_charpos_p (row, PT);
18886 /* Push the property PROP so that it will be rendered at the current
18887 position in IT. Return 1 if PROP was successfully pushed, 0
18888 otherwise. Called from handle_line_prefix to handle the
18889 `line-prefix' and `wrap-prefix' properties. */
18891 static int
18892 push_prefix_prop (struct it *it, Lisp_Object prop)
18894 struct text_pos pos =
18895 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18897 eassert (it->method == GET_FROM_BUFFER
18898 || it->method == GET_FROM_DISPLAY_VECTOR
18899 || it->method == GET_FROM_STRING);
18901 /* We need to save the current buffer/string position, so it will be
18902 restored by pop_it, because iterate_out_of_display_property
18903 depends on that being set correctly, but some situations leave
18904 it->position not yet set when this function is called. */
18905 push_it (it, &pos);
18907 if (STRINGP (prop))
18909 if (SCHARS (prop) == 0)
18911 pop_it (it);
18912 return 0;
18915 it->string = prop;
18916 it->string_from_prefix_prop_p = 1;
18917 it->multibyte_p = STRING_MULTIBYTE (it->string);
18918 it->current.overlay_string_index = -1;
18919 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18920 it->end_charpos = it->string_nchars = SCHARS (it->string);
18921 it->method = GET_FROM_STRING;
18922 it->stop_charpos = 0;
18923 it->prev_stop = 0;
18924 it->base_level_stop = 0;
18926 /* Force paragraph direction to be that of the parent
18927 buffer/string. */
18928 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18929 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18930 else
18931 it->paragraph_embedding = L2R;
18933 /* Set up the bidi iterator for this display string. */
18934 if (it->bidi_p)
18936 it->bidi_it.string.lstring = it->string;
18937 it->bidi_it.string.s = NULL;
18938 it->bidi_it.string.schars = it->end_charpos;
18939 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18940 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18941 it->bidi_it.string.unibyte = !it->multibyte_p;
18942 it->bidi_it.w = it->w;
18943 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18946 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18948 it->method = GET_FROM_STRETCH;
18949 it->object = prop;
18951 #ifdef HAVE_WINDOW_SYSTEM
18952 else if (IMAGEP (prop))
18954 it->what = IT_IMAGE;
18955 it->image_id = lookup_image (it->f, prop);
18956 it->method = GET_FROM_IMAGE;
18958 #endif /* HAVE_WINDOW_SYSTEM */
18959 else
18961 pop_it (it); /* bogus display property, give up */
18962 return 0;
18965 return 1;
18968 /* Return the character-property PROP at the current position in IT. */
18970 static Lisp_Object
18971 get_it_property (struct it *it, Lisp_Object prop)
18973 Lisp_Object position, object = it->object;
18975 if (STRINGP (object))
18976 position = make_number (IT_STRING_CHARPOS (*it));
18977 else if (BUFFERP (object))
18979 position = make_number (IT_CHARPOS (*it));
18980 object = it->window;
18982 else
18983 return Qnil;
18985 return Fget_char_property (position, prop, object);
18988 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18990 static void
18991 handle_line_prefix (struct it *it)
18993 Lisp_Object prefix;
18995 if (it->continuation_lines_width > 0)
18997 prefix = get_it_property (it, Qwrap_prefix);
18998 if (NILP (prefix))
18999 prefix = Vwrap_prefix;
19001 else
19003 prefix = get_it_property (it, Qline_prefix);
19004 if (NILP (prefix))
19005 prefix = Vline_prefix;
19007 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19009 /* If the prefix is wider than the window, and we try to wrap
19010 it, it would acquire its own wrap prefix, and so on till the
19011 iterator stack overflows. So, don't wrap the prefix. */
19012 it->line_wrap = TRUNCATE;
19013 it->avoid_cursor_p = 1;
19019 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19020 only for R2L lines from display_line and display_string, when they
19021 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19022 the line/string needs to be continued on the next glyph row. */
19023 static void
19024 unproduce_glyphs (struct it *it, int n)
19026 struct glyph *glyph, *end;
19028 eassert (it->glyph_row);
19029 eassert (it->glyph_row->reversed_p);
19030 eassert (it->area == TEXT_AREA);
19031 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19033 if (n > it->glyph_row->used[TEXT_AREA])
19034 n = it->glyph_row->used[TEXT_AREA];
19035 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19036 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19037 for ( ; glyph < end; glyph++)
19038 glyph[-n] = *glyph;
19041 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19042 and ROW->maxpos. */
19043 static void
19044 find_row_edges (struct it *it, struct glyph_row *row,
19045 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19046 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19048 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19049 lines' rows is implemented for bidi-reordered rows. */
19051 /* ROW->minpos is the value of min_pos, the minimal buffer position
19052 we have in ROW, or ROW->start.pos if that is smaller. */
19053 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19054 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19055 else
19056 /* We didn't find buffer positions smaller than ROW->start, or
19057 didn't find _any_ valid buffer positions in any of the glyphs,
19058 so we must trust the iterator's computed positions. */
19059 row->minpos = row->start.pos;
19060 if (max_pos <= 0)
19062 max_pos = CHARPOS (it->current.pos);
19063 max_bpos = BYTEPOS (it->current.pos);
19066 /* Here are the various use-cases for ending the row, and the
19067 corresponding values for ROW->maxpos:
19069 Line ends in a newline from buffer eol_pos + 1
19070 Line is continued from buffer max_pos + 1
19071 Line is truncated on right it->current.pos
19072 Line ends in a newline from string max_pos + 1(*)
19073 (*) + 1 only when line ends in a forward scan
19074 Line is continued from string max_pos
19075 Line is continued from display vector max_pos
19076 Line is entirely from a string min_pos == max_pos
19077 Line is entirely from a display vector min_pos == max_pos
19078 Line that ends at ZV ZV
19080 If you discover other use-cases, please add them here as
19081 appropriate. */
19082 if (row->ends_at_zv_p)
19083 row->maxpos = it->current.pos;
19084 else if (row->used[TEXT_AREA])
19086 int seen_this_string = 0;
19087 struct glyph_row *r1 = row - 1;
19089 /* Did we see the same display string on the previous row? */
19090 if (STRINGP (it->object)
19091 /* this is not the first row */
19092 && row > it->w->desired_matrix->rows
19093 /* previous row is not the header line */
19094 && !r1->mode_line_p
19095 /* previous row also ends in a newline from a string */
19096 && r1->ends_in_newline_from_string_p)
19098 struct glyph *start, *end;
19100 /* Search for the last glyph of the previous row that came
19101 from buffer or string. Depending on whether the row is
19102 L2R or R2L, we need to process it front to back or the
19103 other way round. */
19104 if (!r1->reversed_p)
19106 start = r1->glyphs[TEXT_AREA];
19107 end = start + r1->used[TEXT_AREA];
19108 /* Glyphs inserted by redisplay have an integer (zero)
19109 as their object. */
19110 while (end > start
19111 && INTEGERP ((end - 1)->object)
19112 && (end - 1)->charpos <= 0)
19113 --end;
19114 if (end > start)
19116 if (EQ ((end - 1)->object, it->object))
19117 seen_this_string = 1;
19119 else
19120 /* If all the glyphs of the previous row were inserted
19121 by redisplay, it means the previous row was
19122 produced from a single newline, which is only
19123 possible if that newline came from the same string
19124 as the one which produced this ROW. */
19125 seen_this_string = 1;
19127 else
19129 end = r1->glyphs[TEXT_AREA] - 1;
19130 start = end + r1->used[TEXT_AREA];
19131 while (end < start
19132 && INTEGERP ((end + 1)->object)
19133 && (end + 1)->charpos <= 0)
19134 ++end;
19135 if (end < start)
19137 if (EQ ((end + 1)->object, it->object))
19138 seen_this_string = 1;
19140 else
19141 seen_this_string = 1;
19144 /* Take note of each display string that covers a newline only
19145 once, the first time we see it. This is for when a display
19146 string includes more than one newline in it. */
19147 if (row->ends_in_newline_from_string_p && !seen_this_string)
19149 /* If we were scanning the buffer forward when we displayed
19150 the string, we want to account for at least one buffer
19151 position that belongs to this row (position covered by
19152 the display string), so that cursor positioning will
19153 consider this row as a candidate when point is at the end
19154 of the visual line represented by this row. This is not
19155 required when scanning back, because max_pos will already
19156 have a much larger value. */
19157 if (CHARPOS (row->end.pos) > max_pos)
19158 INC_BOTH (max_pos, max_bpos);
19159 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19161 else if (CHARPOS (it->eol_pos) > 0)
19162 SET_TEXT_POS (row->maxpos,
19163 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19164 else if (row->continued_p)
19166 /* If max_pos is different from IT's current position, it
19167 means IT->method does not belong to the display element
19168 at max_pos. However, it also means that the display
19169 element at max_pos was displayed in its entirety on this
19170 line, which is equivalent to saying that the next line
19171 starts at the next buffer position. */
19172 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19173 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19174 else
19176 INC_BOTH (max_pos, max_bpos);
19177 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19180 else if (row->truncated_on_right_p)
19181 /* display_line already called reseat_at_next_visible_line_start,
19182 which puts the iterator at the beginning of the next line, in
19183 the logical order. */
19184 row->maxpos = it->current.pos;
19185 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19186 /* A line that is entirely from a string/image/stretch... */
19187 row->maxpos = row->minpos;
19188 else
19189 emacs_abort ();
19191 else
19192 row->maxpos = it->current.pos;
19195 /* Construct the glyph row IT->glyph_row in the desired matrix of
19196 IT->w from text at the current position of IT. See dispextern.h
19197 for an overview of struct it. Value is non-zero if
19198 IT->glyph_row displays text, as opposed to a line displaying ZV
19199 only. */
19201 static int
19202 display_line (struct it *it)
19204 struct glyph_row *row = it->glyph_row;
19205 Lisp_Object overlay_arrow_string;
19206 struct it wrap_it;
19207 void *wrap_data = NULL;
19208 int may_wrap = 0, wrap_x IF_LINT (= 0);
19209 int wrap_row_used = -1;
19210 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19211 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19212 int wrap_row_extra_line_spacing IF_LINT (= 0);
19213 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19214 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19215 int cvpos;
19216 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19217 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19219 /* We always start displaying at hpos zero even if hscrolled. */
19220 eassert (it->hpos == 0 && it->current_x == 0);
19222 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19223 >= it->w->desired_matrix->nrows)
19225 it->w->nrows_scale_factor++;
19226 fonts_changed_p = 1;
19227 return 0;
19230 /* Is IT->w showing the region? */
19231 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19233 /* Clear the result glyph row and enable it. */
19234 prepare_desired_row (row);
19236 row->y = it->current_y;
19237 row->start = it->start;
19238 row->continuation_lines_width = it->continuation_lines_width;
19239 row->displays_text_p = 1;
19240 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19241 it->starts_in_middle_of_char_p = 0;
19243 /* Arrange the overlays nicely for our purposes. Usually, we call
19244 display_line on only one line at a time, in which case this
19245 can't really hurt too much, or we call it on lines which appear
19246 one after another in the buffer, in which case all calls to
19247 recenter_overlay_lists but the first will be pretty cheap. */
19248 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19250 /* Move over display elements that are not visible because we are
19251 hscrolled. This may stop at an x-position < IT->first_visible_x
19252 if the first glyph is partially visible or if we hit a line end. */
19253 if (it->current_x < it->first_visible_x)
19255 enum move_it_result move_result;
19257 this_line_min_pos = row->start.pos;
19258 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19259 MOVE_TO_POS | MOVE_TO_X);
19260 /* If we are under a large hscroll, move_it_in_display_line_to
19261 could hit the end of the line without reaching
19262 it->first_visible_x. Pretend that we did reach it. This is
19263 especially important on a TTY, where we will call
19264 extend_face_to_end_of_line, which needs to know how many
19265 blank glyphs to produce. */
19266 if (it->current_x < it->first_visible_x
19267 && (move_result == MOVE_NEWLINE_OR_CR
19268 || move_result == MOVE_POS_MATCH_OR_ZV))
19269 it->current_x = it->first_visible_x;
19271 /* Record the smallest positions seen while we moved over
19272 display elements that are not visible. This is needed by
19273 redisplay_internal for optimizing the case where the cursor
19274 stays inside the same line. The rest of this function only
19275 considers positions that are actually displayed, so
19276 RECORD_MAX_MIN_POS will not otherwise record positions that
19277 are hscrolled to the left of the left edge of the window. */
19278 min_pos = CHARPOS (this_line_min_pos);
19279 min_bpos = BYTEPOS (this_line_min_pos);
19281 else
19283 /* We only do this when not calling `move_it_in_display_line_to'
19284 above, because move_it_in_display_line_to calls
19285 handle_line_prefix itself. */
19286 handle_line_prefix (it);
19289 /* Get the initial row height. This is either the height of the
19290 text hscrolled, if there is any, or zero. */
19291 row->ascent = it->max_ascent;
19292 row->height = it->max_ascent + it->max_descent;
19293 row->phys_ascent = it->max_phys_ascent;
19294 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19295 row->extra_line_spacing = it->max_extra_line_spacing;
19297 /* Utility macro to record max and min buffer positions seen until now. */
19298 #define RECORD_MAX_MIN_POS(IT) \
19299 do \
19301 int composition_p = !STRINGP ((IT)->string) \
19302 && ((IT)->what == IT_COMPOSITION); \
19303 ptrdiff_t current_pos = \
19304 composition_p ? (IT)->cmp_it.charpos \
19305 : IT_CHARPOS (*(IT)); \
19306 ptrdiff_t current_bpos = \
19307 composition_p ? CHAR_TO_BYTE (current_pos) \
19308 : IT_BYTEPOS (*(IT)); \
19309 if (current_pos < min_pos) \
19311 min_pos = current_pos; \
19312 min_bpos = current_bpos; \
19314 if (IT_CHARPOS (*it) > max_pos) \
19316 max_pos = IT_CHARPOS (*it); \
19317 max_bpos = IT_BYTEPOS (*it); \
19320 while (0)
19322 /* Loop generating characters. The loop is left with IT on the next
19323 character to display. */
19324 while (1)
19326 int n_glyphs_before, hpos_before, x_before;
19327 int x, nglyphs;
19328 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19330 /* Retrieve the next thing to display. Value is zero if end of
19331 buffer reached. */
19332 if (!get_next_display_element (it))
19334 /* Maybe add a space at the end of this line that is used to
19335 display the cursor there under X. Set the charpos of the
19336 first glyph of blank lines not corresponding to any text
19337 to -1. */
19338 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19339 row->exact_window_width_line_p = 1;
19340 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19341 || row->used[TEXT_AREA] == 0)
19343 row->glyphs[TEXT_AREA]->charpos = -1;
19344 row->displays_text_p = 0;
19346 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19347 && (!MINI_WINDOW_P (it->w)
19348 || (minibuf_level && EQ (it->window, minibuf_window))))
19349 row->indicate_empty_line_p = 1;
19352 it->continuation_lines_width = 0;
19353 row->ends_at_zv_p = 1;
19354 /* A row that displays right-to-left text must always have
19355 its last face extended all the way to the end of line,
19356 even if this row ends in ZV, because we still write to
19357 the screen left to right. We also need to extend the
19358 last face if the default face is remapped to some
19359 different face, otherwise the functions that clear
19360 portions of the screen will clear with the default face's
19361 background color. */
19362 if (row->reversed_p
19363 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19364 extend_face_to_end_of_line (it);
19365 break;
19368 /* Now, get the metrics of what we want to display. This also
19369 generates glyphs in `row' (which is IT->glyph_row). */
19370 n_glyphs_before = row->used[TEXT_AREA];
19371 x = it->current_x;
19373 /* Remember the line height so far in case the next element doesn't
19374 fit on the line. */
19375 if (it->line_wrap != TRUNCATE)
19377 ascent = it->max_ascent;
19378 descent = it->max_descent;
19379 phys_ascent = it->max_phys_ascent;
19380 phys_descent = it->max_phys_descent;
19382 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19384 if (IT_DISPLAYING_WHITESPACE (it))
19385 may_wrap = 1;
19386 else if (may_wrap)
19388 SAVE_IT (wrap_it, *it, wrap_data);
19389 wrap_x = x;
19390 wrap_row_used = row->used[TEXT_AREA];
19391 wrap_row_ascent = row->ascent;
19392 wrap_row_height = row->height;
19393 wrap_row_phys_ascent = row->phys_ascent;
19394 wrap_row_phys_height = row->phys_height;
19395 wrap_row_extra_line_spacing = row->extra_line_spacing;
19396 wrap_row_min_pos = min_pos;
19397 wrap_row_min_bpos = min_bpos;
19398 wrap_row_max_pos = max_pos;
19399 wrap_row_max_bpos = max_bpos;
19400 may_wrap = 0;
19405 PRODUCE_GLYPHS (it);
19407 /* If this display element was in marginal areas, continue with
19408 the next one. */
19409 if (it->area != TEXT_AREA)
19411 row->ascent = max (row->ascent, it->max_ascent);
19412 row->height = max (row->height, it->max_ascent + it->max_descent);
19413 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19414 row->phys_height = max (row->phys_height,
19415 it->max_phys_ascent + it->max_phys_descent);
19416 row->extra_line_spacing = max (row->extra_line_spacing,
19417 it->max_extra_line_spacing);
19418 set_iterator_to_next (it, 1);
19419 continue;
19422 /* Does the display element fit on the line? If we truncate
19423 lines, we should draw past the right edge of the window. If
19424 we don't truncate, we want to stop so that we can display the
19425 continuation glyph before the right margin. If lines are
19426 continued, there are two possible strategies for characters
19427 resulting in more than 1 glyph (e.g. tabs): Display as many
19428 glyphs as possible in this line and leave the rest for the
19429 continuation line, or display the whole element in the next
19430 line. Original redisplay did the former, so we do it also. */
19431 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19432 hpos_before = it->hpos;
19433 x_before = x;
19435 if (/* Not a newline. */
19436 nglyphs > 0
19437 /* Glyphs produced fit entirely in the line. */
19438 && it->current_x < it->last_visible_x)
19440 it->hpos += nglyphs;
19441 row->ascent = max (row->ascent, it->max_ascent);
19442 row->height = max (row->height, it->max_ascent + it->max_descent);
19443 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19444 row->phys_height = max (row->phys_height,
19445 it->max_phys_ascent + it->max_phys_descent);
19446 row->extra_line_spacing = max (row->extra_line_spacing,
19447 it->max_extra_line_spacing);
19448 if (it->current_x - it->pixel_width < it->first_visible_x)
19449 row->x = x - it->first_visible_x;
19450 /* Record the maximum and minimum buffer positions seen so
19451 far in glyphs that will be displayed by this row. */
19452 if (it->bidi_p)
19453 RECORD_MAX_MIN_POS (it);
19455 else
19457 int i, new_x;
19458 struct glyph *glyph;
19460 for (i = 0; i < nglyphs; ++i, x = new_x)
19462 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19463 new_x = x + glyph->pixel_width;
19465 if (/* Lines are continued. */
19466 it->line_wrap != TRUNCATE
19467 && (/* Glyph doesn't fit on the line. */
19468 new_x > it->last_visible_x
19469 /* Or it fits exactly on a window system frame. */
19470 || (new_x == it->last_visible_x
19471 && FRAME_WINDOW_P (it->f)
19472 && (row->reversed_p
19473 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19474 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19476 /* End of a continued line. */
19478 if (it->hpos == 0
19479 || (new_x == it->last_visible_x
19480 && FRAME_WINDOW_P (it->f)
19481 && (row->reversed_p
19482 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19483 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19485 /* Current glyph is the only one on the line or
19486 fits exactly on the line. We must continue
19487 the line because we can't draw the cursor
19488 after the glyph. */
19489 row->continued_p = 1;
19490 it->current_x = new_x;
19491 it->continuation_lines_width += new_x;
19492 ++it->hpos;
19493 if (i == nglyphs - 1)
19495 /* If line-wrap is on, check if a previous
19496 wrap point was found. */
19497 if (wrap_row_used > 0
19498 /* Even if there is a previous wrap
19499 point, continue the line here as
19500 usual, if (i) the previous character
19501 was a space or tab AND (ii) the
19502 current character is not. */
19503 && (!may_wrap
19504 || IT_DISPLAYING_WHITESPACE (it)))
19505 goto back_to_wrap;
19507 /* Record the maximum and minimum buffer
19508 positions seen so far in glyphs that will be
19509 displayed by this row. */
19510 if (it->bidi_p)
19511 RECORD_MAX_MIN_POS (it);
19512 set_iterator_to_next (it, 1);
19513 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19515 if (!get_next_display_element (it))
19517 row->exact_window_width_line_p = 1;
19518 it->continuation_lines_width = 0;
19519 row->continued_p = 0;
19520 row->ends_at_zv_p = 1;
19522 else if (ITERATOR_AT_END_OF_LINE_P (it))
19524 row->continued_p = 0;
19525 row->exact_window_width_line_p = 1;
19529 else if (it->bidi_p)
19530 RECORD_MAX_MIN_POS (it);
19532 else if (CHAR_GLYPH_PADDING_P (*glyph)
19533 && !FRAME_WINDOW_P (it->f))
19535 /* A padding glyph that doesn't fit on this line.
19536 This means the whole character doesn't fit
19537 on the line. */
19538 if (row->reversed_p)
19539 unproduce_glyphs (it, row->used[TEXT_AREA]
19540 - n_glyphs_before);
19541 row->used[TEXT_AREA] = n_glyphs_before;
19543 /* Fill the rest of the row with continuation
19544 glyphs like in 20.x. */
19545 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19546 < row->glyphs[1 + TEXT_AREA])
19547 produce_special_glyphs (it, IT_CONTINUATION);
19549 row->continued_p = 1;
19550 it->current_x = x_before;
19551 it->continuation_lines_width += x_before;
19553 /* Restore the height to what it was before the
19554 element not fitting on the line. */
19555 it->max_ascent = ascent;
19556 it->max_descent = descent;
19557 it->max_phys_ascent = phys_ascent;
19558 it->max_phys_descent = phys_descent;
19560 else if (wrap_row_used > 0)
19562 back_to_wrap:
19563 if (row->reversed_p)
19564 unproduce_glyphs (it,
19565 row->used[TEXT_AREA] - wrap_row_used);
19566 RESTORE_IT (it, &wrap_it, wrap_data);
19567 it->continuation_lines_width += wrap_x;
19568 row->used[TEXT_AREA] = wrap_row_used;
19569 row->ascent = wrap_row_ascent;
19570 row->height = wrap_row_height;
19571 row->phys_ascent = wrap_row_phys_ascent;
19572 row->phys_height = wrap_row_phys_height;
19573 row->extra_line_spacing = wrap_row_extra_line_spacing;
19574 min_pos = wrap_row_min_pos;
19575 min_bpos = wrap_row_min_bpos;
19576 max_pos = wrap_row_max_pos;
19577 max_bpos = wrap_row_max_bpos;
19578 row->continued_p = 1;
19579 row->ends_at_zv_p = 0;
19580 row->exact_window_width_line_p = 0;
19581 it->continuation_lines_width += x;
19583 /* Make sure that a non-default face is extended
19584 up to the right margin of the window. */
19585 extend_face_to_end_of_line (it);
19587 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19589 /* A TAB that extends past the right edge of the
19590 window. This produces a single glyph on
19591 window system frames. We leave the glyph in
19592 this row and let it fill the row, but don't
19593 consume the TAB. */
19594 if ((row->reversed_p
19595 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19596 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19597 produce_special_glyphs (it, IT_CONTINUATION);
19598 it->continuation_lines_width += it->last_visible_x;
19599 row->ends_in_middle_of_char_p = 1;
19600 row->continued_p = 1;
19601 glyph->pixel_width = it->last_visible_x - x;
19602 it->starts_in_middle_of_char_p = 1;
19604 else
19606 /* Something other than a TAB that draws past
19607 the right edge of the window. Restore
19608 positions to values before the element. */
19609 if (row->reversed_p)
19610 unproduce_glyphs (it, row->used[TEXT_AREA]
19611 - (n_glyphs_before + i));
19612 row->used[TEXT_AREA] = n_glyphs_before + i;
19614 /* Display continuation glyphs. */
19615 it->current_x = x_before;
19616 it->continuation_lines_width += x;
19617 if (!FRAME_WINDOW_P (it->f)
19618 || (row->reversed_p
19619 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19620 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19621 produce_special_glyphs (it, IT_CONTINUATION);
19622 row->continued_p = 1;
19624 extend_face_to_end_of_line (it);
19626 if (nglyphs > 1 && i > 0)
19628 row->ends_in_middle_of_char_p = 1;
19629 it->starts_in_middle_of_char_p = 1;
19632 /* Restore the height to what it was before the
19633 element not fitting on the line. */
19634 it->max_ascent = ascent;
19635 it->max_descent = descent;
19636 it->max_phys_ascent = phys_ascent;
19637 it->max_phys_descent = phys_descent;
19640 break;
19642 else if (new_x > it->first_visible_x)
19644 /* Increment number of glyphs actually displayed. */
19645 ++it->hpos;
19647 /* Record the maximum and minimum buffer positions
19648 seen so far in glyphs that will be displayed by
19649 this row. */
19650 if (it->bidi_p)
19651 RECORD_MAX_MIN_POS (it);
19653 if (x < it->first_visible_x)
19654 /* Glyph is partially visible, i.e. row starts at
19655 negative X position. */
19656 row->x = x - it->first_visible_x;
19658 else
19660 /* Glyph is completely off the left margin of the
19661 window. This should not happen because of the
19662 move_it_in_display_line at the start of this
19663 function, unless the text display area of the
19664 window is empty. */
19665 eassert (it->first_visible_x <= it->last_visible_x);
19668 /* Even if this display element produced no glyphs at all,
19669 we want to record its position. */
19670 if (it->bidi_p && nglyphs == 0)
19671 RECORD_MAX_MIN_POS (it);
19673 row->ascent = max (row->ascent, it->max_ascent);
19674 row->height = max (row->height, it->max_ascent + it->max_descent);
19675 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19676 row->phys_height = max (row->phys_height,
19677 it->max_phys_ascent + it->max_phys_descent);
19678 row->extra_line_spacing = max (row->extra_line_spacing,
19679 it->max_extra_line_spacing);
19681 /* End of this display line if row is continued. */
19682 if (row->continued_p || row->ends_at_zv_p)
19683 break;
19686 at_end_of_line:
19687 /* Is this a line end? If yes, we're also done, after making
19688 sure that a non-default face is extended up to the right
19689 margin of the window. */
19690 if (ITERATOR_AT_END_OF_LINE_P (it))
19692 int used_before = row->used[TEXT_AREA];
19694 row->ends_in_newline_from_string_p = STRINGP (it->object);
19696 /* Add a space at the end of the line that is used to
19697 display the cursor there. */
19698 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19699 append_space_for_newline (it, 0);
19701 /* Extend the face to the end of the line. */
19702 extend_face_to_end_of_line (it);
19704 /* Make sure we have the position. */
19705 if (used_before == 0)
19706 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19708 /* Record the position of the newline, for use in
19709 find_row_edges. */
19710 it->eol_pos = it->current.pos;
19712 /* Consume the line end. This skips over invisible lines. */
19713 set_iterator_to_next (it, 1);
19714 it->continuation_lines_width = 0;
19715 break;
19718 /* Proceed with next display element. Note that this skips
19719 over lines invisible because of selective display. */
19720 set_iterator_to_next (it, 1);
19722 /* If we truncate lines, we are done when the last displayed
19723 glyphs reach past the right margin of the window. */
19724 if (it->line_wrap == TRUNCATE
19725 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19726 ? (it->current_x >= it->last_visible_x)
19727 : (it->current_x > it->last_visible_x)))
19729 /* Maybe add truncation glyphs. */
19730 if (!FRAME_WINDOW_P (it->f)
19731 || (row->reversed_p
19732 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19733 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19735 int i, n;
19737 if (!row->reversed_p)
19739 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19740 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19741 break;
19743 else
19745 for (i = 0; i < row->used[TEXT_AREA]; i++)
19746 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19747 break;
19748 /* Remove any padding glyphs at the front of ROW, to
19749 make room for the truncation glyphs we will be
19750 adding below. The loop below always inserts at
19751 least one truncation glyph, so also remove the
19752 last glyph added to ROW. */
19753 unproduce_glyphs (it, i + 1);
19754 /* Adjust i for the loop below. */
19755 i = row->used[TEXT_AREA] - (i + 1);
19758 it->current_x = x_before;
19759 if (!FRAME_WINDOW_P (it->f))
19761 for (n = row->used[TEXT_AREA]; i < n; ++i)
19763 row->used[TEXT_AREA] = i;
19764 produce_special_glyphs (it, IT_TRUNCATION);
19767 else
19769 row->used[TEXT_AREA] = i;
19770 produce_special_glyphs (it, IT_TRUNCATION);
19773 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19775 /* Don't truncate if we can overflow newline into fringe. */
19776 if (!get_next_display_element (it))
19778 it->continuation_lines_width = 0;
19779 row->ends_at_zv_p = 1;
19780 row->exact_window_width_line_p = 1;
19781 break;
19783 if (ITERATOR_AT_END_OF_LINE_P (it))
19785 row->exact_window_width_line_p = 1;
19786 goto at_end_of_line;
19788 it->current_x = x_before;
19791 row->truncated_on_right_p = 1;
19792 it->continuation_lines_width = 0;
19793 reseat_at_next_visible_line_start (it, 0);
19794 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19795 it->hpos = hpos_before;
19796 break;
19800 if (wrap_data)
19801 bidi_unshelve_cache (wrap_data, 1);
19803 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19804 at the left window margin. */
19805 if (it->first_visible_x
19806 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19808 if (!FRAME_WINDOW_P (it->f)
19809 || (row->reversed_p
19810 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19811 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19812 insert_left_trunc_glyphs (it);
19813 row->truncated_on_left_p = 1;
19816 /* Remember the position at which this line ends.
19818 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19819 cannot be before the call to find_row_edges below, since that is
19820 where these positions are determined. */
19821 row->end = it->current;
19822 if (!it->bidi_p)
19824 row->minpos = row->start.pos;
19825 row->maxpos = row->end.pos;
19827 else
19829 /* ROW->minpos and ROW->maxpos must be the smallest and
19830 `1 + the largest' buffer positions in ROW. But if ROW was
19831 bidi-reordered, these two positions can be anywhere in the
19832 row, so we must determine them now. */
19833 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19836 /* If the start of this line is the overlay arrow-position, then
19837 mark this glyph row as the one containing the overlay arrow.
19838 This is clearly a mess with variable size fonts. It would be
19839 better to let it be displayed like cursors under X. */
19840 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19841 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19842 !NILP (overlay_arrow_string)))
19844 /* Overlay arrow in window redisplay is a fringe bitmap. */
19845 if (STRINGP (overlay_arrow_string))
19847 struct glyph_row *arrow_row
19848 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19849 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19850 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19851 struct glyph *p = row->glyphs[TEXT_AREA];
19852 struct glyph *p2, *end;
19854 /* Copy the arrow glyphs. */
19855 while (glyph < arrow_end)
19856 *p++ = *glyph++;
19858 /* Throw away padding glyphs. */
19859 p2 = p;
19860 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19861 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19862 ++p2;
19863 if (p2 > p)
19865 while (p2 < end)
19866 *p++ = *p2++;
19867 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19870 else
19872 eassert (INTEGERP (overlay_arrow_string));
19873 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19875 overlay_arrow_seen = 1;
19878 /* Highlight trailing whitespace. */
19879 if (!NILP (Vshow_trailing_whitespace))
19880 highlight_trailing_whitespace (it->f, it->glyph_row);
19882 /* Compute pixel dimensions of this line. */
19883 compute_line_metrics (it);
19885 /* Implementation note: No changes in the glyphs of ROW or in their
19886 faces can be done past this point, because compute_line_metrics
19887 computes ROW's hash value and stores it within the glyph_row
19888 structure. */
19890 /* Record whether this row ends inside an ellipsis. */
19891 row->ends_in_ellipsis_p
19892 = (it->method == GET_FROM_DISPLAY_VECTOR
19893 && it->ellipsis_p);
19895 /* Save fringe bitmaps in this row. */
19896 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19897 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19898 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19899 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19901 it->left_user_fringe_bitmap = 0;
19902 it->left_user_fringe_face_id = 0;
19903 it->right_user_fringe_bitmap = 0;
19904 it->right_user_fringe_face_id = 0;
19906 /* Maybe set the cursor. */
19907 cvpos = it->w->cursor.vpos;
19908 if ((cvpos < 0
19909 /* In bidi-reordered rows, keep checking for proper cursor
19910 position even if one has been found already, because buffer
19911 positions in such rows change non-linearly with ROW->VPOS,
19912 when a line is continued. One exception: when we are at ZV,
19913 display cursor on the first suitable glyph row, since all
19914 the empty rows after that also have their position set to ZV. */
19915 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19916 lines' rows is implemented for bidi-reordered rows. */
19917 || (it->bidi_p
19918 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19919 && PT >= MATRIX_ROW_START_CHARPOS (row)
19920 && PT <= MATRIX_ROW_END_CHARPOS (row)
19921 && cursor_row_p (row))
19922 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19924 /* Prepare for the next line. This line starts horizontally at (X
19925 HPOS) = (0 0). Vertical positions are incremented. As a
19926 convenience for the caller, IT->glyph_row is set to the next
19927 row to be used. */
19928 it->current_x = it->hpos = 0;
19929 it->current_y += row->height;
19930 SET_TEXT_POS (it->eol_pos, 0, 0);
19931 ++it->vpos;
19932 ++it->glyph_row;
19933 /* The next row should by default use the same value of the
19934 reversed_p flag as this one. set_iterator_to_next decides when
19935 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19936 the flag accordingly. */
19937 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19938 it->glyph_row->reversed_p = row->reversed_p;
19939 it->start = row->end;
19940 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19942 #undef RECORD_MAX_MIN_POS
19945 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19946 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19947 doc: /* Return paragraph direction at point in BUFFER.
19948 Value is either `left-to-right' or `right-to-left'.
19949 If BUFFER is omitted or nil, it defaults to the current buffer.
19951 Paragraph direction determines how the text in the paragraph is displayed.
19952 In left-to-right paragraphs, text begins at the left margin of the window
19953 and the reading direction is generally left to right. In right-to-left
19954 paragraphs, text begins at the right margin and is read from right to left.
19956 See also `bidi-paragraph-direction'. */)
19957 (Lisp_Object buffer)
19959 struct buffer *buf = current_buffer;
19960 struct buffer *old = buf;
19962 if (! NILP (buffer))
19964 CHECK_BUFFER (buffer);
19965 buf = XBUFFER (buffer);
19968 if (NILP (BVAR (buf, bidi_display_reordering))
19969 || NILP (BVAR (buf, enable_multibyte_characters))
19970 /* When we are loading loadup.el, the character property tables
19971 needed for bidi iteration are not yet available. */
19972 || !NILP (Vpurify_flag))
19973 return Qleft_to_right;
19974 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19975 return BVAR (buf, bidi_paragraph_direction);
19976 else
19978 /* Determine the direction from buffer text. We could try to
19979 use current_matrix if it is up to date, but this seems fast
19980 enough as it is. */
19981 struct bidi_it itb;
19982 ptrdiff_t pos = BUF_PT (buf);
19983 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19984 int c;
19985 void *itb_data = bidi_shelve_cache ();
19987 set_buffer_temp (buf);
19988 /* bidi_paragraph_init finds the base direction of the paragraph
19989 by searching forward from paragraph start. We need the base
19990 direction of the current or _previous_ paragraph, so we need
19991 to make sure we are within that paragraph. To that end, find
19992 the previous non-empty line. */
19993 if (pos >= ZV && pos > BEGV)
19994 DEC_BOTH (pos, bytepos);
19995 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19996 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19998 while ((c = FETCH_BYTE (bytepos)) == '\n'
19999 || c == ' ' || c == '\t' || c == '\f')
20001 if (bytepos <= BEGV_BYTE)
20002 break;
20003 bytepos--;
20004 pos--;
20006 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20007 bytepos--;
20009 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20010 itb.paragraph_dir = NEUTRAL_DIR;
20011 itb.string.s = NULL;
20012 itb.string.lstring = Qnil;
20013 itb.string.bufpos = 0;
20014 itb.string.unibyte = 0;
20015 /* We have no window to use here for ignoring window-specific
20016 overlays. Using NULL for window pointer will cause
20017 compute_display_string_pos to use the current buffer. */
20018 itb.w = NULL;
20019 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20020 bidi_unshelve_cache (itb_data, 0);
20021 set_buffer_temp (old);
20022 switch (itb.paragraph_dir)
20024 case L2R:
20025 return Qleft_to_right;
20026 break;
20027 case R2L:
20028 return Qright_to_left;
20029 break;
20030 default:
20031 emacs_abort ();
20038 /***********************************************************************
20039 Menu Bar
20040 ***********************************************************************/
20042 /* Redisplay the menu bar in the frame for window W.
20044 The menu bar of X frames that don't have X toolkit support is
20045 displayed in a special window W->frame->menu_bar_window.
20047 The menu bar of terminal frames is treated specially as far as
20048 glyph matrices are concerned. Menu bar lines are not part of
20049 windows, so the update is done directly on the frame matrix rows
20050 for the menu bar. */
20052 static void
20053 display_menu_bar (struct window *w)
20055 struct frame *f = XFRAME (WINDOW_FRAME (w));
20056 struct it it;
20057 Lisp_Object items;
20058 int i;
20060 /* Don't do all this for graphical frames. */
20061 #ifdef HAVE_NTGUI
20062 if (FRAME_W32_P (f))
20063 return;
20064 #endif
20065 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20066 if (FRAME_X_P (f))
20067 return;
20068 #endif
20070 #ifdef HAVE_NS
20071 if (FRAME_NS_P (f))
20072 return;
20073 #endif /* HAVE_NS */
20075 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20076 eassert (!FRAME_WINDOW_P (f));
20077 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20078 it.first_visible_x = 0;
20079 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20080 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20081 if (FRAME_WINDOW_P (f))
20083 /* Menu bar lines are displayed in the desired matrix of the
20084 dummy window menu_bar_window. */
20085 struct window *menu_w;
20086 menu_w = XWINDOW (f->menu_bar_window);
20087 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20088 MENU_FACE_ID);
20089 it.first_visible_x = 0;
20090 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20092 else
20093 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20095 /* This is a TTY frame, i.e. character hpos/vpos are used as
20096 pixel x/y. */
20097 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20098 MENU_FACE_ID);
20099 it.first_visible_x = 0;
20100 it.last_visible_x = FRAME_COLS (f);
20103 /* FIXME: This should be controlled by a user option. See the
20104 comments in redisplay_tool_bar and display_mode_line about
20105 this. */
20106 it.paragraph_embedding = L2R;
20108 /* Clear all rows of the menu bar. */
20109 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20111 struct glyph_row *row = it.glyph_row + i;
20112 clear_glyph_row (row);
20113 row->enabled_p = 1;
20114 row->full_width_p = 1;
20117 /* Display all items of the menu bar. */
20118 items = FRAME_MENU_BAR_ITEMS (it.f);
20119 for (i = 0; i < ASIZE (items); i += 4)
20121 Lisp_Object string;
20123 /* Stop at nil string. */
20124 string = AREF (items, i + 1);
20125 if (NILP (string))
20126 break;
20128 /* Remember where item was displayed. */
20129 ASET (items, i + 3, make_number (it.hpos));
20131 /* Display the item, pad with one space. */
20132 if (it.current_x < it.last_visible_x)
20133 display_string (NULL, string, Qnil, 0, 0, &it,
20134 SCHARS (string) + 1, 0, 0, -1);
20137 /* Fill out the line with spaces. */
20138 if (it.current_x < it.last_visible_x)
20139 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20141 /* Compute the total height of the lines. */
20142 compute_line_metrics (&it);
20147 /***********************************************************************
20148 Mode Line
20149 ***********************************************************************/
20151 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20152 FORCE is non-zero, redisplay mode lines unconditionally.
20153 Otherwise, redisplay only mode lines that are garbaged. Value is
20154 the number of windows whose mode lines were redisplayed. */
20156 static int
20157 redisplay_mode_lines (Lisp_Object window, int force)
20159 int nwindows = 0;
20161 while (!NILP (window))
20163 struct window *w = XWINDOW (window);
20165 if (WINDOWP (w->contents))
20166 nwindows += redisplay_mode_lines (w->contents, force);
20167 else if (force
20168 || FRAME_GARBAGED_P (XFRAME (w->frame))
20169 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20171 struct text_pos lpoint;
20172 struct buffer *old = current_buffer;
20174 /* Set the window's buffer for the mode line display. */
20175 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20176 set_buffer_internal_1 (XBUFFER (w->contents));
20178 /* Point refers normally to the selected window. For any
20179 other window, set up appropriate value. */
20180 if (!EQ (window, selected_window))
20182 struct text_pos pt;
20184 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20185 if (CHARPOS (pt) < BEGV)
20186 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20187 else if (CHARPOS (pt) > (ZV - 1))
20188 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20189 else
20190 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20193 /* Display mode lines. */
20194 clear_glyph_matrix (w->desired_matrix);
20195 if (display_mode_lines (w))
20197 ++nwindows;
20198 w->must_be_updated_p = 1;
20201 /* Restore old settings. */
20202 set_buffer_internal_1 (old);
20203 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20206 window = w->next;
20209 return nwindows;
20213 /* Display the mode and/or header line of window W. Value is the
20214 sum number of mode lines and header lines displayed. */
20216 static int
20217 display_mode_lines (struct window *w)
20219 Lisp_Object old_selected_window = selected_window;
20220 Lisp_Object old_selected_frame = selected_frame;
20221 Lisp_Object new_frame = w->frame;
20222 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20223 int n = 0;
20225 selected_frame = new_frame;
20226 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20227 or window's point, then we'd need select_window_1 here as well. */
20228 XSETWINDOW (selected_window, w);
20229 XFRAME (new_frame)->selected_window = selected_window;
20231 /* These will be set while the mode line specs are processed. */
20232 line_number_displayed = 0;
20233 w->column_number_displayed = -1;
20235 if (WINDOW_WANTS_MODELINE_P (w))
20237 struct window *sel_w = XWINDOW (old_selected_window);
20239 /* Select mode line face based on the real selected window. */
20240 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20241 BVAR (current_buffer, mode_line_format));
20242 ++n;
20245 if (WINDOW_WANTS_HEADER_LINE_P (w))
20247 display_mode_line (w, HEADER_LINE_FACE_ID,
20248 BVAR (current_buffer, header_line_format));
20249 ++n;
20252 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20253 selected_frame = old_selected_frame;
20254 selected_window = old_selected_window;
20255 return n;
20259 /* Display mode or header line of window W. FACE_ID specifies which
20260 line to display; it is either MODE_LINE_FACE_ID or
20261 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20262 display. Value is the pixel height of the mode/header line
20263 displayed. */
20265 static int
20266 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20268 struct it it;
20269 struct face *face;
20270 ptrdiff_t count = SPECPDL_INDEX ();
20272 init_iterator (&it, w, -1, -1, NULL, face_id);
20273 /* Don't extend on a previously drawn mode-line.
20274 This may happen if called from pos_visible_p. */
20275 it.glyph_row->enabled_p = 0;
20276 prepare_desired_row (it.glyph_row);
20278 it.glyph_row->mode_line_p = 1;
20280 /* FIXME: This should be controlled by a user option. But
20281 supporting such an option is not trivial, since the mode line is
20282 made up of many separate strings. */
20283 it.paragraph_embedding = L2R;
20285 record_unwind_protect (unwind_format_mode_line,
20286 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20288 mode_line_target = MODE_LINE_DISPLAY;
20290 /* Temporarily make frame's keyboard the current kboard so that
20291 kboard-local variables in the mode_line_format will get the right
20292 values. */
20293 push_kboard (FRAME_KBOARD (it.f));
20294 record_unwind_save_match_data ();
20295 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20296 pop_kboard ();
20298 unbind_to (count, Qnil);
20300 /* Fill up with spaces. */
20301 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20303 compute_line_metrics (&it);
20304 it.glyph_row->full_width_p = 1;
20305 it.glyph_row->continued_p = 0;
20306 it.glyph_row->truncated_on_left_p = 0;
20307 it.glyph_row->truncated_on_right_p = 0;
20309 /* Make a 3D mode-line have a shadow at its right end. */
20310 face = FACE_FROM_ID (it.f, face_id);
20311 extend_face_to_end_of_line (&it);
20312 if (face->box != FACE_NO_BOX)
20314 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20315 + it.glyph_row->used[TEXT_AREA] - 1);
20316 last->right_box_line_p = 1;
20319 return it.glyph_row->height;
20322 /* Move element ELT in LIST to the front of LIST.
20323 Return the updated list. */
20325 static Lisp_Object
20326 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20328 register Lisp_Object tail, prev;
20329 register Lisp_Object tem;
20331 tail = list;
20332 prev = Qnil;
20333 while (CONSP (tail))
20335 tem = XCAR (tail);
20337 if (EQ (elt, tem))
20339 /* Splice out the link TAIL. */
20340 if (NILP (prev))
20341 list = XCDR (tail);
20342 else
20343 Fsetcdr (prev, XCDR (tail));
20345 /* Now make it the first. */
20346 Fsetcdr (tail, list);
20347 return tail;
20349 else
20350 prev = tail;
20351 tail = XCDR (tail);
20352 QUIT;
20355 /* Not found--return unchanged LIST. */
20356 return list;
20359 /* Contribute ELT to the mode line for window IT->w. How it
20360 translates into text depends on its data type.
20362 IT describes the display environment in which we display, as usual.
20364 DEPTH is the depth in recursion. It is used to prevent
20365 infinite recursion here.
20367 FIELD_WIDTH is the number of characters the display of ELT should
20368 occupy in the mode line, and PRECISION is the maximum number of
20369 characters to display from ELT's representation. See
20370 display_string for details.
20372 Returns the hpos of the end of the text generated by ELT.
20374 PROPS is a property list to add to any string we encounter.
20376 If RISKY is nonzero, remove (disregard) any properties in any string
20377 we encounter, and ignore :eval and :propertize.
20379 The global variable `mode_line_target' determines whether the
20380 output is passed to `store_mode_line_noprop',
20381 `store_mode_line_string', or `display_string'. */
20383 static int
20384 display_mode_element (struct it *it, int depth, int field_width, int precision,
20385 Lisp_Object elt, Lisp_Object props, int risky)
20387 int n = 0, field, prec;
20388 int literal = 0;
20390 tail_recurse:
20391 if (depth > 100)
20392 elt = build_string ("*too-deep*");
20394 depth++;
20396 switch (XTYPE (elt))
20398 case Lisp_String:
20400 /* A string: output it and check for %-constructs within it. */
20401 unsigned char c;
20402 ptrdiff_t offset = 0;
20404 if (SCHARS (elt) > 0
20405 && (!NILP (props) || risky))
20407 Lisp_Object oprops, aelt;
20408 oprops = Ftext_properties_at (make_number (0), elt);
20410 /* If the starting string's properties are not what
20411 we want, translate the string. Also, if the string
20412 is risky, do that anyway. */
20414 if (NILP (Fequal (props, oprops)) || risky)
20416 /* If the starting string has properties,
20417 merge the specified ones onto the existing ones. */
20418 if (! NILP (oprops) && !risky)
20420 Lisp_Object tem;
20422 oprops = Fcopy_sequence (oprops);
20423 tem = props;
20424 while (CONSP (tem))
20426 oprops = Fplist_put (oprops, XCAR (tem),
20427 XCAR (XCDR (tem)));
20428 tem = XCDR (XCDR (tem));
20430 props = oprops;
20433 aelt = Fassoc (elt, mode_line_proptrans_alist);
20434 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20436 /* AELT is what we want. Move it to the front
20437 without consing. */
20438 elt = XCAR (aelt);
20439 mode_line_proptrans_alist
20440 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20442 else
20444 Lisp_Object tem;
20446 /* If AELT has the wrong props, it is useless.
20447 so get rid of it. */
20448 if (! NILP (aelt))
20449 mode_line_proptrans_alist
20450 = Fdelq (aelt, mode_line_proptrans_alist);
20452 elt = Fcopy_sequence (elt);
20453 Fset_text_properties (make_number (0), Flength (elt),
20454 props, elt);
20455 /* Add this item to mode_line_proptrans_alist. */
20456 mode_line_proptrans_alist
20457 = Fcons (Fcons (elt, props),
20458 mode_line_proptrans_alist);
20459 /* Truncate mode_line_proptrans_alist
20460 to at most 50 elements. */
20461 tem = Fnthcdr (make_number (50),
20462 mode_line_proptrans_alist);
20463 if (! NILP (tem))
20464 XSETCDR (tem, Qnil);
20469 offset = 0;
20471 if (literal)
20473 prec = precision - n;
20474 switch (mode_line_target)
20476 case MODE_LINE_NOPROP:
20477 case MODE_LINE_TITLE:
20478 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20479 break;
20480 case MODE_LINE_STRING:
20481 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20482 break;
20483 case MODE_LINE_DISPLAY:
20484 n += display_string (NULL, elt, Qnil, 0, 0, it,
20485 0, prec, 0, STRING_MULTIBYTE (elt));
20486 break;
20489 break;
20492 /* Handle the non-literal case. */
20494 while ((precision <= 0 || n < precision)
20495 && SREF (elt, offset) != 0
20496 && (mode_line_target != MODE_LINE_DISPLAY
20497 || it->current_x < it->last_visible_x))
20499 ptrdiff_t last_offset = offset;
20501 /* Advance to end of string or next format specifier. */
20502 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20505 if (offset - 1 != last_offset)
20507 ptrdiff_t nchars, nbytes;
20509 /* Output to end of string or up to '%'. Field width
20510 is length of string. Don't output more than
20511 PRECISION allows us. */
20512 offset--;
20514 prec = c_string_width (SDATA (elt) + last_offset,
20515 offset - last_offset, precision - n,
20516 &nchars, &nbytes);
20518 switch (mode_line_target)
20520 case MODE_LINE_NOPROP:
20521 case MODE_LINE_TITLE:
20522 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20523 break;
20524 case MODE_LINE_STRING:
20526 ptrdiff_t bytepos = last_offset;
20527 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20528 ptrdiff_t endpos = (precision <= 0
20529 ? string_byte_to_char (elt, offset)
20530 : charpos + nchars);
20532 n += store_mode_line_string (NULL,
20533 Fsubstring (elt, make_number (charpos),
20534 make_number (endpos)),
20535 0, 0, 0, Qnil);
20537 break;
20538 case MODE_LINE_DISPLAY:
20540 ptrdiff_t bytepos = last_offset;
20541 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20543 if (precision <= 0)
20544 nchars = string_byte_to_char (elt, offset) - charpos;
20545 n += display_string (NULL, elt, Qnil, 0, charpos,
20546 it, 0, nchars, 0,
20547 STRING_MULTIBYTE (elt));
20549 break;
20552 else /* c == '%' */
20554 ptrdiff_t percent_position = offset;
20556 /* Get the specified minimum width. Zero means
20557 don't pad. */
20558 field = 0;
20559 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20560 field = field * 10 + c - '0';
20562 /* Don't pad beyond the total padding allowed. */
20563 if (field_width - n > 0 && field > field_width - n)
20564 field = field_width - n;
20566 /* Note that either PRECISION <= 0 or N < PRECISION. */
20567 prec = precision - n;
20569 if (c == 'M')
20570 n += display_mode_element (it, depth, field, prec,
20571 Vglobal_mode_string, props,
20572 risky);
20573 else if (c != 0)
20575 bool multibyte;
20576 ptrdiff_t bytepos, charpos;
20577 const char *spec;
20578 Lisp_Object string;
20580 bytepos = percent_position;
20581 charpos = (STRING_MULTIBYTE (elt)
20582 ? string_byte_to_char (elt, bytepos)
20583 : bytepos);
20584 spec = decode_mode_spec (it->w, c, field, &string);
20585 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20587 switch (mode_line_target)
20589 case MODE_LINE_NOPROP:
20590 case MODE_LINE_TITLE:
20591 n += store_mode_line_noprop (spec, field, prec);
20592 break;
20593 case MODE_LINE_STRING:
20595 Lisp_Object tem = build_string (spec);
20596 props = Ftext_properties_at (make_number (charpos), elt);
20597 /* Should only keep face property in props */
20598 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20600 break;
20601 case MODE_LINE_DISPLAY:
20603 int nglyphs_before, nwritten;
20605 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20606 nwritten = display_string (spec, string, elt,
20607 charpos, 0, it,
20608 field, prec, 0,
20609 multibyte);
20611 /* Assign to the glyphs written above the
20612 string where the `%x' came from, position
20613 of the `%'. */
20614 if (nwritten > 0)
20616 struct glyph *glyph
20617 = (it->glyph_row->glyphs[TEXT_AREA]
20618 + nglyphs_before);
20619 int i;
20621 for (i = 0; i < nwritten; ++i)
20623 glyph[i].object = elt;
20624 glyph[i].charpos = charpos;
20627 n += nwritten;
20630 break;
20633 else /* c == 0 */
20634 break;
20638 break;
20640 case Lisp_Symbol:
20641 /* A symbol: process the value of the symbol recursively
20642 as if it appeared here directly. Avoid error if symbol void.
20643 Special case: if value of symbol is a string, output the string
20644 literally. */
20646 register Lisp_Object tem;
20648 /* If the variable is not marked as risky to set
20649 then its contents are risky to use. */
20650 if (NILP (Fget (elt, Qrisky_local_variable)))
20651 risky = 1;
20653 tem = Fboundp (elt);
20654 if (!NILP (tem))
20656 tem = Fsymbol_value (elt);
20657 /* If value is a string, output that string literally:
20658 don't check for % within it. */
20659 if (STRINGP (tem))
20660 literal = 1;
20662 if (!EQ (tem, elt))
20664 /* Give up right away for nil or t. */
20665 elt = tem;
20666 goto tail_recurse;
20670 break;
20672 case Lisp_Cons:
20674 register Lisp_Object car, tem;
20676 /* A cons cell: five distinct cases.
20677 If first element is :eval or :propertize, do something special.
20678 If first element is a string or a cons, process all the elements
20679 and effectively concatenate them.
20680 If first element is a negative number, truncate displaying cdr to
20681 at most that many characters. If positive, pad (with spaces)
20682 to at least that many characters.
20683 If first element is a symbol, process the cadr or caddr recursively
20684 according to whether the symbol's value is non-nil or nil. */
20685 car = XCAR (elt);
20686 if (EQ (car, QCeval))
20688 /* An element of the form (:eval FORM) means evaluate FORM
20689 and use the result as mode line elements. */
20691 if (risky)
20692 break;
20694 if (CONSP (XCDR (elt)))
20696 Lisp_Object spec;
20697 spec = safe_eval (XCAR (XCDR (elt)));
20698 n += display_mode_element (it, depth, field_width - n,
20699 precision - n, spec, props,
20700 risky);
20703 else if (EQ (car, QCpropertize))
20705 /* An element of the form (:propertize ELT PROPS...)
20706 means display ELT but applying properties PROPS. */
20708 if (risky)
20709 break;
20711 if (CONSP (XCDR (elt)))
20712 n += display_mode_element (it, depth, field_width - n,
20713 precision - n, XCAR (XCDR (elt)),
20714 XCDR (XCDR (elt)), risky);
20716 else if (SYMBOLP (car))
20718 tem = Fboundp (car);
20719 elt = XCDR (elt);
20720 if (!CONSP (elt))
20721 goto invalid;
20722 /* elt is now the cdr, and we know it is a cons cell.
20723 Use its car if CAR has a non-nil value. */
20724 if (!NILP (tem))
20726 tem = Fsymbol_value (car);
20727 if (!NILP (tem))
20729 elt = XCAR (elt);
20730 goto tail_recurse;
20733 /* Symbol's value is nil (or symbol is unbound)
20734 Get the cddr of the original list
20735 and if possible find the caddr and use that. */
20736 elt = XCDR (elt);
20737 if (NILP (elt))
20738 break;
20739 else if (!CONSP (elt))
20740 goto invalid;
20741 elt = XCAR (elt);
20742 goto tail_recurse;
20744 else if (INTEGERP (car))
20746 register int lim = XINT (car);
20747 elt = XCDR (elt);
20748 if (lim < 0)
20750 /* Negative int means reduce maximum width. */
20751 if (precision <= 0)
20752 precision = -lim;
20753 else
20754 precision = min (precision, -lim);
20756 else if (lim > 0)
20758 /* Padding specified. Don't let it be more than
20759 current maximum. */
20760 if (precision > 0)
20761 lim = min (precision, lim);
20763 /* If that's more padding than already wanted, queue it.
20764 But don't reduce padding already specified even if
20765 that is beyond the current truncation point. */
20766 field_width = max (lim, field_width);
20768 goto tail_recurse;
20770 else if (STRINGP (car) || CONSP (car))
20772 Lisp_Object halftail = elt;
20773 int len = 0;
20775 while (CONSP (elt)
20776 && (precision <= 0 || n < precision))
20778 n += display_mode_element (it, depth,
20779 /* Do padding only after the last
20780 element in the list. */
20781 (! CONSP (XCDR (elt))
20782 ? field_width - n
20783 : 0),
20784 precision - n, XCAR (elt),
20785 props, risky);
20786 elt = XCDR (elt);
20787 len++;
20788 if ((len & 1) == 0)
20789 halftail = XCDR (halftail);
20790 /* Check for cycle. */
20791 if (EQ (halftail, elt))
20792 break;
20796 break;
20798 default:
20799 invalid:
20800 elt = build_string ("*invalid*");
20801 goto tail_recurse;
20804 /* Pad to FIELD_WIDTH. */
20805 if (field_width > 0 && n < field_width)
20807 switch (mode_line_target)
20809 case MODE_LINE_NOPROP:
20810 case MODE_LINE_TITLE:
20811 n += store_mode_line_noprop ("", field_width - n, 0);
20812 break;
20813 case MODE_LINE_STRING:
20814 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20815 break;
20816 case MODE_LINE_DISPLAY:
20817 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20818 0, 0, 0);
20819 break;
20823 return n;
20826 /* Store a mode-line string element in mode_line_string_list.
20828 If STRING is non-null, display that C string. Otherwise, the Lisp
20829 string LISP_STRING is displayed.
20831 FIELD_WIDTH is the minimum number of output glyphs to produce.
20832 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20833 with spaces. FIELD_WIDTH <= 0 means don't pad.
20835 PRECISION is the maximum number of characters to output from
20836 STRING. PRECISION <= 0 means don't truncate the string.
20838 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20839 properties to the string.
20841 PROPS are the properties to add to the string.
20842 The mode_line_string_face face property is always added to the string.
20845 static int
20846 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20847 int field_width, int precision, Lisp_Object props)
20849 ptrdiff_t len;
20850 int n = 0;
20852 if (string != NULL)
20854 len = strlen (string);
20855 if (precision > 0 && len > precision)
20856 len = precision;
20857 lisp_string = make_string (string, len);
20858 if (NILP (props))
20859 props = mode_line_string_face_prop;
20860 else if (!NILP (mode_line_string_face))
20862 Lisp_Object face = Fplist_get (props, Qface);
20863 props = Fcopy_sequence (props);
20864 if (NILP (face))
20865 face = mode_line_string_face;
20866 else
20867 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20868 props = Fplist_put (props, Qface, face);
20870 Fadd_text_properties (make_number (0), make_number (len),
20871 props, lisp_string);
20873 else
20875 len = XFASTINT (Flength (lisp_string));
20876 if (precision > 0 && len > precision)
20878 len = precision;
20879 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20880 precision = -1;
20882 if (!NILP (mode_line_string_face))
20884 Lisp_Object face;
20885 if (NILP (props))
20886 props = Ftext_properties_at (make_number (0), lisp_string);
20887 face = Fplist_get (props, Qface);
20888 if (NILP (face))
20889 face = mode_line_string_face;
20890 else
20891 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20892 props = Fcons (Qface, Fcons (face, Qnil));
20893 if (copy_string)
20894 lisp_string = Fcopy_sequence (lisp_string);
20896 if (!NILP (props))
20897 Fadd_text_properties (make_number (0), make_number (len),
20898 props, lisp_string);
20901 if (len > 0)
20903 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20904 n += len;
20907 if (field_width > len)
20909 field_width -= len;
20910 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20911 if (!NILP (props))
20912 Fadd_text_properties (make_number (0), make_number (field_width),
20913 props, lisp_string);
20914 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20915 n += field_width;
20918 return n;
20922 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20923 1, 4, 0,
20924 doc: /* Format a string out of a mode line format specification.
20925 First arg FORMAT specifies the mode line format (see `mode-line-format'
20926 for details) to use.
20928 By default, the format is evaluated for the currently selected window.
20930 Optional second arg FACE specifies the face property to put on all
20931 characters for which no face is specified. The value nil means the
20932 default face. The value t means whatever face the window's mode line
20933 currently uses (either `mode-line' or `mode-line-inactive',
20934 depending on whether the window is the selected window or not).
20935 An integer value means the value string has no text
20936 properties.
20938 Optional third and fourth args WINDOW and BUFFER specify the window
20939 and buffer to use as the context for the formatting (defaults
20940 are the selected window and the WINDOW's buffer). */)
20941 (Lisp_Object format, Lisp_Object face,
20942 Lisp_Object window, Lisp_Object buffer)
20944 struct it it;
20945 int len;
20946 struct window *w;
20947 struct buffer *old_buffer = NULL;
20948 int face_id;
20949 int no_props = INTEGERP (face);
20950 ptrdiff_t count = SPECPDL_INDEX ();
20951 Lisp_Object str;
20952 int string_start = 0;
20954 w = decode_any_window (window);
20955 XSETWINDOW (window, w);
20957 if (NILP (buffer))
20958 buffer = w->contents;
20959 CHECK_BUFFER (buffer);
20961 /* Make formatting the modeline a non-op when noninteractive, otherwise
20962 there will be problems later caused by a partially initialized frame. */
20963 if (NILP (format) || noninteractive)
20964 return empty_unibyte_string;
20966 if (no_props)
20967 face = Qnil;
20969 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20970 : EQ (face, Qt) ? (EQ (window, selected_window)
20971 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20972 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20973 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20974 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20975 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20976 : DEFAULT_FACE_ID;
20978 old_buffer = current_buffer;
20980 /* Save things including mode_line_proptrans_alist,
20981 and set that to nil so that we don't alter the outer value. */
20982 record_unwind_protect (unwind_format_mode_line,
20983 format_mode_line_unwind_data
20984 (XFRAME (WINDOW_FRAME (w)),
20985 old_buffer, selected_window, 1));
20986 mode_line_proptrans_alist = Qnil;
20988 Fselect_window (window, Qt);
20989 set_buffer_internal_1 (XBUFFER (buffer));
20991 init_iterator (&it, w, -1, -1, NULL, face_id);
20993 if (no_props)
20995 mode_line_target = MODE_LINE_NOPROP;
20996 mode_line_string_face_prop = Qnil;
20997 mode_line_string_list = Qnil;
20998 string_start = MODE_LINE_NOPROP_LEN (0);
21000 else
21002 mode_line_target = MODE_LINE_STRING;
21003 mode_line_string_list = Qnil;
21004 mode_line_string_face = face;
21005 mode_line_string_face_prop
21006 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21009 push_kboard (FRAME_KBOARD (it.f));
21010 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21011 pop_kboard ();
21013 if (no_props)
21015 len = MODE_LINE_NOPROP_LEN (string_start);
21016 str = make_string (mode_line_noprop_buf + string_start, len);
21018 else
21020 mode_line_string_list = Fnreverse (mode_line_string_list);
21021 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21022 empty_unibyte_string);
21025 unbind_to (count, Qnil);
21026 return str;
21029 /* Write a null-terminated, right justified decimal representation of
21030 the positive integer D to BUF using a minimal field width WIDTH. */
21032 static void
21033 pint2str (register char *buf, register int width, register ptrdiff_t d)
21035 register char *p = buf;
21037 if (d <= 0)
21038 *p++ = '0';
21039 else
21041 while (d > 0)
21043 *p++ = d % 10 + '0';
21044 d /= 10;
21048 for (width -= (int) (p - buf); width > 0; --width)
21049 *p++ = ' ';
21050 *p-- = '\0';
21051 while (p > buf)
21053 d = *buf;
21054 *buf++ = *p;
21055 *p-- = d;
21059 /* Write a null-terminated, right justified decimal and "human
21060 readable" representation of the nonnegative integer D to BUF using
21061 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21063 static const char power_letter[] =
21065 0, /* no letter */
21066 'k', /* kilo */
21067 'M', /* mega */
21068 'G', /* giga */
21069 'T', /* tera */
21070 'P', /* peta */
21071 'E', /* exa */
21072 'Z', /* zetta */
21073 'Y' /* yotta */
21076 static void
21077 pint2hrstr (char *buf, int width, ptrdiff_t d)
21079 /* We aim to represent the nonnegative integer D as
21080 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21081 ptrdiff_t quotient = d;
21082 int remainder = 0;
21083 /* -1 means: do not use TENTHS. */
21084 int tenths = -1;
21085 int exponent = 0;
21087 /* Length of QUOTIENT.TENTHS as a string. */
21088 int length;
21090 char * psuffix;
21091 char * p;
21093 if (quotient >= 1000)
21095 /* Scale to the appropriate EXPONENT. */
21098 remainder = quotient % 1000;
21099 quotient /= 1000;
21100 exponent++;
21102 while (quotient >= 1000);
21104 /* Round to nearest and decide whether to use TENTHS or not. */
21105 if (quotient <= 9)
21107 tenths = remainder / 100;
21108 if (remainder % 100 >= 50)
21110 if (tenths < 9)
21111 tenths++;
21112 else
21114 quotient++;
21115 if (quotient == 10)
21116 tenths = -1;
21117 else
21118 tenths = 0;
21122 else
21123 if (remainder >= 500)
21125 if (quotient < 999)
21126 quotient++;
21127 else
21129 quotient = 1;
21130 exponent++;
21131 tenths = 0;
21136 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21137 if (tenths == -1 && quotient <= 99)
21138 if (quotient <= 9)
21139 length = 1;
21140 else
21141 length = 2;
21142 else
21143 length = 3;
21144 p = psuffix = buf + max (width, length);
21146 /* Print EXPONENT. */
21147 *psuffix++ = power_letter[exponent];
21148 *psuffix = '\0';
21150 /* Print TENTHS. */
21151 if (tenths >= 0)
21153 *--p = '0' + tenths;
21154 *--p = '.';
21157 /* Print QUOTIENT. */
21160 int digit = quotient % 10;
21161 *--p = '0' + digit;
21163 while ((quotient /= 10) != 0);
21165 /* Print leading spaces. */
21166 while (buf < p)
21167 *--p = ' ';
21170 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21171 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21172 type of CODING_SYSTEM. Return updated pointer into BUF. */
21174 static unsigned char invalid_eol_type[] = "(*invalid*)";
21176 static char *
21177 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21179 Lisp_Object val;
21180 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21181 const unsigned char *eol_str;
21182 int eol_str_len;
21183 /* The EOL conversion we are using. */
21184 Lisp_Object eoltype;
21186 val = CODING_SYSTEM_SPEC (coding_system);
21187 eoltype = Qnil;
21189 if (!VECTORP (val)) /* Not yet decided. */
21191 *buf++ = multibyte ? '-' : ' ';
21192 if (eol_flag)
21193 eoltype = eol_mnemonic_undecided;
21194 /* Don't mention EOL conversion if it isn't decided. */
21196 else
21198 Lisp_Object attrs;
21199 Lisp_Object eolvalue;
21201 attrs = AREF (val, 0);
21202 eolvalue = AREF (val, 2);
21204 *buf++ = multibyte
21205 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21206 : ' ';
21208 if (eol_flag)
21210 /* The EOL conversion that is normal on this system. */
21212 if (NILP (eolvalue)) /* Not yet decided. */
21213 eoltype = eol_mnemonic_undecided;
21214 else if (VECTORP (eolvalue)) /* Not yet decided. */
21215 eoltype = eol_mnemonic_undecided;
21216 else /* eolvalue is Qunix, Qdos, or Qmac. */
21217 eoltype = (EQ (eolvalue, Qunix)
21218 ? eol_mnemonic_unix
21219 : (EQ (eolvalue, Qdos) == 1
21220 ? eol_mnemonic_dos : eol_mnemonic_mac));
21224 if (eol_flag)
21226 /* Mention the EOL conversion if it is not the usual one. */
21227 if (STRINGP (eoltype))
21229 eol_str = SDATA (eoltype);
21230 eol_str_len = SBYTES (eoltype);
21232 else if (CHARACTERP (eoltype))
21234 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21235 int c = XFASTINT (eoltype);
21236 eol_str_len = CHAR_STRING (c, tmp);
21237 eol_str = tmp;
21239 else
21241 eol_str = invalid_eol_type;
21242 eol_str_len = sizeof (invalid_eol_type) - 1;
21244 memcpy (buf, eol_str, eol_str_len);
21245 buf += eol_str_len;
21248 return buf;
21251 /* Return a string for the output of a mode line %-spec for window W,
21252 generated by character C. FIELD_WIDTH > 0 means pad the string
21253 returned with spaces to that value. Return a Lisp string in
21254 *STRING if the resulting string is taken from that Lisp string.
21256 Note we operate on the current buffer for most purposes. */
21258 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21260 static const char *
21261 decode_mode_spec (struct window *w, register int c, int field_width,
21262 Lisp_Object *string)
21264 Lisp_Object obj;
21265 struct frame *f = XFRAME (WINDOW_FRAME (w));
21266 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21267 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21268 produce strings from numerical values, so limit preposterously
21269 large values of FIELD_WIDTH to avoid overrunning the buffer's
21270 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21271 bytes plus the terminating null. */
21272 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21273 struct buffer *b = current_buffer;
21275 obj = Qnil;
21276 *string = Qnil;
21278 switch (c)
21280 case '*':
21281 if (!NILP (BVAR (b, read_only)))
21282 return "%";
21283 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21284 return "*";
21285 return "-";
21287 case '+':
21288 /* This differs from %* only for a modified read-only buffer. */
21289 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21290 return "*";
21291 if (!NILP (BVAR (b, read_only)))
21292 return "%";
21293 return "-";
21295 case '&':
21296 /* This differs from %* in ignoring read-only-ness. */
21297 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21298 return "*";
21299 return "-";
21301 case '%':
21302 return "%";
21304 case '[':
21306 int i;
21307 char *p;
21309 if (command_loop_level > 5)
21310 return "[[[... ";
21311 p = decode_mode_spec_buf;
21312 for (i = 0; i < command_loop_level; i++)
21313 *p++ = '[';
21314 *p = 0;
21315 return decode_mode_spec_buf;
21318 case ']':
21320 int i;
21321 char *p;
21323 if (command_loop_level > 5)
21324 return " ...]]]";
21325 p = decode_mode_spec_buf;
21326 for (i = 0; i < command_loop_level; i++)
21327 *p++ = ']';
21328 *p = 0;
21329 return decode_mode_spec_buf;
21332 case '-':
21334 register int i;
21336 /* Let lots_of_dashes be a string of infinite length. */
21337 if (mode_line_target == MODE_LINE_NOPROP
21338 || mode_line_target == MODE_LINE_STRING)
21339 return "--";
21340 if (field_width <= 0
21341 || field_width > sizeof (lots_of_dashes))
21343 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21344 decode_mode_spec_buf[i] = '-';
21345 decode_mode_spec_buf[i] = '\0';
21346 return decode_mode_spec_buf;
21348 else
21349 return lots_of_dashes;
21352 case 'b':
21353 obj = BVAR (b, name);
21354 break;
21356 case 'c':
21357 /* %c and %l are ignored in `frame-title-format'.
21358 (In redisplay_internal, the frame title is drawn _before_ the
21359 windows are updated, so the stuff which depends on actual
21360 window contents (such as %l) may fail to render properly, or
21361 even crash emacs.) */
21362 if (mode_line_target == MODE_LINE_TITLE)
21363 return "";
21364 else
21366 ptrdiff_t col = current_column ();
21367 w->column_number_displayed = col;
21368 pint2str (decode_mode_spec_buf, width, col);
21369 return decode_mode_spec_buf;
21372 case 'e':
21373 #ifndef SYSTEM_MALLOC
21375 if (NILP (Vmemory_full))
21376 return "";
21377 else
21378 return "!MEM FULL! ";
21380 #else
21381 return "";
21382 #endif
21384 case 'F':
21385 /* %F displays the frame name. */
21386 if (!NILP (f->title))
21387 return SSDATA (f->title);
21388 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21389 return SSDATA (f->name);
21390 return "Emacs";
21392 case 'f':
21393 obj = BVAR (b, filename);
21394 break;
21396 case 'i':
21398 ptrdiff_t size = ZV - BEGV;
21399 pint2str (decode_mode_spec_buf, width, size);
21400 return decode_mode_spec_buf;
21403 case 'I':
21405 ptrdiff_t size = ZV - BEGV;
21406 pint2hrstr (decode_mode_spec_buf, width, size);
21407 return decode_mode_spec_buf;
21410 case 'l':
21412 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21413 ptrdiff_t topline, nlines, height;
21414 ptrdiff_t junk;
21416 /* %c and %l are ignored in `frame-title-format'. */
21417 if (mode_line_target == MODE_LINE_TITLE)
21418 return "";
21420 startpos = marker_position (w->start);
21421 startpos_byte = marker_byte_position (w->start);
21422 height = WINDOW_TOTAL_LINES (w);
21424 /* If we decided that this buffer isn't suitable for line numbers,
21425 don't forget that too fast. */
21426 if (w->base_line_pos == -1)
21427 goto no_value;
21429 /* If the buffer is very big, don't waste time. */
21430 if (INTEGERP (Vline_number_display_limit)
21431 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21433 w->base_line_pos = 0;
21434 w->base_line_number = 0;
21435 goto no_value;
21438 if (w->base_line_number > 0
21439 && w->base_line_pos > 0
21440 && w->base_line_pos <= startpos)
21442 line = w->base_line_number;
21443 linepos = w->base_line_pos;
21444 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21446 else
21448 line = 1;
21449 linepos = BUF_BEGV (b);
21450 linepos_byte = BUF_BEGV_BYTE (b);
21453 /* Count lines from base line to window start position. */
21454 nlines = display_count_lines (linepos_byte,
21455 startpos_byte,
21456 startpos, &junk);
21458 topline = nlines + line;
21460 /* Determine a new base line, if the old one is too close
21461 or too far away, or if we did not have one.
21462 "Too close" means it's plausible a scroll-down would
21463 go back past it. */
21464 if (startpos == BUF_BEGV (b))
21466 w->base_line_number = topline;
21467 w->base_line_pos = BUF_BEGV (b);
21469 else if (nlines < height + 25 || nlines > height * 3 + 50
21470 || linepos == BUF_BEGV (b))
21472 ptrdiff_t limit = BUF_BEGV (b);
21473 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21474 ptrdiff_t position;
21475 ptrdiff_t distance =
21476 (height * 2 + 30) * line_number_display_limit_width;
21478 if (startpos - distance > limit)
21480 limit = startpos - distance;
21481 limit_byte = CHAR_TO_BYTE (limit);
21484 nlines = display_count_lines (startpos_byte,
21485 limit_byte,
21486 - (height * 2 + 30),
21487 &position);
21488 /* If we couldn't find the lines we wanted within
21489 line_number_display_limit_width chars per line,
21490 give up on line numbers for this window. */
21491 if (position == limit_byte && limit == startpos - distance)
21493 w->base_line_pos = -1;
21494 w->base_line_number = 0;
21495 goto no_value;
21498 w->base_line_number = topline - nlines;
21499 w->base_line_pos = BYTE_TO_CHAR (position);
21502 /* Now count lines from the start pos to point. */
21503 nlines = display_count_lines (startpos_byte,
21504 PT_BYTE, PT, &junk);
21506 /* Record that we did display the line number. */
21507 line_number_displayed = 1;
21509 /* Make the string to show. */
21510 pint2str (decode_mode_spec_buf, width, topline + nlines);
21511 return decode_mode_spec_buf;
21512 no_value:
21514 char* p = decode_mode_spec_buf;
21515 int pad = width - 2;
21516 while (pad-- > 0)
21517 *p++ = ' ';
21518 *p++ = '?';
21519 *p++ = '?';
21520 *p = '\0';
21521 return decode_mode_spec_buf;
21524 break;
21526 case 'm':
21527 obj = BVAR (b, mode_name);
21528 break;
21530 case 'n':
21531 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21532 return " Narrow";
21533 break;
21535 case 'p':
21537 ptrdiff_t pos = marker_position (w->start);
21538 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21540 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21542 if (pos <= BUF_BEGV (b))
21543 return "All";
21544 else
21545 return "Bottom";
21547 else if (pos <= BUF_BEGV (b))
21548 return "Top";
21549 else
21551 if (total > 1000000)
21552 /* Do it differently for a large value, to avoid overflow. */
21553 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21554 else
21555 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21556 /* We can't normally display a 3-digit number,
21557 so get us a 2-digit number that is close. */
21558 if (total == 100)
21559 total = 99;
21560 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21561 return decode_mode_spec_buf;
21565 /* Display percentage of size above the bottom of the screen. */
21566 case 'P':
21568 ptrdiff_t toppos = marker_position (w->start);
21569 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21570 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21572 if (botpos >= BUF_ZV (b))
21574 if (toppos <= BUF_BEGV (b))
21575 return "All";
21576 else
21577 return "Bottom";
21579 else
21581 if (total > 1000000)
21582 /* Do it differently for a large value, to avoid overflow. */
21583 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21584 else
21585 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21586 /* We can't normally display a 3-digit number,
21587 so get us a 2-digit number that is close. */
21588 if (total == 100)
21589 total = 99;
21590 if (toppos <= BUF_BEGV (b))
21591 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21592 else
21593 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21594 return decode_mode_spec_buf;
21598 case 's':
21599 /* status of process */
21600 obj = Fget_buffer_process (Fcurrent_buffer ());
21601 if (NILP (obj))
21602 return "no process";
21603 #ifndef MSDOS
21604 obj = Fsymbol_name (Fprocess_status (obj));
21605 #endif
21606 break;
21608 case '@':
21610 ptrdiff_t count = inhibit_garbage_collection ();
21611 Lisp_Object val = call1 (intern ("file-remote-p"),
21612 BVAR (current_buffer, directory));
21613 unbind_to (count, Qnil);
21615 if (NILP (val))
21616 return "-";
21617 else
21618 return "@";
21621 case 'z':
21622 /* coding-system (not including end-of-line format) */
21623 case 'Z':
21624 /* coding-system (including end-of-line type) */
21626 int eol_flag = (c == 'Z');
21627 char *p = decode_mode_spec_buf;
21629 if (! FRAME_WINDOW_P (f))
21631 /* No need to mention EOL here--the terminal never needs
21632 to do EOL conversion. */
21633 p = decode_mode_spec_coding (CODING_ID_NAME
21634 (FRAME_KEYBOARD_CODING (f)->id),
21635 p, 0);
21636 p = decode_mode_spec_coding (CODING_ID_NAME
21637 (FRAME_TERMINAL_CODING (f)->id),
21638 p, 0);
21640 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21641 p, eol_flag);
21643 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21644 #ifdef subprocesses
21645 obj = Fget_buffer_process (Fcurrent_buffer ());
21646 if (PROCESSP (obj))
21648 p = decode_mode_spec_coding
21649 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21650 p = decode_mode_spec_coding
21651 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21653 #endif /* subprocesses */
21654 #endif /* 0 */
21655 *p = 0;
21656 return decode_mode_spec_buf;
21660 if (STRINGP (obj))
21662 *string = obj;
21663 return SSDATA (obj);
21665 else
21666 return "";
21670 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
21671 means count lines back from START_BYTE. But don't go beyond
21672 LIMIT_BYTE. Return the number of lines thus found (always
21673 nonnegative).
21675 Set *BYTE_POS_PTR to the byte position where we stopped. This is
21676 either the position COUNT lines after/before START_BYTE, if we
21677 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
21678 COUNT lines. */
21680 static ptrdiff_t
21681 display_count_lines (ptrdiff_t start_byte,
21682 ptrdiff_t limit_byte, ptrdiff_t count,
21683 ptrdiff_t *byte_pos_ptr)
21685 register unsigned char *cursor;
21686 unsigned char *base;
21688 register ptrdiff_t ceiling;
21689 register unsigned char *ceiling_addr;
21690 ptrdiff_t orig_count = count;
21692 /* If we are not in selective display mode,
21693 check only for newlines. */
21694 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21695 && !INTEGERP (BVAR (current_buffer, selective_display)));
21697 if (count > 0)
21699 while (start_byte < limit_byte)
21701 ceiling = BUFFER_CEILING_OF (start_byte);
21702 ceiling = min (limit_byte - 1, ceiling);
21703 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21704 base = (cursor = BYTE_POS_ADDR (start_byte));
21708 if (selective_display)
21710 while (*cursor != '\n' && *cursor != 015
21711 && ++cursor != ceiling_addr)
21712 continue;
21713 if (cursor == ceiling_addr)
21714 break;
21716 else
21718 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
21719 if (! cursor)
21720 break;
21723 cursor++;
21725 if (--count == 0)
21727 start_byte += cursor - base;
21728 *byte_pos_ptr = start_byte;
21729 return orig_count;
21732 while (cursor < ceiling_addr);
21734 start_byte += ceiling_addr - base;
21737 else
21739 while (start_byte > limit_byte)
21741 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21742 ceiling = max (limit_byte, ceiling);
21743 ceiling_addr = BYTE_POS_ADDR (ceiling);
21744 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21745 while (1)
21747 if (selective_display)
21749 while (--cursor >= ceiling_addr
21750 && *cursor != '\n' && *cursor != 015)
21751 continue;
21752 if (cursor < ceiling_addr)
21753 break;
21755 else
21757 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
21758 if (! cursor)
21759 break;
21762 if (++count == 0)
21764 start_byte += cursor - base + 1;
21765 *byte_pos_ptr = start_byte;
21766 /* When scanning backwards, we should
21767 not count the newline posterior to which we stop. */
21768 return - orig_count - 1;
21771 start_byte += ceiling_addr - base;
21775 *byte_pos_ptr = limit_byte;
21777 if (count < 0)
21778 return - orig_count + count;
21779 return orig_count - count;
21785 /***********************************************************************
21786 Displaying strings
21787 ***********************************************************************/
21789 /* Display a NUL-terminated string, starting with index START.
21791 If STRING is non-null, display that C string. Otherwise, the Lisp
21792 string LISP_STRING is displayed. There's a case that STRING is
21793 non-null and LISP_STRING is not nil. It means STRING is a string
21794 data of LISP_STRING. In that case, we display LISP_STRING while
21795 ignoring its text properties.
21797 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21798 FACE_STRING. Display STRING or LISP_STRING with the face at
21799 FACE_STRING_POS in FACE_STRING:
21801 Display the string in the environment given by IT, but use the
21802 standard display table, temporarily.
21804 FIELD_WIDTH is the minimum number of output glyphs to produce.
21805 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21806 with spaces. If STRING has more characters, more than FIELD_WIDTH
21807 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21809 PRECISION is the maximum number of characters to output from
21810 STRING. PRECISION < 0 means don't truncate the string.
21812 This is roughly equivalent to printf format specifiers:
21814 FIELD_WIDTH PRECISION PRINTF
21815 ----------------------------------------
21816 -1 -1 %s
21817 -1 10 %.10s
21818 10 -1 %10s
21819 20 10 %20.10s
21821 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21822 display them, and < 0 means obey the current buffer's value of
21823 enable_multibyte_characters.
21825 Value is the number of columns displayed. */
21827 static int
21828 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21829 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21830 int field_width, int precision, int max_x, int multibyte)
21832 int hpos_at_start = it->hpos;
21833 int saved_face_id = it->face_id;
21834 struct glyph_row *row = it->glyph_row;
21835 ptrdiff_t it_charpos;
21837 /* Initialize the iterator IT for iteration over STRING beginning
21838 with index START. */
21839 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21840 precision, field_width, multibyte);
21841 if (string && STRINGP (lisp_string))
21842 /* LISP_STRING is the one returned by decode_mode_spec. We should
21843 ignore its text properties. */
21844 it->stop_charpos = it->end_charpos;
21846 /* If displaying STRING, set up the face of the iterator from
21847 FACE_STRING, if that's given. */
21848 if (STRINGP (face_string))
21850 ptrdiff_t endptr;
21851 struct face *face;
21853 it->face_id
21854 = face_at_string_position (it->w, face_string, face_string_pos,
21855 0, it->region_beg_charpos,
21856 it->region_end_charpos,
21857 &endptr, it->base_face_id, 0);
21858 face = FACE_FROM_ID (it->f, it->face_id);
21859 it->face_box_p = face->box != FACE_NO_BOX;
21862 /* Set max_x to the maximum allowed X position. Don't let it go
21863 beyond the right edge of the window. */
21864 if (max_x <= 0)
21865 max_x = it->last_visible_x;
21866 else
21867 max_x = min (max_x, it->last_visible_x);
21869 /* Skip over display elements that are not visible. because IT->w is
21870 hscrolled. */
21871 if (it->current_x < it->first_visible_x)
21872 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21873 MOVE_TO_POS | MOVE_TO_X);
21875 row->ascent = it->max_ascent;
21876 row->height = it->max_ascent + it->max_descent;
21877 row->phys_ascent = it->max_phys_ascent;
21878 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21879 row->extra_line_spacing = it->max_extra_line_spacing;
21881 if (STRINGP (it->string))
21882 it_charpos = IT_STRING_CHARPOS (*it);
21883 else
21884 it_charpos = IT_CHARPOS (*it);
21886 /* This condition is for the case that we are called with current_x
21887 past last_visible_x. */
21888 while (it->current_x < max_x)
21890 int x_before, x, n_glyphs_before, i, nglyphs;
21892 /* Get the next display element. */
21893 if (!get_next_display_element (it))
21894 break;
21896 /* Produce glyphs. */
21897 x_before = it->current_x;
21898 n_glyphs_before = row->used[TEXT_AREA];
21899 PRODUCE_GLYPHS (it);
21901 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21902 i = 0;
21903 x = x_before;
21904 while (i < nglyphs)
21906 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21908 if (it->line_wrap != TRUNCATE
21909 && x + glyph->pixel_width > max_x)
21911 /* End of continued line or max_x reached. */
21912 if (CHAR_GLYPH_PADDING_P (*glyph))
21914 /* A wide character is unbreakable. */
21915 if (row->reversed_p)
21916 unproduce_glyphs (it, row->used[TEXT_AREA]
21917 - n_glyphs_before);
21918 row->used[TEXT_AREA] = n_glyphs_before;
21919 it->current_x = x_before;
21921 else
21923 if (row->reversed_p)
21924 unproduce_glyphs (it, row->used[TEXT_AREA]
21925 - (n_glyphs_before + i));
21926 row->used[TEXT_AREA] = n_glyphs_before + i;
21927 it->current_x = x;
21929 break;
21931 else if (x + glyph->pixel_width >= it->first_visible_x)
21933 /* Glyph is at least partially visible. */
21934 ++it->hpos;
21935 if (x < it->first_visible_x)
21936 row->x = x - it->first_visible_x;
21938 else
21940 /* Glyph is off the left margin of the display area.
21941 Should not happen. */
21942 emacs_abort ();
21945 row->ascent = max (row->ascent, it->max_ascent);
21946 row->height = max (row->height, it->max_ascent + it->max_descent);
21947 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21948 row->phys_height = max (row->phys_height,
21949 it->max_phys_ascent + it->max_phys_descent);
21950 row->extra_line_spacing = max (row->extra_line_spacing,
21951 it->max_extra_line_spacing);
21952 x += glyph->pixel_width;
21953 ++i;
21956 /* Stop if max_x reached. */
21957 if (i < nglyphs)
21958 break;
21960 /* Stop at line ends. */
21961 if (ITERATOR_AT_END_OF_LINE_P (it))
21963 it->continuation_lines_width = 0;
21964 break;
21967 set_iterator_to_next (it, 1);
21968 if (STRINGP (it->string))
21969 it_charpos = IT_STRING_CHARPOS (*it);
21970 else
21971 it_charpos = IT_CHARPOS (*it);
21973 /* Stop if truncating at the right edge. */
21974 if (it->line_wrap == TRUNCATE
21975 && it->current_x >= it->last_visible_x)
21977 /* Add truncation mark, but don't do it if the line is
21978 truncated at a padding space. */
21979 if (it_charpos < it->string_nchars)
21981 if (!FRAME_WINDOW_P (it->f))
21983 int ii, n;
21985 if (it->current_x > it->last_visible_x)
21987 if (!row->reversed_p)
21989 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21990 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21991 break;
21993 else
21995 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21996 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21997 break;
21998 unproduce_glyphs (it, ii + 1);
21999 ii = row->used[TEXT_AREA] - (ii + 1);
22001 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22003 row->used[TEXT_AREA] = ii;
22004 produce_special_glyphs (it, IT_TRUNCATION);
22007 produce_special_glyphs (it, IT_TRUNCATION);
22009 row->truncated_on_right_p = 1;
22011 break;
22015 /* Maybe insert a truncation at the left. */
22016 if (it->first_visible_x
22017 && it_charpos > 0)
22019 if (!FRAME_WINDOW_P (it->f)
22020 || (row->reversed_p
22021 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22022 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22023 insert_left_trunc_glyphs (it);
22024 row->truncated_on_left_p = 1;
22027 it->face_id = saved_face_id;
22029 /* Value is number of columns displayed. */
22030 return it->hpos - hpos_at_start;
22035 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22036 appears as an element of LIST or as the car of an element of LIST.
22037 If PROPVAL is a list, compare each element against LIST in that
22038 way, and return 1/2 if any element of PROPVAL is found in LIST.
22039 Otherwise return 0. This function cannot quit.
22040 The return value is 2 if the text is invisible but with an ellipsis
22041 and 1 if it's invisible and without an ellipsis. */
22044 invisible_p (register Lisp_Object propval, Lisp_Object list)
22046 register Lisp_Object tail, proptail;
22048 for (tail = list; CONSP (tail); tail = XCDR (tail))
22050 register Lisp_Object tem;
22051 tem = XCAR (tail);
22052 if (EQ (propval, tem))
22053 return 1;
22054 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22055 return NILP (XCDR (tem)) ? 1 : 2;
22058 if (CONSP (propval))
22060 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22062 Lisp_Object propelt;
22063 propelt = XCAR (proptail);
22064 for (tail = list; CONSP (tail); tail = XCDR (tail))
22066 register Lisp_Object tem;
22067 tem = XCAR (tail);
22068 if (EQ (propelt, tem))
22069 return 1;
22070 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22071 return NILP (XCDR (tem)) ? 1 : 2;
22076 return 0;
22079 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22080 doc: /* Non-nil if the property makes the text invisible.
22081 POS-OR-PROP can be a marker or number, in which case it is taken to be
22082 a position in the current buffer and the value of the `invisible' property
22083 is checked; or it can be some other value, which is then presumed to be the
22084 value of the `invisible' property of the text of interest.
22085 The non-nil value returned can be t for truly invisible text or something
22086 else if the text is replaced by an ellipsis. */)
22087 (Lisp_Object pos_or_prop)
22089 Lisp_Object prop
22090 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22091 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22092 : pos_or_prop);
22093 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22094 return (invis == 0 ? Qnil
22095 : invis == 1 ? Qt
22096 : make_number (invis));
22099 /* Calculate a width or height in pixels from a specification using
22100 the following elements:
22102 SPEC ::=
22103 NUM - a (fractional) multiple of the default font width/height
22104 (NUM) - specifies exactly NUM pixels
22105 UNIT - a fixed number of pixels, see below.
22106 ELEMENT - size of a display element in pixels, see below.
22107 (NUM . SPEC) - equals NUM * SPEC
22108 (+ SPEC SPEC ...) - add pixel values
22109 (- SPEC SPEC ...) - subtract pixel values
22110 (- SPEC) - negate pixel value
22112 NUM ::=
22113 INT or FLOAT - a number constant
22114 SYMBOL - use symbol's (buffer local) variable binding.
22116 UNIT ::=
22117 in - pixels per inch *)
22118 mm - pixels per 1/1000 meter *)
22119 cm - pixels per 1/100 meter *)
22120 width - width of current font in pixels.
22121 height - height of current font in pixels.
22123 *) using the ratio(s) defined in display-pixels-per-inch.
22125 ELEMENT ::=
22127 left-fringe - left fringe width in pixels
22128 right-fringe - right fringe width in pixels
22130 left-margin - left margin width in pixels
22131 right-margin - right margin width in pixels
22133 scroll-bar - scroll-bar area width in pixels
22135 Examples:
22137 Pixels corresponding to 5 inches:
22138 (5 . in)
22140 Total width of non-text areas on left side of window (if scroll-bar is on left):
22141 '(space :width (+ left-fringe left-margin scroll-bar))
22143 Align to first text column (in header line):
22144 '(space :align-to 0)
22146 Align to middle of text area minus half the width of variable `my-image'
22147 containing a loaded image:
22148 '(space :align-to (0.5 . (- text my-image)))
22150 Width of left margin minus width of 1 character in the default font:
22151 '(space :width (- left-margin 1))
22153 Width of left margin minus width of 2 characters in the current font:
22154 '(space :width (- left-margin (2 . width)))
22156 Center 1 character over left-margin (in header line):
22157 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22159 Different ways to express width of left fringe plus left margin minus one pixel:
22160 '(space :width (- (+ left-fringe left-margin) (1)))
22161 '(space :width (+ left-fringe left-margin (- (1))))
22162 '(space :width (+ left-fringe left-margin (-1)))
22166 static int
22167 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22168 struct font *font, int width_p, int *align_to)
22170 double pixels;
22172 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22173 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22175 if (NILP (prop))
22176 return OK_PIXELS (0);
22178 eassert (FRAME_LIVE_P (it->f));
22180 if (SYMBOLP (prop))
22182 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22184 char *unit = SSDATA (SYMBOL_NAME (prop));
22186 if (unit[0] == 'i' && unit[1] == 'n')
22187 pixels = 1.0;
22188 else if (unit[0] == 'm' && unit[1] == 'm')
22189 pixels = 25.4;
22190 else if (unit[0] == 'c' && unit[1] == 'm')
22191 pixels = 2.54;
22192 else
22193 pixels = 0;
22194 if (pixels > 0)
22196 double ppi = (width_p ? FRAME_RES_X (it->f)
22197 : FRAME_RES_Y (it->f));
22199 if (ppi > 0)
22200 return OK_PIXELS (ppi / pixels);
22201 return 0;
22205 #ifdef HAVE_WINDOW_SYSTEM
22206 if (EQ (prop, Qheight))
22207 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22208 if (EQ (prop, Qwidth))
22209 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22210 #else
22211 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22212 return OK_PIXELS (1);
22213 #endif
22215 if (EQ (prop, Qtext))
22216 return OK_PIXELS (width_p
22217 ? window_box_width (it->w, TEXT_AREA)
22218 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22220 if (align_to && *align_to < 0)
22222 *res = 0;
22223 if (EQ (prop, Qleft))
22224 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22225 if (EQ (prop, Qright))
22226 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22227 if (EQ (prop, Qcenter))
22228 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22229 + window_box_width (it->w, TEXT_AREA) / 2);
22230 if (EQ (prop, Qleft_fringe))
22231 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22232 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22233 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22234 if (EQ (prop, Qright_fringe))
22235 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22236 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22237 : window_box_right_offset (it->w, TEXT_AREA));
22238 if (EQ (prop, Qleft_margin))
22239 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22240 if (EQ (prop, Qright_margin))
22241 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22242 if (EQ (prop, Qscroll_bar))
22243 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22245 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22246 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22247 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22248 : 0)));
22250 else
22252 if (EQ (prop, Qleft_fringe))
22253 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22254 if (EQ (prop, Qright_fringe))
22255 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22256 if (EQ (prop, Qleft_margin))
22257 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22258 if (EQ (prop, Qright_margin))
22259 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22260 if (EQ (prop, Qscroll_bar))
22261 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22264 prop = buffer_local_value_1 (prop, it->w->contents);
22265 if (EQ (prop, Qunbound))
22266 prop = Qnil;
22269 if (INTEGERP (prop) || FLOATP (prop))
22271 int base_unit = (width_p
22272 ? FRAME_COLUMN_WIDTH (it->f)
22273 : FRAME_LINE_HEIGHT (it->f));
22274 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22277 if (CONSP (prop))
22279 Lisp_Object car = XCAR (prop);
22280 Lisp_Object cdr = XCDR (prop);
22282 if (SYMBOLP (car))
22284 #ifdef HAVE_WINDOW_SYSTEM
22285 if (FRAME_WINDOW_P (it->f)
22286 && valid_image_p (prop))
22288 ptrdiff_t id = lookup_image (it->f, prop);
22289 struct image *img = IMAGE_FROM_ID (it->f, id);
22291 return OK_PIXELS (width_p ? img->width : img->height);
22293 #endif
22294 if (EQ (car, Qplus) || EQ (car, Qminus))
22296 int first = 1;
22297 double px;
22299 pixels = 0;
22300 while (CONSP (cdr))
22302 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22303 font, width_p, align_to))
22304 return 0;
22305 if (first)
22306 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22307 else
22308 pixels += px;
22309 cdr = XCDR (cdr);
22311 if (EQ (car, Qminus))
22312 pixels = -pixels;
22313 return OK_PIXELS (pixels);
22316 car = buffer_local_value_1 (car, it->w->contents);
22317 if (EQ (car, Qunbound))
22318 car = Qnil;
22321 if (INTEGERP (car) || FLOATP (car))
22323 double fact;
22324 pixels = XFLOATINT (car);
22325 if (NILP (cdr))
22326 return OK_PIXELS (pixels);
22327 if (calc_pixel_width_or_height (&fact, it, cdr,
22328 font, width_p, align_to))
22329 return OK_PIXELS (pixels * fact);
22330 return 0;
22333 return 0;
22336 return 0;
22340 /***********************************************************************
22341 Glyph Display
22342 ***********************************************************************/
22344 #ifdef HAVE_WINDOW_SYSTEM
22346 #ifdef GLYPH_DEBUG
22348 void
22349 dump_glyph_string (struct glyph_string *s)
22351 fprintf (stderr, "glyph string\n");
22352 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22353 s->x, s->y, s->width, s->height);
22354 fprintf (stderr, " ybase = %d\n", s->ybase);
22355 fprintf (stderr, " hl = %d\n", s->hl);
22356 fprintf (stderr, " left overhang = %d, right = %d\n",
22357 s->left_overhang, s->right_overhang);
22358 fprintf (stderr, " nchars = %d\n", s->nchars);
22359 fprintf (stderr, " extends to end of line = %d\n",
22360 s->extends_to_end_of_line_p);
22361 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22362 fprintf (stderr, " bg width = %d\n", s->background_width);
22365 #endif /* GLYPH_DEBUG */
22367 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22368 of XChar2b structures for S; it can't be allocated in
22369 init_glyph_string because it must be allocated via `alloca'. W
22370 is the window on which S is drawn. ROW and AREA are the glyph row
22371 and area within the row from which S is constructed. START is the
22372 index of the first glyph structure covered by S. HL is a
22373 face-override for drawing S. */
22375 #ifdef HAVE_NTGUI
22376 #define OPTIONAL_HDC(hdc) HDC hdc,
22377 #define DECLARE_HDC(hdc) HDC hdc;
22378 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22379 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22380 #endif
22382 #ifndef OPTIONAL_HDC
22383 #define OPTIONAL_HDC(hdc)
22384 #define DECLARE_HDC(hdc)
22385 #define ALLOCATE_HDC(hdc, f)
22386 #define RELEASE_HDC(hdc, f)
22387 #endif
22389 static void
22390 init_glyph_string (struct glyph_string *s,
22391 OPTIONAL_HDC (hdc)
22392 XChar2b *char2b, struct window *w, struct glyph_row *row,
22393 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22395 memset (s, 0, sizeof *s);
22396 s->w = w;
22397 s->f = XFRAME (w->frame);
22398 #ifdef HAVE_NTGUI
22399 s->hdc = hdc;
22400 #endif
22401 s->display = FRAME_X_DISPLAY (s->f);
22402 s->window = FRAME_X_WINDOW (s->f);
22403 s->char2b = char2b;
22404 s->hl = hl;
22405 s->row = row;
22406 s->area = area;
22407 s->first_glyph = row->glyphs[area] + start;
22408 s->height = row->height;
22409 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22410 s->ybase = s->y + row->ascent;
22414 /* Append the list of glyph strings with head H and tail T to the list
22415 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22417 static void
22418 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22419 struct glyph_string *h, struct glyph_string *t)
22421 if (h)
22423 if (*head)
22424 (*tail)->next = h;
22425 else
22426 *head = h;
22427 h->prev = *tail;
22428 *tail = t;
22433 /* Prepend the list of glyph strings with head H and tail T to the
22434 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22435 result. */
22437 static void
22438 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22439 struct glyph_string *h, struct glyph_string *t)
22441 if (h)
22443 if (*head)
22444 (*head)->prev = t;
22445 else
22446 *tail = t;
22447 t->next = *head;
22448 *head = h;
22453 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22454 Set *HEAD and *TAIL to the resulting list. */
22456 static void
22457 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22458 struct glyph_string *s)
22460 s->next = s->prev = NULL;
22461 append_glyph_string_lists (head, tail, s, s);
22465 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22466 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22467 make sure that X resources for the face returned are allocated.
22468 Value is a pointer to a realized face that is ready for display if
22469 DISPLAY_P is non-zero. */
22471 static struct face *
22472 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22473 XChar2b *char2b, int display_p)
22475 struct face *face = FACE_FROM_ID (f, face_id);
22476 unsigned code = 0;
22478 if (face->font)
22480 code = face->font->driver->encode_char (face->font, c);
22482 if (code == FONT_INVALID_CODE)
22483 code = 0;
22485 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22487 /* Make sure X resources of the face are allocated. */
22488 #ifdef HAVE_X_WINDOWS
22489 if (display_p)
22490 #endif
22492 eassert (face != NULL);
22493 PREPARE_FACE_FOR_DISPLAY (f, face);
22496 return face;
22500 /* Get face and two-byte form of character glyph GLYPH on frame F.
22501 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22502 a pointer to a realized face that is ready for display. */
22504 static struct face *
22505 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22506 XChar2b *char2b, int *two_byte_p)
22508 struct face *face;
22509 unsigned code = 0;
22511 eassert (glyph->type == CHAR_GLYPH);
22512 face = FACE_FROM_ID (f, glyph->face_id);
22514 /* Make sure X resources of the face are allocated. */
22515 eassert (face != NULL);
22516 PREPARE_FACE_FOR_DISPLAY (f, face);
22518 if (two_byte_p)
22519 *two_byte_p = 0;
22521 if (face->font)
22523 if (CHAR_BYTE8_P (glyph->u.ch))
22524 code = CHAR_TO_BYTE8 (glyph->u.ch);
22525 else
22526 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22528 if (code == FONT_INVALID_CODE)
22529 code = 0;
22532 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22533 return face;
22537 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22538 Return 1 if FONT has a glyph for C, otherwise return 0. */
22540 static int
22541 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22543 unsigned code;
22545 if (CHAR_BYTE8_P (c))
22546 code = CHAR_TO_BYTE8 (c);
22547 else
22548 code = font->driver->encode_char (font, c);
22550 if (code == FONT_INVALID_CODE)
22551 return 0;
22552 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22553 return 1;
22557 /* Fill glyph string S with composition components specified by S->cmp.
22559 BASE_FACE is the base face of the composition.
22560 S->cmp_from is the index of the first component for S.
22562 OVERLAPS non-zero means S should draw the foreground only, and use
22563 its physical height for clipping. See also draw_glyphs.
22565 Value is the index of a component not in S. */
22567 static int
22568 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22569 int overlaps)
22571 int i;
22572 /* For all glyphs of this composition, starting at the offset
22573 S->cmp_from, until we reach the end of the definition or encounter a
22574 glyph that requires the different face, add it to S. */
22575 struct face *face;
22577 eassert (s);
22579 s->for_overlaps = overlaps;
22580 s->face = NULL;
22581 s->font = NULL;
22582 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22584 int c = COMPOSITION_GLYPH (s->cmp, i);
22586 /* TAB in a composition means display glyphs with padding space
22587 on the left or right. */
22588 if (c != '\t')
22590 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22591 -1, Qnil);
22593 face = get_char_face_and_encoding (s->f, c, face_id,
22594 s->char2b + i, 1);
22595 if (face)
22597 if (! s->face)
22599 s->face = face;
22600 s->font = s->face->font;
22602 else if (s->face != face)
22603 break;
22606 ++s->nchars;
22608 s->cmp_to = i;
22610 if (s->face == NULL)
22612 s->face = base_face->ascii_face;
22613 s->font = s->face->font;
22616 /* All glyph strings for the same composition has the same width,
22617 i.e. the width set for the first component of the composition. */
22618 s->width = s->first_glyph->pixel_width;
22620 /* If the specified font could not be loaded, use the frame's
22621 default font, but record the fact that we couldn't load it in
22622 the glyph string so that we can draw rectangles for the
22623 characters of the glyph string. */
22624 if (s->font == NULL)
22626 s->font_not_found_p = 1;
22627 s->font = FRAME_FONT (s->f);
22630 /* Adjust base line for subscript/superscript text. */
22631 s->ybase += s->first_glyph->voffset;
22633 /* This glyph string must always be drawn with 16-bit functions. */
22634 s->two_byte_p = 1;
22636 return s->cmp_to;
22639 static int
22640 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22641 int start, int end, int overlaps)
22643 struct glyph *glyph, *last;
22644 Lisp_Object lgstring;
22645 int i;
22647 s->for_overlaps = overlaps;
22648 glyph = s->row->glyphs[s->area] + start;
22649 last = s->row->glyphs[s->area] + end;
22650 s->cmp_id = glyph->u.cmp.id;
22651 s->cmp_from = glyph->slice.cmp.from;
22652 s->cmp_to = glyph->slice.cmp.to + 1;
22653 s->face = FACE_FROM_ID (s->f, face_id);
22654 lgstring = composition_gstring_from_id (s->cmp_id);
22655 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22656 glyph++;
22657 while (glyph < last
22658 && glyph->u.cmp.automatic
22659 && glyph->u.cmp.id == s->cmp_id
22660 && s->cmp_to == glyph->slice.cmp.from)
22661 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22663 for (i = s->cmp_from; i < s->cmp_to; i++)
22665 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22666 unsigned code = LGLYPH_CODE (lglyph);
22668 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22670 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22671 return glyph - s->row->glyphs[s->area];
22675 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22676 See the comment of fill_glyph_string for arguments.
22677 Value is the index of the first glyph not in S. */
22680 static int
22681 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22682 int start, int end, int overlaps)
22684 struct glyph *glyph, *last;
22685 int voffset;
22687 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22688 s->for_overlaps = overlaps;
22689 glyph = s->row->glyphs[s->area] + start;
22690 last = s->row->glyphs[s->area] + end;
22691 voffset = glyph->voffset;
22692 s->face = FACE_FROM_ID (s->f, face_id);
22693 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22694 s->nchars = 1;
22695 s->width = glyph->pixel_width;
22696 glyph++;
22697 while (glyph < last
22698 && glyph->type == GLYPHLESS_GLYPH
22699 && glyph->voffset == voffset
22700 && glyph->face_id == face_id)
22702 s->nchars++;
22703 s->width += glyph->pixel_width;
22704 glyph++;
22706 s->ybase += voffset;
22707 return glyph - s->row->glyphs[s->area];
22711 /* Fill glyph string S from a sequence of character glyphs.
22713 FACE_ID is the face id of the string. START is the index of the
22714 first glyph to consider, END is the index of the last + 1.
22715 OVERLAPS non-zero means S should draw the foreground only, and use
22716 its physical height for clipping. See also draw_glyphs.
22718 Value is the index of the first glyph not in S. */
22720 static int
22721 fill_glyph_string (struct glyph_string *s, int face_id,
22722 int start, int end, int overlaps)
22724 struct glyph *glyph, *last;
22725 int voffset;
22726 int glyph_not_available_p;
22728 eassert (s->f == XFRAME (s->w->frame));
22729 eassert (s->nchars == 0);
22730 eassert (start >= 0 && end > start);
22732 s->for_overlaps = overlaps;
22733 glyph = s->row->glyphs[s->area] + start;
22734 last = s->row->glyphs[s->area] + end;
22735 voffset = glyph->voffset;
22736 s->padding_p = glyph->padding_p;
22737 glyph_not_available_p = glyph->glyph_not_available_p;
22739 while (glyph < last
22740 && glyph->type == CHAR_GLYPH
22741 && glyph->voffset == voffset
22742 /* Same face id implies same font, nowadays. */
22743 && glyph->face_id == face_id
22744 && glyph->glyph_not_available_p == glyph_not_available_p)
22746 int two_byte_p;
22748 s->face = get_glyph_face_and_encoding (s->f, glyph,
22749 s->char2b + s->nchars,
22750 &two_byte_p);
22751 s->two_byte_p = two_byte_p;
22752 ++s->nchars;
22753 eassert (s->nchars <= end - start);
22754 s->width += glyph->pixel_width;
22755 if (glyph++->padding_p != s->padding_p)
22756 break;
22759 s->font = s->face->font;
22761 /* If the specified font could not be loaded, use the frame's font,
22762 but record the fact that we couldn't load it in
22763 S->font_not_found_p so that we can draw rectangles for the
22764 characters of the glyph string. */
22765 if (s->font == NULL || glyph_not_available_p)
22767 s->font_not_found_p = 1;
22768 s->font = FRAME_FONT (s->f);
22771 /* Adjust base line for subscript/superscript text. */
22772 s->ybase += voffset;
22774 eassert (s->face && s->face->gc);
22775 return glyph - s->row->glyphs[s->area];
22779 /* Fill glyph string S from image glyph S->first_glyph. */
22781 static void
22782 fill_image_glyph_string (struct glyph_string *s)
22784 eassert (s->first_glyph->type == IMAGE_GLYPH);
22785 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22786 eassert (s->img);
22787 s->slice = s->first_glyph->slice.img;
22788 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22789 s->font = s->face->font;
22790 s->width = s->first_glyph->pixel_width;
22792 /* Adjust base line for subscript/superscript text. */
22793 s->ybase += s->first_glyph->voffset;
22797 /* Fill glyph string S from a sequence of stretch glyphs.
22799 START is the index of the first glyph to consider,
22800 END is the index of the last + 1.
22802 Value is the index of the first glyph not in S. */
22804 static int
22805 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22807 struct glyph *glyph, *last;
22808 int voffset, face_id;
22810 eassert (s->first_glyph->type == STRETCH_GLYPH);
22812 glyph = s->row->glyphs[s->area] + start;
22813 last = s->row->glyphs[s->area] + end;
22814 face_id = glyph->face_id;
22815 s->face = FACE_FROM_ID (s->f, face_id);
22816 s->font = s->face->font;
22817 s->width = glyph->pixel_width;
22818 s->nchars = 1;
22819 voffset = glyph->voffset;
22821 for (++glyph;
22822 (glyph < last
22823 && glyph->type == STRETCH_GLYPH
22824 && glyph->voffset == voffset
22825 && glyph->face_id == face_id);
22826 ++glyph)
22827 s->width += glyph->pixel_width;
22829 /* Adjust base line for subscript/superscript text. */
22830 s->ybase += voffset;
22832 /* The case that face->gc == 0 is handled when drawing the glyph
22833 string by calling PREPARE_FACE_FOR_DISPLAY. */
22834 eassert (s->face);
22835 return glyph - s->row->glyphs[s->area];
22838 static struct font_metrics *
22839 get_per_char_metric (struct font *font, XChar2b *char2b)
22841 static struct font_metrics metrics;
22842 unsigned code;
22844 if (! font)
22845 return NULL;
22846 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22847 if (code == FONT_INVALID_CODE)
22848 return NULL;
22849 font->driver->text_extents (font, &code, 1, &metrics);
22850 return &metrics;
22853 /* EXPORT for RIF:
22854 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22855 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22856 assumed to be zero. */
22858 void
22859 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22861 *left = *right = 0;
22863 if (glyph->type == CHAR_GLYPH)
22865 struct face *face;
22866 XChar2b char2b;
22867 struct font_metrics *pcm;
22869 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22870 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22872 if (pcm->rbearing > pcm->width)
22873 *right = pcm->rbearing - pcm->width;
22874 if (pcm->lbearing < 0)
22875 *left = -pcm->lbearing;
22878 else if (glyph->type == COMPOSITE_GLYPH)
22880 if (! glyph->u.cmp.automatic)
22882 struct composition *cmp = composition_table[glyph->u.cmp.id];
22884 if (cmp->rbearing > cmp->pixel_width)
22885 *right = cmp->rbearing - cmp->pixel_width;
22886 if (cmp->lbearing < 0)
22887 *left = - cmp->lbearing;
22889 else
22891 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22892 struct font_metrics metrics;
22894 composition_gstring_width (gstring, glyph->slice.cmp.from,
22895 glyph->slice.cmp.to + 1, &metrics);
22896 if (metrics.rbearing > metrics.width)
22897 *right = metrics.rbearing - metrics.width;
22898 if (metrics.lbearing < 0)
22899 *left = - metrics.lbearing;
22905 /* Return the index of the first glyph preceding glyph string S that
22906 is overwritten by S because of S's left overhang. Value is -1
22907 if no glyphs are overwritten. */
22909 static int
22910 left_overwritten (struct glyph_string *s)
22912 int k;
22914 if (s->left_overhang)
22916 int x = 0, i;
22917 struct glyph *glyphs = s->row->glyphs[s->area];
22918 int first = s->first_glyph - glyphs;
22920 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22921 x -= glyphs[i].pixel_width;
22923 k = i + 1;
22925 else
22926 k = -1;
22928 return k;
22932 /* Return the index of the first glyph preceding glyph string S that
22933 is overwriting S because of its right overhang. Value is -1 if no
22934 glyph in front of S overwrites S. */
22936 static int
22937 left_overwriting (struct glyph_string *s)
22939 int i, k, x;
22940 struct glyph *glyphs = s->row->glyphs[s->area];
22941 int first = s->first_glyph - glyphs;
22943 k = -1;
22944 x = 0;
22945 for (i = first - 1; i >= 0; --i)
22947 int left, right;
22948 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22949 if (x + right > 0)
22950 k = i;
22951 x -= glyphs[i].pixel_width;
22954 return k;
22958 /* Return the index of the last glyph following glyph string S that is
22959 overwritten by S because of S's right overhang. Value is -1 if
22960 no such glyph is found. */
22962 static int
22963 right_overwritten (struct glyph_string *s)
22965 int k = -1;
22967 if (s->right_overhang)
22969 int x = 0, i;
22970 struct glyph *glyphs = s->row->glyphs[s->area];
22971 int first = (s->first_glyph - glyphs
22972 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22973 int end = s->row->used[s->area];
22975 for (i = first; i < end && s->right_overhang > x; ++i)
22976 x += glyphs[i].pixel_width;
22978 k = i;
22981 return k;
22985 /* Return the index of the last glyph following glyph string S that
22986 overwrites S because of its left overhang. Value is negative
22987 if no such glyph is found. */
22989 static int
22990 right_overwriting (struct glyph_string *s)
22992 int i, k, x;
22993 int end = s->row->used[s->area];
22994 struct glyph *glyphs = s->row->glyphs[s->area];
22995 int first = (s->first_glyph - glyphs
22996 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22998 k = -1;
22999 x = 0;
23000 for (i = first; i < end; ++i)
23002 int left, right;
23003 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23004 if (x - left < 0)
23005 k = i;
23006 x += glyphs[i].pixel_width;
23009 return k;
23013 /* Set background width of glyph string S. START is the index of the
23014 first glyph following S. LAST_X is the right-most x-position + 1
23015 in the drawing area. */
23017 static void
23018 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23020 /* If the face of this glyph string has to be drawn to the end of
23021 the drawing area, set S->extends_to_end_of_line_p. */
23023 if (start == s->row->used[s->area]
23024 && s->area == TEXT_AREA
23025 && ((s->row->fill_line_p
23026 && (s->hl == DRAW_NORMAL_TEXT
23027 || s->hl == DRAW_IMAGE_RAISED
23028 || s->hl == DRAW_IMAGE_SUNKEN))
23029 || s->hl == DRAW_MOUSE_FACE))
23030 s->extends_to_end_of_line_p = 1;
23032 /* If S extends its face to the end of the line, set its
23033 background_width to the distance to the right edge of the drawing
23034 area. */
23035 if (s->extends_to_end_of_line_p)
23036 s->background_width = last_x - s->x + 1;
23037 else
23038 s->background_width = s->width;
23042 /* Compute overhangs and x-positions for glyph string S and its
23043 predecessors, or successors. X is the starting x-position for S.
23044 BACKWARD_P non-zero means process predecessors. */
23046 static void
23047 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23049 if (backward_p)
23051 while (s)
23053 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23054 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23055 x -= s->width;
23056 s->x = x;
23057 s = s->prev;
23060 else
23062 while (s)
23064 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23065 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23066 s->x = x;
23067 x += s->width;
23068 s = s->next;
23075 /* The following macros are only called from draw_glyphs below.
23076 They reference the following parameters of that function directly:
23077 `w', `row', `area', and `overlap_p'
23078 as well as the following local variables:
23079 `s', `f', and `hdc' (in W32) */
23081 #ifdef HAVE_NTGUI
23082 /* On W32, silently add local `hdc' variable to argument list of
23083 init_glyph_string. */
23084 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23085 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23086 #else
23087 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23088 init_glyph_string (s, char2b, w, row, area, start, hl)
23089 #endif
23091 /* Add a glyph string for a stretch glyph to the list of strings
23092 between HEAD and TAIL. START is the index of the stretch glyph in
23093 row area AREA of glyph row ROW. END is the index of the last glyph
23094 in that glyph row area. X is the current output position assigned
23095 to the new glyph string constructed. HL overrides that face of the
23096 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23097 is the right-most x-position of the drawing area. */
23099 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23100 and below -- keep them on one line. */
23101 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23102 do \
23104 s = alloca (sizeof *s); \
23105 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23106 START = fill_stretch_glyph_string (s, START, END); \
23107 append_glyph_string (&HEAD, &TAIL, s); \
23108 s->x = (X); \
23110 while (0)
23113 /* Add a glyph string for an image glyph to the list of strings
23114 between HEAD and TAIL. START is the index of the image glyph in
23115 row area AREA of glyph row ROW. END is the index of the last glyph
23116 in that glyph row area. X is the current output position assigned
23117 to the new glyph string constructed. HL overrides that face of the
23118 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23119 is the right-most x-position of the drawing area. */
23121 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23122 do \
23124 s = alloca (sizeof *s); \
23125 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23126 fill_image_glyph_string (s); \
23127 append_glyph_string (&HEAD, &TAIL, s); \
23128 ++START; \
23129 s->x = (X); \
23131 while (0)
23134 /* Add a glyph string for a sequence of character glyphs to the list
23135 of strings between HEAD and TAIL. START is the index of the first
23136 glyph in row area AREA of glyph row ROW that is part of the new
23137 glyph string. END is the index of the last glyph in that glyph row
23138 area. X is the current output position assigned to the new glyph
23139 string constructed. HL overrides that face of the glyph; e.g. it
23140 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23141 right-most x-position of the drawing area. */
23143 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23144 do \
23146 int face_id; \
23147 XChar2b *char2b; \
23149 face_id = (row)->glyphs[area][START].face_id; \
23151 s = alloca (sizeof *s); \
23152 char2b = alloca ((END - START) * sizeof *char2b); \
23153 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23154 append_glyph_string (&HEAD, &TAIL, s); \
23155 s->x = (X); \
23156 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23158 while (0)
23161 /* Add a glyph string for a composite sequence to the list of strings
23162 between HEAD and TAIL. START is the index of the first glyph in
23163 row area AREA of glyph row ROW that is part of the new glyph
23164 string. END is the index of the last glyph in that glyph row area.
23165 X is the current output position assigned to the new glyph string
23166 constructed. HL overrides that face of the glyph; e.g. it is
23167 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23168 x-position of the drawing area. */
23170 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23171 do { \
23172 int face_id = (row)->glyphs[area][START].face_id; \
23173 struct face *base_face = FACE_FROM_ID (f, face_id); \
23174 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23175 struct composition *cmp = composition_table[cmp_id]; \
23176 XChar2b *char2b; \
23177 struct glyph_string *first_s = NULL; \
23178 int n; \
23180 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23182 /* Make glyph_strings for each glyph sequence that is drawable by \
23183 the same face, and append them to HEAD/TAIL. */ \
23184 for (n = 0; n < cmp->glyph_len;) \
23186 s = alloca (sizeof *s); \
23187 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23188 append_glyph_string (&(HEAD), &(TAIL), s); \
23189 s->cmp = cmp; \
23190 s->cmp_from = n; \
23191 s->x = (X); \
23192 if (n == 0) \
23193 first_s = s; \
23194 n = fill_composite_glyph_string (s, base_face, overlaps); \
23197 ++START; \
23198 s = first_s; \
23199 } while (0)
23202 /* Add a glyph string for a glyph-string sequence to the list of strings
23203 between HEAD and TAIL. */
23205 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23206 do { \
23207 int face_id; \
23208 XChar2b *char2b; \
23209 Lisp_Object gstring; \
23211 face_id = (row)->glyphs[area][START].face_id; \
23212 gstring = (composition_gstring_from_id \
23213 ((row)->glyphs[area][START].u.cmp.id)); \
23214 s = alloca (sizeof *s); \
23215 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23216 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23217 append_glyph_string (&(HEAD), &(TAIL), s); \
23218 s->x = (X); \
23219 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23220 } while (0)
23223 /* Add a glyph string for a sequence of glyphless character's glyphs
23224 to the list of strings between HEAD and TAIL. The meanings of
23225 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23227 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23228 do \
23230 int face_id; \
23232 face_id = (row)->glyphs[area][START].face_id; \
23234 s = alloca (sizeof *s); \
23235 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23236 append_glyph_string (&HEAD, &TAIL, s); \
23237 s->x = (X); \
23238 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23239 overlaps); \
23241 while (0)
23244 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23245 of AREA of glyph row ROW on window W between indices START and END.
23246 HL overrides the face for drawing glyph strings, e.g. it is
23247 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23248 x-positions of the drawing area.
23250 This is an ugly monster macro construct because we must use alloca
23251 to allocate glyph strings (because draw_glyphs can be called
23252 asynchronously). */
23254 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23255 do \
23257 HEAD = TAIL = NULL; \
23258 while (START < END) \
23260 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23261 switch (first_glyph->type) \
23263 case CHAR_GLYPH: \
23264 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23265 HL, X, LAST_X); \
23266 break; \
23268 case COMPOSITE_GLYPH: \
23269 if (first_glyph->u.cmp.automatic) \
23270 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23271 HL, X, LAST_X); \
23272 else \
23273 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23274 HL, X, LAST_X); \
23275 break; \
23277 case STRETCH_GLYPH: \
23278 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23279 HL, X, LAST_X); \
23280 break; \
23282 case IMAGE_GLYPH: \
23283 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23284 HL, X, LAST_X); \
23285 break; \
23287 case GLYPHLESS_GLYPH: \
23288 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23289 HL, X, LAST_X); \
23290 break; \
23292 default: \
23293 emacs_abort (); \
23296 if (s) \
23298 set_glyph_string_background_width (s, START, LAST_X); \
23299 (X) += s->width; \
23302 } while (0)
23305 /* Draw glyphs between START and END in AREA of ROW on window W,
23306 starting at x-position X. X is relative to AREA in W. HL is a
23307 face-override with the following meaning:
23309 DRAW_NORMAL_TEXT draw normally
23310 DRAW_CURSOR draw in cursor face
23311 DRAW_MOUSE_FACE draw in mouse face.
23312 DRAW_INVERSE_VIDEO draw in mode line face
23313 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23314 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23316 If OVERLAPS is non-zero, draw only the foreground of characters and
23317 clip to the physical height of ROW. Non-zero value also defines
23318 the overlapping part to be drawn:
23320 OVERLAPS_PRED overlap with preceding rows
23321 OVERLAPS_SUCC overlap with succeeding rows
23322 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23323 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23325 Value is the x-position reached, relative to AREA of W. */
23327 static int
23328 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23329 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23330 enum draw_glyphs_face hl, int overlaps)
23332 struct glyph_string *head, *tail;
23333 struct glyph_string *s;
23334 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23335 int i, j, x_reached, last_x, area_left = 0;
23336 struct frame *f = XFRAME (WINDOW_FRAME (w));
23337 DECLARE_HDC (hdc);
23339 ALLOCATE_HDC (hdc, f);
23341 /* Let's rather be paranoid than getting a SEGV. */
23342 end = min (end, row->used[area]);
23343 start = clip_to_bounds (0, start, end);
23345 /* Translate X to frame coordinates. Set last_x to the right
23346 end of the drawing area. */
23347 if (row->full_width_p)
23349 /* X is relative to the left edge of W, without scroll bars
23350 or fringes. */
23351 area_left = WINDOW_LEFT_EDGE_X (w);
23352 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23354 else
23356 area_left = window_box_left (w, area);
23357 last_x = area_left + window_box_width (w, area);
23359 x += area_left;
23361 /* Build a doubly-linked list of glyph_string structures between
23362 head and tail from what we have to draw. Note that the macro
23363 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23364 the reason we use a separate variable `i'. */
23365 i = start;
23366 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23367 if (tail)
23368 x_reached = tail->x + tail->background_width;
23369 else
23370 x_reached = x;
23372 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23373 the row, redraw some glyphs in front or following the glyph
23374 strings built above. */
23375 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23377 struct glyph_string *h, *t;
23378 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23379 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23380 int check_mouse_face = 0;
23381 int dummy_x = 0;
23383 /* If mouse highlighting is on, we may need to draw adjacent
23384 glyphs using mouse-face highlighting. */
23385 if (area == TEXT_AREA && row->mouse_face_p
23386 && hlinfo->mouse_face_beg_row >= 0
23387 && hlinfo->mouse_face_end_row >= 0)
23389 struct glyph_row *mouse_beg_row, *mouse_end_row;
23391 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23392 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23394 if (row >= mouse_beg_row && row <= mouse_end_row)
23396 check_mouse_face = 1;
23397 mouse_beg_col = (row == mouse_beg_row)
23398 ? hlinfo->mouse_face_beg_col : 0;
23399 mouse_end_col = (row == mouse_end_row)
23400 ? hlinfo->mouse_face_end_col
23401 : row->used[TEXT_AREA];
23405 /* Compute overhangs for all glyph strings. */
23406 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23407 for (s = head; s; s = s->next)
23408 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23410 /* Prepend glyph strings for glyphs in front of the first glyph
23411 string that are overwritten because of the first glyph
23412 string's left overhang. The background of all strings
23413 prepended must be drawn because the first glyph string
23414 draws over it. */
23415 i = left_overwritten (head);
23416 if (i >= 0)
23418 enum draw_glyphs_face overlap_hl;
23420 /* If this row contains mouse highlighting, attempt to draw
23421 the overlapped glyphs with the correct highlight. This
23422 code fails if the overlap encompasses more than one glyph
23423 and mouse-highlight spans only some of these glyphs.
23424 However, making it work perfectly involves a lot more
23425 code, and I don't know if the pathological case occurs in
23426 practice, so we'll stick to this for now. --- cyd */
23427 if (check_mouse_face
23428 && mouse_beg_col < start && mouse_end_col > i)
23429 overlap_hl = DRAW_MOUSE_FACE;
23430 else
23431 overlap_hl = DRAW_NORMAL_TEXT;
23433 j = i;
23434 BUILD_GLYPH_STRINGS (j, start, h, t,
23435 overlap_hl, dummy_x, last_x);
23436 start = i;
23437 compute_overhangs_and_x (t, head->x, 1);
23438 prepend_glyph_string_lists (&head, &tail, h, t);
23439 clip_head = head;
23442 /* Prepend glyph strings for glyphs in front of the first glyph
23443 string that overwrite that glyph string because of their
23444 right overhang. For these strings, only the foreground must
23445 be drawn, because it draws over the glyph string at `head'.
23446 The background must not be drawn because this would overwrite
23447 right overhangs of preceding glyphs for which no glyph
23448 strings exist. */
23449 i = left_overwriting (head);
23450 if (i >= 0)
23452 enum draw_glyphs_face overlap_hl;
23454 if (check_mouse_face
23455 && mouse_beg_col < start && mouse_end_col > i)
23456 overlap_hl = DRAW_MOUSE_FACE;
23457 else
23458 overlap_hl = DRAW_NORMAL_TEXT;
23460 clip_head = head;
23461 BUILD_GLYPH_STRINGS (i, start, h, t,
23462 overlap_hl, dummy_x, last_x);
23463 for (s = h; s; s = s->next)
23464 s->background_filled_p = 1;
23465 compute_overhangs_and_x (t, head->x, 1);
23466 prepend_glyph_string_lists (&head, &tail, h, t);
23469 /* Append glyphs strings for glyphs following the last glyph
23470 string tail that are overwritten by tail. The background of
23471 these strings has to be drawn because tail's foreground draws
23472 over it. */
23473 i = right_overwritten (tail);
23474 if (i >= 0)
23476 enum draw_glyphs_face overlap_hl;
23478 if (check_mouse_face
23479 && mouse_beg_col < i && mouse_end_col > end)
23480 overlap_hl = DRAW_MOUSE_FACE;
23481 else
23482 overlap_hl = DRAW_NORMAL_TEXT;
23484 BUILD_GLYPH_STRINGS (end, i, h, t,
23485 overlap_hl, x, last_x);
23486 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23487 we don't have `end = i;' here. */
23488 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23489 append_glyph_string_lists (&head, &tail, h, t);
23490 clip_tail = tail;
23493 /* Append glyph strings for glyphs following the last glyph
23494 string tail that overwrite tail. The foreground of such
23495 glyphs has to be drawn because it writes into the background
23496 of tail. The background must not be drawn because it could
23497 paint over the foreground of following glyphs. */
23498 i = right_overwriting (tail);
23499 if (i >= 0)
23501 enum draw_glyphs_face overlap_hl;
23502 if (check_mouse_face
23503 && mouse_beg_col < i && mouse_end_col > end)
23504 overlap_hl = DRAW_MOUSE_FACE;
23505 else
23506 overlap_hl = DRAW_NORMAL_TEXT;
23508 clip_tail = tail;
23509 i++; /* We must include the Ith glyph. */
23510 BUILD_GLYPH_STRINGS (end, i, h, t,
23511 overlap_hl, x, last_x);
23512 for (s = h; s; s = s->next)
23513 s->background_filled_p = 1;
23514 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23515 append_glyph_string_lists (&head, &tail, h, t);
23517 if (clip_head || clip_tail)
23518 for (s = head; s; s = s->next)
23520 s->clip_head = clip_head;
23521 s->clip_tail = clip_tail;
23525 /* Draw all strings. */
23526 for (s = head; s; s = s->next)
23527 FRAME_RIF (f)->draw_glyph_string (s);
23529 #ifndef HAVE_NS
23530 /* When focus a sole frame and move horizontally, this sets on_p to 0
23531 causing a failure to erase prev cursor position. */
23532 if (area == TEXT_AREA
23533 && !row->full_width_p
23534 /* When drawing overlapping rows, only the glyph strings'
23535 foreground is drawn, which doesn't erase a cursor
23536 completely. */
23537 && !overlaps)
23539 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23540 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23541 : (tail ? tail->x + tail->background_width : x));
23542 x0 -= area_left;
23543 x1 -= area_left;
23545 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23546 row->y, MATRIX_ROW_BOTTOM_Y (row));
23548 #endif
23550 /* Value is the x-position up to which drawn, relative to AREA of W.
23551 This doesn't include parts drawn because of overhangs. */
23552 if (row->full_width_p)
23553 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23554 else
23555 x_reached -= area_left;
23557 RELEASE_HDC (hdc, f);
23559 return x_reached;
23562 /* Expand row matrix if too narrow. Don't expand if area
23563 is not present. */
23565 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23567 if (!fonts_changed_p \
23568 && (it->glyph_row->glyphs[area] \
23569 < it->glyph_row->glyphs[area + 1])) \
23571 it->w->ncols_scale_factor++; \
23572 fonts_changed_p = 1; \
23576 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23577 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23579 static void
23580 append_glyph (struct it *it)
23582 struct glyph *glyph;
23583 enum glyph_row_area area = it->area;
23585 eassert (it->glyph_row);
23586 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23588 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23589 if (glyph < it->glyph_row->glyphs[area + 1])
23591 /* If the glyph row is reversed, we need to prepend the glyph
23592 rather than append it. */
23593 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23595 struct glyph *g;
23597 /* Make room for the additional glyph. */
23598 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23599 g[1] = *g;
23600 glyph = it->glyph_row->glyphs[area];
23602 glyph->charpos = CHARPOS (it->position);
23603 glyph->object = it->object;
23604 if (it->pixel_width > 0)
23606 glyph->pixel_width = it->pixel_width;
23607 glyph->padding_p = 0;
23609 else
23611 /* Assure at least 1-pixel width. Otherwise, cursor can't
23612 be displayed correctly. */
23613 glyph->pixel_width = 1;
23614 glyph->padding_p = 1;
23616 glyph->ascent = it->ascent;
23617 glyph->descent = it->descent;
23618 glyph->voffset = it->voffset;
23619 glyph->type = CHAR_GLYPH;
23620 glyph->avoid_cursor_p = it->avoid_cursor_p;
23621 glyph->multibyte_p = it->multibyte_p;
23622 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23624 /* In R2L rows, the left and the right box edges need to be
23625 drawn in reverse direction. */
23626 glyph->right_box_line_p = it->start_of_box_run_p;
23627 glyph->left_box_line_p = it->end_of_box_run_p;
23629 else
23631 glyph->left_box_line_p = it->start_of_box_run_p;
23632 glyph->right_box_line_p = it->end_of_box_run_p;
23634 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23635 || it->phys_descent > it->descent);
23636 glyph->glyph_not_available_p = it->glyph_not_available_p;
23637 glyph->face_id = it->face_id;
23638 glyph->u.ch = it->char_to_display;
23639 glyph->slice.img = null_glyph_slice;
23640 glyph->font_type = FONT_TYPE_UNKNOWN;
23641 if (it->bidi_p)
23643 glyph->resolved_level = it->bidi_it.resolved_level;
23644 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23645 emacs_abort ();
23646 glyph->bidi_type = it->bidi_it.type;
23648 else
23650 glyph->resolved_level = 0;
23651 glyph->bidi_type = UNKNOWN_BT;
23653 ++it->glyph_row->used[area];
23655 else
23656 IT_EXPAND_MATRIX_WIDTH (it, area);
23659 /* Store one glyph for the composition IT->cmp_it.id in
23660 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23661 non-null. */
23663 static void
23664 append_composite_glyph (struct it *it)
23666 struct glyph *glyph;
23667 enum glyph_row_area area = it->area;
23669 eassert (it->glyph_row);
23671 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23672 if (glyph < it->glyph_row->glyphs[area + 1])
23674 /* If the glyph row is reversed, we need to prepend the glyph
23675 rather than append it. */
23676 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23678 struct glyph *g;
23680 /* Make room for the new glyph. */
23681 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23682 g[1] = *g;
23683 glyph = it->glyph_row->glyphs[it->area];
23685 glyph->charpos = it->cmp_it.charpos;
23686 glyph->object = it->object;
23687 glyph->pixel_width = it->pixel_width;
23688 glyph->ascent = it->ascent;
23689 glyph->descent = it->descent;
23690 glyph->voffset = it->voffset;
23691 glyph->type = COMPOSITE_GLYPH;
23692 if (it->cmp_it.ch < 0)
23694 glyph->u.cmp.automatic = 0;
23695 glyph->u.cmp.id = it->cmp_it.id;
23696 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23698 else
23700 glyph->u.cmp.automatic = 1;
23701 glyph->u.cmp.id = it->cmp_it.id;
23702 glyph->slice.cmp.from = it->cmp_it.from;
23703 glyph->slice.cmp.to = it->cmp_it.to - 1;
23705 glyph->avoid_cursor_p = it->avoid_cursor_p;
23706 glyph->multibyte_p = it->multibyte_p;
23707 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23709 /* In R2L rows, the left and the right box edges need to be
23710 drawn in reverse direction. */
23711 glyph->right_box_line_p = it->start_of_box_run_p;
23712 glyph->left_box_line_p = it->end_of_box_run_p;
23714 else
23716 glyph->left_box_line_p = it->start_of_box_run_p;
23717 glyph->right_box_line_p = it->end_of_box_run_p;
23719 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23720 || it->phys_descent > it->descent);
23721 glyph->padding_p = 0;
23722 glyph->glyph_not_available_p = 0;
23723 glyph->face_id = it->face_id;
23724 glyph->font_type = FONT_TYPE_UNKNOWN;
23725 if (it->bidi_p)
23727 glyph->resolved_level = it->bidi_it.resolved_level;
23728 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23729 emacs_abort ();
23730 glyph->bidi_type = it->bidi_it.type;
23732 ++it->glyph_row->used[area];
23734 else
23735 IT_EXPAND_MATRIX_WIDTH (it, area);
23739 /* Change IT->ascent and IT->height according to the setting of
23740 IT->voffset. */
23742 static void
23743 take_vertical_position_into_account (struct it *it)
23745 if (it->voffset)
23747 if (it->voffset < 0)
23748 /* Increase the ascent so that we can display the text higher
23749 in the line. */
23750 it->ascent -= it->voffset;
23751 else
23752 /* Increase the descent so that we can display the text lower
23753 in the line. */
23754 it->descent += it->voffset;
23759 /* Produce glyphs/get display metrics for the image IT is loaded with.
23760 See the description of struct display_iterator in dispextern.h for
23761 an overview of struct display_iterator. */
23763 static void
23764 produce_image_glyph (struct it *it)
23766 struct image *img;
23767 struct face *face;
23768 int glyph_ascent, crop;
23769 struct glyph_slice slice;
23771 eassert (it->what == IT_IMAGE);
23773 face = FACE_FROM_ID (it->f, it->face_id);
23774 eassert (face);
23775 /* Make sure X resources of the face is loaded. */
23776 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23778 if (it->image_id < 0)
23780 /* Fringe bitmap. */
23781 it->ascent = it->phys_ascent = 0;
23782 it->descent = it->phys_descent = 0;
23783 it->pixel_width = 0;
23784 it->nglyphs = 0;
23785 return;
23788 img = IMAGE_FROM_ID (it->f, it->image_id);
23789 eassert (img);
23790 /* Make sure X resources of the image is loaded. */
23791 prepare_image_for_display (it->f, img);
23793 slice.x = slice.y = 0;
23794 slice.width = img->width;
23795 slice.height = img->height;
23797 if (INTEGERP (it->slice.x))
23798 slice.x = XINT (it->slice.x);
23799 else if (FLOATP (it->slice.x))
23800 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23802 if (INTEGERP (it->slice.y))
23803 slice.y = XINT (it->slice.y);
23804 else if (FLOATP (it->slice.y))
23805 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23807 if (INTEGERP (it->slice.width))
23808 slice.width = XINT (it->slice.width);
23809 else if (FLOATP (it->slice.width))
23810 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23812 if (INTEGERP (it->slice.height))
23813 slice.height = XINT (it->slice.height);
23814 else if (FLOATP (it->slice.height))
23815 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23817 if (slice.x >= img->width)
23818 slice.x = img->width;
23819 if (slice.y >= img->height)
23820 slice.y = img->height;
23821 if (slice.x + slice.width >= img->width)
23822 slice.width = img->width - slice.x;
23823 if (slice.y + slice.height > img->height)
23824 slice.height = img->height - slice.y;
23826 if (slice.width == 0 || slice.height == 0)
23827 return;
23829 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23831 it->descent = slice.height - glyph_ascent;
23832 if (slice.y == 0)
23833 it->descent += img->vmargin;
23834 if (slice.y + slice.height == img->height)
23835 it->descent += img->vmargin;
23836 it->phys_descent = it->descent;
23838 it->pixel_width = slice.width;
23839 if (slice.x == 0)
23840 it->pixel_width += img->hmargin;
23841 if (slice.x + slice.width == img->width)
23842 it->pixel_width += img->hmargin;
23844 /* It's quite possible for images to have an ascent greater than
23845 their height, so don't get confused in that case. */
23846 if (it->descent < 0)
23847 it->descent = 0;
23849 it->nglyphs = 1;
23851 if (face->box != FACE_NO_BOX)
23853 if (face->box_line_width > 0)
23855 if (slice.y == 0)
23856 it->ascent += face->box_line_width;
23857 if (slice.y + slice.height == img->height)
23858 it->descent += face->box_line_width;
23861 if (it->start_of_box_run_p && slice.x == 0)
23862 it->pixel_width += eabs (face->box_line_width);
23863 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23864 it->pixel_width += eabs (face->box_line_width);
23867 take_vertical_position_into_account (it);
23869 /* Automatically crop wide image glyphs at right edge so we can
23870 draw the cursor on same display row. */
23871 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23872 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23874 it->pixel_width -= crop;
23875 slice.width -= crop;
23878 if (it->glyph_row)
23880 struct glyph *glyph;
23881 enum glyph_row_area area = it->area;
23883 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23884 if (glyph < it->glyph_row->glyphs[area + 1])
23886 glyph->charpos = CHARPOS (it->position);
23887 glyph->object = it->object;
23888 glyph->pixel_width = it->pixel_width;
23889 glyph->ascent = glyph_ascent;
23890 glyph->descent = it->descent;
23891 glyph->voffset = it->voffset;
23892 glyph->type = IMAGE_GLYPH;
23893 glyph->avoid_cursor_p = it->avoid_cursor_p;
23894 glyph->multibyte_p = it->multibyte_p;
23895 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23897 /* In R2L rows, the left and the right box edges need to be
23898 drawn in reverse direction. */
23899 glyph->right_box_line_p = it->start_of_box_run_p;
23900 glyph->left_box_line_p = it->end_of_box_run_p;
23902 else
23904 glyph->left_box_line_p = it->start_of_box_run_p;
23905 glyph->right_box_line_p = it->end_of_box_run_p;
23907 glyph->overlaps_vertically_p = 0;
23908 glyph->padding_p = 0;
23909 glyph->glyph_not_available_p = 0;
23910 glyph->face_id = it->face_id;
23911 glyph->u.img_id = img->id;
23912 glyph->slice.img = slice;
23913 glyph->font_type = FONT_TYPE_UNKNOWN;
23914 if (it->bidi_p)
23916 glyph->resolved_level = it->bidi_it.resolved_level;
23917 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23918 emacs_abort ();
23919 glyph->bidi_type = it->bidi_it.type;
23921 ++it->glyph_row->used[area];
23923 else
23924 IT_EXPAND_MATRIX_WIDTH (it, area);
23929 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23930 of the glyph, WIDTH and HEIGHT are the width and height of the
23931 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23933 static void
23934 append_stretch_glyph (struct it *it, Lisp_Object object,
23935 int width, int height, int ascent)
23937 struct glyph *glyph;
23938 enum glyph_row_area area = it->area;
23940 eassert (ascent >= 0 && ascent <= height);
23942 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23943 if (glyph < it->glyph_row->glyphs[area + 1])
23945 /* If the glyph row is reversed, we need to prepend the glyph
23946 rather than append it. */
23947 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23949 struct glyph *g;
23951 /* Make room for the additional glyph. */
23952 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23953 g[1] = *g;
23954 glyph = it->glyph_row->glyphs[area];
23956 glyph->charpos = CHARPOS (it->position);
23957 glyph->object = object;
23958 glyph->pixel_width = width;
23959 glyph->ascent = ascent;
23960 glyph->descent = height - ascent;
23961 glyph->voffset = it->voffset;
23962 glyph->type = STRETCH_GLYPH;
23963 glyph->avoid_cursor_p = it->avoid_cursor_p;
23964 glyph->multibyte_p = it->multibyte_p;
23965 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23967 /* In R2L rows, the left and the right box edges need to be
23968 drawn in reverse direction. */
23969 glyph->right_box_line_p = it->start_of_box_run_p;
23970 glyph->left_box_line_p = it->end_of_box_run_p;
23972 else
23974 glyph->left_box_line_p = it->start_of_box_run_p;
23975 glyph->right_box_line_p = it->end_of_box_run_p;
23977 glyph->overlaps_vertically_p = 0;
23978 glyph->padding_p = 0;
23979 glyph->glyph_not_available_p = 0;
23980 glyph->face_id = it->face_id;
23981 glyph->u.stretch.ascent = ascent;
23982 glyph->u.stretch.height = height;
23983 glyph->slice.img = null_glyph_slice;
23984 glyph->font_type = FONT_TYPE_UNKNOWN;
23985 if (it->bidi_p)
23987 glyph->resolved_level = it->bidi_it.resolved_level;
23988 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23989 emacs_abort ();
23990 glyph->bidi_type = it->bidi_it.type;
23992 else
23994 glyph->resolved_level = 0;
23995 glyph->bidi_type = UNKNOWN_BT;
23997 ++it->glyph_row->used[area];
23999 else
24000 IT_EXPAND_MATRIX_WIDTH (it, area);
24003 #endif /* HAVE_WINDOW_SYSTEM */
24005 /* Produce a stretch glyph for iterator IT. IT->object is the value
24006 of the glyph property displayed. The value must be a list
24007 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24008 being recognized:
24010 1. `:width WIDTH' specifies that the space should be WIDTH *
24011 canonical char width wide. WIDTH may be an integer or floating
24012 point number.
24014 2. `:relative-width FACTOR' specifies that the width of the stretch
24015 should be computed from the width of the first character having the
24016 `glyph' property, and should be FACTOR times that width.
24018 3. `:align-to HPOS' specifies that the space should be wide enough
24019 to reach HPOS, a value in canonical character units.
24021 Exactly one of the above pairs must be present.
24023 4. `:height HEIGHT' specifies that the height of the stretch produced
24024 should be HEIGHT, measured in canonical character units.
24026 5. `:relative-height FACTOR' specifies that the height of the
24027 stretch should be FACTOR times the height of the characters having
24028 the glyph property.
24030 Either none or exactly one of 4 or 5 must be present.
24032 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24033 of the stretch should be used for the ascent of the stretch.
24034 ASCENT must be in the range 0 <= ASCENT <= 100. */
24036 void
24037 produce_stretch_glyph (struct it *it)
24039 /* (space :width WIDTH :height HEIGHT ...) */
24040 Lisp_Object prop, plist;
24041 int width = 0, height = 0, align_to = -1;
24042 int zero_width_ok_p = 0;
24043 double tem;
24044 struct font *font = NULL;
24046 #ifdef HAVE_WINDOW_SYSTEM
24047 int ascent = 0;
24048 int zero_height_ok_p = 0;
24050 if (FRAME_WINDOW_P (it->f))
24052 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24053 font = face->font ? face->font : FRAME_FONT (it->f);
24054 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24056 #endif
24058 /* List should start with `space'. */
24059 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24060 plist = XCDR (it->object);
24062 /* Compute the width of the stretch. */
24063 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24064 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24066 /* Absolute width `:width WIDTH' specified and valid. */
24067 zero_width_ok_p = 1;
24068 width = (int)tem;
24070 #ifdef HAVE_WINDOW_SYSTEM
24071 else if (FRAME_WINDOW_P (it->f)
24072 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24074 /* Relative width `:relative-width FACTOR' specified and valid.
24075 Compute the width of the characters having the `glyph'
24076 property. */
24077 struct it it2;
24078 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24080 it2 = *it;
24081 if (it->multibyte_p)
24082 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24083 else
24085 it2.c = it2.char_to_display = *p, it2.len = 1;
24086 if (! ASCII_CHAR_P (it2.c))
24087 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24090 it2.glyph_row = NULL;
24091 it2.what = IT_CHARACTER;
24092 x_produce_glyphs (&it2);
24093 width = NUMVAL (prop) * it2.pixel_width;
24095 #endif /* HAVE_WINDOW_SYSTEM */
24096 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24097 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24099 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24100 align_to = (align_to < 0
24102 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24103 else if (align_to < 0)
24104 align_to = window_box_left_offset (it->w, TEXT_AREA);
24105 width = max (0, (int)tem + align_to - it->current_x);
24106 zero_width_ok_p = 1;
24108 else
24109 /* Nothing specified -> width defaults to canonical char width. */
24110 width = FRAME_COLUMN_WIDTH (it->f);
24112 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24113 width = 1;
24115 #ifdef HAVE_WINDOW_SYSTEM
24116 /* Compute height. */
24117 if (FRAME_WINDOW_P (it->f))
24119 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24120 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24122 height = (int)tem;
24123 zero_height_ok_p = 1;
24125 else if (prop = Fplist_get (plist, QCrelative_height),
24126 NUMVAL (prop) > 0)
24127 height = FONT_HEIGHT (font) * NUMVAL (prop);
24128 else
24129 height = FONT_HEIGHT (font);
24131 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24132 height = 1;
24134 /* Compute percentage of height used for ascent. If
24135 `:ascent ASCENT' is present and valid, use that. Otherwise,
24136 derive the ascent from the font in use. */
24137 if (prop = Fplist_get (plist, QCascent),
24138 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24139 ascent = height * NUMVAL (prop) / 100.0;
24140 else if (!NILP (prop)
24141 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24142 ascent = min (max (0, (int)tem), height);
24143 else
24144 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24146 else
24147 #endif /* HAVE_WINDOW_SYSTEM */
24148 height = 1;
24150 if (width > 0 && it->line_wrap != TRUNCATE
24151 && it->current_x + width > it->last_visible_x)
24153 width = it->last_visible_x - it->current_x;
24154 #ifdef HAVE_WINDOW_SYSTEM
24155 /* Subtract one more pixel from the stretch width, but only on
24156 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24157 width -= FRAME_WINDOW_P (it->f);
24158 #endif
24161 if (width > 0 && height > 0 && it->glyph_row)
24163 Lisp_Object o_object = it->object;
24164 Lisp_Object object = it->stack[it->sp - 1].string;
24165 int n = width;
24167 if (!STRINGP (object))
24168 object = it->w->contents;
24169 #ifdef HAVE_WINDOW_SYSTEM
24170 if (FRAME_WINDOW_P (it->f))
24171 append_stretch_glyph (it, object, width, height, ascent);
24172 else
24173 #endif
24175 it->object = object;
24176 it->char_to_display = ' ';
24177 it->pixel_width = it->len = 1;
24178 while (n--)
24179 tty_append_glyph (it);
24180 it->object = o_object;
24184 it->pixel_width = width;
24185 #ifdef HAVE_WINDOW_SYSTEM
24186 if (FRAME_WINDOW_P (it->f))
24188 it->ascent = it->phys_ascent = ascent;
24189 it->descent = it->phys_descent = height - it->ascent;
24190 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24191 take_vertical_position_into_account (it);
24193 else
24194 #endif
24195 it->nglyphs = width;
24198 /* Get information about special display element WHAT in an
24199 environment described by IT. WHAT is one of IT_TRUNCATION or
24200 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24201 non-null glyph_row member. This function ensures that fields like
24202 face_id, c, len of IT are left untouched. */
24204 static void
24205 produce_special_glyphs (struct it *it, enum display_element_type what)
24207 struct it temp_it;
24208 Lisp_Object gc;
24209 GLYPH glyph;
24211 temp_it = *it;
24212 temp_it.object = make_number (0);
24213 memset (&temp_it.current, 0, sizeof temp_it.current);
24215 if (what == IT_CONTINUATION)
24217 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24218 if (it->bidi_it.paragraph_dir == R2L)
24219 SET_GLYPH_FROM_CHAR (glyph, '/');
24220 else
24221 SET_GLYPH_FROM_CHAR (glyph, '\\');
24222 if (it->dp
24223 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24225 /* FIXME: Should we mirror GC for R2L lines? */
24226 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24227 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24230 else if (what == IT_TRUNCATION)
24232 /* Truncation glyph. */
24233 SET_GLYPH_FROM_CHAR (glyph, '$');
24234 if (it->dp
24235 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24237 /* FIXME: Should we mirror GC for R2L lines? */
24238 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24239 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24242 else
24243 emacs_abort ();
24245 #ifdef HAVE_WINDOW_SYSTEM
24246 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24247 is turned off, we precede the truncation/continuation glyphs by a
24248 stretch glyph whose width is computed such that these special
24249 glyphs are aligned at the window margin, even when very different
24250 fonts are used in different glyph rows. */
24251 if (FRAME_WINDOW_P (temp_it.f)
24252 /* init_iterator calls this with it->glyph_row == NULL, and it
24253 wants only the pixel width of the truncation/continuation
24254 glyphs. */
24255 && temp_it.glyph_row
24256 /* insert_left_trunc_glyphs calls us at the beginning of the
24257 row, and it has its own calculation of the stretch glyph
24258 width. */
24259 && temp_it.glyph_row->used[TEXT_AREA] > 0
24260 && (temp_it.glyph_row->reversed_p
24261 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24262 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24264 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24266 if (stretch_width > 0)
24268 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24269 struct font *font =
24270 face->font ? face->font : FRAME_FONT (temp_it.f);
24271 int stretch_ascent =
24272 (((temp_it.ascent + temp_it.descent)
24273 * FONT_BASE (font)) / FONT_HEIGHT (font));
24275 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24276 temp_it.ascent + temp_it.descent,
24277 stretch_ascent);
24280 #endif
24282 temp_it.dp = NULL;
24283 temp_it.what = IT_CHARACTER;
24284 temp_it.len = 1;
24285 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24286 temp_it.face_id = GLYPH_FACE (glyph);
24287 temp_it.len = CHAR_BYTES (temp_it.c);
24289 PRODUCE_GLYPHS (&temp_it);
24290 it->pixel_width = temp_it.pixel_width;
24291 it->nglyphs = temp_it.pixel_width;
24294 #ifdef HAVE_WINDOW_SYSTEM
24296 /* Calculate line-height and line-spacing properties.
24297 An integer value specifies explicit pixel value.
24298 A float value specifies relative value to current face height.
24299 A cons (float . face-name) specifies relative value to
24300 height of specified face font.
24302 Returns height in pixels, or nil. */
24305 static Lisp_Object
24306 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24307 int boff, int override)
24309 Lisp_Object face_name = Qnil;
24310 int ascent, descent, height;
24312 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24313 return val;
24315 if (CONSP (val))
24317 face_name = XCAR (val);
24318 val = XCDR (val);
24319 if (!NUMBERP (val))
24320 val = make_number (1);
24321 if (NILP (face_name))
24323 height = it->ascent + it->descent;
24324 goto scale;
24328 if (NILP (face_name))
24330 font = FRAME_FONT (it->f);
24331 boff = FRAME_BASELINE_OFFSET (it->f);
24333 else if (EQ (face_name, Qt))
24335 override = 0;
24337 else
24339 int face_id;
24340 struct face *face;
24342 face_id = lookup_named_face (it->f, face_name, 0);
24343 if (face_id < 0)
24344 return make_number (-1);
24346 face = FACE_FROM_ID (it->f, face_id);
24347 font = face->font;
24348 if (font == NULL)
24349 return make_number (-1);
24350 boff = font->baseline_offset;
24351 if (font->vertical_centering)
24352 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24355 ascent = FONT_BASE (font) + boff;
24356 descent = FONT_DESCENT (font) - boff;
24358 if (override)
24360 it->override_ascent = ascent;
24361 it->override_descent = descent;
24362 it->override_boff = boff;
24365 height = ascent + descent;
24367 scale:
24368 if (FLOATP (val))
24369 height = (int)(XFLOAT_DATA (val) * height);
24370 else if (INTEGERP (val))
24371 height *= XINT (val);
24373 return make_number (height);
24377 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24378 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24379 and only if this is for a character for which no font was found.
24381 If the display method (it->glyphless_method) is
24382 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24383 length of the acronym or the hexadecimal string, UPPER_XOFF and
24384 UPPER_YOFF are pixel offsets for the upper part of the string,
24385 LOWER_XOFF and LOWER_YOFF are for the lower part.
24387 For the other display methods, LEN through LOWER_YOFF are zero. */
24389 static void
24390 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24391 short upper_xoff, short upper_yoff,
24392 short lower_xoff, short lower_yoff)
24394 struct glyph *glyph;
24395 enum glyph_row_area area = it->area;
24397 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24398 if (glyph < it->glyph_row->glyphs[area + 1])
24400 /* If the glyph row is reversed, we need to prepend the glyph
24401 rather than append it. */
24402 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24404 struct glyph *g;
24406 /* Make room for the additional glyph. */
24407 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24408 g[1] = *g;
24409 glyph = it->glyph_row->glyphs[area];
24411 glyph->charpos = CHARPOS (it->position);
24412 glyph->object = it->object;
24413 glyph->pixel_width = it->pixel_width;
24414 glyph->ascent = it->ascent;
24415 glyph->descent = it->descent;
24416 glyph->voffset = it->voffset;
24417 glyph->type = GLYPHLESS_GLYPH;
24418 glyph->u.glyphless.method = it->glyphless_method;
24419 glyph->u.glyphless.for_no_font = for_no_font;
24420 glyph->u.glyphless.len = len;
24421 glyph->u.glyphless.ch = it->c;
24422 glyph->slice.glyphless.upper_xoff = upper_xoff;
24423 glyph->slice.glyphless.upper_yoff = upper_yoff;
24424 glyph->slice.glyphless.lower_xoff = lower_xoff;
24425 glyph->slice.glyphless.lower_yoff = lower_yoff;
24426 glyph->avoid_cursor_p = it->avoid_cursor_p;
24427 glyph->multibyte_p = it->multibyte_p;
24428 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24430 /* In R2L rows, the left and the right box edges need to be
24431 drawn in reverse direction. */
24432 glyph->right_box_line_p = it->start_of_box_run_p;
24433 glyph->left_box_line_p = it->end_of_box_run_p;
24435 else
24437 glyph->left_box_line_p = it->start_of_box_run_p;
24438 glyph->right_box_line_p = it->end_of_box_run_p;
24440 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24441 || it->phys_descent > it->descent);
24442 glyph->padding_p = 0;
24443 glyph->glyph_not_available_p = 0;
24444 glyph->face_id = face_id;
24445 glyph->font_type = FONT_TYPE_UNKNOWN;
24446 if (it->bidi_p)
24448 glyph->resolved_level = it->bidi_it.resolved_level;
24449 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24450 emacs_abort ();
24451 glyph->bidi_type = it->bidi_it.type;
24453 ++it->glyph_row->used[area];
24455 else
24456 IT_EXPAND_MATRIX_WIDTH (it, area);
24460 /* Produce a glyph for a glyphless character for iterator IT.
24461 IT->glyphless_method specifies which method to use for displaying
24462 the character. See the description of enum
24463 glyphless_display_method in dispextern.h for the detail.
24465 FOR_NO_FONT is nonzero if and only if this is for a character for
24466 which no font was found. ACRONYM, if non-nil, is an acronym string
24467 for the character. */
24469 static void
24470 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24472 int face_id;
24473 struct face *face;
24474 struct font *font;
24475 int base_width, base_height, width, height;
24476 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24477 int len;
24479 /* Get the metrics of the base font. We always refer to the current
24480 ASCII face. */
24481 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24482 font = face->font ? face->font : FRAME_FONT (it->f);
24483 it->ascent = FONT_BASE (font) + font->baseline_offset;
24484 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24485 base_height = it->ascent + it->descent;
24486 base_width = font->average_width;
24488 /* Get a face ID for the glyph by utilizing a cache (the same way as
24489 done for `escape-glyph' in get_next_display_element). */
24490 if (it->f == last_glyphless_glyph_frame
24491 && it->face_id == last_glyphless_glyph_face_id)
24493 face_id = last_glyphless_glyph_merged_face_id;
24495 else
24497 /* Merge the `glyphless-char' face into the current face. */
24498 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24499 last_glyphless_glyph_frame = it->f;
24500 last_glyphless_glyph_face_id = it->face_id;
24501 last_glyphless_glyph_merged_face_id = face_id;
24504 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24506 it->pixel_width = THIN_SPACE_WIDTH;
24507 len = 0;
24508 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24510 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24512 width = CHAR_WIDTH (it->c);
24513 if (width == 0)
24514 width = 1;
24515 else if (width > 4)
24516 width = 4;
24517 it->pixel_width = base_width * width;
24518 len = 0;
24519 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24521 else
24523 char buf[7];
24524 const char *str;
24525 unsigned int code[6];
24526 int upper_len;
24527 int ascent, descent;
24528 struct font_metrics metrics_upper, metrics_lower;
24530 face = FACE_FROM_ID (it->f, face_id);
24531 font = face->font ? face->font : FRAME_FONT (it->f);
24532 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24534 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24536 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24537 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24538 if (CONSP (acronym))
24539 acronym = XCAR (acronym);
24540 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24542 else
24544 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24545 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24546 str = buf;
24548 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24549 code[len] = font->driver->encode_char (font, str[len]);
24550 upper_len = (len + 1) / 2;
24551 font->driver->text_extents (font, code, upper_len,
24552 &metrics_upper);
24553 font->driver->text_extents (font, code + upper_len, len - upper_len,
24554 &metrics_lower);
24558 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24559 width = max (metrics_upper.width, metrics_lower.width) + 4;
24560 upper_xoff = upper_yoff = 2; /* the typical case */
24561 if (base_width >= width)
24563 /* Align the upper to the left, the lower to the right. */
24564 it->pixel_width = base_width;
24565 lower_xoff = base_width - 2 - metrics_lower.width;
24567 else
24569 /* Center the shorter one. */
24570 it->pixel_width = width;
24571 if (metrics_upper.width >= metrics_lower.width)
24572 lower_xoff = (width - metrics_lower.width) / 2;
24573 else
24575 /* FIXME: This code doesn't look right. It formerly was
24576 missing the "lower_xoff = 0;", which couldn't have
24577 been right since it left lower_xoff uninitialized. */
24578 lower_xoff = 0;
24579 upper_xoff = (width - metrics_upper.width) / 2;
24583 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24584 top, bottom, and between upper and lower strings. */
24585 height = (metrics_upper.ascent + metrics_upper.descent
24586 + metrics_lower.ascent + metrics_lower.descent) + 5;
24587 /* Center vertically.
24588 H:base_height, D:base_descent
24589 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24591 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24592 descent = D - H/2 + h/2;
24593 lower_yoff = descent - 2 - ld;
24594 upper_yoff = lower_yoff - la - 1 - ud; */
24595 ascent = - (it->descent - (base_height + height + 1) / 2);
24596 descent = it->descent - (base_height - height) / 2;
24597 lower_yoff = descent - 2 - metrics_lower.descent;
24598 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24599 - metrics_upper.descent);
24600 /* Don't make the height shorter than the base height. */
24601 if (height > base_height)
24603 it->ascent = ascent;
24604 it->descent = descent;
24608 it->phys_ascent = it->ascent;
24609 it->phys_descent = it->descent;
24610 if (it->glyph_row)
24611 append_glyphless_glyph (it, face_id, for_no_font, len,
24612 upper_xoff, upper_yoff,
24613 lower_xoff, lower_yoff);
24614 it->nglyphs = 1;
24615 take_vertical_position_into_account (it);
24619 /* RIF:
24620 Produce glyphs/get display metrics for the display element IT is
24621 loaded with. See the description of struct it in dispextern.h
24622 for an overview of struct it. */
24624 void
24625 x_produce_glyphs (struct it *it)
24627 int extra_line_spacing = it->extra_line_spacing;
24629 it->glyph_not_available_p = 0;
24631 if (it->what == IT_CHARACTER)
24633 XChar2b char2b;
24634 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24635 struct font *font = face->font;
24636 struct font_metrics *pcm = NULL;
24637 int boff; /* baseline offset */
24639 if (font == NULL)
24641 /* When no suitable font is found, display this character by
24642 the method specified in the first extra slot of
24643 Vglyphless_char_display. */
24644 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24646 eassert (it->what == IT_GLYPHLESS);
24647 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24648 goto done;
24651 boff = font->baseline_offset;
24652 if (font->vertical_centering)
24653 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24655 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24657 int stretched_p;
24659 it->nglyphs = 1;
24661 if (it->override_ascent >= 0)
24663 it->ascent = it->override_ascent;
24664 it->descent = it->override_descent;
24665 boff = it->override_boff;
24667 else
24669 it->ascent = FONT_BASE (font) + boff;
24670 it->descent = FONT_DESCENT (font) - boff;
24673 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24675 pcm = get_per_char_metric (font, &char2b);
24676 if (pcm->width == 0
24677 && pcm->rbearing == 0 && pcm->lbearing == 0)
24678 pcm = NULL;
24681 if (pcm)
24683 it->phys_ascent = pcm->ascent + boff;
24684 it->phys_descent = pcm->descent - boff;
24685 it->pixel_width = pcm->width;
24687 else
24689 it->glyph_not_available_p = 1;
24690 it->phys_ascent = it->ascent;
24691 it->phys_descent = it->descent;
24692 it->pixel_width = font->space_width;
24695 if (it->constrain_row_ascent_descent_p)
24697 if (it->descent > it->max_descent)
24699 it->ascent += it->descent - it->max_descent;
24700 it->descent = it->max_descent;
24702 if (it->ascent > it->max_ascent)
24704 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24705 it->ascent = it->max_ascent;
24707 it->phys_ascent = min (it->phys_ascent, it->ascent);
24708 it->phys_descent = min (it->phys_descent, it->descent);
24709 extra_line_spacing = 0;
24712 /* If this is a space inside a region of text with
24713 `space-width' property, change its width. */
24714 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24715 if (stretched_p)
24716 it->pixel_width *= XFLOATINT (it->space_width);
24718 /* If face has a box, add the box thickness to the character
24719 height. If character has a box line to the left and/or
24720 right, add the box line width to the character's width. */
24721 if (face->box != FACE_NO_BOX)
24723 int thick = face->box_line_width;
24725 if (thick > 0)
24727 it->ascent += thick;
24728 it->descent += thick;
24730 else
24731 thick = -thick;
24733 if (it->start_of_box_run_p)
24734 it->pixel_width += thick;
24735 if (it->end_of_box_run_p)
24736 it->pixel_width += thick;
24739 /* If face has an overline, add the height of the overline
24740 (1 pixel) and a 1 pixel margin to the character height. */
24741 if (face->overline_p)
24742 it->ascent += overline_margin;
24744 if (it->constrain_row_ascent_descent_p)
24746 if (it->ascent > it->max_ascent)
24747 it->ascent = it->max_ascent;
24748 if (it->descent > it->max_descent)
24749 it->descent = it->max_descent;
24752 take_vertical_position_into_account (it);
24754 /* If we have to actually produce glyphs, do it. */
24755 if (it->glyph_row)
24757 if (stretched_p)
24759 /* Translate a space with a `space-width' property
24760 into a stretch glyph. */
24761 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24762 / FONT_HEIGHT (font));
24763 append_stretch_glyph (it, it->object, it->pixel_width,
24764 it->ascent + it->descent, ascent);
24766 else
24767 append_glyph (it);
24769 /* If characters with lbearing or rbearing are displayed
24770 in this line, record that fact in a flag of the
24771 glyph row. This is used to optimize X output code. */
24772 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24773 it->glyph_row->contains_overlapping_glyphs_p = 1;
24775 if (! stretched_p && it->pixel_width == 0)
24776 /* We assure that all visible glyphs have at least 1-pixel
24777 width. */
24778 it->pixel_width = 1;
24780 else if (it->char_to_display == '\n')
24782 /* A newline has no width, but we need the height of the
24783 line. But if previous part of the line sets a height,
24784 don't increase that height */
24786 Lisp_Object height;
24787 Lisp_Object total_height = Qnil;
24789 it->override_ascent = -1;
24790 it->pixel_width = 0;
24791 it->nglyphs = 0;
24793 height = get_it_property (it, Qline_height);
24794 /* Split (line-height total-height) list */
24795 if (CONSP (height)
24796 && CONSP (XCDR (height))
24797 && NILP (XCDR (XCDR (height))))
24799 total_height = XCAR (XCDR (height));
24800 height = XCAR (height);
24802 height = calc_line_height_property (it, height, font, boff, 1);
24804 if (it->override_ascent >= 0)
24806 it->ascent = it->override_ascent;
24807 it->descent = it->override_descent;
24808 boff = it->override_boff;
24810 else
24812 it->ascent = FONT_BASE (font) + boff;
24813 it->descent = FONT_DESCENT (font) - boff;
24816 if (EQ (height, Qt))
24818 if (it->descent > it->max_descent)
24820 it->ascent += it->descent - it->max_descent;
24821 it->descent = it->max_descent;
24823 if (it->ascent > it->max_ascent)
24825 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24826 it->ascent = it->max_ascent;
24828 it->phys_ascent = min (it->phys_ascent, it->ascent);
24829 it->phys_descent = min (it->phys_descent, it->descent);
24830 it->constrain_row_ascent_descent_p = 1;
24831 extra_line_spacing = 0;
24833 else
24835 Lisp_Object spacing;
24837 it->phys_ascent = it->ascent;
24838 it->phys_descent = it->descent;
24840 if ((it->max_ascent > 0 || it->max_descent > 0)
24841 && face->box != FACE_NO_BOX
24842 && face->box_line_width > 0)
24844 it->ascent += face->box_line_width;
24845 it->descent += face->box_line_width;
24847 if (!NILP (height)
24848 && XINT (height) > it->ascent + it->descent)
24849 it->ascent = XINT (height) - it->descent;
24851 if (!NILP (total_height))
24852 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24853 else
24855 spacing = get_it_property (it, Qline_spacing);
24856 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24858 if (INTEGERP (spacing))
24860 extra_line_spacing = XINT (spacing);
24861 if (!NILP (total_height))
24862 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24866 else /* i.e. (it->char_to_display == '\t') */
24868 if (font->space_width > 0)
24870 int tab_width = it->tab_width * font->space_width;
24871 int x = it->current_x + it->continuation_lines_width;
24872 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24874 /* If the distance from the current position to the next tab
24875 stop is less than a space character width, use the
24876 tab stop after that. */
24877 if (next_tab_x - x < font->space_width)
24878 next_tab_x += tab_width;
24880 it->pixel_width = next_tab_x - x;
24881 it->nglyphs = 1;
24882 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24883 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24885 if (it->glyph_row)
24887 append_stretch_glyph (it, it->object, it->pixel_width,
24888 it->ascent + it->descent, it->ascent);
24891 else
24893 it->pixel_width = 0;
24894 it->nglyphs = 1;
24898 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24900 /* A static composition.
24902 Note: A composition is represented as one glyph in the
24903 glyph matrix. There are no padding glyphs.
24905 Important note: pixel_width, ascent, and descent are the
24906 values of what is drawn by draw_glyphs (i.e. the values of
24907 the overall glyphs composed). */
24908 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24909 int boff; /* baseline offset */
24910 struct composition *cmp = composition_table[it->cmp_it.id];
24911 int glyph_len = cmp->glyph_len;
24912 struct font *font = face->font;
24914 it->nglyphs = 1;
24916 /* If we have not yet calculated pixel size data of glyphs of
24917 the composition for the current face font, calculate them
24918 now. Theoretically, we have to check all fonts for the
24919 glyphs, but that requires much time and memory space. So,
24920 here we check only the font of the first glyph. This may
24921 lead to incorrect display, but it's very rare, and C-l
24922 (recenter-top-bottom) can correct the display anyway. */
24923 if (! cmp->font || cmp->font != font)
24925 /* Ascent and descent of the font of the first character
24926 of this composition (adjusted by baseline offset).
24927 Ascent and descent of overall glyphs should not be less
24928 than these, respectively. */
24929 int font_ascent, font_descent, font_height;
24930 /* Bounding box of the overall glyphs. */
24931 int leftmost, rightmost, lowest, highest;
24932 int lbearing, rbearing;
24933 int i, width, ascent, descent;
24934 int left_padded = 0, right_padded = 0;
24935 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24936 XChar2b char2b;
24937 struct font_metrics *pcm;
24938 int font_not_found_p;
24939 ptrdiff_t pos;
24941 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24942 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24943 break;
24944 if (glyph_len < cmp->glyph_len)
24945 right_padded = 1;
24946 for (i = 0; i < glyph_len; i++)
24948 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24949 break;
24950 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24952 if (i > 0)
24953 left_padded = 1;
24955 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24956 : IT_CHARPOS (*it));
24957 /* If no suitable font is found, use the default font. */
24958 font_not_found_p = font == NULL;
24959 if (font_not_found_p)
24961 face = face->ascii_face;
24962 font = face->font;
24964 boff = font->baseline_offset;
24965 if (font->vertical_centering)
24966 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24967 font_ascent = FONT_BASE (font) + boff;
24968 font_descent = FONT_DESCENT (font) - boff;
24969 font_height = FONT_HEIGHT (font);
24971 cmp->font = font;
24973 pcm = NULL;
24974 if (! font_not_found_p)
24976 get_char_face_and_encoding (it->f, c, it->face_id,
24977 &char2b, 0);
24978 pcm = get_per_char_metric (font, &char2b);
24981 /* Initialize the bounding box. */
24982 if (pcm)
24984 width = cmp->glyph_len > 0 ? pcm->width : 0;
24985 ascent = pcm->ascent;
24986 descent = pcm->descent;
24987 lbearing = pcm->lbearing;
24988 rbearing = pcm->rbearing;
24990 else
24992 width = cmp->glyph_len > 0 ? font->space_width : 0;
24993 ascent = FONT_BASE (font);
24994 descent = FONT_DESCENT (font);
24995 lbearing = 0;
24996 rbearing = width;
24999 rightmost = width;
25000 leftmost = 0;
25001 lowest = - descent + boff;
25002 highest = ascent + boff;
25004 if (! font_not_found_p
25005 && font->default_ascent
25006 && CHAR_TABLE_P (Vuse_default_ascent)
25007 && !NILP (Faref (Vuse_default_ascent,
25008 make_number (it->char_to_display))))
25009 highest = font->default_ascent + boff;
25011 /* Draw the first glyph at the normal position. It may be
25012 shifted to right later if some other glyphs are drawn
25013 at the left. */
25014 cmp->offsets[i * 2] = 0;
25015 cmp->offsets[i * 2 + 1] = boff;
25016 cmp->lbearing = lbearing;
25017 cmp->rbearing = rbearing;
25019 /* Set cmp->offsets for the remaining glyphs. */
25020 for (i++; i < glyph_len; i++)
25022 int left, right, btm, top;
25023 int ch = COMPOSITION_GLYPH (cmp, i);
25024 int face_id;
25025 struct face *this_face;
25027 if (ch == '\t')
25028 ch = ' ';
25029 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25030 this_face = FACE_FROM_ID (it->f, face_id);
25031 font = this_face->font;
25033 if (font == NULL)
25034 pcm = NULL;
25035 else
25037 get_char_face_and_encoding (it->f, ch, face_id,
25038 &char2b, 0);
25039 pcm = get_per_char_metric (font, &char2b);
25041 if (! pcm)
25042 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25043 else
25045 width = pcm->width;
25046 ascent = pcm->ascent;
25047 descent = pcm->descent;
25048 lbearing = pcm->lbearing;
25049 rbearing = pcm->rbearing;
25050 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25052 /* Relative composition with or without
25053 alternate chars. */
25054 left = (leftmost + rightmost - width) / 2;
25055 btm = - descent + boff;
25056 if (font->relative_compose
25057 && (! CHAR_TABLE_P (Vignore_relative_composition)
25058 || NILP (Faref (Vignore_relative_composition,
25059 make_number (ch)))))
25062 if (- descent >= font->relative_compose)
25063 /* One extra pixel between two glyphs. */
25064 btm = highest + 1;
25065 else if (ascent <= 0)
25066 /* One extra pixel between two glyphs. */
25067 btm = lowest - 1 - ascent - descent;
25070 else
25072 /* A composition rule is specified by an integer
25073 value that encodes global and new reference
25074 points (GREF and NREF). GREF and NREF are
25075 specified by numbers as below:
25077 0---1---2 -- ascent
25081 9--10--11 -- center
25083 ---3---4---5--- baseline
25085 6---7---8 -- descent
25087 int rule = COMPOSITION_RULE (cmp, i);
25088 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25090 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25091 grefx = gref % 3, nrefx = nref % 3;
25092 grefy = gref / 3, nrefy = nref / 3;
25093 if (xoff)
25094 xoff = font_height * (xoff - 128) / 256;
25095 if (yoff)
25096 yoff = font_height * (yoff - 128) / 256;
25098 left = (leftmost
25099 + grefx * (rightmost - leftmost) / 2
25100 - nrefx * width / 2
25101 + xoff);
25103 btm = ((grefy == 0 ? highest
25104 : grefy == 1 ? 0
25105 : grefy == 2 ? lowest
25106 : (highest + lowest) / 2)
25107 - (nrefy == 0 ? ascent + descent
25108 : nrefy == 1 ? descent - boff
25109 : nrefy == 2 ? 0
25110 : (ascent + descent) / 2)
25111 + yoff);
25114 cmp->offsets[i * 2] = left;
25115 cmp->offsets[i * 2 + 1] = btm + descent;
25117 /* Update the bounding box of the overall glyphs. */
25118 if (width > 0)
25120 right = left + width;
25121 if (left < leftmost)
25122 leftmost = left;
25123 if (right > rightmost)
25124 rightmost = right;
25126 top = btm + descent + ascent;
25127 if (top > highest)
25128 highest = top;
25129 if (btm < lowest)
25130 lowest = btm;
25132 if (cmp->lbearing > left + lbearing)
25133 cmp->lbearing = left + lbearing;
25134 if (cmp->rbearing < left + rbearing)
25135 cmp->rbearing = left + rbearing;
25139 /* If there are glyphs whose x-offsets are negative,
25140 shift all glyphs to the right and make all x-offsets
25141 non-negative. */
25142 if (leftmost < 0)
25144 for (i = 0; i < cmp->glyph_len; i++)
25145 cmp->offsets[i * 2] -= leftmost;
25146 rightmost -= leftmost;
25147 cmp->lbearing -= leftmost;
25148 cmp->rbearing -= leftmost;
25151 if (left_padded && cmp->lbearing < 0)
25153 for (i = 0; i < cmp->glyph_len; i++)
25154 cmp->offsets[i * 2] -= cmp->lbearing;
25155 rightmost -= cmp->lbearing;
25156 cmp->rbearing -= cmp->lbearing;
25157 cmp->lbearing = 0;
25159 if (right_padded && rightmost < cmp->rbearing)
25161 rightmost = cmp->rbearing;
25164 cmp->pixel_width = rightmost;
25165 cmp->ascent = highest;
25166 cmp->descent = - lowest;
25167 if (cmp->ascent < font_ascent)
25168 cmp->ascent = font_ascent;
25169 if (cmp->descent < font_descent)
25170 cmp->descent = font_descent;
25173 if (it->glyph_row
25174 && (cmp->lbearing < 0
25175 || cmp->rbearing > cmp->pixel_width))
25176 it->glyph_row->contains_overlapping_glyphs_p = 1;
25178 it->pixel_width = cmp->pixel_width;
25179 it->ascent = it->phys_ascent = cmp->ascent;
25180 it->descent = it->phys_descent = cmp->descent;
25181 if (face->box != FACE_NO_BOX)
25183 int thick = face->box_line_width;
25185 if (thick > 0)
25187 it->ascent += thick;
25188 it->descent += thick;
25190 else
25191 thick = - thick;
25193 if (it->start_of_box_run_p)
25194 it->pixel_width += thick;
25195 if (it->end_of_box_run_p)
25196 it->pixel_width += thick;
25199 /* If face has an overline, add the height of the overline
25200 (1 pixel) and a 1 pixel margin to the character height. */
25201 if (face->overline_p)
25202 it->ascent += overline_margin;
25204 take_vertical_position_into_account (it);
25205 if (it->ascent < 0)
25206 it->ascent = 0;
25207 if (it->descent < 0)
25208 it->descent = 0;
25210 if (it->glyph_row && cmp->glyph_len > 0)
25211 append_composite_glyph (it);
25213 else if (it->what == IT_COMPOSITION)
25215 /* A dynamic (automatic) composition. */
25216 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25217 Lisp_Object gstring;
25218 struct font_metrics metrics;
25220 it->nglyphs = 1;
25222 gstring = composition_gstring_from_id (it->cmp_it.id);
25223 it->pixel_width
25224 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25225 &metrics);
25226 if (it->glyph_row
25227 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25228 it->glyph_row->contains_overlapping_glyphs_p = 1;
25229 it->ascent = it->phys_ascent = metrics.ascent;
25230 it->descent = it->phys_descent = metrics.descent;
25231 if (face->box != FACE_NO_BOX)
25233 int thick = face->box_line_width;
25235 if (thick > 0)
25237 it->ascent += thick;
25238 it->descent += thick;
25240 else
25241 thick = - thick;
25243 if (it->start_of_box_run_p)
25244 it->pixel_width += thick;
25245 if (it->end_of_box_run_p)
25246 it->pixel_width += thick;
25248 /* If face has an overline, add the height of the overline
25249 (1 pixel) and a 1 pixel margin to the character height. */
25250 if (face->overline_p)
25251 it->ascent += overline_margin;
25252 take_vertical_position_into_account (it);
25253 if (it->ascent < 0)
25254 it->ascent = 0;
25255 if (it->descent < 0)
25256 it->descent = 0;
25258 if (it->glyph_row)
25259 append_composite_glyph (it);
25261 else if (it->what == IT_GLYPHLESS)
25262 produce_glyphless_glyph (it, 0, Qnil);
25263 else if (it->what == IT_IMAGE)
25264 produce_image_glyph (it);
25265 else if (it->what == IT_STRETCH)
25266 produce_stretch_glyph (it);
25268 done:
25269 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25270 because this isn't true for images with `:ascent 100'. */
25271 eassert (it->ascent >= 0 && it->descent >= 0);
25272 if (it->area == TEXT_AREA)
25273 it->current_x += it->pixel_width;
25275 if (extra_line_spacing > 0)
25277 it->descent += extra_line_spacing;
25278 if (extra_line_spacing > it->max_extra_line_spacing)
25279 it->max_extra_line_spacing = extra_line_spacing;
25282 it->max_ascent = max (it->max_ascent, it->ascent);
25283 it->max_descent = max (it->max_descent, it->descent);
25284 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25285 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25288 /* EXPORT for RIF:
25289 Output LEN glyphs starting at START at the nominal cursor position.
25290 Advance the nominal cursor over the text. The global variable
25291 updated_window contains the window being updated, updated_row is
25292 the glyph row being updated, and updated_area is the area of that
25293 row being updated. */
25295 void
25296 x_write_glyphs (struct glyph *start, int len)
25298 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25300 eassert (updated_window && updated_row);
25301 /* When the window is hscrolled, cursor hpos can legitimately be out
25302 of bounds, but we draw the cursor at the corresponding window
25303 margin in that case. */
25304 if (!updated_row->reversed_p && chpos < 0)
25305 chpos = 0;
25306 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25307 chpos = updated_row->used[TEXT_AREA] - 1;
25309 block_input ();
25311 /* Write glyphs. */
25313 hpos = start - updated_row->glyphs[updated_area];
25314 x = draw_glyphs (updated_window, output_cursor.x,
25315 updated_row, updated_area,
25316 hpos, hpos + len,
25317 DRAW_NORMAL_TEXT, 0);
25319 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25320 if (updated_area == TEXT_AREA
25321 && updated_window->phys_cursor_on_p
25322 && updated_window->phys_cursor.vpos == output_cursor.vpos
25323 && chpos >= hpos
25324 && chpos < hpos + len)
25325 updated_window->phys_cursor_on_p = 0;
25327 unblock_input ();
25329 /* Advance the output cursor. */
25330 output_cursor.hpos += len;
25331 output_cursor.x = x;
25335 /* EXPORT for RIF:
25336 Insert LEN glyphs from START at the nominal cursor position. */
25338 void
25339 x_insert_glyphs (struct glyph *start, int len)
25341 struct frame *f;
25342 struct window *w;
25343 int line_height, shift_by_width, shifted_region_width;
25344 struct glyph_row *row;
25345 struct glyph *glyph;
25346 int frame_x, frame_y;
25347 ptrdiff_t hpos;
25349 eassert (updated_window && updated_row);
25350 block_input ();
25351 w = updated_window;
25352 f = XFRAME (WINDOW_FRAME (w));
25354 /* Get the height of the line we are in. */
25355 row = updated_row;
25356 line_height = row->height;
25358 /* Get the width of the glyphs to insert. */
25359 shift_by_width = 0;
25360 for (glyph = start; glyph < start + len; ++glyph)
25361 shift_by_width += glyph->pixel_width;
25363 /* Get the width of the region to shift right. */
25364 shifted_region_width = (window_box_width (w, updated_area)
25365 - output_cursor.x
25366 - shift_by_width);
25368 /* Shift right. */
25369 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25370 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25372 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25373 line_height, shift_by_width);
25375 /* Write the glyphs. */
25376 hpos = start - row->glyphs[updated_area];
25377 draw_glyphs (w, output_cursor.x, row, updated_area,
25378 hpos, hpos + len,
25379 DRAW_NORMAL_TEXT, 0);
25381 /* Advance the output cursor. */
25382 output_cursor.hpos += len;
25383 output_cursor.x += shift_by_width;
25384 unblock_input ();
25388 /* EXPORT for RIF:
25389 Erase the current text line from the nominal cursor position
25390 (inclusive) to pixel column TO_X (exclusive). The idea is that
25391 everything from TO_X onward is already erased.
25393 TO_X is a pixel position relative to updated_area of
25394 updated_window. TO_X == -1 means clear to the end of this area. */
25396 void
25397 x_clear_end_of_line (int to_x)
25399 struct frame *f;
25400 struct window *w = updated_window;
25401 int max_x, min_y, max_y;
25402 int from_x, from_y, to_y;
25404 eassert (updated_window && updated_row);
25405 f = XFRAME (w->frame);
25407 if (updated_row->full_width_p)
25408 max_x = WINDOW_TOTAL_WIDTH (w);
25409 else
25410 max_x = window_box_width (w, updated_area);
25411 max_y = window_text_bottom_y (w);
25413 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25414 of window. For TO_X > 0, truncate to end of drawing area. */
25415 if (to_x == 0)
25416 return;
25417 else if (to_x < 0)
25418 to_x = max_x;
25419 else
25420 to_x = min (to_x, max_x);
25422 to_y = min (max_y, output_cursor.y + updated_row->height);
25424 /* Notice if the cursor will be cleared by this operation. */
25425 if (!updated_row->full_width_p)
25426 notice_overwritten_cursor (w, updated_area,
25427 output_cursor.x, -1,
25428 updated_row->y,
25429 MATRIX_ROW_BOTTOM_Y (updated_row));
25431 from_x = output_cursor.x;
25433 /* Translate to frame coordinates. */
25434 if (updated_row->full_width_p)
25436 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25437 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25439 else
25441 int area_left = window_box_left (w, updated_area);
25442 from_x += area_left;
25443 to_x += area_left;
25446 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25447 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25448 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25450 /* Prevent inadvertently clearing to end of the X window. */
25451 if (to_x > from_x && to_y > from_y)
25453 block_input ();
25454 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25455 to_x - from_x, to_y - from_y);
25456 unblock_input ();
25460 #endif /* HAVE_WINDOW_SYSTEM */
25464 /***********************************************************************
25465 Cursor types
25466 ***********************************************************************/
25468 /* Value is the internal representation of the specified cursor type
25469 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25470 of the bar cursor. */
25472 static enum text_cursor_kinds
25473 get_specified_cursor_type (Lisp_Object arg, int *width)
25475 enum text_cursor_kinds type;
25477 if (NILP (arg))
25478 return NO_CURSOR;
25480 if (EQ (arg, Qbox))
25481 return FILLED_BOX_CURSOR;
25483 if (EQ (arg, Qhollow))
25484 return HOLLOW_BOX_CURSOR;
25486 if (EQ (arg, Qbar))
25488 *width = 2;
25489 return BAR_CURSOR;
25492 if (CONSP (arg)
25493 && EQ (XCAR (arg), Qbar)
25494 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25496 *width = XINT (XCDR (arg));
25497 return BAR_CURSOR;
25500 if (EQ (arg, Qhbar))
25502 *width = 2;
25503 return HBAR_CURSOR;
25506 if (CONSP (arg)
25507 && EQ (XCAR (arg), Qhbar)
25508 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25510 *width = XINT (XCDR (arg));
25511 return HBAR_CURSOR;
25514 /* Treat anything unknown as "hollow box cursor".
25515 It was bad to signal an error; people have trouble fixing
25516 .Xdefaults with Emacs, when it has something bad in it. */
25517 type = HOLLOW_BOX_CURSOR;
25519 return type;
25522 /* Set the default cursor types for specified frame. */
25523 void
25524 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25526 int width = 1;
25527 Lisp_Object tem;
25529 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25530 FRAME_CURSOR_WIDTH (f) = width;
25532 /* By default, set up the blink-off state depending on the on-state. */
25534 tem = Fassoc (arg, Vblink_cursor_alist);
25535 if (!NILP (tem))
25537 FRAME_BLINK_OFF_CURSOR (f)
25538 = get_specified_cursor_type (XCDR (tem), &width);
25539 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25541 else
25542 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25546 #ifdef HAVE_WINDOW_SYSTEM
25548 /* Return the cursor we want to be displayed in window W. Return
25549 width of bar/hbar cursor through WIDTH arg. Return with
25550 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25551 (i.e. if the `system caret' should track this cursor).
25553 In a mini-buffer window, we want the cursor only to appear if we
25554 are reading input from this window. For the selected window, we
25555 want the cursor type given by the frame parameter or buffer local
25556 setting of cursor-type. If explicitly marked off, draw no cursor.
25557 In all other cases, we want a hollow box cursor. */
25559 static enum text_cursor_kinds
25560 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25561 int *active_cursor)
25563 struct frame *f = XFRAME (w->frame);
25564 struct buffer *b = XBUFFER (w->contents);
25565 int cursor_type = DEFAULT_CURSOR;
25566 Lisp_Object alt_cursor;
25567 int non_selected = 0;
25569 *active_cursor = 1;
25571 /* Echo area */
25572 if (cursor_in_echo_area
25573 && FRAME_HAS_MINIBUF_P (f)
25574 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25576 if (w == XWINDOW (echo_area_window))
25578 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25580 *width = FRAME_CURSOR_WIDTH (f);
25581 return FRAME_DESIRED_CURSOR (f);
25583 else
25584 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25587 *active_cursor = 0;
25588 non_selected = 1;
25591 /* Detect a nonselected window or nonselected frame. */
25592 else if (w != XWINDOW (f->selected_window)
25593 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25595 *active_cursor = 0;
25597 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25598 return NO_CURSOR;
25600 non_selected = 1;
25603 /* Never display a cursor in a window in which cursor-type is nil. */
25604 if (NILP (BVAR (b, cursor_type)))
25605 return NO_CURSOR;
25607 /* Get the normal cursor type for this window. */
25608 if (EQ (BVAR (b, cursor_type), Qt))
25610 cursor_type = FRAME_DESIRED_CURSOR (f);
25611 *width = FRAME_CURSOR_WIDTH (f);
25613 else
25614 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25616 /* Use cursor-in-non-selected-windows instead
25617 for non-selected window or frame. */
25618 if (non_selected)
25620 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25621 if (!EQ (Qt, alt_cursor))
25622 return get_specified_cursor_type (alt_cursor, width);
25623 /* t means modify the normal cursor type. */
25624 if (cursor_type == FILLED_BOX_CURSOR)
25625 cursor_type = HOLLOW_BOX_CURSOR;
25626 else if (cursor_type == BAR_CURSOR && *width > 1)
25627 --*width;
25628 return cursor_type;
25631 /* Use normal cursor if not blinked off. */
25632 if (!w->cursor_off_p)
25634 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25636 if (cursor_type == FILLED_BOX_CURSOR)
25638 /* Using a block cursor on large images can be very annoying.
25639 So use a hollow cursor for "large" images.
25640 If image is not transparent (no mask), also use hollow cursor. */
25641 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25642 if (img != NULL && IMAGEP (img->spec))
25644 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25645 where N = size of default frame font size.
25646 This should cover most of the "tiny" icons people may use. */
25647 if (!img->mask
25648 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25649 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25650 cursor_type = HOLLOW_BOX_CURSOR;
25653 else if (cursor_type != NO_CURSOR)
25655 /* Display current only supports BOX and HOLLOW cursors for images.
25656 So for now, unconditionally use a HOLLOW cursor when cursor is
25657 not a solid box cursor. */
25658 cursor_type = HOLLOW_BOX_CURSOR;
25661 return cursor_type;
25664 /* Cursor is blinked off, so determine how to "toggle" it. */
25666 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25667 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25668 return get_specified_cursor_type (XCDR (alt_cursor), width);
25670 /* Then see if frame has specified a specific blink off cursor type. */
25671 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25673 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25674 return FRAME_BLINK_OFF_CURSOR (f);
25677 #if 0
25678 /* Some people liked having a permanently visible blinking cursor,
25679 while others had very strong opinions against it. So it was
25680 decided to remove it. KFS 2003-09-03 */
25682 /* Finally perform built-in cursor blinking:
25683 filled box <-> hollow box
25684 wide [h]bar <-> narrow [h]bar
25685 narrow [h]bar <-> no cursor
25686 other type <-> no cursor */
25688 if (cursor_type == FILLED_BOX_CURSOR)
25689 return HOLLOW_BOX_CURSOR;
25691 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25693 *width = 1;
25694 return cursor_type;
25696 #endif
25698 return NO_CURSOR;
25702 /* Notice when the text cursor of window W has been completely
25703 overwritten by a drawing operation that outputs glyphs in AREA
25704 starting at X0 and ending at X1 in the line starting at Y0 and
25705 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25706 the rest of the line after X0 has been written. Y coordinates
25707 are window-relative. */
25709 static void
25710 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25711 int x0, int x1, int y0, int y1)
25713 int cx0, cx1, cy0, cy1;
25714 struct glyph_row *row;
25716 if (!w->phys_cursor_on_p)
25717 return;
25718 if (area != TEXT_AREA)
25719 return;
25721 if (w->phys_cursor.vpos < 0
25722 || w->phys_cursor.vpos >= w->current_matrix->nrows
25723 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25724 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
25725 return;
25727 if (row->cursor_in_fringe_p)
25729 row->cursor_in_fringe_p = 0;
25730 draw_fringe_bitmap (w, row, row->reversed_p);
25731 w->phys_cursor_on_p = 0;
25732 return;
25735 cx0 = w->phys_cursor.x;
25736 cx1 = cx0 + w->phys_cursor_width;
25737 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25738 return;
25740 /* The cursor image will be completely removed from the
25741 screen if the output area intersects the cursor area in
25742 y-direction. When we draw in [y0 y1[, and some part of
25743 the cursor is at y < y0, that part must have been drawn
25744 before. When scrolling, the cursor is erased before
25745 actually scrolling, so we don't come here. When not
25746 scrolling, the rows above the old cursor row must have
25747 changed, and in this case these rows must have written
25748 over the cursor image.
25750 Likewise if part of the cursor is below y1, with the
25751 exception of the cursor being in the first blank row at
25752 the buffer and window end because update_text_area
25753 doesn't draw that row. (Except when it does, but
25754 that's handled in update_text_area.) */
25756 cy0 = w->phys_cursor.y;
25757 cy1 = cy0 + w->phys_cursor_height;
25758 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25759 return;
25761 w->phys_cursor_on_p = 0;
25764 #endif /* HAVE_WINDOW_SYSTEM */
25767 /************************************************************************
25768 Mouse Face
25769 ************************************************************************/
25771 #ifdef HAVE_WINDOW_SYSTEM
25773 /* EXPORT for RIF:
25774 Fix the display of area AREA of overlapping row ROW in window W
25775 with respect to the overlapping part OVERLAPS. */
25777 void
25778 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25779 enum glyph_row_area area, int overlaps)
25781 int i, x;
25783 block_input ();
25785 x = 0;
25786 for (i = 0; i < row->used[area];)
25788 if (row->glyphs[area][i].overlaps_vertically_p)
25790 int start = i, start_x = x;
25794 x += row->glyphs[area][i].pixel_width;
25795 ++i;
25797 while (i < row->used[area]
25798 && row->glyphs[area][i].overlaps_vertically_p);
25800 draw_glyphs (w, start_x, row, area,
25801 start, i,
25802 DRAW_NORMAL_TEXT, overlaps);
25804 else
25806 x += row->glyphs[area][i].pixel_width;
25807 ++i;
25811 unblock_input ();
25815 /* EXPORT:
25816 Draw the cursor glyph of window W in glyph row ROW. See the
25817 comment of draw_glyphs for the meaning of HL. */
25819 void
25820 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25821 enum draw_glyphs_face hl)
25823 /* If cursor hpos is out of bounds, don't draw garbage. This can
25824 happen in mini-buffer windows when switching between echo area
25825 glyphs and mini-buffer. */
25826 if ((row->reversed_p
25827 ? (w->phys_cursor.hpos >= 0)
25828 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25830 int on_p = w->phys_cursor_on_p;
25831 int x1;
25832 int hpos = w->phys_cursor.hpos;
25834 /* When the window is hscrolled, cursor hpos can legitimately be
25835 out of bounds, but we draw the cursor at the corresponding
25836 window margin in that case. */
25837 if (!row->reversed_p && hpos < 0)
25838 hpos = 0;
25839 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25840 hpos = row->used[TEXT_AREA] - 1;
25842 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25843 hl, 0);
25844 w->phys_cursor_on_p = on_p;
25846 if (hl == DRAW_CURSOR)
25847 w->phys_cursor_width = x1 - w->phys_cursor.x;
25848 /* When we erase the cursor, and ROW is overlapped by other
25849 rows, make sure that these overlapping parts of other rows
25850 are redrawn. */
25851 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25853 w->phys_cursor_width = x1 - w->phys_cursor.x;
25855 if (row > w->current_matrix->rows
25856 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25857 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25858 OVERLAPS_ERASED_CURSOR);
25860 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25861 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25862 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25863 OVERLAPS_ERASED_CURSOR);
25869 /* EXPORT:
25870 Erase the image of a cursor of window W from the screen. */
25872 void
25873 erase_phys_cursor (struct window *w)
25875 struct frame *f = XFRAME (w->frame);
25876 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25877 int hpos = w->phys_cursor.hpos;
25878 int vpos = w->phys_cursor.vpos;
25879 int mouse_face_here_p = 0;
25880 struct glyph_matrix *active_glyphs = w->current_matrix;
25881 struct glyph_row *cursor_row;
25882 struct glyph *cursor_glyph;
25883 enum draw_glyphs_face hl;
25885 /* No cursor displayed or row invalidated => nothing to do on the
25886 screen. */
25887 if (w->phys_cursor_type == NO_CURSOR)
25888 goto mark_cursor_off;
25890 /* VPOS >= active_glyphs->nrows means that window has been resized.
25891 Don't bother to erase the cursor. */
25892 if (vpos >= active_glyphs->nrows)
25893 goto mark_cursor_off;
25895 /* If row containing cursor is marked invalid, there is nothing we
25896 can do. */
25897 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25898 if (!cursor_row->enabled_p)
25899 goto mark_cursor_off;
25901 /* If line spacing is > 0, old cursor may only be partially visible in
25902 window after split-window. So adjust visible height. */
25903 cursor_row->visible_height = min (cursor_row->visible_height,
25904 window_text_bottom_y (w) - cursor_row->y);
25906 /* If row is completely invisible, don't attempt to delete a cursor which
25907 isn't there. This can happen if cursor is at top of a window, and
25908 we switch to a buffer with a header line in that window. */
25909 if (cursor_row->visible_height <= 0)
25910 goto mark_cursor_off;
25912 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25913 if (cursor_row->cursor_in_fringe_p)
25915 cursor_row->cursor_in_fringe_p = 0;
25916 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25917 goto mark_cursor_off;
25920 /* This can happen when the new row is shorter than the old one.
25921 In this case, either draw_glyphs or clear_end_of_line
25922 should have cleared the cursor. Note that we wouldn't be
25923 able to erase the cursor in this case because we don't have a
25924 cursor glyph at hand. */
25925 if ((cursor_row->reversed_p
25926 ? (w->phys_cursor.hpos < 0)
25927 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25928 goto mark_cursor_off;
25930 /* When the window is hscrolled, cursor hpos can legitimately be out
25931 of bounds, but we draw the cursor at the corresponding window
25932 margin in that case. */
25933 if (!cursor_row->reversed_p && hpos < 0)
25934 hpos = 0;
25935 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25936 hpos = cursor_row->used[TEXT_AREA] - 1;
25938 /* If the cursor is in the mouse face area, redisplay that when
25939 we clear the cursor. */
25940 if (! NILP (hlinfo->mouse_face_window)
25941 && coords_in_mouse_face_p (w, hpos, vpos)
25942 /* Don't redraw the cursor's spot in mouse face if it is at the
25943 end of a line (on a newline). The cursor appears there, but
25944 mouse highlighting does not. */
25945 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25946 mouse_face_here_p = 1;
25948 /* Maybe clear the display under the cursor. */
25949 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25951 int x, y, left_x;
25952 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25953 int width;
25955 cursor_glyph = get_phys_cursor_glyph (w);
25956 if (cursor_glyph == NULL)
25957 goto mark_cursor_off;
25959 width = cursor_glyph->pixel_width;
25960 left_x = window_box_left_offset (w, TEXT_AREA);
25961 x = w->phys_cursor.x;
25962 if (x < left_x)
25963 width -= left_x - x;
25964 width = min (width, window_box_width (w, TEXT_AREA) - x);
25965 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25966 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25968 if (width > 0)
25969 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25972 /* Erase the cursor by redrawing the character underneath it. */
25973 if (mouse_face_here_p)
25974 hl = DRAW_MOUSE_FACE;
25975 else
25976 hl = DRAW_NORMAL_TEXT;
25977 draw_phys_cursor_glyph (w, cursor_row, hl);
25979 mark_cursor_off:
25980 w->phys_cursor_on_p = 0;
25981 w->phys_cursor_type = NO_CURSOR;
25985 /* EXPORT:
25986 Display or clear cursor of window W. If ON is zero, clear the
25987 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25988 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25990 void
25991 display_and_set_cursor (struct window *w, int on,
25992 int hpos, int vpos, int x, int y)
25994 struct frame *f = XFRAME (w->frame);
25995 int new_cursor_type;
25996 int new_cursor_width;
25997 int active_cursor;
25998 struct glyph_row *glyph_row;
25999 struct glyph *glyph;
26001 /* This is pointless on invisible frames, and dangerous on garbaged
26002 windows and frames; in the latter case, the frame or window may
26003 be in the midst of changing its size, and x and y may be off the
26004 window. */
26005 if (! FRAME_VISIBLE_P (f)
26006 || FRAME_GARBAGED_P (f)
26007 || vpos >= w->current_matrix->nrows
26008 || hpos >= w->current_matrix->matrix_w)
26009 return;
26011 /* If cursor is off and we want it off, return quickly. */
26012 if (!on && !w->phys_cursor_on_p)
26013 return;
26015 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26016 /* If cursor row is not enabled, we don't really know where to
26017 display the cursor. */
26018 if (!glyph_row->enabled_p)
26020 w->phys_cursor_on_p = 0;
26021 return;
26024 glyph = NULL;
26025 if (!glyph_row->exact_window_width_line_p
26026 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26027 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26029 eassert (input_blocked_p ());
26031 /* Set new_cursor_type to the cursor we want to be displayed. */
26032 new_cursor_type = get_window_cursor_type (w, glyph,
26033 &new_cursor_width, &active_cursor);
26035 /* If cursor is currently being shown and we don't want it to be or
26036 it is in the wrong place, or the cursor type is not what we want,
26037 erase it. */
26038 if (w->phys_cursor_on_p
26039 && (!on
26040 || w->phys_cursor.x != x
26041 || w->phys_cursor.y != y
26042 || new_cursor_type != w->phys_cursor_type
26043 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26044 && new_cursor_width != w->phys_cursor_width)))
26045 erase_phys_cursor (w);
26047 /* Don't check phys_cursor_on_p here because that flag is only set
26048 to zero in some cases where we know that the cursor has been
26049 completely erased, to avoid the extra work of erasing the cursor
26050 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26051 still not be visible, or it has only been partly erased. */
26052 if (on)
26054 w->phys_cursor_ascent = glyph_row->ascent;
26055 w->phys_cursor_height = glyph_row->height;
26057 /* Set phys_cursor_.* before x_draw_.* is called because some
26058 of them may need the information. */
26059 w->phys_cursor.x = x;
26060 w->phys_cursor.y = glyph_row->y;
26061 w->phys_cursor.hpos = hpos;
26062 w->phys_cursor.vpos = vpos;
26065 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26066 new_cursor_type, new_cursor_width,
26067 on, active_cursor);
26071 /* Switch the display of W's cursor on or off, according to the value
26072 of ON. */
26074 static void
26075 update_window_cursor (struct window *w, int on)
26077 /* Don't update cursor in windows whose frame is in the process
26078 of being deleted. */
26079 if (w->current_matrix)
26081 int hpos = w->phys_cursor.hpos;
26082 int vpos = w->phys_cursor.vpos;
26083 struct glyph_row *row;
26085 if (vpos >= w->current_matrix->nrows
26086 || hpos >= w->current_matrix->matrix_w)
26087 return;
26089 row = MATRIX_ROW (w->current_matrix, vpos);
26091 /* When the window is hscrolled, cursor hpos can legitimately be
26092 out of bounds, but we draw the cursor at the corresponding
26093 window margin in that case. */
26094 if (!row->reversed_p && hpos < 0)
26095 hpos = 0;
26096 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26097 hpos = row->used[TEXT_AREA] - 1;
26099 block_input ();
26100 display_and_set_cursor (w, on, hpos, vpos,
26101 w->phys_cursor.x, w->phys_cursor.y);
26102 unblock_input ();
26107 /* Call update_window_cursor with parameter ON_P on all leaf windows
26108 in the window tree rooted at W. */
26110 static void
26111 update_cursor_in_window_tree (struct window *w, int on_p)
26113 while (w)
26115 if (WINDOWP (w->contents))
26116 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26117 else
26118 update_window_cursor (w, on_p);
26120 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26125 /* EXPORT:
26126 Display the cursor on window W, or clear it, according to ON_P.
26127 Don't change the cursor's position. */
26129 void
26130 x_update_cursor (struct frame *f, int on_p)
26132 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26136 /* EXPORT:
26137 Clear the cursor of window W to background color, and mark the
26138 cursor as not shown. This is used when the text where the cursor
26139 is about to be rewritten. */
26141 void
26142 x_clear_cursor (struct window *w)
26144 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26145 update_window_cursor (w, 0);
26148 #endif /* HAVE_WINDOW_SYSTEM */
26150 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26151 and MSDOS. */
26152 static void
26153 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26154 int start_hpos, int end_hpos,
26155 enum draw_glyphs_face draw)
26157 #ifdef HAVE_WINDOW_SYSTEM
26158 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26160 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26161 return;
26163 #endif
26164 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26165 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26166 #endif
26169 /* Display the active region described by mouse_face_* according to DRAW. */
26171 static void
26172 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26174 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26175 struct frame *f = XFRAME (WINDOW_FRAME (w));
26177 if (/* If window is in the process of being destroyed, don't bother
26178 to do anything. */
26179 w->current_matrix != NULL
26180 /* Don't update mouse highlight if hidden */
26181 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26182 /* Recognize when we are called to operate on rows that don't exist
26183 anymore. This can happen when a window is split. */
26184 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26186 int phys_cursor_on_p = w->phys_cursor_on_p;
26187 struct glyph_row *row, *first, *last;
26189 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26190 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26192 for (row = first; row <= last && row->enabled_p; ++row)
26194 int start_hpos, end_hpos, start_x;
26196 /* For all but the first row, the highlight starts at column 0. */
26197 if (row == first)
26199 /* R2L rows have BEG and END in reversed order, but the
26200 screen drawing geometry is always left to right. So
26201 we need to mirror the beginning and end of the
26202 highlighted area in R2L rows. */
26203 if (!row->reversed_p)
26205 start_hpos = hlinfo->mouse_face_beg_col;
26206 start_x = hlinfo->mouse_face_beg_x;
26208 else if (row == last)
26210 start_hpos = hlinfo->mouse_face_end_col;
26211 start_x = hlinfo->mouse_face_end_x;
26213 else
26215 start_hpos = 0;
26216 start_x = 0;
26219 else if (row->reversed_p && row == last)
26221 start_hpos = hlinfo->mouse_face_end_col;
26222 start_x = hlinfo->mouse_face_end_x;
26224 else
26226 start_hpos = 0;
26227 start_x = 0;
26230 if (row == last)
26232 if (!row->reversed_p)
26233 end_hpos = hlinfo->mouse_face_end_col;
26234 else if (row == first)
26235 end_hpos = hlinfo->mouse_face_beg_col;
26236 else
26238 end_hpos = row->used[TEXT_AREA];
26239 if (draw == DRAW_NORMAL_TEXT)
26240 row->fill_line_p = 1; /* Clear to end of line */
26243 else if (row->reversed_p && row == first)
26244 end_hpos = hlinfo->mouse_face_beg_col;
26245 else
26247 end_hpos = row->used[TEXT_AREA];
26248 if (draw == DRAW_NORMAL_TEXT)
26249 row->fill_line_p = 1; /* Clear to end of line */
26252 if (end_hpos > start_hpos)
26254 draw_row_with_mouse_face (w, start_x, row,
26255 start_hpos, end_hpos, draw);
26257 row->mouse_face_p
26258 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26262 #ifdef HAVE_WINDOW_SYSTEM
26263 /* When we've written over the cursor, arrange for it to
26264 be displayed again. */
26265 if (FRAME_WINDOW_P (f)
26266 && phys_cursor_on_p && !w->phys_cursor_on_p)
26268 int hpos = w->phys_cursor.hpos;
26270 /* When the window is hscrolled, cursor hpos can legitimately be
26271 out of bounds, but we draw the cursor at the corresponding
26272 window margin in that case. */
26273 if (!row->reversed_p && hpos < 0)
26274 hpos = 0;
26275 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26276 hpos = row->used[TEXT_AREA] - 1;
26278 block_input ();
26279 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26280 w->phys_cursor.x, w->phys_cursor.y);
26281 unblock_input ();
26283 #endif /* HAVE_WINDOW_SYSTEM */
26286 #ifdef HAVE_WINDOW_SYSTEM
26287 /* Change the mouse cursor. */
26288 if (FRAME_WINDOW_P (f))
26290 if (draw == DRAW_NORMAL_TEXT
26291 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26292 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26293 else if (draw == DRAW_MOUSE_FACE)
26294 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26295 else
26296 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26298 #endif /* HAVE_WINDOW_SYSTEM */
26301 /* EXPORT:
26302 Clear out the mouse-highlighted active region.
26303 Redraw it un-highlighted first. Value is non-zero if mouse
26304 face was actually drawn unhighlighted. */
26307 clear_mouse_face (Mouse_HLInfo *hlinfo)
26309 int cleared = 0;
26311 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26313 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26314 cleared = 1;
26317 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26318 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26319 hlinfo->mouse_face_window = Qnil;
26320 hlinfo->mouse_face_overlay = Qnil;
26321 return cleared;
26324 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26325 within the mouse face on that window. */
26326 static int
26327 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26329 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26331 /* Quickly resolve the easy cases. */
26332 if (!(WINDOWP (hlinfo->mouse_face_window)
26333 && XWINDOW (hlinfo->mouse_face_window) == w))
26334 return 0;
26335 if (vpos < hlinfo->mouse_face_beg_row
26336 || vpos > hlinfo->mouse_face_end_row)
26337 return 0;
26338 if (vpos > hlinfo->mouse_face_beg_row
26339 && vpos < hlinfo->mouse_face_end_row)
26340 return 1;
26342 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26344 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26346 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26347 return 1;
26349 else if ((vpos == hlinfo->mouse_face_beg_row
26350 && hpos >= hlinfo->mouse_face_beg_col)
26351 || (vpos == hlinfo->mouse_face_end_row
26352 && hpos < hlinfo->mouse_face_end_col))
26353 return 1;
26355 else
26357 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26359 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26360 return 1;
26362 else if ((vpos == hlinfo->mouse_face_beg_row
26363 && hpos <= hlinfo->mouse_face_beg_col)
26364 || (vpos == hlinfo->mouse_face_end_row
26365 && hpos > hlinfo->mouse_face_end_col))
26366 return 1;
26368 return 0;
26372 /* EXPORT:
26373 Non-zero if physical cursor of window W is within mouse face. */
26376 cursor_in_mouse_face_p (struct window *w)
26378 int hpos = w->phys_cursor.hpos;
26379 int vpos = w->phys_cursor.vpos;
26380 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26382 /* When the window is hscrolled, cursor hpos can legitimately be out
26383 of bounds, but we draw the cursor at the corresponding window
26384 margin in that case. */
26385 if (!row->reversed_p && hpos < 0)
26386 hpos = 0;
26387 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26388 hpos = row->used[TEXT_AREA] - 1;
26390 return coords_in_mouse_face_p (w, hpos, vpos);
26395 /* Find the glyph rows START_ROW and END_ROW of window W that display
26396 characters between buffer positions START_CHARPOS and END_CHARPOS
26397 (excluding END_CHARPOS). DISP_STRING is a display string that
26398 covers these buffer positions. This is similar to
26399 row_containing_pos, but is more accurate when bidi reordering makes
26400 buffer positions change non-linearly with glyph rows. */
26401 static void
26402 rows_from_pos_range (struct window *w,
26403 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26404 Lisp_Object disp_string,
26405 struct glyph_row **start, struct glyph_row **end)
26407 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26408 int last_y = window_text_bottom_y (w);
26409 struct glyph_row *row;
26411 *start = NULL;
26412 *end = NULL;
26414 while (!first->enabled_p
26415 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26416 first++;
26418 /* Find the START row. */
26419 for (row = first;
26420 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26421 row++)
26423 /* A row can potentially be the START row if the range of the
26424 characters it displays intersects the range
26425 [START_CHARPOS..END_CHARPOS). */
26426 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26427 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26428 /* See the commentary in row_containing_pos, for the
26429 explanation of the complicated way to check whether
26430 some position is beyond the end of the characters
26431 displayed by a row. */
26432 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26433 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26434 && !row->ends_at_zv_p
26435 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26436 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26437 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26438 && !row->ends_at_zv_p
26439 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26441 /* Found a candidate row. Now make sure at least one of the
26442 glyphs it displays has a charpos from the range
26443 [START_CHARPOS..END_CHARPOS).
26445 This is not obvious because bidi reordering could make
26446 buffer positions of a row be 1,2,3,102,101,100, and if we
26447 want to highlight characters in [50..60), we don't want
26448 this row, even though [50..60) does intersect [1..103),
26449 the range of character positions given by the row's start
26450 and end positions. */
26451 struct glyph *g = row->glyphs[TEXT_AREA];
26452 struct glyph *e = g + row->used[TEXT_AREA];
26454 while (g < e)
26456 if (((BUFFERP (g->object) || INTEGERP (g->object))
26457 && start_charpos <= g->charpos && g->charpos < end_charpos)
26458 /* A glyph that comes from DISP_STRING is by
26459 definition to be highlighted. */
26460 || EQ (g->object, disp_string))
26461 *start = row;
26462 g++;
26464 if (*start)
26465 break;
26469 /* Find the END row. */
26470 if (!*start
26471 /* If the last row is partially visible, start looking for END
26472 from that row, instead of starting from FIRST. */
26473 && !(row->enabled_p
26474 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26475 row = first;
26476 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26478 struct glyph_row *next = row + 1;
26479 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26481 if (!next->enabled_p
26482 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26483 /* The first row >= START whose range of displayed characters
26484 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26485 is the row END + 1. */
26486 || (start_charpos < next_start
26487 && end_charpos < next_start)
26488 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26489 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26490 && !next->ends_at_zv_p
26491 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26492 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26493 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26494 && !next->ends_at_zv_p
26495 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26497 *end = row;
26498 break;
26500 else
26502 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26503 but none of the characters it displays are in the range, it is
26504 also END + 1. */
26505 struct glyph *g = next->glyphs[TEXT_AREA];
26506 struct glyph *s = g;
26507 struct glyph *e = g + next->used[TEXT_AREA];
26509 while (g < e)
26511 if (((BUFFERP (g->object) || INTEGERP (g->object))
26512 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26513 /* If the buffer position of the first glyph in
26514 the row is equal to END_CHARPOS, it means
26515 the last character to be highlighted is the
26516 newline of ROW, and we must consider NEXT as
26517 END, not END+1. */
26518 || (((!next->reversed_p && g == s)
26519 || (next->reversed_p && g == e - 1))
26520 && (g->charpos == end_charpos
26521 /* Special case for when NEXT is an
26522 empty line at ZV. */
26523 || (g->charpos == -1
26524 && !row->ends_at_zv_p
26525 && next_start == end_charpos)))))
26526 /* A glyph that comes from DISP_STRING is by
26527 definition to be highlighted. */
26528 || EQ (g->object, disp_string))
26529 break;
26530 g++;
26532 if (g == e)
26534 *end = row;
26535 break;
26537 /* The first row that ends at ZV must be the last to be
26538 highlighted. */
26539 else if (next->ends_at_zv_p)
26541 *end = next;
26542 break;
26548 /* This function sets the mouse_face_* elements of HLINFO, assuming
26549 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26550 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26551 for the overlay or run of text properties specifying the mouse
26552 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26553 before-string and after-string that must also be highlighted.
26554 DISP_STRING, if non-nil, is a display string that may cover some
26555 or all of the highlighted text. */
26557 static void
26558 mouse_face_from_buffer_pos (Lisp_Object window,
26559 Mouse_HLInfo *hlinfo,
26560 ptrdiff_t mouse_charpos,
26561 ptrdiff_t start_charpos,
26562 ptrdiff_t end_charpos,
26563 Lisp_Object before_string,
26564 Lisp_Object after_string,
26565 Lisp_Object disp_string)
26567 struct window *w = XWINDOW (window);
26568 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26569 struct glyph_row *r1, *r2;
26570 struct glyph *glyph, *end;
26571 ptrdiff_t ignore, pos;
26572 int x;
26574 eassert (NILP (disp_string) || STRINGP (disp_string));
26575 eassert (NILP (before_string) || STRINGP (before_string));
26576 eassert (NILP (after_string) || STRINGP (after_string));
26578 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26579 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26580 if (r1 == NULL)
26581 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26582 /* If the before-string or display-string contains newlines,
26583 rows_from_pos_range skips to its last row. Move back. */
26584 if (!NILP (before_string) || !NILP (disp_string))
26586 struct glyph_row *prev;
26587 while ((prev = r1 - 1, prev >= first)
26588 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26589 && prev->used[TEXT_AREA] > 0)
26591 struct glyph *beg = prev->glyphs[TEXT_AREA];
26592 glyph = beg + prev->used[TEXT_AREA];
26593 while (--glyph >= beg && INTEGERP (glyph->object));
26594 if (glyph < beg
26595 || !(EQ (glyph->object, before_string)
26596 || EQ (glyph->object, disp_string)))
26597 break;
26598 r1 = prev;
26601 if (r2 == NULL)
26603 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26604 hlinfo->mouse_face_past_end = 1;
26606 else if (!NILP (after_string))
26608 /* If the after-string has newlines, advance to its last row. */
26609 struct glyph_row *next;
26610 struct glyph_row *last
26611 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26613 for (next = r2 + 1;
26614 next <= last
26615 && next->used[TEXT_AREA] > 0
26616 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26617 ++next)
26618 r2 = next;
26620 /* The rest of the display engine assumes that mouse_face_beg_row is
26621 either above mouse_face_end_row or identical to it. But with
26622 bidi-reordered continued lines, the row for START_CHARPOS could
26623 be below the row for END_CHARPOS. If so, swap the rows and store
26624 them in correct order. */
26625 if (r1->y > r2->y)
26627 struct glyph_row *tem = r2;
26629 r2 = r1;
26630 r1 = tem;
26633 hlinfo->mouse_face_beg_y = r1->y;
26634 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26635 hlinfo->mouse_face_end_y = r2->y;
26636 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26638 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26639 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26640 could be anywhere in the row and in any order. The strategy
26641 below is to find the leftmost and the rightmost glyph that
26642 belongs to either of these 3 strings, or whose position is
26643 between START_CHARPOS and END_CHARPOS, and highlight all the
26644 glyphs between those two. This may cover more than just the text
26645 between START_CHARPOS and END_CHARPOS if the range of characters
26646 strides the bidi level boundary, e.g. if the beginning is in R2L
26647 text while the end is in L2R text or vice versa. */
26648 if (!r1->reversed_p)
26650 /* This row is in a left to right paragraph. Scan it left to
26651 right. */
26652 glyph = r1->glyphs[TEXT_AREA];
26653 end = glyph + r1->used[TEXT_AREA];
26654 x = r1->x;
26656 /* Skip truncation glyphs at the start of the glyph row. */
26657 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
26658 for (; glyph < end
26659 && INTEGERP (glyph->object)
26660 && glyph->charpos < 0;
26661 ++glyph)
26662 x += glyph->pixel_width;
26664 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26665 or DISP_STRING, and the first glyph from buffer whose
26666 position is between START_CHARPOS and END_CHARPOS. */
26667 for (; glyph < end
26668 && !INTEGERP (glyph->object)
26669 && !EQ (glyph->object, disp_string)
26670 && !(BUFFERP (glyph->object)
26671 && (glyph->charpos >= start_charpos
26672 && glyph->charpos < end_charpos));
26673 ++glyph)
26675 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26676 are present at buffer positions between START_CHARPOS and
26677 END_CHARPOS, or if they come from an overlay. */
26678 if (EQ (glyph->object, before_string))
26680 pos = string_buffer_position (before_string,
26681 start_charpos);
26682 /* If pos == 0, it means before_string came from an
26683 overlay, not from a buffer position. */
26684 if (!pos || (pos >= start_charpos && pos < end_charpos))
26685 break;
26687 else if (EQ (glyph->object, after_string))
26689 pos = string_buffer_position (after_string, end_charpos);
26690 if (!pos || (pos >= start_charpos && pos < end_charpos))
26691 break;
26693 x += glyph->pixel_width;
26695 hlinfo->mouse_face_beg_x = x;
26696 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26698 else
26700 /* This row is in a right to left paragraph. Scan it right to
26701 left. */
26702 struct glyph *g;
26704 end = r1->glyphs[TEXT_AREA] - 1;
26705 glyph = end + r1->used[TEXT_AREA];
26707 /* Skip truncation glyphs at the start of the glyph row. */
26708 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
26709 for (; glyph > end
26710 && INTEGERP (glyph->object)
26711 && glyph->charpos < 0;
26712 --glyph)
26715 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26716 or DISP_STRING, and the first glyph from buffer whose
26717 position is between START_CHARPOS and END_CHARPOS. */
26718 for (; glyph > end
26719 && !INTEGERP (glyph->object)
26720 && !EQ (glyph->object, disp_string)
26721 && !(BUFFERP (glyph->object)
26722 && (glyph->charpos >= start_charpos
26723 && glyph->charpos < end_charpos));
26724 --glyph)
26726 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26727 are present at buffer positions between START_CHARPOS and
26728 END_CHARPOS, or if they come from an overlay. */
26729 if (EQ (glyph->object, before_string))
26731 pos = string_buffer_position (before_string, start_charpos);
26732 /* If pos == 0, it means before_string came from an
26733 overlay, not from a buffer position. */
26734 if (!pos || (pos >= start_charpos && pos < end_charpos))
26735 break;
26737 else if (EQ (glyph->object, after_string))
26739 pos = string_buffer_position (after_string, end_charpos);
26740 if (!pos || (pos >= start_charpos && pos < end_charpos))
26741 break;
26745 glyph++; /* first glyph to the right of the highlighted area */
26746 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26747 x += g->pixel_width;
26748 hlinfo->mouse_face_beg_x = x;
26749 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26752 /* If the highlight ends in a different row, compute GLYPH and END
26753 for the end row. Otherwise, reuse the values computed above for
26754 the row where the highlight begins. */
26755 if (r2 != r1)
26757 if (!r2->reversed_p)
26759 glyph = r2->glyphs[TEXT_AREA];
26760 end = glyph + r2->used[TEXT_AREA];
26761 x = r2->x;
26763 else
26765 end = r2->glyphs[TEXT_AREA] - 1;
26766 glyph = end + r2->used[TEXT_AREA];
26770 if (!r2->reversed_p)
26772 /* Skip truncation and continuation glyphs near the end of the
26773 row, and also blanks and stretch glyphs inserted by
26774 extend_face_to_end_of_line. */
26775 while (end > glyph
26776 && INTEGERP ((end - 1)->object))
26777 --end;
26778 /* Scan the rest of the glyph row from the end, looking for the
26779 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26780 DISP_STRING, or whose position is between START_CHARPOS
26781 and END_CHARPOS */
26782 for (--end;
26783 end > glyph
26784 && !INTEGERP (end->object)
26785 && !EQ (end->object, disp_string)
26786 && !(BUFFERP (end->object)
26787 && (end->charpos >= start_charpos
26788 && end->charpos < end_charpos));
26789 --end)
26791 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26792 are present at buffer positions between START_CHARPOS and
26793 END_CHARPOS, or if they come from an overlay. */
26794 if (EQ (end->object, before_string))
26796 pos = string_buffer_position (before_string, start_charpos);
26797 if (!pos || (pos >= start_charpos && pos < end_charpos))
26798 break;
26800 else if (EQ (end->object, after_string))
26802 pos = string_buffer_position (after_string, end_charpos);
26803 if (!pos || (pos >= start_charpos && pos < end_charpos))
26804 break;
26807 /* Find the X coordinate of the last glyph to be highlighted. */
26808 for (; glyph <= end; ++glyph)
26809 x += glyph->pixel_width;
26811 hlinfo->mouse_face_end_x = x;
26812 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26814 else
26816 /* Skip truncation and continuation glyphs near the end of the
26817 row, and also blanks and stretch glyphs inserted by
26818 extend_face_to_end_of_line. */
26819 x = r2->x;
26820 end++;
26821 while (end < glyph
26822 && INTEGERP (end->object))
26824 x += end->pixel_width;
26825 ++end;
26827 /* Scan the rest of the glyph row from the end, looking for the
26828 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26829 DISP_STRING, or whose position is between START_CHARPOS
26830 and END_CHARPOS */
26831 for ( ;
26832 end < glyph
26833 && !INTEGERP (end->object)
26834 && !EQ (end->object, disp_string)
26835 && !(BUFFERP (end->object)
26836 && (end->charpos >= start_charpos
26837 && end->charpos < end_charpos));
26838 ++end)
26840 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26841 are present at buffer positions between START_CHARPOS and
26842 END_CHARPOS, or if they come from an overlay. */
26843 if (EQ (end->object, before_string))
26845 pos = string_buffer_position (before_string, start_charpos);
26846 if (!pos || (pos >= start_charpos && pos < end_charpos))
26847 break;
26849 else if (EQ (end->object, after_string))
26851 pos = string_buffer_position (after_string, end_charpos);
26852 if (!pos || (pos >= start_charpos && pos < end_charpos))
26853 break;
26855 x += end->pixel_width;
26857 /* If we exited the above loop because we arrived at the last
26858 glyph of the row, and its buffer position is still not in
26859 range, it means the last character in range is the preceding
26860 newline. Bump the end column and x values to get past the
26861 last glyph. */
26862 if (end == glyph
26863 && BUFFERP (end->object)
26864 && (end->charpos < start_charpos
26865 || end->charpos >= end_charpos))
26867 x += end->pixel_width;
26868 ++end;
26870 hlinfo->mouse_face_end_x = x;
26871 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26874 hlinfo->mouse_face_window = window;
26875 hlinfo->mouse_face_face_id
26876 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26877 mouse_charpos + 1,
26878 !hlinfo->mouse_face_hidden, -1);
26879 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26882 /* The following function is not used anymore (replaced with
26883 mouse_face_from_string_pos), but I leave it here for the time
26884 being, in case someone would. */
26886 #if 0 /* not used */
26888 /* Find the position of the glyph for position POS in OBJECT in
26889 window W's current matrix, and return in *X, *Y the pixel
26890 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26892 RIGHT_P non-zero means return the position of the right edge of the
26893 glyph, RIGHT_P zero means return the left edge position.
26895 If no glyph for POS exists in the matrix, return the position of
26896 the glyph with the next smaller position that is in the matrix, if
26897 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26898 exists in the matrix, return the position of the glyph with the
26899 next larger position in OBJECT.
26901 Value is non-zero if a glyph was found. */
26903 static int
26904 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26905 int *hpos, int *vpos, int *x, int *y, int right_p)
26907 int yb = window_text_bottom_y (w);
26908 struct glyph_row *r;
26909 struct glyph *best_glyph = NULL;
26910 struct glyph_row *best_row = NULL;
26911 int best_x = 0;
26913 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26914 r->enabled_p && r->y < yb;
26915 ++r)
26917 struct glyph *g = r->glyphs[TEXT_AREA];
26918 struct glyph *e = g + r->used[TEXT_AREA];
26919 int gx;
26921 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26922 if (EQ (g->object, object))
26924 if (g->charpos == pos)
26926 best_glyph = g;
26927 best_x = gx;
26928 best_row = r;
26929 goto found;
26931 else if (best_glyph == NULL
26932 || ((eabs (g->charpos - pos)
26933 < eabs (best_glyph->charpos - pos))
26934 && (right_p
26935 ? g->charpos < pos
26936 : g->charpos > pos)))
26938 best_glyph = g;
26939 best_x = gx;
26940 best_row = r;
26945 found:
26947 if (best_glyph)
26949 *x = best_x;
26950 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26952 if (right_p)
26954 *x += best_glyph->pixel_width;
26955 ++*hpos;
26958 *y = best_row->y;
26959 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
26962 return best_glyph != NULL;
26964 #endif /* not used */
26966 /* Find the positions of the first and the last glyphs in window W's
26967 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26968 (assumed to be a string), and return in HLINFO's mouse_face_*
26969 members the pixel and column/row coordinates of those glyphs. */
26971 static void
26972 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26973 Lisp_Object object,
26974 ptrdiff_t startpos, ptrdiff_t endpos)
26976 int yb = window_text_bottom_y (w);
26977 struct glyph_row *r;
26978 struct glyph *g, *e;
26979 int gx;
26980 int found = 0;
26982 /* Find the glyph row with at least one position in the range
26983 [STARTPOS..ENDPOS], and the first glyph in that row whose
26984 position belongs to that range. */
26985 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26986 r->enabled_p && r->y < yb;
26987 ++r)
26989 if (!r->reversed_p)
26991 g = r->glyphs[TEXT_AREA];
26992 e = g + r->used[TEXT_AREA];
26993 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26994 if (EQ (g->object, object)
26995 && startpos <= g->charpos && g->charpos <= endpos)
26997 hlinfo->mouse_face_beg_row
26998 = MATRIX_ROW_VPOS (r, w->current_matrix);
26999 hlinfo->mouse_face_beg_y = r->y;
27000 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27001 hlinfo->mouse_face_beg_x = gx;
27002 found = 1;
27003 break;
27006 else
27008 struct glyph *g1;
27010 e = r->glyphs[TEXT_AREA];
27011 g = e + r->used[TEXT_AREA];
27012 for ( ; g > e; --g)
27013 if (EQ ((g-1)->object, object)
27014 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27016 hlinfo->mouse_face_beg_row
27017 = MATRIX_ROW_VPOS (r, w->current_matrix);
27018 hlinfo->mouse_face_beg_y = r->y;
27019 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27020 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27021 gx += g1->pixel_width;
27022 hlinfo->mouse_face_beg_x = gx;
27023 found = 1;
27024 break;
27027 if (found)
27028 break;
27031 if (!found)
27032 return;
27034 /* Starting with the next row, look for the first row which does NOT
27035 include any glyphs whose positions are in the range. */
27036 for (++r; r->enabled_p && r->y < yb; ++r)
27038 g = r->glyphs[TEXT_AREA];
27039 e = g + r->used[TEXT_AREA];
27040 found = 0;
27041 for ( ; g < e; ++g)
27042 if (EQ (g->object, object)
27043 && startpos <= g->charpos && g->charpos <= endpos)
27045 found = 1;
27046 break;
27048 if (!found)
27049 break;
27052 /* The highlighted region ends on the previous row. */
27053 r--;
27055 /* Set the end row and its vertical pixel coordinate. */
27056 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27057 hlinfo->mouse_face_end_y = r->y;
27059 /* Compute and set the end column and the end column's horizontal
27060 pixel coordinate. */
27061 if (!r->reversed_p)
27063 g = r->glyphs[TEXT_AREA];
27064 e = g + r->used[TEXT_AREA];
27065 for ( ; e > g; --e)
27066 if (EQ ((e-1)->object, object)
27067 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27068 break;
27069 hlinfo->mouse_face_end_col = e - g;
27071 for (gx = r->x; g < e; ++g)
27072 gx += g->pixel_width;
27073 hlinfo->mouse_face_end_x = gx;
27075 else
27077 e = r->glyphs[TEXT_AREA];
27078 g = e + r->used[TEXT_AREA];
27079 for (gx = r->x ; e < g; ++e)
27081 if (EQ (e->object, object)
27082 && startpos <= e->charpos && e->charpos <= endpos)
27083 break;
27084 gx += e->pixel_width;
27086 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27087 hlinfo->mouse_face_end_x = gx;
27091 #ifdef HAVE_WINDOW_SYSTEM
27093 /* See if position X, Y is within a hot-spot of an image. */
27095 static int
27096 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27098 if (!CONSP (hot_spot))
27099 return 0;
27101 if (EQ (XCAR (hot_spot), Qrect))
27103 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27104 Lisp_Object rect = XCDR (hot_spot);
27105 Lisp_Object tem;
27106 if (!CONSP (rect))
27107 return 0;
27108 if (!CONSP (XCAR (rect)))
27109 return 0;
27110 if (!CONSP (XCDR (rect)))
27111 return 0;
27112 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27113 return 0;
27114 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27115 return 0;
27116 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27117 return 0;
27118 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27119 return 0;
27120 return 1;
27122 else if (EQ (XCAR (hot_spot), Qcircle))
27124 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27125 Lisp_Object circ = XCDR (hot_spot);
27126 Lisp_Object lr, lx0, ly0;
27127 if (CONSP (circ)
27128 && CONSP (XCAR (circ))
27129 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27130 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27131 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27133 double r = XFLOATINT (lr);
27134 double dx = XINT (lx0) - x;
27135 double dy = XINT (ly0) - y;
27136 return (dx * dx + dy * dy <= r * r);
27139 else if (EQ (XCAR (hot_spot), Qpoly))
27141 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27142 if (VECTORP (XCDR (hot_spot)))
27144 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27145 Lisp_Object *poly = v->contents;
27146 ptrdiff_t n = v->header.size;
27147 ptrdiff_t i;
27148 int inside = 0;
27149 Lisp_Object lx, ly;
27150 int x0, y0;
27152 /* Need an even number of coordinates, and at least 3 edges. */
27153 if (n < 6 || n & 1)
27154 return 0;
27156 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27157 If count is odd, we are inside polygon. Pixels on edges
27158 may or may not be included depending on actual geometry of the
27159 polygon. */
27160 if ((lx = poly[n-2], !INTEGERP (lx))
27161 || (ly = poly[n-1], !INTEGERP (lx)))
27162 return 0;
27163 x0 = XINT (lx), y0 = XINT (ly);
27164 for (i = 0; i < n; i += 2)
27166 int x1 = x0, y1 = y0;
27167 if ((lx = poly[i], !INTEGERP (lx))
27168 || (ly = poly[i+1], !INTEGERP (ly)))
27169 return 0;
27170 x0 = XINT (lx), y0 = XINT (ly);
27172 /* Does this segment cross the X line? */
27173 if (x0 >= x)
27175 if (x1 >= x)
27176 continue;
27178 else if (x1 < x)
27179 continue;
27180 if (y > y0 && y > y1)
27181 continue;
27182 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27183 inside = !inside;
27185 return inside;
27188 return 0;
27191 Lisp_Object
27192 find_hot_spot (Lisp_Object map, int x, int y)
27194 while (CONSP (map))
27196 if (CONSP (XCAR (map))
27197 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27198 return XCAR (map);
27199 map = XCDR (map);
27202 return Qnil;
27205 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27206 3, 3, 0,
27207 doc: /* Lookup in image map MAP coordinates X and Y.
27208 An image map is an alist where each element has the format (AREA ID PLIST).
27209 An AREA is specified as either a rectangle, a circle, or a polygon:
27210 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27211 pixel coordinates of the upper left and bottom right corners.
27212 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27213 and the radius of the circle; r may be a float or integer.
27214 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27215 vector describes one corner in the polygon.
27216 Returns the alist element for the first matching AREA in MAP. */)
27217 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27219 if (NILP (map))
27220 return Qnil;
27222 CHECK_NUMBER (x);
27223 CHECK_NUMBER (y);
27225 return find_hot_spot (map,
27226 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27227 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27231 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27232 static void
27233 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27235 /* Do not change cursor shape while dragging mouse. */
27236 if (!NILP (do_mouse_tracking))
27237 return;
27239 if (!NILP (pointer))
27241 if (EQ (pointer, Qarrow))
27242 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27243 else if (EQ (pointer, Qhand))
27244 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27245 else if (EQ (pointer, Qtext))
27246 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27247 else if (EQ (pointer, intern ("hdrag")))
27248 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27249 #ifdef HAVE_X_WINDOWS
27250 else if (EQ (pointer, intern ("vdrag")))
27251 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27252 #endif
27253 else if (EQ (pointer, intern ("hourglass")))
27254 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27255 else if (EQ (pointer, Qmodeline))
27256 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27257 else
27258 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27261 if (cursor != No_Cursor)
27262 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27265 #endif /* HAVE_WINDOW_SYSTEM */
27267 /* Take proper action when mouse has moved to the mode or header line
27268 or marginal area AREA of window W, x-position X and y-position Y.
27269 X is relative to the start of the text display area of W, so the
27270 width of bitmap areas and scroll bars must be subtracted to get a
27271 position relative to the start of the mode line. */
27273 static void
27274 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27275 enum window_part area)
27277 struct window *w = XWINDOW (window);
27278 struct frame *f = XFRAME (w->frame);
27279 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27280 #ifdef HAVE_WINDOW_SYSTEM
27281 Display_Info *dpyinfo;
27282 #endif
27283 Cursor cursor = No_Cursor;
27284 Lisp_Object pointer = Qnil;
27285 int dx, dy, width, height;
27286 ptrdiff_t charpos;
27287 Lisp_Object string, object = Qnil;
27288 Lisp_Object pos IF_LINT (= Qnil), help;
27290 Lisp_Object mouse_face;
27291 int original_x_pixel = x;
27292 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27293 struct glyph_row *row IF_LINT (= 0);
27295 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27297 int x0;
27298 struct glyph *end;
27300 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27301 returns them in row/column units! */
27302 string = mode_line_string (w, area, &x, &y, &charpos,
27303 &object, &dx, &dy, &width, &height);
27305 row = (area == ON_MODE_LINE
27306 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27307 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27309 /* Find the glyph under the mouse pointer. */
27310 if (row->mode_line_p && row->enabled_p)
27312 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27313 end = glyph + row->used[TEXT_AREA];
27315 for (x0 = original_x_pixel;
27316 glyph < end && x0 >= glyph->pixel_width;
27317 ++glyph)
27318 x0 -= glyph->pixel_width;
27320 if (glyph >= end)
27321 glyph = NULL;
27324 else
27326 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27327 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27328 returns them in row/column units! */
27329 string = marginal_area_string (w, area, &x, &y, &charpos,
27330 &object, &dx, &dy, &width, &height);
27333 help = Qnil;
27335 #ifdef HAVE_WINDOW_SYSTEM
27336 if (IMAGEP (object))
27338 Lisp_Object image_map, hotspot;
27339 if ((image_map = Fplist_get (XCDR (object), QCmap),
27340 !NILP (image_map))
27341 && (hotspot = find_hot_spot (image_map, dx, dy),
27342 CONSP (hotspot))
27343 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27345 Lisp_Object plist;
27347 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27348 If so, we could look for mouse-enter, mouse-leave
27349 properties in PLIST (and do something...). */
27350 hotspot = XCDR (hotspot);
27351 if (CONSP (hotspot)
27352 && (plist = XCAR (hotspot), CONSP (plist)))
27354 pointer = Fplist_get (plist, Qpointer);
27355 if (NILP (pointer))
27356 pointer = Qhand;
27357 help = Fplist_get (plist, Qhelp_echo);
27358 if (!NILP (help))
27360 help_echo_string = help;
27361 XSETWINDOW (help_echo_window, w);
27362 help_echo_object = w->contents;
27363 help_echo_pos = charpos;
27367 if (NILP (pointer))
27368 pointer = Fplist_get (XCDR (object), QCpointer);
27370 #endif /* HAVE_WINDOW_SYSTEM */
27372 if (STRINGP (string))
27373 pos = make_number (charpos);
27375 /* Set the help text and mouse pointer. If the mouse is on a part
27376 of the mode line without any text (e.g. past the right edge of
27377 the mode line text), use the default help text and pointer. */
27378 if (STRINGP (string) || area == ON_MODE_LINE)
27380 /* Arrange to display the help by setting the global variables
27381 help_echo_string, help_echo_object, and help_echo_pos. */
27382 if (NILP (help))
27384 if (STRINGP (string))
27385 help = Fget_text_property (pos, Qhelp_echo, string);
27387 if (!NILP (help))
27389 help_echo_string = help;
27390 XSETWINDOW (help_echo_window, w);
27391 help_echo_object = string;
27392 help_echo_pos = charpos;
27394 else if (area == ON_MODE_LINE)
27396 Lisp_Object default_help
27397 = buffer_local_value_1 (Qmode_line_default_help_echo,
27398 w->contents);
27400 if (STRINGP (default_help))
27402 help_echo_string = default_help;
27403 XSETWINDOW (help_echo_window, w);
27404 help_echo_object = Qnil;
27405 help_echo_pos = -1;
27410 #ifdef HAVE_WINDOW_SYSTEM
27411 /* Change the mouse pointer according to what is under it. */
27412 if (FRAME_WINDOW_P (f))
27414 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27415 if (STRINGP (string))
27417 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27419 if (NILP (pointer))
27420 pointer = Fget_text_property (pos, Qpointer, string);
27422 /* Change the mouse pointer according to what is under X/Y. */
27423 if (NILP (pointer)
27424 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27426 Lisp_Object map;
27427 map = Fget_text_property (pos, Qlocal_map, string);
27428 if (!KEYMAPP (map))
27429 map = Fget_text_property (pos, Qkeymap, string);
27430 if (!KEYMAPP (map))
27431 cursor = dpyinfo->vertical_scroll_bar_cursor;
27434 else
27435 /* Default mode-line pointer. */
27436 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27438 #endif
27441 /* Change the mouse face according to what is under X/Y. */
27442 if (STRINGP (string))
27444 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27445 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27446 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27447 && glyph)
27449 Lisp_Object b, e;
27451 struct glyph * tmp_glyph;
27453 int gpos;
27454 int gseq_length;
27455 int total_pixel_width;
27456 ptrdiff_t begpos, endpos, ignore;
27458 int vpos, hpos;
27460 b = Fprevious_single_property_change (make_number (charpos + 1),
27461 Qmouse_face, string, Qnil);
27462 if (NILP (b))
27463 begpos = 0;
27464 else
27465 begpos = XINT (b);
27467 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27468 if (NILP (e))
27469 endpos = SCHARS (string);
27470 else
27471 endpos = XINT (e);
27473 /* Calculate the glyph position GPOS of GLYPH in the
27474 displayed string, relative to the beginning of the
27475 highlighted part of the string.
27477 Note: GPOS is different from CHARPOS. CHARPOS is the
27478 position of GLYPH in the internal string object. A mode
27479 line string format has structures which are converted to
27480 a flattened string by the Emacs Lisp interpreter. The
27481 internal string is an element of those structures. The
27482 displayed string is the flattened string. */
27483 tmp_glyph = row_start_glyph;
27484 while (tmp_glyph < glyph
27485 && (!(EQ (tmp_glyph->object, glyph->object)
27486 && begpos <= tmp_glyph->charpos
27487 && tmp_glyph->charpos < endpos)))
27488 tmp_glyph++;
27489 gpos = glyph - tmp_glyph;
27491 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27492 the highlighted part of the displayed string to which
27493 GLYPH belongs. Note: GSEQ_LENGTH is different from
27494 SCHARS (STRING), because the latter returns the length of
27495 the internal string. */
27496 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27497 tmp_glyph > glyph
27498 && (!(EQ (tmp_glyph->object, glyph->object)
27499 && begpos <= tmp_glyph->charpos
27500 && tmp_glyph->charpos < endpos));
27501 tmp_glyph--)
27503 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27505 /* Calculate the total pixel width of all the glyphs between
27506 the beginning of the highlighted area and GLYPH. */
27507 total_pixel_width = 0;
27508 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27509 total_pixel_width += tmp_glyph->pixel_width;
27511 /* Pre calculation of re-rendering position. Note: X is in
27512 column units here, after the call to mode_line_string or
27513 marginal_area_string. */
27514 hpos = x - gpos;
27515 vpos = (area == ON_MODE_LINE
27516 ? (w->current_matrix)->nrows - 1
27517 : 0);
27519 /* If GLYPH's position is included in the region that is
27520 already drawn in mouse face, we have nothing to do. */
27521 if ( EQ (window, hlinfo->mouse_face_window)
27522 && (!row->reversed_p
27523 ? (hlinfo->mouse_face_beg_col <= hpos
27524 && hpos < hlinfo->mouse_face_end_col)
27525 /* In R2L rows we swap BEG and END, see below. */
27526 : (hlinfo->mouse_face_end_col <= hpos
27527 && hpos < hlinfo->mouse_face_beg_col))
27528 && hlinfo->mouse_face_beg_row == vpos )
27529 return;
27531 if (clear_mouse_face (hlinfo))
27532 cursor = No_Cursor;
27534 if (!row->reversed_p)
27536 hlinfo->mouse_face_beg_col = hpos;
27537 hlinfo->mouse_face_beg_x = original_x_pixel
27538 - (total_pixel_width + dx);
27539 hlinfo->mouse_face_end_col = hpos + gseq_length;
27540 hlinfo->mouse_face_end_x = 0;
27542 else
27544 /* In R2L rows, show_mouse_face expects BEG and END
27545 coordinates to be swapped. */
27546 hlinfo->mouse_face_end_col = hpos;
27547 hlinfo->mouse_face_end_x = original_x_pixel
27548 - (total_pixel_width + dx);
27549 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27550 hlinfo->mouse_face_beg_x = 0;
27553 hlinfo->mouse_face_beg_row = vpos;
27554 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27555 hlinfo->mouse_face_beg_y = 0;
27556 hlinfo->mouse_face_end_y = 0;
27557 hlinfo->mouse_face_past_end = 0;
27558 hlinfo->mouse_face_window = window;
27560 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27561 charpos,
27562 0, 0, 0,
27563 &ignore,
27564 glyph->face_id,
27566 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27568 if (NILP (pointer))
27569 pointer = Qhand;
27571 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27572 clear_mouse_face (hlinfo);
27574 #ifdef HAVE_WINDOW_SYSTEM
27575 if (FRAME_WINDOW_P (f))
27576 define_frame_cursor1 (f, cursor, pointer);
27577 #endif
27581 /* EXPORT:
27582 Take proper action when the mouse has moved to position X, Y on
27583 frame F with regards to highlighting portions of display that have
27584 mouse-face properties. Also de-highlight portions of display where
27585 the mouse was before, set the mouse pointer shape as appropriate
27586 for the mouse coordinates, and activate help echo (tooltips).
27587 X and Y can be negative or out of range. */
27589 void
27590 note_mouse_highlight (struct frame *f, int x, int y)
27592 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27593 enum window_part part = ON_NOTHING;
27594 Lisp_Object window;
27595 struct window *w;
27596 Cursor cursor = No_Cursor;
27597 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27598 struct buffer *b;
27600 /* When a menu is active, don't highlight because this looks odd. */
27601 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27602 if (popup_activated ())
27603 return;
27604 #endif
27606 if (!f->glyphs_initialized_p
27607 || f->pointer_invisible)
27608 return;
27610 hlinfo->mouse_face_mouse_x = x;
27611 hlinfo->mouse_face_mouse_y = y;
27612 hlinfo->mouse_face_mouse_frame = f;
27614 if (hlinfo->mouse_face_defer)
27615 return;
27617 /* Which window is that in? */
27618 window = window_from_coordinates (f, x, y, &part, 1);
27620 /* If displaying active text in another window, clear that. */
27621 if (! EQ (window, hlinfo->mouse_face_window)
27622 /* Also clear if we move out of text area in same window. */
27623 || (!NILP (hlinfo->mouse_face_window)
27624 && !NILP (window)
27625 && part != ON_TEXT
27626 && part != ON_MODE_LINE
27627 && part != ON_HEADER_LINE))
27628 clear_mouse_face (hlinfo);
27630 /* Not on a window -> return. */
27631 if (!WINDOWP (window))
27632 return;
27634 /* Reset help_echo_string. It will get recomputed below. */
27635 help_echo_string = Qnil;
27637 /* Convert to window-relative pixel coordinates. */
27638 w = XWINDOW (window);
27639 frame_to_window_pixel_xy (w, &x, &y);
27641 #ifdef HAVE_WINDOW_SYSTEM
27642 /* Handle tool-bar window differently since it doesn't display a
27643 buffer. */
27644 if (EQ (window, f->tool_bar_window))
27646 note_tool_bar_highlight (f, x, y);
27647 return;
27649 #endif
27651 /* Mouse is on the mode, header line or margin? */
27652 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27653 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27655 note_mode_line_or_margin_highlight (window, x, y, part);
27656 return;
27659 #ifdef HAVE_WINDOW_SYSTEM
27660 if (part == ON_VERTICAL_BORDER)
27662 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27663 help_echo_string = build_string ("drag-mouse-1: resize");
27665 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27666 || part == ON_SCROLL_BAR)
27667 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27668 else
27669 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27670 #endif
27672 /* Are we in a window whose display is up to date?
27673 And verify the buffer's text has not changed. */
27674 b = XBUFFER (w->contents);
27675 if (part == ON_TEXT
27676 && w->window_end_valid
27677 && w->last_modified == BUF_MODIFF (b)
27678 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27680 int hpos, vpos, dx, dy, area = LAST_AREA;
27681 ptrdiff_t pos;
27682 struct glyph *glyph;
27683 Lisp_Object object;
27684 Lisp_Object mouse_face = Qnil, position;
27685 Lisp_Object *overlay_vec = NULL;
27686 ptrdiff_t i, noverlays;
27687 struct buffer *obuf;
27688 ptrdiff_t obegv, ozv;
27689 int same_region;
27691 /* Find the glyph under X/Y. */
27692 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27694 #ifdef HAVE_WINDOW_SYSTEM
27695 /* Look for :pointer property on image. */
27696 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27698 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27699 if (img != NULL && IMAGEP (img->spec))
27701 Lisp_Object image_map, hotspot;
27702 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27703 !NILP (image_map))
27704 && (hotspot = find_hot_spot (image_map,
27705 glyph->slice.img.x + dx,
27706 glyph->slice.img.y + dy),
27707 CONSP (hotspot))
27708 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27710 Lisp_Object plist;
27712 /* Could check XCAR (hotspot) to see if we enter/leave
27713 this hot-spot.
27714 If so, we could look for mouse-enter, mouse-leave
27715 properties in PLIST (and do something...). */
27716 hotspot = XCDR (hotspot);
27717 if (CONSP (hotspot)
27718 && (plist = XCAR (hotspot), CONSP (plist)))
27720 pointer = Fplist_get (plist, Qpointer);
27721 if (NILP (pointer))
27722 pointer = Qhand;
27723 help_echo_string = Fplist_get (plist, Qhelp_echo);
27724 if (!NILP (help_echo_string))
27726 help_echo_window = window;
27727 help_echo_object = glyph->object;
27728 help_echo_pos = glyph->charpos;
27732 if (NILP (pointer))
27733 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27736 #endif /* HAVE_WINDOW_SYSTEM */
27738 /* Clear mouse face if X/Y not over text. */
27739 if (glyph == NULL
27740 || area != TEXT_AREA
27741 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
27742 /* Glyph's OBJECT is an integer for glyphs inserted by the
27743 display engine for its internal purposes, like truncation
27744 and continuation glyphs and blanks beyond the end of
27745 line's text on text terminals. If we are over such a
27746 glyph, we are not over any text. */
27747 || INTEGERP (glyph->object)
27748 /* R2L rows have a stretch glyph at their front, which
27749 stands for no text, whereas L2R rows have no glyphs at
27750 all beyond the end of text. Treat such stretch glyphs
27751 like we do with NULL glyphs in L2R rows. */
27752 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27753 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
27754 && glyph->type == STRETCH_GLYPH
27755 && glyph->avoid_cursor_p))
27757 if (clear_mouse_face (hlinfo))
27758 cursor = No_Cursor;
27759 #ifdef HAVE_WINDOW_SYSTEM
27760 if (FRAME_WINDOW_P (f) && NILP (pointer))
27762 if (area != TEXT_AREA)
27763 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27764 else
27765 pointer = Vvoid_text_area_pointer;
27767 #endif
27768 goto set_cursor;
27771 pos = glyph->charpos;
27772 object = glyph->object;
27773 if (!STRINGP (object) && !BUFFERP (object))
27774 goto set_cursor;
27776 /* If we get an out-of-range value, return now; avoid an error. */
27777 if (BUFFERP (object) && pos > BUF_Z (b))
27778 goto set_cursor;
27780 /* Make the window's buffer temporarily current for
27781 overlays_at and compute_char_face. */
27782 obuf = current_buffer;
27783 current_buffer = b;
27784 obegv = BEGV;
27785 ozv = ZV;
27786 BEGV = BEG;
27787 ZV = Z;
27789 /* Is this char mouse-active or does it have help-echo? */
27790 position = make_number (pos);
27792 if (BUFFERP (object))
27794 /* Put all the overlays we want in a vector in overlay_vec. */
27795 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27796 /* Sort overlays into increasing priority order. */
27797 noverlays = sort_overlays (overlay_vec, noverlays, w);
27799 else
27800 noverlays = 0;
27802 if (NILP (Vmouse_highlight))
27804 clear_mouse_face (hlinfo);
27805 goto check_help_echo;
27808 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27810 if (same_region)
27811 cursor = No_Cursor;
27813 /* Check mouse-face highlighting. */
27814 if (! same_region
27815 /* If there exists an overlay with mouse-face overlapping
27816 the one we are currently highlighting, we have to
27817 check if we enter the overlapping overlay, and then
27818 highlight only that. */
27819 || (OVERLAYP (hlinfo->mouse_face_overlay)
27820 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27822 /* Find the highest priority overlay with a mouse-face. */
27823 Lisp_Object overlay = Qnil;
27824 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27826 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27827 if (!NILP (mouse_face))
27828 overlay = overlay_vec[i];
27831 /* If we're highlighting the same overlay as before, there's
27832 no need to do that again. */
27833 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27834 goto check_help_echo;
27835 hlinfo->mouse_face_overlay = overlay;
27837 /* Clear the display of the old active region, if any. */
27838 if (clear_mouse_face (hlinfo))
27839 cursor = No_Cursor;
27841 /* If no overlay applies, get a text property. */
27842 if (NILP (overlay))
27843 mouse_face = Fget_text_property (position, Qmouse_face, object);
27845 /* Next, compute the bounds of the mouse highlighting and
27846 display it. */
27847 if (!NILP (mouse_face) && STRINGP (object))
27849 /* The mouse-highlighting comes from a display string
27850 with a mouse-face. */
27851 Lisp_Object s, e;
27852 ptrdiff_t ignore;
27854 s = Fprevious_single_property_change
27855 (make_number (pos + 1), Qmouse_face, object, Qnil);
27856 e = Fnext_single_property_change
27857 (position, Qmouse_face, object, Qnil);
27858 if (NILP (s))
27859 s = make_number (0);
27860 if (NILP (e))
27861 e = make_number (SCHARS (object) - 1);
27862 mouse_face_from_string_pos (w, hlinfo, object,
27863 XINT (s), XINT (e));
27864 hlinfo->mouse_face_past_end = 0;
27865 hlinfo->mouse_face_window = window;
27866 hlinfo->mouse_face_face_id
27867 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27868 glyph->face_id, 1);
27869 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27870 cursor = No_Cursor;
27872 else
27874 /* The mouse-highlighting, if any, comes from an overlay
27875 or text property in the buffer. */
27876 Lisp_Object buffer IF_LINT (= Qnil);
27877 Lisp_Object disp_string IF_LINT (= Qnil);
27879 if (STRINGP (object))
27881 /* If we are on a display string with no mouse-face,
27882 check if the text under it has one. */
27883 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27884 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27885 pos = string_buffer_position (object, start);
27886 if (pos > 0)
27888 mouse_face = get_char_property_and_overlay
27889 (make_number (pos), Qmouse_face, w->contents, &overlay);
27890 buffer = w->contents;
27891 disp_string = object;
27894 else
27896 buffer = object;
27897 disp_string = Qnil;
27900 if (!NILP (mouse_face))
27902 Lisp_Object before, after;
27903 Lisp_Object before_string, after_string;
27904 /* To correctly find the limits of mouse highlight
27905 in a bidi-reordered buffer, we must not use the
27906 optimization of limiting the search in
27907 previous-single-property-change and
27908 next-single-property-change, because
27909 rows_from_pos_range needs the real start and end
27910 positions to DTRT in this case. That's because
27911 the first row visible in a window does not
27912 necessarily display the character whose position
27913 is the smallest. */
27914 Lisp_Object lim1 =
27915 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27916 ? Fmarker_position (w->start)
27917 : Qnil;
27918 Lisp_Object lim2 =
27919 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27920 ? make_number (BUF_Z (XBUFFER (buffer))
27921 - XFASTINT (w->window_end_pos))
27922 : Qnil;
27924 if (NILP (overlay))
27926 /* Handle the text property case. */
27927 before = Fprevious_single_property_change
27928 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27929 after = Fnext_single_property_change
27930 (make_number (pos), Qmouse_face, buffer, lim2);
27931 before_string = after_string = Qnil;
27933 else
27935 /* Handle the overlay case. */
27936 before = Foverlay_start (overlay);
27937 after = Foverlay_end (overlay);
27938 before_string = Foverlay_get (overlay, Qbefore_string);
27939 after_string = Foverlay_get (overlay, Qafter_string);
27941 if (!STRINGP (before_string)) before_string = Qnil;
27942 if (!STRINGP (after_string)) after_string = Qnil;
27945 mouse_face_from_buffer_pos (window, hlinfo, pos,
27946 NILP (before)
27948 : XFASTINT (before),
27949 NILP (after)
27950 ? BUF_Z (XBUFFER (buffer))
27951 : XFASTINT (after),
27952 before_string, after_string,
27953 disp_string);
27954 cursor = No_Cursor;
27959 check_help_echo:
27961 /* Look for a `help-echo' property. */
27962 if (NILP (help_echo_string)) {
27963 Lisp_Object help, overlay;
27965 /* Check overlays first. */
27966 help = overlay = Qnil;
27967 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27969 overlay = overlay_vec[i];
27970 help = Foverlay_get (overlay, Qhelp_echo);
27973 if (!NILP (help))
27975 help_echo_string = help;
27976 help_echo_window = window;
27977 help_echo_object = overlay;
27978 help_echo_pos = pos;
27980 else
27982 Lisp_Object obj = glyph->object;
27983 ptrdiff_t charpos = glyph->charpos;
27985 /* Try text properties. */
27986 if (STRINGP (obj)
27987 && charpos >= 0
27988 && charpos < SCHARS (obj))
27990 help = Fget_text_property (make_number (charpos),
27991 Qhelp_echo, obj);
27992 if (NILP (help))
27994 /* If the string itself doesn't specify a help-echo,
27995 see if the buffer text ``under'' it does. */
27996 struct glyph_row *r
27997 = MATRIX_ROW (w->current_matrix, vpos);
27998 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27999 ptrdiff_t p = string_buffer_position (obj, start);
28000 if (p > 0)
28002 help = Fget_char_property (make_number (p),
28003 Qhelp_echo, w->contents);
28004 if (!NILP (help))
28006 charpos = p;
28007 obj = w->contents;
28012 else if (BUFFERP (obj)
28013 && charpos >= BEGV
28014 && charpos < ZV)
28015 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28016 obj);
28018 if (!NILP (help))
28020 help_echo_string = help;
28021 help_echo_window = window;
28022 help_echo_object = obj;
28023 help_echo_pos = charpos;
28028 #ifdef HAVE_WINDOW_SYSTEM
28029 /* Look for a `pointer' property. */
28030 if (FRAME_WINDOW_P (f) && NILP (pointer))
28032 /* Check overlays first. */
28033 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28034 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28036 if (NILP (pointer))
28038 Lisp_Object obj = glyph->object;
28039 ptrdiff_t charpos = glyph->charpos;
28041 /* Try text properties. */
28042 if (STRINGP (obj)
28043 && charpos >= 0
28044 && charpos < SCHARS (obj))
28046 pointer = Fget_text_property (make_number (charpos),
28047 Qpointer, obj);
28048 if (NILP (pointer))
28050 /* If the string itself doesn't specify a pointer,
28051 see if the buffer text ``under'' it does. */
28052 struct glyph_row *r
28053 = MATRIX_ROW (w->current_matrix, vpos);
28054 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28055 ptrdiff_t p = string_buffer_position (obj, start);
28056 if (p > 0)
28057 pointer = Fget_char_property (make_number (p),
28058 Qpointer, w->contents);
28061 else if (BUFFERP (obj)
28062 && charpos >= BEGV
28063 && charpos < ZV)
28064 pointer = Fget_text_property (make_number (charpos),
28065 Qpointer, obj);
28068 #endif /* HAVE_WINDOW_SYSTEM */
28070 BEGV = obegv;
28071 ZV = ozv;
28072 current_buffer = obuf;
28075 set_cursor:
28077 #ifdef HAVE_WINDOW_SYSTEM
28078 if (FRAME_WINDOW_P (f))
28079 define_frame_cursor1 (f, cursor, pointer);
28080 #else
28081 /* This is here to prevent a compiler error, about "label at end of
28082 compound statement". */
28083 return;
28084 #endif
28088 /* EXPORT for RIF:
28089 Clear any mouse-face on window W. This function is part of the
28090 redisplay interface, and is called from try_window_id and similar
28091 functions to ensure the mouse-highlight is off. */
28093 void
28094 x_clear_window_mouse_face (struct window *w)
28096 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28097 Lisp_Object window;
28099 block_input ();
28100 XSETWINDOW (window, w);
28101 if (EQ (window, hlinfo->mouse_face_window))
28102 clear_mouse_face (hlinfo);
28103 unblock_input ();
28107 /* EXPORT:
28108 Just discard the mouse face information for frame F, if any.
28109 This is used when the size of F is changed. */
28111 void
28112 cancel_mouse_face (struct frame *f)
28114 Lisp_Object window;
28115 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28117 window = hlinfo->mouse_face_window;
28118 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28120 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28121 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28122 hlinfo->mouse_face_window = Qnil;
28128 /***********************************************************************
28129 Exposure Events
28130 ***********************************************************************/
28132 #ifdef HAVE_WINDOW_SYSTEM
28134 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28135 which intersects rectangle R. R is in window-relative coordinates. */
28137 static void
28138 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28139 enum glyph_row_area area)
28141 struct glyph *first = row->glyphs[area];
28142 struct glyph *end = row->glyphs[area] + row->used[area];
28143 struct glyph *last;
28144 int first_x, start_x, x;
28146 if (area == TEXT_AREA && row->fill_line_p)
28147 /* If row extends face to end of line write the whole line. */
28148 draw_glyphs (w, 0, row, area,
28149 0, row->used[area],
28150 DRAW_NORMAL_TEXT, 0);
28151 else
28153 /* Set START_X to the window-relative start position for drawing glyphs of
28154 AREA. The first glyph of the text area can be partially visible.
28155 The first glyphs of other areas cannot. */
28156 start_x = window_box_left_offset (w, area);
28157 x = start_x;
28158 if (area == TEXT_AREA)
28159 x += row->x;
28161 /* Find the first glyph that must be redrawn. */
28162 while (first < end
28163 && x + first->pixel_width < r->x)
28165 x += first->pixel_width;
28166 ++first;
28169 /* Find the last one. */
28170 last = first;
28171 first_x = x;
28172 while (last < end
28173 && x < r->x + r->width)
28175 x += last->pixel_width;
28176 ++last;
28179 /* Repaint. */
28180 if (last > first)
28181 draw_glyphs (w, first_x - start_x, row, area,
28182 first - row->glyphs[area], last - row->glyphs[area],
28183 DRAW_NORMAL_TEXT, 0);
28188 /* Redraw the parts of the glyph row ROW on window W intersecting
28189 rectangle R. R is in window-relative coordinates. Value is
28190 non-zero if mouse-face was overwritten. */
28192 static int
28193 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28195 eassert (row->enabled_p);
28197 if (row->mode_line_p || w->pseudo_window_p)
28198 draw_glyphs (w, 0, row, TEXT_AREA,
28199 0, row->used[TEXT_AREA],
28200 DRAW_NORMAL_TEXT, 0);
28201 else
28203 if (row->used[LEFT_MARGIN_AREA])
28204 expose_area (w, row, r, LEFT_MARGIN_AREA);
28205 if (row->used[TEXT_AREA])
28206 expose_area (w, row, r, TEXT_AREA);
28207 if (row->used[RIGHT_MARGIN_AREA])
28208 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28209 draw_row_fringe_bitmaps (w, row);
28212 return row->mouse_face_p;
28216 /* Redraw those parts of glyphs rows during expose event handling that
28217 overlap other rows. Redrawing of an exposed line writes over parts
28218 of lines overlapping that exposed line; this function fixes that.
28220 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28221 row in W's current matrix that is exposed and overlaps other rows.
28222 LAST_OVERLAPPING_ROW is the last such row. */
28224 static void
28225 expose_overlaps (struct window *w,
28226 struct glyph_row *first_overlapping_row,
28227 struct glyph_row *last_overlapping_row,
28228 XRectangle *r)
28230 struct glyph_row *row;
28232 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28233 if (row->overlapping_p)
28235 eassert (row->enabled_p && !row->mode_line_p);
28237 row->clip = r;
28238 if (row->used[LEFT_MARGIN_AREA])
28239 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28241 if (row->used[TEXT_AREA])
28242 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28244 if (row->used[RIGHT_MARGIN_AREA])
28245 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28246 row->clip = NULL;
28251 /* Return non-zero if W's cursor intersects rectangle R. */
28253 static int
28254 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28256 XRectangle cr, result;
28257 struct glyph *cursor_glyph;
28258 struct glyph_row *row;
28260 if (w->phys_cursor.vpos >= 0
28261 && w->phys_cursor.vpos < w->current_matrix->nrows
28262 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28263 row->enabled_p)
28264 && row->cursor_in_fringe_p)
28266 /* Cursor is in the fringe. */
28267 cr.x = window_box_right_offset (w,
28268 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28269 ? RIGHT_MARGIN_AREA
28270 : TEXT_AREA));
28271 cr.y = row->y;
28272 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28273 cr.height = row->height;
28274 return x_intersect_rectangles (&cr, r, &result);
28277 cursor_glyph = get_phys_cursor_glyph (w);
28278 if (cursor_glyph)
28280 /* r is relative to W's box, but w->phys_cursor.x is relative
28281 to left edge of W's TEXT area. Adjust it. */
28282 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28283 cr.y = w->phys_cursor.y;
28284 cr.width = cursor_glyph->pixel_width;
28285 cr.height = w->phys_cursor_height;
28286 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28287 I assume the effect is the same -- and this is portable. */
28288 return x_intersect_rectangles (&cr, r, &result);
28290 /* If we don't understand the format, pretend we're not in the hot-spot. */
28291 return 0;
28295 /* EXPORT:
28296 Draw a vertical window border to the right of window W if W doesn't
28297 have vertical scroll bars. */
28299 void
28300 x_draw_vertical_border (struct window *w)
28302 struct frame *f = XFRAME (WINDOW_FRAME (w));
28304 /* We could do better, if we knew what type of scroll-bar the adjacent
28305 windows (on either side) have... But we don't :-(
28306 However, I think this works ok. ++KFS 2003-04-25 */
28308 /* Redraw borders between horizontally adjacent windows. Don't
28309 do it for frames with vertical scroll bars because either the
28310 right scroll bar of a window, or the left scroll bar of its
28311 neighbor will suffice as a border. */
28312 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28313 return;
28315 /* Note: It is necessary to redraw both the left and the right
28316 borders, for when only this single window W is being
28317 redisplayed. */
28318 if (!WINDOW_RIGHTMOST_P (w)
28319 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28321 int x0, x1, y0, y1;
28323 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28324 y1 -= 1;
28326 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28327 x1 -= 1;
28329 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28331 if (!WINDOW_LEFTMOST_P (w)
28332 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28334 int x0, x1, y0, y1;
28336 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28337 y1 -= 1;
28339 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28340 x0 -= 1;
28342 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28347 /* Redraw the part of window W intersection rectangle FR. Pixel
28348 coordinates in FR are frame-relative. Call this function with
28349 input blocked. Value is non-zero if the exposure overwrites
28350 mouse-face. */
28352 static int
28353 expose_window (struct window *w, XRectangle *fr)
28355 struct frame *f = XFRAME (w->frame);
28356 XRectangle wr, r;
28357 int mouse_face_overwritten_p = 0;
28359 /* If window is not yet fully initialized, do nothing. This can
28360 happen when toolkit scroll bars are used and a window is split.
28361 Reconfiguring the scroll bar will generate an expose for a newly
28362 created window. */
28363 if (w->current_matrix == NULL)
28364 return 0;
28366 /* When we're currently updating the window, display and current
28367 matrix usually don't agree. Arrange for a thorough display
28368 later. */
28369 if (w == updated_window)
28371 SET_FRAME_GARBAGED (f);
28372 return 0;
28375 /* Frame-relative pixel rectangle of W. */
28376 wr.x = WINDOW_LEFT_EDGE_X (w);
28377 wr.y = WINDOW_TOP_EDGE_Y (w);
28378 wr.width = WINDOW_TOTAL_WIDTH (w);
28379 wr.height = WINDOW_TOTAL_HEIGHT (w);
28381 if (x_intersect_rectangles (fr, &wr, &r))
28383 int yb = window_text_bottom_y (w);
28384 struct glyph_row *row;
28385 int cursor_cleared_p, phys_cursor_on_p;
28386 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28388 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28389 r.x, r.y, r.width, r.height));
28391 /* Convert to window coordinates. */
28392 r.x -= WINDOW_LEFT_EDGE_X (w);
28393 r.y -= WINDOW_TOP_EDGE_Y (w);
28395 /* Turn off the cursor. */
28396 if (!w->pseudo_window_p
28397 && phys_cursor_in_rect_p (w, &r))
28399 x_clear_cursor (w);
28400 cursor_cleared_p = 1;
28402 else
28403 cursor_cleared_p = 0;
28405 /* If the row containing the cursor extends face to end of line,
28406 then expose_area might overwrite the cursor outside the
28407 rectangle and thus notice_overwritten_cursor might clear
28408 w->phys_cursor_on_p. We remember the original value and
28409 check later if it is changed. */
28410 phys_cursor_on_p = w->phys_cursor_on_p;
28412 /* Update lines intersecting rectangle R. */
28413 first_overlapping_row = last_overlapping_row = NULL;
28414 for (row = w->current_matrix->rows;
28415 row->enabled_p;
28416 ++row)
28418 int y0 = row->y;
28419 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28421 if ((y0 >= r.y && y0 < r.y + r.height)
28422 || (y1 > r.y && y1 < r.y + r.height)
28423 || (r.y >= y0 && r.y < y1)
28424 || (r.y + r.height > y0 && r.y + r.height < y1))
28426 /* A header line may be overlapping, but there is no need
28427 to fix overlapping areas for them. KFS 2005-02-12 */
28428 if (row->overlapping_p && !row->mode_line_p)
28430 if (first_overlapping_row == NULL)
28431 first_overlapping_row = row;
28432 last_overlapping_row = row;
28435 row->clip = fr;
28436 if (expose_line (w, row, &r))
28437 mouse_face_overwritten_p = 1;
28438 row->clip = NULL;
28440 else if (row->overlapping_p)
28442 /* We must redraw a row overlapping the exposed area. */
28443 if (y0 < r.y
28444 ? y0 + row->phys_height > r.y
28445 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28447 if (first_overlapping_row == NULL)
28448 first_overlapping_row = row;
28449 last_overlapping_row = row;
28453 if (y1 >= yb)
28454 break;
28457 /* Display the mode line if there is one. */
28458 if (WINDOW_WANTS_MODELINE_P (w)
28459 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28460 row->enabled_p)
28461 && row->y < r.y + r.height)
28463 if (expose_line (w, row, &r))
28464 mouse_face_overwritten_p = 1;
28467 if (!w->pseudo_window_p)
28469 /* Fix the display of overlapping rows. */
28470 if (first_overlapping_row)
28471 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28472 fr);
28474 /* Draw border between windows. */
28475 x_draw_vertical_border (w);
28477 /* Turn the cursor on again. */
28478 if (cursor_cleared_p
28479 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28480 update_window_cursor (w, 1);
28484 return mouse_face_overwritten_p;
28489 /* Redraw (parts) of all windows in the window tree rooted at W that
28490 intersect R. R contains frame pixel coordinates. Value is
28491 non-zero if the exposure overwrites mouse-face. */
28493 static int
28494 expose_window_tree (struct window *w, XRectangle *r)
28496 struct frame *f = XFRAME (w->frame);
28497 int mouse_face_overwritten_p = 0;
28499 while (w && !FRAME_GARBAGED_P (f))
28501 if (WINDOWP (w->contents))
28502 mouse_face_overwritten_p
28503 |= expose_window_tree (XWINDOW (w->contents), r);
28504 else
28505 mouse_face_overwritten_p |= expose_window (w, r);
28507 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28510 return mouse_face_overwritten_p;
28514 /* EXPORT:
28515 Redisplay an exposed area of frame F. X and Y are the upper-left
28516 corner of the exposed rectangle. W and H are width and height of
28517 the exposed area. All are pixel values. W or H zero means redraw
28518 the entire frame. */
28520 void
28521 expose_frame (struct frame *f, int x, int y, int w, int h)
28523 XRectangle r;
28524 int mouse_face_overwritten_p = 0;
28526 TRACE ((stderr, "expose_frame "));
28528 /* No need to redraw if frame will be redrawn soon. */
28529 if (FRAME_GARBAGED_P (f))
28531 TRACE ((stderr, " garbaged\n"));
28532 return;
28535 /* If basic faces haven't been realized yet, there is no point in
28536 trying to redraw anything. This can happen when we get an expose
28537 event while Emacs is starting, e.g. by moving another window. */
28538 if (FRAME_FACE_CACHE (f) == NULL
28539 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28541 TRACE ((stderr, " no faces\n"));
28542 return;
28545 if (w == 0 || h == 0)
28547 r.x = r.y = 0;
28548 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28549 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28551 else
28553 r.x = x;
28554 r.y = y;
28555 r.width = w;
28556 r.height = h;
28559 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28560 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28562 if (WINDOWP (f->tool_bar_window))
28563 mouse_face_overwritten_p
28564 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28566 #ifdef HAVE_X_WINDOWS
28567 #ifndef MSDOS
28568 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28569 if (WINDOWP (f->menu_bar_window))
28570 mouse_face_overwritten_p
28571 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28572 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28573 #endif
28574 #endif
28576 /* Some window managers support a focus-follows-mouse style with
28577 delayed raising of frames. Imagine a partially obscured frame,
28578 and moving the mouse into partially obscured mouse-face on that
28579 frame. The visible part of the mouse-face will be highlighted,
28580 then the WM raises the obscured frame. With at least one WM, KDE
28581 2.1, Emacs is not getting any event for the raising of the frame
28582 (even tried with SubstructureRedirectMask), only Expose events.
28583 These expose events will draw text normally, i.e. not
28584 highlighted. Which means we must redo the highlight here.
28585 Subsume it under ``we love X''. --gerd 2001-08-15 */
28586 /* Included in Windows version because Windows most likely does not
28587 do the right thing if any third party tool offers
28588 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28589 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28591 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28592 if (f == hlinfo->mouse_face_mouse_frame)
28594 int mouse_x = hlinfo->mouse_face_mouse_x;
28595 int mouse_y = hlinfo->mouse_face_mouse_y;
28596 clear_mouse_face (hlinfo);
28597 note_mouse_highlight (f, mouse_x, mouse_y);
28603 /* EXPORT:
28604 Determine the intersection of two rectangles R1 and R2. Return
28605 the intersection in *RESULT. Value is non-zero if RESULT is not
28606 empty. */
28609 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28611 XRectangle *left, *right;
28612 XRectangle *upper, *lower;
28613 int intersection_p = 0;
28615 /* Rearrange so that R1 is the left-most rectangle. */
28616 if (r1->x < r2->x)
28617 left = r1, right = r2;
28618 else
28619 left = r2, right = r1;
28621 /* X0 of the intersection is right.x0, if this is inside R1,
28622 otherwise there is no intersection. */
28623 if (right->x <= left->x + left->width)
28625 result->x = right->x;
28627 /* The right end of the intersection is the minimum of
28628 the right ends of left and right. */
28629 result->width = (min (left->x + left->width, right->x + right->width)
28630 - result->x);
28632 /* Same game for Y. */
28633 if (r1->y < r2->y)
28634 upper = r1, lower = r2;
28635 else
28636 upper = r2, lower = r1;
28638 /* The upper end of the intersection is lower.y0, if this is inside
28639 of upper. Otherwise, there is no intersection. */
28640 if (lower->y <= upper->y + upper->height)
28642 result->y = lower->y;
28644 /* The lower end of the intersection is the minimum of the lower
28645 ends of upper and lower. */
28646 result->height = (min (lower->y + lower->height,
28647 upper->y + upper->height)
28648 - result->y);
28649 intersection_p = 1;
28653 return intersection_p;
28656 #endif /* HAVE_WINDOW_SYSTEM */
28659 /***********************************************************************
28660 Initialization
28661 ***********************************************************************/
28663 void
28664 syms_of_xdisp (void)
28666 Vwith_echo_area_save_vector = Qnil;
28667 staticpro (&Vwith_echo_area_save_vector);
28669 Vmessage_stack = Qnil;
28670 staticpro (&Vmessage_stack);
28672 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28673 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28675 message_dolog_marker1 = Fmake_marker ();
28676 staticpro (&message_dolog_marker1);
28677 message_dolog_marker2 = Fmake_marker ();
28678 staticpro (&message_dolog_marker2);
28679 message_dolog_marker3 = Fmake_marker ();
28680 staticpro (&message_dolog_marker3);
28682 #ifdef GLYPH_DEBUG
28683 defsubr (&Sdump_frame_glyph_matrix);
28684 defsubr (&Sdump_glyph_matrix);
28685 defsubr (&Sdump_glyph_row);
28686 defsubr (&Sdump_tool_bar_row);
28687 defsubr (&Strace_redisplay);
28688 defsubr (&Strace_to_stderr);
28689 #endif
28690 #ifdef HAVE_WINDOW_SYSTEM
28691 defsubr (&Stool_bar_lines_needed);
28692 defsubr (&Slookup_image_map);
28693 #endif
28694 defsubr (&Sformat_mode_line);
28695 defsubr (&Sinvisible_p);
28696 defsubr (&Scurrent_bidi_paragraph_direction);
28698 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28699 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28700 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28701 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28702 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28703 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28704 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28705 DEFSYM (Qeval, "eval");
28706 DEFSYM (QCdata, ":data");
28707 DEFSYM (Qdisplay, "display");
28708 DEFSYM (Qspace_width, "space-width");
28709 DEFSYM (Qraise, "raise");
28710 DEFSYM (Qslice, "slice");
28711 DEFSYM (Qspace, "space");
28712 DEFSYM (Qmargin, "margin");
28713 DEFSYM (Qpointer, "pointer");
28714 DEFSYM (Qleft_margin, "left-margin");
28715 DEFSYM (Qright_margin, "right-margin");
28716 DEFSYM (Qcenter, "center");
28717 DEFSYM (Qline_height, "line-height");
28718 DEFSYM (QCalign_to, ":align-to");
28719 DEFSYM (QCrelative_width, ":relative-width");
28720 DEFSYM (QCrelative_height, ":relative-height");
28721 DEFSYM (QCeval, ":eval");
28722 DEFSYM (QCpropertize, ":propertize");
28723 DEFSYM (QCfile, ":file");
28724 DEFSYM (Qfontified, "fontified");
28725 DEFSYM (Qfontification_functions, "fontification-functions");
28726 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28727 DEFSYM (Qescape_glyph, "escape-glyph");
28728 DEFSYM (Qnobreak_space, "nobreak-space");
28729 DEFSYM (Qimage, "image");
28730 DEFSYM (Qtext, "text");
28731 DEFSYM (Qboth, "both");
28732 DEFSYM (Qboth_horiz, "both-horiz");
28733 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28734 DEFSYM (QCmap, ":map");
28735 DEFSYM (QCpointer, ":pointer");
28736 DEFSYM (Qrect, "rect");
28737 DEFSYM (Qcircle, "circle");
28738 DEFSYM (Qpoly, "poly");
28739 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28740 DEFSYM (Qgrow_only, "grow-only");
28741 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28742 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28743 DEFSYM (Qposition, "position");
28744 DEFSYM (Qbuffer_position, "buffer-position");
28745 DEFSYM (Qobject, "object");
28746 DEFSYM (Qbar, "bar");
28747 DEFSYM (Qhbar, "hbar");
28748 DEFSYM (Qbox, "box");
28749 DEFSYM (Qhollow, "hollow");
28750 DEFSYM (Qhand, "hand");
28751 DEFSYM (Qarrow, "arrow");
28752 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28754 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28755 Fcons (intern_c_string ("void-variable"), Qnil)),
28756 Qnil);
28757 staticpro (&list_of_error);
28759 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28760 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28761 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28762 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28764 echo_buffer[0] = echo_buffer[1] = Qnil;
28765 staticpro (&echo_buffer[0]);
28766 staticpro (&echo_buffer[1]);
28768 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28769 staticpro (&echo_area_buffer[0]);
28770 staticpro (&echo_area_buffer[1]);
28772 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28773 staticpro (&Vmessages_buffer_name);
28775 mode_line_proptrans_alist = Qnil;
28776 staticpro (&mode_line_proptrans_alist);
28777 mode_line_string_list = Qnil;
28778 staticpro (&mode_line_string_list);
28779 mode_line_string_face = Qnil;
28780 staticpro (&mode_line_string_face);
28781 mode_line_string_face_prop = Qnil;
28782 staticpro (&mode_line_string_face_prop);
28783 Vmode_line_unwind_vector = Qnil;
28784 staticpro (&Vmode_line_unwind_vector);
28786 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28788 help_echo_string = Qnil;
28789 staticpro (&help_echo_string);
28790 help_echo_object = Qnil;
28791 staticpro (&help_echo_object);
28792 help_echo_window = Qnil;
28793 staticpro (&help_echo_window);
28794 previous_help_echo_string = Qnil;
28795 staticpro (&previous_help_echo_string);
28796 help_echo_pos = -1;
28798 DEFSYM (Qright_to_left, "right-to-left");
28799 DEFSYM (Qleft_to_right, "left-to-right");
28801 #ifdef HAVE_WINDOW_SYSTEM
28802 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28803 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28804 For example, if a block cursor is over a tab, it will be drawn as
28805 wide as that tab on the display. */);
28806 x_stretch_cursor_p = 0;
28807 #endif
28809 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28810 doc: /* Non-nil means highlight trailing whitespace.
28811 The face used for trailing whitespace is `trailing-whitespace'. */);
28812 Vshow_trailing_whitespace = Qnil;
28814 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28815 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28816 If the value is t, Emacs highlights non-ASCII chars which have the
28817 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28818 or `escape-glyph' face respectively.
28820 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28821 U+2011 (non-breaking hyphen) are affected.
28823 Any other non-nil value means to display these characters as a escape
28824 glyph followed by an ordinary space or hyphen.
28826 A value of nil means no special handling of these characters. */);
28827 Vnobreak_char_display = Qt;
28829 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28830 doc: /* The pointer shape to show in void text areas.
28831 A value of nil means to show the text pointer. Other options are `arrow',
28832 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28833 Vvoid_text_area_pointer = Qarrow;
28835 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28836 doc: /* Non-nil means don't actually do any redisplay.
28837 This is used for internal purposes. */);
28838 Vinhibit_redisplay = Qnil;
28840 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28841 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28842 Vglobal_mode_string = Qnil;
28844 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28845 doc: /* Marker for where to display an arrow on top of the buffer text.
28846 This must be the beginning of a line in order to work.
28847 See also `overlay-arrow-string'. */);
28848 Voverlay_arrow_position = Qnil;
28850 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28851 doc: /* String to display as an arrow in non-window frames.
28852 See also `overlay-arrow-position'. */);
28853 Voverlay_arrow_string = build_pure_c_string ("=>");
28855 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28856 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28857 The symbols on this list are examined during redisplay to determine
28858 where to display overlay arrows. */);
28859 Voverlay_arrow_variable_list
28860 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28862 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28863 doc: /* The number of lines to try scrolling a window by when point moves out.
28864 If that fails to bring point back on frame, point is centered instead.
28865 If this is zero, point is always centered after it moves off frame.
28866 If you want scrolling to always be a line at a time, you should set
28867 `scroll-conservatively' to a large value rather than set this to 1. */);
28869 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28870 doc: /* Scroll up to this many lines, to bring point back on screen.
28871 If point moves off-screen, redisplay will scroll by up to
28872 `scroll-conservatively' lines in order to bring point just barely
28873 onto the screen again. If that cannot be done, then redisplay
28874 recenters point as usual.
28876 If the value is greater than 100, redisplay will never recenter point,
28877 but will always scroll just enough text to bring point into view, even
28878 if you move far away.
28880 A value of zero means always recenter point if it moves off screen. */);
28881 scroll_conservatively = 0;
28883 DEFVAR_INT ("scroll-margin", scroll_margin,
28884 doc: /* Number of lines of margin at the top and bottom of a window.
28885 Recenter the window whenever point gets within this many lines
28886 of the top or bottom of the window. */);
28887 scroll_margin = 0;
28889 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28890 doc: /* Pixels per inch value for non-window system displays.
28891 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28892 Vdisplay_pixels_per_inch = make_float (72.0);
28894 #ifdef GLYPH_DEBUG
28895 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28896 #endif
28898 DEFVAR_LISP ("truncate-partial-width-windows",
28899 Vtruncate_partial_width_windows,
28900 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28901 For an integer value, truncate lines in each window narrower than the
28902 full frame width, provided the window width is less than that integer;
28903 otherwise, respect the value of `truncate-lines'.
28905 For any other non-nil value, truncate lines in all windows that do
28906 not span the full frame width.
28908 A value of nil means to respect the value of `truncate-lines'.
28910 If `word-wrap' is enabled, you might want to reduce this. */);
28911 Vtruncate_partial_width_windows = make_number (50);
28913 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28914 doc: /* Maximum buffer size for which line number should be displayed.
28915 If the buffer is bigger than this, the line number does not appear
28916 in the mode line. A value of nil means no limit. */);
28917 Vline_number_display_limit = Qnil;
28919 DEFVAR_INT ("line-number-display-limit-width",
28920 line_number_display_limit_width,
28921 doc: /* Maximum line width (in characters) for line number display.
28922 If the average length of the lines near point is bigger than this, then the
28923 line number may be omitted from the mode line. */);
28924 line_number_display_limit_width = 200;
28926 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28927 doc: /* Non-nil means highlight region even in nonselected windows. */);
28928 highlight_nonselected_windows = 0;
28930 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28931 doc: /* Non-nil if more than one frame is visible on this display.
28932 Minibuffer-only frames don't count, but iconified frames do.
28933 This variable is not guaranteed to be accurate except while processing
28934 `frame-title-format' and `icon-title-format'. */);
28936 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28937 doc: /* Template for displaying the title bar of visible frames.
28938 \(Assuming the window manager supports this feature.)
28940 This variable has the same structure as `mode-line-format', except that
28941 the %c and %l constructs are ignored. It is used only on frames for
28942 which no explicit name has been set \(see `modify-frame-parameters'). */);
28944 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28945 doc: /* Template for displaying the title bar of an iconified frame.
28946 \(Assuming the window manager supports this feature.)
28947 This variable has the same structure as `mode-line-format' (which see),
28948 and is used only on frames for which no explicit name has been set
28949 \(see `modify-frame-parameters'). */);
28950 Vicon_title_format
28951 = Vframe_title_format
28952 = listn (CONSTYPE_PURE, 3,
28953 intern_c_string ("multiple-frames"),
28954 build_pure_c_string ("%b"),
28955 listn (CONSTYPE_PURE, 4,
28956 empty_unibyte_string,
28957 intern_c_string ("invocation-name"),
28958 build_pure_c_string ("@"),
28959 intern_c_string ("system-name")));
28961 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28962 doc: /* Maximum number of lines to keep in the message log buffer.
28963 If nil, disable message logging. If t, log messages but don't truncate
28964 the buffer when it becomes large. */);
28965 Vmessage_log_max = make_number (1000);
28967 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28968 doc: /* Functions called before redisplay, if window sizes have changed.
28969 The value should be a list of functions that take one argument.
28970 Just before redisplay, for each frame, if any of its windows have changed
28971 size since the last redisplay, or have been split or deleted,
28972 all the functions in the list are called, with the frame as argument. */);
28973 Vwindow_size_change_functions = Qnil;
28975 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28976 doc: /* List of functions to call before redisplaying a window with scrolling.
28977 Each function is called with two arguments, the window and its new
28978 display-start position. Note that these functions are also called by
28979 `set-window-buffer'. Also note that the value of `window-end' is not
28980 valid when these functions are called.
28982 Warning: Do not use this feature to alter the way the window
28983 is scrolled. It is not designed for that, and such use probably won't
28984 work. */);
28985 Vwindow_scroll_functions = Qnil;
28987 DEFVAR_LISP ("window-text-change-functions",
28988 Vwindow_text_change_functions,
28989 doc: /* Functions to call in redisplay when text in the window might change. */);
28990 Vwindow_text_change_functions = Qnil;
28992 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28993 doc: /* Functions called when redisplay of a window reaches the end trigger.
28994 Each function is called with two arguments, the window and the end trigger value.
28995 See `set-window-redisplay-end-trigger'. */);
28996 Vredisplay_end_trigger_functions = Qnil;
28998 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28999 doc: /* Non-nil means autoselect window with mouse pointer.
29000 If nil, do not autoselect windows.
29001 A positive number means delay autoselection by that many seconds: a
29002 window is autoselected only after the mouse has remained in that
29003 window for the duration of the delay.
29004 A negative number has a similar effect, but causes windows to be
29005 autoselected only after the mouse has stopped moving. \(Because of
29006 the way Emacs compares mouse events, you will occasionally wait twice
29007 that time before the window gets selected.\)
29008 Any other value means to autoselect window instantaneously when the
29009 mouse pointer enters it.
29011 Autoselection selects the minibuffer only if it is active, and never
29012 unselects the minibuffer if it is active.
29014 When customizing this variable make sure that the actual value of
29015 `focus-follows-mouse' matches the behavior of your window manager. */);
29016 Vmouse_autoselect_window = Qnil;
29018 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29019 doc: /* Non-nil means automatically resize tool-bars.
29020 This dynamically changes the tool-bar's height to the minimum height
29021 that is needed to make all tool-bar items visible.
29022 If value is `grow-only', the tool-bar's height is only increased
29023 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29024 Vauto_resize_tool_bars = Qt;
29026 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29027 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29028 auto_raise_tool_bar_buttons_p = 1;
29030 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29031 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29032 make_cursor_line_fully_visible_p = 1;
29034 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29035 doc: /* Border below tool-bar in pixels.
29036 If an integer, use it as the height of the border.
29037 If it is one of `internal-border-width' or `border-width', use the
29038 value of the corresponding frame parameter.
29039 Otherwise, no border is added below the tool-bar. */);
29040 Vtool_bar_border = Qinternal_border_width;
29042 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29043 doc: /* Margin around tool-bar buttons in pixels.
29044 If an integer, use that for both horizontal and vertical margins.
29045 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29046 HORZ specifying the horizontal margin, and VERT specifying the
29047 vertical margin. */);
29048 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29050 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29051 doc: /* Relief thickness of tool-bar buttons. */);
29052 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29054 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29055 doc: /* Tool bar style to use.
29056 It can be one of
29057 image - show images only
29058 text - show text only
29059 both - show both, text below image
29060 both-horiz - show text to the right of the image
29061 text-image-horiz - show text to the left of the image
29062 any other - use system default or image if no system default.
29064 This variable only affects the GTK+ toolkit version of Emacs. */);
29065 Vtool_bar_style = Qnil;
29067 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29068 doc: /* Maximum number of characters a label can have to be shown.
29069 The tool bar style must also show labels for this to have any effect, see
29070 `tool-bar-style'. */);
29071 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29073 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29074 doc: /* List of functions to call to fontify regions of text.
29075 Each function is called with one argument POS. Functions must
29076 fontify a region starting at POS in the current buffer, and give
29077 fontified regions the property `fontified'. */);
29078 Vfontification_functions = Qnil;
29079 Fmake_variable_buffer_local (Qfontification_functions);
29081 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29082 unibyte_display_via_language_environment,
29083 doc: /* Non-nil means display unibyte text according to language environment.
29084 Specifically, this means that raw bytes in the range 160-255 decimal
29085 are displayed by converting them to the equivalent multibyte characters
29086 according to the current language environment. As a result, they are
29087 displayed according to the current fontset.
29089 Note that this variable affects only how these bytes are displayed,
29090 but does not change the fact they are interpreted as raw bytes. */);
29091 unibyte_display_via_language_environment = 0;
29093 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29094 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29095 If a float, it specifies a fraction of the mini-window frame's height.
29096 If an integer, it specifies a number of lines. */);
29097 Vmax_mini_window_height = make_float (0.25);
29099 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29100 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29101 A value of nil means don't automatically resize mini-windows.
29102 A value of t means resize them to fit the text displayed in them.
29103 A value of `grow-only', the default, means let mini-windows grow only;
29104 they return to their normal size when the minibuffer is closed, or the
29105 echo area becomes empty. */);
29106 Vresize_mini_windows = Qgrow_only;
29108 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29109 doc: /* Alist specifying how to blink the cursor off.
29110 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29111 `cursor-type' frame-parameter or variable equals ON-STATE,
29112 comparing using `equal', Emacs uses OFF-STATE to specify
29113 how to blink it off. ON-STATE and OFF-STATE are values for
29114 the `cursor-type' frame parameter.
29116 If a frame's ON-STATE has no entry in this list,
29117 the frame's other specifications determine how to blink the cursor off. */);
29118 Vblink_cursor_alist = Qnil;
29120 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29121 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29122 If non-nil, windows are automatically scrolled horizontally to make
29123 point visible. */);
29124 automatic_hscrolling_p = 1;
29125 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29127 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29128 doc: /* How many columns away from the window edge point is allowed to get
29129 before automatic hscrolling will horizontally scroll the window. */);
29130 hscroll_margin = 5;
29132 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29133 doc: /* How many columns to scroll the window when point gets too close to the edge.
29134 When point is less than `hscroll-margin' columns from the window
29135 edge, automatic hscrolling will scroll the window by the amount of columns
29136 determined by this variable. If its value is a positive integer, scroll that
29137 many columns. If it's a positive floating-point number, it specifies the
29138 fraction of the window's width to scroll. If it's nil or zero, point will be
29139 centered horizontally after the scroll. Any other value, including negative
29140 numbers, are treated as if the value were zero.
29142 Automatic hscrolling always moves point outside the scroll margin, so if
29143 point was more than scroll step columns inside the margin, the window will
29144 scroll more than the value given by the scroll step.
29146 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29147 and `scroll-right' overrides this variable's effect. */);
29148 Vhscroll_step = make_number (0);
29150 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29151 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29152 Bind this around calls to `message' to let it take effect. */);
29153 message_truncate_lines = 0;
29155 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29156 doc: /* Normal hook run to update the menu bar definitions.
29157 Redisplay runs this hook before it redisplays the menu bar.
29158 This is used to update submenus such as Buffers,
29159 whose contents depend on various data. */);
29160 Vmenu_bar_update_hook = Qnil;
29162 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29163 doc: /* Frame for which we are updating a menu.
29164 The enable predicate for a menu binding should check this variable. */);
29165 Vmenu_updating_frame = Qnil;
29167 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29168 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29169 inhibit_menubar_update = 0;
29171 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29172 doc: /* Prefix prepended to all continuation lines at display time.
29173 The value may be a string, an image, or a stretch-glyph; it is
29174 interpreted in the same way as the value of a `display' text property.
29176 This variable is overridden by any `wrap-prefix' text or overlay
29177 property.
29179 To add a prefix to non-continuation lines, use `line-prefix'. */);
29180 Vwrap_prefix = Qnil;
29181 DEFSYM (Qwrap_prefix, "wrap-prefix");
29182 Fmake_variable_buffer_local (Qwrap_prefix);
29184 DEFVAR_LISP ("line-prefix", Vline_prefix,
29185 doc: /* Prefix prepended to all non-continuation lines at display time.
29186 The value may be a string, an image, or a stretch-glyph; it is
29187 interpreted in the same way as the value of a `display' text property.
29189 This variable is overridden by any `line-prefix' text or overlay
29190 property.
29192 To add a prefix to continuation lines, use `wrap-prefix'. */);
29193 Vline_prefix = Qnil;
29194 DEFSYM (Qline_prefix, "line-prefix");
29195 Fmake_variable_buffer_local (Qline_prefix);
29197 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29198 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29199 inhibit_eval_during_redisplay = 0;
29201 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29202 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29203 inhibit_free_realized_faces = 0;
29205 #ifdef GLYPH_DEBUG
29206 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29207 doc: /* Inhibit try_window_id display optimization. */);
29208 inhibit_try_window_id = 0;
29210 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29211 doc: /* Inhibit try_window_reusing display optimization. */);
29212 inhibit_try_window_reusing = 0;
29214 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29215 doc: /* Inhibit try_cursor_movement display optimization. */);
29216 inhibit_try_cursor_movement = 0;
29217 #endif /* GLYPH_DEBUG */
29219 DEFVAR_INT ("overline-margin", overline_margin,
29220 doc: /* Space between overline and text, in pixels.
29221 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29222 margin to the character height. */);
29223 overline_margin = 2;
29225 DEFVAR_INT ("underline-minimum-offset",
29226 underline_minimum_offset,
29227 doc: /* Minimum distance between baseline and underline.
29228 This can improve legibility of underlined text at small font sizes,
29229 particularly when using variable `x-use-underline-position-properties'
29230 with fonts that specify an UNDERLINE_POSITION relatively close to the
29231 baseline. The default value is 1. */);
29232 underline_minimum_offset = 1;
29234 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29235 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29236 This feature only works when on a window system that can change
29237 cursor shapes. */);
29238 display_hourglass_p = 1;
29240 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29241 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29242 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29244 hourglass_atimer = NULL;
29245 hourglass_shown_p = 0;
29247 DEFSYM (Qglyphless_char, "glyphless-char");
29248 DEFSYM (Qhex_code, "hex-code");
29249 DEFSYM (Qempty_box, "empty-box");
29250 DEFSYM (Qthin_space, "thin-space");
29251 DEFSYM (Qzero_width, "zero-width");
29253 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29254 /* Intern this now in case it isn't already done.
29255 Setting this variable twice is harmless.
29256 But don't staticpro it here--that is done in alloc.c. */
29257 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29258 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29260 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29261 doc: /* Char-table defining glyphless characters.
29262 Each element, if non-nil, should be one of the following:
29263 an ASCII acronym string: display this string in a box
29264 `hex-code': display the hexadecimal code of a character in a box
29265 `empty-box': display as an empty box
29266 `thin-space': display as 1-pixel width space
29267 `zero-width': don't display
29268 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29269 display method for graphical terminals and text terminals respectively.
29270 GRAPHICAL and TEXT should each have one of the values listed above.
29272 The char-table has one extra slot to control the display of a character for
29273 which no font is found. This slot only takes effect on graphical terminals.
29274 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29275 `thin-space'. The default is `empty-box'. */);
29276 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29277 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29278 Qempty_box);
29280 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29281 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29282 Vdebug_on_message = Qnil;
29286 /* Initialize this module when Emacs starts. */
29288 void
29289 init_xdisp (void)
29291 current_header_line_height = current_mode_line_height = -1;
29293 CHARPOS (this_line_start_pos) = 0;
29295 if (!noninteractive)
29297 struct window *m = XWINDOW (minibuf_window);
29298 Lisp_Object frame = m->frame;
29299 struct frame *f = XFRAME (frame);
29300 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29301 struct window *r = XWINDOW (root);
29302 int i;
29304 echo_area_window = minibuf_window;
29306 r->top_line = FRAME_TOP_MARGIN (f);
29307 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29308 r->total_cols = FRAME_COLS (f);
29310 m->top_line = FRAME_LINES (f) - 1;
29311 m->total_lines = 1;
29312 m->total_cols = FRAME_COLS (f);
29314 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29315 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29316 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29318 /* The default ellipsis glyphs `...'. */
29319 for (i = 0; i < 3; ++i)
29320 default_invis_vector[i] = make_number ('.');
29324 /* Allocate the buffer for frame titles.
29325 Also used for `format-mode-line'. */
29326 int size = 100;
29327 mode_line_noprop_buf = xmalloc (size);
29328 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29329 mode_line_noprop_ptr = mode_line_noprop_buf;
29330 mode_line_target = MODE_LINE_DISPLAY;
29333 help_echo_showing_p = 0;
29336 /* Platform-independent portion of hourglass implementation. */
29338 /* Cancel a currently active hourglass timer, and start a new one. */
29339 void
29340 start_hourglass (void)
29342 #if defined (HAVE_WINDOW_SYSTEM)
29343 EMACS_TIME delay;
29345 cancel_hourglass ();
29347 if (INTEGERP (Vhourglass_delay)
29348 && XINT (Vhourglass_delay) > 0)
29349 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29350 TYPE_MAXIMUM (time_t)),
29352 else if (FLOATP (Vhourglass_delay)
29353 && XFLOAT_DATA (Vhourglass_delay) > 0)
29354 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29355 else
29356 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29358 #ifdef HAVE_NTGUI
29360 extern void w32_note_current_window (void);
29361 w32_note_current_window ();
29363 #endif /* HAVE_NTGUI */
29365 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29366 show_hourglass, NULL);
29367 #endif
29371 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29372 shown. */
29373 void
29374 cancel_hourglass (void)
29376 #if defined (HAVE_WINDOW_SYSTEM)
29377 if (hourglass_atimer)
29379 cancel_atimer (hourglass_atimer);
29380 hourglass_atimer = NULL;
29383 if (hourglass_shown_p)
29384 hide_hourglass ();
29385 #endif