* nsfont.m (ns_charset_covers): Don't abort if no bitmap.
[emacs.git] / src / xdisp.c
bloba03de37dba97f6040028cc76582ed8bbf01f5def
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22 Redisplay.
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
53 expose_window (asynchronous) |
55 X expose events -----+
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
84 . try_cursor_movement
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
90 . try_window_reusing_current_matrix
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
96 . try_window_id
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
102 . try_window
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
115 Desired matrices.
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
160 Frame matrices.
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
181 Bidirectional display.
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
224 Bidirectional display and character compositions
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
251 Moving an iterator in bidirectional text
252 without producing glyphs
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
315 #include "font.h"
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
321 #define INFINITY 10000000
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
346 static Lisp_Object Qfontification_functions;
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
351 /* Non-nil means don't actually do any redisplay. */
353 Lisp_Object Qinhibit_redisplay;
355 /* Names of text properties relevant for redisplay. */
357 Lisp_Object Qdisplay;
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
368 #ifdef HAVE_WINDOW_SYSTEM
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
386 /* Test if the display element loaded in IT, or the underlying buffer
387 or string character, is a space or a TAB character. This is used
388 to determine where word wrapping can occur. */
390 #define IT_DISPLAYING_WHITESPACE(it) \
391 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
392 || ((STRINGP (it->string) \
393 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
394 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
395 || (it->s \
396 && (it->s[IT_BYTEPOS (*it)] == ' ' \
397 || it->s[IT_BYTEPOS (*it)] == '\t')) \
398 || (IT_BYTEPOS (*it) < ZV_BYTE \
399 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
400 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
402 /* Name of the face used to highlight trailing whitespace. */
404 static Lisp_Object Qtrailing_whitespace;
406 /* Name and number of the face used to highlight escape glyphs. */
408 static Lisp_Object Qescape_glyph;
410 /* Name and number of the face used to highlight non-breaking spaces. */
412 static Lisp_Object Qnobreak_space;
414 /* The symbol `image' which is the car of the lists used to represent
415 images in Lisp. Also a tool bar style. */
417 Lisp_Object Qimage;
419 /* The image map types. */
420 Lisp_Object QCmap;
421 static Lisp_Object QCpointer;
422 static Lisp_Object Qrect, Qcircle, Qpoly;
424 /* Tool bar styles */
425 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
427 /* Non-zero means print newline to stdout before next mini-buffer
428 message. */
430 int noninteractive_need_newline;
432 /* Non-zero means print newline to message log before next message. */
434 static int message_log_need_newline;
436 /* Three markers that message_dolog uses.
437 It could allocate them itself, but that causes trouble
438 in handling memory-full errors. */
439 static Lisp_Object message_dolog_marker1;
440 static Lisp_Object message_dolog_marker2;
441 static Lisp_Object message_dolog_marker3;
443 /* The buffer position of the first character appearing entirely or
444 partially on the line of the selected window which contains the
445 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
446 redisplay optimization in redisplay_internal. */
448 static struct text_pos this_line_start_pos;
450 /* Number of characters past the end of the line above, including the
451 terminating newline. */
453 static struct text_pos this_line_end_pos;
455 /* The vertical positions and the height of this line. */
457 static int this_line_vpos;
458 static int this_line_y;
459 static int this_line_pixel_height;
461 /* X position at which this display line starts. Usually zero;
462 negative if first character is partially visible. */
464 static int this_line_start_x;
466 /* The smallest character position seen by move_it_* functions as they
467 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
468 hscrolled lines, see display_line. */
470 static struct text_pos this_line_min_pos;
472 /* Buffer that this_line_.* variables are referring to. */
474 static struct buffer *this_line_buffer;
477 /* Values of those variables at last redisplay are stored as
478 properties on `overlay-arrow-position' symbol. However, if
479 Voverlay_arrow_position is a marker, last-arrow-position is its
480 numerical position. */
482 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
484 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
485 properties on a symbol in overlay-arrow-variable-list. */
487 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
489 Lisp_Object Qmenu_bar_update_hook;
491 /* Nonzero if an overlay arrow has been displayed in this window. */
493 static int overlay_arrow_seen;
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
499 int buffer_shared;
501 /* Vector containing glyphs for an ellipsis `...'. */
503 static Lisp_Object default_invis_vector[3];
505 /* This is the window where the echo area message was displayed. It
506 is always a mini-buffer window, but it may not be the same window
507 currently active as a mini-buffer. */
509 Lisp_Object echo_area_window;
511 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
512 pushes the current message and the value of
513 message_enable_multibyte on the stack, the function restore_message
514 pops the stack and displays MESSAGE again. */
516 static Lisp_Object Vmessage_stack;
518 /* Nonzero means multibyte characters were enabled when the echo area
519 message was specified. */
521 static int message_enable_multibyte;
523 /* Nonzero if we should redraw the mode lines on the next redisplay. */
525 int update_mode_lines;
527 /* Nonzero if window sizes or contents have changed since last
528 redisplay that finished. */
530 int windows_or_buffers_changed;
532 /* Nonzero means a frame's cursor type has been changed. */
534 int cursor_type_changed;
536 /* Nonzero after display_mode_line if %l was used and it displayed a
537 line number. */
539 static int line_number_displayed;
541 /* The name of the *Messages* buffer, a string. */
543 static Lisp_Object Vmessages_buffer_name;
545 /* Current, index 0, and last displayed echo area message. Either
546 buffers from echo_buffers, or nil to indicate no message. */
548 Lisp_Object echo_area_buffer[2];
550 /* The buffers referenced from echo_area_buffer. */
552 static Lisp_Object echo_buffer[2];
554 /* A vector saved used in with_area_buffer to reduce consing. */
556 static Lisp_Object Vwith_echo_area_save_vector;
558 /* Non-zero means display_echo_area should display the last echo area
559 message again. Set by redisplay_preserve_echo_area. */
561 static int display_last_displayed_message_p;
563 /* Nonzero if echo area is being used by print; zero if being used by
564 message. */
566 static int message_buf_print;
568 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
570 static Lisp_Object Qinhibit_menubar_update;
571 static Lisp_Object Qmessage_truncate_lines;
573 /* Set to 1 in clear_message to make redisplay_internal aware
574 of an emptied echo area. */
576 static int message_cleared_p;
578 /* A scratch glyph row with contents used for generating truncation
579 glyphs. Also used in direct_output_for_insert. */
581 #define MAX_SCRATCH_GLYPHS 100
582 static struct glyph_row scratch_glyph_row;
583 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
585 /* Ascent and height of the last line processed by move_it_to. */
587 static int last_max_ascent, last_height;
589 /* Non-zero if there's a help-echo in the echo area. */
591 int help_echo_showing_p;
593 /* If >= 0, computed, exact values of mode-line and header-line height
594 to use in the macros CURRENT_MODE_LINE_HEIGHT and
595 CURRENT_HEADER_LINE_HEIGHT. */
597 int current_mode_line_height, current_header_line_height;
599 /* The maximum distance to look ahead for text properties. Values
600 that are too small let us call compute_char_face and similar
601 functions too often which is expensive. Values that are too large
602 let us call compute_char_face and alike too often because we
603 might not be interested in text properties that far away. */
605 #define TEXT_PROP_DISTANCE_LIMIT 100
607 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
608 iterator state and later restore it. This is needed because the
609 bidi iterator on bidi.c keeps a stacked cache of its states, which
610 is really a singleton. When we use scratch iterator objects to
611 move around the buffer, we can cause the bidi cache to be pushed or
612 popped, and therefore we need to restore the cache state when we
613 return to the original iterator. */
614 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
615 do { \
616 if (CACHE) \
617 bidi_unshelve_cache (CACHE, 1); \
618 ITCOPY = ITORIG; \
619 CACHE = bidi_shelve_cache (); \
620 } while (0)
622 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
623 do { \
624 if (pITORIG != pITCOPY) \
625 *(pITORIG) = *(pITCOPY); \
626 bidi_unshelve_cache (CACHE, 0); \
627 CACHE = NULL; \
628 } while (0)
630 #ifdef GLYPH_DEBUG
632 /* Non-zero means print traces of redisplay if compiled with
633 GLYPH_DEBUG defined. */
635 int trace_redisplay_p;
637 #endif /* GLYPH_DEBUG */
639 #ifdef DEBUG_TRACE_MOVE
640 /* Non-zero means trace with TRACE_MOVE to stderr. */
641 int trace_move;
643 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
644 #else
645 #define TRACE_MOVE(x) (void) 0
646 #endif
648 static Lisp_Object Qauto_hscroll_mode;
650 /* Buffer being redisplayed -- for redisplay_window_error. */
652 static struct buffer *displayed_buffer;
654 /* Value returned from text property handlers (see below). */
656 enum prop_handled
658 HANDLED_NORMALLY,
659 HANDLED_RECOMPUTE_PROPS,
660 HANDLED_OVERLAY_STRING_CONSUMED,
661 HANDLED_RETURN
664 /* A description of text properties that redisplay is interested
665 in. */
667 struct props
669 /* The name of the property. */
670 Lisp_Object *name;
672 /* A unique index for the property. */
673 enum prop_idx idx;
675 /* A handler function called to set up iterator IT from the property
676 at IT's current position. Value is used to steer handle_stop. */
677 enum prop_handled (*handler) (struct it *it);
680 static enum prop_handled handle_face_prop (struct it *);
681 static enum prop_handled handle_invisible_prop (struct it *);
682 static enum prop_handled handle_display_prop (struct it *);
683 static enum prop_handled handle_composition_prop (struct it *);
684 static enum prop_handled handle_overlay_change (struct it *);
685 static enum prop_handled handle_fontified_prop (struct it *);
687 /* Properties handled by iterators. */
689 static struct props it_props[] =
691 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
692 /* Handle `face' before `display' because some sub-properties of
693 `display' need to know the face. */
694 {&Qface, FACE_PROP_IDX, handle_face_prop},
695 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
696 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
697 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
698 {NULL, 0, NULL}
701 /* Value is the position described by X. If X is a marker, value is
702 the marker_position of X. Otherwise, value is X. */
704 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
706 /* Enumeration returned by some move_it_.* functions internally. */
708 enum move_it_result
710 /* Not used. Undefined value. */
711 MOVE_UNDEFINED,
713 /* Move ended at the requested buffer position or ZV. */
714 MOVE_POS_MATCH_OR_ZV,
716 /* Move ended at the requested X pixel position. */
717 MOVE_X_REACHED,
719 /* Move within a line ended at the end of a line that must be
720 continued. */
721 MOVE_LINE_CONTINUED,
723 /* Move within a line ended at the end of a line that would
724 be displayed truncated. */
725 MOVE_LINE_TRUNCATED,
727 /* Move within a line ended at a line end. */
728 MOVE_NEWLINE_OR_CR
731 /* This counter is used to clear the face cache every once in a while
732 in redisplay_internal. It is incremented for each redisplay.
733 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
734 cleared. */
736 #define CLEAR_FACE_CACHE_COUNT 500
737 static int clear_face_cache_count;
739 /* Similarly for the image cache. */
741 #ifdef HAVE_WINDOW_SYSTEM
742 #define CLEAR_IMAGE_CACHE_COUNT 101
743 static int clear_image_cache_count;
745 /* Null glyph slice */
746 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
747 #endif
749 /* Non-zero while redisplay_internal is in progress. */
751 int redisplaying_p;
753 static Lisp_Object Qinhibit_free_realized_faces;
754 static Lisp_Object Qmode_line_default_help_echo;
756 /* If a string, XTread_socket generates an event to display that string.
757 (The display is done in read_char.) */
759 Lisp_Object help_echo_string;
760 Lisp_Object help_echo_window;
761 Lisp_Object help_echo_object;
762 ptrdiff_t help_echo_pos;
764 /* Temporary variable for XTread_socket. */
766 Lisp_Object previous_help_echo_string;
768 /* Platform-independent portion of hourglass implementation. */
770 /* Non-zero means an hourglass cursor is currently shown. */
771 int hourglass_shown_p;
773 /* If non-null, an asynchronous timer that, when it expires, displays
774 an hourglass cursor on all frames. */
775 struct atimer *hourglass_atimer;
777 /* Name of the face used to display glyphless characters. */
778 Lisp_Object Qglyphless_char;
780 /* Symbol for the purpose of Vglyphless_char_display. */
781 static Lisp_Object Qglyphless_char_display;
783 /* Method symbols for Vglyphless_char_display. */
784 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
786 /* Default pixel width of `thin-space' display method. */
787 #define THIN_SPACE_WIDTH 1
789 /* Default number of seconds to wait before displaying an hourglass
790 cursor. */
791 #define DEFAULT_HOURGLASS_DELAY 1
794 /* Function prototypes. */
796 static void setup_for_ellipsis (struct it *, int);
797 static void set_iterator_to_next (struct it *, int);
798 static void mark_window_display_accurate_1 (struct window *, int);
799 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
800 static int display_prop_string_p (Lisp_Object, Lisp_Object);
801 static int cursor_row_p (struct glyph_row *);
802 static int redisplay_mode_lines (Lisp_Object, int);
803 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
805 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
807 static void handle_line_prefix (struct it *);
809 static void pint2str (char *, int, ptrdiff_t);
810 static void pint2hrstr (char *, int, ptrdiff_t);
811 static struct text_pos run_window_scroll_functions (Lisp_Object,
812 struct text_pos);
813 static void reconsider_clip_changes (struct window *, struct buffer *);
814 static int text_outside_line_unchanged_p (struct window *,
815 ptrdiff_t, ptrdiff_t);
816 static void store_mode_line_noprop_char (char);
817 static int store_mode_line_noprop (const char *, int, int);
818 static void handle_stop (struct it *);
819 static void handle_stop_backwards (struct it *, ptrdiff_t);
820 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
821 static void ensure_echo_area_buffers (void);
822 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
823 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
824 static int with_echo_area_buffer (struct window *, int,
825 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
826 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
827 static void clear_garbaged_frames (void);
828 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
829 static void pop_message (void);
830 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
831 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
832 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
833 static int display_echo_area (struct window *);
834 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
835 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
836 static Lisp_Object unwind_redisplay (Lisp_Object);
837 static int string_char_and_length (const unsigned char *, int *);
838 static struct text_pos display_prop_end (struct it *, Lisp_Object,
839 struct text_pos);
840 static int compute_window_start_on_continuation_line (struct window *);
841 static Lisp_Object safe_eval_handler (Lisp_Object);
842 static void insert_left_trunc_glyphs (struct it *);
843 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
844 Lisp_Object);
845 static void extend_face_to_end_of_line (struct it *);
846 static int append_space_for_newline (struct it *, int);
847 static int cursor_row_fully_visible_p (struct window *, int, int);
848 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
849 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
850 static int trailing_whitespace_p (ptrdiff_t);
851 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
852 static void push_it (struct it *, struct text_pos *);
853 static void iterate_out_of_display_property (struct it *);
854 static void pop_it (struct it *);
855 static void sync_frame_with_window_matrix_rows (struct window *);
856 static void select_frame_for_redisplay (Lisp_Object);
857 static void redisplay_internal (void);
858 static int echo_area_display (int);
859 static void redisplay_windows (Lisp_Object);
860 static void redisplay_window (Lisp_Object, int);
861 static Lisp_Object redisplay_window_error (Lisp_Object);
862 static Lisp_Object redisplay_window_0 (Lisp_Object);
863 static Lisp_Object redisplay_window_1 (Lisp_Object);
864 static int set_cursor_from_row (struct window *, struct glyph_row *,
865 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
866 int, int);
867 static int update_menu_bar (struct frame *, int, int);
868 static int try_window_reusing_current_matrix (struct window *);
869 static int try_window_id (struct window *);
870 static int display_line (struct it *);
871 static int display_mode_lines (struct window *);
872 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
873 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
874 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
875 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
876 static void display_menu_bar (struct window *);
877 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
878 ptrdiff_t *);
879 static int display_string (const char *, Lisp_Object, Lisp_Object,
880 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
881 static void compute_line_metrics (struct it *);
882 static void run_redisplay_end_trigger_hook (struct it *);
883 static int get_overlay_strings (struct it *, ptrdiff_t);
884 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
885 static void next_overlay_string (struct it *);
886 static void reseat (struct it *, struct text_pos, int);
887 static void reseat_1 (struct it *, struct text_pos, int);
888 static void back_to_previous_visible_line_start (struct it *);
889 void reseat_at_previous_visible_line_start (struct it *);
890 static void reseat_at_next_visible_line_start (struct it *, int);
891 static int next_element_from_ellipsis (struct it *);
892 static int next_element_from_display_vector (struct it *);
893 static int next_element_from_string (struct it *);
894 static int next_element_from_c_string (struct it *);
895 static int next_element_from_buffer (struct it *);
896 static int next_element_from_composition (struct it *);
897 static int next_element_from_image (struct it *);
898 static int next_element_from_stretch (struct it *);
899 static void load_overlay_strings (struct it *, ptrdiff_t);
900 static int init_from_display_pos (struct it *, struct window *,
901 struct display_pos *);
902 static void reseat_to_string (struct it *, const char *,
903 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
904 static int get_next_display_element (struct it *);
905 static enum move_it_result
906 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
907 enum move_operation_enum);
908 void move_it_vertically_backward (struct it *, int);
909 static void init_to_row_start (struct it *, struct window *,
910 struct glyph_row *);
911 static int init_to_row_end (struct it *, struct window *,
912 struct glyph_row *);
913 static void back_to_previous_line_start (struct it *);
914 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
915 static struct text_pos string_pos_nchars_ahead (struct text_pos,
916 Lisp_Object, ptrdiff_t);
917 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
918 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
919 static ptrdiff_t number_of_chars (const char *, int);
920 static void compute_stop_pos (struct it *);
921 static void compute_string_pos (struct text_pos *, struct text_pos,
922 Lisp_Object);
923 static int face_before_or_after_it_pos (struct it *, int);
924 static ptrdiff_t next_overlay_change (ptrdiff_t);
925 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
926 Lisp_Object, struct text_pos *, ptrdiff_t, int);
927 static int handle_single_display_spec (struct it *, Lisp_Object,
928 Lisp_Object, Lisp_Object,
929 struct text_pos *, ptrdiff_t, int, int);
930 static int underlying_face_id (struct it *);
931 static int in_ellipses_for_invisible_text_p (struct display_pos *,
932 struct window *);
934 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
935 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
937 #ifdef HAVE_WINDOW_SYSTEM
939 static void x_consider_frame_title (Lisp_Object);
940 static int tool_bar_lines_needed (struct frame *, int *);
941 static void update_tool_bar (struct frame *, int);
942 static void build_desired_tool_bar_string (struct frame *f);
943 static int redisplay_tool_bar (struct frame *);
944 static void display_tool_bar_line (struct it *, int);
945 static void notice_overwritten_cursor (struct window *,
946 enum glyph_row_area,
947 int, int, int, int);
948 static void append_stretch_glyph (struct it *, Lisp_Object,
949 int, int, int);
952 #endif /* HAVE_WINDOW_SYSTEM */
954 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
955 static int coords_in_mouse_face_p (struct window *, int, int);
959 /***********************************************************************
960 Window display dimensions
961 ***********************************************************************/
963 /* Return the bottom boundary y-position for text lines in window W.
964 This is the first y position at which a line cannot start.
965 It is relative to the top of the window.
967 This is the height of W minus the height of a mode line, if any. */
970 window_text_bottom_y (struct window *w)
972 int height = WINDOW_TOTAL_HEIGHT (w);
974 if (WINDOW_WANTS_MODELINE_P (w))
975 height -= CURRENT_MODE_LINE_HEIGHT (w);
976 return height;
979 /* Return the pixel width of display area AREA of window W. AREA < 0
980 means return the total width of W, not including fringes to
981 the left and right of the window. */
984 window_box_width (struct window *w, int area)
986 int cols = XFASTINT (w->total_cols);
987 int pixels = 0;
989 if (!w->pseudo_window_p)
991 cols -= WINDOW_SCROLL_BAR_COLS (w);
993 if (area == TEXT_AREA)
995 if (INTEGERP (w->left_margin_cols))
996 cols -= XFASTINT (w->left_margin_cols);
997 if (INTEGERP (w->right_margin_cols))
998 cols -= XFASTINT (w->right_margin_cols);
999 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1001 else if (area == LEFT_MARGIN_AREA)
1003 cols = (INTEGERP (w->left_margin_cols)
1004 ? XFASTINT (w->left_margin_cols) : 0);
1005 pixels = 0;
1007 else if (area == RIGHT_MARGIN_AREA)
1009 cols = (INTEGERP (w->right_margin_cols)
1010 ? XFASTINT (w->right_margin_cols) : 0);
1011 pixels = 0;
1015 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1019 /* Return the pixel height of the display area of window W, not
1020 including mode lines of W, if any. */
1023 window_box_height (struct window *w)
1025 struct frame *f = XFRAME (w->frame);
1026 int height = WINDOW_TOTAL_HEIGHT (w);
1028 eassert (height >= 0);
1030 /* Note: the code below that determines the mode-line/header-line
1031 height is essentially the same as that contained in the macro
1032 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1033 the appropriate glyph row has its `mode_line_p' flag set,
1034 and if it doesn't, uses estimate_mode_line_height instead. */
1036 if (WINDOW_WANTS_MODELINE_P (w))
1038 struct glyph_row *ml_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (ml_row && ml_row->mode_line_p)
1043 height -= ml_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1048 if (WINDOW_WANTS_HEADER_LINE_P (w))
1050 struct glyph_row *hl_row
1051 = (w->current_matrix && w->current_matrix->rows
1052 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1053 : 0);
1054 if (hl_row && hl_row->mode_line_p)
1055 height -= hl_row->height;
1056 else
1057 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1060 /* With a very small font and a mode-line that's taller than
1061 default, we might end up with a negative height. */
1062 return max (0, height);
1065 /* Return the window-relative coordinate of the left edge of display
1066 area AREA of window W. AREA < 0 means return the left edge of the
1067 whole window, to the right of the left fringe of W. */
1070 window_box_left_offset (struct window *w, int area)
1072 int x;
1074 if (w->pseudo_window_p)
1075 return 0;
1077 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1079 if (area == TEXT_AREA)
1080 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1081 + window_box_width (w, LEFT_MARGIN_AREA));
1082 else if (area == RIGHT_MARGIN_AREA)
1083 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1084 + window_box_width (w, LEFT_MARGIN_AREA)
1085 + window_box_width (w, TEXT_AREA)
1086 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1088 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1089 else if (area == LEFT_MARGIN_AREA
1090 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1091 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1093 return x;
1097 /* Return the window-relative coordinate of the right edge of display
1098 area AREA of window W. AREA < 0 means return the right edge of the
1099 whole window, to the left of the right fringe of W. */
1102 window_box_right_offset (struct window *w, int area)
1104 return window_box_left_offset (w, area) + window_box_width (w, area);
1107 /* Return the frame-relative coordinate of the left edge of display
1108 area AREA of window W. AREA < 0 means return the left edge of the
1109 whole window, to the right of the left fringe of W. */
1112 window_box_left (struct window *w, int area)
1114 struct frame *f = XFRAME (w->frame);
1115 int x;
1117 if (w->pseudo_window_p)
1118 return FRAME_INTERNAL_BORDER_WIDTH (f);
1120 x = (WINDOW_LEFT_EDGE_X (w)
1121 + window_box_left_offset (w, area));
1123 return x;
1127 /* Return the frame-relative coordinate of the right edge of display
1128 area AREA of window W. AREA < 0 means return the right edge of the
1129 whole window, to the left of the right fringe of W. */
1132 window_box_right (struct window *w, int area)
1134 return window_box_left (w, area) + window_box_width (w, area);
1137 /* Get the bounding box of the display area AREA of window W, without
1138 mode lines, in frame-relative coordinates. AREA < 0 means the
1139 whole window, not including the left and right fringes of
1140 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1141 coordinates of the upper-left corner of the box. Return in
1142 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1144 void
1145 window_box (struct window *w, int area, int *box_x, int *box_y,
1146 int *box_width, int *box_height)
1148 if (box_width)
1149 *box_width = window_box_width (w, area);
1150 if (box_height)
1151 *box_height = window_box_height (w);
1152 if (box_x)
1153 *box_x = window_box_left (w, area);
1154 if (box_y)
1156 *box_y = WINDOW_TOP_EDGE_Y (w);
1157 if (WINDOW_WANTS_HEADER_LINE_P (w))
1158 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1163 /* Get the bounding box of the display area AREA of window W, without
1164 mode lines. AREA < 0 means the whole window, not including the
1165 left and right fringe of the window. Return in *TOP_LEFT_X
1166 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1167 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1168 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1169 box. */
1171 static inline void
1172 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1173 int *bottom_right_x, int *bottom_right_y)
1175 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1176 bottom_right_y);
1177 *bottom_right_x += *top_left_x;
1178 *bottom_right_y += *top_left_y;
1183 /***********************************************************************
1184 Utilities
1185 ***********************************************************************/
1187 /* Return the bottom y-position of the line the iterator IT is in.
1188 This can modify IT's settings. */
1191 line_bottom_y (struct it *it)
1193 int line_height = it->max_ascent + it->max_descent;
1194 int line_top_y = it->current_y;
1196 if (line_height == 0)
1198 if (last_height)
1199 line_height = last_height;
1200 else if (IT_CHARPOS (*it) < ZV)
1202 move_it_by_lines (it, 1);
1203 line_height = (it->max_ascent || it->max_descent
1204 ? it->max_ascent + it->max_descent
1205 : last_height);
1207 else
1209 struct glyph_row *row = it->glyph_row;
1211 /* Use the default character height. */
1212 it->glyph_row = NULL;
1213 it->what = IT_CHARACTER;
1214 it->c = ' ';
1215 it->len = 1;
1216 PRODUCE_GLYPHS (it);
1217 line_height = it->ascent + it->descent;
1218 it->glyph_row = row;
1222 return line_top_y + line_height;
1225 /* Subroutine of pos_visible_p below. Extracts a display string, if
1226 any, from the display spec given as its argument. */
1227 static Lisp_Object
1228 string_from_display_spec (Lisp_Object spec)
1230 if (CONSP (spec))
1232 while (CONSP (spec))
1234 if (STRINGP (XCAR (spec)))
1235 return XCAR (spec);
1236 spec = XCDR (spec);
1239 else if (VECTORP (spec))
1241 ptrdiff_t i;
1243 for (i = 0; i < ASIZE (spec); i++)
1245 if (STRINGP (AREF (spec, i)))
1246 return AREF (spec, i);
1248 return Qnil;
1251 return spec;
1255 /* Limit insanely large values of W->hscroll on frame F to the largest
1256 value that will still prevent first_visible_x and last_visible_x of
1257 'struct it' from overflowing an int. */
1258 static inline int
1259 window_hscroll_limited (struct window *w, struct frame *f)
1261 ptrdiff_t window_hscroll = w->hscroll;
1262 int window_text_width = window_box_width (w, TEXT_AREA);
1263 int colwidth = FRAME_COLUMN_WIDTH (f);
1265 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1266 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1268 return window_hscroll;
1271 /* Return 1 if position CHARPOS is visible in window W.
1272 CHARPOS < 0 means return info about WINDOW_END position.
1273 If visible, set *X and *Y to pixel coordinates of top left corner.
1274 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1275 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1278 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1279 int *rtop, int *rbot, int *rowh, int *vpos)
1281 struct it it;
1282 void *itdata = bidi_shelve_cache ();
1283 struct text_pos top;
1284 int visible_p = 0;
1285 struct buffer *old_buffer = NULL;
1287 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1288 return visible_p;
1290 if (XBUFFER (w->buffer) != current_buffer)
1292 old_buffer = current_buffer;
1293 set_buffer_internal_1 (XBUFFER (w->buffer));
1296 SET_TEXT_POS_FROM_MARKER (top, w->start);
1297 /* Scrolling a minibuffer window via scroll bar when the echo area
1298 shows long text sometimes resets the minibuffer contents behind
1299 our backs. */
1300 if (CHARPOS (top) > ZV)
1301 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1303 /* Compute exact mode line heights. */
1304 if (WINDOW_WANTS_MODELINE_P (w))
1305 current_mode_line_height
1306 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1307 BVAR (current_buffer, mode_line_format));
1309 if (WINDOW_WANTS_HEADER_LINE_P (w))
1310 current_header_line_height
1311 = display_mode_line (w, HEADER_LINE_FACE_ID,
1312 BVAR (current_buffer, header_line_format));
1314 start_display (&it, w, top);
1315 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1316 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1318 if (charpos >= 0
1319 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1320 && IT_CHARPOS (it) >= charpos)
1321 /* When scanning backwards under bidi iteration, move_it_to
1322 stops at or _before_ CHARPOS, because it stops at or to
1323 the _right_ of the character at CHARPOS. */
1324 || (it.bidi_p && it.bidi_it.scan_dir == -1
1325 && IT_CHARPOS (it) <= charpos)))
1327 /* We have reached CHARPOS, or passed it. How the call to
1328 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1329 or covered by a display property, move_it_to stops at the end
1330 of the invisible text, to the right of CHARPOS. (ii) If
1331 CHARPOS is in a display vector, move_it_to stops on its last
1332 glyph. */
1333 int top_x = it.current_x;
1334 int top_y = it.current_y;
1335 /* Calling line_bottom_y may change it.method, it.position, etc. */
1336 enum it_method it_method = it.method;
1337 int bottom_y = (last_height = 0, line_bottom_y (&it));
1338 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1340 if (top_y < window_top_y)
1341 visible_p = bottom_y > window_top_y;
1342 else if (top_y < it.last_visible_y)
1343 visible_p = 1;
1344 if (bottom_y >= it.last_visible_y
1345 && it.bidi_p && it.bidi_it.scan_dir == -1
1346 && IT_CHARPOS (it) < charpos)
1348 /* When the last line of the window is scanned backwards
1349 under bidi iteration, we could be duped into thinking
1350 that we have passed CHARPOS, when in fact move_it_to
1351 simply stopped short of CHARPOS because it reached
1352 last_visible_y. To see if that's what happened, we call
1353 move_it_to again with a slightly larger vertical limit,
1354 and see if it actually moved vertically; if it did, we
1355 didn't really reach CHARPOS, which is beyond window end. */
1356 struct it save_it = it;
1357 /* Why 10? because we don't know how many canonical lines
1358 will the height of the next line(s) be. So we guess. */
1359 int ten_more_lines =
1360 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1362 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1363 MOVE_TO_POS | MOVE_TO_Y);
1364 if (it.current_y > top_y)
1365 visible_p = 0;
1367 it = save_it;
1369 if (visible_p)
1371 if (it_method == GET_FROM_DISPLAY_VECTOR)
1373 /* We stopped on the last glyph of a display vector.
1374 Try and recompute. Hack alert! */
1375 if (charpos < 2 || top.charpos >= charpos)
1376 top_x = it.glyph_row->x;
1377 else
1379 struct it it2;
1380 start_display (&it2, w, top);
1381 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1382 get_next_display_element (&it2);
1383 PRODUCE_GLYPHS (&it2);
1384 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1385 || it2.current_x > it2.last_visible_x)
1386 top_x = it.glyph_row->x;
1387 else
1389 top_x = it2.current_x;
1390 top_y = it2.current_y;
1394 else if (IT_CHARPOS (it) != charpos)
1396 Lisp_Object cpos = make_number (charpos);
1397 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1398 Lisp_Object string = string_from_display_spec (spec);
1399 int newline_in_string = 0;
1401 if (STRINGP (string))
1403 const char *s = SSDATA (string);
1404 const char *e = s + SBYTES (string);
1405 while (s < e)
1407 if (*s++ == '\n')
1409 newline_in_string = 1;
1410 break;
1414 /* The tricky code below is needed because there's a
1415 discrepancy between move_it_to and how we set cursor
1416 when the display line ends in a newline from a
1417 display string. move_it_to will stop _after_ such
1418 display strings, whereas set_cursor_from_row
1419 conspires with cursor_row_p to place the cursor on
1420 the first glyph produced from the display string. */
1422 /* We have overshoot PT because it is covered by a
1423 display property whose value is a string. If the
1424 string includes embedded newlines, we are also in the
1425 wrong display line. Backtrack to the correct line,
1426 where the display string begins. */
1427 if (newline_in_string)
1429 Lisp_Object startpos, endpos;
1430 EMACS_INT start, end;
1431 struct it it3;
1432 int it3_moved;
1434 /* Find the first and the last buffer positions
1435 covered by the display string. */
1436 endpos =
1437 Fnext_single_char_property_change (cpos, Qdisplay,
1438 Qnil, Qnil);
1439 startpos =
1440 Fprevious_single_char_property_change (endpos, Qdisplay,
1441 Qnil, Qnil);
1442 start = XFASTINT (startpos);
1443 end = XFASTINT (endpos);
1444 /* Move to the last buffer position before the
1445 display property. */
1446 start_display (&it3, w, top);
1447 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1448 /* Move forward one more line if the position before
1449 the display string is a newline or if it is the
1450 rightmost character on a line that is
1451 continued or word-wrapped. */
1452 if (it3.method == GET_FROM_BUFFER
1453 && it3.c == '\n')
1454 move_it_by_lines (&it3, 1);
1455 else if (move_it_in_display_line_to (&it3, -1,
1456 it3.current_x
1457 + it3.pixel_width,
1458 MOVE_TO_X)
1459 == MOVE_LINE_CONTINUED)
1461 move_it_by_lines (&it3, 1);
1462 /* When we are under word-wrap, the #$@%!
1463 move_it_by_lines moves 2 lines, so we need to
1464 fix that up. */
1465 if (it3.line_wrap == WORD_WRAP)
1466 move_it_by_lines (&it3, -1);
1469 /* Record the vertical coordinate of the display
1470 line where we wound up. */
1471 top_y = it3.current_y;
1472 if (it3.bidi_p)
1474 /* When characters are reordered for display,
1475 the character displayed to the left of the
1476 display string could be _after_ the display
1477 property in the logical order. Use the
1478 smallest vertical position of these two. */
1479 start_display (&it3, w, top);
1480 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1481 if (it3.current_y < top_y)
1482 top_y = it3.current_y;
1484 /* Move from the top of the window to the beginning
1485 of the display line where the display string
1486 begins. */
1487 start_display (&it3, w, top);
1488 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1489 /* If it3_moved stays zero after the 'while' loop
1490 below, that means we already were at a newline
1491 before the loop (e.g., the display string begins
1492 with a newline), so we don't need to (and cannot)
1493 inspect the glyphs of it3.glyph_row, because
1494 PRODUCE_GLYPHS will not produce anything for a
1495 newline, and thus it3.glyph_row stays at its
1496 stale content it got at top of the window. */
1497 it3_moved = 0;
1498 /* Finally, advance the iterator until we hit the
1499 first display element whose character position is
1500 CHARPOS, or until the first newline from the
1501 display string, which signals the end of the
1502 display line. */
1503 while (get_next_display_element (&it3))
1505 PRODUCE_GLYPHS (&it3);
1506 if (IT_CHARPOS (it3) == charpos
1507 || ITERATOR_AT_END_OF_LINE_P (&it3))
1508 break;
1509 it3_moved = 1;
1510 set_iterator_to_next (&it3, 0);
1512 top_x = it3.current_x - it3.pixel_width;
1513 /* Normally, we would exit the above loop because we
1514 found the display element whose character
1515 position is CHARPOS. For the contingency that we
1516 didn't, and stopped at the first newline from the
1517 display string, move back over the glyphs
1518 produced from the string, until we find the
1519 rightmost glyph not from the string. */
1520 if (it3_moved
1521 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1523 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1524 + it3.glyph_row->used[TEXT_AREA];
1526 while (EQ ((g - 1)->object, string))
1528 --g;
1529 top_x -= g->pixel_width;
1531 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1532 + it3.glyph_row->used[TEXT_AREA]);
1537 *x = top_x;
1538 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1539 *rtop = max (0, window_top_y - top_y);
1540 *rbot = max (0, bottom_y - it.last_visible_y);
1541 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1542 - max (top_y, window_top_y)));
1543 *vpos = it.vpos;
1546 else
1548 /* We were asked to provide info about WINDOW_END. */
1549 struct it it2;
1550 void *it2data = NULL;
1552 SAVE_IT (it2, it, it2data);
1553 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1554 move_it_by_lines (&it, 1);
1555 if (charpos < IT_CHARPOS (it)
1556 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1558 visible_p = 1;
1559 RESTORE_IT (&it2, &it2, it2data);
1560 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1561 *x = it2.current_x;
1562 *y = it2.current_y + it2.max_ascent - it2.ascent;
1563 *rtop = max (0, -it2.current_y);
1564 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1565 - it.last_visible_y));
1566 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1567 it.last_visible_y)
1568 - max (it2.current_y,
1569 WINDOW_HEADER_LINE_HEIGHT (w))));
1570 *vpos = it2.vpos;
1572 else
1573 bidi_unshelve_cache (it2data, 1);
1575 bidi_unshelve_cache (itdata, 0);
1577 if (old_buffer)
1578 set_buffer_internal_1 (old_buffer);
1580 current_header_line_height = current_mode_line_height = -1;
1582 if (visible_p && w->hscroll > 0)
1583 *x -=
1584 window_hscroll_limited (w, WINDOW_XFRAME (w))
1585 * WINDOW_FRAME_COLUMN_WIDTH (w);
1587 #if 0
1588 /* Debugging code. */
1589 if (visible_p)
1590 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1591 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1592 else
1593 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1594 #endif
1596 return visible_p;
1600 /* Return the next character from STR. Return in *LEN the length of
1601 the character. This is like STRING_CHAR_AND_LENGTH but never
1602 returns an invalid character. If we find one, we return a `?', but
1603 with the length of the invalid character. */
1605 static inline int
1606 string_char_and_length (const unsigned char *str, int *len)
1608 int c;
1610 c = STRING_CHAR_AND_LENGTH (str, *len);
1611 if (!CHAR_VALID_P (c))
1612 /* We may not change the length here because other places in Emacs
1613 don't use this function, i.e. they silently accept invalid
1614 characters. */
1615 c = '?';
1617 return c;
1622 /* Given a position POS containing a valid character and byte position
1623 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1625 static struct text_pos
1626 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1628 eassert (STRINGP (string) && nchars >= 0);
1630 if (STRING_MULTIBYTE (string))
1632 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1633 int len;
1635 while (nchars--)
1637 string_char_and_length (p, &len);
1638 p += len;
1639 CHARPOS (pos) += 1;
1640 BYTEPOS (pos) += len;
1643 else
1644 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1646 return pos;
1650 /* Value is the text position, i.e. character and byte position,
1651 for character position CHARPOS in STRING. */
1653 static inline struct text_pos
1654 string_pos (ptrdiff_t charpos, Lisp_Object string)
1656 struct text_pos pos;
1657 eassert (STRINGP (string));
1658 eassert (charpos >= 0);
1659 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1660 return pos;
1664 /* Value is a text position, i.e. character and byte position, for
1665 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1666 means recognize multibyte characters. */
1668 static struct text_pos
1669 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1671 struct text_pos pos;
1673 eassert (s != NULL);
1674 eassert (charpos >= 0);
1676 if (multibyte_p)
1678 int len;
1680 SET_TEXT_POS (pos, 0, 0);
1681 while (charpos--)
1683 string_char_and_length ((const unsigned char *) s, &len);
1684 s += len;
1685 CHARPOS (pos) += 1;
1686 BYTEPOS (pos) += len;
1689 else
1690 SET_TEXT_POS (pos, charpos, charpos);
1692 return pos;
1696 /* Value is the number of characters in C string S. MULTIBYTE_P
1697 non-zero means recognize multibyte characters. */
1699 static ptrdiff_t
1700 number_of_chars (const char *s, int multibyte_p)
1702 ptrdiff_t nchars;
1704 if (multibyte_p)
1706 ptrdiff_t rest = strlen (s);
1707 int len;
1708 const unsigned char *p = (const unsigned char *) s;
1710 for (nchars = 0; rest > 0; ++nchars)
1712 string_char_and_length (p, &len);
1713 rest -= len, p += len;
1716 else
1717 nchars = strlen (s);
1719 return nchars;
1723 /* Compute byte position NEWPOS->bytepos corresponding to
1724 NEWPOS->charpos. POS is a known position in string STRING.
1725 NEWPOS->charpos must be >= POS.charpos. */
1727 static void
1728 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1730 eassert (STRINGP (string));
1731 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1733 if (STRING_MULTIBYTE (string))
1734 *newpos = string_pos_nchars_ahead (pos, string,
1735 CHARPOS (*newpos) - CHARPOS (pos));
1736 else
1737 BYTEPOS (*newpos) = CHARPOS (*newpos);
1740 /* EXPORT:
1741 Return an estimation of the pixel height of mode or header lines on
1742 frame F. FACE_ID specifies what line's height to estimate. */
1745 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1747 #ifdef HAVE_WINDOW_SYSTEM
1748 if (FRAME_WINDOW_P (f))
1750 int height = FONT_HEIGHT (FRAME_FONT (f));
1752 /* This function is called so early when Emacs starts that the face
1753 cache and mode line face are not yet initialized. */
1754 if (FRAME_FACE_CACHE (f))
1756 struct face *face = FACE_FROM_ID (f, face_id);
1757 if (face)
1759 if (face->font)
1760 height = FONT_HEIGHT (face->font);
1761 if (face->box_line_width > 0)
1762 height += 2 * face->box_line_width;
1766 return height;
1768 #endif
1770 return 1;
1773 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1774 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1775 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1776 not force the value into range. */
1778 void
1779 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1780 int *x, int *y, NativeRectangle *bounds, int noclip)
1783 #ifdef HAVE_WINDOW_SYSTEM
1784 if (FRAME_WINDOW_P (f))
1786 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1787 even for negative values. */
1788 if (pix_x < 0)
1789 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1790 if (pix_y < 0)
1791 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1793 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1794 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1796 if (bounds)
1797 STORE_NATIVE_RECT (*bounds,
1798 FRAME_COL_TO_PIXEL_X (f, pix_x),
1799 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1800 FRAME_COLUMN_WIDTH (f) - 1,
1801 FRAME_LINE_HEIGHT (f) - 1);
1803 if (!noclip)
1805 if (pix_x < 0)
1806 pix_x = 0;
1807 else if (pix_x > FRAME_TOTAL_COLS (f))
1808 pix_x = FRAME_TOTAL_COLS (f);
1810 if (pix_y < 0)
1811 pix_y = 0;
1812 else if (pix_y > FRAME_LINES (f))
1813 pix_y = FRAME_LINES (f);
1816 #endif
1818 *x = pix_x;
1819 *y = pix_y;
1823 /* Find the glyph under window-relative coordinates X/Y in window W.
1824 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1825 strings. Return in *HPOS and *VPOS the row and column number of
1826 the glyph found. Return in *AREA the glyph area containing X.
1827 Value is a pointer to the glyph found or null if X/Y is not on
1828 text, or we can't tell because W's current matrix is not up to
1829 date. */
1831 static
1832 struct glyph *
1833 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1834 int *dx, int *dy, int *area)
1836 struct glyph *glyph, *end;
1837 struct glyph_row *row = NULL;
1838 int x0, i;
1840 /* Find row containing Y. Give up if some row is not enabled. */
1841 for (i = 0; i < w->current_matrix->nrows; ++i)
1843 row = MATRIX_ROW (w->current_matrix, i);
1844 if (!row->enabled_p)
1845 return NULL;
1846 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1847 break;
1850 *vpos = i;
1851 *hpos = 0;
1853 /* Give up if Y is not in the window. */
1854 if (i == w->current_matrix->nrows)
1855 return NULL;
1857 /* Get the glyph area containing X. */
1858 if (w->pseudo_window_p)
1860 *area = TEXT_AREA;
1861 x0 = 0;
1863 else
1865 if (x < window_box_left_offset (w, TEXT_AREA))
1867 *area = LEFT_MARGIN_AREA;
1868 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1870 else if (x < window_box_right_offset (w, TEXT_AREA))
1872 *area = TEXT_AREA;
1873 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1875 else
1877 *area = RIGHT_MARGIN_AREA;
1878 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1882 /* Find glyph containing X. */
1883 glyph = row->glyphs[*area];
1884 end = glyph + row->used[*area];
1885 x -= x0;
1886 while (glyph < end && x >= glyph->pixel_width)
1888 x -= glyph->pixel_width;
1889 ++glyph;
1892 if (glyph == end)
1893 return NULL;
1895 if (dx)
1897 *dx = x;
1898 *dy = y - (row->y + row->ascent - glyph->ascent);
1901 *hpos = glyph - row->glyphs[*area];
1902 return glyph;
1905 /* Convert frame-relative x/y to coordinates relative to window W.
1906 Takes pseudo-windows into account. */
1908 static void
1909 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1911 if (w->pseudo_window_p)
1913 /* A pseudo-window is always full-width, and starts at the
1914 left edge of the frame, plus a frame border. */
1915 struct frame *f = XFRAME (w->frame);
1916 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1917 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1919 else
1921 *x -= WINDOW_LEFT_EDGE_X (w);
1922 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1926 #ifdef HAVE_WINDOW_SYSTEM
1928 /* EXPORT:
1929 Return in RECTS[] at most N clipping rectangles for glyph string S.
1930 Return the number of stored rectangles. */
1933 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1935 XRectangle r;
1937 if (n <= 0)
1938 return 0;
1940 if (s->row->full_width_p)
1942 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1943 r.x = WINDOW_LEFT_EDGE_X (s->w);
1944 r.width = WINDOW_TOTAL_WIDTH (s->w);
1946 /* Unless displaying a mode or menu bar line, which are always
1947 fully visible, clip to the visible part of the row. */
1948 if (s->w->pseudo_window_p)
1949 r.height = s->row->visible_height;
1950 else
1951 r.height = s->height;
1953 else
1955 /* This is a text line that may be partially visible. */
1956 r.x = window_box_left (s->w, s->area);
1957 r.width = window_box_width (s->w, s->area);
1958 r.height = s->row->visible_height;
1961 if (s->clip_head)
1962 if (r.x < s->clip_head->x)
1964 if (r.width >= s->clip_head->x - r.x)
1965 r.width -= s->clip_head->x - r.x;
1966 else
1967 r.width = 0;
1968 r.x = s->clip_head->x;
1970 if (s->clip_tail)
1971 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1973 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1974 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1975 else
1976 r.width = 0;
1979 /* If S draws overlapping rows, it's sufficient to use the top and
1980 bottom of the window for clipping because this glyph string
1981 intentionally draws over other lines. */
1982 if (s->for_overlaps)
1984 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1985 r.height = window_text_bottom_y (s->w) - r.y;
1987 /* Alas, the above simple strategy does not work for the
1988 environments with anti-aliased text: if the same text is
1989 drawn onto the same place multiple times, it gets thicker.
1990 If the overlap we are processing is for the erased cursor, we
1991 take the intersection with the rectangle of the cursor. */
1992 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1994 XRectangle rc, r_save = r;
1996 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1997 rc.y = s->w->phys_cursor.y;
1998 rc.width = s->w->phys_cursor_width;
1999 rc.height = s->w->phys_cursor_height;
2001 x_intersect_rectangles (&r_save, &rc, &r);
2004 else
2006 /* Don't use S->y for clipping because it doesn't take partially
2007 visible lines into account. For example, it can be negative for
2008 partially visible lines at the top of a window. */
2009 if (!s->row->full_width_p
2010 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2011 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2012 else
2013 r.y = max (0, s->row->y);
2016 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2018 /* If drawing the cursor, don't let glyph draw outside its
2019 advertised boundaries. Cleartype does this under some circumstances. */
2020 if (s->hl == DRAW_CURSOR)
2022 struct glyph *glyph = s->first_glyph;
2023 int height, max_y;
2025 if (s->x > r.x)
2027 r.width -= s->x - r.x;
2028 r.x = s->x;
2030 r.width = min (r.width, glyph->pixel_width);
2032 /* If r.y is below window bottom, ensure that we still see a cursor. */
2033 height = min (glyph->ascent + glyph->descent,
2034 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2035 max_y = window_text_bottom_y (s->w) - height;
2036 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2037 if (s->ybase - glyph->ascent > max_y)
2039 r.y = max_y;
2040 r.height = height;
2042 else
2044 /* Don't draw cursor glyph taller than our actual glyph. */
2045 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2046 if (height < r.height)
2048 max_y = r.y + r.height;
2049 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2050 r.height = min (max_y - r.y, height);
2055 if (s->row->clip)
2057 XRectangle r_save = r;
2059 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2060 r.width = 0;
2063 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2064 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2066 #ifdef CONVERT_FROM_XRECT
2067 CONVERT_FROM_XRECT (r, *rects);
2068 #else
2069 *rects = r;
2070 #endif
2071 return 1;
2073 else
2075 /* If we are processing overlapping and allowed to return
2076 multiple clipping rectangles, we exclude the row of the glyph
2077 string from the clipping rectangle. This is to avoid drawing
2078 the same text on the environment with anti-aliasing. */
2079 #ifdef CONVERT_FROM_XRECT
2080 XRectangle rs[2];
2081 #else
2082 XRectangle *rs = rects;
2083 #endif
2084 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2086 if (s->for_overlaps & OVERLAPS_PRED)
2088 rs[i] = r;
2089 if (r.y + r.height > row_y)
2091 if (r.y < row_y)
2092 rs[i].height = row_y - r.y;
2093 else
2094 rs[i].height = 0;
2096 i++;
2098 if (s->for_overlaps & OVERLAPS_SUCC)
2100 rs[i] = r;
2101 if (r.y < row_y + s->row->visible_height)
2103 if (r.y + r.height > row_y + s->row->visible_height)
2105 rs[i].y = row_y + s->row->visible_height;
2106 rs[i].height = r.y + r.height - rs[i].y;
2108 else
2109 rs[i].height = 0;
2111 i++;
2114 n = i;
2115 #ifdef CONVERT_FROM_XRECT
2116 for (i = 0; i < n; i++)
2117 CONVERT_FROM_XRECT (rs[i], rects[i]);
2118 #endif
2119 return n;
2123 /* EXPORT:
2124 Return in *NR the clipping rectangle for glyph string S. */
2126 void
2127 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2129 get_glyph_string_clip_rects (s, nr, 1);
2133 /* EXPORT:
2134 Return the position and height of the phys cursor in window W.
2135 Set w->phys_cursor_width to width of phys cursor.
2138 void
2139 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2140 struct glyph *glyph, int *xp, int *yp, int *heightp)
2142 struct frame *f = XFRAME (WINDOW_FRAME (w));
2143 int x, y, wd, h, h0, y0;
2145 /* Compute the width of the rectangle to draw. If on a stretch
2146 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2147 rectangle as wide as the glyph, but use a canonical character
2148 width instead. */
2149 wd = glyph->pixel_width - 1;
2150 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2151 wd++; /* Why? */
2152 #endif
2154 x = w->phys_cursor.x;
2155 if (x < 0)
2157 wd += x;
2158 x = 0;
2161 if (glyph->type == STRETCH_GLYPH
2162 && !x_stretch_cursor_p)
2163 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2164 w->phys_cursor_width = wd;
2166 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2168 /* If y is below window bottom, ensure that we still see a cursor. */
2169 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2171 h = max (h0, glyph->ascent + glyph->descent);
2172 h0 = min (h0, glyph->ascent + glyph->descent);
2174 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2175 if (y < y0)
2177 h = max (h - (y0 - y) + 1, h0);
2178 y = y0 - 1;
2180 else
2182 y0 = window_text_bottom_y (w) - h0;
2183 if (y > y0)
2185 h += y - y0;
2186 y = y0;
2190 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2191 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2192 *heightp = h;
2196 * Remember which glyph the mouse is over.
2199 void
2200 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2202 Lisp_Object window;
2203 struct window *w;
2204 struct glyph_row *r, *gr, *end_row;
2205 enum window_part part;
2206 enum glyph_row_area area;
2207 int x, y, width, height;
2209 /* Try to determine frame pixel position and size of the glyph under
2210 frame pixel coordinates X/Y on frame F. */
2212 if (!f->glyphs_initialized_p
2213 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2214 NILP (window)))
2216 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2217 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2218 goto virtual_glyph;
2221 w = XWINDOW (window);
2222 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2223 height = WINDOW_FRAME_LINE_HEIGHT (w);
2225 x = window_relative_x_coord (w, part, gx);
2226 y = gy - WINDOW_TOP_EDGE_Y (w);
2228 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2229 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2231 if (w->pseudo_window_p)
2233 area = TEXT_AREA;
2234 part = ON_MODE_LINE; /* Don't adjust margin. */
2235 goto text_glyph;
2238 switch (part)
2240 case ON_LEFT_MARGIN:
2241 area = LEFT_MARGIN_AREA;
2242 goto text_glyph;
2244 case ON_RIGHT_MARGIN:
2245 area = RIGHT_MARGIN_AREA;
2246 goto text_glyph;
2248 case ON_HEADER_LINE:
2249 case ON_MODE_LINE:
2250 gr = (part == ON_HEADER_LINE
2251 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2252 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2253 gy = gr->y;
2254 area = TEXT_AREA;
2255 goto text_glyph_row_found;
2257 case ON_TEXT:
2258 area = TEXT_AREA;
2260 text_glyph:
2261 gr = 0; gy = 0;
2262 for (; r <= end_row && r->enabled_p; ++r)
2263 if (r->y + r->height > y)
2265 gr = r; gy = r->y;
2266 break;
2269 text_glyph_row_found:
2270 if (gr && gy <= y)
2272 struct glyph *g = gr->glyphs[area];
2273 struct glyph *end = g + gr->used[area];
2275 height = gr->height;
2276 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2277 if (gx + g->pixel_width > x)
2278 break;
2280 if (g < end)
2282 if (g->type == IMAGE_GLYPH)
2284 /* Don't remember when mouse is over image, as
2285 image may have hot-spots. */
2286 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2287 return;
2289 width = g->pixel_width;
2291 else
2293 /* Use nominal char spacing at end of line. */
2294 x -= gx;
2295 gx += (x / width) * width;
2298 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2299 gx += window_box_left_offset (w, area);
2301 else
2303 /* Use nominal line height at end of window. */
2304 gx = (x / width) * width;
2305 y -= gy;
2306 gy += (y / height) * height;
2308 break;
2310 case ON_LEFT_FRINGE:
2311 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2312 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2313 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2314 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2315 goto row_glyph;
2317 case ON_RIGHT_FRINGE:
2318 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2319 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2320 : window_box_right_offset (w, TEXT_AREA));
2321 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2322 goto row_glyph;
2324 case ON_SCROLL_BAR:
2325 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2327 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2328 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2329 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2330 : 0)));
2331 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2333 row_glyph:
2334 gr = 0, gy = 0;
2335 for (; r <= end_row && r->enabled_p; ++r)
2336 if (r->y + r->height > y)
2338 gr = r; gy = r->y;
2339 break;
2342 if (gr && gy <= y)
2343 height = gr->height;
2344 else
2346 /* Use nominal line height at end of window. */
2347 y -= gy;
2348 gy += (y / height) * height;
2350 break;
2352 default:
2354 virtual_glyph:
2355 /* If there is no glyph under the mouse, then we divide the screen
2356 into a grid of the smallest glyph in the frame, and use that
2357 as our "glyph". */
2359 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2360 round down even for negative values. */
2361 if (gx < 0)
2362 gx -= width - 1;
2363 if (gy < 0)
2364 gy -= height - 1;
2366 gx = (gx / width) * width;
2367 gy = (gy / height) * height;
2369 goto store_rect;
2372 gx += WINDOW_LEFT_EDGE_X (w);
2373 gy += WINDOW_TOP_EDGE_Y (w);
2375 store_rect:
2376 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2378 /* Visible feedback for debugging. */
2379 #if 0
2380 #if HAVE_X_WINDOWS
2381 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2382 f->output_data.x->normal_gc,
2383 gx, gy, width, height);
2384 #endif
2385 #endif
2389 #endif /* HAVE_WINDOW_SYSTEM */
2392 /***********************************************************************
2393 Lisp form evaluation
2394 ***********************************************************************/
2396 /* Error handler for safe_eval and safe_call. */
2398 static Lisp_Object
2399 safe_eval_handler (Lisp_Object arg)
2401 add_to_log ("Error during redisplay: %S", arg, Qnil);
2402 return Qnil;
2406 /* Evaluate SEXPR and return the result, or nil if something went
2407 wrong. Prevent redisplay during the evaluation. */
2409 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2410 Return the result, or nil if something went wrong. Prevent
2411 redisplay during the evaluation. */
2413 Lisp_Object
2414 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2416 Lisp_Object val;
2418 if (inhibit_eval_during_redisplay)
2419 val = Qnil;
2420 else
2422 ptrdiff_t count = SPECPDL_INDEX ();
2423 struct gcpro gcpro1;
2425 GCPRO1 (args[0]);
2426 gcpro1.nvars = nargs;
2427 specbind (Qinhibit_redisplay, Qt);
2428 /* Use Qt to ensure debugger does not run,
2429 so there is no possibility of wanting to redisplay. */
2430 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2431 safe_eval_handler);
2432 UNGCPRO;
2433 val = unbind_to (count, val);
2436 return val;
2440 /* Call function FN with one argument ARG.
2441 Return the result, or nil if something went wrong. */
2443 Lisp_Object
2444 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2446 Lisp_Object args[2];
2447 args[0] = fn;
2448 args[1] = arg;
2449 return safe_call (2, args);
2452 static Lisp_Object Qeval;
2454 Lisp_Object
2455 safe_eval (Lisp_Object sexpr)
2457 return safe_call1 (Qeval, sexpr);
2460 /* Call function FN with one argument ARG.
2461 Return the result, or nil if something went wrong. */
2463 Lisp_Object
2464 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2466 Lisp_Object args[3];
2467 args[0] = fn;
2468 args[1] = arg1;
2469 args[2] = arg2;
2470 return safe_call (3, args);
2475 /***********************************************************************
2476 Debugging
2477 ***********************************************************************/
2479 #if 0
2481 /* Define CHECK_IT to perform sanity checks on iterators.
2482 This is for debugging. It is too slow to do unconditionally. */
2484 static void
2485 check_it (struct it *it)
2487 if (it->method == GET_FROM_STRING)
2489 eassert (STRINGP (it->string));
2490 eassert (IT_STRING_CHARPOS (*it) >= 0);
2492 else
2494 eassert (IT_STRING_CHARPOS (*it) < 0);
2495 if (it->method == GET_FROM_BUFFER)
2497 /* Check that character and byte positions agree. */
2498 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2502 if (it->dpvec)
2503 eassert (it->current.dpvec_index >= 0);
2504 else
2505 eassert (it->current.dpvec_index < 0);
2508 #define CHECK_IT(IT) check_it ((IT))
2510 #else /* not 0 */
2512 #define CHECK_IT(IT) (void) 0
2514 #endif /* not 0 */
2517 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2519 /* Check that the window end of window W is what we expect it
2520 to be---the last row in the current matrix displaying text. */
2522 static void
2523 check_window_end (struct window *w)
2525 if (!MINI_WINDOW_P (w)
2526 && !NILP (w->window_end_valid))
2528 struct glyph_row *row;
2529 eassert ((row = MATRIX_ROW (w->current_matrix,
2530 XFASTINT (w->window_end_vpos)),
2531 !row->enabled_p
2532 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2533 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2537 #define CHECK_WINDOW_END(W) check_window_end ((W))
2539 #else
2541 #define CHECK_WINDOW_END(W) (void) 0
2543 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2547 /***********************************************************************
2548 Iterator initialization
2549 ***********************************************************************/
2551 /* Initialize IT for displaying current_buffer in window W, starting
2552 at character position CHARPOS. CHARPOS < 0 means that no buffer
2553 position is specified which is useful when the iterator is assigned
2554 a position later. BYTEPOS is the byte position corresponding to
2555 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2557 If ROW is not null, calls to produce_glyphs with IT as parameter
2558 will produce glyphs in that row.
2560 BASE_FACE_ID is the id of a base face to use. It must be one of
2561 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2562 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2563 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2565 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2566 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2567 will be initialized to use the corresponding mode line glyph row of
2568 the desired matrix of W. */
2570 void
2571 init_iterator (struct it *it, struct window *w,
2572 ptrdiff_t charpos, ptrdiff_t bytepos,
2573 struct glyph_row *row, enum face_id base_face_id)
2575 int highlight_region_p;
2576 enum face_id remapped_base_face_id = base_face_id;
2578 /* Some precondition checks. */
2579 eassert (w != NULL && it != NULL);
2580 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2581 && charpos <= ZV));
2583 /* If face attributes have been changed since the last redisplay,
2584 free realized faces now because they depend on face definitions
2585 that might have changed. Don't free faces while there might be
2586 desired matrices pending which reference these faces. */
2587 if (face_change_count && !inhibit_free_realized_faces)
2589 face_change_count = 0;
2590 free_all_realized_faces (Qnil);
2593 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2594 if (! NILP (Vface_remapping_alist))
2595 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2597 /* Use one of the mode line rows of W's desired matrix if
2598 appropriate. */
2599 if (row == NULL)
2601 if (base_face_id == MODE_LINE_FACE_ID
2602 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2603 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2604 else if (base_face_id == HEADER_LINE_FACE_ID)
2605 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2608 /* Clear IT. */
2609 memset (it, 0, sizeof *it);
2610 it->current.overlay_string_index = -1;
2611 it->current.dpvec_index = -1;
2612 it->base_face_id = remapped_base_face_id;
2613 it->string = Qnil;
2614 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2615 it->paragraph_embedding = L2R;
2616 it->bidi_it.string.lstring = Qnil;
2617 it->bidi_it.string.s = NULL;
2618 it->bidi_it.string.bufpos = 0;
2620 /* The window in which we iterate over current_buffer: */
2621 XSETWINDOW (it->window, w);
2622 it->w = w;
2623 it->f = XFRAME (w->frame);
2625 it->cmp_it.id = -1;
2627 /* Extra space between lines (on window systems only). */
2628 if (base_face_id == DEFAULT_FACE_ID
2629 && FRAME_WINDOW_P (it->f))
2631 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2632 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2633 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2634 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2635 * FRAME_LINE_HEIGHT (it->f));
2636 else if (it->f->extra_line_spacing > 0)
2637 it->extra_line_spacing = it->f->extra_line_spacing;
2638 it->max_extra_line_spacing = 0;
2641 /* If realized faces have been removed, e.g. because of face
2642 attribute changes of named faces, recompute them. When running
2643 in batch mode, the face cache of the initial frame is null. If
2644 we happen to get called, make a dummy face cache. */
2645 if (FRAME_FACE_CACHE (it->f) == NULL)
2646 init_frame_faces (it->f);
2647 if (FRAME_FACE_CACHE (it->f)->used == 0)
2648 recompute_basic_faces (it->f);
2650 /* Current value of the `slice', `space-width', and 'height' properties. */
2651 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2652 it->space_width = Qnil;
2653 it->font_height = Qnil;
2654 it->override_ascent = -1;
2656 /* Are control characters displayed as `^C'? */
2657 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2659 /* -1 means everything between a CR and the following line end
2660 is invisible. >0 means lines indented more than this value are
2661 invisible. */
2662 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2663 ? clip_to_bounds (-1, XINT (BVAR (current_buffer,
2664 selective_display)),
2665 PTRDIFF_MAX)
2666 : (!NILP (BVAR (current_buffer, selective_display))
2667 ? -1 : 0));
2668 it->selective_display_ellipsis_p
2669 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2671 /* Display table to use. */
2672 it->dp = window_display_table (w);
2674 /* Are multibyte characters enabled in current_buffer? */
2675 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2677 /* Non-zero if we should highlight the region. */
2678 highlight_region_p
2679 = (!NILP (Vtransient_mark_mode)
2680 && !NILP (BVAR (current_buffer, mark_active))
2681 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2683 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2684 start and end of a visible region in window IT->w. Set both to
2685 -1 to indicate no region. */
2686 if (highlight_region_p
2687 /* Maybe highlight only in selected window. */
2688 && (/* Either show region everywhere. */
2689 highlight_nonselected_windows
2690 /* Or show region in the selected window. */
2691 || w == XWINDOW (selected_window)
2692 /* Or show the region if we are in the mini-buffer and W is
2693 the window the mini-buffer refers to. */
2694 || (MINI_WINDOW_P (XWINDOW (selected_window))
2695 && WINDOWP (minibuf_selected_window)
2696 && w == XWINDOW (minibuf_selected_window))))
2698 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2699 it->region_beg_charpos = min (PT, markpos);
2700 it->region_end_charpos = max (PT, markpos);
2702 else
2703 it->region_beg_charpos = it->region_end_charpos = -1;
2705 /* Get the position at which the redisplay_end_trigger hook should
2706 be run, if it is to be run at all. */
2707 if (MARKERP (w->redisplay_end_trigger)
2708 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2709 it->redisplay_end_trigger_charpos
2710 = marker_position (w->redisplay_end_trigger);
2711 else if (INTEGERP (w->redisplay_end_trigger))
2712 it->redisplay_end_trigger_charpos =
2713 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2715 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2717 /* Are lines in the display truncated? */
2718 if (base_face_id != DEFAULT_FACE_ID
2719 || it->w->hscroll
2720 || (! WINDOW_FULL_WIDTH_P (it->w)
2721 && ((!NILP (Vtruncate_partial_width_windows)
2722 && !INTEGERP (Vtruncate_partial_width_windows))
2723 || (INTEGERP (Vtruncate_partial_width_windows)
2724 && (WINDOW_TOTAL_COLS (it->w)
2725 < XINT (Vtruncate_partial_width_windows))))))
2726 it->line_wrap = TRUNCATE;
2727 else if (NILP (BVAR (current_buffer, truncate_lines)))
2728 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2729 ? WINDOW_WRAP : WORD_WRAP;
2730 else
2731 it->line_wrap = TRUNCATE;
2733 /* Get dimensions of truncation and continuation glyphs. These are
2734 displayed as fringe bitmaps under X, but we need them for such
2735 frames when the fringes are turned off. */
2736 if (it->line_wrap == TRUNCATE)
2738 /* We will need the truncation glyph. */
2739 eassert (it->glyph_row == NULL);
2740 produce_special_glyphs (it, IT_TRUNCATION);
2741 it->truncation_pixel_width = it->pixel_width;
2743 else
2745 /* We will need the continuation glyph. */
2746 eassert (it->glyph_row == NULL);
2747 produce_special_glyphs (it, IT_CONTINUATION);
2748 it->continuation_pixel_width = it->pixel_width;
2751 /* Reset these values to zero because the produce_special_glyphs
2752 above has changed them. */
2753 it->pixel_width = it->ascent = it->descent = 0;
2754 it->phys_ascent = it->phys_descent = 0;
2756 /* Set this after getting the dimensions of truncation and
2757 continuation glyphs, so that we don't produce glyphs when calling
2758 produce_special_glyphs, above. */
2759 it->glyph_row = row;
2760 it->area = TEXT_AREA;
2762 /* Forget any previous info about this row being reversed. */
2763 if (it->glyph_row)
2764 it->glyph_row->reversed_p = 0;
2766 /* Get the dimensions of the display area. The display area
2767 consists of the visible window area plus a horizontally scrolled
2768 part to the left of the window. All x-values are relative to the
2769 start of this total display area. */
2770 if (base_face_id != DEFAULT_FACE_ID)
2772 /* Mode lines, menu bar in terminal frames. */
2773 it->first_visible_x = 0;
2774 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2776 else
2778 it->first_visible_x =
2779 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2780 it->last_visible_x = (it->first_visible_x
2781 + window_box_width (w, TEXT_AREA));
2783 /* If we truncate lines, leave room for the truncation glyph(s) at
2784 the right margin. Otherwise, leave room for the continuation
2785 glyph(s). Done only if the window has no fringes. Since we
2786 don't know at this point whether there will be any R2L lines in
2787 the window, we reserve space for truncation/continuation glyphs
2788 even if only one of the fringes is absent. */
2789 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2790 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2792 if (it->line_wrap == TRUNCATE)
2793 it->last_visible_x -= it->truncation_pixel_width;
2794 else
2795 it->last_visible_x -= it->continuation_pixel_width;
2798 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2799 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2802 /* Leave room for a border glyph. */
2803 if (!FRAME_WINDOW_P (it->f)
2804 && !WINDOW_RIGHTMOST_P (it->w))
2805 it->last_visible_x -= 1;
2807 it->last_visible_y = window_text_bottom_y (w);
2809 /* For mode lines and alike, arrange for the first glyph having a
2810 left box line if the face specifies a box. */
2811 if (base_face_id != DEFAULT_FACE_ID)
2813 struct face *face;
2815 it->face_id = remapped_base_face_id;
2817 /* If we have a boxed mode line, make the first character appear
2818 with a left box line. */
2819 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2820 if (face->box != FACE_NO_BOX)
2821 it->start_of_box_run_p = 1;
2824 /* If a buffer position was specified, set the iterator there,
2825 getting overlays and face properties from that position. */
2826 if (charpos >= BUF_BEG (current_buffer))
2828 it->end_charpos = ZV;
2829 IT_CHARPOS (*it) = charpos;
2831 /* We will rely on `reseat' to set this up properly, via
2832 handle_face_prop. */
2833 it->face_id = it->base_face_id;
2835 /* Compute byte position if not specified. */
2836 if (bytepos < charpos)
2837 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2838 else
2839 IT_BYTEPOS (*it) = bytepos;
2841 it->start = it->current;
2842 /* Do we need to reorder bidirectional text? Not if this is a
2843 unibyte buffer: by definition, none of the single-byte
2844 characters are strong R2L, so no reordering is needed. And
2845 bidi.c doesn't support unibyte buffers anyway. Also, don't
2846 reorder while we are loading loadup.el, since the tables of
2847 character properties needed for reordering are not yet
2848 available. */
2849 it->bidi_p =
2850 NILP (Vpurify_flag)
2851 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2852 && it->multibyte_p;
2854 /* If we are to reorder bidirectional text, init the bidi
2855 iterator. */
2856 if (it->bidi_p)
2858 /* Note the paragraph direction that this buffer wants to
2859 use. */
2860 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2861 Qleft_to_right))
2862 it->paragraph_embedding = L2R;
2863 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2864 Qright_to_left))
2865 it->paragraph_embedding = R2L;
2866 else
2867 it->paragraph_embedding = NEUTRAL_DIR;
2868 bidi_unshelve_cache (NULL, 0);
2869 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2870 &it->bidi_it);
2873 /* Compute faces etc. */
2874 reseat (it, it->current.pos, 1);
2877 CHECK_IT (it);
2881 /* Initialize IT for the display of window W with window start POS. */
2883 void
2884 start_display (struct it *it, struct window *w, struct text_pos pos)
2886 struct glyph_row *row;
2887 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2889 row = w->desired_matrix->rows + first_vpos;
2890 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2891 it->first_vpos = first_vpos;
2893 /* Don't reseat to previous visible line start if current start
2894 position is in a string or image. */
2895 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2897 int start_at_line_beg_p;
2898 int first_y = it->current_y;
2900 /* If window start is not at a line start, skip forward to POS to
2901 get the correct continuation lines width. */
2902 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2903 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2904 if (!start_at_line_beg_p)
2906 int new_x;
2908 reseat_at_previous_visible_line_start (it);
2909 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2911 new_x = it->current_x + it->pixel_width;
2913 /* If lines are continued, this line may end in the middle
2914 of a multi-glyph character (e.g. a control character
2915 displayed as \003, or in the middle of an overlay
2916 string). In this case move_it_to above will not have
2917 taken us to the start of the continuation line but to the
2918 end of the continued line. */
2919 if (it->current_x > 0
2920 && it->line_wrap != TRUNCATE /* Lines are continued. */
2921 && (/* And glyph doesn't fit on the line. */
2922 new_x > it->last_visible_x
2923 /* Or it fits exactly and we're on a window
2924 system frame. */
2925 || (new_x == it->last_visible_x
2926 && FRAME_WINDOW_P (it->f)
2927 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2928 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2929 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2931 if ((it->current.dpvec_index >= 0
2932 || it->current.overlay_string_index >= 0)
2933 /* If we are on a newline from a display vector or
2934 overlay string, then we are already at the end of
2935 a screen line; no need to go to the next line in
2936 that case, as this line is not really continued.
2937 (If we do go to the next line, C-e will not DTRT.) */
2938 && it->c != '\n')
2940 set_iterator_to_next (it, 1);
2941 move_it_in_display_line_to (it, -1, -1, 0);
2944 it->continuation_lines_width += it->current_x;
2946 /* If the character at POS is displayed via a display
2947 vector, move_it_to above stops at the final glyph of
2948 IT->dpvec. To make the caller redisplay that character
2949 again (a.k.a. start at POS), we need to reset the
2950 dpvec_index to the beginning of IT->dpvec. */
2951 else if (it->current.dpvec_index >= 0)
2952 it->current.dpvec_index = 0;
2954 /* We're starting a new display line, not affected by the
2955 height of the continued line, so clear the appropriate
2956 fields in the iterator structure. */
2957 it->max_ascent = it->max_descent = 0;
2958 it->max_phys_ascent = it->max_phys_descent = 0;
2960 it->current_y = first_y;
2961 it->vpos = 0;
2962 it->current_x = it->hpos = 0;
2968 /* Return 1 if POS is a position in ellipses displayed for invisible
2969 text. W is the window we display, for text property lookup. */
2971 static int
2972 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2974 Lisp_Object prop, window;
2975 int ellipses_p = 0;
2976 ptrdiff_t charpos = CHARPOS (pos->pos);
2978 /* If POS specifies a position in a display vector, this might
2979 be for an ellipsis displayed for invisible text. We won't
2980 get the iterator set up for delivering that ellipsis unless
2981 we make sure that it gets aware of the invisible text. */
2982 if (pos->dpvec_index >= 0
2983 && pos->overlay_string_index < 0
2984 && CHARPOS (pos->string_pos) < 0
2985 && charpos > BEGV
2986 && (XSETWINDOW (window, w),
2987 prop = Fget_char_property (make_number (charpos),
2988 Qinvisible, window),
2989 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2991 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2992 window);
2993 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2996 return ellipses_p;
3000 /* Initialize IT for stepping through current_buffer in window W,
3001 starting at position POS that includes overlay string and display
3002 vector/ control character translation position information. Value
3003 is zero if there are overlay strings with newlines at POS. */
3005 static int
3006 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3008 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3009 int i, overlay_strings_with_newlines = 0;
3011 /* If POS specifies a position in a display vector, this might
3012 be for an ellipsis displayed for invisible text. We won't
3013 get the iterator set up for delivering that ellipsis unless
3014 we make sure that it gets aware of the invisible text. */
3015 if (in_ellipses_for_invisible_text_p (pos, w))
3017 --charpos;
3018 bytepos = 0;
3021 /* Keep in mind: the call to reseat in init_iterator skips invisible
3022 text, so we might end up at a position different from POS. This
3023 is only a problem when POS is a row start after a newline and an
3024 overlay starts there with an after-string, and the overlay has an
3025 invisible property. Since we don't skip invisible text in
3026 display_line and elsewhere immediately after consuming the
3027 newline before the row start, such a POS will not be in a string,
3028 but the call to init_iterator below will move us to the
3029 after-string. */
3030 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3032 /* This only scans the current chunk -- it should scan all chunks.
3033 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3034 to 16 in 22.1 to make this a lesser problem. */
3035 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3037 const char *s = SSDATA (it->overlay_strings[i]);
3038 const char *e = s + SBYTES (it->overlay_strings[i]);
3040 while (s < e && *s != '\n')
3041 ++s;
3043 if (s < e)
3045 overlay_strings_with_newlines = 1;
3046 break;
3050 /* If position is within an overlay string, set up IT to the right
3051 overlay string. */
3052 if (pos->overlay_string_index >= 0)
3054 int relative_index;
3056 /* If the first overlay string happens to have a `display'
3057 property for an image, the iterator will be set up for that
3058 image, and we have to undo that setup first before we can
3059 correct the overlay string index. */
3060 if (it->method == GET_FROM_IMAGE)
3061 pop_it (it);
3063 /* We already have the first chunk of overlay strings in
3064 IT->overlay_strings. Load more until the one for
3065 pos->overlay_string_index is in IT->overlay_strings. */
3066 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3068 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3069 it->current.overlay_string_index = 0;
3070 while (n--)
3072 load_overlay_strings (it, 0);
3073 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3077 it->current.overlay_string_index = pos->overlay_string_index;
3078 relative_index = (it->current.overlay_string_index
3079 % OVERLAY_STRING_CHUNK_SIZE);
3080 it->string = it->overlay_strings[relative_index];
3081 eassert (STRINGP (it->string));
3082 it->current.string_pos = pos->string_pos;
3083 it->method = GET_FROM_STRING;
3086 if (CHARPOS (pos->string_pos) >= 0)
3088 /* Recorded position is not in an overlay string, but in another
3089 string. This can only be a string from a `display' property.
3090 IT should already be filled with that string. */
3091 it->current.string_pos = pos->string_pos;
3092 eassert (STRINGP (it->string));
3095 /* Restore position in display vector translations, control
3096 character translations or ellipses. */
3097 if (pos->dpvec_index >= 0)
3099 if (it->dpvec == NULL)
3100 get_next_display_element (it);
3101 eassert (it->dpvec && it->current.dpvec_index == 0);
3102 it->current.dpvec_index = pos->dpvec_index;
3105 CHECK_IT (it);
3106 return !overlay_strings_with_newlines;
3110 /* Initialize IT for stepping through current_buffer in window W
3111 starting at ROW->start. */
3113 static void
3114 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3116 init_from_display_pos (it, w, &row->start);
3117 it->start = row->start;
3118 it->continuation_lines_width = row->continuation_lines_width;
3119 CHECK_IT (it);
3123 /* Initialize IT for stepping through current_buffer in window W
3124 starting in the line following ROW, i.e. starting at ROW->end.
3125 Value is zero if there are overlay strings with newlines at ROW's
3126 end position. */
3128 static int
3129 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3131 int success = 0;
3133 if (init_from_display_pos (it, w, &row->end))
3135 if (row->continued_p)
3136 it->continuation_lines_width
3137 = row->continuation_lines_width + row->pixel_width;
3138 CHECK_IT (it);
3139 success = 1;
3142 return success;
3148 /***********************************************************************
3149 Text properties
3150 ***********************************************************************/
3152 /* Called when IT reaches IT->stop_charpos. Handle text property and
3153 overlay changes. Set IT->stop_charpos to the next position where
3154 to stop. */
3156 static void
3157 handle_stop (struct it *it)
3159 enum prop_handled handled;
3160 int handle_overlay_change_p;
3161 struct props *p;
3163 it->dpvec = NULL;
3164 it->current.dpvec_index = -1;
3165 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3166 it->ignore_overlay_strings_at_pos_p = 0;
3167 it->ellipsis_p = 0;
3169 /* Use face of preceding text for ellipsis (if invisible) */
3170 if (it->selective_display_ellipsis_p)
3171 it->saved_face_id = it->face_id;
3175 handled = HANDLED_NORMALLY;
3177 /* Call text property handlers. */
3178 for (p = it_props; p->handler; ++p)
3180 handled = p->handler (it);
3182 if (handled == HANDLED_RECOMPUTE_PROPS)
3183 break;
3184 else if (handled == HANDLED_RETURN)
3186 /* We still want to show before and after strings from
3187 overlays even if the actual buffer text is replaced. */
3188 if (!handle_overlay_change_p
3189 || it->sp > 1
3190 /* Don't call get_overlay_strings_1 if we already
3191 have overlay strings loaded, because doing so
3192 will load them again and push the iterator state
3193 onto the stack one more time, which is not
3194 expected by the rest of the code that processes
3195 overlay strings. */
3196 || (it->current.overlay_string_index < 0
3197 ? !get_overlay_strings_1 (it, 0, 0)
3198 : 0))
3200 if (it->ellipsis_p)
3201 setup_for_ellipsis (it, 0);
3202 /* When handling a display spec, we might load an
3203 empty string. In that case, discard it here. We
3204 used to discard it in handle_single_display_spec,
3205 but that causes get_overlay_strings_1, above, to
3206 ignore overlay strings that we must check. */
3207 if (STRINGP (it->string) && !SCHARS (it->string))
3208 pop_it (it);
3209 return;
3211 else if (STRINGP (it->string) && !SCHARS (it->string))
3212 pop_it (it);
3213 else
3215 it->ignore_overlay_strings_at_pos_p = 1;
3216 it->string_from_display_prop_p = 0;
3217 it->from_disp_prop_p = 0;
3218 handle_overlay_change_p = 0;
3220 handled = HANDLED_RECOMPUTE_PROPS;
3221 break;
3223 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3224 handle_overlay_change_p = 0;
3227 if (handled != HANDLED_RECOMPUTE_PROPS)
3229 /* Don't check for overlay strings below when set to deliver
3230 characters from a display vector. */
3231 if (it->method == GET_FROM_DISPLAY_VECTOR)
3232 handle_overlay_change_p = 0;
3234 /* Handle overlay changes.
3235 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3236 if it finds overlays. */
3237 if (handle_overlay_change_p)
3238 handled = handle_overlay_change (it);
3241 if (it->ellipsis_p)
3243 setup_for_ellipsis (it, 0);
3244 break;
3247 while (handled == HANDLED_RECOMPUTE_PROPS);
3249 /* Determine where to stop next. */
3250 if (handled == HANDLED_NORMALLY)
3251 compute_stop_pos (it);
3255 /* Compute IT->stop_charpos from text property and overlay change
3256 information for IT's current position. */
3258 static void
3259 compute_stop_pos (struct it *it)
3261 register INTERVAL iv, next_iv;
3262 Lisp_Object object, limit, position;
3263 ptrdiff_t charpos, bytepos;
3265 if (STRINGP (it->string))
3267 /* Strings are usually short, so don't limit the search for
3268 properties. */
3269 it->stop_charpos = it->end_charpos;
3270 object = it->string;
3271 limit = Qnil;
3272 charpos = IT_STRING_CHARPOS (*it);
3273 bytepos = IT_STRING_BYTEPOS (*it);
3275 else
3277 ptrdiff_t pos;
3279 /* If end_charpos is out of range for some reason, such as a
3280 misbehaving display function, rationalize it (Bug#5984). */
3281 if (it->end_charpos > ZV)
3282 it->end_charpos = ZV;
3283 it->stop_charpos = it->end_charpos;
3285 /* If next overlay change is in front of the current stop pos
3286 (which is IT->end_charpos), stop there. Note: value of
3287 next_overlay_change is point-max if no overlay change
3288 follows. */
3289 charpos = IT_CHARPOS (*it);
3290 bytepos = IT_BYTEPOS (*it);
3291 pos = next_overlay_change (charpos);
3292 if (pos < it->stop_charpos)
3293 it->stop_charpos = pos;
3295 /* If showing the region, we have to stop at the region
3296 start or end because the face might change there. */
3297 if (it->region_beg_charpos > 0)
3299 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3300 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3301 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3302 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3305 /* Set up variables for computing the stop position from text
3306 property changes. */
3307 XSETBUFFER (object, current_buffer);
3308 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3311 /* Get the interval containing IT's position. Value is a null
3312 interval if there isn't such an interval. */
3313 position = make_number (charpos);
3314 iv = validate_interval_range (object, &position, &position, 0);
3315 if (!NULL_INTERVAL_P (iv))
3317 Lisp_Object values_here[LAST_PROP_IDX];
3318 struct props *p;
3320 /* Get properties here. */
3321 for (p = it_props; p->handler; ++p)
3322 values_here[p->idx] = textget (iv->plist, *p->name);
3324 /* Look for an interval following iv that has different
3325 properties. */
3326 for (next_iv = next_interval (iv);
3327 (!NULL_INTERVAL_P (next_iv)
3328 && (NILP (limit)
3329 || XFASTINT (limit) > next_iv->position));
3330 next_iv = next_interval (next_iv))
3332 for (p = it_props; p->handler; ++p)
3334 Lisp_Object new_value;
3336 new_value = textget (next_iv->plist, *p->name);
3337 if (!EQ (values_here[p->idx], new_value))
3338 break;
3341 if (p->handler)
3342 break;
3345 if (!NULL_INTERVAL_P (next_iv))
3347 if (INTEGERP (limit)
3348 && next_iv->position >= XFASTINT (limit))
3349 /* No text property change up to limit. */
3350 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3351 else
3352 /* Text properties change in next_iv. */
3353 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3357 if (it->cmp_it.id < 0)
3359 ptrdiff_t stoppos = it->end_charpos;
3361 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3362 stoppos = -1;
3363 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3364 stoppos, it->string);
3367 eassert (STRINGP (it->string)
3368 || (it->stop_charpos >= BEGV
3369 && it->stop_charpos >= IT_CHARPOS (*it)));
3373 /* Return the position of the next overlay change after POS in
3374 current_buffer. Value is point-max if no overlay change
3375 follows. This is like `next-overlay-change' but doesn't use
3376 xmalloc. */
3378 static ptrdiff_t
3379 next_overlay_change (ptrdiff_t pos)
3381 ptrdiff_t i, noverlays;
3382 ptrdiff_t endpos;
3383 Lisp_Object *overlays;
3385 /* Get all overlays at the given position. */
3386 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3388 /* If any of these overlays ends before endpos,
3389 use its ending point instead. */
3390 for (i = 0; i < noverlays; ++i)
3392 Lisp_Object oend;
3393 ptrdiff_t oendpos;
3395 oend = OVERLAY_END (overlays[i]);
3396 oendpos = OVERLAY_POSITION (oend);
3397 endpos = min (endpos, oendpos);
3400 return endpos;
3403 /* How many characters forward to search for a display property or
3404 display string. Searching too far forward makes the bidi display
3405 sluggish, especially in small windows. */
3406 #define MAX_DISP_SCAN 250
3408 /* Return the character position of a display string at or after
3409 position specified by POSITION. If no display string exists at or
3410 after POSITION, return ZV. A display string is either an overlay
3411 with `display' property whose value is a string, or a `display'
3412 text property whose value is a string. STRING is data about the
3413 string to iterate; if STRING->lstring is nil, we are iterating a
3414 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3415 on a GUI frame. DISP_PROP is set to zero if we searched
3416 MAX_DISP_SCAN characters forward without finding any display
3417 strings, non-zero otherwise. It is set to 2 if the display string
3418 uses any kind of `(space ...)' spec that will produce a stretch of
3419 white space in the text area. */
3420 ptrdiff_t
3421 compute_display_string_pos (struct text_pos *position,
3422 struct bidi_string_data *string,
3423 int frame_window_p, int *disp_prop)
3425 /* OBJECT = nil means current buffer. */
3426 Lisp_Object object =
3427 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3428 Lisp_Object pos, spec, limpos;
3429 int string_p = (string && (STRINGP (string->lstring) || string->s));
3430 ptrdiff_t eob = string_p ? string->schars : ZV;
3431 ptrdiff_t begb = string_p ? 0 : BEGV;
3432 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3433 ptrdiff_t lim =
3434 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3435 struct text_pos tpos;
3436 int rv = 0;
3438 *disp_prop = 1;
3440 if (charpos >= eob
3441 /* We don't support display properties whose values are strings
3442 that have display string properties. */
3443 || string->from_disp_str
3444 /* C strings cannot have display properties. */
3445 || (string->s && !STRINGP (object)))
3447 *disp_prop = 0;
3448 return eob;
3451 /* If the character at CHARPOS is where the display string begins,
3452 return CHARPOS. */
3453 pos = make_number (charpos);
3454 if (STRINGP (object))
3455 bufpos = string->bufpos;
3456 else
3457 bufpos = charpos;
3458 tpos = *position;
3459 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3460 && (charpos <= begb
3461 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3462 object),
3463 spec))
3464 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3465 frame_window_p)))
3467 if (rv == 2)
3468 *disp_prop = 2;
3469 return charpos;
3472 /* Look forward for the first character with a `display' property
3473 that will replace the underlying text when displayed. */
3474 limpos = make_number (lim);
3475 do {
3476 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3477 CHARPOS (tpos) = XFASTINT (pos);
3478 if (CHARPOS (tpos) >= lim)
3480 *disp_prop = 0;
3481 break;
3483 if (STRINGP (object))
3484 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3485 else
3486 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3487 spec = Fget_char_property (pos, Qdisplay, object);
3488 if (!STRINGP (object))
3489 bufpos = CHARPOS (tpos);
3490 } while (NILP (spec)
3491 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3492 bufpos, frame_window_p)));
3493 if (rv == 2)
3494 *disp_prop = 2;
3496 return CHARPOS (tpos);
3499 /* Return the character position of the end of the display string that
3500 started at CHARPOS. If there's no display string at CHARPOS,
3501 return -1. A display string is either an overlay with `display'
3502 property whose value is a string or a `display' text property whose
3503 value is a string. */
3504 ptrdiff_t
3505 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3507 /* OBJECT = nil means current buffer. */
3508 Lisp_Object object =
3509 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3510 Lisp_Object pos = make_number (charpos);
3511 ptrdiff_t eob =
3512 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3514 if (charpos >= eob || (string->s && !STRINGP (object)))
3515 return eob;
3517 /* It could happen that the display property or overlay was removed
3518 since we found it in compute_display_string_pos above. One way
3519 this can happen is if JIT font-lock was called (through
3520 handle_fontified_prop), and jit-lock-functions remove text
3521 properties or overlays from the portion of buffer that includes
3522 CHARPOS. Muse mode is known to do that, for example. In this
3523 case, we return -1 to the caller, to signal that no display
3524 string is actually present at CHARPOS. See bidi_fetch_char for
3525 how this is handled.
3527 An alternative would be to never look for display properties past
3528 it->stop_charpos. But neither compute_display_string_pos nor
3529 bidi_fetch_char that calls it know or care where the next
3530 stop_charpos is. */
3531 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3532 return -1;
3534 /* Look forward for the first character where the `display' property
3535 changes. */
3536 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3538 return XFASTINT (pos);
3543 /***********************************************************************
3544 Fontification
3545 ***********************************************************************/
3547 /* Handle changes in the `fontified' property of the current buffer by
3548 calling hook functions from Qfontification_functions to fontify
3549 regions of text. */
3551 static enum prop_handled
3552 handle_fontified_prop (struct it *it)
3554 Lisp_Object prop, pos;
3555 enum prop_handled handled = HANDLED_NORMALLY;
3557 if (!NILP (Vmemory_full))
3558 return handled;
3560 /* Get the value of the `fontified' property at IT's current buffer
3561 position. (The `fontified' property doesn't have a special
3562 meaning in strings.) If the value is nil, call functions from
3563 Qfontification_functions. */
3564 if (!STRINGP (it->string)
3565 && it->s == NULL
3566 && !NILP (Vfontification_functions)
3567 && !NILP (Vrun_hooks)
3568 && (pos = make_number (IT_CHARPOS (*it)),
3569 prop = Fget_char_property (pos, Qfontified, Qnil),
3570 /* Ignore the special cased nil value always present at EOB since
3571 no amount of fontifying will be able to change it. */
3572 NILP (prop) && IT_CHARPOS (*it) < Z))
3574 ptrdiff_t count = SPECPDL_INDEX ();
3575 Lisp_Object val;
3576 struct buffer *obuf = current_buffer;
3577 int begv = BEGV, zv = ZV;
3578 int old_clip_changed = current_buffer->clip_changed;
3580 val = Vfontification_functions;
3581 specbind (Qfontification_functions, Qnil);
3583 eassert (it->end_charpos == ZV);
3585 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3586 safe_call1 (val, pos);
3587 else
3589 Lisp_Object fns, fn;
3590 struct gcpro gcpro1, gcpro2;
3592 fns = Qnil;
3593 GCPRO2 (val, fns);
3595 for (; CONSP (val); val = XCDR (val))
3597 fn = XCAR (val);
3599 if (EQ (fn, Qt))
3601 /* A value of t indicates this hook has a local
3602 binding; it means to run the global binding too.
3603 In a global value, t should not occur. If it
3604 does, we must ignore it to avoid an endless
3605 loop. */
3606 for (fns = Fdefault_value (Qfontification_functions);
3607 CONSP (fns);
3608 fns = XCDR (fns))
3610 fn = XCAR (fns);
3611 if (!EQ (fn, Qt))
3612 safe_call1 (fn, pos);
3615 else
3616 safe_call1 (fn, pos);
3619 UNGCPRO;
3622 unbind_to (count, Qnil);
3624 /* Fontification functions routinely call `save-restriction'.
3625 Normally, this tags clip_changed, which can confuse redisplay
3626 (see discussion in Bug#6671). Since we don't perform any
3627 special handling of fontification changes in the case where
3628 `save-restriction' isn't called, there's no point doing so in
3629 this case either. So, if the buffer's restrictions are
3630 actually left unchanged, reset clip_changed. */
3631 if (obuf == current_buffer)
3633 if (begv == BEGV && zv == ZV)
3634 current_buffer->clip_changed = old_clip_changed;
3636 /* There isn't much we can reasonably do to protect against
3637 misbehaving fontification, but here's a fig leaf. */
3638 else if (!NILP (BVAR (obuf, name)))
3639 set_buffer_internal_1 (obuf);
3641 /* The fontification code may have added/removed text.
3642 It could do even a lot worse, but let's at least protect against
3643 the most obvious case where only the text past `pos' gets changed',
3644 as is/was done in grep.el where some escapes sequences are turned
3645 into face properties (bug#7876). */
3646 it->end_charpos = ZV;
3648 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3649 something. This avoids an endless loop if they failed to
3650 fontify the text for which reason ever. */
3651 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3652 handled = HANDLED_RECOMPUTE_PROPS;
3655 return handled;
3660 /***********************************************************************
3661 Faces
3662 ***********************************************************************/
3664 /* Set up iterator IT from face properties at its current position.
3665 Called from handle_stop. */
3667 static enum prop_handled
3668 handle_face_prop (struct it *it)
3670 int new_face_id;
3671 ptrdiff_t next_stop;
3673 if (!STRINGP (it->string))
3675 new_face_id
3676 = face_at_buffer_position (it->w,
3677 IT_CHARPOS (*it),
3678 it->region_beg_charpos,
3679 it->region_end_charpos,
3680 &next_stop,
3681 (IT_CHARPOS (*it)
3682 + TEXT_PROP_DISTANCE_LIMIT),
3683 0, it->base_face_id);
3685 /* Is this a start of a run of characters with box face?
3686 Caveat: this can be called for a freshly initialized
3687 iterator; face_id is -1 in this case. We know that the new
3688 face will not change until limit, i.e. if the new face has a
3689 box, all characters up to limit will have one. But, as
3690 usual, we don't know whether limit is really the end. */
3691 if (new_face_id != it->face_id)
3693 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3695 /* If new face has a box but old face has not, this is
3696 the start of a run of characters with box, i.e. it has
3697 a shadow on the left side. The value of face_id of the
3698 iterator will be -1 if this is the initial call that gets
3699 the face. In this case, we have to look in front of IT's
3700 position and see whether there is a face != new_face_id. */
3701 it->start_of_box_run_p
3702 = (new_face->box != FACE_NO_BOX
3703 && (it->face_id >= 0
3704 || IT_CHARPOS (*it) == BEG
3705 || new_face_id != face_before_it_pos (it)));
3706 it->face_box_p = new_face->box != FACE_NO_BOX;
3709 else
3711 int base_face_id;
3712 ptrdiff_t bufpos;
3713 int i;
3714 Lisp_Object from_overlay
3715 = (it->current.overlay_string_index >= 0
3716 ? it->string_overlays[it->current.overlay_string_index
3717 % OVERLAY_STRING_CHUNK_SIZE]
3718 : Qnil);
3720 /* See if we got to this string directly or indirectly from
3721 an overlay property. That includes the before-string or
3722 after-string of an overlay, strings in display properties
3723 provided by an overlay, their text properties, etc.
3725 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3726 if (! NILP (from_overlay))
3727 for (i = it->sp - 1; i >= 0; i--)
3729 if (it->stack[i].current.overlay_string_index >= 0)
3730 from_overlay
3731 = it->string_overlays[it->stack[i].current.overlay_string_index
3732 % OVERLAY_STRING_CHUNK_SIZE];
3733 else if (! NILP (it->stack[i].from_overlay))
3734 from_overlay = it->stack[i].from_overlay;
3736 if (!NILP (from_overlay))
3737 break;
3740 if (! NILP (from_overlay))
3742 bufpos = IT_CHARPOS (*it);
3743 /* For a string from an overlay, the base face depends
3744 only on text properties and ignores overlays. */
3745 base_face_id
3746 = face_for_overlay_string (it->w,
3747 IT_CHARPOS (*it),
3748 it->region_beg_charpos,
3749 it->region_end_charpos,
3750 &next_stop,
3751 (IT_CHARPOS (*it)
3752 + TEXT_PROP_DISTANCE_LIMIT),
3754 from_overlay);
3756 else
3758 bufpos = 0;
3760 /* For strings from a `display' property, use the face at
3761 IT's current buffer position as the base face to merge
3762 with, so that overlay strings appear in the same face as
3763 surrounding text, unless they specify their own
3764 faces. */
3765 base_face_id = it->string_from_prefix_prop_p
3766 ? DEFAULT_FACE_ID
3767 : underlying_face_id (it);
3770 new_face_id = face_at_string_position (it->w,
3771 it->string,
3772 IT_STRING_CHARPOS (*it),
3773 bufpos,
3774 it->region_beg_charpos,
3775 it->region_end_charpos,
3776 &next_stop,
3777 base_face_id, 0);
3779 /* Is this a start of a run of characters with box? Caveat:
3780 this can be called for a freshly allocated iterator; face_id
3781 is -1 is this case. We know that the new face will not
3782 change until the next check pos, i.e. if the new face has a
3783 box, all characters up to that position will have a
3784 box. But, as usual, we don't know whether that position
3785 is really the end. */
3786 if (new_face_id != it->face_id)
3788 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3789 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3791 /* If new face has a box but old face hasn't, this is the
3792 start of a run of characters with box, i.e. it has a
3793 shadow on the left side. */
3794 it->start_of_box_run_p
3795 = new_face->box && (old_face == NULL || !old_face->box);
3796 it->face_box_p = new_face->box != FACE_NO_BOX;
3800 it->face_id = new_face_id;
3801 return HANDLED_NORMALLY;
3805 /* Return the ID of the face ``underlying'' IT's current position,
3806 which is in a string. If the iterator is associated with a
3807 buffer, return the face at IT's current buffer position.
3808 Otherwise, use the iterator's base_face_id. */
3810 static int
3811 underlying_face_id (struct it *it)
3813 int face_id = it->base_face_id, i;
3815 eassert (STRINGP (it->string));
3817 for (i = it->sp - 1; i >= 0; --i)
3818 if (NILP (it->stack[i].string))
3819 face_id = it->stack[i].face_id;
3821 return face_id;
3825 /* Compute the face one character before or after the current position
3826 of IT, in the visual order. BEFORE_P non-zero means get the face
3827 in front (to the left in L2R paragraphs, to the right in R2L
3828 paragraphs) of IT's screen position. Value is the ID of the face. */
3830 static int
3831 face_before_or_after_it_pos (struct it *it, int before_p)
3833 int face_id, limit;
3834 ptrdiff_t next_check_charpos;
3835 struct it it_copy;
3836 void *it_copy_data = NULL;
3838 eassert (it->s == NULL);
3840 if (STRINGP (it->string))
3842 ptrdiff_t bufpos, charpos;
3843 int base_face_id;
3845 /* No face change past the end of the string (for the case
3846 we are padding with spaces). No face change before the
3847 string start. */
3848 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3849 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3850 return it->face_id;
3852 if (!it->bidi_p)
3854 /* Set charpos to the position before or after IT's current
3855 position, in the logical order, which in the non-bidi
3856 case is the same as the visual order. */
3857 if (before_p)
3858 charpos = IT_STRING_CHARPOS (*it) - 1;
3859 else if (it->what == IT_COMPOSITION)
3860 /* For composition, we must check the character after the
3861 composition. */
3862 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3863 else
3864 charpos = IT_STRING_CHARPOS (*it) + 1;
3866 else
3868 if (before_p)
3870 /* With bidi iteration, the character before the current
3871 in the visual order cannot be found by simple
3872 iteration, because "reverse" reordering is not
3873 supported. Instead, we need to use the move_it_*
3874 family of functions. */
3875 /* Ignore face changes before the first visible
3876 character on this display line. */
3877 if (it->current_x <= it->first_visible_x)
3878 return it->face_id;
3879 SAVE_IT (it_copy, *it, it_copy_data);
3880 /* Implementation note: Since move_it_in_display_line
3881 works in the iterator geometry, and thinks the first
3882 character is always the leftmost, even in R2L lines,
3883 we don't need to distinguish between the R2L and L2R
3884 cases here. */
3885 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3886 it_copy.current_x - 1, MOVE_TO_X);
3887 charpos = IT_STRING_CHARPOS (it_copy);
3888 RESTORE_IT (it, it, it_copy_data);
3890 else
3892 /* Set charpos to the string position of the character
3893 that comes after IT's current position in the visual
3894 order. */
3895 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3897 it_copy = *it;
3898 while (n--)
3899 bidi_move_to_visually_next (&it_copy.bidi_it);
3901 charpos = it_copy.bidi_it.charpos;
3904 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3906 if (it->current.overlay_string_index >= 0)
3907 bufpos = IT_CHARPOS (*it);
3908 else
3909 bufpos = 0;
3911 base_face_id = underlying_face_id (it);
3913 /* Get the face for ASCII, or unibyte. */
3914 face_id = face_at_string_position (it->w,
3915 it->string,
3916 charpos,
3917 bufpos,
3918 it->region_beg_charpos,
3919 it->region_end_charpos,
3920 &next_check_charpos,
3921 base_face_id, 0);
3923 /* Correct the face for charsets different from ASCII. Do it
3924 for the multibyte case only. The face returned above is
3925 suitable for unibyte text if IT->string is unibyte. */
3926 if (STRING_MULTIBYTE (it->string))
3928 struct text_pos pos1 = string_pos (charpos, it->string);
3929 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3930 int c, len;
3931 struct face *face = FACE_FROM_ID (it->f, face_id);
3933 c = string_char_and_length (p, &len);
3934 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3937 else
3939 struct text_pos pos;
3941 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3942 || (IT_CHARPOS (*it) <= BEGV && before_p))
3943 return it->face_id;
3945 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3946 pos = it->current.pos;
3948 if (!it->bidi_p)
3950 if (before_p)
3951 DEC_TEXT_POS (pos, it->multibyte_p);
3952 else
3954 if (it->what == IT_COMPOSITION)
3956 /* For composition, we must check the position after
3957 the composition. */
3958 pos.charpos += it->cmp_it.nchars;
3959 pos.bytepos += it->len;
3961 else
3962 INC_TEXT_POS (pos, it->multibyte_p);
3965 else
3967 if (before_p)
3969 /* With bidi iteration, the character before the current
3970 in the visual order cannot be found by simple
3971 iteration, because "reverse" reordering is not
3972 supported. Instead, we need to use the move_it_*
3973 family of functions. */
3974 /* Ignore face changes before the first visible
3975 character on this display line. */
3976 if (it->current_x <= it->first_visible_x)
3977 return it->face_id;
3978 SAVE_IT (it_copy, *it, it_copy_data);
3979 /* Implementation note: Since move_it_in_display_line
3980 works in the iterator geometry, and thinks the first
3981 character is always the leftmost, even in R2L lines,
3982 we don't need to distinguish between the R2L and L2R
3983 cases here. */
3984 move_it_in_display_line (&it_copy, ZV,
3985 it_copy.current_x - 1, MOVE_TO_X);
3986 pos = it_copy.current.pos;
3987 RESTORE_IT (it, it, it_copy_data);
3989 else
3991 /* Set charpos to the buffer position of the character
3992 that comes after IT's current position in the visual
3993 order. */
3994 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3996 it_copy = *it;
3997 while (n--)
3998 bidi_move_to_visually_next (&it_copy.bidi_it);
4000 SET_TEXT_POS (pos,
4001 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4004 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4006 /* Determine face for CHARSET_ASCII, or unibyte. */
4007 face_id = face_at_buffer_position (it->w,
4008 CHARPOS (pos),
4009 it->region_beg_charpos,
4010 it->region_end_charpos,
4011 &next_check_charpos,
4012 limit, 0, -1);
4014 /* Correct the face for charsets different from ASCII. Do it
4015 for the multibyte case only. The face returned above is
4016 suitable for unibyte text if current_buffer is unibyte. */
4017 if (it->multibyte_p)
4019 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4020 struct face *face = FACE_FROM_ID (it->f, face_id);
4021 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4025 return face_id;
4030 /***********************************************************************
4031 Invisible text
4032 ***********************************************************************/
4034 /* Set up iterator IT from invisible properties at its current
4035 position. Called from handle_stop. */
4037 static enum prop_handled
4038 handle_invisible_prop (struct it *it)
4040 enum prop_handled handled = HANDLED_NORMALLY;
4042 if (STRINGP (it->string))
4044 Lisp_Object prop, end_charpos, limit, charpos;
4046 /* Get the value of the invisible text property at the
4047 current position. Value will be nil if there is no such
4048 property. */
4049 charpos = make_number (IT_STRING_CHARPOS (*it));
4050 prop = Fget_text_property (charpos, Qinvisible, it->string);
4052 if (!NILP (prop)
4053 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4055 ptrdiff_t endpos;
4057 handled = HANDLED_RECOMPUTE_PROPS;
4059 /* Get the position at which the next change of the
4060 invisible text property can be found in IT->string.
4061 Value will be nil if the property value is the same for
4062 all the rest of IT->string. */
4063 XSETINT (limit, SCHARS (it->string));
4064 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4065 it->string, limit);
4067 /* Text at current position is invisible. The next
4068 change in the property is at position end_charpos.
4069 Move IT's current position to that position. */
4070 if (INTEGERP (end_charpos)
4071 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4073 struct text_pos old;
4074 ptrdiff_t oldpos;
4076 old = it->current.string_pos;
4077 oldpos = CHARPOS (old);
4078 if (it->bidi_p)
4080 if (it->bidi_it.first_elt
4081 && it->bidi_it.charpos < SCHARS (it->string))
4082 bidi_paragraph_init (it->paragraph_embedding,
4083 &it->bidi_it, 1);
4084 /* Bidi-iterate out of the invisible text. */
4087 bidi_move_to_visually_next (&it->bidi_it);
4089 while (oldpos <= it->bidi_it.charpos
4090 && it->bidi_it.charpos < endpos);
4092 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4093 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4094 if (IT_CHARPOS (*it) >= endpos)
4095 it->prev_stop = endpos;
4097 else
4099 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4100 compute_string_pos (&it->current.string_pos, old, it->string);
4103 else
4105 /* The rest of the string is invisible. If this is an
4106 overlay string, proceed with the next overlay string
4107 or whatever comes and return a character from there. */
4108 if (it->current.overlay_string_index >= 0)
4110 next_overlay_string (it);
4111 /* Don't check for overlay strings when we just
4112 finished processing them. */
4113 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4115 else
4117 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4118 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4123 else
4125 int invis_p;
4126 ptrdiff_t newpos, next_stop, start_charpos, tem;
4127 Lisp_Object pos, prop, overlay;
4129 /* First of all, is there invisible text at this position? */
4130 tem = start_charpos = IT_CHARPOS (*it);
4131 pos = make_number (tem);
4132 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4133 &overlay);
4134 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4136 /* If we are on invisible text, skip over it. */
4137 if (invis_p && start_charpos < it->end_charpos)
4139 /* Record whether we have to display an ellipsis for the
4140 invisible text. */
4141 int display_ellipsis_p = invis_p == 2;
4143 handled = HANDLED_RECOMPUTE_PROPS;
4145 /* Loop skipping over invisible text. The loop is left at
4146 ZV or with IT on the first char being visible again. */
4149 /* Try to skip some invisible text. Return value is the
4150 position reached which can be equal to where we start
4151 if there is nothing invisible there. This skips both
4152 over invisible text properties and overlays with
4153 invisible property. */
4154 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4156 /* If we skipped nothing at all we weren't at invisible
4157 text in the first place. If everything to the end of
4158 the buffer was skipped, end the loop. */
4159 if (newpos == tem || newpos >= ZV)
4160 invis_p = 0;
4161 else
4163 /* We skipped some characters but not necessarily
4164 all there are. Check if we ended up on visible
4165 text. Fget_char_property returns the property of
4166 the char before the given position, i.e. if we
4167 get invis_p = 0, this means that the char at
4168 newpos is visible. */
4169 pos = make_number (newpos);
4170 prop = Fget_char_property (pos, Qinvisible, it->window);
4171 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4174 /* If we ended up on invisible text, proceed to
4175 skip starting with next_stop. */
4176 if (invis_p)
4177 tem = next_stop;
4179 /* If there are adjacent invisible texts, don't lose the
4180 second one's ellipsis. */
4181 if (invis_p == 2)
4182 display_ellipsis_p = 1;
4184 while (invis_p);
4186 /* The position newpos is now either ZV or on visible text. */
4187 if (it->bidi_p)
4189 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4190 int on_newline =
4191 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4192 int after_newline =
4193 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4195 /* If the invisible text ends on a newline or on a
4196 character after a newline, we can avoid the costly,
4197 character by character, bidi iteration to NEWPOS, and
4198 instead simply reseat the iterator there. That's
4199 because all bidi reordering information is tossed at
4200 the newline. This is a big win for modes that hide
4201 complete lines, like Outline, Org, etc. */
4202 if (on_newline || after_newline)
4204 struct text_pos tpos;
4205 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4207 SET_TEXT_POS (tpos, newpos, bpos);
4208 reseat_1 (it, tpos, 0);
4209 /* If we reseat on a newline/ZV, we need to prep the
4210 bidi iterator for advancing to the next character
4211 after the newline/EOB, keeping the current paragraph
4212 direction (so that PRODUCE_GLYPHS does TRT wrt
4213 prepending/appending glyphs to a glyph row). */
4214 if (on_newline)
4216 it->bidi_it.first_elt = 0;
4217 it->bidi_it.paragraph_dir = pdir;
4218 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4219 it->bidi_it.nchars = 1;
4220 it->bidi_it.ch_len = 1;
4223 else /* Must use the slow method. */
4225 /* With bidi iteration, the region of invisible text
4226 could start and/or end in the middle of a
4227 non-base embedding level. Therefore, we need to
4228 skip invisible text using the bidi iterator,
4229 starting at IT's current position, until we find
4230 ourselves outside of the invisible text.
4231 Skipping invisible text _after_ bidi iteration
4232 avoids affecting the visual order of the
4233 displayed text when invisible properties are
4234 added or removed. */
4235 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4237 /* If we were `reseat'ed to a new paragraph,
4238 determine the paragraph base direction. We
4239 need to do it now because
4240 next_element_from_buffer may not have a
4241 chance to do it, if we are going to skip any
4242 text at the beginning, which resets the
4243 FIRST_ELT flag. */
4244 bidi_paragraph_init (it->paragraph_embedding,
4245 &it->bidi_it, 1);
4249 bidi_move_to_visually_next (&it->bidi_it);
4251 while (it->stop_charpos <= it->bidi_it.charpos
4252 && it->bidi_it.charpos < newpos);
4253 IT_CHARPOS (*it) = it->bidi_it.charpos;
4254 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4255 /* If we overstepped NEWPOS, record its position in
4256 the iterator, so that we skip invisible text if
4257 later the bidi iteration lands us in the
4258 invisible region again. */
4259 if (IT_CHARPOS (*it) >= newpos)
4260 it->prev_stop = newpos;
4263 else
4265 IT_CHARPOS (*it) = newpos;
4266 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4269 /* If there are before-strings at the start of invisible
4270 text, and the text is invisible because of a text
4271 property, arrange to show before-strings because 20.x did
4272 it that way. (If the text is invisible because of an
4273 overlay property instead of a text property, this is
4274 already handled in the overlay code.) */
4275 if (NILP (overlay)
4276 && get_overlay_strings (it, it->stop_charpos))
4278 handled = HANDLED_RECOMPUTE_PROPS;
4279 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4281 else if (display_ellipsis_p)
4283 /* Make sure that the glyphs of the ellipsis will get
4284 correct `charpos' values. If we would not update
4285 it->position here, the glyphs would belong to the
4286 last visible character _before_ the invisible
4287 text, which confuses `set_cursor_from_row'.
4289 We use the last invisible position instead of the
4290 first because this way the cursor is always drawn on
4291 the first "." of the ellipsis, whenever PT is inside
4292 the invisible text. Otherwise the cursor would be
4293 placed _after_ the ellipsis when the point is after the
4294 first invisible character. */
4295 if (!STRINGP (it->object))
4297 it->position.charpos = newpos - 1;
4298 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4300 it->ellipsis_p = 1;
4301 /* Let the ellipsis display before
4302 considering any properties of the following char.
4303 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4304 handled = HANDLED_RETURN;
4309 return handled;
4313 /* Make iterator IT return `...' next.
4314 Replaces LEN characters from buffer. */
4316 static void
4317 setup_for_ellipsis (struct it *it, int len)
4319 /* Use the display table definition for `...'. Invalid glyphs
4320 will be handled by the method returning elements from dpvec. */
4321 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4323 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4324 it->dpvec = v->contents;
4325 it->dpend = v->contents + v->header.size;
4327 else
4329 /* Default `...'. */
4330 it->dpvec = default_invis_vector;
4331 it->dpend = default_invis_vector + 3;
4334 it->dpvec_char_len = len;
4335 it->current.dpvec_index = 0;
4336 it->dpvec_face_id = -1;
4338 /* Remember the current face id in case glyphs specify faces.
4339 IT's face is restored in set_iterator_to_next.
4340 saved_face_id was set to preceding char's face in handle_stop. */
4341 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4342 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4344 it->method = GET_FROM_DISPLAY_VECTOR;
4345 it->ellipsis_p = 1;
4350 /***********************************************************************
4351 'display' property
4352 ***********************************************************************/
4354 /* Set up iterator IT from `display' property at its current position.
4355 Called from handle_stop.
4356 We return HANDLED_RETURN if some part of the display property
4357 overrides the display of the buffer text itself.
4358 Otherwise we return HANDLED_NORMALLY. */
4360 static enum prop_handled
4361 handle_display_prop (struct it *it)
4363 Lisp_Object propval, object, overlay;
4364 struct text_pos *position;
4365 ptrdiff_t bufpos;
4366 /* Nonzero if some property replaces the display of the text itself. */
4367 int display_replaced_p = 0;
4369 if (STRINGP (it->string))
4371 object = it->string;
4372 position = &it->current.string_pos;
4373 bufpos = CHARPOS (it->current.pos);
4375 else
4377 XSETWINDOW (object, it->w);
4378 position = &it->current.pos;
4379 bufpos = CHARPOS (*position);
4382 /* Reset those iterator values set from display property values. */
4383 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4384 it->space_width = Qnil;
4385 it->font_height = Qnil;
4386 it->voffset = 0;
4388 /* We don't support recursive `display' properties, i.e. string
4389 values that have a string `display' property, that have a string
4390 `display' property etc. */
4391 if (!it->string_from_display_prop_p)
4392 it->area = TEXT_AREA;
4394 propval = get_char_property_and_overlay (make_number (position->charpos),
4395 Qdisplay, object, &overlay);
4396 if (NILP (propval))
4397 return HANDLED_NORMALLY;
4398 /* Now OVERLAY is the overlay that gave us this property, or nil
4399 if it was a text property. */
4401 if (!STRINGP (it->string))
4402 object = it->w->buffer;
4404 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4405 position, bufpos,
4406 FRAME_WINDOW_P (it->f));
4408 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4411 /* Subroutine of handle_display_prop. Returns non-zero if the display
4412 specification in SPEC is a replacing specification, i.e. it would
4413 replace the text covered by `display' property with something else,
4414 such as an image or a display string. If SPEC includes any kind or
4415 `(space ...) specification, the value is 2; this is used by
4416 compute_display_string_pos, which see.
4418 See handle_single_display_spec for documentation of arguments.
4419 frame_window_p is non-zero if the window being redisplayed is on a
4420 GUI frame; this argument is used only if IT is NULL, see below.
4422 IT can be NULL, if this is called by the bidi reordering code
4423 through compute_display_string_pos, which see. In that case, this
4424 function only examines SPEC, but does not otherwise "handle" it, in
4425 the sense that it doesn't set up members of IT from the display
4426 spec. */
4427 static int
4428 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4429 Lisp_Object overlay, struct text_pos *position,
4430 ptrdiff_t bufpos, int frame_window_p)
4432 int replacing_p = 0;
4433 int rv;
4435 if (CONSP (spec)
4436 /* Simple specifications. */
4437 && !EQ (XCAR (spec), Qimage)
4438 && !EQ (XCAR (spec), Qspace)
4439 && !EQ (XCAR (spec), Qwhen)
4440 && !EQ (XCAR (spec), Qslice)
4441 && !EQ (XCAR (spec), Qspace_width)
4442 && !EQ (XCAR (spec), Qheight)
4443 && !EQ (XCAR (spec), Qraise)
4444 /* Marginal area specifications. */
4445 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4446 && !EQ (XCAR (spec), Qleft_fringe)
4447 && !EQ (XCAR (spec), Qright_fringe)
4448 && !NILP (XCAR (spec)))
4450 for (; CONSP (spec); spec = XCDR (spec))
4452 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4453 overlay, position, bufpos,
4454 replacing_p, frame_window_p)))
4456 replacing_p = rv;
4457 /* If some text in a string is replaced, `position' no
4458 longer points to the position of `object'. */
4459 if (!it || STRINGP (object))
4460 break;
4464 else if (VECTORP (spec))
4466 ptrdiff_t i;
4467 for (i = 0; i < ASIZE (spec); ++i)
4468 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4469 overlay, position, bufpos,
4470 replacing_p, frame_window_p)))
4472 replacing_p = rv;
4473 /* If some text in a string is replaced, `position' no
4474 longer points to the position of `object'. */
4475 if (!it || STRINGP (object))
4476 break;
4479 else
4481 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4482 position, bufpos, 0,
4483 frame_window_p)))
4484 replacing_p = rv;
4487 return replacing_p;
4490 /* Value is the position of the end of the `display' property starting
4491 at START_POS in OBJECT. */
4493 static struct text_pos
4494 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4496 Lisp_Object end;
4497 struct text_pos end_pos;
4499 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4500 Qdisplay, object, Qnil);
4501 CHARPOS (end_pos) = XFASTINT (end);
4502 if (STRINGP (object))
4503 compute_string_pos (&end_pos, start_pos, it->string);
4504 else
4505 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4507 return end_pos;
4511 /* Set up IT from a single `display' property specification SPEC. OBJECT
4512 is the object in which the `display' property was found. *POSITION
4513 is the position in OBJECT at which the `display' property was found.
4514 BUFPOS is the buffer position of OBJECT (different from POSITION if
4515 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4516 previously saw a display specification which already replaced text
4517 display with something else, for example an image; we ignore such
4518 properties after the first one has been processed.
4520 OVERLAY is the overlay this `display' property came from,
4521 or nil if it was a text property.
4523 If SPEC is a `space' or `image' specification, and in some other
4524 cases too, set *POSITION to the position where the `display'
4525 property ends.
4527 If IT is NULL, only examine the property specification in SPEC, but
4528 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4529 is intended to be displayed in a window on a GUI frame.
4531 Value is non-zero if something was found which replaces the display
4532 of buffer or string text. */
4534 static int
4535 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4536 Lisp_Object overlay, struct text_pos *position,
4537 ptrdiff_t bufpos, int display_replaced_p,
4538 int frame_window_p)
4540 Lisp_Object form;
4541 Lisp_Object location, value;
4542 struct text_pos start_pos = *position;
4543 int valid_p;
4545 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4546 If the result is non-nil, use VALUE instead of SPEC. */
4547 form = Qt;
4548 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4550 spec = XCDR (spec);
4551 if (!CONSP (spec))
4552 return 0;
4553 form = XCAR (spec);
4554 spec = XCDR (spec);
4557 if (!NILP (form) && !EQ (form, Qt))
4559 ptrdiff_t count = SPECPDL_INDEX ();
4560 struct gcpro gcpro1;
4562 /* Bind `object' to the object having the `display' property, a
4563 buffer or string. Bind `position' to the position in the
4564 object where the property was found, and `buffer-position'
4565 to the current position in the buffer. */
4567 if (NILP (object))
4568 XSETBUFFER (object, current_buffer);
4569 specbind (Qobject, object);
4570 specbind (Qposition, make_number (CHARPOS (*position)));
4571 specbind (Qbuffer_position, make_number (bufpos));
4572 GCPRO1 (form);
4573 form = safe_eval (form);
4574 UNGCPRO;
4575 unbind_to (count, Qnil);
4578 if (NILP (form))
4579 return 0;
4581 /* Handle `(height HEIGHT)' specifications. */
4582 if (CONSP (spec)
4583 && EQ (XCAR (spec), Qheight)
4584 && CONSP (XCDR (spec)))
4586 if (it)
4588 if (!FRAME_WINDOW_P (it->f))
4589 return 0;
4591 it->font_height = XCAR (XCDR (spec));
4592 if (!NILP (it->font_height))
4594 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4595 int new_height = -1;
4597 if (CONSP (it->font_height)
4598 && (EQ (XCAR (it->font_height), Qplus)
4599 || EQ (XCAR (it->font_height), Qminus))
4600 && CONSP (XCDR (it->font_height))
4601 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4603 /* `(+ N)' or `(- N)' where N is an integer. */
4604 int steps = XINT (XCAR (XCDR (it->font_height)));
4605 if (EQ (XCAR (it->font_height), Qplus))
4606 steps = - steps;
4607 it->face_id = smaller_face (it->f, it->face_id, steps);
4609 else if (FUNCTIONP (it->font_height))
4611 /* Call function with current height as argument.
4612 Value is the new height. */
4613 Lisp_Object height;
4614 height = safe_call1 (it->font_height,
4615 face->lface[LFACE_HEIGHT_INDEX]);
4616 if (NUMBERP (height))
4617 new_height = XFLOATINT (height);
4619 else if (NUMBERP (it->font_height))
4621 /* Value is a multiple of the canonical char height. */
4622 struct face *f;
4624 f = FACE_FROM_ID (it->f,
4625 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4626 new_height = (XFLOATINT (it->font_height)
4627 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4629 else
4631 /* Evaluate IT->font_height with `height' bound to the
4632 current specified height to get the new height. */
4633 ptrdiff_t count = SPECPDL_INDEX ();
4635 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4636 value = safe_eval (it->font_height);
4637 unbind_to (count, Qnil);
4639 if (NUMBERP (value))
4640 new_height = XFLOATINT (value);
4643 if (new_height > 0)
4644 it->face_id = face_with_height (it->f, it->face_id, new_height);
4648 return 0;
4651 /* Handle `(space-width WIDTH)'. */
4652 if (CONSP (spec)
4653 && EQ (XCAR (spec), Qspace_width)
4654 && CONSP (XCDR (spec)))
4656 if (it)
4658 if (!FRAME_WINDOW_P (it->f))
4659 return 0;
4661 value = XCAR (XCDR (spec));
4662 if (NUMBERP (value) && XFLOATINT (value) > 0)
4663 it->space_width = value;
4666 return 0;
4669 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4670 if (CONSP (spec)
4671 && EQ (XCAR (spec), Qslice))
4673 Lisp_Object tem;
4675 if (it)
4677 if (!FRAME_WINDOW_P (it->f))
4678 return 0;
4680 if (tem = XCDR (spec), CONSP (tem))
4682 it->slice.x = XCAR (tem);
4683 if (tem = XCDR (tem), CONSP (tem))
4685 it->slice.y = XCAR (tem);
4686 if (tem = XCDR (tem), CONSP (tem))
4688 it->slice.width = XCAR (tem);
4689 if (tem = XCDR (tem), CONSP (tem))
4690 it->slice.height = XCAR (tem);
4696 return 0;
4699 /* Handle `(raise FACTOR)'. */
4700 if (CONSP (spec)
4701 && EQ (XCAR (spec), Qraise)
4702 && CONSP (XCDR (spec)))
4704 if (it)
4706 if (!FRAME_WINDOW_P (it->f))
4707 return 0;
4709 #ifdef HAVE_WINDOW_SYSTEM
4710 value = XCAR (XCDR (spec));
4711 if (NUMBERP (value))
4713 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4714 it->voffset = - (XFLOATINT (value)
4715 * (FONT_HEIGHT (face->font)));
4717 #endif /* HAVE_WINDOW_SYSTEM */
4720 return 0;
4723 /* Don't handle the other kinds of display specifications
4724 inside a string that we got from a `display' property. */
4725 if (it && it->string_from_display_prop_p)
4726 return 0;
4728 /* Characters having this form of property are not displayed, so
4729 we have to find the end of the property. */
4730 if (it)
4732 start_pos = *position;
4733 *position = display_prop_end (it, object, start_pos);
4735 value = Qnil;
4737 /* Stop the scan at that end position--we assume that all
4738 text properties change there. */
4739 if (it)
4740 it->stop_charpos = position->charpos;
4742 /* Handle `(left-fringe BITMAP [FACE])'
4743 and `(right-fringe BITMAP [FACE])'. */
4744 if (CONSP (spec)
4745 && (EQ (XCAR (spec), Qleft_fringe)
4746 || EQ (XCAR (spec), Qright_fringe))
4747 && CONSP (XCDR (spec)))
4749 int fringe_bitmap;
4751 if (it)
4753 if (!FRAME_WINDOW_P (it->f))
4754 /* If we return here, POSITION has been advanced
4755 across the text with this property. */
4757 /* Synchronize the bidi iterator with POSITION. This is
4758 needed because we are not going to push the iterator
4759 on behalf of this display property, so there will be
4760 no pop_it call to do this synchronization for us. */
4761 if (it->bidi_p)
4763 it->position = *position;
4764 iterate_out_of_display_property (it);
4765 *position = it->position;
4767 return 1;
4770 else if (!frame_window_p)
4771 return 1;
4773 #ifdef HAVE_WINDOW_SYSTEM
4774 value = XCAR (XCDR (spec));
4775 if (!SYMBOLP (value)
4776 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4777 /* If we return here, POSITION has been advanced
4778 across the text with this property. */
4780 if (it && it->bidi_p)
4782 it->position = *position;
4783 iterate_out_of_display_property (it);
4784 *position = it->position;
4786 return 1;
4789 if (it)
4791 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4793 if (CONSP (XCDR (XCDR (spec))))
4795 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4796 int face_id2 = lookup_derived_face (it->f, face_name,
4797 FRINGE_FACE_ID, 0);
4798 if (face_id2 >= 0)
4799 face_id = face_id2;
4802 /* Save current settings of IT so that we can restore them
4803 when we are finished with the glyph property value. */
4804 push_it (it, position);
4806 it->area = TEXT_AREA;
4807 it->what = IT_IMAGE;
4808 it->image_id = -1; /* no image */
4809 it->position = start_pos;
4810 it->object = NILP (object) ? it->w->buffer : object;
4811 it->method = GET_FROM_IMAGE;
4812 it->from_overlay = Qnil;
4813 it->face_id = face_id;
4814 it->from_disp_prop_p = 1;
4816 /* Say that we haven't consumed the characters with
4817 `display' property yet. The call to pop_it in
4818 set_iterator_to_next will clean this up. */
4819 *position = start_pos;
4821 if (EQ (XCAR (spec), Qleft_fringe))
4823 it->left_user_fringe_bitmap = fringe_bitmap;
4824 it->left_user_fringe_face_id = face_id;
4826 else
4828 it->right_user_fringe_bitmap = fringe_bitmap;
4829 it->right_user_fringe_face_id = face_id;
4832 #endif /* HAVE_WINDOW_SYSTEM */
4833 return 1;
4836 /* Prepare to handle `((margin left-margin) ...)',
4837 `((margin right-margin) ...)' and `((margin nil) ...)'
4838 prefixes for display specifications. */
4839 location = Qunbound;
4840 if (CONSP (spec) && CONSP (XCAR (spec)))
4842 Lisp_Object tem;
4844 value = XCDR (spec);
4845 if (CONSP (value))
4846 value = XCAR (value);
4848 tem = XCAR (spec);
4849 if (EQ (XCAR (tem), Qmargin)
4850 && (tem = XCDR (tem),
4851 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4852 (NILP (tem)
4853 || EQ (tem, Qleft_margin)
4854 || EQ (tem, Qright_margin))))
4855 location = tem;
4858 if (EQ (location, Qunbound))
4860 location = Qnil;
4861 value = spec;
4864 /* After this point, VALUE is the property after any
4865 margin prefix has been stripped. It must be a string,
4866 an image specification, or `(space ...)'.
4868 LOCATION specifies where to display: `left-margin',
4869 `right-margin' or nil. */
4871 valid_p = (STRINGP (value)
4872 #ifdef HAVE_WINDOW_SYSTEM
4873 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4874 && valid_image_p (value))
4875 #endif /* not HAVE_WINDOW_SYSTEM */
4876 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4878 if (valid_p && !display_replaced_p)
4880 int retval = 1;
4882 if (!it)
4884 /* Callers need to know whether the display spec is any kind
4885 of `(space ...)' spec that is about to affect text-area
4886 display. */
4887 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4888 retval = 2;
4889 return retval;
4892 /* Save current settings of IT so that we can restore them
4893 when we are finished with the glyph property value. */
4894 push_it (it, position);
4895 it->from_overlay = overlay;
4896 it->from_disp_prop_p = 1;
4898 if (NILP (location))
4899 it->area = TEXT_AREA;
4900 else if (EQ (location, Qleft_margin))
4901 it->area = LEFT_MARGIN_AREA;
4902 else
4903 it->area = RIGHT_MARGIN_AREA;
4905 if (STRINGP (value))
4907 it->string = value;
4908 it->multibyte_p = STRING_MULTIBYTE (it->string);
4909 it->current.overlay_string_index = -1;
4910 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4911 it->end_charpos = it->string_nchars = SCHARS (it->string);
4912 it->method = GET_FROM_STRING;
4913 it->stop_charpos = 0;
4914 it->prev_stop = 0;
4915 it->base_level_stop = 0;
4916 it->string_from_display_prop_p = 1;
4917 /* Say that we haven't consumed the characters with
4918 `display' property yet. The call to pop_it in
4919 set_iterator_to_next will clean this up. */
4920 if (BUFFERP (object))
4921 *position = start_pos;
4923 /* Force paragraph direction to be that of the parent
4924 object. If the parent object's paragraph direction is
4925 not yet determined, default to L2R. */
4926 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4927 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4928 else
4929 it->paragraph_embedding = L2R;
4931 /* Set up the bidi iterator for this display string. */
4932 if (it->bidi_p)
4934 it->bidi_it.string.lstring = it->string;
4935 it->bidi_it.string.s = NULL;
4936 it->bidi_it.string.schars = it->end_charpos;
4937 it->bidi_it.string.bufpos = bufpos;
4938 it->bidi_it.string.from_disp_str = 1;
4939 it->bidi_it.string.unibyte = !it->multibyte_p;
4940 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4943 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4945 it->method = GET_FROM_STRETCH;
4946 it->object = value;
4947 *position = it->position = start_pos;
4948 retval = 1 + (it->area == TEXT_AREA);
4950 #ifdef HAVE_WINDOW_SYSTEM
4951 else
4953 it->what = IT_IMAGE;
4954 it->image_id = lookup_image (it->f, value);
4955 it->position = start_pos;
4956 it->object = NILP (object) ? it->w->buffer : object;
4957 it->method = GET_FROM_IMAGE;
4959 /* Say that we haven't consumed the characters with
4960 `display' property yet. The call to pop_it in
4961 set_iterator_to_next will clean this up. */
4962 *position = start_pos;
4964 #endif /* HAVE_WINDOW_SYSTEM */
4966 return retval;
4969 /* Invalid property or property not supported. Restore
4970 POSITION to what it was before. */
4971 *position = start_pos;
4972 return 0;
4975 /* Check if PROP is a display property value whose text should be
4976 treated as intangible. OVERLAY is the overlay from which PROP
4977 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4978 specify the buffer position covered by PROP. */
4981 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4982 ptrdiff_t charpos, ptrdiff_t bytepos)
4984 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4985 struct text_pos position;
4987 SET_TEXT_POS (position, charpos, bytepos);
4988 return handle_display_spec (NULL, prop, Qnil, overlay,
4989 &position, charpos, frame_window_p);
4993 /* Return 1 if PROP is a display sub-property value containing STRING.
4995 Implementation note: this and the following function are really
4996 special cases of handle_display_spec and
4997 handle_single_display_spec, and should ideally use the same code.
4998 Until they do, these two pairs must be consistent and must be
4999 modified in sync. */
5001 static int
5002 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5004 if (EQ (string, prop))
5005 return 1;
5007 /* Skip over `when FORM'. */
5008 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5010 prop = XCDR (prop);
5011 if (!CONSP (prop))
5012 return 0;
5013 /* Actually, the condition following `when' should be eval'ed,
5014 like handle_single_display_spec does, and we should return
5015 zero if it evaluates to nil. However, this function is
5016 called only when the buffer was already displayed and some
5017 glyph in the glyph matrix was found to come from a display
5018 string. Therefore, the condition was already evaluated, and
5019 the result was non-nil, otherwise the display string wouldn't
5020 have been displayed and we would have never been called for
5021 this property. Thus, we can skip the evaluation and assume
5022 its result is non-nil. */
5023 prop = XCDR (prop);
5026 if (CONSP (prop))
5027 /* Skip over `margin LOCATION'. */
5028 if (EQ (XCAR (prop), Qmargin))
5030 prop = XCDR (prop);
5031 if (!CONSP (prop))
5032 return 0;
5034 prop = XCDR (prop);
5035 if (!CONSP (prop))
5036 return 0;
5039 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5043 /* Return 1 if STRING appears in the `display' property PROP. */
5045 static int
5046 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5048 if (CONSP (prop)
5049 && !EQ (XCAR (prop), Qwhen)
5050 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5052 /* A list of sub-properties. */
5053 while (CONSP (prop))
5055 if (single_display_spec_string_p (XCAR (prop), string))
5056 return 1;
5057 prop = XCDR (prop);
5060 else if (VECTORP (prop))
5062 /* A vector of sub-properties. */
5063 ptrdiff_t i;
5064 for (i = 0; i < ASIZE (prop); ++i)
5065 if (single_display_spec_string_p (AREF (prop, i), string))
5066 return 1;
5068 else
5069 return single_display_spec_string_p (prop, string);
5071 return 0;
5074 /* Look for STRING in overlays and text properties in the current
5075 buffer, between character positions FROM and TO (excluding TO).
5076 BACK_P non-zero means look back (in this case, TO is supposed to be
5077 less than FROM).
5078 Value is the first character position where STRING was found, or
5079 zero if it wasn't found before hitting TO.
5081 This function may only use code that doesn't eval because it is
5082 called asynchronously from note_mouse_highlight. */
5084 static ptrdiff_t
5085 string_buffer_position_lim (Lisp_Object string,
5086 ptrdiff_t from, ptrdiff_t to, int back_p)
5088 Lisp_Object limit, prop, pos;
5089 int found = 0;
5091 pos = make_number (max (from, BEGV));
5093 if (!back_p) /* looking forward */
5095 limit = make_number (min (to, ZV));
5096 while (!found && !EQ (pos, limit))
5098 prop = Fget_char_property (pos, Qdisplay, Qnil);
5099 if (!NILP (prop) && display_prop_string_p (prop, string))
5100 found = 1;
5101 else
5102 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5103 limit);
5106 else /* looking back */
5108 limit = make_number (max (to, BEGV));
5109 while (!found && !EQ (pos, limit))
5111 prop = Fget_char_property (pos, Qdisplay, Qnil);
5112 if (!NILP (prop) && display_prop_string_p (prop, string))
5113 found = 1;
5114 else
5115 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5116 limit);
5120 return found ? XINT (pos) : 0;
5123 /* Determine which buffer position in current buffer STRING comes from.
5124 AROUND_CHARPOS is an approximate position where it could come from.
5125 Value is the buffer position or 0 if it couldn't be determined.
5127 This function is necessary because we don't record buffer positions
5128 in glyphs generated from strings (to keep struct glyph small).
5129 This function may only use code that doesn't eval because it is
5130 called asynchronously from note_mouse_highlight. */
5132 static ptrdiff_t
5133 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5135 const int MAX_DISTANCE = 1000;
5136 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5137 around_charpos + MAX_DISTANCE,
5140 if (!found)
5141 found = string_buffer_position_lim (string, around_charpos,
5142 around_charpos - MAX_DISTANCE, 1);
5143 return found;
5148 /***********************************************************************
5149 `composition' property
5150 ***********************************************************************/
5152 /* Set up iterator IT from `composition' property at its current
5153 position. Called from handle_stop. */
5155 static enum prop_handled
5156 handle_composition_prop (struct it *it)
5158 Lisp_Object prop, string;
5159 ptrdiff_t pos, pos_byte, start, end;
5161 if (STRINGP (it->string))
5163 unsigned char *s;
5165 pos = IT_STRING_CHARPOS (*it);
5166 pos_byte = IT_STRING_BYTEPOS (*it);
5167 string = it->string;
5168 s = SDATA (string) + pos_byte;
5169 it->c = STRING_CHAR (s);
5171 else
5173 pos = IT_CHARPOS (*it);
5174 pos_byte = IT_BYTEPOS (*it);
5175 string = Qnil;
5176 it->c = FETCH_CHAR (pos_byte);
5179 /* If there's a valid composition and point is not inside of the
5180 composition (in the case that the composition is from the current
5181 buffer), draw a glyph composed from the composition components. */
5182 if (find_composition (pos, -1, &start, &end, &prop, string)
5183 && COMPOSITION_VALID_P (start, end, prop)
5184 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5186 if (start < pos)
5187 /* As we can't handle this situation (perhaps font-lock added
5188 a new composition), we just return here hoping that next
5189 redisplay will detect this composition much earlier. */
5190 return HANDLED_NORMALLY;
5191 if (start != pos)
5193 if (STRINGP (it->string))
5194 pos_byte = string_char_to_byte (it->string, start);
5195 else
5196 pos_byte = CHAR_TO_BYTE (start);
5198 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5199 prop, string);
5201 if (it->cmp_it.id >= 0)
5203 it->cmp_it.ch = -1;
5204 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5205 it->cmp_it.nglyphs = -1;
5209 return HANDLED_NORMALLY;
5214 /***********************************************************************
5215 Overlay strings
5216 ***********************************************************************/
5218 /* The following structure is used to record overlay strings for
5219 later sorting in load_overlay_strings. */
5221 struct overlay_entry
5223 Lisp_Object overlay;
5224 Lisp_Object string;
5225 EMACS_INT priority;
5226 int after_string_p;
5230 /* Set up iterator IT from overlay strings at its current position.
5231 Called from handle_stop. */
5233 static enum prop_handled
5234 handle_overlay_change (struct it *it)
5236 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5237 return HANDLED_RECOMPUTE_PROPS;
5238 else
5239 return HANDLED_NORMALLY;
5243 /* Set up the next overlay string for delivery by IT, if there is an
5244 overlay string to deliver. Called by set_iterator_to_next when the
5245 end of the current overlay string is reached. If there are more
5246 overlay strings to display, IT->string and
5247 IT->current.overlay_string_index are set appropriately here.
5248 Otherwise IT->string is set to nil. */
5250 static void
5251 next_overlay_string (struct it *it)
5253 ++it->current.overlay_string_index;
5254 if (it->current.overlay_string_index == it->n_overlay_strings)
5256 /* No more overlay strings. Restore IT's settings to what
5257 they were before overlay strings were processed, and
5258 continue to deliver from current_buffer. */
5260 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5261 pop_it (it);
5262 eassert (it->sp > 0
5263 || (NILP (it->string)
5264 && it->method == GET_FROM_BUFFER
5265 && it->stop_charpos >= BEGV
5266 && it->stop_charpos <= it->end_charpos));
5267 it->current.overlay_string_index = -1;
5268 it->n_overlay_strings = 0;
5269 it->overlay_strings_charpos = -1;
5270 /* If there's an empty display string on the stack, pop the
5271 stack, to resync the bidi iterator with IT's position. Such
5272 empty strings are pushed onto the stack in
5273 get_overlay_strings_1. */
5274 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5275 pop_it (it);
5277 /* If we're at the end of the buffer, record that we have
5278 processed the overlay strings there already, so that
5279 next_element_from_buffer doesn't try it again. */
5280 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5281 it->overlay_strings_at_end_processed_p = 1;
5283 else
5285 /* There are more overlay strings to process. If
5286 IT->current.overlay_string_index has advanced to a position
5287 where we must load IT->overlay_strings with more strings, do
5288 it. We must load at the IT->overlay_strings_charpos where
5289 IT->n_overlay_strings was originally computed; when invisible
5290 text is present, this might not be IT_CHARPOS (Bug#7016). */
5291 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5293 if (it->current.overlay_string_index && i == 0)
5294 load_overlay_strings (it, it->overlay_strings_charpos);
5296 /* Initialize IT to deliver display elements from the overlay
5297 string. */
5298 it->string = it->overlay_strings[i];
5299 it->multibyte_p = STRING_MULTIBYTE (it->string);
5300 SET_TEXT_POS (it->current.string_pos, 0, 0);
5301 it->method = GET_FROM_STRING;
5302 it->stop_charpos = 0;
5303 if (it->cmp_it.stop_pos >= 0)
5304 it->cmp_it.stop_pos = 0;
5305 it->prev_stop = 0;
5306 it->base_level_stop = 0;
5308 /* Set up the bidi iterator for this overlay string. */
5309 if (it->bidi_p)
5311 it->bidi_it.string.lstring = it->string;
5312 it->bidi_it.string.s = NULL;
5313 it->bidi_it.string.schars = SCHARS (it->string);
5314 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5315 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5316 it->bidi_it.string.unibyte = !it->multibyte_p;
5317 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5321 CHECK_IT (it);
5325 /* Compare two overlay_entry structures E1 and E2. Used as a
5326 comparison function for qsort in load_overlay_strings. Overlay
5327 strings for the same position are sorted so that
5329 1. All after-strings come in front of before-strings, except
5330 when they come from the same overlay.
5332 2. Within after-strings, strings are sorted so that overlay strings
5333 from overlays with higher priorities come first.
5335 2. Within before-strings, strings are sorted so that overlay
5336 strings from overlays with higher priorities come last.
5338 Value is analogous to strcmp. */
5341 static int
5342 compare_overlay_entries (const void *e1, const void *e2)
5344 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5345 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5346 int result;
5348 if (entry1->after_string_p != entry2->after_string_p)
5350 /* Let after-strings appear in front of before-strings if
5351 they come from different overlays. */
5352 if (EQ (entry1->overlay, entry2->overlay))
5353 result = entry1->after_string_p ? 1 : -1;
5354 else
5355 result = entry1->after_string_p ? -1 : 1;
5357 else if (entry1->priority != entry2->priority)
5359 if (entry1->after_string_p)
5360 /* After-strings sorted in order of decreasing priority. */
5361 result = entry2->priority < entry1->priority ? -1 : 1;
5362 else
5363 /* Before-strings sorted in order of increasing priority. */
5364 result = entry1->priority < entry2->priority ? -1 : 1;
5366 else
5367 result = 0;
5369 return result;
5373 /* Load the vector IT->overlay_strings with overlay strings from IT's
5374 current buffer position, or from CHARPOS if that is > 0. Set
5375 IT->n_overlays to the total number of overlay strings found.
5377 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5378 a time. On entry into load_overlay_strings,
5379 IT->current.overlay_string_index gives the number of overlay
5380 strings that have already been loaded by previous calls to this
5381 function.
5383 IT->add_overlay_start contains an additional overlay start
5384 position to consider for taking overlay strings from, if non-zero.
5385 This position comes into play when the overlay has an `invisible'
5386 property, and both before and after-strings. When we've skipped to
5387 the end of the overlay, because of its `invisible' property, we
5388 nevertheless want its before-string to appear.
5389 IT->add_overlay_start will contain the overlay start position
5390 in this case.
5392 Overlay strings are sorted so that after-string strings come in
5393 front of before-string strings. Within before and after-strings,
5394 strings are sorted by overlay priority. See also function
5395 compare_overlay_entries. */
5397 static void
5398 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5400 Lisp_Object overlay, window, str, invisible;
5401 struct Lisp_Overlay *ov;
5402 ptrdiff_t start, end;
5403 ptrdiff_t size = 20;
5404 ptrdiff_t n = 0, i, j;
5405 int invis_p;
5406 struct overlay_entry *entries = alloca (size * sizeof *entries);
5407 USE_SAFE_ALLOCA;
5409 if (charpos <= 0)
5410 charpos = IT_CHARPOS (*it);
5412 /* Append the overlay string STRING of overlay OVERLAY to vector
5413 `entries' which has size `size' and currently contains `n'
5414 elements. AFTER_P non-zero means STRING is an after-string of
5415 OVERLAY. */
5416 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5417 do \
5419 Lisp_Object priority; \
5421 if (n == size) \
5423 struct overlay_entry *old = entries; \
5424 SAFE_NALLOCA (entries, 2, size); \
5425 memcpy (entries, old, size * sizeof *entries); \
5426 size *= 2; \
5429 entries[n].string = (STRING); \
5430 entries[n].overlay = (OVERLAY); \
5431 priority = Foverlay_get ((OVERLAY), Qpriority); \
5432 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5433 entries[n].after_string_p = (AFTER_P); \
5434 ++n; \
5436 while (0)
5438 /* Process overlay before the overlay center. */
5439 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5441 XSETMISC (overlay, ov);
5442 eassert (OVERLAYP (overlay));
5443 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5444 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5446 if (end < charpos)
5447 break;
5449 /* Skip this overlay if it doesn't start or end at IT's current
5450 position. */
5451 if (end != charpos && start != charpos)
5452 continue;
5454 /* Skip this overlay if it doesn't apply to IT->w. */
5455 window = Foverlay_get (overlay, Qwindow);
5456 if (WINDOWP (window) && XWINDOW (window) != it->w)
5457 continue;
5459 /* If the text ``under'' the overlay is invisible, both before-
5460 and after-strings from this overlay are visible; start and
5461 end position are indistinguishable. */
5462 invisible = Foverlay_get (overlay, Qinvisible);
5463 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5465 /* If overlay has a non-empty before-string, record it. */
5466 if ((start == charpos || (end == charpos && invis_p))
5467 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5468 && SCHARS (str))
5469 RECORD_OVERLAY_STRING (overlay, str, 0);
5471 /* If overlay has a non-empty after-string, record it. */
5472 if ((end == charpos || (start == charpos && invis_p))
5473 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5474 && SCHARS (str))
5475 RECORD_OVERLAY_STRING (overlay, str, 1);
5478 /* Process overlays after the overlay center. */
5479 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5481 XSETMISC (overlay, ov);
5482 eassert (OVERLAYP (overlay));
5483 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5484 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5486 if (start > charpos)
5487 break;
5489 /* Skip this overlay if it doesn't start or end at IT's current
5490 position. */
5491 if (end != charpos && start != charpos)
5492 continue;
5494 /* Skip this overlay if it doesn't apply to IT->w. */
5495 window = Foverlay_get (overlay, Qwindow);
5496 if (WINDOWP (window) && XWINDOW (window) != it->w)
5497 continue;
5499 /* If the text ``under'' the overlay is invisible, it has a zero
5500 dimension, and both before- and after-strings apply. */
5501 invisible = Foverlay_get (overlay, Qinvisible);
5502 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5504 /* If overlay has a non-empty before-string, record it. */
5505 if ((start == charpos || (end == charpos && invis_p))
5506 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5507 && SCHARS (str))
5508 RECORD_OVERLAY_STRING (overlay, str, 0);
5510 /* If overlay has a non-empty after-string, record it. */
5511 if ((end == charpos || (start == charpos && invis_p))
5512 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5513 && SCHARS (str))
5514 RECORD_OVERLAY_STRING (overlay, str, 1);
5517 #undef RECORD_OVERLAY_STRING
5519 /* Sort entries. */
5520 if (n > 1)
5521 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5523 /* Record number of overlay strings, and where we computed it. */
5524 it->n_overlay_strings = n;
5525 it->overlay_strings_charpos = charpos;
5527 /* IT->current.overlay_string_index is the number of overlay strings
5528 that have already been consumed by IT. Copy some of the
5529 remaining overlay strings to IT->overlay_strings. */
5530 i = 0;
5531 j = it->current.overlay_string_index;
5532 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5534 it->overlay_strings[i] = entries[j].string;
5535 it->string_overlays[i++] = entries[j++].overlay;
5538 CHECK_IT (it);
5539 SAFE_FREE ();
5543 /* Get the first chunk of overlay strings at IT's current buffer
5544 position, or at CHARPOS if that is > 0. Value is non-zero if at
5545 least one overlay string was found. */
5547 static int
5548 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5550 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5551 process. This fills IT->overlay_strings with strings, and sets
5552 IT->n_overlay_strings to the total number of strings to process.
5553 IT->pos.overlay_string_index has to be set temporarily to zero
5554 because load_overlay_strings needs this; it must be set to -1
5555 when no overlay strings are found because a zero value would
5556 indicate a position in the first overlay string. */
5557 it->current.overlay_string_index = 0;
5558 load_overlay_strings (it, charpos);
5560 /* If we found overlay strings, set up IT to deliver display
5561 elements from the first one. Otherwise set up IT to deliver
5562 from current_buffer. */
5563 if (it->n_overlay_strings)
5565 /* Make sure we know settings in current_buffer, so that we can
5566 restore meaningful values when we're done with the overlay
5567 strings. */
5568 if (compute_stop_p)
5569 compute_stop_pos (it);
5570 eassert (it->face_id >= 0);
5572 /* Save IT's settings. They are restored after all overlay
5573 strings have been processed. */
5574 eassert (!compute_stop_p || it->sp == 0);
5576 /* When called from handle_stop, there might be an empty display
5577 string loaded. In that case, don't bother saving it. But
5578 don't use this optimization with the bidi iterator, since we
5579 need the corresponding pop_it call to resync the bidi
5580 iterator's position with IT's position, after we are done
5581 with the overlay strings. (The corresponding call to pop_it
5582 in case of an empty display string is in
5583 next_overlay_string.) */
5584 if (!(!it->bidi_p
5585 && STRINGP (it->string) && !SCHARS (it->string)))
5586 push_it (it, NULL);
5588 /* Set up IT to deliver display elements from the first overlay
5589 string. */
5590 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5591 it->string = it->overlay_strings[0];
5592 it->from_overlay = Qnil;
5593 it->stop_charpos = 0;
5594 eassert (STRINGP (it->string));
5595 it->end_charpos = SCHARS (it->string);
5596 it->prev_stop = 0;
5597 it->base_level_stop = 0;
5598 it->multibyte_p = STRING_MULTIBYTE (it->string);
5599 it->method = GET_FROM_STRING;
5600 it->from_disp_prop_p = 0;
5602 /* Force paragraph direction to be that of the parent
5603 buffer. */
5604 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5605 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5606 else
5607 it->paragraph_embedding = L2R;
5609 /* Set up the bidi iterator for this overlay string. */
5610 if (it->bidi_p)
5612 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5614 it->bidi_it.string.lstring = it->string;
5615 it->bidi_it.string.s = NULL;
5616 it->bidi_it.string.schars = SCHARS (it->string);
5617 it->bidi_it.string.bufpos = pos;
5618 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5619 it->bidi_it.string.unibyte = !it->multibyte_p;
5620 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5622 return 1;
5625 it->current.overlay_string_index = -1;
5626 return 0;
5629 static int
5630 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5632 it->string = Qnil;
5633 it->method = GET_FROM_BUFFER;
5635 (void) get_overlay_strings_1 (it, charpos, 1);
5637 CHECK_IT (it);
5639 /* Value is non-zero if we found at least one overlay string. */
5640 return STRINGP (it->string);
5645 /***********************************************************************
5646 Saving and restoring state
5647 ***********************************************************************/
5649 /* Save current settings of IT on IT->stack. Called, for example,
5650 before setting up IT for an overlay string, to be able to restore
5651 IT's settings to what they were after the overlay string has been
5652 processed. If POSITION is non-NULL, it is the position to save on
5653 the stack instead of IT->position. */
5655 static void
5656 push_it (struct it *it, struct text_pos *position)
5658 struct iterator_stack_entry *p;
5660 eassert (it->sp < IT_STACK_SIZE);
5661 p = it->stack + it->sp;
5663 p->stop_charpos = it->stop_charpos;
5664 p->prev_stop = it->prev_stop;
5665 p->base_level_stop = it->base_level_stop;
5666 p->cmp_it = it->cmp_it;
5667 eassert (it->face_id >= 0);
5668 p->face_id = it->face_id;
5669 p->string = it->string;
5670 p->method = it->method;
5671 p->from_overlay = it->from_overlay;
5672 switch (p->method)
5674 case GET_FROM_IMAGE:
5675 p->u.image.object = it->object;
5676 p->u.image.image_id = it->image_id;
5677 p->u.image.slice = it->slice;
5678 break;
5679 case GET_FROM_STRETCH:
5680 p->u.stretch.object = it->object;
5681 break;
5683 p->position = position ? *position : it->position;
5684 p->current = it->current;
5685 p->end_charpos = it->end_charpos;
5686 p->string_nchars = it->string_nchars;
5687 p->area = it->area;
5688 p->multibyte_p = it->multibyte_p;
5689 p->avoid_cursor_p = it->avoid_cursor_p;
5690 p->space_width = it->space_width;
5691 p->font_height = it->font_height;
5692 p->voffset = it->voffset;
5693 p->string_from_display_prop_p = it->string_from_display_prop_p;
5694 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5695 p->display_ellipsis_p = 0;
5696 p->line_wrap = it->line_wrap;
5697 p->bidi_p = it->bidi_p;
5698 p->paragraph_embedding = it->paragraph_embedding;
5699 p->from_disp_prop_p = it->from_disp_prop_p;
5700 ++it->sp;
5702 /* Save the state of the bidi iterator as well. */
5703 if (it->bidi_p)
5704 bidi_push_it (&it->bidi_it);
5707 static void
5708 iterate_out_of_display_property (struct it *it)
5710 int buffer_p = !STRINGP (it->string);
5711 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5712 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5714 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5716 /* Maybe initialize paragraph direction. If we are at the beginning
5717 of a new paragraph, next_element_from_buffer may not have a
5718 chance to do that. */
5719 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5720 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5721 /* prev_stop can be zero, so check against BEGV as well. */
5722 while (it->bidi_it.charpos >= bob
5723 && it->prev_stop <= it->bidi_it.charpos
5724 && it->bidi_it.charpos < CHARPOS (it->position)
5725 && it->bidi_it.charpos < eob)
5726 bidi_move_to_visually_next (&it->bidi_it);
5727 /* Record the stop_pos we just crossed, for when we cross it
5728 back, maybe. */
5729 if (it->bidi_it.charpos > CHARPOS (it->position))
5730 it->prev_stop = CHARPOS (it->position);
5731 /* If we ended up not where pop_it put us, resync IT's
5732 positional members with the bidi iterator. */
5733 if (it->bidi_it.charpos != CHARPOS (it->position))
5734 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5735 if (buffer_p)
5736 it->current.pos = it->position;
5737 else
5738 it->current.string_pos = it->position;
5741 /* Restore IT's settings from IT->stack. Called, for example, when no
5742 more overlay strings must be processed, and we return to delivering
5743 display elements from a buffer, or when the end of a string from a
5744 `display' property is reached and we return to delivering display
5745 elements from an overlay string, or from a buffer. */
5747 static void
5748 pop_it (struct it *it)
5750 struct iterator_stack_entry *p;
5751 int from_display_prop = it->from_disp_prop_p;
5753 eassert (it->sp > 0);
5754 --it->sp;
5755 p = it->stack + it->sp;
5756 it->stop_charpos = p->stop_charpos;
5757 it->prev_stop = p->prev_stop;
5758 it->base_level_stop = p->base_level_stop;
5759 it->cmp_it = p->cmp_it;
5760 it->face_id = p->face_id;
5761 it->current = p->current;
5762 it->position = p->position;
5763 it->string = p->string;
5764 it->from_overlay = p->from_overlay;
5765 if (NILP (it->string))
5766 SET_TEXT_POS (it->current.string_pos, -1, -1);
5767 it->method = p->method;
5768 switch (it->method)
5770 case GET_FROM_IMAGE:
5771 it->image_id = p->u.image.image_id;
5772 it->object = p->u.image.object;
5773 it->slice = p->u.image.slice;
5774 break;
5775 case GET_FROM_STRETCH:
5776 it->object = p->u.stretch.object;
5777 break;
5778 case GET_FROM_BUFFER:
5779 it->object = it->w->buffer;
5780 break;
5781 case GET_FROM_STRING:
5782 it->object = it->string;
5783 break;
5784 case GET_FROM_DISPLAY_VECTOR:
5785 if (it->s)
5786 it->method = GET_FROM_C_STRING;
5787 else if (STRINGP (it->string))
5788 it->method = GET_FROM_STRING;
5789 else
5791 it->method = GET_FROM_BUFFER;
5792 it->object = it->w->buffer;
5795 it->end_charpos = p->end_charpos;
5796 it->string_nchars = p->string_nchars;
5797 it->area = p->area;
5798 it->multibyte_p = p->multibyte_p;
5799 it->avoid_cursor_p = p->avoid_cursor_p;
5800 it->space_width = p->space_width;
5801 it->font_height = p->font_height;
5802 it->voffset = p->voffset;
5803 it->string_from_display_prop_p = p->string_from_display_prop_p;
5804 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5805 it->line_wrap = p->line_wrap;
5806 it->bidi_p = p->bidi_p;
5807 it->paragraph_embedding = p->paragraph_embedding;
5808 it->from_disp_prop_p = p->from_disp_prop_p;
5809 if (it->bidi_p)
5811 bidi_pop_it (&it->bidi_it);
5812 /* Bidi-iterate until we get out of the portion of text, if any,
5813 covered by a `display' text property or by an overlay with
5814 `display' property. (We cannot just jump there, because the
5815 internal coherency of the bidi iterator state can not be
5816 preserved across such jumps.) We also must determine the
5817 paragraph base direction if the overlay we just processed is
5818 at the beginning of a new paragraph. */
5819 if (from_display_prop
5820 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5821 iterate_out_of_display_property (it);
5823 eassert ((BUFFERP (it->object)
5824 && IT_CHARPOS (*it) == it->bidi_it.charpos
5825 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5826 || (STRINGP (it->object)
5827 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5828 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5829 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5835 /***********************************************************************
5836 Moving over lines
5837 ***********************************************************************/
5839 /* Set IT's current position to the previous line start. */
5841 static void
5842 back_to_previous_line_start (struct it *it)
5844 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5845 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5849 /* Move IT to the next line start.
5851 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5852 we skipped over part of the text (as opposed to moving the iterator
5853 continuously over the text). Otherwise, don't change the value
5854 of *SKIPPED_P.
5856 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5857 iterator on the newline, if it was found.
5859 Newlines may come from buffer text, overlay strings, or strings
5860 displayed via the `display' property. That's the reason we can't
5861 simply use find_next_newline_no_quit.
5863 Note that this function may not skip over invisible text that is so
5864 because of text properties and immediately follows a newline. If
5865 it would, function reseat_at_next_visible_line_start, when called
5866 from set_iterator_to_next, would effectively make invisible
5867 characters following a newline part of the wrong glyph row, which
5868 leads to wrong cursor motion. */
5870 static int
5871 forward_to_next_line_start (struct it *it, int *skipped_p,
5872 struct bidi_it *bidi_it_prev)
5874 ptrdiff_t old_selective;
5875 int newline_found_p, n;
5876 const int MAX_NEWLINE_DISTANCE = 500;
5878 /* If already on a newline, just consume it to avoid unintended
5879 skipping over invisible text below. */
5880 if (it->what == IT_CHARACTER
5881 && it->c == '\n'
5882 && CHARPOS (it->position) == IT_CHARPOS (*it))
5884 if (it->bidi_p && bidi_it_prev)
5885 *bidi_it_prev = it->bidi_it;
5886 set_iterator_to_next (it, 0);
5887 it->c = 0;
5888 return 1;
5891 /* Don't handle selective display in the following. It's (a)
5892 unnecessary because it's done by the caller, and (b) leads to an
5893 infinite recursion because next_element_from_ellipsis indirectly
5894 calls this function. */
5895 old_selective = it->selective;
5896 it->selective = 0;
5898 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5899 from buffer text. */
5900 for (n = newline_found_p = 0;
5901 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5902 n += STRINGP (it->string) ? 0 : 1)
5904 if (!get_next_display_element (it))
5905 return 0;
5906 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5907 if (newline_found_p && it->bidi_p && bidi_it_prev)
5908 *bidi_it_prev = it->bidi_it;
5909 set_iterator_to_next (it, 0);
5912 /* If we didn't find a newline near enough, see if we can use a
5913 short-cut. */
5914 if (!newline_found_p)
5916 ptrdiff_t start = IT_CHARPOS (*it);
5917 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5918 Lisp_Object pos;
5920 eassert (!STRINGP (it->string));
5922 /* If there isn't any `display' property in sight, and no
5923 overlays, we can just use the position of the newline in
5924 buffer text. */
5925 if (it->stop_charpos >= limit
5926 || ((pos = Fnext_single_property_change (make_number (start),
5927 Qdisplay, Qnil,
5928 make_number (limit)),
5929 NILP (pos))
5930 && next_overlay_change (start) == ZV))
5932 if (!it->bidi_p)
5934 IT_CHARPOS (*it) = limit;
5935 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5937 else
5939 struct bidi_it bprev;
5941 /* Help bidi.c avoid expensive searches for display
5942 properties and overlays, by telling it that there are
5943 none up to `limit'. */
5944 if (it->bidi_it.disp_pos < limit)
5946 it->bidi_it.disp_pos = limit;
5947 it->bidi_it.disp_prop = 0;
5949 do {
5950 bprev = it->bidi_it;
5951 bidi_move_to_visually_next (&it->bidi_it);
5952 } while (it->bidi_it.charpos != limit);
5953 IT_CHARPOS (*it) = limit;
5954 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5955 if (bidi_it_prev)
5956 *bidi_it_prev = bprev;
5958 *skipped_p = newline_found_p = 1;
5960 else
5962 while (get_next_display_element (it)
5963 && !newline_found_p)
5965 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5966 if (newline_found_p && it->bidi_p && bidi_it_prev)
5967 *bidi_it_prev = it->bidi_it;
5968 set_iterator_to_next (it, 0);
5973 it->selective = old_selective;
5974 return newline_found_p;
5978 /* Set IT's current position to the previous visible line start. Skip
5979 invisible text that is so either due to text properties or due to
5980 selective display. Caution: this does not change IT->current_x and
5981 IT->hpos. */
5983 static void
5984 back_to_previous_visible_line_start (struct it *it)
5986 while (IT_CHARPOS (*it) > BEGV)
5988 back_to_previous_line_start (it);
5990 if (IT_CHARPOS (*it) <= BEGV)
5991 break;
5993 /* If selective > 0, then lines indented more than its value are
5994 invisible. */
5995 if (it->selective > 0
5996 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5997 it->selective))
5998 continue;
6000 /* Check the newline before point for invisibility. */
6002 Lisp_Object prop;
6003 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6004 Qinvisible, it->window);
6005 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6006 continue;
6009 if (IT_CHARPOS (*it) <= BEGV)
6010 break;
6013 struct it it2;
6014 void *it2data = NULL;
6015 ptrdiff_t pos;
6016 ptrdiff_t beg, end;
6017 Lisp_Object val, overlay;
6019 SAVE_IT (it2, *it, it2data);
6021 /* If newline is part of a composition, continue from start of composition */
6022 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6023 && beg < IT_CHARPOS (*it))
6024 goto replaced;
6026 /* If newline is replaced by a display property, find start of overlay
6027 or interval and continue search from that point. */
6028 pos = --IT_CHARPOS (it2);
6029 --IT_BYTEPOS (it2);
6030 it2.sp = 0;
6031 bidi_unshelve_cache (NULL, 0);
6032 it2.string_from_display_prop_p = 0;
6033 it2.from_disp_prop_p = 0;
6034 if (handle_display_prop (&it2) == HANDLED_RETURN
6035 && !NILP (val = get_char_property_and_overlay
6036 (make_number (pos), Qdisplay, Qnil, &overlay))
6037 && (OVERLAYP (overlay)
6038 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6039 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6041 RESTORE_IT (it, it, it2data);
6042 goto replaced;
6045 /* Newline is not replaced by anything -- so we are done. */
6046 RESTORE_IT (it, it, it2data);
6047 break;
6049 replaced:
6050 if (beg < BEGV)
6051 beg = BEGV;
6052 IT_CHARPOS (*it) = beg;
6053 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6057 it->continuation_lines_width = 0;
6059 eassert (IT_CHARPOS (*it) >= BEGV);
6060 eassert (IT_CHARPOS (*it) == BEGV
6061 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6062 CHECK_IT (it);
6066 /* Reseat iterator IT at the previous visible line start. Skip
6067 invisible text that is so either due to text properties or due to
6068 selective display. At the end, update IT's overlay information,
6069 face information etc. */
6071 void
6072 reseat_at_previous_visible_line_start (struct it *it)
6074 back_to_previous_visible_line_start (it);
6075 reseat (it, it->current.pos, 1);
6076 CHECK_IT (it);
6080 /* Reseat iterator IT on the next visible line start in the current
6081 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6082 preceding the line start. Skip over invisible text that is so
6083 because of selective display. Compute faces, overlays etc at the
6084 new position. Note that this function does not skip over text that
6085 is invisible because of text properties. */
6087 static void
6088 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6090 int newline_found_p, skipped_p = 0;
6091 struct bidi_it bidi_it_prev;
6093 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6095 /* Skip over lines that are invisible because they are indented
6096 more than the value of IT->selective. */
6097 if (it->selective > 0)
6098 while (IT_CHARPOS (*it) < ZV
6099 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6100 it->selective))
6102 eassert (IT_BYTEPOS (*it) == BEGV
6103 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6104 newline_found_p =
6105 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6108 /* Position on the newline if that's what's requested. */
6109 if (on_newline_p && newline_found_p)
6111 if (STRINGP (it->string))
6113 if (IT_STRING_CHARPOS (*it) > 0)
6115 if (!it->bidi_p)
6117 --IT_STRING_CHARPOS (*it);
6118 --IT_STRING_BYTEPOS (*it);
6120 else
6122 /* We need to restore the bidi iterator to the state
6123 it had on the newline, and resync the IT's
6124 position with that. */
6125 it->bidi_it = bidi_it_prev;
6126 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6127 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6131 else if (IT_CHARPOS (*it) > BEGV)
6133 if (!it->bidi_p)
6135 --IT_CHARPOS (*it);
6136 --IT_BYTEPOS (*it);
6138 else
6140 /* We need to restore the bidi iterator to the state it
6141 had on the newline and resync IT with that. */
6142 it->bidi_it = bidi_it_prev;
6143 IT_CHARPOS (*it) = it->bidi_it.charpos;
6144 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6146 reseat (it, it->current.pos, 0);
6149 else if (skipped_p)
6150 reseat (it, it->current.pos, 0);
6152 CHECK_IT (it);
6157 /***********************************************************************
6158 Changing an iterator's position
6159 ***********************************************************************/
6161 /* Change IT's current position to POS in current_buffer. If FORCE_P
6162 is non-zero, always check for text properties at the new position.
6163 Otherwise, text properties are only looked up if POS >=
6164 IT->check_charpos of a property. */
6166 static void
6167 reseat (struct it *it, struct text_pos pos, int force_p)
6169 ptrdiff_t original_pos = IT_CHARPOS (*it);
6171 reseat_1 (it, pos, 0);
6173 /* Determine where to check text properties. Avoid doing it
6174 where possible because text property lookup is very expensive. */
6175 if (force_p
6176 || CHARPOS (pos) > it->stop_charpos
6177 || CHARPOS (pos) < original_pos)
6179 if (it->bidi_p)
6181 /* For bidi iteration, we need to prime prev_stop and
6182 base_level_stop with our best estimations. */
6183 /* Implementation note: Of course, POS is not necessarily a
6184 stop position, so assigning prev_pos to it is a lie; we
6185 should have called compute_stop_backwards. However, if
6186 the current buffer does not include any R2L characters,
6187 that call would be a waste of cycles, because the
6188 iterator will never move back, and thus never cross this
6189 "fake" stop position. So we delay that backward search
6190 until the time we really need it, in next_element_from_buffer. */
6191 if (CHARPOS (pos) != it->prev_stop)
6192 it->prev_stop = CHARPOS (pos);
6193 if (CHARPOS (pos) < it->base_level_stop)
6194 it->base_level_stop = 0; /* meaning it's unknown */
6195 handle_stop (it);
6197 else
6199 handle_stop (it);
6200 it->prev_stop = it->base_level_stop = 0;
6205 CHECK_IT (it);
6209 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6210 IT->stop_pos to POS, also. */
6212 static void
6213 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6215 /* Don't call this function when scanning a C string. */
6216 eassert (it->s == NULL);
6218 /* POS must be a reasonable value. */
6219 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6221 it->current.pos = it->position = pos;
6222 it->end_charpos = ZV;
6223 it->dpvec = NULL;
6224 it->current.dpvec_index = -1;
6225 it->current.overlay_string_index = -1;
6226 IT_STRING_CHARPOS (*it) = -1;
6227 IT_STRING_BYTEPOS (*it) = -1;
6228 it->string = Qnil;
6229 it->method = GET_FROM_BUFFER;
6230 it->object = it->w->buffer;
6231 it->area = TEXT_AREA;
6232 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6233 it->sp = 0;
6234 it->string_from_display_prop_p = 0;
6235 it->string_from_prefix_prop_p = 0;
6237 it->from_disp_prop_p = 0;
6238 it->face_before_selective_p = 0;
6239 if (it->bidi_p)
6241 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6242 &it->bidi_it);
6243 bidi_unshelve_cache (NULL, 0);
6244 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6245 it->bidi_it.string.s = NULL;
6246 it->bidi_it.string.lstring = Qnil;
6247 it->bidi_it.string.bufpos = 0;
6248 it->bidi_it.string.unibyte = 0;
6251 if (set_stop_p)
6253 it->stop_charpos = CHARPOS (pos);
6254 it->base_level_stop = CHARPOS (pos);
6259 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6260 If S is non-null, it is a C string to iterate over. Otherwise,
6261 STRING gives a Lisp string to iterate over.
6263 If PRECISION > 0, don't return more then PRECISION number of
6264 characters from the string.
6266 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6267 characters have been returned. FIELD_WIDTH < 0 means an infinite
6268 field width.
6270 MULTIBYTE = 0 means disable processing of multibyte characters,
6271 MULTIBYTE > 0 means enable it,
6272 MULTIBYTE < 0 means use IT->multibyte_p.
6274 IT must be initialized via a prior call to init_iterator before
6275 calling this function. */
6277 static void
6278 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6279 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6280 int multibyte)
6282 /* No region in strings. */
6283 it->region_beg_charpos = it->region_end_charpos = -1;
6285 /* No text property checks performed by default, but see below. */
6286 it->stop_charpos = -1;
6288 /* Set iterator position and end position. */
6289 memset (&it->current, 0, sizeof it->current);
6290 it->current.overlay_string_index = -1;
6291 it->current.dpvec_index = -1;
6292 eassert (charpos >= 0);
6294 /* If STRING is specified, use its multibyteness, otherwise use the
6295 setting of MULTIBYTE, if specified. */
6296 if (multibyte >= 0)
6297 it->multibyte_p = multibyte > 0;
6299 /* Bidirectional reordering of strings is controlled by the default
6300 value of bidi-display-reordering. Don't try to reorder while
6301 loading loadup.el, as the necessary character property tables are
6302 not yet available. */
6303 it->bidi_p =
6304 NILP (Vpurify_flag)
6305 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6307 if (s == NULL)
6309 eassert (STRINGP (string));
6310 it->string = string;
6311 it->s = NULL;
6312 it->end_charpos = it->string_nchars = SCHARS (string);
6313 it->method = GET_FROM_STRING;
6314 it->current.string_pos = string_pos (charpos, string);
6316 if (it->bidi_p)
6318 it->bidi_it.string.lstring = string;
6319 it->bidi_it.string.s = NULL;
6320 it->bidi_it.string.schars = it->end_charpos;
6321 it->bidi_it.string.bufpos = 0;
6322 it->bidi_it.string.from_disp_str = 0;
6323 it->bidi_it.string.unibyte = !it->multibyte_p;
6324 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6325 FRAME_WINDOW_P (it->f), &it->bidi_it);
6328 else
6330 it->s = (const unsigned char *) s;
6331 it->string = Qnil;
6333 /* Note that we use IT->current.pos, not it->current.string_pos,
6334 for displaying C strings. */
6335 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6336 if (it->multibyte_p)
6338 it->current.pos = c_string_pos (charpos, s, 1);
6339 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6341 else
6343 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6344 it->end_charpos = it->string_nchars = strlen (s);
6347 if (it->bidi_p)
6349 it->bidi_it.string.lstring = Qnil;
6350 it->bidi_it.string.s = (const unsigned char *) s;
6351 it->bidi_it.string.schars = it->end_charpos;
6352 it->bidi_it.string.bufpos = 0;
6353 it->bidi_it.string.from_disp_str = 0;
6354 it->bidi_it.string.unibyte = !it->multibyte_p;
6355 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6356 &it->bidi_it);
6358 it->method = GET_FROM_C_STRING;
6361 /* PRECISION > 0 means don't return more than PRECISION characters
6362 from the string. */
6363 if (precision > 0 && it->end_charpos - charpos > precision)
6365 it->end_charpos = it->string_nchars = charpos + precision;
6366 if (it->bidi_p)
6367 it->bidi_it.string.schars = it->end_charpos;
6370 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6371 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6372 FIELD_WIDTH < 0 means infinite field width. This is useful for
6373 padding with `-' at the end of a mode line. */
6374 if (field_width < 0)
6375 field_width = INFINITY;
6376 /* Implementation note: We deliberately don't enlarge
6377 it->bidi_it.string.schars here to fit it->end_charpos, because
6378 the bidi iterator cannot produce characters out of thin air. */
6379 if (field_width > it->end_charpos - charpos)
6380 it->end_charpos = charpos + field_width;
6382 /* Use the standard display table for displaying strings. */
6383 if (DISP_TABLE_P (Vstandard_display_table))
6384 it->dp = XCHAR_TABLE (Vstandard_display_table);
6386 it->stop_charpos = charpos;
6387 it->prev_stop = charpos;
6388 it->base_level_stop = 0;
6389 if (it->bidi_p)
6391 it->bidi_it.first_elt = 1;
6392 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6393 it->bidi_it.disp_pos = -1;
6395 if (s == NULL && it->multibyte_p)
6397 ptrdiff_t endpos = SCHARS (it->string);
6398 if (endpos > it->end_charpos)
6399 endpos = it->end_charpos;
6400 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6401 it->string);
6403 CHECK_IT (it);
6408 /***********************************************************************
6409 Iteration
6410 ***********************************************************************/
6412 /* Map enum it_method value to corresponding next_element_from_* function. */
6414 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6416 next_element_from_buffer,
6417 next_element_from_display_vector,
6418 next_element_from_string,
6419 next_element_from_c_string,
6420 next_element_from_image,
6421 next_element_from_stretch
6424 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6427 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6428 (possibly with the following characters). */
6430 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6431 ((IT)->cmp_it.id >= 0 \
6432 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6433 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6434 END_CHARPOS, (IT)->w, \
6435 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6436 (IT)->string)))
6439 /* Lookup the char-table Vglyphless_char_display for character C (-1
6440 if we want information for no-font case), and return the display
6441 method symbol. By side-effect, update it->what and
6442 it->glyphless_method. This function is called from
6443 get_next_display_element for each character element, and from
6444 x_produce_glyphs when no suitable font was found. */
6446 Lisp_Object
6447 lookup_glyphless_char_display (int c, struct it *it)
6449 Lisp_Object glyphless_method = Qnil;
6451 if (CHAR_TABLE_P (Vglyphless_char_display)
6452 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6454 if (c >= 0)
6456 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6457 if (CONSP (glyphless_method))
6458 glyphless_method = FRAME_WINDOW_P (it->f)
6459 ? XCAR (glyphless_method)
6460 : XCDR (glyphless_method);
6462 else
6463 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6466 retry:
6467 if (NILP (glyphless_method))
6469 if (c >= 0)
6470 /* The default is to display the character by a proper font. */
6471 return Qnil;
6472 /* The default for the no-font case is to display an empty box. */
6473 glyphless_method = Qempty_box;
6475 if (EQ (glyphless_method, Qzero_width))
6477 if (c >= 0)
6478 return glyphless_method;
6479 /* This method can't be used for the no-font case. */
6480 glyphless_method = Qempty_box;
6482 if (EQ (glyphless_method, Qthin_space))
6483 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6484 else if (EQ (glyphless_method, Qempty_box))
6485 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6486 else if (EQ (glyphless_method, Qhex_code))
6487 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6488 else if (STRINGP (glyphless_method))
6489 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6490 else
6492 /* Invalid value. We use the default method. */
6493 glyphless_method = Qnil;
6494 goto retry;
6496 it->what = IT_GLYPHLESS;
6497 return glyphless_method;
6500 /* Load IT's display element fields with information about the next
6501 display element from the current position of IT. Value is zero if
6502 end of buffer (or C string) is reached. */
6504 static struct frame *last_escape_glyph_frame = NULL;
6505 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6506 static int last_escape_glyph_merged_face_id = 0;
6508 struct frame *last_glyphless_glyph_frame = NULL;
6509 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6510 int last_glyphless_glyph_merged_face_id = 0;
6512 static int
6513 get_next_display_element (struct it *it)
6515 /* Non-zero means that we found a display element. Zero means that
6516 we hit the end of what we iterate over. Performance note: the
6517 function pointer `method' used here turns out to be faster than
6518 using a sequence of if-statements. */
6519 int success_p;
6521 get_next:
6522 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6524 if (it->what == IT_CHARACTER)
6526 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6527 and only if (a) the resolved directionality of that character
6528 is R..." */
6529 /* FIXME: Do we need an exception for characters from display
6530 tables? */
6531 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6532 it->c = bidi_mirror_char (it->c);
6533 /* Map via display table or translate control characters.
6534 IT->c, IT->len etc. have been set to the next character by
6535 the function call above. If we have a display table, and it
6536 contains an entry for IT->c, translate it. Don't do this if
6537 IT->c itself comes from a display table, otherwise we could
6538 end up in an infinite recursion. (An alternative could be to
6539 count the recursion depth of this function and signal an
6540 error when a certain maximum depth is reached.) Is it worth
6541 it? */
6542 if (success_p && it->dpvec == NULL)
6544 Lisp_Object dv;
6545 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6546 int nonascii_space_p = 0;
6547 int nonascii_hyphen_p = 0;
6548 int c = it->c; /* This is the character to display. */
6550 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6552 eassert (SINGLE_BYTE_CHAR_P (c));
6553 if (unibyte_display_via_language_environment)
6555 c = DECODE_CHAR (unibyte, c);
6556 if (c < 0)
6557 c = BYTE8_TO_CHAR (it->c);
6559 else
6560 c = BYTE8_TO_CHAR (it->c);
6563 if (it->dp
6564 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6565 VECTORP (dv)))
6567 struct Lisp_Vector *v = XVECTOR (dv);
6569 /* Return the first character from the display table
6570 entry, if not empty. If empty, don't display the
6571 current character. */
6572 if (v->header.size)
6574 it->dpvec_char_len = it->len;
6575 it->dpvec = v->contents;
6576 it->dpend = v->contents + v->header.size;
6577 it->current.dpvec_index = 0;
6578 it->dpvec_face_id = -1;
6579 it->saved_face_id = it->face_id;
6580 it->method = GET_FROM_DISPLAY_VECTOR;
6581 it->ellipsis_p = 0;
6583 else
6585 set_iterator_to_next (it, 0);
6587 goto get_next;
6590 if (! NILP (lookup_glyphless_char_display (c, it)))
6592 if (it->what == IT_GLYPHLESS)
6593 goto done;
6594 /* Don't display this character. */
6595 set_iterator_to_next (it, 0);
6596 goto get_next;
6599 /* If `nobreak-char-display' is non-nil, we display
6600 non-ASCII spaces and hyphens specially. */
6601 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6603 if (c == 0xA0)
6604 nonascii_space_p = 1;
6605 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6606 nonascii_hyphen_p = 1;
6609 /* Translate control characters into `\003' or `^C' form.
6610 Control characters coming from a display table entry are
6611 currently not translated because we use IT->dpvec to hold
6612 the translation. This could easily be changed but I
6613 don't believe that it is worth doing.
6615 The characters handled by `nobreak-char-display' must be
6616 translated too.
6618 Non-printable characters and raw-byte characters are also
6619 translated to octal form. */
6620 if (((c < ' ' || c == 127) /* ASCII control chars */
6621 ? (it->area != TEXT_AREA
6622 /* In mode line, treat \n, \t like other crl chars. */
6623 || (c != '\t'
6624 && it->glyph_row
6625 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6626 || (c != '\n' && c != '\t'))
6627 : (nonascii_space_p
6628 || nonascii_hyphen_p
6629 || CHAR_BYTE8_P (c)
6630 || ! CHAR_PRINTABLE_P (c))))
6632 /* C is a control character, non-ASCII space/hyphen,
6633 raw-byte, or a non-printable character which must be
6634 displayed either as '\003' or as `^C' where the '\\'
6635 and '^' can be defined in the display table. Fill
6636 IT->ctl_chars with glyphs for what we have to
6637 display. Then, set IT->dpvec to these glyphs. */
6638 Lisp_Object gc;
6639 int ctl_len;
6640 int face_id;
6641 int lface_id = 0;
6642 int escape_glyph;
6644 /* Handle control characters with ^. */
6646 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6648 int g;
6650 g = '^'; /* default glyph for Control */
6651 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6652 if (it->dp
6653 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6655 g = GLYPH_CODE_CHAR (gc);
6656 lface_id = GLYPH_CODE_FACE (gc);
6658 if (lface_id)
6660 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6662 else if (it->f == last_escape_glyph_frame
6663 && it->face_id == last_escape_glyph_face_id)
6665 face_id = last_escape_glyph_merged_face_id;
6667 else
6669 /* Merge the escape-glyph face into the current face. */
6670 face_id = merge_faces (it->f, Qescape_glyph, 0,
6671 it->face_id);
6672 last_escape_glyph_frame = it->f;
6673 last_escape_glyph_face_id = it->face_id;
6674 last_escape_glyph_merged_face_id = face_id;
6677 XSETINT (it->ctl_chars[0], g);
6678 XSETINT (it->ctl_chars[1], c ^ 0100);
6679 ctl_len = 2;
6680 goto display_control;
6683 /* Handle non-ascii space in the mode where it only gets
6684 highlighting. */
6686 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6688 /* Merge `nobreak-space' into the current face. */
6689 face_id = merge_faces (it->f, Qnobreak_space, 0,
6690 it->face_id);
6691 XSETINT (it->ctl_chars[0], ' ');
6692 ctl_len = 1;
6693 goto display_control;
6696 /* Handle sequences that start with the "escape glyph". */
6698 /* the default escape glyph is \. */
6699 escape_glyph = '\\';
6701 if (it->dp
6702 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6704 escape_glyph = GLYPH_CODE_CHAR (gc);
6705 lface_id = GLYPH_CODE_FACE (gc);
6707 if (lface_id)
6709 /* The display table specified a face.
6710 Merge it into face_id and also into escape_glyph. */
6711 face_id = merge_faces (it->f, Qt, lface_id,
6712 it->face_id);
6714 else if (it->f == last_escape_glyph_frame
6715 && it->face_id == last_escape_glyph_face_id)
6717 face_id = last_escape_glyph_merged_face_id;
6719 else
6721 /* Merge the escape-glyph face into the current face. */
6722 face_id = merge_faces (it->f, Qescape_glyph, 0,
6723 it->face_id);
6724 last_escape_glyph_frame = it->f;
6725 last_escape_glyph_face_id = it->face_id;
6726 last_escape_glyph_merged_face_id = face_id;
6729 /* Draw non-ASCII hyphen with just highlighting: */
6731 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6733 XSETINT (it->ctl_chars[0], '-');
6734 ctl_len = 1;
6735 goto display_control;
6738 /* Draw non-ASCII space/hyphen with escape glyph: */
6740 if (nonascii_space_p || nonascii_hyphen_p)
6742 XSETINT (it->ctl_chars[0], escape_glyph);
6743 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6744 ctl_len = 2;
6745 goto display_control;
6749 char str[10];
6750 int len, i;
6752 if (CHAR_BYTE8_P (c))
6753 /* Display \200 instead of \17777600. */
6754 c = CHAR_TO_BYTE8 (c);
6755 len = sprintf (str, "%03o", c);
6757 XSETINT (it->ctl_chars[0], escape_glyph);
6758 for (i = 0; i < len; i++)
6759 XSETINT (it->ctl_chars[i + 1], str[i]);
6760 ctl_len = len + 1;
6763 display_control:
6764 /* Set up IT->dpvec and return first character from it. */
6765 it->dpvec_char_len = it->len;
6766 it->dpvec = it->ctl_chars;
6767 it->dpend = it->dpvec + ctl_len;
6768 it->current.dpvec_index = 0;
6769 it->dpvec_face_id = face_id;
6770 it->saved_face_id = it->face_id;
6771 it->method = GET_FROM_DISPLAY_VECTOR;
6772 it->ellipsis_p = 0;
6773 goto get_next;
6775 it->char_to_display = c;
6777 else if (success_p)
6779 it->char_to_display = it->c;
6783 /* Adjust face id for a multibyte character. There are no multibyte
6784 character in unibyte text. */
6785 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6786 && it->multibyte_p
6787 && success_p
6788 && FRAME_WINDOW_P (it->f))
6790 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6792 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6794 /* Automatic composition with glyph-string. */
6795 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6797 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6799 else
6801 ptrdiff_t pos = (it->s ? -1
6802 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6803 : IT_CHARPOS (*it));
6804 int c;
6806 if (it->what == IT_CHARACTER)
6807 c = it->char_to_display;
6808 else
6810 struct composition *cmp = composition_table[it->cmp_it.id];
6811 int i;
6813 c = ' ';
6814 for (i = 0; i < cmp->glyph_len; i++)
6815 /* TAB in a composition means display glyphs with
6816 padding space on the left or right. */
6817 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6818 break;
6820 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6824 done:
6825 /* Is this character the last one of a run of characters with
6826 box? If yes, set IT->end_of_box_run_p to 1. */
6827 if (it->face_box_p
6828 && it->s == NULL)
6830 if (it->method == GET_FROM_STRING && it->sp)
6832 int face_id = underlying_face_id (it);
6833 struct face *face = FACE_FROM_ID (it->f, face_id);
6835 if (face)
6837 if (face->box == FACE_NO_BOX)
6839 /* If the box comes from face properties in a
6840 display string, check faces in that string. */
6841 int string_face_id = face_after_it_pos (it);
6842 it->end_of_box_run_p
6843 = (FACE_FROM_ID (it->f, string_face_id)->box
6844 == FACE_NO_BOX);
6846 /* Otherwise, the box comes from the underlying face.
6847 If this is the last string character displayed, check
6848 the next buffer location. */
6849 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6850 && (it->current.overlay_string_index
6851 == it->n_overlay_strings - 1))
6853 ptrdiff_t ignore;
6854 int next_face_id;
6855 struct text_pos pos = it->current.pos;
6856 INC_TEXT_POS (pos, it->multibyte_p);
6858 next_face_id = face_at_buffer_position
6859 (it->w, CHARPOS (pos), it->region_beg_charpos,
6860 it->region_end_charpos, &ignore,
6861 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6862 -1);
6863 it->end_of_box_run_p
6864 = (FACE_FROM_ID (it->f, next_face_id)->box
6865 == FACE_NO_BOX);
6869 else
6871 int face_id = face_after_it_pos (it);
6872 it->end_of_box_run_p
6873 = (face_id != it->face_id
6874 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6877 /* If we reached the end of the object we've been iterating (e.g., a
6878 display string or an overlay string), and there's something on
6879 IT->stack, proceed with what's on the stack. It doesn't make
6880 sense to return zero if there's unprocessed stuff on the stack,
6881 because otherwise that stuff will never be displayed. */
6882 if (!success_p && it->sp > 0)
6884 set_iterator_to_next (it, 0);
6885 success_p = get_next_display_element (it);
6888 /* Value is 0 if end of buffer or string reached. */
6889 return success_p;
6893 /* Move IT to the next display element.
6895 RESEAT_P non-zero means if called on a newline in buffer text,
6896 skip to the next visible line start.
6898 Functions get_next_display_element and set_iterator_to_next are
6899 separate because I find this arrangement easier to handle than a
6900 get_next_display_element function that also increments IT's
6901 position. The way it is we can first look at an iterator's current
6902 display element, decide whether it fits on a line, and if it does,
6903 increment the iterator position. The other way around we probably
6904 would either need a flag indicating whether the iterator has to be
6905 incremented the next time, or we would have to implement a
6906 decrement position function which would not be easy to write. */
6908 void
6909 set_iterator_to_next (struct it *it, int reseat_p)
6911 /* Reset flags indicating start and end of a sequence of characters
6912 with box. Reset them at the start of this function because
6913 moving the iterator to a new position might set them. */
6914 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6916 switch (it->method)
6918 case GET_FROM_BUFFER:
6919 /* The current display element of IT is a character from
6920 current_buffer. Advance in the buffer, and maybe skip over
6921 invisible lines that are so because of selective display. */
6922 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6923 reseat_at_next_visible_line_start (it, 0);
6924 else if (it->cmp_it.id >= 0)
6926 /* We are currently getting glyphs from a composition. */
6927 int i;
6929 if (! it->bidi_p)
6931 IT_CHARPOS (*it) += it->cmp_it.nchars;
6932 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6933 if (it->cmp_it.to < it->cmp_it.nglyphs)
6935 it->cmp_it.from = it->cmp_it.to;
6937 else
6939 it->cmp_it.id = -1;
6940 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6941 IT_BYTEPOS (*it),
6942 it->end_charpos, Qnil);
6945 else if (! it->cmp_it.reversed_p)
6947 /* Composition created while scanning forward. */
6948 /* Update IT's char/byte positions to point to the first
6949 character of the next grapheme cluster, or to the
6950 character visually after the current composition. */
6951 for (i = 0; i < it->cmp_it.nchars; i++)
6952 bidi_move_to_visually_next (&it->bidi_it);
6953 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6954 IT_CHARPOS (*it) = it->bidi_it.charpos;
6956 if (it->cmp_it.to < it->cmp_it.nglyphs)
6958 /* Proceed to the next grapheme cluster. */
6959 it->cmp_it.from = it->cmp_it.to;
6961 else
6963 /* No more grapheme clusters in this composition.
6964 Find the next stop position. */
6965 ptrdiff_t stop = it->end_charpos;
6966 if (it->bidi_it.scan_dir < 0)
6967 /* Now we are scanning backward and don't know
6968 where to stop. */
6969 stop = -1;
6970 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6971 IT_BYTEPOS (*it), stop, Qnil);
6974 else
6976 /* Composition created while scanning backward. */
6977 /* Update IT's char/byte positions to point to the last
6978 character of the previous grapheme cluster, or the
6979 character visually after the current composition. */
6980 for (i = 0; i < it->cmp_it.nchars; i++)
6981 bidi_move_to_visually_next (&it->bidi_it);
6982 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6983 IT_CHARPOS (*it) = it->bidi_it.charpos;
6984 if (it->cmp_it.from > 0)
6986 /* Proceed to the previous grapheme cluster. */
6987 it->cmp_it.to = it->cmp_it.from;
6989 else
6991 /* No more grapheme clusters in this composition.
6992 Find the next stop position. */
6993 ptrdiff_t stop = it->end_charpos;
6994 if (it->bidi_it.scan_dir < 0)
6995 /* Now we are scanning backward and don't know
6996 where to stop. */
6997 stop = -1;
6998 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6999 IT_BYTEPOS (*it), stop, Qnil);
7003 else
7005 eassert (it->len != 0);
7007 if (!it->bidi_p)
7009 IT_BYTEPOS (*it) += it->len;
7010 IT_CHARPOS (*it) += 1;
7012 else
7014 int prev_scan_dir = it->bidi_it.scan_dir;
7015 /* If this is a new paragraph, determine its base
7016 direction (a.k.a. its base embedding level). */
7017 if (it->bidi_it.new_paragraph)
7018 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7019 bidi_move_to_visually_next (&it->bidi_it);
7020 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7021 IT_CHARPOS (*it) = it->bidi_it.charpos;
7022 if (prev_scan_dir != it->bidi_it.scan_dir)
7024 /* As the scan direction was changed, we must
7025 re-compute the stop position for composition. */
7026 ptrdiff_t stop = it->end_charpos;
7027 if (it->bidi_it.scan_dir < 0)
7028 stop = -1;
7029 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7030 IT_BYTEPOS (*it), stop, Qnil);
7033 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7035 break;
7037 case GET_FROM_C_STRING:
7038 /* Current display element of IT is from a C string. */
7039 if (!it->bidi_p
7040 /* If the string position is beyond string's end, it means
7041 next_element_from_c_string is padding the string with
7042 blanks, in which case we bypass the bidi iterator,
7043 because it cannot deal with such virtual characters. */
7044 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7046 IT_BYTEPOS (*it) += it->len;
7047 IT_CHARPOS (*it) += 1;
7049 else
7051 bidi_move_to_visually_next (&it->bidi_it);
7052 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7053 IT_CHARPOS (*it) = it->bidi_it.charpos;
7055 break;
7057 case GET_FROM_DISPLAY_VECTOR:
7058 /* Current display element of IT is from a display table entry.
7059 Advance in the display table definition. Reset it to null if
7060 end reached, and continue with characters from buffers/
7061 strings. */
7062 ++it->current.dpvec_index;
7064 /* Restore face of the iterator to what they were before the
7065 display vector entry (these entries may contain faces). */
7066 it->face_id = it->saved_face_id;
7068 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7070 int recheck_faces = it->ellipsis_p;
7072 if (it->s)
7073 it->method = GET_FROM_C_STRING;
7074 else if (STRINGP (it->string))
7075 it->method = GET_FROM_STRING;
7076 else
7078 it->method = GET_FROM_BUFFER;
7079 it->object = it->w->buffer;
7082 it->dpvec = NULL;
7083 it->current.dpvec_index = -1;
7085 /* Skip over characters which were displayed via IT->dpvec. */
7086 if (it->dpvec_char_len < 0)
7087 reseat_at_next_visible_line_start (it, 1);
7088 else if (it->dpvec_char_len > 0)
7090 if (it->method == GET_FROM_STRING
7091 && it->n_overlay_strings > 0)
7092 it->ignore_overlay_strings_at_pos_p = 1;
7093 it->len = it->dpvec_char_len;
7094 set_iterator_to_next (it, reseat_p);
7097 /* Maybe recheck faces after display vector */
7098 if (recheck_faces)
7099 it->stop_charpos = IT_CHARPOS (*it);
7101 break;
7103 case GET_FROM_STRING:
7104 /* Current display element is a character from a Lisp string. */
7105 eassert (it->s == NULL && STRINGP (it->string));
7106 /* Don't advance past string end. These conditions are true
7107 when set_iterator_to_next is called at the end of
7108 get_next_display_element, in which case the Lisp string is
7109 already exhausted, and all we want is pop the iterator
7110 stack. */
7111 if (it->current.overlay_string_index >= 0)
7113 /* This is an overlay string, so there's no padding with
7114 spaces, and the number of characters in the string is
7115 where the string ends. */
7116 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7117 goto consider_string_end;
7119 else
7121 /* Not an overlay string. There could be padding, so test
7122 against it->end_charpos . */
7123 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7124 goto consider_string_end;
7126 if (it->cmp_it.id >= 0)
7128 int i;
7130 if (! it->bidi_p)
7132 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7133 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7134 if (it->cmp_it.to < it->cmp_it.nglyphs)
7135 it->cmp_it.from = it->cmp_it.to;
7136 else
7138 it->cmp_it.id = -1;
7139 composition_compute_stop_pos (&it->cmp_it,
7140 IT_STRING_CHARPOS (*it),
7141 IT_STRING_BYTEPOS (*it),
7142 it->end_charpos, it->string);
7145 else if (! it->cmp_it.reversed_p)
7147 for (i = 0; i < it->cmp_it.nchars; i++)
7148 bidi_move_to_visually_next (&it->bidi_it);
7149 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7150 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7152 if (it->cmp_it.to < it->cmp_it.nglyphs)
7153 it->cmp_it.from = it->cmp_it.to;
7154 else
7156 ptrdiff_t stop = it->end_charpos;
7157 if (it->bidi_it.scan_dir < 0)
7158 stop = -1;
7159 composition_compute_stop_pos (&it->cmp_it,
7160 IT_STRING_CHARPOS (*it),
7161 IT_STRING_BYTEPOS (*it), stop,
7162 it->string);
7165 else
7167 for (i = 0; i < it->cmp_it.nchars; i++)
7168 bidi_move_to_visually_next (&it->bidi_it);
7169 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7170 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7171 if (it->cmp_it.from > 0)
7172 it->cmp_it.to = it->cmp_it.from;
7173 else
7175 ptrdiff_t stop = it->end_charpos;
7176 if (it->bidi_it.scan_dir < 0)
7177 stop = -1;
7178 composition_compute_stop_pos (&it->cmp_it,
7179 IT_STRING_CHARPOS (*it),
7180 IT_STRING_BYTEPOS (*it), stop,
7181 it->string);
7185 else
7187 if (!it->bidi_p
7188 /* If the string position is beyond string's end, it
7189 means next_element_from_string is padding the string
7190 with blanks, in which case we bypass the bidi
7191 iterator, because it cannot deal with such virtual
7192 characters. */
7193 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7195 IT_STRING_BYTEPOS (*it) += it->len;
7196 IT_STRING_CHARPOS (*it) += 1;
7198 else
7200 int prev_scan_dir = it->bidi_it.scan_dir;
7202 bidi_move_to_visually_next (&it->bidi_it);
7203 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7204 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7205 if (prev_scan_dir != it->bidi_it.scan_dir)
7207 ptrdiff_t stop = it->end_charpos;
7209 if (it->bidi_it.scan_dir < 0)
7210 stop = -1;
7211 composition_compute_stop_pos (&it->cmp_it,
7212 IT_STRING_CHARPOS (*it),
7213 IT_STRING_BYTEPOS (*it), stop,
7214 it->string);
7219 consider_string_end:
7221 if (it->current.overlay_string_index >= 0)
7223 /* IT->string is an overlay string. Advance to the
7224 next, if there is one. */
7225 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7227 it->ellipsis_p = 0;
7228 next_overlay_string (it);
7229 if (it->ellipsis_p)
7230 setup_for_ellipsis (it, 0);
7233 else
7235 /* IT->string is not an overlay string. If we reached
7236 its end, and there is something on IT->stack, proceed
7237 with what is on the stack. This can be either another
7238 string, this time an overlay string, or a buffer. */
7239 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7240 && it->sp > 0)
7242 pop_it (it);
7243 if (it->method == GET_FROM_STRING)
7244 goto consider_string_end;
7247 break;
7249 case GET_FROM_IMAGE:
7250 case GET_FROM_STRETCH:
7251 /* The position etc with which we have to proceed are on
7252 the stack. The position may be at the end of a string,
7253 if the `display' property takes up the whole string. */
7254 eassert (it->sp > 0);
7255 pop_it (it);
7256 if (it->method == GET_FROM_STRING)
7257 goto consider_string_end;
7258 break;
7260 default:
7261 /* There are no other methods defined, so this should be a bug. */
7262 abort ();
7265 eassert (it->method != GET_FROM_STRING
7266 || (STRINGP (it->string)
7267 && IT_STRING_CHARPOS (*it) >= 0));
7270 /* Load IT's display element fields with information about the next
7271 display element which comes from a display table entry or from the
7272 result of translating a control character to one of the forms `^C'
7273 or `\003'.
7275 IT->dpvec holds the glyphs to return as characters.
7276 IT->saved_face_id holds the face id before the display vector--it
7277 is restored into IT->face_id in set_iterator_to_next. */
7279 static int
7280 next_element_from_display_vector (struct it *it)
7282 Lisp_Object gc;
7284 /* Precondition. */
7285 eassert (it->dpvec && it->current.dpvec_index >= 0);
7287 it->face_id = it->saved_face_id;
7289 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7290 That seemed totally bogus - so I changed it... */
7291 gc = it->dpvec[it->current.dpvec_index];
7293 if (GLYPH_CODE_P (gc))
7295 it->c = GLYPH_CODE_CHAR (gc);
7296 it->len = CHAR_BYTES (it->c);
7298 /* The entry may contain a face id to use. Such a face id is
7299 the id of a Lisp face, not a realized face. A face id of
7300 zero means no face is specified. */
7301 if (it->dpvec_face_id >= 0)
7302 it->face_id = it->dpvec_face_id;
7303 else
7305 int lface_id = GLYPH_CODE_FACE (gc);
7306 if (lface_id > 0)
7307 it->face_id = merge_faces (it->f, Qt, lface_id,
7308 it->saved_face_id);
7311 else
7312 /* Display table entry is invalid. Return a space. */
7313 it->c = ' ', it->len = 1;
7315 /* Don't change position and object of the iterator here. They are
7316 still the values of the character that had this display table
7317 entry or was translated, and that's what we want. */
7318 it->what = IT_CHARACTER;
7319 return 1;
7322 /* Get the first element of string/buffer in the visual order, after
7323 being reseated to a new position in a string or a buffer. */
7324 static void
7325 get_visually_first_element (struct it *it)
7327 int string_p = STRINGP (it->string) || it->s;
7328 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7329 ptrdiff_t bob = (string_p ? 0 : BEGV);
7331 if (STRINGP (it->string))
7333 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7334 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7336 else
7338 it->bidi_it.charpos = IT_CHARPOS (*it);
7339 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7342 if (it->bidi_it.charpos == eob)
7344 /* Nothing to do, but reset the FIRST_ELT flag, like
7345 bidi_paragraph_init does, because we are not going to
7346 call it. */
7347 it->bidi_it.first_elt = 0;
7349 else if (it->bidi_it.charpos == bob
7350 || (!string_p
7351 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7352 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7354 /* If we are at the beginning of a line/string, we can produce
7355 the next element right away. */
7356 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7357 bidi_move_to_visually_next (&it->bidi_it);
7359 else
7361 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7363 /* We need to prime the bidi iterator starting at the line's or
7364 string's beginning, before we will be able to produce the
7365 next element. */
7366 if (string_p)
7367 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7368 else
7370 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7371 -1);
7372 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7374 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7377 /* Now return to buffer/string position where we were asked
7378 to get the next display element, and produce that. */
7379 bidi_move_to_visually_next (&it->bidi_it);
7381 while (it->bidi_it.bytepos != orig_bytepos
7382 && it->bidi_it.charpos < eob);
7385 /* Adjust IT's position information to where we ended up. */
7386 if (STRINGP (it->string))
7388 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7389 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7391 else
7393 IT_CHARPOS (*it) = it->bidi_it.charpos;
7394 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7397 if (STRINGP (it->string) || !it->s)
7399 ptrdiff_t stop, charpos, bytepos;
7401 if (STRINGP (it->string))
7403 eassert (!it->s);
7404 stop = SCHARS (it->string);
7405 if (stop > it->end_charpos)
7406 stop = it->end_charpos;
7407 charpos = IT_STRING_CHARPOS (*it);
7408 bytepos = IT_STRING_BYTEPOS (*it);
7410 else
7412 stop = it->end_charpos;
7413 charpos = IT_CHARPOS (*it);
7414 bytepos = IT_BYTEPOS (*it);
7416 if (it->bidi_it.scan_dir < 0)
7417 stop = -1;
7418 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7419 it->string);
7423 /* Load IT with the next display element from Lisp string IT->string.
7424 IT->current.string_pos is the current position within the string.
7425 If IT->current.overlay_string_index >= 0, the Lisp string is an
7426 overlay string. */
7428 static int
7429 next_element_from_string (struct it *it)
7431 struct text_pos position;
7433 eassert (STRINGP (it->string));
7434 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7435 eassert (IT_STRING_CHARPOS (*it) >= 0);
7436 position = it->current.string_pos;
7438 /* With bidi reordering, the character to display might not be the
7439 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7440 that we were reseat()ed to a new string, whose paragraph
7441 direction is not known. */
7442 if (it->bidi_p && it->bidi_it.first_elt)
7444 get_visually_first_element (it);
7445 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7448 /* Time to check for invisible text? */
7449 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7451 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7453 if (!(!it->bidi_p
7454 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7455 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7457 /* With bidi non-linear iteration, we could find
7458 ourselves far beyond the last computed stop_charpos,
7459 with several other stop positions in between that we
7460 missed. Scan them all now, in buffer's logical
7461 order, until we find and handle the last stop_charpos
7462 that precedes our current position. */
7463 handle_stop_backwards (it, it->stop_charpos);
7464 return GET_NEXT_DISPLAY_ELEMENT (it);
7466 else
7468 if (it->bidi_p)
7470 /* Take note of the stop position we just moved
7471 across, for when we will move back across it. */
7472 it->prev_stop = it->stop_charpos;
7473 /* If we are at base paragraph embedding level, take
7474 note of the last stop position seen at this
7475 level. */
7476 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7477 it->base_level_stop = it->stop_charpos;
7479 handle_stop (it);
7481 /* Since a handler may have changed IT->method, we must
7482 recurse here. */
7483 return GET_NEXT_DISPLAY_ELEMENT (it);
7486 else if (it->bidi_p
7487 /* If we are before prev_stop, we may have overstepped
7488 on our way backwards a stop_pos, and if so, we need
7489 to handle that stop_pos. */
7490 && IT_STRING_CHARPOS (*it) < it->prev_stop
7491 /* We can sometimes back up for reasons that have nothing
7492 to do with bidi reordering. E.g., compositions. The
7493 code below is only needed when we are above the base
7494 embedding level, so test for that explicitly. */
7495 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7497 /* If we lost track of base_level_stop, we have no better
7498 place for handle_stop_backwards to start from than string
7499 beginning. This happens, e.g., when we were reseated to
7500 the previous screenful of text by vertical-motion. */
7501 if (it->base_level_stop <= 0
7502 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7503 it->base_level_stop = 0;
7504 handle_stop_backwards (it, it->base_level_stop);
7505 return GET_NEXT_DISPLAY_ELEMENT (it);
7509 if (it->current.overlay_string_index >= 0)
7511 /* Get the next character from an overlay string. In overlay
7512 strings, there is no field width or padding with spaces to
7513 do. */
7514 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7516 it->what = IT_EOB;
7517 return 0;
7519 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7520 IT_STRING_BYTEPOS (*it),
7521 it->bidi_it.scan_dir < 0
7522 ? -1
7523 : SCHARS (it->string))
7524 && next_element_from_composition (it))
7526 return 1;
7528 else if (STRING_MULTIBYTE (it->string))
7530 const unsigned char *s = (SDATA (it->string)
7531 + IT_STRING_BYTEPOS (*it));
7532 it->c = string_char_and_length (s, &it->len);
7534 else
7536 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7537 it->len = 1;
7540 else
7542 /* Get the next character from a Lisp string that is not an
7543 overlay string. Such strings come from the mode line, for
7544 example. We may have to pad with spaces, or truncate the
7545 string. See also next_element_from_c_string. */
7546 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7548 it->what = IT_EOB;
7549 return 0;
7551 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7553 /* Pad with spaces. */
7554 it->c = ' ', it->len = 1;
7555 CHARPOS (position) = BYTEPOS (position) = -1;
7557 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7558 IT_STRING_BYTEPOS (*it),
7559 it->bidi_it.scan_dir < 0
7560 ? -1
7561 : it->string_nchars)
7562 && next_element_from_composition (it))
7564 return 1;
7566 else if (STRING_MULTIBYTE (it->string))
7568 const unsigned char *s = (SDATA (it->string)
7569 + IT_STRING_BYTEPOS (*it));
7570 it->c = string_char_and_length (s, &it->len);
7572 else
7574 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7575 it->len = 1;
7579 /* Record what we have and where it came from. */
7580 it->what = IT_CHARACTER;
7581 it->object = it->string;
7582 it->position = position;
7583 return 1;
7587 /* Load IT with next display element from C string IT->s.
7588 IT->string_nchars is the maximum number of characters to return
7589 from the string. IT->end_charpos may be greater than
7590 IT->string_nchars when this function is called, in which case we
7591 may have to return padding spaces. Value is zero if end of string
7592 reached, including padding spaces. */
7594 static int
7595 next_element_from_c_string (struct it *it)
7597 int success_p = 1;
7599 eassert (it->s);
7600 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7601 it->what = IT_CHARACTER;
7602 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7603 it->object = Qnil;
7605 /* With bidi reordering, the character to display might not be the
7606 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7607 we were reseated to a new string, whose paragraph direction is
7608 not known. */
7609 if (it->bidi_p && it->bidi_it.first_elt)
7610 get_visually_first_element (it);
7612 /* IT's position can be greater than IT->string_nchars in case a
7613 field width or precision has been specified when the iterator was
7614 initialized. */
7615 if (IT_CHARPOS (*it) >= it->end_charpos)
7617 /* End of the game. */
7618 it->what = IT_EOB;
7619 success_p = 0;
7621 else if (IT_CHARPOS (*it) >= it->string_nchars)
7623 /* Pad with spaces. */
7624 it->c = ' ', it->len = 1;
7625 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7627 else if (it->multibyte_p)
7628 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7629 else
7630 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7632 return success_p;
7636 /* Set up IT to return characters from an ellipsis, if appropriate.
7637 The definition of the ellipsis glyphs may come from a display table
7638 entry. This function fills IT with the first glyph from the
7639 ellipsis if an ellipsis is to be displayed. */
7641 static int
7642 next_element_from_ellipsis (struct it *it)
7644 if (it->selective_display_ellipsis_p)
7645 setup_for_ellipsis (it, it->len);
7646 else
7648 /* The face at the current position may be different from the
7649 face we find after the invisible text. Remember what it
7650 was in IT->saved_face_id, and signal that it's there by
7651 setting face_before_selective_p. */
7652 it->saved_face_id = it->face_id;
7653 it->method = GET_FROM_BUFFER;
7654 it->object = it->w->buffer;
7655 reseat_at_next_visible_line_start (it, 1);
7656 it->face_before_selective_p = 1;
7659 return GET_NEXT_DISPLAY_ELEMENT (it);
7663 /* Deliver an image display element. The iterator IT is already
7664 filled with image information (done in handle_display_prop). Value
7665 is always 1. */
7668 static int
7669 next_element_from_image (struct it *it)
7671 it->what = IT_IMAGE;
7672 it->ignore_overlay_strings_at_pos_p = 0;
7673 return 1;
7677 /* Fill iterator IT with next display element from a stretch glyph
7678 property. IT->object is the value of the text property. Value is
7679 always 1. */
7681 static int
7682 next_element_from_stretch (struct it *it)
7684 it->what = IT_STRETCH;
7685 return 1;
7688 /* Scan backwards from IT's current position until we find a stop
7689 position, or until BEGV. This is called when we find ourself
7690 before both the last known prev_stop and base_level_stop while
7691 reordering bidirectional text. */
7693 static void
7694 compute_stop_pos_backwards (struct it *it)
7696 const int SCAN_BACK_LIMIT = 1000;
7697 struct text_pos pos;
7698 struct display_pos save_current = it->current;
7699 struct text_pos save_position = it->position;
7700 ptrdiff_t charpos = IT_CHARPOS (*it);
7701 ptrdiff_t where_we_are = charpos;
7702 ptrdiff_t save_stop_pos = it->stop_charpos;
7703 ptrdiff_t save_end_pos = it->end_charpos;
7705 eassert (NILP (it->string) && !it->s);
7706 eassert (it->bidi_p);
7707 it->bidi_p = 0;
7710 it->end_charpos = min (charpos + 1, ZV);
7711 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7712 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7713 reseat_1 (it, pos, 0);
7714 compute_stop_pos (it);
7715 /* We must advance forward, right? */
7716 if (it->stop_charpos <= charpos)
7717 abort ();
7719 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7721 if (it->stop_charpos <= where_we_are)
7722 it->prev_stop = it->stop_charpos;
7723 else
7724 it->prev_stop = BEGV;
7725 it->bidi_p = 1;
7726 it->current = save_current;
7727 it->position = save_position;
7728 it->stop_charpos = save_stop_pos;
7729 it->end_charpos = save_end_pos;
7732 /* Scan forward from CHARPOS in the current buffer/string, until we
7733 find a stop position > current IT's position. Then handle the stop
7734 position before that. This is called when we bump into a stop
7735 position while reordering bidirectional text. CHARPOS should be
7736 the last previously processed stop_pos (or BEGV/0, if none were
7737 processed yet) whose position is less that IT's current
7738 position. */
7740 static void
7741 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7743 int bufp = !STRINGP (it->string);
7744 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7745 struct display_pos save_current = it->current;
7746 struct text_pos save_position = it->position;
7747 struct text_pos pos1;
7748 ptrdiff_t next_stop;
7750 /* Scan in strict logical order. */
7751 eassert (it->bidi_p);
7752 it->bidi_p = 0;
7755 it->prev_stop = charpos;
7756 if (bufp)
7758 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7759 reseat_1 (it, pos1, 0);
7761 else
7762 it->current.string_pos = string_pos (charpos, it->string);
7763 compute_stop_pos (it);
7764 /* We must advance forward, right? */
7765 if (it->stop_charpos <= it->prev_stop)
7766 abort ();
7767 charpos = it->stop_charpos;
7769 while (charpos <= where_we_are);
7771 it->bidi_p = 1;
7772 it->current = save_current;
7773 it->position = save_position;
7774 next_stop = it->stop_charpos;
7775 it->stop_charpos = it->prev_stop;
7776 handle_stop (it);
7777 it->stop_charpos = next_stop;
7780 /* Load IT with the next display element from current_buffer. Value
7781 is zero if end of buffer reached. IT->stop_charpos is the next
7782 position at which to stop and check for text properties or buffer
7783 end. */
7785 static int
7786 next_element_from_buffer (struct it *it)
7788 int success_p = 1;
7790 eassert (IT_CHARPOS (*it) >= BEGV);
7791 eassert (NILP (it->string) && !it->s);
7792 eassert (!it->bidi_p
7793 || (EQ (it->bidi_it.string.lstring, Qnil)
7794 && it->bidi_it.string.s == NULL));
7796 /* With bidi reordering, the character to display might not be the
7797 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7798 we were reseat()ed to a new buffer position, which is potentially
7799 a different paragraph. */
7800 if (it->bidi_p && it->bidi_it.first_elt)
7802 get_visually_first_element (it);
7803 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7806 if (IT_CHARPOS (*it) >= it->stop_charpos)
7808 if (IT_CHARPOS (*it) >= it->end_charpos)
7810 int overlay_strings_follow_p;
7812 /* End of the game, except when overlay strings follow that
7813 haven't been returned yet. */
7814 if (it->overlay_strings_at_end_processed_p)
7815 overlay_strings_follow_p = 0;
7816 else
7818 it->overlay_strings_at_end_processed_p = 1;
7819 overlay_strings_follow_p = get_overlay_strings (it, 0);
7822 if (overlay_strings_follow_p)
7823 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7824 else
7826 it->what = IT_EOB;
7827 it->position = it->current.pos;
7828 success_p = 0;
7831 else if (!(!it->bidi_p
7832 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7833 || IT_CHARPOS (*it) == it->stop_charpos))
7835 /* With bidi non-linear iteration, we could find ourselves
7836 far beyond the last computed stop_charpos, with several
7837 other stop positions in between that we missed. Scan
7838 them all now, in buffer's logical order, until we find
7839 and handle the last stop_charpos that precedes our
7840 current position. */
7841 handle_stop_backwards (it, it->stop_charpos);
7842 return GET_NEXT_DISPLAY_ELEMENT (it);
7844 else
7846 if (it->bidi_p)
7848 /* Take note of the stop position we just moved across,
7849 for when we will move back across it. */
7850 it->prev_stop = it->stop_charpos;
7851 /* If we are at base paragraph embedding level, take
7852 note of the last stop position seen at this
7853 level. */
7854 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7855 it->base_level_stop = it->stop_charpos;
7857 handle_stop (it);
7858 return GET_NEXT_DISPLAY_ELEMENT (it);
7861 else if (it->bidi_p
7862 /* If we are before prev_stop, we may have overstepped on
7863 our way backwards a stop_pos, and if so, we need to
7864 handle that stop_pos. */
7865 && IT_CHARPOS (*it) < it->prev_stop
7866 /* We can sometimes back up for reasons that have nothing
7867 to do with bidi reordering. E.g., compositions. The
7868 code below is only needed when we are above the base
7869 embedding level, so test for that explicitly. */
7870 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7872 if (it->base_level_stop <= 0
7873 || IT_CHARPOS (*it) < it->base_level_stop)
7875 /* If we lost track of base_level_stop, we need to find
7876 prev_stop by looking backwards. This happens, e.g., when
7877 we were reseated to the previous screenful of text by
7878 vertical-motion. */
7879 it->base_level_stop = BEGV;
7880 compute_stop_pos_backwards (it);
7881 handle_stop_backwards (it, it->prev_stop);
7883 else
7884 handle_stop_backwards (it, it->base_level_stop);
7885 return GET_NEXT_DISPLAY_ELEMENT (it);
7887 else
7889 /* No face changes, overlays etc. in sight, so just return a
7890 character from current_buffer. */
7891 unsigned char *p;
7892 ptrdiff_t stop;
7894 /* Maybe run the redisplay end trigger hook. Performance note:
7895 This doesn't seem to cost measurable time. */
7896 if (it->redisplay_end_trigger_charpos
7897 && it->glyph_row
7898 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7899 run_redisplay_end_trigger_hook (it);
7901 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7902 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7903 stop)
7904 && next_element_from_composition (it))
7906 return 1;
7909 /* Get the next character, maybe multibyte. */
7910 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7911 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7912 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7913 else
7914 it->c = *p, it->len = 1;
7916 /* Record what we have and where it came from. */
7917 it->what = IT_CHARACTER;
7918 it->object = it->w->buffer;
7919 it->position = it->current.pos;
7921 /* Normally we return the character found above, except when we
7922 really want to return an ellipsis for selective display. */
7923 if (it->selective)
7925 if (it->c == '\n')
7927 /* A value of selective > 0 means hide lines indented more
7928 than that number of columns. */
7929 if (it->selective > 0
7930 && IT_CHARPOS (*it) + 1 < ZV
7931 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7932 IT_BYTEPOS (*it) + 1,
7933 it->selective))
7935 success_p = next_element_from_ellipsis (it);
7936 it->dpvec_char_len = -1;
7939 else if (it->c == '\r' && it->selective == -1)
7941 /* A value of selective == -1 means that everything from the
7942 CR to the end of the line is invisible, with maybe an
7943 ellipsis displayed for it. */
7944 success_p = next_element_from_ellipsis (it);
7945 it->dpvec_char_len = -1;
7950 /* Value is zero if end of buffer reached. */
7951 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7952 return success_p;
7956 /* Run the redisplay end trigger hook for IT. */
7958 static void
7959 run_redisplay_end_trigger_hook (struct it *it)
7961 Lisp_Object args[3];
7963 /* IT->glyph_row should be non-null, i.e. we should be actually
7964 displaying something, or otherwise we should not run the hook. */
7965 eassert (it->glyph_row);
7967 /* Set up hook arguments. */
7968 args[0] = Qredisplay_end_trigger_functions;
7969 args[1] = it->window;
7970 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7971 it->redisplay_end_trigger_charpos = 0;
7973 /* Since we are *trying* to run these functions, don't try to run
7974 them again, even if they get an error. */
7975 it->w->redisplay_end_trigger = Qnil;
7976 Frun_hook_with_args (3, args);
7978 /* Notice if it changed the face of the character we are on. */
7979 handle_face_prop (it);
7983 /* Deliver a composition display element. Unlike the other
7984 next_element_from_XXX, this function is not registered in the array
7985 get_next_element[]. It is called from next_element_from_buffer and
7986 next_element_from_string when necessary. */
7988 static int
7989 next_element_from_composition (struct it *it)
7991 it->what = IT_COMPOSITION;
7992 it->len = it->cmp_it.nbytes;
7993 if (STRINGP (it->string))
7995 if (it->c < 0)
7997 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7998 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7999 return 0;
8001 it->position = it->current.string_pos;
8002 it->object = it->string;
8003 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8004 IT_STRING_BYTEPOS (*it), it->string);
8006 else
8008 if (it->c < 0)
8010 IT_CHARPOS (*it) += it->cmp_it.nchars;
8011 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8012 if (it->bidi_p)
8014 if (it->bidi_it.new_paragraph)
8015 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8016 /* Resync the bidi iterator with IT's new position.
8017 FIXME: this doesn't support bidirectional text. */
8018 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8019 bidi_move_to_visually_next (&it->bidi_it);
8021 return 0;
8023 it->position = it->current.pos;
8024 it->object = it->w->buffer;
8025 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8026 IT_BYTEPOS (*it), Qnil);
8028 return 1;
8033 /***********************************************************************
8034 Moving an iterator without producing glyphs
8035 ***********************************************************************/
8037 /* Check if iterator is at a position corresponding to a valid buffer
8038 position after some move_it_ call. */
8040 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8041 ((it)->method == GET_FROM_STRING \
8042 ? IT_STRING_CHARPOS (*it) == 0 \
8043 : 1)
8046 /* Move iterator IT to a specified buffer or X position within one
8047 line on the display without producing glyphs.
8049 OP should be a bit mask including some or all of these bits:
8050 MOVE_TO_X: Stop upon reaching x-position TO_X.
8051 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8052 Regardless of OP's value, stop upon reaching the end of the display line.
8054 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8055 This means, in particular, that TO_X includes window's horizontal
8056 scroll amount.
8058 The return value has several possible values that
8059 say what condition caused the scan to stop:
8061 MOVE_POS_MATCH_OR_ZV
8062 - when TO_POS or ZV was reached.
8064 MOVE_X_REACHED
8065 -when TO_X was reached before TO_POS or ZV were reached.
8067 MOVE_LINE_CONTINUED
8068 - when we reached the end of the display area and the line must
8069 be continued.
8071 MOVE_LINE_TRUNCATED
8072 - when we reached the end of the display area and the line is
8073 truncated.
8075 MOVE_NEWLINE_OR_CR
8076 - when we stopped at a line end, i.e. a newline or a CR and selective
8077 display is on. */
8079 static enum move_it_result
8080 move_it_in_display_line_to (struct it *it,
8081 ptrdiff_t to_charpos, int to_x,
8082 enum move_operation_enum op)
8084 enum move_it_result result = MOVE_UNDEFINED;
8085 struct glyph_row *saved_glyph_row;
8086 struct it wrap_it, atpos_it, atx_it, ppos_it;
8087 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8088 void *ppos_data = NULL;
8089 int may_wrap = 0;
8090 enum it_method prev_method = it->method;
8091 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8092 int saw_smaller_pos = prev_pos < to_charpos;
8094 /* Don't produce glyphs in produce_glyphs. */
8095 saved_glyph_row = it->glyph_row;
8096 it->glyph_row = NULL;
8098 /* Use wrap_it to save a copy of IT wherever a word wrap could
8099 occur. Use atpos_it to save a copy of IT at the desired buffer
8100 position, if found, so that we can scan ahead and check if the
8101 word later overshoots the window edge. Use atx_it similarly, for
8102 pixel positions. */
8103 wrap_it.sp = -1;
8104 atpos_it.sp = -1;
8105 atx_it.sp = -1;
8107 /* Use ppos_it under bidi reordering to save a copy of IT for the
8108 position > CHARPOS that is the closest to CHARPOS. We restore
8109 that position in IT when we have scanned the entire display line
8110 without finding a match for CHARPOS and all the character
8111 positions are greater than CHARPOS. */
8112 if (it->bidi_p)
8114 SAVE_IT (ppos_it, *it, ppos_data);
8115 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8116 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8117 SAVE_IT (ppos_it, *it, ppos_data);
8120 #define BUFFER_POS_REACHED_P() \
8121 ((op & MOVE_TO_POS) != 0 \
8122 && BUFFERP (it->object) \
8123 && (IT_CHARPOS (*it) == to_charpos \
8124 || ((!it->bidi_p \
8125 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8126 && IT_CHARPOS (*it) > to_charpos) \
8127 || (it->what == IT_COMPOSITION \
8128 && ((IT_CHARPOS (*it) > to_charpos \
8129 && to_charpos >= it->cmp_it.charpos) \
8130 || (IT_CHARPOS (*it) < to_charpos \
8131 && to_charpos <= it->cmp_it.charpos)))) \
8132 && (it->method == GET_FROM_BUFFER \
8133 || (it->method == GET_FROM_DISPLAY_VECTOR \
8134 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8136 /* If there's a line-/wrap-prefix, handle it. */
8137 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8138 && it->current_y < it->last_visible_y)
8139 handle_line_prefix (it);
8141 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8142 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8144 while (1)
8146 int x, i, ascent = 0, descent = 0;
8148 /* Utility macro to reset an iterator with x, ascent, and descent. */
8149 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8150 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8151 (IT)->max_descent = descent)
8153 /* Stop if we move beyond TO_CHARPOS (after an image or a
8154 display string or stretch glyph). */
8155 if ((op & MOVE_TO_POS) != 0
8156 && BUFFERP (it->object)
8157 && it->method == GET_FROM_BUFFER
8158 && (((!it->bidi_p
8159 /* When the iterator is at base embedding level, we
8160 are guaranteed that characters are delivered for
8161 display in strictly increasing order of their
8162 buffer positions. */
8163 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8164 && IT_CHARPOS (*it) > to_charpos)
8165 || (it->bidi_p
8166 && (prev_method == GET_FROM_IMAGE
8167 || prev_method == GET_FROM_STRETCH
8168 || prev_method == GET_FROM_STRING)
8169 /* Passed TO_CHARPOS from left to right. */
8170 && ((prev_pos < to_charpos
8171 && IT_CHARPOS (*it) > to_charpos)
8172 /* Passed TO_CHARPOS from right to left. */
8173 || (prev_pos > to_charpos
8174 && IT_CHARPOS (*it) < to_charpos)))))
8176 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8178 result = MOVE_POS_MATCH_OR_ZV;
8179 break;
8181 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8182 /* If wrap_it is valid, the current position might be in a
8183 word that is wrapped. So, save the iterator in
8184 atpos_it and continue to see if wrapping happens. */
8185 SAVE_IT (atpos_it, *it, atpos_data);
8188 /* Stop when ZV reached.
8189 We used to stop here when TO_CHARPOS reached as well, but that is
8190 too soon if this glyph does not fit on this line. So we handle it
8191 explicitly below. */
8192 if (!get_next_display_element (it))
8194 result = MOVE_POS_MATCH_OR_ZV;
8195 break;
8198 if (it->line_wrap == TRUNCATE)
8200 if (BUFFER_POS_REACHED_P ())
8202 result = MOVE_POS_MATCH_OR_ZV;
8203 break;
8206 else
8208 if (it->line_wrap == WORD_WRAP)
8210 if (IT_DISPLAYING_WHITESPACE (it))
8211 may_wrap = 1;
8212 else if (may_wrap)
8214 /* We have reached a glyph that follows one or more
8215 whitespace characters. If the position is
8216 already found, we are done. */
8217 if (atpos_it.sp >= 0)
8219 RESTORE_IT (it, &atpos_it, atpos_data);
8220 result = MOVE_POS_MATCH_OR_ZV;
8221 goto done;
8223 if (atx_it.sp >= 0)
8225 RESTORE_IT (it, &atx_it, atx_data);
8226 result = MOVE_X_REACHED;
8227 goto done;
8229 /* Otherwise, we can wrap here. */
8230 SAVE_IT (wrap_it, *it, wrap_data);
8231 may_wrap = 0;
8236 /* Remember the line height for the current line, in case
8237 the next element doesn't fit on the line. */
8238 ascent = it->max_ascent;
8239 descent = it->max_descent;
8241 /* The call to produce_glyphs will get the metrics of the
8242 display element IT is loaded with. Record the x-position
8243 before this display element, in case it doesn't fit on the
8244 line. */
8245 x = it->current_x;
8247 PRODUCE_GLYPHS (it);
8249 if (it->area != TEXT_AREA)
8251 prev_method = it->method;
8252 if (it->method == GET_FROM_BUFFER)
8253 prev_pos = IT_CHARPOS (*it);
8254 set_iterator_to_next (it, 1);
8255 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8256 SET_TEXT_POS (this_line_min_pos,
8257 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8258 if (it->bidi_p
8259 && (op & MOVE_TO_POS)
8260 && IT_CHARPOS (*it) > to_charpos
8261 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8262 SAVE_IT (ppos_it, *it, ppos_data);
8263 continue;
8266 /* The number of glyphs we get back in IT->nglyphs will normally
8267 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8268 character on a terminal frame, or (iii) a line end. For the
8269 second case, IT->nglyphs - 1 padding glyphs will be present.
8270 (On X frames, there is only one glyph produced for a
8271 composite character.)
8273 The behavior implemented below means, for continuation lines,
8274 that as many spaces of a TAB as fit on the current line are
8275 displayed there. For terminal frames, as many glyphs of a
8276 multi-glyph character are displayed in the current line, too.
8277 This is what the old redisplay code did, and we keep it that
8278 way. Under X, the whole shape of a complex character must
8279 fit on the line or it will be completely displayed in the
8280 next line.
8282 Note that both for tabs and padding glyphs, all glyphs have
8283 the same width. */
8284 if (it->nglyphs)
8286 /* More than one glyph or glyph doesn't fit on line. All
8287 glyphs have the same width. */
8288 int single_glyph_width = it->pixel_width / it->nglyphs;
8289 int new_x;
8290 int x_before_this_char = x;
8291 int hpos_before_this_char = it->hpos;
8293 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8295 new_x = x + single_glyph_width;
8297 /* We want to leave anything reaching TO_X to the caller. */
8298 if ((op & MOVE_TO_X) && new_x > to_x)
8300 if (BUFFER_POS_REACHED_P ())
8302 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8303 goto buffer_pos_reached;
8304 if (atpos_it.sp < 0)
8306 SAVE_IT (atpos_it, *it, atpos_data);
8307 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8310 else
8312 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8314 it->current_x = x;
8315 result = MOVE_X_REACHED;
8316 break;
8318 if (atx_it.sp < 0)
8320 SAVE_IT (atx_it, *it, atx_data);
8321 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8326 if (/* Lines are continued. */
8327 it->line_wrap != TRUNCATE
8328 && (/* And glyph doesn't fit on the line. */
8329 new_x > it->last_visible_x
8330 /* Or it fits exactly and we're on a window
8331 system frame. */
8332 || (new_x == it->last_visible_x
8333 && FRAME_WINDOW_P (it->f)
8334 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8335 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8336 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8338 if (/* IT->hpos == 0 means the very first glyph
8339 doesn't fit on the line, e.g. a wide image. */
8340 it->hpos == 0
8341 || (new_x == it->last_visible_x
8342 && FRAME_WINDOW_P (it->f)))
8344 ++it->hpos;
8345 it->current_x = new_x;
8347 /* The character's last glyph just barely fits
8348 in this row. */
8349 if (i == it->nglyphs - 1)
8351 /* If this is the destination position,
8352 return a position *before* it in this row,
8353 now that we know it fits in this row. */
8354 if (BUFFER_POS_REACHED_P ())
8356 if (it->line_wrap != WORD_WRAP
8357 || wrap_it.sp < 0)
8359 it->hpos = hpos_before_this_char;
8360 it->current_x = x_before_this_char;
8361 result = MOVE_POS_MATCH_OR_ZV;
8362 break;
8364 if (it->line_wrap == WORD_WRAP
8365 && atpos_it.sp < 0)
8367 SAVE_IT (atpos_it, *it, atpos_data);
8368 atpos_it.current_x = x_before_this_char;
8369 atpos_it.hpos = hpos_before_this_char;
8373 prev_method = it->method;
8374 if (it->method == GET_FROM_BUFFER)
8375 prev_pos = IT_CHARPOS (*it);
8376 set_iterator_to_next (it, 1);
8377 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8378 SET_TEXT_POS (this_line_min_pos,
8379 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8380 /* On graphical terminals, newlines may
8381 "overflow" into the fringe if
8382 overflow-newline-into-fringe is non-nil.
8383 On text terminals, newlines may overflow
8384 into the last glyph on the display
8385 line.*/
8386 if (!FRAME_WINDOW_P (it->f)
8387 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8389 if (!get_next_display_element (it))
8391 result = MOVE_POS_MATCH_OR_ZV;
8392 break;
8394 if (BUFFER_POS_REACHED_P ())
8396 if (ITERATOR_AT_END_OF_LINE_P (it))
8397 result = MOVE_POS_MATCH_OR_ZV;
8398 else
8399 result = MOVE_LINE_CONTINUED;
8400 break;
8402 if (ITERATOR_AT_END_OF_LINE_P (it))
8404 result = MOVE_NEWLINE_OR_CR;
8405 break;
8410 else
8411 IT_RESET_X_ASCENT_DESCENT (it);
8413 if (wrap_it.sp >= 0)
8415 RESTORE_IT (it, &wrap_it, wrap_data);
8416 atpos_it.sp = -1;
8417 atx_it.sp = -1;
8420 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8421 IT_CHARPOS (*it)));
8422 result = MOVE_LINE_CONTINUED;
8423 break;
8426 if (BUFFER_POS_REACHED_P ())
8428 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8429 goto buffer_pos_reached;
8430 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8432 SAVE_IT (atpos_it, *it, atpos_data);
8433 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8437 if (new_x > it->first_visible_x)
8439 /* Glyph is visible. Increment number of glyphs that
8440 would be displayed. */
8441 ++it->hpos;
8445 if (result != MOVE_UNDEFINED)
8446 break;
8448 else if (BUFFER_POS_REACHED_P ())
8450 buffer_pos_reached:
8451 IT_RESET_X_ASCENT_DESCENT (it);
8452 result = MOVE_POS_MATCH_OR_ZV;
8453 break;
8455 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8457 /* Stop when TO_X specified and reached. This check is
8458 necessary here because of lines consisting of a line end,
8459 only. The line end will not produce any glyphs and we
8460 would never get MOVE_X_REACHED. */
8461 eassert (it->nglyphs == 0);
8462 result = MOVE_X_REACHED;
8463 break;
8466 /* Is this a line end? If yes, we're done. */
8467 if (ITERATOR_AT_END_OF_LINE_P (it))
8469 /* If we are past TO_CHARPOS, but never saw any character
8470 positions smaller than TO_CHARPOS, return
8471 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8472 did. */
8473 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8475 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8477 if (IT_CHARPOS (ppos_it) < ZV)
8479 RESTORE_IT (it, &ppos_it, ppos_data);
8480 result = MOVE_POS_MATCH_OR_ZV;
8482 else
8483 goto buffer_pos_reached;
8485 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8486 && IT_CHARPOS (*it) > to_charpos)
8487 goto buffer_pos_reached;
8488 else
8489 result = MOVE_NEWLINE_OR_CR;
8491 else
8492 result = MOVE_NEWLINE_OR_CR;
8493 break;
8496 prev_method = it->method;
8497 if (it->method == GET_FROM_BUFFER)
8498 prev_pos = IT_CHARPOS (*it);
8499 /* The current display element has been consumed. Advance
8500 to the next. */
8501 set_iterator_to_next (it, 1);
8502 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8503 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8504 if (IT_CHARPOS (*it) < to_charpos)
8505 saw_smaller_pos = 1;
8506 if (it->bidi_p
8507 && (op & MOVE_TO_POS)
8508 && IT_CHARPOS (*it) >= to_charpos
8509 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8510 SAVE_IT (ppos_it, *it, ppos_data);
8512 /* Stop if lines are truncated and IT's current x-position is
8513 past the right edge of the window now. */
8514 if (it->line_wrap == TRUNCATE
8515 && it->current_x >= it->last_visible_x)
8517 if (!FRAME_WINDOW_P (it->f)
8518 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8519 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8520 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
8521 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8523 int at_eob_p = 0;
8525 if ((at_eob_p = !get_next_display_element (it))
8526 || BUFFER_POS_REACHED_P ()
8527 /* If we are past TO_CHARPOS, but never saw any
8528 character positions smaller than TO_CHARPOS,
8529 return MOVE_POS_MATCH_OR_ZV, like the
8530 unidirectional display did. */
8531 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8532 && !saw_smaller_pos
8533 && IT_CHARPOS (*it) > to_charpos))
8535 if (it->bidi_p
8536 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8537 RESTORE_IT (it, &ppos_it, ppos_data);
8538 result = MOVE_POS_MATCH_OR_ZV;
8539 break;
8541 if (ITERATOR_AT_END_OF_LINE_P (it))
8543 result = MOVE_NEWLINE_OR_CR;
8544 break;
8547 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8548 && !saw_smaller_pos
8549 && IT_CHARPOS (*it) > to_charpos)
8551 if (IT_CHARPOS (ppos_it) < ZV)
8552 RESTORE_IT (it, &ppos_it, ppos_data);
8553 result = MOVE_POS_MATCH_OR_ZV;
8554 break;
8556 result = MOVE_LINE_TRUNCATED;
8557 break;
8559 #undef IT_RESET_X_ASCENT_DESCENT
8562 #undef BUFFER_POS_REACHED_P
8564 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8565 restore the saved iterator. */
8566 if (atpos_it.sp >= 0)
8567 RESTORE_IT (it, &atpos_it, atpos_data);
8568 else if (atx_it.sp >= 0)
8569 RESTORE_IT (it, &atx_it, atx_data);
8571 done:
8573 if (atpos_data)
8574 bidi_unshelve_cache (atpos_data, 1);
8575 if (atx_data)
8576 bidi_unshelve_cache (atx_data, 1);
8577 if (wrap_data)
8578 bidi_unshelve_cache (wrap_data, 1);
8579 if (ppos_data)
8580 bidi_unshelve_cache (ppos_data, 1);
8582 /* Restore the iterator settings altered at the beginning of this
8583 function. */
8584 it->glyph_row = saved_glyph_row;
8585 return result;
8588 /* For external use. */
8589 void
8590 move_it_in_display_line (struct it *it,
8591 ptrdiff_t to_charpos, int to_x,
8592 enum move_operation_enum op)
8594 if (it->line_wrap == WORD_WRAP
8595 && (op & MOVE_TO_X))
8597 struct it save_it;
8598 void *save_data = NULL;
8599 int skip;
8601 SAVE_IT (save_it, *it, save_data);
8602 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8603 /* When word-wrap is on, TO_X may lie past the end
8604 of a wrapped line. Then it->current is the
8605 character on the next line, so backtrack to the
8606 space before the wrap point. */
8607 if (skip == MOVE_LINE_CONTINUED)
8609 int prev_x = max (it->current_x - 1, 0);
8610 RESTORE_IT (it, &save_it, save_data);
8611 move_it_in_display_line_to
8612 (it, -1, prev_x, MOVE_TO_X);
8614 else
8615 bidi_unshelve_cache (save_data, 1);
8617 else
8618 move_it_in_display_line_to (it, to_charpos, to_x, op);
8622 /* Move IT forward until it satisfies one or more of the criteria in
8623 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8625 OP is a bit-mask that specifies where to stop, and in particular,
8626 which of those four position arguments makes a difference. See the
8627 description of enum move_operation_enum.
8629 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8630 screen line, this function will set IT to the next position that is
8631 displayed to the right of TO_CHARPOS on the screen. */
8633 void
8634 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8636 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8637 int line_height, line_start_x = 0, reached = 0;
8638 void *backup_data = NULL;
8640 for (;;)
8642 if (op & MOVE_TO_VPOS)
8644 /* If no TO_CHARPOS and no TO_X specified, stop at the
8645 start of the line TO_VPOS. */
8646 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8648 if (it->vpos == to_vpos)
8650 reached = 1;
8651 break;
8653 else
8654 skip = move_it_in_display_line_to (it, -1, -1, 0);
8656 else
8658 /* TO_VPOS >= 0 means stop at TO_X in the line at
8659 TO_VPOS, or at TO_POS, whichever comes first. */
8660 if (it->vpos == to_vpos)
8662 reached = 2;
8663 break;
8666 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8668 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8670 reached = 3;
8671 break;
8673 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8675 /* We have reached TO_X but not in the line we want. */
8676 skip = move_it_in_display_line_to (it, to_charpos,
8677 -1, MOVE_TO_POS);
8678 if (skip == MOVE_POS_MATCH_OR_ZV)
8680 reached = 4;
8681 break;
8686 else if (op & MOVE_TO_Y)
8688 struct it it_backup;
8690 if (it->line_wrap == WORD_WRAP)
8691 SAVE_IT (it_backup, *it, backup_data);
8693 /* TO_Y specified means stop at TO_X in the line containing
8694 TO_Y---or at TO_CHARPOS if this is reached first. The
8695 problem is that we can't really tell whether the line
8696 contains TO_Y before we have completely scanned it, and
8697 this may skip past TO_X. What we do is to first scan to
8698 TO_X.
8700 If TO_X is not specified, use a TO_X of zero. The reason
8701 is to make the outcome of this function more predictable.
8702 If we didn't use TO_X == 0, we would stop at the end of
8703 the line which is probably not what a caller would expect
8704 to happen. */
8705 skip = move_it_in_display_line_to
8706 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8707 (MOVE_TO_X | (op & MOVE_TO_POS)));
8709 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8710 if (skip == MOVE_POS_MATCH_OR_ZV)
8711 reached = 5;
8712 else if (skip == MOVE_X_REACHED)
8714 /* If TO_X was reached, we want to know whether TO_Y is
8715 in the line. We know this is the case if the already
8716 scanned glyphs make the line tall enough. Otherwise,
8717 we must check by scanning the rest of the line. */
8718 line_height = it->max_ascent + it->max_descent;
8719 if (to_y >= it->current_y
8720 && to_y < it->current_y + line_height)
8722 reached = 6;
8723 break;
8725 SAVE_IT (it_backup, *it, backup_data);
8726 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8727 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8728 op & MOVE_TO_POS);
8729 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8730 line_height = it->max_ascent + it->max_descent;
8731 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8733 if (to_y >= it->current_y
8734 && to_y < it->current_y + line_height)
8736 /* If TO_Y is in this line and TO_X was reached
8737 above, we scanned too far. We have to restore
8738 IT's settings to the ones before skipping. But
8739 keep the more accurate values of max_ascent and
8740 max_descent we've found while skipping the rest
8741 of the line, for the sake of callers, such as
8742 pos_visible_p, that need to know the line
8743 height. */
8744 int max_ascent = it->max_ascent;
8745 int max_descent = it->max_descent;
8747 RESTORE_IT (it, &it_backup, backup_data);
8748 it->max_ascent = max_ascent;
8749 it->max_descent = max_descent;
8750 reached = 6;
8752 else
8754 skip = skip2;
8755 if (skip == MOVE_POS_MATCH_OR_ZV)
8756 reached = 7;
8759 else
8761 /* Check whether TO_Y is in this line. */
8762 line_height = it->max_ascent + it->max_descent;
8763 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8765 if (to_y >= it->current_y
8766 && to_y < it->current_y + line_height)
8768 /* When word-wrap is on, TO_X may lie past the end
8769 of a wrapped line. Then it->current is the
8770 character on the next line, so backtrack to the
8771 space before the wrap point. */
8772 if (skip == MOVE_LINE_CONTINUED
8773 && it->line_wrap == WORD_WRAP)
8775 int prev_x = max (it->current_x - 1, 0);
8776 RESTORE_IT (it, &it_backup, backup_data);
8777 skip = move_it_in_display_line_to
8778 (it, -1, prev_x, MOVE_TO_X);
8780 reached = 6;
8784 if (reached)
8785 break;
8787 else if (BUFFERP (it->object)
8788 && (it->method == GET_FROM_BUFFER
8789 || it->method == GET_FROM_STRETCH)
8790 && IT_CHARPOS (*it) >= to_charpos
8791 /* Under bidi iteration, a call to set_iterator_to_next
8792 can scan far beyond to_charpos if the initial
8793 portion of the next line needs to be reordered. In
8794 that case, give move_it_in_display_line_to another
8795 chance below. */
8796 && !(it->bidi_p
8797 && it->bidi_it.scan_dir == -1))
8798 skip = MOVE_POS_MATCH_OR_ZV;
8799 else
8800 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8802 switch (skip)
8804 case MOVE_POS_MATCH_OR_ZV:
8805 reached = 8;
8806 goto out;
8808 case MOVE_NEWLINE_OR_CR:
8809 set_iterator_to_next (it, 1);
8810 it->continuation_lines_width = 0;
8811 break;
8813 case MOVE_LINE_TRUNCATED:
8814 it->continuation_lines_width = 0;
8815 reseat_at_next_visible_line_start (it, 0);
8816 if ((op & MOVE_TO_POS) != 0
8817 && IT_CHARPOS (*it) > to_charpos)
8819 reached = 9;
8820 goto out;
8822 break;
8824 case MOVE_LINE_CONTINUED:
8825 /* For continued lines ending in a tab, some of the glyphs
8826 associated with the tab are displayed on the current
8827 line. Since it->current_x does not include these glyphs,
8828 we use it->last_visible_x instead. */
8829 if (it->c == '\t')
8831 it->continuation_lines_width += it->last_visible_x;
8832 /* When moving by vpos, ensure that the iterator really
8833 advances to the next line (bug#847, bug#969). Fixme:
8834 do we need to do this in other circumstances? */
8835 if (it->current_x != it->last_visible_x
8836 && (op & MOVE_TO_VPOS)
8837 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8839 line_start_x = it->current_x + it->pixel_width
8840 - it->last_visible_x;
8841 set_iterator_to_next (it, 0);
8844 else
8845 it->continuation_lines_width += it->current_x;
8846 break;
8848 default:
8849 abort ();
8852 /* Reset/increment for the next run. */
8853 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8854 it->current_x = line_start_x;
8855 line_start_x = 0;
8856 it->hpos = 0;
8857 it->current_y += it->max_ascent + it->max_descent;
8858 ++it->vpos;
8859 last_height = it->max_ascent + it->max_descent;
8860 last_max_ascent = it->max_ascent;
8861 it->max_ascent = it->max_descent = 0;
8864 out:
8866 /* On text terminals, we may stop at the end of a line in the middle
8867 of a multi-character glyph. If the glyph itself is continued,
8868 i.e. it is actually displayed on the next line, don't treat this
8869 stopping point as valid; move to the next line instead (unless
8870 that brings us offscreen). */
8871 if (!FRAME_WINDOW_P (it->f)
8872 && op & MOVE_TO_POS
8873 && IT_CHARPOS (*it) == to_charpos
8874 && it->what == IT_CHARACTER
8875 && it->nglyphs > 1
8876 && it->line_wrap == WINDOW_WRAP
8877 && it->current_x == it->last_visible_x - 1
8878 && it->c != '\n'
8879 && it->c != '\t'
8880 && it->vpos < XFASTINT (it->w->window_end_vpos))
8882 it->continuation_lines_width += it->current_x;
8883 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8884 it->current_y += it->max_ascent + it->max_descent;
8885 ++it->vpos;
8886 last_height = it->max_ascent + it->max_descent;
8887 last_max_ascent = it->max_ascent;
8890 if (backup_data)
8891 bidi_unshelve_cache (backup_data, 1);
8893 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8897 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8899 If DY > 0, move IT backward at least that many pixels. DY = 0
8900 means move IT backward to the preceding line start or BEGV. This
8901 function may move over more than DY pixels if IT->current_y - DY
8902 ends up in the middle of a line; in this case IT->current_y will be
8903 set to the top of the line moved to. */
8905 void
8906 move_it_vertically_backward (struct it *it, int dy)
8908 int nlines, h;
8909 struct it it2, it3;
8910 void *it2data = NULL, *it3data = NULL;
8911 ptrdiff_t start_pos;
8913 move_further_back:
8914 eassert (dy >= 0);
8916 start_pos = IT_CHARPOS (*it);
8918 /* Estimate how many newlines we must move back. */
8919 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8921 /* Set the iterator's position that many lines back. */
8922 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8923 back_to_previous_visible_line_start (it);
8925 /* Reseat the iterator here. When moving backward, we don't want
8926 reseat to skip forward over invisible text, set up the iterator
8927 to deliver from overlay strings at the new position etc. So,
8928 use reseat_1 here. */
8929 reseat_1 (it, it->current.pos, 1);
8931 /* We are now surely at a line start. */
8932 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8933 reordering is in effect. */
8934 it->continuation_lines_width = 0;
8936 /* Move forward and see what y-distance we moved. First move to the
8937 start of the next line so that we get its height. We need this
8938 height to be able to tell whether we reached the specified
8939 y-distance. */
8940 SAVE_IT (it2, *it, it2data);
8941 it2.max_ascent = it2.max_descent = 0;
8944 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8945 MOVE_TO_POS | MOVE_TO_VPOS);
8947 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8948 /* If we are in a display string which starts at START_POS,
8949 and that display string includes a newline, and we are
8950 right after that newline (i.e. at the beginning of a
8951 display line), exit the loop, because otherwise we will
8952 infloop, since move_it_to will see that it is already at
8953 START_POS and will not move. */
8954 || (it2.method == GET_FROM_STRING
8955 && IT_CHARPOS (it2) == start_pos
8956 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8957 eassert (IT_CHARPOS (*it) >= BEGV);
8958 SAVE_IT (it3, it2, it3data);
8960 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8961 eassert (IT_CHARPOS (*it) >= BEGV);
8962 /* H is the actual vertical distance from the position in *IT
8963 and the starting position. */
8964 h = it2.current_y - it->current_y;
8965 /* NLINES is the distance in number of lines. */
8966 nlines = it2.vpos - it->vpos;
8968 /* Correct IT's y and vpos position
8969 so that they are relative to the starting point. */
8970 it->vpos -= nlines;
8971 it->current_y -= h;
8973 if (dy == 0)
8975 /* DY == 0 means move to the start of the screen line. The
8976 value of nlines is > 0 if continuation lines were involved,
8977 or if the original IT position was at start of a line. */
8978 RESTORE_IT (it, it, it2data);
8979 if (nlines > 0)
8980 move_it_by_lines (it, nlines);
8981 /* The above code moves us to some position NLINES down,
8982 usually to its first glyph (leftmost in an L2R line), but
8983 that's not necessarily the start of the line, under bidi
8984 reordering. We want to get to the character position
8985 that is immediately after the newline of the previous
8986 line. */
8987 if (it->bidi_p
8988 && !it->continuation_lines_width
8989 && !STRINGP (it->string)
8990 && IT_CHARPOS (*it) > BEGV
8991 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8993 ptrdiff_t nl_pos =
8994 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8996 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8998 bidi_unshelve_cache (it3data, 1);
9000 else
9002 /* The y-position we try to reach, relative to *IT.
9003 Note that H has been subtracted in front of the if-statement. */
9004 int target_y = it->current_y + h - dy;
9005 int y0 = it3.current_y;
9006 int y1;
9007 int line_height;
9009 RESTORE_IT (&it3, &it3, it3data);
9010 y1 = line_bottom_y (&it3);
9011 line_height = y1 - y0;
9012 RESTORE_IT (it, it, it2data);
9013 /* If we did not reach target_y, try to move further backward if
9014 we can. If we moved too far backward, try to move forward. */
9015 if (target_y < it->current_y
9016 /* This is heuristic. In a window that's 3 lines high, with
9017 a line height of 13 pixels each, recentering with point
9018 on the bottom line will try to move -39/2 = 19 pixels
9019 backward. Try to avoid moving into the first line. */
9020 && (it->current_y - target_y
9021 > min (window_box_height (it->w), line_height * 2 / 3))
9022 && IT_CHARPOS (*it) > BEGV)
9024 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9025 target_y - it->current_y));
9026 dy = it->current_y - target_y;
9027 goto move_further_back;
9029 else if (target_y >= it->current_y + line_height
9030 && IT_CHARPOS (*it) < ZV)
9032 /* Should move forward by at least one line, maybe more.
9034 Note: Calling move_it_by_lines can be expensive on
9035 terminal frames, where compute_motion is used (via
9036 vmotion) to do the job, when there are very long lines
9037 and truncate-lines is nil. That's the reason for
9038 treating terminal frames specially here. */
9040 if (!FRAME_WINDOW_P (it->f))
9041 move_it_vertically (it, target_y - (it->current_y + line_height));
9042 else
9046 move_it_by_lines (it, 1);
9048 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9055 /* Move IT by a specified amount of pixel lines DY. DY negative means
9056 move backwards. DY = 0 means move to start of screen line. At the
9057 end, IT will be on the start of a screen line. */
9059 void
9060 move_it_vertically (struct it *it, int dy)
9062 if (dy <= 0)
9063 move_it_vertically_backward (it, -dy);
9064 else
9066 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9067 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9068 MOVE_TO_POS | MOVE_TO_Y);
9069 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9071 /* If buffer ends in ZV without a newline, move to the start of
9072 the line to satisfy the post-condition. */
9073 if (IT_CHARPOS (*it) == ZV
9074 && ZV > BEGV
9075 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9076 move_it_by_lines (it, 0);
9081 /* Move iterator IT past the end of the text line it is in. */
9083 void
9084 move_it_past_eol (struct it *it)
9086 enum move_it_result rc;
9088 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9089 if (rc == MOVE_NEWLINE_OR_CR)
9090 set_iterator_to_next (it, 0);
9094 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9095 negative means move up. DVPOS == 0 means move to the start of the
9096 screen line.
9098 Optimization idea: If we would know that IT->f doesn't use
9099 a face with proportional font, we could be faster for
9100 truncate-lines nil. */
9102 void
9103 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9106 /* The commented-out optimization uses vmotion on terminals. This
9107 gives bad results, because elements like it->what, on which
9108 callers such as pos_visible_p rely, aren't updated. */
9109 /* struct position pos;
9110 if (!FRAME_WINDOW_P (it->f))
9112 struct text_pos textpos;
9114 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9115 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9116 reseat (it, textpos, 1);
9117 it->vpos += pos.vpos;
9118 it->current_y += pos.vpos;
9120 else */
9122 if (dvpos == 0)
9124 /* DVPOS == 0 means move to the start of the screen line. */
9125 move_it_vertically_backward (it, 0);
9126 /* Let next call to line_bottom_y calculate real line height */
9127 last_height = 0;
9129 else if (dvpos > 0)
9131 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9132 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9134 /* Only move to the next buffer position if we ended up in a
9135 string from display property, not in an overlay string
9136 (before-string or after-string). That is because the
9137 latter don't conceal the underlying buffer position, so
9138 we can ask to move the iterator to the exact position we
9139 are interested in. Note that, even if we are already at
9140 IT_CHARPOS (*it), the call below is not a no-op, as it
9141 will detect that we are at the end of the string, pop the
9142 iterator, and compute it->current_x and it->hpos
9143 correctly. */
9144 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9145 -1, -1, -1, MOVE_TO_POS);
9148 else
9150 struct it it2;
9151 void *it2data = NULL;
9152 ptrdiff_t start_charpos, i;
9154 /* Start at the beginning of the screen line containing IT's
9155 position. This may actually move vertically backwards,
9156 in case of overlays, so adjust dvpos accordingly. */
9157 dvpos += it->vpos;
9158 move_it_vertically_backward (it, 0);
9159 dvpos -= it->vpos;
9161 /* Go back -DVPOS visible lines and reseat the iterator there. */
9162 start_charpos = IT_CHARPOS (*it);
9163 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9164 back_to_previous_visible_line_start (it);
9165 reseat (it, it->current.pos, 1);
9167 /* Move further back if we end up in a string or an image. */
9168 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9170 /* First try to move to start of display line. */
9171 dvpos += it->vpos;
9172 move_it_vertically_backward (it, 0);
9173 dvpos -= it->vpos;
9174 if (IT_POS_VALID_AFTER_MOVE_P (it))
9175 break;
9176 /* If start of line is still in string or image,
9177 move further back. */
9178 back_to_previous_visible_line_start (it);
9179 reseat (it, it->current.pos, 1);
9180 dvpos--;
9183 it->current_x = it->hpos = 0;
9185 /* Above call may have moved too far if continuation lines
9186 are involved. Scan forward and see if it did. */
9187 SAVE_IT (it2, *it, it2data);
9188 it2.vpos = it2.current_y = 0;
9189 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9190 it->vpos -= it2.vpos;
9191 it->current_y -= it2.current_y;
9192 it->current_x = it->hpos = 0;
9194 /* If we moved too far back, move IT some lines forward. */
9195 if (it2.vpos > -dvpos)
9197 int delta = it2.vpos + dvpos;
9199 RESTORE_IT (&it2, &it2, it2data);
9200 SAVE_IT (it2, *it, it2data);
9201 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9202 /* Move back again if we got too far ahead. */
9203 if (IT_CHARPOS (*it) >= start_charpos)
9204 RESTORE_IT (it, &it2, it2data);
9205 else
9206 bidi_unshelve_cache (it2data, 1);
9208 else
9209 RESTORE_IT (it, it, it2data);
9213 /* Return 1 if IT points into the middle of a display vector. */
9216 in_display_vector_p (struct it *it)
9218 return (it->method == GET_FROM_DISPLAY_VECTOR
9219 && it->current.dpvec_index > 0
9220 && it->dpvec + it->current.dpvec_index != it->dpend);
9224 /***********************************************************************
9225 Messages
9226 ***********************************************************************/
9229 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9230 to *Messages*. */
9232 void
9233 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9235 Lisp_Object args[3];
9236 Lisp_Object msg, fmt;
9237 char *buffer;
9238 ptrdiff_t len;
9239 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9240 USE_SAFE_ALLOCA;
9242 /* Do nothing if called asynchronously. Inserting text into
9243 a buffer may call after-change-functions and alike and
9244 that would means running Lisp asynchronously. */
9245 if (handling_signal)
9246 return;
9248 fmt = msg = Qnil;
9249 GCPRO4 (fmt, msg, arg1, arg2);
9251 args[0] = fmt = build_string (format);
9252 args[1] = arg1;
9253 args[2] = arg2;
9254 msg = Fformat (3, args);
9256 len = SBYTES (msg) + 1;
9257 SAFE_ALLOCA (buffer, char *, len);
9258 memcpy (buffer, SDATA (msg), len);
9260 message_dolog (buffer, len - 1, 1, 0);
9261 SAFE_FREE ();
9263 UNGCPRO;
9267 /* Output a newline in the *Messages* buffer if "needs" one. */
9269 void
9270 message_log_maybe_newline (void)
9272 if (message_log_need_newline)
9273 message_dolog ("", 0, 1, 0);
9277 /* Add a string M of length NBYTES to the message log, optionally
9278 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9279 nonzero, means interpret the contents of M as multibyte. This
9280 function calls low-level routines in order to bypass text property
9281 hooks, etc. which might not be safe to run.
9283 This may GC (insert may run before/after change hooks),
9284 so the buffer M must NOT point to a Lisp string. */
9286 void
9287 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9289 const unsigned char *msg = (const unsigned char *) m;
9291 if (!NILP (Vmemory_full))
9292 return;
9294 if (!NILP (Vmessage_log_max))
9296 struct buffer *oldbuf;
9297 Lisp_Object oldpoint, oldbegv, oldzv;
9298 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9299 ptrdiff_t point_at_end = 0;
9300 ptrdiff_t zv_at_end = 0;
9301 Lisp_Object old_deactivate_mark, tem;
9302 struct gcpro gcpro1;
9304 old_deactivate_mark = Vdeactivate_mark;
9305 oldbuf = current_buffer;
9306 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9307 BVAR (current_buffer, undo_list) = Qt;
9309 oldpoint = message_dolog_marker1;
9310 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9311 oldbegv = message_dolog_marker2;
9312 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9313 oldzv = message_dolog_marker3;
9314 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9315 GCPRO1 (old_deactivate_mark);
9317 if (PT == Z)
9318 point_at_end = 1;
9319 if (ZV == Z)
9320 zv_at_end = 1;
9322 BEGV = BEG;
9323 BEGV_BYTE = BEG_BYTE;
9324 ZV = Z;
9325 ZV_BYTE = Z_BYTE;
9326 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9328 /* Insert the string--maybe converting multibyte to single byte
9329 or vice versa, so that all the text fits the buffer. */
9330 if (multibyte
9331 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9333 ptrdiff_t i;
9334 int c, char_bytes;
9335 char work[1];
9337 /* Convert a multibyte string to single-byte
9338 for the *Message* buffer. */
9339 for (i = 0; i < nbytes; i += char_bytes)
9341 c = string_char_and_length (msg + i, &char_bytes);
9342 work[0] = (ASCII_CHAR_P (c)
9344 : multibyte_char_to_unibyte (c));
9345 insert_1_both (work, 1, 1, 1, 0, 0);
9348 else if (! multibyte
9349 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9351 ptrdiff_t i;
9352 int c, char_bytes;
9353 unsigned char str[MAX_MULTIBYTE_LENGTH];
9354 /* Convert a single-byte string to multibyte
9355 for the *Message* buffer. */
9356 for (i = 0; i < nbytes; i++)
9358 c = msg[i];
9359 MAKE_CHAR_MULTIBYTE (c);
9360 char_bytes = CHAR_STRING (c, str);
9361 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9364 else if (nbytes)
9365 insert_1 (m, nbytes, 1, 0, 0);
9367 if (nlflag)
9369 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9370 printmax_t dups;
9371 insert_1 ("\n", 1, 1, 0, 0);
9373 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9374 this_bol = PT;
9375 this_bol_byte = PT_BYTE;
9377 /* See if this line duplicates the previous one.
9378 If so, combine duplicates. */
9379 if (this_bol > BEG)
9381 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9382 prev_bol = PT;
9383 prev_bol_byte = PT_BYTE;
9385 dups = message_log_check_duplicate (prev_bol_byte,
9386 this_bol_byte);
9387 if (dups)
9389 del_range_both (prev_bol, prev_bol_byte,
9390 this_bol, this_bol_byte, 0);
9391 if (dups > 1)
9393 char dupstr[sizeof " [ times]"
9394 + INT_STRLEN_BOUND (printmax_t)];
9396 /* If you change this format, don't forget to also
9397 change message_log_check_duplicate. */
9398 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9399 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9400 insert_1 (dupstr, duplen, 1, 0, 1);
9405 /* If we have more than the desired maximum number of lines
9406 in the *Messages* buffer now, delete the oldest ones.
9407 This is safe because we don't have undo in this buffer. */
9409 if (NATNUMP (Vmessage_log_max))
9411 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9412 -XFASTINT (Vmessage_log_max) - 1, 0);
9413 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9416 BEGV = XMARKER (oldbegv)->charpos;
9417 BEGV_BYTE = marker_byte_position (oldbegv);
9419 if (zv_at_end)
9421 ZV = Z;
9422 ZV_BYTE = Z_BYTE;
9424 else
9426 ZV = XMARKER (oldzv)->charpos;
9427 ZV_BYTE = marker_byte_position (oldzv);
9430 if (point_at_end)
9431 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9432 else
9433 /* We can't do Fgoto_char (oldpoint) because it will run some
9434 Lisp code. */
9435 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9436 XMARKER (oldpoint)->bytepos);
9438 UNGCPRO;
9439 unchain_marker (XMARKER (oldpoint));
9440 unchain_marker (XMARKER (oldbegv));
9441 unchain_marker (XMARKER (oldzv));
9443 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9444 set_buffer_internal (oldbuf);
9445 if (NILP (tem))
9446 windows_or_buffers_changed = old_windows_or_buffers_changed;
9447 message_log_need_newline = !nlflag;
9448 Vdeactivate_mark = old_deactivate_mark;
9453 /* We are at the end of the buffer after just having inserted a newline.
9454 (Note: We depend on the fact we won't be crossing the gap.)
9455 Check to see if the most recent message looks a lot like the previous one.
9456 Return 0 if different, 1 if the new one should just replace it, or a
9457 value N > 1 if we should also append " [N times]". */
9459 static intmax_t
9460 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9462 ptrdiff_t i;
9463 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9464 int seen_dots = 0;
9465 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9466 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9468 for (i = 0; i < len; i++)
9470 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9471 seen_dots = 1;
9472 if (p1[i] != p2[i])
9473 return seen_dots;
9475 p1 += len;
9476 if (*p1 == '\n')
9477 return 2;
9478 if (*p1++ == ' ' && *p1++ == '[')
9480 char *pend;
9481 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9482 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9483 return n+1;
9485 return 0;
9489 /* Display an echo area message M with a specified length of NBYTES
9490 bytes. The string may include null characters. If M is 0, clear
9491 out any existing message, and let the mini-buffer text show
9492 through.
9494 This may GC, so the buffer M must NOT point to a Lisp string. */
9496 void
9497 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9499 /* First flush out any partial line written with print. */
9500 message_log_maybe_newline ();
9501 if (m)
9502 message_dolog (m, nbytes, 1, multibyte);
9503 message2_nolog (m, nbytes, multibyte);
9507 /* The non-logging counterpart of message2. */
9509 void
9510 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9512 struct frame *sf = SELECTED_FRAME ();
9513 message_enable_multibyte = multibyte;
9515 if (FRAME_INITIAL_P (sf))
9517 if (noninteractive_need_newline)
9518 putc ('\n', stderr);
9519 noninteractive_need_newline = 0;
9520 if (m)
9521 fwrite (m, nbytes, 1, stderr);
9522 if (cursor_in_echo_area == 0)
9523 fprintf (stderr, "\n");
9524 fflush (stderr);
9526 /* A null message buffer means that the frame hasn't really been
9527 initialized yet. Error messages get reported properly by
9528 cmd_error, so this must be just an informative message; toss it. */
9529 else if (INTERACTIVE
9530 && sf->glyphs_initialized_p
9531 && FRAME_MESSAGE_BUF (sf))
9533 Lisp_Object mini_window;
9534 struct frame *f;
9536 /* Get the frame containing the mini-buffer
9537 that the selected frame is using. */
9538 mini_window = FRAME_MINIBUF_WINDOW (sf);
9539 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9541 FRAME_SAMPLE_VISIBILITY (f);
9542 if (FRAME_VISIBLE_P (sf)
9543 && ! FRAME_VISIBLE_P (f))
9544 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9546 if (m)
9548 set_message (m, Qnil, nbytes, multibyte);
9549 if (minibuffer_auto_raise)
9550 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9552 else
9553 clear_message (1, 1);
9555 do_pending_window_change (0);
9556 echo_area_display (1);
9557 do_pending_window_change (0);
9558 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9559 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9564 /* Display an echo area message M with a specified length of NBYTES
9565 bytes. The string may include null characters. If M is not a
9566 string, clear out any existing message, and let the mini-buffer
9567 text show through.
9569 This function cancels echoing. */
9571 void
9572 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9574 struct gcpro gcpro1;
9576 GCPRO1 (m);
9577 clear_message (1,1);
9578 cancel_echoing ();
9580 /* First flush out any partial line written with print. */
9581 message_log_maybe_newline ();
9582 if (STRINGP (m))
9584 char *buffer;
9585 USE_SAFE_ALLOCA;
9587 SAFE_ALLOCA (buffer, char *, nbytes);
9588 memcpy (buffer, SDATA (m), nbytes);
9589 message_dolog (buffer, nbytes, 1, multibyte);
9590 SAFE_FREE ();
9592 message3_nolog (m, nbytes, multibyte);
9594 UNGCPRO;
9598 /* The non-logging version of message3.
9599 This does not cancel echoing, because it is used for echoing.
9600 Perhaps we need to make a separate function for echoing
9601 and make this cancel echoing. */
9603 void
9604 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9606 struct frame *sf = SELECTED_FRAME ();
9607 message_enable_multibyte = multibyte;
9609 if (FRAME_INITIAL_P (sf))
9611 if (noninteractive_need_newline)
9612 putc ('\n', stderr);
9613 noninteractive_need_newline = 0;
9614 if (STRINGP (m))
9615 fwrite (SDATA (m), nbytes, 1, stderr);
9616 if (cursor_in_echo_area == 0)
9617 fprintf (stderr, "\n");
9618 fflush (stderr);
9620 /* A null message buffer means that the frame hasn't really been
9621 initialized yet. Error messages get reported properly by
9622 cmd_error, so this must be just an informative message; toss it. */
9623 else if (INTERACTIVE
9624 && sf->glyphs_initialized_p
9625 && FRAME_MESSAGE_BUF (sf))
9627 Lisp_Object mini_window;
9628 Lisp_Object frame;
9629 struct frame *f;
9631 /* Get the frame containing the mini-buffer
9632 that the selected frame is using. */
9633 mini_window = FRAME_MINIBUF_WINDOW (sf);
9634 frame = XWINDOW (mini_window)->frame;
9635 f = XFRAME (frame);
9637 FRAME_SAMPLE_VISIBILITY (f);
9638 if (FRAME_VISIBLE_P (sf)
9639 && !FRAME_VISIBLE_P (f))
9640 Fmake_frame_visible (frame);
9642 if (STRINGP (m) && SCHARS (m) > 0)
9644 set_message (NULL, m, nbytes, multibyte);
9645 if (minibuffer_auto_raise)
9646 Fraise_frame (frame);
9647 /* Assume we are not echoing.
9648 (If we are, echo_now will override this.) */
9649 echo_message_buffer = Qnil;
9651 else
9652 clear_message (1, 1);
9654 do_pending_window_change (0);
9655 echo_area_display (1);
9656 do_pending_window_change (0);
9657 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9658 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9663 /* Display a null-terminated echo area message M. If M is 0, clear
9664 out any existing message, and let the mini-buffer text show through.
9666 The buffer M must continue to exist until after the echo area gets
9667 cleared or some other message gets displayed there. Do not pass
9668 text that is stored in a Lisp string. Do not pass text in a buffer
9669 that was alloca'd. */
9671 void
9672 message1 (const char *m)
9674 message2 (m, (m ? strlen (m) : 0), 0);
9678 /* The non-logging counterpart of message1. */
9680 void
9681 message1_nolog (const char *m)
9683 message2_nolog (m, (m ? strlen (m) : 0), 0);
9686 /* Display a message M which contains a single %s
9687 which gets replaced with STRING. */
9689 void
9690 message_with_string (const char *m, Lisp_Object string, int log)
9692 CHECK_STRING (string);
9694 if (noninteractive)
9696 if (m)
9698 if (noninteractive_need_newline)
9699 putc ('\n', stderr);
9700 noninteractive_need_newline = 0;
9701 fprintf (stderr, m, SDATA (string));
9702 if (!cursor_in_echo_area)
9703 fprintf (stderr, "\n");
9704 fflush (stderr);
9707 else if (INTERACTIVE)
9709 /* The frame whose minibuffer we're going to display the message on.
9710 It may be larger than the selected frame, so we need
9711 to use its buffer, not the selected frame's buffer. */
9712 Lisp_Object mini_window;
9713 struct frame *f, *sf = SELECTED_FRAME ();
9715 /* Get the frame containing the minibuffer
9716 that the selected frame is using. */
9717 mini_window = FRAME_MINIBUF_WINDOW (sf);
9718 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9720 /* A null message buffer means that the frame hasn't really been
9721 initialized yet. Error messages get reported properly by
9722 cmd_error, so this must be just an informative message; toss it. */
9723 if (FRAME_MESSAGE_BUF (f))
9725 Lisp_Object args[2], msg;
9726 struct gcpro gcpro1, gcpro2;
9728 args[0] = build_string (m);
9729 args[1] = msg = string;
9730 GCPRO2 (args[0], msg);
9731 gcpro1.nvars = 2;
9733 msg = Fformat (2, args);
9735 if (log)
9736 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9737 else
9738 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9740 UNGCPRO;
9742 /* Print should start at the beginning of the message
9743 buffer next time. */
9744 message_buf_print = 0;
9750 /* Dump an informative message to the minibuf. If M is 0, clear out
9751 any existing message, and let the mini-buffer text show through. */
9753 static void
9754 vmessage (const char *m, va_list ap)
9756 if (noninteractive)
9758 if (m)
9760 if (noninteractive_need_newline)
9761 putc ('\n', stderr);
9762 noninteractive_need_newline = 0;
9763 vfprintf (stderr, m, ap);
9764 if (cursor_in_echo_area == 0)
9765 fprintf (stderr, "\n");
9766 fflush (stderr);
9769 else if (INTERACTIVE)
9771 /* The frame whose mini-buffer we're going to display the message
9772 on. It may be larger than the selected frame, so we need to
9773 use its buffer, not the selected frame's buffer. */
9774 Lisp_Object mini_window;
9775 struct frame *f, *sf = SELECTED_FRAME ();
9777 /* Get the frame containing the mini-buffer
9778 that the selected frame is using. */
9779 mini_window = FRAME_MINIBUF_WINDOW (sf);
9780 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9782 /* A null message buffer means that the frame hasn't really been
9783 initialized yet. Error messages get reported properly by
9784 cmd_error, so this must be just an informative message; toss
9785 it. */
9786 if (FRAME_MESSAGE_BUF (f))
9788 if (m)
9790 ptrdiff_t len;
9792 len = doprnt (FRAME_MESSAGE_BUF (f),
9793 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9795 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9797 else
9798 message1 (0);
9800 /* Print should start at the beginning of the message
9801 buffer next time. */
9802 message_buf_print = 0;
9807 void
9808 message (const char *m, ...)
9810 va_list ap;
9811 va_start (ap, m);
9812 vmessage (m, ap);
9813 va_end (ap);
9817 #if 0
9818 /* The non-logging version of message. */
9820 void
9821 message_nolog (const char *m, ...)
9823 Lisp_Object old_log_max;
9824 va_list ap;
9825 va_start (ap, m);
9826 old_log_max = Vmessage_log_max;
9827 Vmessage_log_max = Qnil;
9828 vmessage (m, ap);
9829 Vmessage_log_max = old_log_max;
9830 va_end (ap);
9832 #endif
9835 /* Display the current message in the current mini-buffer. This is
9836 only called from error handlers in process.c, and is not time
9837 critical. */
9839 void
9840 update_echo_area (void)
9842 if (!NILP (echo_area_buffer[0]))
9844 Lisp_Object string;
9845 string = Fcurrent_message ();
9846 message3 (string, SBYTES (string),
9847 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9852 /* Make sure echo area buffers in `echo_buffers' are live.
9853 If they aren't, make new ones. */
9855 static void
9856 ensure_echo_area_buffers (void)
9858 int i;
9860 for (i = 0; i < 2; ++i)
9861 if (!BUFFERP (echo_buffer[i])
9862 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9864 char name[30];
9865 Lisp_Object old_buffer;
9866 int j;
9868 old_buffer = echo_buffer[i];
9869 echo_buffer[i] = Fget_buffer_create
9870 (make_formatted_string (name, " *Echo Area %d*", i));
9871 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9872 /* to force word wrap in echo area -
9873 it was decided to postpone this*/
9874 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9876 for (j = 0; j < 2; ++j)
9877 if (EQ (old_buffer, echo_area_buffer[j]))
9878 echo_area_buffer[j] = echo_buffer[i];
9883 /* Call FN with args A1..A4 with either the current or last displayed
9884 echo_area_buffer as current buffer.
9886 WHICH zero means use the current message buffer
9887 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9888 from echo_buffer[] and clear it.
9890 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9891 suitable buffer from echo_buffer[] and clear it.
9893 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9894 that the current message becomes the last displayed one, make
9895 choose a suitable buffer for echo_area_buffer[0], and clear it.
9897 Value is what FN returns. */
9899 static int
9900 with_echo_area_buffer (struct window *w, int which,
9901 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9902 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9904 Lisp_Object buffer;
9905 int this_one, the_other, clear_buffer_p, rc;
9906 ptrdiff_t count = SPECPDL_INDEX ();
9908 /* If buffers aren't live, make new ones. */
9909 ensure_echo_area_buffers ();
9911 clear_buffer_p = 0;
9913 if (which == 0)
9914 this_one = 0, the_other = 1;
9915 else if (which > 0)
9916 this_one = 1, the_other = 0;
9917 else
9919 this_one = 0, the_other = 1;
9920 clear_buffer_p = 1;
9922 /* We need a fresh one in case the current echo buffer equals
9923 the one containing the last displayed echo area message. */
9924 if (!NILP (echo_area_buffer[this_one])
9925 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9926 echo_area_buffer[this_one] = Qnil;
9929 /* Choose a suitable buffer from echo_buffer[] is we don't
9930 have one. */
9931 if (NILP (echo_area_buffer[this_one]))
9933 echo_area_buffer[this_one]
9934 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9935 ? echo_buffer[the_other]
9936 : echo_buffer[this_one]);
9937 clear_buffer_p = 1;
9940 buffer = echo_area_buffer[this_one];
9942 /* Don't get confused by reusing the buffer used for echoing
9943 for a different purpose. */
9944 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9945 cancel_echoing ();
9947 record_unwind_protect (unwind_with_echo_area_buffer,
9948 with_echo_area_buffer_unwind_data (w));
9950 /* Make the echo area buffer current. Note that for display
9951 purposes, it is not necessary that the displayed window's buffer
9952 == current_buffer, except for text property lookup. So, let's
9953 only set that buffer temporarily here without doing a full
9954 Fset_window_buffer. We must also change w->pointm, though,
9955 because otherwise an assertions in unshow_buffer fails, and Emacs
9956 aborts. */
9957 set_buffer_internal_1 (XBUFFER (buffer));
9958 if (w)
9960 w->buffer = buffer;
9961 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9964 BVAR (current_buffer, undo_list) = Qt;
9965 BVAR (current_buffer, read_only) = Qnil;
9966 specbind (Qinhibit_read_only, Qt);
9967 specbind (Qinhibit_modification_hooks, Qt);
9969 if (clear_buffer_p && Z > BEG)
9970 del_range (BEG, Z);
9972 eassert (BEGV >= BEG);
9973 eassert (ZV <= Z && ZV >= BEGV);
9975 rc = fn (a1, a2, a3, a4);
9977 eassert (BEGV >= BEG);
9978 eassert (ZV <= Z && ZV >= BEGV);
9980 unbind_to (count, Qnil);
9981 return rc;
9985 /* Save state that should be preserved around the call to the function
9986 FN called in with_echo_area_buffer. */
9988 static Lisp_Object
9989 with_echo_area_buffer_unwind_data (struct window *w)
9991 int i = 0;
9992 Lisp_Object vector, tmp;
9994 /* Reduce consing by keeping one vector in
9995 Vwith_echo_area_save_vector. */
9996 vector = Vwith_echo_area_save_vector;
9997 Vwith_echo_area_save_vector = Qnil;
9999 if (NILP (vector))
10000 vector = Fmake_vector (make_number (7), Qnil);
10002 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10003 ASET (vector, i, Vdeactivate_mark); ++i;
10004 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10006 if (w)
10008 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10009 ASET (vector, i, w->buffer); ++i;
10010 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
10011 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
10013 else
10015 int end = i + 4;
10016 for (; i < end; ++i)
10017 ASET (vector, i, Qnil);
10020 eassert (i == ASIZE (vector));
10021 return vector;
10025 /* Restore global state from VECTOR which was created by
10026 with_echo_area_buffer_unwind_data. */
10028 static Lisp_Object
10029 unwind_with_echo_area_buffer (Lisp_Object vector)
10031 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10032 Vdeactivate_mark = AREF (vector, 1);
10033 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10035 if (WINDOWP (AREF (vector, 3)))
10037 struct window *w;
10038 Lisp_Object buffer, charpos, bytepos;
10040 w = XWINDOW (AREF (vector, 3));
10041 buffer = AREF (vector, 4);
10042 charpos = AREF (vector, 5);
10043 bytepos = AREF (vector, 6);
10045 w->buffer = buffer;
10046 set_marker_both (w->pointm, buffer,
10047 XFASTINT (charpos), XFASTINT (bytepos));
10050 Vwith_echo_area_save_vector = vector;
10051 return Qnil;
10055 /* Set up the echo area for use by print functions. MULTIBYTE_P
10056 non-zero means we will print multibyte. */
10058 void
10059 setup_echo_area_for_printing (int multibyte_p)
10061 /* If we can't find an echo area any more, exit. */
10062 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10063 Fkill_emacs (Qnil);
10065 ensure_echo_area_buffers ();
10067 if (!message_buf_print)
10069 /* A message has been output since the last time we printed.
10070 Choose a fresh echo area buffer. */
10071 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10072 echo_area_buffer[0] = echo_buffer[1];
10073 else
10074 echo_area_buffer[0] = echo_buffer[0];
10076 /* Switch to that buffer and clear it. */
10077 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10078 BVAR (current_buffer, truncate_lines) = Qnil;
10080 if (Z > BEG)
10082 ptrdiff_t count = SPECPDL_INDEX ();
10083 specbind (Qinhibit_read_only, Qt);
10084 /* Note that undo recording is always disabled. */
10085 del_range (BEG, Z);
10086 unbind_to (count, Qnil);
10088 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10090 /* Set up the buffer for the multibyteness we need. */
10091 if (multibyte_p
10092 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10093 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10095 /* Raise the frame containing the echo area. */
10096 if (minibuffer_auto_raise)
10098 struct frame *sf = SELECTED_FRAME ();
10099 Lisp_Object mini_window;
10100 mini_window = FRAME_MINIBUF_WINDOW (sf);
10101 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10104 message_log_maybe_newline ();
10105 message_buf_print = 1;
10107 else
10109 if (NILP (echo_area_buffer[0]))
10111 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10112 echo_area_buffer[0] = echo_buffer[1];
10113 else
10114 echo_area_buffer[0] = echo_buffer[0];
10117 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10119 /* Someone switched buffers between print requests. */
10120 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10121 BVAR (current_buffer, truncate_lines) = Qnil;
10127 /* Display an echo area message in window W. Value is non-zero if W's
10128 height is changed. If display_last_displayed_message_p is
10129 non-zero, display the message that was last displayed, otherwise
10130 display the current message. */
10132 static int
10133 display_echo_area (struct window *w)
10135 int i, no_message_p, window_height_changed_p;
10137 /* Temporarily disable garbage collections while displaying the echo
10138 area. This is done because a GC can print a message itself.
10139 That message would modify the echo area buffer's contents while a
10140 redisplay of the buffer is going on, and seriously confuse
10141 redisplay. */
10142 ptrdiff_t count = inhibit_garbage_collection ();
10144 /* If there is no message, we must call display_echo_area_1
10145 nevertheless because it resizes the window. But we will have to
10146 reset the echo_area_buffer in question to nil at the end because
10147 with_echo_area_buffer will sets it to an empty buffer. */
10148 i = display_last_displayed_message_p ? 1 : 0;
10149 no_message_p = NILP (echo_area_buffer[i]);
10151 window_height_changed_p
10152 = with_echo_area_buffer (w, display_last_displayed_message_p,
10153 display_echo_area_1,
10154 (intptr_t) w, Qnil, 0, 0);
10156 if (no_message_p)
10157 echo_area_buffer[i] = Qnil;
10159 unbind_to (count, Qnil);
10160 return window_height_changed_p;
10164 /* Helper for display_echo_area. Display the current buffer which
10165 contains the current echo area message in window W, a mini-window,
10166 a pointer to which is passed in A1. A2..A4 are currently not used.
10167 Change the height of W so that all of the message is displayed.
10168 Value is non-zero if height of W was changed. */
10170 static int
10171 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10173 intptr_t i1 = a1;
10174 struct window *w = (struct window *) i1;
10175 Lisp_Object window;
10176 struct text_pos start;
10177 int window_height_changed_p = 0;
10179 /* Do this before displaying, so that we have a large enough glyph
10180 matrix for the display. If we can't get enough space for the
10181 whole text, display the last N lines. That works by setting w->start. */
10182 window_height_changed_p = resize_mini_window (w, 0);
10184 /* Use the starting position chosen by resize_mini_window. */
10185 SET_TEXT_POS_FROM_MARKER (start, w->start);
10187 /* Display. */
10188 clear_glyph_matrix (w->desired_matrix);
10189 XSETWINDOW (window, w);
10190 try_window (window, start, 0);
10192 return window_height_changed_p;
10196 /* Resize the echo area window to exactly the size needed for the
10197 currently displayed message, if there is one. If a mini-buffer
10198 is active, don't shrink it. */
10200 void
10201 resize_echo_area_exactly (void)
10203 if (BUFFERP (echo_area_buffer[0])
10204 && WINDOWP (echo_area_window))
10206 struct window *w = XWINDOW (echo_area_window);
10207 int resized_p;
10208 Lisp_Object resize_exactly;
10210 if (minibuf_level == 0)
10211 resize_exactly = Qt;
10212 else
10213 resize_exactly = Qnil;
10215 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10216 (intptr_t) w, resize_exactly,
10217 0, 0);
10218 if (resized_p)
10220 ++windows_or_buffers_changed;
10221 ++update_mode_lines;
10222 redisplay_internal ();
10228 /* Callback function for with_echo_area_buffer, when used from
10229 resize_echo_area_exactly. A1 contains a pointer to the window to
10230 resize, EXACTLY non-nil means resize the mini-window exactly to the
10231 size of the text displayed. A3 and A4 are not used. Value is what
10232 resize_mini_window returns. */
10234 static int
10235 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10237 intptr_t i1 = a1;
10238 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10242 /* Resize mini-window W to fit the size of its contents. EXACT_P
10243 means size the window exactly to the size needed. Otherwise, it's
10244 only enlarged until W's buffer is empty.
10246 Set W->start to the right place to begin display. If the whole
10247 contents fit, start at the beginning. Otherwise, start so as
10248 to make the end of the contents appear. This is particularly
10249 important for y-or-n-p, but seems desirable generally.
10251 Value is non-zero if the window height has been changed. */
10254 resize_mini_window (struct window *w, int exact_p)
10256 struct frame *f = XFRAME (w->frame);
10257 int window_height_changed_p = 0;
10259 eassert (MINI_WINDOW_P (w));
10261 /* By default, start display at the beginning. */
10262 set_marker_both (w->start, w->buffer,
10263 BUF_BEGV (XBUFFER (w->buffer)),
10264 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10266 /* Don't resize windows while redisplaying a window; it would
10267 confuse redisplay functions when the size of the window they are
10268 displaying changes from under them. Such a resizing can happen,
10269 for instance, when which-func prints a long message while
10270 we are running fontification-functions. We're running these
10271 functions with safe_call which binds inhibit-redisplay to t. */
10272 if (!NILP (Vinhibit_redisplay))
10273 return 0;
10275 /* Nil means don't try to resize. */
10276 if (NILP (Vresize_mini_windows)
10277 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10278 return 0;
10280 if (!FRAME_MINIBUF_ONLY_P (f))
10282 struct it it;
10283 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10284 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10285 int height;
10286 EMACS_INT max_height;
10287 int unit = FRAME_LINE_HEIGHT (f);
10288 struct text_pos start;
10289 struct buffer *old_current_buffer = NULL;
10291 if (current_buffer != XBUFFER (w->buffer))
10293 old_current_buffer = current_buffer;
10294 set_buffer_internal (XBUFFER (w->buffer));
10297 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10299 /* Compute the max. number of lines specified by the user. */
10300 if (FLOATP (Vmax_mini_window_height))
10301 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10302 else if (INTEGERP (Vmax_mini_window_height))
10303 max_height = XINT (Vmax_mini_window_height);
10304 else
10305 max_height = total_height / 4;
10307 /* Correct that max. height if it's bogus. */
10308 max_height = max (1, max_height);
10309 max_height = min (total_height, max_height);
10311 /* Find out the height of the text in the window. */
10312 if (it.line_wrap == TRUNCATE)
10313 height = 1;
10314 else
10316 last_height = 0;
10317 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10318 if (it.max_ascent == 0 && it.max_descent == 0)
10319 height = it.current_y + last_height;
10320 else
10321 height = it.current_y + it.max_ascent + it.max_descent;
10322 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10323 height = (height + unit - 1) / unit;
10326 /* Compute a suitable window start. */
10327 if (height > max_height)
10329 height = max_height;
10330 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10331 move_it_vertically_backward (&it, (height - 1) * unit);
10332 start = it.current.pos;
10334 else
10335 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10336 SET_MARKER_FROM_TEXT_POS (w->start, start);
10338 if (EQ (Vresize_mini_windows, Qgrow_only))
10340 /* Let it grow only, until we display an empty message, in which
10341 case the window shrinks again. */
10342 if (height > WINDOW_TOTAL_LINES (w))
10344 int old_height = WINDOW_TOTAL_LINES (w);
10345 freeze_window_starts (f, 1);
10346 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10347 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10349 else if (height < WINDOW_TOTAL_LINES (w)
10350 && (exact_p || BEGV == ZV))
10352 int old_height = WINDOW_TOTAL_LINES (w);
10353 freeze_window_starts (f, 0);
10354 shrink_mini_window (w);
10355 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10358 else
10360 /* Always resize to exact size needed. */
10361 if (height > WINDOW_TOTAL_LINES (w))
10363 int old_height = WINDOW_TOTAL_LINES (w);
10364 freeze_window_starts (f, 1);
10365 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10366 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10368 else if (height < WINDOW_TOTAL_LINES (w))
10370 int old_height = WINDOW_TOTAL_LINES (w);
10371 freeze_window_starts (f, 0);
10372 shrink_mini_window (w);
10374 if (height)
10376 freeze_window_starts (f, 1);
10377 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10380 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10384 if (old_current_buffer)
10385 set_buffer_internal (old_current_buffer);
10388 return window_height_changed_p;
10392 /* Value is the current message, a string, or nil if there is no
10393 current message. */
10395 Lisp_Object
10396 current_message (void)
10398 Lisp_Object msg;
10400 if (!BUFFERP (echo_area_buffer[0]))
10401 msg = Qnil;
10402 else
10404 with_echo_area_buffer (0, 0, current_message_1,
10405 (intptr_t) &msg, Qnil, 0, 0);
10406 if (NILP (msg))
10407 echo_area_buffer[0] = Qnil;
10410 return msg;
10414 static int
10415 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10417 intptr_t i1 = a1;
10418 Lisp_Object *msg = (Lisp_Object *) i1;
10420 if (Z > BEG)
10421 *msg = make_buffer_string (BEG, Z, 1);
10422 else
10423 *msg = Qnil;
10424 return 0;
10428 /* Push the current message on Vmessage_stack for later restoration
10429 by restore_message. Value is non-zero if the current message isn't
10430 empty. This is a relatively infrequent operation, so it's not
10431 worth optimizing. */
10434 push_message (void)
10436 Lisp_Object msg;
10437 msg = current_message ();
10438 Vmessage_stack = Fcons (msg, Vmessage_stack);
10439 return STRINGP (msg);
10443 /* Restore message display from the top of Vmessage_stack. */
10445 void
10446 restore_message (void)
10448 Lisp_Object msg;
10450 eassert (CONSP (Vmessage_stack));
10451 msg = XCAR (Vmessage_stack);
10452 if (STRINGP (msg))
10453 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10454 else
10455 message3_nolog (msg, 0, 0);
10459 /* Handler for record_unwind_protect calling pop_message. */
10461 Lisp_Object
10462 pop_message_unwind (Lisp_Object dummy)
10464 pop_message ();
10465 return Qnil;
10468 /* Pop the top-most entry off Vmessage_stack. */
10470 static void
10471 pop_message (void)
10473 eassert (CONSP (Vmessage_stack));
10474 Vmessage_stack = XCDR (Vmessage_stack);
10478 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10479 exits. If the stack is not empty, we have a missing pop_message
10480 somewhere. */
10482 void
10483 check_message_stack (void)
10485 if (!NILP (Vmessage_stack))
10486 abort ();
10490 /* Truncate to NCHARS what will be displayed in the echo area the next
10491 time we display it---but don't redisplay it now. */
10493 void
10494 truncate_echo_area (ptrdiff_t nchars)
10496 if (nchars == 0)
10497 echo_area_buffer[0] = Qnil;
10498 /* A null message buffer means that the frame hasn't really been
10499 initialized yet. Error messages get reported properly by
10500 cmd_error, so this must be just an informative message; toss it. */
10501 else if (!noninteractive
10502 && INTERACTIVE
10503 && !NILP (echo_area_buffer[0]))
10505 struct frame *sf = SELECTED_FRAME ();
10506 if (FRAME_MESSAGE_BUF (sf))
10507 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10512 /* Helper function for truncate_echo_area. Truncate the current
10513 message to at most NCHARS characters. */
10515 static int
10516 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10518 if (BEG + nchars < Z)
10519 del_range (BEG + nchars, Z);
10520 if (Z == BEG)
10521 echo_area_buffer[0] = Qnil;
10522 return 0;
10526 /* Set the current message to a substring of S or STRING.
10528 If STRING is a Lisp string, set the message to the first NBYTES
10529 bytes from STRING. NBYTES zero means use the whole string. If
10530 STRING is multibyte, the message will be displayed multibyte.
10532 If S is not null, set the message to the first LEN bytes of S. LEN
10533 zero means use the whole string. MULTIBYTE_P non-zero means S is
10534 multibyte. Display the message multibyte in that case.
10536 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10537 to t before calling set_message_1 (which calls insert).
10540 static void
10541 set_message (const char *s, Lisp_Object string,
10542 ptrdiff_t nbytes, int multibyte_p)
10544 message_enable_multibyte
10545 = ((s && multibyte_p)
10546 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10548 with_echo_area_buffer (0, -1, set_message_1,
10549 (intptr_t) s, string, nbytes, multibyte_p);
10550 message_buf_print = 0;
10551 help_echo_showing_p = 0;
10555 /* Helper function for set_message. Arguments have the same meaning
10556 as there, with A1 corresponding to S and A2 corresponding to STRING
10557 This function is called with the echo area buffer being
10558 current. */
10560 static int
10561 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10563 intptr_t i1 = a1;
10564 const char *s = (const char *) i1;
10565 const unsigned char *msg = (const unsigned char *) s;
10566 Lisp_Object string = a2;
10568 /* Change multibyteness of the echo buffer appropriately. */
10569 if (message_enable_multibyte
10570 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10571 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10573 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10574 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10575 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10577 /* Insert new message at BEG. */
10578 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10580 if (STRINGP (string))
10582 ptrdiff_t nchars;
10584 if (nbytes == 0)
10585 nbytes = SBYTES (string);
10586 nchars = string_byte_to_char (string, nbytes);
10588 /* This function takes care of single/multibyte conversion. We
10589 just have to ensure that the echo area buffer has the right
10590 setting of enable_multibyte_characters. */
10591 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10593 else if (s)
10595 if (nbytes == 0)
10596 nbytes = strlen (s);
10598 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10600 /* Convert from multi-byte to single-byte. */
10601 ptrdiff_t i;
10602 int c, n;
10603 char work[1];
10605 /* Convert a multibyte string to single-byte. */
10606 for (i = 0; i < nbytes; i += n)
10608 c = string_char_and_length (msg + i, &n);
10609 work[0] = (ASCII_CHAR_P (c)
10611 : multibyte_char_to_unibyte (c));
10612 insert_1_both (work, 1, 1, 1, 0, 0);
10615 else if (!multibyte_p
10616 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10618 /* Convert from single-byte to multi-byte. */
10619 ptrdiff_t i;
10620 int c, n;
10621 unsigned char str[MAX_MULTIBYTE_LENGTH];
10623 /* Convert a single-byte string to multibyte. */
10624 for (i = 0; i < nbytes; i++)
10626 c = msg[i];
10627 MAKE_CHAR_MULTIBYTE (c);
10628 n = CHAR_STRING (c, str);
10629 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10632 else
10633 insert_1 (s, nbytes, 1, 0, 0);
10636 return 0;
10640 /* Clear messages. CURRENT_P non-zero means clear the current
10641 message. LAST_DISPLAYED_P non-zero means clear the message
10642 last displayed. */
10644 void
10645 clear_message (int current_p, int last_displayed_p)
10647 if (current_p)
10649 echo_area_buffer[0] = Qnil;
10650 message_cleared_p = 1;
10653 if (last_displayed_p)
10654 echo_area_buffer[1] = Qnil;
10656 message_buf_print = 0;
10659 /* Clear garbaged frames.
10661 This function is used where the old redisplay called
10662 redraw_garbaged_frames which in turn called redraw_frame which in
10663 turn called clear_frame. The call to clear_frame was a source of
10664 flickering. I believe a clear_frame is not necessary. It should
10665 suffice in the new redisplay to invalidate all current matrices,
10666 and ensure a complete redisplay of all windows. */
10668 static void
10669 clear_garbaged_frames (void)
10671 if (frame_garbaged)
10673 Lisp_Object tail, frame;
10674 int changed_count = 0;
10676 FOR_EACH_FRAME (tail, frame)
10678 struct frame *f = XFRAME (frame);
10680 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10682 if (f->resized_p)
10684 Fredraw_frame (frame);
10685 f->force_flush_display_p = 1;
10687 clear_current_matrices (f);
10688 changed_count++;
10689 f->garbaged = 0;
10690 f->resized_p = 0;
10694 frame_garbaged = 0;
10695 if (changed_count)
10696 ++windows_or_buffers_changed;
10701 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10702 is non-zero update selected_frame. Value is non-zero if the
10703 mini-windows height has been changed. */
10705 static int
10706 echo_area_display (int update_frame_p)
10708 Lisp_Object mini_window;
10709 struct window *w;
10710 struct frame *f;
10711 int window_height_changed_p = 0;
10712 struct frame *sf = SELECTED_FRAME ();
10714 mini_window = FRAME_MINIBUF_WINDOW (sf);
10715 w = XWINDOW (mini_window);
10716 f = XFRAME (WINDOW_FRAME (w));
10718 /* Don't display if frame is invisible or not yet initialized. */
10719 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10720 return 0;
10722 #ifdef HAVE_WINDOW_SYSTEM
10723 /* When Emacs starts, selected_frame may be the initial terminal
10724 frame. If we let this through, a message would be displayed on
10725 the terminal. */
10726 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10727 return 0;
10728 #endif /* HAVE_WINDOW_SYSTEM */
10730 /* Redraw garbaged frames. */
10731 if (frame_garbaged)
10732 clear_garbaged_frames ();
10734 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10736 echo_area_window = mini_window;
10737 window_height_changed_p = display_echo_area (w);
10738 w->must_be_updated_p = 1;
10740 /* Update the display, unless called from redisplay_internal.
10741 Also don't update the screen during redisplay itself. The
10742 update will happen at the end of redisplay, and an update
10743 here could cause confusion. */
10744 if (update_frame_p && !redisplaying_p)
10746 int n = 0;
10748 /* If the display update has been interrupted by pending
10749 input, update mode lines in the frame. Due to the
10750 pending input, it might have been that redisplay hasn't
10751 been called, so that mode lines above the echo area are
10752 garbaged. This looks odd, so we prevent it here. */
10753 if (!display_completed)
10754 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10756 if (window_height_changed_p
10757 /* Don't do this if Emacs is shutting down. Redisplay
10758 needs to run hooks. */
10759 && !NILP (Vrun_hooks))
10761 /* Must update other windows. Likewise as in other
10762 cases, don't let this update be interrupted by
10763 pending input. */
10764 ptrdiff_t count = SPECPDL_INDEX ();
10765 specbind (Qredisplay_dont_pause, Qt);
10766 windows_or_buffers_changed = 1;
10767 redisplay_internal ();
10768 unbind_to (count, Qnil);
10770 else if (FRAME_WINDOW_P (f) && n == 0)
10772 /* Window configuration is the same as before.
10773 Can do with a display update of the echo area,
10774 unless we displayed some mode lines. */
10775 update_single_window (w, 1);
10776 FRAME_RIF (f)->flush_display (f);
10778 else
10779 update_frame (f, 1, 1);
10781 /* If cursor is in the echo area, make sure that the next
10782 redisplay displays the minibuffer, so that the cursor will
10783 be replaced with what the minibuffer wants. */
10784 if (cursor_in_echo_area)
10785 ++windows_or_buffers_changed;
10788 else if (!EQ (mini_window, selected_window))
10789 windows_or_buffers_changed++;
10791 /* Last displayed message is now the current message. */
10792 echo_area_buffer[1] = echo_area_buffer[0];
10793 /* Inform read_char that we're not echoing. */
10794 echo_message_buffer = Qnil;
10796 /* Prevent redisplay optimization in redisplay_internal by resetting
10797 this_line_start_pos. This is done because the mini-buffer now
10798 displays the message instead of its buffer text. */
10799 if (EQ (mini_window, selected_window))
10800 CHARPOS (this_line_start_pos) = 0;
10802 return window_height_changed_p;
10807 /***********************************************************************
10808 Mode Lines and Frame Titles
10809 ***********************************************************************/
10811 /* A buffer for constructing non-propertized mode-line strings and
10812 frame titles in it; allocated from the heap in init_xdisp and
10813 resized as needed in store_mode_line_noprop_char. */
10815 static char *mode_line_noprop_buf;
10817 /* The buffer's end, and a current output position in it. */
10819 static char *mode_line_noprop_buf_end;
10820 static char *mode_line_noprop_ptr;
10822 #define MODE_LINE_NOPROP_LEN(start) \
10823 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10825 static enum {
10826 MODE_LINE_DISPLAY = 0,
10827 MODE_LINE_TITLE,
10828 MODE_LINE_NOPROP,
10829 MODE_LINE_STRING
10830 } mode_line_target;
10832 /* Alist that caches the results of :propertize.
10833 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10834 static Lisp_Object mode_line_proptrans_alist;
10836 /* List of strings making up the mode-line. */
10837 static Lisp_Object mode_line_string_list;
10839 /* Base face property when building propertized mode line string. */
10840 static Lisp_Object mode_line_string_face;
10841 static Lisp_Object mode_line_string_face_prop;
10844 /* Unwind data for mode line strings */
10846 static Lisp_Object Vmode_line_unwind_vector;
10848 static Lisp_Object
10849 format_mode_line_unwind_data (struct frame *target_frame,
10850 struct buffer *obuf,
10851 Lisp_Object owin,
10852 int save_proptrans)
10854 Lisp_Object vector, tmp;
10856 /* Reduce consing by keeping one vector in
10857 Vwith_echo_area_save_vector. */
10858 vector = Vmode_line_unwind_vector;
10859 Vmode_line_unwind_vector = Qnil;
10861 if (NILP (vector))
10862 vector = Fmake_vector (make_number (10), Qnil);
10864 ASET (vector, 0, make_number (mode_line_target));
10865 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10866 ASET (vector, 2, mode_line_string_list);
10867 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10868 ASET (vector, 4, mode_line_string_face);
10869 ASET (vector, 5, mode_line_string_face_prop);
10871 if (obuf)
10872 XSETBUFFER (tmp, obuf);
10873 else
10874 tmp = Qnil;
10875 ASET (vector, 6, tmp);
10876 ASET (vector, 7, owin);
10877 if (target_frame)
10879 /* Similarly to `with-selected-window', if the operation selects
10880 a window on another frame, we must restore that frame's
10881 selected window, and (for a tty) the top-frame. */
10882 ASET (vector, 8, target_frame->selected_window);
10883 if (FRAME_TERMCAP_P (target_frame))
10884 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10887 return vector;
10890 static Lisp_Object
10891 unwind_format_mode_line (Lisp_Object vector)
10893 Lisp_Object old_window = AREF (vector, 7);
10894 Lisp_Object target_frame_window = AREF (vector, 8);
10895 Lisp_Object old_top_frame = AREF (vector, 9);
10897 mode_line_target = XINT (AREF (vector, 0));
10898 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10899 mode_line_string_list = AREF (vector, 2);
10900 if (! EQ (AREF (vector, 3), Qt))
10901 mode_line_proptrans_alist = AREF (vector, 3);
10902 mode_line_string_face = AREF (vector, 4);
10903 mode_line_string_face_prop = AREF (vector, 5);
10905 /* Select window before buffer, since it may change the buffer. */
10906 if (!NILP (old_window))
10908 /* If the operation that we are unwinding had selected a window
10909 on a different frame, reset its frame-selected-window. For a
10910 text terminal, reset its top-frame if necessary. */
10911 if (!NILP (target_frame_window))
10913 Lisp_Object frame
10914 = WINDOW_FRAME (XWINDOW (target_frame_window));
10916 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10917 Fselect_window (target_frame_window, Qt);
10919 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10920 Fselect_frame (old_top_frame, Qt);
10923 Fselect_window (old_window, Qt);
10926 if (!NILP (AREF (vector, 6)))
10928 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10929 ASET (vector, 6, Qnil);
10932 Vmode_line_unwind_vector = vector;
10933 return Qnil;
10937 /* Store a single character C for the frame title in mode_line_noprop_buf.
10938 Re-allocate mode_line_noprop_buf if necessary. */
10940 static void
10941 store_mode_line_noprop_char (char c)
10943 /* If output position has reached the end of the allocated buffer,
10944 increase the buffer's size. */
10945 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10947 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10948 ptrdiff_t size = len;
10949 mode_line_noprop_buf =
10950 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10951 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10952 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10955 *mode_line_noprop_ptr++ = c;
10959 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10960 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10961 characters that yield more columns than PRECISION; PRECISION <= 0
10962 means copy the whole string. Pad with spaces until FIELD_WIDTH
10963 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10964 pad. Called from display_mode_element when it is used to build a
10965 frame title. */
10967 static int
10968 store_mode_line_noprop (const char *string, int field_width, int precision)
10970 const unsigned char *str = (const unsigned char *) string;
10971 int n = 0;
10972 ptrdiff_t dummy, nbytes;
10974 /* Copy at most PRECISION chars from STR. */
10975 nbytes = strlen (string);
10976 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10977 while (nbytes--)
10978 store_mode_line_noprop_char (*str++);
10980 /* Fill up with spaces until FIELD_WIDTH reached. */
10981 while (field_width > 0
10982 && n < field_width)
10984 store_mode_line_noprop_char (' ');
10985 ++n;
10988 return n;
10991 /***********************************************************************
10992 Frame Titles
10993 ***********************************************************************/
10995 #ifdef HAVE_WINDOW_SYSTEM
10997 /* Set the title of FRAME, if it has changed. The title format is
10998 Vicon_title_format if FRAME is iconified, otherwise it is
10999 frame_title_format. */
11001 static void
11002 x_consider_frame_title (Lisp_Object frame)
11004 struct frame *f = XFRAME (frame);
11006 if (FRAME_WINDOW_P (f)
11007 || FRAME_MINIBUF_ONLY_P (f)
11008 || f->explicit_name)
11010 /* Do we have more than one visible frame on this X display? */
11011 Lisp_Object tail;
11012 Lisp_Object fmt;
11013 ptrdiff_t title_start;
11014 char *title;
11015 ptrdiff_t len;
11016 struct it it;
11017 ptrdiff_t count = SPECPDL_INDEX ();
11019 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
11021 Lisp_Object other_frame = XCAR (tail);
11022 struct frame *tf = XFRAME (other_frame);
11024 if (tf != f
11025 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11026 && !FRAME_MINIBUF_ONLY_P (tf)
11027 && !EQ (other_frame, tip_frame)
11028 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11029 break;
11032 /* Set global variable indicating that multiple frames exist. */
11033 multiple_frames = CONSP (tail);
11035 /* Switch to the buffer of selected window of the frame. Set up
11036 mode_line_target so that display_mode_element will output into
11037 mode_line_noprop_buf; then display the title. */
11038 record_unwind_protect (unwind_format_mode_line,
11039 format_mode_line_unwind_data
11040 (f, current_buffer, selected_window, 0));
11042 Fselect_window (f->selected_window, Qt);
11043 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11044 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11046 mode_line_target = MODE_LINE_TITLE;
11047 title_start = MODE_LINE_NOPROP_LEN (0);
11048 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11049 NULL, DEFAULT_FACE_ID);
11050 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11051 len = MODE_LINE_NOPROP_LEN (title_start);
11052 title = mode_line_noprop_buf + title_start;
11053 unbind_to (count, Qnil);
11055 /* Set the title only if it's changed. This avoids consing in
11056 the common case where it hasn't. (If it turns out that we've
11057 already wasted too much time by walking through the list with
11058 display_mode_element, then we might need to optimize at a
11059 higher level than this.) */
11060 if (! STRINGP (f->name)
11061 || SBYTES (f->name) != len
11062 || memcmp (title, SDATA (f->name), len) != 0)
11063 x_implicitly_set_name (f, make_string (title, len), Qnil);
11067 #endif /* not HAVE_WINDOW_SYSTEM */
11070 /***********************************************************************
11071 Menu Bars
11072 ***********************************************************************/
11075 /* Prepare for redisplay by updating menu-bar item lists when
11076 appropriate. This can call eval. */
11078 void
11079 prepare_menu_bars (void)
11081 int all_windows;
11082 struct gcpro gcpro1, gcpro2;
11083 struct frame *f;
11084 Lisp_Object tooltip_frame;
11086 #ifdef HAVE_WINDOW_SYSTEM
11087 tooltip_frame = tip_frame;
11088 #else
11089 tooltip_frame = Qnil;
11090 #endif
11092 /* Update all frame titles based on their buffer names, etc. We do
11093 this before the menu bars so that the buffer-menu will show the
11094 up-to-date frame titles. */
11095 #ifdef HAVE_WINDOW_SYSTEM
11096 if (windows_or_buffers_changed || update_mode_lines)
11098 Lisp_Object tail, frame;
11100 FOR_EACH_FRAME (tail, frame)
11102 f = XFRAME (frame);
11103 if (!EQ (frame, tooltip_frame)
11104 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11105 x_consider_frame_title (frame);
11108 #endif /* HAVE_WINDOW_SYSTEM */
11110 /* Update the menu bar item lists, if appropriate. This has to be
11111 done before any actual redisplay or generation of display lines. */
11112 all_windows = (update_mode_lines
11113 || buffer_shared > 1
11114 || windows_or_buffers_changed);
11115 if (all_windows)
11117 Lisp_Object tail, frame;
11118 ptrdiff_t count = SPECPDL_INDEX ();
11119 /* 1 means that update_menu_bar has run its hooks
11120 so any further calls to update_menu_bar shouldn't do so again. */
11121 int menu_bar_hooks_run = 0;
11123 record_unwind_save_match_data ();
11125 FOR_EACH_FRAME (tail, frame)
11127 f = XFRAME (frame);
11129 /* Ignore tooltip frame. */
11130 if (EQ (frame, tooltip_frame))
11131 continue;
11133 /* If a window on this frame changed size, report that to
11134 the user and clear the size-change flag. */
11135 if (FRAME_WINDOW_SIZES_CHANGED (f))
11137 Lisp_Object functions;
11139 /* Clear flag first in case we get an error below. */
11140 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11141 functions = Vwindow_size_change_functions;
11142 GCPRO2 (tail, functions);
11144 while (CONSP (functions))
11146 if (!EQ (XCAR (functions), Qt))
11147 call1 (XCAR (functions), frame);
11148 functions = XCDR (functions);
11150 UNGCPRO;
11153 GCPRO1 (tail);
11154 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11155 #ifdef HAVE_WINDOW_SYSTEM
11156 update_tool_bar (f, 0);
11157 #endif
11158 #ifdef HAVE_NS
11159 if (windows_or_buffers_changed
11160 && FRAME_NS_P (f))
11161 ns_set_doc_edited (f, Fbuffer_modified_p
11162 (XWINDOW (f->selected_window)->buffer));
11163 #endif
11164 UNGCPRO;
11167 unbind_to (count, Qnil);
11169 else
11171 struct frame *sf = SELECTED_FRAME ();
11172 update_menu_bar (sf, 1, 0);
11173 #ifdef HAVE_WINDOW_SYSTEM
11174 update_tool_bar (sf, 1);
11175 #endif
11180 /* Update the menu bar item list for frame F. This has to be done
11181 before we start to fill in any display lines, because it can call
11182 eval.
11184 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11186 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11187 already ran the menu bar hooks for this redisplay, so there
11188 is no need to run them again. The return value is the
11189 updated value of this flag, to pass to the next call. */
11191 static int
11192 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11194 Lisp_Object window;
11195 register struct window *w;
11197 /* If called recursively during a menu update, do nothing. This can
11198 happen when, for instance, an activate-menubar-hook causes a
11199 redisplay. */
11200 if (inhibit_menubar_update)
11201 return hooks_run;
11203 window = FRAME_SELECTED_WINDOW (f);
11204 w = XWINDOW (window);
11206 if (FRAME_WINDOW_P (f)
11208 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11209 || defined (HAVE_NS) || defined (USE_GTK)
11210 FRAME_EXTERNAL_MENU_BAR (f)
11211 #else
11212 FRAME_MENU_BAR_LINES (f) > 0
11213 #endif
11214 : FRAME_MENU_BAR_LINES (f) > 0)
11216 /* If the user has switched buffers or windows, we need to
11217 recompute to reflect the new bindings. But we'll
11218 recompute when update_mode_lines is set too; that means
11219 that people can use force-mode-line-update to request
11220 that the menu bar be recomputed. The adverse effect on
11221 the rest of the redisplay algorithm is about the same as
11222 windows_or_buffers_changed anyway. */
11223 if (windows_or_buffers_changed
11224 /* This used to test w->update_mode_line, but we believe
11225 there is no need to recompute the menu in that case. */
11226 || update_mode_lines
11227 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11228 < BUF_MODIFF (XBUFFER (w->buffer)))
11229 != w->last_had_star)
11230 || ((!NILP (Vtransient_mark_mode)
11231 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11232 != !NILP (w->region_showing)))
11234 struct buffer *prev = current_buffer;
11235 ptrdiff_t count = SPECPDL_INDEX ();
11237 specbind (Qinhibit_menubar_update, Qt);
11239 set_buffer_internal_1 (XBUFFER (w->buffer));
11240 if (save_match_data)
11241 record_unwind_save_match_data ();
11242 if (NILP (Voverriding_local_map_menu_flag))
11244 specbind (Qoverriding_terminal_local_map, Qnil);
11245 specbind (Qoverriding_local_map, Qnil);
11248 if (!hooks_run)
11250 /* Run the Lucid hook. */
11251 safe_run_hooks (Qactivate_menubar_hook);
11253 /* If it has changed current-menubar from previous value,
11254 really recompute the menu-bar from the value. */
11255 if (! NILP (Vlucid_menu_bar_dirty_flag))
11256 call0 (Qrecompute_lucid_menubar);
11258 safe_run_hooks (Qmenu_bar_update_hook);
11260 hooks_run = 1;
11263 XSETFRAME (Vmenu_updating_frame, f);
11264 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11266 /* Redisplay the menu bar in case we changed it. */
11267 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11268 || defined (HAVE_NS) || defined (USE_GTK)
11269 if (FRAME_WINDOW_P (f))
11271 #if defined (HAVE_NS)
11272 /* All frames on Mac OS share the same menubar. So only
11273 the selected frame should be allowed to set it. */
11274 if (f == SELECTED_FRAME ())
11275 #endif
11276 set_frame_menubar (f, 0, 0);
11278 else
11279 /* On a terminal screen, the menu bar is an ordinary screen
11280 line, and this makes it get updated. */
11281 w->update_mode_line = 1;
11282 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11283 /* In the non-toolkit version, the menu bar is an ordinary screen
11284 line, and this makes it get updated. */
11285 w->update_mode_line = 1;
11286 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11288 unbind_to (count, Qnil);
11289 set_buffer_internal_1 (prev);
11293 return hooks_run;
11298 /***********************************************************************
11299 Output Cursor
11300 ***********************************************************************/
11302 #ifdef HAVE_WINDOW_SYSTEM
11304 /* EXPORT:
11305 Nominal cursor position -- where to draw output.
11306 HPOS and VPOS are window relative glyph matrix coordinates.
11307 X and Y are window relative pixel coordinates. */
11309 struct cursor_pos output_cursor;
11312 /* EXPORT:
11313 Set the global variable output_cursor to CURSOR. All cursor
11314 positions are relative to updated_window. */
11316 void
11317 set_output_cursor (struct cursor_pos *cursor)
11319 output_cursor.hpos = cursor->hpos;
11320 output_cursor.vpos = cursor->vpos;
11321 output_cursor.x = cursor->x;
11322 output_cursor.y = cursor->y;
11326 /* EXPORT for RIF:
11327 Set a nominal cursor position.
11329 HPOS and VPOS are column/row positions in a window glyph matrix. X
11330 and Y are window text area relative pixel positions.
11332 If this is done during an update, updated_window will contain the
11333 window that is being updated and the position is the future output
11334 cursor position for that window. If updated_window is null, use
11335 selected_window and display the cursor at the given position. */
11337 void
11338 x_cursor_to (int vpos, int hpos, int y, int x)
11340 struct window *w;
11342 /* If updated_window is not set, work on selected_window. */
11343 if (updated_window)
11344 w = updated_window;
11345 else
11346 w = XWINDOW (selected_window);
11348 /* Set the output cursor. */
11349 output_cursor.hpos = hpos;
11350 output_cursor.vpos = vpos;
11351 output_cursor.x = x;
11352 output_cursor.y = y;
11354 /* If not called as part of an update, really display the cursor.
11355 This will also set the cursor position of W. */
11356 if (updated_window == NULL)
11358 BLOCK_INPUT;
11359 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11360 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11361 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11362 UNBLOCK_INPUT;
11366 #endif /* HAVE_WINDOW_SYSTEM */
11369 /***********************************************************************
11370 Tool-bars
11371 ***********************************************************************/
11373 #ifdef HAVE_WINDOW_SYSTEM
11375 /* Where the mouse was last time we reported a mouse event. */
11377 FRAME_PTR last_mouse_frame;
11379 /* Tool-bar item index of the item on which a mouse button was pressed
11380 or -1. */
11382 int last_tool_bar_item;
11385 static Lisp_Object
11386 update_tool_bar_unwind (Lisp_Object frame)
11388 selected_frame = frame;
11389 return Qnil;
11392 /* Update the tool-bar item list for frame F. This has to be done
11393 before we start to fill in any display lines. Called from
11394 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11395 and restore it here. */
11397 static void
11398 update_tool_bar (struct frame *f, int save_match_data)
11400 #if defined (USE_GTK) || defined (HAVE_NS)
11401 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11402 #else
11403 int do_update = WINDOWP (f->tool_bar_window)
11404 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11405 #endif
11407 if (do_update)
11409 Lisp_Object window;
11410 struct window *w;
11412 window = FRAME_SELECTED_WINDOW (f);
11413 w = XWINDOW (window);
11415 /* If the user has switched buffers or windows, we need to
11416 recompute to reflect the new bindings. But we'll
11417 recompute when update_mode_lines is set too; that means
11418 that people can use force-mode-line-update to request
11419 that the menu bar be recomputed. The adverse effect on
11420 the rest of the redisplay algorithm is about the same as
11421 windows_or_buffers_changed anyway. */
11422 if (windows_or_buffers_changed
11423 || w->update_mode_line
11424 || update_mode_lines
11425 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11426 < BUF_MODIFF (XBUFFER (w->buffer)))
11427 != w->last_had_star)
11428 || ((!NILP (Vtransient_mark_mode)
11429 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11430 != !NILP (w->region_showing)))
11432 struct buffer *prev = current_buffer;
11433 ptrdiff_t count = SPECPDL_INDEX ();
11434 Lisp_Object frame, new_tool_bar;
11435 int new_n_tool_bar;
11436 struct gcpro gcpro1;
11438 /* Set current_buffer to the buffer of the selected
11439 window of the frame, so that we get the right local
11440 keymaps. */
11441 set_buffer_internal_1 (XBUFFER (w->buffer));
11443 /* Save match data, if we must. */
11444 if (save_match_data)
11445 record_unwind_save_match_data ();
11447 /* Make sure that we don't accidentally use bogus keymaps. */
11448 if (NILP (Voverriding_local_map_menu_flag))
11450 specbind (Qoverriding_terminal_local_map, Qnil);
11451 specbind (Qoverriding_local_map, Qnil);
11454 GCPRO1 (new_tool_bar);
11456 /* We must temporarily set the selected frame to this frame
11457 before calling tool_bar_items, because the calculation of
11458 the tool-bar keymap uses the selected frame (see
11459 `tool-bar-make-keymap' in tool-bar.el). */
11460 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11461 XSETFRAME (frame, f);
11462 selected_frame = frame;
11464 /* Build desired tool-bar items from keymaps. */
11465 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11466 &new_n_tool_bar);
11468 /* Redisplay the tool-bar if we changed it. */
11469 if (new_n_tool_bar != f->n_tool_bar_items
11470 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11472 /* Redisplay that happens asynchronously due to an expose event
11473 may access f->tool_bar_items. Make sure we update both
11474 variables within BLOCK_INPUT so no such event interrupts. */
11475 BLOCK_INPUT;
11476 f->tool_bar_items = new_tool_bar;
11477 f->n_tool_bar_items = new_n_tool_bar;
11478 w->update_mode_line = 1;
11479 UNBLOCK_INPUT;
11482 UNGCPRO;
11484 unbind_to (count, Qnil);
11485 set_buffer_internal_1 (prev);
11491 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11492 F's desired tool-bar contents. F->tool_bar_items must have
11493 been set up previously by calling prepare_menu_bars. */
11495 static void
11496 build_desired_tool_bar_string (struct frame *f)
11498 int i, size, size_needed;
11499 struct gcpro gcpro1, gcpro2, gcpro3;
11500 Lisp_Object image, plist, props;
11502 image = plist = props = Qnil;
11503 GCPRO3 (image, plist, props);
11505 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11506 Otherwise, make a new string. */
11508 /* The size of the string we might be able to reuse. */
11509 size = (STRINGP (f->desired_tool_bar_string)
11510 ? SCHARS (f->desired_tool_bar_string)
11511 : 0);
11513 /* We need one space in the string for each image. */
11514 size_needed = f->n_tool_bar_items;
11516 /* Reuse f->desired_tool_bar_string, if possible. */
11517 if (size < size_needed || NILP (f->desired_tool_bar_string))
11518 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11519 make_number (' '));
11520 else
11522 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11523 Fremove_text_properties (make_number (0), make_number (size),
11524 props, f->desired_tool_bar_string);
11527 /* Put a `display' property on the string for the images to display,
11528 put a `menu_item' property on tool-bar items with a value that
11529 is the index of the item in F's tool-bar item vector. */
11530 for (i = 0; i < f->n_tool_bar_items; ++i)
11532 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11534 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11535 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11536 int hmargin, vmargin, relief, idx, end;
11538 /* If image is a vector, choose the image according to the
11539 button state. */
11540 image = PROP (TOOL_BAR_ITEM_IMAGES);
11541 if (VECTORP (image))
11543 if (enabled_p)
11544 idx = (selected_p
11545 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11546 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11547 else
11548 idx = (selected_p
11549 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11550 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11552 eassert (ASIZE (image) >= idx);
11553 image = AREF (image, idx);
11555 else
11556 idx = -1;
11558 /* Ignore invalid image specifications. */
11559 if (!valid_image_p (image))
11560 continue;
11562 /* Display the tool-bar button pressed, or depressed. */
11563 plist = Fcopy_sequence (XCDR (image));
11565 /* Compute margin and relief to draw. */
11566 relief = (tool_bar_button_relief >= 0
11567 ? tool_bar_button_relief
11568 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11569 hmargin = vmargin = relief;
11571 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11572 INT_MAX - max (hmargin, vmargin)))
11574 hmargin += XFASTINT (Vtool_bar_button_margin);
11575 vmargin += XFASTINT (Vtool_bar_button_margin);
11577 else if (CONSP (Vtool_bar_button_margin))
11579 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11580 INT_MAX - hmargin))
11581 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11583 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11584 INT_MAX - vmargin))
11585 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11588 if (auto_raise_tool_bar_buttons_p)
11590 /* Add a `:relief' property to the image spec if the item is
11591 selected. */
11592 if (selected_p)
11594 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11595 hmargin -= relief;
11596 vmargin -= relief;
11599 else
11601 /* If image is selected, display it pressed, i.e. with a
11602 negative relief. If it's not selected, display it with a
11603 raised relief. */
11604 plist = Fplist_put (plist, QCrelief,
11605 (selected_p
11606 ? make_number (-relief)
11607 : make_number (relief)));
11608 hmargin -= relief;
11609 vmargin -= relief;
11612 /* Put a margin around the image. */
11613 if (hmargin || vmargin)
11615 if (hmargin == vmargin)
11616 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11617 else
11618 plist = Fplist_put (plist, QCmargin,
11619 Fcons (make_number (hmargin),
11620 make_number (vmargin)));
11623 /* If button is not enabled, and we don't have special images
11624 for the disabled state, make the image appear disabled by
11625 applying an appropriate algorithm to it. */
11626 if (!enabled_p && idx < 0)
11627 plist = Fplist_put (plist, QCconversion, Qdisabled);
11629 /* Put a `display' text property on the string for the image to
11630 display. Put a `menu-item' property on the string that gives
11631 the start of this item's properties in the tool-bar items
11632 vector. */
11633 image = Fcons (Qimage, plist);
11634 props = list4 (Qdisplay, image,
11635 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11637 /* Let the last image hide all remaining spaces in the tool bar
11638 string. The string can be longer than needed when we reuse a
11639 previous string. */
11640 if (i + 1 == f->n_tool_bar_items)
11641 end = SCHARS (f->desired_tool_bar_string);
11642 else
11643 end = i + 1;
11644 Fadd_text_properties (make_number (i), make_number (end),
11645 props, f->desired_tool_bar_string);
11646 #undef PROP
11649 UNGCPRO;
11653 /* Display one line of the tool-bar of frame IT->f.
11655 HEIGHT specifies the desired height of the tool-bar line.
11656 If the actual height of the glyph row is less than HEIGHT, the
11657 row's height is increased to HEIGHT, and the icons are centered
11658 vertically in the new height.
11660 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11661 count a final empty row in case the tool-bar width exactly matches
11662 the window width.
11665 static void
11666 display_tool_bar_line (struct it *it, int height)
11668 struct glyph_row *row = it->glyph_row;
11669 int max_x = it->last_visible_x;
11670 struct glyph *last;
11672 prepare_desired_row (row);
11673 row->y = it->current_y;
11675 /* Note that this isn't made use of if the face hasn't a box,
11676 so there's no need to check the face here. */
11677 it->start_of_box_run_p = 1;
11679 while (it->current_x < max_x)
11681 int x, n_glyphs_before, i, nglyphs;
11682 struct it it_before;
11684 /* Get the next display element. */
11685 if (!get_next_display_element (it))
11687 /* Don't count empty row if we are counting needed tool-bar lines. */
11688 if (height < 0 && !it->hpos)
11689 return;
11690 break;
11693 /* Produce glyphs. */
11694 n_glyphs_before = row->used[TEXT_AREA];
11695 it_before = *it;
11697 PRODUCE_GLYPHS (it);
11699 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11700 i = 0;
11701 x = it_before.current_x;
11702 while (i < nglyphs)
11704 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11706 if (x + glyph->pixel_width > max_x)
11708 /* Glyph doesn't fit on line. Backtrack. */
11709 row->used[TEXT_AREA] = n_glyphs_before;
11710 *it = it_before;
11711 /* If this is the only glyph on this line, it will never fit on the
11712 tool-bar, so skip it. But ensure there is at least one glyph,
11713 so we don't accidentally disable the tool-bar. */
11714 if (n_glyphs_before == 0
11715 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11716 break;
11717 goto out;
11720 ++it->hpos;
11721 x += glyph->pixel_width;
11722 ++i;
11725 /* Stop at line end. */
11726 if (ITERATOR_AT_END_OF_LINE_P (it))
11727 break;
11729 set_iterator_to_next (it, 1);
11732 out:;
11734 row->displays_text_p = row->used[TEXT_AREA] != 0;
11736 /* Use default face for the border below the tool bar.
11738 FIXME: When auto-resize-tool-bars is grow-only, there is
11739 no additional border below the possibly empty tool-bar lines.
11740 So to make the extra empty lines look "normal", we have to
11741 use the tool-bar face for the border too. */
11742 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11743 it->face_id = DEFAULT_FACE_ID;
11745 extend_face_to_end_of_line (it);
11746 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11747 last->right_box_line_p = 1;
11748 if (last == row->glyphs[TEXT_AREA])
11749 last->left_box_line_p = 1;
11751 /* Make line the desired height and center it vertically. */
11752 if ((height -= it->max_ascent + it->max_descent) > 0)
11754 /* Don't add more than one line height. */
11755 height %= FRAME_LINE_HEIGHT (it->f);
11756 it->max_ascent += height / 2;
11757 it->max_descent += (height + 1) / 2;
11760 compute_line_metrics (it);
11762 /* If line is empty, make it occupy the rest of the tool-bar. */
11763 if (!row->displays_text_p)
11765 row->height = row->phys_height = it->last_visible_y - row->y;
11766 row->visible_height = row->height;
11767 row->ascent = row->phys_ascent = 0;
11768 row->extra_line_spacing = 0;
11771 row->full_width_p = 1;
11772 row->continued_p = 0;
11773 row->truncated_on_left_p = 0;
11774 row->truncated_on_right_p = 0;
11776 it->current_x = it->hpos = 0;
11777 it->current_y += row->height;
11778 ++it->vpos;
11779 ++it->glyph_row;
11783 /* Max tool-bar height. */
11785 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11786 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11788 /* Value is the number of screen lines needed to make all tool-bar
11789 items of frame F visible. The number of actual rows needed is
11790 returned in *N_ROWS if non-NULL. */
11792 static int
11793 tool_bar_lines_needed (struct frame *f, int *n_rows)
11795 struct window *w = XWINDOW (f->tool_bar_window);
11796 struct it it;
11797 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11798 the desired matrix, so use (unused) mode-line row as temporary row to
11799 avoid destroying the first tool-bar row. */
11800 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11802 /* Initialize an iterator for iteration over
11803 F->desired_tool_bar_string in the tool-bar window of frame F. */
11804 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11805 it.first_visible_x = 0;
11806 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11807 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11808 it.paragraph_embedding = L2R;
11810 while (!ITERATOR_AT_END_P (&it))
11812 clear_glyph_row (temp_row);
11813 it.glyph_row = temp_row;
11814 display_tool_bar_line (&it, -1);
11816 clear_glyph_row (temp_row);
11818 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11819 if (n_rows)
11820 *n_rows = it.vpos > 0 ? it.vpos : -1;
11822 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11826 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11827 0, 1, 0,
11828 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11829 (Lisp_Object frame)
11831 struct frame *f;
11832 struct window *w;
11833 int nlines = 0;
11835 if (NILP (frame))
11836 frame = selected_frame;
11837 else
11838 CHECK_FRAME (frame);
11839 f = XFRAME (frame);
11841 if (WINDOWP (f->tool_bar_window)
11842 && (w = XWINDOW (f->tool_bar_window),
11843 WINDOW_TOTAL_LINES (w) > 0))
11845 update_tool_bar (f, 1);
11846 if (f->n_tool_bar_items)
11848 build_desired_tool_bar_string (f);
11849 nlines = tool_bar_lines_needed (f, NULL);
11853 return make_number (nlines);
11857 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11858 height should be changed. */
11860 static int
11861 redisplay_tool_bar (struct frame *f)
11863 struct window *w;
11864 struct it it;
11865 struct glyph_row *row;
11867 #if defined (USE_GTK) || defined (HAVE_NS)
11868 if (FRAME_EXTERNAL_TOOL_BAR (f))
11869 update_frame_tool_bar (f);
11870 return 0;
11871 #endif
11873 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11874 do anything. This means you must start with tool-bar-lines
11875 non-zero to get the auto-sizing effect. Or in other words, you
11876 can turn off tool-bars by specifying tool-bar-lines zero. */
11877 if (!WINDOWP (f->tool_bar_window)
11878 || (w = XWINDOW (f->tool_bar_window),
11879 WINDOW_TOTAL_LINES (w) == 0))
11880 return 0;
11882 /* Set up an iterator for the tool-bar window. */
11883 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11884 it.first_visible_x = 0;
11885 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11886 row = it.glyph_row;
11888 /* Build a string that represents the contents of the tool-bar. */
11889 build_desired_tool_bar_string (f);
11890 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11891 /* FIXME: This should be controlled by a user option. But it
11892 doesn't make sense to have an R2L tool bar if the menu bar cannot
11893 be drawn also R2L, and making the menu bar R2L is tricky due
11894 toolkit-specific code that implements it. If an R2L tool bar is
11895 ever supported, display_tool_bar_line should also be augmented to
11896 call unproduce_glyphs like display_line and display_string
11897 do. */
11898 it.paragraph_embedding = L2R;
11900 if (f->n_tool_bar_rows == 0)
11902 int nlines;
11904 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11905 nlines != WINDOW_TOTAL_LINES (w)))
11907 Lisp_Object frame;
11908 int old_height = WINDOW_TOTAL_LINES (w);
11910 XSETFRAME (frame, f);
11911 Fmodify_frame_parameters (frame,
11912 Fcons (Fcons (Qtool_bar_lines,
11913 make_number (nlines)),
11914 Qnil));
11915 if (WINDOW_TOTAL_LINES (w) != old_height)
11917 clear_glyph_matrix (w->desired_matrix);
11918 fonts_changed_p = 1;
11919 return 1;
11924 /* Display as many lines as needed to display all tool-bar items. */
11926 if (f->n_tool_bar_rows > 0)
11928 int border, rows, height, extra;
11930 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11931 border = XINT (Vtool_bar_border);
11932 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11933 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11934 else if (EQ (Vtool_bar_border, Qborder_width))
11935 border = f->border_width;
11936 else
11937 border = 0;
11938 if (border < 0)
11939 border = 0;
11941 rows = f->n_tool_bar_rows;
11942 height = max (1, (it.last_visible_y - border) / rows);
11943 extra = it.last_visible_y - border - height * rows;
11945 while (it.current_y < it.last_visible_y)
11947 int h = 0;
11948 if (extra > 0 && rows-- > 0)
11950 h = (extra + rows - 1) / rows;
11951 extra -= h;
11953 display_tool_bar_line (&it, height + h);
11956 else
11958 while (it.current_y < it.last_visible_y)
11959 display_tool_bar_line (&it, 0);
11962 /* It doesn't make much sense to try scrolling in the tool-bar
11963 window, so don't do it. */
11964 w->desired_matrix->no_scrolling_p = 1;
11965 w->must_be_updated_p = 1;
11967 if (!NILP (Vauto_resize_tool_bars))
11969 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11970 int change_height_p = 0;
11972 /* If we couldn't display everything, change the tool-bar's
11973 height if there is room for more. */
11974 if (IT_STRING_CHARPOS (it) < it.end_charpos
11975 && it.current_y < max_tool_bar_height)
11976 change_height_p = 1;
11978 row = it.glyph_row - 1;
11980 /* If there are blank lines at the end, except for a partially
11981 visible blank line at the end that is smaller than
11982 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11983 if (!row->displays_text_p
11984 && row->height >= FRAME_LINE_HEIGHT (f))
11985 change_height_p = 1;
11987 /* If row displays tool-bar items, but is partially visible,
11988 change the tool-bar's height. */
11989 if (row->displays_text_p
11990 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11991 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11992 change_height_p = 1;
11994 /* Resize windows as needed by changing the `tool-bar-lines'
11995 frame parameter. */
11996 if (change_height_p)
11998 Lisp_Object frame;
11999 int old_height = WINDOW_TOTAL_LINES (w);
12000 int nrows;
12001 int nlines = tool_bar_lines_needed (f, &nrows);
12003 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12004 && !f->minimize_tool_bar_window_p)
12005 ? (nlines > old_height)
12006 : (nlines != old_height));
12007 f->minimize_tool_bar_window_p = 0;
12009 if (change_height_p)
12011 XSETFRAME (frame, f);
12012 Fmodify_frame_parameters (frame,
12013 Fcons (Fcons (Qtool_bar_lines,
12014 make_number (nlines)),
12015 Qnil));
12016 if (WINDOW_TOTAL_LINES (w) != old_height)
12018 clear_glyph_matrix (w->desired_matrix);
12019 f->n_tool_bar_rows = nrows;
12020 fonts_changed_p = 1;
12021 return 1;
12027 f->minimize_tool_bar_window_p = 0;
12028 return 0;
12032 /* Get information about the tool-bar item which is displayed in GLYPH
12033 on frame F. Return in *PROP_IDX the index where tool-bar item
12034 properties start in F->tool_bar_items. Value is zero if
12035 GLYPH doesn't display a tool-bar item. */
12037 static int
12038 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12040 Lisp_Object prop;
12041 int success_p;
12042 int charpos;
12044 /* This function can be called asynchronously, which means we must
12045 exclude any possibility that Fget_text_property signals an
12046 error. */
12047 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12048 charpos = max (0, charpos);
12050 /* Get the text property `menu-item' at pos. The value of that
12051 property is the start index of this item's properties in
12052 F->tool_bar_items. */
12053 prop = Fget_text_property (make_number (charpos),
12054 Qmenu_item, f->current_tool_bar_string);
12055 if (INTEGERP (prop))
12057 *prop_idx = XINT (prop);
12058 success_p = 1;
12060 else
12061 success_p = 0;
12063 return success_p;
12067 /* Get information about the tool-bar item at position X/Y on frame F.
12068 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12069 the current matrix of the tool-bar window of F, or NULL if not
12070 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12071 item in F->tool_bar_items. Value is
12073 -1 if X/Y is not on a tool-bar item
12074 0 if X/Y is on the same item that was highlighted before.
12075 1 otherwise. */
12077 static int
12078 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12079 int *hpos, int *vpos, int *prop_idx)
12081 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12082 struct window *w = XWINDOW (f->tool_bar_window);
12083 int area;
12085 /* Find the glyph under X/Y. */
12086 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12087 if (*glyph == NULL)
12088 return -1;
12090 /* Get the start of this tool-bar item's properties in
12091 f->tool_bar_items. */
12092 if (!tool_bar_item_info (f, *glyph, prop_idx))
12093 return -1;
12095 /* Is mouse on the highlighted item? */
12096 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12097 && *vpos >= hlinfo->mouse_face_beg_row
12098 && *vpos <= hlinfo->mouse_face_end_row
12099 && (*vpos > hlinfo->mouse_face_beg_row
12100 || *hpos >= hlinfo->mouse_face_beg_col)
12101 && (*vpos < hlinfo->mouse_face_end_row
12102 || *hpos < hlinfo->mouse_face_end_col
12103 || hlinfo->mouse_face_past_end))
12104 return 0;
12106 return 1;
12110 /* EXPORT:
12111 Handle mouse button event on the tool-bar of frame F, at
12112 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12113 0 for button release. MODIFIERS is event modifiers for button
12114 release. */
12116 void
12117 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12118 int modifiers)
12120 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12121 struct window *w = XWINDOW (f->tool_bar_window);
12122 int hpos, vpos, prop_idx;
12123 struct glyph *glyph;
12124 Lisp_Object enabled_p;
12126 /* If not on the highlighted tool-bar item, return. */
12127 frame_to_window_pixel_xy (w, &x, &y);
12128 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12129 return;
12131 /* If item is disabled, do nothing. */
12132 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12133 if (NILP (enabled_p))
12134 return;
12136 if (down_p)
12138 /* Show item in pressed state. */
12139 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12140 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12141 last_tool_bar_item = prop_idx;
12143 else
12145 Lisp_Object key, frame;
12146 struct input_event event;
12147 EVENT_INIT (event);
12149 /* Show item in released state. */
12150 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12151 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12153 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12155 XSETFRAME (frame, f);
12156 event.kind = TOOL_BAR_EVENT;
12157 event.frame_or_window = frame;
12158 event.arg = frame;
12159 kbd_buffer_store_event (&event);
12161 event.kind = TOOL_BAR_EVENT;
12162 event.frame_or_window = frame;
12163 event.arg = key;
12164 event.modifiers = modifiers;
12165 kbd_buffer_store_event (&event);
12166 last_tool_bar_item = -1;
12171 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12172 tool-bar window-relative coordinates X/Y. Called from
12173 note_mouse_highlight. */
12175 static void
12176 note_tool_bar_highlight (struct frame *f, int x, int y)
12178 Lisp_Object window = f->tool_bar_window;
12179 struct window *w = XWINDOW (window);
12180 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12181 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12182 int hpos, vpos;
12183 struct glyph *glyph;
12184 struct glyph_row *row;
12185 int i;
12186 Lisp_Object enabled_p;
12187 int prop_idx;
12188 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12189 int mouse_down_p, rc;
12191 /* Function note_mouse_highlight is called with negative X/Y
12192 values when mouse moves outside of the frame. */
12193 if (x <= 0 || y <= 0)
12195 clear_mouse_face (hlinfo);
12196 return;
12199 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12200 if (rc < 0)
12202 /* Not on tool-bar item. */
12203 clear_mouse_face (hlinfo);
12204 return;
12206 else if (rc == 0)
12207 /* On same tool-bar item as before. */
12208 goto set_help_echo;
12210 clear_mouse_face (hlinfo);
12212 /* Mouse is down, but on different tool-bar item? */
12213 mouse_down_p = (dpyinfo->grabbed
12214 && f == last_mouse_frame
12215 && FRAME_LIVE_P (f));
12216 if (mouse_down_p
12217 && last_tool_bar_item != prop_idx)
12218 return;
12220 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12221 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12223 /* If tool-bar item is not enabled, don't highlight it. */
12224 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12225 if (!NILP (enabled_p))
12227 /* Compute the x-position of the glyph. In front and past the
12228 image is a space. We include this in the highlighted area. */
12229 row = MATRIX_ROW (w->current_matrix, vpos);
12230 for (i = x = 0; i < hpos; ++i)
12231 x += row->glyphs[TEXT_AREA][i].pixel_width;
12233 /* Record this as the current active region. */
12234 hlinfo->mouse_face_beg_col = hpos;
12235 hlinfo->mouse_face_beg_row = vpos;
12236 hlinfo->mouse_face_beg_x = x;
12237 hlinfo->mouse_face_beg_y = row->y;
12238 hlinfo->mouse_face_past_end = 0;
12240 hlinfo->mouse_face_end_col = hpos + 1;
12241 hlinfo->mouse_face_end_row = vpos;
12242 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12243 hlinfo->mouse_face_end_y = row->y;
12244 hlinfo->mouse_face_window = window;
12245 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12247 /* Display it as active. */
12248 show_mouse_face (hlinfo, draw);
12249 hlinfo->mouse_face_image_state = draw;
12252 set_help_echo:
12254 /* Set help_echo_string to a help string to display for this tool-bar item.
12255 XTread_socket does the rest. */
12256 help_echo_object = help_echo_window = Qnil;
12257 help_echo_pos = -1;
12258 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12259 if (NILP (help_echo_string))
12260 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12263 #endif /* HAVE_WINDOW_SYSTEM */
12267 /************************************************************************
12268 Horizontal scrolling
12269 ************************************************************************/
12271 static int hscroll_window_tree (Lisp_Object);
12272 static int hscroll_windows (Lisp_Object);
12274 /* For all leaf windows in the window tree rooted at WINDOW, set their
12275 hscroll value so that PT is (i) visible in the window, and (ii) so
12276 that it is not within a certain margin at the window's left and
12277 right border. Value is non-zero if any window's hscroll has been
12278 changed. */
12280 static int
12281 hscroll_window_tree (Lisp_Object window)
12283 int hscrolled_p = 0;
12284 int hscroll_relative_p = FLOATP (Vhscroll_step);
12285 int hscroll_step_abs = 0;
12286 double hscroll_step_rel = 0;
12288 if (hscroll_relative_p)
12290 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12291 if (hscroll_step_rel < 0)
12293 hscroll_relative_p = 0;
12294 hscroll_step_abs = 0;
12297 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12299 hscroll_step_abs = XINT (Vhscroll_step);
12300 if (hscroll_step_abs < 0)
12301 hscroll_step_abs = 0;
12303 else
12304 hscroll_step_abs = 0;
12306 while (WINDOWP (window))
12308 struct window *w = XWINDOW (window);
12310 if (WINDOWP (w->hchild))
12311 hscrolled_p |= hscroll_window_tree (w->hchild);
12312 else if (WINDOWP (w->vchild))
12313 hscrolled_p |= hscroll_window_tree (w->vchild);
12314 else if (w->cursor.vpos >= 0)
12316 int h_margin;
12317 int text_area_width;
12318 struct glyph_row *current_cursor_row
12319 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12320 struct glyph_row *desired_cursor_row
12321 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12322 struct glyph_row *cursor_row
12323 = (desired_cursor_row->enabled_p
12324 ? desired_cursor_row
12325 : current_cursor_row);
12326 int row_r2l_p = cursor_row->reversed_p;
12328 text_area_width = window_box_width (w, TEXT_AREA);
12330 /* Scroll when cursor is inside this scroll margin. */
12331 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12333 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12334 /* For left-to-right rows, hscroll when cursor is either
12335 (i) inside the right hscroll margin, or (ii) if it is
12336 inside the left margin and the window is already
12337 hscrolled. */
12338 && ((!row_r2l_p
12339 && ((w->hscroll
12340 && w->cursor.x <= h_margin)
12341 || (cursor_row->enabled_p
12342 && cursor_row->truncated_on_right_p
12343 && (w->cursor.x >= text_area_width - h_margin))))
12344 /* For right-to-left rows, the logic is similar,
12345 except that rules for scrolling to left and right
12346 are reversed. E.g., if cursor.x <= h_margin, we
12347 need to hscroll "to the right" unconditionally,
12348 and that will scroll the screen to the left so as
12349 to reveal the next portion of the row. */
12350 || (row_r2l_p
12351 && ((cursor_row->enabled_p
12352 /* FIXME: It is confusing to set the
12353 truncated_on_right_p flag when R2L rows
12354 are actually truncated on the left. */
12355 && cursor_row->truncated_on_right_p
12356 && w->cursor.x <= h_margin)
12357 || (w->hscroll
12358 && (w->cursor.x >= text_area_width - h_margin))))))
12360 struct it it;
12361 ptrdiff_t hscroll;
12362 struct buffer *saved_current_buffer;
12363 ptrdiff_t pt;
12364 int wanted_x;
12366 /* Find point in a display of infinite width. */
12367 saved_current_buffer = current_buffer;
12368 current_buffer = XBUFFER (w->buffer);
12370 if (w == XWINDOW (selected_window))
12371 pt = PT;
12372 else
12374 pt = marker_position (w->pointm);
12375 pt = max (BEGV, pt);
12376 pt = min (ZV, pt);
12379 /* Move iterator to pt starting at cursor_row->start in
12380 a line with infinite width. */
12381 init_to_row_start (&it, w, cursor_row);
12382 it.last_visible_x = INFINITY;
12383 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12384 current_buffer = saved_current_buffer;
12386 /* Position cursor in window. */
12387 if (!hscroll_relative_p && hscroll_step_abs == 0)
12388 hscroll = max (0, (it.current_x
12389 - (ITERATOR_AT_END_OF_LINE_P (&it)
12390 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12391 : (text_area_width / 2))))
12392 / FRAME_COLUMN_WIDTH (it.f);
12393 else if ((!row_r2l_p
12394 && w->cursor.x >= text_area_width - h_margin)
12395 || (row_r2l_p && w->cursor.x <= h_margin))
12397 if (hscroll_relative_p)
12398 wanted_x = text_area_width * (1 - hscroll_step_rel)
12399 - h_margin;
12400 else
12401 wanted_x = text_area_width
12402 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12403 - h_margin;
12404 hscroll
12405 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12407 else
12409 if (hscroll_relative_p)
12410 wanted_x = text_area_width * hscroll_step_rel
12411 + h_margin;
12412 else
12413 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12414 + h_margin;
12415 hscroll
12416 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12418 hscroll = max (hscroll, w->min_hscroll);
12420 /* Don't prevent redisplay optimizations if hscroll
12421 hasn't changed, as it will unnecessarily slow down
12422 redisplay. */
12423 if (w->hscroll != hscroll)
12425 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12426 w->hscroll = hscroll;
12427 hscrolled_p = 1;
12432 window = w->next;
12435 /* Value is non-zero if hscroll of any leaf window has been changed. */
12436 return hscrolled_p;
12440 /* Set hscroll so that cursor is visible and not inside horizontal
12441 scroll margins for all windows in the tree rooted at WINDOW. See
12442 also hscroll_window_tree above. Value is non-zero if any window's
12443 hscroll has been changed. If it has, desired matrices on the frame
12444 of WINDOW are cleared. */
12446 static int
12447 hscroll_windows (Lisp_Object window)
12449 int hscrolled_p = hscroll_window_tree (window);
12450 if (hscrolled_p)
12451 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12452 return hscrolled_p;
12457 /************************************************************************
12458 Redisplay
12459 ************************************************************************/
12461 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12462 to a non-zero value. This is sometimes handy to have in a debugger
12463 session. */
12465 #ifdef GLYPH_DEBUG
12467 /* First and last unchanged row for try_window_id. */
12469 static int debug_first_unchanged_at_end_vpos;
12470 static int debug_last_unchanged_at_beg_vpos;
12472 /* Delta vpos and y. */
12474 static int debug_dvpos, debug_dy;
12476 /* Delta in characters and bytes for try_window_id. */
12478 static ptrdiff_t debug_delta, debug_delta_bytes;
12480 /* Values of window_end_pos and window_end_vpos at the end of
12481 try_window_id. */
12483 static ptrdiff_t debug_end_vpos;
12485 /* Append a string to W->desired_matrix->method. FMT is a printf
12486 format string. If trace_redisplay_p is non-zero also printf the
12487 resulting string to stderr. */
12489 static void debug_method_add (struct window *, char const *, ...)
12490 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12492 static void
12493 debug_method_add (struct window *w, char const *fmt, ...)
12495 char *method = w->desired_matrix->method;
12496 int len = strlen (method);
12497 int size = sizeof w->desired_matrix->method;
12498 int remaining = size - len - 1;
12499 va_list ap;
12501 if (len && remaining)
12503 method[len] = '|';
12504 --remaining, ++len;
12507 va_start (ap, fmt);
12508 vsnprintf (method + len, remaining + 1, fmt, ap);
12509 va_end (ap);
12511 if (trace_redisplay_p)
12512 fprintf (stderr, "%p (%s): %s\n",
12514 ((BUFFERP (w->buffer)
12515 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12516 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12517 : "no buffer"),
12518 method + len);
12521 #endif /* GLYPH_DEBUG */
12524 /* Value is non-zero if all changes in window W, which displays
12525 current_buffer, are in the text between START and END. START is a
12526 buffer position, END is given as a distance from Z. Used in
12527 redisplay_internal for display optimization. */
12529 static inline int
12530 text_outside_line_unchanged_p (struct window *w,
12531 ptrdiff_t start, ptrdiff_t end)
12533 int unchanged_p = 1;
12535 /* If text or overlays have changed, see where. */
12536 if (w->last_modified < MODIFF
12537 || w->last_overlay_modified < OVERLAY_MODIFF)
12539 /* Gap in the line? */
12540 if (GPT < start || Z - GPT < end)
12541 unchanged_p = 0;
12543 /* Changes start in front of the line, or end after it? */
12544 if (unchanged_p
12545 && (BEG_UNCHANGED < start - 1
12546 || END_UNCHANGED < end))
12547 unchanged_p = 0;
12549 /* If selective display, can't optimize if changes start at the
12550 beginning of the line. */
12551 if (unchanged_p
12552 && INTEGERP (BVAR (current_buffer, selective_display))
12553 && XINT (BVAR (current_buffer, selective_display)) > 0
12554 && (BEG_UNCHANGED < start || GPT <= start))
12555 unchanged_p = 0;
12557 /* If there are overlays at the start or end of the line, these
12558 may have overlay strings with newlines in them. A change at
12559 START, for instance, may actually concern the display of such
12560 overlay strings as well, and they are displayed on different
12561 lines. So, quickly rule out this case. (For the future, it
12562 might be desirable to implement something more telling than
12563 just BEG/END_UNCHANGED.) */
12564 if (unchanged_p)
12566 if (BEG + BEG_UNCHANGED == start
12567 && overlay_touches_p (start))
12568 unchanged_p = 0;
12569 if (END_UNCHANGED == end
12570 && overlay_touches_p (Z - end))
12571 unchanged_p = 0;
12574 /* Under bidi reordering, adding or deleting a character in the
12575 beginning of a paragraph, before the first strong directional
12576 character, can change the base direction of the paragraph (unless
12577 the buffer specifies a fixed paragraph direction), which will
12578 require to redisplay the whole paragraph. It might be worthwhile
12579 to find the paragraph limits and widen the range of redisplayed
12580 lines to that, but for now just give up this optimization. */
12581 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12582 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12583 unchanged_p = 0;
12586 return unchanged_p;
12590 /* Do a frame update, taking possible shortcuts into account. This is
12591 the main external entry point for redisplay.
12593 If the last redisplay displayed an echo area message and that message
12594 is no longer requested, we clear the echo area or bring back the
12595 mini-buffer if that is in use. */
12597 void
12598 redisplay (void)
12600 redisplay_internal ();
12604 static Lisp_Object
12605 overlay_arrow_string_or_property (Lisp_Object var)
12607 Lisp_Object val;
12609 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12610 return val;
12612 return Voverlay_arrow_string;
12615 /* Return 1 if there are any overlay-arrows in current_buffer. */
12616 static int
12617 overlay_arrow_in_current_buffer_p (void)
12619 Lisp_Object vlist;
12621 for (vlist = Voverlay_arrow_variable_list;
12622 CONSP (vlist);
12623 vlist = XCDR (vlist))
12625 Lisp_Object var = XCAR (vlist);
12626 Lisp_Object val;
12628 if (!SYMBOLP (var))
12629 continue;
12630 val = find_symbol_value (var);
12631 if (MARKERP (val)
12632 && current_buffer == XMARKER (val)->buffer)
12633 return 1;
12635 return 0;
12639 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12640 has changed. */
12642 static int
12643 overlay_arrows_changed_p (void)
12645 Lisp_Object vlist;
12647 for (vlist = Voverlay_arrow_variable_list;
12648 CONSP (vlist);
12649 vlist = XCDR (vlist))
12651 Lisp_Object var = XCAR (vlist);
12652 Lisp_Object val, pstr;
12654 if (!SYMBOLP (var))
12655 continue;
12656 val = find_symbol_value (var);
12657 if (!MARKERP (val))
12658 continue;
12659 if (! EQ (COERCE_MARKER (val),
12660 Fget (var, Qlast_arrow_position))
12661 || ! (pstr = overlay_arrow_string_or_property (var),
12662 EQ (pstr, Fget (var, Qlast_arrow_string))))
12663 return 1;
12665 return 0;
12668 /* Mark overlay arrows to be updated on next redisplay. */
12670 static void
12671 update_overlay_arrows (int up_to_date)
12673 Lisp_Object vlist;
12675 for (vlist = Voverlay_arrow_variable_list;
12676 CONSP (vlist);
12677 vlist = XCDR (vlist))
12679 Lisp_Object var = XCAR (vlist);
12681 if (!SYMBOLP (var))
12682 continue;
12684 if (up_to_date > 0)
12686 Lisp_Object val = find_symbol_value (var);
12687 Fput (var, Qlast_arrow_position,
12688 COERCE_MARKER (val));
12689 Fput (var, Qlast_arrow_string,
12690 overlay_arrow_string_or_property (var));
12692 else if (up_to_date < 0
12693 || !NILP (Fget (var, Qlast_arrow_position)))
12695 Fput (var, Qlast_arrow_position, Qt);
12696 Fput (var, Qlast_arrow_string, Qt);
12702 /* Return overlay arrow string to display at row.
12703 Return integer (bitmap number) for arrow bitmap in left fringe.
12704 Return nil if no overlay arrow. */
12706 static Lisp_Object
12707 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12709 Lisp_Object vlist;
12711 for (vlist = Voverlay_arrow_variable_list;
12712 CONSP (vlist);
12713 vlist = XCDR (vlist))
12715 Lisp_Object var = XCAR (vlist);
12716 Lisp_Object val;
12718 if (!SYMBOLP (var))
12719 continue;
12721 val = find_symbol_value (var);
12723 if (MARKERP (val)
12724 && current_buffer == XMARKER (val)->buffer
12725 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12727 if (FRAME_WINDOW_P (it->f)
12728 /* FIXME: if ROW->reversed_p is set, this should test
12729 the right fringe, not the left one. */
12730 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12732 #ifdef HAVE_WINDOW_SYSTEM
12733 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12735 int fringe_bitmap;
12736 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12737 return make_number (fringe_bitmap);
12739 #endif
12740 return make_number (-1); /* Use default arrow bitmap */
12742 return overlay_arrow_string_or_property (var);
12746 return Qnil;
12749 /* Return 1 if point moved out of or into a composition. Otherwise
12750 return 0. PREV_BUF and PREV_PT are the last point buffer and
12751 position. BUF and PT are the current point buffer and position. */
12753 static int
12754 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12755 struct buffer *buf, ptrdiff_t pt)
12757 ptrdiff_t start, end;
12758 Lisp_Object prop;
12759 Lisp_Object buffer;
12761 XSETBUFFER (buffer, buf);
12762 /* Check a composition at the last point if point moved within the
12763 same buffer. */
12764 if (prev_buf == buf)
12766 if (prev_pt == pt)
12767 /* Point didn't move. */
12768 return 0;
12770 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12771 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12772 && COMPOSITION_VALID_P (start, end, prop)
12773 && start < prev_pt && end > prev_pt)
12774 /* The last point was within the composition. Return 1 iff
12775 point moved out of the composition. */
12776 return (pt <= start || pt >= end);
12779 /* Check a composition at the current point. */
12780 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12781 && find_composition (pt, -1, &start, &end, &prop, buffer)
12782 && COMPOSITION_VALID_P (start, end, prop)
12783 && start < pt && end > pt);
12787 /* Reconsider the setting of B->clip_changed which is displayed
12788 in window W. */
12790 static inline void
12791 reconsider_clip_changes (struct window *w, struct buffer *b)
12793 if (b->clip_changed
12794 && !NILP (w->window_end_valid)
12795 && w->current_matrix->buffer == b
12796 && w->current_matrix->zv == BUF_ZV (b)
12797 && w->current_matrix->begv == BUF_BEGV (b))
12798 b->clip_changed = 0;
12800 /* If display wasn't paused, and W is not a tool bar window, see if
12801 point has been moved into or out of a composition. In that case,
12802 we set b->clip_changed to 1 to force updating the screen. If
12803 b->clip_changed has already been set to 1, we can skip this
12804 check. */
12805 if (!b->clip_changed
12806 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12808 ptrdiff_t pt;
12810 if (w == XWINDOW (selected_window))
12811 pt = PT;
12812 else
12813 pt = marker_position (w->pointm);
12815 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12816 || pt != w->last_point)
12817 && check_point_in_composition (w->current_matrix->buffer,
12818 w->last_point,
12819 XBUFFER (w->buffer), pt))
12820 b->clip_changed = 1;
12825 /* Select FRAME to forward the values of frame-local variables into C
12826 variables so that the redisplay routines can access those values
12827 directly. */
12829 static void
12830 select_frame_for_redisplay (Lisp_Object frame)
12832 Lisp_Object tail, tem;
12833 Lisp_Object old = selected_frame;
12834 struct Lisp_Symbol *sym;
12836 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12838 selected_frame = frame;
12840 do {
12841 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12842 if (CONSP (XCAR (tail))
12843 && (tem = XCAR (XCAR (tail)),
12844 SYMBOLP (tem))
12845 && (sym = indirect_variable (XSYMBOL (tem)),
12846 sym->redirect == SYMBOL_LOCALIZED)
12847 && sym->val.blv->frame_local)
12848 /* Use find_symbol_value rather than Fsymbol_value
12849 to avoid an error if it is void. */
12850 find_symbol_value (tem);
12851 } while (!EQ (frame, old) && (frame = old, 1));
12855 #define STOP_POLLING \
12856 do { if (! polling_stopped_here) stop_polling (); \
12857 polling_stopped_here = 1; } while (0)
12859 #define RESUME_POLLING \
12860 do { if (polling_stopped_here) start_polling (); \
12861 polling_stopped_here = 0; } while (0)
12864 /* Perhaps in the future avoid recentering windows if it
12865 is not necessary; currently that causes some problems. */
12867 static void
12868 redisplay_internal (void)
12870 struct window *w = XWINDOW (selected_window);
12871 struct window *sw;
12872 struct frame *fr;
12873 int pending;
12874 int must_finish = 0;
12875 struct text_pos tlbufpos, tlendpos;
12876 int number_of_visible_frames;
12877 ptrdiff_t count, count1;
12878 struct frame *sf;
12879 int polling_stopped_here = 0;
12880 Lisp_Object old_frame = selected_frame;
12882 /* Non-zero means redisplay has to consider all windows on all
12883 frames. Zero means, only selected_window is considered. */
12884 int consider_all_windows_p;
12886 /* Non-zero means redisplay has to redisplay the miniwindow */
12887 int update_miniwindow_p = 0;
12889 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12891 /* No redisplay if running in batch mode or frame is not yet fully
12892 initialized, or redisplay is explicitly turned off by setting
12893 Vinhibit_redisplay. */
12894 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12895 || !NILP (Vinhibit_redisplay))
12896 return;
12898 /* Don't examine these until after testing Vinhibit_redisplay.
12899 When Emacs is shutting down, perhaps because its connection to
12900 X has dropped, we should not look at them at all. */
12901 fr = XFRAME (w->frame);
12902 sf = SELECTED_FRAME ();
12904 if (!fr->glyphs_initialized_p)
12905 return;
12907 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12908 if (popup_activated ())
12909 return;
12910 #endif
12912 /* I don't think this happens but let's be paranoid. */
12913 if (redisplaying_p)
12914 return;
12916 /* Record a function that resets redisplaying_p to its old value
12917 when we leave this function. */
12918 count = SPECPDL_INDEX ();
12919 record_unwind_protect (unwind_redisplay,
12920 Fcons (make_number (redisplaying_p), selected_frame));
12921 ++redisplaying_p;
12922 specbind (Qinhibit_free_realized_faces, Qnil);
12925 Lisp_Object tail, frame;
12927 FOR_EACH_FRAME (tail, frame)
12929 struct frame *f = XFRAME (frame);
12930 f->already_hscrolled_p = 0;
12934 retry:
12935 /* Remember the currently selected window. */
12936 sw = w;
12938 if (!EQ (old_frame, selected_frame)
12939 && FRAME_LIVE_P (XFRAME (old_frame)))
12940 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12941 selected_frame and selected_window to be temporarily out-of-sync so
12942 when we come back here via `goto retry', we need to resync because we
12943 may need to run Elisp code (via prepare_menu_bars). */
12944 select_frame_for_redisplay (old_frame);
12946 pending = 0;
12947 reconsider_clip_changes (w, current_buffer);
12948 last_escape_glyph_frame = NULL;
12949 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12950 last_glyphless_glyph_frame = NULL;
12951 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12953 /* If new fonts have been loaded that make a glyph matrix adjustment
12954 necessary, do it. */
12955 if (fonts_changed_p)
12957 adjust_glyphs (NULL);
12958 ++windows_or_buffers_changed;
12959 fonts_changed_p = 0;
12962 /* If face_change_count is non-zero, init_iterator will free all
12963 realized faces, which includes the faces referenced from current
12964 matrices. So, we can't reuse current matrices in this case. */
12965 if (face_change_count)
12966 ++windows_or_buffers_changed;
12968 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12969 && FRAME_TTY (sf)->previous_frame != sf)
12971 /* Since frames on a single ASCII terminal share the same
12972 display area, displaying a different frame means redisplay
12973 the whole thing. */
12974 windows_or_buffers_changed++;
12975 SET_FRAME_GARBAGED (sf);
12976 #ifndef DOS_NT
12977 set_tty_color_mode (FRAME_TTY (sf), sf);
12978 #endif
12979 FRAME_TTY (sf)->previous_frame = sf;
12982 /* Set the visible flags for all frames. Do this before checking
12983 for resized or garbaged frames; they want to know if their frames
12984 are visible. See the comment in frame.h for
12985 FRAME_SAMPLE_VISIBILITY. */
12987 Lisp_Object tail, frame;
12989 number_of_visible_frames = 0;
12991 FOR_EACH_FRAME (tail, frame)
12993 struct frame *f = XFRAME (frame);
12995 FRAME_SAMPLE_VISIBILITY (f);
12996 if (FRAME_VISIBLE_P (f))
12997 ++number_of_visible_frames;
12998 clear_desired_matrices (f);
13002 /* Notice any pending interrupt request to change frame size. */
13003 do_pending_window_change (1);
13005 /* do_pending_window_change could change the selected_window due to
13006 frame resizing which makes the selected window too small. */
13007 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13009 sw = w;
13010 reconsider_clip_changes (w, current_buffer);
13013 /* Clear frames marked as garbaged. */
13014 if (frame_garbaged)
13015 clear_garbaged_frames ();
13017 /* Build menubar and tool-bar items. */
13018 if (NILP (Vmemory_full))
13019 prepare_menu_bars ();
13021 if (windows_or_buffers_changed)
13022 update_mode_lines++;
13024 /* Detect case that we need to write or remove a star in the mode line. */
13025 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13027 w->update_mode_line = 1;
13028 if (buffer_shared > 1)
13029 update_mode_lines++;
13032 /* Avoid invocation of point motion hooks by `current_column' below. */
13033 count1 = SPECPDL_INDEX ();
13034 specbind (Qinhibit_point_motion_hooks, Qt);
13036 /* If %c is in the mode line, update it if needed. */
13037 if (!NILP (w->column_number_displayed)
13038 /* This alternative quickly identifies a common case
13039 where no change is needed. */
13040 && !(PT == w->last_point
13041 && w->last_modified >= MODIFF
13042 && w->last_overlay_modified >= OVERLAY_MODIFF)
13043 && (XFASTINT (w->column_number_displayed) != current_column ()))
13044 w->update_mode_line = 1;
13046 unbind_to (count1, Qnil);
13048 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13050 /* The variable buffer_shared is set in redisplay_window and
13051 indicates that we redisplay a buffer in different windows. See
13052 there. */
13053 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13054 || cursor_type_changed);
13056 /* If specs for an arrow have changed, do thorough redisplay
13057 to ensure we remove any arrow that should no longer exist. */
13058 if (overlay_arrows_changed_p ())
13059 consider_all_windows_p = windows_or_buffers_changed = 1;
13061 /* Normally the message* functions will have already displayed and
13062 updated the echo area, but the frame may have been trashed, or
13063 the update may have been preempted, so display the echo area
13064 again here. Checking message_cleared_p captures the case that
13065 the echo area should be cleared. */
13066 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13067 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13068 || (message_cleared_p
13069 && minibuf_level == 0
13070 /* If the mini-window is currently selected, this means the
13071 echo-area doesn't show through. */
13072 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13074 int window_height_changed_p = echo_area_display (0);
13076 if (message_cleared_p)
13077 update_miniwindow_p = 1;
13079 must_finish = 1;
13081 /* If we don't display the current message, don't clear the
13082 message_cleared_p flag, because, if we did, we wouldn't clear
13083 the echo area in the next redisplay which doesn't preserve
13084 the echo area. */
13085 if (!display_last_displayed_message_p)
13086 message_cleared_p = 0;
13088 if (fonts_changed_p)
13089 goto retry;
13090 else if (window_height_changed_p)
13092 consider_all_windows_p = 1;
13093 ++update_mode_lines;
13094 ++windows_or_buffers_changed;
13096 /* If window configuration was changed, frames may have been
13097 marked garbaged. Clear them or we will experience
13098 surprises wrt scrolling. */
13099 if (frame_garbaged)
13100 clear_garbaged_frames ();
13103 else if (EQ (selected_window, minibuf_window)
13104 && (current_buffer->clip_changed
13105 || w->last_modified < MODIFF
13106 || w->last_overlay_modified < OVERLAY_MODIFF)
13107 && resize_mini_window (w, 0))
13109 /* Resized active mini-window to fit the size of what it is
13110 showing if its contents might have changed. */
13111 must_finish = 1;
13112 /* FIXME: this causes all frames to be updated, which seems unnecessary
13113 since only the current frame needs to be considered. This function needs
13114 to be rewritten with two variables, consider_all_windows and
13115 consider_all_frames. */
13116 consider_all_windows_p = 1;
13117 ++windows_or_buffers_changed;
13118 ++update_mode_lines;
13120 /* If window configuration was changed, frames may have been
13121 marked garbaged. Clear them or we will experience
13122 surprises wrt scrolling. */
13123 if (frame_garbaged)
13124 clear_garbaged_frames ();
13128 /* If showing the region, and mark has changed, we must redisplay
13129 the whole window. The assignment to this_line_start_pos prevents
13130 the optimization directly below this if-statement. */
13131 if (((!NILP (Vtransient_mark_mode)
13132 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13133 != !NILP (w->region_showing))
13134 || (!NILP (w->region_showing)
13135 && !EQ (w->region_showing,
13136 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13137 CHARPOS (this_line_start_pos) = 0;
13139 /* Optimize the case that only the line containing the cursor in the
13140 selected window has changed. Variables starting with this_ are
13141 set in display_line and record information about the line
13142 containing the cursor. */
13143 tlbufpos = this_line_start_pos;
13144 tlendpos = this_line_end_pos;
13145 if (!consider_all_windows_p
13146 && CHARPOS (tlbufpos) > 0
13147 && !w->update_mode_line
13148 && !current_buffer->clip_changed
13149 && !current_buffer->prevent_redisplay_optimizations_p
13150 && FRAME_VISIBLE_P (XFRAME (w->frame))
13151 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13152 /* Make sure recorded data applies to current buffer, etc. */
13153 && this_line_buffer == current_buffer
13154 && current_buffer == XBUFFER (w->buffer)
13155 && !w->force_start
13156 && !w->optional_new_start
13157 /* Point must be on the line that we have info recorded about. */
13158 && PT >= CHARPOS (tlbufpos)
13159 && PT <= Z - CHARPOS (tlendpos)
13160 /* All text outside that line, including its final newline,
13161 must be unchanged. */
13162 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13163 CHARPOS (tlendpos)))
13165 if (CHARPOS (tlbufpos) > BEGV
13166 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13167 && (CHARPOS (tlbufpos) == ZV
13168 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13169 /* Former continuation line has disappeared by becoming empty. */
13170 goto cancel;
13171 else if (w->last_modified < MODIFF
13172 || w->last_overlay_modified < OVERLAY_MODIFF
13173 || MINI_WINDOW_P (w))
13175 /* We have to handle the case of continuation around a
13176 wide-column character (see the comment in indent.c around
13177 line 1340).
13179 For instance, in the following case:
13181 -------- Insert --------
13182 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13183 J_I_ ==> J_I_ `^^' are cursors.
13184 ^^ ^^
13185 -------- --------
13187 As we have to redraw the line above, we cannot use this
13188 optimization. */
13190 struct it it;
13191 int line_height_before = this_line_pixel_height;
13193 /* Note that start_display will handle the case that the
13194 line starting at tlbufpos is a continuation line. */
13195 start_display (&it, w, tlbufpos);
13197 /* Implementation note: It this still necessary? */
13198 if (it.current_x != this_line_start_x)
13199 goto cancel;
13201 TRACE ((stderr, "trying display optimization 1\n"));
13202 w->cursor.vpos = -1;
13203 overlay_arrow_seen = 0;
13204 it.vpos = this_line_vpos;
13205 it.current_y = this_line_y;
13206 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13207 display_line (&it);
13209 /* If line contains point, is not continued,
13210 and ends at same distance from eob as before, we win. */
13211 if (w->cursor.vpos >= 0
13212 /* Line is not continued, otherwise this_line_start_pos
13213 would have been set to 0 in display_line. */
13214 && CHARPOS (this_line_start_pos)
13215 /* Line ends as before. */
13216 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13217 /* Line has same height as before. Otherwise other lines
13218 would have to be shifted up or down. */
13219 && this_line_pixel_height == line_height_before)
13221 /* If this is not the window's last line, we must adjust
13222 the charstarts of the lines below. */
13223 if (it.current_y < it.last_visible_y)
13225 struct glyph_row *row
13226 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13227 ptrdiff_t delta, delta_bytes;
13229 /* We used to distinguish between two cases here,
13230 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13231 when the line ends in a newline or the end of the
13232 buffer's accessible portion. But both cases did
13233 the same, so they were collapsed. */
13234 delta = (Z
13235 - CHARPOS (tlendpos)
13236 - MATRIX_ROW_START_CHARPOS (row));
13237 delta_bytes = (Z_BYTE
13238 - BYTEPOS (tlendpos)
13239 - MATRIX_ROW_START_BYTEPOS (row));
13241 increment_matrix_positions (w->current_matrix,
13242 this_line_vpos + 1,
13243 w->current_matrix->nrows,
13244 delta, delta_bytes);
13247 /* If this row displays text now but previously didn't,
13248 or vice versa, w->window_end_vpos may have to be
13249 adjusted. */
13250 if ((it.glyph_row - 1)->displays_text_p)
13252 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13253 XSETINT (w->window_end_vpos, this_line_vpos);
13255 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13256 && this_line_vpos > 0)
13257 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13258 w->window_end_valid = Qnil;
13260 /* Update hint: No need to try to scroll in update_window. */
13261 w->desired_matrix->no_scrolling_p = 1;
13263 #ifdef GLYPH_DEBUG
13264 *w->desired_matrix->method = 0;
13265 debug_method_add (w, "optimization 1");
13266 #endif
13267 #ifdef HAVE_WINDOW_SYSTEM
13268 update_window_fringes (w, 0);
13269 #endif
13270 goto update;
13272 else
13273 goto cancel;
13275 else if (/* Cursor position hasn't changed. */
13276 PT == w->last_point
13277 /* Make sure the cursor was last displayed
13278 in this window. Otherwise we have to reposition it. */
13279 && 0 <= w->cursor.vpos
13280 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13282 if (!must_finish)
13284 do_pending_window_change (1);
13285 /* If selected_window changed, redisplay again. */
13286 if (WINDOWP (selected_window)
13287 && (w = XWINDOW (selected_window)) != sw)
13288 goto retry;
13290 /* We used to always goto end_of_redisplay here, but this
13291 isn't enough if we have a blinking cursor. */
13292 if (w->cursor_off_p == w->last_cursor_off_p)
13293 goto end_of_redisplay;
13295 goto update;
13297 /* If highlighting the region, or if the cursor is in the echo area,
13298 then we can't just move the cursor. */
13299 else if (! (!NILP (Vtransient_mark_mode)
13300 && !NILP (BVAR (current_buffer, mark_active)))
13301 && (EQ (selected_window,
13302 BVAR (current_buffer, last_selected_window))
13303 || highlight_nonselected_windows)
13304 && NILP (w->region_showing)
13305 && NILP (Vshow_trailing_whitespace)
13306 && !cursor_in_echo_area)
13308 struct it it;
13309 struct glyph_row *row;
13311 /* Skip from tlbufpos to PT and see where it is. Note that
13312 PT may be in invisible text. If so, we will end at the
13313 next visible position. */
13314 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13315 NULL, DEFAULT_FACE_ID);
13316 it.current_x = this_line_start_x;
13317 it.current_y = this_line_y;
13318 it.vpos = this_line_vpos;
13320 /* The call to move_it_to stops in front of PT, but
13321 moves over before-strings. */
13322 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13324 if (it.vpos == this_line_vpos
13325 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13326 row->enabled_p))
13328 eassert (this_line_vpos == it.vpos);
13329 eassert (this_line_y == it.current_y);
13330 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13331 #ifdef GLYPH_DEBUG
13332 *w->desired_matrix->method = 0;
13333 debug_method_add (w, "optimization 3");
13334 #endif
13335 goto update;
13337 else
13338 goto cancel;
13341 cancel:
13342 /* Text changed drastically or point moved off of line. */
13343 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13346 CHARPOS (this_line_start_pos) = 0;
13347 consider_all_windows_p |= buffer_shared > 1;
13348 ++clear_face_cache_count;
13349 #ifdef HAVE_WINDOW_SYSTEM
13350 ++clear_image_cache_count;
13351 #endif
13353 /* Build desired matrices, and update the display. If
13354 consider_all_windows_p is non-zero, do it for all windows on all
13355 frames. Otherwise do it for selected_window, only. */
13357 if (consider_all_windows_p)
13359 Lisp_Object tail, frame;
13361 FOR_EACH_FRAME (tail, frame)
13362 XFRAME (frame)->updated_p = 0;
13364 /* Recompute # windows showing selected buffer. This will be
13365 incremented each time such a window is displayed. */
13366 buffer_shared = 0;
13368 FOR_EACH_FRAME (tail, frame)
13370 struct frame *f = XFRAME (frame);
13372 /* We don't have to do anything for unselected terminal
13373 frames. */
13374 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13375 && !EQ (FRAME_TTY (f)->top_frame, frame))
13376 continue;
13378 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13380 if (! EQ (frame, selected_frame))
13381 /* Select the frame, for the sake of frame-local
13382 variables. */
13383 select_frame_for_redisplay (frame);
13385 /* Mark all the scroll bars to be removed; we'll redeem
13386 the ones we want when we redisplay their windows. */
13387 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13388 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13390 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13391 redisplay_windows (FRAME_ROOT_WINDOW (f));
13393 /* The X error handler may have deleted that frame. */
13394 if (!FRAME_LIVE_P (f))
13395 continue;
13397 /* Any scroll bars which redisplay_windows should have
13398 nuked should now go away. */
13399 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13400 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13402 /* If fonts changed, display again. */
13403 /* ??? rms: I suspect it is a mistake to jump all the way
13404 back to retry here. It should just retry this frame. */
13405 if (fonts_changed_p)
13406 goto retry;
13408 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13410 /* See if we have to hscroll. */
13411 if (!f->already_hscrolled_p)
13413 f->already_hscrolled_p = 1;
13414 if (hscroll_windows (f->root_window))
13415 goto retry;
13418 /* Prevent various kinds of signals during display
13419 update. stdio is not robust about handling
13420 signals, which can cause an apparent I/O
13421 error. */
13422 if (interrupt_input)
13423 unrequest_sigio ();
13424 STOP_POLLING;
13426 /* Update the display. */
13427 set_window_update_flags (XWINDOW (f->root_window), 1);
13428 pending |= update_frame (f, 0, 0);
13429 f->updated_p = 1;
13434 if (!EQ (old_frame, selected_frame)
13435 && FRAME_LIVE_P (XFRAME (old_frame)))
13436 /* We played a bit fast-and-loose above and allowed selected_frame
13437 and selected_window to be temporarily out-of-sync but let's make
13438 sure this stays contained. */
13439 select_frame_for_redisplay (old_frame);
13440 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13442 if (!pending)
13444 /* Do the mark_window_display_accurate after all windows have
13445 been redisplayed because this call resets flags in buffers
13446 which are needed for proper redisplay. */
13447 FOR_EACH_FRAME (tail, frame)
13449 struct frame *f = XFRAME (frame);
13450 if (f->updated_p)
13452 mark_window_display_accurate (f->root_window, 1);
13453 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13454 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13459 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13461 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13462 struct frame *mini_frame;
13464 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13465 /* Use list_of_error, not Qerror, so that
13466 we catch only errors and don't run the debugger. */
13467 internal_condition_case_1 (redisplay_window_1, selected_window,
13468 list_of_error,
13469 redisplay_window_error);
13470 if (update_miniwindow_p)
13471 internal_condition_case_1 (redisplay_window_1, mini_window,
13472 list_of_error,
13473 redisplay_window_error);
13475 /* Compare desired and current matrices, perform output. */
13477 update:
13478 /* If fonts changed, display again. */
13479 if (fonts_changed_p)
13480 goto retry;
13482 /* Prevent various kinds of signals during display update.
13483 stdio is not robust about handling signals,
13484 which can cause an apparent I/O error. */
13485 if (interrupt_input)
13486 unrequest_sigio ();
13487 STOP_POLLING;
13489 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13491 if (hscroll_windows (selected_window))
13492 goto retry;
13494 XWINDOW (selected_window)->must_be_updated_p = 1;
13495 pending = update_frame (sf, 0, 0);
13498 /* We may have called echo_area_display at the top of this
13499 function. If the echo area is on another frame, that may
13500 have put text on a frame other than the selected one, so the
13501 above call to update_frame would not have caught it. Catch
13502 it here. */
13503 mini_window = FRAME_MINIBUF_WINDOW (sf);
13504 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13506 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13508 XWINDOW (mini_window)->must_be_updated_p = 1;
13509 pending |= update_frame (mini_frame, 0, 0);
13510 if (!pending && hscroll_windows (mini_window))
13511 goto retry;
13515 /* If display was paused because of pending input, make sure we do a
13516 thorough update the next time. */
13517 if (pending)
13519 /* Prevent the optimization at the beginning of
13520 redisplay_internal that tries a single-line update of the
13521 line containing the cursor in the selected window. */
13522 CHARPOS (this_line_start_pos) = 0;
13524 /* Let the overlay arrow be updated the next time. */
13525 update_overlay_arrows (0);
13527 /* If we pause after scrolling, some rows in the current
13528 matrices of some windows are not valid. */
13529 if (!WINDOW_FULL_WIDTH_P (w)
13530 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13531 update_mode_lines = 1;
13533 else
13535 if (!consider_all_windows_p)
13537 /* This has already been done above if
13538 consider_all_windows_p is set. */
13539 mark_window_display_accurate_1 (w, 1);
13541 /* Say overlay arrows are up to date. */
13542 update_overlay_arrows (1);
13544 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13545 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13548 update_mode_lines = 0;
13549 windows_or_buffers_changed = 0;
13550 cursor_type_changed = 0;
13553 /* Start SIGIO interrupts coming again. Having them off during the
13554 code above makes it less likely one will discard output, but not
13555 impossible, since there might be stuff in the system buffer here.
13556 But it is much hairier to try to do anything about that. */
13557 if (interrupt_input)
13558 request_sigio ();
13559 RESUME_POLLING;
13561 /* If a frame has become visible which was not before, redisplay
13562 again, so that we display it. Expose events for such a frame
13563 (which it gets when becoming visible) don't call the parts of
13564 redisplay constructing glyphs, so simply exposing a frame won't
13565 display anything in this case. So, we have to display these
13566 frames here explicitly. */
13567 if (!pending)
13569 Lisp_Object tail, frame;
13570 int new_count = 0;
13572 FOR_EACH_FRAME (tail, frame)
13574 int this_is_visible = 0;
13576 if (XFRAME (frame)->visible)
13577 this_is_visible = 1;
13578 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13579 if (XFRAME (frame)->visible)
13580 this_is_visible = 1;
13582 if (this_is_visible)
13583 new_count++;
13586 if (new_count != number_of_visible_frames)
13587 windows_or_buffers_changed++;
13590 /* Change frame size now if a change is pending. */
13591 do_pending_window_change (1);
13593 /* If we just did a pending size change, or have additional
13594 visible frames, or selected_window changed, redisplay again. */
13595 if ((windows_or_buffers_changed && !pending)
13596 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13597 goto retry;
13599 /* Clear the face and image caches.
13601 We used to do this only if consider_all_windows_p. But the cache
13602 needs to be cleared if a timer creates images in the current
13603 buffer (e.g. the test case in Bug#6230). */
13605 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13607 clear_face_cache (0);
13608 clear_face_cache_count = 0;
13611 #ifdef HAVE_WINDOW_SYSTEM
13612 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13614 clear_image_caches (Qnil);
13615 clear_image_cache_count = 0;
13617 #endif /* HAVE_WINDOW_SYSTEM */
13619 end_of_redisplay:
13620 unbind_to (count, Qnil);
13621 RESUME_POLLING;
13625 /* Redisplay, but leave alone any recent echo area message unless
13626 another message has been requested in its place.
13628 This is useful in situations where you need to redisplay but no
13629 user action has occurred, making it inappropriate for the message
13630 area to be cleared. See tracking_off and
13631 wait_reading_process_output for examples of these situations.
13633 FROM_WHERE is an integer saying from where this function was
13634 called. This is useful for debugging. */
13636 void
13637 redisplay_preserve_echo_area (int from_where)
13639 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13641 if (!NILP (echo_area_buffer[1]))
13643 /* We have a previously displayed message, but no current
13644 message. Redisplay the previous message. */
13645 display_last_displayed_message_p = 1;
13646 redisplay_internal ();
13647 display_last_displayed_message_p = 0;
13649 else
13650 redisplay_internal ();
13652 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13653 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13654 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13658 /* Function registered with record_unwind_protect in
13659 redisplay_internal. Reset redisplaying_p to the value it had
13660 before redisplay_internal was called, and clear
13661 prevent_freeing_realized_faces_p. It also selects the previously
13662 selected frame, unless it has been deleted (by an X connection
13663 failure during redisplay, for example). */
13665 static Lisp_Object
13666 unwind_redisplay (Lisp_Object val)
13668 Lisp_Object old_redisplaying_p, old_frame;
13670 old_redisplaying_p = XCAR (val);
13671 redisplaying_p = XFASTINT (old_redisplaying_p);
13672 old_frame = XCDR (val);
13673 if (! EQ (old_frame, selected_frame)
13674 && FRAME_LIVE_P (XFRAME (old_frame)))
13675 select_frame_for_redisplay (old_frame);
13676 return Qnil;
13680 /* Mark the display of window W as accurate or inaccurate. If
13681 ACCURATE_P is non-zero mark display of W as accurate. If
13682 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13683 redisplay_internal is called. */
13685 static void
13686 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13688 if (BUFFERP (w->buffer))
13690 struct buffer *b = XBUFFER (w->buffer);
13692 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13693 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13694 w->last_had_star
13695 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13697 if (accurate_p)
13699 b->clip_changed = 0;
13700 b->prevent_redisplay_optimizations_p = 0;
13702 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13703 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13704 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13705 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13707 w->current_matrix->buffer = b;
13708 w->current_matrix->begv = BUF_BEGV (b);
13709 w->current_matrix->zv = BUF_ZV (b);
13711 w->last_cursor = w->cursor;
13712 w->last_cursor_off_p = w->cursor_off_p;
13714 if (w == XWINDOW (selected_window))
13715 w->last_point = BUF_PT (b);
13716 else
13717 w->last_point = XMARKER (w->pointm)->charpos;
13721 if (accurate_p)
13723 w->window_end_valid = w->buffer;
13724 w->update_mode_line = 0;
13729 /* Mark the display of windows in the window tree rooted at WINDOW as
13730 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13731 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13732 be redisplayed the next time redisplay_internal is called. */
13734 void
13735 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13737 struct window *w;
13739 for (; !NILP (window); window = w->next)
13741 w = XWINDOW (window);
13742 mark_window_display_accurate_1 (w, accurate_p);
13744 if (!NILP (w->vchild))
13745 mark_window_display_accurate (w->vchild, accurate_p);
13746 if (!NILP (w->hchild))
13747 mark_window_display_accurate (w->hchild, accurate_p);
13750 if (accurate_p)
13752 update_overlay_arrows (1);
13754 else
13756 /* Force a thorough redisplay the next time by setting
13757 last_arrow_position and last_arrow_string to t, which is
13758 unequal to any useful value of Voverlay_arrow_... */
13759 update_overlay_arrows (-1);
13764 /* Return value in display table DP (Lisp_Char_Table *) for character
13765 C. Since a display table doesn't have any parent, we don't have to
13766 follow parent. Do not call this function directly but use the
13767 macro DISP_CHAR_VECTOR. */
13769 Lisp_Object
13770 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13772 Lisp_Object val;
13774 if (ASCII_CHAR_P (c))
13776 val = dp->ascii;
13777 if (SUB_CHAR_TABLE_P (val))
13778 val = XSUB_CHAR_TABLE (val)->contents[c];
13780 else
13782 Lisp_Object table;
13784 XSETCHAR_TABLE (table, dp);
13785 val = char_table_ref (table, c);
13787 if (NILP (val))
13788 val = dp->defalt;
13789 return val;
13794 /***********************************************************************
13795 Window Redisplay
13796 ***********************************************************************/
13798 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13800 static void
13801 redisplay_windows (Lisp_Object window)
13803 while (!NILP (window))
13805 struct window *w = XWINDOW (window);
13807 if (!NILP (w->hchild))
13808 redisplay_windows (w->hchild);
13809 else if (!NILP (w->vchild))
13810 redisplay_windows (w->vchild);
13811 else if (!NILP (w->buffer))
13813 displayed_buffer = XBUFFER (w->buffer);
13814 /* Use list_of_error, not Qerror, so that
13815 we catch only errors and don't run the debugger. */
13816 internal_condition_case_1 (redisplay_window_0, window,
13817 list_of_error,
13818 redisplay_window_error);
13821 window = w->next;
13825 static Lisp_Object
13826 redisplay_window_error (Lisp_Object ignore)
13828 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13829 return Qnil;
13832 static Lisp_Object
13833 redisplay_window_0 (Lisp_Object window)
13835 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13836 redisplay_window (window, 0);
13837 return Qnil;
13840 static Lisp_Object
13841 redisplay_window_1 (Lisp_Object window)
13843 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13844 redisplay_window (window, 1);
13845 return Qnil;
13849 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13850 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13851 which positions recorded in ROW differ from current buffer
13852 positions.
13854 Return 0 if cursor is not on this row, 1 otherwise. */
13856 static int
13857 set_cursor_from_row (struct window *w, struct glyph_row *row,
13858 struct glyph_matrix *matrix,
13859 ptrdiff_t delta, ptrdiff_t delta_bytes,
13860 int dy, int dvpos)
13862 struct glyph *glyph = row->glyphs[TEXT_AREA];
13863 struct glyph *end = glyph + row->used[TEXT_AREA];
13864 struct glyph *cursor = NULL;
13865 /* The last known character position in row. */
13866 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13867 int x = row->x;
13868 ptrdiff_t pt_old = PT - delta;
13869 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13870 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13871 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13872 /* A glyph beyond the edge of TEXT_AREA which we should never
13873 touch. */
13874 struct glyph *glyphs_end = end;
13875 /* Non-zero means we've found a match for cursor position, but that
13876 glyph has the avoid_cursor_p flag set. */
13877 int match_with_avoid_cursor = 0;
13878 /* Non-zero means we've seen at least one glyph that came from a
13879 display string. */
13880 int string_seen = 0;
13881 /* Largest and smallest buffer positions seen so far during scan of
13882 glyph row. */
13883 ptrdiff_t bpos_max = pos_before;
13884 ptrdiff_t bpos_min = pos_after;
13885 /* Last buffer position covered by an overlay string with an integer
13886 `cursor' property. */
13887 ptrdiff_t bpos_covered = 0;
13888 /* Non-zero means the display string on which to display the cursor
13889 comes from a text property, not from an overlay. */
13890 int string_from_text_prop = 0;
13892 /* Don't even try doing anything if called for a mode-line or
13893 header-line row, since the rest of the code isn't prepared to
13894 deal with such calamities. */
13895 eassert (!row->mode_line_p);
13896 if (row->mode_line_p)
13897 return 0;
13899 /* Skip over glyphs not having an object at the start and the end of
13900 the row. These are special glyphs like truncation marks on
13901 terminal frames. */
13902 if (row->displays_text_p)
13904 if (!row->reversed_p)
13906 while (glyph < end
13907 && INTEGERP (glyph->object)
13908 && glyph->charpos < 0)
13910 x += glyph->pixel_width;
13911 ++glyph;
13913 while (end > glyph
13914 && INTEGERP ((end - 1)->object)
13915 /* CHARPOS is zero for blanks and stretch glyphs
13916 inserted by extend_face_to_end_of_line. */
13917 && (end - 1)->charpos <= 0)
13918 --end;
13919 glyph_before = glyph - 1;
13920 glyph_after = end;
13922 else
13924 struct glyph *g;
13926 /* If the glyph row is reversed, we need to process it from back
13927 to front, so swap the edge pointers. */
13928 glyphs_end = end = glyph - 1;
13929 glyph += row->used[TEXT_AREA] - 1;
13931 while (glyph > end + 1
13932 && INTEGERP (glyph->object)
13933 && glyph->charpos < 0)
13935 --glyph;
13936 x -= glyph->pixel_width;
13938 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13939 --glyph;
13940 /* By default, in reversed rows we put the cursor on the
13941 rightmost (first in the reading order) glyph. */
13942 for (g = end + 1; g < glyph; g++)
13943 x += g->pixel_width;
13944 while (end < glyph
13945 && INTEGERP ((end + 1)->object)
13946 && (end + 1)->charpos <= 0)
13947 ++end;
13948 glyph_before = glyph + 1;
13949 glyph_after = end;
13952 else if (row->reversed_p)
13954 /* In R2L rows that don't display text, put the cursor on the
13955 rightmost glyph. Case in point: an empty last line that is
13956 part of an R2L paragraph. */
13957 cursor = end - 1;
13958 /* Avoid placing the cursor on the last glyph of the row, where
13959 on terminal frames we hold the vertical border between
13960 adjacent windows. */
13961 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13962 && !WINDOW_RIGHTMOST_P (w)
13963 && cursor == row->glyphs[LAST_AREA] - 1)
13964 cursor--;
13965 x = -1; /* will be computed below, at label compute_x */
13968 /* Step 1: Try to find the glyph whose character position
13969 corresponds to point. If that's not possible, find 2 glyphs
13970 whose character positions are the closest to point, one before
13971 point, the other after it. */
13972 if (!row->reversed_p)
13973 while (/* not marched to end of glyph row */
13974 glyph < end
13975 /* glyph was not inserted by redisplay for internal purposes */
13976 && !INTEGERP (glyph->object))
13978 if (BUFFERP (glyph->object))
13980 ptrdiff_t dpos = glyph->charpos - pt_old;
13982 if (glyph->charpos > bpos_max)
13983 bpos_max = glyph->charpos;
13984 if (glyph->charpos < bpos_min)
13985 bpos_min = glyph->charpos;
13986 if (!glyph->avoid_cursor_p)
13988 /* If we hit point, we've found the glyph on which to
13989 display the cursor. */
13990 if (dpos == 0)
13992 match_with_avoid_cursor = 0;
13993 break;
13995 /* See if we've found a better approximation to
13996 POS_BEFORE or to POS_AFTER. */
13997 if (0 > dpos && dpos > pos_before - pt_old)
13999 pos_before = glyph->charpos;
14000 glyph_before = glyph;
14002 else if (0 < dpos && dpos < pos_after - pt_old)
14004 pos_after = glyph->charpos;
14005 glyph_after = glyph;
14008 else if (dpos == 0)
14009 match_with_avoid_cursor = 1;
14011 else if (STRINGP (glyph->object))
14013 Lisp_Object chprop;
14014 ptrdiff_t glyph_pos = glyph->charpos;
14016 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14017 glyph->object);
14018 if (!NILP (chprop))
14020 /* If the string came from a `display' text property,
14021 look up the buffer position of that property and
14022 use that position to update bpos_max, as if we
14023 actually saw such a position in one of the row's
14024 glyphs. This helps with supporting integer values
14025 of `cursor' property on the display string in
14026 situations where most or all of the row's buffer
14027 text is completely covered by display properties,
14028 so that no glyph with valid buffer positions is
14029 ever seen in the row. */
14030 ptrdiff_t prop_pos =
14031 string_buffer_position_lim (glyph->object, pos_before,
14032 pos_after, 0);
14034 if (prop_pos >= pos_before)
14035 bpos_max = prop_pos - 1;
14037 if (INTEGERP (chprop))
14039 bpos_covered = bpos_max + XINT (chprop);
14040 /* If the `cursor' property covers buffer positions up
14041 to and including point, we should display cursor on
14042 this glyph. Note that, if a `cursor' property on one
14043 of the string's characters has an integer value, we
14044 will break out of the loop below _before_ we get to
14045 the position match above. IOW, integer values of
14046 the `cursor' property override the "exact match for
14047 point" strategy of positioning the cursor. */
14048 /* Implementation note: bpos_max == pt_old when, e.g.,
14049 we are in an empty line, where bpos_max is set to
14050 MATRIX_ROW_START_CHARPOS, see above. */
14051 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14053 cursor = glyph;
14054 break;
14058 string_seen = 1;
14060 x += glyph->pixel_width;
14061 ++glyph;
14063 else if (glyph > end) /* row is reversed */
14064 while (!INTEGERP (glyph->object))
14066 if (BUFFERP (glyph->object))
14068 ptrdiff_t dpos = glyph->charpos - pt_old;
14070 if (glyph->charpos > bpos_max)
14071 bpos_max = glyph->charpos;
14072 if (glyph->charpos < bpos_min)
14073 bpos_min = glyph->charpos;
14074 if (!glyph->avoid_cursor_p)
14076 if (dpos == 0)
14078 match_with_avoid_cursor = 0;
14079 break;
14081 if (0 > dpos && dpos > pos_before - pt_old)
14083 pos_before = glyph->charpos;
14084 glyph_before = glyph;
14086 else if (0 < dpos && dpos < pos_after - pt_old)
14088 pos_after = glyph->charpos;
14089 glyph_after = glyph;
14092 else if (dpos == 0)
14093 match_with_avoid_cursor = 1;
14095 else if (STRINGP (glyph->object))
14097 Lisp_Object chprop;
14098 ptrdiff_t glyph_pos = glyph->charpos;
14100 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14101 glyph->object);
14102 if (!NILP (chprop))
14104 ptrdiff_t prop_pos =
14105 string_buffer_position_lim (glyph->object, pos_before,
14106 pos_after, 0);
14108 if (prop_pos >= pos_before)
14109 bpos_max = prop_pos - 1;
14111 if (INTEGERP (chprop))
14113 bpos_covered = bpos_max + XINT (chprop);
14114 /* If the `cursor' property covers buffer positions up
14115 to and including point, we should display cursor on
14116 this glyph. */
14117 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14119 cursor = glyph;
14120 break;
14123 string_seen = 1;
14125 --glyph;
14126 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14128 x--; /* can't use any pixel_width */
14129 break;
14131 x -= glyph->pixel_width;
14134 /* Step 2: If we didn't find an exact match for point, we need to
14135 look for a proper place to put the cursor among glyphs between
14136 GLYPH_BEFORE and GLYPH_AFTER. */
14137 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14138 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14139 && bpos_covered < pt_old)
14141 /* An empty line has a single glyph whose OBJECT is zero and
14142 whose CHARPOS is the position of a newline on that line.
14143 Note that on a TTY, there are more glyphs after that, which
14144 were produced by extend_face_to_end_of_line, but their
14145 CHARPOS is zero or negative. */
14146 int empty_line_p =
14147 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14148 && INTEGERP (glyph->object) && glyph->charpos > 0;
14150 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14152 ptrdiff_t ellipsis_pos;
14154 /* Scan back over the ellipsis glyphs. */
14155 if (!row->reversed_p)
14157 ellipsis_pos = (glyph - 1)->charpos;
14158 while (glyph > row->glyphs[TEXT_AREA]
14159 && (glyph - 1)->charpos == ellipsis_pos)
14160 glyph--, x -= glyph->pixel_width;
14161 /* That loop always goes one position too far, including
14162 the glyph before the ellipsis. So scan forward over
14163 that one. */
14164 x += glyph->pixel_width;
14165 glyph++;
14167 else /* row is reversed */
14169 ellipsis_pos = (glyph + 1)->charpos;
14170 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14171 && (glyph + 1)->charpos == ellipsis_pos)
14172 glyph++, x += glyph->pixel_width;
14173 x -= glyph->pixel_width;
14174 glyph--;
14177 else if (match_with_avoid_cursor)
14179 cursor = glyph_after;
14180 x = -1;
14182 else if (string_seen)
14184 int incr = row->reversed_p ? -1 : +1;
14186 /* Need to find the glyph that came out of a string which is
14187 present at point. That glyph is somewhere between
14188 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14189 positioned between POS_BEFORE and POS_AFTER in the
14190 buffer. */
14191 struct glyph *start, *stop;
14192 ptrdiff_t pos = pos_before;
14194 x = -1;
14196 /* If the row ends in a newline from a display string,
14197 reordering could have moved the glyphs belonging to the
14198 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14199 in this case we extend the search to the last glyph in
14200 the row that was not inserted by redisplay. */
14201 if (row->ends_in_newline_from_string_p)
14203 glyph_after = end;
14204 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14207 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14208 correspond to POS_BEFORE and POS_AFTER, respectively. We
14209 need START and STOP in the order that corresponds to the
14210 row's direction as given by its reversed_p flag. If the
14211 directionality of characters between POS_BEFORE and
14212 POS_AFTER is the opposite of the row's base direction,
14213 these characters will have been reordered for display,
14214 and we need to reverse START and STOP. */
14215 if (!row->reversed_p)
14217 start = min (glyph_before, glyph_after);
14218 stop = max (glyph_before, glyph_after);
14220 else
14222 start = max (glyph_before, glyph_after);
14223 stop = min (glyph_before, glyph_after);
14225 for (glyph = start + incr;
14226 row->reversed_p ? glyph > stop : glyph < stop; )
14229 /* Any glyphs that come from the buffer are here because
14230 of bidi reordering. Skip them, and only pay
14231 attention to glyphs that came from some string. */
14232 if (STRINGP (glyph->object))
14234 Lisp_Object str;
14235 ptrdiff_t tem;
14236 /* If the display property covers the newline, we
14237 need to search for it one position farther. */
14238 ptrdiff_t lim = pos_after
14239 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14241 string_from_text_prop = 0;
14242 str = glyph->object;
14243 tem = string_buffer_position_lim (str, pos, lim, 0);
14244 if (tem == 0 /* from overlay */
14245 || pos <= tem)
14247 /* If the string from which this glyph came is
14248 found in the buffer at point, or at position
14249 that is closer to point than pos_after, then
14250 we've found the glyph we've been looking for.
14251 If it comes from an overlay (tem == 0), and
14252 it has the `cursor' property on one of its
14253 glyphs, record that glyph as a candidate for
14254 displaying the cursor. (As in the
14255 unidirectional version, we will display the
14256 cursor on the last candidate we find.) */
14257 if (tem == 0
14258 || tem == pt_old
14259 || (tem - pt_old > 0 && tem < pos_after))
14261 /* The glyphs from this string could have
14262 been reordered. Find the one with the
14263 smallest string position. Or there could
14264 be a character in the string with the
14265 `cursor' property, which means display
14266 cursor on that character's glyph. */
14267 ptrdiff_t strpos = glyph->charpos;
14269 if (tem)
14271 cursor = glyph;
14272 string_from_text_prop = 1;
14274 for ( ;
14275 (row->reversed_p ? glyph > stop : glyph < stop)
14276 && EQ (glyph->object, str);
14277 glyph += incr)
14279 Lisp_Object cprop;
14280 ptrdiff_t gpos = glyph->charpos;
14282 cprop = Fget_char_property (make_number (gpos),
14283 Qcursor,
14284 glyph->object);
14285 if (!NILP (cprop))
14287 cursor = glyph;
14288 break;
14290 if (tem && glyph->charpos < strpos)
14292 strpos = glyph->charpos;
14293 cursor = glyph;
14297 if (tem == pt_old
14298 || (tem - pt_old > 0 && tem < pos_after))
14299 goto compute_x;
14301 if (tem)
14302 pos = tem + 1; /* don't find previous instances */
14304 /* This string is not what we want; skip all of the
14305 glyphs that came from it. */
14306 while ((row->reversed_p ? glyph > stop : glyph < stop)
14307 && EQ (glyph->object, str))
14308 glyph += incr;
14310 else
14311 glyph += incr;
14314 /* If we reached the end of the line, and END was from a string,
14315 the cursor is not on this line. */
14316 if (cursor == NULL
14317 && (row->reversed_p ? glyph <= end : glyph >= end)
14318 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14319 && STRINGP (end->object)
14320 && row->continued_p)
14321 return 0;
14323 /* A truncated row may not include PT among its character positions.
14324 Setting the cursor inside the scroll margin will trigger
14325 recalculation of hscroll in hscroll_window_tree. But if a
14326 display string covers point, defer to the string-handling
14327 code below to figure this out. */
14328 else if (row->truncated_on_left_p && pt_old < bpos_min)
14330 cursor = glyph_before;
14331 x = -1;
14333 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14334 /* Zero-width characters produce no glyphs. */
14335 || (!empty_line_p
14336 && (row->reversed_p
14337 ? glyph_after > glyphs_end
14338 : glyph_after < glyphs_end)))
14340 cursor = glyph_after;
14341 x = -1;
14345 compute_x:
14346 if (cursor != NULL)
14347 glyph = cursor;
14348 else if (glyph == glyphs_end
14349 && pos_before == pos_after
14350 && STRINGP ((row->reversed_p
14351 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14352 : row->glyphs[TEXT_AREA])->object))
14354 /* If all the glyphs of this row came from strings, put the
14355 cursor on the first glyph of the row. This avoids having the
14356 cursor outside of the text area in this very rare and hard
14357 use case. */
14358 glyph =
14359 row->reversed_p
14360 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14361 : row->glyphs[TEXT_AREA];
14363 if (x < 0)
14365 struct glyph *g;
14367 /* Need to compute x that corresponds to GLYPH. */
14368 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14370 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14371 abort ();
14372 x += g->pixel_width;
14376 /* ROW could be part of a continued line, which, under bidi
14377 reordering, might have other rows whose start and end charpos
14378 occlude point. Only set w->cursor if we found a better
14379 approximation to the cursor position than we have from previously
14380 examined candidate rows belonging to the same continued line. */
14381 if (/* we already have a candidate row */
14382 w->cursor.vpos >= 0
14383 /* that candidate is not the row we are processing */
14384 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14385 /* Make sure cursor.vpos specifies a row whose start and end
14386 charpos occlude point, and it is valid candidate for being a
14387 cursor-row. This is because some callers of this function
14388 leave cursor.vpos at the row where the cursor was displayed
14389 during the last redisplay cycle. */
14390 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14391 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14392 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14394 struct glyph *g1 =
14395 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14397 /* Don't consider glyphs that are outside TEXT_AREA. */
14398 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14399 return 0;
14400 /* Keep the candidate whose buffer position is the closest to
14401 point or has the `cursor' property. */
14402 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14403 w->cursor.hpos >= 0
14404 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14405 && ((BUFFERP (g1->object)
14406 && (g1->charpos == pt_old /* an exact match always wins */
14407 || (BUFFERP (glyph->object)
14408 && eabs (g1->charpos - pt_old)
14409 < eabs (glyph->charpos - pt_old))))
14410 /* previous candidate is a glyph from a string that has
14411 a non-nil `cursor' property */
14412 || (STRINGP (g1->object)
14413 && (!NILP (Fget_char_property (make_number (g1->charpos),
14414 Qcursor, g1->object))
14415 /* previous candidate is from the same display
14416 string as this one, and the display string
14417 came from a text property */
14418 || (EQ (g1->object, glyph->object)
14419 && string_from_text_prop)
14420 /* this candidate is from newline and its
14421 position is not an exact match */
14422 || (INTEGERP (glyph->object)
14423 && glyph->charpos != pt_old)))))
14424 return 0;
14425 /* If this candidate gives an exact match, use that. */
14426 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14427 /* If this candidate is a glyph created for the
14428 terminating newline of a line, and point is on that
14429 newline, it wins because it's an exact match. */
14430 || (!row->continued_p
14431 && INTEGERP (glyph->object)
14432 && glyph->charpos == 0
14433 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14434 /* Otherwise, keep the candidate that comes from a row
14435 spanning less buffer positions. This may win when one or
14436 both candidate positions are on glyphs that came from
14437 display strings, for which we cannot compare buffer
14438 positions. */
14439 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14440 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14441 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14442 return 0;
14444 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14445 w->cursor.x = x;
14446 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14447 w->cursor.y = row->y + dy;
14449 if (w == XWINDOW (selected_window))
14451 if (!row->continued_p
14452 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14453 && row->x == 0)
14455 this_line_buffer = XBUFFER (w->buffer);
14457 CHARPOS (this_line_start_pos)
14458 = MATRIX_ROW_START_CHARPOS (row) + delta;
14459 BYTEPOS (this_line_start_pos)
14460 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14462 CHARPOS (this_line_end_pos)
14463 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14464 BYTEPOS (this_line_end_pos)
14465 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14467 this_line_y = w->cursor.y;
14468 this_line_pixel_height = row->height;
14469 this_line_vpos = w->cursor.vpos;
14470 this_line_start_x = row->x;
14472 else
14473 CHARPOS (this_line_start_pos) = 0;
14476 return 1;
14480 /* Run window scroll functions, if any, for WINDOW with new window
14481 start STARTP. Sets the window start of WINDOW to that position.
14483 We assume that the window's buffer is really current. */
14485 static inline struct text_pos
14486 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14488 struct window *w = XWINDOW (window);
14489 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14491 if (current_buffer != XBUFFER (w->buffer))
14492 abort ();
14494 if (!NILP (Vwindow_scroll_functions))
14496 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14497 make_number (CHARPOS (startp)));
14498 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14499 /* In case the hook functions switch buffers. */
14500 if (current_buffer != XBUFFER (w->buffer))
14501 set_buffer_internal_1 (XBUFFER (w->buffer));
14504 return startp;
14508 /* Make sure the line containing the cursor is fully visible.
14509 A value of 1 means there is nothing to be done.
14510 (Either the line is fully visible, or it cannot be made so,
14511 or we cannot tell.)
14513 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14514 is higher than window.
14516 A value of 0 means the caller should do scrolling
14517 as if point had gone off the screen. */
14519 static int
14520 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14522 struct glyph_matrix *matrix;
14523 struct glyph_row *row;
14524 int window_height;
14526 if (!make_cursor_line_fully_visible_p)
14527 return 1;
14529 /* It's not always possible to find the cursor, e.g, when a window
14530 is full of overlay strings. Don't do anything in that case. */
14531 if (w->cursor.vpos < 0)
14532 return 1;
14534 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14535 row = MATRIX_ROW (matrix, w->cursor.vpos);
14537 /* If the cursor row is not partially visible, there's nothing to do. */
14538 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14539 return 1;
14541 /* If the row the cursor is in is taller than the window's height,
14542 it's not clear what to do, so do nothing. */
14543 window_height = window_box_height (w);
14544 if (row->height >= window_height)
14546 if (!force_p || MINI_WINDOW_P (w)
14547 || w->vscroll || w->cursor.vpos == 0)
14548 return 1;
14550 return 0;
14554 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14555 non-zero means only WINDOW is redisplayed in redisplay_internal.
14556 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14557 in redisplay_window to bring a partially visible line into view in
14558 the case that only the cursor has moved.
14560 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14561 last screen line's vertical height extends past the end of the screen.
14563 Value is
14565 1 if scrolling succeeded
14567 0 if scrolling didn't find point.
14569 -1 if new fonts have been loaded so that we must interrupt
14570 redisplay, adjust glyph matrices, and try again. */
14572 enum
14574 SCROLLING_SUCCESS,
14575 SCROLLING_FAILED,
14576 SCROLLING_NEED_LARGER_MATRICES
14579 /* If scroll-conservatively is more than this, never recenter.
14581 If you change this, don't forget to update the doc string of
14582 `scroll-conservatively' and the Emacs manual. */
14583 #define SCROLL_LIMIT 100
14585 static int
14586 try_scrolling (Lisp_Object window, int just_this_one_p,
14587 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14588 int temp_scroll_step, int last_line_misfit)
14590 struct window *w = XWINDOW (window);
14591 struct frame *f = XFRAME (w->frame);
14592 struct text_pos pos, startp;
14593 struct it it;
14594 int this_scroll_margin, scroll_max, rc, height;
14595 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14596 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14597 Lisp_Object aggressive;
14598 /* We will never try scrolling more than this number of lines. */
14599 int scroll_limit = SCROLL_LIMIT;
14601 #ifdef GLYPH_DEBUG
14602 debug_method_add (w, "try_scrolling");
14603 #endif
14605 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14607 /* Compute scroll margin height in pixels. We scroll when point is
14608 within this distance from the top or bottom of the window. */
14609 if (scroll_margin > 0)
14610 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14611 * FRAME_LINE_HEIGHT (f);
14612 else
14613 this_scroll_margin = 0;
14615 /* Force arg_scroll_conservatively to have a reasonable value, to
14616 avoid scrolling too far away with slow move_it_* functions. Note
14617 that the user can supply scroll-conservatively equal to
14618 `most-positive-fixnum', which can be larger than INT_MAX. */
14619 if (arg_scroll_conservatively > scroll_limit)
14621 arg_scroll_conservatively = scroll_limit + 1;
14622 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14624 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14625 /* Compute how much we should try to scroll maximally to bring
14626 point into view. */
14627 scroll_max = (max (scroll_step,
14628 max (arg_scroll_conservatively, temp_scroll_step))
14629 * FRAME_LINE_HEIGHT (f));
14630 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14631 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14632 /* We're trying to scroll because of aggressive scrolling but no
14633 scroll_step is set. Choose an arbitrary one. */
14634 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14635 else
14636 scroll_max = 0;
14638 too_near_end:
14640 /* Decide whether to scroll down. */
14641 if (PT > CHARPOS (startp))
14643 int scroll_margin_y;
14645 /* Compute the pixel ypos of the scroll margin, then move IT to
14646 either that ypos or PT, whichever comes first. */
14647 start_display (&it, w, startp);
14648 scroll_margin_y = it.last_visible_y - this_scroll_margin
14649 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14650 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14651 (MOVE_TO_POS | MOVE_TO_Y));
14653 if (PT > CHARPOS (it.current.pos))
14655 int y0 = line_bottom_y (&it);
14656 /* Compute how many pixels below window bottom to stop searching
14657 for PT. This avoids costly search for PT that is far away if
14658 the user limited scrolling by a small number of lines, but
14659 always finds PT if scroll_conservatively is set to a large
14660 number, such as most-positive-fixnum. */
14661 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14662 int y_to_move = it.last_visible_y + slack;
14664 /* Compute the distance from the scroll margin to PT or to
14665 the scroll limit, whichever comes first. This should
14666 include the height of the cursor line, to make that line
14667 fully visible. */
14668 move_it_to (&it, PT, -1, y_to_move,
14669 -1, MOVE_TO_POS | MOVE_TO_Y);
14670 dy = line_bottom_y (&it) - y0;
14672 if (dy > scroll_max)
14673 return SCROLLING_FAILED;
14675 if (dy > 0)
14676 scroll_down_p = 1;
14680 if (scroll_down_p)
14682 /* Point is in or below the bottom scroll margin, so move the
14683 window start down. If scrolling conservatively, move it just
14684 enough down to make point visible. If scroll_step is set,
14685 move it down by scroll_step. */
14686 if (arg_scroll_conservatively)
14687 amount_to_scroll
14688 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14689 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14690 else if (scroll_step || temp_scroll_step)
14691 amount_to_scroll = scroll_max;
14692 else
14694 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14695 height = WINDOW_BOX_TEXT_HEIGHT (w);
14696 if (NUMBERP (aggressive))
14698 double float_amount = XFLOATINT (aggressive) * height;
14699 amount_to_scroll = float_amount;
14700 if (amount_to_scroll == 0 && float_amount > 0)
14701 amount_to_scroll = 1;
14702 /* Don't let point enter the scroll margin near top of
14703 the window. */
14704 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14705 amount_to_scroll = height - 2*this_scroll_margin + dy;
14709 if (amount_to_scroll <= 0)
14710 return SCROLLING_FAILED;
14712 start_display (&it, w, startp);
14713 if (arg_scroll_conservatively <= scroll_limit)
14714 move_it_vertically (&it, amount_to_scroll);
14715 else
14717 /* Extra precision for users who set scroll-conservatively
14718 to a large number: make sure the amount we scroll
14719 the window start is never less than amount_to_scroll,
14720 which was computed as distance from window bottom to
14721 point. This matters when lines at window top and lines
14722 below window bottom have different height. */
14723 struct it it1;
14724 void *it1data = NULL;
14725 /* We use a temporary it1 because line_bottom_y can modify
14726 its argument, if it moves one line down; see there. */
14727 int start_y;
14729 SAVE_IT (it1, it, it1data);
14730 start_y = line_bottom_y (&it1);
14731 do {
14732 RESTORE_IT (&it, &it, it1data);
14733 move_it_by_lines (&it, 1);
14734 SAVE_IT (it1, it, it1data);
14735 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14738 /* If STARTP is unchanged, move it down another screen line. */
14739 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14740 move_it_by_lines (&it, 1);
14741 startp = it.current.pos;
14743 else
14745 struct text_pos scroll_margin_pos = startp;
14747 /* See if point is inside the scroll margin at the top of the
14748 window. */
14749 if (this_scroll_margin)
14751 start_display (&it, w, startp);
14752 move_it_vertically (&it, this_scroll_margin);
14753 scroll_margin_pos = it.current.pos;
14756 if (PT < CHARPOS (scroll_margin_pos))
14758 /* Point is in the scroll margin at the top of the window or
14759 above what is displayed in the window. */
14760 int y0, y_to_move;
14762 /* Compute the vertical distance from PT to the scroll
14763 margin position. Move as far as scroll_max allows, or
14764 one screenful, or 10 screen lines, whichever is largest.
14765 Give up if distance is greater than scroll_max. */
14766 SET_TEXT_POS (pos, PT, PT_BYTE);
14767 start_display (&it, w, pos);
14768 y0 = it.current_y;
14769 y_to_move = max (it.last_visible_y,
14770 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14771 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14772 y_to_move, -1,
14773 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14774 dy = it.current_y - y0;
14775 if (dy > scroll_max)
14776 return SCROLLING_FAILED;
14778 /* Compute new window start. */
14779 start_display (&it, w, startp);
14781 if (arg_scroll_conservatively)
14782 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14783 max (scroll_step, temp_scroll_step));
14784 else if (scroll_step || temp_scroll_step)
14785 amount_to_scroll = scroll_max;
14786 else
14788 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14789 height = WINDOW_BOX_TEXT_HEIGHT (w);
14790 if (NUMBERP (aggressive))
14792 double float_amount = XFLOATINT (aggressive) * height;
14793 amount_to_scroll = float_amount;
14794 if (amount_to_scroll == 0 && float_amount > 0)
14795 amount_to_scroll = 1;
14796 amount_to_scroll -=
14797 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14798 /* Don't let point enter the scroll margin near
14799 bottom of the window. */
14800 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14801 amount_to_scroll = height - 2*this_scroll_margin + dy;
14805 if (amount_to_scroll <= 0)
14806 return SCROLLING_FAILED;
14808 move_it_vertically_backward (&it, amount_to_scroll);
14809 startp = it.current.pos;
14813 /* Run window scroll functions. */
14814 startp = run_window_scroll_functions (window, startp);
14816 /* Display the window. Give up if new fonts are loaded, or if point
14817 doesn't appear. */
14818 if (!try_window (window, startp, 0))
14819 rc = SCROLLING_NEED_LARGER_MATRICES;
14820 else if (w->cursor.vpos < 0)
14822 clear_glyph_matrix (w->desired_matrix);
14823 rc = SCROLLING_FAILED;
14825 else
14827 /* Maybe forget recorded base line for line number display. */
14828 if (!just_this_one_p
14829 || current_buffer->clip_changed
14830 || BEG_UNCHANGED < CHARPOS (startp))
14831 w->base_line_number = Qnil;
14833 /* If cursor ends up on a partially visible line,
14834 treat that as being off the bottom of the screen. */
14835 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14836 /* It's possible that the cursor is on the first line of the
14837 buffer, which is partially obscured due to a vscroll
14838 (Bug#7537). In that case, avoid looping forever . */
14839 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14841 clear_glyph_matrix (w->desired_matrix);
14842 ++extra_scroll_margin_lines;
14843 goto too_near_end;
14845 rc = SCROLLING_SUCCESS;
14848 return rc;
14852 /* Compute a suitable window start for window W if display of W starts
14853 on a continuation line. Value is non-zero if a new window start
14854 was computed.
14856 The new window start will be computed, based on W's width, starting
14857 from the start of the continued line. It is the start of the
14858 screen line with the minimum distance from the old start W->start. */
14860 static int
14861 compute_window_start_on_continuation_line (struct window *w)
14863 struct text_pos pos, start_pos;
14864 int window_start_changed_p = 0;
14866 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14868 /* If window start is on a continuation line... Window start may be
14869 < BEGV in case there's invisible text at the start of the
14870 buffer (M-x rmail, for example). */
14871 if (CHARPOS (start_pos) > BEGV
14872 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14874 struct it it;
14875 struct glyph_row *row;
14877 /* Handle the case that the window start is out of range. */
14878 if (CHARPOS (start_pos) < BEGV)
14879 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14880 else if (CHARPOS (start_pos) > ZV)
14881 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14883 /* Find the start of the continued line. This should be fast
14884 because scan_buffer is fast (newline cache). */
14885 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14886 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14887 row, DEFAULT_FACE_ID);
14888 reseat_at_previous_visible_line_start (&it);
14890 /* If the line start is "too far" away from the window start,
14891 say it takes too much time to compute a new window start. */
14892 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14893 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14895 int min_distance, distance;
14897 /* Move forward by display lines to find the new window
14898 start. If window width was enlarged, the new start can
14899 be expected to be > the old start. If window width was
14900 decreased, the new window start will be < the old start.
14901 So, we're looking for the display line start with the
14902 minimum distance from the old window start. */
14903 pos = it.current.pos;
14904 min_distance = INFINITY;
14905 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14906 distance < min_distance)
14908 min_distance = distance;
14909 pos = it.current.pos;
14910 move_it_by_lines (&it, 1);
14913 /* Set the window start there. */
14914 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14915 window_start_changed_p = 1;
14919 return window_start_changed_p;
14923 /* Try cursor movement in case text has not changed in window WINDOW,
14924 with window start STARTP. Value is
14926 CURSOR_MOVEMENT_SUCCESS if successful
14928 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14930 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14931 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14932 we want to scroll as if scroll-step were set to 1. See the code.
14934 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14935 which case we have to abort this redisplay, and adjust matrices
14936 first. */
14938 enum
14940 CURSOR_MOVEMENT_SUCCESS,
14941 CURSOR_MOVEMENT_CANNOT_BE_USED,
14942 CURSOR_MOVEMENT_MUST_SCROLL,
14943 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14946 static int
14947 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14949 struct window *w = XWINDOW (window);
14950 struct frame *f = XFRAME (w->frame);
14951 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14953 #ifdef GLYPH_DEBUG
14954 if (inhibit_try_cursor_movement)
14955 return rc;
14956 #endif
14958 /* Previously, there was a check for Lisp integer in the
14959 if-statement below. Now, this field is converted to
14960 ptrdiff_t, thus zero means invalid position in a buffer. */
14961 eassert (w->last_point > 0);
14963 /* Handle case where text has not changed, only point, and it has
14964 not moved off the frame. */
14965 if (/* Point may be in this window. */
14966 PT >= CHARPOS (startp)
14967 /* Selective display hasn't changed. */
14968 && !current_buffer->clip_changed
14969 /* Function force-mode-line-update is used to force a thorough
14970 redisplay. It sets either windows_or_buffers_changed or
14971 update_mode_lines. So don't take a shortcut here for these
14972 cases. */
14973 && !update_mode_lines
14974 && !windows_or_buffers_changed
14975 && !cursor_type_changed
14976 /* Can't use this case if highlighting a region. When a
14977 region exists, cursor movement has to do more than just
14978 set the cursor. */
14979 && !(!NILP (Vtransient_mark_mode)
14980 && !NILP (BVAR (current_buffer, mark_active)))
14981 && NILP (w->region_showing)
14982 && NILP (Vshow_trailing_whitespace)
14983 /* This code is not used for mini-buffer for the sake of the case
14984 of redisplaying to replace an echo area message; since in
14985 that case the mini-buffer contents per se are usually
14986 unchanged. This code is of no real use in the mini-buffer
14987 since the handling of this_line_start_pos, etc., in redisplay
14988 handles the same cases. */
14989 && !EQ (window, minibuf_window)
14990 /* When splitting windows or for new windows, it happens that
14991 redisplay is called with a nil window_end_vpos or one being
14992 larger than the window. This should really be fixed in
14993 window.c. I don't have this on my list, now, so we do
14994 approximately the same as the old redisplay code. --gerd. */
14995 && INTEGERP (w->window_end_vpos)
14996 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14997 && (FRAME_WINDOW_P (f)
14998 || !overlay_arrow_in_current_buffer_p ()))
15000 int this_scroll_margin, top_scroll_margin;
15001 struct glyph_row *row = NULL;
15003 #ifdef GLYPH_DEBUG
15004 debug_method_add (w, "cursor movement");
15005 #endif
15007 /* Scroll if point within this distance from the top or bottom
15008 of the window. This is a pixel value. */
15009 if (scroll_margin > 0)
15011 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15012 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15014 else
15015 this_scroll_margin = 0;
15017 top_scroll_margin = this_scroll_margin;
15018 if (WINDOW_WANTS_HEADER_LINE_P (w))
15019 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15021 /* Start with the row the cursor was displayed during the last
15022 not paused redisplay. Give up if that row is not valid. */
15023 if (w->last_cursor.vpos < 0
15024 || w->last_cursor.vpos >= w->current_matrix->nrows)
15025 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15026 else
15028 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15029 if (row->mode_line_p)
15030 ++row;
15031 if (!row->enabled_p)
15032 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15035 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15037 int scroll_p = 0, must_scroll = 0;
15038 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15040 if (PT > w->last_point)
15042 /* Point has moved forward. */
15043 while (MATRIX_ROW_END_CHARPOS (row) < PT
15044 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15046 eassert (row->enabled_p);
15047 ++row;
15050 /* If the end position of a row equals the start
15051 position of the next row, and PT is at that position,
15052 we would rather display cursor in the next line. */
15053 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15054 && MATRIX_ROW_END_CHARPOS (row) == PT
15055 && row < w->current_matrix->rows
15056 + w->current_matrix->nrows - 1
15057 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15058 && !cursor_row_p (row))
15059 ++row;
15061 /* If within the scroll margin, scroll. Note that
15062 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15063 the next line would be drawn, and that
15064 this_scroll_margin can be zero. */
15065 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15066 || PT > MATRIX_ROW_END_CHARPOS (row)
15067 /* Line is completely visible last line in window
15068 and PT is to be set in the next line. */
15069 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15070 && PT == MATRIX_ROW_END_CHARPOS (row)
15071 && !row->ends_at_zv_p
15072 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15073 scroll_p = 1;
15075 else if (PT < w->last_point)
15077 /* Cursor has to be moved backward. Note that PT >=
15078 CHARPOS (startp) because of the outer if-statement. */
15079 while (!row->mode_line_p
15080 && (MATRIX_ROW_START_CHARPOS (row) > PT
15081 || (MATRIX_ROW_START_CHARPOS (row) == PT
15082 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15083 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15084 row > w->current_matrix->rows
15085 && (row-1)->ends_in_newline_from_string_p))))
15086 && (row->y > top_scroll_margin
15087 || CHARPOS (startp) == BEGV))
15089 eassert (row->enabled_p);
15090 --row;
15093 /* Consider the following case: Window starts at BEGV,
15094 there is invisible, intangible text at BEGV, so that
15095 display starts at some point START > BEGV. It can
15096 happen that we are called with PT somewhere between
15097 BEGV and START. Try to handle that case. */
15098 if (row < w->current_matrix->rows
15099 || row->mode_line_p)
15101 row = w->current_matrix->rows;
15102 if (row->mode_line_p)
15103 ++row;
15106 /* Due to newlines in overlay strings, we may have to
15107 skip forward over overlay strings. */
15108 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15109 && MATRIX_ROW_END_CHARPOS (row) == PT
15110 && !cursor_row_p (row))
15111 ++row;
15113 /* If within the scroll margin, scroll. */
15114 if (row->y < top_scroll_margin
15115 && CHARPOS (startp) != BEGV)
15116 scroll_p = 1;
15118 else
15120 /* Cursor did not move. So don't scroll even if cursor line
15121 is partially visible, as it was so before. */
15122 rc = CURSOR_MOVEMENT_SUCCESS;
15125 if (PT < MATRIX_ROW_START_CHARPOS (row)
15126 || PT > MATRIX_ROW_END_CHARPOS (row))
15128 /* if PT is not in the glyph row, give up. */
15129 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15130 must_scroll = 1;
15132 else if (rc != CURSOR_MOVEMENT_SUCCESS
15133 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15135 struct glyph_row *row1;
15137 /* If rows are bidi-reordered and point moved, back up
15138 until we find a row that does not belong to a
15139 continuation line. This is because we must consider
15140 all rows of a continued line as candidates for the
15141 new cursor positioning, since row start and end
15142 positions change non-linearly with vertical position
15143 in such rows. */
15144 /* FIXME: Revisit this when glyph ``spilling'' in
15145 continuation lines' rows is implemented for
15146 bidi-reordered rows. */
15147 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15148 MATRIX_ROW_CONTINUATION_LINE_P (row);
15149 --row)
15151 /* If we hit the beginning of the displayed portion
15152 without finding the first row of a continued
15153 line, give up. */
15154 if (row <= row1)
15156 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15157 break;
15159 eassert (row->enabled_p);
15162 if (must_scroll)
15164 else if (rc != CURSOR_MOVEMENT_SUCCESS
15165 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15166 /* Make sure this isn't a header line by any chance, since
15167 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15168 && !row->mode_line_p
15169 && make_cursor_line_fully_visible_p)
15171 if (PT == MATRIX_ROW_END_CHARPOS (row)
15172 && !row->ends_at_zv_p
15173 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15174 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15175 else if (row->height > window_box_height (w))
15177 /* If we end up in a partially visible line, let's
15178 make it fully visible, except when it's taller
15179 than the window, in which case we can't do much
15180 about it. */
15181 *scroll_step = 1;
15182 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15184 else
15186 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15187 if (!cursor_row_fully_visible_p (w, 0, 1))
15188 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15189 else
15190 rc = CURSOR_MOVEMENT_SUCCESS;
15193 else if (scroll_p)
15194 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15195 else if (rc != CURSOR_MOVEMENT_SUCCESS
15196 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15198 /* With bidi-reordered rows, there could be more than
15199 one candidate row whose start and end positions
15200 occlude point. We need to let set_cursor_from_row
15201 find the best candidate. */
15202 /* FIXME: Revisit this when glyph ``spilling'' in
15203 continuation lines' rows is implemented for
15204 bidi-reordered rows. */
15205 int rv = 0;
15209 int at_zv_p = 0, exact_match_p = 0;
15211 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15212 && PT <= MATRIX_ROW_END_CHARPOS (row)
15213 && cursor_row_p (row))
15214 rv |= set_cursor_from_row (w, row, w->current_matrix,
15215 0, 0, 0, 0);
15216 /* As soon as we've found the exact match for point,
15217 or the first suitable row whose ends_at_zv_p flag
15218 is set, we are done. */
15219 at_zv_p =
15220 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15221 if (rv && !at_zv_p
15222 && w->cursor.hpos >= 0
15223 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15224 w->cursor.vpos))
15226 struct glyph_row *candidate =
15227 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15228 struct glyph *g =
15229 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15230 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15232 exact_match_p =
15233 (BUFFERP (g->object) && g->charpos == PT)
15234 || (INTEGERP (g->object)
15235 && (g->charpos == PT
15236 || (g->charpos == 0 && endpos - 1 == PT)));
15238 if (rv && (at_zv_p || exact_match_p))
15240 rc = CURSOR_MOVEMENT_SUCCESS;
15241 break;
15243 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15244 break;
15245 ++row;
15247 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15248 || row->continued_p)
15249 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15250 || (MATRIX_ROW_START_CHARPOS (row) == PT
15251 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15252 /* If we didn't find any candidate rows, or exited the
15253 loop before all the candidates were examined, signal
15254 to the caller that this method failed. */
15255 if (rc != CURSOR_MOVEMENT_SUCCESS
15256 && !(rv
15257 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15258 && !row->continued_p))
15259 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15260 else if (rv)
15261 rc = CURSOR_MOVEMENT_SUCCESS;
15263 else
15267 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15269 rc = CURSOR_MOVEMENT_SUCCESS;
15270 break;
15272 ++row;
15274 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15275 && MATRIX_ROW_START_CHARPOS (row) == PT
15276 && cursor_row_p (row));
15281 return rc;
15284 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15285 static
15286 #endif
15287 void
15288 set_vertical_scroll_bar (struct window *w)
15290 ptrdiff_t start, end, whole;
15292 /* Calculate the start and end positions for the current window.
15293 At some point, it would be nice to choose between scrollbars
15294 which reflect the whole buffer size, with special markers
15295 indicating narrowing, and scrollbars which reflect only the
15296 visible region.
15298 Note that mini-buffers sometimes aren't displaying any text. */
15299 if (!MINI_WINDOW_P (w)
15300 || (w == XWINDOW (minibuf_window)
15301 && NILP (echo_area_buffer[0])))
15303 struct buffer *buf = XBUFFER (w->buffer);
15304 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15305 start = marker_position (w->start) - BUF_BEGV (buf);
15306 /* I don't think this is guaranteed to be right. For the
15307 moment, we'll pretend it is. */
15308 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15310 if (end < start)
15311 end = start;
15312 if (whole < (end - start))
15313 whole = end - start;
15315 else
15316 start = end = whole = 0;
15318 /* Indicate what this scroll bar ought to be displaying now. */
15319 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15320 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15321 (w, end - start, whole, start);
15325 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15326 selected_window is redisplayed.
15328 We can return without actually redisplaying the window if
15329 fonts_changed_p is nonzero. In that case, redisplay_internal will
15330 retry. */
15332 static void
15333 redisplay_window (Lisp_Object window, int just_this_one_p)
15335 struct window *w = XWINDOW (window);
15336 struct frame *f = XFRAME (w->frame);
15337 struct buffer *buffer = XBUFFER (w->buffer);
15338 struct buffer *old = current_buffer;
15339 struct text_pos lpoint, opoint, startp;
15340 int update_mode_line;
15341 int tem;
15342 struct it it;
15343 /* Record it now because it's overwritten. */
15344 int current_matrix_up_to_date_p = 0;
15345 int used_current_matrix_p = 0;
15346 /* This is less strict than current_matrix_up_to_date_p.
15347 It indicates that the buffer contents and narrowing are unchanged. */
15348 int buffer_unchanged_p = 0;
15349 int temp_scroll_step = 0;
15350 ptrdiff_t count = SPECPDL_INDEX ();
15351 int rc;
15352 int centering_position = -1;
15353 int last_line_misfit = 0;
15354 ptrdiff_t beg_unchanged, end_unchanged;
15356 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15357 opoint = lpoint;
15359 /* W must be a leaf window here. */
15360 eassert (!NILP (w->buffer));
15361 #ifdef GLYPH_DEBUG
15362 *w->desired_matrix->method = 0;
15363 #endif
15365 restart:
15366 reconsider_clip_changes (w, buffer);
15368 /* Has the mode line to be updated? */
15369 update_mode_line = (w->update_mode_line
15370 || update_mode_lines
15371 || buffer->clip_changed
15372 || buffer->prevent_redisplay_optimizations_p);
15374 if (MINI_WINDOW_P (w))
15376 if (w == XWINDOW (echo_area_window)
15377 && !NILP (echo_area_buffer[0]))
15379 if (update_mode_line)
15380 /* We may have to update a tty frame's menu bar or a
15381 tool-bar. Example `M-x C-h C-h C-g'. */
15382 goto finish_menu_bars;
15383 else
15384 /* We've already displayed the echo area glyphs in this window. */
15385 goto finish_scroll_bars;
15387 else if ((w != XWINDOW (minibuf_window)
15388 || minibuf_level == 0)
15389 /* When buffer is nonempty, redisplay window normally. */
15390 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15391 /* Quail displays non-mini buffers in minibuffer window.
15392 In that case, redisplay the window normally. */
15393 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15395 /* W is a mini-buffer window, but it's not active, so clear
15396 it. */
15397 int yb = window_text_bottom_y (w);
15398 struct glyph_row *row;
15399 int y;
15401 for (y = 0, row = w->desired_matrix->rows;
15402 y < yb;
15403 y += row->height, ++row)
15404 blank_row (w, row, y);
15405 goto finish_scroll_bars;
15408 clear_glyph_matrix (w->desired_matrix);
15411 /* Otherwise set up data on this window; select its buffer and point
15412 value. */
15413 /* Really select the buffer, for the sake of buffer-local
15414 variables. */
15415 set_buffer_internal_1 (XBUFFER (w->buffer));
15417 current_matrix_up_to_date_p
15418 = (!NILP (w->window_end_valid)
15419 && !current_buffer->clip_changed
15420 && !current_buffer->prevent_redisplay_optimizations_p
15421 && w->last_modified >= MODIFF
15422 && w->last_overlay_modified >= OVERLAY_MODIFF);
15424 /* Run the window-bottom-change-functions
15425 if it is possible that the text on the screen has changed
15426 (either due to modification of the text, or any other reason). */
15427 if (!current_matrix_up_to_date_p
15428 && !NILP (Vwindow_text_change_functions))
15430 safe_run_hooks (Qwindow_text_change_functions);
15431 goto restart;
15434 beg_unchanged = BEG_UNCHANGED;
15435 end_unchanged = END_UNCHANGED;
15437 SET_TEXT_POS (opoint, PT, PT_BYTE);
15439 specbind (Qinhibit_point_motion_hooks, Qt);
15441 buffer_unchanged_p
15442 = (!NILP (w->window_end_valid)
15443 && !current_buffer->clip_changed
15444 && w->last_modified >= MODIFF
15445 && w->last_overlay_modified >= OVERLAY_MODIFF);
15447 /* When windows_or_buffers_changed is non-zero, we can't rely on
15448 the window end being valid, so set it to nil there. */
15449 if (windows_or_buffers_changed)
15451 /* If window starts on a continuation line, maybe adjust the
15452 window start in case the window's width changed. */
15453 if (XMARKER (w->start)->buffer == current_buffer)
15454 compute_window_start_on_continuation_line (w);
15456 w->window_end_valid = Qnil;
15459 /* Some sanity checks. */
15460 CHECK_WINDOW_END (w);
15461 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15462 abort ();
15463 if (BYTEPOS (opoint) < CHARPOS (opoint))
15464 abort ();
15466 /* If %c is in mode line, update it if needed. */
15467 if (!NILP (w->column_number_displayed)
15468 /* This alternative quickly identifies a common case
15469 where no change is needed. */
15470 && !(PT == w->last_point
15471 && w->last_modified >= MODIFF
15472 && w->last_overlay_modified >= OVERLAY_MODIFF)
15473 && (XFASTINT (w->column_number_displayed) != current_column ()))
15474 update_mode_line = 1;
15476 /* Count number of windows showing the selected buffer. An indirect
15477 buffer counts as its base buffer. */
15478 if (!just_this_one_p)
15480 struct buffer *current_base, *window_base;
15481 current_base = current_buffer;
15482 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15483 if (current_base->base_buffer)
15484 current_base = current_base->base_buffer;
15485 if (window_base->base_buffer)
15486 window_base = window_base->base_buffer;
15487 if (current_base == window_base)
15488 buffer_shared++;
15491 /* Point refers normally to the selected window. For any other
15492 window, set up appropriate value. */
15493 if (!EQ (window, selected_window))
15495 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15496 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15497 if (new_pt < BEGV)
15499 new_pt = BEGV;
15500 new_pt_byte = BEGV_BYTE;
15501 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15503 else if (new_pt > (ZV - 1))
15505 new_pt = ZV;
15506 new_pt_byte = ZV_BYTE;
15507 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15510 /* We don't use SET_PT so that the point-motion hooks don't run. */
15511 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15514 /* If any of the character widths specified in the display table
15515 have changed, invalidate the width run cache. It's true that
15516 this may be a bit late to catch such changes, but the rest of
15517 redisplay goes (non-fatally) haywire when the display table is
15518 changed, so why should we worry about doing any better? */
15519 if (current_buffer->width_run_cache)
15521 struct Lisp_Char_Table *disptab = buffer_display_table ();
15523 if (! disptab_matches_widthtab (disptab,
15524 XVECTOR (BVAR (current_buffer, width_table))))
15526 invalidate_region_cache (current_buffer,
15527 current_buffer->width_run_cache,
15528 BEG, Z);
15529 recompute_width_table (current_buffer, disptab);
15533 /* If window-start is screwed up, choose a new one. */
15534 if (XMARKER (w->start)->buffer != current_buffer)
15535 goto recenter;
15537 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15539 /* If someone specified a new starting point but did not insist,
15540 check whether it can be used. */
15541 if (w->optional_new_start
15542 && CHARPOS (startp) >= BEGV
15543 && CHARPOS (startp) <= ZV)
15545 w->optional_new_start = 0;
15546 start_display (&it, w, startp);
15547 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15548 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15549 if (IT_CHARPOS (it) == PT)
15550 w->force_start = 1;
15551 /* IT may overshoot PT if text at PT is invisible. */
15552 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15553 w->force_start = 1;
15556 force_start:
15558 /* Handle case where place to start displaying has been specified,
15559 unless the specified location is outside the accessible range. */
15560 if (w->force_start || w->frozen_window_start_p)
15562 /* We set this later on if we have to adjust point. */
15563 int new_vpos = -1;
15565 w->force_start = 0;
15566 w->vscroll = 0;
15567 w->window_end_valid = Qnil;
15569 /* Forget any recorded base line for line number display. */
15570 if (!buffer_unchanged_p)
15571 w->base_line_number = Qnil;
15573 /* Redisplay the mode line. Select the buffer properly for that.
15574 Also, run the hook window-scroll-functions
15575 because we have scrolled. */
15576 /* Note, we do this after clearing force_start because
15577 if there's an error, it is better to forget about force_start
15578 than to get into an infinite loop calling the hook functions
15579 and having them get more errors. */
15580 if (!update_mode_line
15581 || ! NILP (Vwindow_scroll_functions))
15583 update_mode_line = 1;
15584 w->update_mode_line = 1;
15585 startp = run_window_scroll_functions (window, startp);
15588 w->last_modified = 0;
15589 w->last_overlay_modified = 0;
15590 if (CHARPOS (startp) < BEGV)
15591 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15592 else if (CHARPOS (startp) > ZV)
15593 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15595 /* Redisplay, then check if cursor has been set during the
15596 redisplay. Give up if new fonts were loaded. */
15597 /* We used to issue a CHECK_MARGINS argument to try_window here,
15598 but this causes scrolling to fail when point begins inside
15599 the scroll margin (bug#148) -- cyd */
15600 if (!try_window (window, startp, 0))
15602 w->force_start = 1;
15603 clear_glyph_matrix (w->desired_matrix);
15604 goto need_larger_matrices;
15607 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15609 /* If point does not appear, try to move point so it does
15610 appear. The desired matrix has been built above, so we
15611 can use it here. */
15612 new_vpos = window_box_height (w) / 2;
15615 if (!cursor_row_fully_visible_p (w, 0, 0))
15617 /* Point does appear, but on a line partly visible at end of window.
15618 Move it back to a fully-visible line. */
15619 new_vpos = window_box_height (w);
15622 /* If we need to move point for either of the above reasons,
15623 now actually do it. */
15624 if (new_vpos >= 0)
15626 struct glyph_row *row;
15628 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15629 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15630 ++row;
15632 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15633 MATRIX_ROW_START_BYTEPOS (row));
15635 if (w != XWINDOW (selected_window))
15636 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15637 else if (current_buffer == old)
15638 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15640 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15642 /* If we are highlighting the region, then we just changed
15643 the region, so redisplay to show it. */
15644 if (!NILP (Vtransient_mark_mode)
15645 && !NILP (BVAR (current_buffer, mark_active)))
15647 clear_glyph_matrix (w->desired_matrix);
15648 if (!try_window (window, startp, 0))
15649 goto need_larger_matrices;
15653 #ifdef GLYPH_DEBUG
15654 debug_method_add (w, "forced window start");
15655 #endif
15656 goto done;
15659 /* Handle case where text has not changed, only point, and it has
15660 not moved off the frame, and we are not retrying after hscroll.
15661 (current_matrix_up_to_date_p is nonzero when retrying.) */
15662 if (current_matrix_up_to_date_p
15663 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15664 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15666 switch (rc)
15668 case CURSOR_MOVEMENT_SUCCESS:
15669 used_current_matrix_p = 1;
15670 goto done;
15672 case CURSOR_MOVEMENT_MUST_SCROLL:
15673 goto try_to_scroll;
15675 default:
15676 abort ();
15679 /* If current starting point was originally the beginning of a line
15680 but no longer is, find a new starting point. */
15681 else if (w->start_at_line_beg
15682 && !(CHARPOS (startp) <= BEGV
15683 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15685 #ifdef GLYPH_DEBUG
15686 debug_method_add (w, "recenter 1");
15687 #endif
15688 goto recenter;
15691 /* Try scrolling with try_window_id. Value is > 0 if update has
15692 been done, it is -1 if we know that the same window start will
15693 not work. It is 0 if unsuccessful for some other reason. */
15694 else if ((tem = try_window_id (w)) != 0)
15696 #ifdef GLYPH_DEBUG
15697 debug_method_add (w, "try_window_id %d", tem);
15698 #endif
15700 if (fonts_changed_p)
15701 goto need_larger_matrices;
15702 if (tem > 0)
15703 goto done;
15705 /* Otherwise try_window_id has returned -1 which means that we
15706 don't want the alternative below this comment to execute. */
15708 else if (CHARPOS (startp) >= BEGV
15709 && CHARPOS (startp) <= ZV
15710 && PT >= CHARPOS (startp)
15711 && (CHARPOS (startp) < ZV
15712 /* Avoid starting at end of buffer. */
15713 || CHARPOS (startp) == BEGV
15714 || (w->last_modified >= MODIFF
15715 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15717 int d1, d2, d3, d4, d5, d6;
15719 /* If first window line is a continuation line, and window start
15720 is inside the modified region, but the first change is before
15721 current window start, we must select a new window start.
15723 However, if this is the result of a down-mouse event (e.g. by
15724 extending the mouse-drag-overlay), we don't want to select a
15725 new window start, since that would change the position under
15726 the mouse, resulting in an unwanted mouse-movement rather
15727 than a simple mouse-click. */
15728 if (!w->start_at_line_beg
15729 && NILP (do_mouse_tracking)
15730 && CHARPOS (startp) > BEGV
15731 && CHARPOS (startp) > BEG + beg_unchanged
15732 && CHARPOS (startp) <= Z - end_unchanged
15733 /* Even if w->start_at_line_beg is nil, a new window may
15734 start at a line_beg, since that's how set_buffer_window
15735 sets it. So, we need to check the return value of
15736 compute_window_start_on_continuation_line. (See also
15737 bug#197). */
15738 && XMARKER (w->start)->buffer == current_buffer
15739 && compute_window_start_on_continuation_line (w)
15740 /* It doesn't make sense to force the window start like we
15741 do at label force_start if it is already known that point
15742 will not be visible in the resulting window, because
15743 doing so will move point from its correct position
15744 instead of scrolling the window to bring point into view.
15745 See bug#9324. */
15746 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15748 w->force_start = 1;
15749 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15750 goto force_start;
15753 #ifdef GLYPH_DEBUG
15754 debug_method_add (w, "same window start");
15755 #endif
15757 /* Try to redisplay starting at same place as before.
15758 If point has not moved off frame, accept the results. */
15759 if (!current_matrix_up_to_date_p
15760 /* Don't use try_window_reusing_current_matrix in this case
15761 because a window scroll function can have changed the
15762 buffer. */
15763 || !NILP (Vwindow_scroll_functions)
15764 || MINI_WINDOW_P (w)
15765 || !(used_current_matrix_p
15766 = try_window_reusing_current_matrix (w)))
15768 IF_DEBUG (debug_method_add (w, "1"));
15769 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15770 /* -1 means we need to scroll.
15771 0 means we need new matrices, but fonts_changed_p
15772 is set in that case, so we will detect it below. */
15773 goto try_to_scroll;
15776 if (fonts_changed_p)
15777 goto need_larger_matrices;
15779 if (w->cursor.vpos >= 0)
15781 if (!just_this_one_p
15782 || current_buffer->clip_changed
15783 || BEG_UNCHANGED < CHARPOS (startp))
15784 /* Forget any recorded base line for line number display. */
15785 w->base_line_number = Qnil;
15787 if (!cursor_row_fully_visible_p (w, 1, 0))
15789 clear_glyph_matrix (w->desired_matrix);
15790 last_line_misfit = 1;
15792 /* Drop through and scroll. */
15793 else
15794 goto done;
15796 else
15797 clear_glyph_matrix (w->desired_matrix);
15800 try_to_scroll:
15802 w->last_modified = 0;
15803 w->last_overlay_modified = 0;
15805 /* Redisplay the mode line. Select the buffer properly for that. */
15806 if (!update_mode_line)
15808 update_mode_line = 1;
15809 w->update_mode_line = 1;
15812 /* Try to scroll by specified few lines. */
15813 if ((scroll_conservatively
15814 || emacs_scroll_step
15815 || temp_scroll_step
15816 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15817 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15818 && CHARPOS (startp) >= BEGV
15819 && CHARPOS (startp) <= ZV)
15821 /* The function returns -1 if new fonts were loaded, 1 if
15822 successful, 0 if not successful. */
15823 int ss = try_scrolling (window, just_this_one_p,
15824 scroll_conservatively,
15825 emacs_scroll_step,
15826 temp_scroll_step, last_line_misfit);
15827 switch (ss)
15829 case SCROLLING_SUCCESS:
15830 goto done;
15832 case SCROLLING_NEED_LARGER_MATRICES:
15833 goto need_larger_matrices;
15835 case SCROLLING_FAILED:
15836 break;
15838 default:
15839 abort ();
15843 /* Finally, just choose a place to start which positions point
15844 according to user preferences. */
15846 recenter:
15848 #ifdef GLYPH_DEBUG
15849 debug_method_add (w, "recenter");
15850 #endif
15852 /* w->vscroll = 0; */
15854 /* Forget any previously recorded base line for line number display. */
15855 if (!buffer_unchanged_p)
15856 w->base_line_number = Qnil;
15858 /* Determine the window start relative to point. */
15859 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15860 it.current_y = it.last_visible_y;
15861 if (centering_position < 0)
15863 int margin =
15864 scroll_margin > 0
15865 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15866 : 0;
15867 ptrdiff_t margin_pos = CHARPOS (startp);
15868 Lisp_Object aggressive;
15869 int scrolling_up;
15871 /* If there is a scroll margin at the top of the window, find
15872 its character position. */
15873 if (margin
15874 /* Cannot call start_display if startp is not in the
15875 accessible region of the buffer. This can happen when we
15876 have just switched to a different buffer and/or changed
15877 its restriction. In that case, startp is initialized to
15878 the character position 1 (BEGV) because we did not yet
15879 have chance to display the buffer even once. */
15880 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15882 struct it it1;
15883 void *it1data = NULL;
15885 SAVE_IT (it1, it, it1data);
15886 start_display (&it1, w, startp);
15887 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15888 margin_pos = IT_CHARPOS (it1);
15889 RESTORE_IT (&it, &it, it1data);
15891 scrolling_up = PT > margin_pos;
15892 aggressive =
15893 scrolling_up
15894 ? BVAR (current_buffer, scroll_up_aggressively)
15895 : BVAR (current_buffer, scroll_down_aggressively);
15897 if (!MINI_WINDOW_P (w)
15898 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15900 int pt_offset = 0;
15902 /* Setting scroll-conservatively overrides
15903 scroll-*-aggressively. */
15904 if (!scroll_conservatively && NUMBERP (aggressive))
15906 double float_amount = XFLOATINT (aggressive);
15908 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15909 if (pt_offset == 0 && float_amount > 0)
15910 pt_offset = 1;
15911 if (pt_offset && margin > 0)
15912 margin -= 1;
15914 /* Compute how much to move the window start backward from
15915 point so that point will be displayed where the user
15916 wants it. */
15917 if (scrolling_up)
15919 centering_position = it.last_visible_y;
15920 if (pt_offset)
15921 centering_position -= pt_offset;
15922 centering_position -=
15923 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15924 + WINDOW_HEADER_LINE_HEIGHT (w);
15925 /* Don't let point enter the scroll margin near top of
15926 the window. */
15927 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15928 centering_position = margin * FRAME_LINE_HEIGHT (f);
15930 else
15931 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15933 else
15934 /* Set the window start half the height of the window backward
15935 from point. */
15936 centering_position = window_box_height (w) / 2;
15938 move_it_vertically_backward (&it, centering_position);
15940 eassert (IT_CHARPOS (it) >= BEGV);
15942 /* The function move_it_vertically_backward may move over more
15943 than the specified y-distance. If it->w is small, e.g. a
15944 mini-buffer window, we may end up in front of the window's
15945 display area. Start displaying at the start of the line
15946 containing PT in this case. */
15947 if (it.current_y <= 0)
15949 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15950 move_it_vertically_backward (&it, 0);
15951 it.current_y = 0;
15954 it.current_x = it.hpos = 0;
15956 /* Set the window start position here explicitly, to avoid an
15957 infinite loop in case the functions in window-scroll-functions
15958 get errors. */
15959 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15961 /* Run scroll hooks. */
15962 startp = run_window_scroll_functions (window, it.current.pos);
15964 /* Redisplay the window. */
15965 if (!current_matrix_up_to_date_p
15966 || windows_or_buffers_changed
15967 || cursor_type_changed
15968 /* Don't use try_window_reusing_current_matrix in this case
15969 because it can have changed the buffer. */
15970 || !NILP (Vwindow_scroll_functions)
15971 || !just_this_one_p
15972 || MINI_WINDOW_P (w)
15973 || !(used_current_matrix_p
15974 = try_window_reusing_current_matrix (w)))
15975 try_window (window, startp, 0);
15977 /* If new fonts have been loaded (due to fontsets), give up. We
15978 have to start a new redisplay since we need to re-adjust glyph
15979 matrices. */
15980 if (fonts_changed_p)
15981 goto need_larger_matrices;
15983 /* If cursor did not appear assume that the middle of the window is
15984 in the first line of the window. Do it again with the next line.
15985 (Imagine a window of height 100, displaying two lines of height
15986 60. Moving back 50 from it->last_visible_y will end in the first
15987 line.) */
15988 if (w->cursor.vpos < 0)
15990 if (!NILP (w->window_end_valid)
15991 && PT >= Z - XFASTINT (w->window_end_pos))
15993 clear_glyph_matrix (w->desired_matrix);
15994 move_it_by_lines (&it, 1);
15995 try_window (window, it.current.pos, 0);
15997 else if (PT < IT_CHARPOS (it))
15999 clear_glyph_matrix (w->desired_matrix);
16000 move_it_by_lines (&it, -1);
16001 try_window (window, it.current.pos, 0);
16003 else
16005 /* Not much we can do about it. */
16009 /* Consider the following case: Window starts at BEGV, there is
16010 invisible, intangible text at BEGV, so that display starts at
16011 some point START > BEGV. It can happen that we are called with
16012 PT somewhere between BEGV and START. Try to handle that case. */
16013 if (w->cursor.vpos < 0)
16015 struct glyph_row *row = w->current_matrix->rows;
16016 if (row->mode_line_p)
16017 ++row;
16018 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16021 if (!cursor_row_fully_visible_p (w, 0, 0))
16023 /* If vscroll is enabled, disable it and try again. */
16024 if (w->vscroll)
16026 w->vscroll = 0;
16027 clear_glyph_matrix (w->desired_matrix);
16028 goto recenter;
16031 /* Users who set scroll-conservatively to a large number want
16032 point just above/below the scroll margin. If we ended up
16033 with point's row partially visible, move the window start to
16034 make that row fully visible and out of the margin. */
16035 if (scroll_conservatively > SCROLL_LIMIT)
16037 int margin =
16038 scroll_margin > 0
16039 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16040 : 0;
16041 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16043 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16044 clear_glyph_matrix (w->desired_matrix);
16045 if (1 == try_window (window, it.current.pos,
16046 TRY_WINDOW_CHECK_MARGINS))
16047 goto done;
16050 /* If centering point failed to make the whole line visible,
16051 put point at the top instead. That has to make the whole line
16052 visible, if it can be done. */
16053 if (centering_position == 0)
16054 goto done;
16056 clear_glyph_matrix (w->desired_matrix);
16057 centering_position = 0;
16058 goto recenter;
16061 done:
16063 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16064 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16065 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16067 /* Display the mode line, if we must. */
16068 if ((update_mode_line
16069 /* If window not full width, must redo its mode line
16070 if (a) the window to its side is being redone and
16071 (b) we do a frame-based redisplay. This is a consequence
16072 of how inverted lines are drawn in frame-based redisplay. */
16073 || (!just_this_one_p
16074 && !FRAME_WINDOW_P (f)
16075 && !WINDOW_FULL_WIDTH_P (w))
16076 /* Line number to display. */
16077 || INTEGERP (w->base_line_pos)
16078 /* Column number is displayed and different from the one displayed. */
16079 || (!NILP (w->column_number_displayed)
16080 && (XFASTINT (w->column_number_displayed) != current_column ())))
16081 /* This means that the window has a mode line. */
16082 && (WINDOW_WANTS_MODELINE_P (w)
16083 || WINDOW_WANTS_HEADER_LINE_P (w)))
16085 display_mode_lines (w);
16087 /* If mode line height has changed, arrange for a thorough
16088 immediate redisplay using the correct mode line height. */
16089 if (WINDOW_WANTS_MODELINE_P (w)
16090 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16092 fonts_changed_p = 1;
16093 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16094 = DESIRED_MODE_LINE_HEIGHT (w);
16097 /* If header line height has changed, arrange for a thorough
16098 immediate redisplay using the correct header line height. */
16099 if (WINDOW_WANTS_HEADER_LINE_P (w)
16100 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16102 fonts_changed_p = 1;
16103 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16104 = DESIRED_HEADER_LINE_HEIGHT (w);
16107 if (fonts_changed_p)
16108 goto need_larger_matrices;
16111 if (!line_number_displayed
16112 && !BUFFERP (w->base_line_pos))
16114 w->base_line_pos = Qnil;
16115 w->base_line_number = Qnil;
16118 finish_menu_bars:
16120 /* When we reach a frame's selected window, redo the frame's menu bar. */
16121 if (update_mode_line
16122 && EQ (FRAME_SELECTED_WINDOW (f), window))
16124 int redisplay_menu_p = 0;
16126 if (FRAME_WINDOW_P (f))
16128 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16129 || defined (HAVE_NS) || defined (USE_GTK)
16130 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16131 #else
16132 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16133 #endif
16135 else
16136 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16138 if (redisplay_menu_p)
16139 display_menu_bar (w);
16141 #ifdef HAVE_WINDOW_SYSTEM
16142 if (FRAME_WINDOW_P (f))
16144 #if defined (USE_GTK) || defined (HAVE_NS)
16145 if (FRAME_EXTERNAL_TOOL_BAR (f))
16146 redisplay_tool_bar (f);
16147 #else
16148 if (WINDOWP (f->tool_bar_window)
16149 && (FRAME_TOOL_BAR_LINES (f) > 0
16150 || !NILP (Vauto_resize_tool_bars))
16151 && redisplay_tool_bar (f))
16152 ignore_mouse_drag_p = 1;
16153 #endif
16155 #endif
16158 #ifdef HAVE_WINDOW_SYSTEM
16159 if (FRAME_WINDOW_P (f)
16160 && update_window_fringes (w, (just_this_one_p
16161 || (!used_current_matrix_p && !overlay_arrow_seen)
16162 || w->pseudo_window_p)))
16164 update_begin (f);
16165 BLOCK_INPUT;
16166 if (draw_window_fringes (w, 1))
16167 x_draw_vertical_border (w);
16168 UNBLOCK_INPUT;
16169 update_end (f);
16171 #endif /* HAVE_WINDOW_SYSTEM */
16173 /* We go to this label, with fonts_changed_p nonzero,
16174 if it is necessary to try again using larger glyph matrices.
16175 We have to redeem the scroll bar even in this case,
16176 because the loop in redisplay_internal expects that. */
16177 need_larger_matrices:
16179 finish_scroll_bars:
16181 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16183 /* Set the thumb's position and size. */
16184 set_vertical_scroll_bar (w);
16186 /* Note that we actually used the scroll bar attached to this
16187 window, so it shouldn't be deleted at the end of redisplay. */
16188 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16189 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16192 /* Restore current_buffer and value of point in it. The window
16193 update may have changed the buffer, so first make sure `opoint'
16194 is still valid (Bug#6177). */
16195 if (CHARPOS (opoint) < BEGV)
16196 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16197 else if (CHARPOS (opoint) > ZV)
16198 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16199 else
16200 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16202 set_buffer_internal_1 (old);
16203 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16204 shorter. This can be caused by log truncation in *Messages*. */
16205 if (CHARPOS (lpoint) <= ZV)
16206 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16208 unbind_to (count, Qnil);
16212 /* Build the complete desired matrix of WINDOW with a window start
16213 buffer position POS.
16215 Value is 1 if successful. It is zero if fonts were loaded during
16216 redisplay which makes re-adjusting glyph matrices necessary, and -1
16217 if point would appear in the scroll margins.
16218 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16219 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16220 set in FLAGS.) */
16223 try_window (Lisp_Object window, struct text_pos pos, int flags)
16225 struct window *w = XWINDOW (window);
16226 struct it it;
16227 struct glyph_row *last_text_row = NULL;
16228 struct frame *f = XFRAME (w->frame);
16230 /* Make POS the new window start. */
16231 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16233 /* Mark cursor position as unknown. No overlay arrow seen. */
16234 w->cursor.vpos = -1;
16235 overlay_arrow_seen = 0;
16237 /* Initialize iterator and info to start at POS. */
16238 start_display (&it, w, pos);
16240 /* Display all lines of W. */
16241 while (it.current_y < it.last_visible_y)
16243 if (display_line (&it))
16244 last_text_row = it.glyph_row - 1;
16245 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16246 return 0;
16249 /* Don't let the cursor end in the scroll margins. */
16250 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16251 && !MINI_WINDOW_P (w))
16253 int this_scroll_margin;
16255 if (scroll_margin > 0)
16257 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16258 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16260 else
16261 this_scroll_margin = 0;
16263 if ((w->cursor.y >= 0 /* not vscrolled */
16264 && w->cursor.y < this_scroll_margin
16265 && CHARPOS (pos) > BEGV
16266 && IT_CHARPOS (it) < ZV)
16267 /* rms: considering make_cursor_line_fully_visible_p here
16268 seems to give wrong results. We don't want to recenter
16269 when the last line is partly visible, we want to allow
16270 that case to be handled in the usual way. */
16271 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16273 w->cursor.vpos = -1;
16274 clear_glyph_matrix (w->desired_matrix);
16275 return -1;
16279 /* If bottom moved off end of frame, change mode line percentage. */
16280 if (XFASTINT (w->window_end_pos) <= 0
16281 && Z != IT_CHARPOS (it))
16282 w->update_mode_line = 1;
16284 /* Set window_end_pos to the offset of the last character displayed
16285 on the window from the end of current_buffer. Set
16286 window_end_vpos to its row number. */
16287 if (last_text_row)
16289 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16290 w->window_end_bytepos
16291 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16292 w->window_end_pos
16293 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16294 w->window_end_vpos
16295 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16296 eassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16297 ->displays_text_p);
16299 else
16301 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16302 w->window_end_pos = make_number (Z - ZV);
16303 w->window_end_vpos = make_number (0);
16306 /* But that is not valid info until redisplay finishes. */
16307 w->window_end_valid = Qnil;
16308 return 1;
16313 /************************************************************************
16314 Window redisplay reusing current matrix when buffer has not changed
16315 ************************************************************************/
16317 /* Try redisplay of window W showing an unchanged buffer with a
16318 different window start than the last time it was displayed by
16319 reusing its current matrix. Value is non-zero if successful.
16320 W->start is the new window start. */
16322 static int
16323 try_window_reusing_current_matrix (struct window *w)
16325 struct frame *f = XFRAME (w->frame);
16326 struct glyph_row *bottom_row;
16327 struct it it;
16328 struct run run;
16329 struct text_pos start, new_start;
16330 int nrows_scrolled, i;
16331 struct glyph_row *last_text_row;
16332 struct glyph_row *last_reused_text_row;
16333 struct glyph_row *start_row;
16334 int start_vpos, min_y, max_y;
16336 #ifdef GLYPH_DEBUG
16337 if (inhibit_try_window_reusing)
16338 return 0;
16339 #endif
16341 if (/* This function doesn't handle terminal frames. */
16342 !FRAME_WINDOW_P (f)
16343 /* Don't try to reuse the display if windows have been split
16344 or such. */
16345 || windows_or_buffers_changed
16346 || cursor_type_changed)
16347 return 0;
16349 /* Can't do this if region may have changed. */
16350 if ((!NILP (Vtransient_mark_mode)
16351 && !NILP (BVAR (current_buffer, mark_active)))
16352 || !NILP (w->region_showing)
16353 || !NILP (Vshow_trailing_whitespace))
16354 return 0;
16356 /* If top-line visibility has changed, give up. */
16357 if (WINDOW_WANTS_HEADER_LINE_P (w)
16358 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16359 return 0;
16361 /* Give up if old or new display is scrolled vertically. We could
16362 make this function handle this, but right now it doesn't. */
16363 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16364 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16365 return 0;
16367 /* The variable new_start now holds the new window start. The old
16368 start `start' can be determined from the current matrix. */
16369 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16370 start = start_row->minpos;
16371 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16373 /* Clear the desired matrix for the display below. */
16374 clear_glyph_matrix (w->desired_matrix);
16376 if (CHARPOS (new_start) <= CHARPOS (start))
16378 /* Don't use this method if the display starts with an ellipsis
16379 displayed for invisible text. It's not easy to handle that case
16380 below, and it's certainly not worth the effort since this is
16381 not a frequent case. */
16382 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16383 return 0;
16385 IF_DEBUG (debug_method_add (w, "twu1"));
16387 /* Display up to a row that can be reused. The variable
16388 last_text_row is set to the last row displayed that displays
16389 text. Note that it.vpos == 0 if or if not there is a
16390 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16391 start_display (&it, w, new_start);
16392 w->cursor.vpos = -1;
16393 last_text_row = last_reused_text_row = NULL;
16395 while (it.current_y < it.last_visible_y
16396 && !fonts_changed_p)
16398 /* If we have reached into the characters in the START row,
16399 that means the line boundaries have changed. So we
16400 can't start copying with the row START. Maybe it will
16401 work to start copying with the following row. */
16402 while (IT_CHARPOS (it) > CHARPOS (start))
16404 /* Advance to the next row as the "start". */
16405 start_row++;
16406 start = start_row->minpos;
16407 /* If there are no more rows to try, or just one, give up. */
16408 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16409 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16410 || CHARPOS (start) == ZV)
16412 clear_glyph_matrix (w->desired_matrix);
16413 return 0;
16416 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16418 /* If we have reached alignment, we can copy the rest of the
16419 rows. */
16420 if (IT_CHARPOS (it) == CHARPOS (start)
16421 /* Don't accept "alignment" inside a display vector,
16422 since start_row could have started in the middle of
16423 that same display vector (thus their character
16424 positions match), and we have no way of telling if
16425 that is the case. */
16426 && it.current.dpvec_index < 0)
16427 break;
16429 if (display_line (&it))
16430 last_text_row = it.glyph_row - 1;
16434 /* A value of current_y < last_visible_y means that we stopped
16435 at the previous window start, which in turn means that we
16436 have at least one reusable row. */
16437 if (it.current_y < it.last_visible_y)
16439 struct glyph_row *row;
16441 /* IT.vpos always starts from 0; it counts text lines. */
16442 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16444 /* Find PT if not already found in the lines displayed. */
16445 if (w->cursor.vpos < 0)
16447 int dy = it.current_y - start_row->y;
16449 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16450 row = row_containing_pos (w, PT, row, NULL, dy);
16451 if (row)
16452 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16453 dy, nrows_scrolled);
16454 else
16456 clear_glyph_matrix (w->desired_matrix);
16457 return 0;
16461 /* Scroll the display. Do it before the current matrix is
16462 changed. The problem here is that update has not yet
16463 run, i.e. part of the current matrix is not up to date.
16464 scroll_run_hook will clear the cursor, and use the
16465 current matrix to get the height of the row the cursor is
16466 in. */
16467 run.current_y = start_row->y;
16468 run.desired_y = it.current_y;
16469 run.height = it.last_visible_y - it.current_y;
16471 if (run.height > 0 && run.current_y != run.desired_y)
16473 update_begin (f);
16474 FRAME_RIF (f)->update_window_begin_hook (w);
16475 FRAME_RIF (f)->clear_window_mouse_face (w);
16476 FRAME_RIF (f)->scroll_run_hook (w, &run);
16477 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16478 update_end (f);
16481 /* Shift current matrix down by nrows_scrolled lines. */
16482 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16483 rotate_matrix (w->current_matrix,
16484 start_vpos,
16485 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16486 nrows_scrolled);
16488 /* Disable lines that must be updated. */
16489 for (i = 0; i < nrows_scrolled; ++i)
16490 (start_row + i)->enabled_p = 0;
16492 /* Re-compute Y positions. */
16493 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16494 max_y = it.last_visible_y;
16495 for (row = start_row + nrows_scrolled;
16496 row < bottom_row;
16497 ++row)
16499 row->y = it.current_y;
16500 row->visible_height = row->height;
16502 if (row->y < min_y)
16503 row->visible_height -= min_y - row->y;
16504 if (row->y + row->height > max_y)
16505 row->visible_height -= row->y + row->height - max_y;
16506 if (row->fringe_bitmap_periodic_p)
16507 row->redraw_fringe_bitmaps_p = 1;
16509 it.current_y += row->height;
16511 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16512 last_reused_text_row = row;
16513 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16514 break;
16517 /* Disable lines in the current matrix which are now
16518 below the window. */
16519 for (++row; row < bottom_row; ++row)
16520 row->enabled_p = row->mode_line_p = 0;
16523 /* Update window_end_pos etc.; last_reused_text_row is the last
16524 reused row from the current matrix containing text, if any.
16525 The value of last_text_row is the last displayed line
16526 containing text. */
16527 if (last_reused_text_row)
16529 w->window_end_bytepos
16530 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16531 w->window_end_pos
16532 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16533 w->window_end_vpos
16534 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16535 w->current_matrix));
16537 else if (last_text_row)
16539 w->window_end_bytepos
16540 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16541 w->window_end_pos
16542 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16543 w->window_end_vpos
16544 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16546 else
16548 /* This window must be completely empty. */
16549 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16550 w->window_end_pos = make_number (Z - ZV);
16551 w->window_end_vpos = make_number (0);
16553 w->window_end_valid = Qnil;
16555 /* Update hint: don't try scrolling again in update_window. */
16556 w->desired_matrix->no_scrolling_p = 1;
16558 #ifdef GLYPH_DEBUG
16559 debug_method_add (w, "try_window_reusing_current_matrix 1");
16560 #endif
16561 return 1;
16563 else if (CHARPOS (new_start) > CHARPOS (start))
16565 struct glyph_row *pt_row, *row;
16566 struct glyph_row *first_reusable_row;
16567 struct glyph_row *first_row_to_display;
16568 int dy;
16569 int yb = window_text_bottom_y (w);
16571 /* Find the row starting at new_start, if there is one. Don't
16572 reuse a partially visible line at the end. */
16573 first_reusable_row = start_row;
16574 while (first_reusable_row->enabled_p
16575 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16576 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16577 < CHARPOS (new_start)))
16578 ++first_reusable_row;
16580 /* Give up if there is no row to reuse. */
16581 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16582 || !first_reusable_row->enabled_p
16583 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16584 != CHARPOS (new_start)))
16585 return 0;
16587 /* We can reuse fully visible rows beginning with
16588 first_reusable_row to the end of the window. Set
16589 first_row_to_display to the first row that cannot be reused.
16590 Set pt_row to the row containing point, if there is any. */
16591 pt_row = NULL;
16592 for (first_row_to_display = first_reusable_row;
16593 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16594 ++first_row_to_display)
16596 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16597 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16598 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16599 && first_row_to_display->ends_at_zv_p
16600 && pt_row == NULL)))
16601 pt_row = first_row_to_display;
16604 /* Start displaying at the start of first_row_to_display. */
16605 eassert (first_row_to_display->y < yb);
16606 init_to_row_start (&it, w, first_row_to_display);
16608 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16609 - start_vpos);
16610 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16611 - nrows_scrolled);
16612 it.current_y = (first_row_to_display->y - first_reusable_row->y
16613 + WINDOW_HEADER_LINE_HEIGHT (w));
16615 /* Display lines beginning with first_row_to_display in the
16616 desired matrix. Set last_text_row to the last row displayed
16617 that displays text. */
16618 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16619 if (pt_row == NULL)
16620 w->cursor.vpos = -1;
16621 last_text_row = NULL;
16622 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16623 if (display_line (&it))
16624 last_text_row = it.glyph_row - 1;
16626 /* If point is in a reused row, adjust y and vpos of the cursor
16627 position. */
16628 if (pt_row)
16630 w->cursor.vpos -= nrows_scrolled;
16631 w->cursor.y -= first_reusable_row->y - start_row->y;
16634 /* Give up if point isn't in a row displayed or reused. (This
16635 also handles the case where w->cursor.vpos < nrows_scrolled
16636 after the calls to display_line, which can happen with scroll
16637 margins. See bug#1295.) */
16638 if (w->cursor.vpos < 0)
16640 clear_glyph_matrix (w->desired_matrix);
16641 return 0;
16644 /* Scroll the display. */
16645 run.current_y = first_reusable_row->y;
16646 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16647 run.height = it.last_visible_y - run.current_y;
16648 dy = run.current_y - run.desired_y;
16650 if (run.height)
16652 update_begin (f);
16653 FRAME_RIF (f)->update_window_begin_hook (w);
16654 FRAME_RIF (f)->clear_window_mouse_face (w);
16655 FRAME_RIF (f)->scroll_run_hook (w, &run);
16656 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16657 update_end (f);
16660 /* Adjust Y positions of reused rows. */
16661 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16662 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16663 max_y = it.last_visible_y;
16664 for (row = first_reusable_row; row < first_row_to_display; ++row)
16666 row->y -= dy;
16667 row->visible_height = row->height;
16668 if (row->y < min_y)
16669 row->visible_height -= min_y - row->y;
16670 if (row->y + row->height > max_y)
16671 row->visible_height -= row->y + row->height - max_y;
16672 if (row->fringe_bitmap_periodic_p)
16673 row->redraw_fringe_bitmaps_p = 1;
16676 /* Scroll the current matrix. */
16677 eassert (nrows_scrolled > 0);
16678 rotate_matrix (w->current_matrix,
16679 start_vpos,
16680 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16681 -nrows_scrolled);
16683 /* Disable rows not reused. */
16684 for (row -= nrows_scrolled; row < bottom_row; ++row)
16685 row->enabled_p = 0;
16687 /* Point may have moved to a different line, so we cannot assume that
16688 the previous cursor position is valid; locate the correct row. */
16689 if (pt_row)
16691 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16692 row < bottom_row
16693 && PT >= MATRIX_ROW_END_CHARPOS (row)
16694 && !row->ends_at_zv_p;
16695 row++)
16697 w->cursor.vpos++;
16698 w->cursor.y = row->y;
16700 if (row < bottom_row)
16702 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16703 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16705 /* Can't use this optimization with bidi-reordered glyph
16706 rows, unless cursor is already at point. */
16707 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16709 if (!(w->cursor.hpos >= 0
16710 && w->cursor.hpos < row->used[TEXT_AREA]
16711 && BUFFERP (glyph->object)
16712 && glyph->charpos == PT))
16713 return 0;
16715 else
16716 for (; glyph < end
16717 && (!BUFFERP (glyph->object)
16718 || glyph->charpos < PT);
16719 glyph++)
16721 w->cursor.hpos++;
16722 w->cursor.x += glyph->pixel_width;
16727 /* Adjust window end. A null value of last_text_row means that
16728 the window end is in reused rows which in turn means that
16729 only its vpos can have changed. */
16730 if (last_text_row)
16732 w->window_end_bytepos
16733 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16734 w->window_end_pos
16735 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16736 w->window_end_vpos
16737 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16739 else
16741 w->window_end_vpos
16742 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16745 w->window_end_valid = Qnil;
16746 w->desired_matrix->no_scrolling_p = 1;
16748 #ifdef GLYPH_DEBUG
16749 debug_method_add (w, "try_window_reusing_current_matrix 2");
16750 #endif
16751 return 1;
16754 return 0;
16759 /************************************************************************
16760 Window redisplay reusing current matrix when buffer has changed
16761 ************************************************************************/
16763 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16764 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16765 ptrdiff_t *, ptrdiff_t *);
16766 static struct glyph_row *
16767 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16768 struct glyph_row *);
16771 /* Return the last row in MATRIX displaying text. If row START is
16772 non-null, start searching with that row. IT gives the dimensions
16773 of the display. Value is null if matrix is empty; otherwise it is
16774 a pointer to the row found. */
16776 static struct glyph_row *
16777 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16778 struct glyph_row *start)
16780 struct glyph_row *row, *row_found;
16782 /* Set row_found to the last row in IT->w's current matrix
16783 displaying text. The loop looks funny but think of partially
16784 visible lines. */
16785 row_found = NULL;
16786 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16787 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16789 eassert (row->enabled_p);
16790 row_found = row;
16791 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16792 break;
16793 ++row;
16796 return row_found;
16800 /* Return the last row in the current matrix of W that is not affected
16801 by changes at the start of current_buffer that occurred since W's
16802 current matrix was built. Value is null if no such row exists.
16804 BEG_UNCHANGED us the number of characters unchanged at the start of
16805 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16806 first changed character in current_buffer. Characters at positions <
16807 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16808 when the current matrix was built. */
16810 static struct glyph_row *
16811 find_last_unchanged_at_beg_row (struct window *w)
16813 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16814 struct glyph_row *row;
16815 struct glyph_row *row_found = NULL;
16816 int yb = window_text_bottom_y (w);
16818 /* Find the last row displaying unchanged text. */
16819 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16820 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16821 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16822 ++row)
16824 if (/* If row ends before first_changed_pos, it is unchanged,
16825 except in some case. */
16826 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16827 /* When row ends in ZV and we write at ZV it is not
16828 unchanged. */
16829 && !row->ends_at_zv_p
16830 /* When first_changed_pos is the end of a continued line,
16831 row is not unchanged because it may be no longer
16832 continued. */
16833 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16834 && (row->continued_p
16835 || row->exact_window_width_line_p))
16836 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16837 needs to be recomputed, so don't consider this row as
16838 unchanged. This happens when the last line was
16839 bidi-reordered and was killed immediately before this
16840 redisplay cycle. In that case, ROW->end stores the
16841 buffer position of the first visual-order character of
16842 the killed text, which is now beyond ZV. */
16843 && CHARPOS (row->end.pos) <= ZV)
16844 row_found = row;
16846 /* Stop if last visible row. */
16847 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16848 break;
16851 return row_found;
16855 /* Find the first glyph row in the current matrix of W that is not
16856 affected by changes at the end of current_buffer since the
16857 time W's current matrix was built.
16859 Return in *DELTA the number of chars by which buffer positions in
16860 unchanged text at the end of current_buffer must be adjusted.
16862 Return in *DELTA_BYTES the corresponding number of bytes.
16864 Value is null if no such row exists, i.e. all rows are affected by
16865 changes. */
16867 static struct glyph_row *
16868 find_first_unchanged_at_end_row (struct window *w,
16869 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16871 struct glyph_row *row;
16872 struct glyph_row *row_found = NULL;
16874 *delta = *delta_bytes = 0;
16876 /* Display must not have been paused, otherwise the current matrix
16877 is not up to date. */
16878 eassert (!NILP (w->window_end_valid));
16880 /* A value of window_end_pos >= END_UNCHANGED means that the window
16881 end is in the range of changed text. If so, there is no
16882 unchanged row at the end of W's current matrix. */
16883 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16884 return NULL;
16886 /* Set row to the last row in W's current matrix displaying text. */
16887 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16889 /* If matrix is entirely empty, no unchanged row exists. */
16890 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16892 /* The value of row is the last glyph row in the matrix having a
16893 meaningful buffer position in it. The end position of row
16894 corresponds to window_end_pos. This allows us to translate
16895 buffer positions in the current matrix to current buffer
16896 positions for characters not in changed text. */
16897 ptrdiff_t Z_old =
16898 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16899 ptrdiff_t Z_BYTE_old =
16900 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16901 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16902 struct glyph_row *first_text_row
16903 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16905 *delta = Z - Z_old;
16906 *delta_bytes = Z_BYTE - Z_BYTE_old;
16908 /* Set last_unchanged_pos to the buffer position of the last
16909 character in the buffer that has not been changed. Z is the
16910 index + 1 of the last character in current_buffer, i.e. by
16911 subtracting END_UNCHANGED we get the index of the last
16912 unchanged character, and we have to add BEG to get its buffer
16913 position. */
16914 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16915 last_unchanged_pos_old = last_unchanged_pos - *delta;
16917 /* Search backward from ROW for a row displaying a line that
16918 starts at a minimum position >= last_unchanged_pos_old. */
16919 for (; row > first_text_row; --row)
16921 /* This used to abort, but it can happen.
16922 It is ok to just stop the search instead here. KFS. */
16923 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16924 break;
16926 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16927 row_found = row;
16931 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16933 return row_found;
16937 /* Make sure that glyph rows in the current matrix of window W
16938 reference the same glyph memory as corresponding rows in the
16939 frame's frame matrix. This function is called after scrolling W's
16940 current matrix on a terminal frame in try_window_id and
16941 try_window_reusing_current_matrix. */
16943 static void
16944 sync_frame_with_window_matrix_rows (struct window *w)
16946 struct frame *f = XFRAME (w->frame);
16947 struct glyph_row *window_row, *window_row_end, *frame_row;
16949 /* Preconditions: W must be a leaf window and full-width. Its frame
16950 must have a frame matrix. */
16951 eassert (NILP (w->hchild) && NILP (w->vchild));
16952 eassert (WINDOW_FULL_WIDTH_P (w));
16953 eassert (!FRAME_WINDOW_P (f));
16955 /* If W is a full-width window, glyph pointers in W's current matrix
16956 have, by definition, to be the same as glyph pointers in the
16957 corresponding frame matrix. Note that frame matrices have no
16958 marginal areas (see build_frame_matrix). */
16959 window_row = w->current_matrix->rows;
16960 window_row_end = window_row + w->current_matrix->nrows;
16961 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16962 while (window_row < window_row_end)
16964 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16965 struct glyph *end = window_row->glyphs[LAST_AREA];
16967 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16968 frame_row->glyphs[TEXT_AREA] = start;
16969 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16970 frame_row->glyphs[LAST_AREA] = end;
16972 /* Disable frame rows whose corresponding window rows have
16973 been disabled in try_window_id. */
16974 if (!window_row->enabled_p)
16975 frame_row->enabled_p = 0;
16977 ++window_row, ++frame_row;
16982 /* Find the glyph row in window W containing CHARPOS. Consider all
16983 rows between START and END (not inclusive). END null means search
16984 all rows to the end of the display area of W. Value is the row
16985 containing CHARPOS or null. */
16987 struct glyph_row *
16988 row_containing_pos (struct window *w, ptrdiff_t charpos,
16989 struct glyph_row *start, struct glyph_row *end, int dy)
16991 struct glyph_row *row = start;
16992 struct glyph_row *best_row = NULL;
16993 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16994 int last_y;
16996 /* If we happen to start on a header-line, skip that. */
16997 if (row->mode_line_p)
16998 ++row;
17000 if ((end && row >= end) || !row->enabled_p)
17001 return NULL;
17003 last_y = window_text_bottom_y (w) - dy;
17005 while (1)
17007 /* Give up if we have gone too far. */
17008 if (end && row >= end)
17009 return NULL;
17010 /* This formerly returned if they were equal.
17011 I think that both quantities are of a "last plus one" type;
17012 if so, when they are equal, the row is within the screen. -- rms. */
17013 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17014 return NULL;
17016 /* If it is in this row, return this row. */
17017 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17018 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17019 /* The end position of a row equals the start
17020 position of the next row. If CHARPOS is there, we
17021 would rather display it in the next line, except
17022 when this line ends in ZV. */
17023 && !row->ends_at_zv_p
17024 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17025 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17027 struct glyph *g;
17029 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17030 || (!best_row && !row->continued_p))
17031 return row;
17032 /* In bidi-reordered rows, there could be several rows
17033 occluding point, all of them belonging to the same
17034 continued line. We need to find the row which fits
17035 CHARPOS the best. */
17036 for (g = row->glyphs[TEXT_AREA];
17037 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17038 g++)
17040 if (!STRINGP (g->object))
17042 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17044 mindif = eabs (g->charpos - charpos);
17045 best_row = row;
17046 /* Exact match always wins. */
17047 if (mindif == 0)
17048 return best_row;
17053 else if (best_row && !row->continued_p)
17054 return best_row;
17055 ++row;
17060 /* Try to redisplay window W by reusing its existing display. W's
17061 current matrix must be up to date when this function is called,
17062 i.e. window_end_valid must not be nil.
17064 Value is
17066 1 if display has been updated
17067 0 if otherwise unsuccessful
17068 -1 if redisplay with same window start is known not to succeed
17070 The following steps are performed:
17072 1. Find the last row in the current matrix of W that is not
17073 affected by changes at the start of current_buffer. If no such row
17074 is found, give up.
17076 2. Find the first row in W's current matrix that is not affected by
17077 changes at the end of current_buffer. Maybe there is no such row.
17079 3. Display lines beginning with the row + 1 found in step 1 to the
17080 row found in step 2 or, if step 2 didn't find a row, to the end of
17081 the window.
17083 4. If cursor is not known to appear on the window, give up.
17085 5. If display stopped at the row found in step 2, scroll the
17086 display and current matrix as needed.
17088 6. Maybe display some lines at the end of W, if we must. This can
17089 happen under various circumstances, like a partially visible line
17090 becoming fully visible, or because newly displayed lines are displayed
17091 in smaller font sizes.
17093 7. Update W's window end information. */
17095 static int
17096 try_window_id (struct window *w)
17098 struct frame *f = XFRAME (w->frame);
17099 struct glyph_matrix *current_matrix = w->current_matrix;
17100 struct glyph_matrix *desired_matrix = w->desired_matrix;
17101 struct glyph_row *last_unchanged_at_beg_row;
17102 struct glyph_row *first_unchanged_at_end_row;
17103 struct glyph_row *row;
17104 struct glyph_row *bottom_row;
17105 int bottom_vpos;
17106 struct it it;
17107 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17108 int dvpos, dy;
17109 struct text_pos start_pos;
17110 struct run run;
17111 int first_unchanged_at_end_vpos = 0;
17112 struct glyph_row *last_text_row, *last_text_row_at_end;
17113 struct text_pos start;
17114 ptrdiff_t first_changed_charpos, last_changed_charpos;
17116 #ifdef GLYPH_DEBUG
17117 if (inhibit_try_window_id)
17118 return 0;
17119 #endif
17121 /* This is handy for debugging. */
17122 #if 0
17123 #define GIVE_UP(X) \
17124 do { \
17125 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17126 return 0; \
17127 } while (0)
17128 #else
17129 #define GIVE_UP(X) return 0
17130 #endif
17132 SET_TEXT_POS_FROM_MARKER (start, w->start);
17134 /* Don't use this for mini-windows because these can show
17135 messages and mini-buffers, and we don't handle that here. */
17136 if (MINI_WINDOW_P (w))
17137 GIVE_UP (1);
17139 /* This flag is used to prevent redisplay optimizations. */
17140 if (windows_or_buffers_changed || cursor_type_changed)
17141 GIVE_UP (2);
17143 /* Verify that narrowing has not changed.
17144 Also verify that we were not told to prevent redisplay optimizations.
17145 It would be nice to further
17146 reduce the number of cases where this prevents try_window_id. */
17147 if (current_buffer->clip_changed
17148 || current_buffer->prevent_redisplay_optimizations_p)
17149 GIVE_UP (3);
17151 /* Window must either use window-based redisplay or be full width. */
17152 if (!FRAME_WINDOW_P (f)
17153 && (!FRAME_LINE_INS_DEL_OK (f)
17154 || !WINDOW_FULL_WIDTH_P (w)))
17155 GIVE_UP (4);
17157 /* Give up if point is known NOT to appear in W. */
17158 if (PT < CHARPOS (start))
17159 GIVE_UP (5);
17161 /* Another way to prevent redisplay optimizations. */
17162 if (w->last_modified == 0)
17163 GIVE_UP (6);
17165 /* Verify that window is not hscrolled. */
17166 if (w->hscroll != 0)
17167 GIVE_UP (7);
17169 /* Verify that display wasn't paused. */
17170 if (NILP (w->window_end_valid))
17171 GIVE_UP (8);
17173 /* Can't use this if highlighting a region because a cursor movement
17174 will do more than just set the cursor. */
17175 if (!NILP (Vtransient_mark_mode)
17176 && !NILP (BVAR (current_buffer, mark_active)))
17177 GIVE_UP (9);
17179 /* Likewise if highlighting trailing whitespace. */
17180 if (!NILP (Vshow_trailing_whitespace))
17181 GIVE_UP (11);
17183 /* Likewise if showing a region. */
17184 if (!NILP (w->region_showing))
17185 GIVE_UP (10);
17187 /* Can't use this if overlay arrow position and/or string have
17188 changed. */
17189 if (overlay_arrows_changed_p ())
17190 GIVE_UP (12);
17192 /* When word-wrap is on, adding a space to the first word of a
17193 wrapped line can change the wrap position, altering the line
17194 above it. It might be worthwhile to handle this more
17195 intelligently, but for now just redisplay from scratch. */
17196 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17197 GIVE_UP (21);
17199 /* Under bidi reordering, adding or deleting a character in the
17200 beginning of a paragraph, before the first strong directional
17201 character, can change the base direction of the paragraph (unless
17202 the buffer specifies a fixed paragraph direction), which will
17203 require to redisplay the whole paragraph. It might be worthwhile
17204 to find the paragraph limits and widen the range of redisplayed
17205 lines to that, but for now just give up this optimization and
17206 redisplay from scratch. */
17207 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17208 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17209 GIVE_UP (22);
17211 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17212 only if buffer has really changed. The reason is that the gap is
17213 initially at Z for freshly visited files. The code below would
17214 set end_unchanged to 0 in that case. */
17215 if (MODIFF > SAVE_MODIFF
17216 /* This seems to happen sometimes after saving a buffer. */
17217 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17219 if (GPT - BEG < BEG_UNCHANGED)
17220 BEG_UNCHANGED = GPT - BEG;
17221 if (Z - GPT < END_UNCHANGED)
17222 END_UNCHANGED = Z - GPT;
17225 /* The position of the first and last character that has been changed. */
17226 first_changed_charpos = BEG + BEG_UNCHANGED;
17227 last_changed_charpos = Z - END_UNCHANGED;
17229 /* If window starts after a line end, and the last change is in
17230 front of that newline, then changes don't affect the display.
17231 This case happens with stealth-fontification. Note that although
17232 the display is unchanged, glyph positions in the matrix have to
17233 be adjusted, of course. */
17234 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17235 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17236 && ((last_changed_charpos < CHARPOS (start)
17237 && CHARPOS (start) == BEGV)
17238 || (last_changed_charpos < CHARPOS (start) - 1
17239 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17241 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17242 struct glyph_row *r0;
17244 /* Compute how many chars/bytes have been added to or removed
17245 from the buffer. */
17246 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17247 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17248 Z_delta = Z - Z_old;
17249 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17251 /* Give up if PT is not in the window. Note that it already has
17252 been checked at the start of try_window_id that PT is not in
17253 front of the window start. */
17254 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17255 GIVE_UP (13);
17257 /* If window start is unchanged, we can reuse the whole matrix
17258 as is, after adjusting glyph positions. No need to compute
17259 the window end again, since its offset from Z hasn't changed. */
17260 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17261 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17262 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17263 /* PT must not be in a partially visible line. */
17264 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17265 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17267 /* Adjust positions in the glyph matrix. */
17268 if (Z_delta || Z_delta_bytes)
17270 struct glyph_row *r1
17271 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17272 increment_matrix_positions (w->current_matrix,
17273 MATRIX_ROW_VPOS (r0, current_matrix),
17274 MATRIX_ROW_VPOS (r1, current_matrix),
17275 Z_delta, Z_delta_bytes);
17278 /* Set the cursor. */
17279 row = row_containing_pos (w, PT, r0, NULL, 0);
17280 if (row)
17281 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17282 else
17283 abort ();
17284 return 1;
17288 /* Handle the case that changes are all below what is displayed in
17289 the window, and that PT is in the window. This shortcut cannot
17290 be taken if ZV is visible in the window, and text has been added
17291 there that is visible in the window. */
17292 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17293 /* ZV is not visible in the window, or there are no
17294 changes at ZV, actually. */
17295 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17296 || first_changed_charpos == last_changed_charpos))
17298 struct glyph_row *r0;
17300 /* Give up if PT is not in the window. Note that it already has
17301 been checked at the start of try_window_id that PT is not in
17302 front of the window start. */
17303 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17304 GIVE_UP (14);
17306 /* If window start is unchanged, we can reuse the whole matrix
17307 as is, without changing glyph positions since no text has
17308 been added/removed in front of the window end. */
17309 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17310 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17311 /* PT must not be in a partially visible line. */
17312 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17313 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17315 /* We have to compute the window end anew since text
17316 could have been added/removed after it. */
17317 w->window_end_pos
17318 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17319 w->window_end_bytepos
17320 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17322 /* Set the cursor. */
17323 row = row_containing_pos (w, PT, r0, NULL, 0);
17324 if (row)
17325 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17326 else
17327 abort ();
17328 return 2;
17332 /* Give up if window start is in the changed area.
17334 The condition used to read
17336 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17338 but why that was tested escapes me at the moment. */
17339 if (CHARPOS (start) >= first_changed_charpos
17340 && CHARPOS (start) <= last_changed_charpos)
17341 GIVE_UP (15);
17343 /* Check that window start agrees with the start of the first glyph
17344 row in its current matrix. Check this after we know the window
17345 start is not in changed text, otherwise positions would not be
17346 comparable. */
17347 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17348 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17349 GIVE_UP (16);
17351 /* Give up if the window ends in strings. Overlay strings
17352 at the end are difficult to handle, so don't try. */
17353 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17354 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17355 GIVE_UP (20);
17357 /* Compute the position at which we have to start displaying new
17358 lines. Some of the lines at the top of the window might be
17359 reusable because they are not displaying changed text. Find the
17360 last row in W's current matrix not affected by changes at the
17361 start of current_buffer. Value is null if changes start in the
17362 first line of window. */
17363 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17364 if (last_unchanged_at_beg_row)
17366 /* Avoid starting to display in the middle of a character, a TAB
17367 for instance. This is easier than to set up the iterator
17368 exactly, and it's not a frequent case, so the additional
17369 effort wouldn't really pay off. */
17370 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17371 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17372 && last_unchanged_at_beg_row > w->current_matrix->rows)
17373 --last_unchanged_at_beg_row;
17375 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17376 GIVE_UP (17);
17378 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17379 GIVE_UP (18);
17380 start_pos = it.current.pos;
17382 /* Start displaying new lines in the desired matrix at the same
17383 vpos we would use in the current matrix, i.e. below
17384 last_unchanged_at_beg_row. */
17385 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17386 current_matrix);
17387 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17388 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17390 eassert (it.hpos == 0 && it.current_x == 0);
17392 else
17394 /* There are no reusable lines at the start of the window.
17395 Start displaying in the first text line. */
17396 start_display (&it, w, start);
17397 it.vpos = it.first_vpos;
17398 start_pos = it.current.pos;
17401 /* Find the first row that is not affected by changes at the end of
17402 the buffer. Value will be null if there is no unchanged row, in
17403 which case we must redisplay to the end of the window. delta
17404 will be set to the value by which buffer positions beginning with
17405 first_unchanged_at_end_row have to be adjusted due to text
17406 changes. */
17407 first_unchanged_at_end_row
17408 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17409 IF_DEBUG (debug_delta = delta);
17410 IF_DEBUG (debug_delta_bytes = delta_bytes);
17412 /* Set stop_pos to the buffer position up to which we will have to
17413 display new lines. If first_unchanged_at_end_row != NULL, this
17414 is the buffer position of the start of the line displayed in that
17415 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17416 that we don't stop at a buffer position. */
17417 stop_pos = 0;
17418 if (first_unchanged_at_end_row)
17420 eassert (last_unchanged_at_beg_row == NULL
17421 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17423 /* If this is a continuation line, move forward to the next one
17424 that isn't. Changes in lines above affect this line.
17425 Caution: this may move first_unchanged_at_end_row to a row
17426 not displaying text. */
17427 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17428 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17429 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17430 < it.last_visible_y))
17431 ++first_unchanged_at_end_row;
17433 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17434 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17435 >= it.last_visible_y))
17436 first_unchanged_at_end_row = NULL;
17437 else
17439 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17440 + delta);
17441 first_unchanged_at_end_vpos
17442 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17443 eassert (stop_pos >= Z - END_UNCHANGED);
17446 else if (last_unchanged_at_beg_row == NULL)
17447 GIVE_UP (19);
17450 #ifdef GLYPH_DEBUG
17452 /* Either there is no unchanged row at the end, or the one we have
17453 now displays text. This is a necessary condition for the window
17454 end pos calculation at the end of this function. */
17455 eassert (first_unchanged_at_end_row == NULL
17456 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17458 debug_last_unchanged_at_beg_vpos
17459 = (last_unchanged_at_beg_row
17460 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17461 : -1);
17462 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17464 #endif /* GLYPH_DEBUG */
17467 /* Display new lines. Set last_text_row to the last new line
17468 displayed which has text on it, i.e. might end up as being the
17469 line where the window_end_vpos is. */
17470 w->cursor.vpos = -1;
17471 last_text_row = NULL;
17472 overlay_arrow_seen = 0;
17473 while (it.current_y < it.last_visible_y
17474 && !fonts_changed_p
17475 && (first_unchanged_at_end_row == NULL
17476 || IT_CHARPOS (it) < stop_pos))
17478 if (display_line (&it))
17479 last_text_row = it.glyph_row - 1;
17482 if (fonts_changed_p)
17483 return -1;
17486 /* Compute differences in buffer positions, y-positions etc. for
17487 lines reused at the bottom of the window. Compute what we can
17488 scroll. */
17489 if (first_unchanged_at_end_row
17490 /* No lines reused because we displayed everything up to the
17491 bottom of the window. */
17492 && it.current_y < it.last_visible_y)
17494 dvpos = (it.vpos
17495 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17496 current_matrix));
17497 dy = it.current_y - first_unchanged_at_end_row->y;
17498 run.current_y = first_unchanged_at_end_row->y;
17499 run.desired_y = run.current_y + dy;
17500 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17502 else
17504 delta = delta_bytes = dvpos = dy
17505 = run.current_y = run.desired_y = run.height = 0;
17506 first_unchanged_at_end_row = NULL;
17508 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17511 /* Find the cursor if not already found. We have to decide whether
17512 PT will appear on this window (it sometimes doesn't, but this is
17513 not a very frequent case.) This decision has to be made before
17514 the current matrix is altered. A value of cursor.vpos < 0 means
17515 that PT is either in one of the lines beginning at
17516 first_unchanged_at_end_row or below the window. Don't care for
17517 lines that might be displayed later at the window end; as
17518 mentioned, this is not a frequent case. */
17519 if (w->cursor.vpos < 0)
17521 /* Cursor in unchanged rows at the top? */
17522 if (PT < CHARPOS (start_pos)
17523 && last_unchanged_at_beg_row)
17525 row = row_containing_pos (w, PT,
17526 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17527 last_unchanged_at_beg_row + 1, 0);
17528 if (row)
17529 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17532 /* Start from first_unchanged_at_end_row looking for PT. */
17533 else if (first_unchanged_at_end_row)
17535 row = row_containing_pos (w, PT - delta,
17536 first_unchanged_at_end_row, NULL, 0);
17537 if (row)
17538 set_cursor_from_row (w, row, w->current_matrix, delta,
17539 delta_bytes, dy, dvpos);
17542 /* Give up if cursor was not found. */
17543 if (w->cursor.vpos < 0)
17545 clear_glyph_matrix (w->desired_matrix);
17546 return -1;
17550 /* Don't let the cursor end in the scroll margins. */
17552 int this_scroll_margin, cursor_height;
17554 this_scroll_margin =
17555 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17556 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17557 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17559 if ((w->cursor.y < this_scroll_margin
17560 && CHARPOS (start) > BEGV)
17561 /* Old redisplay didn't take scroll margin into account at the bottom,
17562 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17563 || (w->cursor.y + (make_cursor_line_fully_visible_p
17564 ? cursor_height + this_scroll_margin
17565 : 1)) > it.last_visible_y)
17567 w->cursor.vpos = -1;
17568 clear_glyph_matrix (w->desired_matrix);
17569 return -1;
17573 /* Scroll the display. Do it before changing the current matrix so
17574 that xterm.c doesn't get confused about where the cursor glyph is
17575 found. */
17576 if (dy && run.height)
17578 update_begin (f);
17580 if (FRAME_WINDOW_P (f))
17582 FRAME_RIF (f)->update_window_begin_hook (w);
17583 FRAME_RIF (f)->clear_window_mouse_face (w);
17584 FRAME_RIF (f)->scroll_run_hook (w, &run);
17585 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17587 else
17589 /* Terminal frame. In this case, dvpos gives the number of
17590 lines to scroll by; dvpos < 0 means scroll up. */
17591 int from_vpos
17592 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17593 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17594 int end = (WINDOW_TOP_EDGE_LINE (w)
17595 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17596 + window_internal_height (w));
17598 #if defined (HAVE_GPM) || defined (MSDOS)
17599 x_clear_window_mouse_face (w);
17600 #endif
17601 /* Perform the operation on the screen. */
17602 if (dvpos > 0)
17604 /* Scroll last_unchanged_at_beg_row to the end of the
17605 window down dvpos lines. */
17606 set_terminal_window (f, end);
17608 /* On dumb terminals delete dvpos lines at the end
17609 before inserting dvpos empty lines. */
17610 if (!FRAME_SCROLL_REGION_OK (f))
17611 ins_del_lines (f, end - dvpos, -dvpos);
17613 /* Insert dvpos empty lines in front of
17614 last_unchanged_at_beg_row. */
17615 ins_del_lines (f, from, dvpos);
17617 else if (dvpos < 0)
17619 /* Scroll up last_unchanged_at_beg_vpos to the end of
17620 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17621 set_terminal_window (f, end);
17623 /* Delete dvpos lines in front of
17624 last_unchanged_at_beg_vpos. ins_del_lines will set
17625 the cursor to the given vpos and emit |dvpos| delete
17626 line sequences. */
17627 ins_del_lines (f, from + dvpos, dvpos);
17629 /* On a dumb terminal insert dvpos empty lines at the
17630 end. */
17631 if (!FRAME_SCROLL_REGION_OK (f))
17632 ins_del_lines (f, end + dvpos, -dvpos);
17635 set_terminal_window (f, 0);
17638 update_end (f);
17641 /* Shift reused rows of the current matrix to the right position.
17642 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17643 text. */
17644 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17645 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17646 if (dvpos < 0)
17648 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17649 bottom_vpos, dvpos);
17650 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17651 bottom_vpos, 0);
17653 else if (dvpos > 0)
17655 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17656 bottom_vpos, dvpos);
17657 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17658 first_unchanged_at_end_vpos + dvpos, 0);
17661 /* For frame-based redisplay, make sure that current frame and window
17662 matrix are in sync with respect to glyph memory. */
17663 if (!FRAME_WINDOW_P (f))
17664 sync_frame_with_window_matrix_rows (w);
17666 /* Adjust buffer positions in reused rows. */
17667 if (delta || delta_bytes)
17668 increment_matrix_positions (current_matrix,
17669 first_unchanged_at_end_vpos + dvpos,
17670 bottom_vpos, delta, delta_bytes);
17672 /* Adjust Y positions. */
17673 if (dy)
17674 shift_glyph_matrix (w, current_matrix,
17675 first_unchanged_at_end_vpos + dvpos,
17676 bottom_vpos, dy);
17678 if (first_unchanged_at_end_row)
17680 first_unchanged_at_end_row += dvpos;
17681 if (first_unchanged_at_end_row->y >= it.last_visible_y
17682 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17683 first_unchanged_at_end_row = NULL;
17686 /* If scrolling up, there may be some lines to display at the end of
17687 the window. */
17688 last_text_row_at_end = NULL;
17689 if (dy < 0)
17691 /* Scrolling up can leave for example a partially visible line
17692 at the end of the window to be redisplayed. */
17693 /* Set last_row to the glyph row in the current matrix where the
17694 window end line is found. It has been moved up or down in
17695 the matrix by dvpos. */
17696 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17697 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17699 /* If last_row is the window end line, it should display text. */
17700 eassert (last_row->displays_text_p);
17702 /* If window end line was partially visible before, begin
17703 displaying at that line. Otherwise begin displaying with the
17704 line following it. */
17705 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17707 init_to_row_start (&it, w, last_row);
17708 it.vpos = last_vpos;
17709 it.current_y = last_row->y;
17711 else
17713 init_to_row_end (&it, w, last_row);
17714 it.vpos = 1 + last_vpos;
17715 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17716 ++last_row;
17719 /* We may start in a continuation line. If so, we have to
17720 get the right continuation_lines_width and current_x. */
17721 it.continuation_lines_width = last_row->continuation_lines_width;
17722 it.hpos = it.current_x = 0;
17724 /* Display the rest of the lines at the window end. */
17725 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17726 while (it.current_y < it.last_visible_y
17727 && !fonts_changed_p)
17729 /* Is it always sure that the display agrees with lines in
17730 the current matrix? I don't think so, so we mark rows
17731 displayed invalid in the current matrix by setting their
17732 enabled_p flag to zero. */
17733 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17734 if (display_line (&it))
17735 last_text_row_at_end = it.glyph_row - 1;
17739 /* Update window_end_pos and window_end_vpos. */
17740 if (first_unchanged_at_end_row
17741 && !last_text_row_at_end)
17743 /* Window end line if one of the preserved rows from the current
17744 matrix. Set row to the last row displaying text in current
17745 matrix starting at first_unchanged_at_end_row, after
17746 scrolling. */
17747 eassert (first_unchanged_at_end_row->displays_text_p);
17748 row = find_last_row_displaying_text (w->current_matrix, &it,
17749 first_unchanged_at_end_row);
17750 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17752 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17753 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17754 w->window_end_vpos
17755 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17756 eassert (w->window_end_bytepos >= 0);
17757 IF_DEBUG (debug_method_add (w, "A"));
17759 else if (last_text_row_at_end)
17761 w->window_end_pos
17762 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17763 w->window_end_bytepos
17764 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17765 w->window_end_vpos
17766 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17767 eassert (w->window_end_bytepos >= 0);
17768 IF_DEBUG (debug_method_add (w, "B"));
17770 else if (last_text_row)
17772 /* We have displayed either to the end of the window or at the
17773 end of the window, i.e. the last row with text is to be found
17774 in the desired matrix. */
17775 w->window_end_pos
17776 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17777 w->window_end_bytepos
17778 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17779 w->window_end_vpos
17780 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17781 eassert (w->window_end_bytepos >= 0);
17783 else if (first_unchanged_at_end_row == NULL
17784 && last_text_row == NULL
17785 && last_text_row_at_end == NULL)
17787 /* Displayed to end of window, but no line containing text was
17788 displayed. Lines were deleted at the end of the window. */
17789 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17790 int vpos = XFASTINT (w->window_end_vpos);
17791 struct glyph_row *current_row = current_matrix->rows + vpos;
17792 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17794 for (row = NULL;
17795 row == NULL && vpos >= first_vpos;
17796 --vpos, --current_row, --desired_row)
17798 if (desired_row->enabled_p)
17800 if (desired_row->displays_text_p)
17801 row = desired_row;
17803 else if (current_row->displays_text_p)
17804 row = current_row;
17807 eassert (row != NULL);
17808 w->window_end_vpos = make_number (vpos + 1);
17809 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17810 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17811 eassert (w->window_end_bytepos >= 0);
17812 IF_DEBUG (debug_method_add (w, "C"));
17814 else
17815 abort ();
17817 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17818 debug_end_vpos = XFASTINT (w->window_end_vpos));
17820 /* Record that display has not been completed. */
17821 w->window_end_valid = Qnil;
17822 w->desired_matrix->no_scrolling_p = 1;
17823 return 3;
17825 #undef GIVE_UP
17830 /***********************************************************************
17831 More debugging support
17832 ***********************************************************************/
17834 #ifdef GLYPH_DEBUG
17836 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17837 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17838 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17841 /* Dump the contents of glyph matrix MATRIX on stderr.
17843 GLYPHS 0 means don't show glyph contents.
17844 GLYPHS 1 means show glyphs in short form
17845 GLYPHS > 1 means show glyphs in long form. */
17847 void
17848 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17850 int i;
17851 for (i = 0; i < matrix->nrows; ++i)
17852 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17856 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17857 the glyph row and area where the glyph comes from. */
17859 void
17860 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17862 if (glyph->type == CHAR_GLYPH)
17864 fprintf (stderr,
17865 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17866 glyph - row->glyphs[TEXT_AREA],
17867 'C',
17868 glyph->charpos,
17869 (BUFFERP (glyph->object)
17870 ? 'B'
17871 : (STRINGP (glyph->object)
17872 ? 'S'
17873 : '-')),
17874 glyph->pixel_width,
17875 glyph->u.ch,
17876 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17877 ? glyph->u.ch
17878 : '.'),
17879 glyph->face_id,
17880 glyph->left_box_line_p,
17881 glyph->right_box_line_p);
17883 else if (glyph->type == STRETCH_GLYPH)
17885 fprintf (stderr,
17886 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17887 glyph - row->glyphs[TEXT_AREA],
17888 'S',
17889 glyph->charpos,
17890 (BUFFERP (glyph->object)
17891 ? 'B'
17892 : (STRINGP (glyph->object)
17893 ? 'S'
17894 : '-')),
17895 glyph->pixel_width,
17897 '.',
17898 glyph->face_id,
17899 glyph->left_box_line_p,
17900 glyph->right_box_line_p);
17902 else if (glyph->type == IMAGE_GLYPH)
17904 fprintf (stderr,
17905 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17906 glyph - row->glyphs[TEXT_AREA],
17907 'I',
17908 glyph->charpos,
17909 (BUFFERP (glyph->object)
17910 ? 'B'
17911 : (STRINGP (glyph->object)
17912 ? 'S'
17913 : '-')),
17914 glyph->pixel_width,
17915 glyph->u.img_id,
17916 '.',
17917 glyph->face_id,
17918 glyph->left_box_line_p,
17919 glyph->right_box_line_p);
17921 else if (glyph->type == COMPOSITE_GLYPH)
17923 fprintf (stderr,
17924 " %5td %4c %6"pI"d %c %3d 0x%05x",
17925 glyph - row->glyphs[TEXT_AREA],
17926 '+',
17927 glyph->charpos,
17928 (BUFFERP (glyph->object)
17929 ? 'B'
17930 : (STRINGP (glyph->object)
17931 ? 'S'
17932 : '-')),
17933 glyph->pixel_width,
17934 glyph->u.cmp.id);
17935 if (glyph->u.cmp.automatic)
17936 fprintf (stderr,
17937 "[%d-%d]",
17938 glyph->slice.cmp.from, glyph->slice.cmp.to);
17939 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17940 glyph->face_id,
17941 glyph->left_box_line_p,
17942 glyph->right_box_line_p);
17947 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17948 GLYPHS 0 means don't show glyph contents.
17949 GLYPHS 1 means show glyphs in short form
17950 GLYPHS > 1 means show glyphs in long form. */
17952 void
17953 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17955 if (glyphs != 1)
17957 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17958 fprintf (stderr, "======================================================================\n");
17960 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17961 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17962 vpos,
17963 MATRIX_ROW_START_CHARPOS (row),
17964 MATRIX_ROW_END_CHARPOS (row),
17965 row->used[TEXT_AREA],
17966 row->contains_overlapping_glyphs_p,
17967 row->enabled_p,
17968 row->truncated_on_left_p,
17969 row->truncated_on_right_p,
17970 row->continued_p,
17971 MATRIX_ROW_CONTINUATION_LINE_P (row),
17972 row->displays_text_p,
17973 row->ends_at_zv_p,
17974 row->fill_line_p,
17975 row->ends_in_middle_of_char_p,
17976 row->starts_in_middle_of_char_p,
17977 row->mouse_face_p,
17978 row->x,
17979 row->y,
17980 row->pixel_width,
17981 row->height,
17982 row->visible_height,
17983 row->ascent,
17984 row->phys_ascent);
17985 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
17986 row->end.overlay_string_index,
17987 row->continuation_lines_width);
17988 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17989 CHARPOS (row->start.string_pos),
17990 CHARPOS (row->end.string_pos));
17991 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17992 row->end.dpvec_index);
17995 if (glyphs > 1)
17997 int area;
17999 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18001 struct glyph *glyph = row->glyphs[area];
18002 struct glyph *glyph_end = glyph + row->used[area];
18004 /* Glyph for a line end in text. */
18005 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18006 ++glyph_end;
18008 if (glyph < glyph_end)
18009 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18011 for (; glyph < glyph_end; ++glyph)
18012 dump_glyph (row, glyph, area);
18015 else if (glyphs == 1)
18017 int area;
18019 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18021 char *s = alloca (row->used[area] + 1);
18022 int i;
18024 for (i = 0; i < row->used[area]; ++i)
18026 struct glyph *glyph = row->glyphs[area] + i;
18027 if (glyph->type == CHAR_GLYPH
18028 && glyph->u.ch < 0x80
18029 && glyph->u.ch >= ' ')
18030 s[i] = glyph->u.ch;
18031 else
18032 s[i] = '.';
18035 s[i] = '\0';
18036 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18042 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18043 Sdump_glyph_matrix, 0, 1, "p",
18044 doc: /* Dump the current matrix of the selected window to stderr.
18045 Shows contents of glyph row structures. With non-nil
18046 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18047 glyphs in short form, otherwise show glyphs in long form. */)
18048 (Lisp_Object glyphs)
18050 struct window *w = XWINDOW (selected_window);
18051 struct buffer *buffer = XBUFFER (w->buffer);
18053 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18054 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18055 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18056 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18057 fprintf (stderr, "=============================================\n");
18058 dump_glyph_matrix (w->current_matrix,
18059 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18060 return Qnil;
18064 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18065 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18066 (void)
18068 struct frame *f = XFRAME (selected_frame);
18069 dump_glyph_matrix (f->current_matrix, 1);
18070 return Qnil;
18074 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18075 doc: /* Dump glyph row ROW to stderr.
18076 GLYPH 0 means don't dump glyphs.
18077 GLYPH 1 means dump glyphs in short form.
18078 GLYPH > 1 or omitted means dump glyphs in long form. */)
18079 (Lisp_Object row, Lisp_Object glyphs)
18081 struct glyph_matrix *matrix;
18082 EMACS_INT vpos;
18084 CHECK_NUMBER (row);
18085 matrix = XWINDOW (selected_window)->current_matrix;
18086 vpos = XINT (row);
18087 if (vpos >= 0 && vpos < matrix->nrows)
18088 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18089 vpos,
18090 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18091 return Qnil;
18095 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18096 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18097 GLYPH 0 means don't dump glyphs.
18098 GLYPH 1 means dump glyphs in short form.
18099 GLYPH > 1 or omitted means dump glyphs in long form. */)
18100 (Lisp_Object row, Lisp_Object glyphs)
18102 struct frame *sf = SELECTED_FRAME ();
18103 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18104 EMACS_INT vpos;
18106 CHECK_NUMBER (row);
18107 vpos = XINT (row);
18108 if (vpos >= 0 && vpos < m->nrows)
18109 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18110 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18111 return Qnil;
18115 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18116 doc: /* Toggle tracing of redisplay.
18117 With ARG, turn tracing on if and only if ARG is positive. */)
18118 (Lisp_Object arg)
18120 if (NILP (arg))
18121 trace_redisplay_p = !trace_redisplay_p;
18122 else
18124 arg = Fprefix_numeric_value (arg);
18125 trace_redisplay_p = XINT (arg) > 0;
18128 return Qnil;
18132 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18133 doc: /* Like `format', but print result to stderr.
18134 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18135 (ptrdiff_t nargs, Lisp_Object *args)
18137 Lisp_Object s = Fformat (nargs, args);
18138 fprintf (stderr, "%s", SDATA (s));
18139 return Qnil;
18142 #endif /* GLYPH_DEBUG */
18146 /***********************************************************************
18147 Building Desired Matrix Rows
18148 ***********************************************************************/
18150 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18151 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18153 static struct glyph_row *
18154 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18156 struct frame *f = XFRAME (WINDOW_FRAME (w));
18157 struct buffer *buffer = XBUFFER (w->buffer);
18158 struct buffer *old = current_buffer;
18159 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18160 int arrow_len = SCHARS (overlay_arrow_string);
18161 const unsigned char *arrow_end = arrow_string + arrow_len;
18162 const unsigned char *p;
18163 struct it it;
18164 int multibyte_p;
18165 int n_glyphs_before;
18167 set_buffer_temp (buffer);
18168 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18169 it.glyph_row->used[TEXT_AREA] = 0;
18170 SET_TEXT_POS (it.position, 0, 0);
18172 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18173 p = arrow_string;
18174 while (p < arrow_end)
18176 Lisp_Object face, ilisp;
18178 /* Get the next character. */
18179 if (multibyte_p)
18180 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18181 else
18183 it.c = it.char_to_display = *p, it.len = 1;
18184 if (! ASCII_CHAR_P (it.c))
18185 it.char_to_display = BYTE8_TO_CHAR (it.c);
18187 p += it.len;
18189 /* Get its face. */
18190 ilisp = make_number (p - arrow_string);
18191 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18192 it.face_id = compute_char_face (f, it.char_to_display, face);
18194 /* Compute its width, get its glyphs. */
18195 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18196 SET_TEXT_POS (it.position, -1, -1);
18197 PRODUCE_GLYPHS (&it);
18199 /* If this character doesn't fit any more in the line, we have
18200 to remove some glyphs. */
18201 if (it.current_x > it.last_visible_x)
18203 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18204 break;
18208 set_buffer_temp (old);
18209 return it.glyph_row;
18213 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18214 glyphs to insert is determined by produce_special_glyphs. */
18216 static void
18217 insert_left_trunc_glyphs (struct it *it)
18219 struct it truncate_it;
18220 struct glyph *from, *end, *to, *toend;
18222 eassert (!FRAME_WINDOW_P (it->f)
18223 || (!it->glyph_row->reversed_p
18224 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18225 || (it->glyph_row->reversed_p
18226 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18228 /* Get the truncation glyphs. */
18229 truncate_it = *it;
18230 truncate_it.current_x = 0;
18231 truncate_it.face_id = DEFAULT_FACE_ID;
18232 truncate_it.glyph_row = &scratch_glyph_row;
18233 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18234 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18235 truncate_it.object = make_number (0);
18236 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18238 /* Overwrite glyphs from IT with truncation glyphs. */
18239 if (!it->glyph_row->reversed_p)
18241 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18243 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18244 end = from + tused;
18245 to = it->glyph_row->glyphs[TEXT_AREA];
18246 toend = to + it->glyph_row->used[TEXT_AREA];
18247 if (FRAME_WINDOW_P (it->f))
18249 /* On GUI frames, when variable-size fonts are displayed,
18250 the truncation glyphs may need more pixels than the row's
18251 glyphs they overwrite. We overwrite more glyphs to free
18252 enough screen real estate, and enlarge the stretch glyph
18253 on the right (see display_line), if there is one, to
18254 preserve the screen position of the truncation glyphs on
18255 the right. */
18256 int w = 0;
18257 struct glyph *g = to;
18258 short used;
18260 /* The first glyph could be partially visible, in which case
18261 it->glyph_row->x will be negative. But we want the left
18262 truncation glyphs to be aligned at the left margin of the
18263 window, so we override the x coordinate at which the row
18264 will begin. */
18265 it->glyph_row->x = 0;
18266 while (g < toend && w < it->truncation_pixel_width)
18268 w += g->pixel_width;
18269 ++g;
18271 if (g - to - tused > 0)
18273 memmove (to + tused, g, (toend - g) * sizeof(*g));
18274 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18276 used = it->glyph_row->used[TEXT_AREA];
18277 if (it->glyph_row->truncated_on_right_p
18278 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18279 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18280 == STRETCH_GLYPH)
18282 int extra = w - it->truncation_pixel_width;
18284 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18288 while (from < end)
18289 *to++ = *from++;
18291 /* There may be padding glyphs left over. Overwrite them too. */
18292 if (!FRAME_WINDOW_P (it->f))
18294 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18296 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18297 while (from < end)
18298 *to++ = *from++;
18302 if (to > toend)
18303 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18305 else
18307 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18309 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18310 that back to front. */
18311 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18312 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18313 toend = it->glyph_row->glyphs[TEXT_AREA];
18314 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18315 if (FRAME_WINDOW_P (it->f))
18317 int w = 0;
18318 struct glyph *g = to;
18320 while (g >= toend && w < it->truncation_pixel_width)
18322 w += g->pixel_width;
18323 --g;
18325 if (to - g - tused > 0)
18326 to = g + tused;
18327 if (it->glyph_row->truncated_on_right_p
18328 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18329 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18331 int extra = w - it->truncation_pixel_width;
18333 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18337 while (from >= end && to >= toend)
18338 *to-- = *from--;
18339 if (!FRAME_WINDOW_P (it->f))
18341 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18343 from =
18344 truncate_it.glyph_row->glyphs[TEXT_AREA]
18345 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18346 while (from >= end && to >= toend)
18347 *to-- = *from--;
18350 if (from >= end)
18352 /* Need to free some room before prepending additional
18353 glyphs. */
18354 int move_by = from - end + 1;
18355 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18356 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18358 for ( ; g >= g0; g--)
18359 g[move_by] = *g;
18360 while (from >= end)
18361 *to-- = *from--;
18362 it->glyph_row->used[TEXT_AREA] += move_by;
18367 /* Compute the hash code for ROW. */
18368 unsigned
18369 row_hash (struct glyph_row *row)
18371 int area, k;
18372 unsigned hashval = 0;
18374 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18375 for (k = 0; k < row->used[area]; ++k)
18376 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18377 + row->glyphs[area][k].u.val
18378 + row->glyphs[area][k].face_id
18379 + row->glyphs[area][k].padding_p
18380 + (row->glyphs[area][k].type << 2));
18382 return hashval;
18385 /* Compute the pixel height and width of IT->glyph_row.
18387 Most of the time, ascent and height of a display line will be equal
18388 to the max_ascent and max_height values of the display iterator
18389 structure. This is not the case if
18391 1. We hit ZV without displaying anything. In this case, max_ascent
18392 and max_height will be zero.
18394 2. We have some glyphs that don't contribute to the line height.
18395 (The glyph row flag contributes_to_line_height_p is for future
18396 pixmap extensions).
18398 The first case is easily covered by using default values because in
18399 these cases, the line height does not really matter, except that it
18400 must not be zero. */
18402 static void
18403 compute_line_metrics (struct it *it)
18405 struct glyph_row *row = it->glyph_row;
18407 if (FRAME_WINDOW_P (it->f))
18409 int i, min_y, max_y;
18411 /* The line may consist of one space only, that was added to
18412 place the cursor on it. If so, the row's height hasn't been
18413 computed yet. */
18414 if (row->height == 0)
18416 if (it->max_ascent + it->max_descent == 0)
18417 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18418 row->ascent = it->max_ascent;
18419 row->height = it->max_ascent + it->max_descent;
18420 row->phys_ascent = it->max_phys_ascent;
18421 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18422 row->extra_line_spacing = it->max_extra_line_spacing;
18425 /* Compute the width of this line. */
18426 row->pixel_width = row->x;
18427 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18428 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18430 eassert (row->pixel_width >= 0);
18431 eassert (row->ascent >= 0 && row->height > 0);
18433 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18434 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18436 /* If first line's physical ascent is larger than its logical
18437 ascent, use the physical ascent, and make the row taller.
18438 This makes accented characters fully visible. */
18439 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18440 && row->phys_ascent > row->ascent)
18442 row->height += row->phys_ascent - row->ascent;
18443 row->ascent = row->phys_ascent;
18446 /* Compute how much of the line is visible. */
18447 row->visible_height = row->height;
18449 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18450 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18452 if (row->y < min_y)
18453 row->visible_height -= min_y - row->y;
18454 if (row->y + row->height > max_y)
18455 row->visible_height -= row->y + row->height - max_y;
18457 else
18459 row->pixel_width = row->used[TEXT_AREA];
18460 if (row->continued_p)
18461 row->pixel_width -= it->continuation_pixel_width;
18462 else if (row->truncated_on_right_p)
18463 row->pixel_width -= it->truncation_pixel_width;
18464 row->ascent = row->phys_ascent = 0;
18465 row->height = row->phys_height = row->visible_height = 1;
18466 row->extra_line_spacing = 0;
18469 /* Compute a hash code for this row. */
18470 row->hash = row_hash (row);
18472 it->max_ascent = it->max_descent = 0;
18473 it->max_phys_ascent = it->max_phys_descent = 0;
18477 /* Append one space to the glyph row of iterator IT if doing a
18478 window-based redisplay. The space has the same face as
18479 IT->face_id. Value is non-zero if a space was added.
18481 This function is called to make sure that there is always one glyph
18482 at the end of a glyph row that the cursor can be set on under
18483 window-systems. (If there weren't such a glyph we would not know
18484 how wide and tall a box cursor should be displayed).
18486 At the same time this space let's a nicely handle clearing to the
18487 end of the line if the row ends in italic text. */
18489 static int
18490 append_space_for_newline (struct it *it, int default_face_p)
18492 if (FRAME_WINDOW_P (it->f))
18494 int n = it->glyph_row->used[TEXT_AREA];
18496 if (it->glyph_row->glyphs[TEXT_AREA] + n
18497 < it->glyph_row->glyphs[1 + TEXT_AREA])
18499 /* Save some values that must not be changed.
18500 Must save IT->c and IT->len because otherwise
18501 ITERATOR_AT_END_P wouldn't work anymore after
18502 append_space_for_newline has been called. */
18503 enum display_element_type saved_what = it->what;
18504 int saved_c = it->c, saved_len = it->len;
18505 int saved_char_to_display = it->char_to_display;
18506 int saved_x = it->current_x;
18507 int saved_face_id = it->face_id;
18508 struct text_pos saved_pos;
18509 Lisp_Object saved_object;
18510 struct face *face;
18512 saved_object = it->object;
18513 saved_pos = it->position;
18515 it->what = IT_CHARACTER;
18516 memset (&it->position, 0, sizeof it->position);
18517 it->object = make_number (0);
18518 it->c = it->char_to_display = ' ';
18519 it->len = 1;
18521 /* If the default face was remapped, be sure to use the
18522 remapped face for the appended newline. */
18523 if (default_face_p)
18524 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18525 else if (it->face_before_selective_p)
18526 it->face_id = it->saved_face_id;
18527 face = FACE_FROM_ID (it->f, it->face_id);
18528 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18530 PRODUCE_GLYPHS (it);
18532 it->override_ascent = -1;
18533 it->constrain_row_ascent_descent_p = 0;
18534 it->current_x = saved_x;
18535 it->object = saved_object;
18536 it->position = saved_pos;
18537 it->what = saved_what;
18538 it->face_id = saved_face_id;
18539 it->len = saved_len;
18540 it->c = saved_c;
18541 it->char_to_display = saved_char_to_display;
18542 return 1;
18546 return 0;
18550 /* Extend the face of the last glyph in the text area of IT->glyph_row
18551 to the end of the display line. Called from display_line. If the
18552 glyph row is empty, add a space glyph to it so that we know the
18553 face to draw. Set the glyph row flag fill_line_p. If the glyph
18554 row is R2L, prepend a stretch glyph to cover the empty space to the
18555 left of the leftmost glyph. */
18557 static void
18558 extend_face_to_end_of_line (struct it *it)
18560 struct face *face, *default_face;
18561 struct frame *f = it->f;
18563 /* If line is already filled, do nothing. Non window-system frames
18564 get a grace of one more ``pixel'' because their characters are
18565 1-``pixel'' wide, so they hit the equality too early. This grace
18566 is needed only for R2L rows that are not continued, to produce
18567 one extra blank where we could display the cursor. */
18568 if (it->current_x >= it->last_visible_x
18569 + (!FRAME_WINDOW_P (f)
18570 && it->glyph_row->reversed_p
18571 && !it->glyph_row->continued_p))
18572 return;
18574 /* The default face, possibly remapped. */
18575 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18577 /* Face extension extends the background and box of IT->face_id
18578 to the end of the line. If the background equals the background
18579 of the frame, we don't have to do anything. */
18580 if (it->face_before_selective_p)
18581 face = FACE_FROM_ID (f, it->saved_face_id);
18582 else
18583 face = FACE_FROM_ID (f, it->face_id);
18585 if (FRAME_WINDOW_P (f)
18586 && it->glyph_row->displays_text_p
18587 && face->box == FACE_NO_BOX
18588 && face->background == FRAME_BACKGROUND_PIXEL (f)
18589 && !face->stipple
18590 && !it->glyph_row->reversed_p)
18591 return;
18593 /* Set the glyph row flag indicating that the face of the last glyph
18594 in the text area has to be drawn to the end of the text area. */
18595 it->glyph_row->fill_line_p = 1;
18597 /* If current character of IT is not ASCII, make sure we have the
18598 ASCII face. This will be automatically undone the next time
18599 get_next_display_element returns a multibyte character. Note
18600 that the character will always be single byte in unibyte
18601 text. */
18602 if (!ASCII_CHAR_P (it->c))
18604 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18607 if (FRAME_WINDOW_P (f))
18609 /* If the row is empty, add a space with the current face of IT,
18610 so that we know which face to draw. */
18611 if (it->glyph_row->used[TEXT_AREA] == 0)
18613 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18614 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18615 it->glyph_row->used[TEXT_AREA] = 1;
18617 #ifdef HAVE_WINDOW_SYSTEM
18618 if (it->glyph_row->reversed_p)
18620 /* Prepend a stretch glyph to the row, such that the
18621 rightmost glyph will be drawn flushed all the way to the
18622 right margin of the window. The stretch glyph that will
18623 occupy the empty space, if any, to the left of the
18624 glyphs. */
18625 struct font *font = face->font ? face->font : FRAME_FONT (f);
18626 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18627 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18628 struct glyph *g;
18629 int row_width, stretch_ascent, stretch_width;
18630 struct text_pos saved_pos;
18631 int saved_face_id, saved_avoid_cursor;
18633 for (row_width = 0, g = row_start; g < row_end; g++)
18634 row_width += g->pixel_width;
18635 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18636 if (stretch_width > 0)
18638 stretch_ascent =
18639 (((it->ascent + it->descent)
18640 * FONT_BASE (font)) / FONT_HEIGHT (font));
18641 saved_pos = it->position;
18642 memset (&it->position, 0, sizeof it->position);
18643 saved_avoid_cursor = it->avoid_cursor_p;
18644 it->avoid_cursor_p = 1;
18645 saved_face_id = it->face_id;
18646 /* The last row's stretch glyph should get the default
18647 face, to avoid painting the rest of the window with
18648 the region face, if the region ends at ZV. */
18649 if (it->glyph_row->ends_at_zv_p)
18650 it->face_id = default_face->id;
18651 else
18652 it->face_id = face->id;
18653 append_stretch_glyph (it, make_number (0), stretch_width,
18654 it->ascent + it->descent, stretch_ascent);
18655 it->position = saved_pos;
18656 it->avoid_cursor_p = saved_avoid_cursor;
18657 it->face_id = saved_face_id;
18660 #endif /* HAVE_WINDOW_SYSTEM */
18662 else
18664 /* Save some values that must not be changed. */
18665 int saved_x = it->current_x;
18666 struct text_pos saved_pos;
18667 Lisp_Object saved_object;
18668 enum display_element_type saved_what = it->what;
18669 int saved_face_id = it->face_id;
18671 saved_object = it->object;
18672 saved_pos = it->position;
18674 it->what = IT_CHARACTER;
18675 memset (&it->position, 0, sizeof it->position);
18676 it->object = make_number (0);
18677 it->c = it->char_to_display = ' ';
18678 it->len = 1;
18679 /* The last row's blank glyphs should get the default face, to
18680 avoid painting the rest of the window with the region face,
18681 if the region ends at ZV. */
18682 if (it->glyph_row->ends_at_zv_p)
18683 it->face_id = default_face->id;
18684 else
18685 it->face_id = face->id;
18687 PRODUCE_GLYPHS (it);
18689 while (it->current_x <= it->last_visible_x)
18690 PRODUCE_GLYPHS (it);
18692 /* Don't count these blanks really. It would let us insert a left
18693 truncation glyph below and make us set the cursor on them, maybe. */
18694 it->current_x = saved_x;
18695 it->object = saved_object;
18696 it->position = saved_pos;
18697 it->what = saved_what;
18698 it->face_id = saved_face_id;
18703 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18704 trailing whitespace. */
18706 static int
18707 trailing_whitespace_p (ptrdiff_t charpos)
18709 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18710 int c = 0;
18712 while (bytepos < ZV_BYTE
18713 && (c = FETCH_CHAR (bytepos),
18714 c == ' ' || c == '\t'))
18715 ++bytepos;
18717 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18719 if (bytepos != PT_BYTE)
18720 return 1;
18722 return 0;
18726 /* Highlight trailing whitespace, if any, in ROW. */
18728 static void
18729 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18731 int used = row->used[TEXT_AREA];
18733 if (used)
18735 struct glyph *start = row->glyphs[TEXT_AREA];
18736 struct glyph *glyph = start + used - 1;
18738 if (row->reversed_p)
18740 /* Right-to-left rows need to be processed in the opposite
18741 direction, so swap the edge pointers. */
18742 glyph = start;
18743 start = row->glyphs[TEXT_AREA] + used - 1;
18746 /* Skip over glyphs inserted to display the cursor at the
18747 end of a line, for extending the face of the last glyph
18748 to the end of the line on terminals, and for truncation
18749 and continuation glyphs. */
18750 if (!row->reversed_p)
18752 while (glyph >= start
18753 && glyph->type == CHAR_GLYPH
18754 && INTEGERP (glyph->object))
18755 --glyph;
18757 else
18759 while (glyph <= start
18760 && glyph->type == CHAR_GLYPH
18761 && INTEGERP (glyph->object))
18762 ++glyph;
18765 /* If last glyph is a space or stretch, and it's trailing
18766 whitespace, set the face of all trailing whitespace glyphs in
18767 IT->glyph_row to `trailing-whitespace'. */
18768 if ((row->reversed_p ? glyph <= start : glyph >= start)
18769 && BUFFERP (glyph->object)
18770 && (glyph->type == STRETCH_GLYPH
18771 || (glyph->type == CHAR_GLYPH
18772 && glyph->u.ch == ' '))
18773 && trailing_whitespace_p (glyph->charpos))
18775 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18776 if (face_id < 0)
18777 return;
18779 if (!row->reversed_p)
18781 while (glyph >= start
18782 && BUFFERP (glyph->object)
18783 && (glyph->type == STRETCH_GLYPH
18784 || (glyph->type == CHAR_GLYPH
18785 && glyph->u.ch == ' ')))
18786 (glyph--)->face_id = face_id;
18788 else
18790 while (glyph <= start
18791 && BUFFERP (glyph->object)
18792 && (glyph->type == STRETCH_GLYPH
18793 || (glyph->type == CHAR_GLYPH
18794 && glyph->u.ch == ' ')))
18795 (glyph++)->face_id = face_id;
18802 /* Value is non-zero if glyph row ROW should be
18803 used to hold the cursor. */
18805 static int
18806 cursor_row_p (struct glyph_row *row)
18808 int result = 1;
18810 if (PT == CHARPOS (row->end.pos)
18811 || PT == MATRIX_ROW_END_CHARPOS (row))
18813 /* Suppose the row ends on a string.
18814 Unless the row is continued, that means it ends on a newline
18815 in the string. If it's anything other than a display string
18816 (e.g., a before-string from an overlay), we don't want the
18817 cursor there. (This heuristic seems to give the optimal
18818 behavior for the various types of multi-line strings.)
18819 One exception: if the string has `cursor' property on one of
18820 its characters, we _do_ want the cursor there. */
18821 if (CHARPOS (row->end.string_pos) >= 0)
18823 if (row->continued_p)
18824 result = 1;
18825 else
18827 /* Check for `display' property. */
18828 struct glyph *beg = row->glyphs[TEXT_AREA];
18829 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18830 struct glyph *glyph;
18832 result = 0;
18833 for (glyph = end; glyph >= beg; --glyph)
18834 if (STRINGP (glyph->object))
18836 Lisp_Object prop
18837 = Fget_char_property (make_number (PT),
18838 Qdisplay, Qnil);
18839 result =
18840 (!NILP (prop)
18841 && display_prop_string_p (prop, glyph->object));
18842 /* If there's a `cursor' property on one of the
18843 string's characters, this row is a cursor row,
18844 even though this is not a display string. */
18845 if (!result)
18847 Lisp_Object s = glyph->object;
18849 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18851 ptrdiff_t gpos = glyph->charpos;
18853 if (!NILP (Fget_char_property (make_number (gpos),
18854 Qcursor, s)))
18856 result = 1;
18857 break;
18861 break;
18865 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18867 /* If the row ends in middle of a real character,
18868 and the line is continued, we want the cursor here.
18869 That's because CHARPOS (ROW->end.pos) would equal
18870 PT if PT is before the character. */
18871 if (!row->ends_in_ellipsis_p)
18872 result = row->continued_p;
18873 else
18874 /* If the row ends in an ellipsis, then
18875 CHARPOS (ROW->end.pos) will equal point after the
18876 invisible text. We want that position to be displayed
18877 after the ellipsis. */
18878 result = 0;
18880 /* If the row ends at ZV, display the cursor at the end of that
18881 row instead of at the start of the row below. */
18882 else if (row->ends_at_zv_p)
18883 result = 1;
18884 else
18885 result = 0;
18888 return result;
18893 /* Push the property PROP so that it will be rendered at the current
18894 position in IT. Return 1 if PROP was successfully pushed, 0
18895 otherwise. Called from handle_line_prefix to handle the
18896 `line-prefix' and `wrap-prefix' properties. */
18898 static int
18899 push_prefix_prop (struct it *it, Lisp_Object prop)
18901 struct text_pos pos =
18902 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18904 eassert (it->method == GET_FROM_BUFFER
18905 || it->method == GET_FROM_DISPLAY_VECTOR
18906 || it->method == GET_FROM_STRING);
18908 /* We need to save the current buffer/string position, so it will be
18909 restored by pop_it, because iterate_out_of_display_property
18910 depends on that being set correctly, but some situations leave
18911 it->position not yet set when this function is called. */
18912 push_it (it, &pos);
18914 if (STRINGP (prop))
18916 if (SCHARS (prop) == 0)
18918 pop_it (it);
18919 return 0;
18922 it->string = prop;
18923 it->string_from_prefix_prop_p = 1;
18924 it->multibyte_p = STRING_MULTIBYTE (it->string);
18925 it->current.overlay_string_index = -1;
18926 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18927 it->end_charpos = it->string_nchars = SCHARS (it->string);
18928 it->method = GET_FROM_STRING;
18929 it->stop_charpos = 0;
18930 it->prev_stop = 0;
18931 it->base_level_stop = 0;
18933 /* Force paragraph direction to be that of the parent
18934 buffer/string. */
18935 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18936 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18937 else
18938 it->paragraph_embedding = L2R;
18940 /* Set up the bidi iterator for this display string. */
18941 if (it->bidi_p)
18943 it->bidi_it.string.lstring = it->string;
18944 it->bidi_it.string.s = NULL;
18945 it->bidi_it.string.schars = it->end_charpos;
18946 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18947 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18948 it->bidi_it.string.unibyte = !it->multibyte_p;
18949 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18952 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18954 it->method = GET_FROM_STRETCH;
18955 it->object = prop;
18957 #ifdef HAVE_WINDOW_SYSTEM
18958 else if (IMAGEP (prop))
18960 it->what = IT_IMAGE;
18961 it->image_id = lookup_image (it->f, prop);
18962 it->method = GET_FROM_IMAGE;
18964 #endif /* HAVE_WINDOW_SYSTEM */
18965 else
18967 pop_it (it); /* bogus display property, give up */
18968 return 0;
18971 return 1;
18974 /* Return the character-property PROP at the current position in IT. */
18976 static Lisp_Object
18977 get_it_property (struct it *it, Lisp_Object prop)
18979 Lisp_Object position;
18981 if (STRINGP (it->object))
18982 position = make_number (IT_STRING_CHARPOS (*it));
18983 else if (BUFFERP (it->object))
18984 position = make_number (IT_CHARPOS (*it));
18985 else
18986 return Qnil;
18988 return Fget_char_property (position, prop, it->object);
18991 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18993 static void
18994 handle_line_prefix (struct it *it)
18996 Lisp_Object prefix;
18998 if (it->continuation_lines_width > 0)
19000 prefix = get_it_property (it, Qwrap_prefix);
19001 if (NILP (prefix))
19002 prefix = Vwrap_prefix;
19004 else
19006 prefix = get_it_property (it, Qline_prefix);
19007 if (NILP (prefix))
19008 prefix = Vline_prefix;
19010 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19012 /* If the prefix is wider than the window, and we try to wrap
19013 it, it would acquire its own wrap prefix, and so on till the
19014 iterator stack overflows. So, don't wrap the prefix. */
19015 it->line_wrap = TRUNCATE;
19016 it->avoid_cursor_p = 1;
19022 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19023 only for R2L lines from display_line and display_string, when they
19024 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19025 the line/string needs to be continued on the next glyph row. */
19026 static void
19027 unproduce_glyphs (struct it *it, int n)
19029 struct glyph *glyph, *end;
19031 eassert (it->glyph_row);
19032 eassert (it->glyph_row->reversed_p);
19033 eassert (it->area == TEXT_AREA);
19034 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19036 if (n > it->glyph_row->used[TEXT_AREA])
19037 n = it->glyph_row->used[TEXT_AREA];
19038 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19039 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19040 for ( ; glyph < end; glyph++)
19041 glyph[-n] = *glyph;
19044 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19045 and ROW->maxpos. */
19046 static void
19047 find_row_edges (struct it *it, struct glyph_row *row,
19048 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19049 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19051 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19052 lines' rows is implemented for bidi-reordered rows. */
19054 /* ROW->minpos is the value of min_pos, the minimal buffer position
19055 we have in ROW, or ROW->start.pos if that is smaller. */
19056 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19057 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19058 else
19059 /* We didn't find buffer positions smaller than ROW->start, or
19060 didn't find _any_ valid buffer positions in any of the glyphs,
19061 so we must trust the iterator's computed positions. */
19062 row->minpos = row->start.pos;
19063 if (max_pos <= 0)
19065 max_pos = CHARPOS (it->current.pos);
19066 max_bpos = BYTEPOS (it->current.pos);
19069 /* Here are the various use-cases for ending the row, and the
19070 corresponding values for ROW->maxpos:
19072 Line ends in a newline from buffer eol_pos + 1
19073 Line is continued from buffer max_pos + 1
19074 Line is truncated on right it->current.pos
19075 Line ends in a newline from string max_pos + 1(*)
19076 (*) + 1 only when line ends in a forward scan
19077 Line is continued from string max_pos
19078 Line is continued from display vector max_pos
19079 Line is entirely from a string min_pos == max_pos
19080 Line is entirely from a display vector min_pos == max_pos
19081 Line that ends at ZV ZV
19083 If you discover other use-cases, please add them here as
19084 appropriate. */
19085 if (row->ends_at_zv_p)
19086 row->maxpos = it->current.pos;
19087 else if (row->used[TEXT_AREA])
19089 int seen_this_string = 0;
19090 struct glyph_row *r1 = row - 1;
19092 /* Did we see the same display string on the previous row? */
19093 if (STRINGP (it->object)
19094 /* this is not the first row */
19095 && row > it->w->desired_matrix->rows
19096 /* previous row is not the header line */
19097 && !r1->mode_line_p
19098 /* previous row also ends in a newline from a string */
19099 && r1->ends_in_newline_from_string_p)
19101 struct glyph *start, *end;
19103 /* Search for the last glyph of the previous row that came
19104 from buffer or string. Depending on whether the row is
19105 L2R or R2L, we need to process it front to back or the
19106 other way round. */
19107 if (!r1->reversed_p)
19109 start = r1->glyphs[TEXT_AREA];
19110 end = start + r1->used[TEXT_AREA];
19111 /* Glyphs inserted by redisplay have an integer (zero)
19112 as their object. */
19113 while (end > start
19114 && INTEGERP ((end - 1)->object)
19115 && (end - 1)->charpos <= 0)
19116 --end;
19117 if (end > start)
19119 if (EQ ((end - 1)->object, it->object))
19120 seen_this_string = 1;
19122 else
19123 /* If all the glyphs of the previous row were inserted
19124 by redisplay, it means the previous row was
19125 produced from a single newline, which is only
19126 possible if that newline came from the same string
19127 as the one which produced this ROW. */
19128 seen_this_string = 1;
19130 else
19132 end = r1->glyphs[TEXT_AREA] - 1;
19133 start = end + r1->used[TEXT_AREA];
19134 while (end < start
19135 && INTEGERP ((end + 1)->object)
19136 && (end + 1)->charpos <= 0)
19137 ++end;
19138 if (end < start)
19140 if (EQ ((end + 1)->object, it->object))
19141 seen_this_string = 1;
19143 else
19144 seen_this_string = 1;
19147 /* Take note of each display string that covers a newline only
19148 once, the first time we see it. This is for when a display
19149 string includes more than one newline in it. */
19150 if (row->ends_in_newline_from_string_p && !seen_this_string)
19152 /* If we were scanning the buffer forward when we displayed
19153 the string, we want to account for at least one buffer
19154 position that belongs to this row (position covered by
19155 the display string), so that cursor positioning will
19156 consider this row as a candidate when point is at the end
19157 of the visual line represented by this row. This is not
19158 required when scanning back, because max_pos will already
19159 have a much larger value. */
19160 if (CHARPOS (row->end.pos) > max_pos)
19161 INC_BOTH (max_pos, max_bpos);
19162 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19164 else if (CHARPOS (it->eol_pos) > 0)
19165 SET_TEXT_POS (row->maxpos,
19166 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19167 else if (row->continued_p)
19169 /* If max_pos is different from IT's current position, it
19170 means IT->method does not belong to the display element
19171 at max_pos. However, it also means that the display
19172 element at max_pos was displayed in its entirety on this
19173 line, which is equivalent to saying that the next line
19174 starts at the next buffer position. */
19175 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19176 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19177 else
19179 INC_BOTH (max_pos, max_bpos);
19180 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19183 else if (row->truncated_on_right_p)
19184 /* display_line already called reseat_at_next_visible_line_start,
19185 which puts the iterator at the beginning of the next line, in
19186 the logical order. */
19187 row->maxpos = it->current.pos;
19188 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19189 /* A line that is entirely from a string/image/stretch... */
19190 row->maxpos = row->minpos;
19191 else
19192 abort ();
19194 else
19195 row->maxpos = it->current.pos;
19198 /* Construct the glyph row IT->glyph_row in the desired matrix of
19199 IT->w from text at the current position of IT. See dispextern.h
19200 for an overview of struct it. Value is non-zero if
19201 IT->glyph_row displays text, as opposed to a line displaying ZV
19202 only. */
19204 static int
19205 display_line (struct it *it)
19207 struct glyph_row *row = it->glyph_row;
19208 Lisp_Object overlay_arrow_string;
19209 struct it wrap_it;
19210 void *wrap_data = NULL;
19211 int may_wrap = 0, wrap_x IF_LINT (= 0);
19212 int wrap_row_used = -1;
19213 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19214 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19215 int wrap_row_extra_line_spacing IF_LINT (= 0);
19216 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19217 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19218 int cvpos;
19219 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19220 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19222 /* We always start displaying at hpos zero even if hscrolled. */
19223 eassert (it->hpos == 0 && it->current_x == 0);
19225 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19226 >= it->w->desired_matrix->nrows)
19228 it->w->nrows_scale_factor++;
19229 fonts_changed_p = 1;
19230 return 0;
19233 /* Is IT->w showing the region? */
19234 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19236 /* Clear the result glyph row and enable it. */
19237 prepare_desired_row (row);
19239 row->y = it->current_y;
19240 row->start = it->start;
19241 row->continuation_lines_width = it->continuation_lines_width;
19242 row->displays_text_p = 1;
19243 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19244 it->starts_in_middle_of_char_p = 0;
19246 /* Arrange the overlays nicely for our purposes. Usually, we call
19247 display_line on only one line at a time, in which case this
19248 can't really hurt too much, or we call it on lines which appear
19249 one after another in the buffer, in which case all calls to
19250 recenter_overlay_lists but the first will be pretty cheap. */
19251 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19253 /* Move over display elements that are not visible because we are
19254 hscrolled. This may stop at an x-position < IT->first_visible_x
19255 if the first glyph is partially visible or if we hit a line end. */
19256 if (it->current_x < it->first_visible_x)
19258 enum move_it_result move_result;
19260 this_line_min_pos = row->start.pos;
19261 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19262 MOVE_TO_POS | MOVE_TO_X);
19263 /* If we are under a large hscroll, move_it_in_display_line_to
19264 could hit the end of the line without reaching
19265 it->first_visible_x. Pretend that we did reach it. This is
19266 especially important on a TTY, where we will call
19267 extend_face_to_end_of_line, which needs to know how many
19268 blank glyphs to produce. */
19269 if (it->current_x < it->first_visible_x
19270 && (move_result == MOVE_NEWLINE_OR_CR
19271 || move_result == MOVE_POS_MATCH_OR_ZV))
19272 it->current_x = it->first_visible_x;
19274 /* Record the smallest positions seen while we moved over
19275 display elements that are not visible. This is needed by
19276 redisplay_internal for optimizing the case where the cursor
19277 stays inside the same line. The rest of this function only
19278 considers positions that are actually displayed, so
19279 RECORD_MAX_MIN_POS will not otherwise record positions that
19280 are hscrolled to the left of the left edge of the window. */
19281 min_pos = CHARPOS (this_line_min_pos);
19282 min_bpos = BYTEPOS (this_line_min_pos);
19284 else
19286 /* We only do this when not calling `move_it_in_display_line_to'
19287 above, because move_it_in_display_line_to calls
19288 handle_line_prefix itself. */
19289 handle_line_prefix (it);
19292 /* Get the initial row height. This is either the height of the
19293 text hscrolled, if there is any, or zero. */
19294 row->ascent = it->max_ascent;
19295 row->height = it->max_ascent + it->max_descent;
19296 row->phys_ascent = it->max_phys_ascent;
19297 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19298 row->extra_line_spacing = it->max_extra_line_spacing;
19300 /* Utility macro to record max and min buffer positions seen until now. */
19301 #define RECORD_MAX_MIN_POS(IT) \
19302 do \
19304 int composition_p = !STRINGP ((IT)->string) \
19305 && ((IT)->what == IT_COMPOSITION); \
19306 ptrdiff_t current_pos = \
19307 composition_p ? (IT)->cmp_it.charpos \
19308 : IT_CHARPOS (*(IT)); \
19309 ptrdiff_t current_bpos = \
19310 composition_p ? CHAR_TO_BYTE (current_pos) \
19311 : IT_BYTEPOS (*(IT)); \
19312 if (current_pos < min_pos) \
19314 min_pos = current_pos; \
19315 min_bpos = current_bpos; \
19317 if (IT_CHARPOS (*it) > max_pos) \
19319 max_pos = IT_CHARPOS (*it); \
19320 max_bpos = IT_BYTEPOS (*it); \
19323 while (0)
19325 /* Loop generating characters. The loop is left with IT on the next
19326 character to display. */
19327 while (1)
19329 int n_glyphs_before, hpos_before, x_before;
19330 int x, nglyphs;
19331 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19333 /* Retrieve the next thing to display. Value is zero if end of
19334 buffer reached. */
19335 if (!get_next_display_element (it))
19337 /* Maybe add a space at the end of this line that is used to
19338 display the cursor there under X. Set the charpos of the
19339 first glyph of blank lines not corresponding to any text
19340 to -1. */
19341 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19342 row->exact_window_width_line_p = 1;
19343 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19344 || row->used[TEXT_AREA] == 0)
19346 row->glyphs[TEXT_AREA]->charpos = -1;
19347 row->displays_text_p = 0;
19349 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19350 && (!MINI_WINDOW_P (it->w)
19351 || (minibuf_level && EQ (it->window, minibuf_window))))
19352 row->indicate_empty_line_p = 1;
19355 it->continuation_lines_width = 0;
19356 row->ends_at_zv_p = 1;
19357 /* A row that displays right-to-left text must always have
19358 its last face extended all the way to the end of line,
19359 even if this row ends in ZV, because we still write to
19360 the screen left to right. We also need to extend the
19361 last face if the default face is remapped to some
19362 different face, otherwise the functions that clear
19363 portions of the screen will clear with the default face's
19364 background color. */
19365 if (row->reversed_p
19366 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19367 extend_face_to_end_of_line (it);
19368 break;
19371 /* Now, get the metrics of what we want to display. This also
19372 generates glyphs in `row' (which is IT->glyph_row). */
19373 n_glyphs_before = row->used[TEXT_AREA];
19374 x = it->current_x;
19376 /* Remember the line height so far in case the next element doesn't
19377 fit on the line. */
19378 if (it->line_wrap != TRUNCATE)
19380 ascent = it->max_ascent;
19381 descent = it->max_descent;
19382 phys_ascent = it->max_phys_ascent;
19383 phys_descent = it->max_phys_descent;
19385 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19387 if (IT_DISPLAYING_WHITESPACE (it))
19388 may_wrap = 1;
19389 else if (may_wrap)
19391 SAVE_IT (wrap_it, *it, wrap_data);
19392 wrap_x = x;
19393 wrap_row_used = row->used[TEXT_AREA];
19394 wrap_row_ascent = row->ascent;
19395 wrap_row_height = row->height;
19396 wrap_row_phys_ascent = row->phys_ascent;
19397 wrap_row_phys_height = row->phys_height;
19398 wrap_row_extra_line_spacing = row->extra_line_spacing;
19399 wrap_row_min_pos = min_pos;
19400 wrap_row_min_bpos = min_bpos;
19401 wrap_row_max_pos = max_pos;
19402 wrap_row_max_bpos = max_bpos;
19403 may_wrap = 0;
19408 PRODUCE_GLYPHS (it);
19410 /* If this display element was in marginal areas, continue with
19411 the next one. */
19412 if (it->area != TEXT_AREA)
19414 row->ascent = max (row->ascent, it->max_ascent);
19415 row->height = max (row->height, it->max_ascent + it->max_descent);
19416 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19417 row->phys_height = max (row->phys_height,
19418 it->max_phys_ascent + it->max_phys_descent);
19419 row->extra_line_spacing = max (row->extra_line_spacing,
19420 it->max_extra_line_spacing);
19421 set_iterator_to_next (it, 1);
19422 continue;
19425 /* Does the display element fit on the line? If we truncate
19426 lines, we should draw past the right edge of the window. If
19427 we don't truncate, we want to stop so that we can display the
19428 continuation glyph before the right margin. If lines are
19429 continued, there are two possible strategies for characters
19430 resulting in more than 1 glyph (e.g. tabs): Display as many
19431 glyphs as possible in this line and leave the rest for the
19432 continuation line, or display the whole element in the next
19433 line. Original redisplay did the former, so we do it also. */
19434 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19435 hpos_before = it->hpos;
19436 x_before = x;
19438 if (/* Not a newline. */
19439 nglyphs > 0
19440 /* Glyphs produced fit entirely in the line. */
19441 && it->current_x < it->last_visible_x)
19443 it->hpos += nglyphs;
19444 row->ascent = max (row->ascent, it->max_ascent);
19445 row->height = max (row->height, it->max_ascent + it->max_descent);
19446 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19447 row->phys_height = max (row->phys_height,
19448 it->max_phys_ascent + it->max_phys_descent);
19449 row->extra_line_spacing = max (row->extra_line_spacing,
19450 it->max_extra_line_spacing);
19451 if (it->current_x - it->pixel_width < it->first_visible_x)
19452 row->x = x - it->first_visible_x;
19453 /* Record the maximum and minimum buffer positions seen so
19454 far in glyphs that will be displayed by this row. */
19455 if (it->bidi_p)
19456 RECORD_MAX_MIN_POS (it);
19458 else
19460 int i, new_x;
19461 struct glyph *glyph;
19463 for (i = 0; i < nglyphs; ++i, x = new_x)
19465 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19466 new_x = x + glyph->pixel_width;
19468 if (/* Lines are continued. */
19469 it->line_wrap != TRUNCATE
19470 && (/* Glyph doesn't fit on the line. */
19471 new_x > it->last_visible_x
19472 /* Or it fits exactly on a window system frame. */
19473 || (new_x == it->last_visible_x
19474 && FRAME_WINDOW_P (it->f)
19475 && (row->reversed_p
19476 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19477 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19479 /* End of a continued line. */
19481 if (it->hpos == 0
19482 || (new_x == it->last_visible_x
19483 && FRAME_WINDOW_P (it->f)
19484 && (row->reversed_p
19485 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19486 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19488 /* Current glyph is the only one on the line or
19489 fits exactly on the line. We must continue
19490 the line because we can't draw the cursor
19491 after the glyph. */
19492 row->continued_p = 1;
19493 it->current_x = new_x;
19494 it->continuation_lines_width += new_x;
19495 ++it->hpos;
19496 if (i == nglyphs - 1)
19498 /* If line-wrap is on, check if a previous
19499 wrap point was found. */
19500 if (wrap_row_used > 0
19501 /* Even if there is a previous wrap
19502 point, continue the line here as
19503 usual, if (i) the previous character
19504 was a space or tab AND (ii) the
19505 current character is not. */
19506 && (!may_wrap
19507 || IT_DISPLAYING_WHITESPACE (it)))
19508 goto back_to_wrap;
19510 /* Record the maximum and minimum buffer
19511 positions seen so far in glyphs that will be
19512 displayed by this row. */
19513 if (it->bidi_p)
19514 RECORD_MAX_MIN_POS (it);
19515 set_iterator_to_next (it, 1);
19516 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19518 if (!get_next_display_element (it))
19520 row->exact_window_width_line_p = 1;
19521 it->continuation_lines_width = 0;
19522 row->continued_p = 0;
19523 row->ends_at_zv_p = 1;
19525 else if (ITERATOR_AT_END_OF_LINE_P (it))
19527 row->continued_p = 0;
19528 row->exact_window_width_line_p = 1;
19532 else if (it->bidi_p)
19533 RECORD_MAX_MIN_POS (it);
19535 else if (CHAR_GLYPH_PADDING_P (*glyph)
19536 && !FRAME_WINDOW_P (it->f))
19538 /* A padding glyph that doesn't fit on this line.
19539 This means the whole character doesn't fit
19540 on the line. */
19541 if (row->reversed_p)
19542 unproduce_glyphs (it, row->used[TEXT_AREA]
19543 - n_glyphs_before);
19544 row->used[TEXT_AREA] = n_glyphs_before;
19546 /* Fill the rest of the row with continuation
19547 glyphs like in 20.x. */
19548 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19549 < row->glyphs[1 + TEXT_AREA])
19550 produce_special_glyphs (it, IT_CONTINUATION);
19552 row->continued_p = 1;
19553 it->current_x = x_before;
19554 it->continuation_lines_width += x_before;
19556 /* Restore the height to what it was before the
19557 element not fitting on the line. */
19558 it->max_ascent = ascent;
19559 it->max_descent = descent;
19560 it->max_phys_ascent = phys_ascent;
19561 it->max_phys_descent = phys_descent;
19563 else if (wrap_row_used > 0)
19565 back_to_wrap:
19566 if (row->reversed_p)
19567 unproduce_glyphs (it,
19568 row->used[TEXT_AREA] - wrap_row_used);
19569 RESTORE_IT (it, &wrap_it, wrap_data);
19570 it->continuation_lines_width += wrap_x;
19571 row->used[TEXT_AREA] = wrap_row_used;
19572 row->ascent = wrap_row_ascent;
19573 row->height = wrap_row_height;
19574 row->phys_ascent = wrap_row_phys_ascent;
19575 row->phys_height = wrap_row_phys_height;
19576 row->extra_line_spacing = wrap_row_extra_line_spacing;
19577 min_pos = wrap_row_min_pos;
19578 min_bpos = wrap_row_min_bpos;
19579 max_pos = wrap_row_max_pos;
19580 max_bpos = wrap_row_max_bpos;
19581 row->continued_p = 1;
19582 row->ends_at_zv_p = 0;
19583 row->exact_window_width_line_p = 0;
19584 it->continuation_lines_width += x;
19586 /* Make sure that a non-default face is extended
19587 up to the right margin of the window. */
19588 extend_face_to_end_of_line (it);
19590 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19592 /* A TAB that extends past the right edge of the
19593 window. This produces a single glyph on
19594 window system frames. We leave the glyph in
19595 this row and let it fill the row, but don't
19596 consume the TAB. */
19597 if ((row->reversed_p
19598 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19599 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19600 produce_special_glyphs (it, IT_CONTINUATION);
19601 it->continuation_lines_width += it->last_visible_x;
19602 row->ends_in_middle_of_char_p = 1;
19603 row->continued_p = 1;
19604 glyph->pixel_width = it->last_visible_x - x;
19605 it->starts_in_middle_of_char_p = 1;
19607 else
19609 /* Something other than a TAB that draws past
19610 the right edge of the window. Restore
19611 positions to values before the element. */
19612 if (row->reversed_p)
19613 unproduce_glyphs (it, row->used[TEXT_AREA]
19614 - (n_glyphs_before + i));
19615 row->used[TEXT_AREA] = n_glyphs_before + i;
19617 /* Display continuation glyphs. */
19618 it->current_x = x_before;
19619 it->continuation_lines_width += x;
19620 if (!FRAME_WINDOW_P (it->f)
19621 || (row->reversed_p
19622 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19623 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19624 produce_special_glyphs (it, IT_CONTINUATION);
19625 row->continued_p = 1;
19627 extend_face_to_end_of_line (it);
19629 if (nglyphs > 1 && i > 0)
19631 row->ends_in_middle_of_char_p = 1;
19632 it->starts_in_middle_of_char_p = 1;
19635 /* Restore the height to what it was before the
19636 element not fitting on the line. */
19637 it->max_ascent = ascent;
19638 it->max_descent = descent;
19639 it->max_phys_ascent = phys_ascent;
19640 it->max_phys_descent = phys_descent;
19643 break;
19645 else if (new_x > it->first_visible_x)
19647 /* Increment number of glyphs actually displayed. */
19648 ++it->hpos;
19650 /* Record the maximum and minimum buffer positions
19651 seen so far in glyphs that will be displayed by
19652 this row. */
19653 if (it->bidi_p)
19654 RECORD_MAX_MIN_POS (it);
19656 if (x < it->first_visible_x)
19657 /* Glyph is partially visible, i.e. row starts at
19658 negative X position. */
19659 row->x = x - it->first_visible_x;
19661 else
19663 /* Glyph is completely off the left margin of the
19664 window. This should not happen because of the
19665 move_it_in_display_line at the start of this
19666 function, unless the text display area of the
19667 window is empty. */
19668 eassert (it->first_visible_x <= it->last_visible_x);
19671 /* Even if this display element produced no glyphs at all,
19672 we want to record its position. */
19673 if (it->bidi_p && nglyphs == 0)
19674 RECORD_MAX_MIN_POS (it);
19676 row->ascent = max (row->ascent, it->max_ascent);
19677 row->height = max (row->height, it->max_ascent + it->max_descent);
19678 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19679 row->phys_height = max (row->phys_height,
19680 it->max_phys_ascent + it->max_phys_descent);
19681 row->extra_line_spacing = max (row->extra_line_spacing,
19682 it->max_extra_line_spacing);
19684 /* End of this display line if row is continued. */
19685 if (row->continued_p || row->ends_at_zv_p)
19686 break;
19689 at_end_of_line:
19690 /* Is this a line end? If yes, we're also done, after making
19691 sure that a non-default face is extended up to the right
19692 margin of the window. */
19693 if (ITERATOR_AT_END_OF_LINE_P (it))
19695 int used_before = row->used[TEXT_AREA];
19697 row->ends_in_newline_from_string_p = STRINGP (it->object);
19699 /* Add a space at the end of the line that is used to
19700 display the cursor there. */
19701 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19702 append_space_for_newline (it, 0);
19704 /* Extend the face to the end of the line. */
19705 extend_face_to_end_of_line (it);
19707 /* Make sure we have the position. */
19708 if (used_before == 0)
19709 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19711 /* Record the position of the newline, for use in
19712 find_row_edges. */
19713 it->eol_pos = it->current.pos;
19715 /* Consume the line end. This skips over invisible lines. */
19716 set_iterator_to_next (it, 1);
19717 it->continuation_lines_width = 0;
19718 break;
19721 /* Proceed with next display element. Note that this skips
19722 over lines invisible because of selective display. */
19723 set_iterator_to_next (it, 1);
19725 /* If we truncate lines, we are done when the last displayed
19726 glyphs reach past the right margin of the window. */
19727 if (it->line_wrap == TRUNCATE
19728 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19729 ? (it->current_x >= it->last_visible_x)
19730 : (it->current_x > it->last_visible_x)))
19732 /* Maybe add truncation glyphs. */
19733 if (!FRAME_WINDOW_P (it->f)
19734 || (row->reversed_p
19735 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19736 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19738 int i, n;
19740 if (!row->reversed_p)
19742 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19743 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19744 break;
19746 else
19748 for (i = 0; i < row->used[TEXT_AREA]; i++)
19749 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19750 break;
19751 /* Remove any padding glyphs at the front of ROW, to
19752 make room for the truncation glyphs we will be
19753 adding below. The loop below always inserts at
19754 least one truncation glyph, so also remove the
19755 last glyph added to ROW. */
19756 unproduce_glyphs (it, i + 1);
19757 /* Adjust i for the loop below. */
19758 i = row->used[TEXT_AREA] - (i + 1);
19761 it->current_x = x_before;
19762 if (!FRAME_WINDOW_P (it->f))
19764 for (n = row->used[TEXT_AREA]; i < n; ++i)
19766 row->used[TEXT_AREA] = i;
19767 produce_special_glyphs (it, IT_TRUNCATION);
19770 else
19772 row->used[TEXT_AREA] = i;
19773 produce_special_glyphs (it, IT_TRUNCATION);
19776 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19778 /* Don't truncate if we can overflow newline into fringe. */
19779 if (!get_next_display_element (it))
19781 it->continuation_lines_width = 0;
19782 row->ends_at_zv_p = 1;
19783 row->exact_window_width_line_p = 1;
19784 break;
19786 if (ITERATOR_AT_END_OF_LINE_P (it))
19788 row->exact_window_width_line_p = 1;
19789 goto at_end_of_line;
19791 it->current_x = x_before;
19794 row->truncated_on_right_p = 1;
19795 it->continuation_lines_width = 0;
19796 reseat_at_next_visible_line_start (it, 0);
19797 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19798 it->hpos = hpos_before;
19799 break;
19803 if (wrap_data)
19804 bidi_unshelve_cache (wrap_data, 1);
19806 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19807 at the left window margin. */
19808 if (it->first_visible_x
19809 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19811 if (!FRAME_WINDOW_P (it->f)
19812 || (row->reversed_p
19813 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19814 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19815 insert_left_trunc_glyphs (it);
19816 row->truncated_on_left_p = 1;
19819 /* Remember the position at which this line ends.
19821 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19822 cannot be before the call to find_row_edges below, since that is
19823 where these positions are determined. */
19824 row->end = it->current;
19825 if (!it->bidi_p)
19827 row->minpos = row->start.pos;
19828 row->maxpos = row->end.pos;
19830 else
19832 /* ROW->minpos and ROW->maxpos must be the smallest and
19833 `1 + the largest' buffer positions in ROW. But if ROW was
19834 bidi-reordered, these two positions can be anywhere in the
19835 row, so we must determine them now. */
19836 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19839 /* If the start of this line is the overlay arrow-position, then
19840 mark this glyph row as the one containing the overlay arrow.
19841 This is clearly a mess with variable size fonts. It would be
19842 better to let it be displayed like cursors under X. */
19843 if ((row->displays_text_p || !overlay_arrow_seen)
19844 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19845 !NILP (overlay_arrow_string)))
19847 /* Overlay arrow in window redisplay is a fringe bitmap. */
19848 if (STRINGP (overlay_arrow_string))
19850 struct glyph_row *arrow_row
19851 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19852 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19853 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19854 struct glyph *p = row->glyphs[TEXT_AREA];
19855 struct glyph *p2, *end;
19857 /* Copy the arrow glyphs. */
19858 while (glyph < arrow_end)
19859 *p++ = *glyph++;
19861 /* Throw away padding glyphs. */
19862 p2 = p;
19863 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19864 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19865 ++p2;
19866 if (p2 > p)
19868 while (p2 < end)
19869 *p++ = *p2++;
19870 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19873 else
19875 eassert (INTEGERP (overlay_arrow_string));
19876 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19878 overlay_arrow_seen = 1;
19881 /* Highlight trailing whitespace. */
19882 if (!NILP (Vshow_trailing_whitespace))
19883 highlight_trailing_whitespace (it->f, it->glyph_row);
19885 /* Compute pixel dimensions of this line. */
19886 compute_line_metrics (it);
19888 /* Implementation note: No changes in the glyphs of ROW or in their
19889 faces can be done past this point, because compute_line_metrics
19890 computes ROW's hash value and stores it within the glyph_row
19891 structure. */
19893 /* Record whether this row ends inside an ellipsis. */
19894 row->ends_in_ellipsis_p
19895 = (it->method == GET_FROM_DISPLAY_VECTOR
19896 && it->ellipsis_p);
19898 /* Save fringe bitmaps in this row. */
19899 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19900 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19901 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19902 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19904 it->left_user_fringe_bitmap = 0;
19905 it->left_user_fringe_face_id = 0;
19906 it->right_user_fringe_bitmap = 0;
19907 it->right_user_fringe_face_id = 0;
19909 /* Maybe set the cursor. */
19910 cvpos = it->w->cursor.vpos;
19911 if ((cvpos < 0
19912 /* In bidi-reordered rows, keep checking for proper cursor
19913 position even if one has been found already, because buffer
19914 positions in such rows change non-linearly with ROW->VPOS,
19915 when a line is continued. One exception: when we are at ZV,
19916 display cursor on the first suitable glyph row, since all
19917 the empty rows after that also have their position set to ZV. */
19918 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19919 lines' rows is implemented for bidi-reordered rows. */
19920 || (it->bidi_p
19921 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19922 && PT >= MATRIX_ROW_START_CHARPOS (row)
19923 && PT <= MATRIX_ROW_END_CHARPOS (row)
19924 && cursor_row_p (row))
19925 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19927 /* Prepare for the next line. This line starts horizontally at (X
19928 HPOS) = (0 0). Vertical positions are incremented. As a
19929 convenience for the caller, IT->glyph_row is set to the next
19930 row to be used. */
19931 it->current_x = it->hpos = 0;
19932 it->current_y += row->height;
19933 SET_TEXT_POS (it->eol_pos, 0, 0);
19934 ++it->vpos;
19935 ++it->glyph_row;
19936 /* The next row should by default use the same value of the
19937 reversed_p flag as this one. set_iterator_to_next decides when
19938 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19939 the flag accordingly. */
19940 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19941 it->glyph_row->reversed_p = row->reversed_p;
19942 it->start = row->end;
19943 return row->displays_text_p;
19945 #undef RECORD_MAX_MIN_POS
19948 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19949 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19950 doc: /* Return paragraph direction at point in BUFFER.
19951 Value is either `left-to-right' or `right-to-left'.
19952 If BUFFER is omitted or nil, it defaults to the current buffer.
19954 Paragraph direction determines how the text in the paragraph is displayed.
19955 In left-to-right paragraphs, text begins at the left margin of the window
19956 and the reading direction is generally left to right. In right-to-left
19957 paragraphs, text begins at the right margin and is read from right to left.
19959 See also `bidi-paragraph-direction'. */)
19960 (Lisp_Object buffer)
19962 struct buffer *buf = current_buffer;
19963 struct buffer *old = buf;
19965 if (! NILP (buffer))
19967 CHECK_BUFFER (buffer);
19968 buf = XBUFFER (buffer);
19971 if (NILP (BVAR (buf, bidi_display_reordering))
19972 || NILP (BVAR (buf, enable_multibyte_characters))
19973 /* When we are loading loadup.el, the character property tables
19974 needed for bidi iteration are not yet available. */
19975 || !NILP (Vpurify_flag))
19976 return Qleft_to_right;
19977 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19978 return BVAR (buf, bidi_paragraph_direction);
19979 else
19981 /* Determine the direction from buffer text. We could try to
19982 use current_matrix if it is up to date, but this seems fast
19983 enough as it is. */
19984 struct bidi_it itb;
19985 ptrdiff_t pos = BUF_PT (buf);
19986 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19987 int c;
19988 void *itb_data = bidi_shelve_cache ();
19990 set_buffer_temp (buf);
19991 /* bidi_paragraph_init finds the base direction of the paragraph
19992 by searching forward from paragraph start. We need the base
19993 direction of the current or _previous_ paragraph, so we need
19994 to make sure we are within that paragraph. To that end, find
19995 the previous non-empty line. */
19996 if (pos >= ZV && pos > BEGV)
19998 pos--;
19999 bytepos = CHAR_TO_BYTE (pos);
20001 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20002 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20004 while ((c = FETCH_BYTE (bytepos)) == '\n'
20005 || c == ' ' || c == '\t' || c == '\f')
20007 if (bytepos <= BEGV_BYTE)
20008 break;
20009 bytepos--;
20010 pos--;
20012 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20013 bytepos--;
20015 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20016 itb.paragraph_dir = NEUTRAL_DIR;
20017 itb.string.s = NULL;
20018 itb.string.lstring = Qnil;
20019 itb.string.bufpos = 0;
20020 itb.string.unibyte = 0;
20021 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20022 bidi_unshelve_cache (itb_data, 0);
20023 set_buffer_temp (old);
20024 switch (itb.paragraph_dir)
20026 case L2R:
20027 return Qleft_to_right;
20028 break;
20029 case R2L:
20030 return Qright_to_left;
20031 break;
20032 default:
20033 abort ();
20040 /***********************************************************************
20041 Menu Bar
20042 ***********************************************************************/
20044 /* Redisplay the menu bar in the frame for window W.
20046 The menu bar of X frames that don't have X toolkit support is
20047 displayed in a special window W->frame->menu_bar_window.
20049 The menu bar of terminal frames is treated specially as far as
20050 glyph matrices are concerned. Menu bar lines are not part of
20051 windows, so the update is done directly on the frame matrix rows
20052 for the menu bar. */
20054 static void
20055 display_menu_bar (struct window *w)
20057 struct frame *f = XFRAME (WINDOW_FRAME (w));
20058 struct it it;
20059 Lisp_Object items;
20060 int i;
20062 /* Don't do all this for graphical frames. */
20063 #ifdef HAVE_NTGUI
20064 if (FRAME_W32_P (f))
20065 return;
20066 #endif
20067 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20068 if (FRAME_X_P (f))
20069 return;
20070 #endif
20072 #ifdef HAVE_NS
20073 if (FRAME_NS_P (f))
20074 return;
20075 #endif /* HAVE_NS */
20077 #ifdef USE_X_TOOLKIT
20078 eassert (!FRAME_WINDOW_P (f));
20079 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20080 it.first_visible_x = 0;
20081 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20082 #else /* not USE_X_TOOLKIT */
20083 if (FRAME_WINDOW_P (f))
20085 /* Menu bar lines are displayed in the desired matrix of the
20086 dummy window menu_bar_window. */
20087 struct window *menu_w;
20088 eassert (WINDOWP (f->menu_bar_window));
20089 menu_w = XWINDOW (f->menu_bar_window);
20090 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20091 MENU_FACE_ID);
20092 it.first_visible_x = 0;
20093 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20095 else
20097 /* This is a TTY frame, i.e. character hpos/vpos are used as
20098 pixel x/y. */
20099 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20100 MENU_FACE_ID);
20101 it.first_visible_x = 0;
20102 it.last_visible_x = FRAME_COLS (f);
20104 #endif /* not USE_X_TOOLKIT */
20106 /* FIXME: This should be controlled by a user option. See the
20107 comments in redisplay_tool_bar and display_mode_line about
20108 this. */
20109 it.paragraph_embedding = L2R;
20111 if (! mode_line_inverse_video)
20112 /* Force the menu-bar to be displayed in the default face. */
20113 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20115 /* Clear all rows of the menu bar. */
20116 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20118 struct glyph_row *row = it.glyph_row + i;
20119 clear_glyph_row (row);
20120 row->enabled_p = 1;
20121 row->full_width_p = 1;
20124 /* Display all items of the menu bar. */
20125 items = FRAME_MENU_BAR_ITEMS (it.f);
20126 for (i = 0; i < ASIZE (items); i += 4)
20128 Lisp_Object string;
20130 /* Stop at nil string. */
20131 string = AREF (items, i + 1);
20132 if (NILP (string))
20133 break;
20135 /* Remember where item was displayed. */
20136 ASET (items, i + 3, make_number (it.hpos));
20138 /* Display the item, pad with one space. */
20139 if (it.current_x < it.last_visible_x)
20140 display_string (NULL, string, Qnil, 0, 0, &it,
20141 SCHARS (string) + 1, 0, 0, -1);
20144 /* Fill out the line with spaces. */
20145 if (it.current_x < it.last_visible_x)
20146 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20148 /* Compute the total height of the lines. */
20149 compute_line_metrics (&it);
20154 /***********************************************************************
20155 Mode Line
20156 ***********************************************************************/
20158 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20159 FORCE is non-zero, redisplay mode lines unconditionally.
20160 Otherwise, redisplay only mode lines that are garbaged. Value is
20161 the number of windows whose mode lines were redisplayed. */
20163 static int
20164 redisplay_mode_lines (Lisp_Object window, int force)
20166 int nwindows = 0;
20168 while (!NILP (window))
20170 struct window *w = XWINDOW (window);
20172 if (WINDOWP (w->hchild))
20173 nwindows += redisplay_mode_lines (w->hchild, force);
20174 else if (WINDOWP (w->vchild))
20175 nwindows += redisplay_mode_lines (w->vchild, force);
20176 else if (force
20177 || FRAME_GARBAGED_P (XFRAME (w->frame))
20178 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20180 struct text_pos lpoint;
20181 struct buffer *old = current_buffer;
20183 /* Set the window's buffer for the mode line display. */
20184 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20185 set_buffer_internal_1 (XBUFFER (w->buffer));
20187 /* Point refers normally to the selected window. For any
20188 other window, set up appropriate value. */
20189 if (!EQ (window, selected_window))
20191 struct text_pos pt;
20193 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20194 if (CHARPOS (pt) < BEGV)
20195 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20196 else if (CHARPOS (pt) > (ZV - 1))
20197 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20198 else
20199 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20202 /* Display mode lines. */
20203 clear_glyph_matrix (w->desired_matrix);
20204 if (display_mode_lines (w))
20206 ++nwindows;
20207 w->must_be_updated_p = 1;
20210 /* Restore old settings. */
20211 set_buffer_internal_1 (old);
20212 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20215 window = w->next;
20218 return nwindows;
20222 /* Display the mode and/or header line of window W. Value is the
20223 sum number of mode lines and header lines displayed. */
20225 static int
20226 display_mode_lines (struct window *w)
20228 Lisp_Object old_selected_window, old_selected_frame;
20229 int n = 0;
20231 old_selected_frame = selected_frame;
20232 selected_frame = w->frame;
20233 old_selected_window = selected_window;
20234 XSETWINDOW (selected_window, w);
20236 /* These will be set while the mode line specs are processed. */
20237 line_number_displayed = 0;
20238 w->column_number_displayed = Qnil;
20240 if (WINDOW_WANTS_MODELINE_P (w))
20242 struct window *sel_w = XWINDOW (old_selected_window);
20244 /* Select mode line face based on the real selected window. */
20245 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20246 BVAR (current_buffer, mode_line_format));
20247 ++n;
20250 if (WINDOW_WANTS_HEADER_LINE_P (w))
20252 display_mode_line (w, HEADER_LINE_FACE_ID,
20253 BVAR (current_buffer, header_line_format));
20254 ++n;
20257 selected_frame = old_selected_frame;
20258 selected_window = old_selected_window;
20259 return n;
20263 /* Display mode or header line of window W. FACE_ID specifies which
20264 line to display; it is either MODE_LINE_FACE_ID or
20265 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20266 display. Value is the pixel height of the mode/header line
20267 displayed. */
20269 static int
20270 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20272 struct it it;
20273 struct face *face;
20274 ptrdiff_t count = SPECPDL_INDEX ();
20276 init_iterator (&it, w, -1, -1, NULL, face_id);
20277 /* Don't extend on a previously drawn mode-line.
20278 This may happen if called from pos_visible_p. */
20279 it.glyph_row->enabled_p = 0;
20280 prepare_desired_row (it.glyph_row);
20282 it.glyph_row->mode_line_p = 1;
20284 if (! mode_line_inverse_video)
20285 /* Force the mode-line to be displayed in the default face. */
20286 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20288 /* FIXME: This should be controlled by a user option. But
20289 supporting such an option is not trivial, since the mode line is
20290 made up of many separate strings. */
20291 it.paragraph_embedding = L2R;
20293 record_unwind_protect (unwind_format_mode_line,
20294 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20296 mode_line_target = MODE_LINE_DISPLAY;
20298 /* Temporarily make frame's keyboard the current kboard so that
20299 kboard-local variables in the mode_line_format will get the right
20300 values. */
20301 push_kboard (FRAME_KBOARD (it.f));
20302 record_unwind_save_match_data ();
20303 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20304 pop_kboard ();
20306 unbind_to (count, Qnil);
20308 /* Fill up with spaces. */
20309 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20311 compute_line_metrics (&it);
20312 it.glyph_row->full_width_p = 1;
20313 it.glyph_row->continued_p = 0;
20314 it.glyph_row->truncated_on_left_p = 0;
20315 it.glyph_row->truncated_on_right_p = 0;
20317 /* Make a 3D mode-line have a shadow at its right end. */
20318 face = FACE_FROM_ID (it.f, face_id);
20319 extend_face_to_end_of_line (&it);
20320 if (face->box != FACE_NO_BOX)
20322 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20323 + it.glyph_row->used[TEXT_AREA] - 1);
20324 last->right_box_line_p = 1;
20327 return it.glyph_row->height;
20330 /* Move element ELT in LIST to the front of LIST.
20331 Return the updated list. */
20333 static Lisp_Object
20334 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20336 register Lisp_Object tail, prev;
20337 register Lisp_Object tem;
20339 tail = list;
20340 prev = Qnil;
20341 while (CONSP (tail))
20343 tem = XCAR (tail);
20345 if (EQ (elt, tem))
20347 /* Splice out the link TAIL. */
20348 if (NILP (prev))
20349 list = XCDR (tail);
20350 else
20351 Fsetcdr (prev, XCDR (tail));
20353 /* Now make it the first. */
20354 Fsetcdr (tail, list);
20355 return tail;
20357 else
20358 prev = tail;
20359 tail = XCDR (tail);
20360 QUIT;
20363 /* Not found--return unchanged LIST. */
20364 return list;
20367 /* Contribute ELT to the mode line for window IT->w. How it
20368 translates into text depends on its data type.
20370 IT describes the display environment in which we display, as usual.
20372 DEPTH is the depth in recursion. It is used to prevent
20373 infinite recursion here.
20375 FIELD_WIDTH is the number of characters the display of ELT should
20376 occupy in the mode line, and PRECISION is the maximum number of
20377 characters to display from ELT's representation. See
20378 display_string for details.
20380 Returns the hpos of the end of the text generated by ELT.
20382 PROPS is a property list to add to any string we encounter.
20384 If RISKY is nonzero, remove (disregard) any properties in any string
20385 we encounter, and ignore :eval and :propertize.
20387 The global variable `mode_line_target' determines whether the
20388 output is passed to `store_mode_line_noprop',
20389 `store_mode_line_string', or `display_string'. */
20391 static int
20392 display_mode_element (struct it *it, int depth, int field_width, int precision,
20393 Lisp_Object elt, Lisp_Object props, int risky)
20395 int n = 0, field, prec;
20396 int literal = 0;
20398 tail_recurse:
20399 if (depth > 100)
20400 elt = build_string ("*too-deep*");
20402 depth++;
20404 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20406 case Lisp_String:
20408 /* A string: output it and check for %-constructs within it. */
20409 unsigned char c;
20410 ptrdiff_t offset = 0;
20412 if (SCHARS (elt) > 0
20413 && (!NILP (props) || risky))
20415 Lisp_Object oprops, aelt;
20416 oprops = Ftext_properties_at (make_number (0), elt);
20418 /* If the starting string's properties are not what
20419 we want, translate the string. Also, if the string
20420 is risky, do that anyway. */
20422 if (NILP (Fequal (props, oprops)) || risky)
20424 /* If the starting string has properties,
20425 merge the specified ones onto the existing ones. */
20426 if (! NILP (oprops) && !risky)
20428 Lisp_Object tem;
20430 oprops = Fcopy_sequence (oprops);
20431 tem = props;
20432 while (CONSP (tem))
20434 oprops = Fplist_put (oprops, XCAR (tem),
20435 XCAR (XCDR (tem)));
20436 tem = XCDR (XCDR (tem));
20438 props = oprops;
20441 aelt = Fassoc (elt, mode_line_proptrans_alist);
20442 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20444 /* AELT is what we want. Move it to the front
20445 without consing. */
20446 elt = XCAR (aelt);
20447 mode_line_proptrans_alist
20448 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20450 else
20452 Lisp_Object tem;
20454 /* If AELT has the wrong props, it is useless.
20455 so get rid of it. */
20456 if (! NILP (aelt))
20457 mode_line_proptrans_alist
20458 = Fdelq (aelt, mode_line_proptrans_alist);
20460 elt = Fcopy_sequence (elt);
20461 Fset_text_properties (make_number (0), Flength (elt),
20462 props, elt);
20463 /* Add this item to mode_line_proptrans_alist. */
20464 mode_line_proptrans_alist
20465 = Fcons (Fcons (elt, props),
20466 mode_line_proptrans_alist);
20467 /* Truncate mode_line_proptrans_alist
20468 to at most 50 elements. */
20469 tem = Fnthcdr (make_number (50),
20470 mode_line_proptrans_alist);
20471 if (! NILP (tem))
20472 XSETCDR (tem, Qnil);
20477 offset = 0;
20479 if (literal)
20481 prec = precision - n;
20482 switch (mode_line_target)
20484 case MODE_LINE_NOPROP:
20485 case MODE_LINE_TITLE:
20486 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20487 break;
20488 case MODE_LINE_STRING:
20489 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20490 break;
20491 case MODE_LINE_DISPLAY:
20492 n += display_string (NULL, elt, Qnil, 0, 0, it,
20493 0, prec, 0, STRING_MULTIBYTE (elt));
20494 break;
20497 break;
20500 /* Handle the non-literal case. */
20502 while ((precision <= 0 || n < precision)
20503 && SREF (elt, offset) != 0
20504 && (mode_line_target != MODE_LINE_DISPLAY
20505 || it->current_x < it->last_visible_x))
20507 ptrdiff_t last_offset = offset;
20509 /* Advance to end of string or next format specifier. */
20510 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20513 if (offset - 1 != last_offset)
20515 ptrdiff_t nchars, nbytes;
20517 /* Output to end of string or up to '%'. Field width
20518 is length of string. Don't output more than
20519 PRECISION allows us. */
20520 offset--;
20522 prec = c_string_width (SDATA (elt) + last_offset,
20523 offset - last_offset, precision - n,
20524 &nchars, &nbytes);
20526 switch (mode_line_target)
20528 case MODE_LINE_NOPROP:
20529 case MODE_LINE_TITLE:
20530 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20531 break;
20532 case MODE_LINE_STRING:
20534 ptrdiff_t bytepos = last_offset;
20535 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20536 ptrdiff_t endpos = (precision <= 0
20537 ? string_byte_to_char (elt, offset)
20538 : charpos + nchars);
20540 n += store_mode_line_string (NULL,
20541 Fsubstring (elt, make_number (charpos),
20542 make_number (endpos)),
20543 0, 0, 0, Qnil);
20545 break;
20546 case MODE_LINE_DISPLAY:
20548 ptrdiff_t bytepos = last_offset;
20549 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20551 if (precision <= 0)
20552 nchars = string_byte_to_char (elt, offset) - charpos;
20553 n += display_string (NULL, elt, Qnil, 0, charpos,
20554 it, 0, nchars, 0,
20555 STRING_MULTIBYTE (elt));
20557 break;
20560 else /* c == '%' */
20562 ptrdiff_t percent_position = offset;
20564 /* Get the specified minimum width. Zero means
20565 don't pad. */
20566 field = 0;
20567 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20568 field = field * 10 + c - '0';
20570 /* Don't pad beyond the total padding allowed. */
20571 if (field_width - n > 0 && field > field_width - n)
20572 field = field_width - n;
20574 /* Note that either PRECISION <= 0 or N < PRECISION. */
20575 prec = precision - n;
20577 if (c == 'M')
20578 n += display_mode_element (it, depth, field, prec,
20579 Vglobal_mode_string, props,
20580 risky);
20581 else if (c != 0)
20583 int multibyte;
20584 ptrdiff_t bytepos, charpos;
20585 const char *spec;
20586 Lisp_Object string;
20588 bytepos = percent_position;
20589 charpos = (STRING_MULTIBYTE (elt)
20590 ? string_byte_to_char (elt, bytepos)
20591 : bytepos);
20592 spec = decode_mode_spec (it->w, c, field, &string);
20593 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20595 switch (mode_line_target)
20597 case MODE_LINE_NOPROP:
20598 case MODE_LINE_TITLE:
20599 n += store_mode_line_noprop (spec, field, prec);
20600 break;
20601 case MODE_LINE_STRING:
20603 Lisp_Object tem = build_string (spec);
20604 props = Ftext_properties_at (make_number (charpos), elt);
20605 /* Should only keep face property in props */
20606 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20608 break;
20609 case MODE_LINE_DISPLAY:
20611 int nglyphs_before, nwritten;
20613 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20614 nwritten = display_string (spec, string, elt,
20615 charpos, 0, it,
20616 field, prec, 0,
20617 multibyte);
20619 /* Assign to the glyphs written above the
20620 string where the `%x' came from, position
20621 of the `%'. */
20622 if (nwritten > 0)
20624 struct glyph *glyph
20625 = (it->glyph_row->glyphs[TEXT_AREA]
20626 + nglyphs_before);
20627 int i;
20629 for (i = 0; i < nwritten; ++i)
20631 glyph[i].object = elt;
20632 glyph[i].charpos = charpos;
20635 n += nwritten;
20638 break;
20641 else /* c == 0 */
20642 break;
20646 break;
20648 case Lisp_Symbol:
20649 /* A symbol: process the value of the symbol recursively
20650 as if it appeared here directly. Avoid error if symbol void.
20651 Special case: if value of symbol is a string, output the string
20652 literally. */
20654 register Lisp_Object tem;
20656 /* If the variable is not marked as risky to set
20657 then its contents are risky to use. */
20658 if (NILP (Fget (elt, Qrisky_local_variable)))
20659 risky = 1;
20661 tem = Fboundp (elt);
20662 if (!NILP (tem))
20664 tem = Fsymbol_value (elt);
20665 /* If value is a string, output that string literally:
20666 don't check for % within it. */
20667 if (STRINGP (tem))
20668 literal = 1;
20670 if (!EQ (tem, elt))
20672 /* Give up right away for nil or t. */
20673 elt = tem;
20674 goto tail_recurse;
20678 break;
20680 case Lisp_Cons:
20682 register Lisp_Object car, tem;
20684 /* A cons cell: five distinct cases.
20685 If first element is :eval or :propertize, do something special.
20686 If first element is a string or a cons, process all the elements
20687 and effectively concatenate them.
20688 If first element is a negative number, truncate displaying cdr to
20689 at most that many characters. If positive, pad (with spaces)
20690 to at least that many characters.
20691 If first element is a symbol, process the cadr or caddr recursively
20692 according to whether the symbol's value is non-nil or nil. */
20693 car = XCAR (elt);
20694 if (EQ (car, QCeval))
20696 /* An element of the form (:eval FORM) means evaluate FORM
20697 and use the result as mode line elements. */
20699 if (risky)
20700 break;
20702 if (CONSP (XCDR (elt)))
20704 Lisp_Object spec;
20705 spec = safe_eval (XCAR (XCDR (elt)));
20706 n += display_mode_element (it, depth, field_width - n,
20707 precision - n, spec, props,
20708 risky);
20711 else if (EQ (car, QCpropertize))
20713 /* An element of the form (:propertize ELT PROPS...)
20714 means display ELT but applying properties PROPS. */
20716 if (risky)
20717 break;
20719 if (CONSP (XCDR (elt)))
20720 n += display_mode_element (it, depth, field_width - n,
20721 precision - n, XCAR (XCDR (elt)),
20722 XCDR (XCDR (elt)), risky);
20724 else if (SYMBOLP (car))
20726 tem = Fboundp (car);
20727 elt = XCDR (elt);
20728 if (!CONSP (elt))
20729 goto invalid;
20730 /* elt is now the cdr, and we know it is a cons cell.
20731 Use its car if CAR has a non-nil value. */
20732 if (!NILP (tem))
20734 tem = Fsymbol_value (car);
20735 if (!NILP (tem))
20737 elt = XCAR (elt);
20738 goto tail_recurse;
20741 /* Symbol's value is nil (or symbol is unbound)
20742 Get the cddr of the original list
20743 and if possible find the caddr and use that. */
20744 elt = XCDR (elt);
20745 if (NILP (elt))
20746 break;
20747 else if (!CONSP (elt))
20748 goto invalid;
20749 elt = XCAR (elt);
20750 goto tail_recurse;
20752 else if (INTEGERP (car))
20754 register int lim = XINT (car);
20755 elt = XCDR (elt);
20756 if (lim < 0)
20758 /* Negative int means reduce maximum width. */
20759 if (precision <= 0)
20760 precision = -lim;
20761 else
20762 precision = min (precision, -lim);
20764 else if (lim > 0)
20766 /* Padding specified. Don't let it be more than
20767 current maximum. */
20768 if (precision > 0)
20769 lim = min (precision, lim);
20771 /* If that's more padding than already wanted, queue it.
20772 But don't reduce padding already specified even if
20773 that is beyond the current truncation point. */
20774 field_width = max (lim, field_width);
20776 goto tail_recurse;
20778 else if (STRINGP (car) || CONSP (car))
20780 Lisp_Object halftail = elt;
20781 int len = 0;
20783 while (CONSP (elt)
20784 && (precision <= 0 || n < precision))
20786 n += display_mode_element (it, depth,
20787 /* Do padding only after the last
20788 element in the list. */
20789 (! CONSP (XCDR (elt))
20790 ? field_width - n
20791 : 0),
20792 precision - n, XCAR (elt),
20793 props, risky);
20794 elt = XCDR (elt);
20795 len++;
20796 if ((len & 1) == 0)
20797 halftail = XCDR (halftail);
20798 /* Check for cycle. */
20799 if (EQ (halftail, elt))
20800 break;
20804 break;
20806 default:
20807 invalid:
20808 elt = build_string ("*invalid*");
20809 goto tail_recurse;
20812 /* Pad to FIELD_WIDTH. */
20813 if (field_width > 0 && n < field_width)
20815 switch (mode_line_target)
20817 case MODE_LINE_NOPROP:
20818 case MODE_LINE_TITLE:
20819 n += store_mode_line_noprop ("", field_width - n, 0);
20820 break;
20821 case MODE_LINE_STRING:
20822 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20823 break;
20824 case MODE_LINE_DISPLAY:
20825 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20826 0, 0, 0);
20827 break;
20831 return n;
20834 /* Store a mode-line string element in mode_line_string_list.
20836 If STRING is non-null, display that C string. Otherwise, the Lisp
20837 string LISP_STRING is displayed.
20839 FIELD_WIDTH is the minimum number of output glyphs to produce.
20840 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20841 with spaces. FIELD_WIDTH <= 0 means don't pad.
20843 PRECISION is the maximum number of characters to output from
20844 STRING. PRECISION <= 0 means don't truncate the string.
20846 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20847 properties to the string.
20849 PROPS are the properties to add to the string.
20850 The mode_line_string_face face property is always added to the string.
20853 static int
20854 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20855 int field_width, int precision, Lisp_Object props)
20857 ptrdiff_t len;
20858 int n = 0;
20860 if (string != NULL)
20862 len = strlen (string);
20863 if (precision > 0 && len > precision)
20864 len = precision;
20865 lisp_string = make_string (string, len);
20866 if (NILP (props))
20867 props = mode_line_string_face_prop;
20868 else if (!NILP (mode_line_string_face))
20870 Lisp_Object face = Fplist_get (props, Qface);
20871 props = Fcopy_sequence (props);
20872 if (NILP (face))
20873 face = mode_line_string_face;
20874 else
20875 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20876 props = Fplist_put (props, Qface, face);
20878 Fadd_text_properties (make_number (0), make_number (len),
20879 props, lisp_string);
20881 else
20883 len = XFASTINT (Flength (lisp_string));
20884 if (precision > 0 && len > precision)
20886 len = precision;
20887 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20888 precision = -1;
20890 if (!NILP (mode_line_string_face))
20892 Lisp_Object face;
20893 if (NILP (props))
20894 props = Ftext_properties_at (make_number (0), lisp_string);
20895 face = Fplist_get (props, Qface);
20896 if (NILP (face))
20897 face = mode_line_string_face;
20898 else
20899 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20900 props = Fcons (Qface, Fcons (face, Qnil));
20901 if (copy_string)
20902 lisp_string = Fcopy_sequence (lisp_string);
20904 if (!NILP (props))
20905 Fadd_text_properties (make_number (0), make_number (len),
20906 props, lisp_string);
20909 if (len > 0)
20911 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20912 n += len;
20915 if (field_width > len)
20917 field_width -= len;
20918 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20919 if (!NILP (props))
20920 Fadd_text_properties (make_number (0), make_number (field_width),
20921 props, lisp_string);
20922 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20923 n += field_width;
20926 return n;
20930 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20931 1, 4, 0,
20932 doc: /* Format a string out of a mode line format specification.
20933 First arg FORMAT specifies the mode line format (see `mode-line-format'
20934 for details) to use.
20936 By default, the format is evaluated for the currently selected window.
20938 Optional second arg FACE specifies the face property to put on all
20939 characters for which no face is specified. The value nil means the
20940 default face. The value t means whatever face the window's mode line
20941 currently uses (either `mode-line' or `mode-line-inactive',
20942 depending on whether the window is the selected window or not).
20943 An integer value means the value string has no text
20944 properties.
20946 Optional third and fourth args WINDOW and BUFFER specify the window
20947 and buffer to use as the context for the formatting (defaults
20948 are the selected window and the WINDOW's buffer). */)
20949 (Lisp_Object format, Lisp_Object face,
20950 Lisp_Object window, Lisp_Object buffer)
20952 struct it it;
20953 int len;
20954 struct window *w;
20955 struct buffer *old_buffer = NULL;
20956 int face_id;
20957 int no_props = INTEGERP (face);
20958 ptrdiff_t count = SPECPDL_INDEX ();
20959 Lisp_Object str;
20960 int string_start = 0;
20962 if (NILP (window))
20963 window = selected_window;
20964 CHECK_WINDOW (window);
20965 w = XWINDOW (window);
20967 if (NILP (buffer))
20968 buffer = w->buffer;
20969 CHECK_BUFFER (buffer);
20971 /* Make formatting the modeline a non-op when noninteractive, otherwise
20972 there will be problems later caused by a partially initialized frame. */
20973 if (NILP (format) || noninteractive)
20974 return empty_unibyte_string;
20976 if (no_props)
20977 face = Qnil;
20979 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20980 : EQ (face, Qt) ? (EQ (window, selected_window)
20981 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20982 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20983 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20984 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20985 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20986 : DEFAULT_FACE_ID;
20988 if (XBUFFER (buffer) != current_buffer)
20989 old_buffer = current_buffer;
20991 /* Save things including mode_line_proptrans_alist,
20992 and set that to nil so that we don't alter the outer value. */
20993 record_unwind_protect (unwind_format_mode_line,
20994 format_mode_line_unwind_data
20995 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
20996 old_buffer, selected_window, 1));
20997 mode_line_proptrans_alist = Qnil;
20999 Fselect_window (window, Qt);
21000 if (old_buffer)
21001 set_buffer_internal_1 (XBUFFER (buffer));
21003 init_iterator (&it, w, -1, -1, NULL, face_id);
21005 if (no_props)
21007 mode_line_target = MODE_LINE_NOPROP;
21008 mode_line_string_face_prop = Qnil;
21009 mode_line_string_list = Qnil;
21010 string_start = MODE_LINE_NOPROP_LEN (0);
21012 else
21014 mode_line_target = MODE_LINE_STRING;
21015 mode_line_string_list = Qnil;
21016 mode_line_string_face = face;
21017 mode_line_string_face_prop
21018 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21021 push_kboard (FRAME_KBOARD (it.f));
21022 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21023 pop_kboard ();
21025 if (no_props)
21027 len = MODE_LINE_NOPROP_LEN (string_start);
21028 str = make_string (mode_line_noprop_buf + string_start, len);
21030 else
21032 mode_line_string_list = Fnreverse (mode_line_string_list);
21033 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21034 empty_unibyte_string);
21037 unbind_to (count, Qnil);
21038 return str;
21041 /* Write a null-terminated, right justified decimal representation of
21042 the positive integer D to BUF using a minimal field width WIDTH. */
21044 static void
21045 pint2str (register char *buf, register int width, register ptrdiff_t d)
21047 register char *p = buf;
21049 if (d <= 0)
21050 *p++ = '0';
21051 else
21053 while (d > 0)
21055 *p++ = d % 10 + '0';
21056 d /= 10;
21060 for (width -= (int) (p - buf); width > 0; --width)
21061 *p++ = ' ';
21062 *p-- = '\0';
21063 while (p > buf)
21065 d = *buf;
21066 *buf++ = *p;
21067 *p-- = d;
21071 /* Write a null-terminated, right justified decimal and "human
21072 readable" representation of the nonnegative integer D to BUF using
21073 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21075 static const char power_letter[] =
21077 0, /* no letter */
21078 'k', /* kilo */
21079 'M', /* mega */
21080 'G', /* giga */
21081 'T', /* tera */
21082 'P', /* peta */
21083 'E', /* exa */
21084 'Z', /* zetta */
21085 'Y' /* yotta */
21088 static void
21089 pint2hrstr (char *buf, int width, ptrdiff_t d)
21091 /* We aim to represent the nonnegative integer D as
21092 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21093 ptrdiff_t quotient = d;
21094 int remainder = 0;
21095 /* -1 means: do not use TENTHS. */
21096 int tenths = -1;
21097 int exponent = 0;
21099 /* Length of QUOTIENT.TENTHS as a string. */
21100 int length;
21102 char * psuffix;
21103 char * p;
21105 if (1000 <= quotient)
21107 /* Scale to the appropriate EXPONENT. */
21110 remainder = quotient % 1000;
21111 quotient /= 1000;
21112 exponent++;
21114 while (1000 <= quotient);
21116 /* Round to nearest and decide whether to use TENTHS or not. */
21117 if (quotient <= 9)
21119 tenths = remainder / 100;
21120 if (50 <= remainder % 100)
21122 if (tenths < 9)
21123 tenths++;
21124 else
21126 quotient++;
21127 if (quotient == 10)
21128 tenths = -1;
21129 else
21130 tenths = 0;
21134 else
21135 if (500 <= remainder)
21137 if (quotient < 999)
21138 quotient++;
21139 else
21141 quotient = 1;
21142 exponent++;
21143 tenths = 0;
21148 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21149 if (tenths == -1 && quotient <= 99)
21150 if (quotient <= 9)
21151 length = 1;
21152 else
21153 length = 2;
21154 else
21155 length = 3;
21156 p = psuffix = buf + max (width, length);
21158 /* Print EXPONENT. */
21159 *psuffix++ = power_letter[exponent];
21160 *psuffix = '\0';
21162 /* Print TENTHS. */
21163 if (tenths >= 0)
21165 *--p = '0' + tenths;
21166 *--p = '.';
21169 /* Print QUOTIENT. */
21172 int digit = quotient % 10;
21173 *--p = '0' + digit;
21175 while ((quotient /= 10) != 0);
21177 /* Print leading spaces. */
21178 while (buf < p)
21179 *--p = ' ';
21182 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21183 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21184 type of CODING_SYSTEM. Return updated pointer into BUF. */
21186 static unsigned char invalid_eol_type[] = "(*invalid*)";
21188 static char *
21189 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21191 Lisp_Object val;
21192 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21193 const unsigned char *eol_str;
21194 int eol_str_len;
21195 /* The EOL conversion we are using. */
21196 Lisp_Object eoltype;
21198 val = CODING_SYSTEM_SPEC (coding_system);
21199 eoltype = Qnil;
21201 if (!VECTORP (val)) /* Not yet decided. */
21203 *buf++ = multibyte ? '-' : ' ';
21204 if (eol_flag)
21205 eoltype = eol_mnemonic_undecided;
21206 /* Don't mention EOL conversion if it isn't decided. */
21208 else
21210 Lisp_Object attrs;
21211 Lisp_Object eolvalue;
21213 attrs = AREF (val, 0);
21214 eolvalue = AREF (val, 2);
21216 *buf++ = multibyte
21217 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21218 : ' ';
21220 if (eol_flag)
21222 /* The EOL conversion that is normal on this system. */
21224 if (NILP (eolvalue)) /* Not yet decided. */
21225 eoltype = eol_mnemonic_undecided;
21226 else if (VECTORP (eolvalue)) /* Not yet decided. */
21227 eoltype = eol_mnemonic_undecided;
21228 else /* eolvalue is Qunix, Qdos, or Qmac. */
21229 eoltype = (EQ (eolvalue, Qunix)
21230 ? eol_mnemonic_unix
21231 : (EQ (eolvalue, Qdos) == 1
21232 ? eol_mnemonic_dos : eol_mnemonic_mac));
21236 if (eol_flag)
21238 /* Mention the EOL conversion if it is not the usual one. */
21239 if (STRINGP (eoltype))
21241 eol_str = SDATA (eoltype);
21242 eol_str_len = SBYTES (eoltype);
21244 else if (CHARACTERP (eoltype))
21246 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21247 int c = XFASTINT (eoltype);
21248 eol_str_len = CHAR_STRING (c, tmp);
21249 eol_str = tmp;
21251 else
21253 eol_str = invalid_eol_type;
21254 eol_str_len = sizeof (invalid_eol_type) - 1;
21256 memcpy (buf, eol_str, eol_str_len);
21257 buf += eol_str_len;
21260 return buf;
21263 /* Return a string for the output of a mode line %-spec for window W,
21264 generated by character C. FIELD_WIDTH > 0 means pad the string
21265 returned with spaces to that value. Return a Lisp string in
21266 *STRING if the resulting string is taken from that Lisp string.
21268 Note we operate on the current buffer for most purposes,
21269 the exception being w->base_line_pos. */
21271 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21273 static const char *
21274 decode_mode_spec (struct window *w, register int c, int field_width,
21275 Lisp_Object *string)
21277 Lisp_Object obj;
21278 struct frame *f = XFRAME (WINDOW_FRAME (w));
21279 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21280 struct buffer *b = current_buffer;
21282 obj = Qnil;
21283 *string = Qnil;
21285 switch (c)
21287 case '*':
21288 if (!NILP (BVAR (b, read_only)))
21289 return "%";
21290 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21291 return "*";
21292 return "-";
21294 case '+':
21295 /* This differs from %* only for a modified read-only buffer. */
21296 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21297 return "*";
21298 if (!NILP (BVAR (b, read_only)))
21299 return "%";
21300 return "-";
21302 case '&':
21303 /* This differs from %* in ignoring read-only-ness. */
21304 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21305 return "*";
21306 return "-";
21308 case '%':
21309 return "%";
21311 case '[':
21313 int i;
21314 char *p;
21316 if (command_loop_level > 5)
21317 return "[[[... ";
21318 p = decode_mode_spec_buf;
21319 for (i = 0; i < command_loop_level; i++)
21320 *p++ = '[';
21321 *p = 0;
21322 return decode_mode_spec_buf;
21325 case ']':
21327 int i;
21328 char *p;
21330 if (command_loop_level > 5)
21331 return " ...]]]";
21332 p = decode_mode_spec_buf;
21333 for (i = 0; i < command_loop_level; i++)
21334 *p++ = ']';
21335 *p = 0;
21336 return decode_mode_spec_buf;
21339 case '-':
21341 register int i;
21343 /* Let lots_of_dashes be a string of infinite length. */
21344 if (mode_line_target == MODE_LINE_NOPROP ||
21345 mode_line_target == MODE_LINE_STRING)
21346 return "--";
21347 if (field_width <= 0
21348 || field_width > sizeof (lots_of_dashes))
21350 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21351 decode_mode_spec_buf[i] = '-';
21352 decode_mode_spec_buf[i] = '\0';
21353 return decode_mode_spec_buf;
21355 else
21356 return lots_of_dashes;
21359 case 'b':
21360 obj = BVAR (b, name);
21361 break;
21363 case 'c':
21364 /* %c and %l are ignored in `frame-title-format'.
21365 (In redisplay_internal, the frame title is drawn _before_ the
21366 windows are updated, so the stuff which depends on actual
21367 window contents (such as %l) may fail to render properly, or
21368 even crash emacs.) */
21369 if (mode_line_target == MODE_LINE_TITLE)
21370 return "";
21371 else
21373 ptrdiff_t col = current_column ();
21374 w->column_number_displayed = make_number (col);
21375 pint2str (decode_mode_spec_buf, field_width, col);
21376 return decode_mode_spec_buf;
21379 case 'e':
21380 #ifndef SYSTEM_MALLOC
21382 if (NILP (Vmemory_full))
21383 return "";
21384 else
21385 return "!MEM FULL! ";
21387 #else
21388 return "";
21389 #endif
21391 case 'F':
21392 /* %F displays the frame name. */
21393 if (!NILP (f->title))
21394 return SSDATA (f->title);
21395 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21396 return SSDATA (f->name);
21397 return "Emacs";
21399 case 'f':
21400 obj = BVAR (b, filename);
21401 break;
21403 case 'i':
21405 ptrdiff_t size = ZV - BEGV;
21406 pint2str (decode_mode_spec_buf, field_width, size);
21407 return decode_mode_spec_buf;
21410 case 'I':
21412 ptrdiff_t size = ZV - BEGV;
21413 pint2hrstr (decode_mode_spec_buf, field_width, size);
21414 return decode_mode_spec_buf;
21417 case 'l':
21419 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21420 ptrdiff_t topline, nlines, height;
21421 ptrdiff_t junk;
21423 /* %c and %l are ignored in `frame-title-format'. */
21424 if (mode_line_target == MODE_LINE_TITLE)
21425 return "";
21427 startpos = XMARKER (w->start)->charpos;
21428 startpos_byte = marker_byte_position (w->start);
21429 height = WINDOW_TOTAL_LINES (w);
21431 /* If we decided that this buffer isn't suitable for line numbers,
21432 don't forget that too fast. */
21433 if (EQ (w->base_line_pos, w->buffer))
21434 goto no_value;
21435 /* But do forget it, if the window shows a different buffer now. */
21436 else if (BUFFERP (w->base_line_pos))
21437 w->base_line_pos = Qnil;
21439 /* If the buffer is very big, don't waste time. */
21440 if (INTEGERP (Vline_number_display_limit)
21441 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21443 w->base_line_pos = Qnil;
21444 w->base_line_number = Qnil;
21445 goto no_value;
21448 if (INTEGERP (w->base_line_number)
21449 && INTEGERP (w->base_line_pos)
21450 && XFASTINT (w->base_line_pos) <= startpos)
21452 line = XFASTINT (w->base_line_number);
21453 linepos = XFASTINT (w->base_line_pos);
21454 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21456 else
21458 line = 1;
21459 linepos = BUF_BEGV (b);
21460 linepos_byte = BUF_BEGV_BYTE (b);
21463 /* Count lines from base line to window start position. */
21464 nlines = display_count_lines (linepos_byte,
21465 startpos_byte,
21466 startpos, &junk);
21468 topline = nlines + line;
21470 /* Determine a new base line, if the old one is too close
21471 or too far away, or if we did not have one.
21472 "Too close" means it's plausible a scroll-down would
21473 go back past it. */
21474 if (startpos == BUF_BEGV (b))
21476 w->base_line_number = make_number (topline);
21477 w->base_line_pos = make_number (BUF_BEGV (b));
21479 else if (nlines < height + 25 || nlines > height * 3 + 50
21480 || linepos == BUF_BEGV (b))
21482 ptrdiff_t limit = BUF_BEGV (b);
21483 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21484 ptrdiff_t position;
21485 ptrdiff_t distance =
21486 (height * 2 + 30) * line_number_display_limit_width;
21488 if (startpos - distance > limit)
21490 limit = startpos - distance;
21491 limit_byte = CHAR_TO_BYTE (limit);
21494 nlines = display_count_lines (startpos_byte,
21495 limit_byte,
21496 - (height * 2 + 30),
21497 &position);
21498 /* If we couldn't find the lines we wanted within
21499 line_number_display_limit_width chars per line,
21500 give up on line numbers for this window. */
21501 if (position == limit_byte && limit == startpos - distance)
21503 w->base_line_pos = w->buffer;
21504 w->base_line_number = Qnil;
21505 goto no_value;
21508 w->base_line_number = make_number (topline - nlines);
21509 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21512 /* Now count lines from the start pos to point. */
21513 nlines = display_count_lines (startpos_byte,
21514 PT_BYTE, PT, &junk);
21516 /* Record that we did display the line number. */
21517 line_number_displayed = 1;
21519 /* Make the string to show. */
21520 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21521 return decode_mode_spec_buf;
21522 no_value:
21524 char* p = decode_mode_spec_buf;
21525 int pad = field_width - 2;
21526 while (pad-- > 0)
21527 *p++ = ' ';
21528 *p++ = '?';
21529 *p++ = '?';
21530 *p = '\0';
21531 return decode_mode_spec_buf;
21534 break;
21536 case 'm':
21537 obj = BVAR (b, mode_name);
21538 break;
21540 case 'n':
21541 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21542 return " Narrow";
21543 break;
21545 case 'p':
21547 ptrdiff_t pos = marker_position (w->start);
21548 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21550 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21552 if (pos <= BUF_BEGV (b))
21553 return "All";
21554 else
21555 return "Bottom";
21557 else if (pos <= BUF_BEGV (b))
21558 return "Top";
21559 else
21561 if (total > 1000000)
21562 /* Do it differently for a large value, to avoid overflow. */
21563 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21564 else
21565 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21566 /* We can't normally display a 3-digit number,
21567 so get us a 2-digit number that is close. */
21568 if (total == 100)
21569 total = 99;
21570 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21571 return decode_mode_spec_buf;
21575 /* Display percentage of size above the bottom of the screen. */
21576 case 'P':
21578 ptrdiff_t toppos = marker_position (w->start);
21579 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21580 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21582 if (botpos >= BUF_ZV (b))
21584 if (toppos <= BUF_BEGV (b))
21585 return "All";
21586 else
21587 return "Bottom";
21589 else
21591 if (total > 1000000)
21592 /* Do it differently for a large value, to avoid overflow. */
21593 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21594 else
21595 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21596 /* We can't normally display a 3-digit number,
21597 so get us a 2-digit number that is close. */
21598 if (total == 100)
21599 total = 99;
21600 if (toppos <= BUF_BEGV (b))
21601 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21602 else
21603 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21604 return decode_mode_spec_buf;
21608 case 's':
21609 /* status of process */
21610 obj = Fget_buffer_process (Fcurrent_buffer ());
21611 if (NILP (obj))
21612 return "no process";
21613 #ifndef MSDOS
21614 obj = Fsymbol_name (Fprocess_status (obj));
21615 #endif
21616 break;
21618 case '@':
21620 ptrdiff_t count = inhibit_garbage_collection ();
21621 Lisp_Object val = call1 (intern ("file-remote-p"),
21622 BVAR (current_buffer, directory));
21623 unbind_to (count, Qnil);
21625 if (NILP (val))
21626 return "-";
21627 else
21628 return "@";
21631 case 't': /* indicate TEXT or BINARY */
21632 return "T";
21634 case 'z':
21635 /* coding-system (not including end-of-line format) */
21636 case 'Z':
21637 /* coding-system (including end-of-line type) */
21639 int eol_flag = (c == 'Z');
21640 char *p = decode_mode_spec_buf;
21642 if (! FRAME_WINDOW_P (f))
21644 /* No need to mention EOL here--the terminal never needs
21645 to do EOL conversion. */
21646 p = decode_mode_spec_coding (CODING_ID_NAME
21647 (FRAME_KEYBOARD_CODING (f)->id),
21648 p, 0);
21649 p = decode_mode_spec_coding (CODING_ID_NAME
21650 (FRAME_TERMINAL_CODING (f)->id),
21651 p, 0);
21653 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21654 p, eol_flag);
21656 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21657 #ifdef subprocesses
21658 obj = Fget_buffer_process (Fcurrent_buffer ());
21659 if (PROCESSP (obj))
21661 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21662 p, eol_flag);
21663 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21664 p, eol_flag);
21666 #endif /* subprocesses */
21667 #endif /* 0 */
21668 *p = 0;
21669 return decode_mode_spec_buf;
21673 if (STRINGP (obj))
21675 *string = obj;
21676 return SSDATA (obj);
21678 else
21679 return "";
21683 /* Count up to COUNT lines starting from START_BYTE.
21684 But don't go beyond LIMIT_BYTE.
21685 Return the number of lines thus found (always nonnegative).
21687 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21689 static ptrdiff_t
21690 display_count_lines (ptrdiff_t start_byte,
21691 ptrdiff_t limit_byte, ptrdiff_t count,
21692 ptrdiff_t *byte_pos_ptr)
21694 register unsigned char *cursor;
21695 unsigned char *base;
21697 register ptrdiff_t ceiling;
21698 register unsigned char *ceiling_addr;
21699 ptrdiff_t orig_count = count;
21701 /* If we are not in selective display mode,
21702 check only for newlines. */
21703 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21704 && !INTEGERP (BVAR (current_buffer, selective_display)));
21706 if (count > 0)
21708 while (start_byte < limit_byte)
21710 ceiling = BUFFER_CEILING_OF (start_byte);
21711 ceiling = min (limit_byte - 1, ceiling);
21712 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21713 base = (cursor = BYTE_POS_ADDR (start_byte));
21714 while (1)
21716 if (selective_display)
21717 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21719 else
21720 while (*cursor != '\n' && ++cursor != ceiling_addr)
21723 if (cursor != ceiling_addr)
21725 if (--count == 0)
21727 start_byte += cursor - base + 1;
21728 *byte_pos_ptr = start_byte;
21729 return orig_count;
21731 else
21732 if (++cursor == ceiling_addr)
21733 break;
21735 else
21736 break;
21738 start_byte += cursor - base;
21741 else
21743 while (start_byte > limit_byte)
21745 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21746 ceiling = max (limit_byte, ceiling);
21747 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21748 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21749 while (1)
21751 if (selective_display)
21752 while (--cursor != ceiling_addr
21753 && *cursor != '\n' && *cursor != 015)
21755 else
21756 while (--cursor != ceiling_addr && *cursor != '\n')
21759 if (cursor != ceiling_addr)
21761 if (++count == 0)
21763 start_byte += cursor - base + 1;
21764 *byte_pos_ptr = start_byte;
21765 /* When scanning backwards, we should
21766 not count the newline posterior to which we stop. */
21767 return - orig_count - 1;
21770 else
21771 break;
21773 /* Here we add 1 to compensate for the last decrement
21774 of CURSOR, which took it past the valid range. */
21775 start_byte += cursor - base + 1;
21779 *byte_pos_ptr = limit_byte;
21781 if (count < 0)
21782 return - orig_count + count;
21783 return orig_count - count;
21789 /***********************************************************************
21790 Displaying strings
21791 ***********************************************************************/
21793 /* Display a NUL-terminated string, starting with index START.
21795 If STRING is non-null, display that C string. Otherwise, the Lisp
21796 string LISP_STRING is displayed. There's a case that STRING is
21797 non-null and LISP_STRING is not nil. It means STRING is a string
21798 data of LISP_STRING. In that case, we display LISP_STRING while
21799 ignoring its text properties.
21801 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21802 FACE_STRING. Display STRING or LISP_STRING with the face at
21803 FACE_STRING_POS in FACE_STRING:
21805 Display the string in the environment given by IT, but use the
21806 standard display table, temporarily.
21808 FIELD_WIDTH is the minimum number of output glyphs to produce.
21809 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21810 with spaces. If STRING has more characters, more than FIELD_WIDTH
21811 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21813 PRECISION is the maximum number of characters to output from
21814 STRING. PRECISION < 0 means don't truncate the string.
21816 This is roughly equivalent to printf format specifiers:
21818 FIELD_WIDTH PRECISION PRINTF
21819 ----------------------------------------
21820 -1 -1 %s
21821 -1 10 %.10s
21822 10 -1 %10s
21823 20 10 %20.10s
21825 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21826 display them, and < 0 means obey the current buffer's value of
21827 enable_multibyte_characters.
21829 Value is the number of columns displayed. */
21831 static int
21832 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21833 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21834 int field_width, int precision, int max_x, int multibyte)
21836 int hpos_at_start = it->hpos;
21837 int saved_face_id = it->face_id;
21838 struct glyph_row *row = it->glyph_row;
21839 ptrdiff_t it_charpos;
21841 /* Initialize the iterator IT for iteration over STRING beginning
21842 with index START. */
21843 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21844 precision, field_width, multibyte);
21845 if (string && STRINGP (lisp_string))
21846 /* LISP_STRING is the one returned by decode_mode_spec. We should
21847 ignore its text properties. */
21848 it->stop_charpos = it->end_charpos;
21850 /* If displaying STRING, set up the face of the iterator from
21851 FACE_STRING, if that's given. */
21852 if (STRINGP (face_string))
21854 ptrdiff_t endptr;
21855 struct face *face;
21857 it->face_id
21858 = face_at_string_position (it->w, face_string, face_string_pos,
21859 0, it->region_beg_charpos,
21860 it->region_end_charpos,
21861 &endptr, it->base_face_id, 0);
21862 face = FACE_FROM_ID (it->f, it->face_id);
21863 it->face_box_p = face->box != FACE_NO_BOX;
21866 /* Set max_x to the maximum allowed X position. Don't let it go
21867 beyond the right edge of the window. */
21868 if (max_x <= 0)
21869 max_x = it->last_visible_x;
21870 else
21871 max_x = min (max_x, it->last_visible_x);
21873 /* Skip over display elements that are not visible. because IT->w is
21874 hscrolled. */
21875 if (it->current_x < it->first_visible_x)
21876 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21877 MOVE_TO_POS | MOVE_TO_X);
21879 row->ascent = it->max_ascent;
21880 row->height = it->max_ascent + it->max_descent;
21881 row->phys_ascent = it->max_phys_ascent;
21882 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21883 row->extra_line_spacing = it->max_extra_line_spacing;
21885 if (STRINGP (it->string))
21886 it_charpos = IT_STRING_CHARPOS (*it);
21887 else
21888 it_charpos = IT_CHARPOS (*it);
21890 /* This condition is for the case that we are called with current_x
21891 past last_visible_x. */
21892 while (it->current_x < max_x)
21894 int x_before, x, n_glyphs_before, i, nglyphs;
21896 /* Get the next display element. */
21897 if (!get_next_display_element (it))
21898 break;
21900 /* Produce glyphs. */
21901 x_before = it->current_x;
21902 n_glyphs_before = row->used[TEXT_AREA];
21903 PRODUCE_GLYPHS (it);
21905 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21906 i = 0;
21907 x = x_before;
21908 while (i < nglyphs)
21910 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21912 if (it->line_wrap != TRUNCATE
21913 && x + glyph->pixel_width > max_x)
21915 /* End of continued line or max_x reached. */
21916 if (CHAR_GLYPH_PADDING_P (*glyph))
21918 /* A wide character is unbreakable. */
21919 if (row->reversed_p)
21920 unproduce_glyphs (it, row->used[TEXT_AREA]
21921 - n_glyphs_before);
21922 row->used[TEXT_AREA] = n_glyphs_before;
21923 it->current_x = x_before;
21925 else
21927 if (row->reversed_p)
21928 unproduce_glyphs (it, row->used[TEXT_AREA]
21929 - (n_glyphs_before + i));
21930 row->used[TEXT_AREA] = n_glyphs_before + i;
21931 it->current_x = x;
21933 break;
21935 else if (x + glyph->pixel_width >= it->first_visible_x)
21937 /* Glyph is at least partially visible. */
21938 ++it->hpos;
21939 if (x < it->first_visible_x)
21940 row->x = x - it->first_visible_x;
21942 else
21944 /* Glyph is off the left margin of the display area.
21945 Should not happen. */
21946 abort ();
21949 row->ascent = max (row->ascent, it->max_ascent);
21950 row->height = max (row->height, it->max_ascent + it->max_descent);
21951 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21952 row->phys_height = max (row->phys_height,
21953 it->max_phys_ascent + it->max_phys_descent);
21954 row->extra_line_spacing = max (row->extra_line_spacing,
21955 it->max_extra_line_spacing);
21956 x += glyph->pixel_width;
21957 ++i;
21960 /* Stop if max_x reached. */
21961 if (i < nglyphs)
21962 break;
21964 /* Stop at line ends. */
21965 if (ITERATOR_AT_END_OF_LINE_P (it))
21967 it->continuation_lines_width = 0;
21968 break;
21971 set_iterator_to_next (it, 1);
21972 if (STRINGP (it->string))
21973 it_charpos = IT_STRING_CHARPOS (*it);
21974 else
21975 it_charpos = IT_CHARPOS (*it);
21977 /* Stop if truncating at the right edge. */
21978 if (it->line_wrap == TRUNCATE
21979 && it->current_x >= it->last_visible_x)
21981 /* Add truncation mark, but don't do it if the line is
21982 truncated at a padding space. */
21983 if (it_charpos < it->string_nchars)
21985 if (!FRAME_WINDOW_P (it->f))
21987 int ii, n;
21989 if (it->current_x > it->last_visible_x)
21991 if (!row->reversed_p)
21993 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21994 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21995 break;
21997 else
21999 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22000 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22001 break;
22002 unproduce_glyphs (it, ii + 1);
22003 ii = row->used[TEXT_AREA] - (ii + 1);
22005 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22007 row->used[TEXT_AREA] = ii;
22008 produce_special_glyphs (it, IT_TRUNCATION);
22011 produce_special_glyphs (it, IT_TRUNCATION);
22013 row->truncated_on_right_p = 1;
22015 break;
22019 /* Maybe insert a truncation at the left. */
22020 if (it->first_visible_x
22021 && it_charpos > 0)
22023 if (!FRAME_WINDOW_P (it->f)
22024 || (row->reversed_p
22025 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22026 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22027 insert_left_trunc_glyphs (it);
22028 row->truncated_on_left_p = 1;
22031 it->face_id = saved_face_id;
22033 /* Value is number of columns displayed. */
22034 return it->hpos - hpos_at_start;
22039 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22040 appears as an element of LIST or as the car of an element of LIST.
22041 If PROPVAL is a list, compare each element against LIST in that
22042 way, and return 1/2 if any element of PROPVAL is found in LIST.
22043 Otherwise return 0. This function cannot quit.
22044 The return value is 2 if the text is invisible but with an ellipsis
22045 and 1 if it's invisible and without an ellipsis. */
22048 invisible_p (register Lisp_Object propval, Lisp_Object list)
22050 register Lisp_Object tail, proptail;
22052 for (tail = list; CONSP (tail); tail = XCDR (tail))
22054 register Lisp_Object tem;
22055 tem = XCAR (tail);
22056 if (EQ (propval, tem))
22057 return 1;
22058 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22059 return NILP (XCDR (tem)) ? 1 : 2;
22062 if (CONSP (propval))
22064 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22066 Lisp_Object propelt;
22067 propelt = XCAR (proptail);
22068 for (tail = list; CONSP (tail); tail = XCDR (tail))
22070 register Lisp_Object tem;
22071 tem = XCAR (tail);
22072 if (EQ (propelt, tem))
22073 return 1;
22074 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22075 return NILP (XCDR (tem)) ? 1 : 2;
22080 return 0;
22083 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22084 doc: /* Non-nil if the property makes the text invisible.
22085 POS-OR-PROP can be a marker or number, in which case it is taken to be
22086 a position in the current buffer and the value of the `invisible' property
22087 is checked; or it can be some other value, which is then presumed to be the
22088 value of the `invisible' property of the text of interest.
22089 The non-nil value returned can be t for truly invisible text or something
22090 else if the text is replaced by an ellipsis. */)
22091 (Lisp_Object pos_or_prop)
22093 Lisp_Object prop
22094 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22095 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22096 : pos_or_prop);
22097 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22098 return (invis == 0 ? Qnil
22099 : invis == 1 ? Qt
22100 : make_number (invis));
22103 /* Calculate a width or height in pixels from a specification using
22104 the following elements:
22106 SPEC ::=
22107 NUM - a (fractional) multiple of the default font width/height
22108 (NUM) - specifies exactly NUM pixels
22109 UNIT - a fixed number of pixels, see below.
22110 ELEMENT - size of a display element in pixels, see below.
22111 (NUM . SPEC) - equals NUM * SPEC
22112 (+ SPEC SPEC ...) - add pixel values
22113 (- SPEC SPEC ...) - subtract pixel values
22114 (- SPEC) - negate pixel value
22116 NUM ::=
22117 INT or FLOAT - a number constant
22118 SYMBOL - use symbol's (buffer local) variable binding.
22120 UNIT ::=
22121 in - pixels per inch *)
22122 mm - pixels per 1/1000 meter *)
22123 cm - pixels per 1/100 meter *)
22124 width - width of current font in pixels.
22125 height - height of current font in pixels.
22127 *) using the ratio(s) defined in display-pixels-per-inch.
22129 ELEMENT ::=
22131 left-fringe - left fringe width in pixels
22132 right-fringe - right fringe width in pixels
22134 left-margin - left margin width in pixels
22135 right-margin - right margin width in pixels
22137 scroll-bar - scroll-bar area width in pixels
22139 Examples:
22141 Pixels corresponding to 5 inches:
22142 (5 . in)
22144 Total width of non-text areas on left side of window (if scroll-bar is on left):
22145 '(space :width (+ left-fringe left-margin scroll-bar))
22147 Align to first text column (in header line):
22148 '(space :align-to 0)
22150 Align to middle of text area minus half the width of variable `my-image'
22151 containing a loaded image:
22152 '(space :align-to (0.5 . (- text my-image)))
22154 Width of left margin minus width of 1 character in the default font:
22155 '(space :width (- left-margin 1))
22157 Width of left margin minus width of 2 characters in the current font:
22158 '(space :width (- left-margin (2 . width)))
22160 Center 1 character over left-margin (in header line):
22161 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22163 Different ways to express width of left fringe plus left margin minus one pixel:
22164 '(space :width (- (+ left-fringe left-margin) (1)))
22165 '(space :width (+ left-fringe left-margin (- (1))))
22166 '(space :width (+ left-fringe left-margin (-1)))
22170 #define NUMVAL(X) \
22171 ((INTEGERP (X) || FLOATP (X)) \
22172 ? XFLOATINT (X) \
22173 : - 1)
22175 static int
22176 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22177 struct font *font, int width_p, int *align_to)
22179 double pixels;
22181 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22182 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22184 if (NILP (prop))
22185 return OK_PIXELS (0);
22187 eassert (FRAME_LIVE_P (it->f));
22189 if (SYMBOLP (prop))
22191 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22193 char *unit = SSDATA (SYMBOL_NAME (prop));
22195 if (unit[0] == 'i' && unit[1] == 'n')
22196 pixels = 1.0;
22197 else if (unit[0] == 'm' && unit[1] == 'm')
22198 pixels = 25.4;
22199 else if (unit[0] == 'c' && unit[1] == 'm')
22200 pixels = 2.54;
22201 else
22202 pixels = 0;
22203 if (pixels > 0)
22205 double ppi;
22206 #ifdef HAVE_WINDOW_SYSTEM
22207 if (FRAME_WINDOW_P (it->f)
22208 && (ppi = (width_p
22209 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22210 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22211 ppi > 0))
22212 return OK_PIXELS (ppi / pixels);
22213 #endif
22215 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22216 || (CONSP (Vdisplay_pixels_per_inch)
22217 && (ppi = (width_p
22218 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22219 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22220 ppi > 0)))
22221 return OK_PIXELS (ppi / pixels);
22223 return 0;
22227 #ifdef HAVE_WINDOW_SYSTEM
22228 if (EQ (prop, Qheight))
22229 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22230 if (EQ (prop, Qwidth))
22231 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22232 #else
22233 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22234 return OK_PIXELS (1);
22235 #endif
22237 if (EQ (prop, Qtext))
22238 return OK_PIXELS (width_p
22239 ? window_box_width (it->w, TEXT_AREA)
22240 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22242 if (align_to && *align_to < 0)
22244 *res = 0;
22245 if (EQ (prop, Qleft))
22246 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22247 if (EQ (prop, Qright))
22248 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22249 if (EQ (prop, Qcenter))
22250 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22251 + window_box_width (it->w, TEXT_AREA) / 2);
22252 if (EQ (prop, Qleft_fringe))
22253 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22254 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22255 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22256 if (EQ (prop, Qright_fringe))
22257 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22258 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22259 : window_box_right_offset (it->w, TEXT_AREA));
22260 if (EQ (prop, Qleft_margin))
22261 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22262 if (EQ (prop, Qright_margin))
22263 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22264 if (EQ (prop, Qscroll_bar))
22265 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22267 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22268 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22269 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22270 : 0)));
22272 else
22274 if (EQ (prop, Qleft_fringe))
22275 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22276 if (EQ (prop, Qright_fringe))
22277 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22278 if (EQ (prop, Qleft_margin))
22279 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22280 if (EQ (prop, Qright_margin))
22281 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22282 if (EQ (prop, Qscroll_bar))
22283 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22286 prop = buffer_local_value_1 (prop, it->w->buffer);
22287 if (EQ (prop, Qunbound))
22288 prop = Qnil;
22291 if (INTEGERP (prop) || FLOATP (prop))
22293 int base_unit = (width_p
22294 ? FRAME_COLUMN_WIDTH (it->f)
22295 : FRAME_LINE_HEIGHT (it->f));
22296 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22299 if (CONSP (prop))
22301 Lisp_Object car = XCAR (prop);
22302 Lisp_Object cdr = XCDR (prop);
22304 if (SYMBOLP (car))
22306 #ifdef HAVE_WINDOW_SYSTEM
22307 if (FRAME_WINDOW_P (it->f)
22308 && valid_image_p (prop))
22310 ptrdiff_t id = lookup_image (it->f, prop);
22311 struct image *img = IMAGE_FROM_ID (it->f, id);
22313 return OK_PIXELS (width_p ? img->width : img->height);
22315 #endif
22316 if (EQ (car, Qplus) || EQ (car, Qminus))
22318 int first = 1;
22319 double px;
22321 pixels = 0;
22322 while (CONSP (cdr))
22324 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22325 font, width_p, align_to))
22326 return 0;
22327 if (first)
22328 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22329 else
22330 pixels += px;
22331 cdr = XCDR (cdr);
22333 if (EQ (car, Qminus))
22334 pixels = -pixels;
22335 return OK_PIXELS (pixels);
22338 car = buffer_local_value_1 (car, it->w->buffer);
22339 if (EQ (car, Qunbound))
22340 car = Qnil;
22343 if (INTEGERP (car) || FLOATP (car))
22345 double fact;
22346 pixels = XFLOATINT (car);
22347 if (NILP (cdr))
22348 return OK_PIXELS (pixels);
22349 if (calc_pixel_width_or_height (&fact, it, cdr,
22350 font, width_p, align_to))
22351 return OK_PIXELS (pixels * fact);
22352 return 0;
22355 return 0;
22358 return 0;
22362 /***********************************************************************
22363 Glyph Display
22364 ***********************************************************************/
22366 #ifdef HAVE_WINDOW_SYSTEM
22368 #ifdef GLYPH_DEBUG
22370 void
22371 dump_glyph_string (struct glyph_string *s)
22373 fprintf (stderr, "glyph string\n");
22374 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22375 s->x, s->y, s->width, s->height);
22376 fprintf (stderr, " ybase = %d\n", s->ybase);
22377 fprintf (stderr, " hl = %d\n", s->hl);
22378 fprintf (stderr, " left overhang = %d, right = %d\n",
22379 s->left_overhang, s->right_overhang);
22380 fprintf (stderr, " nchars = %d\n", s->nchars);
22381 fprintf (stderr, " extends to end of line = %d\n",
22382 s->extends_to_end_of_line_p);
22383 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22384 fprintf (stderr, " bg width = %d\n", s->background_width);
22387 #endif /* GLYPH_DEBUG */
22389 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22390 of XChar2b structures for S; it can't be allocated in
22391 init_glyph_string because it must be allocated via `alloca'. W
22392 is the window on which S is drawn. ROW and AREA are the glyph row
22393 and area within the row from which S is constructed. START is the
22394 index of the first glyph structure covered by S. HL is a
22395 face-override for drawing S. */
22397 #ifdef HAVE_NTGUI
22398 #define OPTIONAL_HDC(hdc) HDC hdc,
22399 #define DECLARE_HDC(hdc) HDC hdc;
22400 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22401 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22402 #endif
22404 #ifndef OPTIONAL_HDC
22405 #define OPTIONAL_HDC(hdc)
22406 #define DECLARE_HDC(hdc)
22407 #define ALLOCATE_HDC(hdc, f)
22408 #define RELEASE_HDC(hdc, f)
22409 #endif
22411 static void
22412 init_glyph_string (struct glyph_string *s,
22413 OPTIONAL_HDC (hdc)
22414 XChar2b *char2b, struct window *w, struct glyph_row *row,
22415 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22417 memset (s, 0, sizeof *s);
22418 s->w = w;
22419 s->f = XFRAME (w->frame);
22420 #ifdef HAVE_NTGUI
22421 s->hdc = hdc;
22422 #endif
22423 s->display = FRAME_X_DISPLAY (s->f);
22424 s->window = FRAME_X_WINDOW (s->f);
22425 s->char2b = char2b;
22426 s->hl = hl;
22427 s->row = row;
22428 s->area = area;
22429 s->first_glyph = row->glyphs[area] + start;
22430 s->height = row->height;
22431 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22432 s->ybase = s->y + row->ascent;
22436 /* Append the list of glyph strings with head H and tail T to the list
22437 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22439 static inline void
22440 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22441 struct glyph_string *h, struct glyph_string *t)
22443 if (h)
22445 if (*head)
22446 (*tail)->next = h;
22447 else
22448 *head = h;
22449 h->prev = *tail;
22450 *tail = t;
22455 /* Prepend the list of glyph strings with head H and tail T to the
22456 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22457 result. */
22459 static inline void
22460 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22461 struct glyph_string *h, struct glyph_string *t)
22463 if (h)
22465 if (*head)
22466 (*head)->prev = t;
22467 else
22468 *tail = t;
22469 t->next = *head;
22470 *head = h;
22475 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22476 Set *HEAD and *TAIL to the resulting list. */
22478 static inline void
22479 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22480 struct glyph_string *s)
22482 s->next = s->prev = NULL;
22483 append_glyph_string_lists (head, tail, s, s);
22487 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22488 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22489 make sure that X resources for the face returned are allocated.
22490 Value is a pointer to a realized face that is ready for display if
22491 DISPLAY_P is non-zero. */
22493 static inline struct face *
22494 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22495 XChar2b *char2b, int display_p)
22497 struct face *face = FACE_FROM_ID (f, face_id);
22499 if (face->font)
22501 unsigned code = face->font->driver->encode_char (face->font, c);
22503 if (code != FONT_INVALID_CODE)
22504 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22505 else
22506 STORE_XCHAR2B (char2b, 0, 0);
22509 /* Make sure X resources of the face are allocated. */
22510 #ifdef HAVE_X_WINDOWS
22511 if (display_p)
22512 #endif
22514 eassert (face != NULL);
22515 PREPARE_FACE_FOR_DISPLAY (f, face);
22518 return face;
22522 /* Get face and two-byte form of character glyph GLYPH on frame F.
22523 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22524 a pointer to a realized face that is ready for display. */
22526 static inline struct face *
22527 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22528 XChar2b *char2b, int *two_byte_p)
22530 struct face *face;
22532 eassert (glyph->type == CHAR_GLYPH);
22533 face = FACE_FROM_ID (f, glyph->face_id);
22535 if (two_byte_p)
22536 *two_byte_p = 0;
22538 if (face->font)
22540 unsigned code;
22542 if (CHAR_BYTE8_P (glyph->u.ch))
22543 code = CHAR_TO_BYTE8 (glyph->u.ch);
22544 else
22545 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22547 if (code != FONT_INVALID_CODE)
22548 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22549 else
22550 STORE_XCHAR2B (char2b, 0, 0);
22553 /* Make sure X resources of the face are allocated. */
22554 eassert (face != NULL);
22555 PREPARE_FACE_FOR_DISPLAY (f, face);
22556 return face;
22560 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22561 Return 1 if FONT has a glyph for C, otherwise return 0. */
22563 static inline int
22564 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22566 unsigned code;
22568 if (CHAR_BYTE8_P (c))
22569 code = CHAR_TO_BYTE8 (c);
22570 else
22571 code = font->driver->encode_char (font, c);
22573 if (code == FONT_INVALID_CODE)
22574 return 0;
22575 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22576 return 1;
22580 /* Fill glyph string S with composition components specified by S->cmp.
22582 BASE_FACE is the base face of the composition.
22583 S->cmp_from is the index of the first component for S.
22585 OVERLAPS non-zero means S should draw the foreground only, and use
22586 its physical height for clipping. See also draw_glyphs.
22588 Value is the index of a component not in S. */
22590 static int
22591 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22592 int overlaps)
22594 int i;
22595 /* For all glyphs of this composition, starting at the offset
22596 S->cmp_from, until we reach the end of the definition or encounter a
22597 glyph that requires the different face, add it to S. */
22598 struct face *face;
22600 eassert (s);
22602 s->for_overlaps = overlaps;
22603 s->face = NULL;
22604 s->font = NULL;
22605 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22607 int c = COMPOSITION_GLYPH (s->cmp, i);
22609 /* TAB in a composition means display glyphs with padding space
22610 on the left or right. */
22611 if (c != '\t')
22613 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22614 -1, Qnil);
22616 face = get_char_face_and_encoding (s->f, c, face_id,
22617 s->char2b + i, 1);
22618 if (face)
22620 if (! s->face)
22622 s->face = face;
22623 s->font = s->face->font;
22625 else if (s->face != face)
22626 break;
22629 ++s->nchars;
22631 s->cmp_to = i;
22633 if (s->face == NULL)
22635 s->face = base_face->ascii_face;
22636 s->font = s->face->font;
22639 /* All glyph strings for the same composition has the same width,
22640 i.e. the width set for the first component of the composition. */
22641 s->width = s->first_glyph->pixel_width;
22643 /* If the specified font could not be loaded, use the frame's
22644 default font, but record the fact that we couldn't load it in
22645 the glyph string so that we can draw rectangles for the
22646 characters of the glyph string. */
22647 if (s->font == NULL)
22649 s->font_not_found_p = 1;
22650 s->font = FRAME_FONT (s->f);
22653 /* Adjust base line for subscript/superscript text. */
22654 s->ybase += s->first_glyph->voffset;
22656 /* This glyph string must always be drawn with 16-bit functions. */
22657 s->two_byte_p = 1;
22659 return s->cmp_to;
22662 static int
22663 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22664 int start, int end, int overlaps)
22666 struct glyph *glyph, *last;
22667 Lisp_Object lgstring;
22668 int i;
22670 s->for_overlaps = overlaps;
22671 glyph = s->row->glyphs[s->area] + start;
22672 last = s->row->glyphs[s->area] + end;
22673 s->cmp_id = glyph->u.cmp.id;
22674 s->cmp_from = glyph->slice.cmp.from;
22675 s->cmp_to = glyph->slice.cmp.to + 1;
22676 s->face = FACE_FROM_ID (s->f, face_id);
22677 lgstring = composition_gstring_from_id (s->cmp_id);
22678 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22679 glyph++;
22680 while (glyph < last
22681 && glyph->u.cmp.automatic
22682 && glyph->u.cmp.id == s->cmp_id
22683 && s->cmp_to == glyph->slice.cmp.from)
22684 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22686 for (i = s->cmp_from; i < s->cmp_to; i++)
22688 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22689 unsigned code = LGLYPH_CODE (lglyph);
22691 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22693 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22694 return glyph - s->row->glyphs[s->area];
22698 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22699 See the comment of fill_glyph_string for arguments.
22700 Value is the index of the first glyph not in S. */
22703 static int
22704 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22705 int start, int end, int overlaps)
22707 struct glyph *glyph, *last;
22708 int voffset;
22710 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22711 s->for_overlaps = overlaps;
22712 glyph = s->row->glyphs[s->area] + start;
22713 last = s->row->glyphs[s->area] + end;
22714 voffset = glyph->voffset;
22715 s->face = FACE_FROM_ID (s->f, face_id);
22716 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22717 s->nchars = 1;
22718 s->width = glyph->pixel_width;
22719 glyph++;
22720 while (glyph < last
22721 && glyph->type == GLYPHLESS_GLYPH
22722 && glyph->voffset == voffset
22723 && glyph->face_id == face_id)
22725 s->nchars++;
22726 s->width += glyph->pixel_width;
22727 glyph++;
22729 s->ybase += voffset;
22730 return glyph - s->row->glyphs[s->area];
22734 /* Fill glyph string S from a sequence of character glyphs.
22736 FACE_ID is the face id of the string. START is the index of the
22737 first glyph to consider, END is the index of the last + 1.
22738 OVERLAPS non-zero means S should draw the foreground only, and use
22739 its physical height for clipping. See also draw_glyphs.
22741 Value is the index of the first glyph not in S. */
22743 static int
22744 fill_glyph_string (struct glyph_string *s, int face_id,
22745 int start, int end, int overlaps)
22747 struct glyph *glyph, *last;
22748 int voffset;
22749 int glyph_not_available_p;
22751 eassert (s->f == XFRAME (s->w->frame));
22752 eassert (s->nchars == 0);
22753 eassert (start >= 0 && end > start);
22755 s->for_overlaps = overlaps;
22756 glyph = s->row->glyphs[s->area] + start;
22757 last = s->row->glyphs[s->area] + end;
22758 voffset = glyph->voffset;
22759 s->padding_p = glyph->padding_p;
22760 glyph_not_available_p = glyph->glyph_not_available_p;
22762 while (glyph < last
22763 && glyph->type == CHAR_GLYPH
22764 && glyph->voffset == voffset
22765 /* Same face id implies same font, nowadays. */
22766 && glyph->face_id == face_id
22767 && glyph->glyph_not_available_p == glyph_not_available_p)
22769 int two_byte_p;
22771 s->face = get_glyph_face_and_encoding (s->f, glyph,
22772 s->char2b + s->nchars,
22773 &two_byte_p);
22774 s->two_byte_p = two_byte_p;
22775 ++s->nchars;
22776 eassert (s->nchars <= end - start);
22777 s->width += glyph->pixel_width;
22778 if (glyph++->padding_p != s->padding_p)
22779 break;
22782 s->font = s->face->font;
22784 /* If the specified font could not be loaded, use the frame's font,
22785 but record the fact that we couldn't load it in
22786 S->font_not_found_p so that we can draw rectangles for the
22787 characters of the glyph string. */
22788 if (s->font == NULL || glyph_not_available_p)
22790 s->font_not_found_p = 1;
22791 s->font = FRAME_FONT (s->f);
22794 /* Adjust base line for subscript/superscript text. */
22795 s->ybase += voffset;
22797 eassert (s->face && s->face->gc);
22798 return glyph - s->row->glyphs[s->area];
22802 /* Fill glyph string S from image glyph S->first_glyph. */
22804 static void
22805 fill_image_glyph_string (struct glyph_string *s)
22807 eassert (s->first_glyph->type == IMAGE_GLYPH);
22808 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22809 eassert (s->img);
22810 s->slice = s->first_glyph->slice.img;
22811 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22812 s->font = s->face->font;
22813 s->width = s->first_glyph->pixel_width;
22815 /* Adjust base line for subscript/superscript text. */
22816 s->ybase += s->first_glyph->voffset;
22820 /* Fill glyph string S from a sequence of stretch glyphs.
22822 START is the index of the first glyph to consider,
22823 END is the index of the last + 1.
22825 Value is the index of the first glyph not in S. */
22827 static int
22828 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22830 struct glyph *glyph, *last;
22831 int voffset, face_id;
22833 eassert (s->first_glyph->type == STRETCH_GLYPH);
22835 glyph = s->row->glyphs[s->area] + start;
22836 last = s->row->glyphs[s->area] + end;
22837 face_id = glyph->face_id;
22838 s->face = FACE_FROM_ID (s->f, face_id);
22839 s->font = s->face->font;
22840 s->width = glyph->pixel_width;
22841 s->nchars = 1;
22842 voffset = glyph->voffset;
22844 for (++glyph;
22845 (glyph < last
22846 && glyph->type == STRETCH_GLYPH
22847 && glyph->voffset == voffset
22848 && glyph->face_id == face_id);
22849 ++glyph)
22850 s->width += glyph->pixel_width;
22852 /* Adjust base line for subscript/superscript text. */
22853 s->ybase += voffset;
22855 /* The case that face->gc == 0 is handled when drawing the glyph
22856 string by calling PREPARE_FACE_FOR_DISPLAY. */
22857 eassert (s->face);
22858 return glyph - s->row->glyphs[s->area];
22861 static struct font_metrics *
22862 get_per_char_metric (struct font *font, XChar2b *char2b)
22864 static struct font_metrics metrics;
22865 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22867 if (! font || code == FONT_INVALID_CODE)
22868 return NULL;
22869 font->driver->text_extents (font, &code, 1, &metrics);
22870 return &metrics;
22873 /* EXPORT for RIF:
22874 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22875 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22876 assumed to be zero. */
22878 void
22879 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22881 *left = *right = 0;
22883 if (glyph->type == CHAR_GLYPH)
22885 struct face *face;
22886 XChar2b char2b;
22887 struct font_metrics *pcm;
22889 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22890 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22892 if (pcm->rbearing > pcm->width)
22893 *right = pcm->rbearing - pcm->width;
22894 if (pcm->lbearing < 0)
22895 *left = -pcm->lbearing;
22898 else if (glyph->type == COMPOSITE_GLYPH)
22900 if (! glyph->u.cmp.automatic)
22902 struct composition *cmp = composition_table[glyph->u.cmp.id];
22904 if (cmp->rbearing > cmp->pixel_width)
22905 *right = cmp->rbearing - cmp->pixel_width;
22906 if (cmp->lbearing < 0)
22907 *left = - cmp->lbearing;
22909 else
22911 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22912 struct font_metrics metrics;
22914 composition_gstring_width (gstring, glyph->slice.cmp.from,
22915 glyph->slice.cmp.to + 1, &metrics);
22916 if (metrics.rbearing > metrics.width)
22917 *right = metrics.rbearing - metrics.width;
22918 if (metrics.lbearing < 0)
22919 *left = - metrics.lbearing;
22925 /* Return the index of the first glyph preceding glyph string S that
22926 is overwritten by S because of S's left overhang. Value is -1
22927 if no glyphs are overwritten. */
22929 static int
22930 left_overwritten (struct glyph_string *s)
22932 int k;
22934 if (s->left_overhang)
22936 int x = 0, i;
22937 struct glyph *glyphs = s->row->glyphs[s->area];
22938 int first = s->first_glyph - glyphs;
22940 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22941 x -= glyphs[i].pixel_width;
22943 k = i + 1;
22945 else
22946 k = -1;
22948 return k;
22952 /* Return the index of the first glyph preceding glyph string S that
22953 is overwriting S because of its right overhang. Value is -1 if no
22954 glyph in front of S overwrites S. */
22956 static int
22957 left_overwriting (struct glyph_string *s)
22959 int i, k, x;
22960 struct glyph *glyphs = s->row->glyphs[s->area];
22961 int first = s->first_glyph - glyphs;
22963 k = -1;
22964 x = 0;
22965 for (i = first - 1; i >= 0; --i)
22967 int left, right;
22968 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22969 if (x + right > 0)
22970 k = i;
22971 x -= glyphs[i].pixel_width;
22974 return k;
22978 /* Return the index of the last glyph following glyph string S that is
22979 overwritten by S because of S's right overhang. Value is -1 if
22980 no such glyph is found. */
22982 static int
22983 right_overwritten (struct glyph_string *s)
22985 int k = -1;
22987 if (s->right_overhang)
22989 int x = 0, i;
22990 struct glyph *glyphs = s->row->glyphs[s->area];
22991 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22992 int end = s->row->used[s->area];
22994 for (i = first; i < end && s->right_overhang > x; ++i)
22995 x += glyphs[i].pixel_width;
22997 k = i;
23000 return k;
23004 /* Return the index of the last glyph following glyph string S that
23005 overwrites S because of its left overhang. Value is negative
23006 if no such glyph is found. */
23008 static int
23009 right_overwriting (struct glyph_string *s)
23011 int i, k, x;
23012 int end = s->row->used[s->area];
23013 struct glyph *glyphs = s->row->glyphs[s->area];
23014 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23016 k = -1;
23017 x = 0;
23018 for (i = first; i < end; ++i)
23020 int left, right;
23021 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23022 if (x - left < 0)
23023 k = i;
23024 x += glyphs[i].pixel_width;
23027 return k;
23031 /* Set background width of glyph string S. START is the index of the
23032 first glyph following S. LAST_X is the right-most x-position + 1
23033 in the drawing area. */
23035 static inline void
23036 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23038 /* If the face of this glyph string has to be drawn to the end of
23039 the drawing area, set S->extends_to_end_of_line_p. */
23041 if (start == s->row->used[s->area]
23042 && s->area == TEXT_AREA
23043 && ((s->row->fill_line_p
23044 && (s->hl == DRAW_NORMAL_TEXT
23045 || s->hl == DRAW_IMAGE_RAISED
23046 || s->hl == DRAW_IMAGE_SUNKEN))
23047 || s->hl == DRAW_MOUSE_FACE))
23048 s->extends_to_end_of_line_p = 1;
23050 /* If S extends its face to the end of the line, set its
23051 background_width to the distance to the right edge of the drawing
23052 area. */
23053 if (s->extends_to_end_of_line_p)
23054 s->background_width = last_x - s->x + 1;
23055 else
23056 s->background_width = s->width;
23060 /* Compute overhangs and x-positions for glyph string S and its
23061 predecessors, or successors. X is the starting x-position for S.
23062 BACKWARD_P non-zero means process predecessors. */
23064 static void
23065 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23067 if (backward_p)
23069 while (s)
23071 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23072 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23073 x -= s->width;
23074 s->x = x;
23075 s = s->prev;
23078 else
23080 while (s)
23082 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23083 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23084 s->x = x;
23085 x += s->width;
23086 s = s->next;
23093 /* The following macros are only called from draw_glyphs below.
23094 They reference the following parameters of that function directly:
23095 `w', `row', `area', and `overlap_p'
23096 as well as the following local variables:
23097 `s', `f', and `hdc' (in W32) */
23099 #ifdef HAVE_NTGUI
23100 /* On W32, silently add local `hdc' variable to argument list of
23101 init_glyph_string. */
23102 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23103 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23104 #else
23105 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23106 init_glyph_string (s, char2b, w, row, area, start, hl)
23107 #endif
23109 /* Add a glyph string for a stretch glyph to the list of strings
23110 between HEAD and TAIL. START is the index of the stretch glyph in
23111 row area AREA of glyph row ROW. END is the index of the last glyph
23112 in that glyph row area. X is the current output position assigned
23113 to the new glyph string constructed. HL overrides that face of the
23114 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23115 is the right-most x-position of the drawing area. */
23117 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23118 and below -- keep them on one line. */
23119 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23120 do \
23122 s = alloca (sizeof *s); \
23123 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23124 START = fill_stretch_glyph_string (s, START, END); \
23125 append_glyph_string (&HEAD, &TAIL, s); \
23126 s->x = (X); \
23128 while (0)
23131 /* Add a glyph string for an image glyph to the list of strings
23132 between HEAD and TAIL. START is the index of the image glyph in
23133 row area AREA of glyph row ROW. END is the index of the last glyph
23134 in that glyph row area. X is the current output position assigned
23135 to the new glyph string constructed. HL overrides that face of the
23136 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23137 is the right-most x-position of the drawing area. */
23139 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23140 do \
23142 s = alloca (sizeof *s); \
23143 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23144 fill_image_glyph_string (s); \
23145 append_glyph_string (&HEAD, &TAIL, s); \
23146 ++START; \
23147 s->x = (X); \
23149 while (0)
23152 /* Add a glyph string for a sequence of character glyphs to the list
23153 of strings between HEAD and TAIL. START is the index of the first
23154 glyph in row area AREA of glyph row ROW that is part of the new
23155 glyph string. END is the index of the last glyph in that glyph row
23156 area. X is the current output position assigned to the new glyph
23157 string constructed. HL overrides that face of the glyph; e.g. it
23158 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23159 right-most x-position of the drawing area. */
23161 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23162 do \
23164 int face_id; \
23165 XChar2b *char2b; \
23167 face_id = (row)->glyphs[area][START].face_id; \
23169 s = alloca (sizeof *s); \
23170 char2b = alloca ((END - START) * sizeof *char2b); \
23171 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23172 append_glyph_string (&HEAD, &TAIL, s); \
23173 s->x = (X); \
23174 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23176 while (0)
23179 /* Add a glyph string for a composite sequence to the list of strings
23180 between HEAD and TAIL. START is the index of the first glyph in
23181 row area AREA of glyph row ROW that is part of the new glyph
23182 string. END is the index of the last glyph in that glyph row area.
23183 X is the current output position assigned to the new glyph string
23184 constructed. HL overrides that face of the glyph; e.g. it is
23185 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23186 x-position of the drawing area. */
23188 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23189 do { \
23190 int face_id = (row)->glyphs[area][START].face_id; \
23191 struct face *base_face = FACE_FROM_ID (f, face_id); \
23192 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23193 struct composition *cmp = composition_table[cmp_id]; \
23194 XChar2b *char2b; \
23195 struct glyph_string *first_s = NULL; \
23196 int n; \
23198 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23200 /* Make glyph_strings for each glyph sequence that is drawable by \
23201 the same face, and append them to HEAD/TAIL. */ \
23202 for (n = 0; n < cmp->glyph_len;) \
23204 s = alloca (sizeof *s); \
23205 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23206 append_glyph_string (&(HEAD), &(TAIL), s); \
23207 s->cmp = cmp; \
23208 s->cmp_from = n; \
23209 s->x = (X); \
23210 if (n == 0) \
23211 first_s = s; \
23212 n = fill_composite_glyph_string (s, base_face, overlaps); \
23215 ++START; \
23216 s = first_s; \
23217 } while (0)
23220 /* Add a glyph string for a glyph-string sequence to the list of strings
23221 between HEAD and TAIL. */
23223 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23224 do { \
23225 int face_id; \
23226 XChar2b *char2b; \
23227 Lisp_Object gstring; \
23229 face_id = (row)->glyphs[area][START].face_id; \
23230 gstring = (composition_gstring_from_id \
23231 ((row)->glyphs[area][START].u.cmp.id)); \
23232 s = alloca (sizeof *s); \
23233 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23234 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23235 append_glyph_string (&(HEAD), &(TAIL), s); \
23236 s->x = (X); \
23237 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23238 } while (0)
23241 /* Add a glyph string for a sequence of glyphless character's glyphs
23242 to the list of strings between HEAD and TAIL. The meanings of
23243 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23245 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23246 do \
23248 int face_id; \
23250 face_id = (row)->glyphs[area][START].face_id; \
23252 s = alloca (sizeof *s); \
23253 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23254 append_glyph_string (&HEAD, &TAIL, s); \
23255 s->x = (X); \
23256 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23257 overlaps); \
23259 while (0)
23262 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23263 of AREA of glyph row ROW on window W between indices START and END.
23264 HL overrides the face for drawing glyph strings, e.g. it is
23265 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23266 x-positions of the drawing area.
23268 This is an ugly monster macro construct because we must use alloca
23269 to allocate glyph strings (because draw_glyphs can be called
23270 asynchronously). */
23272 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23273 do \
23275 HEAD = TAIL = NULL; \
23276 while (START < END) \
23278 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23279 switch (first_glyph->type) \
23281 case CHAR_GLYPH: \
23282 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23283 HL, X, LAST_X); \
23284 break; \
23286 case COMPOSITE_GLYPH: \
23287 if (first_glyph->u.cmp.automatic) \
23288 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23289 HL, X, LAST_X); \
23290 else \
23291 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23292 HL, X, LAST_X); \
23293 break; \
23295 case STRETCH_GLYPH: \
23296 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23297 HL, X, LAST_X); \
23298 break; \
23300 case IMAGE_GLYPH: \
23301 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23302 HL, X, LAST_X); \
23303 break; \
23305 case GLYPHLESS_GLYPH: \
23306 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23307 HL, X, LAST_X); \
23308 break; \
23310 default: \
23311 abort (); \
23314 if (s) \
23316 set_glyph_string_background_width (s, START, LAST_X); \
23317 (X) += s->width; \
23320 } while (0)
23323 /* Draw glyphs between START and END in AREA of ROW on window W,
23324 starting at x-position X. X is relative to AREA in W. HL is a
23325 face-override with the following meaning:
23327 DRAW_NORMAL_TEXT draw normally
23328 DRAW_CURSOR draw in cursor face
23329 DRAW_MOUSE_FACE draw in mouse face.
23330 DRAW_INVERSE_VIDEO draw in mode line face
23331 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23332 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23334 If OVERLAPS is non-zero, draw only the foreground of characters and
23335 clip to the physical height of ROW. Non-zero value also defines
23336 the overlapping part to be drawn:
23338 OVERLAPS_PRED overlap with preceding rows
23339 OVERLAPS_SUCC overlap with succeeding rows
23340 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23341 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23343 Value is the x-position reached, relative to AREA of W. */
23345 static int
23346 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23347 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23348 enum draw_glyphs_face hl, int overlaps)
23350 struct glyph_string *head, *tail;
23351 struct glyph_string *s;
23352 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23353 int i, j, x_reached, last_x, area_left = 0;
23354 struct frame *f = XFRAME (WINDOW_FRAME (w));
23355 DECLARE_HDC (hdc);
23357 ALLOCATE_HDC (hdc, f);
23359 /* Let's rather be paranoid than getting a SEGV. */
23360 end = min (end, row->used[area]);
23361 start = max (0, start);
23362 start = min (end, start);
23364 /* Translate X to frame coordinates. Set last_x to the right
23365 end of the drawing area. */
23366 if (row->full_width_p)
23368 /* X is relative to the left edge of W, without scroll bars
23369 or fringes. */
23370 area_left = WINDOW_LEFT_EDGE_X (w);
23371 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23373 else
23375 area_left = window_box_left (w, area);
23376 last_x = area_left + window_box_width (w, area);
23378 x += area_left;
23380 /* Build a doubly-linked list of glyph_string structures between
23381 head and tail from what we have to draw. Note that the macro
23382 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23383 the reason we use a separate variable `i'. */
23384 i = start;
23385 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23386 if (tail)
23387 x_reached = tail->x + tail->background_width;
23388 else
23389 x_reached = x;
23391 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23392 the row, redraw some glyphs in front or following the glyph
23393 strings built above. */
23394 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23396 struct glyph_string *h, *t;
23397 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23398 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23399 int check_mouse_face = 0;
23400 int dummy_x = 0;
23402 /* If mouse highlighting is on, we may need to draw adjacent
23403 glyphs using mouse-face highlighting. */
23404 if (area == TEXT_AREA && row->mouse_face_p)
23406 struct glyph_row *mouse_beg_row, *mouse_end_row;
23408 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23409 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23411 if (row >= mouse_beg_row && row <= mouse_end_row)
23413 check_mouse_face = 1;
23414 mouse_beg_col = (row == mouse_beg_row)
23415 ? hlinfo->mouse_face_beg_col : 0;
23416 mouse_end_col = (row == mouse_end_row)
23417 ? hlinfo->mouse_face_end_col
23418 : row->used[TEXT_AREA];
23422 /* Compute overhangs for all glyph strings. */
23423 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23424 for (s = head; s; s = s->next)
23425 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23427 /* Prepend glyph strings for glyphs in front of the first glyph
23428 string that are overwritten because of the first glyph
23429 string's left overhang. The background of all strings
23430 prepended must be drawn because the first glyph string
23431 draws over it. */
23432 i = left_overwritten (head);
23433 if (i >= 0)
23435 enum draw_glyphs_face overlap_hl;
23437 /* If this row contains mouse highlighting, attempt to draw
23438 the overlapped glyphs with the correct highlight. This
23439 code fails if the overlap encompasses more than one glyph
23440 and mouse-highlight spans only some of these glyphs.
23441 However, making it work perfectly involves a lot more
23442 code, and I don't know if the pathological case occurs in
23443 practice, so we'll stick to this for now. --- cyd */
23444 if (check_mouse_face
23445 && mouse_beg_col < start && mouse_end_col > i)
23446 overlap_hl = DRAW_MOUSE_FACE;
23447 else
23448 overlap_hl = DRAW_NORMAL_TEXT;
23450 j = i;
23451 BUILD_GLYPH_STRINGS (j, start, h, t,
23452 overlap_hl, dummy_x, last_x);
23453 start = i;
23454 compute_overhangs_and_x (t, head->x, 1);
23455 prepend_glyph_string_lists (&head, &tail, h, t);
23456 clip_head = head;
23459 /* Prepend glyph strings for glyphs in front of the first glyph
23460 string that overwrite that glyph string because of their
23461 right overhang. For these strings, only the foreground must
23462 be drawn, because it draws over the glyph string at `head'.
23463 The background must not be drawn because this would overwrite
23464 right overhangs of preceding glyphs for which no glyph
23465 strings exist. */
23466 i = left_overwriting (head);
23467 if (i >= 0)
23469 enum draw_glyphs_face overlap_hl;
23471 if (check_mouse_face
23472 && mouse_beg_col < start && mouse_end_col > i)
23473 overlap_hl = DRAW_MOUSE_FACE;
23474 else
23475 overlap_hl = DRAW_NORMAL_TEXT;
23477 clip_head = head;
23478 BUILD_GLYPH_STRINGS (i, start, h, t,
23479 overlap_hl, dummy_x, last_x);
23480 for (s = h; s; s = s->next)
23481 s->background_filled_p = 1;
23482 compute_overhangs_and_x (t, head->x, 1);
23483 prepend_glyph_string_lists (&head, &tail, h, t);
23486 /* Append glyphs strings for glyphs following the last glyph
23487 string tail that are overwritten by tail. The background of
23488 these strings has to be drawn because tail's foreground draws
23489 over it. */
23490 i = right_overwritten (tail);
23491 if (i >= 0)
23493 enum draw_glyphs_face overlap_hl;
23495 if (check_mouse_face
23496 && mouse_beg_col < i && mouse_end_col > end)
23497 overlap_hl = DRAW_MOUSE_FACE;
23498 else
23499 overlap_hl = DRAW_NORMAL_TEXT;
23501 BUILD_GLYPH_STRINGS (end, i, h, t,
23502 overlap_hl, x, last_x);
23503 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23504 we don't have `end = i;' here. */
23505 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23506 append_glyph_string_lists (&head, &tail, h, t);
23507 clip_tail = tail;
23510 /* Append glyph strings for glyphs following the last glyph
23511 string tail that overwrite tail. The foreground of such
23512 glyphs has to be drawn because it writes into the background
23513 of tail. The background must not be drawn because it could
23514 paint over the foreground of following glyphs. */
23515 i = right_overwriting (tail);
23516 if (i >= 0)
23518 enum draw_glyphs_face overlap_hl;
23519 if (check_mouse_face
23520 && mouse_beg_col < i && mouse_end_col > end)
23521 overlap_hl = DRAW_MOUSE_FACE;
23522 else
23523 overlap_hl = DRAW_NORMAL_TEXT;
23525 clip_tail = tail;
23526 i++; /* We must include the Ith glyph. */
23527 BUILD_GLYPH_STRINGS (end, i, h, t,
23528 overlap_hl, x, last_x);
23529 for (s = h; s; s = s->next)
23530 s->background_filled_p = 1;
23531 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23532 append_glyph_string_lists (&head, &tail, h, t);
23534 if (clip_head || clip_tail)
23535 for (s = head; s; s = s->next)
23537 s->clip_head = clip_head;
23538 s->clip_tail = clip_tail;
23542 /* Draw all strings. */
23543 for (s = head; s; s = s->next)
23544 FRAME_RIF (f)->draw_glyph_string (s);
23546 #ifndef HAVE_NS
23547 /* When focus a sole frame and move horizontally, this sets on_p to 0
23548 causing a failure to erase prev cursor position. */
23549 if (area == TEXT_AREA
23550 && !row->full_width_p
23551 /* When drawing overlapping rows, only the glyph strings'
23552 foreground is drawn, which doesn't erase a cursor
23553 completely. */
23554 && !overlaps)
23556 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23557 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23558 : (tail ? tail->x + tail->background_width : x));
23559 x0 -= area_left;
23560 x1 -= area_left;
23562 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23563 row->y, MATRIX_ROW_BOTTOM_Y (row));
23565 #endif
23567 /* Value is the x-position up to which drawn, relative to AREA of W.
23568 This doesn't include parts drawn because of overhangs. */
23569 if (row->full_width_p)
23570 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23571 else
23572 x_reached -= area_left;
23574 RELEASE_HDC (hdc, f);
23576 return x_reached;
23579 /* Expand row matrix if too narrow. Don't expand if area
23580 is not present. */
23582 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23584 if (!fonts_changed_p \
23585 && (it->glyph_row->glyphs[area] \
23586 < it->glyph_row->glyphs[area + 1])) \
23588 it->w->ncols_scale_factor++; \
23589 fonts_changed_p = 1; \
23593 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23594 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23596 static inline void
23597 append_glyph (struct it *it)
23599 struct glyph *glyph;
23600 enum glyph_row_area area = it->area;
23602 eassert (it->glyph_row);
23603 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23605 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23606 if (glyph < it->glyph_row->glyphs[area + 1])
23608 /* If the glyph row is reversed, we need to prepend the glyph
23609 rather than append it. */
23610 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23612 struct glyph *g;
23614 /* Make room for the additional glyph. */
23615 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23616 g[1] = *g;
23617 glyph = it->glyph_row->glyphs[area];
23619 glyph->charpos = CHARPOS (it->position);
23620 glyph->object = it->object;
23621 if (it->pixel_width > 0)
23623 glyph->pixel_width = it->pixel_width;
23624 glyph->padding_p = 0;
23626 else
23628 /* Assure at least 1-pixel width. Otherwise, cursor can't
23629 be displayed correctly. */
23630 glyph->pixel_width = 1;
23631 glyph->padding_p = 1;
23633 glyph->ascent = it->ascent;
23634 glyph->descent = it->descent;
23635 glyph->voffset = it->voffset;
23636 glyph->type = CHAR_GLYPH;
23637 glyph->avoid_cursor_p = it->avoid_cursor_p;
23638 glyph->multibyte_p = it->multibyte_p;
23639 glyph->left_box_line_p = it->start_of_box_run_p;
23640 glyph->right_box_line_p = it->end_of_box_run_p;
23641 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23642 || it->phys_descent > it->descent);
23643 glyph->glyph_not_available_p = it->glyph_not_available_p;
23644 glyph->face_id = it->face_id;
23645 glyph->u.ch = it->char_to_display;
23646 glyph->slice.img = null_glyph_slice;
23647 glyph->font_type = FONT_TYPE_UNKNOWN;
23648 if (it->bidi_p)
23650 glyph->resolved_level = it->bidi_it.resolved_level;
23651 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23652 abort ();
23653 glyph->bidi_type = it->bidi_it.type;
23655 else
23657 glyph->resolved_level = 0;
23658 glyph->bidi_type = UNKNOWN_BT;
23660 ++it->glyph_row->used[area];
23662 else
23663 IT_EXPAND_MATRIX_WIDTH (it, area);
23666 /* Store one glyph for the composition IT->cmp_it.id in
23667 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23668 non-null. */
23670 static inline void
23671 append_composite_glyph (struct it *it)
23673 struct glyph *glyph;
23674 enum glyph_row_area area = it->area;
23676 eassert (it->glyph_row);
23678 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23679 if (glyph < it->glyph_row->glyphs[area + 1])
23681 /* If the glyph row is reversed, we need to prepend the glyph
23682 rather than append it. */
23683 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23685 struct glyph *g;
23687 /* Make room for the new glyph. */
23688 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23689 g[1] = *g;
23690 glyph = it->glyph_row->glyphs[it->area];
23692 glyph->charpos = it->cmp_it.charpos;
23693 glyph->object = it->object;
23694 glyph->pixel_width = it->pixel_width;
23695 glyph->ascent = it->ascent;
23696 glyph->descent = it->descent;
23697 glyph->voffset = it->voffset;
23698 glyph->type = COMPOSITE_GLYPH;
23699 if (it->cmp_it.ch < 0)
23701 glyph->u.cmp.automatic = 0;
23702 glyph->u.cmp.id = it->cmp_it.id;
23703 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23705 else
23707 glyph->u.cmp.automatic = 1;
23708 glyph->u.cmp.id = it->cmp_it.id;
23709 glyph->slice.cmp.from = it->cmp_it.from;
23710 glyph->slice.cmp.to = it->cmp_it.to - 1;
23712 glyph->avoid_cursor_p = it->avoid_cursor_p;
23713 glyph->multibyte_p = it->multibyte_p;
23714 glyph->left_box_line_p = it->start_of_box_run_p;
23715 glyph->right_box_line_p = it->end_of_box_run_p;
23716 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23717 || it->phys_descent > it->descent);
23718 glyph->padding_p = 0;
23719 glyph->glyph_not_available_p = 0;
23720 glyph->face_id = it->face_id;
23721 glyph->font_type = FONT_TYPE_UNKNOWN;
23722 if (it->bidi_p)
23724 glyph->resolved_level = it->bidi_it.resolved_level;
23725 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23726 abort ();
23727 glyph->bidi_type = it->bidi_it.type;
23729 ++it->glyph_row->used[area];
23731 else
23732 IT_EXPAND_MATRIX_WIDTH (it, area);
23736 /* Change IT->ascent and IT->height according to the setting of
23737 IT->voffset. */
23739 static inline void
23740 take_vertical_position_into_account (struct it *it)
23742 if (it->voffset)
23744 if (it->voffset < 0)
23745 /* Increase the ascent so that we can display the text higher
23746 in the line. */
23747 it->ascent -= it->voffset;
23748 else
23749 /* Increase the descent so that we can display the text lower
23750 in the line. */
23751 it->descent += it->voffset;
23756 /* Produce glyphs/get display metrics for the image IT is loaded with.
23757 See the description of struct display_iterator in dispextern.h for
23758 an overview of struct display_iterator. */
23760 static void
23761 produce_image_glyph (struct it *it)
23763 struct image *img;
23764 struct face *face;
23765 int glyph_ascent, crop;
23766 struct glyph_slice slice;
23768 eassert (it->what == IT_IMAGE);
23770 face = FACE_FROM_ID (it->f, it->face_id);
23771 eassert (face);
23772 /* Make sure X resources of the face is loaded. */
23773 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23775 if (it->image_id < 0)
23777 /* Fringe bitmap. */
23778 it->ascent = it->phys_ascent = 0;
23779 it->descent = it->phys_descent = 0;
23780 it->pixel_width = 0;
23781 it->nglyphs = 0;
23782 return;
23785 img = IMAGE_FROM_ID (it->f, it->image_id);
23786 eassert (img);
23787 /* Make sure X resources of the image is loaded. */
23788 prepare_image_for_display (it->f, img);
23790 slice.x = slice.y = 0;
23791 slice.width = img->width;
23792 slice.height = img->height;
23794 if (INTEGERP (it->slice.x))
23795 slice.x = XINT (it->slice.x);
23796 else if (FLOATP (it->slice.x))
23797 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23799 if (INTEGERP (it->slice.y))
23800 slice.y = XINT (it->slice.y);
23801 else if (FLOATP (it->slice.y))
23802 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23804 if (INTEGERP (it->slice.width))
23805 slice.width = XINT (it->slice.width);
23806 else if (FLOATP (it->slice.width))
23807 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23809 if (INTEGERP (it->slice.height))
23810 slice.height = XINT (it->slice.height);
23811 else if (FLOATP (it->slice.height))
23812 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23814 if (slice.x >= img->width)
23815 slice.x = img->width;
23816 if (slice.y >= img->height)
23817 slice.y = img->height;
23818 if (slice.x + slice.width >= img->width)
23819 slice.width = img->width - slice.x;
23820 if (slice.y + slice.height > img->height)
23821 slice.height = img->height - slice.y;
23823 if (slice.width == 0 || slice.height == 0)
23824 return;
23826 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23828 it->descent = slice.height - glyph_ascent;
23829 if (slice.y == 0)
23830 it->descent += img->vmargin;
23831 if (slice.y + slice.height == img->height)
23832 it->descent += img->vmargin;
23833 it->phys_descent = it->descent;
23835 it->pixel_width = slice.width;
23836 if (slice.x == 0)
23837 it->pixel_width += img->hmargin;
23838 if (slice.x + slice.width == img->width)
23839 it->pixel_width += img->hmargin;
23841 /* It's quite possible for images to have an ascent greater than
23842 their height, so don't get confused in that case. */
23843 if (it->descent < 0)
23844 it->descent = 0;
23846 it->nglyphs = 1;
23848 if (face->box != FACE_NO_BOX)
23850 if (face->box_line_width > 0)
23852 if (slice.y == 0)
23853 it->ascent += face->box_line_width;
23854 if (slice.y + slice.height == img->height)
23855 it->descent += face->box_line_width;
23858 if (it->start_of_box_run_p && slice.x == 0)
23859 it->pixel_width += eabs (face->box_line_width);
23860 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23861 it->pixel_width += eabs (face->box_line_width);
23864 take_vertical_position_into_account (it);
23866 /* Automatically crop wide image glyphs at right edge so we can
23867 draw the cursor on same display row. */
23868 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23869 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23871 it->pixel_width -= crop;
23872 slice.width -= crop;
23875 if (it->glyph_row)
23877 struct glyph *glyph;
23878 enum glyph_row_area area = it->area;
23880 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23881 if (glyph < it->glyph_row->glyphs[area + 1])
23883 glyph->charpos = CHARPOS (it->position);
23884 glyph->object = it->object;
23885 glyph->pixel_width = it->pixel_width;
23886 glyph->ascent = glyph_ascent;
23887 glyph->descent = it->descent;
23888 glyph->voffset = it->voffset;
23889 glyph->type = IMAGE_GLYPH;
23890 glyph->avoid_cursor_p = it->avoid_cursor_p;
23891 glyph->multibyte_p = it->multibyte_p;
23892 glyph->left_box_line_p = it->start_of_box_run_p;
23893 glyph->right_box_line_p = it->end_of_box_run_p;
23894 glyph->overlaps_vertically_p = 0;
23895 glyph->padding_p = 0;
23896 glyph->glyph_not_available_p = 0;
23897 glyph->face_id = it->face_id;
23898 glyph->u.img_id = img->id;
23899 glyph->slice.img = slice;
23900 glyph->font_type = FONT_TYPE_UNKNOWN;
23901 if (it->bidi_p)
23903 glyph->resolved_level = it->bidi_it.resolved_level;
23904 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23905 abort ();
23906 glyph->bidi_type = it->bidi_it.type;
23908 ++it->glyph_row->used[area];
23910 else
23911 IT_EXPAND_MATRIX_WIDTH (it, area);
23916 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23917 of the glyph, WIDTH and HEIGHT are the width and height of the
23918 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23920 static void
23921 append_stretch_glyph (struct it *it, Lisp_Object object,
23922 int width, int height, int ascent)
23924 struct glyph *glyph;
23925 enum glyph_row_area area = it->area;
23927 eassert (ascent >= 0 && ascent <= height);
23929 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23930 if (glyph < it->glyph_row->glyphs[area + 1])
23932 /* If the glyph row is reversed, we need to prepend the glyph
23933 rather than append it. */
23934 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23936 struct glyph *g;
23938 /* Make room for the additional glyph. */
23939 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23940 g[1] = *g;
23941 glyph = it->glyph_row->glyphs[area];
23943 glyph->charpos = CHARPOS (it->position);
23944 glyph->object = object;
23945 glyph->pixel_width = width;
23946 glyph->ascent = ascent;
23947 glyph->descent = height - ascent;
23948 glyph->voffset = it->voffset;
23949 glyph->type = STRETCH_GLYPH;
23950 glyph->avoid_cursor_p = it->avoid_cursor_p;
23951 glyph->multibyte_p = it->multibyte_p;
23952 glyph->left_box_line_p = it->start_of_box_run_p;
23953 glyph->right_box_line_p = it->end_of_box_run_p;
23954 glyph->overlaps_vertically_p = 0;
23955 glyph->padding_p = 0;
23956 glyph->glyph_not_available_p = 0;
23957 glyph->face_id = it->face_id;
23958 glyph->u.stretch.ascent = ascent;
23959 glyph->u.stretch.height = height;
23960 glyph->slice.img = null_glyph_slice;
23961 glyph->font_type = FONT_TYPE_UNKNOWN;
23962 if (it->bidi_p)
23964 glyph->resolved_level = it->bidi_it.resolved_level;
23965 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23966 abort ();
23967 glyph->bidi_type = it->bidi_it.type;
23969 else
23971 glyph->resolved_level = 0;
23972 glyph->bidi_type = UNKNOWN_BT;
23974 ++it->glyph_row->used[area];
23976 else
23977 IT_EXPAND_MATRIX_WIDTH (it, area);
23980 #endif /* HAVE_WINDOW_SYSTEM */
23982 /* Produce a stretch glyph for iterator IT. IT->object is the value
23983 of the glyph property displayed. The value must be a list
23984 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23985 being recognized:
23987 1. `:width WIDTH' specifies that the space should be WIDTH *
23988 canonical char width wide. WIDTH may be an integer or floating
23989 point number.
23991 2. `:relative-width FACTOR' specifies that the width of the stretch
23992 should be computed from the width of the first character having the
23993 `glyph' property, and should be FACTOR times that width.
23995 3. `:align-to HPOS' specifies that the space should be wide enough
23996 to reach HPOS, a value in canonical character units.
23998 Exactly one of the above pairs must be present.
24000 4. `:height HEIGHT' specifies that the height of the stretch produced
24001 should be HEIGHT, measured in canonical character units.
24003 5. `:relative-height FACTOR' specifies that the height of the
24004 stretch should be FACTOR times the height of the characters having
24005 the glyph property.
24007 Either none or exactly one of 4 or 5 must be present.
24009 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24010 of the stretch should be used for the ascent of the stretch.
24011 ASCENT must be in the range 0 <= ASCENT <= 100. */
24013 void
24014 produce_stretch_glyph (struct it *it)
24016 /* (space :width WIDTH :height HEIGHT ...) */
24017 Lisp_Object prop, plist;
24018 int width = 0, height = 0, align_to = -1;
24019 int zero_width_ok_p = 0;
24020 int ascent = 0;
24021 double tem;
24022 struct face *face = NULL;
24023 struct font *font = NULL;
24025 #ifdef HAVE_WINDOW_SYSTEM
24026 int zero_height_ok_p = 0;
24028 if (FRAME_WINDOW_P (it->f))
24030 face = FACE_FROM_ID (it->f, it->face_id);
24031 font = face->font ? face->font : FRAME_FONT (it->f);
24032 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24034 #endif
24036 /* List should start with `space'. */
24037 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24038 plist = XCDR (it->object);
24040 /* Compute the width of the stretch. */
24041 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24042 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24044 /* Absolute width `:width WIDTH' specified and valid. */
24045 zero_width_ok_p = 1;
24046 width = (int)tem;
24048 #ifdef HAVE_WINDOW_SYSTEM
24049 else if (FRAME_WINDOW_P (it->f)
24050 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24052 /* Relative width `:relative-width FACTOR' specified and valid.
24053 Compute the width of the characters having the `glyph'
24054 property. */
24055 struct it it2;
24056 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24058 it2 = *it;
24059 if (it->multibyte_p)
24060 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24061 else
24063 it2.c = it2.char_to_display = *p, it2.len = 1;
24064 if (! ASCII_CHAR_P (it2.c))
24065 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24068 it2.glyph_row = NULL;
24069 it2.what = IT_CHARACTER;
24070 x_produce_glyphs (&it2);
24071 width = NUMVAL (prop) * it2.pixel_width;
24073 #endif /* HAVE_WINDOW_SYSTEM */
24074 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24075 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24077 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24078 align_to = (align_to < 0
24080 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24081 else if (align_to < 0)
24082 align_to = window_box_left_offset (it->w, TEXT_AREA);
24083 width = max (0, (int)tem + align_to - it->current_x);
24084 zero_width_ok_p = 1;
24086 else
24087 /* Nothing specified -> width defaults to canonical char width. */
24088 width = FRAME_COLUMN_WIDTH (it->f);
24090 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24091 width = 1;
24093 #ifdef HAVE_WINDOW_SYSTEM
24094 /* Compute height. */
24095 if (FRAME_WINDOW_P (it->f))
24097 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24098 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24100 height = (int)tem;
24101 zero_height_ok_p = 1;
24103 else if (prop = Fplist_get (plist, QCrelative_height),
24104 NUMVAL (prop) > 0)
24105 height = FONT_HEIGHT (font) * NUMVAL (prop);
24106 else
24107 height = FONT_HEIGHT (font);
24109 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24110 height = 1;
24112 /* Compute percentage of height used for ascent. If
24113 `:ascent ASCENT' is present and valid, use that. Otherwise,
24114 derive the ascent from the font in use. */
24115 if (prop = Fplist_get (plist, QCascent),
24116 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24117 ascent = height * NUMVAL (prop) / 100.0;
24118 else if (!NILP (prop)
24119 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24120 ascent = min (max (0, (int)tem), height);
24121 else
24122 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24124 else
24125 #endif /* HAVE_WINDOW_SYSTEM */
24126 height = 1;
24128 if (width > 0 && it->line_wrap != TRUNCATE
24129 && it->current_x + width > it->last_visible_x)
24131 width = it->last_visible_x - it->current_x;
24132 #ifdef HAVE_WINDOW_SYSTEM
24133 /* Subtract one more pixel from the stretch width, but only on
24134 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24135 width -= FRAME_WINDOW_P (it->f);
24136 #endif
24139 if (width > 0 && height > 0 && it->glyph_row)
24141 Lisp_Object o_object = it->object;
24142 Lisp_Object object = it->stack[it->sp - 1].string;
24143 int n = width;
24145 if (!STRINGP (object))
24146 object = it->w->buffer;
24147 #ifdef HAVE_WINDOW_SYSTEM
24148 if (FRAME_WINDOW_P (it->f))
24149 append_stretch_glyph (it, object, width, height, ascent);
24150 else
24151 #endif
24153 it->object = object;
24154 it->char_to_display = ' ';
24155 it->pixel_width = it->len = 1;
24156 while (n--)
24157 tty_append_glyph (it);
24158 it->object = o_object;
24162 it->pixel_width = width;
24163 #ifdef HAVE_WINDOW_SYSTEM
24164 if (FRAME_WINDOW_P (it->f))
24166 it->ascent = it->phys_ascent = ascent;
24167 it->descent = it->phys_descent = height - it->ascent;
24168 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24169 take_vertical_position_into_account (it);
24171 else
24172 #endif
24173 it->nglyphs = width;
24176 /* Get information about special display element WHAT in an
24177 environment described by IT. WHAT is one of IT_TRUNCATION or
24178 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24179 non-null glyph_row member. This function ensures that fields like
24180 face_id, c, len of IT are left untouched. */
24182 void
24183 produce_special_glyphs (struct it *it, enum display_element_type what)
24185 struct it temp_it;
24186 Lisp_Object gc;
24187 GLYPH glyph;
24189 temp_it = *it;
24190 temp_it.object = make_number (0);
24191 memset (&temp_it.current, 0, sizeof temp_it.current);
24193 if (what == IT_CONTINUATION)
24195 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24196 if (it->bidi_it.paragraph_dir == R2L)
24197 SET_GLYPH_FROM_CHAR (glyph, '/');
24198 else
24199 SET_GLYPH_FROM_CHAR (glyph, '\\');
24200 if (it->dp
24201 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24203 /* FIXME: Should we mirror GC for R2L lines? */
24204 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24205 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24208 else if (what == IT_TRUNCATION)
24210 /* Truncation glyph. */
24211 SET_GLYPH_FROM_CHAR (glyph, '$');
24212 if (it->dp
24213 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24215 /* FIXME: Should we mirror GC for R2L lines? */
24216 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24217 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24220 else
24221 abort ();
24223 #ifdef HAVE_WINDOW_SYSTEM
24224 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24225 is turned off, we precede the truncation/continuation glyphs by a
24226 stretch glyph whose width is computed such that these special
24227 glyphs are aligned at the window margin, even when very different
24228 fonts are used in different glyph rows. */
24229 if (FRAME_WINDOW_P (temp_it.f)
24230 /* init_iterator calls this with it->glyph_row == NULL, and it
24231 wants only the pixel width of the truncation/continuation
24232 glyphs. */
24233 && temp_it.glyph_row
24234 /* insert_left_trunc_glyphs calls us at the beginning of the
24235 row, and it has its own calculation of the stretch glyph
24236 width. */
24237 && temp_it.glyph_row->used[TEXT_AREA] > 0
24238 && (temp_it.glyph_row->reversed_p
24239 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24240 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24242 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24244 if (stretch_width > 0)
24246 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24247 struct font *font =
24248 face->font ? face->font : FRAME_FONT (temp_it.f);
24249 int stretch_ascent =
24250 (((temp_it.ascent + temp_it.descent)
24251 * FONT_BASE (font)) / FONT_HEIGHT (font));
24253 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24254 temp_it.ascent + temp_it.descent,
24255 stretch_ascent);
24258 #endif
24260 temp_it.dp = NULL;
24261 temp_it.what = IT_CHARACTER;
24262 temp_it.len = 1;
24263 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24264 temp_it.face_id = GLYPH_FACE (glyph);
24265 temp_it.len = CHAR_BYTES (temp_it.c);
24267 PRODUCE_GLYPHS (&temp_it);
24268 it->pixel_width = temp_it.pixel_width;
24269 it->nglyphs = temp_it.pixel_width;
24272 #ifdef HAVE_WINDOW_SYSTEM
24274 /* Calculate line-height and line-spacing properties.
24275 An integer value specifies explicit pixel value.
24276 A float value specifies relative value to current face height.
24277 A cons (float . face-name) specifies relative value to
24278 height of specified face font.
24280 Returns height in pixels, or nil. */
24283 static Lisp_Object
24284 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24285 int boff, int override)
24287 Lisp_Object face_name = Qnil;
24288 int ascent, descent, height;
24290 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24291 return val;
24293 if (CONSP (val))
24295 face_name = XCAR (val);
24296 val = XCDR (val);
24297 if (!NUMBERP (val))
24298 val = make_number (1);
24299 if (NILP (face_name))
24301 height = it->ascent + it->descent;
24302 goto scale;
24306 if (NILP (face_name))
24308 font = FRAME_FONT (it->f);
24309 boff = FRAME_BASELINE_OFFSET (it->f);
24311 else if (EQ (face_name, Qt))
24313 override = 0;
24315 else
24317 int face_id;
24318 struct face *face;
24320 face_id = lookup_named_face (it->f, face_name, 0);
24321 if (face_id < 0)
24322 return make_number (-1);
24324 face = FACE_FROM_ID (it->f, face_id);
24325 font = face->font;
24326 if (font == NULL)
24327 return make_number (-1);
24328 boff = font->baseline_offset;
24329 if (font->vertical_centering)
24330 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24333 ascent = FONT_BASE (font) + boff;
24334 descent = FONT_DESCENT (font) - boff;
24336 if (override)
24338 it->override_ascent = ascent;
24339 it->override_descent = descent;
24340 it->override_boff = boff;
24343 height = ascent + descent;
24345 scale:
24346 if (FLOATP (val))
24347 height = (int)(XFLOAT_DATA (val) * height);
24348 else if (INTEGERP (val))
24349 height *= XINT (val);
24351 return make_number (height);
24355 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24356 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24357 and only if this is for a character for which no font was found.
24359 If the display method (it->glyphless_method) is
24360 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24361 length of the acronym or the hexadecimal string, UPPER_XOFF and
24362 UPPER_YOFF are pixel offsets for the upper part of the string,
24363 LOWER_XOFF and LOWER_YOFF are for the lower part.
24365 For the other display methods, LEN through LOWER_YOFF are zero. */
24367 static void
24368 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24369 short upper_xoff, short upper_yoff,
24370 short lower_xoff, short lower_yoff)
24372 struct glyph *glyph;
24373 enum glyph_row_area area = it->area;
24375 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24376 if (glyph < it->glyph_row->glyphs[area + 1])
24378 /* If the glyph row is reversed, we need to prepend the glyph
24379 rather than append it. */
24380 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24382 struct glyph *g;
24384 /* Make room for the additional glyph. */
24385 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24386 g[1] = *g;
24387 glyph = it->glyph_row->glyphs[area];
24389 glyph->charpos = CHARPOS (it->position);
24390 glyph->object = it->object;
24391 glyph->pixel_width = it->pixel_width;
24392 glyph->ascent = it->ascent;
24393 glyph->descent = it->descent;
24394 glyph->voffset = it->voffset;
24395 glyph->type = GLYPHLESS_GLYPH;
24396 glyph->u.glyphless.method = it->glyphless_method;
24397 glyph->u.glyphless.for_no_font = for_no_font;
24398 glyph->u.glyphless.len = len;
24399 glyph->u.glyphless.ch = it->c;
24400 glyph->slice.glyphless.upper_xoff = upper_xoff;
24401 glyph->slice.glyphless.upper_yoff = upper_yoff;
24402 glyph->slice.glyphless.lower_xoff = lower_xoff;
24403 glyph->slice.glyphless.lower_yoff = lower_yoff;
24404 glyph->avoid_cursor_p = it->avoid_cursor_p;
24405 glyph->multibyte_p = it->multibyte_p;
24406 glyph->left_box_line_p = it->start_of_box_run_p;
24407 glyph->right_box_line_p = it->end_of_box_run_p;
24408 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24409 || it->phys_descent > it->descent);
24410 glyph->padding_p = 0;
24411 glyph->glyph_not_available_p = 0;
24412 glyph->face_id = face_id;
24413 glyph->font_type = FONT_TYPE_UNKNOWN;
24414 if (it->bidi_p)
24416 glyph->resolved_level = it->bidi_it.resolved_level;
24417 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24418 abort ();
24419 glyph->bidi_type = it->bidi_it.type;
24421 ++it->glyph_row->used[area];
24423 else
24424 IT_EXPAND_MATRIX_WIDTH (it, area);
24428 /* Produce a glyph for a glyphless character for iterator IT.
24429 IT->glyphless_method specifies which method to use for displaying
24430 the character. See the description of enum
24431 glyphless_display_method in dispextern.h for the detail.
24433 FOR_NO_FONT is nonzero if and only if this is for a character for
24434 which no font was found. ACRONYM, if non-nil, is an acronym string
24435 for the character. */
24437 static void
24438 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24440 int face_id;
24441 struct face *face;
24442 struct font *font;
24443 int base_width, base_height, width, height;
24444 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24445 int len;
24447 /* Get the metrics of the base font. We always refer to the current
24448 ASCII face. */
24449 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24450 font = face->font ? face->font : FRAME_FONT (it->f);
24451 it->ascent = FONT_BASE (font) + font->baseline_offset;
24452 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24453 base_height = it->ascent + it->descent;
24454 base_width = font->average_width;
24456 /* Get a face ID for the glyph by utilizing a cache (the same way as
24457 done for `escape-glyph' in get_next_display_element). */
24458 if (it->f == last_glyphless_glyph_frame
24459 && it->face_id == last_glyphless_glyph_face_id)
24461 face_id = last_glyphless_glyph_merged_face_id;
24463 else
24465 /* Merge the `glyphless-char' face into the current face. */
24466 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24467 last_glyphless_glyph_frame = it->f;
24468 last_glyphless_glyph_face_id = it->face_id;
24469 last_glyphless_glyph_merged_face_id = face_id;
24472 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24474 it->pixel_width = THIN_SPACE_WIDTH;
24475 len = 0;
24476 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24478 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24480 width = CHAR_WIDTH (it->c);
24481 if (width == 0)
24482 width = 1;
24483 else if (width > 4)
24484 width = 4;
24485 it->pixel_width = base_width * width;
24486 len = 0;
24487 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24489 else
24491 char buf[7];
24492 const char *str;
24493 unsigned int code[6];
24494 int upper_len;
24495 int ascent, descent;
24496 struct font_metrics metrics_upper, metrics_lower;
24498 face = FACE_FROM_ID (it->f, face_id);
24499 font = face->font ? face->font : FRAME_FONT (it->f);
24500 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24502 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24504 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24505 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24506 if (CONSP (acronym))
24507 acronym = XCAR (acronym);
24508 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24510 else
24512 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24513 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24514 str = buf;
24516 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24517 code[len] = font->driver->encode_char (font, str[len]);
24518 upper_len = (len + 1) / 2;
24519 font->driver->text_extents (font, code, upper_len,
24520 &metrics_upper);
24521 font->driver->text_extents (font, code + upper_len, len - upper_len,
24522 &metrics_lower);
24526 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24527 width = max (metrics_upper.width, metrics_lower.width) + 4;
24528 upper_xoff = upper_yoff = 2; /* the typical case */
24529 if (base_width >= width)
24531 /* Align the upper to the left, the lower to the right. */
24532 it->pixel_width = base_width;
24533 lower_xoff = base_width - 2 - metrics_lower.width;
24535 else
24537 /* Center the shorter one. */
24538 it->pixel_width = width;
24539 if (metrics_upper.width >= metrics_lower.width)
24540 lower_xoff = (width - metrics_lower.width) / 2;
24541 else
24543 /* FIXME: This code doesn't look right. It formerly was
24544 missing the "lower_xoff = 0;", which couldn't have
24545 been right since it left lower_xoff uninitialized. */
24546 lower_xoff = 0;
24547 upper_xoff = (width - metrics_upper.width) / 2;
24551 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24552 top, bottom, and between upper and lower strings. */
24553 height = (metrics_upper.ascent + metrics_upper.descent
24554 + metrics_lower.ascent + metrics_lower.descent) + 5;
24555 /* Center vertically.
24556 H:base_height, D:base_descent
24557 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24559 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24560 descent = D - H/2 + h/2;
24561 lower_yoff = descent - 2 - ld;
24562 upper_yoff = lower_yoff - la - 1 - ud; */
24563 ascent = - (it->descent - (base_height + height + 1) / 2);
24564 descent = it->descent - (base_height - height) / 2;
24565 lower_yoff = descent - 2 - metrics_lower.descent;
24566 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24567 - metrics_upper.descent);
24568 /* Don't make the height shorter than the base height. */
24569 if (height > base_height)
24571 it->ascent = ascent;
24572 it->descent = descent;
24576 it->phys_ascent = it->ascent;
24577 it->phys_descent = it->descent;
24578 if (it->glyph_row)
24579 append_glyphless_glyph (it, face_id, for_no_font, len,
24580 upper_xoff, upper_yoff,
24581 lower_xoff, lower_yoff);
24582 it->nglyphs = 1;
24583 take_vertical_position_into_account (it);
24587 /* RIF:
24588 Produce glyphs/get display metrics for the display element IT is
24589 loaded with. See the description of struct it in dispextern.h
24590 for an overview of struct it. */
24592 void
24593 x_produce_glyphs (struct it *it)
24595 int extra_line_spacing = it->extra_line_spacing;
24597 it->glyph_not_available_p = 0;
24599 if (it->what == IT_CHARACTER)
24601 XChar2b char2b;
24602 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24603 struct font *font = face->font;
24604 struct font_metrics *pcm = NULL;
24605 int boff; /* baseline offset */
24607 if (font == NULL)
24609 /* When no suitable font is found, display this character by
24610 the method specified in the first extra slot of
24611 Vglyphless_char_display. */
24612 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24614 eassert (it->what == IT_GLYPHLESS);
24615 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24616 goto done;
24619 boff = font->baseline_offset;
24620 if (font->vertical_centering)
24621 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24623 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24625 int stretched_p;
24627 it->nglyphs = 1;
24629 if (it->override_ascent >= 0)
24631 it->ascent = it->override_ascent;
24632 it->descent = it->override_descent;
24633 boff = it->override_boff;
24635 else
24637 it->ascent = FONT_BASE (font) + boff;
24638 it->descent = FONT_DESCENT (font) - boff;
24641 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24643 pcm = get_per_char_metric (font, &char2b);
24644 if (pcm->width == 0
24645 && pcm->rbearing == 0 && pcm->lbearing == 0)
24646 pcm = NULL;
24649 if (pcm)
24651 it->phys_ascent = pcm->ascent + boff;
24652 it->phys_descent = pcm->descent - boff;
24653 it->pixel_width = pcm->width;
24655 else
24657 it->glyph_not_available_p = 1;
24658 it->phys_ascent = it->ascent;
24659 it->phys_descent = it->descent;
24660 it->pixel_width = font->space_width;
24663 if (it->constrain_row_ascent_descent_p)
24665 if (it->descent > it->max_descent)
24667 it->ascent += it->descent - it->max_descent;
24668 it->descent = it->max_descent;
24670 if (it->ascent > it->max_ascent)
24672 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24673 it->ascent = it->max_ascent;
24675 it->phys_ascent = min (it->phys_ascent, it->ascent);
24676 it->phys_descent = min (it->phys_descent, it->descent);
24677 extra_line_spacing = 0;
24680 /* If this is a space inside a region of text with
24681 `space-width' property, change its width. */
24682 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24683 if (stretched_p)
24684 it->pixel_width *= XFLOATINT (it->space_width);
24686 /* If face has a box, add the box thickness to the character
24687 height. If character has a box line to the left and/or
24688 right, add the box line width to the character's width. */
24689 if (face->box != FACE_NO_BOX)
24691 int thick = face->box_line_width;
24693 if (thick > 0)
24695 it->ascent += thick;
24696 it->descent += thick;
24698 else
24699 thick = -thick;
24701 if (it->start_of_box_run_p)
24702 it->pixel_width += thick;
24703 if (it->end_of_box_run_p)
24704 it->pixel_width += thick;
24707 /* If face has an overline, add the height of the overline
24708 (1 pixel) and a 1 pixel margin to the character height. */
24709 if (face->overline_p)
24710 it->ascent += overline_margin;
24712 if (it->constrain_row_ascent_descent_p)
24714 if (it->ascent > it->max_ascent)
24715 it->ascent = it->max_ascent;
24716 if (it->descent > it->max_descent)
24717 it->descent = it->max_descent;
24720 take_vertical_position_into_account (it);
24722 /* If we have to actually produce glyphs, do it. */
24723 if (it->glyph_row)
24725 if (stretched_p)
24727 /* Translate a space with a `space-width' property
24728 into a stretch glyph. */
24729 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24730 / FONT_HEIGHT (font));
24731 append_stretch_glyph (it, it->object, it->pixel_width,
24732 it->ascent + it->descent, ascent);
24734 else
24735 append_glyph (it);
24737 /* If characters with lbearing or rbearing are displayed
24738 in this line, record that fact in a flag of the
24739 glyph row. This is used to optimize X output code. */
24740 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24741 it->glyph_row->contains_overlapping_glyphs_p = 1;
24743 if (! stretched_p && it->pixel_width == 0)
24744 /* We assure that all visible glyphs have at least 1-pixel
24745 width. */
24746 it->pixel_width = 1;
24748 else if (it->char_to_display == '\n')
24750 /* A newline has no width, but we need the height of the
24751 line. But if previous part of the line sets a height,
24752 don't increase that height */
24754 Lisp_Object height;
24755 Lisp_Object total_height = Qnil;
24757 it->override_ascent = -1;
24758 it->pixel_width = 0;
24759 it->nglyphs = 0;
24761 height = get_it_property (it, Qline_height);
24762 /* Split (line-height total-height) list */
24763 if (CONSP (height)
24764 && CONSP (XCDR (height))
24765 && NILP (XCDR (XCDR (height))))
24767 total_height = XCAR (XCDR (height));
24768 height = XCAR (height);
24770 height = calc_line_height_property (it, height, font, boff, 1);
24772 if (it->override_ascent >= 0)
24774 it->ascent = it->override_ascent;
24775 it->descent = it->override_descent;
24776 boff = it->override_boff;
24778 else
24780 it->ascent = FONT_BASE (font) + boff;
24781 it->descent = FONT_DESCENT (font) - boff;
24784 if (EQ (height, Qt))
24786 if (it->descent > it->max_descent)
24788 it->ascent += it->descent - it->max_descent;
24789 it->descent = it->max_descent;
24791 if (it->ascent > it->max_ascent)
24793 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24794 it->ascent = it->max_ascent;
24796 it->phys_ascent = min (it->phys_ascent, it->ascent);
24797 it->phys_descent = min (it->phys_descent, it->descent);
24798 it->constrain_row_ascent_descent_p = 1;
24799 extra_line_spacing = 0;
24801 else
24803 Lisp_Object spacing;
24805 it->phys_ascent = it->ascent;
24806 it->phys_descent = it->descent;
24808 if ((it->max_ascent > 0 || it->max_descent > 0)
24809 && face->box != FACE_NO_BOX
24810 && face->box_line_width > 0)
24812 it->ascent += face->box_line_width;
24813 it->descent += face->box_line_width;
24815 if (!NILP (height)
24816 && XINT (height) > it->ascent + it->descent)
24817 it->ascent = XINT (height) - it->descent;
24819 if (!NILP (total_height))
24820 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24821 else
24823 spacing = get_it_property (it, Qline_spacing);
24824 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24826 if (INTEGERP (spacing))
24828 extra_line_spacing = XINT (spacing);
24829 if (!NILP (total_height))
24830 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24834 else /* i.e. (it->char_to_display == '\t') */
24836 if (font->space_width > 0)
24838 int tab_width = it->tab_width * font->space_width;
24839 int x = it->current_x + it->continuation_lines_width;
24840 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24842 /* If the distance from the current position to the next tab
24843 stop is less than a space character width, use the
24844 tab stop after that. */
24845 if (next_tab_x - x < font->space_width)
24846 next_tab_x += tab_width;
24848 it->pixel_width = next_tab_x - x;
24849 it->nglyphs = 1;
24850 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24851 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24853 if (it->glyph_row)
24855 append_stretch_glyph (it, it->object, it->pixel_width,
24856 it->ascent + it->descent, it->ascent);
24859 else
24861 it->pixel_width = 0;
24862 it->nglyphs = 1;
24866 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24868 /* A static composition.
24870 Note: A composition is represented as one glyph in the
24871 glyph matrix. There are no padding glyphs.
24873 Important note: pixel_width, ascent, and descent are the
24874 values of what is drawn by draw_glyphs (i.e. the values of
24875 the overall glyphs composed). */
24876 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24877 int boff; /* baseline offset */
24878 struct composition *cmp = composition_table[it->cmp_it.id];
24879 int glyph_len = cmp->glyph_len;
24880 struct font *font = face->font;
24882 it->nglyphs = 1;
24884 /* If we have not yet calculated pixel size data of glyphs of
24885 the composition for the current face font, calculate them
24886 now. Theoretically, we have to check all fonts for the
24887 glyphs, but that requires much time and memory space. So,
24888 here we check only the font of the first glyph. This may
24889 lead to incorrect display, but it's very rare, and C-l
24890 (recenter-top-bottom) can correct the display anyway. */
24891 if (! cmp->font || cmp->font != font)
24893 /* Ascent and descent of the font of the first character
24894 of this composition (adjusted by baseline offset).
24895 Ascent and descent of overall glyphs should not be less
24896 than these, respectively. */
24897 int font_ascent, font_descent, font_height;
24898 /* Bounding box of the overall glyphs. */
24899 int leftmost, rightmost, lowest, highest;
24900 int lbearing, rbearing;
24901 int i, width, ascent, descent;
24902 int left_padded = 0, right_padded = 0;
24903 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24904 XChar2b char2b;
24905 struct font_metrics *pcm;
24906 int font_not_found_p;
24907 ptrdiff_t pos;
24909 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24910 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24911 break;
24912 if (glyph_len < cmp->glyph_len)
24913 right_padded = 1;
24914 for (i = 0; i < glyph_len; i++)
24916 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24917 break;
24918 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24920 if (i > 0)
24921 left_padded = 1;
24923 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24924 : IT_CHARPOS (*it));
24925 /* If no suitable font is found, use the default font. */
24926 font_not_found_p = font == NULL;
24927 if (font_not_found_p)
24929 face = face->ascii_face;
24930 font = face->font;
24932 boff = font->baseline_offset;
24933 if (font->vertical_centering)
24934 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24935 font_ascent = FONT_BASE (font) + boff;
24936 font_descent = FONT_DESCENT (font) - boff;
24937 font_height = FONT_HEIGHT (font);
24939 cmp->font = (void *) font;
24941 pcm = NULL;
24942 if (! font_not_found_p)
24944 get_char_face_and_encoding (it->f, c, it->face_id,
24945 &char2b, 0);
24946 pcm = get_per_char_metric (font, &char2b);
24949 /* Initialize the bounding box. */
24950 if (pcm)
24952 width = cmp->glyph_len > 0 ? pcm->width : 0;
24953 ascent = pcm->ascent;
24954 descent = pcm->descent;
24955 lbearing = pcm->lbearing;
24956 rbearing = pcm->rbearing;
24958 else
24960 width = cmp->glyph_len > 0 ? font->space_width : 0;
24961 ascent = FONT_BASE (font);
24962 descent = FONT_DESCENT (font);
24963 lbearing = 0;
24964 rbearing = width;
24967 rightmost = width;
24968 leftmost = 0;
24969 lowest = - descent + boff;
24970 highest = ascent + boff;
24972 if (! font_not_found_p
24973 && font->default_ascent
24974 && CHAR_TABLE_P (Vuse_default_ascent)
24975 && !NILP (Faref (Vuse_default_ascent,
24976 make_number (it->char_to_display))))
24977 highest = font->default_ascent + boff;
24979 /* Draw the first glyph at the normal position. It may be
24980 shifted to right later if some other glyphs are drawn
24981 at the left. */
24982 cmp->offsets[i * 2] = 0;
24983 cmp->offsets[i * 2 + 1] = boff;
24984 cmp->lbearing = lbearing;
24985 cmp->rbearing = rbearing;
24987 /* Set cmp->offsets for the remaining glyphs. */
24988 for (i++; i < glyph_len; i++)
24990 int left, right, btm, top;
24991 int ch = COMPOSITION_GLYPH (cmp, i);
24992 int face_id;
24993 struct face *this_face;
24995 if (ch == '\t')
24996 ch = ' ';
24997 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24998 this_face = FACE_FROM_ID (it->f, face_id);
24999 font = this_face->font;
25001 if (font == NULL)
25002 pcm = NULL;
25003 else
25005 get_char_face_and_encoding (it->f, ch, face_id,
25006 &char2b, 0);
25007 pcm = get_per_char_metric (font, &char2b);
25009 if (! pcm)
25010 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25011 else
25013 width = pcm->width;
25014 ascent = pcm->ascent;
25015 descent = pcm->descent;
25016 lbearing = pcm->lbearing;
25017 rbearing = pcm->rbearing;
25018 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25020 /* Relative composition with or without
25021 alternate chars. */
25022 left = (leftmost + rightmost - width) / 2;
25023 btm = - descent + boff;
25024 if (font->relative_compose
25025 && (! CHAR_TABLE_P (Vignore_relative_composition)
25026 || NILP (Faref (Vignore_relative_composition,
25027 make_number (ch)))))
25030 if (- descent >= font->relative_compose)
25031 /* One extra pixel between two glyphs. */
25032 btm = highest + 1;
25033 else if (ascent <= 0)
25034 /* One extra pixel between two glyphs. */
25035 btm = lowest - 1 - ascent - descent;
25038 else
25040 /* A composition rule is specified by an integer
25041 value that encodes global and new reference
25042 points (GREF and NREF). GREF and NREF are
25043 specified by numbers as below:
25045 0---1---2 -- ascent
25049 9--10--11 -- center
25051 ---3---4---5--- baseline
25053 6---7---8 -- descent
25055 int rule = COMPOSITION_RULE (cmp, i);
25056 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25058 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25059 grefx = gref % 3, nrefx = nref % 3;
25060 grefy = gref / 3, nrefy = nref / 3;
25061 if (xoff)
25062 xoff = font_height * (xoff - 128) / 256;
25063 if (yoff)
25064 yoff = font_height * (yoff - 128) / 256;
25066 left = (leftmost
25067 + grefx * (rightmost - leftmost) / 2
25068 - nrefx * width / 2
25069 + xoff);
25071 btm = ((grefy == 0 ? highest
25072 : grefy == 1 ? 0
25073 : grefy == 2 ? lowest
25074 : (highest + lowest) / 2)
25075 - (nrefy == 0 ? ascent + descent
25076 : nrefy == 1 ? descent - boff
25077 : nrefy == 2 ? 0
25078 : (ascent + descent) / 2)
25079 + yoff);
25082 cmp->offsets[i * 2] = left;
25083 cmp->offsets[i * 2 + 1] = btm + descent;
25085 /* Update the bounding box of the overall glyphs. */
25086 if (width > 0)
25088 right = left + width;
25089 if (left < leftmost)
25090 leftmost = left;
25091 if (right > rightmost)
25092 rightmost = right;
25094 top = btm + descent + ascent;
25095 if (top > highest)
25096 highest = top;
25097 if (btm < lowest)
25098 lowest = btm;
25100 if (cmp->lbearing > left + lbearing)
25101 cmp->lbearing = left + lbearing;
25102 if (cmp->rbearing < left + rbearing)
25103 cmp->rbearing = left + rbearing;
25107 /* If there are glyphs whose x-offsets are negative,
25108 shift all glyphs to the right and make all x-offsets
25109 non-negative. */
25110 if (leftmost < 0)
25112 for (i = 0; i < cmp->glyph_len; i++)
25113 cmp->offsets[i * 2] -= leftmost;
25114 rightmost -= leftmost;
25115 cmp->lbearing -= leftmost;
25116 cmp->rbearing -= leftmost;
25119 if (left_padded && cmp->lbearing < 0)
25121 for (i = 0; i < cmp->glyph_len; i++)
25122 cmp->offsets[i * 2] -= cmp->lbearing;
25123 rightmost -= cmp->lbearing;
25124 cmp->rbearing -= cmp->lbearing;
25125 cmp->lbearing = 0;
25127 if (right_padded && rightmost < cmp->rbearing)
25129 rightmost = cmp->rbearing;
25132 cmp->pixel_width = rightmost;
25133 cmp->ascent = highest;
25134 cmp->descent = - lowest;
25135 if (cmp->ascent < font_ascent)
25136 cmp->ascent = font_ascent;
25137 if (cmp->descent < font_descent)
25138 cmp->descent = font_descent;
25141 if (it->glyph_row
25142 && (cmp->lbearing < 0
25143 || cmp->rbearing > cmp->pixel_width))
25144 it->glyph_row->contains_overlapping_glyphs_p = 1;
25146 it->pixel_width = cmp->pixel_width;
25147 it->ascent = it->phys_ascent = cmp->ascent;
25148 it->descent = it->phys_descent = cmp->descent;
25149 if (face->box != FACE_NO_BOX)
25151 int thick = face->box_line_width;
25153 if (thick > 0)
25155 it->ascent += thick;
25156 it->descent += thick;
25158 else
25159 thick = - thick;
25161 if (it->start_of_box_run_p)
25162 it->pixel_width += thick;
25163 if (it->end_of_box_run_p)
25164 it->pixel_width += thick;
25167 /* If face has an overline, add the height of the overline
25168 (1 pixel) and a 1 pixel margin to the character height. */
25169 if (face->overline_p)
25170 it->ascent += overline_margin;
25172 take_vertical_position_into_account (it);
25173 if (it->ascent < 0)
25174 it->ascent = 0;
25175 if (it->descent < 0)
25176 it->descent = 0;
25178 if (it->glyph_row && cmp->glyph_len > 0)
25179 append_composite_glyph (it);
25181 else if (it->what == IT_COMPOSITION)
25183 /* A dynamic (automatic) composition. */
25184 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25185 Lisp_Object gstring;
25186 struct font_metrics metrics;
25188 it->nglyphs = 1;
25190 gstring = composition_gstring_from_id (it->cmp_it.id);
25191 it->pixel_width
25192 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25193 &metrics);
25194 if (it->glyph_row
25195 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25196 it->glyph_row->contains_overlapping_glyphs_p = 1;
25197 it->ascent = it->phys_ascent = metrics.ascent;
25198 it->descent = it->phys_descent = metrics.descent;
25199 if (face->box != FACE_NO_BOX)
25201 int thick = face->box_line_width;
25203 if (thick > 0)
25205 it->ascent += thick;
25206 it->descent += thick;
25208 else
25209 thick = - thick;
25211 if (it->start_of_box_run_p)
25212 it->pixel_width += thick;
25213 if (it->end_of_box_run_p)
25214 it->pixel_width += thick;
25216 /* If face has an overline, add the height of the overline
25217 (1 pixel) and a 1 pixel margin to the character height. */
25218 if (face->overline_p)
25219 it->ascent += overline_margin;
25220 take_vertical_position_into_account (it);
25221 if (it->ascent < 0)
25222 it->ascent = 0;
25223 if (it->descent < 0)
25224 it->descent = 0;
25226 if (it->glyph_row)
25227 append_composite_glyph (it);
25229 else if (it->what == IT_GLYPHLESS)
25230 produce_glyphless_glyph (it, 0, Qnil);
25231 else if (it->what == IT_IMAGE)
25232 produce_image_glyph (it);
25233 else if (it->what == IT_STRETCH)
25234 produce_stretch_glyph (it);
25236 done:
25237 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25238 because this isn't true for images with `:ascent 100'. */
25239 eassert (it->ascent >= 0 && it->descent >= 0);
25240 if (it->area == TEXT_AREA)
25241 it->current_x += it->pixel_width;
25243 if (extra_line_spacing > 0)
25245 it->descent += extra_line_spacing;
25246 if (extra_line_spacing > it->max_extra_line_spacing)
25247 it->max_extra_line_spacing = extra_line_spacing;
25250 it->max_ascent = max (it->max_ascent, it->ascent);
25251 it->max_descent = max (it->max_descent, it->descent);
25252 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25253 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25256 /* EXPORT for RIF:
25257 Output LEN glyphs starting at START at the nominal cursor position.
25258 Advance the nominal cursor over the text. The global variable
25259 updated_window contains the window being updated, updated_row is
25260 the glyph row being updated, and updated_area is the area of that
25261 row being updated. */
25263 void
25264 x_write_glyphs (struct glyph *start, int len)
25266 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25268 eassert (updated_window && updated_row);
25269 /* When the window is hscrolled, cursor hpos can legitimately be out
25270 of bounds, but we draw the cursor at the corresponding window
25271 margin in that case. */
25272 if (!updated_row->reversed_p && chpos < 0)
25273 chpos = 0;
25274 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25275 chpos = updated_row->used[TEXT_AREA] - 1;
25277 BLOCK_INPUT;
25279 /* Write glyphs. */
25281 hpos = start - updated_row->glyphs[updated_area];
25282 x = draw_glyphs (updated_window, output_cursor.x,
25283 updated_row, updated_area,
25284 hpos, hpos + len,
25285 DRAW_NORMAL_TEXT, 0);
25287 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25288 if (updated_area == TEXT_AREA
25289 && updated_window->phys_cursor_on_p
25290 && updated_window->phys_cursor.vpos == output_cursor.vpos
25291 && chpos >= hpos
25292 && chpos < hpos + len)
25293 updated_window->phys_cursor_on_p = 0;
25295 UNBLOCK_INPUT;
25297 /* Advance the output cursor. */
25298 output_cursor.hpos += len;
25299 output_cursor.x = x;
25303 /* EXPORT for RIF:
25304 Insert LEN glyphs from START at the nominal cursor position. */
25306 void
25307 x_insert_glyphs (struct glyph *start, int len)
25309 struct frame *f;
25310 struct window *w;
25311 int line_height, shift_by_width, shifted_region_width;
25312 struct glyph_row *row;
25313 struct glyph *glyph;
25314 int frame_x, frame_y;
25315 ptrdiff_t hpos;
25317 eassert (updated_window && updated_row);
25318 BLOCK_INPUT;
25319 w = updated_window;
25320 f = XFRAME (WINDOW_FRAME (w));
25322 /* Get the height of the line we are in. */
25323 row = updated_row;
25324 line_height = row->height;
25326 /* Get the width of the glyphs to insert. */
25327 shift_by_width = 0;
25328 for (glyph = start; glyph < start + len; ++glyph)
25329 shift_by_width += glyph->pixel_width;
25331 /* Get the width of the region to shift right. */
25332 shifted_region_width = (window_box_width (w, updated_area)
25333 - output_cursor.x
25334 - shift_by_width);
25336 /* Shift right. */
25337 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25338 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25340 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25341 line_height, shift_by_width);
25343 /* Write the glyphs. */
25344 hpos = start - row->glyphs[updated_area];
25345 draw_glyphs (w, output_cursor.x, row, updated_area,
25346 hpos, hpos + len,
25347 DRAW_NORMAL_TEXT, 0);
25349 /* Advance the output cursor. */
25350 output_cursor.hpos += len;
25351 output_cursor.x += shift_by_width;
25352 UNBLOCK_INPUT;
25356 /* EXPORT for RIF:
25357 Erase the current text line from the nominal cursor position
25358 (inclusive) to pixel column TO_X (exclusive). The idea is that
25359 everything from TO_X onward is already erased.
25361 TO_X is a pixel position relative to updated_area of
25362 updated_window. TO_X == -1 means clear to the end of this area. */
25364 void
25365 x_clear_end_of_line (int to_x)
25367 struct frame *f;
25368 struct window *w = updated_window;
25369 int max_x, min_y, max_y;
25370 int from_x, from_y, to_y;
25372 eassert (updated_window && updated_row);
25373 f = XFRAME (w->frame);
25375 if (updated_row->full_width_p)
25376 max_x = WINDOW_TOTAL_WIDTH (w);
25377 else
25378 max_x = window_box_width (w, updated_area);
25379 max_y = window_text_bottom_y (w);
25381 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25382 of window. For TO_X > 0, truncate to end of drawing area. */
25383 if (to_x == 0)
25384 return;
25385 else if (to_x < 0)
25386 to_x = max_x;
25387 else
25388 to_x = min (to_x, max_x);
25390 to_y = min (max_y, output_cursor.y + updated_row->height);
25392 /* Notice if the cursor will be cleared by this operation. */
25393 if (!updated_row->full_width_p)
25394 notice_overwritten_cursor (w, updated_area,
25395 output_cursor.x, -1,
25396 updated_row->y,
25397 MATRIX_ROW_BOTTOM_Y (updated_row));
25399 from_x = output_cursor.x;
25401 /* Translate to frame coordinates. */
25402 if (updated_row->full_width_p)
25404 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25405 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25407 else
25409 int area_left = window_box_left (w, updated_area);
25410 from_x += area_left;
25411 to_x += area_left;
25414 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25415 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25416 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25418 /* Prevent inadvertently clearing to end of the X window. */
25419 if (to_x > from_x && to_y > from_y)
25421 BLOCK_INPUT;
25422 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25423 to_x - from_x, to_y - from_y);
25424 UNBLOCK_INPUT;
25428 #endif /* HAVE_WINDOW_SYSTEM */
25432 /***********************************************************************
25433 Cursor types
25434 ***********************************************************************/
25436 /* Value is the internal representation of the specified cursor type
25437 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25438 of the bar cursor. */
25440 static enum text_cursor_kinds
25441 get_specified_cursor_type (Lisp_Object arg, int *width)
25443 enum text_cursor_kinds type;
25445 if (NILP (arg))
25446 return NO_CURSOR;
25448 if (EQ (arg, Qbox))
25449 return FILLED_BOX_CURSOR;
25451 if (EQ (arg, Qhollow))
25452 return HOLLOW_BOX_CURSOR;
25454 if (EQ (arg, Qbar))
25456 *width = 2;
25457 return BAR_CURSOR;
25460 if (CONSP (arg)
25461 && EQ (XCAR (arg), Qbar)
25462 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25464 *width = XINT (XCDR (arg));
25465 return BAR_CURSOR;
25468 if (EQ (arg, Qhbar))
25470 *width = 2;
25471 return HBAR_CURSOR;
25474 if (CONSP (arg)
25475 && EQ (XCAR (arg), Qhbar)
25476 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25478 *width = XINT (XCDR (arg));
25479 return HBAR_CURSOR;
25482 /* Treat anything unknown as "hollow box cursor".
25483 It was bad to signal an error; people have trouble fixing
25484 .Xdefaults with Emacs, when it has something bad in it. */
25485 type = HOLLOW_BOX_CURSOR;
25487 return type;
25490 /* Set the default cursor types for specified frame. */
25491 void
25492 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25494 int width = 1;
25495 Lisp_Object tem;
25497 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25498 FRAME_CURSOR_WIDTH (f) = width;
25500 /* By default, set up the blink-off state depending on the on-state. */
25502 tem = Fassoc (arg, Vblink_cursor_alist);
25503 if (!NILP (tem))
25505 FRAME_BLINK_OFF_CURSOR (f)
25506 = get_specified_cursor_type (XCDR (tem), &width);
25507 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25509 else
25510 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25514 #ifdef HAVE_WINDOW_SYSTEM
25516 /* Return the cursor we want to be displayed in window W. Return
25517 width of bar/hbar cursor through WIDTH arg. Return with
25518 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25519 (i.e. if the `system caret' should track this cursor).
25521 In a mini-buffer window, we want the cursor only to appear if we
25522 are reading input from this window. For the selected window, we
25523 want the cursor type given by the frame parameter or buffer local
25524 setting of cursor-type. If explicitly marked off, draw no cursor.
25525 In all other cases, we want a hollow box cursor. */
25527 static enum text_cursor_kinds
25528 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25529 int *active_cursor)
25531 struct frame *f = XFRAME (w->frame);
25532 struct buffer *b = XBUFFER (w->buffer);
25533 int cursor_type = DEFAULT_CURSOR;
25534 Lisp_Object alt_cursor;
25535 int non_selected = 0;
25537 *active_cursor = 1;
25539 /* Echo area */
25540 if (cursor_in_echo_area
25541 && FRAME_HAS_MINIBUF_P (f)
25542 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25544 if (w == XWINDOW (echo_area_window))
25546 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25548 *width = FRAME_CURSOR_WIDTH (f);
25549 return FRAME_DESIRED_CURSOR (f);
25551 else
25552 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25555 *active_cursor = 0;
25556 non_selected = 1;
25559 /* Detect a nonselected window or nonselected frame. */
25560 else if (w != XWINDOW (f->selected_window)
25561 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25563 *active_cursor = 0;
25565 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25566 return NO_CURSOR;
25568 non_selected = 1;
25571 /* Never display a cursor in a window in which cursor-type is nil. */
25572 if (NILP (BVAR (b, cursor_type)))
25573 return NO_CURSOR;
25575 /* Get the normal cursor type for this window. */
25576 if (EQ (BVAR (b, cursor_type), Qt))
25578 cursor_type = FRAME_DESIRED_CURSOR (f);
25579 *width = FRAME_CURSOR_WIDTH (f);
25581 else
25582 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25584 /* Use cursor-in-non-selected-windows instead
25585 for non-selected window or frame. */
25586 if (non_selected)
25588 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25589 if (!EQ (Qt, alt_cursor))
25590 return get_specified_cursor_type (alt_cursor, width);
25591 /* t means modify the normal cursor type. */
25592 if (cursor_type == FILLED_BOX_CURSOR)
25593 cursor_type = HOLLOW_BOX_CURSOR;
25594 else if (cursor_type == BAR_CURSOR && *width > 1)
25595 --*width;
25596 return cursor_type;
25599 /* Use normal cursor if not blinked off. */
25600 if (!w->cursor_off_p)
25602 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25604 if (cursor_type == FILLED_BOX_CURSOR)
25606 /* Using a block cursor on large images can be very annoying.
25607 So use a hollow cursor for "large" images.
25608 If image is not transparent (no mask), also use hollow cursor. */
25609 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25610 if (img != NULL && IMAGEP (img->spec))
25612 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25613 where N = size of default frame font size.
25614 This should cover most of the "tiny" icons people may use. */
25615 if (!img->mask
25616 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25617 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25618 cursor_type = HOLLOW_BOX_CURSOR;
25621 else if (cursor_type != NO_CURSOR)
25623 /* Display current only supports BOX and HOLLOW cursors for images.
25624 So for now, unconditionally use a HOLLOW cursor when cursor is
25625 not a solid box cursor. */
25626 cursor_type = HOLLOW_BOX_CURSOR;
25629 return cursor_type;
25632 /* Cursor is blinked off, so determine how to "toggle" it. */
25634 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25635 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25636 return get_specified_cursor_type (XCDR (alt_cursor), width);
25638 /* Then see if frame has specified a specific blink off cursor type. */
25639 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25641 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25642 return FRAME_BLINK_OFF_CURSOR (f);
25645 #if 0
25646 /* Some people liked having a permanently visible blinking cursor,
25647 while others had very strong opinions against it. So it was
25648 decided to remove it. KFS 2003-09-03 */
25650 /* Finally perform built-in cursor blinking:
25651 filled box <-> hollow box
25652 wide [h]bar <-> narrow [h]bar
25653 narrow [h]bar <-> no cursor
25654 other type <-> no cursor */
25656 if (cursor_type == FILLED_BOX_CURSOR)
25657 return HOLLOW_BOX_CURSOR;
25659 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25661 *width = 1;
25662 return cursor_type;
25664 #endif
25666 return NO_CURSOR;
25670 /* Notice when the text cursor of window W has been completely
25671 overwritten by a drawing operation that outputs glyphs in AREA
25672 starting at X0 and ending at X1 in the line starting at Y0 and
25673 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25674 the rest of the line after X0 has been written. Y coordinates
25675 are window-relative. */
25677 static void
25678 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25679 int x0, int x1, int y0, int y1)
25681 int cx0, cx1, cy0, cy1;
25682 struct glyph_row *row;
25684 if (!w->phys_cursor_on_p)
25685 return;
25686 if (area != TEXT_AREA)
25687 return;
25689 if (w->phys_cursor.vpos < 0
25690 || w->phys_cursor.vpos >= w->current_matrix->nrows
25691 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25692 !(row->enabled_p && row->displays_text_p)))
25693 return;
25695 if (row->cursor_in_fringe_p)
25697 row->cursor_in_fringe_p = 0;
25698 draw_fringe_bitmap (w, row, row->reversed_p);
25699 w->phys_cursor_on_p = 0;
25700 return;
25703 cx0 = w->phys_cursor.x;
25704 cx1 = cx0 + w->phys_cursor_width;
25705 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25706 return;
25708 /* The cursor image will be completely removed from the
25709 screen if the output area intersects the cursor area in
25710 y-direction. When we draw in [y0 y1[, and some part of
25711 the cursor is at y < y0, that part must have been drawn
25712 before. When scrolling, the cursor is erased before
25713 actually scrolling, so we don't come here. When not
25714 scrolling, the rows above the old cursor row must have
25715 changed, and in this case these rows must have written
25716 over the cursor image.
25718 Likewise if part of the cursor is below y1, with the
25719 exception of the cursor being in the first blank row at
25720 the buffer and window end because update_text_area
25721 doesn't draw that row. (Except when it does, but
25722 that's handled in update_text_area.) */
25724 cy0 = w->phys_cursor.y;
25725 cy1 = cy0 + w->phys_cursor_height;
25726 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25727 return;
25729 w->phys_cursor_on_p = 0;
25732 #endif /* HAVE_WINDOW_SYSTEM */
25735 /************************************************************************
25736 Mouse Face
25737 ************************************************************************/
25739 #ifdef HAVE_WINDOW_SYSTEM
25741 /* EXPORT for RIF:
25742 Fix the display of area AREA of overlapping row ROW in window W
25743 with respect to the overlapping part OVERLAPS. */
25745 void
25746 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25747 enum glyph_row_area area, int overlaps)
25749 int i, x;
25751 BLOCK_INPUT;
25753 x = 0;
25754 for (i = 0; i < row->used[area];)
25756 if (row->glyphs[area][i].overlaps_vertically_p)
25758 int start = i, start_x = x;
25762 x += row->glyphs[area][i].pixel_width;
25763 ++i;
25765 while (i < row->used[area]
25766 && row->glyphs[area][i].overlaps_vertically_p);
25768 draw_glyphs (w, start_x, row, area,
25769 start, i,
25770 DRAW_NORMAL_TEXT, overlaps);
25772 else
25774 x += row->glyphs[area][i].pixel_width;
25775 ++i;
25779 UNBLOCK_INPUT;
25783 /* EXPORT:
25784 Draw the cursor glyph of window W in glyph row ROW. See the
25785 comment of draw_glyphs for the meaning of HL. */
25787 void
25788 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25789 enum draw_glyphs_face hl)
25791 /* If cursor hpos is out of bounds, don't draw garbage. This can
25792 happen in mini-buffer windows when switching between echo area
25793 glyphs and mini-buffer. */
25794 if ((row->reversed_p
25795 ? (w->phys_cursor.hpos >= 0)
25796 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25798 int on_p = w->phys_cursor_on_p;
25799 int x1;
25800 int hpos = w->phys_cursor.hpos;
25802 /* When the window is hscrolled, cursor hpos can legitimately be
25803 out of bounds, but we draw the cursor at the corresponding
25804 window margin in that case. */
25805 if (!row->reversed_p && hpos < 0)
25806 hpos = 0;
25807 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25808 hpos = row->used[TEXT_AREA] - 1;
25810 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25811 hl, 0);
25812 w->phys_cursor_on_p = on_p;
25814 if (hl == DRAW_CURSOR)
25815 w->phys_cursor_width = x1 - w->phys_cursor.x;
25816 /* When we erase the cursor, and ROW is overlapped by other
25817 rows, make sure that these overlapping parts of other rows
25818 are redrawn. */
25819 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25821 w->phys_cursor_width = x1 - w->phys_cursor.x;
25823 if (row > w->current_matrix->rows
25824 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25825 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25826 OVERLAPS_ERASED_CURSOR);
25828 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25829 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25830 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25831 OVERLAPS_ERASED_CURSOR);
25837 /* EXPORT:
25838 Erase the image of a cursor of window W from the screen. */
25840 void
25841 erase_phys_cursor (struct window *w)
25843 struct frame *f = XFRAME (w->frame);
25844 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25845 int hpos = w->phys_cursor.hpos;
25846 int vpos = w->phys_cursor.vpos;
25847 int mouse_face_here_p = 0;
25848 struct glyph_matrix *active_glyphs = w->current_matrix;
25849 struct glyph_row *cursor_row;
25850 struct glyph *cursor_glyph;
25851 enum draw_glyphs_face hl;
25853 /* No cursor displayed or row invalidated => nothing to do on the
25854 screen. */
25855 if (w->phys_cursor_type == NO_CURSOR)
25856 goto mark_cursor_off;
25858 /* VPOS >= active_glyphs->nrows means that window has been resized.
25859 Don't bother to erase the cursor. */
25860 if (vpos >= active_glyphs->nrows)
25861 goto mark_cursor_off;
25863 /* If row containing cursor is marked invalid, there is nothing we
25864 can do. */
25865 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25866 if (!cursor_row->enabled_p)
25867 goto mark_cursor_off;
25869 /* If line spacing is > 0, old cursor may only be partially visible in
25870 window after split-window. So adjust visible height. */
25871 cursor_row->visible_height = min (cursor_row->visible_height,
25872 window_text_bottom_y (w) - cursor_row->y);
25874 /* If row is completely invisible, don't attempt to delete a cursor which
25875 isn't there. This can happen if cursor is at top of a window, and
25876 we switch to a buffer with a header line in that window. */
25877 if (cursor_row->visible_height <= 0)
25878 goto mark_cursor_off;
25880 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25881 if (cursor_row->cursor_in_fringe_p)
25883 cursor_row->cursor_in_fringe_p = 0;
25884 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25885 goto mark_cursor_off;
25888 /* This can happen when the new row is shorter than the old one.
25889 In this case, either draw_glyphs or clear_end_of_line
25890 should have cleared the cursor. Note that we wouldn't be
25891 able to erase the cursor in this case because we don't have a
25892 cursor glyph at hand. */
25893 if ((cursor_row->reversed_p
25894 ? (w->phys_cursor.hpos < 0)
25895 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25896 goto mark_cursor_off;
25898 /* When the window is hscrolled, cursor hpos can legitimately be out
25899 of bounds, but we draw the cursor at the corresponding window
25900 margin in that case. */
25901 if (!cursor_row->reversed_p && hpos < 0)
25902 hpos = 0;
25903 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25904 hpos = cursor_row->used[TEXT_AREA] - 1;
25906 /* If the cursor is in the mouse face area, redisplay that when
25907 we clear the cursor. */
25908 if (! NILP (hlinfo->mouse_face_window)
25909 && coords_in_mouse_face_p (w, hpos, vpos)
25910 /* Don't redraw the cursor's spot in mouse face if it is at the
25911 end of a line (on a newline). The cursor appears there, but
25912 mouse highlighting does not. */
25913 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25914 mouse_face_here_p = 1;
25916 /* Maybe clear the display under the cursor. */
25917 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25919 int x, y, left_x;
25920 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25921 int width;
25923 cursor_glyph = get_phys_cursor_glyph (w);
25924 if (cursor_glyph == NULL)
25925 goto mark_cursor_off;
25927 width = cursor_glyph->pixel_width;
25928 left_x = window_box_left_offset (w, TEXT_AREA);
25929 x = w->phys_cursor.x;
25930 if (x < left_x)
25931 width -= left_x - x;
25932 width = min (width, window_box_width (w, TEXT_AREA) - x);
25933 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25934 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25936 if (width > 0)
25937 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25940 /* Erase the cursor by redrawing the character underneath it. */
25941 if (mouse_face_here_p)
25942 hl = DRAW_MOUSE_FACE;
25943 else
25944 hl = DRAW_NORMAL_TEXT;
25945 draw_phys_cursor_glyph (w, cursor_row, hl);
25947 mark_cursor_off:
25948 w->phys_cursor_on_p = 0;
25949 w->phys_cursor_type = NO_CURSOR;
25953 /* EXPORT:
25954 Display or clear cursor of window W. If ON is zero, clear the
25955 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25956 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25958 void
25959 display_and_set_cursor (struct window *w, int on,
25960 int hpos, int vpos, int x, int y)
25962 struct frame *f = XFRAME (w->frame);
25963 int new_cursor_type;
25964 int new_cursor_width;
25965 int active_cursor;
25966 struct glyph_row *glyph_row;
25967 struct glyph *glyph;
25969 /* This is pointless on invisible frames, and dangerous on garbaged
25970 windows and frames; in the latter case, the frame or window may
25971 be in the midst of changing its size, and x and y may be off the
25972 window. */
25973 if (! FRAME_VISIBLE_P (f)
25974 || FRAME_GARBAGED_P (f)
25975 || vpos >= w->current_matrix->nrows
25976 || hpos >= w->current_matrix->matrix_w)
25977 return;
25979 /* If cursor is off and we want it off, return quickly. */
25980 if (!on && !w->phys_cursor_on_p)
25981 return;
25983 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25984 /* If cursor row is not enabled, we don't really know where to
25985 display the cursor. */
25986 if (!glyph_row->enabled_p)
25988 w->phys_cursor_on_p = 0;
25989 return;
25992 glyph = NULL;
25993 if (!glyph_row->exact_window_width_line_p
25994 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25995 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25997 eassert (interrupt_input_blocked);
25999 /* Set new_cursor_type to the cursor we want to be displayed. */
26000 new_cursor_type = get_window_cursor_type (w, glyph,
26001 &new_cursor_width, &active_cursor);
26003 /* If cursor is currently being shown and we don't want it to be or
26004 it is in the wrong place, or the cursor type is not what we want,
26005 erase it. */
26006 if (w->phys_cursor_on_p
26007 && (!on
26008 || w->phys_cursor.x != x
26009 || w->phys_cursor.y != y
26010 || new_cursor_type != w->phys_cursor_type
26011 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26012 && new_cursor_width != w->phys_cursor_width)))
26013 erase_phys_cursor (w);
26015 /* Don't check phys_cursor_on_p here because that flag is only set
26016 to zero in some cases where we know that the cursor has been
26017 completely erased, to avoid the extra work of erasing the cursor
26018 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26019 still not be visible, or it has only been partly erased. */
26020 if (on)
26022 w->phys_cursor_ascent = glyph_row->ascent;
26023 w->phys_cursor_height = glyph_row->height;
26025 /* Set phys_cursor_.* before x_draw_.* is called because some
26026 of them may need the information. */
26027 w->phys_cursor.x = x;
26028 w->phys_cursor.y = glyph_row->y;
26029 w->phys_cursor.hpos = hpos;
26030 w->phys_cursor.vpos = vpos;
26033 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26034 new_cursor_type, new_cursor_width,
26035 on, active_cursor);
26039 /* Switch the display of W's cursor on or off, according to the value
26040 of ON. */
26042 static void
26043 update_window_cursor (struct window *w, int on)
26045 /* Don't update cursor in windows whose frame is in the process
26046 of being deleted. */
26047 if (w->current_matrix)
26049 int hpos = w->phys_cursor.hpos;
26050 int vpos = w->phys_cursor.vpos;
26051 struct glyph_row *row;
26053 if (vpos >= w->current_matrix->nrows
26054 || hpos >= w->current_matrix->matrix_w)
26055 return;
26057 row = MATRIX_ROW (w->current_matrix, vpos);
26059 /* When the window is hscrolled, cursor hpos can legitimately be
26060 out of bounds, but we draw the cursor at the corresponding
26061 window margin in that case. */
26062 if (!row->reversed_p && hpos < 0)
26063 hpos = 0;
26064 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26065 hpos = row->used[TEXT_AREA] - 1;
26067 BLOCK_INPUT;
26068 display_and_set_cursor (w, on, hpos, vpos,
26069 w->phys_cursor.x, w->phys_cursor.y);
26070 UNBLOCK_INPUT;
26075 /* Call update_window_cursor with parameter ON_P on all leaf windows
26076 in the window tree rooted at W. */
26078 static void
26079 update_cursor_in_window_tree (struct window *w, int on_p)
26081 while (w)
26083 if (!NILP (w->hchild))
26084 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26085 else if (!NILP (w->vchild))
26086 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26087 else
26088 update_window_cursor (w, on_p);
26090 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26095 /* EXPORT:
26096 Display the cursor on window W, or clear it, according to ON_P.
26097 Don't change the cursor's position. */
26099 void
26100 x_update_cursor (struct frame *f, int on_p)
26102 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26106 /* EXPORT:
26107 Clear the cursor of window W to background color, and mark the
26108 cursor as not shown. This is used when the text where the cursor
26109 is about to be rewritten. */
26111 void
26112 x_clear_cursor (struct window *w)
26114 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26115 update_window_cursor (w, 0);
26118 #endif /* HAVE_WINDOW_SYSTEM */
26120 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26121 and MSDOS. */
26122 static void
26123 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26124 int start_hpos, int end_hpos,
26125 enum draw_glyphs_face draw)
26127 #ifdef HAVE_WINDOW_SYSTEM
26128 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26130 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26131 return;
26133 #endif
26134 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26135 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26136 #endif
26139 /* Display the active region described by mouse_face_* according to DRAW. */
26141 static void
26142 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26144 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26145 struct frame *f = XFRAME (WINDOW_FRAME (w));
26147 if (/* If window is in the process of being destroyed, don't bother
26148 to do anything. */
26149 w->current_matrix != NULL
26150 /* Don't update mouse highlight if hidden */
26151 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26152 /* Recognize when we are called to operate on rows that don't exist
26153 anymore. This can happen when a window is split. */
26154 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26156 int phys_cursor_on_p = w->phys_cursor_on_p;
26157 struct glyph_row *row, *first, *last;
26159 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26160 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26162 for (row = first; row <= last && row->enabled_p; ++row)
26164 int start_hpos, end_hpos, start_x;
26166 /* For all but the first row, the highlight starts at column 0. */
26167 if (row == first)
26169 /* R2L rows have BEG and END in reversed order, but the
26170 screen drawing geometry is always left to right. So
26171 we need to mirror the beginning and end of the
26172 highlighted area in R2L rows. */
26173 if (!row->reversed_p)
26175 start_hpos = hlinfo->mouse_face_beg_col;
26176 start_x = hlinfo->mouse_face_beg_x;
26178 else if (row == last)
26180 start_hpos = hlinfo->mouse_face_end_col;
26181 start_x = hlinfo->mouse_face_end_x;
26183 else
26185 start_hpos = 0;
26186 start_x = 0;
26189 else if (row->reversed_p && row == last)
26191 start_hpos = hlinfo->mouse_face_end_col;
26192 start_x = hlinfo->mouse_face_end_x;
26194 else
26196 start_hpos = 0;
26197 start_x = 0;
26200 if (row == last)
26202 if (!row->reversed_p)
26203 end_hpos = hlinfo->mouse_face_end_col;
26204 else if (row == first)
26205 end_hpos = hlinfo->mouse_face_beg_col;
26206 else
26208 end_hpos = row->used[TEXT_AREA];
26209 if (draw == DRAW_NORMAL_TEXT)
26210 row->fill_line_p = 1; /* Clear to end of line */
26213 else if (row->reversed_p && row == first)
26214 end_hpos = hlinfo->mouse_face_beg_col;
26215 else
26217 end_hpos = row->used[TEXT_AREA];
26218 if (draw == DRAW_NORMAL_TEXT)
26219 row->fill_line_p = 1; /* Clear to end of line */
26222 if (end_hpos > start_hpos)
26224 draw_row_with_mouse_face (w, start_x, row,
26225 start_hpos, end_hpos, draw);
26227 row->mouse_face_p
26228 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26232 #ifdef HAVE_WINDOW_SYSTEM
26233 /* When we've written over the cursor, arrange for it to
26234 be displayed again. */
26235 if (FRAME_WINDOW_P (f)
26236 && phys_cursor_on_p && !w->phys_cursor_on_p)
26238 int hpos = w->phys_cursor.hpos;
26240 /* When the window is hscrolled, cursor hpos can legitimately be
26241 out of bounds, but we draw the cursor at the corresponding
26242 window margin in that case. */
26243 if (!row->reversed_p && hpos < 0)
26244 hpos = 0;
26245 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26246 hpos = row->used[TEXT_AREA] - 1;
26248 BLOCK_INPUT;
26249 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26250 w->phys_cursor.x, w->phys_cursor.y);
26251 UNBLOCK_INPUT;
26253 #endif /* HAVE_WINDOW_SYSTEM */
26256 #ifdef HAVE_WINDOW_SYSTEM
26257 /* Change the mouse cursor. */
26258 if (FRAME_WINDOW_P (f))
26260 if (draw == DRAW_NORMAL_TEXT
26261 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26262 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26263 else if (draw == DRAW_MOUSE_FACE)
26264 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26265 else
26266 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26268 #endif /* HAVE_WINDOW_SYSTEM */
26271 /* EXPORT:
26272 Clear out the mouse-highlighted active region.
26273 Redraw it un-highlighted first. Value is non-zero if mouse
26274 face was actually drawn unhighlighted. */
26277 clear_mouse_face (Mouse_HLInfo *hlinfo)
26279 int cleared = 0;
26281 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26283 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26284 cleared = 1;
26287 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26288 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26289 hlinfo->mouse_face_window = Qnil;
26290 hlinfo->mouse_face_overlay = Qnil;
26291 return cleared;
26294 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26295 within the mouse face on that window. */
26296 static int
26297 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26299 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26301 /* Quickly resolve the easy cases. */
26302 if (!(WINDOWP (hlinfo->mouse_face_window)
26303 && XWINDOW (hlinfo->mouse_face_window) == w))
26304 return 0;
26305 if (vpos < hlinfo->mouse_face_beg_row
26306 || vpos > hlinfo->mouse_face_end_row)
26307 return 0;
26308 if (vpos > hlinfo->mouse_face_beg_row
26309 && vpos < hlinfo->mouse_face_end_row)
26310 return 1;
26312 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26314 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26316 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26317 return 1;
26319 else if ((vpos == hlinfo->mouse_face_beg_row
26320 && hpos >= hlinfo->mouse_face_beg_col)
26321 || (vpos == hlinfo->mouse_face_end_row
26322 && hpos < hlinfo->mouse_face_end_col))
26323 return 1;
26325 else
26327 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26329 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26330 return 1;
26332 else if ((vpos == hlinfo->mouse_face_beg_row
26333 && hpos <= hlinfo->mouse_face_beg_col)
26334 || (vpos == hlinfo->mouse_face_end_row
26335 && hpos > hlinfo->mouse_face_end_col))
26336 return 1;
26338 return 0;
26342 /* EXPORT:
26343 Non-zero if physical cursor of window W is within mouse face. */
26346 cursor_in_mouse_face_p (struct window *w)
26348 int hpos = w->phys_cursor.hpos;
26349 int vpos = w->phys_cursor.vpos;
26350 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26352 /* When the window is hscrolled, cursor hpos can legitimately be out
26353 of bounds, but we draw the cursor at the corresponding window
26354 margin in that case. */
26355 if (!row->reversed_p && hpos < 0)
26356 hpos = 0;
26357 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26358 hpos = row->used[TEXT_AREA] - 1;
26360 return coords_in_mouse_face_p (w, hpos, vpos);
26365 /* Find the glyph rows START_ROW and END_ROW of window W that display
26366 characters between buffer positions START_CHARPOS and END_CHARPOS
26367 (excluding END_CHARPOS). DISP_STRING is a display string that
26368 covers these buffer positions. This is similar to
26369 row_containing_pos, but is more accurate when bidi reordering makes
26370 buffer positions change non-linearly with glyph rows. */
26371 static void
26372 rows_from_pos_range (struct window *w,
26373 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26374 Lisp_Object disp_string,
26375 struct glyph_row **start, struct glyph_row **end)
26377 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26378 int last_y = window_text_bottom_y (w);
26379 struct glyph_row *row;
26381 *start = NULL;
26382 *end = NULL;
26384 while (!first->enabled_p
26385 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26386 first++;
26388 /* Find the START row. */
26389 for (row = first;
26390 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26391 row++)
26393 /* A row can potentially be the START row if the range of the
26394 characters it displays intersects the range
26395 [START_CHARPOS..END_CHARPOS). */
26396 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26397 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26398 /* See the commentary in row_containing_pos, for the
26399 explanation of the complicated way to check whether
26400 some position is beyond the end of the characters
26401 displayed by a row. */
26402 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26403 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26404 && !row->ends_at_zv_p
26405 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26406 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26407 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26408 && !row->ends_at_zv_p
26409 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26411 /* Found a candidate row. Now make sure at least one of the
26412 glyphs it displays has a charpos from the range
26413 [START_CHARPOS..END_CHARPOS).
26415 This is not obvious because bidi reordering could make
26416 buffer positions of a row be 1,2,3,102,101,100, and if we
26417 want to highlight characters in [50..60), we don't want
26418 this row, even though [50..60) does intersect [1..103),
26419 the range of character positions given by the row's start
26420 and end positions. */
26421 struct glyph *g = row->glyphs[TEXT_AREA];
26422 struct glyph *e = g + row->used[TEXT_AREA];
26424 while (g < e)
26426 if (((BUFFERP (g->object) || INTEGERP (g->object))
26427 && start_charpos <= g->charpos && g->charpos < end_charpos)
26428 /* A glyph that comes from DISP_STRING is by
26429 definition to be highlighted. */
26430 || EQ (g->object, disp_string))
26431 *start = row;
26432 g++;
26434 if (*start)
26435 break;
26439 /* Find the END row. */
26440 if (!*start
26441 /* If the last row is partially visible, start looking for END
26442 from that row, instead of starting from FIRST. */
26443 && !(row->enabled_p
26444 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26445 row = first;
26446 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26448 struct glyph_row *next = row + 1;
26449 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26451 if (!next->enabled_p
26452 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26453 /* The first row >= START whose range of displayed characters
26454 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26455 is the row END + 1. */
26456 || (start_charpos < next_start
26457 && end_charpos < next_start)
26458 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26459 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26460 && !next->ends_at_zv_p
26461 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26462 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26463 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26464 && !next->ends_at_zv_p
26465 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26467 *end = row;
26468 break;
26470 else
26472 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26473 but none of the characters it displays are in the range, it is
26474 also END + 1. */
26475 struct glyph *g = next->glyphs[TEXT_AREA];
26476 struct glyph *s = g;
26477 struct glyph *e = g + next->used[TEXT_AREA];
26479 while (g < e)
26481 if (((BUFFERP (g->object) || INTEGERP (g->object))
26482 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26483 /* If the buffer position of the first glyph in
26484 the row is equal to END_CHARPOS, it means
26485 the last character to be highlighted is the
26486 newline of ROW, and we must consider NEXT as
26487 END, not END+1. */
26488 || (((!next->reversed_p && g == s)
26489 || (next->reversed_p && g == e - 1))
26490 && (g->charpos == end_charpos
26491 /* Special case for when NEXT is an
26492 empty line at ZV. */
26493 || (g->charpos == -1
26494 && !row->ends_at_zv_p
26495 && next_start == end_charpos)))))
26496 /* A glyph that comes from DISP_STRING is by
26497 definition to be highlighted. */
26498 || EQ (g->object, disp_string))
26499 break;
26500 g++;
26502 if (g == e)
26504 *end = row;
26505 break;
26507 /* The first row that ends at ZV must be the last to be
26508 highlighted. */
26509 else if (next->ends_at_zv_p)
26511 *end = next;
26512 break;
26518 /* This function sets the mouse_face_* elements of HLINFO, assuming
26519 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26520 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26521 for the overlay or run of text properties specifying the mouse
26522 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26523 before-string and after-string that must also be highlighted.
26524 DISP_STRING, if non-nil, is a display string that may cover some
26525 or all of the highlighted text. */
26527 static void
26528 mouse_face_from_buffer_pos (Lisp_Object window,
26529 Mouse_HLInfo *hlinfo,
26530 ptrdiff_t mouse_charpos,
26531 ptrdiff_t start_charpos,
26532 ptrdiff_t end_charpos,
26533 Lisp_Object before_string,
26534 Lisp_Object after_string,
26535 Lisp_Object disp_string)
26537 struct window *w = XWINDOW (window);
26538 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26539 struct glyph_row *r1, *r2;
26540 struct glyph *glyph, *end;
26541 ptrdiff_t ignore, pos;
26542 int x;
26544 eassert (NILP (disp_string) || STRINGP (disp_string));
26545 eassert (NILP (before_string) || STRINGP (before_string));
26546 eassert (NILP (after_string) || STRINGP (after_string));
26548 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26549 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26550 if (r1 == NULL)
26551 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26552 /* If the before-string or display-string contains newlines,
26553 rows_from_pos_range skips to its last row. Move back. */
26554 if (!NILP (before_string) || !NILP (disp_string))
26556 struct glyph_row *prev;
26557 while ((prev = r1 - 1, prev >= first)
26558 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26559 && prev->used[TEXT_AREA] > 0)
26561 struct glyph *beg = prev->glyphs[TEXT_AREA];
26562 glyph = beg + prev->used[TEXT_AREA];
26563 while (--glyph >= beg && INTEGERP (glyph->object));
26564 if (glyph < beg
26565 || !(EQ (glyph->object, before_string)
26566 || EQ (glyph->object, disp_string)))
26567 break;
26568 r1 = prev;
26571 if (r2 == NULL)
26573 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26574 hlinfo->mouse_face_past_end = 1;
26576 else if (!NILP (after_string))
26578 /* If the after-string has newlines, advance to its last row. */
26579 struct glyph_row *next;
26580 struct glyph_row *last
26581 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26583 for (next = r2 + 1;
26584 next <= last
26585 && next->used[TEXT_AREA] > 0
26586 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26587 ++next)
26588 r2 = next;
26590 /* The rest of the display engine assumes that mouse_face_beg_row is
26591 either above mouse_face_end_row or identical to it. But with
26592 bidi-reordered continued lines, the row for START_CHARPOS could
26593 be below the row for END_CHARPOS. If so, swap the rows and store
26594 them in correct order. */
26595 if (r1->y > r2->y)
26597 struct glyph_row *tem = r2;
26599 r2 = r1;
26600 r1 = tem;
26603 hlinfo->mouse_face_beg_y = r1->y;
26604 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26605 hlinfo->mouse_face_end_y = r2->y;
26606 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26608 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26609 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26610 could be anywhere in the row and in any order. The strategy
26611 below is to find the leftmost and the rightmost glyph that
26612 belongs to either of these 3 strings, or whose position is
26613 between START_CHARPOS and END_CHARPOS, and highlight all the
26614 glyphs between those two. This may cover more than just the text
26615 between START_CHARPOS and END_CHARPOS if the range of characters
26616 strides the bidi level boundary, e.g. if the beginning is in R2L
26617 text while the end is in L2R text or vice versa. */
26618 if (!r1->reversed_p)
26620 /* This row is in a left to right paragraph. Scan it left to
26621 right. */
26622 glyph = r1->glyphs[TEXT_AREA];
26623 end = glyph + r1->used[TEXT_AREA];
26624 x = r1->x;
26626 /* Skip truncation glyphs at the start of the glyph row. */
26627 if (r1->displays_text_p)
26628 for (; glyph < end
26629 && INTEGERP (glyph->object)
26630 && glyph->charpos < 0;
26631 ++glyph)
26632 x += glyph->pixel_width;
26634 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26635 or DISP_STRING, and the first glyph from buffer whose
26636 position is between START_CHARPOS and END_CHARPOS. */
26637 for (; glyph < end
26638 && !INTEGERP (glyph->object)
26639 && !EQ (glyph->object, disp_string)
26640 && !(BUFFERP (glyph->object)
26641 && (glyph->charpos >= start_charpos
26642 && glyph->charpos < end_charpos));
26643 ++glyph)
26645 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26646 are present at buffer positions between START_CHARPOS and
26647 END_CHARPOS, or if they come from an overlay. */
26648 if (EQ (glyph->object, before_string))
26650 pos = string_buffer_position (before_string,
26651 start_charpos);
26652 /* If pos == 0, it means before_string came from an
26653 overlay, not from a buffer position. */
26654 if (!pos || (pos >= start_charpos && pos < end_charpos))
26655 break;
26657 else if (EQ (glyph->object, after_string))
26659 pos = string_buffer_position (after_string, end_charpos);
26660 if (!pos || (pos >= start_charpos && pos < end_charpos))
26661 break;
26663 x += glyph->pixel_width;
26665 hlinfo->mouse_face_beg_x = x;
26666 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26668 else
26670 /* This row is in a right to left paragraph. Scan it right to
26671 left. */
26672 struct glyph *g;
26674 end = r1->glyphs[TEXT_AREA] - 1;
26675 glyph = end + r1->used[TEXT_AREA];
26677 /* Skip truncation glyphs at the start of the glyph row. */
26678 if (r1->displays_text_p)
26679 for (; glyph > end
26680 && INTEGERP (glyph->object)
26681 && glyph->charpos < 0;
26682 --glyph)
26685 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26686 or DISP_STRING, and the first glyph from buffer whose
26687 position is between START_CHARPOS and END_CHARPOS. */
26688 for (; glyph > end
26689 && !INTEGERP (glyph->object)
26690 && !EQ (glyph->object, disp_string)
26691 && !(BUFFERP (glyph->object)
26692 && (glyph->charpos >= start_charpos
26693 && glyph->charpos < end_charpos));
26694 --glyph)
26696 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26697 are present at buffer positions between START_CHARPOS and
26698 END_CHARPOS, or if they come from an overlay. */
26699 if (EQ (glyph->object, before_string))
26701 pos = string_buffer_position (before_string, start_charpos);
26702 /* If pos == 0, it means before_string came from an
26703 overlay, not from a buffer position. */
26704 if (!pos || (pos >= start_charpos && pos < end_charpos))
26705 break;
26707 else if (EQ (glyph->object, after_string))
26709 pos = string_buffer_position (after_string, end_charpos);
26710 if (!pos || (pos >= start_charpos && pos < end_charpos))
26711 break;
26715 glyph++; /* first glyph to the right of the highlighted area */
26716 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26717 x += g->pixel_width;
26718 hlinfo->mouse_face_beg_x = x;
26719 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26722 /* If the highlight ends in a different row, compute GLYPH and END
26723 for the end row. Otherwise, reuse the values computed above for
26724 the row where the highlight begins. */
26725 if (r2 != r1)
26727 if (!r2->reversed_p)
26729 glyph = r2->glyphs[TEXT_AREA];
26730 end = glyph + r2->used[TEXT_AREA];
26731 x = r2->x;
26733 else
26735 end = r2->glyphs[TEXT_AREA] - 1;
26736 glyph = end + r2->used[TEXT_AREA];
26740 if (!r2->reversed_p)
26742 /* Skip truncation and continuation glyphs near the end of the
26743 row, and also blanks and stretch glyphs inserted by
26744 extend_face_to_end_of_line. */
26745 while (end > glyph
26746 && INTEGERP ((end - 1)->object))
26747 --end;
26748 /* Scan the rest of the glyph row from the end, looking for the
26749 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26750 DISP_STRING, or whose position is between START_CHARPOS
26751 and END_CHARPOS */
26752 for (--end;
26753 end > glyph
26754 && !INTEGERP (end->object)
26755 && !EQ (end->object, disp_string)
26756 && !(BUFFERP (end->object)
26757 && (end->charpos >= start_charpos
26758 && end->charpos < end_charpos));
26759 --end)
26761 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26762 are present at buffer positions between START_CHARPOS and
26763 END_CHARPOS, or if they come from an overlay. */
26764 if (EQ (end->object, before_string))
26766 pos = string_buffer_position (before_string, start_charpos);
26767 if (!pos || (pos >= start_charpos && pos < end_charpos))
26768 break;
26770 else if (EQ (end->object, after_string))
26772 pos = string_buffer_position (after_string, end_charpos);
26773 if (!pos || (pos >= start_charpos && pos < end_charpos))
26774 break;
26777 /* Find the X coordinate of the last glyph to be highlighted. */
26778 for (; glyph <= end; ++glyph)
26779 x += glyph->pixel_width;
26781 hlinfo->mouse_face_end_x = x;
26782 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26784 else
26786 /* Skip truncation and continuation glyphs near the end of the
26787 row, and also blanks and stretch glyphs inserted by
26788 extend_face_to_end_of_line. */
26789 x = r2->x;
26790 end++;
26791 while (end < glyph
26792 && INTEGERP (end->object))
26794 x += end->pixel_width;
26795 ++end;
26797 /* Scan the rest of the glyph row from the end, looking for the
26798 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26799 DISP_STRING, or whose position is between START_CHARPOS
26800 and END_CHARPOS */
26801 for ( ;
26802 end < glyph
26803 && !INTEGERP (end->object)
26804 && !EQ (end->object, disp_string)
26805 && !(BUFFERP (end->object)
26806 && (end->charpos >= start_charpos
26807 && end->charpos < end_charpos));
26808 ++end)
26810 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26811 are present at buffer positions between START_CHARPOS and
26812 END_CHARPOS, or if they come from an overlay. */
26813 if (EQ (end->object, before_string))
26815 pos = string_buffer_position (before_string, start_charpos);
26816 if (!pos || (pos >= start_charpos && pos < end_charpos))
26817 break;
26819 else if (EQ (end->object, after_string))
26821 pos = string_buffer_position (after_string, end_charpos);
26822 if (!pos || (pos >= start_charpos && pos < end_charpos))
26823 break;
26825 x += end->pixel_width;
26827 /* If we exited the above loop because we arrived at the last
26828 glyph of the row, and its buffer position is still not in
26829 range, it means the last character in range is the preceding
26830 newline. Bump the end column and x values to get past the
26831 last glyph. */
26832 if (end == glyph
26833 && BUFFERP (end->object)
26834 && (end->charpos < start_charpos
26835 || end->charpos >= end_charpos))
26837 x += end->pixel_width;
26838 ++end;
26840 hlinfo->mouse_face_end_x = x;
26841 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26844 hlinfo->mouse_face_window = window;
26845 hlinfo->mouse_face_face_id
26846 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26847 mouse_charpos + 1,
26848 !hlinfo->mouse_face_hidden, -1);
26849 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26852 /* The following function is not used anymore (replaced with
26853 mouse_face_from_string_pos), but I leave it here for the time
26854 being, in case someone would. */
26856 #if 0 /* not used */
26858 /* Find the position of the glyph for position POS in OBJECT in
26859 window W's current matrix, and return in *X, *Y the pixel
26860 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26862 RIGHT_P non-zero means return the position of the right edge of the
26863 glyph, RIGHT_P zero means return the left edge position.
26865 If no glyph for POS exists in the matrix, return the position of
26866 the glyph with the next smaller position that is in the matrix, if
26867 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26868 exists in the matrix, return the position of the glyph with the
26869 next larger position in OBJECT.
26871 Value is non-zero if a glyph was found. */
26873 static int
26874 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26875 int *hpos, int *vpos, int *x, int *y, int right_p)
26877 int yb = window_text_bottom_y (w);
26878 struct glyph_row *r;
26879 struct glyph *best_glyph = NULL;
26880 struct glyph_row *best_row = NULL;
26881 int best_x = 0;
26883 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26884 r->enabled_p && r->y < yb;
26885 ++r)
26887 struct glyph *g = r->glyphs[TEXT_AREA];
26888 struct glyph *e = g + r->used[TEXT_AREA];
26889 int gx;
26891 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26892 if (EQ (g->object, object))
26894 if (g->charpos == pos)
26896 best_glyph = g;
26897 best_x = gx;
26898 best_row = r;
26899 goto found;
26901 else if (best_glyph == NULL
26902 || ((eabs (g->charpos - pos)
26903 < eabs (best_glyph->charpos - pos))
26904 && (right_p
26905 ? g->charpos < pos
26906 : g->charpos > pos)))
26908 best_glyph = g;
26909 best_x = gx;
26910 best_row = r;
26915 found:
26917 if (best_glyph)
26919 *x = best_x;
26920 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26922 if (right_p)
26924 *x += best_glyph->pixel_width;
26925 ++*hpos;
26928 *y = best_row->y;
26929 *vpos = best_row - w->current_matrix->rows;
26932 return best_glyph != NULL;
26934 #endif /* not used */
26936 /* Find the positions of the first and the last glyphs in window W's
26937 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26938 (assumed to be a string), and return in HLINFO's mouse_face_*
26939 members the pixel and column/row coordinates of those glyphs. */
26941 static void
26942 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26943 Lisp_Object object,
26944 ptrdiff_t startpos, ptrdiff_t endpos)
26946 int yb = window_text_bottom_y (w);
26947 struct glyph_row *r;
26948 struct glyph *g, *e;
26949 int gx;
26950 int found = 0;
26952 /* Find the glyph row with at least one position in the range
26953 [STARTPOS..ENDPOS], and the first glyph in that row whose
26954 position belongs to that range. */
26955 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26956 r->enabled_p && r->y < yb;
26957 ++r)
26959 if (!r->reversed_p)
26961 g = r->glyphs[TEXT_AREA];
26962 e = g + r->used[TEXT_AREA];
26963 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26964 if (EQ (g->object, object)
26965 && startpos <= g->charpos && g->charpos <= endpos)
26967 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26968 hlinfo->mouse_face_beg_y = r->y;
26969 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26970 hlinfo->mouse_face_beg_x = gx;
26971 found = 1;
26972 break;
26975 else
26977 struct glyph *g1;
26979 e = r->glyphs[TEXT_AREA];
26980 g = e + r->used[TEXT_AREA];
26981 for ( ; g > e; --g)
26982 if (EQ ((g-1)->object, object)
26983 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26985 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26986 hlinfo->mouse_face_beg_y = r->y;
26987 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26988 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26989 gx += g1->pixel_width;
26990 hlinfo->mouse_face_beg_x = gx;
26991 found = 1;
26992 break;
26995 if (found)
26996 break;
26999 if (!found)
27000 return;
27002 /* Starting with the next row, look for the first row which does NOT
27003 include any glyphs whose positions are in the range. */
27004 for (++r; r->enabled_p && r->y < yb; ++r)
27006 g = r->glyphs[TEXT_AREA];
27007 e = g + r->used[TEXT_AREA];
27008 found = 0;
27009 for ( ; g < e; ++g)
27010 if (EQ (g->object, object)
27011 && startpos <= g->charpos && g->charpos <= endpos)
27013 found = 1;
27014 break;
27016 if (!found)
27017 break;
27020 /* The highlighted region ends on the previous row. */
27021 r--;
27023 /* Set the end row and its vertical pixel coordinate. */
27024 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27025 hlinfo->mouse_face_end_y = r->y;
27027 /* Compute and set the end column and the end column's horizontal
27028 pixel coordinate. */
27029 if (!r->reversed_p)
27031 g = r->glyphs[TEXT_AREA];
27032 e = g + r->used[TEXT_AREA];
27033 for ( ; e > g; --e)
27034 if (EQ ((e-1)->object, object)
27035 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27036 break;
27037 hlinfo->mouse_face_end_col = e - g;
27039 for (gx = r->x; g < e; ++g)
27040 gx += g->pixel_width;
27041 hlinfo->mouse_face_end_x = gx;
27043 else
27045 e = r->glyphs[TEXT_AREA];
27046 g = e + r->used[TEXT_AREA];
27047 for (gx = r->x ; e < g; ++e)
27049 if (EQ (e->object, object)
27050 && startpos <= e->charpos && e->charpos <= endpos)
27051 break;
27052 gx += e->pixel_width;
27054 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27055 hlinfo->mouse_face_end_x = gx;
27059 #ifdef HAVE_WINDOW_SYSTEM
27061 /* See if position X, Y is within a hot-spot of an image. */
27063 static int
27064 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27066 if (!CONSP (hot_spot))
27067 return 0;
27069 if (EQ (XCAR (hot_spot), Qrect))
27071 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27072 Lisp_Object rect = XCDR (hot_spot);
27073 Lisp_Object tem;
27074 if (!CONSP (rect))
27075 return 0;
27076 if (!CONSP (XCAR (rect)))
27077 return 0;
27078 if (!CONSP (XCDR (rect)))
27079 return 0;
27080 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27081 return 0;
27082 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27083 return 0;
27084 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27085 return 0;
27086 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27087 return 0;
27088 return 1;
27090 else if (EQ (XCAR (hot_spot), Qcircle))
27092 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27093 Lisp_Object circ = XCDR (hot_spot);
27094 Lisp_Object lr, lx0, ly0;
27095 if (CONSP (circ)
27096 && CONSP (XCAR (circ))
27097 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27098 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27099 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27101 double r = XFLOATINT (lr);
27102 double dx = XINT (lx0) - x;
27103 double dy = XINT (ly0) - y;
27104 return (dx * dx + dy * dy <= r * r);
27107 else if (EQ (XCAR (hot_spot), Qpoly))
27109 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27110 if (VECTORP (XCDR (hot_spot)))
27112 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27113 Lisp_Object *poly = v->contents;
27114 ptrdiff_t n = v->header.size;
27115 ptrdiff_t i;
27116 int inside = 0;
27117 Lisp_Object lx, ly;
27118 int x0, y0;
27120 /* Need an even number of coordinates, and at least 3 edges. */
27121 if (n < 6 || n & 1)
27122 return 0;
27124 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27125 If count is odd, we are inside polygon. Pixels on edges
27126 may or may not be included depending on actual geometry of the
27127 polygon. */
27128 if ((lx = poly[n-2], !INTEGERP (lx))
27129 || (ly = poly[n-1], !INTEGERP (lx)))
27130 return 0;
27131 x0 = XINT (lx), y0 = XINT (ly);
27132 for (i = 0; i < n; i += 2)
27134 int x1 = x0, y1 = y0;
27135 if ((lx = poly[i], !INTEGERP (lx))
27136 || (ly = poly[i+1], !INTEGERP (ly)))
27137 return 0;
27138 x0 = XINT (lx), y0 = XINT (ly);
27140 /* Does this segment cross the X line? */
27141 if (x0 >= x)
27143 if (x1 >= x)
27144 continue;
27146 else if (x1 < x)
27147 continue;
27148 if (y > y0 && y > y1)
27149 continue;
27150 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27151 inside = !inside;
27153 return inside;
27156 return 0;
27159 Lisp_Object
27160 find_hot_spot (Lisp_Object map, int x, int y)
27162 while (CONSP (map))
27164 if (CONSP (XCAR (map))
27165 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27166 return XCAR (map);
27167 map = XCDR (map);
27170 return Qnil;
27173 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27174 3, 3, 0,
27175 doc: /* Lookup in image map MAP coordinates X and Y.
27176 An image map is an alist where each element has the format (AREA ID PLIST).
27177 An AREA is specified as either a rectangle, a circle, or a polygon:
27178 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27179 pixel coordinates of the upper left and bottom right corners.
27180 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27181 and the radius of the circle; r may be a float or integer.
27182 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27183 vector describes one corner in the polygon.
27184 Returns the alist element for the first matching AREA in MAP. */)
27185 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27187 if (NILP (map))
27188 return Qnil;
27190 CHECK_NUMBER (x);
27191 CHECK_NUMBER (y);
27193 return find_hot_spot (map,
27194 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27195 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27199 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27200 static void
27201 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27203 /* Do not change cursor shape while dragging mouse. */
27204 if (!NILP (do_mouse_tracking))
27205 return;
27207 if (!NILP (pointer))
27209 if (EQ (pointer, Qarrow))
27210 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27211 else if (EQ (pointer, Qhand))
27212 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27213 else if (EQ (pointer, Qtext))
27214 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27215 else if (EQ (pointer, intern ("hdrag")))
27216 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27217 #ifdef HAVE_X_WINDOWS
27218 else if (EQ (pointer, intern ("vdrag")))
27219 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27220 #endif
27221 else if (EQ (pointer, intern ("hourglass")))
27222 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27223 else if (EQ (pointer, Qmodeline))
27224 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27225 else
27226 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27229 if (cursor != No_Cursor)
27230 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27233 #endif /* HAVE_WINDOW_SYSTEM */
27235 /* Take proper action when mouse has moved to the mode or header line
27236 or marginal area AREA of window W, x-position X and y-position Y.
27237 X is relative to the start of the text display area of W, so the
27238 width of bitmap areas and scroll bars must be subtracted to get a
27239 position relative to the start of the mode line. */
27241 static void
27242 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27243 enum window_part area)
27245 struct window *w = XWINDOW (window);
27246 struct frame *f = XFRAME (w->frame);
27247 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27248 #ifdef HAVE_WINDOW_SYSTEM
27249 Display_Info *dpyinfo;
27250 #endif
27251 Cursor cursor = No_Cursor;
27252 Lisp_Object pointer = Qnil;
27253 int dx, dy, width, height;
27254 ptrdiff_t charpos;
27255 Lisp_Object string, object = Qnil;
27256 Lisp_Object pos IF_LINT (= Qnil), help;
27258 Lisp_Object mouse_face;
27259 int original_x_pixel = x;
27260 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27261 struct glyph_row *row IF_LINT (= 0);
27263 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27265 int x0;
27266 struct glyph *end;
27268 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27269 returns them in row/column units! */
27270 string = mode_line_string (w, area, &x, &y, &charpos,
27271 &object, &dx, &dy, &width, &height);
27273 row = (area == ON_MODE_LINE
27274 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27275 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27277 /* Find the glyph under the mouse pointer. */
27278 if (row->mode_line_p && row->enabled_p)
27280 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27281 end = glyph + row->used[TEXT_AREA];
27283 for (x0 = original_x_pixel;
27284 glyph < end && x0 >= glyph->pixel_width;
27285 ++glyph)
27286 x0 -= glyph->pixel_width;
27288 if (glyph >= end)
27289 glyph = NULL;
27292 else
27294 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27295 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27296 returns them in row/column units! */
27297 string = marginal_area_string (w, area, &x, &y, &charpos,
27298 &object, &dx, &dy, &width, &height);
27301 help = Qnil;
27303 #ifdef HAVE_WINDOW_SYSTEM
27304 if (IMAGEP (object))
27306 Lisp_Object image_map, hotspot;
27307 if ((image_map = Fplist_get (XCDR (object), QCmap),
27308 !NILP (image_map))
27309 && (hotspot = find_hot_spot (image_map, dx, dy),
27310 CONSP (hotspot))
27311 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27313 Lisp_Object plist;
27315 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27316 If so, we could look for mouse-enter, mouse-leave
27317 properties in PLIST (and do something...). */
27318 hotspot = XCDR (hotspot);
27319 if (CONSP (hotspot)
27320 && (plist = XCAR (hotspot), CONSP (plist)))
27322 pointer = Fplist_get (plist, Qpointer);
27323 if (NILP (pointer))
27324 pointer = Qhand;
27325 help = Fplist_get (plist, Qhelp_echo);
27326 if (!NILP (help))
27328 help_echo_string = help;
27329 XSETWINDOW (help_echo_window, w);
27330 help_echo_object = w->buffer;
27331 help_echo_pos = charpos;
27335 if (NILP (pointer))
27336 pointer = Fplist_get (XCDR (object), QCpointer);
27338 #endif /* HAVE_WINDOW_SYSTEM */
27340 if (STRINGP (string))
27341 pos = make_number (charpos);
27343 /* Set the help text and mouse pointer. If the mouse is on a part
27344 of the mode line without any text (e.g. past the right edge of
27345 the mode line text), use the default help text and pointer. */
27346 if (STRINGP (string) || area == ON_MODE_LINE)
27348 /* Arrange to display the help by setting the global variables
27349 help_echo_string, help_echo_object, and help_echo_pos. */
27350 if (NILP (help))
27352 if (STRINGP (string))
27353 help = Fget_text_property (pos, Qhelp_echo, string);
27355 if (!NILP (help))
27357 help_echo_string = help;
27358 XSETWINDOW (help_echo_window, w);
27359 help_echo_object = string;
27360 help_echo_pos = charpos;
27362 else if (area == ON_MODE_LINE)
27364 Lisp_Object default_help
27365 = buffer_local_value_1 (Qmode_line_default_help_echo,
27366 w->buffer);
27368 if (STRINGP (default_help))
27370 help_echo_string = default_help;
27371 XSETWINDOW (help_echo_window, w);
27372 help_echo_object = Qnil;
27373 help_echo_pos = -1;
27378 #ifdef HAVE_WINDOW_SYSTEM
27379 /* Change the mouse pointer according to what is under it. */
27380 if (FRAME_WINDOW_P (f))
27382 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27383 if (STRINGP (string))
27385 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27387 if (NILP (pointer))
27388 pointer = Fget_text_property (pos, Qpointer, string);
27390 /* Change the mouse pointer according to what is under X/Y. */
27391 if (NILP (pointer)
27392 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27394 Lisp_Object map;
27395 map = Fget_text_property (pos, Qlocal_map, string);
27396 if (!KEYMAPP (map))
27397 map = Fget_text_property (pos, Qkeymap, string);
27398 if (!KEYMAPP (map))
27399 cursor = dpyinfo->vertical_scroll_bar_cursor;
27402 else
27403 /* Default mode-line pointer. */
27404 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27406 #endif
27409 /* Change the mouse face according to what is under X/Y. */
27410 if (STRINGP (string))
27412 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27413 if (!NILP (mouse_face)
27414 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27415 && glyph)
27417 Lisp_Object b, e;
27419 struct glyph * tmp_glyph;
27421 int gpos;
27422 int gseq_length;
27423 int total_pixel_width;
27424 ptrdiff_t begpos, endpos, ignore;
27426 int vpos, hpos;
27428 b = Fprevious_single_property_change (make_number (charpos + 1),
27429 Qmouse_face, string, Qnil);
27430 if (NILP (b))
27431 begpos = 0;
27432 else
27433 begpos = XINT (b);
27435 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27436 if (NILP (e))
27437 endpos = SCHARS (string);
27438 else
27439 endpos = XINT (e);
27441 /* Calculate the glyph position GPOS of GLYPH in the
27442 displayed string, relative to the beginning of the
27443 highlighted part of the string.
27445 Note: GPOS is different from CHARPOS. CHARPOS is the
27446 position of GLYPH in the internal string object. A mode
27447 line string format has structures which are converted to
27448 a flattened string by the Emacs Lisp interpreter. The
27449 internal string is an element of those structures. The
27450 displayed string is the flattened string. */
27451 tmp_glyph = row_start_glyph;
27452 while (tmp_glyph < glyph
27453 && (!(EQ (tmp_glyph->object, glyph->object)
27454 && begpos <= tmp_glyph->charpos
27455 && tmp_glyph->charpos < endpos)))
27456 tmp_glyph++;
27457 gpos = glyph - tmp_glyph;
27459 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27460 the highlighted part of the displayed string to which
27461 GLYPH belongs. Note: GSEQ_LENGTH is different from
27462 SCHARS (STRING), because the latter returns the length of
27463 the internal string. */
27464 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27465 tmp_glyph > glyph
27466 && (!(EQ (tmp_glyph->object, glyph->object)
27467 && begpos <= tmp_glyph->charpos
27468 && tmp_glyph->charpos < endpos));
27469 tmp_glyph--)
27471 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27473 /* Calculate the total pixel width of all the glyphs between
27474 the beginning of the highlighted area and GLYPH. */
27475 total_pixel_width = 0;
27476 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27477 total_pixel_width += tmp_glyph->pixel_width;
27479 /* Pre calculation of re-rendering position. Note: X is in
27480 column units here, after the call to mode_line_string or
27481 marginal_area_string. */
27482 hpos = x - gpos;
27483 vpos = (area == ON_MODE_LINE
27484 ? (w->current_matrix)->nrows - 1
27485 : 0);
27487 /* If GLYPH's position is included in the region that is
27488 already drawn in mouse face, we have nothing to do. */
27489 if ( EQ (window, hlinfo->mouse_face_window)
27490 && (!row->reversed_p
27491 ? (hlinfo->mouse_face_beg_col <= hpos
27492 && hpos < hlinfo->mouse_face_end_col)
27493 /* In R2L rows we swap BEG and END, see below. */
27494 : (hlinfo->mouse_face_end_col <= hpos
27495 && hpos < hlinfo->mouse_face_beg_col))
27496 && hlinfo->mouse_face_beg_row == vpos )
27497 return;
27499 if (clear_mouse_face (hlinfo))
27500 cursor = No_Cursor;
27502 if (!row->reversed_p)
27504 hlinfo->mouse_face_beg_col = hpos;
27505 hlinfo->mouse_face_beg_x = original_x_pixel
27506 - (total_pixel_width + dx);
27507 hlinfo->mouse_face_end_col = hpos + gseq_length;
27508 hlinfo->mouse_face_end_x = 0;
27510 else
27512 /* In R2L rows, show_mouse_face expects BEG and END
27513 coordinates to be swapped. */
27514 hlinfo->mouse_face_end_col = hpos;
27515 hlinfo->mouse_face_end_x = original_x_pixel
27516 - (total_pixel_width + dx);
27517 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27518 hlinfo->mouse_face_beg_x = 0;
27521 hlinfo->mouse_face_beg_row = vpos;
27522 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27523 hlinfo->mouse_face_beg_y = 0;
27524 hlinfo->mouse_face_end_y = 0;
27525 hlinfo->mouse_face_past_end = 0;
27526 hlinfo->mouse_face_window = window;
27528 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27529 charpos,
27530 0, 0, 0,
27531 &ignore,
27532 glyph->face_id,
27534 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27536 if (NILP (pointer))
27537 pointer = Qhand;
27539 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27540 clear_mouse_face (hlinfo);
27542 #ifdef HAVE_WINDOW_SYSTEM
27543 if (FRAME_WINDOW_P (f))
27544 define_frame_cursor1 (f, cursor, pointer);
27545 #endif
27549 /* EXPORT:
27550 Take proper action when the mouse has moved to position X, Y on
27551 frame F as regards highlighting characters that have mouse-face
27552 properties. Also de-highlighting chars where the mouse was before.
27553 X and Y can be negative or out of range. */
27555 void
27556 note_mouse_highlight (struct frame *f, int x, int y)
27558 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27559 enum window_part part = ON_NOTHING;
27560 Lisp_Object window;
27561 struct window *w;
27562 Cursor cursor = No_Cursor;
27563 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27564 struct buffer *b;
27566 /* When a menu is active, don't highlight because this looks odd. */
27567 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27568 if (popup_activated ())
27569 return;
27570 #endif
27572 if (NILP (Vmouse_highlight)
27573 || !f->glyphs_initialized_p
27574 || f->pointer_invisible)
27575 return;
27577 hlinfo->mouse_face_mouse_x = x;
27578 hlinfo->mouse_face_mouse_y = y;
27579 hlinfo->mouse_face_mouse_frame = f;
27581 if (hlinfo->mouse_face_defer)
27582 return;
27584 if (gc_in_progress)
27586 hlinfo->mouse_face_deferred_gc = 1;
27587 return;
27590 /* Which window is that in? */
27591 window = window_from_coordinates (f, x, y, &part, 1);
27593 /* If displaying active text in another window, clear that. */
27594 if (! EQ (window, hlinfo->mouse_face_window)
27595 /* Also clear if we move out of text area in same window. */
27596 || (!NILP (hlinfo->mouse_face_window)
27597 && !NILP (window)
27598 && part != ON_TEXT
27599 && part != ON_MODE_LINE
27600 && part != ON_HEADER_LINE))
27601 clear_mouse_face (hlinfo);
27603 /* Not on a window -> return. */
27604 if (!WINDOWP (window))
27605 return;
27607 /* Reset help_echo_string. It will get recomputed below. */
27608 help_echo_string = Qnil;
27610 /* Convert to window-relative pixel coordinates. */
27611 w = XWINDOW (window);
27612 frame_to_window_pixel_xy (w, &x, &y);
27614 #ifdef HAVE_WINDOW_SYSTEM
27615 /* Handle tool-bar window differently since it doesn't display a
27616 buffer. */
27617 if (EQ (window, f->tool_bar_window))
27619 note_tool_bar_highlight (f, x, y);
27620 return;
27622 #endif
27624 /* Mouse is on the mode, header line or margin? */
27625 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27626 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27628 note_mode_line_or_margin_highlight (window, x, y, part);
27629 return;
27632 #ifdef HAVE_WINDOW_SYSTEM
27633 if (part == ON_VERTICAL_BORDER)
27635 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27636 help_echo_string = build_string ("drag-mouse-1: resize");
27638 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27639 || part == ON_SCROLL_BAR)
27640 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27641 else
27642 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27643 #endif
27645 /* Are we in a window whose display is up to date?
27646 And verify the buffer's text has not changed. */
27647 b = XBUFFER (w->buffer);
27648 if (part == ON_TEXT
27649 && EQ (w->window_end_valid, w->buffer)
27650 && w->last_modified == BUF_MODIFF (b)
27651 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27653 int hpos, vpos, dx, dy, area = LAST_AREA;
27654 ptrdiff_t pos;
27655 struct glyph *glyph;
27656 Lisp_Object object;
27657 Lisp_Object mouse_face = Qnil, position;
27658 Lisp_Object *overlay_vec = NULL;
27659 ptrdiff_t i, noverlays;
27660 struct buffer *obuf;
27661 ptrdiff_t obegv, ozv;
27662 int same_region;
27664 /* Find the glyph under X/Y. */
27665 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27667 #ifdef HAVE_WINDOW_SYSTEM
27668 /* Look for :pointer property on image. */
27669 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27671 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27672 if (img != NULL && IMAGEP (img->spec))
27674 Lisp_Object image_map, hotspot;
27675 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27676 !NILP (image_map))
27677 && (hotspot = find_hot_spot (image_map,
27678 glyph->slice.img.x + dx,
27679 glyph->slice.img.y + dy),
27680 CONSP (hotspot))
27681 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27683 Lisp_Object plist;
27685 /* Could check XCAR (hotspot) to see if we enter/leave
27686 this hot-spot.
27687 If so, we could look for mouse-enter, mouse-leave
27688 properties in PLIST (and do something...). */
27689 hotspot = XCDR (hotspot);
27690 if (CONSP (hotspot)
27691 && (plist = XCAR (hotspot), CONSP (plist)))
27693 pointer = Fplist_get (plist, Qpointer);
27694 if (NILP (pointer))
27695 pointer = Qhand;
27696 help_echo_string = Fplist_get (plist, Qhelp_echo);
27697 if (!NILP (help_echo_string))
27699 help_echo_window = window;
27700 help_echo_object = glyph->object;
27701 help_echo_pos = glyph->charpos;
27705 if (NILP (pointer))
27706 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27709 #endif /* HAVE_WINDOW_SYSTEM */
27711 /* Clear mouse face if X/Y not over text. */
27712 if (glyph == NULL
27713 || area != TEXT_AREA
27714 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27715 /* Glyph's OBJECT is an integer for glyphs inserted by the
27716 display engine for its internal purposes, like truncation
27717 and continuation glyphs and blanks beyond the end of
27718 line's text on text terminals. If we are over such a
27719 glyph, we are not over any text. */
27720 || INTEGERP (glyph->object)
27721 /* R2L rows have a stretch glyph at their front, which
27722 stands for no text, whereas L2R rows have no glyphs at
27723 all beyond the end of text. Treat such stretch glyphs
27724 like we do with NULL glyphs in L2R rows. */
27725 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27726 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27727 && glyph->type == STRETCH_GLYPH
27728 && glyph->avoid_cursor_p))
27730 if (clear_mouse_face (hlinfo))
27731 cursor = No_Cursor;
27732 #ifdef HAVE_WINDOW_SYSTEM
27733 if (FRAME_WINDOW_P (f) && NILP (pointer))
27735 if (area != TEXT_AREA)
27736 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27737 else
27738 pointer = Vvoid_text_area_pointer;
27740 #endif
27741 goto set_cursor;
27744 pos = glyph->charpos;
27745 object = glyph->object;
27746 if (!STRINGP (object) && !BUFFERP (object))
27747 goto set_cursor;
27749 /* If we get an out-of-range value, return now; avoid an error. */
27750 if (BUFFERP (object) && pos > BUF_Z (b))
27751 goto set_cursor;
27753 /* Make the window's buffer temporarily current for
27754 overlays_at and compute_char_face. */
27755 obuf = current_buffer;
27756 current_buffer = b;
27757 obegv = BEGV;
27758 ozv = ZV;
27759 BEGV = BEG;
27760 ZV = Z;
27762 /* Is this char mouse-active or does it have help-echo? */
27763 position = make_number (pos);
27765 if (BUFFERP (object))
27767 /* Put all the overlays we want in a vector in overlay_vec. */
27768 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27769 /* Sort overlays into increasing priority order. */
27770 noverlays = sort_overlays (overlay_vec, noverlays, w);
27772 else
27773 noverlays = 0;
27775 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27777 if (same_region)
27778 cursor = No_Cursor;
27780 /* Check mouse-face highlighting. */
27781 if (! same_region
27782 /* If there exists an overlay with mouse-face overlapping
27783 the one we are currently highlighting, we have to
27784 check if we enter the overlapping overlay, and then
27785 highlight only that. */
27786 || (OVERLAYP (hlinfo->mouse_face_overlay)
27787 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27789 /* Find the highest priority overlay with a mouse-face. */
27790 Lisp_Object overlay = Qnil;
27791 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27793 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27794 if (!NILP (mouse_face))
27795 overlay = overlay_vec[i];
27798 /* If we're highlighting the same overlay as before, there's
27799 no need to do that again. */
27800 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27801 goto check_help_echo;
27802 hlinfo->mouse_face_overlay = overlay;
27804 /* Clear the display of the old active region, if any. */
27805 if (clear_mouse_face (hlinfo))
27806 cursor = No_Cursor;
27808 /* If no overlay applies, get a text property. */
27809 if (NILP (overlay))
27810 mouse_face = Fget_text_property (position, Qmouse_face, object);
27812 /* Next, compute the bounds of the mouse highlighting and
27813 display it. */
27814 if (!NILP (mouse_face) && STRINGP (object))
27816 /* The mouse-highlighting comes from a display string
27817 with a mouse-face. */
27818 Lisp_Object s, e;
27819 ptrdiff_t ignore;
27821 s = Fprevious_single_property_change
27822 (make_number (pos + 1), Qmouse_face, object, Qnil);
27823 e = Fnext_single_property_change
27824 (position, Qmouse_face, object, Qnil);
27825 if (NILP (s))
27826 s = make_number (0);
27827 if (NILP (e))
27828 e = make_number (SCHARS (object) - 1);
27829 mouse_face_from_string_pos (w, hlinfo, object,
27830 XINT (s), XINT (e));
27831 hlinfo->mouse_face_past_end = 0;
27832 hlinfo->mouse_face_window = window;
27833 hlinfo->mouse_face_face_id
27834 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27835 glyph->face_id, 1);
27836 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27837 cursor = No_Cursor;
27839 else
27841 /* The mouse-highlighting, if any, comes from an overlay
27842 or text property in the buffer. */
27843 Lisp_Object buffer IF_LINT (= Qnil);
27844 Lisp_Object disp_string IF_LINT (= Qnil);
27846 if (STRINGP (object))
27848 /* If we are on a display string with no mouse-face,
27849 check if the text under it has one. */
27850 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27851 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27852 pos = string_buffer_position (object, start);
27853 if (pos > 0)
27855 mouse_face = get_char_property_and_overlay
27856 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27857 buffer = w->buffer;
27858 disp_string = object;
27861 else
27863 buffer = object;
27864 disp_string = Qnil;
27867 if (!NILP (mouse_face))
27869 Lisp_Object before, after;
27870 Lisp_Object before_string, after_string;
27871 /* To correctly find the limits of mouse highlight
27872 in a bidi-reordered buffer, we must not use the
27873 optimization of limiting the search in
27874 previous-single-property-change and
27875 next-single-property-change, because
27876 rows_from_pos_range needs the real start and end
27877 positions to DTRT in this case. That's because
27878 the first row visible in a window does not
27879 necessarily display the character whose position
27880 is the smallest. */
27881 Lisp_Object lim1 =
27882 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27883 ? Fmarker_position (w->start)
27884 : Qnil;
27885 Lisp_Object lim2 =
27886 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27887 ? make_number (BUF_Z (XBUFFER (buffer))
27888 - XFASTINT (w->window_end_pos))
27889 : Qnil;
27891 if (NILP (overlay))
27893 /* Handle the text property case. */
27894 before = Fprevious_single_property_change
27895 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27896 after = Fnext_single_property_change
27897 (make_number (pos), Qmouse_face, buffer, lim2);
27898 before_string = after_string = Qnil;
27900 else
27902 /* Handle the overlay case. */
27903 before = Foverlay_start (overlay);
27904 after = Foverlay_end (overlay);
27905 before_string = Foverlay_get (overlay, Qbefore_string);
27906 after_string = Foverlay_get (overlay, Qafter_string);
27908 if (!STRINGP (before_string)) before_string = Qnil;
27909 if (!STRINGP (after_string)) after_string = Qnil;
27912 mouse_face_from_buffer_pos (window, hlinfo, pos,
27913 NILP (before)
27915 : XFASTINT (before),
27916 NILP (after)
27917 ? BUF_Z (XBUFFER (buffer))
27918 : XFASTINT (after),
27919 before_string, after_string,
27920 disp_string);
27921 cursor = No_Cursor;
27926 check_help_echo:
27928 /* Look for a `help-echo' property. */
27929 if (NILP (help_echo_string)) {
27930 Lisp_Object help, overlay;
27932 /* Check overlays first. */
27933 help = overlay = Qnil;
27934 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27936 overlay = overlay_vec[i];
27937 help = Foverlay_get (overlay, Qhelp_echo);
27940 if (!NILP (help))
27942 help_echo_string = help;
27943 help_echo_window = window;
27944 help_echo_object = overlay;
27945 help_echo_pos = pos;
27947 else
27949 Lisp_Object obj = glyph->object;
27950 ptrdiff_t charpos = glyph->charpos;
27952 /* Try text properties. */
27953 if (STRINGP (obj)
27954 && charpos >= 0
27955 && charpos < SCHARS (obj))
27957 help = Fget_text_property (make_number (charpos),
27958 Qhelp_echo, obj);
27959 if (NILP (help))
27961 /* If the string itself doesn't specify a help-echo,
27962 see if the buffer text ``under'' it does. */
27963 struct glyph_row *r
27964 = MATRIX_ROW (w->current_matrix, vpos);
27965 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27966 ptrdiff_t p = string_buffer_position (obj, start);
27967 if (p > 0)
27969 help = Fget_char_property (make_number (p),
27970 Qhelp_echo, w->buffer);
27971 if (!NILP (help))
27973 charpos = p;
27974 obj = w->buffer;
27979 else if (BUFFERP (obj)
27980 && charpos >= BEGV
27981 && charpos < ZV)
27982 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27983 obj);
27985 if (!NILP (help))
27987 help_echo_string = help;
27988 help_echo_window = window;
27989 help_echo_object = obj;
27990 help_echo_pos = charpos;
27995 #ifdef HAVE_WINDOW_SYSTEM
27996 /* Look for a `pointer' property. */
27997 if (FRAME_WINDOW_P (f) && NILP (pointer))
27999 /* Check overlays first. */
28000 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28001 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28003 if (NILP (pointer))
28005 Lisp_Object obj = glyph->object;
28006 ptrdiff_t charpos = glyph->charpos;
28008 /* Try text properties. */
28009 if (STRINGP (obj)
28010 && charpos >= 0
28011 && charpos < SCHARS (obj))
28013 pointer = Fget_text_property (make_number (charpos),
28014 Qpointer, obj);
28015 if (NILP (pointer))
28017 /* If the string itself doesn't specify a pointer,
28018 see if the buffer text ``under'' it does. */
28019 struct glyph_row *r
28020 = MATRIX_ROW (w->current_matrix, vpos);
28021 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28022 ptrdiff_t p = string_buffer_position (obj, start);
28023 if (p > 0)
28024 pointer = Fget_char_property (make_number (p),
28025 Qpointer, w->buffer);
28028 else if (BUFFERP (obj)
28029 && charpos >= BEGV
28030 && charpos < ZV)
28031 pointer = Fget_text_property (make_number (charpos),
28032 Qpointer, obj);
28035 #endif /* HAVE_WINDOW_SYSTEM */
28037 BEGV = obegv;
28038 ZV = ozv;
28039 current_buffer = obuf;
28042 set_cursor:
28044 #ifdef HAVE_WINDOW_SYSTEM
28045 if (FRAME_WINDOW_P (f))
28046 define_frame_cursor1 (f, cursor, pointer);
28047 #else
28048 /* This is here to prevent a compiler error, about "label at end of
28049 compound statement". */
28050 return;
28051 #endif
28055 /* EXPORT for RIF:
28056 Clear any mouse-face on window W. This function is part of the
28057 redisplay interface, and is called from try_window_id and similar
28058 functions to ensure the mouse-highlight is off. */
28060 void
28061 x_clear_window_mouse_face (struct window *w)
28063 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28064 Lisp_Object window;
28066 BLOCK_INPUT;
28067 XSETWINDOW (window, w);
28068 if (EQ (window, hlinfo->mouse_face_window))
28069 clear_mouse_face (hlinfo);
28070 UNBLOCK_INPUT;
28074 /* EXPORT:
28075 Just discard the mouse face information for frame F, if any.
28076 This is used when the size of F is changed. */
28078 void
28079 cancel_mouse_face (struct frame *f)
28081 Lisp_Object window;
28082 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28084 window = hlinfo->mouse_face_window;
28085 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28087 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28088 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28089 hlinfo->mouse_face_window = Qnil;
28095 /***********************************************************************
28096 Exposure Events
28097 ***********************************************************************/
28099 #ifdef HAVE_WINDOW_SYSTEM
28101 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28102 which intersects rectangle R. R is in window-relative coordinates. */
28104 static void
28105 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28106 enum glyph_row_area area)
28108 struct glyph *first = row->glyphs[area];
28109 struct glyph *end = row->glyphs[area] + row->used[area];
28110 struct glyph *last;
28111 int first_x, start_x, x;
28113 if (area == TEXT_AREA && row->fill_line_p)
28114 /* If row extends face to end of line write the whole line. */
28115 draw_glyphs (w, 0, row, area,
28116 0, row->used[area],
28117 DRAW_NORMAL_TEXT, 0);
28118 else
28120 /* Set START_X to the window-relative start position for drawing glyphs of
28121 AREA. The first glyph of the text area can be partially visible.
28122 The first glyphs of other areas cannot. */
28123 start_x = window_box_left_offset (w, area);
28124 x = start_x;
28125 if (area == TEXT_AREA)
28126 x += row->x;
28128 /* Find the first glyph that must be redrawn. */
28129 while (first < end
28130 && x + first->pixel_width < r->x)
28132 x += first->pixel_width;
28133 ++first;
28136 /* Find the last one. */
28137 last = first;
28138 first_x = x;
28139 while (last < end
28140 && x < r->x + r->width)
28142 x += last->pixel_width;
28143 ++last;
28146 /* Repaint. */
28147 if (last > first)
28148 draw_glyphs (w, first_x - start_x, row, area,
28149 first - row->glyphs[area], last - row->glyphs[area],
28150 DRAW_NORMAL_TEXT, 0);
28155 /* Redraw the parts of the glyph row ROW on window W intersecting
28156 rectangle R. R is in window-relative coordinates. Value is
28157 non-zero if mouse-face was overwritten. */
28159 static int
28160 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28162 eassert (row->enabled_p);
28164 if (row->mode_line_p || w->pseudo_window_p)
28165 draw_glyphs (w, 0, row, TEXT_AREA,
28166 0, row->used[TEXT_AREA],
28167 DRAW_NORMAL_TEXT, 0);
28168 else
28170 if (row->used[LEFT_MARGIN_AREA])
28171 expose_area (w, row, r, LEFT_MARGIN_AREA);
28172 if (row->used[TEXT_AREA])
28173 expose_area (w, row, r, TEXT_AREA);
28174 if (row->used[RIGHT_MARGIN_AREA])
28175 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28176 draw_row_fringe_bitmaps (w, row);
28179 return row->mouse_face_p;
28183 /* Redraw those parts of glyphs rows during expose event handling that
28184 overlap other rows. Redrawing of an exposed line writes over parts
28185 of lines overlapping that exposed line; this function fixes that.
28187 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28188 row in W's current matrix that is exposed and overlaps other rows.
28189 LAST_OVERLAPPING_ROW is the last such row. */
28191 static void
28192 expose_overlaps (struct window *w,
28193 struct glyph_row *first_overlapping_row,
28194 struct glyph_row *last_overlapping_row,
28195 XRectangle *r)
28197 struct glyph_row *row;
28199 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28200 if (row->overlapping_p)
28202 eassert (row->enabled_p && !row->mode_line_p);
28204 row->clip = r;
28205 if (row->used[LEFT_MARGIN_AREA])
28206 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28208 if (row->used[TEXT_AREA])
28209 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28211 if (row->used[RIGHT_MARGIN_AREA])
28212 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28213 row->clip = NULL;
28218 /* Return non-zero if W's cursor intersects rectangle R. */
28220 static int
28221 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28223 XRectangle cr, result;
28224 struct glyph *cursor_glyph;
28225 struct glyph_row *row;
28227 if (w->phys_cursor.vpos >= 0
28228 && w->phys_cursor.vpos < w->current_matrix->nrows
28229 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28230 row->enabled_p)
28231 && row->cursor_in_fringe_p)
28233 /* Cursor is in the fringe. */
28234 cr.x = window_box_right_offset (w,
28235 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28236 ? RIGHT_MARGIN_AREA
28237 : TEXT_AREA));
28238 cr.y = row->y;
28239 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28240 cr.height = row->height;
28241 return x_intersect_rectangles (&cr, r, &result);
28244 cursor_glyph = get_phys_cursor_glyph (w);
28245 if (cursor_glyph)
28247 /* r is relative to W's box, but w->phys_cursor.x is relative
28248 to left edge of W's TEXT area. Adjust it. */
28249 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28250 cr.y = w->phys_cursor.y;
28251 cr.width = cursor_glyph->pixel_width;
28252 cr.height = w->phys_cursor_height;
28253 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28254 I assume the effect is the same -- and this is portable. */
28255 return x_intersect_rectangles (&cr, r, &result);
28257 /* If we don't understand the format, pretend we're not in the hot-spot. */
28258 return 0;
28262 /* EXPORT:
28263 Draw a vertical window border to the right of window W if W doesn't
28264 have vertical scroll bars. */
28266 void
28267 x_draw_vertical_border (struct window *w)
28269 struct frame *f = XFRAME (WINDOW_FRAME (w));
28271 /* We could do better, if we knew what type of scroll-bar the adjacent
28272 windows (on either side) have... But we don't :-(
28273 However, I think this works ok. ++KFS 2003-04-25 */
28275 /* Redraw borders between horizontally adjacent windows. Don't
28276 do it for frames with vertical scroll bars because either the
28277 right scroll bar of a window, or the left scroll bar of its
28278 neighbor will suffice as a border. */
28279 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28280 return;
28282 if (!WINDOW_RIGHTMOST_P (w)
28283 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28285 int x0, x1, y0, y1;
28287 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28288 y1 -= 1;
28290 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28291 x1 -= 1;
28293 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28295 else if (!WINDOW_LEFTMOST_P (w)
28296 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28298 int x0, x1, y0, y1;
28300 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28301 y1 -= 1;
28303 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28304 x0 -= 1;
28306 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28311 /* Redraw the part of window W intersection rectangle FR. Pixel
28312 coordinates in FR are frame-relative. Call this function with
28313 input blocked. Value is non-zero if the exposure overwrites
28314 mouse-face. */
28316 static int
28317 expose_window (struct window *w, XRectangle *fr)
28319 struct frame *f = XFRAME (w->frame);
28320 XRectangle wr, r;
28321 int mouse_face_overwritten_p = 0;
28323 /* If window is not yet fully initialized, do nothing. This can
28324 happen when toolkit scroll bars are used and a window is split.
28325 Reconfiguring the scroll bar will generate an expose for a newly
28326 created window. */
28327 if (w->current_matrix == NULL)
28328 return 0;
28330 /* When we're currently updating the window, display and current
28331 matrix usually don't agree. Arrange for a thorough display
28332 later. */
28333 if (w == updated_window)
28335 SET_FRAME_GARBAGED (f);
28336 return 0;
28339 /* Frame-relative pixel rectangle of W. */
28340 wr.x = WINDOW_LEFT_EDGE_X (w);
28341 wr.y = WINDOW_TOP_EDGE_Y (w);
28342 wr.width = WINDOW_TOTAL_WIDTH (w);
28343 wr.height = WINDOW_TOTAL_HEIGHT (w);
28345 if (x_intersect_rectangles (fr, &wr, &r))
28347 int yb = window_text_bottom_y (w);
28348 struct glyph_row *row;
28349 int cursor_cleared_p, phys_cursor_on_p;
28350 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28352 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28353 r.x, r.y, r.width, r.height));
28355 /* Convert to window coordinates. */
28356 r.x -= WINDOW_LEFT_EDGE_X (w);
28357 r.y -= WINDOW_TOP_EDGE_Y (w);
28359 /* Turn off the cursor. */
28360 if (!w->pseudo_window_p
28361 && phys_cursor_in_rect_p (w, &r))
28363 x_clear_cursor (w);
28364 cursor_cleared_p = 1;
28366 else
28367 cursor_cleared_p = 0;
28369 /* If the row containing the cursor extends face to end of line,
28370 then expose_area might overwrite the cursor outside the
28371 rectangle and thus notice_overwritten_cursor might clear
28372 w->phys_cursor_on_p. We remember the original value and
28373 check later if it is changed. */
28374 phys_cursor_on_p = w->phys_cursor_on_p;
28376 /* Update lines intersecting rectangle R. */
28377 first_overlapping_row = last_overlapping_row = NULL;
28378 for (row = w->current_matrix->rows;
28379 row->enabled_p;
28380 ++row)
28382 int y0 = row->y;
28383 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28385 if ((y0 >= r.y && y0 < r.y + r.height)
28386 || (y1 > r.y && y1 < r.y + r.height)
28387 || (r.y >= y0 && r.y < y1)
28388 || (r.y + r.height > y0 && r.y + r.height < y1))
28390 /* A header line may be overlapping, but there is no need
28391 to fix overlapping areas for them. KFS 2005-02-12 */
28392 if (row->overlapping_p && !row->mode_line_p)
28394 if (first_overlapping_row == NULL)
28395 first_overlapping_row = row;
28396 last_overlapping_row = row;
28399 row->clip = fr;
28400 if (expose_line (w, row, &r))
28401 mouse_face_overwritten_p = 1;
28402 row->clip = NULL;
28404 else if (row->overlapping_p)
28406 /* We must redraw a row overlapping the exposed area. */
28407 if (y0 < r.y
28408 ? y0 + row->phys_height > r.y
28409 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28411 if (first_overlapping_row == NULL)
28412 first_overlapping_row = row;
28413 last_overlapping_row = row;
28417 if (y1 >= yb)
28418 break;
28421 /* Display the mode line if there is one. */
28422 if (WINDOW_WANTS_MODELINE_P (w)
28423 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28424 row->enabled_p)
28425 && row->y < r.y + r.height)
28427 if (expose_line (w, row, &r))
28428 mouse_face_overwritten_p = 1;
28431 if (!w->pseudo_window_p)
28433 /* Fix the display of overlapping rows. */
28434 if (first_overlapping_row)
28435 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28436 fr);
28438 /* Draw border between windows. */
28439 x_draw_vertical_border (w);
28441 /* Turn the cursor on again. */
28442 if (cursor_cleared_p
28443 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28444 update_window_cursor (w, 1);
28448 return mouse_face_overwritten_p;
28453 /* Redraw (parts) of all windows in the window tree rooted at W that
28454 intersect R. R contains frame pixel coordinates. Value is
28455 non-zero if the exposure overwrites mouse-face. */
28457 static int
28458 expose_window_tree (struct window *w, XRectangle *r)
28460 struct frame *f = XFRAME (w->frame);
28461 int mouse_face_overwritten_p = 0;
28463 while (w && !FRAME_GARBAGED_P (f))
28465 if (!NILP (w->hchild))
28466 mouse_face_overwritten_p
28467 |= expose_window_tree (XWINDOW (w->hchild), r);
28468 else if (!NILP (w->vchild))
28469 mouse_face_overwritten_p
28470 |= expose_window_tree (XWINDOW (w->vchild), r);
28471 else
28472 mouse_face_overwritten_p |= expose_window (w, r);
28474 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28477 return mouse_face_overwritten_p;
28481 /* EXPORT:
28482 Redisplay an exposed area of frame F. X and Y are the upper-left
28483 corner of the exposed rectangle. W and H are width and height of
28484 the exposed area. All are pixel values. W or H zero means redraw
28485 the entire frame. */
28487 void
28488 expose_frame (struct frame *f, int x, int y, int w, int h)
28490 XRectangle r;
28491 int mouse_face_overwritten_p = 0;
28493 TRACE ((stderr, "expose_frame "));
28495 /* No need to redraw if frame will be redrawn soon. */
28496 if (FRAME_GARBAGED_P (f))
28498 TRACE ((stderr, " garbaged\n"));
28499 return;
28502 /* If basic faces haven't been realized yet, there is no point in
28503 trying to redraw anything. This can happen when we get an expose
28504 event while Emacs is starting, e.g. by moving another window. */
28505 if (FRAME_FACE_CACHE (f) == NULL
28506 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28508 TRACE ((stderr, " no faces\n"));
28509 return;
28512 if (w == 0 || h == 0)
28514 r.x = r.y = 0;
28515 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28516 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28518 else
28520 r.x = x;
28521 r.y = y;
28522 r.width = w;
28523 r.height = h;
28526 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28527 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28529 if (WINDOWP (f->tool_bar_window))
28530 mouse_face_overwritten_p
28531 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28533 #ifdef HAVE_X_WINDOWS
28534 #ifndef MSDOS
28535 #ifndef USE_X_TOOLKIT
28536 if (WINDOWP (f->menu_bar_window))
28537 mouse_face_overwritten_p
28538 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28539 #endif /* not USE_X_TOOLKIT */
28540 #endif
28541 #endif
28543 /* Some window managers support a focus-follows-mouse style with
28544 delayed raising of frames. Imagine a partially obscured frame,
28545 and moving the mouse into partially obscured mouse-face on that
28546 frame. The visible part of the mouse-face will be highlighted,
28547 then the WM raises the obscured frame. With at least one WM, KDE
28548 2.1, Emacs is not getting any event for the raising of the frame
28549 (even tried with SubstructureRedirectMask), only Expose events.
28550 These expose events will draw text normally, i.e. not
28551 highlighted. Which means we must redo the highlight here.
28552 Subsume it under ``we love X''. --gerd 2001-08-15 */
28553 /* Included in Windows version because Windows most likely does not
28554 do the right thing if any third party tool offers
28555 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28556 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28558 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28559 if (f == hlinfo->mouse_face_mouse_frame)
28561 int mouse_x = hlinfo->mouse_face_mouse_x;
28562 int mouse_y = hlinfo->mouse_face_mouse_y;
28563 clear_mouse_face (hlinfo);
28564 note_mouse_highlight (f, mouse_x, mouse_y);
28570 /* EXPORT:
28571 Determine the intersection of two rectangles R1 and R2. Return
28572 the intersection in *RESULT. Value is non-zero if RESULT is not
28573 empty. */
28576 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28578 XRectangle *left, *right;
28579 XRectangle *upper, *lower;
28580 int intersection_p = 0;
28582 /* Rearrange so that R1 is the left-most rectangle. */
28583 if (r1->x < r2->x)
28584 left = r1, right = r2;
28585 else
28586 left = r2, right = r1;
28588 /* X0 of the intersection is right.x0, if this is inside R1,
28589 otherwise there is no intersection. */
28590 if (right->x <= left->x + left->width)
28592 result->x = right->x;
28594 /* The right end of the intersection is the minimum of
28595 the right ends of left and right. */
28596 result->width = (min (left->x + left->width, right->x + right->width)
28597 - result->x);
28599 /* Same game for Y. */
28600 if (r1->y < r2->y)
28601 upper = r1, lower = r2;
28602 else
28603 upper = r2, lower = r1;
28605 /* The upper end of the intersection is lower.y0, if this is inside
28606 of upper. Otherwise, there is no intersection. */
28607 if (lower->y <= upper->y + upper->height)
28609 result->y = lower->y;
28611 /* The lower end of the intersection is the minimum of the lower
28612 ends of upper and lower. */
28613 result->height = (min (lower->y + lower->height,
28614 upper->y + upper->height)
28615 - result->y);
28616 intersection_p = 1;
28620 return intersection_p;
28623 #endif /* HAVE_WINDOW_SYSTEM */
28626 /***********************************************************************
28627 Initialization
28628 ***********************************************************************/
28630 void
28631 syms_of_xdisp (void)
28633 Vwith_echo_area_save_vector = Qnil;
28634 staticpro (&Vwith_echo_area_save_vector);
28636 Vmessage_stack = Qnil;
28637 staticpro (&Vmessage_stack);
28639 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28641 message_dolog_marker1 = Fmake_marker ();
28642 staticpro (&message_dolog_marker1);
28643 message_dolog_marker2 = Fmake_marker ();
28644 staticpro (&message_dolog_marker2);
28645 message_dolog_marker3 = Fmake_marker ();
28646 staticpro (&message_dolog_marker3);
28648 #ifdef GLYPH_DEBUG
28649 defsubr (&Sdump_frame_glyph_matrix);
28650 defsubr (&Sdump_glyph_matrix);
28651 defsubr (&Sdump_glyph_row);
28652 defsubr (&Sdump_tool_bar_row);
28653 defsubr (&Strace_redisplay);
28654 defsubr (&Strace_to_stderr);
28655 #endif
28656 #ifdef HAVE_WINDOW_SYSTEM
28657 defsubr (&Stool_bar_lines_needed);
28658 defsubr (&Slookup_image_map);
28659 #endif
28660 defsubr (&Sformat_mode_line);
28661 defsubr (&Sinvisible_p);
28662 defsubr (&Scurrent_bidi_paragraph_direction);
28664 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28665 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28666 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28667 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28668 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28669 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28670 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28671 DEFSYM (Qeval, "eval");
28672 DEFSYM (QCdata, ":data");
28673 DEFSYM (Qdisplay, "display");
28674 DEFSYM (Qspace_width, "space-width");
28675 DEFSYM (Qraise, "raise");
28676 DEFSYM (Qslice, "slice");
28677 DEFSYM (Qspace, "space");
28678 DEFSYM (Qmargin, "margin");
28679 DEFSYM (Qpointer, "pointer");
28680 DEFSYM (Qleft_margin, "left-margin");
28681 DEFSYM (Qright_margin, "right-margin");
28682 DEFSYM (Qcenter, "center");
28683 DEFSYM (Qline_height, "line-height");
28684 DEFSYM (QCalign_to, ":align-to");
28685 DEFSYM (QCrelative_width, ":relative-width");
28686 DEFSYM (QCrelative_height, ":relative-height");
28687 DEFSYM (QCeval, ":eval");
28688 DEFSYM (QCpropertize, ":propertize");
28689 DEFSYM (QCfile, ":file");
28690 DEFSYM (Qfontified, "fontified");
28691 DEFSYM (Qfontification_functions, "fontification-functions");
28692 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28693 DEFSYM (Qescape_glyph, "escape-glyph");
28694 DEFSYM (Qnobreak_space, "nobreak-space");
28695 DEFSYM (Qimage, "image");
28696 DEFSYM (Qtext, "text");
28697 DEFSYM (Qboth, "both");
28698 DEFSYM (Qboth_horiz, "both-horiz");
28699 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28700 DEFSYM (QCmap, ":map");
28701 DEFSYM (QCpointer, ":pointer");
28702 DEFSYM (Qrect, "rect");
28703 DEFSYM (Qcircle, "circle");
28704 DEFSYM (Qpoly, "poly");
28705 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28706 DEFSYM (Qgrow_only, "grow-only");
28707 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28708 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28709 DEFSYM (Qposition, "position");
28710 DEFSYM (Qbuffer_position, "buffer-position");
28711 DEFSYM (Qobject, "object");
28712 DEFSYM (Qbar, "bar");
28713 DEFSYM (Qhbar, "hbar");
28714 DEFSYM (Qbox, "box");
28715 DEFSYM (Qhollow, "hollow");
28716 DEFSYM (Qhand, "hand");
28717 DEFSYM (Qarrow, "arrow");
28718 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28720 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28721 Fcons (intern_c_string ("void-variable"), Qnil)),
28722 Qnil);
28723 staticpro (&list_of_error);
28725 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28726 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28727 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28728 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28730 echo_buffer[0] = echo_buffer[1] = Qnil;
28731 staticpro (&echo_buffer[0]);
28732 staticpro (&echo_buffer[1]);
28734 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28735 staticpro (&echo_area_buffer[0]);
28736 staticpro (&echo_area_buffer[1]);
28738 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28739 staticpro (&Vmessages_buffer_name);
28741 mode_line_proptrans_alist = Qnil;
28742 staticpro (&mode_line_proptrans_alist);
28743 mode_line_string_list = Qnil;
28744 staticpro (&mode_line_string_list);
28745 mode_line_string_face = Qnil;
28746 staticpro (&mode_line_string_face);
28747 mode_line_string_face_prop = Qnil;
28748 staticpro (&mode_line_string_face_prop);
28749 Vmode_line_unwind_vector = Qnil;
28750 staticpro (&Vmode_line_unwind_vector);
28752 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28754 help_echo_string = Qnil;
28755 staticpro (&help_echo_string);
28756 help_echo_object = Qnil;
28757 staticpro (&help_echo_object);
28758 help_echo_window = Qnil;
28759 staticpro (&help_echo_window);
28760 previous_help_echo_string = Qnil;
28761 staticpro (&previous_help_echo_string);
28762 help_echo_pos = -1;
28764 DEFSYM (Qright_to_left, "right-to-left");
28765 DEFSYM (Qleft_to_right, "left-to-right");
28767 #ifdef HAVE_WINDOW_SYSTEM
28768 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28769 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28770 For example, if a block cursor is over a tab, it will be drawn as
28771 wide as that tab on the display. */);
28772 x_stretch_cursor_p = 0;
28773 #endif
28775 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28776 doc: /* Non-nil means highlight trailing whitespace.
28777 The face used for trailing whitespace is `trailing-whitespace'. */);
28778 Vshow_trailing_whitespace = Qnil;
28780 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28781 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28782 If the value is t, Emacs highlights non-ASCII chars which have the
28783 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28784 or `escape-glyph' face respectively.
28786 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28787 U+2011 (non-breaking hyphen) are affected.
28789 Any other non-nil value means to display these characters as a escape
28790 glyph followed by an ordinary space or hyphen.
28792 A value of nil means no special handling of these characters. */);
28793 Vnobreak_char_display = Qt;
28795 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28796 doc: /* The pointer shape to show in void text areas.
28797 A value of nil means to show the text pointer. Other options are `arrow',
28798 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28799 Vvoid_text_area_pointer = Qarrow;
28801 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28802 doc: /* Non-nil means don't actually do any redisplay.
28803 This is used for internal purposes. */);
28804 Vinhibit_redisplay = Qnil;
28806 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28807 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28808 Vglobal_mode_string = Qnil;
28810 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28811 doc: /* Marker for where to display an arrow on top of the buffer text.
28812 This must be the beginning of a line in order to work.
28813 See also `overlay-arrow-string'. */);
28814 Voverlay_arrow_position = Qnil;
28816 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28817 doc: /* String to display as an arrow in non-window frames.
28818 See also `overlay-arrow-position'. */);
28819 Voverlay_arrow_string = build_pure_c_string ("=>");
28821 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28822 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28823 The symbols on this list are examined during redisplay to determine
28824 where to display overlay arrows. */);
28825 Voverlay_arrow_variable_list
28826 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28828 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28829 doc: /* The number of lines to try scrolling a window by when point moves out.
28830 If that fails to bring point back on frame, point is centered instead.
28831 If this is zero, point is always centered after it moves off frame.
28832 If you want scrolling to always be a line at a time, you should set
28833 `scroll-conservatively' to a large value rather than set this to 1. */);
28835 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28836 doc: /* Scroll up to this many lines, to bring point back on screen.
28837 If point moves off-screen, redisplay will scroll by up to
28838 `scroll-conservatively' lines in order to bring point just barely
28839 onto the screen again. If that cannot be done, then redisplay
28840 recenters point as usual.
28842 If the value is greater than 100, redisplay will never recenter point,
28843 but will always scroll just enough text to bring point into view, even
28844 if you move far away.
28846 A value of zero means always recenter point if it moves off screen. */);
28847 scroll_conservatively = 0;
28849 DEFVAR_INT ("scroll-margin", scroll_margin,
28850 doc: /* Number of lines of margin at the top and bottom of a window.
28851 Recenter the window whenever point gets within this many lines
28852 of the top or bottom of the window. */);
28853 scroll_margin = 0;
28855 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28856 doc: /* Pixels per inch value for non-window system displays.
28857 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28858 Vdisplay_pixels_per_inch = make_float (72.0);
28860 #ifdef GLYPH_DEBUG
28861 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28862 #endif
28864 DEFVAR_LISP ("truncate-partial-width-windows",
28865 Vtruncate_partial_width_windows,
28866 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28867 For an integer value, truncate lines in each window narrower than the
28868 full frame width, provided the window width is less than that integer;
28869 otherwise, respect the value of `truncate-lines'.
28871 For any other non-nil value, truncate lines in all windows that do
28872 not span the full frame width.
28874 A value of nil means to respect the value of `truncate-lines'.
28876 If `word-wrap' is enabled, you might want to reduce this. */);
28877 Vtruncate_partial_width_windows = make_number (50);
28879 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28880 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28881 Any other value means to use the appropriate face, `mode-line',
28882 `header-line', or `menu' respectively. */);
28883 mode_line_inverse_video = 1;
28885 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28886 doc: /* Maximum buffer size for which line number should be displayed.
28887 If the buffer is bigger than this, the line number does not appear
28888 in the mode line. A value of nil means no limit. */);
28889 Vline_number_display_limit = Qnil;
28891 DEFVAR_INT ("line-number-display-limit-width",
28892 line_number_display_limit_width,
28893 doc: /* Maximum line width (in characters) for line number display.
28894 If the average length of the lines near point is bigger than this, then the
28895 line number may be omitted from the mode line. */);
28896 line_number_display_limit_width = 200;
28898 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28899 doc: /* Non-nil means highlight region even in nonselected windows. */);
28900 highlight_nonselected_windows = 0;
28902 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28903 doc: /* Non-nil if more than one frame is visible on this display.
28904 Minibuffer-only frames don't count, but iconified frames do.
28905 This variable is not guaranteed to be accurate except while processing
28906 `frame-title-format' and `icon-title-format'. */);
28908 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28909 doc: /* Template for displaying the title bar of visible frames.
28910 \(Assuming the window manager supports this feature.)
28912 This variable has the same structure as `mode-line-format', except that
28913 the %c and %l constructs are ignored. It is used only on frames for
28914 which no explicit name has been set \(see `modify-frame-parameters'). */);
28916 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28917 doc: /* Template for displaying the title bar of an iconified frame.
28918 \(Assuming the window manager supports this feature.)
28919 This variable has the same structure as `mode-line-format' (which see),
28920 and is used only on frames for which no explicit name has been set
28921 \(see `modify-frame-parameters'). */);
28922 Vicon_title_format
28923 = Vframe_title_format
28924 = pure_cons (intern_c_string ("multiple-frames"),
28925 pure_cons (build_pure_c_string ("%b"),
28926 pure_cons (pure_cons (empty_unibyte_string,
28927 pure_cons (intern_c_string ("invocation-name"),
28928 pure_cons (build_pure_c_string ("@"),
28929 pure_cons (intern_c_string ("system-name"),
28930 Qnil)))),
28931 Qnil)));
28933 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28934 doc: /* Maximum number of lines to keep in the message log buffer.
28935 If nil, disable message logging. If t, log messages but don't truncate
28936 the buffer when it becomes large. */);
28937 Vmessage_log_max = make_number (100);
28939 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28940 doc: /* Functions called before redisplay, if window sizes have changed.
28941 The value should be a list of functions that take one argument.
28942 Just before redisplay, for each frame, if any of its windows have changed
28943 size since the last redisplay, or have been split or deleted,
28944 all the functions in the list are called, with the frame as argument. */);
28945 Vwindow_size_change_functions = Qnil;
28947 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28948 doc: /* List of functions to call before redisplaying a window with scrolling.
28949 Each function is called with two arguments, the window and its new
28950 display-start position. Note that these functions are also called by
28951 `set-window-buffer'. Also note that the value of `window-end' is not
28952 valid when these functions are called.
28954 Warning: Do not use this feature to alter the way the window
28955 is scrolled. It is not designed for that, and such use probably won't
28956 work. */);
28957 Vwindow_scroll_functions = Qnil;
28959 DEFVAR_LISP ("window-text-change-functions",
28960 Vwindow_text_change_functions,
28961 doc: /* Functions to call in redisplay when text in the window might change. */);
28962 Vwindow_text_change_functions = Qnil;
28964 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28965 doc: /* Functions called when redisplay of a window reaches the end trigger.
28966 Each function is called with two arguments, the window and the end trigger value.
28967 See `set-window-redisplay-end-trigger'. */);
28968 Vredisplay_end_trigger_functions = Qnil;
28970 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28971 doc: /* Non-nil means autoselect window with mouse pointer.
28972 If nil, do not autoselect windows.
28973 A positive number means delay autoselection by that many seconds: a
28974 window is autoselected only after the mouse has remained in that
28975 window for the duration of the delay.
28976 A negative number has a similar effect, but causes windows to be
28977 autoselected only after the mouse has stopped moving. \(Because of
28978 the way Emacs compares mouse events, you will occasionally wait twice
28979 that time before the window gets selected.\)
28980 Any other value means to autoselect window instantaneously when the
28981 mouse pointer enters it.
28983 Autoselection selects the minibuffer only if it is active, and never
28984 unselects the minibuffer if it is active.
28986 When customizing this variable make sure that the actual value of
28987 `focus-follows-mouse' matches the behavior of your window manager. */);
28988 Vmouse_autoselect_window = Qnil;
28990 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28991 doc: /* Non-nil means automatically resize tool-bars.
28992 This dynamically changes the tool-bar's height to the minimum height
28993 that is needed to make all tool-bar items visible.
28994 If value is `grow-only', the tool-bar's height is only increased
28995 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28996 Vauto_resize_tool_bars = Qt;
28998 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28999 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29000 auto_raise_tool_bar_buttons_p = 1;
29002 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29003 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29004 make_cursor_line_fully_visible_p = 1;
29006 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29007 doc: /* Border below tool-bar in pixels.
29008 If an integer, use it as the height of the border.
29009 If it is one of `internal-border-width' or `border-width', use the
29010 value of the corresponding frame parameter.
29011 Otherwise, no border is added below the tool-bar. */);
29012 Vtool_bar_border = Qinternal_border_width;
29014 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29015 doc: /* Margin around tool-bar buttons in pixels.
29016 If an integer, use that for both horizontal and vertical margins.
29017 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29018 HORZ specifying the horizontal margin, and VERT specifying the
29019 vertical margin. */);
29020 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29022 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29023 doc: /* Relief thickness of tool-bar buttons. */);
29024 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29026 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29027 doc: /* Tool bar style to use.
29028 It can be one of
29029 image - show images only
29030 text - show text only
29031 both - show both, text below image
29032 both-horiz - show text to the right of the image
29033 text-image-horiz - show text to the left of the image
29034 any other - use system default or image if no system default.
29036 This variable only affects the GTK+ toolkit version of Emacs. */);
29037 Vtool_bar_style = Qnil;
29039 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29040 doc: /* Maximum number of characters a label can have to be shown.
29041 The tool bar style must also show labels for this to have any effect, see
29042 `tool-bar-style'. */);
29043 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29045 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29046 doc: /* List of functions to call to fontify regions of text.
29047 Each function is called with one argument POS. Functions must
29048 fontify a region starting at POS in the current buffer, and give
29049 fontified regions the property `fontified'. */);
29050 Vfontification_functions = Qnil;
29051 Fmake_variable_buffer_local (Qfontification_functions);
29053 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29054 unibyte_display_via_language_environment,
29055 doc: /* Non-nil means display unibyte text according to language environment.
29056 Specifically, this means that raw bytes in the range 160-255 decimal
29057 are displayed by converting them to the equivalent multibyte characters
29058 according to the current language environment. As a result, they are
29059 displayed according to the current fontset.
29061 Note that this variable affects only how these bytes are displayed,
29062 but does not change the fact they are interpreted as raw bytes. */);
29063 unibyte_display_via_language_environment = 0;
29065 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29066 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29067 If a float, it specifies a fraction of the mini-window frame's height.
29068 If an integer, it specifies a number of lines. */);
29069 Vmax_mini_window_height = make_float (0.25);
29071 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29072 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29073 A value of nil means don't automatically resize mini-windows.
29074 A value of t means resize them to fit the text displayed in them.
29075 A value of `grow-only', the default, means let mini-windows grow only;
29076 they return to their normal size when the minibuffer is closed, or the
29077 echo area becomes empty. */);
29078 Vresize_mini_windows = Qgrow_only;
29080 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29081 doc: /* Alist specifying how to blink the cursor off.
29082 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29083 `cursor-type' frame-parameter or variable equals ON-STATE,
29084 comparing using `equal', Emacs uses OFF-STATE to specify
29085 how to blink it off. ON-STATE and OFF-STATE are values for
29086 the `cursor-type' frame parameter.
29088 If a frame's ON-STATE has no entry in this list,
29089 the frame's other specifications determine how to blink the cursor off. */);
29090 Vblink_cursor_alist = Qnil;
29092 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29093 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29094 If non-nil, windows are automatically scrolled horizontally to make
29095 point visible. */);
29096 automatic_hscrolling_p = 1;
29097 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29099 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29100 doc: /* How many columns away from the window edge point is allowed to get
29101 before automatic hscrolling will horizontally scroll the window. */);
29102 hscroll_margin = 5;
29104 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29105 doc: /* How many columns to scroll the window when point gets too close to the edge.
29106 When point is less than `hscroll-margin' columns from the window
29107 edge, automatic hscrolling will scroll the window by the amount of columns
29108 determined by this variable. If its value is a positive integer, scroll that
29109 many columns. If it's a positive floating-point number, it specifies the
29110 fraction of the window's width to scroll. If it's nil or zero, point will be
29111 centered horizontally after the scroll. Any other value, including negative
29112 numbers, are treated as if the value were zero.
29114 Automatic hscrolling always moves point outside the scroll margin, so if
29115 point was more than scroll step columns inside the margin, the window will
29116 scroll more than the value given by the scroll step.
29118 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29119 and `scroll-right' overrides this variable's effect. */);
29120 Vhscroll_step = make_number (0);
29122 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29123 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29124 Bind this around calls to `message' to let it take effect. */);
29125 message_truncate_lines = 0;
29127 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29128 doc: /* Normal hook run to update the menu bar definitions.
29129 Redisplay runs this hook before it redisplays the menu bar.
29130 This is used to update submenus such as Buffers,
29131 whose contents depend on various data. */);
29132 Vmenu_bar_update_hook = Qnil;
29134 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29135 doc: /* Frame for which we are updating a menu.
29136 The enable predicate for a menu binding should check this variable. */);
29137 Vmenu_updating_frame = Qnil;
29139 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29140 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29141 inhibit_menubar_update = 0;
29143 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29144 doc: /* Prefix prepended to all continuation lines at display time.
29145 The value may be a string, an image, or a stretch-glyph; it is
29146 interpreted in the same way as the value of a `display' text property.
29148 This variable is overridden by any `wrap-prefix' text or overlay
29149 property.
29151 To add a prefix to non-continuation lines, use `line-prefix'. */);
29152 Vwrap_prefix = Qnil;
29153 DEFSYM (Qwrap_prefix, "wrap-prefix");
29154 Fmake_variable_buffer_local (Qwrap_prefix);
29156 DEFVAR_LISP ("line-prefix", Vline_prefix,
29157 doc: /* Prefix prepended to all non-continuation lines at display time.
29158 The value may be a string, an image, or a stretch-glyph; it is
29159 interpreted in the same way as the value of a `display' text property.
29161 This variable is overridden by any `line-prefix' text or overlay
29162 property.
29164 To add a prefix to continuation lines, use `wrap-prefix'. */);
29165 Vline_prefix = Qnil;
29166 DEFSYM (Qline_prefix, "line-prefix");
29167 Fmake_variable_buffer_local (Qline_prefix);
29169 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29170 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29171 inhibit_eval_during_redisplay = 0;
29173 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29174 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29175 inhibit_free_realized_faces = 0;
29177 #ifdef GLYPH_DEBUG
29178 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29179 doc: /* Inhibit try_window_id display optimization. */);
29180 inhibit_try_window_id = 0;
29182 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29183 doc: /* Inhibit try_window_reusing display optimization. */);
29184 inhibit_try_window_reusing = 0;
29186 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29187 doc: /* Inhibit try_cursor_movement display optimization. */);
29188 inhibit_try_cursor_movement = 0;
29189 #endif /* GLYPH_DEBUG */
29191 DEFVAR_INT ("overline-margin", overline_margin,
29192 doc: /* Space between overline and text, in pixels.
29193 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29194 margin to the character height. */);
29195 overline_margin = 2;
29197 DEFVAR_INT ("underline-minimum-offset",
29198 underline_minimum_offset,
29199 doc: /* Minimum distance between baseline and underline.
29200 This can improve legibility of underlined text at small font sizes,
29201 particularly when using variable `x-use-underline-position-properties'
29202 with fonts that specify an UNDERLINE_POSITION relatively close to the
29203 baseline. The default value is 1. */);
29204 underline_minimum_offset = 1;
29206 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29207 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29208 This feature only works when on a window system that can change
29209 cursor shapes. */);
29210 display_hourglass_p = 1;
29212 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29213 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29214 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29216 hourglass_atimer = NULL;
29217 hourglass_shown_p = 0;
29219 DEFSYM (Qglyphless_char, "glyphless-char");
29220 DEFSYM (Qhex_code, "hex-code");
29221 DEFSYM (Qempty_box, "empty-box");
29222 DEFSYM (Qthin_space, "thin-space");
29223 DEFSYM (Qzero_width, "zero-width");
29225 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29226 /* Intern this now in case it isn't already done.
29227 Setting this variable twice is harmless.
29228 But don't staticpro it here--that is done in alloc.c. */
29229 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29230 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29232 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29233 doc: /* Char-table defining glyphless characters.
29234 Each element, if non-nil, should be one of the following:
29235 an ASCII acronym string: display this string in a box
29236 `hex-code': display the hexadecimal code of a character in a box
29237 `empty-box': display as an empty box
29238 `thin-space': display as 1-pixel width space
29239 `zero-width': don't display
29240 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29241 display method for graphical terminals and text terminals respectively.
29242 GRAPHICAL and TEXT should each have one of the values listed above.
29244 The char-table has one extra slot to control the display of a character for
29245 which no font is found. This slot only takes effect on graphical terminals.
29246 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29247 `thin-space'. The default is `empty-box'. */);
29248 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29249 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29250 Qempty_box);
29254 /* Initialize this module when Emacs starts. */
29256 void
29257 init_xdisp (void)
29259 current_header_line_height = current_mode_line_height = -1;
29261 CHARPOS (this_line_start_pos) = 0;
29263 if (!noninteractive)
29265 struct window *m = XWINDOW (minibuf_window);
29266 Lisp_Object frame = m->frame;
29267 struct frame *f = XFRAME (frame);
29268 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29269 struct window *r = XWINDOW (root);
29270 int i;
29272 echo_area_window = minibuf_window;
29274 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
29275 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
29276 XSETFASTINT (r->total_cols, FRAME_COLS (f));
29277 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
29278 XSETFASTINT (m->total_lines, 1);
29279 XSETFASTINT (m->total_cols, FRAME_COLS (f));
29281 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29282 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29283 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29285 /* The default ellipsis glyphs `...'. */
29286 for (i = 0; i < 3; ++i)
29287 default_invis_vector[i] = make_number ('.');
29291 /* Allocate the buffer for frame titles.
29292 Also used for `format-mode-line'. */
29293 int size = 100;
29294 mode_line_noprop_buf = xmalloc (size);
29295 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29296 mode_line_noprop_ptr = mode_line_noprop_buf;
29297 mode_line_target = MODE_LINE_DISPLAY;
29300 help_echo_showing_p = 0;
29303 /* Since w32 does not support atimers, it defines its own implementation of
29304 the following three functions in w32fns.c. */
29305 #ifndef WINDOWSNT
29307 /* Platform-independent portion of hourglass implementation. */
29309 /* Cancel a currently active hourglass timer, and start a new one. */
29310 void
29311 start_hourglass (void)
29313 #if defined (HAVE_WINDOW_SYSTEM)
29314 EMACS_TIME delay;
29316 cancel_hourglass ();
29318 if (INTEGERP (Vhourglass_delay)
29319 && XINT (Vhourglass_delay) > 0)
29320 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29321 TYPE_MAXIMUM (time_t)),
29323 else if (FLOATP (Vhourglass_delay)
29324 && XFLOAT_DATA (Vhourglass_delay) > 0)
29325 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29326 else
29327 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29329 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29330 show_hourglass, NULL);
29331 #endif
29335 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29336 shown. */
29337 void
29338 cancel_hourglass (void)
29340 #if defined (HAVE_WINDOW_SYSTEM)
29341 if (hourglass_atimer)
29343 cancel_atimer (hourglass_atimer);
29344 hourglass_atimer = NULL;
29347 if (hourglass_shown_p)
29348 hide_hourglass ();
29349 #endif
29351 #endif /* ! WINDOWSNT */